blob: 4f34b75cd33426d368d8d543e27c822f5224f9dc [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 Shmidtcce06662013-11-04 18:44:24 -0800490 wpa_s->show_group_started = 0;
491 os_free(wpa_s->go_params);
492 wpa_s->go_params = NULL;
493
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
495 if (ssid && (ssid->p2p_group ||
496 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
497 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
498 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700499 if (ssid == wpa_s->current_ssid) {
500 wpa_sm_set_config(wpa_s->wpa, NULL);
501 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700503 }
504 /*
505 * Networks objects created during any P2P activities are not
506 * exposed out as they might/will confuse certain non-P2P aware
507 * applications since these network objects won't behave like
508 * regular ones.
509 *
510 * Likewise, we don't send out network removed signals for such
511 * network objects.
512 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 wpa_config_remove_network(wpa_s->conf, id);
514 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800515 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700516 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700517 } else {
518 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
519 "found");
520 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700521 if (wpa_s->ap_iface)
522 wpa_supplicant_ap_deinit(wpa_s);
523 else
524 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700525
526 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527}
528
529
530static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
531 u8 *go_dev_addr,
532 const u8 *ssid, size_t ssid_len)
533{
534 struct wpa_bss *bss;
535 const u8 *bssid;
536 struct wpabuf *p2p;
537 u8 group_capab;
538 const u8 *addr;
539
540 if (wpa_s->go_params)
541 bssid = wpa_s->go_params->peer_interface_addr;
542 else
543 bssid = wpa_s->bssid;
544
545 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
546 if (bss == NULL) {
547 u8 iface_addr[ETH_ALEN];
548 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
549 iface_addr) == 0)
550 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
551 }
552 if (bss == NULL) {
553 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
554 "group is persistent - BSS " MACSTR " not found",
555 MAC2STR(bssid));
556 return 0;
557 }
558
559 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700560 if (p2p == NULL)
561 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
562 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 if (p2p == NULL) {
564 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
565 "group is persistent - BSS " MACSTR
566 " did not include P2P IE", MAC2STR(bssid));
567 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
568 (u8 *) (bss + 1), bss->ie_len);
569 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
570 ((u8 *) bss + 1) + bss->ie_len,
571 bss->beacon_ie_len);
572 return 0;
573 }
574
575 group_capab = p2p_get_group_capab(p2p);
576 addr = p2p_get_go_dev_addr(p2p);
577 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
578 "group_capab=0x%x", group_capab);
579 if (addr) {
580 os_memcpy(go_dev_addr, addr, ETH_ALEN);
581 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
582 MAC2STR(addr));
583 } else
584 os_memset(go_dev_addr, 0, ETH_ALEN);
585 wpabuf_free(p2p);
586
587 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
588 "go_dev_addr=" MACSTR,
589 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
590
591 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
592}
593
594
Jouni Malinen75ecf522011-06-27 15:19:46 -0700595static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
596 struct wpa_ssid *ssid,
597 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598{
599 struct wpa_ssid *s;
600 int changed = 0;
601
602 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
603 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
604 for (s = wpa_s->conf->ssid; s; s = s->next) {
605 if (s->disabled == 2 &&
606 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
607 s->ssid_len == ssid->ssid_len &&
608 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
609 break;
610 }
611
612 if (s) {
613 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
614 "entry");
615 if (ssid->passphrase && !s->passphrase)
616 changed = 1;
617 else if (ssid->passphrase && s->passphrase &&
618 os_strcmp(ssid->passphrase, s->passphrase) != 0)
619 changed = 1;
620 } else {
621 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
622 "entry");
623 changed = 1;
624 s = wpa_config_add_network(wpa_s->conf);
625 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700626 return -1;
627
628 /*
629 * Instead of network_added we emit persistent_group_added
630 * notification. Also to keep the defense checks in
631 * persistent_group obj registration method, we set the
632 * relevant flags in s to designate it as a persistent group.
633 */
634 s->p2p_group = 1;
635 s->p2p_persistent_group = 1;
636 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637 wpa_config_set_network_defaults(s);
638 }
639
640 s->p2p_group = 1;
641 s->p2p_persistent_group = 1;
642 s->disabled = 2;
643 s->bssid_set = 1;
644 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
645 s->mode = ssid->mode;
646 s->auth_alg = WPA_AUTH_ALG_OPEN;
647 s->key_mgmt = WPA_KEY_MGMT_PSK;
648 s->proto = WPA_PROTO_RSN;
649 s->pairwise_cipher = WPA_CIPHER_CCMP;
650 s->export_keys = 1;
651 if (ssid->passphrase) {
652 os_free(s->passphrase);
653 s->passphrase = os_strdup(ssid->passphrase);
654 }
655 if (ssid->psk_set) {
656 s->psk_set = 1;
657 os_memcpy(s->psk, ssid->psk, 32);
658 }
659 if (s->passphrase && !s->psk_set)
660 wpa_config_update_psk(s);
661 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
662 os_free(s->ssid);
663 s->ssid = os_malloc(ssid->ssid_len);
664 }
665 if (s->ssid) {
666 s->ssid_len = ssid->ssid_len;
667 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
668 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700669 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
670 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
671 wpa_s->global->add_psk = NULL;
672 changed = 1;
673 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674
675#ifndef CONFIG_NO_CONFIG_WRITE
676 if (changed && wpa_s->conf->update_config &&
677 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
678 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
679 }
680#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700681
682 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683}
684
685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800686static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
687 const u8 *addr)
688{
689 struct wpa_ssid *ssid, *s;
690 u8 *n;
691 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700692 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800693
694 ssid = wpa_s->current_ssid;
695 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
696 !ssid->p2p_persistent_group)
697 return;
698
699 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
700 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
701 continue;
702
703 if (s->ssid_len == ssid->ssid_len &&
704 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
705 break;
706 }
707
708 if (s == NULL)
709 return;
710
711 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
712 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700713 ETH_ALEN) != 0)
714 continue;
715
716 if (i == s->num_p2p_clients - 1)
717 return; /* already the most recent entry */
718
719 /* move the entry to mark it most recent */
720 os_memmove(s->p2p_client_list + i * ETH_ALEN,
721 s->p2p_client_list + (i + 1) * ETH_ALEN,
722 (s->num_p2p_clients - i - 1) * ETH_ALEN);
723 os_memcpy(s->p2p_client_list +
724 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
725 found = 1;
726 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800727 }
728
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700729 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
730 n = os_realloc_array(s->p2p_client_list,
731 s->num_p2p_clients + 1, ETH_ALEN);
732 if (n == NULL)
733 return;
734 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
735 s->p2p_client_list = n;
736 s->num_p2p_clients++;
737 } else if (!found) {
738 /* Not enough room for an additional entry - drop the oldest
739 * entry */
740 os_memmove(s->p2p_client_list,
741 s->p2p_client_list + ETH_ALEN,
742 (s->num_p2p_clients - 1) * ETH_ALEN);
743 os_memcpy(s->p2p_client_list +
744 (s->num_p2p_clients - 1) * ETH_ALEN,
745 addr, ETH_ALEN);
746 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800747
748#ifndef CONFIG_NO_CONFIG_WRITE
749 if (wpa_s->parent->conf->update_config &&
750 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
751 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
752#endif /* CONFIG_NO_CONFIG_WRITE */
753}
754
755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
757 int success)
758{
759 struct wpa_ssid *ssid;
760 const char *ssid_txt;
761 int client;
762 int persistent;
763 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700764 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765
766 /*
767 * This callback is likely called for the main interface. Update wpa_s
768 * to use the group interface if a new interface was created for the
769 * group.
770 */
771 if (wpa_s->global->p2p_group_formation)
772 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700773 if (wpa_s->p2p_go_group_formation_completed) {
774 wpa_s->global->p2p_group_formation = NULL;
775 wpa_s->p2p_in_provisioning = 0;
776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777
778 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700779 wpa_msg_global(wpa_s->parent, MSG_INFO,
780 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700781 wpas_p2p_group_delete(wpa_s,
782 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783 return;
784 }
785
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700786 wpa_msg_global(wpa_s->parent, MSG_INFO,
787 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788
789 ssid = wpa_s->current_ssid;
790 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
791 ssid->mode = WPAS_MODE_P2P_GO;
792 p2p_group_notif_formation_done(wpa_s->p2p_group);
793 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
794 }
795
796 persistent = 0;
797 if (ssid) {
798 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
799 client = ssid->mode == WPAS_MODE_INFRA;
800 if (ssid->mode == WPAS_MODE_P2P_GO) {
801 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700802 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
803 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804 } else
805 persistent = wpas_p2p_persistent_group(wpa_s,
806 go_dev_addr,
807 ssid->ssid,
808 ssid->ssid_len);
809 } else {
810 ssid_txt = "";
811 client = wpa_s->p2p_group_interface ==
812 P2P_GROUP_INTERFACE_CLIENT;
813 os_memset(go_dev_addr, 0, ETH_ALEN);
814 }
815
816 wpa_s->show_group_started = 0;
817 if (client) {
818 /*
819 * Indicate event only after successfully completed 4-way
820 * handshake, i.e., when the interface is ready for data
821 * packets.
822 */
823 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700824#ifdef ANDROID_P2P
825 /* For client Second phase of Group formation (4-way handshake) can be still pending
826 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700827 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 -0700828 wpa_s->global->p2p_group_formation = wpa_s;
829#endif
830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
832 char psk[65];
833 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700834 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
835 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
836 MACSTR "%s",
837 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
838 MAC2STR(go_dev_addr),
839 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 wpas_p2p_cross_connect_setup(wpa_s);
841 wpas_p2p_set_group_idle_timeout(wpa_s);
842 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700843 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
844 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
845 "go_dev_addr=" MACSTR "%s",
846 wpa_s->ifname, ssid_txt,
847 ssid ? ssid->frequency : 0,
848 ssid && ssid->passphrase ? ssid->passphrase : "",
849 MAC2STR(go_dev_addr),
850 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 wpas_p2p_cross_connect_setup(wpa_s);
852 wpas_p2p_set_group_idle_timeout(wpa_s);
853 }
854
855 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700856 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
857 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700858 else {
859 os_free(wpa_s->global->add_psk);
860 wpa_s->global->add_psk = NULL;
861 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700863 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700864 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700865 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700866 os_get_time(&wpa_s->global->p2p_go_wait_client);
867 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868}
869
870
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800871static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
872 unsigned int freq,
873 const u8 *dst, const u8 *src,
874 const u8 *bssid,
875 const u8 *data, size_t data_len,
876 enum offchannel_send_action_result
877 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800879 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
882 return;
883 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
884 return;
885
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800886 switch (result) {
887 case OFFCHANNEL_SEND_ACTION_SUCCESS:
888 res = P2P_SEND_ACTION_SUCCESS;
889 break;
890 case OFFCHANNEL_SEND_ACTION_NO_ACK:
891 res = P2P_SEND_ACTION_NO_ACK;
892 break;
893 case OFFCHANNEL_SEND_ACTION_FAILED:
894 res = P2P_SEND_ACTION_FAILED;
895 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 }
897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800898 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800900 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
901 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800902 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800903 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
904 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800906 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
907 "during p2p_connect-auto");
908 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
909 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 }
911}
912
913
914static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
915 const u8 *src, const u8 *bssid, const u8 *buf,
916 size_t len, unsigned int wait_time)
917{
918 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800919 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
920 wait_time,
921 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922}
923
924
925static void wpas_send_action_done(void *ctx)
926{
927 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800928 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929}
930
931
932static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
933 struct p2p_go_neg_results *params)
934{
935 if (wpa_s->go_params == NULL) {
936 wpa_s->go_params = os_malloc(sizeof(*params));
937 if (wpa_s->go_params == NULL)
938 return -1;
939 }
940 os_memcpy(wpa_s->go_params, params, sizeof(*params));
941 return 0;
942}
943
944
945static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
946 struct p2p_go_neg_results *res)
947{
948 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
949 MAC2STR(res->peer_interface_addr));
950 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
951 res->ssid, res->ssid_len);
952 wpa_supplicant_ap_deinit(wpa_s);
953 wpas_copy_go_neg_results(wpa_s, res);
954 if (res->wps_method == WPS_PBC)
955 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
956 else {
957 u16 dev_pw_id = DEV_PW_DEFAULT;
958 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
959 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
960 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
961 wpa_s->p2p_pin, 1, dev_pw_id);
962 }
963}
964
965
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700966static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
967 struct wpa_ssid *ssid)
968{
969 struct wpa_ssid *persistent;
970 struct psk_list_entry *psk;
971 struct hostapd_data *hapd;
972
973 if (!wpa_s->ap_iface)
974 return;
975
976 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
977 ssid->ssid_len);
978 if (persistent == NULL)
979 return;
980
981 hapd = wpa_s->ap_iface->bss[0];
982
983 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
984 list) {
985 struct hostapd_wpa_psk *hpsk;
986
987 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
988 MACSTR " psk=%d",
989 MAC2STR(psk->addr), psk->p2p);
990 hpsk = os_zalloc(sizeof(*hpsk));
991 if (hpsk == NULL)
992 break;
993 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
994 if (psk->p2p)
995 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
996 else
997 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
998 hpsk->next = hapd->conf->ssid.wpa_psk;
999 hapd->conf->ssid.wpa_psk = hpsk;
1000 }
1001}
1002
1003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004static void p2p_go_configured(void *ctx, void *data)
1005{
1006 struct wpa_supplicant *wpa_s = ctx;
1007 struct p2p_go_neg_results *params = data;
1008 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001009 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010
1011 ssid = wpa_s->current_ssid;
1012 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1013 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1014 if (wpa_s->global->p2p_group_formation == wpa_s)
1015 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001016 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001017 wpa_msg_global(wpa_s->parent, MSG_INFO,
1018 P2P_EVENT_GROUP_STARTED
1019 "%s GO ssid=\"%s\" freq=%d "
1020 "passphrase=\"%s\" go_dev_addr=" MACSTR
1021 "%s", wpa_s->ifname,
1022 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1023 ssid->frequency, params->passphrase,
1024 MAC2STR(wpa_s->global->p2p_dev_addr),
1025 params->persistent_group ?
1026 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001027 } else {
1028 char psk[65];
1029 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1030 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001031 wpa_msg_global(wpa_s->parent, MSG_INFO,
1032 P2P_EVENT_GROUP_STARTED
1033 "%s GO ssid=\"%s\" freq=%d psk=%s "
1034 "go_dev_addr=" MACSTR "%s",
1035 wpa_s->ifname,
1036 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1037 ssid->frequency, psk,
1038 MAC2STR(wpa_s->global->p2p_dev_addr),
1039 params->persistent_group ?
1040 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001041 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001042
Dmitry Shmidt56052862013-10-04 10:23:25 -07001043 os_get_time(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001044 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001045 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001047 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001048 wpas_p2p_add_psk_list(wpa_s, ssid);
1049 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001050 if (network_id < 0)
1051 network_id = ssid->id;
1052 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 wpas_p2p_cross_connect_setup(wpa_s);
1054 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001055
1056 if (wpa_s->p2p_first_connection_timeout) {
1057 wpa_dbg(wpa_s, MSG_DEBUG,
1058 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1059 wpa_s->p2p_first_connection_timeout);
1060 wpa_s->p2p_go_group_formation_completed = 0;
1061 wpa_s->global->p2p_group_formation = wpa_s;
1062 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1063 wpa_s->parent, NULL);
1064 eloop_register_timeout(
1065 wpa_s->p2p_first_connection_timeout, 0,
1066 wpas_p2p_group_formation_timeout,
1067 wpa_s->parent, NULL);
1068 }
1069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 return;
1071 }
1072
1073 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1074 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1075 params->peer_interface_addr)) {
1076 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1077 "filtering");
1078 return;
1079 }
1080 if (params->wps_method == WPS_PBC)
1081 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001082 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 else if (wpa_s->p2p_pin[0])
1084 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001085 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 os_free(wpa_s->go_params);
1087 wpa_s->go_params = NULL;
1088}
1089
1090
1091static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1092 struct p2p_go_neg_results *params,
1093 int group_formation)
1094{
1095 struct wpa_ssid *ssid;
1096
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001097 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1098 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1099 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1100 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001102 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103
1104 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001105 if (ssid == NULL) {
1106 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001110 wpa_s->show_group_started = 0;
1111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 wpa_config_set_network_defaults(ssid);
1113 ssid->temporary = 1;
1114 ssid->p2p_group = 1;
1115 ssid->p2p_persistent_group = params->persistent_group;
1116 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1117 WPAS_MODE_P2P_GO;
1118 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001119 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001120 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121 ssid->ssid = os_zalloc(params->ssid_len + 1);
1122 if (ssid->ssid) {
1123 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1124 ssid->ssid_len = params->ssid_len;
1125 }
1126 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1127 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1128 ssid->proto = WPA_PROTO_RSN;
1129 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001130 if (os_strlen(params->passphrase) > 0) {
1131 ssid->passphrase = os_strdup(params->passphrase);
1132 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001133 wpa_msg_global(wpa_s, MSG_ERROR,
1134 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001135 wpa_config_remove_network(wpa_s->conf, ssid->id);
1136 return;
1137 }
1138 } else
1139 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001140 ssid->psk_set = params->psk_set;
1141 if (ssid->psk_set)
1142 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001143 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001144 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001145 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146
1147 wpa_s->ap_configured_cb = p2p_go_configured;
1148 wpa_s->ap_configured_cb_ctx = wpa_s;
1149 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001150 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 wpa_s->reassociate = 1;
1152 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001153 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1154 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 wpa_supplicant_req_scan(wpa_s, 0, 0);
1156}
1157
1158
1159static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1160 const struct wpa_supplicant *src)
1161{
1162 struct wpa_config *d;
1163 const struct wpa_config *s;
1164
1165 d = dst->conf;
1166 s = src->conf;
1167
1168#define C(n) if (s->n) d->n = os_strdup(s->n)
1169 C(device_name);
1170 C(manufacturer);
1171 C(model_name);
1172 C(model_number);
1173 C(serial_number);
1174 C(config_methods);
1175#undef C
1176
1177 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1178 os_memcpy(d->sec_device_type, s->sec_device_type,
1179 sizeof(d->sec_device_type));
1180 d->num_sec_device_types = s->num_sec_device_types;
1181
1182 d->p2p_group_idle = s->p2p_group_idle;
1183 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001184 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001185 d->max_num_sta = s->max_num_sta;
1186 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001187 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001188 d->beacon_int = s->beacon_int;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001189 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001190 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191}
1192
1193
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001194static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1195 char *ifname, size_t len)
1196{
1197 char *ifname_ptr = wpa_s->ifname;
1198
1199 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1200 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1201 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1202 }
1203
1204 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1205 if (os_strlen(ifname) >= IFNAMSIZ &&
1206 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1207 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001208 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001209 }
1210}
1211
1212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1214 enum wpa_driver_if_type type)
1215{
1216 char ifname[120], force_ifname[120];
1217
1218 if (wpa_s->pending_interface_name[0]) {
1219 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1220 "- skip creation of a new one");
1221 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1222 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1223 "unknown?! ifname='%s'",
1224 wpa_s->pending_interface_name);
1225 return -1;
1226 }
1227 return 0;
1228 }
1229
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001230 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 force_ifname[0] = '\0';
1232
1233 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1234 ifname);
1235 wpa_s->p2p_group_idx++;
1236
1237 wpa_s->pending_interface_type = type;
1238 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1239 wpa_s->pending_interface_addr, NULL) < 0) {
1240 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1241 "interface");
1242 return -1;
1243 }
1244
1245 if (force_ifname[0]) {
1246 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1247 force_ifname);
1248 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1249 sizeof(wpa_s->pending_interface_name));
1250 } else
1251 os_strlcpy(wpa_s->pending_interface_name, ifname,
1252 sizeof(wpa_s->pending_interface_name));
1253 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1254 MACSTR, wpa_s->pending_interface_name,
1255 MAC2STR(wpa_s->pending_interface_addr));
1256
1257 return 0;
1258}
1259
1260
1261static void wpas_p2p_remove_pending_group_interface(
1262 struct wpa_supplicant *wpa_s)
1263{
1264 if (!wpa_s->pending_interface_name[0] ||
1265 is_zero_ether_addr(wpa_s->pending_interface_addr))
1266 return; /* No pending virtual interface */
1267
1268 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1269 wpa_s->pending_interface_name);
1270 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1271 wpa_s->pending_interface_name);
1272 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1273 wpa_s->pending_interface_name[0] = '\0';
1274}
1275
1276
1277static struct wpa_supplicant *
1278wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1279{
1280 struct wpa_interface iface;
1281 struct wpa_supplicant *group_wpa_s;
1282
1283 if (!wpa_s->pending_interface_name[0]) {
1284 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1285 if (!wpas_p2p_create_iface(wpa_s))
1286 return NULL;
1287 /*
1288 * Something has forced us to remove the pending interface; try
1289 * to create a new one and hope for the best that we will get
1290 * the same local address.
1291 */
1292 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1293 WPA_IF_P2P_CLIENT) < 0)
1294 return NULL;
1295 }
1296
1297 os_memset(&iface, 0, sizeof(iface));
1298 iface.ifname = wpa_s->pending_interface_name;
1299 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001300 if (wpa_s->conf->ctrl_interface == NULL &&
1301 wpa_s->parent != wpa_s &&
1302 wpa_s->p2p_mgmt &&
1303 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1304 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1305 else
1306 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 iface.driver_param = wpa_s->conf->driver_param;
1308 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1309 if (group_wpa_s == NULL) {
1310 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1311 "wpa_supplicant interface");
1312 return NULL;
1313 }
1314 wpa_s->pending_interface_name[0] = '\0';
1315 group_wpa_s->parent = wpa_s;
1316 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1317 P2P_GROUP_INTERFACE_CLIENT;
1318 wpa_s->global->p2p_group_formation = group_wpa_s;
1319
1320 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1321
1322 return group_wpa_s;
1323}
1324
1325
1326static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1327 void *timeout_ctx)
1328{
1329 struct wpa_supplicant *wpa_s = eloop_ctx;
1330 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001331 wpas_p2p_group_formation_failed(wpa_s);
1332}
1333
1334
1335void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1336{
1337 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1338 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 if (wpa_s->global->p2p)
1340 p2p_group_formation_failed(wpa_s->global->p2p);
1341 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1342 wpa_drv_p2p_group_formation_failed(wpa_s);
1343 wpas_group_formation_completed(wpa_s, 0);
1344}
1345
1346
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001347void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1348{
1349 if (wpa_s->global->p2p_group_formation != wpa_s)
1350 return;
1351 /* Speed up group formation timeout since this cannot succeed */
1352 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1353 wpa_s->parent, NULL);
1354 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1355 wpa_s->parent, NULL);
1356}
1357
1358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1360{
1361 struct wpa_supplicant *wpa_s = ctx;
1362
1363 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1364 wpa_drv_cancel_remain_on_channel(wpa_s);
1365 wpa_s->off_channel_freq = 0;
1366 wpa_s->roc_waiting_drv_freq = 0;
1367 }
1368
1369 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001370 wpa_msg_global(wpa_s, MSG_INFO,
1371 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1372 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001373 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 wpas_p2p_remove_pending_group_interface(wpa_s);
1375 return;
1376 }
1377
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001378 if (wpa_s->p2p_go_ht40)
1379 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001380 if (wpa_s->p2p_go_vht)
1381 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001382
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001383 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1384 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1385 " wps_method=%s",
1386 res->role_go ? "GO" : "client", res->freq, res->ht40,
1387 MAC2STR(res->peer_device_addr),
1388 MAC2STR(res->peer_interface_addr),
1389 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001390 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391
Dmitry Shmidt04949592012-07-19 12:16:46 -07001392 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1393 struct wpa_ssid *ssid;
1394 ssid = wpa_config_get_network(wpa_s->conf,
1395 wpa_s->p2p_persistent_id);
1396 if (ssid && ssid->disabled == 2 &&
1397 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1398 size_t len = os_strlen(ssid->passphrase);
1399 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1400 "on requested persistent group");
1401 os_memcpy(res->passphrase, ssid->passphrase, len);
1402 res->passphrase[len] = '\0';
1403 }
1404 }
1405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406 if (wpa_s->create_p2p_iface) {
1407 struct wpa_supplicant *group_wpa_s =
1408 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1409 if (group_wpa_s == NULL) {
1410 wpas_p2p_remove_pending_group_interface(wpa_s);
1411 return;
1412 }
1413 if (group_wpa_s != wpa_s) {
1414 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1415 sizeof(group_wpa_s->p2p_pin));
1416 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1417 }
1418 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1419 wpa_s->pending_interface_name[0] = '\0';
1420 group_wpa_s->p2p_in_provisioning = 1;
1421
1422 if (res->role_go)
1423 wpas_start_wps_go(group_wpa_s, res, 1);
1424 else
1425 wpas_start_wps_enrollee(group_wpa_s, res);
1426 } else {
1427 wpa_s->p2p_in_provisioning = 1;
1428 wpa_s->global->p2p_group_formation = wpa_s;
1429
1430 if (res->role_go)
1431 wpas_start_wps_go(wpa_s, res, 1);
1432 else
1433 wpas_start_wps_enrollee(ctx, res);
1434 }
1435
1436 wpa_s->p2p_long_listen = 0;
1437 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1438
1439 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1440 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1441 (res->peer_config_timeout % 100) * 10000,
1442 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1443}
1444
1445
1446void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1447{
1448 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001449 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1450 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451
1452 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1453}
1454
1455
1456void wpas_dev_found(void *ctx, const u8 *addr,
1457 const struct p2p_peer_info *info,
1458 int new_device)
1459{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001460#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 struct wpa_supplicant *wpa_s = ctx;
1462 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001463#define WFD_DEV_INFO_SIZE 9
1464 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001465 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001466#ifdef CONFIG_WIFI_DISPLAY
1467 if (info->wfd_subelems) {
1468 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1469 wpabuf_head(info->wfd_subelems),
1470 WFD_DEV_INFO_SIZE);
1471 }
1472#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001473 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1474 " p2p_dev_addr=" MACSTR
1475 " pri_dev_type=%s name='%s' config_methods=0x%x "
1476 "dev_capab=0x%x group_capab=0x%x%s%s",
1477 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1478 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1479 sizeof(devtype)),
1480 info->device_name, info->config_methods,
1481 info->dev_capab, info->group_capab,
1482 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001483#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484
1485 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1486}
1487
1488
1489static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1490{
1491 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001492
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001493 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1494 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001495
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1497}
1498
1499
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001500static void wpas_find_stopped(void *ctx)
1501{
1502 struct wpa_supplicant *wpa_s = ctx;
1503 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1504}
1505
1506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507static int wpas_start_listen(void *ctx, unsigned int freq,
1508 unsigned int duration,
1509 const struct wpabuf *probe_resp_ie)
1510{
1511 struct wpa_supplicant *wpa_s = ctx;
1512
1513 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1514
1515 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1516 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1517 "report received Probe Request frames");
1518 return -1;
1519 }
1520
1521 wpa_s->pending_listen_freq = freq;
1522 wpa_s->pending_listen_duration = duration;
1523
1524 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1525 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1526 "to remain on channel (%u MHz) for Listen "
1527 "state", freq);
1528 wpa_s->pending_listen_freq = 0;
1529 return -1;
1530 }
1531 wpa_s->off_channel_freq = 0;
1532 wpa_s->roc_waiting_drv_freq = freq;
1533
1534 return 0;
1535}
1536
1537
1538static void wpas_stop_listen(void *ctx)
1539{
1540 struct wpa_supplicant *wpa_s = ctx;
1541 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1542 wpa_drv_cancel_remain_on_channel(wpa_s);
1543 wpa_s->off_channel_freq = 0;
1544 wpa_s->roc_waiting_drv_freq = 0;
1545 }
1546 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1547 wpa_drv_probe_req_report(wpa_s, 0);
1548}
1549
1550
1551static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1552{
1553 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001554 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555}
1556
1557
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001558/*
1559 * DNS Header section is used only to calculate compression pointers, so the
1560 * contents of this data does not matter, but the length needs to be reserved
1561 * in the virtual packet.
1562 */
1563#define DNS_HEADER_LEN 12
1564
1565/*
1566 * 27-octet in-memory packet from P2P specification containing two implied
1567 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1568 */
1569#define P2P_SD_IN_MEMORY_LEN 27
1570
1571static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1572 u8 **spos, const u8 *end)
1573{
1574 while (*spos < end) {
1575 u8 val = ((*spos)[0] & 0xc0) >> 6;
1576 int len;
1577
1578 if (val == 1 || val == 2) {
1579 /* These are reserved values in RFC 1035 */
1580 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1581 "sequence starting with 0x%x", val);
1582 return -1;
1583 }
1584
1585 if (val == 3) {
1586 u16 offset;
1587 u8 *spos_tmp;
1588
1589 /* Offset */
1590 if (*spos + 2 > end) {
1591 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1592 "DNS offset field");
1593 return -1;
1594 }
1595
1596 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1597 if (offset >= *spos - start) {
1598 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1599 "pointer offset %u", offset);
1600 return -1;
1601 }
1602
1603 (*spos) += 2;
1604 spos_tmp = start + offset;
1605 return p2p_sd_dns_uncompress_label(upos, uend, start,
1606 &spos_tmp,
1607 *spos - 2);
1608 }
1609
1610 /* Label */
1611 len = (*spos)[0] & 0x3f;
1612 if (len == 0)
1613 return 0;
1614
1615 (*spos)++;
1616 if (*spos + len > end) {
1617 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1618 "sequence - no room for label with length "
1619 "%u", len);
1620 return -1;
1621 }
1622
1623 if (*upos + len + 2 > uend)
1624 return -2;
1625
1626 os_memcpy(*upos, *spos, len);
1627 *spos += len;
1628 *upos += len;
1629 (*upos)[0] = '.';
1630 (*upos)++;
1631 (*upos)[0] = '\0';
1632 }
1633
1634 return 0;
1635}
1636
1637
1638/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1639 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1640 * not large enough */
1641static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1642 size_t msg_len, size_t offset)
1643{
1644 /* 27-octet in-memory packet from P2P specification */
1645 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1646 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1647 u8 *tmp, *end, *spos;
1648 char *upos, *uend;
1649 int ret = 0;
1650
1651 if (buf_len < 2)
1652 return -1;
1653 if (offset > msg_len)
1654 return -1;
1655
1656 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1657 if (tmp == NULL)
1658 return -1;
1659 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1660 end = spos + msg_len;
1661 spos += offset;
1662
1663 os_memset(tmp, 0, DNS_HEADER_LEN);
1664 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1665 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1666
1667 upos = buf;
1668 uend = buf + buf_len;
1669
1670 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1671 if (ret) {
1672 os_free(tmp);
1673 return ret;
1674 }
1675
1676 if (upos == buf) {
1677 upos[0] = '.';
1678 upos[1] = '\0';
1679 } else if (upos[-1] == '.')
1680 upos[-1] = '\0';
1681
1682 os_free(tmp);
1683 return 0;
1684}
1685
1686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687static struct p2p_srv_bonjour *
1688wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1689 const struct wpabuf *query)
1690{
1691 struct p2p_srv_bonjour *bsrv;
1692 size_t len;
1693
1694 len = wpabuf_len(query);
1695 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1696 struct p2p_srv_bonjour, list) {
1697 if (len == wpabuf_len(bsrv->query) &&
1698 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1699 len) == 0)
1700 return bsrv;
1701 }
1702 return NULL;
1703}
1704
1705
1706static struct p2p_srv_upnp *
1707wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1708 const char *service)
1709{
1710 struct p2p_srv_upnp *usrv;
1711
1712 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1713 struct p2p_srv_upnp, list) {
1714 if (version == usrv->version &&
1715 os_strcmp(service, usrv->service) == 0)
1716 return usrv;
1717 }
1718 return NULL;
1719}
1720
1721
1722static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1723 u8 srv_trans_id)
1724{
1725 u8 *len_pos;
1726
1727 if (wpabuf_tailroom(resp) < 5)
1728 return;
1729
1730 /* Length (to be filled) */
1731 len_pos = wpabuf_put(resp, 2);
1732 wpabuf_put_u8(resp, srv_proto);
1733 wpabuf_put_u8(resp, srv_trans_id);
1734 /* Status Code */
1735 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1736 /* Response Data: empty */
1737 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1738}
1739
1740
1741static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1742 struct wpabuf *resp, u8 srv_trans_id)
1743{
1744 struct p2p_srv_bonjour *bsrv;
1745 u8 *len_pos;
1746
1747 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1748
1749 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1750 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1751 return;
1752 }
1753
1754 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1755 struct p2p_srv_bonjour, list) {
1756 if (wpabuf_tailroom(resp) <
1757 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1758 return;
1759 /* Length (to be filled) */
1760 len_pos = wpabuf_put(resp, 2);
1761 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1762 wpabuf_put_u8(resp, srv_trans_id);
1763 /* Status Code */
1764 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1765 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1766 wpabuf_head(bsrv->resp),
1767 wpabuf_len(bsrv->resp));
1768 /* Response Data */
1769 wpabuf_put_buf(resp, bsrv->query); /* Key */
1770 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1771 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1772 2);
1773 }
1774}
1775
1776
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001777static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1778 size_t query_len)
1779{
1780 char str_rx[256], str_srv[256];
1781
1782 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1783 return 0; /* Too short to include DNS Type and Version */
1784 if (os_memcmp(query + query_len - 3,
1785 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1786 3) != 0)
1787 return 0; /* Mismatch in DNS Type or Version */
1788 if (query_len == wpabuf_len(bsrv->query) &&
1789 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1790 return 1; /* Binary match */
1791
1792 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1793 0))
1794 return 0; /* Failed to uncompress query */
1795 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1796 wpabuf_head(bsrv->query),
1797 wpabuf_len(bsrv->query) - 3, 0))
1798 return 0; /* Failed to uncompress service */
1799
1800 return os_strcmp(str_rx, str_srv) == 0;
1801}
1802
1803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1805 struct wpabuf *resp, u8 srv_trans_id,
1806 const u8 *query, size_t query_len)
1807{
1808 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001810 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811
1812 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1813 query, query_len);
1814 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1815 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1816 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1817 srv_trans_id);
1818 return;
1819 }
1820
1821 if (query_len == 0) {
1822 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1823 return;
1824 }
1825
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001826 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1827 struct p2p_srv_bonjour, list) {
1828 if (!match_bonjour_query(bsrv, query, query_len))
1829 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001831 if (wpabuf_tailroom(resp) <
1832 5 + query_len + wpabuf_len(bsrv->resp))
1833 return;
1834
1835 matches++;
1836
1837 /* Length (to be filled) */
1838 len_pos = wpabuf_put(resp, 2);
1839 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1840 wpabuf_put_u8(resp, srv_trans_id);
1841
1842 /* Status Code */
1843 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1844 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1845 wpabuf_head(bsrv->resp),
1846 wpabuf_len(bsrv->resp));
1847
1848 /* Response Data */
1849 wpabuf_put_data(resp, query, query_len); /* Key */
1850 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1851
1852 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1853 }
1854
1855 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1857 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001858 if (wpabuf_tailroom(resp) < 5)
1859 return;
1860
1861 /* Length (to be filled) */
1862 len_pos = wpabuf_put(resp, 2);
1863 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1864 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865
1866 /* Status Code */
1867 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1868 /* Response Data: empty */
1869 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1870 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872}
1873
1874
1875static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1876 struct wpabuf *resp, u8 srv_trans_id)
1877{
1878 struct p2p_srv_upnp *usrv;
1879 u8 *len_pos;
1880
1881 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1882
1883 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1884 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1885 return;
1886 }
1887
1888 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1889 struct p2p_srv_upnp, list) {
1890 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1891 return;
1892
1893 /* Length (to be filled) */
1894 len_pos = wpabuf_put(resp, 2);
1895 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1896 wpabuf_put_u8(resp, srv_trans_id);
1897
1898 /* Status Code */
1899 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1900 /* Response Data */
1901 wpabuf_put_u8(resp, usrv->version);
1902 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1903 usrv->service);
1904 wpabuf_put_str(resp, usrv->service);
1905 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1906 2);
1907 }
1908}
1909
1910
1911static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1912 struct wpabuf *resp, u8 srv_trans_id,
1913 const u8 *query, size_t query_len)
1914{
1915 struct p2p_srv_upnp *usrv;
1916 u8 *len_pos;
1917 u8 version;
1918 char *str;
1919 int count = 0;
1920
1921 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1922 query, query_len);
1923
1924 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1925 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1926 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1927 srv_trans_id);
1928 return;
1929 }
1930
1931 if (query_len == 0) {
1932 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1933 return;
1934 }
1935
1936 if (wpabuf_tailroom(resp) < 5)
1937 return;
1938
1939 /* Length (to be filled) */
1940 len_pos = wpabuf_put(resp, 2);
1941 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1942 wpabuf_put_u8(resp, srv_trans_id);
1943
1944 version = query[0];
1945 str = os_malloc(query_len);
1946 if (str == NULL)
1947 return;
1948 os_memcpy(str, query + 1, query_len - 1);
1949 str[query_len - 1] = '\0';
1950
1951 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1952 struct p2p_srv_upnp, list) {
1953 if (version != usrv->version)
1954 continue;
1955
1956 if (os_strcmp(str, "ssdp:all") != 0 &&
1957 os_strstr(usrv->service, str) == NULL)
1958 continue;
1959
1960 if (wpabuf_tailroom(resp) < 2)
1961 break;
1962 if (count == 0) {
1963 /* Status Code */
1964 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1965 /* Response Data */
1966 wpabuf_put_u8(resp, version);
1967 } else
1968 wpabuf_put_u8(resp, ',');
1969
1970 count++;
1971
1972 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1973 usrv->service);
1974 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1975 break;
1976 wpabuf_put_str(resp, usrv->service);
1977 }
1978 os_free(str);
1979
1980 if (count == 0) {
1981 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1982 "available");
1983 /* Status Code */
1984 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1985 /* Response Data: empty */
1986 }
1987
1988 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1989}
1990
1991
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001992#ifdef CONFIG_WIFI_DISPLAY
1993static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1994 struct wpabuf *resp, u8 srv_trans_id,
1995 const u8 *query, size_t query_len)
1996{
1997 const u8 *pos;
1998 u8 role;
1999 u8 *len_pos;
2000
2001 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2002
2003 if (!wpa_s->global->wifi_display) {
2004 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2005 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2006 srv_trans_id);
2007 return;
2008 }
2009
2010 if (query_len < 1) {
2011 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2012 "Role");
2013 return;
2014 }
2015
2016 if (wpabuf_tailroom(resp) < 5)
2017 return;
2018
2019 pos = query;
2020 role = *pos++;
2021 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2022
2023 /* TODO: role specific handling */
2024
2025 /* Length (to be filled) */
2026 len_pos = wpabuf_put(resp, 2);
2027 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2028 wpabuf_put_u8(resp, srv_trans_id);
2029 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2030
2031 while (pos < query + query_len) {
2032 if (*pos < MAX_WFD_SUBELEMS &&
2033 wpa_s->global->wfd_subelem[*pos] &&
2034 wpabuf_tailroom(resp) >=
2035 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2036 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2037 "subelement %u", *pos);
2038 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2039 }
2040 pos++;
2041 }
2042
2043 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2044}
2045#endif /* CONFIG_WIFI_DISPLAY */
2046
2047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2049 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2050{
2051 struct wpa_supplicant *wpa_s = ctx;
2052 const u8 *pos = tlvs;
2053 const u8 *end = tlvs + tlvs_len;
2054 const u8 *tlv_end;
2055 u16 slen;
2056 struct wpabuf *resp;
2057 u8 srv_proto, srv_trans_id;
2058 size_t buf_len;
2059 char *buf;
2060
2061 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2062 tlvs, tlvs_len);
2063 buf_len = 2 * tlvs_len + 1;
2064 buf = os_malloc(buf_len);
2065 if (buf) {
2066 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2067 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2068 MACSTR " %u %u %s",
2069 freq, MAC2STR(sa), dialog_token, update_indic,
2070 buf);
2071 os_free(buf);
2072 }
2073
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002074 if (wpa_s->p2p_sd_over_ctrl_iface) {
2075 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2076 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002078 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079
2080 resp = wpabuf_alloc(10000);
2081 if (resp == NULL)
2082 return;
2083
2084 while (pos + 1 < end) {
2085 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2086 slen = WPA_GET_LE16(pos);
2087 pos += 2;
2088 if (pos + slen > end || slen < 2) {
2089 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2090 "length");
2091 wpabuf_free(resp);
2092 return;
2093 }
2094 tlv_end = pos + slen;
2095
2096 srv_proto = *pos++;
2097 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2098 srv_proto);
2099 srv_trans_id = *pos++;
2100 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2101 srv_trans_id);
2102
2103 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2104 pos, tlv_end - pos);
2105
2106
2107 if (wpa_s->force_long_sd) {
2108 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2109 "response");
2110 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2111 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2112 goto done;
2113 }
2114
2115 switch (srv_proto) {
2116 case P2P_SERV_ALL_SERVICES:
2117 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2118 "for all services");
2119 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2120 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2121 wpa_printf(MSG_DEBUG, "P2P: No service "
2122 "discovery protocols available");
2123 wpas_sd_add_proto_not_avail(
2124 resp, P2P_SERV_ALL_SERVICES,
2125 srv_trans_id);
2126 break;
2127 }
2128 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2129 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2130 break;
2131 case P2P_SERV_BONJOUR:
2132 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2133 pos, tlv_end - pos);
2134 break;
2135 case P2P_SERV_UPNP:
2136 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2137 pos, tlv_end - pos);
2138 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002139#ifdef CONFIG_WIFI_DISPLAY
2140 case P2P_SERV_WIFI_DISPLAY:
2141 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2142 pos, tlv_end - pos);
2143 break;
2144#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 default:
2146 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2147 "protocol %u", srv_proto);
2148 wpas_sd_add_proto_not_avail(resp, srv_proto,
2149 srv_trans_id);
2150 break;
2151 }
2152
2153 pos = tlv_end;
2154 }
2155
2156done:
2157 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2158 update_indic, tlvs, tlvs_len);
2159
2160 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2161
2162 wpabuf_free(resp);
2163}
2164
2165
2166void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2167 const u8 *tlvs, size_t tlvs_len)
2168{
2169 struct wpa_supplicant *wpa_s = ctx;
2170 const u8 *pos = tlvs;
2171 const u8 *end = tlvs + tlvs_len;
2172 const u8 *tlv_end;
2173 u16 slen;
2174 size_t buf_len;
2175 char *buf;
2176
2177 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2178 tlvs, tlvs_len);
2179 if (tlvs_len > 1500) {
2180 /* TODO: better way for handling this */
2181 wpa_msg_ctrl(wpa_s, MSG_INFO,
2182 P2P_EVENT_SERV_DISC_RESP MACSTR
2183 " %u <long response: %u bytes>",
2184 MAC2STR(sa), update_indic,
2185 (unsigned int) tlvs_len);
2186 } else {
2187 buf_len = 2 * tlvs_len + 1;
2188 buf = os_malloc(buf_len);
2189 if (buf) {
2190 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2191 wpa_msg_ctrl(wpa_s, MSG_INFO,
2192 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2193 MAC2STR(sa), update_indic, buf);
2194 os_free(buf);
2195 }
2196 }
2197
2198 while (pos < end) {
2199 u8 srv_proto, srv_trans_id, status;
2200
2201 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2202 slen = WPA_GET_LE16(pos);
2203 pos += 2;
2204 if (pos + slen > end || slen < 3) {
2205 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2206 "length");
2207 return;
2208 }
2209 tlv_end = pos + slen;
2210
2211 srv_proto = *pos++;
2212 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2213 srv_proto);
2214 srv_trans_id = *pos++;
2215 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2216 srv_trans_id);
2217 status = *pos++;
2218 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2219 status);
2220
2221 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2222 pos, tlv_end - pos);
2223
2224 pos = tlv_end;
2225 }
2226
2227 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2228}
2229
2230
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002231u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2232 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233{
2234 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002235 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002237 return 0;
2238 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239}
2240
2241
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002242u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2243 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002244{
2245 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002246 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247
2248 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2249 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002250 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2252 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2253 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2254 wpabuf_put_u8(tlvs, version);
2255 wpabuf_put_str(tlvs, query);
2256 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2257 wpabuf_free(tlvs);
2258 return ret;
2259}
2260
2261
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002262#ifdef CONFIG_WIFI_DISPLAY
2263
2264static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2265 const struct wpabuf *tlvs)
2266{
2267 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2268 return 0;
2269 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2270 return 0;
2271 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2272}
2273
2274
2275#define MAX_WFD_SD_SUBELEMS 20
2276
2277static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2278 const char *subelems)
2279{
2280 u8 *len;
2281 const char *pos;
2282 int val;
2283 int count = 0;
2284
2285 len = wpabuf_put(tlvs, 2);
2286 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2287 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2288
2289 wpabuf_put_u8(tlvs, role);
2290
2291 pos = subelems;
2292 while (*pos) {
2293 val = atoi(pos);
2294 if (val >= 0 && val < 256) {
2295 wpabuf_put_u8(tlvs, val);
2296 count++;
2297 if (count == MAX_WFD_SD_SUBELEMS)
2298 break;
2299 }
2300 pos = os_strchr(pos + 1, ',');
2301 if (pos == NULL)
2302 break;
2303 pos++;
2304 }
2305
2306 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2307}
2308
2309
2310u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2311 const u8 *dst, const char *role)
2312{
2313 struct wpabuf *tlvs;
2314 u64 ret;
2315 const char *subelems;
2316 u8 id = 1;
2317
2318 subelems = os_strchr(role, ' ');
2319 if (subelems == NULL)
2320 return 0;
2321 subelems++;
2322
2323 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2324 if (tlvs == NULL)
2325 return 0;
2326
2327 if (os_strstr(role, "[source]"))
2328 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2329 if (os_strstr(role, "[pri-sink]"))
2330 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2331 if (os_strstr(role, "[sec-sink]"))
2332 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2333 if (os_strstr(role, "[source+sink]"))
2334 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2335
2336 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2337 wpabuf_free(tlvs);
2338 return ret;
2339}
2340
2341#endif /* CONFIG_WIFI_DISPLAY */
2342
2343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002344int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345{
2346 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002347 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2349 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002350 return p2p_sd_cancel_request(wpa_s->global->p2p,
2351 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352}
2353
2354
2355void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2356 const u8 *dst, u8 dialog_token,
2357 const struct wpabuf *resp_tlvs)
2358{
2359 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2360 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2361 resp_tlvs);
2362 return;
2363 }
2364 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2365 return;
2366 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2367 resp_tlvs);
2368}
2369
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002371void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2372{
2373 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2374 wpa_drv_p2p_service_update(wpa_s);
2375 return;
2376 }
2377 if (wpa_s->global->p2p)
2378 p2p_sd_service_update(wpa_s->global->p2p);
2379}
2380
2381
2382static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2383{
2384 dl_list_del(&bsrv->list);
2385 wpabuf_free(bsrv->query);
2386 wpabuf_free(bsrv->resp);
2387 os_free(bsrv);
2388}
2389
2390
2391static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2392{
2393 dl_list_del(&usrv->list);
2394 os_free(usrv->service);
2395 os_free(usrv);
2396}
2397
2398
2399void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2400{
2401 struct p2p_srv_bonjour *bsrv, *bn;
2402 struct p2p_srv_upnp *usrv, *un;
2403
2404 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2405 struct p2p_srv_bonjour, list)
2406 wpas_p2p_srv_bonjour_free(bsrv);
2407
2408 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2409 struct p2p_srv_upnp, list)
2410 wpas_p2p_srv_upnp_free(usrv);
2411
2412 wpas_p2p_sd_service_update(wpa_s);
2413}
2414
2415
2416int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2417 struct wpabuf *query, struct wpabuf *resp)
2418{
2419 struct p2p_srv_bonjour *bsrv;
2420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002421 bsrv = os_zalloc(sizeof(*bsrv));
2422 if (bsrv == NULL)
2423 return -1;
2424 bsrv->query = query;
2425 bsrv->resp = resp;
2426 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2427
2428 wpas_p2p_sd_service_update(wpa_s);
2429 return 0;
2430}
2431
2432
2433int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2434 const struct wpabuf *query)
2435{
2436 struct p2p_srv_bonjour *bsrv;
2437
2438 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2439 if (bsrv == NULL)
2440 return -1;
2441 wpas_p2p_srv_bonjour_free(bsrv);
2442 wpas_p2p_sd_service_update(wpa_s);
2443 return 0;
2444}
2445
2446
2447int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2448 const char *service)
2449{
2450 struct p2p_srv_upnp *usrv;
2451
2452 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2453 return 0; /* Already listed */
2454 usrv = os_zalloc(sizeof(*usrv));
2455 if (usrv == NULL)
2456 return -1;
2457 usrv->version = version;
2458 usrv->service = os_strdup(service);
2459 if (usrv->service == NULL) {
2460 os_free(usrv);
2461 return -1;
2462 }
2463 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2464
2465 wpas_p2p_sd_service_update(wpa_s);
2466 return 0;
2467}
2468
2469
2470int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2471 const char *service)
2472{
2473 struct p2p_srv_upnp *usrv;
2474
2475 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2476 if (usrv == NULL)
2477 return -1;
2478 wpas_p2p_srv_upnp_free(usrv);
2479 wpas_p2p_sd_service_update(wpa_s);
2480 return 0;
2481}
2482
2483
2484static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2485 const u8 *peer, const char *params,
2486 unsigned int generated_pin)
2487{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002488 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2489 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490}
2491
2492
2493static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2494 const u8 *peer, const char *params)
2495{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002496 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2497 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498}
2499
2500
2501void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2502 const u8 *dev_addr, const u8 *pri_dev_type,
2503 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002504 u8 dev_capab, u8 group_capab, const u8 *group_id,
2505 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506{
2507 struct wpa_supplicant *wpa_s = ctx;
2508 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002509 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 u8 empty_dev_type[8];
2511 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002512 struct wpa_supplicant *group = NULL;
2513
2514 if (group_id) {
2515 for (group = wpa_s->global->ifaces; group; group = group->next)
2516 {
2517 struct wpa_ssid *s = group->current_ssid;
2518 if (s != NULL &&
2519 s->mode == WPAS_MODE_P2P_GO &&
2520 group_id_len - ETH_ALEN == s->ssid_len &&
2521 os_memcmp(group_id + ETH_ALEN, s->ssid,
2522 s->ssid_len) == 0)
2523 break;
2524 }
2525 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002526
2527 if (pri_dev_type == NULL) {
2528 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2529 pri_dev_type = empty_dev_type;
2530 }
2531 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2532 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002533 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 MAC2STR(dev_addr),
2535 wps_dev_type_bin2str(pri_dev_type, devtype,
2536 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002537 dev_name, supp_config_methods, dev_capab, group_capab,
2538 group ? " group=" : "",
2539 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542 if (config_methods & WPS_CONFIG_DISPLAY) {
2543 generated_pin = wps_generate_pin();
2544 wpas_prov_disc_local_display(wpa_s, peer, params,
2545 generated_pin);
2546 } else if (config_methods & WPS_CONFIG_KEYPAD)
2547 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2548 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002549 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2550 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002551
2552 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2553 P2P_PROV_DISC_SUCCESS,
2554 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555}
2556
2557
2558void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2559{
2560 struct wpa_supplicant *wpa_s = ctx;
2561 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002562 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002564 if (wpa_s->pending_pd_before_join &&
2565 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2566 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2567 wpa_s->pending_pd_before_join = 0;
2568 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2569 "join-existing-group operation");
2570 wpas_p2p_join_start(wpa_s);
2571 return;
2572 }
2573
Dmitry Shmidt04949592012-07-19 12:16:46 -07002574 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2575 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2576 os_snprintf(params, sizeof(params), " peer_go=%d",
2577 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2578 else
2579 params[0] = '\0';
2580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002582 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 else if (config_methods & WPS_CONFIG_KEYPAD) {
2584 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002585 wpas_prov_disc_local_display(wpa_s, peer, params,
2586 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002588 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2589 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590
Jouni Malinen75ecf522011-06-27 15:19:46 -07002591 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2592 P2P_PROV_DISC_SUCCESS,
2593 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002594}
2595
2596
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002597static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2598 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002599{
2600 struct wpa_supplicant *wpa_s = ctx;
2601
Dmitry Shmidt04949592012-07-19 12:16:46 -07002602 if (wpa_s->p2p_fallback_to_go_neg) {
2603 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2604 "failed - fall back to GO Negotiation");
2605 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2606 return;
2607 }
2608
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002609 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002610 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002611 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2612 "join-existing-group operation (no ACK for PD "
2613 "Req attempts)");
2614 wpas_p2p_join_start(wpa_s);
2615 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002616 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002617
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002618 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2619 " p2p_dev_addr=" MACSTR " status=%d",
2620 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002621
Jouni Malinen75ecf522011-06-27 15:19:46 -07002622 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2623 status, 0, 0);
2624}
2625
2626
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002627static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2628{
2629 if (channels == NULL)
2630 return 1; /* Assume no restrictions */
2631 return p2p_channels_includes_freq(channels, freq);
2632
2633}
2634
2635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2637 const u8 *go_dev_addr, const u8 *ssid,
2638 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002639 int *force_freq, int persistent_group,
2640 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641{
2642 struct wpa_supplicant *wpa_s = ctx;
2643 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 int res;
2645 struct wpa_supplicant *grp;
2646
2647 if (!persistent_group) {
2648 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2649 " to join an active group", MAC2STR(sa));
2650 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2651 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2652 == 0 ||
2653 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2654 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2655 "authorized invitation");
2656 goto accept_inv;
2657 }
2658 /*
2659 * Do not accept the invitation automatically; notify user and
2660 * request approval.
2661 */
2662 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2663 }
2664
2665 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2666 if (grp) {
2667 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2668 "running persistent group");
2669 if (*go)
2670 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2671 goto accept_inv;
2672 }
2673
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002674 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2675 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2676 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2677 "invitation to re-invoke a persistent group");
2678 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2680
2681 for (s = wpa_s->conf->ssid; s; s = s->next) {
2682 if (s->disabled == 2 &&
2683 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2684 s->ssid_len == ssid_len &&
2685 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2686 break;
2687 }
2688
2689 if (!s) {
2690 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2691 " requested reinvocation of an unknown group",
2692 MAC2STR(sa));
2693 return P2P_SC_FAIL_UNKNOWN_GROUP;
2694 }
2695
2696 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2697 *go = 1;
2698 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2699 wpa_printf(MSG_DEBUG, "P2P: The only available "
2700 "interface is already in use - reject "
2701 "invitation");
2702 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2703 }
2704 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2705 } else if (s->mode == WPAS_MODE_P2P_GO) {
2706 *go = 1;
2707 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2708 {
2709 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2710 "interface address for the group");
2711 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2712 }
2713 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2714 ETH_ALEN);
2715 }
2716
2717accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002718 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2719
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002720 /* Get one of the frequencies currently in use */
2721 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2722 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 -07002723 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002724 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002725 }
2726
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002727 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2728 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002729 if (*go == 0) {
2730 /* We are the client */
2731 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2732 "running a GO but we are capable of MCC, "
2733 "figure out the best channel to use");
2734 *force_freq = 0;
2735 } else if (!freq_included(channels, *force_freq)) {
2736 /* We are the GO, and *force_freq is not in the
2737 * intersection */
2738 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2739 "in intersection but we are capable of MCC, "
2740 "figure out the best channel to use",
2741 *force_freq);
2742 *force_freq = 0;
2743 }
2744 }
2745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 return P2P_SC_SUCCESS;
2747}
2748
2749
2750static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2751 const u8 *ssid, size_t ssid_len,
2752 const u8 *go_dev_addr, u8 status,
2753 int op_freq)
2754{
2755 struct wpa_supplicant *wpa_s = ctx;
2756 struct wpa_ssid *s;
2757
2758 for (s = wpa_s->conf->ssid; s; s = s->next) {
2759 if (s->disabled == 2 &&
2760 s->ssid_len == ssid_len &&
2761 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2762 break;
2763 }
2764
2765 if (status == P2P_SC_SUCCESS) {
2766 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2767 " was accepted; op_freq=%d MHz",
2768 MAC2STR(sa), op_freq);
2769 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002770 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771 wpas_p2p_group_add_persistent(
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002772 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002773 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002775 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002776 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002777 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778 }
2779 return;
2780 }
2781
2782 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2783 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2784 " was rejected (status %u)", MAC2STR(sa), status);
2785 return;
2786 }
2787
2788 if (!s) {
2789 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002790 wpa_msg_global(wpa_s, MSG_INFO,
2791 P2P_EVENT_INVITATION_RECEIVED
2792 "sa=" MACSTR " go_dev_addr=" MACSTR
2793 " bssid=" MACSTR " unknown-network",
2794 MAC2STR(sa), MAC2STR(go_dev_addr),
2795 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002797 wpa_msg_global(wpa_s, MSG_INFO,
2798 P2P_EVENT_INVITATION_RECEIVED
2799 "sa=" MACSTR " go_dev_addr=" MACSTR
2800 " unknown-network",
2801 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002802 }
2803 return;
2804 }
2805
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002806 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002807 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2808 "sa=" MACSTR " persistent=%d freq=%d",
2809 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002810 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002811 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2812 "sa=" MACSTR " persistent=%d",
2813 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002814 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815}
2816
2817
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002818static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2819 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002820 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002821{
2822 size_t i;
2823
2824 if (ssid == NULL)
2825 return;
2826
2827 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2828 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2829 ETH_ALEN) == 0)
2830 break;
2831 }
2832 if (i >= ssid->num_p2p_clients) {
2833 if (ssid->mode != WPAS_MODE_P2P_GO &&
2834 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2835 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2836 "due to invitation result", ssid->id);
2837 wpas_notify_network_removed(wpa_s, ssid);
2838 wpa_config_remove_network(wpa_s->conf, ssid->id);
2839 return;
2840 }
2841 return; /* Peer not found in client list */
2842 }
2843
2844 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002845 "group %d client list%s",
2846 MAC2STR(peer), ssid->id,
2847 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002848 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2849 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2850 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2851 ssid->num_p2p_clients--;
2852#ifndef CONFIG_NO_CONFIG_WRITE
2853 if (wpa_s->parent->conf->update_config &&
2854 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2855 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2856#endif /* CONFIG_NO_CONFIG_WRITE */
2857}
2858
2859
2860static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2861 const u8 *peer)
2862{
2863 struct wpa_ssid *ssid;
2864
2865 wpa_s = wpa_s->global->p2p_invite_group;
2866 if (wpa_s == NULL)
2867 return; /* No known invitation group */
2868 ssid = wpa_s->current_ssid;
2869 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2870 !ssid->p2p_persistent_group)
2871 return; /* Not operating as a GO in persistent group */
2872 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2873 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002874 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002875}
2876
2877
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002878static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002879 const struct p2p_channels *channels,
2880 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881{
2882 struct wpa_supplicant *wpa_s = ctx;
2883 struct wpa_ssid *ssid;
2884
2885 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002886 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2887 "status=%d " MACSTR,
2888 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002890 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2891 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892 }
2893 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2894
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002895 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2896 status, MAC2STR(peer));
2897 if (wpa_s->pending_invite_ssid_id == -1) {
2898 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2899 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002901 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002903 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2904 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2905 "invitation exchange to indicate readiness for "
2906 "re-invocation");
2907 }
2908
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002910 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2911 ssid = wpa_config_get_network(
2912 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002913 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002914 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002915 wpas_p2p_remove_pending_group_interface(wpa_s);
2916 return;
2917 }
2918
2919 ssid = wpa_config_get_network(wpa_s->conf,
2920 wpa_s->pending_invite_ssid_id);
2921 if (ssid == NULL) {
2922 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2923 "data matching with invitation");
2924 return;
2925 }
2926
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002927 /*
2928 * The peer could have missed our ctrl::ack frame for Invitation
2929 * Response and continue retransmitting the frame. To reduce the
2930 * likelihood of the peer not getting successful TX status for the
2931 * Invitation Response frame, wait a short time here before starting
2932 * the persistent group so that we will remain on the current channel to
2933 * acknowledge any possible retransmission from the peer.
2934 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002935#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002936 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2937 "starting persistent group");
2938 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002939#else
2940 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2941 "starting persistent group");
2942 os_sleep(0, 100000);
2943#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002946 ssid->mode == WPAS_MODE_P2P_GO,
2947 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002948 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2949 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002950 ssid->mode == WPAS_MODE_P2P_GO ?
2951 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2952 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002953}
2954
2955
Dmitry Shmidt04949592012-07-19 12:16:46 -07002956static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2957 unsigned int freq)
2958{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002959 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002960}
2961
2962
2963static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2964{
2965 reg->channel[reg->channels] = chan;
2966 reg->channels++;
2967}
2968
2969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002970static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002971 struct p2p_channels *chan,
2972 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002973{
2974 int i, cla = 0;
2975
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002976 os_memset(cli_chan, 0, sizeof(*cli_chan));
2977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2979 "band");
2980
2981 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2982 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002983 chan->reg_class[cla].channels = 0;
2984 for (i = 0; i < 11; i++) {
2985 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2986 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2987 }
2988 if (chan->reg_class[cla].channels)
2989 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002990
2991 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2992 "band");
2993
2994 /* Operating class 115 - 5 GHz, channels 36-48 */
2995 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002996 chan->reg_class[cla].channels = 0;
2997 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2998 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2999 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3000 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3001 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3002 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3003 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3004 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3005 if (chan->reg_class[cla].channels)
3006 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007
3008 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3009 "band");
3010
3011 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3012 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003013 chan->reg_class[cla].channels = 0;
3014 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3015 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3016 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3017 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3018 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3019 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3020 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3021 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3022 if (chan->reg_class[cla].channels)
3023 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003024
3025 chan->reg_classes = cla;
3026 return 0;
3027}
3028
3029
3030static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3031 u16 num_modes,
3032 enum hostapd_hw_mode mode)
3033{
3034 u16 i;
3035
3036 for (i = 0; i < num_modes; i++) {
3037 if (modes[i].mode == mode)
3038 return &modes[i];
3039 }
3040
3041 return NULL;
3042}
3043
3044
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003045enum chan_allowed {
3046 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3047};
3048
Dmitry Shmidt04949592012-07-19 12:16:46 -07003049static int has_channel(struct wpa_global *global,
3050 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051{
3052 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003053 unsigned int freq;
3054
3055 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3056 chan * 5;
3057 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003058 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059
3060 for (i = 0; i < mode->num_channels; i++) {
3061 if (mode->channels[i].chan == chan) {
3062 if (flags)
3063 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003064 if (mode->channels[i].flag &
3065 (HOSTAPD_CHAN_DISABLED |
3066 HOSTAPD_CHAN_RADAR))
3067 return NOT_ALLOWED;
3068 if (mode->channels[i].flag &
3069 (HOSTAPD_CHAN_PASSIVE_SCAN |
3070 HOSTAPD_CHAN_NO_IBSS))
3071 return PASSIVE_ONLY;
3072 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073 }
3074 }
3075
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003076 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003077}
3078
3079
3080struct p2p_oper_class_map {
3081 enum hostapd_hw_mode mode;
3082 u8 op_class;
3083 u8 min_chan;
3084 u8 max_chan;
3085 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003086 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003087};
3088
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003089static struct p2p_oper_class_map op_class[] = {
3090 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003091#if 0 /* Do not enable HT40 on 2 GHz for now */
3092 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3093 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3094#endif
3095 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3096 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3097 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3098 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3099 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3100 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003101
3102 /*
3103 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3104 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3105 * 80 MHz, but currently use the following definition for simplicity
3106 * (these center frequencies are not actual channels, which makes
3107 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3108 * removing invalid channels.
3109 */
3110 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003111 { -1, 0, 0, 0, 0, BW20 }
3112};
3113
3114
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003115static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3116 struct hostapd_hw_modes *mode,
3117 u8 channel)
3118{
3119 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3120 unsigned int i;
3121
3122 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3123 return 0;
3124
3125 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3126 /*
3127 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3128 * so the center channel is 6 channels away from the start/end.
3129 */
3130 if (channel >= center_channels[i] - 6 &&
3131 channel <= center_channels[i] + 6)
3132 return center_channels[i];
3133
3134 return 0;
3135}
3136
3137
3138static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3139 struct hostapd_hw_modes *mode,
3140 u8 channel, u8 bw)
3141{
3142 u8 center_chan;
3143 int i, flags;
3144 enum chan_allowed res, ret = ALLOWED;
3145
3146 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3147 if (!center_chan)
3148 return NOT_ALLOWED;
3149 if (center_chan >= 58 && center_chan <= 138)
3150 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3151
3152 /* check all the channels are available */
3153 for (i = 0; i < 4; i++) {
3154 int adj_chan = center_chan - 6 + i * 4;
3155
3156 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3157 if (res == NOT_ALLOWED)
3158 return NOT_ALLOWED;
3159 if (res == PASSIVE_ONLY)
3160 ret = PASSIVE_ONLY;
3161
3162 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3163 return NOT_ALLOWED;
3164 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3165 return NOT_ALLOWED;
3166 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3167 return NOT_ALLOWED;
3168 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3169 return NOT_ALLOWED;
3170 }
3171
3172 return ret;
3173}
3174
3175
3176static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3177 struct hostapd_hw_modes *mode,
3178 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003179{
3180 int flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003181 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003182
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003183 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3184 if (bw == BW40MINUS) {
3185 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3186 return NOT_ALLOWED;
3187 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3188 } else if (bw == BW40PLUS) {
3189 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3190 return NOT_ALLOWED;
3191 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3192 } else if (bw == BW80) {
3193 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3194 }
3195
3196 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3197 return NOT_ALLOWED;
3198 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3199 return PASSIVE_ONLY;
3200 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003201}
3202
3203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003204static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003205 struct p2p_channels *chan,
3206 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003208 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003209 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003211 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3213 "of all supported channels; assume dualband "
3214 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003215 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216 }
3217
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003218 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219
3220 for (op = 0; op_class[op].op_class; op++) {
3221 struct p2p_oper_class_map *o = &op_class[op];
3222 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003223 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003225 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 if (mode == NULL)
3227 continue;
3228 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003229 enum chan_allowed res;
3230 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3231 if (res == ALLOWED) {
3232 if (reg == NULL) {
3233 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3234 o->op_class);
3235 reg = &chan->reg_class[cla];
3236 cla++;
3237 reg->reg_class = o->op_class;
3238 }
3239 reg->channel[reg->channels] = ch;
3240 reg->channels++;
3241 } else if (res == PASSIVE_ONLY &&
3242 wpa_s->conf->p2p_add_cli_chan) {
3243 if (cli_reg == NULL) {
3244 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3245 o->op_class);
3246 cli_reg = &cli_chan->reg_class[cli_cla];
3247 cli_cla++;
3248 cli_reg->reg_class = o->op_class;
3249 }
3250 cli_reg->channel[cli_reg->channels] = ch;
3251 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003252 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253 }
3254 if (reg) {
3255 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3256 reg->channel, reg->channels);
3257 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003258 if (cli_reg) {
3259 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3260 cli_reg->channel, cli_reg->channels);
3261 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262 }
3263
3264 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003265 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267 return 0;
3268}
3269
3270
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003271int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3272 struct hostapd_hw_modes *mode, u8 channel)
3273{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003274 int op;
3275 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003276
3277 for (op = 0; op_class[op].op_class; op++) {
3278 struct p2p_oper_class_map *o = &op_class[op];
3279 u8 ch;
3280
3281 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3282 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3283 o->bw == BW20 || ch != channel)
3284 continue;
3285 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003286 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003287 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003288 }
3289 }
3290 return 0;
3291}
3292
3293
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003294int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3295 struct hostapd_hw_modes *mode, u8 channel)
3296{
3297 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3298 return 0;
3299
3300 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3301}
3302
3303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3305 size_t buf_len)
3306{
3307 struct wpa_supplicant *wpa_s = ctx;
3308
3309 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3310 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3311 break;
3312 }
3313 if (wpa_s == NULL)
3314 return -1;
3315
3316 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3317}
3318
3319
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003320static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3321{
3322 struct wpa_supplicant *wpa_s = ctx;
3323
3324 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3325 struct wpa_ssid *ssid = wpa_s->current_ssid;
3326 if (ssid == NULL)
3327 continue;
3328 if (ssid->mode != WPAS_MODE_INFRA)
3329 continue;
3330 if (wpa_s->wpa_state != WPA_COMPLETED &&
3331 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3332 continue;
3333 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3334 return 1;
3335 }
3336
3337 return 0;
3338}
3339
3340
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003341static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3342{
3343 struct wpa_supplicant *wpa_s = ctx;
3344 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3345}
3346
3347
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003348int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3349{
3350 struct wpa_interface iface;
3351 struct wpa_supplicant *p2pdev_wpa_s;
3352 char ifname[100];
3353 char force_name[100];
3354 int ret;
3355
3356 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3357 wpa_s->ifname);
3358 force_name[0] = '\0';
3359 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3360 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3361 force_name, wpa_s->pending_interface_addr, NULL);
3362 if (ret < 0) {
3363 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3364 return ret;
3365 }
3366 os_strlcpy(wpa_s->pending_interface_name, ifname,
3367 sizeof(wpa_s->pending_interface_name));
3368
3369 os_memset(&iface, 0, sizeof(iface));
3370 iface.p2p_mgmt = 1;
3371 iface.ifname = wpa_s->pending_interface_name;
3372 iface.driver = wpa_s->driver->name;
3373 iface.driver_param = wpa_s->conf->driver_param;
3374 iface.confname = wpa_s->confname;
3375 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3376 if (!p2pdev_wpa_s) {
3377 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3378 return -1;
3379 }
3380 p2pdev_wpa_s->parent = wpa_s;
3381
3382 wpa_s->pending_interface_name[0] = '\0';
3383 return 0;
3384}
3385
3386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387/**
3388 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3389 * @global: Pointer to global data from wpa_supplicant_init()
3390 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3391 * Returns: 0 on success, -1 on failure
3392 */
3393int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3394{
3395 struct p2p_config p2p;
3396 unsigned int r;
3397 int i;
3398
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003399 if (wpa_s->conf->p2p_disabled)
3400 return 0;
3401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003402 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3403 return 0;
3404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003405 if (global->p2p)
3406 return 0;
3407
3408 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3409 struct p2p_params params;
3410
3411 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3412 os_memset(&params, 0, sizeof(params));
3413 params.dev_name = wpa_s->conf->device_name;
3414 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3415 WPS_DEV_TYPE_LEN);
3416 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3417 os_memcpy(params.sec_dev_type,
3418 wpa_s->conf->sec_device_type,
3419 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3420
3421 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3422 return -1;
3423
3424 return 0;
3425 }
3426
3427 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003429 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003430 p2p.p2p_scan = wpas_p2p_scan;
3431 p2p.send_action = wpas_send_action;
3432 p2p.send_action_done = wpas_send_action_done;
3433 p2p.go_neg_completed = wpas_go_neg_completed;
3434 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3435 p2p.dev_found = wpas_dev_found;
3436 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003437 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003438 p2p.start_listen = wpas_start_listen;
3439 p2p.stop_listen = wpas_stop_listen;
3440 p2p.send_probe_resp = wpas_send_probe_resp;
3441 p2p.sd_request = wpas_sd_request;
3442 p2p.sd_response = wpas_sd_response;
3443 p2p.prov_disc_req = wpas_prov_disc_req;
3444 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003445 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446 p2p.invitation_process = wpas_invitation_process;
3447 p2p.invitation_received = wpas_invitation_received;
3448 p2p.invitation_result = wpas_invitation_result;
3449 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003450 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003451
3452 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003453 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003454 p2p.dev_name = wpa_s->conf->device_name;
3455 p2p.manufacturer = wpa_s->conf->manufacturer;
3456 p2p.model_name = wpa_s->conf->model_name;
3457 p2p.model_number = wpa_s->conf->model_number;
3458 p2p.serial_number = wpa_s->conf->serial_number;
3459 if (wpa_s->wps) {
3460 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3461 p2p.config_methods = wpa_s->wps->config_methods;
3462 }
3463
3464 if (wpa_s->conf->p2p_listen_reg_class &&
3465 wpa_s->conf->p2p_listen_channel) {
3466 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3467 p2p.channel = wpa_s->conf->p2p_listen_channel;
3468 } else {
3469 p2p.reg_class = 81;
3470 /*
3471 * Pick one of the social channels randomly as the listen
3472 * channel.
3473 */
3474 os_get_random((u8 *) &r, sizeof(r));
3475 p2p.channel = 1 + (r % 3) * 5;
3476 }
3477 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3478
3479 if (wpa_s->conf->p2p_oper_reg_class &&
3480 wpa_s->conf->p2p_oper_channel) {
3481 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3482 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3483 p2p.cfg_op_channel = 1;
3484 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3485 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3486
3487 } else {
3488 p2p.op_reg_class = 81;
3489 /*
3490 * Use random operation channel from (1, 6, 11) if no other
3491 * preference is indicated.
3492 */
3493 os_get_random((u8 *) &r, sizeof(r));
3494 p2p.op_channel = 1 + (r % 3) * 5;
3495 p2p.cfg_op_channel = 0;
3496 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3497 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3498 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003499
3500 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3501 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3502 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3503 }
3504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003505 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3506 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3507 p2p.country[2] = 0x04;
3508 } else
3509 os_memcpy(p2p.country, "XX\x04", 3);
3510
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003511 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3513 "channel list");
3514 return -1;
3515 }
3516
3517 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3518 WPS_DEV_TYPE_LEN);
3519
3520 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3521 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3522 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3523
3524 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3525 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3526
3527 p2p.max_peers = 100;
3528
3529 if (wpa_s->conf->p2p_ssid_postfix) {
3530 p2p.ssid_postfix_len =
3531 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3532 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3533 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3534 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3535 p2p.ssid_postfix_len);
3536 }
3537
3538 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3539
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003540 p2p.max_listen = wpa_s->max_remain_on_chan;
3541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542 global->p2p = p2p_init(&p2p);
3543 if (global->p2p == NULL)
3544 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003545 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546
3547 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3548 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3549 continue;
3550 p2p_add_wps_vendor_extension(
3551 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3552 }
3553
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003554 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003556 return 0;
3557}
3558
3559
3560/**
3561 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3562 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3563 *
3564 * This function deinitialize per-interface P2P data.
3565 */
3566void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3567{
3568 if (wpa_s->driver && wpa_s->drv_priv)
3569 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003570
3571 if (wpa_s->go_params) {
3572 /* Clear any stored provisioning info */
3573 p2p_clear_provisioning_info(
3574 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003575 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003576 }
3577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003578 os_free(wpa_s->go_params);
3579 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003580#ifdef ANDROID_P2P
3581 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3582#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3584 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3585 wpa_s->p2p_long_listen = 0;
3586 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3587 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3588 wpas_p2p_remove_pending_group_interface(wpa_s);
3589
3590 /* TODO: remove group interface from the driver if this wpa_s instance
3591 * is on top of a P2P group interface */
3592}
3593
3594
3595/**
3596 * wpas_p2p_deinit_global - Deinitialize global P2P module
3597 * @global: Pointer to global data from wpa_supplicant_init()
3598 *
3599 * This function deinitializes the global (per device) P2P module.
3600 */
3601void wpas_p2p_deinit_global(struct wpa_global *global)
3602{
3603 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003605 wpa_s = global->ifaces;
3606 if (wpa_s)
3607 wpas_p2p_service_flush(wpa_s);
3608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 if (global->p2p == NULL)
3610 return;
3611
3612 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3614 wpa_s = wpa_s->next;
3615 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616 tmp = global->ifaces;
3617 while (tmp &&
3618 (tmp == wpa_s ||
3619 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3620 tmp = tmp->next;
3621 }
3622 if (tmp == NULL)
3623 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003624 /* Disconnect from the P2P group and deinit the interface */
3625 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003626 }
3627
3628 /*
3629 * Deinit GO data on any possibly remaining interface (if main
3630 * interface is used as GO).
3631 */
3632 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3633 if (wpa_s->ap_iface)
3634 wpas_p2p_group_deinit(wpa_s);
3635 }
3636
3637 p2p_deinit(global->p2p);
3638 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003639 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640}
3641
3642
3643static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3644{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003645 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3646 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003647 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 if (wpa_s->drv_flags &
3649 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3650 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3651 return 1; /* P2P group requires a new interface in every case
3652 */
3653 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3654 return 0; /* driver does not support concurrent operations */
3655 if (wpa_s->global->ifaces->next)
3656 return 1; /* more that one interface already in use */
3657 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3658 return 1; /* this interface is already in use */
3659 return 0;
3660}
3661
3662
3663static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3664 const u8 *peer_addr,
3665 enum p2p_wps_method wps_method,
3666 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003667 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003668 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003670 if (persistent_group && wpa_s->conf->persistent_reconnect)
3671 persistent_group = 2;
3672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3674 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3675 go_intent, own_interface_addr,
3676 force_freq, persistent_group);
3677 }
3678
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003679 /*
3680 * Increase GO config timeout if HT40 is used since it takes some time
3681 * to scan channels for coex purposes before the BSS can be started.
3682 */
3683 p2p_set_config_timeout(wpa_s->global->p2p,
3684 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3687 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003688 persistent_group, ssid ? ssid->ssid : NULL,
3689 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003690 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691}
3692
3693
3694static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3695 const u8 *peer_addr,
3696 enum p2p_wps_method wps_method,
3697 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003698 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003699 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003701 if (persistent_group && wpa_s->conf->persistent_reconnect)
3702 persistent_group = 2;
3703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003704 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3705 return -1;
3706
3707 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3708 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003709 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003710 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003711}
3712
3713
3714static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3715{
3716 wpa_s->p2p_join_scan_count++;
3717 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3718 wpa_s->p2p_join_scan_count);
3719 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3720 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3721 " for join operationg - stop join attempt",
3722 MAC2STR(wpa_s->pending_join_iface_addr));
3723 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003724 if (wpa_s->p2p_auto_pd) {
3725 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003726 wpa_msg_global(wpa_s, MSG_INFO,
3727 P2P_EVENT_PROV_DISC_FAILURE
3728 " p2p_dev_addr=" MACSTR " status=N/A",
3729 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003730 return;
3731 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003732 wpa_msg_global(wpa_s->parent, MSG_INFO,
3733 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003734 }
3735}
3736
3737
Dmitry Shmidt04949592012-07-19 12:16:46 -07003738static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3739{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003740 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003741
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003742 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3743 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003744 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003745 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003746
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003747 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3748 if (!freqs)
3749 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003750
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003751 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3752 wpa_s->num_multichan_concurrent);
3753 if (num < 0) {
3754 res = 1;
3755 goto exit_free;
3756 }
3757
3758 for (i = 0; i < num; i++) {
3759 if (freqs[i] == freq) {
3760 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3761 freq);
3762 res = 0;
3763 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003764 }
3765 }
3766
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003767 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003768
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003769exit_free:
3770 os_free(freqs);
3771 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003772}
3773
3774
3775static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3776 const u8 *peer_dev_addr)
3777{
3778 struct wpa_bss *bss;
3779 int updated;
3780
3781 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3782 if (bss == NULL)
3783 return -1;
3784 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3785 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3786 "last scan");
3787 return 0;
3788 }
3789
3790 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3791 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3792 "%ld.%06ld (%supdated in last scan)",
3793 bss->last_update.sec, bss->last_update.usec,
3794 updated ? "": "not ");
3795
3796 return updated;
3797}
3798
3799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3801 struct wpa_scan_results *scan_res)
3802{
3803 struct wpa_bss *bss;
3804 int freq;
3805 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3808
3809 if (wpa_s->global->p2p_disabled)
3810 return;
3811
Dmitry Shmidt04949592012-07-19 12:16:46 -07003812 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3813 scan_res ? (int) scan_res->num : -1,
3814 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003815
3816 if (scan_res)
3817 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3818
Dmitry Shmidt04949592012-07-19 12:16:46 -07003819 if (wpa_s->p2p_auto_pd) {
3820 int join = wpas_p2p_peer_go(wpa_s,
3821 wpa_s->pending_join_dev_addr);
3822 if (join == 0 &&
3823 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3824 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003825 bss = wpa_bss_get_bssid_latest(
3826 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003827 if (bss) {
3828 freq = bss->freq;
3829 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3830 "the peer " MACSTR " at %d MHz",
3831 wpa_s->auto_pd_scan_retry,
3832 MAC2STR(wpa_s->
3833 pending_join_dev_addr),
3834 freq);
3835 wpas_p2p_join_scan_req(wpa_s, freq);
3836 return;
3837 }
3838 }
3839
3840 if (join < 0)
3841 join = 0;
3842
3843 wpa_s->p2p_auto_pd = 0;
3844 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3845 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3846 MAC2STR(wpa_s->pending_join_dev_addr), join);
3847 if (p2p_prov_disc_req(wpa_s->global->p2p,
3848 wpa_s->pending_join_dev_addr,
3849 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003850 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003851 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003852 wpa_msg_global(wpa_s, MSG_INFO,
3853 P2P_EVENT_PROV_DISC_FAILURE
3854 " p2p_dev_addr=" MACSTR " status=N/A",
3855 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003856 }
3857 return;
3858 }
3859
3860 if (wpa_s->p2p_auto_join) {
3861 int join = wpas_p2p_peer_go(wpa_s,
3862 wpa_s->pending_join_dev_addr);
3863 if (join < 0) {
3864 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3865 "running a GO -> use GO Negotiation");
3866 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3867 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3868 wpa_s->p2p_persistent_group, 0, 0, 0,
3869 wpa_s->p2p_go_intent,
3870 wpa_s->p2p_connect_freq,
3871 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003872 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003873 wpa_s->p2p_go_ht40,
3874 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003875 return;
3876 }
3877
3878 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3879 "try to join the group", join ? "" :
3880 " in older scan");
3881 if (!join)
3882 wpa_s->p2p_fallback_to_go_neg = 1;
3883 }
3884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3886 wpa_s->pending_join_iface_addr);
3887 if (freq < 0 &&
3888 p2p_get_interface_addr(wpa_s->global->p2p,
3889 wpa_s->pending_join_dev_addr,
3890 iface_addr) == 0 &&
3891 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3892 {
3893 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3894 "address for join from " MACSTR " to " MACSTR
3895 " based on newly discovered P2P peer entry",
3896 MAC2STR(wpa_s->pending_join_iface_addr),
3897 MAC2STR(iface_addr));
3898 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3899 ETH_ALEN);
3900
3901 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3902 wpa_s->pending_join_iface_addr);
3903 }
3904 if (freq >= 0) {
3905 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3906 "from P2P peer table: %d MHz", freq);
3907 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003908 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909 if (bss) {
3910 freq = bss->freq;
3911 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07003912 "from BSS table: %d MHz (SSID %s)", freq,
3913 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914 }
3915 if (freq > 0) {
3916 u16 method;
3917
Dmitry Shmidt04949592012-07-19 12:16:46 -07003918 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003919 wpa_msg_global(wpa_s->parent, MSG_INFO,
3920 P2P_EVENT_GROUP_FORMATION_FAILURE
3921 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003922 return;
3923 }
3924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3926 "prior to joining an existing group (GO " MACSTR
3927 " freq=%u MHz)",
3928 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3929 wpa_s->pending_pd_before_join = 1;
3930
3931 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932 case WPS_PIN_DISPLAY:
3933 method = WPS_CONFIG_KEYPAD;
3934 break;
3935 case WPS_PIN_KEYPAD:
3936 method = WPS_CONFIG_DISPLAY;
3937 break;
3938 case WPS_PBC:
3939 method = WPS_CONFIG_PUSHBUTTON;
3940 break;
3941 default:
3942 method = 0;
3943 break;
3944 }
3945
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003946 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3947 wpa_s->pending_join_dev_addr) ==
3948 method)) {
3949 /*
3950 * We have already performed provision discovery for
3951 * joining the group. Proceed directly to join
3952 * operation without duplicated provision discovery. */
3953 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3954 "with " MACSTR " already done - proceed to "
3955 "join",
3956 MAC2STR(wpa_s->pending_join_dev_addr));
3957 wpa_s->pending_pd_before_join = 0;
3958 goto start;
3959 }
3960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003961 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003962 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003963 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3965 "Discovery Request before joining an "
3966 "existing group");
3967 wpa_s->pending_pd_before_join = 0;
3968 goto start;
3969 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003970 return;
3971 }
3972
3973 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3974 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3975 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3976 wpas_p2p_check_join_scan_limit(wpa_s);
3977 return;
3978
3979start:
3980 /* Start join operation immediately */
3981 wpas_p2p_join_start(wpa_s);
3982}
3983
3984
Dmitry Shmidt04949592012-07-19 12:16:46 -07003985static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003986{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987 int ret;
3988 struct wpa_driver_scan_params params;
3989 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003990 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003991 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003992#ifdef ANDROID_P2P
3993 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003995 /* If freq is not provided, check the operating freq of the GO and do a
3996 * a directed scan to save time
3997 */
3998 if(!freq) {
3999 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4000 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
4001 }
4002#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003 os_memset(&params, 0, sizeof(params));
4004
4005 /* P2P Wildcard SSID */
4006 params.num_ssids = 1;
4007 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4008 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4009
4010 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004011 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4012 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4013 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014 if (wps_ie == NULL) {
4015 wpas_p2p_scan_res_join(wpa_s, NULL);
4016 return;
4017 }
4018
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004019 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4020 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021 if (ies == NULL) {
4022 wpabuf_free(wps_ie);
4023 wpas_p2p_scan_res_join(wpa_s, NULL);
4024 return;
4025 }
4026 wpabuf_put_buf(ies, wps_ie);
4027 wpabuf_free(wps_ie);
4028
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004029 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004030
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004031 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004032 params.extra_ies = wpabuf_head(ies);
4033 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004034 if (freq > 0) {
4035 freqs[0] = freq;
4036 params.freqs = freqs;
4037 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004038
4039 /*
4040 * Run a scan to update BSS table and start Provision Discovery once
4041 * the new scan results become available.
4042 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004043 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004044 if (!ret) {
4045 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004046 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004047 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004048
4049 wpabuf_free(ies);
4050
4051 if (ret) {
4052 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4053 "try again later");
4054 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4055 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4056 wpas_p2p_check_join_scan_limit(wpa_s);
4057 }
4058}
4059
4060
Dmitry Shmidt04949592012-07-19 12:16:46 -07004061static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4062{
4063 struct wpa_supplicant *wpa_s = eloop_ctx;
4064 wpas_p2p_join_scan_req(wpa_s, 0);
4065}
4066
4067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004068static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004069 const u8 *dev_addr, enum p2p_wps_method wps_method,
4070 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071{
4072 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004073 MACSTR " dev " MACSTR ")%s",
4074 MAC2STR(iface_addr), MAC2STR(dev_addr),
4075 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076
Dmitry Shmidt04949592012-07-19 12:16:46 -07004077 wpa_s->p2p_auto_pd = 0;
4078 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4080 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4081 wpa_s->pending_join_wps_method = wps_method;
4082
4083 /* Make sure we are not running find during connection establishment */
4084 wpas_p2p_stop_find(wpa_s);
4085
4086 wpa_s->p2p_join_scan_count = 0;
4087 wpas_p2p_join_scan(wpa_s, NULL);
4088 return 0;
4089}
4090
4091
4092static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4093{
4094 struct wpa_supplicant *group;
4095 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004096 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097
4098 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4099 if (group == NULL)
4100 return -1;
4101 if (group != wpa_s) {
4102 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4103 sizeof(group->p2p_pin));
4104 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004105 } else {
4106 /*
4107 * Need to mark the current interface for p2p_group_formation
4108 * when a separate group interface is not used. This is needed
4109 * to allow p2p_cancel stop a pending p2p_connect-join.
4110 * wpas_p2p_init_group_interface() addresses this for the case
4111 * where a separate group interface is used.
4112 */
4113 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114 }
4115
4116 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004117 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004118
4119 os_memset(&res, 0, sizeof(res));
4120 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4121 ETH_ALEN);
4122 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004123 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004124 if (bss) {
4125 res.freq = bss->freq;
4126 res.ssid_len = bss->ssid_len;
4127 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07004128 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4129 "from BSS table: %d MHz (SSID %s)", bss->freq,
4130 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004131 }
4132
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004133 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4134 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4135 "starting client");
4136 wpa_drv_cancel_remain_on_channel(wpa_s);
4137 wpa_s->off_channel_freq = 0;
4138 wpa_s->roc_waiting_drv_freq = 0;
4139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004140 wpas_start_wps_enrollee(group, &res);
4141
4142 /*
4143 * Allow a longer timeout for join-a-running-group than normal 15
4144 * second group formation timeout since the GO may not have authorized
4145 * our connection yet.
4146 */
4147 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4148 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4149 wpa_s, NULL);
4150
4151 return 0;
4152}
4153
4154
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004155static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004156 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004157{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004158 int *freqs, res;
4159 unsigned int freq_in_use = 0, num, i;
4160
4161 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4162 if (!freqs)
4163 return -1;
4164
4165 num = get_shared_radio_freqs(wpa_s, freqs,
4166 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004167 wpa_printf(MSG_DEBUG,
4168 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4169 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004170
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004171 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004172 int ret;
4173 if (go)
4174 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4175 else
4176 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4177 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004178 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4179 "(%u MHz) is not supported for P2P uses",
4180 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004181 res = -3;
4182 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004183 }
4184
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004185 for (i = 0; i < num; i++) {
4186 if (freqs[i] == freq)
4187 freq_in_use = 1;
4188 }
4189
4190 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4191 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4192 freq);
4193 res = -2;
4194 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004195 }
4196 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4197 "requested channel (%u MHz)", freq);
4198 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004199 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004200 }
4201
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004202 for (i = 0; i < num; i++) {
4203 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4204 continue;
4205
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004206#ifndef ANDROID_P2P
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004207 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4208 *force_freq);
4209 *force_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004210#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004211
4212 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4213 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004214 *pref_freq = freqs[i];
4215#ifdef ANDROID_P2P
4216 } else {
4217 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4218 *force_freq);
4219 *force_freq = freqs[i];
4220#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004221 }
4222 break;
4223 }
4224
4225 if (i == num) {
4226 if (num < wpa_s->num_multichan_concurrent) {
4227 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4228 *force_freq = 0;
4229 } else {
4230 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4231 res = -2;
4232 goto exit_free;
4233 }
4234 }
4235
4236exit_ok:
4237 res = 0;
4238exit_free:
4239 os_free(freqs);
4240 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004241}
4242
4243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004244/**
4245 * wpas_p2p_connect - Request P2P Group Formation to be started
4246 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4247 * @peer_addr: Address of the peer P2P Device
4248 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4249 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004250 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251 * @join: Whether to join an existing group (as a client) instead of starting
4252 * Group Owner negotiation; @peer_addr is BSSID in that case
4253 * @auth: Whether to only authorize the connection instead of doing that and
4254 * initiating Group Owner negotiation
4255 * @go_intent: GO Intent or -1 to use default
4256 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004257 * @persistent_id: Persistent group credentials to use for forcing GO
4258 * parameters or -1 to generate new values (SSID/passphrase)
4259 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4260 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004261 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004262 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4264 * failure, -2 on failure due to channel not currently available,
4265 * -3 if forced channel is not supported
4266 */
4267int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4268 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004269 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004270 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004271 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004272{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004273 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004274 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004275 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004276 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004277 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004278
4279 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4280 return -1;
4281
Dmitry Shmidt04949592012-07-19 12:16:46 -07004282 if (persistent_id >= 0) {
4283 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4284 if (ssid == NULL || ssid->disabled != 2 ||
4285 ssid->mode != WPAS_MODE_P2P_GO)
4286 return -1;
4287 }
4288
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004289 os_free(wpa_s->global->add_psk);
4290 wpa_s->global->add_psk = NULL;
4291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292 if (go_intent < 0)
4293 go_intent = wpa_s->conf->p2p_go_intent;
4294
4295 if (!auth)
4296 wpa_s->p2p_long_listen = 0;
4297
4298 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004299 wpa_s->p2p_persistent_group = !!persistent_group;
4300 wpa_s->p2p_persistent_id = persistent_id;
4301 wpa_s->p2p_go_intent = go_intent;
4302 wpa_s->p2p_connect_freq = freq;
4303 wpa_s->p2p_fallback_to_go_neg = 0;
4304 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004305 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004306 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004307
4308 if (pin)
4309 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4310 else if (wps_method == WPS_PIN_DISPLAY) {
4311 ret = wps_generate_pin();
4312 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4313 ret);
4314 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4315 wpa_s->p2p_pin);
4316 } else
4317 wpa_s->p2p_pin[0] = '\0';
4318
Dmitry Shmidt04949592012-07-19 12:16:46 -07004319 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4321 if (auth) {
4322 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4323 "connect a running group from " MACSTR,
4324 MAC2STR(peer_addr));
4325 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4326 return ret;
4327 }
4328 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4329 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4330 iface_addr) < 0) {
4331 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4332 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4333 dev_addr);
4334 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004335 if (auto_join) {
4336 os_get_time(&wpa_s->p2p_auto_started);
4337 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4338 "%ld.%06ld",
4339 wpa_s->p2p_auto_started.sec,
4340 wpa_s->p2p_auto_started.usec);
4341 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004342 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004343 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4344 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004345 return -1;
4346 return ret;
4347 }
4348
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004349 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4350 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004351 if (res)
4352 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004353 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004354
4355 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4356
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004357 if (wpa_s->create_p2p_iface) {
4358 /* Prepare to add a new interface for the group */
4359 iftype = WPA_IF_P2P_GROUP;
4360 if (go_intent == 15)
4361 iftype = WPA_IF_P2P_GO;
4362 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4363 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4364 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004367
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004368 if_addr = wpa_s->pending_interface_addr;
4369 } else
4370 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004371
4372 if (auth) {
4373 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004374 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004375 force_freq, persistent_group, ssid,
4376 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004377 return -1;
4378 return ret;
4379 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004380
4381 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4382 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004383 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004384 if (wpa_s->create_p2p_iface)
4385 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004386 return -1;
4387 }
4388 return ret;
4389}
4390
4391
4392/**
4393 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4394 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4395 * @freq: Frequency of the channel in MHz
4396 * @duration: Duration of the stay on the channel in milliseconds
4397 *
4398 * This callback is called when the driver indicates that it has started the
4399 * requested remain-on-channel duration.
4400 */
4401void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4402 unsigned int freq, unsigned int duration)
4403{
4404 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4405 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004406 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4407 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4408 wpa_s->pending_listen_duration);
4409 wpa_s->pending_listen_freq = 0;
4410 }
4411}
4412
4413
4414static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4415 unsigned int timeout)
4416{
4417 /* Limit maximum Listen state time based on driver limitation. */
4418 if (timeout > wpa_s->max_remain_on_chan)
4419 timeout = wpa_s->max_remain_on_chan;
4420
4421 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4422 return wpa_drv_p2p_listen(wpa_s, timeout);
4423
4424 return p2p_listen(wpa_s->global->p2p, timeout);
4425}
4426
4427
4428/**
4429 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4430 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4431 * @freq: Frequency of the channel in MHz
4432 *
4433 * This callback is called when the driver indicates that a remain-on-channel
4434 * operation has been completed, i.e., the duration on the requested channel
4435 * has timed out.
4436 */
4437void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4438 unsigned int freq)
4439{
4440 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4441 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004442 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004443 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4444 return;
4445 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4446 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004447 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 return;
4449 if (wpa_s->p2p_long_listen > 0)
4450 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4451 if (wpa_s->p2p_long_listen > 0) {
4452 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4453 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004454 } else {
4455 /*
4456 * When listen duration is over, stop listen & update p2p_state
4457 * to IDLE.
4458 */
4459 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004460 }
4461}
4462
4463
4464/**
4465 * wpas_p2p_group_remove - Remove a P2P group
4466 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4467 * @ifname: Network interface name of the group interface or "*" to remove all
4468 * groups
4469 * Returns: 0 on success, -1 on failure
4470 *
4471 * This function is used to remove a P2P group. This can be used to disconnect
4472 * from a group in which the local end is a P2P Client or to end a P2P Group in
4473 * case the local end is the Group Owner. If a virtual network interface was
4474 * created for this group, that interface will be removed. Otherwise, only the
4475 * configured P2P group network will be removed from the interface.
4476 */
4477int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4478{
4479 struct wpa_global *global = wpa_s->global;
4480
4481 if (os_strcmp(ifname, "*") == 0) {
4482 struct wpa_supplicant *prev;
4483 wpa_s = global->ifaces;
4484 while (wpa_s) {
4485 prev = wpa_s;
4486 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004487 if (prev->p2p_group_interface !=
4488 NOT_P2P_GROUP_INTERFACE ||
4489 (prev->current_ssid &&
4490 prev->current_ssid->p2p_group))
4491 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004492 }
4493 return 0;
4494 }
4495
4496 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4497 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4498 break;
4499 }
4500
4501 return wpas_p2p_disconnect(wpa_s);
4502}
4503
4504
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004505static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4506{
4507 unsigned int r;
4508
4509 if (freq == 2) {
4510 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4511 "band");
4512 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004513 p2p_supported_freq_go(wpa_s->global->p2p,
4514 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004515 freq = wpa_s->best_24_freq;
4516 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4517 "channel: %d MHz", freq);
4518 } else {
4519 os_get_random((u8 *) &r, sizeof(r));
4520 freq = 2412 + (r % 3) * 25;
4521 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4522 "channel: %d MHz", freq);
4523 }
4524 }
4525
4526 if (freq == 5) {
4527 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4528 "band");
4529 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004530 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004531 wpa_s->best_5_freq)) {
4532 freq = wpa_s->best_5_freq;
4533 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4534 "channel: %d MHz", freq);
4535 } else {
4536 os_get_random((u8 *) &r, sizeof(r));
4537 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004538 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004539 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4540 "5 GHz channel for P2P group");
4541 return -1;
4542 }
4543 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4544 "channel: %d MHz", freq);
4545 }
4546 }
4547
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004548 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004549 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4550 "(%u MHz) is not supported for P2P uses",
4551 freq);
4552 return -1;
4553 }
4554
4555 return freq;
4556}
4557
4558
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004559static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4560 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004561 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004562 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004564 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004565 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004566 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004567
4568 os_memset(params, 0, sizeof(*params));
4569 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004570 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004571 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004573 if (!freq_included(channels, freq)) {
4574 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4575 "accepted", freq);
4576 return -1;
4577 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004578 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4579 "frequency %d MHz", freq);
4580 params->freq = freq;
4581 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4582 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004583 wpa_s->conf->p2p_oper_channel <= 11 &&
4584 freq_included(channels,
4585 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4587 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4588 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004589 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4590 wpa_s->conf->p2p_oper_reg_class == 116 ||
4591 wpa_s->conf->p2p_oper_reg_class == 117 ||
4592 wpa_s->conf->p2p_oper_reg_class == 124 ||
4593 wpa_s->conf->p2p_oper_reg_class == 126 ||
4594 wpa_s->conf->p2p_oper_reg_class == 127) &&
4595 freq_included(channels,
4596 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004597 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4598 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4599 "frequency %d MHz", params->freq);
4600 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4601 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004602 p2p_supported_freq_go(wpa_s->global->p2p,
4603 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004604 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605 params->freq = wpa_s->best_overall_freq;
4606 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4607 "channel %d MHz", params->freq);
4608 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4609 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004610 p2p_supported_freq_go(wpa_s->global->p2p,
4611 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004612 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004613 params->freq = wpa_s->best_24_freq;
4614 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4615 "channel %d MHz", params->freq);
4616 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4617 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004618 p2p_supported_freq_go(wpa_s->global->p2p,
4619 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004620 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004621 params->freq = wpa_s->best_5_freq;
4622 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4623 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004624 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4625 channels))) {
4626 params->freq = pref_freq;
4627 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4628 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004630 int chan;
4631 for (chan = 0; chan < 11; chan++) {
4632 params->freq = 2412 + chan * 5;
4633 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004634 params->freq) &&
4635 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004636 break;
4637 }
4638 if (chan == 11) {
4639 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4640 "allowed");
4641 return -1;
4642 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4644 "known)", params->freq);
4645 }
4646
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004647 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4648 if (!freqs)
4649 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004650
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004651 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4652 wpa_s->num_multichan_concurrent);
4653 if (res < 0) {
4654 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004655 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004656 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004657 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004658
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004659 if (!freq) {
4660 for (i = 0; i < num; i++) {
4661 if (freq_included(channels, freqs[i])) {
4662 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4663 freqs[i]);
4664 params->freq = freqs[i];
4665 break;
4666 }
4667 }
4668
4669 if (i == num) {
4670 if (num == wpa_s->num_multichan_concurrent) {
4671 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4672 os_free(freqs);
4673 return -1;
4674 } else {
4675 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4676 }
4677 }
4678 } else {
4679 for (i = 0; i < num; i++) {
4680 if (freqs[i] == freq)
4681 break;
4682 }
4683
4684 if (i == num) {
4685 if (num == wpa_s->num_multichan_concurrent) {
4686 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4687 os_free(freqs);
4688 return -1;
4689 } else {
4690 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4691 }
4692 }
4693 }
4694 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004695 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004696}
4697
4698
4699static struct wpa_supplicant *
4700wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4701 int go)
4702{
4703 struct wpa_supplicant *group_wpa_s;
4704
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004705 if (!wpas_p2p_create_iface(wpa_s)) {
4706 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4707 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004708 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004709 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004710 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004711
4712 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004713 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004714 wpa_msg_global(wpa_s, MSG_ERROR,
4715 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004717 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4719 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004720 wpa_msg_global(wpa_s, MSG_ERROR,
4721 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 wpas_p2p_remove_pending_group_interface(wpa_s);
4723 return NULL;
4724 }
4725
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004726 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4727 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004728 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729 return group_wpa_s;
4730}
4731
4732
4733/**
4734 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4735 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4736 * @persistent_group: Whether to create a persistent group
4737 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004738 * @ht40: Start GO with 40 MHz channel width
4739 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740 * Returns: 0 on success, -1 on failure
4741 *
4742 * This function creates a new P2P group with the local end as the Group Owner,
4743 * i.e., without using Group Owner Negotiation.
4744 */
4745int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004746 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004747{
4748 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004749
4750 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4751 return -1;
4752
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004753 os_free(wpa_s->global->add_psk);
4754 wpa_s->global->add_psk = NULL;
4755
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004756 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004757 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004758 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004759
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004760 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4761 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004763
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004764 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004765 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004766 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004767 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004768 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4769 "(%u MHz) is not supported for P2P uses",
4770 params.freq);
4771 return -1;
4772 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004773 p2p_go_params(wpa_s->global->p2p, &params);
4774 params.persistent_group = persistent_group;
4775
4776 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4777 if (wpa_s == NULL)
4778 return -1;
4779 wpas_start_wps_go(wpa_s, &params, 0);
4780
4781 return 0;
4782}
4783
4784
4785static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4786 struct wpa_ssid *params, int addr_allocated)
4787{
4788 struct wpa_ssid *ssid;
4789
4790 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4791 if (wpa_s == NULL)
4792 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004793 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004794
4795 wpa_supplicant_ap_deinit(wpa_s);
4796
4797 ssid = wpa_config_add_network(wpa_s->conf);
4798 if (ssid == NULL)
4799 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800 wpa_config_set_network_defaults(ssid);
4801 ssid->temporary = 1;
4802 ssid->proto = WPA_PROTO_RSN;
4803 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4804 ssid->group_cipher = WPA_CIPHER_CCMP;
4805 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4806 ssid->ssid = os_malloc(params->ssid_len);
4807 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004808 wpa_config_remove_network(wpa_s->conf, ssid->id);
4809 return -1;
4810 }
4811 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4812 ssid->ssid_len = params->ssid_len;
4813 ssid->p2p_group = 1;
4814 ssid->export_keys = 1;
4815 if (params->psk_set) {
4816 os_memcpy(ssid->psk, params->psk, 32);
4817 ssid->psk_set = 1;
4818 }
4819 if (params->passphrase)
4820 ssid->passphrase = os_strdup(params->passphrase);
4821
4822 wpa_supplicant_select_network(wpa_s, ssid);
4823
4824 wpa_s->show_group_started = 1;
4825
4826 return 0;
4827}
4828
4829
4830int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4831 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004832 int freq, int ht40, int vht,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004833 const struct p2p_channels *channels,
4834 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835{
4836 struct p2p_go_neg_results params;
4837 int go = 0;
4838
4839 if (ssid->disabled != 2 || ssid->ssid == NULL)
4840 return -1;
4841
4842 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4843 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4844 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4845 "already running");
4846 return 0;
4847 }
4848
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004849 os_free(wpa_s->global->add_psk);
4850 wpa_s->global->add_psk = NULL;
4851
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004852 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004853 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854
Dmitry Shmidt04949592012-07-19 12:16:46 -07004855 wpa_s->p2p_fallback_to_go_neg = 0;
4856
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004857 if (ssid->mode == WPAS_MODE_INFRA)
4858 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4859
4860 if (ssid->mode != WPAS_MODE_P2P_GO)
4861 return -1;
4862
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004863 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4864 if (freq < 0)
4865 return -1;
4866
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004867 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004868 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869
4870 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004871 params.psk_set = ssid->psk_set;
4872 if (params.psk_set)
4873 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004874 if (ssid->passphrase) {
4875 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4876 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4877 "persistent group");
4878 return -1;
4879 }
4880 os_strlcpy(params.passphrase, ssid->passphrase,
4881 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004882 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4884 params.ssid_len = ssid->ssid_len;
4885 params.persistent_group = 1;
4886
4887 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4888 if (wpa_s == NULL)
4889 return -1;
4890
Dmitry Shmidt56052862013-10-04 10:23:25 -07004891 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892 wpas_start_wps_go(wpa_s, &params, 0);
4893
4894 return 0;
4895}
4896
4897
4898static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4899 struct wpabuf *proberesp_ies)
4900{
4901 struct wpa_supplicant *wpa_s = ctx;
4902 if (wpa_s->ap_iface) {
4903 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004904 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4905 wpabuf_free(beacon_ies);
4906 wpabuf_free(proberesp_ies);
4907 return;
4908 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909 if (beacon_ies) {
4910 wpabuf_free(hapd->p2p_beacon_ie);
4911 hapd->p2p_beacon_ie = beacon_ies;
4912 }
4913 wpabuf_free(hapd->p2p_probe_resp_ie);
4914 hapd->p2p_probe_resp_ie = proberesp_ies;
4915 } else {
4916 wpabuf_free(beacon_ies);
4917 wpabuf_free(proberesp_ies);
4918 }
4919 wpa_supplicant_ap_update_beacon(wpa_s);
4920}
4921
4922
4923static void wpas_p2p_idle_update(void *ctx, int idle)
4924{
4925 struct wpa_supplicant *wpa_s = ctx;
4926 if (!wpa_s->ap_iface)
4927 return;
4928 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004929 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930 wpas_p2p_set_group_idle_timeout(wpa_s);
4931 else
4932 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4933}
4934
4935
4936struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004937 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004938{
4939 struct p2p_group *group;
4940 struct p2p_group_config *cfg;
4941
4942 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4943 return NULL;
4944 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4945 return NULL;
4946
4947 cfg = os_zalloc(sizeof(*cfg));
4948 if (cfg == NULL)
4949 return NULL;
4950
Dmitry Shmidt04949592012-07-19 12:16:46 -07004951 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004952 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004953 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004954 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4956 if (wpa_s->max_stations &&
4957 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4958 cfg->max_clients = wpa_s->max_stations;
4959 else
4960 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004961 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4962 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004963 cfg->cb_ctx = wpa_s;
4964 cfg->ie_update = wpas_p2p_ie_update;
4965 cfg->idle_update = wpas_p2p_idle_update;
4966
4967 group = p2p_group_init(wpa_s->global->p2p, cfg);
4968 if (group == NULL)
4969 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004970 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004971 p2p_group_notif_formation_done(group);
4972 wpa_s->p2p_group = group;
4973 return group;
4974}
4975
4976
4977void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4978 int registrar)
4979{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004980 struct wpa_ssid *ssid = wpa_s->current_ssid;
4981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004982 if (!wpa_s->p2p_in_provisioning) {
4983 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4984 "provisioning not in progress");
4985 return;
4986 }
4987
Dmitry Shmidt04949592012-07-19 12:16:46 -07004988 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4989 u8 go_dev_addr[ETH_ALEN];
4990 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4991 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4992 ssid->ssid_len);
4993 /* Clear any stored provisioning info */
4994 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4995 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4998 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004999 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005000 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5001 /*
5002 * Use a separate timeout for initial data connection to
5003 * complete to allow the group to be removed automatically if
5004 * something goes wrong in this step before the P2P group idle
5005 * timeout mechanism is taken into use.
5006 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005007 wpa_dbg(wpa_s, MSG_DEBUG,
5008 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5009 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005010 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5011 wpas_p2p_group_formation_timeout,
5012 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005013 } else if (ssid) {
5014 /*
5015 * Use a separate timeout for initial data connection to
5016 * complete to allow the group to be removed automatically if
5017 * the client does not complete data connection successfully.
5018 */
5019 wpa_dbg(wpa_s, MSG_DEBUG,
5020 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5021 P2P_MAX_INITIAL_CONN_WAIT_GO);
5022 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5023 wpas_p2p_group_formation_timeout,
5024 wpa_s->parent, NULL);
5025 /*
5026 * Complete group formation on first successful data connection
5027 */
5028 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005029 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 if (wpa_s->global->p2p)
5031 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5032 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5033 wpa_drv_wps_success_cb(wpa_s, peer_addr);
5034 wpas_group_formation_completed(wpa_s, 1);
5035}
5036
5037
Jouni Malinen75ecf522011-06-27 15:19:46 -07005038void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5039 struct wps_event_fail *fail)
5040{
5041 if (!wpa_s->p2p_in_provisioning) {
5042 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5043 "provisioning not in progress");
5044 return;
5045 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005046
5047 if (wpa_s->go_params) {
5048 p2p_clear_provisioning_info(
5049 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005050 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005051 }
5052
Jouni Malinen75ecf522011-06-27 15:19:46 -07005053 wpas_notify_p2p_wps_failed(wpa_s, fail);
5054}
5055
5056
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005058 const char *config_method,
5059 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005060{
5061 u16 config_methods;
5062
Dmitry Shmidt04949592012-07-19 12:16:46 -07005063 wpa_s->p2p_fallback_to_go_neg = 0;
5064 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005065 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005067 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005068 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005069 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5070 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005071 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005072 else {
5073 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005075 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005076
Dmitry Shmidt04949592012-07-19 12:16:46 -07005077 if (use == WPAS_P2P_PD_AUTO) {
5078 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5079 wpa_s->pending_pd_config_methods = config_methods;
5080 wpa_s->p2p_auto_pd = 1;
5081 wpa_s->p2p_auto_join = 0;
5082 wpa_s->pending_pd_before_join = 0;
5083 wpa_s->auto_pd_scan_retry = 0;
5084 wpas_p2p_stop_find(wpa_s);
5085 wpa_s->p2p_join_scan_count = 0;
5086 os_get_time(&wpa_s->p2p_auto_started);
5087 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5088 wpa_s->p2p_auto_started.sec,
5089 wpa_s->p2p_auto_started.usec);
5090 wpas_p2p_join_scan(wpa_s, NULL);
5091 return 0;
5092 }
5093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005094 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5095 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005096 config_methods,
5097 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 }
5099
5100 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5101 return -1;
5102
5103 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005104 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005105 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106}
5107
5108
5109int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5110 char *end)
5111{
5112 return p2p_scan_result_text(ies, ies_len, buf, end);
5113}
5114
5115
5116static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5117{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005118 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005119 return;
5120
5121 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5122 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005123 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005124}
5125
5126
5127int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5128 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005129 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005130 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131{
5132 wpas_p2p_clear_pending_action_tx(wpa_s);
5133 wpa_s->p2p_long_listen = 0;
5134
5135 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5136 return wpa_drv_p2p_find(wpa_s, timeout, type);
5137
Dmitry Shmidt04949592012-07-19 12:16:46 -07005138 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5139 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140 return -1;
5141
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005142 wpa_supplicant_cancel_sched_scan(wpa_s);
5143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005144 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005145 num_req_dev_types, req_dev_types, dev_id,
5146 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147}
5148
5149
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005150static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005151{
5152 wpas_p2p_clear_pending_action_tx(wpa_s);
5153 wpa_s->p2p_long_listen = 0;
5154 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5155 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07005156 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005157
5158 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5159 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005160 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005161 }
5162
5163 if (wpa_s->global->p2p)
5164 p2p_stop_find(wpa_s->global->p2p);
5165
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005166 return 0;
5167}
5168
5169
5170void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5171{
5172 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5173 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174 wpas_p2p_remove_pending_group_interface(wpa_s);
5175}
5176
5177
5178static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5179{
5180 struct wpa_supplicant *wpa_s = eloop_ctx;
5181 wpa_s->p2p_long_listen = 0;
5182}
5183
5184
5185int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5186{
5187 int res;
5188
5189 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5190 return -1;
5191
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005192 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005193 wpas_p2p_clear_pending_action_tx(wpa_s);
5194
5195 if (timeout == 0) {
5196 /*
5197 * This is a request for unlimited Listen state. However, at
5198 * least for now, this is mapped to a Listen state for one
5199 * hour.
5200 */
5201 timeout = 3600;
5202 }
5203 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5204 wpa_s->p2p_long_listen = 0;
5205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005206 /*
5207 * Stop previous find/listen operation to avoid trying to request a new
5208 * remain-on-channel operation while the driver is still running the
5209 * previous one.
5210 */
5211 if (wpa_s->global->p2p)
5212 p2p_stop_find(wpa_s->global->p2p);
5213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005214 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5215 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5216 wpa_s->p2p_long_listen = timeout * 1000;
5217 eloop_register_timeout(timeout, 0,
5218 wpas_p2p_long_listen_timeout,
5219 wpa_s, NULL);
5220 }
5221
5222 return res;
5223}
5224
5225
5226int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5227 u8 *buf, size_t len, int p2p_group)
5228{
5229 struct wpabuf *p2p_ie;
5230 int ret;
5231
5232 if (wpa_s->global->p2p_disabled)
5233 return -1;
5234 if (wpa_s->global->p2p == NULL)
5235 return -1;
5236 if (bss == NULL)
5237 return -1;
5238
5239 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5240 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5241 p2p_group, p2p_ie);
5242 wpabuf_free(p2p_ie);
5243
5244 return ret;
5245}
5246
5247
5248int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005249 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005250 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005251{
5252 if (wpa_s->global->p2p_disabled)
5253 return 0;
5254 if (wpa_s->global->p2p == NULL)
5255 return 0;
5256
Dmitry Shmidt04949592012-07-19 12:16:46 -07005257 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5258 ie, ie_len)) {
5259 case P2P_PREQ_NOT_P2P:
5260 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5261 ssi_signal);
5262 /* fall through */
5263 case P2P_PREQ_MALFORMED:
5264 case P2P_PREQ_NOT_LISTEN:
5265 case P2P_PREQ_NOT_PROCESSED:
5266 default: /* make gcc happy */
5267 return 0;
5268 case P2P_PREQ_PROCESSED:
5269 return 1;
5270 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005271}
5272
5273
5274void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5275 const u8 *sa, const u8 *bssid,
5276 u8 category, const u8 *data, size_t len, int freq)
5277{
5278 if (wpa_s->global->p2p_disabled)
5279 return;
5280 if (wpa_s->global->p2p == NULL)
5281 return;
5282
5283 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5284 freq);
5285}
5286
5287
5288void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5289{
5290 if (wpa_s->global->p2p_disabled)
5291 return;
5292 if (wpa_s->global->p2p == NULL)
5293 return;
5294
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005295 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005296}
5297
5298
5299void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5300{
5301 p2p_group_deinit(wpa_s->p2p_group);
5302 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005303
5304 wpa_s->ap_configured_cb = NULL;
5305 wpa_s->ap_configured_cb_ctx = NULL;
5306 wpa_s->ap_configured_cb_data = NULL;
5307 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005308}
5309
5310
5311int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5312{
5313 wpa_s->p2p_long_listen = 0;
5314
5315 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5316 return wpa_drv_p2p_reject(wpa_s, addr);
5317
5318 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5319 return -1;
5320
5321 return p2p_reject(wpa_s->global->p2p, addr);
5322}
5323
5324
5325/* Invite to reinvoke a persistent group */
5326int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005327 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005328 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005329{
5330 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005331 u8 *bssid = NULL;
5332 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005333 int res;
5334
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005335 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005336 if (peer_addr)
5337 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5338 else
5339 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005340
Jouni Malinen31be0a42012-08-31 21:20:51 +03005341 wpa_s->p2p_persistent_go_freq = freq;
5342 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343 if (ssid->mode == WPAS_MODE_P2P_GO) {
5344 role = P2P_INVITE_ROLE_GO;
5345 if (peer_addr == NULL) {
5346 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5347 "address in invitation command");
5348 return -1;
5349 }
5350 if (wpas_p2p_create_iface(wpa_s)) {
5351 if (wpas_p2p_add_group_interface(wpa_s,
5352 WPA_IF_P2P_GO) < 0) {
5353 wpa_printf(MSG_ERROR, "P2P: Failed to "
5354 "allocate a new interface for the "
5355 "group");
5356 return -1;
5357 }
5358 bssid = wpa_s->pending_interface_addr;
5359 } else
5360 bssid = wpa_s->own_addr;
5361 } else {
5362 role = P2P_INVITE_ROLE_CLIENT;
5363 peer_addr = ssid->bssid;
5364 }
5365 wpa_s->pending_invite_ssid_id = ssid->id;
5366
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005367 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5368 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005369 if (res)
5370 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005372 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5373 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5374 ssid->ssid, ssid->ssid_len,
5375 go_dev_addr, 1);
5376
5377 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5378 return -1;
5379
5380 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005381 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5382 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383}
5384
5385
5386/* Invite to join an active group */
5387int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5388 const u8 *peer_addr, const u8 *go_dev_addr)
5389{
5390 struct wpa_global *global = wpa_s->global;
5391 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005392 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005393 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005394 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005395 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005396 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005397
Jouni Malinen31be0a42012-08-31 21:20:51 +03005398 wpa_s->p2p_persistent_go_freq = 0;
5399 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005400 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005402 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5403 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5404 break;
5405 }
5406 if (wpa_s == NULL) {
5407 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5408 return -1;
5409 }
5410
5411 ssid = wpa_s->current_ssid;
5412 if (ssid == NULL) {
5413 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5414 "invitation");
5415 return -1;
5416 }
5417
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005418 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005419 persistent = ssid->p2p_persistent_group &&
5420 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5421 ssid->ssid, ssid->ssid_len);
5422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005423 if (ssid->mode == WPAS_MODE_P2P_GO) {
5424 role = P2P_INVITE_ROLE_ACTIVE_GO;
5425 bssid = wpa_s->own_addr;
5426 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005427 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005428 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005429 } else {
5430 role = P2P_INVITE_ROLE_CLIENT;
5431 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5432 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5433 "invite to current group");
5434 return -1;
5435 }
5436 bssid = wpa_s->bssid;
5437 if (go_dev_addr == NULL &&
5438 !is_zero_ether_addr(wpa_s->go_dev_addr))
5439 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005440 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5441 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442 }
5443 wpa_s->parent->pending_invite_ssid_id = -1;
5444
5445 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5446 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5447 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005448 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005449
5450 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5451 return -1;
5452
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005453 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5454 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005455 if (res)
5456 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005457 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005460 ssid->ssid, ssid->ssid_len, force_freq,
5461 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462}
5463
5464
5465void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5466{
5467 struct wpa_ssid *ssid = wpa_s->current_ssid;
5468 const char *ssid_txt;
5469 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005470 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005471 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005472 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473
Dmitry Shmidt04949592012-07-19 12:16:46 -07005474 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5475 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5476 wpa_s->parent, NULL);
5477 }
5478
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005479 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005480 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481
5482 wpa_s->show_group_started = 0;
5483
5484 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5485 os_memset(go_dev_addr, 0, ETH_ALEN);
5486 if (ssid->bssid_set)
5487 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5488 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5489 ssid->ssid_len);
5490 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5491
5492 if (wpa_s->global->p2p_group_formation == wpa_s)
5493 wpa_s->global->p2p_group_formation = NULL;
5494
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005495 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5496 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005497 if (ssid->passphrase == NULL && ssid->psk_set) {
5498 char psk[65];
5499 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005500 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5501 "%s client ssid=\"%s\" freq=%d psk=%s "
5502 "go_dev_addr=" MACSTR "%s",
5503 wpa_s->ifname, ssid_txt, freq, psk,
5504 MAC2STR(go_dev_addr),
5505 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005506 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005507 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5508 "%s client ssid=\"%s\" freq=%d "
5509 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5510 wpa_s->ifname, ssid_txt, freq,
5511 ssid->passphrase ? ssid->passphrase : "",
5512 MAC2STR(go_dev_addr),
5513 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005514 }
5515
5516 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005517 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5518 ssid, go_dev_addr);
5519 if (network_id < 0)
5520 network_id = ssid->id;
5521 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005522
5523done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005524 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005525}
5526
5527
5528int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5529 u32 interval1, u32 duration2, u32 interval2)
5530{
5531 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5532 return -1;
5533 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5534 return -1;
5535
5536 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5537 wpa_s->current_ssid == NULL ||
5538 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5539 return -1;
5540
5541 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5542 wpa_s->own_addr, wpa_s->assoc_freq,
5543 duration1, interval1, duration2, interval2);
5544}
5545
5546
5547int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5548 unsigned int interval)
5549{
5550 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5551 return -1;
5552
5553 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5554 return -1;
5555
5556 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5557}
5558
5559
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005560static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5561{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005562 if (wpa_s->current_ssid == NULL) {
5563 /*
5564 * current_ssid can be cleared when P2P client interface gets
5565 * disconnected, so assume this interface was used as P2P
5566 * client.
5567 */
5568 return 1;
5569 }
5570 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005571 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5572}
5573
5574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005575static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5576{
5577 struct wpa_supplicant *wpa_s = eloop_ctx;
5578
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005579 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5581 "disabled");
5582 return;
5583 }
5584
Dmitry Shmidt04949592012-07-19 12:16:46 -07005585 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5586 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005587 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005588}
5589
5590
5591static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5592{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005593 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005594
Dmitry Shmidt04949592012-07-19 12:16:46 -07005595 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5596 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005598 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5599 return;
5600
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005601 timeout = wpa_s->conf->p2p_group_idle;
5602 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5603 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5604 timeout = P2P_MAX_CLIENT_IDLE;
5605
5606 if (timeout == 0)
5607 return;
5608
Dmitry Shmidt04949592012-07-19 12:16:46 -07005609 if (timeout < 0) {
5610 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5611 timeout = 0; /* special client mode no-timeout */
5612 else
5613 return;
5614 }
5615
5616 if (wpa_s->p2p_in_provisioning) {
5617 /*
5618 * Use the normal group formation timeout during the
5619 * provisioning phase to avoid terminating this process too
5620 * early due to group idle timeout.
5621 */
5622 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5623 "during provisioning");
5624 return;
5625 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305626
Dmitry Shmidt04949592012-07-19 12:16:46 -07005627 if (wpa_s->show_group_started) {
5628 /*
5629 * Use the normal group formation timeout between the end of
5630 * the provisioning phase and completion of 4-way handshake to
5631 * avoid terminating this process too early due to group idle
5632 * timeout.
5633 */
5634 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5635 "while waiting for initial 4-way handshake to "
5636 "complete");
5637 return;
5638 }
5639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005640 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005641 timeout);
5642 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5643 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005644}
5645
5646
Jouni Malinen2b89da82012-08-31 22:04:41 +03005647/* Returns 1 if the interface was removed */
5648int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5649 u16 reason_code, const u8 *ie, size_t ie_len,
5650 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005651{
5652 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005653 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005654 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005655 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005656
Dmitry Shmidt04949592012-07-19 12:16:46 -07005657 if (!locally_generated)
5658 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5659 ie_len);
5660
5661 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5662 wpa_s->current_ssid &&
5663 wpa_s->current_ssid->p2p_group &&
5664 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5665 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5666 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005667 if (wpas_p2p_group_delete(wpa_s,
5668 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5669 > 0)
5670 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005671 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005672
5673 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674}
5675
5676
5677void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005678 u16 reason_code, const u8 *ie, size_t ie_len,
5679 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680{
5681 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5682 return;
5683 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5684 return;
5685
Dmitry Shmidt04949592012-07-19 12:16:46 -07005686 if (!locally_generated)
5687 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5688 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005689}
5690
5691
5692void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5693{
5694 struct p2p_data *p2p = wpa_s->global->p2p;
5695
5696 if (p2p == NULL)
5697 return;
5698
5699 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5700 return;
5701
5702 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5703 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5704
5705 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5706 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5707
5708 if (wpa_s->wps &&
5709 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5710 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5711
5712 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5713 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5714
5715 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5716 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5717 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5718 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5719 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5720 }
5721
5722 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5723 p2p_set_sec_dev_types(p2p,
5724 (void *) wpa_s->conf->sec_device_type,
5725 wpa_s->conf->num_sec_device_types);
5726
5727 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5728 int i;
5729 p2p_remove_wps_vendor_extensions(p2p);
5730 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5731 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5732 continue;
5733 p2p_add_wps_vendor_extension(
5734 p2p, wpa_s->conf->wps_vendor_ext[i]);
5735 }
5736 }
5737
5738 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5739 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5740 char country[3];
5741 country[0] = wpa_s->conf->country[0];
5742 country[1] = wpa_s->conf->country[1];
5743 country[2] = 0x04;
5744 p2p_set_country(p2p, country);
5745 }
5746
5747 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5748 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5749 wpa_s->conf->p2p_ssid_postfix ?
5750 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5751 0);
5752 }
5753
5754 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5755 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005756
5757 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5758 u8 reg_class, channel;
5759 int ret;
5760 unsigned int r;
5761 if (wpa_s->conf->p2p_listen_reg_class &&
5762 wpa_s->conf->p2p_listen_channel) {
5763 reg_class = wpa_s->conf->p2p_listen_reg_class;
5764 channel = wpa_s->conf->p2p_listen_channel;
5765 } else {
5766 reg_class = 81;
5767 /*
5768 * Pick one of the social channels randomly as the
5769 * listen channel.
5770 */
5771 os_get_random((u8 *) &r, sizeof(r));
5772 channel = 1 + (r % 3) * 5;
5773 }
5774 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5775 if (ret)
5776 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5777 "failed: %d", ret);
5778 }
5779 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5780 u8 op_reg_class, op_channel, cfg_op_channel;
5781 int ret = 0;
5782 unsigned int r;
5783 if (wpa_s->conf->p2p_oper_reg_class &&
5784 wpa_s->conf->p2p_oper_channel) {
5785 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5786 op_channel = wpa_s->conf->p2p_oper_channel;
5787 cfg_op_channel = 1;
5788 } else {
5789 op_reg_class = 81;
5790 /*
5791 * Use random operation channel from (1, 6, 11)
5792 *if no other preference is indicated.
5793 */
5794 os_get_random((u8 *) &r, sizeof(r));
5795 op_channel = 1 + (r % 3) * 5;
5796 cfg_op_channel = 0;
5797 }
5798 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5799 cfg_op_channel);
5800 if (ret)
5801 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5802 "failed: %d", ret);
5803 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005804
5805 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5806 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5807 wpa_s->conf->p2p_pref_chan) < 0) {
5808 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5809 "update failed");
5810 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005811
5812 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
5813 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
5814 "update failed");
5815 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005816 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005817}
5818
5819
5820int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5821 int duration)
5822{
5823 if (!wpa_s->ap_iface)
5824 return -1;
5825 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5826 duration);
5827}
5828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005829
5830int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5831{
5832 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5833 return -1;
5834 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5835 return -1;
5836
5837 wpa_s->global->cross_connection = enabled;
5838 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5839
5840 if (!enabled) {
5841 struct wpa_supplicant *iface;
5842
5843 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5844 {
5845 if (iface->cross_connect_enabled == 0)
5846 continue;
5847
5848 iface->cross_connect_enabled = 0;
5849 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005850 wpa_msg_global(iface->parent, MSG_INFO,
5851 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5852 iface->ifname,
5853 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005854 }
5855 }
5856
5857 return 0;
5858}
5859
5860
5861static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5862{
5863 struct wpa_supplicant *iface;
5864
5865 if (!uplink->global->cross_connection)
5866 return;
5867
5868 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5869 if (!iface->cross_connect_enabled)
5870 continue;
5871 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5872 0)
5873 continue;
5874 if (iface->ap_iface == NULL)
5875 continue;
5876 if (iface->cross_connect_in_use)
5877 continue;
5878
5879 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005880 wpa_msg_global(iface->parent, MSG_INFO,
5881 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5882 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005883 }
5884}
5885
5886
5887static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5888{
5889 struct wpa_supplicant *iface;
5890
5891 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5892 if (!iface->cross_connect_enabled)
5893 continue;
5894 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5895 0)
5896 continue;
5897 if (!iface->cross_connect_in_use)
5898 continue;
5899
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005900 wpa_msg_global(iface->parent, MSG_INFO,
5901 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5902 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005903 iface->cross_connect_in_use = 0;
5904 }
5905}
5906
5907
5908void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5909{
5910 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5911 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5912 wpa_s->cross_connect_disallowed)
5913 wpas_p2p_disable_cross_connect(wpa_s);
5914 else
5915 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005916 if (!wpa_s->ap_iface &&
5917 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5918 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005919}
5920
5921
5922void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5923{
5924 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005925 if (!wpa_s->ap_iface &&
5926 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5927 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005928 wpas_p2p_set_group_idle_timeout(wpa_s);
5929}
5930
5931
5932static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5933{
5934 struct wpa_supplicant *iface;
5935
5936 if (!wpa_s->global->cross_connection)
5937 return;
5938
5939 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5940 if (iface == wpa_s)
5941 continue;
5942 if (iface->drv_flags &
5943 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5944 continue;
5945 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5946 continue;
5947
5948 wpa_s->cross_connect_enabled = 1;
5949 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5950 sizeof(wpa_s->cross_connect_uplink));
5951 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5952 "%s to %s whenever uplink is available",
5953 wpa_s->ifname, wpa_s->cross_connect_uplink);
5954
5955 if (iface->ap_iface || iface->current_ssid == NULL ||
5956 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5957 iface->cross_connect_disallowed ||
5958 iface->wpa_state != WPA_COMPLETED)
5959 break;
5960
5961 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005962 wpa_msg_global(wpa_s->parent, MSG_INFO,
5963 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5964 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005965 break;
5966 }
5967}
5968
5969
5970int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5971{
5972 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5973 !wpa_s->p2p_in_provisioning)
5974 return 0; /* not P2P client operation */
5975
5976 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5977 "session overlap");
5978 if (wpa_s != wpa_s->parent)
5979 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005980 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005981 return 1;
5982}
5983
5984
5985void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5986{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005987 struct p2p_channels chan, cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005988
5989 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5990 return;
5991
5992 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005993 os_memset(&cli_chan, 0, sizeof(cli_chan));
5994 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005995 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5996 "channel list");
5997 return;
5998 }
5999
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006000 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006001}
6002
6003
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006004static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6005 struct wpa_scan_results *scan_res)
6006{
6007 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6008}
6009
6010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006011int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6012{
6013 struct wpa_global *global = wpa_s->global;
6014 int found = 0;
6015 const u8 *peer;
6016
6017 if (global->p2p == NULL)
6018 return -1;
6019
6020 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6021
6022 if (wpa_s->pending_interface_name[0] &&
6023 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6024 found = 1;
6025
6026 peer = p2p_get_go_neg_peer(global->p2p);
6027 if (peer) {
6028 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6029 MACSTR, MAC2STR(peer));
6030 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006031 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032 }
6033
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006034 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6035 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6036 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6037 found = 1;
6038 }
6039
6040 if (wpa_s->pending_pd_before_join) {
6041 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6042 wpa_s->pending_pd_before_join = 0;
6043 found = 1;
6044 }
6045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046 wpas_p2p_stop_find(wpa_s);
6047
6048 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6049 if (wpa_s == global->p2p_group_formation &&
6050 (wpa_s->p2p_in_provisioning ||
6051 wpa_s->parent->pending_interface_type ==
6052 WPA_IF_P2P_CLIENT)) {
6053 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6054 "formation found - cancelling",
6055 wpa_s->ifname);
6056 found = 1;
6057 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6058 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006059 if (wpa_s->p2p_in_provisioning) {
6060 wpas_group_formation_completed(wpa_s, 0);
6061 break;
6062 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006063 wpas_p2p_group_delete(wpa_s,
6064 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065 break;
6066 }
6067 }
6068
6069 if (!found) {
6070 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6071 return -1;
6072 }
6073
6074 return 0;
6075}
6076
6077
6078void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6079{
6080 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6081 return;
6082
6083 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6084 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006085 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006086}
6087
6088
6089void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6090 int freq_24, int freq_5, int freq_overall)
6091{
6092 struct p2p_data *p2p = wpa_s->global->p2p;
6093 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6094 return;
6095 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6096}
6097
6098
6099int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6100{
6101 u8 peer[ETH_ALEN];
6102 struct p2p_data *p2p = wpa_s->global->p2p;
6103
6104 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6105 return -1;
6106
6107 if (hwaddr_aton(addr, peer))
6108 return -1;
6109
6110 return p2p_unauthorize(p2p, peer);
6111}
6112
6113
6114/**
6115 * wpas_p2p_disconnect - Disconnect from a P2P Group
6116 * @wpa_s: Pointer to wpa_supplicant data
6117 * Returns: 0 on success, -1 on failure
6118 *
6119 * This can be used to disconnect from a group in which the local end is a P2P
6120 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6121 * virtual network interface was created for this group, that interface will be
6122 * removed. Otherwise, only the configured P2P group network will be removed
6123 * from the interface.
6124 */
6125int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6126{
6127
6128 if (wpa_s == NULL)
6129 return -1;
6130
Jouni Malinen2b89da82012-08-31 22:04:41 +03006131 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6132 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006133}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006134
6135
6136int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6137{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006138 int ret;
6139
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006140 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6141 return 0;
6142
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006143 ret = p2p_in_progress(wpa_s->global->p2p);
6144 if (ret == 0) {
6145 /*
6146 * Check whether there is an ongoing WPS provisioning step (or
6147 * other parts of group formation) on another interface since
6148 * p2p_in_progress() does not report this to avoid issues for
6149 * scans during such provisioning step.
6150 */
6151 if (wpa_s->global->p2p_group_formation &&
6152 wpa_s->global->p2p_group_formation != wpa_s) {
6153 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6154 "in group formation",
6155 wpa_s->global->p2p_group_formation->ifname);
6156 ret = 1;
6157 }
6158 }
6159
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006160 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6161 struct os_time now;
6162 os_get_time(&now);
6163 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
6164 P2P_MAX_INITIAL_CONN_WAIT_GO) {
6165 /* Wait for the first client has expired */
6166 wpa_s->global->p2p_go_wait_client.sec = 0;
6167 } else {
6168 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6169 ret = 1;
6170 }
6171 }
6172
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006173 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006174}
6175
6176
6177void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6178 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006179{
6180 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6181 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6182 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006183 /**
6184 * Remove the network by scheduling the group formation
6185 * timeout to happen immediately. The teardown code
6186 * needs to be scheduled to run asynch later so that we
6187 * don't delete data from under ourselves unexpectedly.
6188 * Calling wpas_p2p_group_formation_timeout directly
6189 * causes a series of crashes in WPS failure scenarios.
6190 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006191 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6192 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006193 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6194 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006195 }
6196}
6197
6198
6199struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006200 const u8 *addr, const u8 *ssid,
6201 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006202{
6203 struct wpa_ssid *s;
6204 size_t i;
6205
6206 for (s = wpa_s->conf->ssid; s; s = s->next) {
6207 if (s->disabled != 2)
6208 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006209 if (ssid &&
6210 (ssid_len != s->ssid_len ||
6211 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6212 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006213 if (addr == NULL) {
6214 if (s->mode == WPAS_MODE_P2P_GO)
6215 return s;
6216 continue;
6217 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006218 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6219 return s; /* peer is GO in the persistent group */
6220 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6221 continue;
6222 for (i = 0; i < s->num_p2p_clients; i++) {
6223 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6224 addr, ETH_ALEN) == 0)
6225 return s; /* peer is P2P client in persistent
6226 * group */
6227 }
6228 }
6229
6230 return NULL;
6231}
6232
6233
6234void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6235 const u8 *addr)
6236{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006237 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6238 wpa_s->parent, NULL) > 0) {
6239 /*
6240 * This can happen if WPS provisioning step is not terminated
6241 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6242 * peer was able to connect, there is no need to time out group
6243 * formation after this, though. In addition, this is used with
6244 * the initial connection wait on the GO as a separate formation
6245 * timeout and as such, expected to be hit after the initial WPS
6246 * provisioning step.
6247 */
6248 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6249 }
6250 if (!wpa_s->p2p_go_group_formation_completed) {
6251 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6252 wpa_s->p2p_go_group_formation_completed = 1;
6253 wpa_s->global->p2p_group_formation = NULL;
6254 wpa_s->p2p_in_provisioning = 0;
6255 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006256 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006257 if (addr == NULL)
6258 return;
6259 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6260}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006261
Dmitry Shmidt04949592012-07-19 12:16:46 -07006262
6263static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6264 int group_added)
6265{
6266 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006267 if (wpa_s->global->p2p_group_formation)
6268 group = wpa_s->global->p2p_group_formation;
6269 wpa_s = wpa_s->parent;
6270 offchannel_send_action_done(wpa_s);
6271 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006272 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006273 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6274 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6275 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6276 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6277 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006278 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006279 wpa_s->p2p_go_ht40,
6280 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006281}
6282
6283
6284int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6285{
6286 if (!wpa_s->p2p_fallback_to_go_neg ||
6287 wpa_s->p2p_in_provisioning <= 5)
6288 return 0;
6289
6290 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6291 return 0; /* peer operating as a GO */
6292
6293 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6294 "fallback to GO Negotiation");
6295 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6296
6297 return 1;
6298}
6299
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006300
6301unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6302{
6303 const char *rn, *rn2;
6304 struct wpa_supplicant *ifs;
6305
6306 if (wpa_s->wpa_state > WPA_SCANNING) {
6307 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6308 "concurrent operation",
6309 P2P_CONCURRENT_SEARCH_DELAY);
6310 return P2P_CONCURRENT_SEARCH_DELAY;
6311 }
6312
6313 if (!wpa_s->driver->get_radio_name)
6314 return 0;
6315 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
6316 if (rn == NULL || rn[0] == '\0')
6317 return 0;
6318
6319 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6320 if (ifs == wpa_s || !ifs->driver->get_radio_name)
6321 continue;
6322
6323 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
6324 if (!rn2 || os_strcmp(rn, rn2) != 0)
6325 continue;
6326 if (ifs->wpa_state > WPA_SCANNING) {
6327 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6328 "delay due to concurrent operation on "
6329 "interface %s",
6330 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6331 return P2P_CONCURRENT_SEARCH_DELAY;
6332 }
6333 }
6334
6335 return 0;
6336}
6337
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006338
6339void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6340{
6341 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6342 "pending anymore (sta_scan_pending=%d "
6343 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6344 wpa_s->global->p2p_cb_on_scan_complete);
6345 wpa_s->sta_scan_pending = 0;
6346
6347 if (!wpa_s->global->p2p_cb_on_scan_complete)
6348 return;
6349 wpa_s->global->p2p_cb_on_scan_complete = 0;
6350
6351 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6352 return;
6353
6354 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6355 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6356 "continued after successful connection");
6357 p2p_increase_search_delay(wpa_s->global->p2p,
6358 wpas_p2p_search_delay(wpa_s));
6359 }
6360}
6361
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006362
6363static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6364 struct wpa_ssid *s, const u8 *addr,
6365 int iface_addr)
6366{
6367 struct psk_list_entry *psk, *tmp;
6368 int changed = 0;
6369
6370 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6371 list) {
6372 if ((iface_addr && !psk->p2p &&
6373 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6374 (!iface_addr && psk->p2p &&
6375 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6376 wpa_dbg(wpa_s, MSG_DEBUG,
6377 "P2P: Remove persistent group PSK list entry for "
6378 MACSTR " p2p=%u",
6379 MAC2STR(psk->addr), psk->p2p);
6380 dl_list_del(&psk->list);
6381 os_free(psk);
6382 changed++;
6383 }
6384 }
6385
6386 return changed;
6387}
6388
6389
6390void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6391 const u8 *p2p_dev_addr,
6392 const u8 *psk, size_t psk_len)
6393{
6394 struct wpa_ssid *ssid = wpa_s->current_ssid;
6395 struct wpa_ssid *persistent;
6396 struct psk_list_entry *p;
6397
6398 if (psk_len != sizeof(p->psk))
6399 return;
6400
6401 if (p2p_dev_addr) {
6402 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6403 " p2p_dev_addr=" MACSTR,
6404 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6405 if (is_zero_ether_addr(p2p_dev_addr))
6406 p2p_dev_addr = NULL;
6407 } else {
6408 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6409 MAC2STR(mac_addr));
6410 }
6411
6412 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6413 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6414 /* To be added to persistent group once created */
6415 if (wpa_s->global->add_psk == NULL) {
6416 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6417 if (wpa_s->global->add_psk == NULL)
6418 return;
6419 }
6420 p = wpa_s->global->add_psk;
6421 if (p2p_dev_addr) {
6422 p->p2p = 1;
6423 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6424 } else {
6425 p->p2p = 0;
6426 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6427 }
6428 os_memcpy(p->psk, psk, psk_len);
6429 return;
6430 }
6431
6432 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6433 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6434 return;
6435 }
6436
6437 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6438 ssid->ssid_len);
6439 if (!persistent) {
6440 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6441 return;
6442 }
6443
6444 p = os_zalloc(sizeof(*p));
6445 if (p == NULL)
6446 return;
6447 if (p2p_dev_addr) {
6448 p->p2p = 1;
6449 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6450 } else {
6451 p->p2p = 0;
6452 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6453 }
6454 os_memcpy(p->psk, psk, psk_len);
6455
6456 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6457 struct psk_list_entry *last;
6458 last = dl_list_last(&persistent->psk_list,
6459 struct psk_list_entry, list);
6460 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6461 MACSTR " (p2p=%u) to make room for a new one",
6462 MAC2STR(last->addr), last->p2p);
6463 dl_list_del(&last->list);
6464 os_free(last);
6465 }
6466
6467 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6468 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6469 p2p_dev_addr == NULL);
6470 if (p2p_dev_addr) {
6471 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6472 MACSTR, MAC2STR(p2p_dev_addr));
6473 } else {
6474 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6475 MAC2STR(mac_addr));
6476 }
6477 dl_list_add(&persistent->psk_list, &p->list);
6478
6479#ifndef CONFIG_NO_CONFIG_WRITE
6480 if (wpa_s->parent->conf->update_config &&
6481 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6482 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6483#endif /* CONFIG_NO_CONFIG_WRITE */
6484}
6485
6486
6487static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6488 struct wpa_ssid *s, const u8 *addr,
6489 int iface_addr)
6490{
6491 int res;
6492
6493 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6494 if (res > 0) {
6495#ifndef CONFIG_NO_CONFIG_WRITE
6496 if (wpa_s->conf->update_config &&
6497 wpa_config_write(wpa_s->confname, wpa_s->conf))
6498 wpa_dbg(wpa_s, MSG_DEBUG,
6499 "P2P: Failed to update configuration");
6500#endif /* CONFIG_NO_CONFIG_WRITE */
6501 }
6502}
6503
6504
6505static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6506 const u8 *peer, int iface_addr)
6507{
6508 struct hostapd_data *hapd;
6509 struct hostapd_wpa_psk *psk, *prev, *rem;
6510 struct sta_info *sta;
6511
6512 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6513 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6514 return;
6515
6516 /* Remove per-station PSK entry */
6517 hapd = wpa_s->ap_iface->bss[0];
6518 prev = NULL;
6519 psk = hapd->conf->ssid.wpa_psk;
6520 while (psk) {
6521 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6522 (!iface_addr &&
6523 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6524 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6525 MACSTR " iface_addr=%d",
6526 MAC2STR(peer), iface_addr);
6527 if (prev)
6528 prev->next = psk->next;
6529 else
6530 hapd->conf->ssid.wpa_psk = psk->next;
6531 rem = psk;
6532 psk = psk->next;
6533 os_free(rem);
6534 } else {
6535 prev = psk;
6536 psk = psk->next;
6537 }
6538 }
6539
6540 /* Disconnect from group */
6541 if (iface_addr)
6542 sta = ap_get_sta(hapd, peer);
6543 else
6544 sta = ap_get_sta_p2p(hapd, peer);
6545 if (sta) {
6546 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6547 " (iface_addr=%d) from group",
6548 MAC2STR(peer), iface_addr);
6549 hostapd_drv_sta_deauth(hapd, sta->addr,
6550 WLAN_REASON_DEAUTH_LEAVING);
6551 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6552 }
6553}
6554
6555
6556void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6557 int iface_addr)
6558{
6559 struct wpa_ssid *s;
6560 struct wpa_supplicant *w;
6561
6562 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6563
6564 /* Remove from any persistent group */
6565 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6566 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6567 continue;
6568 if (!iface_addr)
6569 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6570 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6571 }
6572
6573 /* Remove from any operating group */
6574 for (w = wpa_s->global->ifaces; w; w = w->next)
6575 wpas_p2p_remove_client_go(w, peer, iface_addr);
6576}
6577
6578
6579static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6580{
6581 struct wpa_supplicant *wpa_s = eloop_ctx;
6582 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6583}
6584
6585
6586int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6587{
6588 struct wpa_ssid *ssid = wpa_s->current_ssid;
6589
6590 if (ssid == NULL || !ssid->p2p_group)
6591 return 0;
6592
6593 if (wpa_s->p2p_last_4way_hs_fail &&
6594 wpa_s->p2p_last_4way_hs_fail == ssid) {
6595 u8 go_dev_addr[ETH_ALEN];
6596 struct wpa_ssid *persistent;
6597
6598 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6599 ssid->ssid,
6600 ssid->ssid_len) <= 0) {
6601 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6602 goto disconnect;
6603 }
6604
6605 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6606 MACSTR, MAC2STR(go_dev_addr));
6607 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6608 ssid->ssid,
6609 ssid->ssid_len);
6610 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6611 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6612 goto disconnect;
6613 }
6614 wpa_msg_global(wpa_s->parent, MSG_INFO,
6615 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6616 persistent->id);
6617 disconnect:
6618 wpa_s->p2p_last_4way_hs_fail = NULL;
6619 /*
6620 * Remove the group from a timeout to avoid issues with caller
6621 * continuing to use the interface if this is on a P2P group
6622 * interface.
6623 */
6624 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6625 wpa_s, NULL);
6626 return 1;
6627 }
6628
6629 wpa_s->p2p_last_4way_hs_fail = ssid;
6630 return 0;
6631}
6632
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006633#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006634static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6635{
6636 struct wpa_supplicant *wpa_s = eloop_ctx;
6637
6638 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6639 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6640}
6641
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006642int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6643 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006644{
6645 struct wpa_supplicant *iface = NULL;
6646 struct p2p_data *p2p = wpa_s->global->p2p;
6647
6648 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006649 if ((iface->current_ssid) &&
6650 (iface->current_ssid->frequency != freq) &&
6651 ((iface->p2p_group_interface) ||
6652 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006653
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006654 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
6655 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
6656 /* Try to see whether we can move the GO. If it
6657 * is not possible, remove the GO interface
6658 */
6659 if (wpa_drv_switch_channel(iface, freq) == 0) {
6660 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
6661 iface->current_ssid->frequency = freq;
6662 continue;
6663 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006664 }
6665
6666 /* If GO cannot be moved or if the conflicting interface is a
6667 * P2P Client, remove the interface depending up on the connection
6668 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006669 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07006670 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006671 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006672 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006673 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006674 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
6675 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006676 /* If connection in progress is p2p connection, do not proceed for the connection */
6677 if (wpa_s == iface)
6678 return -1;
6679 else
6680 /* If connection in progress is STA connection, proceed for the connection */
6681 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006682 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006683 /* P2p connection has priority, disable the STA network*/
6684 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
6685 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6686 " id=%d", ssid->id);
6687 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6688 if (wpa_s == iface) {
6689 /* p2p connection is in progress, continue connecting...*/
6690 return 0;
6691 }
6692 else {
6693 /* STA connection is in progress, do not allow to continue */
6694 return -1;
6695 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006696 }
6697 }
6698 }
6699 return 0;
6700}
6701#endif