blob: 66849d37adf7da9b90567ec25d86739b16e72f85 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "common/ieee802_11_common.h"
14#include "common/ieee802_11_defs.h"
15#include "common/wpa_ctrl.h"
16#include "wps/wps_i.h"
17#include "p2p/p2p.h"
18#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080019#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070020#include "ap/sta_info.h"
21#include "ap/ap_drv_ops.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070023#include "eapol_supp/eapol_supp_sm.h"
24#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "wpa_supplicant_i.h"
26#include "driver_i.h"
27#include "ap.h"
28#include "config_ssid.h"
29#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "notify.h"
31#include "scan.h"
32#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "wps_supplicant.h"
35#include "p2p_supplicant.h"
36
37
38/*
39 * How many times to try to scan to find the GO before giving up on join
40 * request.
41 */
42#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
43
Dmitry Shmidt04949592012-07-19 12:16:46 -070044#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
45
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080046#ifndef P2P_MAX_CLIENT_IDLE
47/*
48 * How many seconds to try to reconnect to the GO when connection in P2P client
49 * role has been lost.
50 */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070051#ifdef ANDROID_P2P
52#define P2P_MAX_CLIENT_IDLE 20
53#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054#define P2P_MAX_CLIENT_IDLE 10
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070055#endif /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056#endif /* P2P_MAX_CLIENT_IDLE */
57
Dmitry Shmidt04949592012-07-19 12:16:46 -070058#ifndef P2P_MAX_INITIAL_CONN_WAIT
59/*
60 * How many seconds to wait for initial 4-way handshake to get completed after
61 * WPS provisioning step.
62 */
63#define P2P_MAX_INITIAL_CONN_WAIT 10
64#endif /* P2P_MAX_INITIAL_CONN_WAIT */
65
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070066#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
67/*
68 * How many seconds to wait for initial 4-way handshake to get completed after
69 * WPS provisioning step on the GO. This controls the extra time the P2P
70 * operation is considered to be in progress (e.g., to delay other scans) after
71 * WPS provisioning has been completed on the GO during group formation.
72 */
73#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
74#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
75
Dmitry Shmidt56052862013-10-04 10:23:25 -070076#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
77/*
78 * How many seconds to wait for initial 4-way handshake to get completed after
79 * re-invocation of a persistent group on the GO when the client is expected
80 * to connect automatically (no user interaction).
81 */
82#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
83#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
84
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070085#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070086#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087#endif /* P2P_CONCURRENT_SEARCH_DELAY */
88
Dmitry Shmidt34af3062013-07-11 10:46:32 -070089#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
90
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070091enum p2p_group_removal_reason {
92 P2P_GROUP_REMOVAL_UNKNOWN,
93 P2P_GROUP_REMOVAL_SILENT,
94 P2P_GROUP_REMOVAL_FORMATION_FAILED,
95 P2P_GROUP_REMOVAL_REQUESTED,
96 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
97 P2P_GROUP_REMOVAL_UNAVAILABLE,
98 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070099 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700100#ifdef ANDROID_P2P
101 P2P_GROUP_REMOVAL_FREQ_CONFLICT
102#endif
103};
104
Jouni Malinendc7b7132012-09-14 12:53:47 -0700105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
107static struct wpa_supplicant *
108wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
109 int go);
110static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700111static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
113static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700114 const u8 *dev_addr, enum p2p_wps_method wps_method,
115 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
117static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
118static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
119static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800120static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
121 void *timeout_ctx);
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700122#ifdef ANDROID_P2P
123static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
124#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700125static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
126 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800127static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128
129
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700130/*
131 * Get the number of concurrent channels that the HW can operate, but that are
132 * currently not in use by any of the wpa_supplicant interfaces.
133 */
134static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
135{
136 int *freqs;
137 int num;
138
139 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
140 if (!freqs)
141 return -1;
142
143 num = get_shared_radio_freqs(wpa_s, freqs,
144 wpa_s->num_multichan_concurrent);
145 os_free(freqs);
146
147 return wpa_s->num_multichan_concurrent - num;
148}
149
150
151/*
152 * Get the frequencies that are currently in use by one or more of the virtual
153 * interfaces, and that are also valid for P2P operation.
154 */
155static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
156 int *p2p_freqs, unsigned int len)
157{
158 int *freqs;
159 unsigned int num, i, j;
160
161 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
162 if (!freqs)
163 return -1;
164
165 num = get_shared_radio_freqs(wpa_s, freqs,
166 wpa_s->num_multichan_concurrent);
167
168 os_memset(p2p_freqs, 0, sizeof(int) * len);
169
170 for (i = 0, j = 0; i < num && j < len; i++) {
171 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
172 p2p_freqs[j++] = freqs[i];
173 }
174
175 os_free(freqs);
176
177 return j;
178}
179
180
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700181static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
182 int freq)
183{
184 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
185 return;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700186 if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
187 wpas_p2p_num_unused_channels(wpa_s) > 0 &&
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700188 wpa_s->parent->conf->p2p_ignore_shared_freq)
189 freq = 0;
190 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
191}
192
193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
195 struct wpa_scan_results *scan_res)
196{
197 size_t i;
198
199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
200 return;
201
202 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
203 (int) scan_res->num);
204
205 for (i = 0; i < scan_res->num; i++) {
206 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800207 struct os_time time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700208 const u8 *ies;
209 size_t ies_len;
210
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800211 time_tmp_age.sec = bss->age / 1000;
212 time_tmp_age.usec = (bss->age % 1000) * 1000;
213 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700214
215 ies = (const u8 *) (bss + 1);
216 ies_len = bss->ie_len;
217 if (bss->beacon_ie_len > 0 &&
218 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
219 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
220 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
221 MACSTR, MAC2STR(bss->bssid));
222 ies = ies + ies_len;
223 ies_len = bss->beacon_ie_len;
224 }
225
226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800228 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700229 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 break;
231 }
232
233 p2p_scan_res_handled(wpa_s->global->p2p);
234}
235
236
237static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
238 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700239 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240{
241 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700242 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 struct wpa_driver_scan_params params;
244 int ret;
245 struct wpabuf *wps_ie, *ies;
246 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248
249 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
250 return -1;
251
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700252 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
253 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700254 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700255 wpas_p2p_in_progress(wpa_s) == 2) {
256 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
257 "pending station mode scan to be "
258 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700259 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700260 wpa_supplicant_req_scan(ifs, 0, 0);
261 return 1;
262 }
263 }
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 os_memset(&params, 0, sizeof(params));
266
267 /* P2P Wildcard SSID */
268 params.num_ssids = 1;
269 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
270 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
271
272 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700273 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
274 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 num_req_dev_types, req_dev_types);
276 if (wps_ie == NULL)
277 return -1;
278
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800279 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
280 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 if (ies == NULL) {
282 wpabuf_free(wps_ie);
283 return -1;
284 }
285 wpabuf_put_buf(ies, wps_ie);
286 wpabuf_free(wps_ie);
287
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800288 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800290 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 params.extra_ies = wpabuf_head(ies);
292 params.extra_ies_len = wpabuf_len(ies);
293
294 switch (type) {
295 case P2P_SCAN_SOCIAL:
296 params.freqs = social_channels;
297 break;
298 case P2P_SCAN_FULL:
299 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 case P2P_SCAN_SOCIAL_PLUS_ONE:
301 social_channels[3] = freq;
302 params.freqs = social_channels;
303 break;
304 }
305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307
308 wpabuf_free(ies);
309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800310 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700311 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
312 if (ifs->scanning ||
313 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
314 wpa_s->global->p2p_cb_on_scan_complete = 1;
315 ret = 1;
316 break;
317 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800318 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700319 } else {
320 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700321 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700322 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 return ret;
325}
326
327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
329{
330 switch (p2p_group_interface) {
331 case P2P_GROUP_INTERFACE_PENDING:
332 return WPA_IF_P2P_GROUP;
333 case P2P_GROUP_INTERFACE_GO:
334 return WPA_IF_P2P_GO;
335 case P2P_GROUP_INTERFACE_CLIENT:
336 return WPA_IF_P2P_CLIENT;
337 }
338
339 return WPA_IF_P2P_GROUP;
340}
341
342
343static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
344 const u8 *ssid,
345 size_t ssid_len, int *go)
346{
347 struct wpa_ssid *s;
348
349 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
350 for (s = wpa_s->conf->ssid; s; s = s->next) {
351 if (s->disabled != 0 || !s->p2p_group ||
352 s->ssid_len != ssid_len ||
353 os_memcmp(ssid, s->ssid, ssid_len) != 0)
354 continue;
355 if (s->mode == WPAS_MODE_P2P_GO &&
356 s != wpa_s->current_ssid)
357 continue;
358 if (go)
359 *go = s->mode == WPAS_MODE_P2P_GO;
360 return wpa_s;
361 }
362 }
363
364 return NULL;
365}
366
367
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700368static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
369 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370{
371 struct wpa_ssid *ssid;
372 char *gtype;
373 const char *reason;
374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 ssid = wpa_s->current_ssid;
376 if (ssid == NULL) {
377 /*
378 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700379 * pending P2P group interface waiting for provisioning or a
380 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 */
382 ssid = wpa_s->conf->ssid;
383 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700384 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 break;
386 ssid = ssid->next;
387 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300388 if (ssid == NULL &&
389 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
390 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700391 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
392 "not found");
393 return -1;
394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395 }
396 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
397 gtype = "GO";
398 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
399 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
400 wpa_s->reassociate = 0;
401 wpa_s->disconnected = 1;
402 wpa_supplicant_deauthenticate(wpa_s,
403 WLAN_REASON_DEAUTH_LEAVING);
404 gtype = "client";
405 } else
406 gtype = "GO";
407 if (wpa_s->cross_connect_in_use) {
408 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700409 wpa_msg_global(wpa_s->parent, MSG_INFO,
410 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
411 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700413 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 case P2P_GROUP_REMOVAL_REQUESTED:
415 reason = " reason=REQUESTED";
416 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700417 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
418 reason = " reason=FORMATION_FAILED";
419 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
421 reason = " reason=IDLE";
422 break;
423 case P2P_GROUP_REMOVAL_UNAVAILABLE:
424 reason = " reason=UNAVAILABLE";
425 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700426 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
427 reason = " reason=GO_ENDING_SESSION";
428 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700429 case P2P_GROUP_REMOVAL_PSK_FAILURE:
430 reason = " reason=PSK_FAILURE";
431 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700432#ifdef ANDROID_P2P
433 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
434 reason = " reason=FREQ_CONFLICT";
435 break;
436#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 default:
438 reason = "";
439 break;
440 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700441 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700442 wpa_msg_global(wpa_s->parent, MSG_INFO,
443 P2P_EVENT_GROUP_REMOVED "%s %s%s",
444 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700445 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700447#ifdef ANDROID_P2P
448 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
449#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700450 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
451 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800452 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700453 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800454 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
455 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700456 wpa_s->p2p_in_provisioning = 0;
457 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700458
Dmitry Shmidt96571392013-10-14 12:54:46 -0700459 /*
460 * Make sure wait for the first client does not remain active after the
461 * group has been removed.
462 */
463 wpa_s->global->p2p_go_wait_client.sec = 0;
464
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
467
468 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
469 struct wpa_global *global;
470 char *ifname;
471 enum wpa_driver_if_type type;
472 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
473 wpa_s->ifname);
474 global = wpa_s->global;
475 ifname = os_strdup(wpa_s->ifname);
476 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700477 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 wpa_s = global->ifaces;
479 if (wpa_s && ifname)
480 wpa_drv_if_remove(wpa_s, type, ifname);
481 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300482 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 }
484
Dmitry Shmidt96571392013-10-14 12:54:46 -0700485 if (!wpa_s->p2p_go_group_formation_completed) {
486 wpa_s->global->p2p_group_formation = NULL;
487 wpa_s->p2p_in_provisioning = 0;
488 }
489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
491 if (ssid && (ssid->p2p_group ||
492 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
493 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
494 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700495 if (ssid == wpa_s->current_ssid) {
496 wpa_sm_set_config(wpa_s->wpa, NULL);
497 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700499 }
500 /*
501 * Networks objects created during any P2P activities are not
502 * exposed out as they might/will confuse certain non-P2P aware
503 * applications since these network objects won't behave like
504 * regular ones.
505 *
506 * Likewise, we don't send out network removed signals for such
507 * network objects.
508 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509 wpa_config_remove_network(wpa_s->conf, id);
510 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800511 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700512 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 } else {
514 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
515 "found");
516 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700517 if (wpa_s->ap_iface)
518 wpa_supplicant_ap_deinit(wpa_s);
519 else
520 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700521
522 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523}
524
525
526static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
527 u8 *go_dev_addr,
528 const u8 *ssid, size_t ssid_len)
529{
530 struct wpa_bss *bss;
531 const u8 *bssid;
532 struct wpabuf *p2p;
533 u8 group_capab;
534 const u8 *addr;
535
536 if (wpa_s->go_params)
537 bssid = wpa_s->go_params->peer_interface_addr;
538 else
539 bssid = wpa_s->bssid;
540
541 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
542 if (bss == NULL) {
543 u8 iface_addr[ETH_ALEN];
544 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
545 iface_addr) == 0)
546 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
547 }
548 if (bss == NULL) {
549 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
550 "group is persistent - BSS " MACSTR " not found",
551 MAC2STR(bssid));
552 return 0;
553 }
554
555 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700556 if (p2p == NULL)
557 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
558 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559 if (p2p == NULL) {
560 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
561 "group is persistent - BSS " MACSTR
562 " did not include P2P IE", MAC2STR(bssid));
563 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
564 (u8 *) (bss + 1), bss->ie_len);
565 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
566 ((u8 *) bss + 1) + bss->ie_len,
567 bss->beacon_ie_len);
568 return 0;
569 }
570
571 group_capab = p2p_get_group_capab(p2p);
572 addr = p2p_get_go_dev_addr(p2p);
573 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
574 "group_capab=0x%x", group_capab);
575 if (addr) {
576 os_memcpy(go_dev_addr, addr, ETH_ALEN);
577 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
578 MAC2STR(addr));
579 } else
580 os_memset(go_dev_addr, 0, ETH_ALEN);
581 wpabuf_free(p2p);
582
583 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
584 "go_dev_addr=" MACSTR,
585 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
586
587 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
588}
589
590
Jouni Malinen75ecf522011-06-27 15:19:46 -0700591static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
592 struct wpa_ssid *ssid,
593 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594{
595 struct wpa_ssid *s;
596 int changed = 0;
597
598 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
599 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
600 for (s = wpa_s->conf->ssid; s; s = s->next) {
601 if (s->disabled == 2 &&
602 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
603 s->ssid_len == ssid->ssid_len &&
604 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
605 break;
606 }
607
608 if (s) {
609 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
610 "entry");
611 if (ssid->passphrase && !s->passphrase)
612 changed = 1;
613 else if (ssid->passphrase && s->passphrase &&
614 os_strcmp(ssid->passphrase, s->passphrase) != 0)
615 changed = 1;
616 } else {
617 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
618 "entry");
619 changed = 1;
620 s = wpa_config_add_network(wpa_s->conf);
621 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700622 return -1;
623
624 /*
625 * Instead of network_added we emit persistent_group_added
626 * notification. Also to keep the defense checks in
627 * persistent_group obj registration method, we set the
628 * relevant flags in s to designate it as a persistent group.
629 */
630 s->p2p_group = 1;
631 s->p2p_persistent_group = 1;
632 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633 wpa_config_set_network_defaults(s);
634 }
635
636 s->p2p_group = 1;
637 s->p2p_persistent_group = 1;
638 s->disabled = 2;
639 s->bssid_set = 1;
640 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
641 s->mode = ssid->mode;
642 s->auth_alg = WPA_AUTH_ALG_OPEN;
643 s->key_mgmt = WPA_KEY_MGMT_PSK;
644 s->proto = WPA_PROTO_RSN;
645 s->pairwise_cipher = WPA_CIPHER_CCMP;
646 s->export_keys = 1;
647 if (ssid->passphrase) {
648 os_free(s->passphrase);
649 s->passphrase = os_strdup(ssid->passphrase);
650 }
651 if (ssid->psk_set) {
652 s->psk_set = 1;
653 os_memcpy(s->psk, ssid->psk, 32);
654 }
655 if (s->passphrase && !s->psk_set)
656 wpa_config_update_psk(s);
657 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
658 os_free(s->ssid);
659 s->ssid = os_malloc(ssid->ssid_len);
660 }
661 if (s->ssid) {
662 s->ssid_len = ssid->ssid_len;
663 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
664 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700665 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
666 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
667 wpa_s->global->add_psk = NULL;
668 changed = 1;
669 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670
671#ifndef CONFIG_NO_CONFIG_WRITE
672 if (changed && wpa_s->conf->update_config &&
673 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
674 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
675 }
676#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700677
678 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679}
680
681
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800682static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
683 const u8 *addr)
684{
685 struct wpa_ssid *ssid, *s;
686 u8 *n;
687 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700688 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800689
690 ssid = wpa_s->current_ssid;
691 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
692 !ssid->p2p_persistent_group)
693 return;
694
695 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
696 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
697 continue;
698
699 if (s->ssid_len == ssid->ssid_len &&
700 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
701 break;
702 }
703
704 if (s == NULL)
705 return;
706
707 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
708 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700709 ETH_ALEN) != 0)
710 continue;
711
712 if (i == s->num_p2p_clients - 1)
713 return; /* already the most recent entry */
714
715 /* move the entry to mark it most recent */
716 os_memmove(s->p2p_client_list + i * ETH_ALEN,
717 s->p2p_client_list + (i + 1) * ETH_ALEN,
718 (s->num_p2p_clients - i - 1) * ETH_ALEN);
719 os_memcpy(s->p2p_client_list +
720 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
721 found = 1;
722 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800723 }
724
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700725 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
726 n = os_realloc_array(s->p2p_client_list,
727 s->num_p2p_clients + 1, ETH_ALEN);
728 if (n == NULL)
729 return;
730 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
731 s->p2p_client_list = n;
732 s->num_p2p_clients++;
733 } else if (!found) {
734 /* Not enough room for an additional entry - drop the oldest
735 * entry */
736 os_memmove(s->p2p_client_list,
737 s->p2p_client_list + ETH_ALEN,
738 (s->num_p2p_clients - 1) * ETH_ALEN);
739 os_memcpy(s->p2p_client_list +
740 (s->num_p2p_clients - 1) * ETH_ALEN,
741 addr, ETH_ALEN);
742 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800743
744#ifndef CONFIG_NO_CONFIG_WRITE
745 if (wpa_s->parent->conf->update_config &&
746 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
747 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
748#endif /* CONFIG_NO_CONFIG_WRITE */
749}
750
751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
753 int success)
754{
755 struct wpa_ssid *ssid;
756 const char *ssid_txt;
757 int client;
758 int persistent;
759 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700760 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761
762 /*
763 * This callback is likely called for the main interface. Update wpa_s
764 * to use the group interface if a new interface was created for the
765 * group.
766 */
767 if (wpa_s->global->p2p_group_formation)
768 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700769 if (wpa_s->p2p_go_group_formation_completed) {
770 wpa_s->global->p2p_group_formation = NULL;
771 wpa_s->p2p_in_provisioning = 0;
772 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773
774 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700775 wpa_msg_global(wpa_s->parent, MSG_INFO,
776 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700777 wpas_p2p_group_delete(wpa_s,
778 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 return;
780 }
781
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700782 wpa_msg_global(wpa_s->parent, MSG_INFO,
783 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784
785 ssid = wpa_s->current_ssid;
786 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
787 ssid->mode = WPAS_MODE_P2P_GO;
788 p2p_group_notif_formation_done(wpa_s->p2p_group);
789 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
790 }
791
792 persistent = 0;
793 if (ssid) {
794 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
795 client = ssid->mode == WPAS_MODE_INFRA;
796 if (ssid->mode == WPAS_MODE_P2P_GO) {
797 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700798 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
799 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 } else
801 persistent = wpas_p2p_persistent_group(wpa_s,
802 go_dev_addr,
803 ssid->ssid,
804 ssid->ssid_len);
805 } else {
806 ssid_txt = "";
807 client = wpa_s->p2p_group_interface ==
808 P2P_GROUP_INTERFACE_CLIENT;
809 os_memset(go_dev_addr, 0, ETH_ALEN);
810 }
811
812 wpa_s->show_group_started = 0;
813 if (client) {
814 /*
815 * Indicate event only after successfully completed 4-way
816 * handshake, i.e., when the interface is ready for data
817 * packets.
818 */
819 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700820#ifdef ANDROID_P2P
821 /* For client Second phase of Group formation (4-way handshake) can be still pending
822 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700823 wpa_printf(MSG_INFO, "Restoring back wpa_s->global->p2p_group_formation to wpa_s %p\n", wpa_s);
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700824 wpa_s->global->p2p_group_formation = wpa_s;
825#endif
826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
828 char psk[65];
829 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700830 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
831 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
832 MACSTR "%s",
833 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
834 MAC2STR(go_dev_addr),
835 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 wpas_p2p_cross_connect_setup(wpa_s);
837 wpas_p2p_set_group_idle_timeout(wpa_s);
838 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700839 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
840 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
841 "go_dev_addr=" MACSTR "%s",
842 wpa_s->ifname, ssid_txt,
843 ssid ? ssid->frequency : 0,
844 ssid && ssid->passphrase ? ssid->passphrase : "",
845 MAC2STR(go_dev_addr),
846 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 wpas_p2p_cross_connect_setup(wpa_s);
848 wpas_p2p_set_group_idle_timeout(wpa_s);
849 }
850
851 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700852 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
853 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700854 else {
855 os_free(wpa_s->global->add_psk);
856 wpa_s->global->add_psk = NULL;
857 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800858 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700859 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700860 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700861 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700862 os_get_time(&wpa_s->global->p2p_go_wait_client);
863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864}
865
866
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
868 unsigned int freq,
869 const u8 *dst, const u8 *src,
870 const u8 *bssid,
871 const u8 *data, size_t data_len,
872 enum offchannel_send_action_result
873 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800875 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
878 return;
879 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
880 return;
881
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800882 switch (result) {
883 case OFFCHANNEL_SEND_ACTION_SUCCESS:
884 res = P2P_SEND_ACTION_SUCCESS;
885 break;
886 case OFFCHANNEL_SEND_ACTION_NO_ACK:
887 res = P2P_SEND_ACTION_NO_ACK;
888 break;
889 case OFFCHANNEL_SEND_ACTION_FAILED:
890 res = P2P_SEND_ACTION_FAILED;
891 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 }
893
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800894 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800896 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
897 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800898 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800899 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
900 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800902 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
903 "during p2p_connect-auto");
904 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
905 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 }
907}
908
909
910static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
911 const u8 *src, const u8 *bssid, const u8 *buf,
912 size_t len, unsigned int wait_time)
913{
914 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800915 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
916 wait_time,
917 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918}
919
920
921static void wpas_send_action_done(void *ctx)
922{
923 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800924 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925}
926
927
928static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
929 struct p2p_go_neg_results *params)
930{
931 if (wpa_s->go_params == NULL) {
932 wpa_s->go_params = os_malloc(sizeof(*params));
933 if (wpa_s->go_params == NULL)
934 return -1;
935 }
936 os_memcpy(wpa_s->go_params, params, sizeof(*params));
937 return 0;
938}
939
940
941static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
942 struct p2p_go_neg_results *res)
943{
944 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
945 MAC2STR(res->peer_interface_addr));
946 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
947 res->ssid, res->ssid_len);
948 wpa_supplicant_ap_deinit(wpa_s);
949 wpas_copy_go_neg_results(wpa_s, res);
950 if (res->wps_method == WPS_PBC)
951 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
952 else {
953 u16 dev_pw_id = DEV_PW_DEFAULT;
954 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
955 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
956 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
957 wpa_s->p2p_pin, 1, dev_pw_id);
958 }
959}
960
961
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700962static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
963 struct wpa_ssid *ssid)
964{
965 struct wpa_ssid *persistent;
966 struct psk_list_entry *psk;
967 struct hostapd_data *hapd;
968
969 if (!wpa_s->ap_iface)
970 return;
971
972 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
973 ssid->ssid_len);
974 if (persistent == NULL)
975 return;
976
977 hapd = wpa_s->ap_iface->bss[0];
978
979 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
980 list) {
981 struct hostapd_wpa_psk *hpsk;
982
983 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
984 MACSTR " psk=%d",
985 MAC2STR(psk->addr), psk->p2p);
986 hpsk = os_zalloc(sizeof(*hpsk));
987 if (hpsk == NULL)
988 break;
989 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
990 if (psk->p2p)
991 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
992 else
993 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
994 hpsk->next = hapd->conf->ssid.wpa_psk;
995 hapd->conf->ssid.wpa_psk = hpsk;
996 }
997}
998
999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000static void p2p_go_configured(void *ctx, void *data)
1001{
1002 struct wpa_supplicant *wpa_s = ctx;
1003 struct p2p_go_neg_results *params = data;
1004 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001005 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006
1007 ssid = wpa_s->current_ssid;
1008 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1009 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1010 if (wpa_s->global->p2p_group_formation == wpa_s)
1011 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001012 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001013 wpa_msg_global(wpa_s->parent, MSG_INFO,
1014 P2P_EVENT_GROUP_STARTED
1015 "%s GO ssid=\"%s\" freq=%d "
1016 "passphrase=\"%s\" go_dev_addr=" MACSTR
1017 "%s", wpa_s->ifname,
1018 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1019 ssid->frequency, params->passphrase,
1020 MAC2STR(wpa_s->global->p2p_dev_addr),
1021 params->persistent_group ?
1022 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001023 } else {
1024 char psk[65];
1025 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1026 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001027 wpa_msg_global(wpa_s->parent, MSG_INFO,
1028 P2P_EVENT_GROUP_STARTED
1029 "%s GO ssid=\"%s\" freq=%d psk=%s "
1030 "go_dev_addr=" MACSTR "%s",
1031 wpa_s->ifname,
1032 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1033 ssid->frequency, psk,
1034 MAC2STR(wpa_s->global->p2p_dev_addr),
1035 params->persistent_group ?
1036 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001037 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001038
Dmitry Shmidt56052862013-10-04 10:23:25 -07001039 os_get_time(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001040 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001041 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001043 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001044 wpas_p2p_add_psk_list(wpa_s, ssid);
1045 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001046 if (network_id < 0)
1047 network_id = ssid->id;
1048 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 wpas_p2p_cross_connect_setup(wpa_s);
1050 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001051
1052 if (wpa_s->p2p_first_connection_timeout) {
1053 wpa_dbg(wpa_s, MSG_DEBUG,
1054 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1055 wpa_s->p2p_first_connection_timeout);
1056 wpa_s->p2p_go_group_formation_completed = 0;
1057 wpa_s->global->p2p_group_formation = wpa_s;
1058 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1059 wpa_s->parent, NULL);
1060 eloop_register_timeout(
1061 wpa_s->p2p_first_connection_timeout, 0,
1062 wpas_p2p_group_formation_timeout,
1063 wpa_s->parent, NULL);
1064 }
1065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066 return;
1067 }
1068
1069 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1070 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1071 params->peer_interface_addr)) {
1072 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1073 "filtering");
1074 return;
1075 }
1076 if (params->wps_method == WPS_PBC)
1077 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001078 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079 else if (wpa_s->p2p_pin[0])
1080 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001081 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082 os_free(wpa_s->go_params);
1083 wpa_s->go_params = NULL;
1084}
1085
1086
1087static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1088 struct p2p_go_neg_results *params,
1089 int group_formation)
1090{
1091 struct wpa_ssid *ssid;
1092
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001093 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1094 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1095 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1096 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001098 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099
1100 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001101 if (ssid == NULL) {
1102 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001106 wpa_s->show_group_started = 0;
1107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 wpa_config_set_network_defaults(ssid);
1109 ssid->temporary = 1;
1110 ssid->p2p_group = 1;
1111 ssid->p2p_persistent_group = params->persistent_group;
1112 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1113 WPAS_MODE_P2P_GO;
1114 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001115 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116 ssid->ssid = os_zalloc(params->ssid_len + 1);
1117 if (ssid->ssid) {
1118 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1119 ssid->ssid_len = params->ssid_len;
1120 }
1121 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1122 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1123 ssid->proto = WPA_PROTO_RSN;
1124 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001125 if (os_strlen(params->passphrase) > 0) {
1126 ssid->passphrase = os_strdup(params->passphrase);
1127 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001128 wpa_msg_global(wpa_s, MSG_ERROR,
1129 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001130 wpa_config_remove_network(wpa_s->conf, ssid->id);
1131 return;
1132 }
1133 } else
1134 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001135 ssid->psk_set = params->psk_set;
1136 if (ssid->psk_set)
1137 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001138 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001139 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001140 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141
1142 wpa_s->ap_configured_cb = p2p_go_configured;
1143 wpa_s->ap_configured_cb_ctx = wpa_s;
1144 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001145 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 wpa_s->reassociate = 1;
1147 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001148 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1149 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 wpa_supplicant_req_scan(wpa_s, 0, 0);
1151}
1152
1153
1154static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1155 const struct wpa_supplicant *src)
1156{
1157 struct wpa_config *d;
1158 const struct wpa_config *s;
1159
1160 d = dst->conf;
1161 s = src->conf;
1162
1163#define C(n) if (s->n) d->n = os_strdup(s->n)
1164 C(device_name);
1165 C(manufacturer);
1166 C(model_name);
1167 C(model_number);
1168 C(serial_number);
1169 C(config_methods);
1170#undef C
1171
1172 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1173 os_memcpy(d->sec_device_type, s->sec_device_type,
1174 sizeof(d->sec_device_type));
1175 d->num_sec_device_types = s->num_sec_device_types;
1176
1177 d->p2p_group_idle = s->p2p_group_idle;
1178 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001179 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001180 d->max_num_sta = s->max_num_sta;
1181 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001182 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001183 d->beacon_int = s->beacon_int;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001184 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001185 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186}
1187
1188
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001189static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1190 char *ifname, size_t len)
1191{
1192 char *ifname_ptr = wpa_s->ifname;
1193
1194 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1195 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1196 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1197 }
1198
1199 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1200 if (os_strlen(ifname) >= IFNAMSIZ &&
1201 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1202 /* Try to avoid going over the IFNAMSIZ length limit */
1203 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1204 wpa_s->p2p_group_idx);
1205 }
1206}
1207
1208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1210 enum wpa_driver_if_type type)
1211{
1212 char ifname[120], force_ifname[120];
1213
1214 if (wpa_s->pending_interface_name[0]) {
1215 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1216 "- skip creation of a new one");
1217 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1218 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1219 "unknown?! ifname='%s'",
1220 wpa_s->pending_interface_name);
1221 return -1;
1222 }
1223 return 0;
1224 }
1225
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001226 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 force_ifname[0] = '\0';
1228
1229 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1230 ifname);
1231 wpa_s->p2p_group_idx++;
1232
1233 wpa_s->pending_interface_type = type;
1234 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1235 wpa_s->pending_interface_addr, NULL) < 0) {
1236 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1237 "interface");
1238 return -1;
1239 }
1240
1241 if (force_ifname[0]) {
1242 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1243 force_ifname);
1244 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1245 sizeof(wpa_s->pending_interface_name));
1246 } else
1247 os_strlcpy(wpa_s->pending_interface_name, ifname,
1248 sizeof(wpa_s->pending_interface_name));
1249 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1250 MACSTR, wpa_s->pending_interface_name,
1251 MAC2STR(wpa_s->pending_interface_addr));
1252
1253 return 0;
1254}
1255
1256
1257static void wpas_p2p_remove_pending_group_interface(
1258 struct wpa_supplicant *wpa_s)
1259{
1260 if (!wpa_s->pending_interface_name[0] ||
1261 is_zero_ether_addr(wpa_s->pending_interface_addr))
1262 return; /* No pending virtual interface */
1263
1264 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1265 wpa_s->pending_interface_name);
1266 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1267 wpa_s->pending_interface_name);
1268 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1269 wpa_s->pending_interface_name[0] = '\0';
1270}
1271
1272
1273static struct wpa_supplicant *
1274wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1275{
1276 struct wpa_interface iface;
1277 struct wpa_supplicant *group_wpa_s;
1278
1279 if (!wpa_s->pending_interface_name[0]) {
1280 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1281 if (!wpas_p2p_create_iface(wpa_s))
1282 return NULL;
1283 /*
1284 * Something has forced us to remove the pending interface; try
1285 * to create a new one and hope for the best that we will get
1286 * the same local address.
1287 */
1288 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1289 WPA_IF_P2P_CLIENT) < 0)
1290 return NULL;
1291 }
1292
1293 os_memset(&iface, 0, sizeof(iface));
1294 iface.ifname = wpa_s->pending_interface_name;
1295 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001296 if (wpa_s->conf->ctrl_interface == NULL &&
1297 wpa_s->parent != wpa_s &&
1298 wpa_s->p2p_mgmt &&
1299 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1300 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1301 else
1302 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 iface.driver_param = wpa_s->conf->driver_param;
1304 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1305 if (group_wpa_s == NULL) {
1306 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1307 "wpa_supplicant interface");
1308 return NULL;
1309 }
1310 wpa_s->pending_interface_name[0] = '\0';
1311 group_wpa_s->parent = wpa_s;
1312 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1313 P2P_GROUP_INTERFACE_CLIENT;
1314 wpa_s->global->p2p_group_formation = group_wpa_s;
1315
1316 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1317
1318 return group_wpa_s;
1319}
1320
1321
1322static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1323 void *timeout_ctx)
1324{
1325 struct wpa_supplicant *wpa_s = eloop_ctx;
1326 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001327 wpas_p2p_group_formation_failed(wpa_s);
1328}
1329
1330
1331void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1332{
1333 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1334 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 if (wpa_s->global->p2p)
1336 p2p_group_formation_failed(wpa_s->global->p2p);
1337 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1338 wpa_drv_p2p_group_formation_failed(wpa_s);
1339 wpas_group_formation_completed(wpa_s, 0);
1340}
1341
1342
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001343void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1344{
1345 if (wpa_s->global->p2p_group_formation != wpa_s)
1346 return;
1347 /* Speed up group formation timeout since this cannot succeed */
1348 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1349 wpa_s->parent, NULL);
1350 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1351 wpa_s->parent, NULL);
1352}
1353
1354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1356{
1357 struct wpa_supplicant *wpa_s = ctx;
1358
1359 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1360 wpa_drv_cancel_remain_on_channel(wpa_s);
1361 wpa_s->off_channel_freq = 0;
1362 wpa_s->roc_waiting_drv_freq = 0;
1363 }
1364
1365 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001366 wpa_msg_global(wpa_s, MSG_INFO,
1367 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1368 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001369 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 wpas_p2p_remove_pending_group_interface(wpa_s);
1371 return;
1372 }
1373
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001374 if (wpa_s->p2p_go_ht40)
1375 res->ht40 = 1;
1376
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001377 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1378 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1379 " wps_method=%s",
1380 res->role_go ? "GO" : "client", res->freq, res->ht40,
1381 MAC2STR(res->peer_device_addr),
1382 MAC2STR(res->peer_interface_addr),
1383 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001384 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385
Dmitry Shmidt04949592012-07-19 12:16:46 -07001386 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1387 struct wpa_ssid *ssid;
1388 ssid = wpa_config_get_network(wpa_s->conf,
1389 wpa_s->p2p_persistent_id);
1390 if (ssid && ssid->disabled == 2 &&
1391 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1392 size_t len = os_strlen(ssid->passphrase);
1393 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1394 "on requested persistent group");
1395 os_memcpy(res->passphrase, ssid->passphrase, len);
1396 res->passphrase[len] = '\0';
1397 }
1398 }
1399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 if (wpa_s->create_p2p_iface) {
1401 struct wpa_supplicant *group_wpa_s =
1402 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1403 if (group_wpa_s == NULL) {
1404 wpas_p2p_remove_pending_group_interface(wpa_s);
1405 return;
1406 }
1407 if (group_wpa_s != wpa_s) {
1408 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1409 sizeof(group_wpa_s->p2p_pin));
1410 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1411 }
1412 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1413 wpa_s->pending_interface_name[0] = '\0';
1414 group_wpa_s->p2p_in_provisioning = 1;
1415
1416 if (res->role_go)
1417 wpas_start_wps_go(group_wpa_s, res, 1);
1418 else
1419 wpas_start_wps_enrollee(group_wpa_s, res);
1420 } else {
1421 wpa_s->p2p_in_provisioning = 1;
1422 wpa_s->global->p2p_group_formation = wpa_s;
1423
1424 if (res->role_go)
1425 wpas_start_wps_go(wpa_s, res, 1);
1426 else
1427 wpas_start_wps_enrollee(ctx, res);
1428 }
1429
1430 wpa_s->p2p_long_listen = 0;
1431 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1432
1433 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1434 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1435 (res->peer_config_timeout % 100) * 10000,
1436 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1437}
1438
1439
1440void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1441{
1442 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001443 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1444 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445
1446 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1447}
1448
1449
1450void wpas_dev_found(void *ctx, const u8 *addr,
1451 const struct p2p_peer_info *info,
1452 int new_device)
1453{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001454#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 struct wpa_supplicant *wpa_s = ctx;
1456 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001457#define WFD_DEV_INFO_SIZE 9
1458 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001459 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001460#ifdef CONFIG_WIFI_DISPLAY
1461 if (info->wfd_subelems) {
1462 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1463 wpabuf_head(info->wfd_subelems),
1464 WFD_DEV_INFO_SIZE);
1465 }
1466#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001467 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1468 " p2p_dev_addr=" MACSTR
1469 " pri_dev_type=%s name='%s' config_methods=0x%x "
1470 "dev_capab=0x%x group_capab=0x%x%s%s",
1471 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1472 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1473 sizeof(devtype)),
1474 info->device_name, info->config_methods,
1475 info->dev_capab, info->group_capab,
1476 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001477#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478
1479 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1480}
1481
1482
1483static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1484{
1485 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001486
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001487 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1488 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1491}
1492
1493
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001494static void wpas_find_stopped(void *ctx)
1495{
1496 struct wpa_supplicant *wpa_s = ctx;
1497 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1498}
1499
1500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501static int wpas_start_listen(void *ctx, unsigned int freq,
1502 unsigned int duration,
1503 const struct wpabuf *probe_resp_ie)
1504{
1505 struct wpa_supplicant *wpa_s = ctx;
1506
1507 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1508
1509 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1510 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1511 "report received Probe Request frames");
1512 return -1;
1513 }
1514
1515 wpa_s->pending_listen_freq = freq;
1516 wpa_s->pending_listen_duration = duration;
1517
1518 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1519 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1520 "to remain on channel (%u MHz) for Listen "
1521 "state", freq);
1522 wpa_s->pending_listen_freq = 0;
1523 return -1;
1524 }
1525 wpa_s->off_channel_freq = 0;
1526 wpa_s->roc_waiting_drv_freq = freq;
1527
1528 return 0;
1529}
1530
1531
1532static void wpas_stop_listen(void *ctx)
1533{
1534 struct wpa_supplicant *wpa_s = ctx;
1535 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1536 wpa_drv_cancel_remain_on_channel(wpa_s);
1537 wpa_s->off_channel_freq = 0;
1538 wpa_s->roc_waiting_drv_freq = 0;
1539 }
1540 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1541 wpa_drv_probe_req_report(wpa_s, 0);
1542}
1543
1544
1545static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1546{
1547 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001548 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001549}
1550
1551
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001552/*
1553 * DNS Header section is used only to calculate compression pointers, so the
1554 * contents of this data does not matter, but the length needs to be reserved
1555 * in the virtual packet.
1556 */
1557#define DNS_HEADER_LEN 12
1558
1559/*
1560 * 27-octet in-memory packet from P2P specification containing two implied
1561 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1562 */
1563#define P2P_SD_IN_MEMORY_LEN 27
1564
1565static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1566 u8 **spos, const u8 *end)
1567{
1568 while (*spos < end) {
1569 u8 val = ((*spos)[0] & 0xc0) >> 6;
1570 int len;
1571
1572 if (val == 1 || val == 2) {
1573 /* These are reserved values in RFC 1035 */
1574 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1575 "sequence starting with 0x%x", val);
1576 return -1;
1577 }
1578
1579 if (val == 3) {
1580 u16 offset;
1581 u8 *spos_tmp;
1582
1583 /* Offset */
1584 if (*spos + 2 > end) {
1585 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1586 "DNS offset field");
1587 return -1;
1588 }
1589
1590 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1591 if (offset >= *spos - start) {
1592 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1593 "pointer offset %u", offset);
1594 return -1;
1595 }
1596
1597 (*spos) += 2;
1598 spos_tmp = start + offset;
1599 return p2p_sd_dns_uncompress_label(upos, uend, start,
1600 &spos_tmp,
1601 *spos - 2);
1602 }
1603
1604 /* Label */
1605 len = (*spos)[0] & 0x3f;
1606 if (len == 0)
1607 return 0;
1608
1609 (*spos)++;
1610 if (*spos + len > end) {
1611 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1612 "sequence - no room for label with length "
1613 "%u", len);
1614 return -1;
1615 }
1616
1617 if (*upos + len + 2 > uend)
1618 return -2;
1619
1620 os_memcpy(*upos, *spos, len);
1621 *spos += len;
1622 *upos += len;
1623 (*upos)[0] = '.';
1624 (*upos)++;
1625 (*upos)[0] = '\0';
1626 }
1627
1628 return 0;
1629}
1630
1631
1632/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1633 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1634 * not large enough */
1635static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1636 size_t msg_len, size_t offset)
1637{
1638 /* 27-octet in-memory packet from P2P specification */
1639 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1640 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1641 u8 *tmp, *end, *spos;
1642 char *upos, *uend;
1643 int ret = 0;
1644
1645 if (buf_len < 2)
1646 return -1;
1647 if (offset > msg_len)
1648 return -1;
1649
1650 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1651 if (tmp == NULL)
1652 return -1;
1653 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1654 end = spos + msg_len;
1655 spos += offset;
1656
1657 os_memset(tmp, 0, DNS_HEADER_LEN);
1658 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1659 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1660
1661 upos = buf;
1662 uend = buf + buf_len;
1663
1664 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1665 if (ret) {
1666 os_free(tmp);
1667 return ret;
1668 }
1669
1670 if (upos == buf) {
1671 upos[0] = '.';
1672 upos[1] = '\0';
1673 } else if (upos[-1] == '.')
1674 upos[-1] = '\0';
1675
1676 os_free(tmp);
1677 return 0;
1678}
1679
1680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681static struct p2p_srv_bonjour *
1682wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1683 const struct wpabuf *query)
1684{
1685 struct p2p_srv_bonjour *bsrv;
1686 size_t len;
1687
1688 len = wpabuf_len(query);
1689 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1690 struct p2p_srv_bonjour, list) {
1691 if (len == wpabuf_len(bsrv->query) &&
1692 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1693 len) == 0)
1694 return bsrv;
1695 }
1696 return NULL;
1697}
1698
1699
1700static struct p2p_srv_upnp *
1701wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1702 const char *service)
1703{
1704 struct p2p_srv_upnp *usrv;
1705
1706 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1707 struct p2p_srv_upnp, list) {
1708 if (version == usrv->version &&
1709 os_strcmp(service, usrv->service) == 0)
1710 return usrv;
1711 }
1712 return NULL;
1713}
1714
1715
1716static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1717 u8 srv_trans_id)
1718{
1719 u8 *len_pos;
1720
1721 if (wpabuf_tailroom(resp) < 5)
1722 return;
1723
1724 /* Length (to be filled) */
1725 len_pos = wpabuf_put(resp, 2);
1726 wpabuf_put_u8(resp, srv_proto);
1727 wpabuf_put_u8(resp, srv_trans_id);
1728 /* Status Code */
1729 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1730 /* Response Data: empty */
1731 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1732}
1733
1734
1735static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1736 struct wpabuf *resp, u8 srv_trans_id)
1737{
1738 struct p2p_srv_bonjour *bsrv;
1739 u8 *len_pos;
1740
1741 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1742
1743 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1744 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1745 return;
1746 }
1747
1748 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1749 struct p2p_srv_bonjour, list) {
1750 if (wpabuf_tailroom(resp) <
1751 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1752 return;
1753 /* Length (to be filled) */
1754 len_pos = wpabuf_put(resp, 2);
1755 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1756 wpabuf_put_u8(resp, srv_trans_id);
1757 /* Status Code */
1758 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1759 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1760 wpabuf_head(bsrv->resp),
1761 wpabuf_len(bsrv->resp));
1762 /* Response Data */
1763 wpabuf_put_buf(resp, bsrv->query); /* Key */
1764 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1765 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1766 2);
1767 }
1768}
1769
1770
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001771static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1772 size_t query_len)
1773{
1774 char str_rx[256], str_srv[256];
1775
1776 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1777 return 0; /* Too short to include DNS Type and Version */
1778 if (os_memcmp(query + query_len - 3,
1779 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1780 3) != 0)
1781 return 0; /* Mismatch in DNS Type or Version */
1782 if (query_len == wpabuf_len(bsrv->query) &&
1783 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1784 return 1; /* Binary match */
1785
1786 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1787 0))
1788 return 0; /* Failed to uncompress query */
1789 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1790 wpabuf_head(bsrv->query),
1791 wpabuf_len(bsrv->query) - 3, 0))
1792 return 0; /* Failed to uncompress service */
1793
1794 return os_strcmp(str_rx, str_srv) == 0;
1795}
1796
1797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1799 struct wpabuf *resp, u8 srv_trans_id,
1800 const u8 *query, size_t query_len)
1801{
1802 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001804 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805
1806 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1807 query, query_len);
1808 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1809 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1810 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1811 srv_trans_id);
1812 return;
1813 }
1814
1815 if (query_len == 0) {
1816 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1817 return;
1818 }
1819
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001820 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1821 struct p2p_srv_bonjour, list) {
1822 if (!match_bonjour_query(bsrv, query, query_len))
1823 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001825 if (wpabuf_tailroom(resp) <
1826 5 + query_len + wpabuf_len(bsrv->resp))
1827 return;
1828
1829 matches++;
1830
1831 /* Length (to be filled) */
1832 len_pos = wpabuf_put(resp, 2);
1833 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1834 wpabuf_put_u8(resp, srv_trans_id);
1835
1836 /* Status Code */
1837 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1838 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1839 wpabuf_head(bsrv->resp),
1840 wpabuf_len(bsrv->resp));
1841
1842 /* Response Data */
1843 wpabuf_put_data(resp, query, query_len); /* Key */
1844 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1845
1846 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1847 }
1848
1849 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1851 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001852 if (wpabuf_tailroom(resp) < 5)
1853 return;
1854
1855 /* Length (to be filled) */
1856 len_pos = wpabuf_put(resp, 2);
1857 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1858 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859
1860 /* Status Code */
1861 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1862 /* Response Data: empty */
1863 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1864 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866}
1867
1868
1869static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1870 struct wpabuf *resp, u8 srv_trans_id)
1871{
1872 struct p2p_srv_upnp *usrv;
1873 u8 *len_pos;
1874
1875 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1876
1877 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1878 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1879 return;
1880 }
1881
1882 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1883 struct p2p_srv_upnp, list) {
1884 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1885 return;
1886
1887 /* Length (to be filled) */
1888 len_pos = wpabuf_put(resp, 2);
1889 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1890 wpabuf_put_u8(resp, srv_trans_id);
1891
1892 /* Status Code */
1893 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1894 /* Response Data */
1895 wpabuf_put_u8(resp, usrv->version);
1896 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1897 usrv->service);
1898 wpabuf_put_str(resp, usrv->service);
1899 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1900 2);
1901 }
1902}
1903
1904
1905static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1906 struct wpabuf *resp, u8 srv_trans_id,
1907 const u8 *query, size_t query_len)
1908{
1909 struct p2p_srv_upnp *usrv;
1910 u8 *len_pos;
1911 u8 version;
1912 char *str;
1913 int count = 0;
1914
1915 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1916 query, query_len);
1917
1918 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1919 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1920 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1921 srv_trans_id);
1922 return;
1923 }
1924
1925 if (query_len == 0) {
1926 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1927 return;
1928 }
1929
1930 if (wpabuf_tailroom(resp) < 5)
1931 return;
1932
1933 /* Length (to be filled) */
1934 len_pos = wpabuf_put(resp, 2);
1935 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1936 wpabuf_put_u8(resp, srv_trans_id);
1937
1938 version = query[0];
1939 str = os_malloc(query_len);
1940 if (str == NULL)
1941 return;
1942 os_memcpy(str, query + 1, query_len - 1);
1943 str[query_len - 1] = '\0';
1944
1945 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1946 struct p2p_srv_upnp, list) {
1947 if (version != usrv->version)
1948 continue;
1949
1950 if (os_strcmp(str, "ssdp:all") != 0 &&
1951 os_strstr(usrv->service, str) == NULL)
1952 continue;
1953
1954 if (wpabuf_tailroom(resp) < 2)
1955 break;
1956 if (count == 0) {
1957 /* Status Code */
1958 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1959 /* Response Data */
1960 wpabuf_put_u8(resp, version);
1961 } else
1962 wpabuf_put_u8(resp, ',');
1963
1964 count++;
1965
1966 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1967 usrv->service);
1968 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1969 break;
1970 wpabuf_put_str(resp, usrv->service);
1971 }
1972 os_free(str);
1973
1974 if (count == 0) {
1975 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1976 "available");
1977 /* Status Code */
1978 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1979 /* Response Data: empty */
1980 }
1981
1982 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1983}
1984
1985
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001986#ifdef CONFIG_WIFI_DISPLAY
1987static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1988 struct wpabuf *resp, u8 srv_trans_id,
1989 const u8 *query, size_t query_len)
1990{
1991 const u8 *pos;
1992 u8 role;
1993 u8 *len_pos;
1994
1995 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1996
1997 if (!wpa_s->global->wifi_display) {
1998 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1999 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2000 srv_trans_id);
2001 return;
2002 }
2003
2004 if (query_len < 1) {
2005 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2006 "Role");
2007 return;
2008 }
2009
2010 if (wpabuf_tailroom(resp) < 5)
2011 return;
2012
2013 pos = query;
2014 role = *pos++;
2015 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2016
2017 /* TODO: role specific handling */
2018
2019 /* Length (to be filled) */
2020 len_pos = wpabuf_put(resp, 2);
2021 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2022 wpabuf_put_u8(resp, srv_trans_id);
2023 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2024
2025 while (pos < query + query_len) {
2026 if (*pos < MAX_WFD_SUBELEMS &&
2027 wpa_s->global->wfd_subelem[*pos] &&
2028 wpabuf_tailroom(resp) >=
2029 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2030 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2031 "subelement %u", *pos);
2032 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2033 }
2034 pos++;
2035 }
2036
2037 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2038}
2039#endif /* CONFIG_WIFI_DISPLAY */
2040
2041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2043 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2044{
2045 struct wpa_supplicant *wpa_s = ctx;
2046 const u8 *pos = tlvs;
2047 const u8 *end = tlvs + tlvs_len;
2048 const u8 *tlv_end;
2049 u16 slen;
2050 struct wpabuf *resp;
2051 u8 srv_proto, srv_trans_id;
2052 size_t buf_len;
2053 char *buf;
2054
2055 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2056 tlvs, tlvs_len);
2057 buf_len = 2 * tlvs_len + 1;
2058 buf = os_malloc(buf_len);
2059 if (buf) {
2060 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2061 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2062 MACSTR " %u %u %s",
2063 freq, MAC2STR(sa), dialog_token, update_indic,
2064 buf);
2065 os_free(buf);
2066 }
2067
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002068 if (wpa_s->p2p_sd_over_ctrl_iface) {
2069 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2070 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002072 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073
2074 resp = wpabuf_alloc(10000);
2075 if (resp == NULL)
2076 return;
2077
2078 while (pos + 1 < end) {
2079 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2080 slen = WPA_GET_LE16(pos);
2081 pos += 2;
2082 if (pos + slen > end || slen < 2) {
2083 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2084 "length");
2085 wpabuf_free(resp);
2086 return;
2087 }
2088 tlv_end = pos + slen;
2089
2090 srv_proto = *pos++;
2091 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2092 srv_proto);
2093 srv_trans_id = *pos++;
2094 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2095 srv_trans_id);
2096
2097 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2098 pos, tlv_end - pos);
2099
2100
2101 if (wpa_s->force_long_sd) {
2102 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2103 "response");
2104 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2105 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2106 goto done;
2107 }
2108
2109 switch (srv_proto) {
2110 case P2P_SERV_ALL_SERVICES:
2111 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2112 "for all services");
2113 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2114 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2115 wpa_printf(MSG_DEBUG, "P2P: No service "
2116 "discovery protocols available");
2117 wpas_sd_add_proto_not_avail(
2118 resp, P2P_SERV_ALL_SERVICES,
2119 srv_trans_id);
2120 break;
2121 }
2122 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2123 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2124 break;
2125 case P2P_SERV_BONJOUR:
2126 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2127 pos, tlv_end - pos);
2128 break;
2129 case P2P_SERV_UPNP:
2130 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2131 pos, tlv_end - pos);
2132 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002133#ifdef CONFIG_WIFI_DISPLAY
2134 case P2P_SERV_WIFI_DISPLAY:
2135 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2136 pos, tlv_end - pos);
2137 break;
2138#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 default:
2140 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2141 "protocol %u", srv_proto);
2142 wpas_sd_add_proto_not_avail(resp, srv_proto,
2143 srv_trans_id);
2144 break;
2145 }
2146
2147 pos = tlv_end;
2148 }
2149
2150done:
2151 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2152 update_indic, tlvs, tlvs_len);
2153
2154 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2155
2156 wpabuf_free(resp);
2157}
2158
2159
2160void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2161 const u8 *tlvs, size_t tlvs_len)
2162{
2163 struct wpa_supplicant *wpa_s = ctx;
2164 const u8 *pos = tlvs;
2165 const u8 *end = tlvs + tlvs_len;
2166 const u8 *tlv_end;
2167 u16 slen;
2168 size_t buf_len;
2169 char *buf;
2170
2171 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2172 tlvs, tlvs_len);
2173 if (tlvs_len > 1500) {
2174 /* TODO: better way for handling this */
2175 wpa_msg_ctrl(wpa_s, MSG_INFO,
2176 P2P_EVENT_SERV_DISC_RESP MACSTR
2177 " %u <long response: %u bytes>",
2178 MAC2STR(sa), update_indic,
2179 (unsigned int) tlvs_len);
2180 } else {
2181 buf_len = 2 * tlvs_len + 1;
2182 buf = os_malloc(buf_len);
2183 if (buf) {
2184 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2185 wpa_msg_ctrl(wpa_s, MSG_INFO,
2186 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2187 MAC2STR(sa), update_indic, buf);
2188 os_free(buf);
2189 }
2190 }
2191
2192 while (pos < end) {
2193 u8 srv_proto, srv_trans_id, status;
2194
2195 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2196 slen = WPA_GET_LE16(pos);
2197 pos += 2;
2198 if (pos + slen > end || slen < 3) {
2199 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2200 "length");
2201 return;
2202 }
2203 tlv_end = pos + slen;
2204
2205 srv_proto = *pos++;
2206 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2207 srv_proto);
2208 srv_trans_id = *pos++;
2209 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2210 srv_trans_id);
2211 status = *pos++;
2212 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2213 status);
2214
2215 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2216 pos, tlv_end - pos);
2217
2218 pos = tlv_end;
2219 }
2220
2221 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2222}
2223
2224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002225u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2226 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227{
2228 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002229 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002231 return 0;
2232 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233}
2234
2235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002236u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2237 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238{
2239 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002240 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241
2242 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2243 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002244 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2246 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2247 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2248 wpabuf_put_u8(tlvs, version);
2249 wpabuf_put_str(tlvs, query);
2250 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2251 wpabuf_free(tlvs);
2252 return ret;
2253}
2254
2255
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002256#ifdef CONFIG_WIFI_DISPLAY
2257
2258static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2259 const struct wpabuf *tlvs)
2260{
2261 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2262 return 0;
2263 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2264 return 0;
2265 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2266}
2267
2268
2269#define MAX_WFD_SD_SUBELEMS 20
2270
2271static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2272 const char *subelems)
2273{
2274 u8 *len;
2275 const char *pos;
2276 int val;
2277 int count = 0;
2278
2279 len = wpabuf_put(tlvs, 2);
2280 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2281 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2282
2283 wpabuf_put_u8(tlvs, role);
2284
2285 pos = subelems;
2286 while (*pos) {
2287 val = atoi(pos);
2288 if (val >= 0 && val < 256) {
2289 wpabuf_put_u8(tlvs, val);
2290 count++;
2291 if (count == MAX_WFD_SD_SUBELEMS)
2292 break;
2293 }
2294 pos = os_strchr(pos + 1, ',');
2295 if (pos == NULL)
2296 break;
2297 pos++;
2298 }
2299
2300 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2301}
2302
2303
2304u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2305 const u8 *dst, const char *role)
2306{
2307 struct wpabuf *tlvs;
2308 u64 ret;
2309 const char *subelems;
2310 u8 id = 1;
2311
2312 subelems = os_strchr(role, ' ');
2313 if (subelems == NULL)
2314 return 0;
2315 subelems++;
2316
2317 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2318 if (tlvs == NULL)
2319 return 0;
2320
2321 if (os_strstr(role, "[source]"))
2322 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2323 if (os_strstr(role, "[pri-sink]"))
2324 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2325 if (os_strstr(role, "[sec-sink]"))
2326 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2327 if (os_strstr(role, "[source+sink]"))
2328 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2329
2330 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2331 wpabuf_free(tlvs);
2332 return ret;
2333}
2334
2335#endif /* CONFIG_WIFI_DISPLAY */
2336
2337
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002338int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002339{
2340 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002341 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2343 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002344 return p2p_sd_cancel_request(wpa_s->global->p2p,
2345 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346}
2347
2348
2349void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2350 const u8 *dst, u8 dialog_token,
2351 const struct wpabuf *resp_tlvs)
2352{
2353 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2354 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2355 resp_tlvs);
2356 return;
2357 }
2358 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2359 return;
2360 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2361 resp_tlvs);
2362}
2363
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2366{
2367 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2368 wpa_drv_p2p_service_update(wpa_s);
2369 return;
2370 }
2371 if (wpa_s->global->p2p)
2372 p2p_sd_service_update(wpa_s->global->p2p);
2373}
2374
2375
2376static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2377{
2378 dl_list_del(&bsrv->list);
2379 wpabuf_free(bsrv->query);
2380 wpabuf_free(bsrv->resp);
2381 os_free(bsrv);
2382}
2383
2384
2385static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2386{
2387 dl_list_del(&usrv->list);
2388 os_free(usrv->service);
2389 os_free(usrv);
2390}
2391
2392
2393void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2394{
2395 struct p2p_srv_bonjour *bsrv, *bn;
2396 struct p2p_srv_upnp *usrv, *un;
2397
2398 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2399 struct p2p_srv_bonjour, list)
2400 wpas_p2p_srv_bonjour_free(bsrv);
2401
2402 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2403 struct p2p_srv_upnp, list)
2404 wpas_p2p_srv_upnp_free(usrv);
2405
2406 wpas_p2p_sd_service_update(wpa_s);
2407}
2408
2409
2410int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2411 struct wpabuf *query, struct wpabuf *resp)
2412{
2413 struct p2p_srv_bonjour *bsrv;
2414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415 bsrv = os_zalloc(sizeof(*bsrv));
2416 if (bsrv == NULL)
2417 return -1;
2418 bsrv->query = query;
2419 bsrv->resp = resp;
2420 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2421
2422 wpas_p2p_sd_service_update(wpa_s);
2423 return 0;
2424}
2425
2426
2427int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2428 const struct wpabuf *query)
2429{
2430 struct p2p_srv_bonjour *bsrv;
2431
2432 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2433 if (bsrv == NULL)
2434 return -1;
2435 wpas_p2p_srv_bonjour_free(bsrv);
2436 wpas_p2p_sd_service_update(wpa_s);
2437 return 0;
2438}
2439
2440
2441int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2442 const char *service)
2443{
2444 struct p2p_srv_upnp *usrv;
2445
2446 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2447 return 0; /* Already listed */
2448 usrv = os_zalloc(sizeof(*usrv));
2449 if (usrv == NULL)
2450 return -1;
2451 usrv->version = version;
2452 usrv->service = os_strdup(service);
2453 if (usrv->service == NULL) {
2454 os_free(usrv);
2455 return -1;
2456 }
2457 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2458
2459 wpas_p2p_sd_service_update(wpa_s);
2460 return 0;
2461}
2462
2463
2464int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2465 const char *service)
2466{
2467 struct p2p_srv_upnp *usrv;
2468
2469 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2470 if (usrv == NULL)
2471 return -1;
2472 wpas_p2p_srv_upnp_free(usrv);
2473 wpas_p2p_sd_service_update(wpa_s);
2474 return 0;
2475}
2476
2477
2478static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2479 const u8 *peer, const char *params,
2480 unsigned int generated_pin)
2481{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002482 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2483 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484}
2485
2486
2487static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2488 const u8 *peer, const char *params)
2489{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002490 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2491 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492}
2493
2494
2495void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2496 const u8 *dev_addr, const u8 *pri_dev_type,
2497 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002498 u8 dev_capab, u8 group_capab, const u8 *group_id,
2499 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500{
2501 struct wpa_supplicant *wpa_s = ctx;
2502 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002503 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 u8 empty_dev_type[8];
2505 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002506 struct wpa_supplicant *group = NULL;
2507
2508 if (group_id) {
2509 for (group = wpa_s->global->ifaces; group; group = group->next)
2510 {
2511 struct wpa_ssid *s = group->current_ssid;
2512 if (s != NULL &&
2513 s->mode == WPAS_MODE_P2P_GO &&
2514 group_id_len - ETH_ALEN == s->ssid_len &&
2515 os_memcmp(group_id + ETH_ALEN, s->ssid,
2516 s->ssid_len) == 0)
2517 break;
2518 }
2519 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520
2521 if (pri_dev_type == NULL) {
2522 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2523 pri_dev_type = empty_dev_type;
2524 }
2525 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2526 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002527 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 MAC2STR(dev_addr),
2529 wps_dev_type_bin2str(pri_dev_type, devtype,
2530 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002531 dev_name, supp_config_methods, dev_capab, group_capab,
2532 group ? " group=" : "",
2533 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536 if (config_methods & WPS_CONFIG_DISPLAY) {
2537 generated_pin = wps_generate_pin();
2538 wpas_prov_disc_local_display(wpa_s, peer, params,
2539 generated_pin);
2540 } else if (config_methods & WPS_CONFIG_KEYPAD)
2541 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2542 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002543 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2544 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002545
2546 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2547 P2P_PROV_DISC_SUCCESS,
2548 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549}
2550
2551
2552void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2553{
2554 struct wpa_supplicant *wpa_s = ctx;
2555 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002556 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002558 if (wpa_s->pending_pd_before_join &&
2559 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2560 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2561 wpa_s->pending_pd_before_join = 0;
2562 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2563 "join-existing-group operation");
2564 wpas_p2p_join_start(wpa_s);
2565 return;
2566 }
2567
Dmitry Shmidt04949592012-07-19 12:16:46 -07002568 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2569 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2570 os_snprintf(params, sizeof(params), " peer_go=%d",
2571 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2572 else
2573 params[0] = '\0';
2574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002576 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 else if (config_methods & WPS_CONFIG_KEYPAD) {
2578 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002579 wpas_prov_disc_local_display(wpa_s, peer, params,
2580 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002582 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2583 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584
Jouni Malinen75ecf522011-06-27 15:19:46 -07002585 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2586 P2P_PROV_DISC_SUCCESS,
2587 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588}
2589
2590
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002591static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2592 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002593{
2594 struct wpa_supplicant *wpa_s = ctx;
2595
Dmitry Shmidt04949592012-07-19 12:16:46 -07002596 if (wpa_s->p2p_fallback_to_go_neg) {
2597 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2598 "failed - fall back to GO Negotiation");
2599 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2600 return;
2601 }
2602
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002603 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002604 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002605 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2606 "join-existing-group operation (no ACK for PD "
2607 "Req attempts)");
2608 wpas_p2p_join_start(wpa_s);
2609 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002610 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002611
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002612 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2613 " p2p_dev_addr=" MACSTR " status=%d",
2614 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002615
Jouni Malinen75ecf522011-06-27 15:19:46 -07002616 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2617 status, 0, 0);
2618}
2619
2620
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002621static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2622{
2623 if (channels == NULL)
2624 return 1; /* Assume no restrictions */
2625 return p2p_channels_includes_freq(channels, freq);
2626
2627}
2628
2629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2631 const u8 *go_dev_addr, const u8 *ssid,
2632 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002633 int *force_freq, int persistent_group,
2634 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635{
2636 struct wpa_supplicant *wpa_s = ctx;
2637 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 int res;
2639 struct wpa_supplicant *grp;
2640
2641 if (!persistent_group) {
2642 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2643 " to join an active group", MAC2STR(sa));
2644 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2645 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2646 == 0 ||
2647 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2648 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2649 "authorized invitation");
2650 goto accept_inv;
2651 }
2652 /*
2653 * Do not accept the invitation automatically; notify user and
2654 * request approval.
2655 */
2656 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2657 }
2658
2659 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2660 if (grp) {
2661 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2662 "running persistent group");
2663 if (*go)
2664 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2665 goto accept_inv;
2666 }
2667
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002668 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2669 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2670 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2671 "invitation to re-invoke a persistent group");
2672 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2674
2675 for (s = wpa_s->conf->ssid; s; s = s->next) {
2676 if (s->disabled == 2 &&
2677 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2678 s->ssid_len == ssid_len &&
2679 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2680 break;
2681 }
2682
2683 if (!s) {
2684 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2685 " requested reinvocation of an unknown group",
2686 MAC2STR(sa));
2687 return P2P_SC_FAIL_UNKNOWN_GROUP;
2688 }
2689
2690 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2691 *go = 1;
2692 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2693 wpa_printf(MSG_DEBUG, "P2P: The only available "
2694 "interface is already in use - reject "
2695 "invitation");
2696 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2697 }
2698 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2699 } else if (s->mode == WPAS_MODE_P2P_GO) {
2700 *go = 1;
2701 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2702 {
2703 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2704 "interface address for the group");
2705 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2706 }
2707 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2708 ETH_ALEN);
2709 }
2710
2711accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002712 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2713
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002714 /* Get one of the frequencies currently in use */
2715 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2716 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 -07002717 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002718 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719 }
2720
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002721 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2722 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002723 if (*go == 0) {
2724 /* We are the client */
2725 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2726 "running a GO but we are capable of MCC, "
2727 "figure out the best channel to use");
2728 *force_freq = 0;
2729 } else if (!freq_included(channels, *force_freq)) {
2730 /* We are the GO, and *force_freq is not in the
2731 * intersection */
2732 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2733 "in intersection but we are capable of MCC, "
2734 "figure out the best channel to use",
2735 *force_freq);
2736 *force_freq = 0;
2737 }
2738 }
2739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002740 return P2P_SC_SUCCESS;
2741}
2742
2743
2744static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2745 const u8 *ssid, size_t ssid_len,
2746 const u8 *go_dev_addr, u8 status,
2747 int op_freq)
2748{
2749 struct wpa_supplicant *wpa_s = ctx;
2750 struct wpa_ssid *s;
2751
2752 for (s = wpa_s->conf->ssid; s; s = s->next) {
2753 if (s->disabled == 2 &&
2754 s->ssid_len == ssid_len &&
2755 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2756 break;
2757 }
2758
2759 if (status == P2P_SC_SUCCESS) {
2760 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2761 " was accepted; op_freq=%d MHz",
2762 MAC2STR(sa), op_freq);
2763 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002764 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765 wpas_p2p_group_add_persistent(
Dmitry Shmidt56052862013-10-04 10:23:25 -07002766 wpa_s, s, go, go ? op_freq : 0, 0, NULL,
2767 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002768 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002769 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002771 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772 }
2773 return;
2774 }
2775
2776 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2777 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2778 " was rejected (status %u)", MAC2STR(sa), status);
2779 return;
2780 }
2781
2782 if (!s) {
2783 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002784 wpa_msg_global(wpa_s, MSG_INFO,
2785 P2P_EVENT_INVITATION_RECEIVED
2786 "sa=" MACSTR " go_dev_addr=" MACSTR
2787 " bssid=" MACSTR " unknown-network",
2788 MAC2STR(sa), MAC2STR(go_dev_addr),
2789 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002791 wpa_msg_global(wpa_s, MSG_INFO,
2792 P2P_EVENT_INVITATION_RECEIVED
2793 "sa=" MACSTR " go_dev_addr=" MACSTR
2794 " unknown-network",
2795 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796 }
2797 return;
2798 }
2799
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002800 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002801 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2802 "sa=" MACSTR " persistent=%d freq=%d",
2803 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002804 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002805 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2806 "sa=" MACSTR " persistent=%d",
2807 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002808 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809}
2810
2811
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002812static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2813 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002814 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002815{
2816 size_t i;
2817
2818 if (ssid == NULL)
2819 return;
2820
2821 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2822 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2823 ETH_ALEN) == 0)
2824 break;
2825 }
2826 if (i >= ssid->num_p2p_clients) {
2827 if (ssid->mode != WPAS_MODE_P2P_GO &&
2828 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2829 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2830 "due to invitation result", ssid->id);
2831 wpas_notify_network_removed(wpa_s, ssid);
2832 wpa_config_remove_network(wpa_s->conf, ssid->id);
2833 return;
2834 }
2835 return; /* Peer not found in client list */
2836 }
2837
2838 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002839 "group %d client list%s",
2840 MAC2STR(peer), ssid->id,
2841 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002842 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2843 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2844 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2845 ssid->num_p2p_clients--;
2846#ifndef CONFIG_NO_CONFIG_WRITE
2847 if (wpa_s->parent->conf->update_config &&
2848 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2849 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2850#endif /* CONFIG_NO_CONFIG_WRITE */
2851}
2852
2853
2854static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2855 const u8 *peer)
2856{
2857 struct wpa_ssid *ssid;
2858
2859 wpa_s = wpa_s->global->p2p_invite_group;
2860 if (wpa_s == NULL)
2861 return; /* No known invitation group */
2862 ssid = wpa_s->current_ssid;
2863 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2864 !ssid->p2p_persistent_group)
2865 return; /* Not operating as a GO in persistent group */
2866 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2867 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002868 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002869}
2870
2871
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002872static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002873 const struct p2p_channels *channels,
2874 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875{
2876 struct wpa_supplicant *wpa_s = ctx;
2877 struct wpa_ssid *ssid;
2878
2879 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002880 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2881 "status=%d " MACSTR,
2882 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002884 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2885 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 }
2887 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2888
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002889 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2890 status, MAC2STR(peer));
2891 if (wpa_s->pending_invite_ssid_id == -1) {
2892 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2893 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002895 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002897 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2898 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2899 "invitation exchange to indicate readiness for "
2900 "re-invocation");
2901 }
2902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002904 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2905 ssid = wpa_config_get_network(
2906 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002907 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002908 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909 wpas_p2p_remove_pending_group_interface(wpa_s);
2910 return;
2911 }
2912
2913 ssid = wpa_config_get_network(wpa_s->conf,
2914 wpa_s->pending_invite_ssid_id);
2915 if (ssid == NULL) {
2916 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2917 "data matching with invitation");
2918 return;
2919 }
2920
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002921 /*
2922 * The peer could have missed our ctrl::ack frame for Invitation
2923 * Response and continue retransmitting the frame. To reduce the
2924 * likelihood of the peer not getting successful TX status for the
2925 * Invitation Response frame, wait a short time here before starting
2926 * the persistent group so that we will remain on the current channel to
2927 * acknowledge any possible retransmission from the peer.
2928 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002929#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002930 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2931 "starting persistent group");
2932 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002933#else
2934 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2935 "starting persistent group");
2936 os_sleep(0, 100000);
2937#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002939 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002940 ssid->mode == WPAS_MODE_P2P_GO,
2941 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002942 wpa_s->p2p_go_ht40, channels,
2943 ssid->mode == WPAS_MODE_P2P_GO ?
2944 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2945 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002946}
2947
2948
Dmitry Shmidt04949592012-07-19 12:16:46 -07002949static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2950 unsigned int freq)
2951{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002952 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002953}
2954
2955
2956static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2957{
2958 reg->channel[reg->channels] = chan;
2959 reg->channels++;
2960}
2961
2962
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002963static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2964 struct p2p_channels *chan)
2965{
2966 int i, cla = 0;
2967
2968 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2969 "band");
2970
2971 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2972 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002973 chan->reg_class[cla].channels = 0;
2974 for (i = 0; i < 11; i++) {
2975 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2976 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2977 }
2978 if (chan->reg_class[cla].channels)
2979 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980
2981 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2982 "band");
2983
2984 /* Operating class 115 - 5 GHz, channels 36-48 */
2985 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002986 chan->reg_class[cla].channels = 0;
2987 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2988 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2989 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2990 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2991 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2992 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2993 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2994 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2995 if (chan->reg_class[cla].channels)
2996 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997
2998 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2999 "band");
3000
3001 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3002 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003003 chan->reg_class[cla].channels = 0;
3004 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3005 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3006 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3007 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3008 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3009 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3010 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3011 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3012 if (chan->reg_class[cla].channels)
3013 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014
3015 chan->reg_classes = cla;
3016 return 0;
3017}
3018
3019
3020static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3021 u16 num_modes,
3022 enum hostapd_hw_mode mode)
3023{
3024 u16 i;
3025
3026 for (i = 0; i < num_modes; i++) {
3027 if (modes[i].mode == mode)
3028 return &modes[i];
3029 }
3030
3031 return NULL;
3032}
3033
3034
Dmitry Shmidt04949592012-07-19 12:16:46 -07003035static int has_channel(struct wpa_global *global,
3036 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037{
3038 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003039 unsigned int freq;
3040
3041 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3042 chan * 5;
3043 if (wpas_p2p_disallowed_freq(global, freq))
3044 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045
3046 for (i = 0; i < mode->num_channels; i++) {
3047 if (mode->channels[i].chan == chan) {
3048 if (flags)
3049 *flags = mode->channels[i].flag;
3050 return !(mode->channels[i].flag &
3051 (HOSTAPD_CHAN_DISABLED |
3052 HOSTAPD_CHAN_PASSIVE_SCAN |
3053 HOSTAPD_CHAN_NO_IBSS |
3054 HOSTAPD_CHAN_RADAR));
3055 }
3056 }
3057
3058 return 0;
3059}
3060
3061
3062struct p2p_oper_class_map {
3063 enum hostapd_hw_mode mode;
3064 u8 op_class;
3065 u8 min_chan;
3066 u8 max_chan;
3067 u8 inc;
3068 enum { BW20, BW40PLUS, BW40MINUS } bw;
3069};
3070
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003071static struct p2p_oper_class_map op_class[] = {
3072 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003073#if 0 /* Do not enable HT40 on 2 GHz for now */
3074 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3075 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3076#endif
3077 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3078 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3079 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3080 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3081 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3082 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3083 { -1, 0, 0, 0, 0, BW20 }
3084};
3085
3086
3087static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3088 struct hostapd_hw_modes *mode,
3089 u8 channel, u8 bw)
3090{
3091 int flag;
3092
3093 if (!has_channel(wpa_s->global, mode, channel, &flag))
3094 return -1;
3095 if (bw == BW40MINUS &&
3096 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
3097 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
3098 return 0;
3099 if (bw == BW40PLUS &&
3100 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
3101 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
3102 return 0;
3103 return 1;
3104}
3105
3106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003107static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3108 struct p2p_channels *chan)
3109{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003110 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003113 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3115 "of all supported channels; assume dualband "
3116 "support");
3117 return wpas_p2p_default_channels(wpa_s, chan);
3118 }
3119
3120 cla = 0;
3121
3122 for (op = 0; op_class[op].op_class; op++) {
3123 struct p2p_oper_class_map *o = &op_class[op];
3124 u8 ch;
3125 struct p2p_reg_class *reg = NULL;
3126
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003127 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128 if (mode == NULL)
3129 continue;
3130 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003131 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 continue;
3133 if (reg == NULL) {
3134 wpa_printf(MSG_DEBUG, "P2P: Add operating "
3135 "class %u", o->op_class);
3136 reg = &chan->reg_class[cla];
3137 cla++;
3138 reg->reg_class = o->op_class;
3139 }
3140 reg->channel[reg->channels] = ch;
3141 reg->channels++;
3142 }
3143 if (reg) {
3144 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3145 reg->channel, reg->channels);
3146 }
3147 }
3148
3149 chan->reg_classes = cla;
3150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151 return 0;
3152}
3153
3154
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003155int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3156 struct hostapd_hw_modes *mode, u8 channel)
3157{
3158 int op, ret;
3159
3160 for (op = 0; op_class[op].op_class; op++) {
3161 struct p2p_oper_class_map *o = &op_class[op];
3162 u8 ch;
3163
3164 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3165 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3166 o->bw == BW20 || ch != channel)
3167 continue;
3168 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3169 if (ret < 0)
3170 continue;
3171 else if (ret > 0)
3172 return (o->bw == BW40MINUS) ? -1 : 1;
3173 else
3174 return 0;
3175 }
3176 }
3177 return 0;
3178}
3179
3180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3182 size_t buf_len)
3183{
3184 struct wpa_supplicant *wpa_s = ctx;
3185
3186 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3187 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3188 break;
3189 }
3190 if (wpa_s == NULL)
3191 return -1;
3192
3193 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3194}
3195
3196
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003197static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3198{
3199 struct wpa_supplicant *wpa_s = ctx;
3200
3201 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3202 struct wpa_ssid *ssid = wpa_s->current_ssid;
3203 if (ssid == NULL)
3204 continue;
3205 if (ssid->mode != WPAS_MODE_INFRA)
3206 continue;
3207 if (wpa_s->wpa_state != WPA_COMPLETED &&
3208 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3209 continue;
3210 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3211 return 1;
3212 }
3213
3214 return 0;
3215}
3216
3217
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003218static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3219{
3220 struct wpa_supplicant *wpa_s = ctx;
3221 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3222}
3223
3224
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003225int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3226{
3227 struct wpa_interface iface;
3228 struct wpa_supplicant *p2pdev_wpa_s;
3229 char ifname[100];
3230 char force_name[100];
3231 int ret;
3232
3233 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3234 wpa_s->ifname);
3235 force_name[0] = '\0';
3236 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3237 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3238 force_name, wpa_s->pending_interface_addr, NULL);
3239 if (ret < 0) {
3240 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3241 return ret;
3242 }
3243 os_strlcpy(wpa_s->pending_interface_name, ifname,
3244 sizeof(wpa_s->pending_interface_name));
3245
3246 os_memset(&iface, 0, sizeof(iface));
3247 iface.p2p_mgmt = 1;
3248 iface.ifname = wpa_s->pending_interface_name;
3249 iface.driver = wpa_s->driver->name;
3250 iface.driver_param = wpa_s->conf->driver_param;
3251 iface.confname = wpa_s->confname;
3252 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3253 if (!p2pdev_wpa_s) {
3254 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3255 return -1;
3256 }
3257 p2pdev_wpa_s->parent = wpa_s;
3258
3259 wpa_s->pending_interface_name[0] = '\0';
3260 return 0;
3261}
3262
3263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264/**
3265 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3266 * @global: Pointer to global data from wpa_supplicant_init()
3267 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3268 * Returns: 0 on success, -1 on failure
3269 */
3270int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3271{
3272 struct p2p_config p2p;
3273 unsigned int r;
3274 int i;
3275
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003276 if (wpa_s->conf->p2p_disabled)
3277 return 0;
3278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3280 return 0;
3281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 if (global->p2p)
3283 return 0;
3284
3285 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3286 struct p2p_params params;
3287
3288 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3289 os_memset(&params, 0, sizeof(params));
3290 params.dev_name = wpa_s->conf->device_name;
3291 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3292 WPS_DEV_TYPE_LEN);
3293 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3294 os_memcpy(params.sec_dev_type,
3295 wpa_s->conf->sec_device_type,
3296 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3297
3298 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3299 return -1;
3300
3301 return 0;
3302 }
3303
3304 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003305 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003306 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 p2p.p2p_scan = wpas_p2p_scan;
3308 p2p.send_action = wpas_send_action;
3309 p2p.send_action_done = wpas_send_action_done;
3310 p2p.go_neg_completed = wpas_go_neg_completed;
3311 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3312 p2p.dev_found = wpas_dev_found;
3313 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003314 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003315 p2p.start_listen = wpas_start_listen;
3316 p2p.stop_listen = wpas_stop_listen;
3317 p2p.send_probe_resp = wpas_send_probe_resp;
3318 p2p.sd_request = wpas_sd_request;
3319 p2p.sd_response = wpas_sd_response;
3320 p2p.prov_disc_req = wpas_prov_disc_req;
3321 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003322 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003323 p2p.invitation_process = wpas_invitation_process;
3324 p2p.invitation_received = wpas_invitation_received;
3325 p2p.invitation_result = wpas_invitation_result;
3326 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003327 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003328
3329 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003330 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331 p2p.dev_name = wpa_s->conf->device_name;
3332 p2p.manufacturer = wpa_s->conf->manufacturer;
3333 p2p.model_name = wpa_s->conf->model_name;
3334 p2p.model_number = wpa_s->conf->model_number;
3335 p2p.serial_number = wpa_s->conf->serial_number;
3336 if (wpa_s->wps) {
3337 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3338 p2p.config_methods = wpa_s->wps->config_methods;
3339 }
3340
3341 if (wpa_s->conf->p2p_listen_reg_class &&
3342 wpa_s->conf->p2p_listen_channel) {
3343 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3344 p2p.channel = wpa_s->conf->p2p_listen_channel;
3345 } else {
3346 p2p.reg_class = 81;
3347 /*
3348 * Pick one of the social channels randomly as the listen
3349 * channel.
3350 */
3351 os_get_random((u8 *) &r, sizeof(r));
3352 p2p.channel = 1 + (r % 3) * 5;
3353 }
3354 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3355
3356 if (wpa_s->conf->p2p_oper_reg_class &&
3357 wpa_s->conf->p2p_oper_channel) {
3358 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3359 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3360 p2p.cfg_op_channel = 1;
3361 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3362 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3363
3364 } else {
3365 p2p.op_reg_class = 81;
3366 /*
3367 * Use random operation channel from (1, 6, 11) if no other
3368 * preference is indicated.
3369 */
3370 os_get_random((u8 *) &r, sizeof(r));
3371 p2p.op_channel = 1 + (r % 3) * 5;
3372 p2p.cfg_op_channel = 0;
3373 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3374 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3375 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003376
3377 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3378 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3379 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3380 }
3381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3383 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3384 p2p.country[2] = 0x04;
3385 } else
3386 os_memcpy(p2p.country, "XX\x04", 3);
3387
3388 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3389 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3390 "channel list");
3391 return -1;
3392 }
3393
3394 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3395 WPS_DEV_TYPE_LEN);
3396
3397 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3398 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3399 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3400
3401 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3402 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3403
3404 p2p.max_peers = 100;
3405
3406 if (wpa_s->conf->p2p_ssid_postfix) {
3407 p2p.ssid_postfix_len =
3408 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3409 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3410 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3411 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3412 p2p.ssid_postfix_len);
3413 }
3414
3415 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3416
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003417 p2p.max_listen = wpa_s->max_remain_on_chan;
3418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419 global->p2p = p2p_init(&p2p);
3420 if (global->p2p == NULL)
3421 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003422 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003423
3424 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3425 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3426 continue;
3427 p2p_add_wps_vendor_extension(
3428 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3429 }
3430
3431 return 0;
3432}
3433
3434
3435/**
3436 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3437 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3438 *
3439 * This function deinitialize per-interface P2P data.
3440 */
3441void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3442{
3443 if (wpa_s->driver && wpa_s->drv_priv)
3444 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003445
3446 if (wpa_s->go_params) {
3447 /* Clear any stored provisioning info */
3448 p2p_clear_provisioning_info(
3449 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003450 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003451 }
3452
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003453 os_free(wpa_s->go_params);
3454 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003455#ifdef ANDROID_P2P
3456 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3457#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003458 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3459 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3460 wpa_s->p2p_long_listen = 0;
3461 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3462 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3463 wpas_p2p_remove_pending_group_interface(wpa_s);
3464
3465 /* TODO: remove group interface from the driver if this wpa_s instance
3466 * is on top of a P2P group interface */
3467}
3468
3469
3470/**
3471 * wpas_p2p_deinit_global - Deinitialize global P2P module
3472 * @global: Pointer to global data from wpa_supplicant_init()
3473 *
3474 * This function deinitializes the global (per device) P2P module.
3475 */
3476void wpas_p2p_deinit_global(struct wpa_global *global)
3477{
3478 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003479
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003480 wpa_s = global->ifaces;
3481 if (wpa_s)
3482 wpas_p2p_service_flush(wpa_s);
3483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003484 if (global->p2p == NULL)
3485 return;
3486
3487 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3489 wpa_s = wpa_s->next;
3490 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003491 tmp = global->ifaces;
3492 while (tmp &&
3493 (tmp == wpa_s ||
3494 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3495 tmp = tmp->next;
3496 }
3497 if (tmp == NULL)
3498 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003499 /* Disconnect from the P2P group and deinit the interface */
3500 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 }
3502
3503 /*
3504 * Deinit GO data on any possibly remaining interface (if main
3505 * interface is used as GO).
3506 */
3507 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3508 if (wpa_s->ap_iface)
3509 wpas_p2p_group_deinit(wpa_s);
3510 }
3511
3512 p2p_deinit(global->p2p);
3513 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003514 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515}
3516
3517
3518static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3519{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003520 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3521 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003522 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523 if (wpa_s->drv_flags &
3524 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3525 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3526 return 1; /* P2P group requires a new interface in every case
3527 */
3528 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3529 return 0; /* driver does not support concurrent operations */
3530 if (wpa_s->global->ifaces->next)
3531 return 1; /* more that one interface already in use */
3532 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3533 return 1; /* this interface is already in use */
3534 return 0;
3535}
3536
3537
3538static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3539 const u8 *peer_addr,
3540 enum p2p_wps_method wps_method,
3541 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003542 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003543 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003544{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003545 if (persistent_group && wpa_s->conf->persistent_reconnect)
3546 persistent_group = 2;
3547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003548 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3549 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3550 go_intent, own_interface_addr,
3551 force_freq, persistent_group);
3552 }
3553
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003554 /*
3555 * Increase GO config timeout if HT40 is used since it takes some time
3556 * to scan channels for coex purposes before the BSS can be started.
3557 */
3558 p2p_set_config_timeout(wpa_s->global->p2p,
3559 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3562 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003563 persistent_group, ssid ? ssid->ssid : NULL,
3564 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003565 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566}
3567
3568
3569static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3570 const u8 *peer_addr,
3571 enum p2p_wps_method wps_method,
3572 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003573 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003574 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003576 if (persistent_group && wpa_s->conf->persistent_reconnect)
3577 persistent_group = 2;
3578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3580 return -1;
3581
3582 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3583 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003584 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003585 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586}
3587
3588
3589static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3590{
3591 wpa_s->p2p_join_scan_count++;
3592 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3593 wpa_s->p2p_join_scan_count);
3594 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3595 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3596 " for join operationg - stop join attempt",
3597 MAC2STR(wpa_s->pending_join_iface_addr));
3598 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003599 if (wpa_s->p2p_auto_pd) {
3600 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003601 wpa_msg_global(wpa_s, MSG_INFO,
3602 P2P_EVENT_PROV_DISC_FAILURE
3603 " p2p_dev_addr=" MACSTR " status=N/A",
3604 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003605 return;
3606 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003607 wpa_msg_global(wpa_s->parent, MSG_INFO,
3608 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 }
3610}
3611
3612
Dmitry Shmidt04949592012-07-19 12:16:46 -07003613static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3614{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003615 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003616
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003617 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3618 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003619 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003620 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003621
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003622 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3623 if (!freqs)
3624 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003625
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003626 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3627 wpa_s->num_multichan_concurrent);
3628 if (num < 0) {
3629 res = 1;
3630 goto exit_free;
3631 }
3632
3633 for (i = 0; i < num; i++) {
3634 if (freqs[i] == freq) {
3635 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3636 freq);
3637 res = 0;
3638 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003639 }
3640 }
3641
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003642 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003643
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003644exit_free:
3645 os_free(freqs);
3646 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003647}
3648
3649
3650static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3651 const u8 *peer_dev_addr)
3652{
3653 struct wpa_bss *bss;
3654 int updated;
3655
3656 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3657 if (bss == NULL)
3658 return -1;
3659 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3660 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3661 "last scan");
3662 return 0;
3663 }
3664
3665 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3666 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3667 "%ld.%06ld (%supdated in last scan)",
3668 bss->last_update.sec, bss->last_update.usec,
3669 updated ? "": "not ");
3670
3671 return updated;
3672}
3673
3674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3676 struct wpa_scan_results *scan_res)
3677{
3678 struct wpa_bss *bss;
3679 int freq;
3680 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3683
3684 if (wpa_s->global->p2p_disabled)
3685 return;
3686
Dmitry Shmidt04949592012-07-19 12:16:46 -07003687 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3688 scan_res ? (int) scan_res->num : -1,
3689 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003690
3691 if (scan_res)
3692 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3693
Dmitry Shmidt04949592012-07-19 12:16:46 -07003694 if (wpa_s->p2p_auto_pd) {
3695 int join = wpas_p2p_peer_go(wpa_s,
3696 wpa_s->pending_join_dev_addr);
3697 if (join == 0 &&
3698 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3699 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003700 bss = wpa_bss_get_bssid_latest(
3701 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003702 if (bss) {
3703 freq = bss->freq;
3704 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3705 "the peer " MACSTR " at %d MHz",
3706 wpa_s->auto_pd_scan_retry,
3707 MAC2STR(wpa_s->
3708 pending_join_dev_addr),
3709 freq);
3710 wpas_p2p_join_scan_req(wpa_s, freq);
3711 return;
3712 }
3713 }
3714
3715 if (join < 0)
3716 join = 0;
3717
3718 wpa_s->p2p_auto_pd = 0;
3719 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3720 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3721 MAC2STR(wpa_s->pending_join_dev_addr), join);
3722 if (p2p_prov_disc_req(wpa_s->global->p2p,
3723 wpa_s->pending_join_dev_addr,
3724 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003725 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003726 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003727 wpa_msg_global(wpa_s, MSG_INFO,
3728 P2P_EVENT_PROV_DISC_FAILURE
3729 " p2p_dev_addr=" MACSTR " status=N/A",
3730 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003731 }
3732 return;
3733 }
3734
3735 if (wpa_s->p2p_auto_join) {
3736 int join = wpas_p2p_peer_go(wpa_s,
3737 wpa_s->pending_join_dev_addr);
3738 if (join < 0) {
3739 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3740 "running a GO -> use GO Negotiation");
3741 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3742 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3743 wpa_s->p2p_persistent_group, 0, 0, 0,
3744 wpa_s->p2p_go_intent,
3745 wpa_s->p2p_connect_freq,
3746 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003747 wpa_s->p2p_pd_before_go_neg,
3748 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003749 return;
3750 }
3751
3752 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3753 "try to join the group", join ? "" :
3754 " in older scan");
3755 if (!join)
3756 wpa_s->p2p_fallback_to_go_neg = 1;
3757 }
3758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3760 wpa_s->pending_join_iface_addr);
3761 if (freq < 0 &&
3762 p2p_get_interface_addr(wpa_s->global->p2p,
3763 wpa_s->pending_join_dev_addr,
3764 iface_addr) == 0 &&
3765 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3766 {
3767 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3768 "address for join from " MACSTR " to " MACSTR
3769 " based on newly discovered P2P peer entry",
3770 MAC2STR(wpa_s->pending_join_iface_addr),
3771 MAC2STR(iface_addr));
3772 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3773 ETH_ALEN);
3774
3775 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3776 wpa_s->pending_join_iface_addr);
3777 }
3778 if (freq >= 0) {
3779 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3780 "from P2P peer table: %d MHz", freq);
3781 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003782 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783 if (bss) {
3784 freq = bss->freq;
3785 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07003786 "from BSS table: %d MHz (SSID %s)", freq,
3787 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788 }
3789 if (freq > 0) {
3790 u16 method;
3791
Dmitry Shmidt04949592012-07-19 12:16:46 -07003792 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003793 wpa_msg_global(wpa_s->parent, MSG_INFO,
3794 P2P_EVENT_GROUP_FORMATION_FAILURE
3795 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003796 return;
3797 }
3798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003799 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3800 "prior to joining an existing group (GO " MACSTR
3801 " freq=%u MHz)",
3802 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3803 wpa_s->pending_pd_before_join = 1;
3804
3805 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806 case WPS_PIN_DISPLAY:
3807 method = WPS_CONFIG_KEYPAD;
3808 break;
3809 case WPS_PIN_KEYPAD:
3810 method = WPS_CONFIG_DISPLAY;
3811 break;
3812 case WPS_PBC:
3813 method = WPS_CONFIG_PUSHBUTTON;
3814 break;
3815 default:
3816 method = 0;
3817 break;
3818 }
3819
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003820 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3821 wpa_s->pending_join_dev_addr) ==
3822 method)) {
3823 /*
3824 * We have already performed provision discovery for
3825 * joining the group. Proceed directly to join
3826 * operation without duplicated provision discovery. */
3827 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3828 "with " MACSTR " already done - proceed to "
3829 "join",
3830 MAC2STR(wpa_s->pending_join_dev_addr));
3831 wpa_s->pending_pd_before_join = 0;
3832 goto start;
3833 }
3834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003836 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003837 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3839 "Discovery Request before joining an "
3840 "existing group");
3841 wpa_s->pending_pd_before_join = 0;
3842 goto start;
3843 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844 return;
3845 }
3846
3847 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3848 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3849 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3850 wpas_p2p_check_join_scan_limit(wpa_s);
3851 return;
3852
3853start:
3854 /* Start join operation immediately */
3855 wpas_p2p_join_start(wpa_s);
3856}
3857
3858
Dmitry Shmidt04949592012-07-19 12:16:46 -07003859static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003860{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003861 int ret;
3862 struct wpa_driver_scan_params params;
3863 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003864 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003865 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003866#ifdef ANDROID_P2P
3867 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003869 /* If freq is not provided, check the operating freq of the GO and do a
3870 * a directed scan to save time
3871 */
3872 if(!freq) {
3873 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3874 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3875 }
3876#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003877 os_memset(&params, 0, sizeof(params));
3878
3879 /* P2P Wildcard SSID */
3880 params.num_ssids = 1;
3881 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3882 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3883
3884 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003885 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3886 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3887 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 if (wps_ie == NULL) {
3889 wpas_p2p_scan_res_join(wpa_s, NULL);
3890 return;
3891 }
3892
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003893 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3894 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003895 if (ies == NULL) {
3896 wpabuf_free(wps_ie);
3897 wpas_p2p_scan_res_join(wpa_s, NULL);
3898 return;
3899 }
3900 wpabuf_put_buf(ies, wps_ie);
3901 wpabuf_free(wps_ie);
3902
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003903 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003905 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003906 params.extra_ies = wpabuf_head(ies);
3907 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003908 if (freq > 0) {
3909 freqs[0] = freq;
3910 params.freqs = freqs;
3911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912
3913 /*
3914 * Run a scan to update BSS table and start Provision Discovery once
3915 * the new scan results become available.
3916 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003917 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003918 if (!ret) {
3919 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003920 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003922
3923 wpabuf_free(ies);
3924
3925 if (ret) {
3926 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3927 "try again later");
3928 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3929 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3930 wpas_p2p_check_join_scan_limit(wpa_s);
3931 }
3932}
3933
3934
Dmitry Shmidt04949592012-07-19 12:16:46 -07003935static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3936{
3937 struct wpa_supplicant *wpa_s = eloop_ctx;
3938 wpas_p2p_join_scan_req(wpa_s, 0);
3939}
3940
3941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003942static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003943 const u8 *dev_addr, enum p2p_wps_method wps_method,
3944 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003945{
3946 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003947 MACSTR " dev " MACSTR ")%s",
3948 MAC2STR(iface_addr), MAC2STR(dev_addr),
3949 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950
Dmitry Shmidt04949592012-07-19 12:16:46 -07003951 wpa_s->p2p_auto_pd = 0;
3952 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3954 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3955 wpa_s->pending_join_wps_method = wps_method;
3956
3957 /* Make sure we are not running find during connection establishment */
3958 wpas_p2p_stop_find(wpa_s);
3959
3960 wpa_s->p2p_join_scan_count = 0;
3961 wpas_p2p_join_scan(wpa_s, NULL);
3962 return 0;
3963}
3964
3965
3966static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3967{
3968 struct wpa_supplicant *group;
3969 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003970 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003971
3972 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3973 if (group == NULL)
3974 return -1;
3975 if (group != wpa_s) {
3976 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3977 sizeof(group->p2p_pin));
3978 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003979 } else {
3980 /*
3981 * Need to mark the current interface for p2p_group_formation
3982 * when a separate group interface is not used. This is needed
3983 * to allow p2p_cancel stop a pending p2p_connect-join.
3984 * wpas_p2p_init_group_interface() addresses this for the case
3985 * where a separate group interface is used.
3986 */
3987 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988 }
3989
3990 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003991 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992
3993 os_memset(&res, 0, sizeof(res));
3994 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3995 ETH_ALEN);
3996 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003997 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003998 if (bss) {
3999 res.freq = bss->freq;
4000 res.ssid_len = bss->ssid_len;
4001 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07004002 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4003 "from BSS table: %d MHz (SSID %s)", bss->freq,
4004 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004005 }
4006
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004007 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4008 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4009 "starting client");
4010 wpa_drv_cancel_remain_on_channel(wpa_s);
4011 wpa_s->off_channel_freq = 0;
4012 wpa_s->roc_waiting_drv_freq = 0;
4013 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014 wpas_start_wps_enrollee(group, &res);
4015
4016 /*
4017 * Allow a longer timeout for join-a-running-group than normal 15
4018 * second group formation timeout since the GO may not have authorized
4019 * our connection yet.
4020 */
4021 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4022 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4023 wpa_s, NULL);
4024
4025 return 0;
4026}
4027
4028
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004029static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004030 int *force_freq, int *pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004031{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004032 int *freqs, res;
4033 unsigned int freq_in_use = 0, num, i;
4034
4035 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4036 if (!freqs)
4037 return -1;
4038
4039 num = get_shared_radio_freqs(wpa_s, freqs,
4040 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004041 wpa_printf(MSG_DEBUG,
4042 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4043 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004044
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004045 if (freq > 0) {
4046 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4047 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4048 "(%u MHz) is not supported for P2P uses",
4049 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004050 res = -3;
4051 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004052 }
4053
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004054 for (i = 0; i < num; i++) {
4055 if (freqs[i] == freq)
4056 freq_in_use = 1;
4057 }
4058
4059 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4060 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4061 freq);
4062 res = -2;
4063 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004064 }
4065 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4066 "requested channel (%u MHz)", freq);
4067 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004068 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004069 }
4070
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004071 for (i = 0; i < num; i++) {
4072 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4073 continue;
4074
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004075#ifndef ANDROID_P2P
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004076 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4077 *force_freq);
4078 *force_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004079#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004080
4081 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4082 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004083 *pref_freq = freqs[i];
4084#ifdef ANDROID_P2P
4085 } else {
4086 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4087 *force_freq);
4088 *force_freq = freqs[i];
4089#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004090 }
4091 break;
4092 }
4093
4094 if (i == num) {
4095 if (num < wpa_s->num_multichan_concurrent) {
4096 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4097 *force_freq = 0;
4098 } else {
4099 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4100 res = -2;
4101 goto exit_free;
4102 }
4103 }
4104
4105exit_ok:
4106 res = 0;
4107exit_free:
4108 os_free(freqs);
4109 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004110}
4111
4112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004113/**
4114 * wpas_p2p_connect - Request P2P Group Formation to be started
4115 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4116 * @peer_addr: Address of the peer P2P Device
4117 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4118 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004119 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120 * @join: Whether to join an existing group (as a client) instead of starting
4121 * Group Owner negotiation; @peer_addr is BSSID in that case
4122 * @auth: Whether to only authorize the connection instead of doing that and
4123 * initiating Group Owner negotiation
4124 * @go_intent: GO Intent or -1 to use default
4125 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004126 * @persistent_id: Persistent group credentials to use for forcing GO
4127 * parameters or -1 to generate new values (SSID/passphrase)
4128 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4129 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004130 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4132 * failure, -2 on failure due to channel not currently available,
4133 * -3 if forced channel is not supported
4134 */
4135int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4136 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004137 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004138 int go_intent, int freq, int persistent_id, int pd,
4139 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004140{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004141 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004142 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004144 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004145 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146
4147 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4148 return -1;
4149
Dmitry Shmidt04949592012-07-19 12:16:46 -07004150 if (persistent_id >= 0) {
4151 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4152 if (ssid == NULL || ssid->disabled != 2 ||
4153 ssid->mode != WPAS_MODE_P2P_GO)
4154 return -1;
4155 }
4156
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004157 os_free(wpa_s->global->add_psk);
4158 wpa_s->global->add_psk = NULL;
4159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004160 if (go_intent < 0)
4161 go_intent = wpa_s->conf->p2p_go_intent;
4162
4163 if (!auth)
4164 wpa_s->p2p_long_listen = 0;
4165
4166 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004167 wpa_s->p2p_persistent_group = !!persistent_group;
4168 wpa_s->p2p_persistent_id = persistent_id;
4169 wpa_s->p2p_go_intent = go_intent;
4170 wpa_s->p2p_connect_freq = freq;
4171 wpa_s->p2p_fallback_to_go_neg = 0;
4172 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004173 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174
4175 if (pin)
4176 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4177 else if (wps_method == WPS_PIN_DISPLAY) {
4178 ret = wps_generate_pin();
4179 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4180 ret);
4181 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4182 wpa_s->p2p_pin);
4183 } else
4184 wpa_s->p2p_pin[0] = '\0';
4185
Dmitry Shmidt04949592012-07-19 12:16:46 -07004186 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004187 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4188 if (auth) {
4189 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4190 "connect a running group from " MACSTR,
4191 MAC2STR(peer_addr));
4192 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4193 return ret;
4194 }
4195 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4196 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4197 iface_addr) < 0) {
4198 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4199 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4200 dev_addr);
4201 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004202 if (auto_join) {
4203 os_get_time(&wpa_s->p2p_auto_started);
4204 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4205 "%ld.%06ld",
4206 wpa_s->p2p_auto_started.sec,
4207 wpa_s->p2p_auto_started.usec);
4208 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004209 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004210 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4211 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004212 return -1;
4213 return ret;
4214 }
4215
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004216 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004217 if (res)
4218 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004219 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220
4221 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4222
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004223 if (wpa_s->create_p2p_iface) {
4224 /* Prepare to add a new interface for the group */
4225 iftype = WPA_IF_P2P_GROUP;
4226 if (go_intent == 15)
4227 iftype = WPA_IF_P2P_GO;
4228 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4229 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4230 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004231 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004232 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004234 if_addr = wpa_s->pending_interface_addr;
4235 } else
4236 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004237
4238 if (auth) {
4239 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004240 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004241 force_freq, persistent_group, ssid,
4242 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243 return -1;
4244 return ret;
4245 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004246
4247 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4248 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004249 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004250 if (wpa_s->create_p2p_iface)
4251 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252 return -1;
4253 }
4254 return ret;
4255}
4256
4257
4258/**
4259 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4260 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4261 * @freq: Frequency of the channel in MHz
4262 * @duration: Duration of the stay on the channel in milliseconds
4263 *
4264 * This callback is called when the driver indicates that it has started the
4265 * requested remain-on-channel duration.
4266 */
4267void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4268 unsigned int freq, unsigned int duration)
4269{
4270 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4271 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004272 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4273 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4274 wpa_s->pending_listen_duration);
4275 wpa_s->pending_listen_freq = 0;
4276 }
4277}
4278
4279
4280static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4281 unsigned int timeout)
4282{
4283 /* Limit maximum Listen state time based on driver limitation. */
4284 if (timeout > wpa_s->max_remain_on_chan)
4285 timeout = wpa_s->max_remain_on_chan;
4286
4287 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4288 return wpa_drv_p2p_listen(wpa_s, timeout);
4289
4290 return p2p_listen(wpa_s->global->p2p, timeout);
4291}
4292
4293
4294/**
4295 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4296 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4297 * @freq: Frequency of the channel in MHz
4298 *
4299 * This callback is called when the driver indicates that a remain-on-channel
4300 * operation has been completed, i.e., the duration on the requested channel
4301 * has timed out.
4302 */
4303void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4304 unsigned int freq)
4305{
4306 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4307 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004308 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4310 return;
4311 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4312 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004313 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004314 return;
4315 if (wpa_s->p2p_long_listen > 0)
4316 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4317 if (wpa_s->p2p_long_listen > 0) {
4318 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4319 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004320 } else {
4321 /*
4322 * When listen duration is over, stop listen & update p2p_state
4323 * to IDLE.
4324 */
4325 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326 }
4327}
4328
4329
4330/**
4331 * wpas_p2p_group_remove - Remove a P2P group
4332 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4333 * @ifname: Network interface name of the group interface or "*" to remove all
4334 * groups
4335 * Returns: 0 on success, -1 on failure
4336 *
4337 * This function is used to remove a P2P group. This can be used to disconnect
4338 * from a group in which the local end is a P2P Client or to end a P2P Group in
4339 * case the local end is the Group Owner. If a virtual network interface was
4340 * created for this group, that interface will be removed. Otherwise, only the
4341 * configured P2P group network will be removed from the interface.
4342 */
4343int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4344{
4345 struct wpa_global *global = wpa_s->global;
4346
4347 if (os_strcmp(ifname, "*") == 0) {
4348 struct wpa_supplicant *prev;
4349 wpa_s = global->ifaces;
4350 while (wpa_s) {
4351 prev = wpa_s;
4352 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004353 if (prev->p2p_group_interface !=
4354 NOT_P2P_GROUP_INTERFACE ||
4355 (prev->current_ssid &&
4356 prev->current_ssid->p2p_group))
4357 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004358 }
4359 return 0;
4360 }
4361
4362 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4363 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4364 break;
4365 }
4366
4367 return wpas_p2p_disconnect(wpa_s);
4368}
4369
4370
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004371static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4372{
4373 unsigned int r;
4374
4375 if (freq == 2) {
4376 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4377 "band");
4378 if (wpa_s->best_24_freq > 0 &&
4379 p2p_supported_freq(wpa_s->global->p2p,
4380 wpa_s->best_24_freq)) {
4381 freq = wpa_s->best_24_freq;
4382 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4383 "channel: %d MHz", freq);
4384 } else {
4385 os_get_random((u8 *) &r, sizeof(r));
4386 freq = 2412 + (r % 3) * 25;
4387 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4388 "channel: %d MHz", freq);
4389 }
4390 }
4391
4392 if (freq == 5) {
4393 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4394 "band");
4395 if (wpa_s->best_5_freq > 0 &&
4396 p2p_supported_freq(wpa_s->global->p2p,
4397 wpa_s->best_5_freq)) {
4398 freq = wpa_s->best_5_freq;
4399 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4400 "channel: %d MHz", freq);
4401 } else {
4402 os_get_random((u8 *) &r, sizeof(r));
4403 freq = 5180 + (r % 4) * 20;
4404 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4405 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4406 "5 GHz channel for P2P group");
4407 return -1;
4408 }
4409 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4410 "channel: %d MHz", freq);
4411 }
4412 }
4413
4414 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4415 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4416 "(%u MHz) is not supported for P2P uses",
4417 freq);
4418 return -1;
4419 }
4420
4421 return freq;
4422}
4423
4424
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004425static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4426 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004427 int freq, int ht40,
4428 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004429{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004430 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004431 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004432 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004433
4434 os_memset(params, 0, sizeof(*params));
4435 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004436 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004437 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004438 if (!freq_included(channels, freq)) {
4439 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4440 "accepted", freq);
4441 return -1;
4442 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004443 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4444 "frequency %d MHz", freq);
4445 params->freq = freq;
4446 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4447 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004448 wpa_s->conf->p2p_oper_channel <= 11 &&
4449 freq_included(channels,
4450 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004451 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4452 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4453 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004454 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4455 wpa_s->conf->p2p_oper_reg_class == 116 ||
4456 wpa_s->conf->p2p_oper_reg_class == 117 ||
4457 wpa_s->conf->p2p_oper_reg_class == 124 ||
4458 wpa_s->conf->p2p_oper_reg_class == 126 ||
4459 wpa_s->conf->p2p_oper_reg_class == 127) &&
4460 freq_included(channels,
4461 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4463 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4464 "frequency %d MHz", params->freq);
4465 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4466 wpa_s->best_overall_freq > 0 &&
4467 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004468 wpa_s->best_overall_freq) &&
4469 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470 params->freq = wpa_s->best_overall_freq;
4471 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4472 "channel %d MHz", params->freq);
4473 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4474 wpa_s->best_24_freq > 0 &&
4475 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004476 wpa_s->best_24_freq) &&
4477 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478 params->freq = wpa_s->best_24_freq;
4479 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4480 "channel %d MHz", params->freq);
4481 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4482 wpa_s->best_5_freq > 0 &&
4483 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004484 wpa_s->best_5_freq) &&
4485 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004486 params->freq = wpa_s->best_5_freq;
4487 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4488 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004489 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4490 channels))) {
4491 params->freq = pref_freq;
4492 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4493 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004494 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004495 int chan;
4496 for (chan = 0; chan < 11; chan++) {
4497 params->freq = 2412 + chan * 5;
4498 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004499 params->freq) &&
4500 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004501 break;
4502 }
4503 if (chan == 11) {
4504 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4505 "allowed");
4506 return -1;
4507 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4509 "known)", params->freq);
4510 }
4511
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004512 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4513 if (!freqs)
4514 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004516 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4517 wpa_s->num_multichan_concurrent);
4518 if (res < 0) {
4519 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004520 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004521 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004522 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004523
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004524 if (!freq) {
4525 for (i = 0; i < num; i++) {
4526 if (freq_included(channels, freqs[i])) {
4527 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4528 freqs[i]);
4529 params->freq = freqs[i];
4530 break;
4531 }
4532 }
4533
4534 if (i == num) {
4535 if (num == wpa_s->num_multichan_concurrent) {
4536 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4537 os_free(freqs);
4538 return -1;
4539 } else {
4540 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4541 }
4542 }
4543 } else {
4544 for (i = 0; i < num; i++) {
4545 if (freqs[i] == freq)
4546 break;
4547 }
4548
4549 if (i == num) {
4550 if (num == wpa_s->num_multichan_concurrent) {
4551 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4552 os_free(freqs);
4553 return -1;
4554 } else {
4555 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4556 }
4557 }
4558 }
4559 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004560 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004561}
4562
4563
4564static struct wpa_supplicant *
4565wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4566 int go)
4567{
4568 struct wpa_supplicant *group_wpa_s;
4569
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004570 if (!wpas_p2p_create_iface(wpa_s)) {
4571 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4572 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004573 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004574 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004575 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004576
4577 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004578 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004579 wpa_msg_global(wpa_s, MSG_ERROR,
4580 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004581 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004582 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004583 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4584 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004585 wpa_msg_global(wpa_s, MSG_ERROR,
4586 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587 wpas_p2p_remove_pending_group_interface(wpa_s);
4588 return NULL;
4589 }
4590
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004591 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4592 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004593 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004594 return group_wpa_s;
4595}
4596
4597
4598/**
4599 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4600 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4601 * @persistent_group: Whether to create a persistent group
4602 * @freq: Frequency for the group or 0 to indicate no hardcoding
4603 * Returns: 0 on success, -1 on failure
4604 *
4605 * This function creates a new P2P group with the local end as the Group Owner,
4606 * i.e., without using Group Owner Negotiation.
4607 */
4608int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004609 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004610{
4611 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612
4613 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4614 return -1;
4615
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004616 os_free(wpa_s->global->add_psk);
4617 wpa_s->global->add_psk = NULL;
4618
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004619 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004620 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004621 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004622
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004623 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4624 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004627 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004628 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004629 if (params.freq &&
4630 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4631 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4632 "(%u MHz) is not supported for P2P uses",
4633 params.freq);
4634 return -1;
4635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636 p2p_go_params(wpa_s->global->p2p, &params);
4637 params.persistent_group = persistent_group;
4638
4639 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4640 if (wpa_s == NULL)
4641 return -1;
4642 wpas_start_wps_go(wpa_s, &params, 0);
4643
4644 return 0;
4645}
4646
4647
4648static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4649 struct wpa_ssid *params, int addr_allocated)
4650{
4651 struct wpa_ssid *ssid;
4652
4653 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4654 if (wpa_s == NULL)
4655 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004656 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004657
4658 wpa_supplicant_ap_deinit(wpa_s);
4659
4660 ssid = wpa_config_add_network(wpa_s->conf);
4661 if (ssid == NULL)
4662 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004663 wpa_config_set_network_defaults(ssid);
4664 ssid->temporary = 1;
4665 ssid->proto = WPA_PROTO_RSN;
4666 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4667 ssid->group_cipher = WPA_CIPHER_CCMP;
4668 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4669 ssid->ssid = os_malloc(params->ssid_len);
4670 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671 wpa_config_remove_network(wpa_s->conf, ssid->id);
4672 return -1;
4673 }
4674 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4675 ssid->ssid_len = params->ssid_len;
4676 ssid->p2p_group = 1;
4677 ssid->export_keys = 1;
4678 if (params->psk_set) {
4679 os_memcpy(ssid->psk, params->psk, 32);
4680 ssid->psk_set = 1;
4681 }
4682 if (params->passphrase)
4683 ssid->passphrase = os_strdup(params->passphrase);
4684
4685 wpa_supplicant_select_network(wpa_s, ssid);
4686
4687 wpa_s->show_group_started = 1;
4688
4689 return 0;
4690}
4691
4692
4693int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4694 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004695 int freq, int ht40,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004696 const struct p2p_channels *channels,
4697 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004698{
4699 struct p2p_go_neg_results params;
4700 int go = 0;
4701
4702 if (ssid->disabled != 2 || ssid->ssid == NULL)
4703 return -1;
4704
4705 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4706 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4707 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4708 "already running");
4709 return 0;
4710 }
4711
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004712 os_free(wpa_s->global->add_psk);
4713 wpa_s->global->add_psk = NULL;
4714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004715 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004716 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004717
Dmitry Shmidt04949592012-07-19 12:16:46 -07004718 wpa_s->p2p_fallback_to_go_neg = 0;
4719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720 if (ssid->mode == WPAS_MODE_INFRA)
4721 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4722
4723 if (ssid->mode != WPAS_MODE_P2P_GO)
4724 return -1;
4725
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004726 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4727 if (freq < 0)
4728 return -1;
4729
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004730 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004731 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004732
4733 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004734 params.psk_set = ssid->psk_set;
4735 if (params.psk_set)
4736 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004737 if (ssid->passphrase) {
4738 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4739 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4740 "persistent group");
4741 return -1;
4742 }
4743 os_strlcpy(params.passphrase, ssid->passphrase,
4744 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004746 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4747 params.ssid_len = ssid->ssid_len;
4748 params.persistent_group = 1;
4749
4750 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4751 if (wpa_s == NULL)
4752 return -1;
4753
Dmitry Shmidt56052862013-10-04 10:23:25 -07004754 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004755 wpas_start_wps_go(wpa_s, &params, 0);
4756
4757 return 0;
4758}
4759
4760
4761static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4762 struct wpabuf *proberesp_ies)
4763{
4764 struct wpa_supplicant *wpa_s = ctx;
4765 if (wpa_s->ap_iface) {
4766 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004767 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4768 wpabuf_free(beacon_ies);
4769 wpabuf_free(proberesp_ies);
4770 return;
4771 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004772 if (beacon_ies) {
4773 wpabuf_free(hapd->p2p_beacon_ie);
4774 hapd->p2p_beacon_ie = beacon_ies;
4775 }
4776 wpabuf_free(hapd->p2p_probe_resp_ie);
4777 hapd->p2p_probe_resp_ie = proberesp_ies;
4778 } else {
4779 wpabuf_free(beacon_ies);
4780 wpabuf_free(proberesp_ies);
4781 }
4782 wpa_supplicant_ap_update_beacon(wpa_s);
4783}
4784
4785
4786static void wpas_p2p_idle_update(void *ctx, int idle)
4787{
4788 struct wpa_supplicant *wpa_s = ctx;
4789 if (!wpa_s->ap_iface)
4790 return;
4791 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004792 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004793 wpas_p2p_set_group_idle_timeout(wpa_s);
4794 else
4795 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4796}
4797
4798
4799struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004800 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801{
4802 struct p2p_group *group;
4803 struct p2p_group_config *cfg;
4804
4805 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4806 return NULL;
4807 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4808 return NULL;
4809
4810 cfg = os_zalloc(sizeof(*cfg));
4811 if (cfg == NULL)
4812 return NULL;
4813
Dmitry Shmidt04949592012-07-19 12:16:46 -07004814 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004815 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004816 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004817 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4819 if (wpa_s->max_stations &&
4820 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4821 cfg->max_clients = wpa_s->max_stations;
4822 else
4823 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004824 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4825 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826 cfg->cb_ctx = wpa_s;
4827 cfg->ie_update = wpas_p2p_ie_update;
4828 cfg->idle_update = wpas_p2p_idle_update;
4829
4830 group = p2p_group_init(wpa_s->global->p2p, cfg);
4831 if (group == NULL)
4832 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004833 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004834 p2p_group_notif_formation_done(group);
4835 wpa_s->p2p_group = group;
4836 return group;
4837}
4838
4839
4840void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4841 int registrar)
4842{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004843 struct wpa_ssid *ssid = wpa_s->current_ssid;
4844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845 if (!wpa_s->p2p_in_provisioning) {
4846 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4847 "provisioning not in progress");
4848 return;
4849 }
4850
Dmitry Shmidt04949592012-07-19 12:16:46 -07004851 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4852 u8 go_dev_addr[ETH_ALEN];
4853 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4854 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4855 ssid->ssid_len);
4856 /* Clear any stored provisioning info */
4857 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4858 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004860 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4861 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004862 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004863 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4864 /*
4865 * Use a separate timeout for initial data connection to
4866 * complete to allow the group to be removed automatically if
4867 * something goes wrong in this step before the P2P group idle
4868 * timeout mechanism is taken into use.
4869 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07004870 wpa_dbg(wpa_s, MSG_DEBUG,
4871 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4872 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004873 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4874 wpas_p2p_group_formation_timeout,
4875 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004876 } else if (ssid) {
4877 /*
4878 * Use a separate timeout for initial data connection to
4879 * complete to allow the group to be removed automatically if
4880 * the client does not complete data connection successfully.
4881 */
4882 wpa_dbg(wpa_s, MSG_DEBUG,
4883 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
4884 P2P_MAX_INITIAL_CONN_WAIT_GO);
4885 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
4886 wpas_p2p_group_formation_timeout,
4887 wpa_s->parent, NULL);
4888 /*
4889 * Complete group formation on first successful data connection
4890 */
4891 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004892 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893 if (wpa_s->global->p2p)
4894 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4895 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4896 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4897 wpas_group_formation_completed(wpa_s, 1);
4898}
4899
4900
Jouni Malinen75ecf522011-06-27 15:19:46 -07004901void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4902 struct wps_event_fail *fail)
4903{
4904 if (!wpa_s->p2p_in_provisioning) {
4905 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4906 "provisioning not in progress");
4907 return;
4908 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004909
4910 if (wpa_s->go_params) {
4911 p2p_clear_provisioning_info(
4912 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004913 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004914 }
4915
Jouni Malinen75ecf522011-06-27 15:19:46 -07004916 wpas_notify_p2p_wps_failed(wpa_s, fail);
4917}
4918
4919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004921 const char *config_method,
4922 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923{
4924 u16 config_methods;
4925
Dmitry Shmidt04949592012-07-19 12:16:46 -07004926 wpa_s->p2p_fallback_to_go_neg = 0;
4927 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004928 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004929 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004930 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004932 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4933 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004934 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004935 else {
4936 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004938 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004939
Dmitry Shmidt04949592012-07-19 12:16:46 -07004940 if (use == WPAS_P2P_PD_AUTO) {
4941 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4942 wpa_s->pending_pd_config_methods = config_methods;
4943 wpa_s->p2p_auto_pd = 1;
4944 wpa_s->p2p_auto_join = 0;
4945 wpa_s->pending_pd_before_join = 0;
4946 wpa_s->auto_pd_scan_retry = 0;
4947 wpas_p2p_stop_find(wpa_s);
4948 wpa_s->p2p_join_scan_count = 0;
4949 os_get_time(&wpa_s->p2p_auto_started);
4950 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4951 wpa_s->p2p_auto_started.sec,
4952 wpa_s->p2p_auto_started.usec);
4953 wpas_p2p_join_scan(wpa_s, NULL);
4954 return 0;
4955 }
4956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4958 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004959 config_methods,
4960 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961 }
4962
4963 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4964 return -1;
4965
4966 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004967 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004968 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004969}
4970
4971
4972int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4973 char *end)
4974{
4975 return p2p_scan_result_text(ies, ies_len, buf, end);
4976}
4977
4978
4979static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4980{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004981 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004982 return;
4983
4984 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4985 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004986 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987}
4988
4989
4990int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4991 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004992 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004993 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004994{
4995 wpas_p2p_clear_pending_action_tx(wpa_s);
4996 wpa_s->p2p_long_listen = 0;
4997
4998 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4999 return wpa_drv_p2p_find(wpa_s, timeout, type);
5000
Dmitry Shmidt04949592012-07-19 12:16:46 -07005001 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5002 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003 return -1;
5004
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005005 wpa_supplicant_cancel_sched_scan(wpa_s);
5006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005007 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005008 num_req_dev_types, req_dev_types, dev_id,
5009 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010}
5011
5012
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005013static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014{
5015 wpas_p2p_clear_pending_action_tx(wpa_s);
5016 wpa_s->p2p_long_listen = 0;
5017 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5018 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07005019 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005020
5021 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5022 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005023 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024 }
5025
5026 if (wpa_s->global->p2p)
5027 p2p_stop_find(wpa_s->global->p2p);
5028
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005029 return 0;
5030}
5031
5032
5033void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5034{
5035 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5036 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005037 wpas_p2p_remove_pending_group_interface(wpa_s);
5038}
5039
5040
5041static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5042{
5043 struct wpa_supplicant *wpa_s = eloop_ctx;
5044 wpa_s->p2p_long_listen = 0;
5045}
5046
5047
5048int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5049{
5050 int res;
5051
5052 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5053 return -1;
5054
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005055 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056 wpas_p2p_clear_pending_action_tx(wpa_s);
5057
5058 if (timeout == 0) {
5059 /*
5060 * This is a request for unlimited Listen state. However, at
5061 * least for now, this is mapped to a Listen state for one
5062 * hour.
5063 */
5064 timeout = 3600;
5065 }
5066 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5067 wpa_s->p2p_long_listen = 0;
5068
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005069 /*
5070 * Stop previous find/listen operation to avoid trying to request a new
5071 * remain-on-channel operation while the driver is still running the
5072 * previous one.
5073 */
5074 if (wpa_s->global->p2p)
5075 p2p_stop_find(wpa_s->global->p2p);
5076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005077 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5078 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5079 wpa_s->p2p_long_listen = timeout * 1000;
5080 eloop_register_timeout(timeout, 0,
5081 wpas_p2p_long_listen_timeout,
5082 wpa_s, NULL);
5083 }
5084
5085 return res;
5086}
5087
5088
5089int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5090 u8 *buf, size_t len, int p2p_group)
5091{
5092 struct wpabuf *p2p_ie;
5093 int ret;
5094
5095 if (wpa_s->global->p2p_disabled)
5096 return -1;
5097 if (wpa_s->global->p2p == NULL)
5098 return -1;
5099 if (bss == NULL)
5100 return -1;
5101
5102 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5103 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5104 p2p_group, p2p_ie);
5105 wpabuf_free(p2p_ie);
5106
5107 return ret;
5108}
5109
5110
5111int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005112 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005113 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114{
5115 if (wpa_s->global->p2p_disabled)
5116 return 0;
5117 if (wpa_s->global->p2p == NULL)
5118 return 0;
5119
Dmitry Shmidt04949592012-07-19 12:16:46 -07005120 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5121 ie, ie_len)) {
5122 case P2P_PREQ_NOT_P2P:
5123 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5124 ssi_signal);
5125 /* fall through */
5126 case P2P_PREQ_MALFORMED:
5127 case P2P_PREQ_NOT_LISTEN:
5128 case P2P_PREQ_NOT_PROCESSED:
5129 default: /* make gcc happy */
5130 return 0;
5131 case P2P_PREQ_PROCESSED:
5132 return 1;
5133 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134}
5135
5136
5137void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5138 const u8 *sa, const u8 *bssid,
5139 u8 category, const u8 *data, size_t len, int freq)
5140{
5141 if (wpa_s->global->p2p_disabled)
5142 return;
5143 if (wpa_s->global->p2p == NULL)
5144 return;
5145
5146 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5147 freq);
5148}
5149
5150
5151void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5152{
5153 if (wpa_s->global->p2p_disabled)
5154 return;
5155 if (wpa_s->global->p2p == NULL)
5156 return;
5157
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005158 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005159}
5160
5161
5162void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5163{
5164 p2p_group_deinit(wpa_s->p2p_group);
5165 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005166
5167 wpa_s->ap_configured_cb = NULL;
5168 wpa_s->ap_configured_cb_ctx = NULL;
5169 wpa_s->ap_configured_cb_data = NULL;
5170 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005171}
5172
5173
5174int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5175{
5176 wpa_s->p2p_long_listen = 0;
5177
5178 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5179 return wpa_drv_p2p_reject(wpa_s, addr);
5180
5181 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5182 return -1;
5183
5184 return p2p_reject(wpa_s->global->p2p, addr);
5185}
5186
5187
5188/* Invite to reinvoke a persistent group */
5189int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005190 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005191 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005192{
5193 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005194 u8 *bssid = NULL;
5195 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005196 int res;
5197
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005198 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005199 if (peer_addr)
5200 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5201 else
5202 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005203
Jouni Malinen31be0a42012-08-31 21:20:51 +03005204 wpa_s->p2p_persistent_go_freq = freq;
5205 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005206 if (ssid->mode == WPAS_MODE_P2P_GO) {
5207 role = P2P_INVITE_ROLE_GO;
5208 if (peer_addr == NULL) {
5209 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5210 "address in invitation command");
5211 return -1;
5212 }
5213 if (wpas_p2p_create_iface(wpa_s)) {
5214 if (wpas_p2p_add_group_interface(wpa_s,
5215 WPA_IF_P2P_GO) < 0) {
5216 wpa_printf(MSG_ERROR, "P2P: Failed to "
5217 "allocate a new interface for the "
5218 "group");
5219 return -1;
5220 }
5221 bssid = wpa_s->pending_interface_addr;
5222 } else
5223 bssid = wpa_s->own_addr;
5224 } else {
5225 role = P2P_INVITE_ROLE_CLIENT;
5226 peer_addr = ssid->bssid;
5227 }
5228 wpa_s->pending_invite_ssid_id = ssid->id;
5229
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005230 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005231 if (res)
5232 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005234 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5235 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5236 ssid->ssid, ssid->ssid_len,
5237 go_dev_addr, 1);
5238
5239 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5240 return -1;
5241
5242 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005243 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5244 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005245}
5246
5247
5248/* Invite to join an active group */
5249int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5250 const u8 *peer_addr, const u8 *go_dev_addr)
5251{
5252 struct wpa_global *global = wpa_s->global;
5253 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005254 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005256 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005257 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005258 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259
Jouni Malinen31be0a42012-08-31 21:20:51 +03005260 wpa_s->p2p_persistent_go_freq = 0;
5261 wpa_s->p2p_go_ht40 = 0;
5262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005263 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5264 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5265 break;
5266 }
5267 if (wpa_s == NULL) {
5268 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5269 return -1;
5270 }
5271
5272 ssid = wpa_s->current_ssid;
5273 if (ssid == NULL) {
5274 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5275 "invitation");
5276 return -1;
5277 }
5278
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005279 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005280 persistent = ssid->p2p_persistent_group &&
5281 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5282 ssid->ssid, ssid->ssid_len);
5283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005284 if (ssid->mode == WPAS_MODE_P2P_GO) {
5285 role = P2P_INVITE_ROLE_ACTIVE_GO;
5286 bssid = wpa_s->own_addr;
5287 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005288 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005289 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005290 } else {
5291 role = P2P_INVITE_ROLE_CLIENT;
5292 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5293 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5294 "invite to current group");
5295 return -1;
5296 }
5297 bssid = wpa_s->bssid;
5298 if (go_dev_addr == NULL &&
5299 !is_zero_ether_addr(wpa_s->go_dev_addr))
5300 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005301 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5302 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005303 }
5304 wpa_s->parent->pending_invite_ssid_id = -1;
5305
5306 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5307 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5308 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005309 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005310
5311 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5312 return -1;
5313
Dmitry Shmidt051af732013-10-22 13:52:46 -07005314 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005315 if (res)
5316 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005317 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005319 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005320 ssid->ssid, ssid->ssid_len, force_freq,
5321 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005322}
5323
5324
5325void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5326{
5327 struct wpa_ssid *ssid = wpa_s->current_ssid;
5328 const char *ssid_txt;
5329 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005330 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005331 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005332 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005333
Dmitry Shmidt04949592012-07-19 12:16:46 -07005334 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5335 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5336 wpa_s->parent, NULL);
5337 }
5338
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005339 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005340 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005341
5342 wpa_s->show_group_started = 0;
5343
5344 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5345 os_memset(go_dev_addr, 0, ETH_ALEN);
5346 if (ssid->bssid_set)
5347 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5348 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5349 ssid->ssid_len);
5350 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5351
5352 if (wpa_s->global->p2p_group_formation == wpa_s)
5353 wpa_s->global->p2p_group_formation = NULL;
5354
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005355 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5356 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005357 if (ssid->passphrase == NULL && ssid->psk_set) {
5358 char psk[65];
5359 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005360 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5361 "%s client ssid=\"%s\" freq=%d psk=%s "
5362 "go_dev_addr=" MACSTR "%s",
5363 wpa_s->ifname, ssid_txt, freq, psk,
5364 MAC2STR(go_dev_addr),
5365 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005366 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005367 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5368 "%s client ssid=\"%s\" freq=%d "
5369 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5370 wpa_s->ifname, ssid_txt, freq,
5371 ssid->passphrase ? ssid->passphrase : "",
5372 MAC2STR(go_dev_addr),
5373 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005374 }
5375
5376 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005377 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5378 ssid, go_dev_addr);
5379 if (network_id < 0)
5380 network_id = ssid->id;
5381 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005382
5383done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005384 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005385}
5386
5387
5388int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5389 u32 interval1, u32 duration2, u32 interval2)
5390{
5391 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5392 return -1;
5393 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5394 return -1;
5395
5396 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5397 wpa_s->current_ssid == NULL ||
5398 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5399 return -1;
5400
5401 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5402 wpa_s->own_addr, wpa_s->assoc_freq,
5403 duration1, interval1, duration2, interval2);
5404}
5405
5406
5407int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5408 unsigned int interval)
5409{
5410 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5411 return -1;
5412
5413 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5414 return -1;
5415
5416 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5417}
5418
5419
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005420static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5421{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005422 if (wpa_s->current_ssid == NULL) {
5423 /*
5424 * current_ssid can be cleared when P2P client interface gets
5425 * disconnected, so assume this interface was used as P2P
5426 * client.
5427 */
5428 return 1;
5429 }
5430 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005431 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5432}
5433
5434
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5436{
5437 struct wpa_supplicant *wpa_s = eloop_ctx;
5438
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005439 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5441 "disabled");
5442 return;
5443 }
5444
Dmitry Shmidt04949592012-07-19 12:16:46 -07005445 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5446 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005447 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005448}
5449
5450
5451static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5452{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005453 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005454
Dmitry Shmidt04949592012-07-19 12:16:46 -07005455 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5456 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5459 return;
5460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005461 timeout = wpa_s->conf->p2p_group_idle;
5462 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5463 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5464 timeout = P2P_MAX_CLIENT_IDLE;
5465
5466 if (timeout == 0)
5467 return;
5468
Dmitry Shmidt04949592012-07-19 12:16:46 -07005469 if (timeout < 0) {
5470 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5471 timeout = 0; /* special client mode no-timeout */
5472 else
5473 return;
5474 }
5475
5476 if (wpa_s->p2p_in_provisioning) {
5477 /*
5478 * Use the normal group formation timeout during the
5479 * provisioning phase to avoid terminating this process too
5480 * early due to group idle timeout.
5481 */
5482 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5483 "during provisioning");
5484 return;
5485 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305486
Dmitry Shmidt04949592012-07-19 12:16:46 -07005487 if (wpa_s->show_group_started) {
5488 /*
5489 * Use the normal group formation timeout between the end of
5490 * the provisioning phase and completion of 4-way handshake to
5491 * avoid terminating this process too early due to group idle
5492 * timeout.
5493 */
5494 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5495 "while waiting for initial 4-way handshake to "
5496 "complete");
5497 return;
5498 }
5499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005500 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005501 timeout);
5502 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5503 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005504}
5505
5506
Jouni Malinen2b89da82012-08-31 22:04:41 +03005507/* Returns 1 if the interface was removed */
5508int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5509 u16 reason_code, const u8 *ie, size_t ie_len,
5510 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005511{
5512 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005513 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005514 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005515 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005516
Dmitry Shmidt04949592012-07-19 12:16:46 -07005517 if (!locally_generated)
5518 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5519 ie_len);
5520
5521 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5522 wpa_s->current_ssid &&
5523 wpa_s->current_ssid->p2p_group &&
5524 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5525 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5526 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005527 if (wpas_p2p_group_delete(wpa_s,
5528 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5529 > 0)
5530 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005531 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005532
5533 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005534}
5535
5536
5537void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005538 u16 reason_code, const u8 *ie, size_t ie_len,
5539 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005540{
5541 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5542 return;
5543 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5544 return;
5545
Dmitry Shmidt04949592012-07-19 12:16:46 -07005546 if (!locally_generated)
5547 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5548 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005549}
5550
5551
5552void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5553{
5554 struct p2p_data *p2p = wpa_s->global->p2p;
5555
5556 if (p2p == NULL)
5557 return;
5558
5559 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5560 return;
5561
5562 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5563 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5564
5565 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5566 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5567
5568 if (wpa_s->wps &&
5569 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5570 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5571
5572 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5573 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5574
5575 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5576 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5577 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5578 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5579 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5580 }
5581
5582 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5583 p2p_set_sec_dev_types(p2p,
5584 (void *) wpa_s->conf->sec_device_type,
5585 wpa_s->conf->num_sec_device_types);
5586
5587 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5588 int i;
5589 p2p_remove_wps_vendor_extensions(p2p);
5590 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5591 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5592 continue;
5593 p2p_add_wps_vendor_extension(
5594 p2p, wpa_s->conf->wps_vendor_ext[i]);
5595 }
5596 }
5597
5598 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5599 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5600 char country[3];
5601 country[0] = wpa_s->conf->country[0];
5602 country[1] = wpa_s->conf->country[1];
5603 country[2] = 0x04;
5604 p2p_set_country(p2p, country);
5605 }
5606
5607 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5608 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5609 wpa_s->conf->p2p_ssid_postfix ?
5610 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5611 0);
5612 }
5613
5614 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5615 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005616
5617 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5618 u8 reg_class, channel;
5619 int ret;
5620 unsigned int r;
5621 if (wpa_s->conf->p2p_listen_reg_class &&
5622 wpa_s->conf->p2p_listen_channel) {
5623 reg_class = wpa_s->conf->p2p_listen_reg_class;
5624 channel = wpa_s->conf->p2p_listen_channel;
5625 } else {
5626 reg_class = 81;
5627 /*
5628 * Pick one of the social channels randomly as the
5629 * listen channel.
5630 */
5631 os_get_random((u8 *) &r, sizeof(r));
5632 channel = 1 + (r % 3) * 5;
5633 }
5634 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5635 if (ret)
5636 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5637 "failed: %d", ret);
5638 }
5639 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5640 u8 op_reg_class, op_channel, cfg_op_channel;
5641 int ret = 0;
5642 unsigned int r;
5643 if (wpa_s->conf->p2p_oper_reg_class &&
5644 wpa_s->conf->p2p_oper_channel) {
5645 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5646 op_channel = wpa_s->conf->p2p_oper_channel;
5647 cfg_op_channel = 1;
5648 } else {
5649 op_reg_class = 81;
5650 /*
5651 * Use random operation channel from (1, 6, 11)
5652 *if no other preference is indicated.
5653 */
5654 os_get_random((u8 *) &r, sizeof(r));
5655 op_channel = 1 + (r % 3) * 5;
5656 cfg_op_channel = 0;
5657 }
5658 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5659 cfg_op_channel);
5660 if (ret)
5661 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5662 "failed: %d", ret);
5663 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005664
5665 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5666 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5667 wpa_s->conf->p2p_pref_chan) < 0) {
5668 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5669 "update failed");
5670 }
5671 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672}
5673
5674
5675int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5676 int duration)
5677{
5678 if (!wpa_s->ap_iface)
5679 return -1;
5680 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5681 duration);
5682}
5683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005684
5685int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5686{
5687 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5688 return -1;
5689 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5690 return -1;
5691
5692 wpa_s->global->cross_connection = enabled;
5693 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5694
5695 if (!enabled) {
5696 struct wpa_supplicant *iface;
5697
5698 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5699 {
5700 if (iface->cross_connect_enabled == 0)
5701 continue;
5702
5703 iface->cross_connect_enabled = 0;
5704 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005705 wpa_msg_global(iface->parent, MSG_INFO,
5706 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5707 iface->ifname,
5708 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005709 }
5710 }
5711
5712 return 0;
5713}
5714
5715
5716static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5717{
5718 struct wpa_supplicant *iface;
5719
5720 if (!uplink->global->cross_connection)
5721 return;
5722
5723 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5724 if (!iface->cross_connect_enabled)
5725 continue;
5726 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5727 0)
5728 continue;
5729 if (iface->ap_iface == NULL)
5730 continue;
5731 if (iface->cross_connect_in_use)
5732 continue;
5733
5734 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005735 wpa_msg_global(iface->parent, MSG_INFO,
5736 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5737 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005738 }
5739}
5740
5741
5742static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5743{
5744 struct wpa_supplicant *iface;
5745
5746 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5747 if (!iface->cross_connect_enabled)
5748 continue;
5749 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5750 0)
5751 continue;
5752 if (!iface->cross_connect_in_use)
5753 continue;
5754
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005755 wpa_msg_global(iface->parent, MSG_INFO,
5756 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5757 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005758 iface->cross_connect_in_use = 0;
5759 }
5760}
5761
5762
5763void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5764{
5765 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5766 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5767 wpa_s->cross_connect_disallowed)
5768 wpas_p2p_disable_cross_connect(wpa_s);
5769 else
5770 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005771 if (!wpa_s->ap_iface &&
5772 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5773 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005774}
5775
5776
5777void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5778{
5779 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005780 if (!wpa_s->ap_iface &&
5781 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5782 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005783 wpas_p2p_set_group_idle_timeout(wpa_s);
5784}
5785
5786
5787static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5788{
5789 struct wpa_supplicant *iface;
5790
5791 if (!wpa_s->global->cross_connection)
5792 return;
5793
5794 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5795 if (iface == wpa_s)
5796 continue;
5797 if (iface->drv_flags &
5798 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5799 continue;
5800 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5801 continue;
5802
5803 wpa_s->cross_connect_enabled = 1;
5804 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5805 sizeof(wpa_s->cross_connect_uplink));
5806 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5807 "%s to %s whenever uplink is available",
5808 wpa_s->ifname, wpa_s->cross_connect_uplink);
5809
5810 if (iface->ap_iface || iface->current_ssid == NULL ||
5811 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5812 iface->cross_connect_disallowed ||
5813 iface->wpa_state != WPA_COMPLETED)
5814 break;
5815
5816 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005817 wpa_msg_global(wpa_s->parent, MSG_INFO,
5818 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5819 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005820 break;
5821 }
5822}
5823
5824
5825int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5826{
5827 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5828 !wpa_s->p2p_in_provisioning)
5829 return 0; /* not P2P client operation */
5830
5831 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5832 "session overlap");
5833 if (wpa_s != wpa_s->parent)
5834 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005835 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836 return 1;
5837}
5838
5839
5840void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5841{
5842 struct p2p_channels chan;
5843
5844 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5845 return;
5846
5847 os_memset(&chan, 0, sizeof(chan));
5848 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5849 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5850 "channel list");
5851 return;
5852 }
5853
5854 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5855}
5856
5857
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005858static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5859 struct wpa_scan_results *scan_res)
5860{
5861 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5862}
5863
5864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005865int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5866{
5867 struct wpa_global *global = wpa_s->global;
5868 int found = 0;
5869 const u8 *peer;
5870
5871 if (global->p2p == NULL)
5872 return -1;
5873
5874 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5875
5876 if (wpa_s->pending_interface_name[0] &&
5877 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5878 found = 1;
5879
5880 peer = p2p_get_go_neg_peer(global->p2p);
5881 if (peer) {
5882 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5883 MACSTR, MAC2STR(peer));
5884 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005885 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005886 }
5887
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005888 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5889 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5890 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5891 found = 1;
5892 }
5893
5894 if (wpa_s->pending_pd_before_join) {
5895 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5896 wpa_s->pending_pd_before_join = 0;
5897 found = 1;
5898 }
5899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005900 wpas_p2p_stop_find(wpa_s);
5901
5902 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5903 if (wpa_s == global->p2p_group_formation &&
5904 (wpa_s->p2p_in_provisioning ||
5905 wpa_s->parent->pending_interface_type ==
5906 WPA_IF_P2P_CLIENT)) {
5907 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5908 "formation found - cancelling",
5909 wpa_s->ifname);
5910 found = 1;
5911 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5912 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005913 if (wpa_s->p2p_in_provisioning) {
5914 wpas_group_formation_completed(wpa_s, 0);
5915 break;
5916 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005917 wpas_p2p_group_delete(wpa_s,
5918 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005919 break;
5920 }
5921 }
5922
5923 if (!found) {
5924 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5925 return -1;
5926 }
5927
5928 return 0;
5929}
5930
5931
5932void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5933{
5934 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5935 return;
5936
5937 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5938 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005939 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005940}
5941
5942
5943void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5944 int freq_24, int freq_5, int freq_overall)
5945{
5946 struct p2p_data *p2p = wpa_s->global->p2p;
5947 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5948 return;
5949 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5950}
5951
5952
5953int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5954{
5955 u8 peer[ETH_ALEN];
5956 struct p2p_data *p2p = wpa_s->global->p2p;
5957
5958 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5959 return -1;
5960
5961 if (hwaddr_aton(addr, peer))
5962 return -1;
5963
5964 return p2p_unauthorize(p2p, peer);
5965}
5966
5967
5968/**
5969 * wpas_p2p_disconnect - Disconnect from a P2P Group
5970 * @wpa_s: Pointer to wpa_supplicant data
5971 * Returns: 0 on success, -1 on failure
5972 *
5973 * This can be used to disconnect from a group in which the local end is a P2P
5974 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5975 * virtual network interface was created for this group, that interface will be
5976 * removed. Otherwise, only the configured P2P group network will be removed
5977 * from the interface.
5978 */
5979int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5980{
5981
5982 if (wpa_s == NULL)
5983 return -1;
5984
Jouni Malinen2b89da82012-08-31 22:04:41 +03005985 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5986 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005987}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005988
5989
5990int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5991{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005992 int ret;
5993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005994 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5995 return 0;
5996
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005997 ret = p2p_in_progress(wpa_s->global->p2p);
5998 if (ret == 0) {
5999 /*
6000 * Check whether there is an ongoing WPS provisioning step (or
6001 * other parts of group formation) on another interface since
6002 * p2p_in_progress() does not report this to avoid issues for
6003 * scans during such provisioning step.
6004 */
6005 if (wpa_s->global->p2p_group_formation &&
6006 wpa_s->global->p2p_group_formation != wpa_s) {
6007 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6008 "in group formation",
6009 wpa_s->global->p2p_group_formation->ifname);
6010 ret = 1;
6011 }
6012 }
6013
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006014 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6015 struct os_time now;
6016 os_get_time(&now);
6017 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
6018 P2P_MAX_INITIAL_CONN_WAIT_GO) {
6019 /* Wait for the first client has expired */
6020 wpa_s->global->p2p_go_wait_client.sec = 0;
6021 } else {
6022 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6023 ret = 1;
6024 }
6025 }
6026
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006027 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006028}
6029
6030
6031void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6032 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006033{
6034 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6035 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6036 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006037 /**
6038 * Remove the network by scheduling the group formation
6039 * timeout to happen immediately. The teardown code
6040 * needs to be scheduled to run asynch later so that we
6041 * don't delete data from under ourselves unexpectedly.
6042 * Calling wpas_p2p_group_formation_timeout directly
6043 * causes a series of crashes in WPS failure scenarios.
6044 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006045 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6046 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006047 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6048 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006049 }
6050}
6051
6052
6053struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006054 const u8 *addr, const u8 *ssid,
6055 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006056{
6057 struct wpa_ssid *s;
6058 size_t i;
6059
6060 for (s = wpa_s->conf->ssid; s; s = s->next) {
6061 if (s->disabled != 2)
6062 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006063 if (ssid &&
6064 (ssid_len != s->ssid_len ||
6065 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6066 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006067 if (addr == NULL) {
6068 if (s->mode == WPAS_MODE_P2P_GO)
6069 return s;
6070 continue;
6071 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006072 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6073 return s; /* peer is GO in the persistent group */
6074 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6075 continue;
6076 for (i = 0; i < s->num_p2p_clients; i++) {
6077 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6078 addr, ETH_ALEN) == 0)
6079 return s; /* peer is P2P client in persistent
6080 * group */
6081 }
6082 }
6083
6084 return NULL;
6085}
6086
6087
6088void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6089 const u8 *addr)
6090{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006091 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6092 wpa_s->parent, NULL) > 0) {
6093 /*
6094 * This can happen if WPS provisioning step is not terminated
6095 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6096 * peer was able to connect, there is no need to time out group
6097 * formation after this, though. In addition, this is used with
6098 * the initial connection wait on the GO as a separate formation
6099 * timeout and as such, expected to be hit after the initial WPS
6100 * provisioning step.
6101 */
6102 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6103 }
6104 if (!wpa_s->p2p_go_group_formation_completed) {
6105 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6106 wpa_s->p2p_go_group_formation_completed = 1;
6107 wpa_s->global->p2p_group_formation = NULL;
6108 wpa_s->p2p_in_provisioning = 0;
6109 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006110 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006111 if (addr == NULL)
6112 return;
6113 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6114}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006115
Dmitry Shmidt04949592012-07-19 12:16:46 -07006116
6117static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6118 int group_added)
6119{
6120 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006121 if (wpa_s->global->p2p_group_formation)
6122 group = wpa_s->global->p2p_group_formation;
6123 wpa_s = wpa_s->parent;
6124 offchannel_send_action_done(wpa_s);
6125 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006126 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006127 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6128 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6129 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6130 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6131 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006132 wpa_s->p2p_pd_before_go_neg,
6133 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006134}
6135
6136
6137int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6138{
6139 if (!wpa_s->p2p_fallback_to_go_neg ||
6140 wpa_s->p2p_in_provisioning <= 5)
6141 return 0;
6142
6143 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6144 return 0; /* peer operating as a GO */
6145
6146 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6147 "fallback to GO Negotiation");
6148 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6149
6150 return 1;
6151}
6152
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006153
6154unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6155{
6156 const char *rn, *rn2;
6157 struct wpa_supplicant *ifs;
6158
6159 if (wpa_s->wpa_state > WPA_SCANNING) {
6160 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6161 "concurrent operation",
6162 P2P_CONCURRENT_SEARCH_DELAY);
6163 return P2P_CONCURRENT_SEARCH_DELAY;
6164 }
6165
6166 if (!wpa_s->driver->get_radio_name)
6167 return 0;
6168 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
6169 if (rn == NULL || rn[0] == '\0')
6170 return 0;
6171
6172 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6173 if (ifs == wpa_s || !ifs->driver->get_radio_name)
6174 continue;
6175
6176 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
6177 if (!rn2 || os_strcmp(rn, rn2) != 0)
6178 continue;
6179 if (ifs->wpa_state > WPA_SCANNING) {
6180 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6181 "delay due to concurrent operation on "
6182 "interface %s",
6183 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6184 return P2P_CONCURRENT_SEARCH_DELAY;
6185 }
6186 }
6187
6188 return 0;
6189}
6190
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006191
6192void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6193{
6194 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6195 "pending anymore (sta_scan_pending=%d "
6196 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6197 wpa_s->global->p2p_cb_on_scan_complete);
6198 wpa_s->sta_scan_pending = 0;
6199
6200 if (!wpa_s->global->p2p_cb_on_scan_complete)
6201 return;
6202 wpa_s->global->p2p_cb_on_scan_complete = 0;
6203
6204 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6205 return;
6206
6207 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6208 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6209 "continued after successful connection");
6210 p2p_increase_search_delay(wpa_s->global->p2p,
6211 wpas_p2p_search_delay(wpa_s));
6212 }
6213}
6214
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006215
6216static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6217 struct wpa_ssid *s, const u8 *addr,
6218 int iface_addr)
6219{
6220 struct psk_list_entry *psk, *tmp;
6221 int changed = 0;
6222
6223 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6224 list) {
6225 if ((iface_addr && !psk->p2p &&
6226 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6227 (!iface_addr && psk->p2p &&
6228 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6229 wpa_dbg(wpa_s, MSG_DEBUG,
6230 "P2P: Remove persistent group PSK list entry for "
6231 MACSTR " p2p=%u",
6232 MAC2STR(psk->addr), psk->p2p);
6233 dl_list_del(&psk->list);
6234 os_free(psk);
6235 changed++;
6236 }
6237 }
6238
6239 return changed;
6240}
6241
6242
6243void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6244 const u8 *p2p_dev_addr,
6245 const u8 *psk, size_t psk_len)
6246{
6247 struct wpa_ssid *ssid = wpa_s->current_ssid;
6248 struct wpa_ssid *persistent;
6249 struct psk_list_entry *p;
6250
6251 if (psk_len != sizeof(p->psk))
6252 return;
6253
6254 if (p2p_dev_addr) {
6255 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6256 " p2p_dev_addr=" MACSTR,
6257 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6258 if (is_zero_ether_addr(p2p_dev_addr))
6259 p2p_dev_addr = NULL;
6260 } else {
6261 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6262 MAC2STR(mac_addr));
6263 }
6264
6265 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6266 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6267 /* To be added to persistent group once created */
6268 if (wpa_s->global->add_psk == NULL) {
6269 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6270 if (wpa_s->global->add_psk == NULL)
6271 return;
6272 }
6273 p = wpa_s->global->add_psk;
6274 if (p2p_dev_addr) {
6275 p->p2p = 1;
6276 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6277 } else {
6278 p->p2p = 0;
6279 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6280 }
6281 os_memcpy(p->psk, psk, psk_len);
6282 return;
6283 }
6284
6285 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6286 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6287 return;
6288 }
6289
6290 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6291 ssid->ssid_len);
6292 if (!persistent) {
6293 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6294 return;
6295 }
6296
6297 p = os_zalloc(sizeof(*p));
6298 if (p == NULL)
6299 return;
6300 if (p2p_dev_addr) {
6301 p->p2p = 1;
6302 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6303 } else {
6304 p->p2p = 0;
6305 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6306 }
6307 os_memcpy(p->psk, psk, psk_len);
6308
6309 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6310 struct psk_list_entry *last;
6311 last = dl_list_last(&persistent->psk_list,
6312 struct psk_list_entry, list);
6313 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6314 MACSTR " (p2p=%u) to make room for a new one",
6315 MAC2STR(last->addr), last->p2p);
6316 dl_list_del(&last->list);
6317 os_free(last);
6318 }
6319
6320 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6321 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6322 p2p_dev_addr == NULL);
6323 if (p2p_dev_addr) {
6324 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6325 MACSTR, MAC2STR(p2p_dev_addr));
6326 } else {
6327 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6328 MAC2STR(mac_addr));
6329 }
6330 dl_list_add(&persistent->psk_list, &p->list);
6331
6332#ifndef CONFIG_NO_CONFIG_WRITE
6333 if (wpa_s->parent->conf->update_config &&
6334 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6335 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6336#endif /* CONFIG_NO_CONFIG_WRITE */
6337}
6338
6339
6340static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6341 struct wpa_ssid *s, const u8 *addr,
6342 int iface_addr)
6343{
6344 int res;
6345
6346 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6347 if (res > 0) {
6348#ifndef CONFIG_NO_CONFIG_WRITE
6349 if (wpa_s->conf->update_config &&
6350 wpa_config_write(wpa_s->confname, wpa_s->conf))
6351 wpa_dbg(wpa_s, MSG_DEBUG,
6352 "P2P: Failed to update configuration");
6353#endif /* CONFIG_NO_CONFIG_WRITE */
6354 }
6355}
6356
6357
6358static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6359 const u8 *peer, int iface_addr)
6360{
6361 struct hostapd_data *hapd;
6362 struct hostapd_wpa_psk *psk, *prev, *rem;
6363 struct sta_info *sta;
6364
6365 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6366 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6367 return;
6368
6369 /* Remove per-station PSK entry */
6370 hapd = wpa_s->ap_iface->bss[0];
6371 prev = NULL;
6372 psk = hapd->conf->ssid.wpa_psk;
6373 while (psk) {
6374 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6375 (!iface_addr &&
6376 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6377 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6378 MACSTR " iface_addr=%d",
6379 MAC2STR(peer), iface_addr);
6380 if (prev)
6381 prev->next = psk->next;
6382 else
6383 hapd->conf->ssid.wpa_psk = psk->next;
6384 rem = psk;
6385 psk = psk->next;
6386 os_free(rem);
6387 } else {
6388 prev = psk;
6389 psk = psk->next;
6390 }
6391 }
6392
6393 /* Disconnect from group */
6394 if (iface_addr)
6395 sta = ap_get_sta(hapd, peer);
6396 else
6397 sta = ap_get_sta_p2p(hapd, peer);
6398 if (sta) {
6399 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6400 " (iface_addr=%d) from group",
6401 MAC2STR(peer), iface_addr);
6402 hostapd_drv_sta_deauth(hapd, sta->addr,
6403 WLAN_REASON_DEAUTH_LEAVING);
6404 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6405 }
6406}
6407
6408
6409void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6410 int iface_addr)
6411{
6412 struct wpa_ssid *s;
6413 struct wpa_supplicant *w;
6414
6415 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6416
6417 /* Remove from any persistent group */
6418 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6419 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6420 continue;
6421 if (!iface_addr)
6422 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6423 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6424 }
6425
6426 /* Remove from any operating group */
6427 for (w = wpa_s->global->ifaces; w; w = w->next)
6428 wpas_p2p_remove_client_go(w, peer, iface_addr);
6429}
6430
6431
6432static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6433{
6434 struct wpa_supplicant *wpa_s = eloop_ctx;
6435 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6436}
6437
6438
6439int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6440{
6441 struct wpa_ssid *ssid = wpa_s->current_ssid;
6442
6443 if (ssid == NULL || !ssid->p2p_group)
6444 return 0;
6445
6446 if (wpa_s->p2p_last_4way_hs_fail &&
6447 wpa_s->p2p_last_4way_hs_fail == ssid) {
6448 u8 go_dev_addr[ETH_ALEN];
6449 struct wpa_ssid *persistent;
6450
6451 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6452 ssid->ssid,
6453 ssid->ssid_len) <= 0) {
6454 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6455 goto disconnect;
6456 }
6457
6458 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6459 MACSTR, MAC2STR(go_dev_addr));
6460 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6461 ssid->ssid,
6462 ssid->ssid_len);
6463 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6464 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6465 goto disconnect;
6466 }
6467 wpa_msg_global(wpa_s->parent, MSG_INFO,
6468 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6469 persistent->id);
6470 disconnect:
6471 wpa_s->p2p_last_4way_hs_fail = NULL;
6472 /*
6473 * Remove the group from a timeout to avoid issues with caller
6474 * continuing to use the interface if this is on a P2P group
6475 * interface.
6476 */
6477 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6478 wpa_s, NULL);
6479 return 1;
6480 }
6481
6482 wpa_s->p2p_last_4way_hs_fail = ssid;
6483 return 0;
6484}
6485
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006486#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006487static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6488{
6489 struct wpa_supplicant *wpa_s = eloop_ctx;
6490
6491 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6492 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6493}
6494
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006495int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6496 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006497{
6498 struct wpa_supplicant *iface = NULL;
6499 struct p2p_data *p2p = wpa_s->global->p2p;
6500
6501 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006502 if ((iface->current_ssid) &&
6503 (iface->current_ssid->frequency != freq) &&
6504 ((iface->p2p_group_interface) ||
6505 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006506
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006507 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
6508 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
6509 /* Try to see whether we can move the GO. If it
6510 * is not possible, remove the GO interface
6511 */
6512 if (wpa_drv_switch_channel(iface, freq) == 0) {
6513 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
6514 iface->current_ssid->frequency = freq;
6515 continue;
6516 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006517 }
6518
6519 /* If GO cannot be moved or if the conflicting interface is a
6520 * P2P Client, remove the interface depending up on the connection
6521 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006522 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07006523 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006524 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006525 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006526 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006527 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
6528 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006529 /* If connection in progress is p2p connection, do not proceed for the connection */
6530 if (wpa_s == iface)
6531 return -1;
6532 else
6533 /* If connection in progress is STA connection, proceed for the connection */
6534 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006535 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006536 /* P2p connection has priority, disable the STA network*/
6537 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
6538 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6539 " id=%d", ssid->id);
6540 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6541 if (wpa_s == iface) {
6542 /* p2p connection is in progress, continue connecting...*/
6543 return 0;
6544 }
6545 else {
6546 /* STA connection is in progress, do not allow to continue */
6547 return -1;
6548 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006549 }
6550 }
6551 }
6552 return 0;
6553}
6554#endif