blob: 86bcf87017d53252b984b3f4d986bb68c7d447fc [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{
2952 unsigned int i;
2953
2954 if (global->p2p_disallow_freq == NULL)
2955 return 0;
2956
2957 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2958 if (freq >= global->p2p_disallow_freq[i].min &&
2959 freq <= global->p2p_disallow_freq[i].max)
2960 return 1;
2961 }
2962
2963 return 0;
2964}
2965
2966
2967static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2968{
2969 reg->channel[reg->channels] = chan;
2970 reg->channels++;
2971}
2972
2973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2975 struct p2p_channels *chan)
2976{
2977 int i, cla = 0;
2978
2979 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2980 "band");
2981
2982 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2983 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002984 chan->reg_class[cla].channels = 0;
2985 for (i = 0; i < 11; i++) {
2986 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2987 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2988 }
2989 if (chan->reg_class[cla].channels)
2990 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991
2992 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2993 "band");
2994
2995 /* Operating class 115 - 5 GHz, channels 36-48 */
2996 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002997 chan->reg_class[cla].channels = 0;
2998 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2999 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3000 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3001 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3002 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3003 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3004 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3005 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3006 if (chan->reg_class[cla].channels)
3007 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003008
3009 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3010 "band");
3011
3012 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3013 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003014 chan->reg_class[cla].channels = 0;
3015 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3016 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3017 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3018 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3019 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3020 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3021 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3022 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3023 if (chan->reg_class[cla].channels)
3024 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003025
3026 chan->reg_classes = cla;
3027 return 0;
3028}
3029
3030
3031static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3032 u16 num_modes,
3033 enum hostapd_hw_mode mode)
3034{
3035 u16 i;
3036
3037 for (i = 0; i < num_modes; i++) {
3038 if (modes[i].mode == mode)
3039 return &modes[i];
3040 }
3041
3042 return NULL;
3043}
3044
3045
Dmitry Shmidt04949592012-07-19 12:16:46 -07003046static int has_channel(struct wpa_global *global,
3047 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048{
3049 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003050 unsigned int freq;
3051
3052 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3053 chan * 5;
3054 if (wpas_p2p_disallowed_freq(global, freq))
3055 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056
3057 for (i = 0; i < mode->num_channels; i++) {
3058 if (mode->channels[i].chan == chan) {
3059 if (flags)
3060 *flags = mode->channels[i].flag;
3061 return !(mode->channels[i].flag &
3062 (HOSTAPD_CHAN_DISABLED |
3063 HOSTAPD_CHAN_PASSIVE_SCAN |
3064 HOSTAPD_CHAN_NO_IBSS |
3065 HOSTAPD_CHAN_RADAR));
3066 }
3067 }
3068
3069 return 0;
3070}
3071
3072
3073struct p2p_oper_class_map {
3074 enum hostapd_hw_mode mode;
3075 u8 op_class;
3076 u8 min_chan;
3077 u8 max_chan;
3078 u8 inc;
3079 enum { BW20, BW40PLUS, BW40MINUS } bw;
3080};
3081
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003082static struct p2p_oper_class_map op_class[] = {
3083 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003084#if 0 /* Do not enable HT40 on 2 GHz for now */
3085 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3086 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3087#endif
3088 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3089 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3090 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3091 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3092 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3093 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3094 { -1, 0, 0, 0, 0, BW20 }
3095};
3096
3097
3098static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3099 struct hostapd_hw_modes *mode,
3100 u8 channel, u8 bw)
3101{
3102 int flag;
3103
3104 if (!has_channel(wpa_s->global, mode, channel, &flag))
3105 return -1;
3106 if (bw == BW40MINUS &&
3107 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
3108 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
3109 return 0;
3110 if (bw == BW40PLUS &&
3111 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
3112 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
3113 return 0;
3114 return 1;
3115}
3116
3117
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3119 struct p2p_channels *chan)
3120{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003121 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003123
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003124 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003125 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3126 "of all supported channels; assume dualband "
3127 "support");
3128 return wpas_p2p_default_channels(wpa_s, chan);
3129 }
3130
3131 cla = 0;
3132
3133 for (op = 0; op_class[op].op_class; op++) {
3134 struct p2p_oper_class_map *o = &op_class[op];
3135 u8 ch;
3136 struct p2p_reg_class *reg = NULL;
3137
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003138 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003139 if (mode == NULL)
3140 continue;
3141 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003142 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 continue;
3144 if (reg == NULL) {
3145 wpa_printf(MSG_DEBUG, "P2P: Add operating "
3146 "class %u", o->op_class);
3147 reg = &chan->reg_class[cla];
3148 cla++;
3149 reg->reg_class = o->op_class;
3150 }
3151 reg->channel[reg->channels] = ch;
3152 reg->channels++;
3153 }
3154 if (reg) {
3155 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3156 reg->channel, reg->channels);
3157 }
3158 }
3159
3160 chan->reg_classes = cla;
3161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003162 return 0;
3163}
3164
3165
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003166int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3167 struct hostapd_hw_modes *mode, u8 channel)
3168{
3169 int op, ret;
3170
3171 for (op = 0; op_class[op].op_class; op++) {
3172 struct p2p_oper_class_map *o = &op_class[op];
3173 u8 ch;
3174
3175 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3176 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3177 o->bw == BW20 || ch != channel)
3178 continue;
3179 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3180 if (ret < 0)
3181 continue;
3182 else if (ret > 0)
3183 return (o->bw == BW40MINUS) ? -1 : 1;
3184 else
3185 return 0;
3186 }
3187 }
3188 return 0;
3189}
3190
3191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3193 size_t buf_len)
3194{
3195 struct wpa_supplicant *wpa_s = ctx;
3196
3197 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3198 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3199 break;
3200 }
3201 if (wpa_s == NULL)
3202 return -1;
3203
3204 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3205}
3206
3207
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003208static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3209{
3210 struct wpa_supplicant *wpa_s = ctx;
3211
3212 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3213 struct wpa_ssid *ssid = wpa_s->current_ssid;
3214 if (ssid == NULL)
3215 continue;
3216 if (ssid->mode != WPAS_MODE_INFRA)
3217 continue;
3218 if (wpa_s->wpa_state != WPA_COMPLETED &&
3219 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3220 continue;
3221 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3222 return 1;
3223 }
3224
3225 return 0;
3226}
3227
3228
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003229static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3230{
3231 struct wpa_supplicant *wpa_s = ctx;
3232 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3233}
3234
3235
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003236int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3237{
3238 struct wpa_interface iface;
3239 struct wpa_supplicant *p2pdev_wpa_s;
3240 char ifname[100];
3241 char force_name[100];
3242 int ret;
3243
3244 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3245 wpa_s->ifname);
3246 force_name[0] = '\0';
3247 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3248 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3249 force_name, wpa_s->pending_interface_addr, NULL);
3250 if (ret < 0) {
3251 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3252 return ret;
3253 }
3254 os_strlcpy(wpa_s->pending_interface_name, ifname,
3255 sizeof(wpa_s->pending_interface_name));
3256
3257 os_memset(&iface, 0, sizeof(iface));
3258 iface.p2p_mgmt = 1;
3259 iface.ifname = wpa_s->pending_interface_name;
3260 iface.driver = wpa_s->driver->name;
3261 iface.driver_param = wpa_s->conf->driver_param;
3262 iface.confname = wpa_s->confname;
3263 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3264 if (!p2pdev_wpa_s) {
3265 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3266 return -1;
3267 }
3268 p2pdev_wpa_s->parent = wpa_s;
3269
3270 wpa_s->pending_interface_name[0] = '\0';
3271 return 0;
3272}
3273
3274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275/**
3276 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3277 * @global: Pointer to global data from wpa_supplicant_init()
3278 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3279 * Returns: 0 on success, -1 on failure
3280 */
3281int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3282{
3283 struct p2p_config p2p;
3284 unsigned int r;
3285 int i;
3286
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003287 if (wpa_s->conf->p2p_disabled)
3288 return 0;
3289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3291 return 0;
3292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 if (global->p2p)
3294 return 0;
3295
3296 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3297 struct p2p_params params;
3298
3299 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3300 os_memset(&params, 0, sizeof(params));
3301 params.dev_name = wpa_s->conf->device_name;
3302 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3303 WPS_DEV_TYPE_LEN);
3304 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3305 os_memcpy(params.sec_dev_type,
3306 wpa_s->conf->sec_device_type,
3307 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3308
3309 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3310 return -1;
3311
3312 return 0;
3313 }
3314
3315 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003317 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003318 p2p.p2p_scan = wpas_p2p_scan;
3319 p2p.send_action = wpas_send_action;
3320 p2p.send_action_done = wpas_send_action_done;
3321 p2p.go_neg_completed = wpas_go_neg_completed;
3322 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3323 p2p.dev_found = wpas_dev_found;
3324 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003325 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326 p2p.start_listen = wpas_start_listen;
3327 p2p.stop_listen = wpas_stop_listen;
3328 p2p.send_probe_resp = wpas_send_probe_resp;
3329 p2p.sd_request = wpas_sd_request;
3330 p2p.sd_response = wpas_sd_response;
3331 p2p.prov_disc_req = wpas_prov_disc_req;
3332 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003333 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 p2p.invitation_process = wpas_invitation_process;
3335 p2p.invitation_received = wpas_invitation_received;
3336 p2p.invitation_result = wpas_invitation_result;
3337 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003338 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339
3340 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003341 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342 p2p.dev_name = wpa_s->conf->device_name;
3343 p2p.manufacturer = wpa_s->conf->manufacturer;
3344 p2p.model_name = wpa_s->conf->model_name;
3345 p2p.model_number = wpa_s->conf->model_number;
3346 p2p.serial_number = wpa_s->conf->serial_number;
3347 if (wpa_s->wps) {
3348 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3349 p2p.config_methods = wpa_s->wps->config_methods;
3350 }
3351
3352 if (wpa_s->conf->p2p_listen_reg_class &&
3353 wpa_s->conf->p2p_listen_channel) {
3354 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3355 p2p.channel = wpa_s->conf->p2p_listen_channel;
3356 } else {
3357 p2p.reg_class = 81;
3358 /*
3359 * Pick one of the social channels randomly as the listen
3360 * channel.
3361 */
3362 os_get_random((u8 *) &r, sizeof(r));
3363 p2p.channel = 1 + (r % 3) * 5;
3364 }
3365 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3366
3367 if (wpa_s->conf->p2p_oper_reg_class &&
3368 wpa_s->conf->p2p_oper_channel) {
3369 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3370 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3371 p2p.cfg_op_channel = 1;
3372 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3373 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3374
3375 } else {
3376 p2p.op_reg_class = 81;
3377 /*
3378 * Use random operation channel from (1, 6, 11) if no other
3379 * preference is indicated.
3380 */
3381 os_get_random((u8 *) &r, sizeof(r));
3382 p2p.op_channel = 1 + (r % 3) * 5;
3383 p2p.cfg_op_channel = 0;
3384 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3385 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3386 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003387
3388 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3389 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3390 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3391 }
3392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3394 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3395 p2p.country[2] = 0x04;
3396 } else
3397 os_memcpy(p2p.country, "XX\x04", 3);
3398
3399 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3400 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3401 "channel list");
3402 return -1;
3403 }
3404
3405 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3406 WPS_DEV_TYPE_LEN);
3407
3408 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3409 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3410 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3411
3412 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3413 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3414
3415 p2p.max_peers = 100;
3416
3417 if (wpa_s->conf->p2p_ssid_postfix) {
3418 p2p.ssid_postfix_len =
3419 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3420 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3421 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3422 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3423 p2p.ssid_postfix_len);
3424 }
3425
3426 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3427
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003428 p2p.max_listen = wpa_s->max_remain_on_chan;
3429
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003430 global->p2p = p2p_init(&p2p);
3431 if (global->p2p == NULL)
3432 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003433 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434
3435 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3436 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3437 continue;
3438 p2p_add_wps_vendor_extension(
3439 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3440 }
3441
3442 return 0;
3443}
3444
3445
3446/**
3447 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3448 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3449 *
3450 * This function deinitialize per-interface P2P data.
3451 */
3452void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3453{
3454 if (wpa_s->driver && wpa_s->drv_priv)
3455 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003456
3457 if (wpa_s->go_params) {
3458 /* Clear any stored provisioning info */
3459 p2p_clear_provisioning_info(
3460 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003461 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003462 }
3463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003464 os_free(wpa_s->go_params);
3465 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003466#ifdef ANDROID_P2P
3467 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3468#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003469 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3470 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3471 wpa_s->p2p_long_listen = 0;
3472 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3473 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3474 wpas_p2p_remove_pending_group_interface(wpa_s);
3475
3476 /* TODO: remove group interface from the driver if this wpa_s instance
3477 * is on top of a P2P group interface */
3478}
3479
3480
3481/**
3482 * wpas_p2p_deinit_global - Deinitialize global P2P module
3483 * @global: Pointer to global data from wpa_supplicant_init()
3484 *
3485 * This function deinitializes the global (per device) P2P module.
3486 */
3487void wpas_p2p_deinit_global(struct wpa_global *global)
3488{
3489 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003490
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003491 wpa_s = global->ifaces;
3492 if (wpa_s)
3493 wpas_p2p_service_flush(wpa_s);
3494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495 if (global->p2p == NULL)
3496 return;
3497
3498 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003499 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3500 wpa_s = wpa_s->next;
3501 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 tmp = global->ifaces;
3503 while (tmp &&
3504 (tmp == wpa_s ||
3505 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3506 tmp = tmp->next;
3507 }
3508 if (tmp == NULL)
3509 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003510 /* Disconnect from the P2P group and deinit the interface */
3511 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 }
3513
3514 /*
3515 * Deinit GO data on any possibly remaining interface (if main
3516 * interface is used as GO).
3517 */
3518 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3519 if (wpa_s->ap_iface)
3520 wpas_p2p_group_deinit(wpa_s);
3521 }
3522
3523 p2p_deinit(global->p2p);
3524 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003525 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526}
3527
3528
3529static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3530{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003531 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3532 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003533 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534 if (wpa_s->drv_flags &
3535 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3536 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3537 return 1; /* P2P group requires a new interface in every case
3538 */
3539 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3540 return 0; /* driver does not support concurrent operations */
3541 if (wpa_s->global->ifaces->next)
3542 return 1; /* more that one interface already in use */
3543 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3544 return 1; /* this interface is already in use */
3545 return 0;
3546}
3547
3548
3549static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3550 const u8 *peer_addr,
3551 enum p2p_wps_method wps_method,
3552 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003553 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003554 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003555{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003556 if (persistent_group && wpa_s->conf->persistent_reconnect)
3557 persistent_group = 2;
3558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003559 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3560 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3561 go_intent, own_interface_addr,
3562 force_freq, persistent_group);
3563 }
3564
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003565 /*
3566 * Increase GO config timeout if HT40 is used since it takes some time
3567 * to scan channels for coex purposes before the BSS can be started.
3568 */
3569 p2p_set_config_timeout(wpa_s->global->p2p,
3570 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003572 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3573 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003574 persistent_group, ssid ? ssid->ssid : NULL,
3575 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003576 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003577}
3578
3579
3580static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3581 const u8 *peer_addr,
3582 enum p2p_wps_method wps_method,
3583 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003584 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003585 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003587 if (persistent_group && wpa_s->conf->persistent_reconnect)
3588 persistent_group = 2;
3589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3591 return -1;
3592
3593 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3594 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003595 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003596 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003597}
3598
3599
3600static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3601{
3602 wpa_s->p2p_join_scan_count++;
3603 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3604 wpa_s->p2p_join_scan_count);
3605 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3606 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3607 " for join operationg - stop join attempt",
3608 MAC2STR(wpa_s->pending_join_iface_addr));
3609 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003610 if (wpa_s->p2p_auto_pd) {
3611 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003612 wpa_msg_global(wpa_s, MSG_INFO,
3613 P2P_EVENT_PROV_DISC_FAILURE
3614 " p2p_dev_addr=" MACSTR " status=N/A",
3615 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003616 return;
3617 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003618 wpa_msg_global(wpa_s->parent, MSG_INFO,
3619 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 }
3621}
3622
3623
Dmitry Shmidt04949592012-07-19 12:16:46 -07003624static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3625{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003626 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003627
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003628 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3629 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003630 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003631 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003632
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003633 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3634 if (!freqs)
3635 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003636
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003637 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3638 wpa_s->num_multichan_concurrent);
3639 if (num < 0) {
3640 res = 1;
3641 goto exit_free;
3642 }
3643
3644 for (i = 0; i < num; i++) {
3645 if (freqs[i] == freq) {
3646 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3647 freq);
3648 res = 0;
3649 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003650 }
3651 }
3652
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003653 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003654
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003655exit_free:
3656 os_free(freqs);
3657 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003658}
3659
3660
3661static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3662 const u8 *peer_dev_addr)
3663{
3664 struct wpa_bss *bss;
3665 int updated;
3666
3667 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3668 if (bss == NULL)
3669 return -1;
3670 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3671 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3672 "last scan");
3673 return 0;
3674 }
3675
3676 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3677 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3678 "%ld.%06ld (%supdated in last scan)",
3679 bss->last_update.sec, bss->last_update.usec,
3680 updated ? "": "not ");
3681
3682 return updated;
3683}
3684
3685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3687 struct wpa_scan_results *scan_res)
3688{
3689 struct wpa_bss *bss;
3690 int freq;
3691 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3694
3695 if (wpa_s->global->p2p_disabled)
3696 return;
3697
Dmitry Shmidt04949592012-07-19 12:16:46 -07003698 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3699 scan_res ? (int) scan_res->num : -1,
3700 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003701
3702 if (scan_res)
3703 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3704
Dmitry Shmidt04949592012-07-19 12:16:46 -07003705 if (wpa_s->p2p_auto_pd) {
3706 int join = wpas_p2p_peer_go(wpa_s,
3707 wpa_s->pending_join_dev_addr);
3708 if (join == 0 &&
3709 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3710 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003711 bss = wpa_bss_get_bssid_latest(
3712 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003713 if (bss) {
3714 freq = bss->freq;
3715 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3716 "the peer " MACSTR " at %d MHz",
3717 wpa_s->auto_pd_scan_retry,
3718 MAC2STR(wpa_s->
3719 pending_join_dev_addr),
3720 freq);
3721 wpas_p2p_join_scan_req(wpa_s, freq);
3722 return;
3723 }
3724 }
3725
3726 if (join < 0)
3727 join = 0;
3728
3729 wpa_s->p2p_auto_pd = 0;
3730 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3731 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3732 MAC2STR(wpa_s->pending_join_dev_addr), join);
3733 if (p2p_prov_disc_req(wpa_s->global->p2p,
3734 wpa_s->pending_join_dev_addr,
3735 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003736 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003737 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003738 wpa_msg_global(wpa_s, MSG_INFO,
3739 P2P_EVENT_PROV_DISC_FAILURE
3740 " p2p_dev_addr=" MACSTR " status=N/A",
3741 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003742 }
3743 return;
3744 }
3745
3746 if (wpa_s->p2p_auto_join) {
3747 int join = wpas_p2p_peer_go(wpa_s,
3748 wpa_s->pending_join_dev_addr);
3749 if (join < 0) {
3750 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3751 "running a GO -> use GO Negotiation");
3752 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3753 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3754 wpa_s->p2p_persistent_group, 0, 0, 0,
3755 wpa_s->p2p_go_intent,
3756 wpa_s->p2p_connect_freq,
3757 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003758 wpa_s->p2p_pd_before_go_neg,
3759 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003760 return;
3761 }
3762
3763 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3764 "try to join the group", join ? "" :
3765 " in older scan");
3766 if (!join)
3767 wpa_s->p2p_fallback_to_go_neg = 1;
3768 }
3769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003770 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3771 wpa_s->pending_join_iface_addr);
3772 if (freq < 0 &&
3773 p2p_get_interface_addr(wpa_s->global->p2p,
3774 wpa_s->pending_join_dev_addr,
3775 iface_addr) == 0 &&
3776 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3777 {
3778 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3779 "address for join from " MACSTR " to " MACSTR
3780 " based on newly discovered P2P peer entry",
3781 MAC2STR(wpa_s->pending_join_iface_addr),
3782 MAC2STR(iface_addr));
3783 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3784 ETH_ALEN);
3785
3786 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3787 wpa_s->pending_join_iface_addr);
3788 }
3789 if (freq >= 0) {
3790 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3791 "from P2P peer table: %d MHz", freq);
3792 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003793 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794 if (bss) {
3795 freq = bss->freq;
3796 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3797 "from BSS table: %d MHz", freq);
3798 }
3799 if (freq > 0) {
3800 u16 method;
3801
Dmitry Shmidt04949592012-07-19 12:16:46 -07003802 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003803 wpa_msg_global(wpa_s->parent, MSG_INFO,
3804 P2P_EVENT_GROUP_FORMATION_FAILURE
3805 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003806 return;
3807 }
3808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3810 "prior to joining an existing group (GO " MACSTR
3811 " freq=%u MHz)",
3812 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3813 wpa_s->pending_pd_before_join = 1;
3814
3815 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003816 case WPS_PIN_DISPLAY:
3817 method = WPS_CONFIG_KEYPAD;
3818 break;
3819 case WPS_PIN_KEYPAD:
3820 method = WPS_CONFIG_DISPLAY;
3821 break;
3822 case WPS_PBC:
3823 method = WPS_CONFIG_PUSHBUTTON;
3824 break;
3825 default:
3826 method = 0;
3827 break;
3828 }
3829
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003830 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3831 wpa_s->pending_join_dev_addr) ==
3832 method)) {
3833 /*
3834 * We have already performed provision discovery for
3835 * joining the group. Proceed directly to join
3836 * operation without duplicated provision discovery. */
3837 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3838 "with " MACSTR " already done - proceed to "
3839 "join",
3840 MAC2STR(wpa_s->pending_join_dev_addr));
3841 wpa_s->pending_pd_before_join = 0;
3842 goto start;
3843 }
3844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003846 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003847 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003848 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3849 "Discovery Request before joining an "
3850 "existing group");
3851 wpa_s->pending_pd_before_join = 0;
3852 goto start;
3853 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003854 return;
3855 }
3856
3857 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3858 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3859 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3860 wpas_p2p_check_join_scan_limit(wpa_s);
3861 return;
3862
3863start:
3864 /* Start join operation immediately */
3865 wpas_p2p_join_start(wpa_s);
3866}
3867
3868
Dmitry Shmidt04949592012-07-19 12:16:46 -07003869static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003870{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871 int ret;
3872 struct wpa_driver_scan_params params;
3873 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003874 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003875 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003876#ifdef ANDROID_P2P
3877 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003879 /* If freq is not provided, check the operating freq of the GO and do a
3880 * a directed scan to save time
3881 */
3882 if(!freq) {
3883 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3884 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3885 }
3886#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003887 os_memset(&params, 0, sizeof(params));
3888
3889 /* P2P Wildcard SSID */
3890 params.num_ssids = 1;
3891 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3892 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3893
3894 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003895 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3896 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3897 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898 if (wps_ie == NULL) {
3899 wpas_p2p_scan_res_join(wpa_s, NULL);
3900 return;
3901 }
3902
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003903 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3904 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 if (ies == NULL) {
3906 wpabuf_free(wps_ie);
3907 wpas_p2p_scan_res_join(wpa_s, NULL);
3908 return;
3909 }
3910 wpabuf_put_buf(ies, wps_ie);
3911 wpabuf_free(wps_ie);
3912
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003913 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003915 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003916 params.extra_ies = wpabuf_head(ies);
3917 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003918 if (freq > 0) {
3919 freqs[0] = freq;
3920 params.freqs = freqs;
3921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003922
3923 /*
3924 * Run a scan to update BSS table and start Provision Discovery once
3925 * the new scan results become available.
3926 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003927 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003928 if (!ret) {
3929 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003930 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003931 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932
3933 wpabuf_free(ies);
3934
3935 if (ret) {
3936 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3937 "try again later");
3938 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3939 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3940 wpas_p2p_check_join_scan_limit(wpa_s);
3941 }
3942}
3943
3944
Dmitry Shmidt04949592012-07-19 12:16:46 -07003945static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3946{
3947 struct wpa_supplicant *wpa_s = eloop_ctx;
3948 wpas_p2p_join_scan_req(wpa_s, 0);
3949}
3950
3951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003953 const u8 *dev_addr, enum p2p_wps_method wps_method,
3954 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003955{
3956 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003957 MACSTR " dev " MACSTR ")%s",
3958 MAC2STR(iface_addr), MAC2STR(dev_addr),
3959 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960
Dmitry Shmidt04949592012-07-19 12:16:46 -07003961 wpa_s->p2p_auto_pd = 0;
3962 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3964 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3965 wpa_s->pending_join_wps_method = wps_method;
3966
3967 /* Make sure we are not running find during connection establishment */
3968 wpas_p2p_stop_find(wpa_s);
3969
3970 wpa_s->p2p_join_scan_count = 0;
3971 wpas_p2p_join_scan(wpa_s, NULL);
3972 return 0;
3973}
3974
3975
3976static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3977{
3978 struct wpa_supplicant *group;
3979 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003980 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003981
3982 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3983 if (group == NULL)
3984 return -1;
3985 if (group != wpa_s) {
3986 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3987 sizeof(group->p2p_pin));
3988 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003989 } else {
3990 /*
3991 * Need to mark the current interface for p2p_group_formation
3992 * when a separate group interface is not used. This is needed
3993 * to allow p2p_cancel stop a pending p2p_connect-join.
3994 * wpas_p2p_init_group_interface() addresses this for the case
3995 * where a separate group interface is used.
3996 */
3997 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998 }
3999
4000 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004001 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004002
4003 os_memset(&res, 0, sizeof(res));
4004 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4005 ETH_ALEN);
4006 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004007 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004008 if (bss) {
4009 res.freq = bss->freq;
4010 res.ssid_len = bss->ssid_len;
4011 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4012 }
4013
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004014 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4015 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4016 "starting client");
4017 wpa_drv_cancel_remain_on_channel(wpa_s);
4018 wpa_s->off_channel_freq = 0;
4019 wpa_s->roc_waiting_drv_freq = 0;
4020 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021 wpas_start_wps_enrollee(group, &res);
4022
4023 /*
4024 * Allow a longer timeout for join-a-running-group than normal 15
4025 * second group formation timeout since the GO may not have authorized
4026 * our connection yet.
4027 */
4028 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4029 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4030 wpa_s, NULL);
4031
4032 return 0;
4033}
4034
4035
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004036static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004037 int *force_freq, int *pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004038{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004039 int *freqs, res;
4040 unsigned int freq_in_use = 0, num, i;
4041
4042 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4043 if (!freqs)
4044 return -1;
4045
4046 num = get_shared_radio_freqs(wpa_s, freqs,
4047 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004048 wpa_printf(MSG_DEBUG,
4049 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4050 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004051
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004052 if (freq > 0) {
4053 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4054 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4055 "(%u MHz) is not supported for P2P uses",
4056 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004057 res = -3;
4058 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004059 }
4060
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004061 for (i = 0; i < num; i++) {
4062 if (freqs[i] == freq)
4063 freq_in_use = 1;
4064 }
4065
4066 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4067 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4068 freq);
4069 res = -2;
4070 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004071 }
4072 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4073 "requested channel (%u MHz)", freq);
4074 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004075 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004076 }
4077
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004078 for (i = 0; i < num; i++) {
4079 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4080 continue;
4081
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004082#ifndef ANDROID_P2P
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004083 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4084 *force_freq);
4085 *force_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004086#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004087
4088 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4089 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004090 *pref_freq = freqs[i];
4091#ifdef ANDROID_P2P
4092 } else {
4093 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4094 *force_freq);
4095 *force_freq = freqs[i];
4096#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004097 }
4098 break;
4099 }
4100
4101 if (i == num) {
4102 if (num < wpa_s->num_multichan_concurrent) {
4103 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4104 *force_freq = 0;
4105 } else {
4106 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4107 res = -2;
4108 goto exit_free;
4109 }
4110 }
4111
4112exit_ok:
4113 res = 0;
4114exit_free:
4115 os_free(freqs);
4116 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004117}
4118
4119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120/**
4121 * wpas_p2p_connect - Request P2P Group Formation to be started
4122 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4123 * @peer_addr: Address of the peer P2P Device
4124 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4125 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004126 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004127 * @join: Whether to join an existing group (as a client) instead of starting
4128 * Group Owner negotiation; @peer_addr is BSSID in that case
4129 * @auth: Whether to only authorize the connection instead of doing that and
4130 * initiating Group Owner negotiation
4131 * @go_intent: GO Intent or -1 to use default
4132 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004133 * @persistent_id: Persistent group credentials to use for forcing GO
4134 * parameters or -1 to generate new values (SSID/passphrase)
4135 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4136 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004137 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004138 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4139 * failure, -2 on failure due to channel not currently available,
4140 * -3 if forced channel is not supported
4141 */
4142int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4143 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004144 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004145 int go_intent, int freq, int persistent_id, int pd,
4146 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004147{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004148 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004149 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004150 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004151 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004152 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004153
4154 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4155 return -1;
4156
Dmitry Shmidt04949592012-07-19 12:16:46 -07004157 if (persistent_id >= 0) {
4158 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4159 if (ssid == NULL || ssid->disabled != 2 ||
4160 ssid->mode != WPAS_MODE_P2P_GO)
4161 return -1;
4162 }
4163
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004164 os_free(wpa_s->global->add_psk);
4165 wpa_s->global->add_psk = NULL;
4166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004167 if (go_intent < 0)
4168 go_intent = wpa_s->conf->p2p_go_intent;
4169
4170 if (!auth)
4171 wpa_s->p2p_long_listen = 0;
4172
4173 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004174 wpa_s->p2p_persistent_group = !!persistent_group;
4175 wpa_s->p2p_persistent_id = persistent_id;
4176 wpa_s->p2p_go_intent = go_intent;
4177 wpa_s->p2p_connect_freq = freq;
4178 wpa_s->p2p_fallback_to_go_neg = 0;
4179 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004180 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004181
4182 if (pin)
4183 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4184 else if (wps_method == WPS_PIN_DISPLAY) {
4185 ret = wps_generate_pin();
4186 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4187 ret);
4188 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4189 wpa_s->p2p_pin);
4190 } else
4191 wpa_s->p2p_pin[0] = '\0';
4192
Dmitry Shmidt04949592012-07-19 12:16:46 -07004193 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4195 if (auth) {
4196 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4197 "connect a running group from " MACSTR,
4198 MAC2STR(peer_addr));
4199 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4200 return ret;
4201 }
4202 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4203 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4204 iface_addr) < 0) {
4205 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4206 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4207 dev_addr);
4208 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004209 if (auto_join) {
4210 os_get_time(&wpa_s->p2p_auto_started);
4211 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4212 "%ld.%06ld",
4213 wpa_s->p2p_auto_started.sec,
4214 wpa_s->p2p_auto_started.usec);
4215 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004216 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004217 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4218 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004219 return -1;
4220 return ret;
4221 }
4222
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004223 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004224 if (res)
4225 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004226 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227
4228 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004230 if (wpa_s->create_p2p_iface) {
4231 /* Prepare to add a new interface for the group */
4232 iftype = WPA_IF_P2P_GROUP;
4233 if (go_intent == 15)
4234 iftype = WPA_IF_P2P_GO;
4235 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4236 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4237 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004238 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004239 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004241 if_addr = wpa_s->pending_interface_addr;
4242 } else
4243 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004244
4245 if (auth) {
4246 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004247 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004248 force_freq, persistent_group, ssid,
4249 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250 return -1;
4251 return ret;
4252 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004253
4254 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4255 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004256 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004257 if (wpa_s->create_p2p_iface)
4258 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259 return -1;
4260 }
4261 return ret;
4262}
4263
4264
4265/**
4266 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4267 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4268 * @freq: Frequency of the channel in MHz
4269 * @duration: Duration of the stay on the channel in milliseconds
4270 *
4271 * This callback is called when the driver indicates that it has started the
4272 * requested remain-on-channel duration.
4273 */
4274void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4275 unsigned int freq, unsigned int duration)
4276{
4277 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4278 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004279 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4280 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4281 wpa_s->pending_listen_duration);
4282 wpa_s->pending_listen_freq = 0;
4283 }
4284}
4285
4286
4287static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4288 unsigned int timeout)
4289{
4290 /* Limit maximum Listen state time based on driver limitation. */
4291 if (timeout > wpa_s->max_remain_on_chan)
4292 timeout = wpa_s->max_remain_on_chan;
4293
4294 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4295 return wpa_drv_p2p_listen(wpa_s, timeout);
4296
4297 return p2p_listen(wpa_s->global->p2p, timeout);
4298}
4299
4300
4301/**
4302 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4303 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4304 * @freq: Frequency of the channel in MHz
4305 *
4306 * This callback is called when the driver indicates that a remain-on-channel
4307 * operation has been completed, i.e., the duration on the requested channel
4308 * has timed out.
4309 */
4310void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4311 unsigned int freq)
4312{
4313 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4314 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004315 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004316 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4317 return;
4318 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4319 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004320 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004321 return;
4322 if (wpa_s->p2p_long_listen > 0)
4323 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4324 if (wpa_s->p2p_long_listen > 0) {
4325 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4326 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004327 } else {
4328 /*
4329 * When listen duration is over, stop listen & update p2p_state
4330 * to IDLE.
4331 */
4332 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004333 }
4334}
4335
4336
4337/**
4338 * wpas_p2p_group_remove - Remove a P2P group
4339 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4340 * @ifname: Network interface name of the group interface or "*" to remove all
4341 * groups
4342 * Returns: 0 on success, -1 on failure
4343 *
4344 * This function is used to remove a P2P group. This can be used to disconnect
4345 * from a group in which the local end is a P2P Client or to end a P2P Group in
4346 * case the local end is the Group Owner. If a virtual network interface was
4347 * created for this group, that interface will be removed. Otherwise, only the
4348 * configured P2P group network will be removed from the interface.
4349 */
4350int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4351{
4352 struct wpa_global *global = wpa_s->global;
4353
4354 if (os_strcmp(ifname, "*") == 0) {
4355 struct wpa_supplicant *prev;
4356 wpa_s = global->ifaces;
4357 while (wpa_s) {
4358 prev = wpa_s;
4359 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004360 if (prev->p2p_group_interface !=
4361 NOT_P2P_GROUP_INTERFACE ||
4362 (prev->current_ssid &&
4363 prev->current_ssid->p2p_group))
4364 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365 }
4366 return 0;
4367 }
4368
4369 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4370 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4371 break;
4372 }
4373
4374 return wpas_p2p_disconnect(wpa_s);
4375}
4376
4377
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004378static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4379{
4380 unsigned int r;
4381
4382 if (freq == 2) {
4383 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4384 "band");
4385 if (wpa_s->best_24_freq > 0 &&
4386 p2p_supported_freq(wpa_s->global->p2p,
4387 wpa_s->best_24_freq)) {
4388 freq = wpa_s->best_24_freq;
4389 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4390 "channel: %d MHz", freq);
4391 } else {
4392 os_get_random((u8 *) &r, sizeof(r));
4393 freq = 2412 + (r % 3) * 25;
4394 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4395 "channel: %d MHz", freq);
4396 }
4397 }
4398
4399 if (freq == 5) {
4400 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4401 "band");
4402 if (wpa_s->best_5_freq > 0 &&
4403 p2p_supported_freq(wpa_s->global->p2p,
4404 wpa_s->best_5_freq)) {
4405 freq = wpa_s->best_5_freq;
4406 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4407 "channel: %d MHz", freq);
4408 } else {
4409 os_get_random((u8 *) &r, sizeof(r));
4410 freq = 5180 + (r % 4) * 20;
4411 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4412 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4413 "5 GHz channel for P2P group");
4414 return -1;
4415 }
4416 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4417 "channel: %d MHz", freq);
4418 }
4419 }
4420
4421 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4422 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4423 "(%u MHz) is not supported for P2P uses",
4424 freq);
4425 return -1;
4426 }
4427
4428 return freq;
4429}
4430
4431
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004432static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4433 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004434 int freq, int ht40,
4435 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004437 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004438 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004439 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004440
4441 os_memset(params, 0, sizeof(*params));
4442 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004443 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004445 if (!freq_included(channels, freq)) {
4446 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4447 "accepted", freq);
4448 return -1;
4449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4451 "frequency %d MHz", freq);
4452 params->freq = freq;
4453 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4454 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004455 wpa_s->conf->p2p_oper_channel <= 11 &&
4456 freq_included(channels,
4457 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4459 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4460 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004461 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4462 wpa_s->conf->p2p_oper_reg_class == 116 ||
4463 wpa_s->conf->p2p_oper_reg_class == 117 ||
4464 wpa_s->conf->p2p_oper_reg_class == 124 ||
4465 wpa_s->conf->p2p_oper_reg_class == 126 ||
4466 wpa_s->conf->p2p_oper_reg_class == 127) &&
4467 freq_included(channels,
4468 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004469 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4470 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4471 "frequency %d MHz", params->freq);
4472 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4473 wpa_s->best_overall_freq > 0 &&
4474 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004475 wpa_s->best_overall_freq) &&
4476 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004477 params->freq = wpa_s->best_overall_freq;
4478 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4479 "channel %d MHz", params->freq);
4480 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4481 wpa_s->best_24_freq > 0 &&
4482 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004483 wpa_s->best_24_freq) &&
4484 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004485 params->freq = wpa_s->best_24_freq;
4486 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4487 "channel %d MHz", params->freq);
4488 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4489 wpa_s->best_5_freq > 0 &&
4490 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004491 wpa_s->best_5_freq) &&
4492 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004493 params->freq = wpa_s->best_5_freq;
4494 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4495 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004496 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4497 channels))) {
4498 params->freq = pref_freq;
4499 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4500 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004502 int chan;
4503 for (chan = 0; chan < 11; chan++) {
4504 params->freq = 2412 + chan * 5;
4505 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004506 params->freq) &&
4507 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004508 break;
4509 }
4510 if (chan == 11) {
4511 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4512 "allowed");
4513 return -1;
4514 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4516 "known)", params->freq);
4517 }
4518
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004519 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4520 if (!freqs)
4521 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004522
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004523 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4524 wpa_s->num_multichan_concurrent);
4525 if (res < 0) {
4526 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004527 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004528 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004529 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004530
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004531 if (!freq) {
4532 for (i = 0; i < num; i++) {
4533 if (freq_included(channels, freqs[i])) {
4534 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4535 freqs[i]);
4536 params->freq = freqs[i];
4537 break;
4538 }
4539 }
4540
4541 if (i == num) {
4542 if (num == wpa_s->num_multichan_concurrent) {
4543 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4544 os_free(freqs);
4545 return -1;
4546 } else {
4547 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4548 }
4549 }
4550 } else {
4551 for (i = 0; i < num; i++) {
4552 if (freqs[i] == freq)
4553 break;
4554 }
4555
4556 if (i == num) {
4557 if (num == wpa_s->num_multichan_concurrent) {
4558 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4559 os_free(freqs);
4560 return -1;
4561 } else {
4562 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4563 }
4564 }
4565 }
4566 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004567 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568}
4569
4570
4571static struct wpa_supplicant *
4572wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4573 int go)
4574{
4575 struct wpa_supplicant *group_wpa_s;
4576
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004577 if (!wpas_p2p_create_iface(wpa_s)) {
4578 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4579 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004580 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004581 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004582 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004583
4584 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004585 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004586 wpa_msg_global(wpa_s, MSG_ERROR,
4587 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004588 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004589 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004590 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4591 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004592 wpa_msg_global(wpa_s, MSG_ERROR,
4593 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004594 wpas_p2p_remove_pending_group_interface(wpa_s);
4595 return NULL;
4596 }
4597
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004598 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4599 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004600 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601 return group_wpa_s;
4602}
4603
4604
4605/**
4606 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4607 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4608 * @persistent_group: Whether to create a persistent group
4609 * @freq: Frequency for the group or 0 to indicate no hardcoding
4610 * Returns: 0 on success, -1 on failure
4611 *
4612 * This function creates a new P2P group with the local end as the Group Owner,
4613 * i.e., without using Group Owner Negotiation.
4614 */
4615int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004616 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617{
4618 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619
4620 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4621 return -1;
4622
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004623 os_free(wpa_s->global->add_psk);
4624 wpa_s->global->add_psk = NULL;
4625
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004626 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004627 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004628 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004629
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004630 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4631 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004634 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004635 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004636 if (params.freq &&
4637 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4638 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4639 "(%u MHz) is not supported for P2P uses",
4640 params.freq);
4641 return -1;
4642 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643 p2p_go_params(wpa_s->global->p2p, &params);
4644 params.persistent_group = persistent_group;
4645
4646 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4647 if (wpa_s == NULL)
4648 return -1;
4649 wpas_start_wps_go(wpa_s, &params, 0);
4650
4651 return 0;
4652}
4653
4654
4655static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4656 struct wpa_ssid *params, int addr_allocated)
4657{
4658 struct wpa_ssid *ssid;
4659
4660 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4661 if (wpa_s == NULL)
4662 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004663 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004664
4665 wpa_supplicant_ap_deinit(wpa_s);
4666
4667 ssid = wpa_config_add_network(wpa_s->conf);
4668 if (ssid == NULL)
4669 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670 wpa_config_set_network_defaults(ssid);
4671 ssid->temporary = 1;
4672 ssid->proto = WPA_PROTO_RSN;
4673 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4674 ssid->group_cipher = WPA_CIPHER_CCMP;
4675 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4676 ssid->ssid = os_malloc(params->ssid_len);
4677 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004678 wpa_config_remove_network(wpa_s->conf, ssid->id);
4679 return -1;
4680 }
4681 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4682 ssid->ssid_len = params->ssid_len;
4683 ssid->p2p_group = 1;
4684 ssid->export_keys = 1;
4685 if (params->psk_set) {
4686 os_memcpy(ssid->psk, params->psk, 32);
4687 ssid->psk_set = 1;
4688 }
4689 if (params->passphrase)
4690 ssid->passphrase = os_strdup(params->passphrase);
4691
4692 wpa_supplicant_select_network(wpa_s, ssid);
4693
4694 wpa_s->show_group_started = 1;
4695
4696 return 0;
4697}
4698
4699
4700int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4701 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004702 int freq, int ht40,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004703 const struct p2p_channels *channels,
4704 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004705{
4706 struct p2p_go_neg_results params;
4707 int go = 0;
4708
4709 if (ssid->disabled != 2 || ssid->ssid == NULL)
4710 return -1;
4711
4712 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4713 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4714 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4715 "already running");
4716 return 0;
4717 }
4718
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004719 os_free(wpa_s->global->add_psk);
4720 wpa_s->global->add_psk = NULL;
4721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004723 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004724
Dmitry Shmidt04949592012-07-19 12:16:46 -07004725 wpa_s->p2p_fallback_to_go_neg = 0;
4726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 if (ssid->mode == WPAS_MODE_INFRA)
4728 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4729
4730 if (ssid->mode != WPAS_MODE_P2P_GO)
4731 return -1;
4732
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004733 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4734 if (freq < 0)
4735 return -1;
4736
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004737 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004738 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739
4740 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004741 params.psk_set = ssid->psk_set;
4742 if (params.psk_set)
4743 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004744 if (ssid->passphrase) {
4745 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4746 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4747 "persistent group");
4748 return -1;
4749 }
4750 os_strlcpy(params.passphrase, ssid->passphrase,
4751 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004752 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4754 params.ssid_len = ssid->ssid_len;
4755 params.persistent_group = 1;
4756
4757 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4758 if (wpa_s == NULL)
4759 return -1;
4760
Dmitry Shmidt56052862013-10-04 10:23:25 -07004761 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762 wpas_start_wps_go(wpa_s, &params, 0);
4763
4764 return 0;
4765}
4766
4767
4768static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4769 struct wpabuf *proberesp_ies)
4770{
4771 struct wpa_supplicant *wpa_s = ctx;
4772 if (wpa_s->ap_iface) {
4773 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004774 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4775 wpabuf_free(beacon_ies);
4776 wpabuf_free(proberesp_ies);
4777 return;
4778 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004779 if (beacon_ies) {
4780 wpabuf_free(hapd->p2p_beacon_ie);
4781 hapd->p2p_beacon_ie = beacon_ies;
4782 }
4783 wpabuf_free(hapd->p2p_probe_resp_ie);
4784 hapd->p2p_probe_resp_ie = proberesp_ies;
4785 } else {
4786 wpabuf_free(beacon_ies);
4787 wpabuf_free(proberesp_ies);
4788 }
4789 wpa_supplicant_ap_update_beacon(wpa_s);
4790}
4791
4792
4793static void wpas_p2p_idle_update(void *ctx, int idle)
4794{
4795 struct wpa_supplicant *wpa_s = ctx;
4796 if (!wpa_s->ap_iface)
4797 return;
4798 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004799 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800 wpas_p2p_set_group_idle_timeout(wpa_s);
4801 else
4802 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4803}
4804
4805
4806struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004807 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004808{
4809 struct p2p_group *group;
4810 struct p2p_group_config *cfg;
4811
4812 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4813 return NULL;
4814 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4815 return NULL;
4816
4817 cfg = os_zalloc(sizeof(*cfg));
4818 if (cfg == NULL)
4819 return NULL;
4820
Dmitry Shmidt04949592012-07-19 12:16:46 -07004821 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004822 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004823 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004824 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4826 if (wpa_s->max_stations &&
4827 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4828 cfg->max_clients = wpa_s->max_stations;
4829 else
4830 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004831 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4832 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004833 cfg->cb_ctx = wpa_s;
4834 cfg->ie_update = wpas_p2p_ie_update;
4835 cfg->idle_update = wpas_p2p_idle_update;
4836
4837 group = p2p_group_init(wpa_s->global->p2p, cfg);
4838 if (group == NULL)
4839 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004840 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004841 p2p_group_notif_formation_done(group);
4842 wpa_s->p2p_group = group;
4843 return group;
4844}
4845
4846
4847void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4848 int registrar)
4849{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004850 struct wpa_ssid *ssid = wpa_s->current_ssid;
4851
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004852 if (!wpa_s->p2p_in_provisioning) {
4853 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4854 "provisioning not in progress");
4855 return;
4856 }
4857
Dmitry Shmidt04949592012-07-19 12:16:46 -07004858 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4859 u8 go_dev_addr[ETH_ALEN];
4860 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4861 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4862 ssid->ssid_len);
4863 /* Clear any stored provisioning info */
4864 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4865 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4868 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004869 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004870 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4871 /*
4872 * Use a separate timeout for initial data connection to
4873 * complete to allow the group to be removed automatically if
4874 * something goes wrong in this step before the P2P group idle
4875 * timeout mechanism is taken into use.
4876 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07004877 wpa_dbg(wpa_s, MSG_DEBUG,
4878 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4879 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004880 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4881 wpas_p2p_group_formation_timeout,
4882 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004883 } else if (ssid) {
4884 /*
4885 * Use a separate timeout for initial data connection to
4886 * complete to allow the group to be removed automatically if
4887 * the client does not complete data connection successfully.
4888 */
4889 wpa_dbg(wpa_s, MSG_DEBUG,
4890 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
4891 P2P_MAX_INITIAL_CONN_WAIT_GO);
4892 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
4893 wpas_p2p_group_formation_timeout,
4894 wpa_s->parent, NULL);
4895 /*
4896 * Complete group formation on first successful data connection
4897 */
4898 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004899 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004900 if (wpa_s->global->p2p)
4901 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4902 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4903 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4904 wpas_group_formation_completed(wpa_s, 1);
4905}
4906
4907
Jouni Malinen75ecf522011-06-27 15:19:46 -07004908void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4909 struct wps_event_fail *fail)
4910{
4911 if (!wpa_s->p2p_in_provisioning) {
4912 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4913 "provisioning not in progress");
4914 return;
4915 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004916
4917 if (wpa_s->go_params) {
4918 p2p_clear_provisioning_info(
4919 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004920 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004921 }
4922
Jouni Malinen75ecf522011-06-27 15:19:46 -07004923 wpas_notify_p2p_wps_failed(wpa_s, fail);
4924}
4925
4926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004927int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004928 const char *config_method,
4929 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930{
4931 u16 config_methods;
4932
Dmitry Shmidt04949592012-07-19 12:16:46 -07004933 wpa_s->p2p_fallback_to_go_neg = 0;
4934 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004935 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004936 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004937 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004938 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004939 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4940 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004942 else {
4943 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004945 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004946
Dmitry Shmidt04949592012-07-19 12:16:46 -07004947 if (use == WPAS_P2P_PD_AUTO) {
4948 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4949 wpa_s->pending_pd_config_methods = config_methods;
4950 wpa_s->p2p_auto_pd = 1;
4951 wpa_s->p2p_auto_join = 0;
4952 wpa_s->pending_pd_before_join = 0;
4953 wpa_s->auto_pd_scan_retry = 0;
4954 wpas_p2p_stop_find(wpa_s);
4955 wpa_s->p2p_join_scan_count = 0;
4956 os_get_time(&wpa_s->p2p_auto_started);
4957 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4958 wpa_s->p2p_auto_started.sec,
4959 wpa_s->p2p_auto_started.usec);
4960 wpas_p2p_join_scan(wpa_s, NULL);
4961 return 0;
4962 }
4963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004964 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4965 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004966 config_methods,
4967 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968 }
4969
4970 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4971 return -1;
4972
4973 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004974 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004975 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004976}
4977
4978
4979int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4980 char *end)
4981{
4982 return p2p_scan_result_text(ies, ies_len, buf, end);
4983}
4984
4985
4986static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4987{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004988 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004989 return;
4990
4991 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4992 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004993 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004994}
4995
4996
4997int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4998 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004999 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005000 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005001{
5002 wpas_p2p_clear_pending_action_tx(wpa_s);
5003 wpa_s->p2p_long_listen = 0;
5004
5005 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5006 return wpa_drv_p2p_find(wpa_s, timeout, type);
5007
Dmitry Shmidt04949592012-07-19 12:16:46 -07005008 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5009 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010 return -1;
5011
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005012 wpa_supplicant_cancel_sched_scan(wpa_s);
5013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005015 num_req_dev_types, req_dev_types, dev_id,
5016 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017}
5018
5019
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005020static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021{
5022 wpas_p2p_clear_pending_action_tx(wpa_s);
5023 wpa_s->p2p_long_listen = 0;
5024 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5025 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07005026 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005027
5028 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5029 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005030 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005031 }
5032
5033 if (wpa_s->global->p2p)
5034 p2p_stop_find(wpa_s->global->p2p);
5035
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005036 return 0;
5037}
5038
5039
5040void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5041{
5042 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5043 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044 wpas_p2p_remove_pending_group_interface(wpa_s);
5045}
5046
5047
5048static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5049{
5050 struct wpa_supplicant *wpa_s = eloop_ctx;
5051 wpa_s->p2p_long_listen = 0;
5052}
5053
5054
5055int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5056{
5057 int res;
5058
5059 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5060 return -1;
5061
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005062 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063 wpas_p2p_clear_pending_action_tx(wpa_s);
5064
5065 if (timeout == 0) {
5066 /*
5067 * This is a request for unlimited Listen state. However, at
5068 * least for now, this is mapped to a Listen state for one
5069 * hour.
5070 */
5071 timeout = 3600;
5072 }
5073 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5074 wpa_s->p2p_long_listen = 0;
5075
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005076 /*
5077 * Stop previous find/listen operation to avoid trying to request a new
5078 * remain-on-channel operation while the driver is still running the
5079 * previous one.
5080 */
5081 if (wpa_s->global->p2p)
5082 p2p_stop_find(wpa_s->global->p2p);
5083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005084 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5085 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5086 wpa_s->p2p_long_listen = timeout * 1000;
5087 eloop_register_timeout(timeout, 0,
5088 wpas_p2p_long_listen_timeout,
5089 wpa_s, NULL);
5090 }
5091
5092 return res;
5093}
5094
5095
5096int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5097 u8 *buf, size_t len, int p2p_group)
5098{
5099 struct wpabuf *p2p_ie;
5100 int ret;
5101
5102 if (wpa_s->global->p2p_disabled)
5103 return -1;
5104 if (wpa_s->global->p2p == NULL)
5105 return -1;
5106 if (bss == NULL)
5107 return -1;
5108
5109 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5110 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5111 p2p_group, p2p_ie);
5112 wpabuf_free(p2p_ie);
5113
5114 return ret;
5115}
5116
5117
5118int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005119 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005120 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121{
5122 if (wpa_s->global->p2p_disabled)
5123 return 0;
5124 if (wpa_s->global->p2p == NULL)
5125 return 0;
5126
Dmitry Shmidt04949592012-07-19 12:16:46 -07005127 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5128 ie, ie_len)) {
5129 case P2P_PREQ_NOT_P2P:
5130 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5131 ssi_signal);
5132 /* fall through */
5133 case P2P_PREQ_MALFORMED:
5134 case P2P_PREQ_NOT_LISTEN:
5135 case P2P_PREQ_NOT_PROCESSED:
5136 default: /* make gcc happy */
5137 return 0;
5138 case P2P_PREQ_PROCESSED:
5139 return 1;
5140 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005141}
5142
5143
5144void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5145 const u8 *sa, const u8 *bssid,
5146 u8 category, const u8 *data, size_t len, int freq)
5147{
5148 if (wpa_s->global->p2p_disabled)
5149 return;
5150 if (wpa_s->global->p2p == NULL)
5151 return;
5152
5153 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5154 freq);
5155}
5156
5157
5158void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5159{
5160 if (wpa_s->global->p2p_disabled)
5161 return;
5162 if (wpa_s->global->p2p == NULL)
5163 return;
5164
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005165 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005166}
5167
5168
5169void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5170{
5171 p2p_group_deinit(wpa_s->p2p_group);
5172 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005173
5174 wpa_s->ap_configured_cb = NULL;
5175 wpa_s->ap_configured_cb_ctx = NULL;
5176 wpa_s->ap_configured_cb_data = NULL;
5177 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005178}
5179
5180
5181int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5182{
5183 wpa_s->p2p_long_listen = 0;
5184
5185 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5186 return wpa_drv_p2p_reject(wpa_s, addr);
5187
5188 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5189 return -1;
5190
5191 return p2p_reject(wpa_s->global->p2p, addr);
5192}
5193
5194
5195/* Invite to reinvoke a persistent group */
5196int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005197 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005198 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005199{
5200 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005201 u8 *bssid = NULL;
5202 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005203 int res;
5204
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005205 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005206 if (peer_addr)
5207 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5208 else
5209 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005210
Jouni Malinen31be0a42012-08-31 21:20:51 +03005211 wpa_s->p2p_persistent_go_freq = freq;
5212 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005213 if (ssid->mode == WPAS_MODE_P2P_GO) {
5214 role = P2P_INVITE_ROLE_GO;
5215 if (peer_addr == NULL) {
5216 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5217 "address in invitation command");
5218 return -1;
5219 }
5220 if (wpas_p2p_create_iface(wpa_s)) {
5221 if (wpas_p2p_add_group_interface(wpa_s,
5222 WPA_IF_P2P_GO) < 0) {
5223 wpa_printf(MSG_ERROR, "P2P: Failed to "
5224 "allocate a new interface for the "
5225 "group");
5226 return -1;
5227 }
5228 bssid = wpa_s->pending_interface_addr;
5229 } else
5230 bssid = wpa_s->own_addr;
5231 } else {
5232 role = P2P_INVITE_ROLE_CLIENT;
5233 peer_addr = ssid->bssid;
5234 }
5235 wpa_s->pending_invite_ssid_id = ssid->id;
5236
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005237 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005238 if (res)
5239 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005241 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5242 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5243 ssid->ssid, ssid->ssid_len,
5244 go_dev_addr, 1);
5245
5246 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5247 return -1;
5248
5249 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005250 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5251 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005252}
5253
5254
5255/* Invite to join an active group */
5256int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5257 const u8 *peer_addr, const u8 *go_dev_addr)
5258{
5259 struct wpa_global *global = wpa_s->global;
5260 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005261 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005262 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005263 int persistent;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005264 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005265 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005266
Jouni Malinen31be0a42012-08-31 21:20:51 +03005267 wpa_s->p2p_persistent_go_freq = 0;
5268 wpa_s->p2p_go_ht40 = 0;
5269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005270 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5271 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5272 break;
5273 }
5274 if (wpa_s == NULL) {
5275 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5276 return -1;
5277 }
5278
5279 ssid = wpa_s->current_ssid;
5280 if (ssid == NULL) {
5281 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5282 "invitation");
5283 return -1;
5284 }
5285
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005286 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005287 persistent = ssid->p2p_persistent_group &&
5288 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5289 ssid->ssid, ssid->ssid_len);
5290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005291 if (ssid->mode == WPAS_MODE_P2P_GO) {
5292 role = P2P_INVITE_ROLE_ACTIVE_GO;
5293 bssid = wpa_s->own_addr;
5294 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005295 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005296 } else {
5297 role = P2P_INVITE_ROLE_CLIENT;
5298 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5299 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5300 "invite to current group");
5301 return -1;
5302 }
5303 bssid = wpa_s->bssid;
5304 if (go_dev_addr == NULL &&
5305 !is_zero_ether_addr(wpa_s->go_dev_addr))
5306 go_dev_addr = wpa_s->go_dev_addr;
5307 }
5308 wpa_s->parent->pending_invite_ssid_id = -1;
5309
5310 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5311 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5312 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005313 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005314
5315 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5316 return -1;
5317
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005318 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005319 if (res)
5320 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005321 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005323 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005324 ssid->ssid, ssid->ssid_len, force_freq,
5325 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005326}
5327
5328
5329void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5330{
5331 struct wpa_ssid *ssid = wpa_s->current_ssid;
5332 const char *ssid_txt;
5333 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005334 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005335 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005336 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005337
Dmitry Shmidt04949592012-07-19 12:16:46 -07005338 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5339 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5340 wpa_s->parent, NULL);
5341 }
5342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005344 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005345
5346 wpa_s->show_group_started = 0;
5347
5348 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5349 os_memset(go_dev_addr, 0, ETH_ALEN);
5350 if (ssid->bssid_set)
5351 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5352 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5353 ssid->ssid_len);
5354 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5355
5356 if (wpa_s->global->p2p_group_formation == wpa_s)
5357 wpa_s->global->p2p_group_formation = NULL;
5358
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005359 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5360 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005361 if (ssid->passphrase == NULL && ssid->psk_set) {
5362 char psk[65];
5363 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005364 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5365 "%s client ssid=\"%s\" freq=%d psk=%s "
5366 "go_dev_addr=" MACSTR "%s",
5367 wpa_s->ifname, ssid_txt, freq, psk,
5368 MAC2STR(go_dev_addr),
5369 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005370 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005371 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5372 "%s client ssid=\"%s\" freq=%d "
5373 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5374 wpa_s->ifname, ssid_txt, freq,
5375 ssid->passphrase ? ssid->passphrase : "",
5376 MAC2STR(go_dev_addr),
5377 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005378 }
5379
5380 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005381 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5382 ssid, go_dev_addr);
5383 if (network_id < 0)
5384 network_id = ssid->id;
5385 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005386
5387done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005388 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389}
5390
5391
5392int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5393 u32 interval1, u32 duration2, u32 interval2)
5394{
5395 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5396 return -1;
5397 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5398 return -1;
5399
5400 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5401 wpa_s->current_ssid == NULL ||
5402 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5403 return -1;
5404
5405 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5406 wpa_s->own_addr, wpa_s->assoc_freq,
5407 duration1, interval1, duration2, interval2);
5408}
5409
5410
5411int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5412 unsigned int interval)
5413{
5414 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5415 return -1;
5416
5417 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5418 return -1;
5419
5420 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5421}
5422
5423
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005424static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5425{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005426 if (wpa_s->current_ssid == NULL) {
5427 /*
5428 * current_ssid can be cleared when P2P client interface gets
5429 * disconnected, so assume this interface was used as P2P
5430 * client.
5431 */
5432 return 1;
5433 }
5434 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005435 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5436}
5437
5438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005439static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5440{
5441 struct wpa_supplicant *wpa_s = eloop_ctx;
5442
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005443 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005444 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5445 "disabled");
5446 return;
5447 }
5448
Dmitry Shmidt04949592012-07-19 12:16:46 -07005449 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5450 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005451 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452}
5453
5454
5455static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5456{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005457 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458
Dmitry Shmidt04949592012-07-19 12:16:46 -07005459 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5460 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5463 return;
5464
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005465 timeout = wpa_s->conf->p2p_group_idle;
5466 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5467 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5468 timeout = P2P_MAX_CLIENT_IDLE;
5469
5470 if (timeout == 0)
5471 return;
5472
Dmitry Shmidt04949592012-07-19 12:16:46 -07005473 if (timeout < 0) {
5474 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5475 timeout = 0; /* special client mode no-timeout */
5476 else
5477 return;
5478 }
5479
5480 if (wpa_s->p2p_in_provisioning) {
5481 /*
5482 * Use the normal group formation timeout during the
5483 * provisioning phase to avoid terminating this process too
5484 * early due to group idle timeout.
5485 */
5486 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5487 "during provisioning");
5488 return;
5489 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305490
Dmitry Shmidt04949592012-07-19 12:16:46 -07005491 if (wpa_s->show_group_started) {
5492 /*
5493 * Use the normal group formation timeout between the end of
5494 * the provisioning phase and completion of 4-way handshake to
5495 * avoid terminating this process too early due to group idle
5496 * timeout.
5497 */
5498 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5499 "while waiting for initial 4-way handshake to "
5500 "complete");
5501 return;
5502 }
5503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005504 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005505 timeout);
5506 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5507 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508}
5509
5510
Jouni Malinen2b89da82012-08-31 22:04:41 +03005511/* Returns 1 if the interface was removed */
5512int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5513 u16 reason_code, const u8 *ie, size_t ie_len,
5514 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005515{
5516 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005517 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005518 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005519 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005520
Dmitry Shmidt04949592012-07-19 12:16:46 -07005521 if (!locally_generated)
5522 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5523 ie_len);
5524
5525 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5526 wpa_s->current_ssid &&
5527 wpa_s->current_ssid->p2p_group &&
5528 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5529 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5530 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005531 if (wpas_p2p_group_delete(wpa_s,
5532 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5533 > 0)
5534 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005535 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005536
5537 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005538}
5539
5540
5541void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005542 u16 reason_code, const u8 *ie, size_t ie_len,
5543 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005544{
5545 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5546 return;
5547 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5548 return;
5549
Dmitry Shmidt04949592012-07-19 12:16:46 -07005550 if (!locally_generated)
5551 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5552 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005553}
5554
5555
5556void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5557{
5558 struct p2p_data *p2p = wpa_s->global->p2p;
5559
5560 if (p2p == NULL)
5561 return;
5562
5563 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5564 return;
5565
5566 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5567 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5568
5569 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5570 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5571
5572 if (wpa_s->wps &&
5573 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5574 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5575
5576 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5577 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5578
5579 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5580 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5581 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5582 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5583 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5584 }
5585
5586 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5587 p2p_set_sec_dev_types(p2p,
5588 (void *) wpa_s->conf->sec_device_type,
5589 wpa_s->conf->num_sec_device_types);
5590
5591 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5592 int i;
5593 p2p_remove_wps_vendor_extensions(p2p);
5594 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5595 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5596 continue;
5597 p2p_add_wps_vendor_extension(
5598 p2p, wpa_s->conf->wps_vendor_ext[i]);
5599 }
5600 }
5601
5602 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5603 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5604 char country[3];
5605 country[0] = wpa_s->conf->country[0];
5606 country[1] = wpa_s->conf->country[1];
5607 country[2] = 0x04;
5608 p2p_set_country(p2p, country);
5609 }
5610
5611 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5612 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5613 wpa_s->conf->p2p_ssid_postfix ?
5614 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5615 0);
5616 }
5617
5618 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5619 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005620
5621 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5622 u8 reg_class, channel;
5623 int ret;
5624 unsigned int r;
5625 if (wpa_s->conf->p2p_listen_reg_class &&
5626 wpa_s->conf->p2p_listen_channel) {
5627 reg_class = wpa_s->conf->p2p_listen_reg_class;
5628 channel = wpa_s->conf->p2p_listen_channel;
5629 } else {
5630 reg_class = 81;
5631 /*
5632 * Pick one of the social channels randomly as the
5633 * listen channel.
5634 */
5635 os_get_random((u8 *) &r, sizeof(r));
5636 channel = 1 + (r % 3) * 5;
5637 }
5638 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5639 if (ret)
5640 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5641 "failed: %d", ret);
5642 }
5643 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5644 u8 op_reg_class, op_channel, cfg_op_channel;
5645 int ret = 0;
5646 unsigned int r;
5647 if (wpa_s->conf->p2p_oper_reg_class &&
5648 wpa_s->conf->p2p_oper_channel) {
5649 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5650 op_channel = wpa_s->conf->p2p_oper_channel;
5651 cfg_op_channel = 1;
5652 } else {
5653 op_reg_class = 81;
5654 /*
5655 * Use random operation channel from (1, 6, 11)
5656 *if no other preference is indicated.
5657 */
5658 os_get_random((u8 *) &r, sizeof(r));
5659 op_channel = 1 + (r % 3) * 5;
5660 cfg_op_channel = 0;
5661 }
5662 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5663 cfg_op_channel);
5664 if (ret)
5665 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5666 "failed: %d", ret);
5667 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005668
5669 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5670 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5671 wpa_s->conf->p2p_pref_chan) < 0) {
5672 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5673 "update failed");
5674 }
5675 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005676}
5677
5678
5679int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5680 int duration)
5681{
5682 if (!wpa_s->ap_iface)
5683 return -1;
5684 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5685 duration);
5686}
5687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005688
5689int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5690{
5691 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5692 return -1;
5693 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5694 return -1;
5695
5696 wpa_s->global->cross_connection = enabled;
5697 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5698
5699 if (!enabled) {
5700 struct wpa_supplicant *iface;
5701
5702 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5703 {
5704 if (iface->cross_connect_enabled == 0)
5705 continue;
5706
5707 iface->cross_connect_enabled = 0;
5708 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005709 wpa_msg_global(iface->parent, MSG_INFO,
5710 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5711 iface->ifname,
5712 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005713 }
5714 }
5715
5716 return 0;
5717}
5718
5719
5720static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5721{
5722 struct wpa_supplicant *iface;
5723
5724 if (!uplink->global->cross_connection)
5725 return;
5726
5727 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5728 if (!iface->cross_connect_enabled)
5729 continue;
5730 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5731 0)
5732 continue;
5733 if (iface->ap_iface == NULL)
5734 continue;
5735 if (iface->cross_connect_in_use)
5736 continue;
5737
5738 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005739 wpa_msg_global(iface->parent, MSG_INFO,
5740 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5741 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005742 }
5743}
5744
5745
5746static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5747{
5748 struct wpa_supplicant *iface;
5749
5750 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5751 if (!iface->cross_connect_enabled)
5752 continue;
5753 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5754 0)
5755 continue;
5756 if (!iface->cross_connect_in_use)
5757 continue;
5758
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005759 wpa_msg_global(iface->parent, MSG_INFO,
5760 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5761 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005762 iface->cross_connect_in_use = 0;
5763 }
5764}
5765
5766
5767void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5768{
5769 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5770 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5771 wpa_s->cross_connect_disallowed)
5772 wpas_p2p_disable_cross_connect(wpa_s);
5773 else
5774 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005775 if (!wpa_s->ap_iface &&
5776 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5777 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005778}
5779
5780
5781void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5782{
5783 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005784 if (!wpa_s->ap_iface &&
5785 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5786 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005787 wpas_p2p_set_group_idle_timeout(wpa_s);
5788}
5789
5790
5791static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5792{
5793 struct wpa_supplicant *iface;
5794
5795 if (!wpa_s->global->cross_connection)
5796 return;
5797
5798 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5799 if (iface == wpa_s)
5800 continue;
5801 if (iface->drv_flags &
5802 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5803 continue;
5804 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5805 continue;
5806
5807 wpa_s->cross_connect_enabled = 1;
5808 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5809 sizeof(wpa_s->cross_connect_uplink));
5810 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5811 "%s to %s whenever uplink is available",
5812 wpa_s->ifname, wpa_s->cross_connect_uplink);
5813
5814 if (iface->ap_iface || iface->current_ssid == NULL ||
5815 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5816 iface->cross_connect_disallowed ||
5817 iface->wpa_state != WPA_COMPLETED)
5818 break;
5819
5820 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005821 wpa_msg_global(wpa_s->parent, MSG_INFO,
5822 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5823 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005824 break;
5825 }
5826}
5827
5828
5829int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5830{
5831 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5832 !wpa_s->p2p_in_provisioning)
5833 return 0; /* not P2P client operation */
5834
5835 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5836 "session overlap");
5837 if (wpa_s != wpa_s->parent)
5838 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005839 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005840 return 1;
5841}
5842
5843
5844void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5845{
5846 struct p2p_channels chan;
5847
5848 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5849 return;
5850
5851 os_memset(&chan, 0, sizeof(chan));
5852 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5853 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5854 "channel list");
5855 return;
5856 }
5857
5858 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5859}
5860
5861
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005862static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5863 struct wpa_scan_results *scan_res)
5864{
5865 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5866}
5867
5868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005869int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5870{
5871 struct wpa_global *global = wpa_s->global;
5872 int found = 0;
5873 const u8 *peer;
5874
5875 if (global->p2p == NULL)
5876 return -1;
5877
5878 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5879
5880 if (wpa_s->pending_interface_name[0] &&
5881 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5882 found = 1;
5883
5884 peer = p2p_get_go_neg_peer(global->p2p);
5885 if (peer) {
5886 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5887 MACSTR, MAC2STR(peer));
5888 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005889 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005890 }
5891
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005892 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5893 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5894 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5895 found = 1;
5896 }
5897
5898 if (wpa_s->pending_pd_before_join) {
5899 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5900 wpa_s->pending_pd_before_join = 0;
5901 found = 1;
5902 }
5903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005904 wpas_p2p_stop_find(wpa_s);
5905
5906 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5907 if (wpa_s == global->p2p_group_formation &&
5908 (wpa_s->p2p_in_provisioning ||
5909 wpa_s->parent->pending_interface_type ==
5910 WPA_IF_P2P_CLIENT)) {
5911 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5912 "formation found - cancelling",
5913 wpa_s->ifname);
5914 found = 1;
5915 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5916 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005917 if (wpa_s->p2p_in_provisioning) {
5918 wpas_group_formation_completed(wpa_s, 0);
5919 break;
5920 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005921 wpas_p2p_group_delete(wpa_s,
5922 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005923 break;
5924 }
5925 }
5926
5927 if (!found) {
5928 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5929 return -1;
5930 }
5931
5932 return 0;
5933}
5934
5935
5936void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5937{
5938 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5939 return;
5940
5941 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5942 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005943 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005944}
5945
5946
5947void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5948 int freq_24, int freq_5, int freq_overall)
5949{
5950 struct p2p_data *p2p = wpa_s->global->p2p;
5951 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5952 return;
5953 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5954}
5955
5956
5957int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5958{
5959 u8 peer[ETH_ALEN];
5960 struct p2p_data *p2p = wpa_s->global->p2p;
5961
5962 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5963 return -1;
5964
5965 if (hwaddr_aton(addr, peer))
5966 return -1;
5967
5968 return p2p_unauthorize(p2p, peer);
5969}
5970
5971
5972/**
5973 * wpas_p2p_disconnect - Disconnect from a P2P Group
5974 * @wpa_s: Pointer to wpa_supplicant data
5975 * Returns: 0 on success, -1 on failure
5976 *
5977 * This can be used to disconnect from a group in which the local end is a P2P
5978 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5979 * virtual network interface was created for this group, that interface will be
5980 * removed. Otherwise, only the configured P2P group network will be removed
5981 * from the interface.
5982 */
5983int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5984{
5985
5986 if (wpa_s == NULL)
5987 return -1;
5988
Jouni Malinen2b89da82012-08-31 22:04:41 +03005989 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5990 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005991}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005992
5993
5994int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5995{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005996 int ret;
5997
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005998 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5999 return 0;
6000
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006001 ret = p2p_in_progress(wpa_s->global->p2p);
6002 if (ret == 0) {
6003 /*
6004 * Check whether there is an ongoing WPS provisioning step (or
6005 * other parts of group formation) on another interface since
6006 * p2p_in_progress() does not report this to avoid issues for
6007 * scans during such provisioning step.
6008 */
6009 if (wpa_s->global->p2p_group_formation &&
6010 wpa_s->global->p2p_group_formation != wpa_s) {
6011 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6012 "in group formation",
6013 wpa_s->global->p2p_group_formation->ifname);
6014 ret = 1;
6015 }
6016 }
6017
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006018 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6019 struct os_time now;
6020 os_get_time(&now);
6021 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
6022 P2P_MAX_INITIAL_CONN_WAIT_GO) {
6023 /* Wait for the first client has expired */
6024 wpa_s->global->p2p_go_wait_client.sec = 0;
6025 } else {
6026 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6027 ret = 1;
6028 }
6029 }
6030
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006031 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006032}
6033
6034
6035void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6036 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006037{
6038 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6039 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6040 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006041 /**
6042 * Remove the network by scheduling the group formation
6043 * timeout to happen immediately. The teardown code
6044 * needs to be scheduled to run asynch later so that we
6045 * don't delete data from under ourselves unexpectedly.
6046 * Calling wpas_p2p_group_formation_timeout directly
6047 * causes a series of crashes in WPS failure scenarios.
6048 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006049 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6050 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006051 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6052 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006053 }
6054}
6055
6056
6057struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006058 const u8 *addr, const u8 *ssid,
6059 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006060{
6061 struct wpa_ssid *s;
6062 size_t i;
6063
6064 for (s = wpa_s->conf->ssid; s; s = s->next) {
6065 if (s->disabled != 2)
6066 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006067 if (ssid &&
6068 (ssid_len != s->ssid_len ||
6069 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6070 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006071 if (addr == NULL) {
6072 if (s->mode == WPAS_MODE_P2P_GO)
6073 return s;
6074 continue;
6075 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006076 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6077 return s; /* peer is GO in the persistent group */
6078 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6079 continue;
6080 for (i = 0; i < s->num_p2p_clients; i++) {
6081 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6082 addr, ETH_ALEN) == 0)
6083 return s; /* peer is P2P client in persistent
6084 * group */
6085 }
6086 }
6087
6088 return NULL;
6089}
6090
6091
6092void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6093 const u8 *addr)
6094{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006095 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6096 wpa_s->parent, NULL) > 0) {
6097 /*
6098 * This can happen if WPS provisioning step is not terminated
6099 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6100 * peer was able to connect, there is no need to time out group
6101 * formation after this, though. In addition, this is used with
6102 * the initial connection wait on the GO as a separate formation
6103 * timeout and as such, expected to be hit after the initial WPS
6104 * provisioning step.
6105 */
6106 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6107 }
6108 if (!wpa_s->p2p_go_group_formation_completed) {
6109 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6110 wpa_s->p2p_go_group_formation_completed = 1;
6111 wpa_s->global->p2p_group_formation = NULL;
6112 wpa_s->p2p_in_provisioning = 0;
6113 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006114 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006115 if (addr == NULL)
6116 return;
6117 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6118}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006119
Dmitry Shmidt04949592012-07-19 12:16:46 -07006120
6121static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6122 int group_added)
6123{
6124 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006125 if (wpa_s->global->p2p_group_formation)
6126 group = wpa_s->global->p2p_group_formation;
6127 wpa_s = wpa_s->parent;
6128 offchannel_send_action_done(wpa_s);
6129 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006130 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006131 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6132 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6133 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6134 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6135 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006136 wpa_s->p2p_pd_before_go_neg,
6137 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006138}
6139
6140
6141int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6142{
6143 if (!wpa_s->p2p_fallback_to_go_neg ||
6144 wpa_s->p2p_in_provisioning <= 5)
6145 return 0;
6146
6147 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6148 return 0; /* peer operating as a GO */
6149
6150 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6151 "fallback to GO Negotiation");
6152 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6153
6154 return 1;
6155}
6156
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006157
6158unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6159{
6160 const char *rn, *rn2;
6161 struct wpa_supplicant *ifs;
6162
6163 if (wpa_s->wpa_state > WPA_SCANNING) {
6164 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6165 "concurrent operation",
6166 P2P_CONCURRENT_SEARCH_DELAY);
6167 return P2P_CONCURRENT_SEARCH_DELAY;
6168 }
6169
6170 if (!wpa_s->driver->get_radio_name)
6171 return 0;
6172 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
6173 if (rn == NULL || rn[0] == '\0')
6174 return 0;
6175
6176 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6177 if (ifs == wpa_s || !ifs->driver->get_radio_name)
6178 continue;
6179
6180 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
6181 if (!rn2 || os_strcmp(rn, rn2) != 0)
6182 continue;
6183 if (ifs->wpa_state > WPA_SCANNING) {
6184 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6185 "delay due to concurrent operation on "
6186 "interface %s",
6187 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6188 return P2P_CONCURRENT_SEARCH_DELAY;
6189 }
6190 }
6191
6192 return 0;
6193}
6194
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006195
6196void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6197{
6198 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6199 "pending anymore (sta_scan_pending=%d "
6200 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6201 wpa_s->global->p2p_cb_on_scan_complete);
6202 wpa_s->sta_scan_pending = 0;
6203
6204 if (!wpa_s->global->p2p_cb_on_scan_complete)
6205 return;
6206 wpa_s->global->p2p_cb_on_scan_complete = 0;
6207
6208 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6209 return;
6210
6211 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6212 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6213 "continued after successful connection");
6214 p2p_increase_search_delay(wpa_s->global->p2p,
6215 wpas_p2p_search_delay(wpa_s));
6216 }
6217}
6218
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006219
6220static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6221 struct wpa_ssid *s, const u8 *addr,
6222 int iface_addr)
6223{
6224 struct psk_list_entry *psk, *tmp;
6225 int changed = 0;
6226
6227 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6228 list) {
6229 if ((iface_addr && !psk->p2p &&
6230 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6231 (!iface_addr && psk->p2p &&
6232 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6233 wpa_dbg(wpa_s, MSG_DEBUG,
6234 "P2P: Remove persistent group PSK list entry for "
6235 MACSTR " p2p=%u",
6236 MAC2STR(psk->addr), psk->p2p);
6237 dl_list_del(&psk->list);
6238 os_free(psk);
6239 changed++;
6240 }
6241 }
6242
6243 return changed;
6244}
6245
6246
6247void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6248 const u8 *p2p_dev_addr,
6249 const u8 *psk, size_t psk_len)
6250{
6251 struct wpa_ssid *ssid = wpa_s->current_ssid;
6252 struct wpa_ssid *persistent;
6253 struct psk_list_entry *p;
6254
6255 if (psk_len != sizeof(p->psk))
6256 return;
6257
6258 if (p2p_dev_addr) {
6259 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6260 " p2p_dev_addr=" MACSTR,
6261 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6262 if (is_zero_ether_addr(p2p_dev_addr))
6263 p2p_dev_addr = NULL;
6264 } else {
6265 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6266 MAC2STR(mac_addr));
6267 }
6268
6269 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6270 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6271 /* To be added to persistent group once created */
6272 if (wpa_s->global->add_psk == NULL) {
6273 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6274 if (wpa_s->global->add_psk == NULL)
6275 return;
6276 }
6277 p = wpa_s->global->add_psk;
6278 if (p2p_dev_addr) {
6279 p->p2p = 1;
6280 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6281 } else {
6282 p->p2p = 0;
6283 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6284 }
6285 os_memcpy(p->psk, psk, psk_len);
6286 return;
6287 }
6288
6289 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6290 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6291 return;
6292 }
6293
6294 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6295 ssid->ssid_len);
6296 if (!persistent) {
6297 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6298 return;
6299 }
6300
6301 p = os_zalloc(sizeof(*p));
6302 if (p == NULL)
6303 return;
6304 if (p2p_dev_addr) {
6305 p->p2p = 1;
6306 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6307 } else {
6308 p->p2p = 0;
6309 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6310 }
6311 os_memcpy(p->psk, psk, psk_len);
6312
6313 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6314 struct psk_list_entry *last;
6315 last = dl_list_last(&persistent->psk_list,
6316 struct psk_list_entry, list);
6317 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6318 MACSTR " (p2p=%u) to make room for a new one",
6319 MAC2STR(last->addr), last->p2p);
6320 dl_list_del(&last->list);
6321 os_free(last);
6322 }
6323
6324 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6325 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6326 p2p_dev_addr == NULL);
6327 if (p2p_dev_addr) {
6328 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6329 MACSTR, MAC2STR(p2p_dev_addr));
6330 } else {
6331 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6332 MAC2STR(mac_addr));
6333 }
6334 dl_list_add(&persistent->psk_list, &p->list);
6335
6336#ifndef CONFIG_NO_CONFIG_WRITE
6337 if (wpa_s->parent->conf->update_config &&
6338 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6339 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6340#endif /* CONFIG_NO_CONFIG_WRITE */
6341}
6342
6343
6344static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6345 struct wpa_ssid *s, const u8 *addr,
6346 int iface_addr)
6347{
6348 int res;
6349
6350 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6351 if (res > 0) {
6352#ifndef CONFIG_NO_CONFIG_WRITE
6353 if (wpa_s->conf->update_config &&
6354 wpa_config_write(wpa_s->confname, wpa_s->conf))
6355 wpa_dbg(wpa_s, MSG_DEBUG,
6356 "P2P: Failed to update configuration");
6357#endif /* CONFIG_NO_CONFIG_WRITE */
6358 }
6359}
6360
6361
6362static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6363 const u8 *peer, int iface_addr)
6364{
6365 struct hostapd_data *hapd;
6366 struct hostapd_wpa_psk *psk, *prev, *rem;
6367 struct sta_info *sta;
6368
6369 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6370 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6371 return;
6372
6373 /* Remove per-station PSK entry */
6374 hapd = wpa_s->ap_iface->bss[0];
6375 prev = NULL;
6376 psk = hapd->conf->ssid.wpa_psk;
6377 while (psk) {
6378 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6379 (!iface_addr &&
6380 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6381 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6382 MACSTR " iface_addr=%d",
6383 MAC2STR(peer), iface_addr);
6384 if (prev)
6385 prev->next = psk->next;
6386 else
6387 hapd->conf->ssid.wpa_psk = psk->next;
6388 rem = psk;
6389 psk = psk->next;
6390 os_free(rem);
6391 } else {
6392 prev = psk;
6393 psk = psk->next;
6394 }
6395 }
6396
6397 /* Disconnect from group */
6398 if (iface_addr)
6399 sta = ap_get_sta(hapd, peer);
6400 else
6401 sta = ap_get_sta_p2p(hapd, peer);
6402 if (sta) {
6403 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6404 " (iface_addr=%d) from group",
6405 MAC2STR(peer), iface_addr);
6406 hostapd_drv_sta_deauth(hapd, sta->addr,
6407 WLAN_REASON_DEAUTH_LEAVING);
6408 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6409 }
6410}
6411
6412
6413void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6414 int iface_addr)
6415{
6416 struct wpa_ssid *s;
6417 struct wpa_supplicant *w;
6418
6419 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6420
6421 /* Remove from any persistent group */
6422 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6423 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6424 continue;
6425 if (!iface_addr)
6426 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6427 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6428 }
6429
6430 /* Remove from any operating group */
6431 for (w = wpa_s->global->ifaces; w; w = w->next)
6432 wpas_p2p_remove_client_go(w, peer, iface_addr);
6433}
6434
6435
6436static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6437{
6438 struct wpa_supplicant *wpa_s = eloop_ctx;
6439 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6440}
6441
6442
6443int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6444{
6445 struct wpa_ssid *ssid = wpa_s->current_ssid;
6446
6447 if (ssid == NULL || !ssid->p2p_group)
6448 return 0;
6449
6450 if (wpa_s->p2p_last_4way_hs_fail &&
6451 wpa_s->p2p_last_4way_hs_fail == ssid) {
6452 u8 go_dev_addr[ETH_ALEN];
6453 struct wpa_ssid *persistent;
6454
6455 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6456 ssid->ssid,
6457 ssid->ssid_len) <= 0) {
6458 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6459 goto disconnect;
6460 }
6461
6462 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6463 MACSTR, MAC2STR(go_dev_addr));
6464 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6465 ssid->ssid,
6466 ssid->ssid_len);
6467 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6468 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6469 goto disconnect;
6470 }
6471 wpa_msg_global(wpa_s->parent, MSG_INFO,
6472 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6473 persistent->id);
6474 disconnect:
6475 wpa_s->p2p_last_4way_hs_fail = NULL;
6476 /*
6477 * Remove the group from a timeout to avoid issues with caller
6478 * continuing to use the interface if this is on a P2P group
6479 * interface.
6480 */
6481 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6482 wpa_s, NULL);
6483 return 1;
6484 }
6485
6486 wpa_s->p2p_last_4way_hs_fail = ssid;
6487 return 0;
6488}
6489
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006490#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006491static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6492{
6493 struct wpa_supplicant *wpa_s = eloop_ctx;
6494
6495 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6496 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6497}
6498
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006499int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6500 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006501{
6502 struct wpa_supplicant *iface = NULL;
6503 struct p2p_data *p2p = wpa_s->global->p2p;
6504
6505 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006506 if ((iface->current_ssid) &&
6507 (iface->current_ssid->frequency != freq) &&
6508 ((iface->p2p_group_interface) ||
6509 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006510
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006511 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
6512 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
6513 /* Try to see whether we can move the GO. If it
6514 * is not possible, remove the GO interface
6515 */
6516 if (wpa_drv_switch_channel(iface, freq) == 0) {
6517 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
6518 iface->current_ssid->frequency = freq;
6519 continue;
6520 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006521 }
6522
6523 /* If GO cannot be moved or if the conflicting interface is a
6524 * P2P Client, remove the interface depending up on the connection
6525 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006526 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07006527 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006528 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006529 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006530 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006531 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
6532 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006533 /* If connection in progress is p2p connection, do not proceed for the connection */
6534 if (wpa_s == iface)
6535 return -1;
6536 else
6537 /* If connection in progress is STA connection, proceed for the connection */
6538 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006539 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006540 /* P2p connection has priority, disable the STA network*/
6541 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
6542 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6543 " id=%d", ssid->id);
6544 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6545 if (wpa_s == iface) {
6546 /* p2p connection is in progress, continue connecting...*/
6547 return 0;
6548 }
6549 else {
6550 /* STA connection is in progress, do not allow to continue */
6551 return -1;
6552 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006553 }
6554 }
6555 }
6556 return 0;
6557}
6558#endif