blob: e23e96f7b945c44dc82bf6341c890d92cc974c58 [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 */
51#define P2P_MAX_CLIENT_IDLE 10
52#endif /* P2P_MAX_CLIENT_IDLE */
53
Dmitry Shmidt04949592012-07-19 12:16:46 -070054#ifndef P2P_MAX_INITIAL_CONN_WAIT
55/*
56 * How many seconds to wait for initial 4-way handshake to get completed after
57 * WPS provisioning step.
58 */
59#define P2P_MAX_INITIAL_CONN_WAIT 10
60#endif /* P2P_MAX_INITIAL_CONN_WAIT */
61
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070062#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
63/*
64 * How many seconds to wait for initial 4-way handshake to get completed after
65 * WPS provisioning step on the GO. This controls the extra time the P2P
66 * operation is considered to be in progress (e.g., to delay other scans) after
67 * WPS provisioning has been completed on the GO during group formation.
68 */
69#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
70#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
71
Dmitry Shmidt56052862013-10-04 10:23:25 -070072#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
73/*
74 * How many seconds to wait for initial 4-way handshake to get completed after
75 * re-invocation of a persistent group on the GO when the client is expected
76 * to connect automatically (no user interaction).
77 */
78#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
79#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
80
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070081#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070082#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070083#endif /* P2P_CONCURRENT_SEARCH_DELAY */
84
Dmitry Shmidt34af3062013-07-11 10:46:32 -070085#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
86
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087enum p2p_group_removal_reason {
88 P2P_GROUP_REMOVAL_UNKNOWN,
89 P2P_GROUP_REMOVAL_SILENT,
90 P2P_GROUP_REMOVAL_FORMATION_FAILED,
91 P2P_GROUP_REMOVAL_REQUESTED,
92 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
93 P2P_GROUP_REMOVAL_UNAVAILABLE,
94 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070095 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070096#ifdef ANDROID_P2P
97 P2P_GROUP_REMOVAL_FREQ_CONFLICT
98#endif
99};
100
Jouni Malinendc7b7132012-09-14 12:53:47 -0700101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
103static struct wpa_supplicant *
104wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
105 int go);
106static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700107static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
109static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700110 const u8 *dev_addr, enum p2p_wps_method wps_method,
111 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
113static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
114static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
115static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800116static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
117 void *timeout_ctx);
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700118#ifdef ANDROID_P2P
119static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
120#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700121static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
122 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800123static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124
125
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700126/*
127 * Get the number of concurrent channels that the HW can operate, but that are
128 * currently not in use by any of the wpa_supplicant interfaces.
129 */
130static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
131{
132 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800133 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700134
135 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
136 if (!freqs)
137 return -1;
138
139 num = get_shared_radio_freqs(wpa_s, freqs,
140 wpa_s->num_multichan_concurrent);
141 os_free(freqs);
142
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800143 unused = wpa_s->num_multichan_concurrent - num;
144 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
145 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700146}
147
148
149/*
150 * Get the frequencies that are currently in use by one or more of the virtual
151 * interfaces, and that are also valid for P2P operation.
152 */
153static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
154 int *p2p_freqs, unsigned int len)
155{
156 int *freqs;
157 unsigned int num, i, j;
158
159 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
160 if (!freqs)
161 return -1;
162
163 num = get_shared_radio_freqs(wpa_s, freqs,
164 wpa_s->num_multichan_concurrent);
165
166 os_memset(p2p_freqs, 0, sizeof(int) * len);
167
168 for (i = 0, j = 0; i < num && j < len; i++) {
169 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
170 p2p_freqs[j++] = freqs[i];
171 }
172
173 os_free(freqs);
174
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800175 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
176
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700177 return j;
178}
179
180
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700181static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
182 int freq)
183{
184 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
185 return;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700186 if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
187 wpas_p2p_num_unused_channels(wpa_s) > 0 &&
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700188 wpa_s->parent->conf->p2p_ignore_shared_freq)
189 freq = 0;
190 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
191}
192
193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
195 struct wpa_scan_results *scan_res)
196{
197 size_t i;
198
199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
200 return;
201
202 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
203 (int) scan_res->num);
204
205 for (i = 0; i < scan_res->num; i++) {
206 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800207 struct os_time time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700208 const u8 *ies;
209 size_t ies_len;
210
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800211 time_tmp_age.sec = bss->age / 1000;
212 time_tmp_age.usec = (bss->age % 1000) * 1000;
213 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700214
215 ies = (const u8 *) (bss + 1);
216 ies_len = bss->ie_len;
217 if (bss->beacon_ie_len > 0 &&
218 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
219 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
220 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
221 MACSTR, MAC2STR(bss->bssid));
222 ies = ies + ies_len;
223 ies_len = bss->beacon_ie_len;
224 }
225
226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800228 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700229 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 break;
231 }
232
233 p2p_scan_res_handled(wpa_s->global->p2p);
234}
235
236
237static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
238 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700239 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240{
241 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700242 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 struct wpa_driver_scan_params params;
244 int ret;
245 struct wpabuf *wps_ie, *ies;
246 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248
249 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
250 return -1;
251
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700252 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
253 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700254 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700255 wpas_p2p_in_progress(wpa_s) == 2) {
256 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
257 "pending station mode scan to be "
258 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700259 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700260 wpa_supplicant_req_scan(ifs, 0, 0);
261 return 1;
262 }
263 }
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 os_memset(&params, 0, sizeof(params));
266
267 /* P2P Wildcard SSID */
268 params.num_ssids = 1;
269 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
270 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
271
272 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700273 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
274 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 num_req_dev_types, req_dev_types);
276 if (wps_ie == NULL)
277 return -1;
278
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800279 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
280 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 if (ies == NULL) {
282 wpabuf_free(wps_ie);
283 return -1;
284 }
285 wpabuf_put_buf(ies, wps_ie);
286 wpabuf_free(wps_ie);
287
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800288 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800290 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 params.extra_ies = wpabuf_head(ies);
292 params.extra_ies_len = wpabuf_len(ies);
293
294 switch (type) {
295 case P2P_SCAN_SOCIAL:
296 params.freqs = social_channels;
297 break;
298 case P2P_SCAN_FULL:
299 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 case P2P_SCAN_SOCIAL_PLUS_ONE:
301 social_channels[3] = freq;
302 params.freqs = social_channels;
303 break;
304 }
305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307
308 wpabuf_free(ies);
309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800310 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700311 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
312 if (ifs->scanning ||
313 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
314 wpa_s->global->p2p_cb_on_scan_complete = 1;
315 ret = 1;
316 break;
317 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800318 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700319 } else {
320 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700321 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700322 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 return ret;
325}
326
327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
329{
330 switch (p2p_group_interface) {
331 case P2P_GROUP_INTERFACE_PENDING:
332 return WPA_IF_P2P_GROUP;
333 case P2P_GROUP_INTERFACE_GO:
334 return WPA_IF_P2P_GO;
335 case P2P_GROUP_INTERFACE_CLIENT:
336 return WPA_IF_P2P_CLIENT;
337 }
338
339 return WPA_IF_P2P_GROUP;
340}
341
342
343static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
344 const u8 *ssid,
345 size_t ssid_len, int *go)
346{
347 struct wpa_ssid *s;
348
349 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
350 for (s = wpa_s->conf->ssid; s; s = s->next) {
351 if (s->disabled != 0 || !s->p2p_group ||
352 s->ssid_len != ssid_len ||
353 os_memcmp(ssid, s->ssid, ssid_len) != 0)
354 continue;
355 if (s->mode == WPAS_MODE_P2P_GO &&
356 s != wpa_s->current_ssid)
357 continue;
358 if (go)
359 *go = s->mode == WPAS_MODE_P2P_GO;
360 return wpa_s;
361 }
362 }
363
364 return NULL;
365}
366
367
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700368static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
369 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370{
371 struct wpa_ssid *ssid;
372 char *gtype;
373 const char *reason;
374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 ssid = wpa_s->current_ssid;
376 if (ssid == NULL) {
377 /*
378 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700379 * pending P2P group interface waiting for provisioning or a
380 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 */
382 ssid = wpa_s->conf->ssid;
383 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700384 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 break;
386 ssid = ssid->next;
387 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300388 if (ssid == NULL &&
389 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
390 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700391 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
392 "not found");
393 return -1;
394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395 }
396 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
397 gtype = "GO";
398 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
399 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
400 wpa_s->reassociate = 0;
401 wpa_s->disconnected = 1;
402 wpa_supplicant_deauthenticate(wpa_s,
403 WLAN_REASON_DEAUTH_LEAVING);
404 gtype = "client";
405 } else
406 gtype = "GO";
407 if (wpa_s->cross_connect_in_use) {
408 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700409 wpa_msg_global(wpa_s->parent, MSG_INFO,
410 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
411 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700413 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 case P2P_GROUP_REMOVAL_REQUESTED:
415 reason = " reason=REQUESTED";
416 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700417 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
418 reason = " reason=FORMATION_FAILED";
419 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
421 reason = " reason=IDLE";
422 break;
423 case P2P_GROUP_REMOVAL_UNAVAILABLE:
424 reason = " reason=UNAVAILABLE";
425 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700426 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
427 reason = " reason=GO_ENDING_SESSION";
428 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700429 case P2P_GROUP_REMOVAL_PSK_FAILURE:
430 reason = " reason=PSK_FAILURE";
431 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700432#ifdef ANDROID_P2P
433 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
434 reason = " reason=FREQ_CONFLICT";
435 break;
436#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 default:
438 reason = "";
439 break;
440 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700441 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700442 wpa_msg_global(wpa_s->parent, MSG_INFO,
443 P2P_EVENT_GROUP_REMOVED "%s %s%s",
444 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700445 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700447#ifdef ANDROID_P2P
448 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
449#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700450 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
451 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800452 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700453 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800454 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
455 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700456 wpa_s->p2p_in_provisioning = 0;
457 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700458
Dmitry Shmidt96571392013-10-14 12:54:46 -0700459 /*
460 * Make sure wait for the first client does not remain active after the
461 * group has been removed.
462 */
463 wpa_s->global->p2p_go_wait_client.sec = 0;
464
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
467
468 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
469 struct wpa_global *global;
470 char *ifname;
471 enum wpa_driver_if_type type;
472 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
473 wpa_s->ifname);
474 global = wpa_s->global;
475 ifname = os_strdup(wpa_s->ifname);
476 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700477 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 wpa_s = global->ifaces;
479 if (wpa_s && ifname)
480 wpa_drv_if_remove(wpa_s, type, ifname);
481 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300482 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 }
484
Dmitry Shmidt96571392013-10-14 12:54:46 -0700485 if (!wpa_s->p2p_go_group_formation_completed) {
486 wpa_s->global->p2p_group_formation = NULL;
487 wpa_s->p2p_in_provisioning = 0;
488 }
489
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800490 wpa_s->show_group_started = 0;
491 os_free(wpa_s->go_params);
492 wpa_s->go_params = NULL;
493
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
495 if (ssid && (ssid->p2p_group ||
496 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
497 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
498 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700499 if (ssid == wpa_s->current_ssid) {
500 wpa_sm_set_config(wpa_s->wpa, NULL);
501 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700503 }
504 /*
505 * Networks objects created during any P2P activities are not
506 * exposed out as they might/will confuse certain non-P2P aware
507 * applications since these network objects won't behave like
508 * regular ones.
509 *
510 * Likewise, we don't send out network removed signals for such
511 * network objects.
512 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 wpa_config_remove_network(wpa_s->conf, id);
514 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800515 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700516 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700517 } else {
518 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
519 "found");
520 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700521 if (wpa_s->ap_iface)
522 wpa_supplicant_ap_deinit(wpa_s);
523 else
524 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700525
526 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527}
528
529
530static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
531 u8 *go_dev_addr,
532 const u8 *ssid, size_t ssid_len)
533{
534 struct wpa_bss *bss;
535 const u8 *bssid;
536 struct wpabuf *p2p;
537 u8 group_capab;
538 const u8 *addr;
539
540 if (wpa_s->go_params)
541 bssid = wpa_s->go_params->peer_interface_addr;
542 else
543 bssid = wpa_s->bssid;
544
545 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
546 if (bss == NULL) {
547 u8 iface_addr[ETH_ALEN];
548 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
549 iface_addr) == 0)
550 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
551 }
552 if (bss == NULL) {
553 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
554 "group is persistent - BSS " MACSTR " not found",
555 MAC2STR(bssid));
556 return 0;
557 }
558
559 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700560 if (p2p == NULL)
561 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
562 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 if (p2p == NULL) {
564 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
565 "group is persistent - BSS " MACSTR
566 " did not include P2P IE", MAC2STR(bssid));
567 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
568 (u8 *) (bss + 1), bss->ie_len);
569 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
570 ((u8 *) bss + 1) + bss->ie_len,
571 bss->beacon_ie_len);
572 return 0;
573 }
574
575 group_capab = p2p_get_group_capab(p2p);
576 addr = p2p_get_go_dev_addr(p2p);
577 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
578 "group_capab=0x%x", group_capab);
579 if (addr) {
580 os_memcpy(go_dev_addr, addr, ETH_ALEN);
581 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
582 MAC2STR(addr));
583 } else
584 os_memset(go_dev_addr, 0, ETH_ALEN);
585 wpabuf_free(p2p);
586
587 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
588 "go_dev_addr=" MACSTR,
589 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
590
591 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
592}
593
594
Jouni Malinen75ecf522011-06-27 15:19:46 -0700595static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
596 struct wpa_ssid *ssid,
597 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598{
599 struct wpa_ssid *s;
600 int changed = 0;
601
602 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
603 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
604 for (s = wpa_s->conf->ssid; s; s = s->next) {
605 if (s->disabled == 2 &&
606 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
607 s->ssid_len == ssid->ssid_len &&
608 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
609 break;
610 }
611
612 if (s) {
613 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
614 "entry");
615 if (ssid->passphrase && !s->passphrase)
616 changed = 1;
617 else if (ssid->passphrase && s->passphrase &&
618 os_strcmp(ssid->passphrase, s->passphrase) != 0)
619 changed = 1;
620 } else {
621 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
622 "entry");
623 changed = 1;
624 s = wpa_config_add_network(wpa_s->conf);
625 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700626 return -1;
627
628 /*
629 * Instead of network_added we emit persistent_group_added
630 * notification. Also to keep the defense checks in
631 * persistent_group obj registration method, we set the
632 * relevant flags in s to designate it as a persistent group.
633 */
634 s->p2p_group = 1;
635 s->p2p_persistent_group = 1;
636 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637 wpa_config_set_network_defaults(s);
638 }
639
640 s->p2p_group = 1;
641 s->p2p_persistent_group = 1;
642 s->disabled = 2;
643 s->bssid_set = 1;
644 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
645 s->mode = ssid->mode;
646 s->auth_alg = WPA_AUTH_ALG_OPEN;
647 s->key_mgmt = WPA_KEY_MGMT_PSK;
648 s->proto = WPA_PROTO_RSN;
649 s->pairwise_cipher = WPA_CIPHER_CCMP;
650 s->export_keys = 1;
651 if (ssid->passphrase) {
652 os_free(s->passphrase);
653 s->passphrase = os_strdup(ssid->passphrase);
654 }
655 if (ssid->psk_set) {
656 s->psk_set = 1;
657 os_memcpy(s->psk, ssid->psk, 32);
658 }
659 if (s->passphrase && !s->psk_set)
660 wpa_config_update_psk(s);
661 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
662 os_free(s->ssid);
663 s->ssid = os_malloc(ssid->ssid_len);
664 }
665 if (s->ssid) {
666 s->ssid_len = ssid->ssid_len;
667 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
668 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700669 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
670 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
671 wpa_s->global->add_psk = NULL;
672 changed = 1;
673 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674
675#ifndef CONFIG_NO_CONFIG_WRITE
676 if (changed && wpa_s->conf->update_config &&
677 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
678 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
679 }
680#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700681
682 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683}
684
685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800686static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
687 const u8 *addr)
688{
689 struct wpa_ssid *ssid, *s;
690 u8 *n;
691 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700692 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800693
694 ssid = wpa_s->current_ssid;
695 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
696 !ssid->p2p_persistent_group)
697 return;
698
699 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
700 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
701 continue;
702
703 if (s->ssid_len == ssid->ssid_len &&
704 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
705 break;
706 }
707
708 if (s == NULL)
709 return;
710
711 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
712 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700713 ETH_ALEN) != 0)
714 continue;
715
716 if (i == s->num_p2p_clients - 1)
717 return; /* already the most recent entry */
718
719 /* move the entry to mark it most recent */
720 os_memmove(s->p2p_client_list + i * ETH_ALEN,
721 s->p2p_client_list + (i + 1) * ETH_ALEN,
722 (s->num_p2p_clients - i - 1) * ETH_ALEN);
723 os_memcpy(s->p2p_client_list +
724 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
725 found = 1;
726 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800727 }
728
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700729 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
730 n = os_realloc_array(s->p2p_client_list,
731 s->num_p2p_clients + 1, ETH_ALEN);
732 if (n == NULL)
733 return;
734 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
735 s->p2p_client_list = n;
736 s->num_p2p_clients++;
737 } else if (!found) {
738 /* Not enough room for an additional entry - drop the oldest
739 * entry */
740 os_memmove(s->p2p_client_list,
741 s->p2p_client_list + ETH_ALEN,
742 (s->num_p2p_clients - 1) * ETH_ALEN);
743 os_memcpy(s->p2p_client_list +
744 (s->num_p2p_clients - 1) * ETH_ALEN,
745 addr, ETH_ALEN);
746 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800747
748#ifndef CONFIG_NO_CONFIG_WRITE
749 if (wpa_s->parent->conf->update_config &&
750 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
751 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
752#endif /* CONFIG_NO_CONFIG_WRITE */
753}
754
755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
757 int success)
758{
759 struct wpa_ssid *ssid;
760 const char *ssid_txt;
761 int client;
762 int persistent;
763 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700764 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765
766 /*
767 * This callback is likely called for the main interface. Update wpa_s
768 * to use the group interface if a new interface was created for the
769 * group.
770 */
771 if (wpa_s->global->p2p_group_formation)
772 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700773 if (wpa_s->p2p_go_group_formation_completed) {
774 wpa_s->global->p2p_group_formation = NULL;
775 wpa_s->p2p_in_provisioning = 0;
776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777
778 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700779 wpa_msg_global(wpa_s->parent, MSG_INFO,
780 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700781 wpas_p2p_group_delete(wpa_s,
782 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783 return;
784 }
785
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700786 wpa_msg_global(wpa_s->parent, MSG_INFO,
787 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788
789 ssid = wpa_s->current_ssid;
790 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
791 ssid->mode = WPAS_MODE_P2P_GO;
792 p2p_group_notif_formation_done(wpa_s->p2p_group);
793 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
794 }
795
796 persistent = 0;
797 if (ssid) {
798 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
799 client = ssid->mode == WPAS_MODE_INFRA;
800 if (ssid->mode == WPAS_MODE_P2P_GO) {
801 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700802 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
803 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804 } else
805 persistent = wpas_p2p_persistent_group(wpa_s,
806 go_dev_addr,
807 ssid->ssid,
808 ssid->ssid_len);
809 } else {
810 ssid_txt = "";
811 client = wpa_s->p2p_group_interface ==
812 P2P_GROUP_INTERFACE_CLIENT;
813 os_memset(go_dev_addr, 0, ETH_ALEN);
814 }
815
816 wpa_s->show_group_started = 0;
817 if (client) {
818 /*
819 * Indicate event only after successfully completed 4-way
820 * handshake, i.e., when the interface is ready for data
821 * packets.
822 */
823 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700824#ifdef ANDROID_P2P
825 /* For client Second phase of Group formation (4-way handshake) can be still pending
826 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700827 wpa_printf(MSG_INFO, "Restoring back wpa_s->global->p2p_group_formation to wpa_s %p\n", wpa_s);
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700828 wpa_s->global->p2p_group_formation = wpa_s;
829#endif
830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
832 char psk[65];
833 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700834 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
835 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
836 MACSTR "%s",
837 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
838 MAC2STR(go_dev_addr),
839 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 wpas_p2p_cross_connect_setup(wpa_s);
841 wpas_p2p_set_group_idle_timeout(wpa_s);
842 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700843 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
844 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
845 "go_dev_addr=" MACSTR "%s",
846 wpa_s->ifname, ssid_txt,
847 ssid ? ssid->frequency : 0,
848 ssid && ssid->passphrase ? ssid->passphrase : "",
849 MAC2STR(go_dev_addr),
850 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 wpas_p2p_cross_connect_setup(wpa_s);
852 wpas_p2p_set_group_idle_timeout(wpa_s);
853 }
854
855 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700856 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
857 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700858 else {
859 os_free(wpa_s->global->add_psk);
860 wpa_s->global->add_psk = NULL;
861 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700863 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700864 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700865 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700866 os_get_time(&wpa_s->global->p2p_go_wait_client);
867 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868}
869
870
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800871static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
872 unsigned int freq,
873 const u8 *dst, const u8 *src,
874 const u8 *bssid,
875 const u8 *data, size_t data_len,
876 enum offchannel_send_action_result
877 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800879 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
882 return;
883 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
884 return;
885
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800886 switch (result) {
887 case OFFCHANNEL_SEND_ACTION_SUCCESS:
888 res = P2P_SEND_ACTION_SUCCESS;
889 break;
890 case OFFCHANNEL_SEND_ACTION_NO_ACK:
891 res = P2P_SEND_ACTION_NO_ACK;
892 break;
893 case OFFCHANNEL_SEND_ACTION_FAILED:
894 res = P2P_SEND_ACTION_FAILED;
895 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 }
897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800898 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800900 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
901 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800902 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800903 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
904 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800906 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
907 "during p2p_connect-auto");
908 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
909 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 }
911}
912
913
914static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
915 const u8 *src, const u8 *bssid, const u8 *buf,
916 size_t len, unsigned int wait_time)
917{
918 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800919 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
920 wait_time,
921 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922}
923
924
925static void wpas_send_action_done(void *ctx)
926{
927 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800928 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929}
930
931
932static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
933 struct p2p_go_neg_results *params)
934{
935 if (wpa_s->go_params == NULL) {
936 wpa_s->go_params = os_malloc(sizeof(*params));
937 if (wpa_s->go_params == NULL)
938 return -1;
939 }
940 os_memcpy(wpa_s->go_params, params, sizeof(*params));
941 return 0;
942}
943
944
945static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
946 struct p2p_go_neg_results *res)
947{
948 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
949 MAC2STR(res->peer_interface_addr));
950 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
951 res->ssid, res->ssid_len);
952 wpa_supplicant_ap_deinit(wpa_s);
953 wpas_copy_go_neg_results(wpa_s, res);
954 if (res->wps_method == WPS_PBC)
955 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
956 else {
957 u16 dev_pw_id = DEV_PW_DEFAULT;
958 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
959 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
960 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
961 wpa_s->p2p_pin, 1, dev_pw_id);
962 }
963}
964
965
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700966static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
967 struct wpa_ssid *ssid)
968{
969 struct wpa_ssid *persistent;
970 struct psk_list_entry *psk;
971 struct hostapd_data *hapd;
972
973 if (!wpa_s->ap_iface)
974 return;
975
976 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
977 ssid->ssid_len);
978 if (persistent == NULL)
979 return;
980
981 hapd = wpa_s->ap_iface->bss[0];
982
983 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
984 list) {
985 struct hostapd_wpa_psk *hpsk;
986
987 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
988 MACSTR " psk=%d",
989 MAC2STR(psk->addr), psk->p2p);
990 hpsk = os_zalloc(sizeof(*hpsk));
991 if (hpsk == NULL)
992 break;
993 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
994 if (psk->p2p)
995 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
996 else
997 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
998 hpsk->next = hapd->conf->ssid.wpa_psk;
999 hapd->conf->ssid.wpa_psk = hpsk;
1000 }
1001}
1002
1003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004static void p2p_go_configured(void *ctx, void *data)
1005{
1006 struct wpa_supplicant *wpa_s = ctx;
1007 struct p2p_go_neg_results *params = data;
1008 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001009 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010
1011 ssid = wpa_s->current_ssid;
1012 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1013 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1014 if (wpa_s->global->p2p_group_formation == wpa_s)
1015 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001016 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001017 wpa_msg_global(wpa_s->parent, MSG_INFO,
1018 P2P_EVENT_GROUP_STARTED
1019 "%s GO ssid=\"%s\" freq=%d "
1020 "passphrase=\"%s\" go_dev_addr=" MACSTR
1021 "%s", wpa_s->ifname,
1022 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1023 ssid->frequency, params->passphrase,
1024 MAC2STR(wpa_s->global->p2p_dev_addr),
1025 params->persistent_group ?
1026 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001027 } else {
1028 char psk[65];
1029 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1030 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001031 wpa_msg_global(wpa_s->parent, MSG_INFO,
1032 P2P_EVENT_GROUP_STARTED
1033 "%s GO ssid=\"%s\" freq=%d psk=%s "
1034 "go_dev_addr=" MACSTR "%s",
1035 wpa_s->ifname,
1036 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1037 ssid->frequency, psk,
1038 MAC2STR(wpa_s->global->p2p_dev_addr),
1039 params->persistent_group ?
1040 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001041 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001042
Dmitry Shmidt56052862013-10-04 10:23:25 -07001043 os_get_time(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001044 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001045 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001047 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001048 wpas_p2p_add_psk_list(wpa_s, ssid);
1049 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001050 if (network_id < 0)
1051 network_id = ssid->id;
1052 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 wpas_p2p_cross_connect_setup(wpa_s);
1054 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001055
1056 if (wpa_s->p2p_first_connection_timeout) {
1057 wpa_dbg(wpa_s, MSG_DEBUG,
1058 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1059 wpa_s->p2p_first_connection_timeout);
1060 wpa_s->p2p_go_group_formation_completed = 0;
1061 wpa_s->global->p2p_group_formation = wpa_s;
1062 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1063 wpa_s->parent, NULL);
1064 eloop_register_timeout(
1065 wpa_s->p2p_first_connection_timeout, 0,
1066 wpas_p2p_group_formation_timeout,
1067 wpa_s->parent, NULL);
1068 }
1069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 return;
1071 }
1072
1073 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1074 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1075 params->peer_interface_addr)) {
1076 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1077 "filtering");
1078 return;
1079 }
1080 if (params->wps_method == WPS_PBC)
1081 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001082 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 else if (wpa_s->p2p_pin[0])
1084 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001085 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 os_free(wpa_s->go_params);
1087 wpa_s->go_params = NULL;
1088}
1089
1090
1091static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1092 struct p2p_go_neg_results *params,
1093 int group_formation)
1094{
1095 struct wpa_ssid *ssid;
1096
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001097 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1098 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1099 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1100 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001102 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103
1104 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001105 if (ssid == NULL) {
1106 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001110 wpa_s->show_group_started = 0;
1111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 wpa_config_set_network_defaults(ssid);
1113 ssid->temporary = 1;
1114 ssid->p2p_group = 1;
1115 ssid->p2p_persistent_group = params->persistent_group;
1116 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1117 WPAS_MODE_P2P_GO;
1118 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001119 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001120 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121 ssid->ssid = os_zalloc(params->ssid_len + 1);
1122 if (ssid->ssid) {
1123 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1124 ssid->ssid_len = params->ssid_len;
1125 }
1126 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1127 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1128 ssid->proto = WPA_PROTO_RSN;
1129 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001130 if (os_strlen(params->passphrase) > 0) {
1131 ssid->passphrase = os_strdup(params->passphrase);
1132 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001133 wpa_msg_global(wpa_s, MSG_ERROR,
1134 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001135 wpa_config_remove_network(wpa_s->conf, ssid->id);
1136 return;
1137 }
1138 } else
1139 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001140 ssid->psk_set = params->psk_set;
1141 if (ssid->psk_set)
1142 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001143 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001144 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001145 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146
1147 wpa_s->ap_configured_cb = p2p_go_configured;
1148 wpa_s->ap_configured_cb_ctx = wpa_s;
1149 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001150 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 wpa_s->reassociate = 1;
1152 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001153 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1154 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 wpa_supplicant_req_scan(wpa_s, 0, 0);
1156}
1157
1158
1159static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1160 const struct wpa_supplicant *src)
1161{
1162 struct wpa_config *d;
1163 const struct wpa_config *s;
1164
1165 d = dst->conf;
1166 s = src->conf;
1167
1168#define C(n) if (s->n) d->n = os_strdup(s->n)
1169 C(device_name);
1170 C(manufacturer);
1171 C(model_name);
1172 C(model_number);
1173 C(serial_number);
1174 C(config_methods);
1175#undef C
1176
1177 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1178 os_memcpy(d->sec_device_type, s->sec_device_type,
1179 sizeof(d->sec_device_type));
1180 d->num_sec_device_types = s->num_sec_device_types;
1181
1182 d->p2p_group_idle = s->p2p_group_idle;
1183 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001184 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001185 d->max_num_sta = s->max_num_sta;
1186 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001187 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001188 d->beacon_int = s->beacon_int;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001189 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001190 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191}
1192
1193
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001194static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1195 char *ifname, size_t len)
1196{
1197 char *ifname_ptr = wpa_s->ifname;
1198
1199 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1200 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1201 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1202 }
1203
1204 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1205 if (os_strlen(ifname) >= IFNAMSIZ &&
1206 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1207 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001208 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001209 }
1210}
1211
1212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1214 enum wpa_driver_if_type type)
1215{
1216 char ifname[120], force_ifname[120];
1217
1218 if (wpa_s->pending_interface_name[0]) {
1219 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1220 "- skip creation of a new one");
1221 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1222 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1223 "unknown?! ifname='%s'",
1224 wpa_s->pending_interface_name);
1225 return -1;
1226 }
1227 return 0;
1228 }
1229
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001230 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 force_ifname[0] = '\0';
1232
1233 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1234 ifname);
1235 wpa_s->p2p_group_idx++;
1236
1237 wpa_s->pending_interface_type = type;
1238 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1239 wpa_s->pending_interface_addr, NULL) < 0) {
1240 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1241 "interface");
1242 return -1;
1243 }
1244
1245 if (force_ifname[0]) {
1246 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1247 force_ifname);
1248 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1249 sizeof(wpa_s->pending_interface_name));
1250 } else
1251 os_strlcpy(wpa_s->pending_interface_name, ifname,
1252 sizeof(wpa_s->pending_interface_name));
1253 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1254 MACSTR, wpa_s->pending_interface_name,
1255 MAC2STR(wpa_s->pending_interface_addr));
1256
1257 return 0;
1258}
1259
1260
1261static void wpas_p2p_remove_pending_group_interface(
1262 struct wpa_supplicant *wpa_s)
1263{
1264 if (!wpa_s->pending_interface_name[0] ||
1265 is_zero_ether_addr(wpa_s->pending_interface_addr))
1266 return; /* No pending virtual interface */
1267
1268 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1269 wpa_s->pending_interface_name);
1270 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1271 wpa_s->pending_interface_name);
1272 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1273 wpa_s->pending_interface_name[0] = '\0';
1274}
1275
1276
1277static struct wpa_supplicant *
1278wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1279{
1280 struct wpa_interface iface;
1281 struct wpa_supplicant *group_wpa_s;
1282
1283 if (!wpa_s->pending_interface_name[0]) {
1284 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1285 if (!wpas_p2p_create_iface(wpa_s))
1286 return NULL;
1287 /*
1288 * Something has forced us to remove the pending interface; try
1289 * to create a new one and hope for the best that we will get
1290 * the same local address.
1291 */
1292 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1293 WPA_IF_P2P_CLIENT) < 0)
1294 return NULL;
1295 }
1296
1297 os_memset(&iface, 0, sizeof(iface));
1298 iface.ifname = wpa_s->pending_interface_name;
1299 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001300 if (wpa_s->conf->ctrl_interface == NULL &&
1301 wpa_s->parent != wpa_s &&
1302 wpa_s->p2p_mgmt &&
1303 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1304 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1305 else
1306 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 iface.driver_param = wpa_s->conf->driver_param;
1308 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1309 if (group_wpa_s == NULL) {
1310 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1311 "wpa_supplicant interface");
1312 return NULL;
1313 }
1314 wpa_s->pending_interface_name[0] = '\0';
1315 group_wpa_s->parent = wpa_s;
1316 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1317 P2P_GROUP_INTERFACE_CLIENT;
1318 wpa_s->global->p2p_group_formation = group_wpa_s;
1319
1320 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1321
1322 return group_wpa_s;
1323}
1324
1325
1326static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1327 void *timeout_ctx)
1328{
1329 struct wpa_supplicant *wpa_s = eloop_ctx;
1330 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001331 wpas_p2p_group_formation_failed(wpa_s);
1332}
1333
1334
1335void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1336{
1337 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1338 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 if (wpa_s->global->p2p)
1340 p2p_group_formation_failed(wpa_s->global->p2p);
1341 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1342 wpa_drv_p2p_group_formation_failed(wpa_s);
1343 wpas_group_formation_completed(wpa_s, 0);
1344}
1345
1346
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001347void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1348{
1349 if (wpa_s->global->p2p_group_formation != wpa_s)
1350 return;
1351 /* Speed up group formation timeout since this cannot succeed */
1352 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1353 wpa_s->parent, NULL);
1354 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1355 wpa_s->parent, NULL);
1356}
1357
1358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1360{
1361 struct wpa_supplicant *wpa_s = ctx;
1362
1363 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1364 wpa_drv_cancel_remain_on_channel(wpa_s);
1365 wpa_s->off_channel_freq = 0;
1366 wpa_s->roc_waiting_drv_freq = 0;
1367 }
1368
1369 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001370 wpa_msg_global(wpa_s, MSG_INFO,
1371 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1372 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001373 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 wpas_p2p_remove_pending_group_interface(wpa_s);
1375 return;
1376 }
1377
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001378 if (wpa_s->p2p_go_ht40)
1379 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001380 if (wpa_s->p2p_go_vht)
1381 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001382
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001383 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1384 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1385 " wps_method=%s",
1386 res->role_go ? "GO" : "client", res->freq, res->ht40,
1387 MAC2STR(res->peer_device_addr),
1388 MAC2STR(res->peer_interface_addr),
1389 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001390 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391
Dmitry Shmidt04949592012-07-19 12:16:46 -07001392 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1393 struct wpa_ssid *ssid;
1394 ssid = wpa_config_get_network(wpa_s->conf,
1395 wpa_s->p2p_persistent_id);
1396 if (ssid && ssid->disabled == 2 &&
1397 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1398 size_t len = os_strlen(ssid->passphrase);
1399 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1400 "on requested persistent group");
1401 os_memcpy(res->passphrase, ssid->passphrase, len);
1402 res->passphrase[len] = '\0';
1403 }
1404 }
1405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406 if (wpa_s->create_p2p_iface) {
1407 struct wpa_supplicant *group_wpa_s =
1408 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1409 if (group_wpa_s == NULL) {
1410 wpas_p2p_remove_pending_group_interface(wpa_s);
1411 return;
1412 }
1413 if (group_wpa_s != wpa_s) {
1414 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1415 sizeof(group_wpa_s->p2p_pin));
1416 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1417 }
1418 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1419 wpa_s->pending_interface_name[0] = '\0';
1420 group_wpa_s->p2p_in_provisioning = 1;
1421
1422 if (res->role_go)
1423 wpas_start_wps_go(group_wpa_s, res, 1);
1424 else
1425 wpas_start_wps_enrollee(group_wpa_s, res);
1426 } else {
1427 wpa_s->p2p_in_provisioning = 1;
1428 wpa_s->global->p2p_group_formation = wpa_s;
1429
1430 if (res->role_go)
1431 wpas_start_wps_go(wpa_s, res, 1);
1432 else
1433 wpas_start_wps_enrollee(ctx, res);
1434 }
1435
1436 wpa_s->p2p_long_listen = 0;
1437 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1438
1439 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1440 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1441 (res->peer_config_timeout % 100) * 10000,
1442 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1443}
1444
1445
1446void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1447{
1448 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001449 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1450 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451
1452 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1453}
1454
1455
1456void wpas_dev_found(void *ctx, const u8 *addr,
1457 const struct p2p_peer_info *info,
1458 int new_device)
1459{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001460#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 struct wpa_supplicant *wpa_s = ctx;
1462 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001463#define WFD_DEV_INFO_SIZE 9
1464 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001465 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001466#ifdef CONFIG_WIFI_DISPLAY
1467 if (info->wfd_subelems) {
1468 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1469 wpabuf_head(info->wfd_subelems),
1470 WFD_DEV_INFO_SIZE);
1471 }
1472#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001473 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1474 " p2p_dev_addr=" MACSTR
1475 " pri_dev_type=%s name='%s' config_methods=0x%x "
1476 "dev_capab=0x%x group_capab=0x%x%s%s",
1477 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1478 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1479 sizeof(devtype)),
1480 info->device_name, info->config_methods,
1481 info->dev_capab, info->group_capab,
1482 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001483#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484
1485 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1486}
1487
1488
1489static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1490{
1491 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001492
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001493 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1494 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001495
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1497}
1498
1499
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001500static void wpas_find_stopped(void *ctx)
1501{
1502 struct wpa_supplicant *wpa_s = ctx;
1503 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1504}
1505
1506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507static int wpas_start_listen(void *ctx, unsigned int freq,
1508 unsigned int duration,
1509 const struct wpabuf *probe_resp_ie)
1510{
1511 struct wpa_supplicant *wpa_s = ctx;
1512
1513 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1514
1515 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1516 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1517 "report received Probe Request frames");
1518 return -1;
1519 }
1520
1521 wpa_s->pending_listen_freq = freq;
1522 wpa_s->pending_listen_duration = duration;
1523
1524 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1525 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1526 "to remain on channel (%u MHz) for Listen "
1527 "state", freq);
1528 wpa_s->pending_listen_freq = 0;
1529 return -1;
1530 }
1531 wpa_s->off_channel_freq = 0;
1532 wpa_s->roc_waiting_drv_freq = freq;
1533
1534 return 0;
1535}
1536
1537
1538static void wpas_stop_listen(void *ctx)
1539{
1540 struct wpa_supplicant *wpa_s = ctx;
1541 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1542 wpa_drv_cancel_remain_on_channel(wpa_s);
1543 wpa_s->off_channel_freq = 0;
1544 wpa_s->roc_waiting_drv_freq = 0;
1545 }
1546 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1547 wpa_drv_probe_req_report(wpa_s, 0);
1548}
1549
1550
1551static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1552{
1553 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001554 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555}
1556
1557
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001558/*
1559 * DNS Header section is used only to calculate compression pointers, so the
1560 * contents of this data does not matter, but the length needs to be reserved
1561 * in the virtual packet.
1562 */
1563#define DNS_HEADER_LEN 12
1564
1565/*
1566 * 27-octet in-memory packet from P2P specification containing two implied
1567 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1568 */
1569#define P2P_SD_IN_MEMORY_LEN 27
1570
1571static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1572 u8 **spos, const u8 *end)
1573{
1574 while (*spos < end) {
1575 u8 val = ((*spos)[0] & 0xc0) >> 6;
1576 int len;
1577
1578 if (val == 1 || val == 2) {
1579 /* These are reserved values in RFC 1035 */
1580 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1581 "sequence starting with 0x%x", val);
1582 return -1;
1583 }
1584
1585 if (val == 3) {
1586 u16 offset;
1587 u8 *spos_tmp;
1588
1589 /* Offset */
1590 if (*spos + 2 > end) {
1591 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1592 "DNS offset field");
1593 return -1;
1594 }
1595
1596 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1597 if (offset >= *spos - start) {
1598 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1599 "pointer offset %u", offset);
1600 return -1;
1601 }
1602
1603 (*spos) += 2;
1604 spos_tmp = start + offset;
1605 return p2p_sd_dns_uncompress_label(upos, uend, start,
1606 &spos_tmp,
1607 *spos - 2);
1608 }
1609
1610 /* Label */
1611 len = (*spos)[0] & 0x3f;
1612 if (len == 0)
1613 return 0;
1614
1615 (*spos)++;
1616 if (*spos + len > end) {
1617 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1618 "sequence - no room for label with length "
1619 "%u", len);
1620 return -1;
1621 }
1622
1623 if (*upos + len + 2 > uend)
1624 return -2;
1625
1626 os_memcpy(*upos, *spos, len);
1627 *spos += len;
1628 *upos += len;
1629 (*upos)[0] = '.';
1630 (*upos)++;
1631 (*upos)[0] = '\0';
1632 }
1633
1634 return 0;
1635}
1636
1637
1638/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1639 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1640 * not large enough */
1641static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1642 size_t msg_len, size_t offset)
1643{
1644 /* 27-octet in-memory packet from P2P specification */
1645 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1646 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1647 u8 *tmp, *end, *spos;
1648 char *upos, *uend;
1649 int ret = 0;
1650
1651 if (buf_len < 2)
1652 return -1;
1653 if (offset > msg_len)
1654 return -1;
1655
1656 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1657 if (tmp == NULL)
1658 return -1;
1659 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1660 end = spos + msg_len;
1661 spos += offset;
1662
1663 os_memset(tmp, 0, DNS_HEADER_LEN);
1664 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1665 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1666
1667 upos = buf;
1668 uend = buf + buf_len;
1669
1670 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1671 if (ret) {
1672 os_free(tmp);
1673 return ret;
1674 }
1675
1676 if (upos == buf) {
1677 upos[0] = '.';
1678 upos[1] = '\0';
1679 } else if (upos[-1] == '.')
1680 upos[-1] = '\0';
1681
1682 os_free(tmp);
1683 return 0;
1684}
1685
1686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687static struct p2p_srv_bonjour *
1688wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1689 const struct wpabuf *query)
1690{
1691 struct p2p_srv_bonjour *bsrv;
1692 size_t len;
1693
1694 len = wpabuf_len(query);
1695 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1696 struct p2p_srv_bonjour, list) {
1697 if (len == wpabuf_len(bsrv->query) &&
1698 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1699 len) == 0)
1700 return bsrv;
1701 }
1702 return NULL;
1703}
1704
1705
1706static struct p2p_srv_upnp *
1707wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1708 const char *service)
1709{
1710 struct p2p_srv_upnp *usrv;
1711
1712 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1713 struct p2p_srv_upnp, list) {
1714 if (version == usrv->version &&
1715 os_strcmp(service, usrv->service) == 0)
1716 return usrv;
1717 }
1718 return NULL;
1719}
1720
1721
1722static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1723 u8 srv_trans_id)
1724{
1725 u8 *len_pos;
1726
1727 if (wpabuf_tailroom(resp) < 5)
1728 return;
1729
1730 /* Length (to be filled) */
1731 len_pos = wpabuf_put(resp, 2);
1732 wpabuf_put_u8(resp, srv_proto);
1733 wpabuf_put_u8(resp, srv_trans_id);
1734 /* Status Code */
1735 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1736 /* Response Data: empty */
1737 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1738}
1739
1740
1741static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1742 struct wpabuf *resp, u8 srv_trans_id)
1743{
1744 struct p2p_srv_bonjour *bsrv;
1745 u8 *len_pos;
1746
1747 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1748
1749 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1750 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1751 return;
1752 }
1753
1754 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1755 struct p2p_srv_bonjour, list) {
1756 if (wpabuf_tailroom(resp) <
1757 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1758 return;
1759 /* Length (to be filled) */
1760 len_pos = wpabuf_put(resp, 2);
1761 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1762 wpabuf_put_u8(resp, srv_trans_id);
1763 /* Status Code */
1764 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1765 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1766 wpabuf_head(bsrv->resp),
1767 wpabuf_len(bsrv->resp));
1768 /* Response Data */
1769 wpabuf_put_buf(resp, bsrv->query); /* Key */
1770 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1771 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1772 2);
1773 }
1774}
1775
1776
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001777static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1778 size_t query_len)
1779{
1780 char str_rx[256], str_srv[256];
1781
1782 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1783 return 0; /* Too short to include DNS Type and Version */
1784 if (os_memcmp(query + query_len - 3,
1785 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1786 3) != 0)
1787 return 0; /* Mismatch in DNS Type or Version */
1788 if (query_len == wpabuf_len(bsrv->query) &&
1789 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1790 return 1; /* Binary match */
1791
1792 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1793 0))
1794 return 0; /* Failed to uncompress query */
1795 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1796 wpabuf_head(bsrv->query),
1797 wpabuf_len(bsrv->query) - 3, 0))
1798 return 0; /* Failed to uncompress service */
1799
1800 return os_strcmp(str_rx, str_srv) == 0;
1801}
1802
1803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1805 struct wpabuf *resp, u8 srv_trans_id,
1806 const u8 *query, size_t query_len)
1807{
1808 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001810 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811
1812 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1813 query, query_len);
1814 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1815 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1816 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1817 srv_trans_id);
1818 return;
1819 }
1820
1821 if (query_len == 0) {
1822 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1823 return;
1824 }
1825
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001826 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1827 struct p2p_srv_bonjour, list) {
1828 if (!match_bonjour_query(bsrv, query, query_len))
1829 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001831 if (wpabuf_tailroom(resp) <
1832 5 + query_len + wpabuf_len(bsrv->resp))
1833 return;
1834
1835 matches++;
1836
1837 /* Length (to be filled) */
1838 len_pos = wpabuf_put(resp, 2);
1839 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1840 wpabuf_put_u8(resp, srv_trans_id);
1841
1842 /* Status Code */
1843 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1844 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1845 wpabuf_head(bsrv->resp),
1846 wpabuf_len(bsrv->resp));
1847
1848 /* Response Data */
1849 wpabuf_put_data(resp, query, query_len); /* Key */
1850 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1851
1852 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1853 }
1854
1855 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1857 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001858 if (wpabuf_tailroom(resp) < 5)
1859 return;
1860
1861 /* Length (to be filled) */
1862 len_pos = wpabuf_put(resp, 2);
1863 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1864 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865
1866 /* Status Code */
1867 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1868 /* Response Data: empty */
1869 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1870 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872}
1873
1874
1875static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1876 struct wpabuf *resp, u8 srv_trans_id)
1877{
1878 struct p2p_srv_upnp *usrv;
1879 u8 *len_pos;
1880
1881 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1882
1883 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1884 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1885 return;
1886 }
1887
1888 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1889 struct p2p_srv_upnp, list) {
1890 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1891 return;
1892
1893 /* Length (to be filled) */
1894 len_pos = wpabuf_put(resp, 2);
1895 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1896 wpabuf_put_u8(resp, srv_trans_id);
1897
1898 /* Status Code */
1899 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1900 /* Response Data */
1901 wpabuf_put_u8(resp, usrv->version);
1902 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1903 usrv->service);
1904 wpabuf_put_str(resp, usrv->service);
1905 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1906 2);
1907 }
1908}
1909
1910
1911static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1912 struct wpabuf *resp, u8 srv_trans_id,
1913 const u8 *query, size_t query_len)
1914{
1915 struct p2p_srv_upnp *usrv;
1916 u8 *len_pos;
1917 u8 version;
1918 char *str;
1919 int count = 0;
1920
1921 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1922 query, query_len);
1923
1924 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1925 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1926 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1927 srv_trans_id);
1928 return;
1929 }
1930
1931 if (query_len == 0) {
1932 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1933 return;
1934 }
1935
1936 if (wpabuf_tailroom(resp) < 5)
1937 return;
1938
1939 /* Length (to be filled) */
1940 len_pos = wpabuf_put(resp, 2);
1941 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1942 wpabuf_put_u8(resp, srv_trans_id);
1943
1944 version = query[0];
1945 str = os_malloc(query_len);
1946 if (str == NULL)
1947 return;
1948 os_memcpy(str, query + 1, query_len - 1);
1949 str[query_len - 1] = '\0';
1950
1951 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1952 struct p2p_srv_upnp, list) {
1953 if (version != usrv->version)
1954 continue;
1955
1956 if (os_strcmp(str, "ssdp:all") != 0 &&
1957 os_strstr(usrv->service, str) == NULL)
1958 continue;
1959
1960 if (wpabuf_tailroom(resp) < 2)
1961 break;
1962 if (count == 0) {
1963 /* Status Code */
1964 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1965 /* Response Data */
1966 wpabuf_put_u8(resp, version);
1967 } else
1968 wpabuf_put_u8(resp, ',');
1969
1970 count++;
1971
1972 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1973 usrv->service);
1974 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1975 break;
1976 wpabuf_put_str(resp, usrv->service);
1977 }
1978 os_free(str);
1979
1980 if (count == 0) {
1981 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1982 "available");
1983 /* Status Code */
1984 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1985 /* Response Data: empty */
1986 }
1987
1988 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1989}
1990
1991
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001992#ifdef CONFIG_WIFI_DISPLAY
1993static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1994 struct wpabuf *resp, u8 srv_trans_id,
1995 const u8 *query, size_t query_len)
1996{
1997 const u8 *pos;
1998 u8 role;
1999 u8 *len_pos;
2000
2001 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2002
2003 if (!wpa_s->global->wifi_display) {
2004 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2005 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2006 srv_trans_id);
2007 return;
2008 }
2009
2010 if (query_len < 1) {
2011 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2012 "Role");
2013 return;
2014 }
2015
2016 if (wpabuf_tailroom(resp) < 5)
2017 return;
2018
2019 pos = query;
2020 role = *pos++;
2021 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2022
2023 /* TODO: role specific handling */
2024
2025 /* Length (to be filled) */
2026 len_pos = wpabuf_put(resp, 2);
2027 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2028 wpabuf_put_u8(resp, srv_trans_id);
2029 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2030
2031 while (pos < query + query_len) {
2032 if (*pos < MAX_WFD_SUBELEMS &&
2033 wpa_s->global->wfd_subelem[*pos] &&
2034 wpabuf_tailroom(resp) >=
2035 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2036 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2037 "subelement %u", *pos);
2038 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2039 }
2040 pos++;
2041 }
2042
2043 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2044}
2045#endif /* CONFIG_WIFI_DISPLAY */
2046
2047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2049 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2050{
2051 struct wpa_supplicant *wpa_s = ctx;
2052 const u8 *pos = tlvs;
2053 const u8 *end = tlvs + tlvs_len;
2054 const u8 *tlv_end;
2055 u16 slen;
2056 struct wpabuf *resp;
2057 u8 srv_proto, srv_trans_id;
2058 size_t buf_len;
2059 char *buf;
2060
2061 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2062 tlvs, tlvs_len);
2063 buf_len = 2 * tlvs_len + 1;
2064 buf = os_malloc(buf_len);
2065 if (buf) {
2066 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2067 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2068 MACSTR " %u %u %s",
2069 freq, MAC2STR(sa), dialog_token, update_indic,
2070 buf);
2071 os_free(buf);
2072 }
2073
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002074 if (wpa_s->p2p_sd_over_ctrl_iface) {
2075 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2076 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002078 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079
2080 resp = wpabuf_alloc(10000);
2081 if (resp == NULL)
2082 return;
2083
2084 while (pos + 1 < end) {
2085 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2086 slen = WPA_GET_LE16(pos);
2087 pos += 2;
2088 if (pos + slen > end || slen < 2) {
2089 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2090 "length");
2091 wpabuf_free(resp);
2092 return;
2093 }
2094 tlv_end = pos + slen;
2095
2096 srv_proto = *pos++;
2097 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2098 srv_proto);
2099 srv_trans_id = *pos++;
2100 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2101 srv_trans_id);
2102
2103 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2104 pos, tlv_end - pos);
2105
2106
2107 if (wpa_s->force_long_sd) {
2108 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2109 "response");
2110 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2111 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2112 goto done;
2113 }
2114
2115 switch (srv_proto) {
2116 case P2P_SERV_ALL_SERVICES:
2117 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2118 "for all services");
2119 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2120 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2121 wpa_printf(MSG_DEBUG, "P2P: No service "
2122 "discovery protocols available");
2123 wpas_sd_add_proto_not_avail(
2124 resp, P2P_SERV_ALL_SERVICES,
2125 srv_trans_id);
2126 break;
2127 }
2128 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2129 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2130 break;
2131 case P2P_SERV_BONJOUR:
2132 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2133 pos, tlv_end - pos);
2134 break;
2135 case P2P_SERV_UPNP:
2136 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2137 pos, tlv_end - pos);
2138 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002139#ifdef CONFIG_WIFI_DISPLAY
2140 case P2P_SERV_WIFI_DISPLAY:
2141 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2142 pos, tlv_end - pos);
2143 break;
2144#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 default:
2146 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2147 "protocol %u", srv_proto);
2148 wpas_sd_add_proto_not_avail(resp, srv_proto,
2149 srv_trans_id);
2150 break;
2151 }
2152
2153 pos = tlv_end;
2154 }
2155
2156done:
2157 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2158 update_indic, tlvs, tlvs_len);
2159
2160 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2161
2162 wpabuf_free(resp);
2163}
2164
2165
2166void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2167 const u8 *tlvs, size_t tlvs_len)
2168{
2169 struct wpa_supplicant *wpa_s = ctx;
2170 const u8 *pos = tlvs;
2171 const u8 *end = tlvs + tlvs_len;
2172 const u8 *tlv_end;
2173 u16 slen;
2174 size_t buf_len;
2175 char *buf;
2176
2177 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2178 tlvs, tlvs_len);
2179 if (tlvs_len > 1500) {
2180 /* TODO: better way for handling this */
2181 wpa_msg_ctrl(wpa_s, MSG_INFO,
2182 P2P_EVENT_SERV_DISC_RESP MACSTR
2183 " %u <long response: %u bytes>",
2184 MAC2STR(sa), update_indic,
2185 (unsigned int) tlvs_len);
2186 } else {
2187 buf_len = 2 * tlvs_len + 1;
2188 buf = os_malloc(buf_len);
2189 if (buf) {
2190 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2191 wpa_msg_ctrl(wpa_s, MSG_INFO,
2192 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2193 MAC2STR(sa), update_indic, buf);
2194 os_free(buf);
2195 }
2196 }
2197
2198 while (pos < end) {
2199 u8 srv_proto, srv_trans_id, status;
2200
2201 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2202 slen = WPA_GET_LE16(pos);
2203 pos += 2;
2204 if (pos + slen > end || slen < 3) {
2205 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2206 "length");
2207 return;
2208 }
2209 tlv_end = pos + slen;
2210
2211 srv_proto = *pos++;
2212 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2213 srv_proto);
2214 srv_trans_id = *pos++;
2215 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2216 srv_trans_id);
2217 status = *pos++;
2218 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2219 status);
2220
2221 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2222 pos, tlv_end - pos);
2223
2224 pos = tlv_end;
2225 }
2226
2227 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2228}
2229
2230
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002231u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2232 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233{
2234 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002235 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002237 return 0;
2238 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239}
2240
2241
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002242u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2243 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002244{
2245 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002246 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247
2248 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2249 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002250 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2252 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2253 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2254 wpabuf_put_u8(tlvs, version);
2255 wpabuf_put_str(tlvs, query);
2256 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2257 wpabuf_free(tlvs);
2258 return ret;
2259}
2260
2261
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002262#ifdef CONFIG_WIFI_DISPLAY
2263
2264static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2265 const struct wpabuf *tlvs)
2266{
2267 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2268 return 0;
2269 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2270 return 0;
2271 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2272}
2273
2274
2275#define MAX_WFD_SD_SUBELEMS 20
2276
2277static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2278 const char *subelems)
2279{
2280 u8 *len;
2281 const char *pos;
2282 int val;
2283 int count = 0;
2284
2285 len = wpabuf_put(tlvs, 2);
2286 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2287 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2288
2289 wpabuf_put_u8(tlvs, role);
2290
2291 pos = subelems;
2292 while (*pos) {
2293 val = atoi(pos);
2294 if (val >= 0 && val < 256) {
2295 wpabuf_put_u8(tlvs, val);
2296 count++;
2297 if (count == MAX_WFD_SD_SUBELEMS)
2298 break;
2299 }
2300 pos = os_strchr(pos + 1, ',');
2301 if (pos == NULL)
2302 break;
2303 pos++;
2304 }
2305
2306 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2307}
2308
2309
2310u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2311 const u8 *dst, const char *role)
2312{
2313 struct wpabuf *tlvs;
2314 u64 ret;
2315 const char *subelems;
2316 u8 id = 1;
2317
2318 subelems = os_strchr(role, ' ');
2319 if (subelems == NULL)
2320 return 0;
2321 subelems++;
2322
2323 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2324 if (tlvs == NULL)
2325 return 0;
2326
2327 if (os_strstr(role, "[source]"))
2328 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2329 if (os_strstr(role, "[pri-sink]"))
2330 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2331 if (os_strstr(role, "[sec-sink]"))
2332 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2333 if (os_strstr(role, "[source+sink]"))
2334 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2335
2336 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2337 wpabuf_free(tlvs);
2338 return ret;
2339}
2340
2341#endif /* CONFIG_WIFI_DISPLAY */
2342
2343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002344int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345{
2346 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002347 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2349 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002350 return p2p_sd_cancel_request(wpa_s->global->p2p,
2351 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352}
2353
2354
2355void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2356 const u8 *dst, u8 dialog_token,
2357 const struct wpabuf *resp_tlvs)
2358{
2359 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2360 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2361 resp_tlvs);
2362 return;
2363 }
2364 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2365 return;
2366 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2367 resp_tlvs);
2368}
2369
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002371void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2372{
2373 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2374 wpa_drv_p2p_service_update(wpa_s);
2375 return;
2376 }
2377 if (wpa_s->global->p2p)
2378 p2p_sd_service_update(wpa_s->global->p2p);
2379}
2380
2381
2382static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2383{
2384 dl_list_del(&bsrv->list);
2385 wpabuf_free(bsrv->query);
2386 wpabuf_free(bsrv->resp);
2387 os_free(bsrv);
2388}
2389
2390
2391static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2392{
2393 dl_list_del(&usrv->list);
2394 os_free(usrv->service);
2395 os_free(usrv);
2396}
2397
2398
2399void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2400{
2401 struct p2p_srv_bonjour *bsrv, *bn;
2402 struct p2p_srv_upnp *usrv, *un;
2403
2404 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2405 struct p2p_srv_bonjour, list)
2406 wpas_p2p_srv_bonjour_free(bsrv);
2407
2408 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2409 struct p2p_srv_upnp, list)
2410 wpas_p2p_srv_upnp_free(usrv);
2411
2412 wpas_p2p_sd_service_update(wpa_s);
2413}
2414
2415
2416int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2417 struct wpabuf *query, struct wpabuf *resp)
2418{
2419 struct p2p_srv_bonjour *bsrv;
2420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002421 bsrv = os_zalloc(sizeof(*bsrv));
2422 if (bsrv == NULL)
2423 return -1;
2424 bsrv->query = query;
2425 bsrv->resp = resp;
2426 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2427
2428 wpas_p2p_sd_service_update(wpa_s);
2429 return 0;
2430}
2431
2432
2433int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2434 const struct wpabuf *query)
2435{
2436 struct p2p_srv_bonjour *bsrv;
2437
2438 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2439 if (bsrv == NULL)
2440 return -1;
2441 wpas_p2p_srv_bonjour_free(bsrv);
2442 wpas_p2p_sd_service_update(wpa_s);
2443 return 0;
2444}
2445
2446
2447int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2448 const char *service)
2449{
2450 struct p2p_srv_upnp *usrv;
2451
2452 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2453 return 0; /* Already listed */
2454 usrv = os_zalloc(sizeof(*usrv));
2455 if (usrv == NULL)
2456 return -1;
2457 usrv->version = version;
2458 usrv->service = os_strdup(service);
2459 if (usrv->service == NULL) {
2460 os_free(usrv);
2461 return -1;
2462 }
2463 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2464
2465 wpas_p2p_sd_service_update(wpa_s);
2466 return 0;
2467}
2468
2469
2470int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2471 const char *service)
2472{
2473 struct p2p_srv_upnp *usrv;
2474
2475 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2476 if (usrv == NULL)
2477 return -1;
2478 wpas_p2p_srv_upnp_free(usrv);
2479 wpas_p2p_sd_service_update(wpa_s);
2480 return 0;
2481}
2482
2483
2484static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2485 const u8 *peer, const char *params,
2486 unsigned int generated_pin)
2487{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002488 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2489 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490}
2491
2492
2493static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2494 const u8 *peer, const char *params)
2495{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002496 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2497 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498}
2499
2500
2501void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2502 const u8 *dev_addr, const u8 *pri_dev_type,
2503 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002504 u8 dev_capab, u8 group_capab, const u8 *group_id,
2505 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506{
2507 struct wpa_supplicant *wpa_s = ctx;
2508 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002509 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 u8 empty_dev_type[8];
2511 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002512 struct wpa_supplicant *group = NULL;
2513
2514 if (group_id) {
2515 for (group = wpa_s->global->ifaces; group; group = group->next)
2516 {
2517 struct wpa_ssid *s = group->current_ssid;
2518 if (s != NULL &&
2519 s->mode == WPAS_MODE_P2P_GO &&
2520 group_id_len - ETH_ALEN == s->ssid_len &&
2521 os_memcmp(group_id + ETH_ALEN, s->ssid,
2522 s->ssid_len) == 0)
2523 break;
2524 }
2525 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002526
2527 if (pri_dev_type == NULL) {
2528 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2529 pri_dev_type = empty_dev_type;
2530 }
2531 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2532 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002533 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 MAC2STR(dev_addr),
2535 wps_dev_type_bin2str(pri_dev_type, devtype,
2536 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002537 dev_name, supp_config_methods, dev_capab, group_capab,
2538 group ? " group=" : "",
2539 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542 if (config_methods & WPS_CONFIG_DISPLAY) {
2543 generated_pin = wps_generate_pin();
2544 wpas_prov_disc_local_display(wpa_s, peer, params,
2545 generated_pin);
2546 } else if (config_methods & WPS_CONFIG_KEYPAD)
2547 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2548 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002549 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2550 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002551
2552 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2553 P2P_PROV_DISC_SUCCESS,
2554 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555}
2556
2557
2558void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2559{
2560 struct wpa_supplicant *wpa_s = ctx;
2561 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002562 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002564 if (wpa_s->pending_pd_before_join &&
2565 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2566 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2567 wpa_s->pending_pd_before_join = 0;
2568 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2569 "join-existing-group operation");
2570 wpas_p2p_join_start(wpa_s);
2571 return;
2572 }
2573
Dmitry Shmidt04949592012-07-19 12:16:46 -07002574 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2575 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2576 os_snprintf(params, sizeof(params), " peer_go=%d",
2577 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2578 else
2579 params[0] = '\0';
2580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002582 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 else if (config_methods & WPS_CONFIG_KEYPAD) {
2584 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002585 wpas_prov_disc_local_display(wpa_s, peer, params,
2586 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002588 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2589 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590
Jouni Malinen75ecf522011-06-27 15:19:46 -07002591 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2592 P2P_PROV_DISC_SUCCESS,
2593 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002594}
2595
2596
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002597static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2598 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002599{
2600 struct wpa_supplicant *wpa_s = ctx;
2601
Dmitry Shmidt04949592012-07-19 12:16:46 -07002602 if (wpa_s->p2p_fallback_to_go_neg) {
2603 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2604 "failed - fall back to GO Negotiation");
2605 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2606 return;
2607 }
2608
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002609 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002610 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002611 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2612 "join-existing-group operation (no ACK for PD "
2613 "Req attempts)");
2614 wpas_p2p_join_start(wpa_s);
2615 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002616 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002617
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002618 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2619 " p2p_dev_addr=" MACSTR " status=%d",
2620 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002621
Jouni Malinen75ecf522011-06-27 15:19:46 -07002622 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2623 status, 0, 0);
2624}
2625
2626
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002627static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2628{
2629 if (channels == NULL)
2630 return 1; /* Assume no restrictions */
2631 return p2p_channels_includes_freq(channels, freq);
2632
2633}
2634
2635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2637 const u8 *go_dev_addr, const u8 *ssid,
2638 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002639 int *force_freq, int persistent_group,
2640 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641{
2642 struct wpa_supplicant *wpa_s = ctx;
2643 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 int res;
2645 struct wpa_supplicant *grp;
2646
2647 if (!persistent_group) {
2648 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2649 " to join an active group", MAC2STR(sa));
2650 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2651 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2652 == 0 ||
2653 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2654 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2655 "authorized invitation");
2656 goto accept_inv;
2657 }
2658 /*
2659 * Do not accept the invitation automatically; notify user and
2660 * request approval.
2661 */
2662 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2663 }
2664
2665 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2666 if (grp) {
2667 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2668 "running persistent group");
2669 if (*go)
2670 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2671 goto accept_inv;
2672 }
2673
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002674 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2675 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2676 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2677 "invitation to re-invoke a persistent group");
2678 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2680
2681 for (s = wpa_s->conf->ssid; s; s = s->next) {
2682 if (s->disabled == 2 &&
2683 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2684 s->ssid_len == ssid_len &&
2685 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2686 break;
2687 }
2688
2689 if (!s) {
2690 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2691 " requested reinvocation of an unknown group",
2692 MAC2STR(sa));
2693 return P2P_SC_FAIL_UNKNOWN_GROUP;
2694 }
2695
2696 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2697 *go = 1;
2698 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2699 wpa_printf(MSG_DEBUG, "P2P: The only available "
2700 "interface is already in use - reject "
2701 "invitation");
2702 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2703 }
2704 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2705 } else if (s->mode == WPAS_MODE_P2P_GO) {
2706 *go = 1;
2707 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2708 {
2709 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2710 "interface address for the group");
2711 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2712 }
2713 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2714 ETH_ALEN);
2715 }
2716
2717accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002718 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2719
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002720 /* Get one of the frequencies currently in use */
2721 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2722 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002723 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002724 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002725 }
2726
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002727 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2728 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002729 if (*go == 0) {
2730 /* We are the client */
2731 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2732 "running a GO but we are capable of MCC, "
2733 "figure out the best channel to use");
2734 *force_freq = 0;
2735 } else if (!freq_included(channels, *force_freq)) {
2736 /* We are the GO, and *force_freq is not in the
2737 * intersection */
2738 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2739 "in intersection but we are capable of MCC, "
2740 "figure out the best channel to use",
2741 *force_freq);
2742 *force_freq = 0;
2743 }
2744 }
2745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 return P2P_SC_SUCCESS;
2747}
2748
2749
2750static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2751 const u8 *ssid, size_t ssid_len,
2752 const u8 *go_dev_addr, u8 status,
2753 int op_freq)
2754{
2755 struct wpa_supplicant *wpa_s = ctx;
2756 struct wpa_ssid *s;
2757
2758 for (s = wpa_s->conf->ssid; s; s = s->next) {
2759 if (s->disabled == 2 &&
2760 s->ssid_len == ssid_len &&
2761 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2762 break;
2763 }
2764
2765 if (status == P2P_SC_SUCCESS) {
2766 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2767 " was accepted; op_freq=%d MHz",
2768 MAC2STR(sa), op_freq);
2769 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002770 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771 wpas_p2p_group_add_persistent(
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002772 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002773 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002775 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002776 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002777 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778 }
2779 return;
2780 }
2781
2782 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2783 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2784 " was rejected (status %u)", MAC2STR(sa), status);
2785 return;
2786 }
2787
2788 if (!s) {
2789 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002790 wpa_msg_global(wpa_s, MSG_INFO,
2791 P2P_EVENT_INVITATION_RECEIVED
2792 "sa=" MACSTR " go_dev_addr=" MACSTR
2793 " bssid=" MACSTR " unknown-network",
2794 MAC2STR(sa), MAC2STR(go_dev_addr),
2795 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002797 wpa_msg_global(wpa_s, MSG_INFO,
2798 P2P_EVENT_INVITATION_RECEIVED
2799 "sa=" MACSTR " go_dev_addr=" MACSTR
2800 " unknown-network",
2801 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002802 }
2803 return;
2804 }
2805
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002806 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002807 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2808 "sa=" MACSTR " persistent=%d freq=%d",
2809 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002810 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002811 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2812 "sa=" MACSTR " persistent=%d",
2813 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002814 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815}
2816
2817
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002818static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2819 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002820 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002821{
2822 size_t i;
2823
2824 if (ssid == NULL)
2825 return;
2826
2827 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2828 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2829 ETH_ALEN) == 0)
2830 break;
2831 }
2832 if (i >= ssid->num_p2p_clients) {
2833 if (ssid->mode != WPAS_MODE_P2P_GO &&
2834 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2835 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2836 "due to invitation result", ssid->id);
2837 wpas_notify_network_removed(wpa_s, ssid);
2838 wpa_config_remove_network(wpa_s->conf, ssid->id);
2839 return;
2840 }
2841 return; /* Peer not found in client list */
2842 }
2843
2844 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002845 "group %d client list%s",
2846 MAC2STR(peer), ssid->id,
2847 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002848 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2849 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2850 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2851 ssid->num_p2p_clients--;
2852#ifndef CONFIG_NO_CONFIG_WRITE
2853 if (wpa_s->parent->conf->update_config &&
2854 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2855 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2856#endif /* CONFIG_NO_CONFIG_WRITE */
2857}
2858
2859
2860static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2861 const u8 *peer)
2862{
2863 struct wpa_ssid *ssid;
2864
2865 wpa_s = wpa_s->global->p2p_invite_group;
2866 if (wpa_s == NULL)
2867 return; /* No known invitation group */
2868 ssid = wpa_s->current_ssid;
2869 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2870 !ssid->p2p_persistent_group)
2871 return; /* Not operating as a GO in persistent group */
2872 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2873 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002874 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002875}
2876
2877
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002878static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002879 const struct p2p_channels *channels,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002880 const u8 *peer, int neg_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881{
2882 struct wpa_supplicant *wpa_s = ctx;
2883 struct wpa_ssid *ssid;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002884 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885
2886 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002887 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2888 "status=%d " MACSTR,
2889 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002890 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002891 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2892 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 }
2894 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2895
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002896 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2897 status, MAC2STR(peer));
2898 if (wpa_s->pending_invite_ssid_id == -1) {
2899 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2900 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002902 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002904 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2905 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2906 "invitation exchange to indicate readiness for "
2907 "re-invocation");
2908 }
2909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002911 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2912 ssid = wpa_config_get_network(
2913 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002914 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002915 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916 wpas_p2p_remove_pending_group_interface(wpa_s);
2917 return;
2918 }
2919
2920 ssid = wpa_config_get_network(wpa_s->conf,
2921 wpa_s->pending_invite_ssid_id);
2922 if (ssid == NULL) {
2923 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2924 "data matching with invitation");
2925 return;
2926 }
2927
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002928 /*
2929 * The peer could have missed our ctrl::ack frame for Invitation
2930 * Response and continue retransmitting the frame. To reduce the
2931 * likelihood of the peer not getting successful TX status for the
2932 * Invitation Response frame, wait a short time here before starting
2933 * the persistent group so that we will remain on the current channel to
2934 * acknowledge any possible retransmission from the peer.
2935 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002936#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002937 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2938 "starting persistent group");
2939 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002940#else
2941 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2942 "starting persistent group");
2943 os_sleep(0, 100000);
2944#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002945
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002946 freq = wpa_s->p2p_persistent_go_freq;
2947 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2948 freq_included(channels, neg_freq)) {
2949 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
2950 neg_freq);
2951 freq = neg_freq;
2952 }
2953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002954 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002955 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002956 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002957 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2958 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002959 ssid->mode == WPAS_MODE_P2P_GO ?
2960 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2961 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962}
2963
2964
Dmitry Shmidt04949592012-07-19 12:16:46 -07002965static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2966 unsigned int freq)
2967{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002968 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002969}
2970
2971
2972static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2973{
2974 reg->channel[reg->channels] = chan;
2975 reg->channels++;
2976}
2977
2978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002980 struct p2p_channels *chan,
2981 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982{
2983 int i, cla = 0;
2984
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002985 os_memset(cli_chan, 0, sizeof(*cli_chan));
2986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002987 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2988 "band");
2989
2990 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2991 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002992 chan->reg_class[cla].channels = 0;
2993 for (i = 0; i < 11; i++) {
2994 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2995 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2996 }
2997 if (chan->reg_class[cla].channels)
2998 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999
3000 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3001 "band");
3002
3003 /* Operating class 115 - 5 GHz, channels 36-48 */
3004 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003005 chan->reg_class[cla].channels = 0;
3006 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3007 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3008 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3009 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3010 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3011 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3012 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3013 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3014 if (chan->reg_class[cla].channels)
3015 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016
3017 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3018 "band");
3019
3020 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3021 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003022 chan->reg_class[cla].channels = 0;
3023 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3024 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3025 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3026 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3027 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3028 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3029 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3030 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3031 if (chan->reg_class[cla].channels)
3032 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003033
3034 chan->reg_classes = cla;
3035 return 0;
3036}
3037
3038
3039static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3040 u16 num_modes,
3041 enum hostapd_hw_mode mode)
3042{
3043 u16 i;
3044
3045 for (i = 0; i < num_modes; i++) {
3046 if (modes[i].mode == mode)
3047 return &modes[i];
3048 }
3049
3050 return NULL;
3051}
3052
3053
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003054enum chan_allowed {
3055 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3056};
3057
Dmitry Shmidt04949592012-07-19 12:16:46 -07003058static int has_channel(struct wpa_global *global,
3059 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060{
3061 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003062 unsigned int freq;
3063
3064 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3065 chan * 5;
3066 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003067 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068
3069 for (i = 0; i < mode->num_channels; i++) {
3070 if (mode->channels[i].chan == chan) {
3071 if (flags)
3072 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003073 if (mode->channels[i].flag &
3074 (HOSTAPD_CHAN_DISABLED |
3075 HOSTAPD_CHAN_RADAR))
3076 return NOT_ALLOWED;
3077 if (mode->channels[i].flag &
3078 (HOSTAPD_CHAN_PASSIVE_SCAN |
3079 HOSTAPD_CHAN_NO_IBSS))
3080 return PASSIVE_ONLY;
3081 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082 }
3083 }
3084
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003085 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086}
3087
3088
3089struct p2p_oper_class_map {
3090 enum hostapd_hw_mode mode;
3091 u8 op_class;
3092 u8 min_chan;
3093 u8 max_chan;
3094 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003095 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003096};
3097
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003098static struct p2p_oper_class_map op_class[] = {
3099 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003100#if 0 /* Do not enable HT40 on 2 GHz for now */
3101 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3102 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3103#endif
3104 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3105 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3106 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3107 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3108 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3109 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003110
3111 /*
3112 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3113 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3114 * 80 MHz, but currently use the following definition for simplicity
3115 * (these center frequencies are not actual channels, which makes
3116 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3117 * removing invalid channels.
3118 */
3119 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003120 { -1, 0, 0, 0, 0, BW20 }
3121};
3122
3123
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003124static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3125 struct hostapd_hw_modes *mode,
3126 u8 channel)
3127{
3128 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3129 unsigned int i;
3130
3131 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3132 return 0;
3133
3134 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3135 /*
3136 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3137 * so the center channel is 6 channels away from the start/end.
3138 */
3139 if (channel >= center_channels[i] - 6 &&
3140 channel <= center_channels[i] + 6)
3141 return center_channels[i];
3142
3143 return 0;
3144}
3145
3146
3147static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3148 struct hostapd_hw_modes *mode,
3149 u8 channel, u8 bw)
3150{
3151 u8 center_chan;
3152 int i, flags;
3153 enum chan_allowed res, ret = ALLOWED;
3154
3155 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3156 if (!center_chan)
3157 return NOT_ALLOWED;
3158 if (center_chan >= 58 && center_chan <= 138)
3159 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3160
3161 /* check all the channels are available */
3162 for (i = 0; i < 4; i++) {
3163 int adj_chan = center_chan - 6 + i * 4;
3164
3165 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3166 if (res == NOT_ALLOWED)
3167 return NOT_ALLOWED;
3168 if (res == PASSIVE_ONLY)
3169 ret = PASSIVE_ONLY;
3170
3171 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3172 return NOT_ALLOWED;
3173 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3174 return NOT_ALLOWED;
3175 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3176 return NOT_ALLOWED;
3177 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3178 return NOT_ALLOWED;
3179 }
3180
3181 return ret;
3182}
3183
3184
3185static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3186 struct hostapd_hw_modes *mode,
3187 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003188{
3189 int flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003190 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003191
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003192 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3193 if (bw == BW40MINUS) {
3194 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3195 return NOT_ALLOWED;
3196 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3197 } else if (bw == BW40PLUS) {
3198 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3199 return NOT_ALLOWED;
3200 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3201 } else if (bw == BW80) {
3202 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3203 }
3204
3205 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3206 return NOT_ALLOWED;
3207 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3208 return PASSIVE_ONLY;
3209 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003210}
3211
3212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003214 struct p2p_channels *chan,
3215 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003217 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003218 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003220 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003221 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3222 "of all supported channels; assume dualband "
3223 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003224 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225 }
3226
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003227 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228
3229 for (op = 0; op_class[op].op_class; op++) {
3230 struct p2p_oper_class_map *o = &op_class[op];
3231 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003232 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003234 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235 if (mode == NULL)
3236 continue;
3237 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003238 enum chan_allowed res;
3239 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3240 if (res == ALLOWED) {
3241 if (reg == NULL) {
3242 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3243 o->op_class);
3244 reg = &chan->reg_class[cla];
3245 cla++;
3246 reg->reg_class = o->op_class;
3247 }
3248 reg->channel[reg->channels] = ch;
3249 reg->channels++;
3250 } else if (res == PASSIVE_ONLY &&
3251 wpa_s->conf->p2p_add_cli_chan) {
3252 if (cli_reg == NULL) {
3253 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3254 o->op_class);
3255 cli_reg = &cli_chan->reg_class[cli_cla];
3256 cli_cla++;
3257 cli_reg->reg_class = o->op_class;
3258 }
3259 cli_reg->channel[cli_reg->channels] = ch;
3260 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003261 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262 }
3263 if (reg) {
3264 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3265 reg->channel, reg->channels);
3266 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003267 if (cli_reg) {
3268 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3269 cli_reg->channel, cli_reg->channels);
3270 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271 }
3272
3273 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003274 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003276 return 0;
3277}
3278
3279
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003280int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3281 struct hostapd_hw_modes *mode, u8 channel)
3282{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003283 int op;
3284 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003285
3286 for (op = 0; op_class[op].op_class; op++) {
3287 struct p2p_oper_class_map *o = &op_class[op];
3288 u8 ch;
3289
3290 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3291 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3292 o->bw == BW20 || ch != channel)
3293 continue;
3294 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003295 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003296 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003297 }
3298 }
3299 return 0;
3300}
3301
3302
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003303int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3304 struct hostapd_hw_modes *mode, u8 channel)
3305{
3306 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3307 return 0;
3308
3309 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3310}
3311
3312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3314 size_t buf_len)
3315{
3316 struct wpa_supplicant *wpa_s = ctx;
3317
3318 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3319 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3320 break;
3321 }
3322 if (wpa_s == NULL)
3323 return -1;
3324
3325 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3326}
3327
3328
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003329static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3330{
3331 struct wpa_supplicant *wpa_s = ctx;
3332
3333 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3334 struct wpa_ssid *ssid = wpa_s->current_ssid;
3335 if (ssid == NULL)
3336 continue;
3337 if (ssid->mode != WPAS_MODE_INFRA)
3338 continue;
3339 if (wpa_s->wpa_state != WPA_COMPLETED &&
3340 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3341 continue;
3342 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3343 return 1;
3344 }
3345
3346 return 0;
3347}
3348
3349
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003350static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3351{
3352 struct wpa_supplicant *wpa_s = ctx;
3353 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3354}
3355
3356
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003357int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3358{
3359 struct wpa_interface iface;
3360 struct wpa_supplicant *p2pdev_wpa_s;
3361 char ifname[100];
3362 char force_name[100];
3363 int ret;
3364
3365 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3366 wpa_s->ifname);
3367 force_name[0] = '\0';
3368 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3369 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3370 force_name, wpa_s->pending_interface_addr, NULL);
3371 if (ret < 0) {
3372 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3373 return ret;
3374 }
3375 os_strlcpy(wpa_s->pending_interface_name, ifname,
3376 sizeof(wpa_s->pending_interface_name));
3377
3378 os_memset(&iface, 0, sizeof(iface));
3379 iface.p2p_mgmt = 1;
3380 iface.ifname = wpa_s->pending_interface_name;
3381 iface.driver = wpa_s->driver->name;
3382 iface.driver_param = wpa_s->conf->driver_param;
3383 iface.confname = wpa_s->confname;
3384 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3385 if (!p2pdev_wpa_s) {
3386 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3387 return -1;
3388 }
3389 p2pdev_wpa_s->parent = wpa_s;
3390
3391 wpa_s->pending_interface_name[0] = '\0';
3392 return 0;
3393}
3394
3395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396/**
3397 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3398 * @global: Pointer to global data from wpa_supplicant_init()
3399 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3400 * Returns: 0 on success, -1 on failure
3401 */
3402int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3403{
3404 struct p2p_config p2p;
3405 unsigned int r;
3406 int i;
3407
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003408 if (wpa_s->conf->p2p_disabled)
3409 return 0;
3410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003411 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3412 return 0;
3413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414 if (global->p2p)
3415 return 0;
3416
3417 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3418 struct p2p_params params;
3419
3420 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3421 os_memset(&params, 0, sizeof(params));
3422 params.dev_name = wpa_s->conf->device_name;
3423 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3424 WPS_DEV_TYPE_LEN);
3425 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3426 os_memcpy(params.sec_dev_type,
3427 wpa_s->conf->sec_device_type,
3428 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3429
3430 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3431 return -1;
3432
3433 return 0;
3434 }
3435
3436 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003437 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003438 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003439 p2p.p2p_scan = wpas_p2p_scan;
3440 p2p.send_action = wpas_send_action;
3441 p2p.send_action_done = wpas_send_action_done;
3442 p2p.go_neg_completed = wpas_go_neg_completed;
3443 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3444 p2p.dev_found = wpas_dev_found;
3445 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003446 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003447 p2p.start_listen = wpas_start_listen;
3448 p2p.stop_listen = wpas_stop_listen;
3449 p2p.send_probe_resp = wpas_send_probe_resp;
3450 p2p.sd_request = wpas_sd_request;
3451 p2p.sd_response = wpas_sd_response;
3452 p2p.prov_disc_req = wpas_prov_disc_req;
3453 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003454 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 p2p.invitation_process = wpas_invitation_process;
3456 p2p.invitation_received = wpas_invitation_received;
3457 p2p.invitation_result = wpas_invitation_result;
3458 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003459 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460
3461 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003462 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003463 p2p.dev_name = wpa_s->conf->device_name;
3464 p2p.manufacturer = wpa_s->conf->manufacturer;
3465 p2p.model_name = wpa_s->conf->model_name;
3466 p2p.model_number = wpa_s->conf->model_number;
3467 p2p.serial_number = wpa_s->conf->serial_number;
3468 if (wpa_s->wps) {
3469 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3470 p2p.config_methods = wpa_s->wps->config_methods;
3471 }
3472
3473 if (wpa_s->conf->p2p_listen_reg_class &&
3474 wpa_s->conf->p2p_listen_channel) {
3475 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3476 p2p.channel = wpa_s->conf->p2p_listen_channel;
3477 } else {
3478 p2p.reg_class = 81;
3479 /*
3480 * Pick one of the social channels randomly as the listen
3481 * channel.
3482 */
3483 os_get_random((u8 *) &r, sizeof(r));
3484 p2p.channel = 1 + (r % 3) * 5;
3485 }
3486 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3487
3488 if (wpa_s->conf->p2p_oper_reg_class &&
3489 wpa_s->conf->p2p_oper_channel) {
3490 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3491 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3492 p2p.cfg_op_channel = 1;
3493 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3494 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3495
3496 } else {
3497 p2p.op_reg_class = 81;
3498 /*
3499 * Use random operation channel from (1, 6, 11) if no other
3500 * preference is indicated.
3501 */
3502 os_get_random((u8 *) &r, sizeof(r));
3503 p2p.op_channel = 1 + (r % 3) * 5;
3504 p2p.cfg_op_channel = 0;
3505 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3506 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3507 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003508
3509 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3510 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3511 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3512 }
3513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003514 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3515 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3516 p2p.country[2] = 0x04;
3517 } else
3518 os_memcpy(p2p.country, "XX\x04", 3);
3519
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003520 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003521 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3522 "channel list");
3523 return -1;
3524 }
3525
3526 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3527 WPS_DEV_TYPE_LEN);
3528
3529 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3530 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3531 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3532
3533 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3534 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3535
3536 p2p.max_peers = 100;
3537
3538 if (wpa_s->conf->p2p_ssid_postfix) {
3539 p2p.ssid_postfix_len =
3540 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3541 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3542 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3543 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3544 p2p.ssid_postfix_len);
3545 }
3546
3547 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3548
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003549 p2p.max_listen = wpa_s->max_remain_on_chan;
3550
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551 global->p2p = p2p_init(&p2p);
3552 if (global->p2p == NULL)
3553 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003554 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003555
3556 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3557 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3558 continue;
3559 p2p_add_wps_vendor_extension(
3560 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3561 }
3562
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003563 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3564
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003565 return 0;
3566}
3567
3568
3569/**
3570 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3571 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3572 *
3573 * This function deinitialize per-interface P2P data.
3574 */
3575void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3576{
3577 if (wpa_s->driver && wpa_s->drv_priv)
3578 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003579
3580 if (wpa_s->go_params) {
3581 /* Clear any stored provisioning info */
3582 p2p_clear_provisioning_info(
3583 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003584 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003585 }
3586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003587 os_free(wpa_s->go_params);
3588 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003589#ifdef ANDROID_P2P
3590 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3591#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003592 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3593 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3594 wpa_s->p2p_long_listen = 0;
3595 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3596 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3597 wpas_p2p_remove_pending_group_interface(wpa_s);
3598
3599 /* TODO: remove group interface from the driver if this wpa_s instance
3600 * is on top of a P2P group interface */
3601}
3602
3603
3604/**
3605 * wpas_p2p_deinit_global - Deinitialize global P2P module
3606 * @global: Pointer to global data from wpa_supplicant_init()
3607 *
3608 * This function deinitializes the global (per device) P2P module.
3609 */
3610void wpas_p2p_deinit_global(struct wpa_global *global)
3611{
3612 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003614 wpa_s = global->ifaces;
3615 if (wpa_s)
3616 wpas_p2p_service_flush(wpa_s);
3617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618 if (global->p2p == NULL)
3619 return;
3620
3621 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3623 wpa_s = wpa_s->next;
3624 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625 tmp = global->ifaces;
3626 while (tmp &&
3627 (tmp == wpa_s ||
3628 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3629 tmp = tmp->next;
3630 }
3631 if (tmp == NULL)
3632 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003633 /* Disconnect from the P2P group and deinit the interface */
3634 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003635 }
3636
3637 /*
3638 * Deinit GO data on any possibly remaining interface (if main
3639 * interface is used as GO).
3640 */
3641 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3642 if (wpa_s->ap_iface)
3643 wpas_p2p_group_deinit(wpa_s);
3644 }
3645
3646 p2p_deinit(global->p2p);
3647 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003648 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649}
3650
3651
3652static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3653{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003654 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3655 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003656 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003657 if (wpa_s->drv_flags &
3658 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3659 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3660 return 1; /* P2P group requires a new interface in every case
3661 */
3662 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3663 return 0; /* driver does not support concurrent operations */
3664 if (wpa_s->global->ifaces->next)
3665 return 1; /* more that one interface already in use */
3666 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3667 return 1; /* this interface is already in use */
3668 return 0;
3669}
3670
3671
3672static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3673 const u8 *peer_addr,
3674 enum p2p_wps_method wps_method,
3675 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003676 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003677 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003678{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003679 if (persistent_group && wpa_s->conf->persistent_reconnect)
3680 persistent_group = 2;
3681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3683 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3684 go_intent, own_interface_addr,
3685 force_freq, persistent_group);
3686 }
3687
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003688 /*
3689 * Increase GO config timeout if HT40 is used since it takes some time
3690 * to scan channels for coex purposes before the BSS can be started.
3691 */
3692 p2p_set_config_timeout(wpa_s->global->p2p,
3693 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3696 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003697 persistent_group, ssid ? ssid->ssid : NULL,
3698 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003699 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700}
3701
3702
3703static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3704 const u8 *peer_addr,
3705 enum p2p_wps_method wps_method,
3706 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003707 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003708 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003710 if (persistent_group && wpa_s->conf->persistent_reconnect)
3711 persistent_group = 2;
3712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003713 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3714 return -1;
3715
3716 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3717 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003718 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003719 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720}
3721
3722
3723static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3724{
3725 wpa_s->p2p_join_scan_count++;
3726 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3727 wpa_s->p2p_join_scan_count);
3728 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3729 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3730 " for join operationg - stop join attempt",
3731 MAC2STR(wpa_s->pending_join_iface_addr));
3732 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003733 if (wpa_s->p2p_auto_pd) {
3734 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003735 wpa_msg_global(wpa_s, MSG_INFO,
3736 P2P_EVENT_PROV_DISC_FAILURE
3737 " p2p_dev_addr=" MACSTR " status=N/A",
3738 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003739 return;
3740 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003741 wpa_msg_global(wpa_s->parent, MSG_INFO,
3742 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003743 }
3744}
3745
3746
Dmitry Shmidt04949592012-07-19 12:16:46 -07003747static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3748{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003749 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003750
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003751 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3752 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003753 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003754 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003755
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003756 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3757 if (!freqs)
3758 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003759
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003760 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3761 wpa_s->num_multichan_concurrent);
3762 if (num < 0) {
3763 res = 1;
3764 goto exit_free;
3765 }
3766
3767 for (i = 0; i < num; i++) {
3768 if (freqs[i] == freq) {
3769 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3770 freq);
3771 res = 0;
3772 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003773 }
3774 }
3775
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003776 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003777
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003778exit_free:
3779 os_free(freqs);
3780 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003781}
3782
3783
3784static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3785 const u8 *peer_dev_addr)
3786{
3787 struct wpa_bss *bss;
3788 int updated;
3789
3790 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3791 if (bss == NULL)
3792 return -1;
3793 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3794 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3795 "last scan");
3796 return 0;
3797 }
3798
3799 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3800 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3801 "%ld.%06ld (%supdated in last scan)",
3802 bss->last_update.sec, bss->last_update.usec,
3803 updated ? "": "not ");
3804
3805 return updated;
3806}
3807
3808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3810 struct wpa_scan_results *scan_res)
3811{
3812 struct wpa_bss *bss;
3813 int freq;
3814 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003816 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3817
3818 if (wpa_s->global->p2p_disabled)
3819 return;
3820
Dmitry Shmidt04949592012-07-19 12:16:46 -07003821 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3822 scan_res ? (int) scan_res->num : -1,
3823 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824
3825 if (scan_res)
3826 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3827
Dmitry Shmidt04949592012-07-19 12:16:46 -07003828 if (wpa_s->p2p_auto_pd) {
3829 int join = wpas_p2p_peer_go(wpa_s,
3830 wpa_s->pending_join_dev_addr);
3831 if (join == 0 &&
3832 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3833 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003834 bss = wpa_bss_get_bssid_latest(
3835 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003836 if (bss) {
3837 freq = bss->freq;
3838 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3839 "the peer " MACSTR " at %d MHz",
3840 wpa_s->auto_pd_scan_retry,
3841 MAC2STR(wpa_s->
3842 pending_join_dev_addr),
3843 freq);
3844 wpas_p2p_join_scan_req(wpa_s, freq);
3845 return;
3846 }
3847 }
3848
3849 if (join < 0)
3850 join = 0;
3851
3852 wpa_s->p2p_auto_pd = 0;
3853 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3854 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3855 MAC2STR(wpa_s->pending_join_dev_addr), join);
3856 if (p2p_prov_disc_req(wpa_s->global->p2p,
3857 wpa_s->pending_join_dev_addr,
3858 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003859 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003860 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003861 wpa_msg_global(wpa_s, MSG_INFO,
3862 P2P_EVENT_PROV_DISC_FAILURE
3863 " p2p_dev_addr=" MACSTR " status=N/A",
3864 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003865 }
3866 return;
3867 }
3868
3869 if (wpa_s->p2p_auto_join) {
3870 int join = wpas_p2p_peer_go(wpa_s,
3871 wpa_s->pending_join_dev_addr);
3872 if (join < 0) {
3873 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3874 "running a GO -> use GO Negotiation");
3875 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3876 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3877 wpa_s->p2p_persistent_group, 0, 0, 0,
3878 wpa_s->p2p_go_intent,
3879 wpa_s->p2p_connect_freq,
3880 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003881 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003882 wpa_s->p2p_go_ht40,
3883 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003884 return;
3885 }
3886
3887 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3888 "try to join the group", join ? "" :
3889 " in older scan");
3890 if (!join)
3891 wpa_s->p2p_fallback_to_go_neg = 1;
3892 }
3893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3895 wpa_s->pending_join_iface_addr);
3896 if (freq < 0 &&
3897 p2p_get_interface_addr(wpa_s->global->p2p,
3898 wpa_s->pending_join_dev_addr,
3899 iface_addr) == 0 &&
3900 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3901 {
3902 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3903 "address for join from " MACSTR " to " MACSTR
3904 " based on newly discovered P2P peer entry",
3905 MAC2STR(wpa_s->pending_join_iface_addr),
3906 MAC2STR(iface_addr));
3907 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3908 ETH_ALEN);
3909
3910 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3911 wpa_s->pending_join_iface_addr);
3912 }
3913 if (freq >= 0) {
3914 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3915 "from P2P peer table: %d MHz", freq);
3916 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003917 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918 if (bss) {
3919 freq = bss->freq;
3920 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07003921 "from BSS table: %d MHz (SSID %s)", freq,
3922 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003923 }
3924 if (freq > 0) {
3925 u16 method;
3926
Dmitry Shmidt04949592012-07-19 12:16:46 -07003927 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003928 wpa_msg_global(wpa_s->parent, MSG_INFO,
3929 P2P_EVENT_GROUP_FORMATION_FAILURE
3930 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003931 return;
3932 }
3933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003934 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3935 "prior to joining an existing group (GO " MACSTR
3936 " freq=%u MHz)",
3937 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3938 wpa_s->pending_pd_before_join = 1;
3939
3940 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 case WPS_PIN_DISPLAY:
3942 method = WPS_CONFIG_KEYPAD;
3943 break;
3944 case WPS_PIN_KEYPAD:
3945 method = WPS_CONFIG_DISPLAY;
3946 break;
3947 case WPS_PBC:
3948 method = WPS_CONFIG_PUSHBUTTON;
3949 break;
3950 default:
3951 method = 0;
3952 break;
3953 }
3954
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003955 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3956 wpa_s->pending_join_dev_addr) ==
3957 method)) {
3958 /*
3959 * We have already performed provision discovery for
3960 * joining the group. Proceed directly to join
3961 * operation without duplicated provision discovery. */
3962 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3963 "with " MACSTR " already done - proceed to "
3964 "join",
3965 MAC2STR(wpa_s->pending_join_dev_addr));
3966 wpa_s->pending_pd_before_join = 0;
3967 goto start;
3968 }
3969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003970 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003971 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003972 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003973 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3974 "Discovery Request before joining an "
3975 "existing group");
3976 wpa_s->pending_pd_before_join = 0;
3977 goto start;
3978 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003979 return;
3980 }
3981
3982 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3983 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3984 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3985 wpas_p2p_check_join_scan_limit(wpa_s);
3986 return;
3987
3988start:
3989 /* Start join operation immediately */
3990 wpas_p2p_join_start(wpa_s);
3991}
3992
3993
Dmitry Shmidt04949592012-07-19 12:16:46 -07003994static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003995{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996 int ret;
3997 struct wpa_driver_scan_params params;
3998 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003999 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004000 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004001#ifdef ANDROID_P2P
4002 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004004 /* If freq is not provided, check the operating freq of the GO and do a
4005 * a directed scan to save time
4006 */
4007 if(!freq) {
4008 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4009 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
4010 }
4011#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012 os_memset(&params, 0, sizeof(params));
4013
4014 /* P2P Wildcard SSID */
4015 params.num_ssids = 1;
4016 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4017 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4018
4019 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004020 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4021 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4022 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023 if (wps_ie == NULL) {
4024 wpas_p2p_scan_res_join(wpa_s, NULL);
4025 return;
4026 }
4027
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004028 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4029 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004030 if (ies == NULL) {
4031 wpabuf_free(wps_ie);
4032 wpas_p2p_scan_res_join(wpa_s, NULL);
4033 return;
4034 }
4035 wpabuf_put_buf(ies, wps_ie);
4036 wpabuf_free(wps_ie);
4037
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004038 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004039
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004040 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041 params.extra_ies = wpabuf_head(ies);
4042 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004043 if (freq > 0) {
4044 freqs[0] = freq;
4045 params.freqs = freqs;
4046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004047
4048 /*
4049 * Run a scan to update BSS table and start Provision Discovery once
4050 * the new scan results become available.
4051 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004052 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004053 if (!ret) {
4054 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004055 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004056 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004057
4058 wpabuf_free(ies);
4059
4060 if (ret) {
4061 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4062 "try again later");
4063 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4064 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4065 wpas_p2p_check_join_scan_limit(wpa_s);
4066 }
4067}
4068
4069
Dmitry Shmidt04949592012-07-19 12:16:46 -07004070static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4071{
4072 struct wpa_supplicant *wpa_s = eloop_ctx;
4073 wpas_p2p_join_scan_req(wpa_s, 0);
4074}
4075
4076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004078 const u8 *dev_addr, enum p2p_wps_method wps_method,
4079 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004080{
4081 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004082 MACSTR " dev " MACSTR ")%s",
4083 MAC2STR(iface_addr), MAC2STR(dev_addr),
4084 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085
Dmitry Shmidt04949592012-07-19 12:16:46 -07004086 wpa_s->p2p_auto_pd = 0;
4087 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4089 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4090 wpa_s->pending_join_wps_method = wps_method;
4091
4092 /* Make sure we are not running find during connection establishment */
4093 wpas_p2p_stop_find(wpa_s);
4094
4095 wpa_s->p2p_join_scan_count = 0;
4096 wpas_p2p_join_scan(wpa_s, NULL);
4097 return 0;
4098}
4099
4100
4101static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4102{
4103 struct wpa_supplicant *group;
4104 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004105 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106
4107 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4108 if (group == NULL)
4109 return -1;
4110 if (group != wpa_s) {
4111 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4112 sizeof(group->p2p_pin));
4113 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004114 } else {
4115 /*
4116 * Need to mark the current interface for p2p_group_formation
4117 * when a separate group interface is not used. This is needed
4118 * to allow p2p_cancel stop a pending p2p_connect-join.
4119 * wpas_p2p_init_group_interface() addresses this for the case
4120 * where a separate group interface is used.
4121 */
4122 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123 }
4124
4125 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004126 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004127
4128 os_memset(&res, 0, sizeof(res));
4129 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4130 ETH_ALEN);
4131 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004132 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004133 if (bss) {
4134 res.freq = bss->freq;
4135 res.ssid_len = bss->ssid_len;
4136 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07004137 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4138 "from BSS table: %d MHz (SSID %s)", bss->freq,
4139 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004140 }
4141
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004142 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4143 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4144 "starting client");
4145 wpa_drv_cancel_remain_on_channel(wpa_s);
4146 wpa_s->off_channel_freq = 0;
4147 wpa_s->roc_waiting_drv_freq = 0;
4148 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149 wpas_start_wps_enrollee(group, &res);
4150
4151 /*
4152 * Allow a longer timeout for join-a-running-group than normal 15
4153 * second group formation timeout since the GO may not have authorized
4154 * our connection yet.
4155 */
4156 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4157 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4158 wpa_s, NULL);
4159
4160 return 0;
4161}
4162
4163
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004164static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004165 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004166{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004167 int *freqs, res;
4168 unsigned int freq_in_use = 0, num, i;
4169
4170 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4171 if (!freqs)
4172 return -1;
4173
4174 num = get_shared_radio_freqs(wpa_s, freqs,
4175 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004176 wpa_printf(MSG_DEBUG,
4177 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4178 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004179
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004180 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004181 int ret;
4182 if (go)
4183 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4184 else
4185 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4186 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004187 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4188 "(%u MHz) is not supported for P2P uses",
4189 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004190 res = -3;
4191 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004192 }
4193
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004194 for (i = 0; i < num; i++) {
4195 if (freqs[i] == freq)
4196 freq_in_use = 1;
4197 }
4198
4199 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4200 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4201 freq);
4202 res = -2;
4203 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004204 }
4205 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4206 "requested channel (%u MHz)", freq);
4207 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004208 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004209 }
4210
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004211 for (i = 0; i < num; i++) {
4212 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4213 continue;
4214
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004215 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004216 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4217 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004218 *pref_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004219 } else {
4220 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004221 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004222 *force_freq = freqs[i];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004223 }
4224 break;
4225 }
4226
4227 if (i == num) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004228 if (num < wpa_s->num_multichan_concurrent && num > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004229 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4230 *force_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004231 } else if (num < wpa_s->num_multichan_concurrent) {
4232 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4233 *force_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004234 } else {
4235 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4236 res = -2;
4237 goto exit_free;
4238 }
4239 }
4240
4241exit_ok:
4242 res = 0;
4243exit_free:
4244 os_free(freqs);
4245 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004246}
4247
4248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249/**
4250 * wpas_p2p_connect - Request P2P Group Formation to be started
4251 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4252 * @peer_addr: Address of the peer P2P Device
4253 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4254 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004255 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004256 * @join: Whether to join an existing group (as a client) instead of starting
4257 * Group Owner negotiation; @peer_addr is BSSID in that case
4258 * @auth: Whether to only authorize the connection instead of doing that and
4259 * initiating Group Owner negotiation
4260 * @go_intent: GO Intent or -1 to use default
4261 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004262 * @persistent_id: Persistent group credentials to use for forcing GO
4263 * parameters or -1 to generate new values (SSID/passphrase)
4264 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4265 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004266 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004267 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004268 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4269 * failure, -2 on failure due to channel not currently available,
4270 * -3 if forced channel is not supported
4271 */
4272int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4273 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004274 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004275 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004276 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004277{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004278 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004279 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004280 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004281 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004282 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283
4284 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4285 return -1;
4286
Dmitry Shmidt04949592012-07-19 12:16:46 -07004287 if (persistent_id >= 0) {
4288 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4289 if (ssid == NULL || ssid->disabled != 2 ||
4290 ssid->mode != WPAS_MODE_P2P_GO)
4291 return -1;
4292 }
4293
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004294 os_free(wpa_s->global->add_psk);
4295 wpa_s->global->add_psk = NULL;
4296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004297 if (go_intent < 0)
4298 go_intent = wpa_s->conf->p2p_go_intent;
4299
4300 if (!auth)
4301 wpa_s->p2p_long_listen = 0;
4302
4303 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004304 wpa_s->p2p_persistent_group = !!persistent_group;
4305 wpa_s->p2p_persistent_id = persistent_id;
4306 wpa_s->p2p_go_intent = go_intent;
4307 wpa_s->p2p_connect_freq = freq;
4308 wpa_s->p2p_fallback_to_go_neg = 0;
4309 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004310 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004311 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312
4313 if (pin)
4314 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4315 else if (wps_method == WPS_PIN_DISPLAY) {
4316 ret = wps_generate_pin();
4317 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4318 ret);
4319 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4320 wpa_s->p2p_pin);
4321 } else
4322 wpa_s->p2p_pin[0] = '\0';
4323
Dmitry Shmidt04949592012-07-19 12:16:46 -07004324 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004325 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4326 if (auth) {
4327 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4328 "connect a running group from " MACSTR,
4329 MAC2STR(peer_addr));
4330 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4331 return ret;
4332 }
4333 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4334 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4335 iface_addr) < 0) {
4336 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4337 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4338 dev_addr);
4339 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004340 if (auto_join) {
4341 os_get_time(&wpa_s->p2p_auto_started);
4342 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4343 "%ld.%06ld",
4344 wpa_s->p2p_auto_started.sec,
4345 wpa_s->p2p_auto_started.usec);
4346 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004347 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004348 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4349 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350 return -1;
4351 return ret;
4352 }
4353
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004354 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4355 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004356 if (res)
4357 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004358 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004359
4360 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4361
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004362 if (wpa_s->create_p2p_iface) {
4363 /* Prepare to add a new interface for the group */
4364 iftype = WPA_IF_P2P_GROUP;
4365 if (go_intent == 15)
4366 iftype = WPA_IF_P2P_GO;
4367 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4368 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4369 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004372
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004373 if_addr = wpa_s->pending_interface_addr;
4374 } else
4375 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004376
4377 if (auth) {
4378 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004379 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004380 force_freq, persistent_group, ssid,
4381 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382 return -1;
4383 return ret;
4384 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004385
4386 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4387 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004388 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004389 if (wpa_s->create_p2p_iface)
4390 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391 return -1;
4392 }
4393 return ret;
4394}
4395
4396
4397/**
4398 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4399 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4400 * @freq: Frequency of the channel in MHz
4401 * @duration: Duration of the stay on the channel in milliseconds
4402 *
4403 * This callback is called when the driver indicates that it has started the
4404 * requested remain-on-channel duration.
4405 */
4406void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4407 unsigned int freq, unsigned int duration)
4408{
4409 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4410 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004411 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4412 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4413 wpa_s->pending_listen_duration);
4414 wpa_s->pending_listen_freq = 0;
4415 }
4416}
4417
4418
4419static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4420 unsigned int timeout)
4421{
4422 /* Limit maximum Listen state time based on driver limitation. */
4423 if (timeout > wpa_s->max_remain_on_chan)
4424 timeout = wpa_s->max_remain_on_chan;
4425
4426 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4427 return wpa_drv_p2p_listen(wpa_s, timeout);
4428
4429 return p2p_listen(wpa_s->global->p2p, timeout);
4430}
4431
4432
4433/**
4434 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4435 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4436 * @freq: Frequency of the channel in MHz
4437 *
4438 * This callback is called when the driver indicates that a remain-on-channel
4439 * operation has been completed, i.e., the duration on the requested channel
4440 * has timed out.
4441 */
4442void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4443 unsigned int freq)
4444{
4445 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4446 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004447 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4449 return;
4450 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4451 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004452 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004453 return;
4454 if (wpa_s->p2p_long_listen > 0)
4455 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4456 if (wpa_s->p2p_long_listen > 0) {
4457 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4458 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004459 } else {
4460 /*
4461 * When listen duration is over, stop listen & update p2p_state
4462 * to IDLE.
4463 */
4464 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004465 }
4466}
4467
4468
4469/**
4470 * wpas_p2p_group_remove - Remove a P2P group
4471 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4472 * @ifname: Network interface name of the group interface or "*" to remove all
4473 * groups
4474 * Returns: 0 on success, -1 on failure
4475 *
4476 * This function is used to remove a P2P group. This can be used to disconnect
4477 * from a group in which the local end is a P2P Client or to end a P2P Group in
4478 * case the local end is the Group Owner. If a virtual network interface was
4479 * created for this group, that interface will be removed. Otherwise, only the
4480 * configured P2P group network will be removed from the interface.
4481 */
4482int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4483{
4484 struct wpa_global *global = wpa_s->global;
4485
4486 if (os_strcmp(ifname, "*") == 0) {
4487 struct wpa_supplicant *prev;
4488 wpa_s = global->ifaces;
4489 while (wpa_s) {
4490 prev = wpa_s;
4491 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004492 if (prev->p2p_group_interface !=
4493 NOT_P2P_GROUP_INTERFACE ||
4494 (prev->current_ssid &&
4495 prev->current_ssid->p2p_group))
4496 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 }
4498 return 0;
4499 }
4500
4501 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4502 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4503 break;
4504 }
4505
4506 return wpas_p2p_disconnect(wpa_s);
4507}
4508
4509
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004510static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4511{
4512 unsigned int r;
4513
4514 if (freq == 2) {
4515 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4516 "band");
4517 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004518 p2p_supported_freq_go(wpa_s->global->p2p,
4519 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004520 freq = wpa_s->best_24_freq;
4521 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4522 "channel: %d MHz", freq);
4523 } else {
4524 os_get_random((u8 *) &r, sizeof(r));
4525 freq = 2412 + (r % 3) * 25;
4526 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4527 "channel: %d MHz", freq);
4528 }
4529 }
4530
4531 if (freq == 5) {
4532 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4533 "band");
4534 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004535 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004536 wpa_s->best_5_freq)) {
4537 freq = wpa_s->best_5_freq;
4538 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4539 "channel: %d MHz", freq);
4540 } else {
4541 os_get_random((u8 *) &r, sizeof(r));
4542 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004543 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004544 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4545 "5 GHz channel for P2P group");
4546 return -1;
4547 }
4548 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4549 "channel: %d MHz", freq);
4550 }
4551 }
4552
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004553 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004554 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4555 "(%u MHz) is not supported for P2P uses",
4556 freq);
4557 return -1;
4558 }
4559
4560 return freq;
4561}
4562
4563
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004564static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4565 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004566 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004567 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004569 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004570 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004571 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572
4573 os_memset(params, 0, sizeof(*params));
4574 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004575 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004576 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004578 if (!freq_included(channels, freq)) {
4579 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4580 "accepted", freq);
4581 return -1;
4582 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004583 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4584 "frequency %d MHz", freq);
4585 params->freq = freq;
4586 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4587 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004588 wpa_s->conf->p2p_oper_channel <= 11 &&
4589 freq_included(channels,
4590 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004591 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4592 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4593 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004594 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4595 wpa_s->conf->p2p_oper_reg_class == 116 ||
4596 wpa_s->conf->p2p_oper_reg_class == 117 ||
4597 wpa_s->conf->p2p_oper_reg_class == 124 ||
4598 wpa_s->conf->p2p_oper_reg_class == 126 ||
4599 wpa_s->conf->p2p_oper_reg_class == 127) &&
4600 freq_included(channels,
4601 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4603 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4604 "frequency %d MHz", params->freq);
4605 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4606 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004607 p2p_supported_freq_go(wpa_s->global->p2p,
4608 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004609 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004610 params->freq = wpa_s->best_overall_freq;
4611 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4612 "channel %d MHz", params->freq);
4613 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4614 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004615 p2p_supported_freq_go(wpa_s->global->p2p,
4616 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004617 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004618 params->freq = wpa_s->best_24_freq;
4619 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4620 "channel %d MHz", params->freq);
4621 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4622 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004623 p2p_supported_freq_go(wpa_s->global->p2p,
4624 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004625 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626 params->freq = wpa_s->best_5_freq;
4627 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4628 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004629 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4630 channels))) {
4631 params->freq = pref_freq;
4632 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4633 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004634 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004635 int chan;
4636 for (chan = 0; chan < 11; chan++) {
4637 params->freq = 2412 + chan * 5;
4638 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004639 params->freq) &&
4640 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004641 break;
4642 }
4643 if (chan == 11) {
4644 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4645 "allowed");
4646 return -1;
4647 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4649 "known)", params->freq);
4650 }
4651
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004652 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4653 if (!freqs)
4654 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004656 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4657 wpa_s->num_multichan_concurrent);
4658 if (res < 0) {
4659 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004660 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004661 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004662 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004663
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004664 for (i = 0; i < num; i++) {
4665 if (freq && freqs[i] == freq)
4666 break;
4667 if (!freq && freq_included(channels, freqs[i])) {
4668 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4669 freqs[i]);
4670 params->freq = freqs[i];
4671 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004672 }
4673 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004674
4675 if (i == num) {
4676 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
4677 if (freq)
4678 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4679 else
4680 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4681 os_free(freqs);
4682 return -1;
4683 } else if (num == 0) {
4684 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
4685 } else {
4686 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4687 }
4688 }
4689
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004690 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004691 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692}
4693
4694
4695static struct wpa_supplicant *
4696wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4697 int go)
4698{
4699 struct wpa_supplicant *group_wpa_s;
4700
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004701 if (!wpas_p2p_create_iface(wpa_s)) {
4702 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4703 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004704 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004705 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004706 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707
4708 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004709 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004710 wpa_msg_global(wpa_s, MSG_ERROR,
4711 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004712 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004713 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004714 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4715 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004716 wpa_msg_global(wpa_s, MSG_ERROR,
4717 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718 wpas_p2p_remove_pending_group_interface(wpa_s);
4719 return NULL;
4720 }
4721
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004722 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4723 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004724 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004725 return group_wpa_s;
4726}
4727
4728
4729/**
4730 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4731 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4732 * @persistent_group: Whether to create a persistent group
4733 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004734 * @ht40: Start GO with 40 MHz channel width
4735 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004736 * Returns: 0 on success, -1 on failure
4737 *
4738 * This function creates a new P2P group with the local end as the Group Owner,
4739 * i.e., without using Group Owner Negotiation.
4740 */
4741int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004742 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743{
4744 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745
4746 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4747 return -1;
4748
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004749 os_free(wpa_s->global->add_psk);
4750 wpa_s->global->add_psk = NULL;
4751
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004752 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004753 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004754 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004755
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004756 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4757 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004760 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004761 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004762 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004763 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004764 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4765 "(%u MHz) is not supported for P2P uses",
4766 params.freq);
4767 return -1;
4768 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769 p2p_go_params(wpa_s->global->p2p, &params);
4770 params.persistent_group = persistent_group;
4771
4772 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4773 if (wpa_s == NULL)
4774 return -1;
4775 wpas_start_wps_go(wpa_s, &params, 0);
4776
4777 return 0;
4778}
4779
4780
4781static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4782 struct wpa_ssid *params, int addr_allocated)
4783{
4784 struct wpa_ssid *ssid;
4785
4786 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4787 if (wpa_s == NULL)
4788 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004789 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790
4791 wpa_supplicant_ap_deinit(wpa_s);
4792
4793 ssid = wpa_config_add_network(wpa_s->conf);
4794 if (ssid == NULL)
4795 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004796 wpa_config_set_network_defaults(ssid);
4797 ssid->temporary = 1;
4798 ssid->proto = WPA_PROTO_RSN;
4799 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4800 ssid->group_cipher = WPA_CIPHER_CCMP;
4801 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4802 ssid->ssid = os_malloc(params->ssid_len);
4803 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804 wpa_config_remove_network(wpa_s->conf, ssid->id);
4805 return -1;
4806 }
4807 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4808 ssid->ssid_len = params->ssid_len;
4809 ssid->p2p_group = 1;
4810 ssid->export_keys = 1;
4811 if (params->psk_set) {
4812 os_memcpy(ssid->psk, params->psk, 32);
4813 ssid->psk_set = 1;
4814 }
4815 if (params->passphrase)
4816 ssid->passphrase = os_strdup(params->passphrase);
4817
4818 wpa_supplicant_select_network(wpa_s, ssid);
4819
4820 wpa_s->show_group_started = 1;
4821
4822 return 0;
4823}
4824
4825
4826int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4827 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004828 int freq, int ht40, int vht,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004829 const struct p2p_channels *channels,
4830 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004831{
4832 struct p2p_go_neg_results params;
4833 int go = 0;
4834
4835 if (ssid->disabled != 2 || ssid->ssid == NULL)
4836 return -1;
4837
4838 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4839 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4840 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4841 "already running");
4842 return 0;
4843 }
4844
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004845 os_free(wpa_s->global->add_psk);
4846 wpa_s->global->add_psk = NULL;
4847
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004849 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004850
Dmitry Shmidt04949592012-07-19 12:16:46 -07004851 wpa_s->p2p_fallback_to_go_neg = 0;
4852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004853 if (ssid->mode == WPAS_MODE_INFRA)
4854 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4855
4856 if (ssid->mode != WPAS_MODE_P2P_GO)
4857 return -1;
4858
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004859 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4860 if (freq < 0)
4861 return -1;
4862
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004863 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004864 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865
4866 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004867 params.psk_set = ssid->psk_set;
4868 if (params.psk_set)
4869 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004870 if (ssid->passphrase) {
4871 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4872 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4873 "persistent group");
4874 return -1;
4875 }
4876 os_strlcpy(params.passphrase, ssid->passphrase,
4877 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004878 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004879 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4880 params.ssid_len = ssid->ssid_len;
4881 params.persistent_group = 1;
4882
4883 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4884 if (wpa_s == NULL)
4885 return -1;
4886
Dmitry Shmidt56052862013-10-04 10:23:25 -07004887 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888 wpas_start_wps_go(wpa_s, &params, 0);
4889
4890 return 0;
4891}
4892
4893
4894static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4895 struct wpabuf *proberesp_ies)
4896{
4897 struct wpa_supplicant *wpa_s = ctx;
4898 if (wpa_s->ap_iface) {
4899 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004900 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4901 wpabuf_free(beacon_ies);
4902 wpabuf_free(proberesp_ies);
4903 return;
4904 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905 if (beacon_ies) {
4906 wpabuf_free(hapd->p2p_beacon_ie);
4907 hapd->p2p_beacon_ie = beacon_ies;
4908 }
4909 wpabuf_free(hapd->p2p_probe_resp_ie);
4910 hapd->p2p_probe_resp_ie = proberesp_ies;
4911 } else {
4912 wpabuf_free(beacon_ies);
4913 wpabuf_free(proberesp_ies);
4914 }
4915 wpa_supplicant_ap_update_beacon(wpa_s);
4916}
4917
4918
4919static void wpas_p2p_idle_update(void *ctx, int idle)
4920{
4921 struct wpa_supplicant *wpa_s = ctx;
4922 if (!wpa_s->ap_iface)
4923 return;
4924 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004925 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 wpas_p2p_set_group_idle_timeout(wpa_s);
4927 else
4928 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4929}
4930
4931
4932struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004933 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004934{
4935 struct p2p_group *group;
4936 struct p2p_group_config *cfg;
4937
4938 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4939 return NULL;
4940 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4941 return NULL;
4942
4943 cfg = os_zalloc(sizeof(*cfg));
4944 if (cfg == NULL)
4945 return NULL;
4946
Dmitry Shmidt04949592012-07-19 12:16:46 -07004947 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004948 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004949 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004950 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4952 if (wpa_s->max_stations &&
4953 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4954 cfg->max_clients = wpa_s->max_stations;
4955 else
4956 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004957 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4958 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959 cfg->cb_ctx = wpa_s;
4960 cfg->ie_update = wpas_p2p_ie_update;
4961 cfg->idle_update = wpas_p2p_idle_update;
4962
4963 group = p2p_group_init(wpa_s->global->p2p, cfg);
4964 if (group == NULL)
4965 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004966 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 p2p_group_notif_formation_done(group);
4968 wpa_s->p2p_group = group;
4969 return group;
4970}
4971
4972
4973void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4974 int registrar)
4975{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004976 struct wpa_ssid *ssid = wpa_s->current_ssid;
4977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004978 if (!wpa_s->p2p_in_provisioning) {
4979 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4980 "provisioning not in progress");
4981 return;
4982 }
4983
Dmitry Shmidt04949592012-07-19 12:16:46 -07004984 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4985 u8 go_dev_addr[ETH_ALEN];
4986 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4987 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4988 ssid->ssid_len);
4989 /* Clear any stored provisioning info */
4990 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4991 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4994 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004995 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004996 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4997 /*
4998 * Use a separate timeout for initial data connection to
4999 * complete to allow the group to be removed automatically if
5000 * something goes wrong in this step before the P2P group idle
5001 * timeout mechanism is taken into use.
5002 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005003 wpa_dbg(wpa_s, MSG_DEBUG,
5004 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5005 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005006 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5007 wpas_p2p_group_formation_timeout,
5008 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005009 } else if (ssid) {
5010 /*
5011 * Use a separate timeout for initial data connection to
5012 * complete to allow the group to be removed automatically if
5013 * the client does not complete data connection successfully.
5014 */
5015 wpa_dbg(wpa_s, MSG_DEBUG,
5016 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5017 P2P_MAX_INITIAL_CONN_WAIT_GO);
5018 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5019 wpas_p2p_group_formation_timeout,
5020 wpa_s->parent, NULL);
5021 /*
5022 * Complete group formation on first successful data connection
5023 */
5024 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005025 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 if (wpa_s->global->p2p)
5027 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5028 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5029 wpa_drv_wps_success_cb(wpa_s, peer_addr);
5030 wpas_group_formation_completed(wpa_s, 1);
5031}
5032
5033
Jouni Malinen75ecf522011-06-27 15:19:46 -07005034void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5035 struct wps_event_fail *fail)
5036{
5037 if (!wpa_s->p2p_in_provisioning) {
5038 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5039 "provisioning not in progress");
5040 return;
5041 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005042
5043 if (wpa_s->go_params) {
5044 p2p_clear_provisioning_info(
5045 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005046 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005047 }
5048
Jouni Malinen75ecf522011-06-27 15:19:46 -07005049 wpas_notify_p2p_wps_failed(wpa_s, fail);
5050}
5051
5052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005054 const char *config_method,
5055 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056{
5057 u16 config_methods;
5058
Dmitry Shmidt04949592012-07-19 12:16:46 -07005059 wpa_s->p2p_fallback_to_go_neg = 0;
5060 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005061 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005063 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005065 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5066 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005067 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005068 else {
5069 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005070 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072
Dmitry Shmidt04949592012-07-19 12:16:46 -07005073 if (use == WPAS_P2P_PD_AUTO) {
5074 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5075 wpa_s->pending_pd_config_methods = config_methods;
5076 wpa_s->p2p_auto_pd = 1;
5077 wpa_s->p2p_auto_join = 0;
5078 wpa_s->pending_pd_before_join = 0;
5079 wpa_s->auto_pd_scan_retry = 0;
5080 wpas_p2p_stop_find(wpa_s);
5081 wpa_s->p2p_join_scan_count = 0;
5082 os_get_time(&wpa_s->p2p_auto_started);
5083 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5084 wpa_s->p2p_auto_started.sec,
5085 wpa_s->p2p_auto_started.usec);
5086 wpas_p2p_join_scan(wpa_s, NULL);
5087 return 0;
5088 }
5089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5091 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005092 config_methods,
5093 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005094 }
5095
5096 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5097 return -1;
5098
5099 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005100 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005101 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102}
5103
5104
5105int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5106 char *end)
5107{
5108 return p2p_scan_result_text(ies, ies_len, buf, end);
5109}
5110
5111
5112static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5113{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005114 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005115 return;
5116
5117 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5118 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005119 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120}
5121
5122
5123int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5124 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005125 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005126 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005127{
5128 wpas_p2p_clear_pending_action_tx(wpa_s);
5129 wpa_s->p2p_long_listen = 0;
5130
5131 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5132 return wpa_drv_p2p_find(wpa_s, timeout, type);
5133
Dmitry Shmidt04949592012-07-19 12:16:46 -07005134 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5135 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005136 return -1;
5137
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005138 wpa_supplicant_cancel_sched_scan(wpa_s);
5139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005141 num_req_dev_types, req_dev_types, dev_id,
5142 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005143}
5144
5145
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005146static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147{
5148 wpas_p2p_clear_pending_action_tx(wpa_s);
5149 wpa_s->p2p_long_listen = 0;
5150 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5151 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07005152 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005153
5154 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5155 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005156 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005157 }
5158
5159 if (wpa_s->global->p2p)
5160 p2p_stop_find(wpa_s->global->p2p);
5161
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005162 return 0;
5163}
5164
5165
5166void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5167{
5168 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5169 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170 wpas_p2p_remove_pending_group_interface(wpa_s);
5171}
5172
5173
5174static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5175{
5176 struct wpa_supplicant *wpa_s = eloop_ctx;
5177 wpa_s->p2p_long_listen = 0;
5178}
5179
5180
5181int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5182{
5183 int res;
5184
5185 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5186 return -1;
5187
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005188 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005189 wpas_p2p_clear_pending_action_tx(wpa_s);
5190
5191 if (timeout == 0) {
5192 /*
5193 * This is a request for unlimited Listen state. However, at
5194 * least for now, this is mapped to a Listen state for one
5195 * hour.
5196 */
5197 timeout = 3600;
5198 }
5199 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5200 wpa_s->p2p_long_listen = 0;
5201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005202 /*
5203 * Stop previous find/listen operation to avoid trying to request a new
5204 * remain-on-channel operation while the driver is still running the
5205 * previous one.
5206 */
5207 if (wpa_s->global->p2p)
5208 p2p_stop_find(wpa_s->global->p2p);
5209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005210 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5211 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5212 wpa_s->p2p_long_listen = timeout * 1000;
5213 eloop_register_timeout(timeout, 0,
5214 wpas_p2p_long_listen_timeout,
5215 wpa_s, NULL);
5216 }
5217
5218 return res;
5219}
5220
5221
5222int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5223 u8 *buf, size_t len, int p2p_group)
5224{
5225 struct wpabuf *p2p_ie;
5226 int ret;
5227
5228 if (wpa_s->global->p2p_disabled)
5229 return -1;
5230 if (wpa_s->global->p2p == NULL)
5231 return -1;
5232 if (bss == NULL)
5233 return -1;
5234
5235 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5236 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5237 p2p_group, p2p_ie);
5238 wpabuf_free(p2p_ie);
5239
5240 return ret;
5241}
5242
5243
5244int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005245 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005246 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005247{
5248 if (wpa_s->global->p2p_disabled)
5249 return 0;
5250 if (wpa_s->global->p2p == NULL)
5251 return 0;
5252
Dmitry Shmidt04949592012-07-19 12:16:46 -07005253 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5254 ie, ie_len)) {
5255 case P2P_PREQ_NOT_P2P:
5256 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5257 ssi_signal);
5258 /* fall through */
5259 case P2P_PREQ_MALFORMED:
5260 case P2P_PREQ_NOT_LISTEN:
5261 case P2P_PREQ_NOT_PROCESSED:
5262 default: /* make gcc happy */
5263 return 0;
5264 case P2P_PREQ_PROCESSED:
5265 return 1;
5266 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005267}
5268
5269
5270void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5271 const u8 *sa, const u8 *bssid,
5272 u8 category, const u8 *data, size_t len, int freq)
5273{
5274 if (wpa_s->global->p2p_disabled)
5275 return;
5276 if (wpa_s->global->p2p == NULL)
5277 return;
5278
5279 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5280 freq);
5281}
5282
5283
5284void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5285{
5286 if (wpa_s->global->p2p_disabled)
5287 return;
5288 if (wpa_s->global->p2p == NULL)
5289 return;
5290
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005291 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005292}
5293
5294
5295void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5296{
5297 p2p_group_deinit(wpa_s->p2p_group);
5298 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005299
5300 wpa_s->ap_configured_cb = NULL;
5301 wpa_s->ap_configured_cb_ctx = NULL;
5302 wpa_s->ap_configured_cb_data = NULL;
5303 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005304}
5305
5306
5307int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5308{
5309 wpa_s->p2p_long_listen = 0;
5310
5311 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5312 return wpa_drv_p2p_reject(wpa_s, addr);
5313
5314 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5315 return -1;
5316
5317 return p2p_reject(wpa_s->global->p2p, addr);
5318}
5319
5320
5321/* Invite to reinvoke a persistent group */
5322int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005323 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005324 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005325{
5326 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005327 u8 *bssid = NULL;
5328 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005329 int res;
5330
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005331 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005332 if (peer_addr)
5333 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5334 else
5335 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005336
Jouni Malinen31be0a42012-08-31 21:20:51 +03005337 wpa_s->p2p_persistent_go_freq = freq;
5338 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005339 if (ssid->mode == WPAS_MODE_P2P_GO) {
5340 role = P2P_INVITE_ROLE_GO;
5341 if (peer_addr == NULL) {
5342 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5343 "address in invitation command");
5344 return -1;
5345 }
5346 if (wpas_p2p_create_iface(wpa_s)) {
5347 if (wpas_p2p_add_group_interface(wpa_s,
5348 WPA_IF_P2P_GO) < 0) {
5349 wpa_printf(MSG_ERROR, "P2P: Failed to "
5350 "allocate a new interface for the "
5351 "group");
5352 return -1;
5353 }
5354 bssid = wpa_s->pending_interface_addr;
5355 } else
5356 bssid = wpa_s->own_addr;
5357 } else {
5358 role = P2P_INVITE_ROLE_CLIENT;
5359 peer_addr = ssid->bssid;
5360 }
5361 wpa_s->pending_invite_ssid_id = ssid->id;
5362
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005363 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5364 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005365 if (res)
5366 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005368 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5369 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5370 ssid->ssid, ssid->ssid_len,
5371 go_dev_addr, 1);
5372
5373 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5374 return -1;
5375
5376 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005377 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5378 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005379}
5380
5381
5382/* Invite to join an active group */
5383int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5384 const u8 *peer_addr, const u8 *go_dev_addr)
5385{
5386 struct wpa_global *global = wpa_s->global;
5387 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005388 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005390 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005391 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005392 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005393
Jouni Malinen31be0a42012-08-31 21:20:51 +03005394 wpa_s->p2p_persistent_go_freq = 0;
5395 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005396 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005398 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5399 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5400 break;
5401 }
5402 if (wpa_s == NULL) {
5403 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5404 return -1;
5405 }
5406
5407 ssid = wpa_s->current_ssid;
5408 if (ssid == NULL) {
5409 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5410 "invitation");
5411 return -1;
5412 }
5413
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005414 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005415 persistent = ssid->p2p_persistent_group &&
5416 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5417 ssid->ssid, ssid->ssid_len);
5418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005419 if (ssid->mode == WPAS_MODE_P2P_GO) {
5420 role = P2P_INVITE_ROLE_ACTIVE_GO;
5421 bssid = wpa_s->own_addr;
5422 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005423 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005424 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005425 } else {
5426 role = P2P_INVITE_ROLE_CLIENT;
5427 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5428 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5429 "invite to current group");
5430 return -1;
5431 }
5432 bssid = wpa_s->bssid;
5433 if (go_dev_addr == NULL &&
5434 !is_zero_ether_addr(wpa_s->go_dev_addr))
5435 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005436 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5437 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005438 }
5439 wpa_s->parent->pending_invite_ssid_id = -1;
5440
5441 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5442 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5443 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005444 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005445
5446 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5447 return -1;
5448
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005449 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5450 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005451 if (res)
5452 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005453 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005456 ssid->ssid, ssid->ssid_len, force_freq,
5457 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458}
5459
5460
5461void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5462{
5463 struct wpa_ssid *ssid = wpa_s->current_ssid;
5464 const char *ssid_txt;
5465 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005466 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005468 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005469
Dmitry Shmidt04949592012-07-19 12:16:46 -07005470 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5471 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5472 wpa_s->parent, NULL);
5473 }
5474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005475 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005476 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477
5478 wpa_s->show_group_started = 0;
5479
5480 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5481 os_memset(go_dev_addr, 0, ETH_ALEN);
5482 if (ssid->bssid_set)
5483 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5484 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5485 ssid->ssid_len);
5486 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5487
5488 if (wpa_s->global->p2p_group_formation == wpa_s)
5489 wpa_s->global->p2p_group_formation = NULL;
5490
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005491 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5492 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493 if (ssid->passphrase == NULL && ssid->psk_set) {
5494 char psk[65];
5495 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005496 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5497 "%s client ssid=\"%s\" freq=%d psk=%s "
5498 "go_dev_addr=" MACSTR "%s",
5499 wpa_s->ifname, ssid_txt, freq, psk,
5500 MAC2STR(go_dev_addr),
5501 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005503 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5504 "%s client ssid=\"%s\" freq=%d "
5505 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5506 wpa_s->ifname, ssid_txt, freq,
5507 ssid->passphrase ? ssid->passphrase : "",
5508 MAC2STR(go_dev_addr),
5509 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510 }
5511
5512 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005513 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5514 ssid, go_dev_addr);
5515 if (network_id < 0)
5516 network_id = ssid->id;
5517 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005518
5519done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005520 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521}
5522
5523
5524int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5525 u32 interval1, u32 duration2, u32 interval2)
5526{
5527 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5528 return -1;
5529 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5530 return -1;
5531
5532 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5533 wpa_s->current_ssid == NULL ||
5534 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5535 return -1;
5536
5537 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5538 wpa_s->own_addr, wpa_s->assoc_freq,
5539 duration1, interval1, duration2, interval2);
5540}
5541
5542
5543int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5544 unsigned int interval)
5545{
5546 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5547 return -1;
5548
5549 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5550 return -1;
5551
5552 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5553}
5554
5555
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005556static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5557{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005558 if (wpa_s->current_ssid == NULL) {
5559 /*
5560 * current_ssid can be cleared when P2P client interface gets
5561 * disconnected, so assume this interface was used as P2P
5562 * client.
5563 */
5564 return 1;
5565 }
5566 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005567 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5568}
5569
5570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005571static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5572{
5573 struct wpa_supplicant *wpa_s = eloop_ctx;
5574
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005575 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005576 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5577 "disabled");
5578 return;
5579 }
5580
Dmitry Shmidt04949592012-07-19 12:16:46 -07005581 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5582 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005583 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005584}
5585
5586
5587static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5588{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005589 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005590
Dmitry Shmidt04949592012-07-19 12:16:46 -07005591 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5592 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005594 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5595 return;
5596
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005597 timeout = wpa_s->conf->p2p_group_idle;
5598 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5599 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5600 timeout = P2P_MAX_CLIENT_IDLE;
5601
5602 if (timeout == 0)
5603 return;
5604
Dmitry Shmidt04949592012-07-19 12:16:46 -07005605 if (timeout < 0) {
5606 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5607 timeout = 0; /* special client mode no-timeout */
5608 else
5609 return;
5610 }
5611
5612 if (wpa_s->p2p_in_provisioning) {
5613 /*
5614 * Use the normal group formation timeout during the
5615 * provisioning phase to avoid terminating this process too
5616 * early due to group idle timeout.
5617 */
5618 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5619 "during provisioning");
5620 return;
5621 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305622
Dmitry Shmidt04949592012-07-19 12:16:46 -07005623 if (wpa_s->show_group_started) {
5624 /*
5625 * Use the normal group formation timeout between the end of
5626 * the provisioning phase and completion of 4-way handshake to
5627 * avoid terminating this process too early due to group idle
5628 * timeout.
5629 */
5630 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5631 "while waiting for initial 4-way handshake to "
5632 "complete");
5633 return;
5634 }
5635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005636 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005637 timeout);
5638 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5639 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005640}
5641
5642
Jouni Malinen2b89da82012-08-31 22:04:41 +03005643/* Returns 1 if the interface was removed */
5644int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5645 u16 reason_code, const u8 *ie, size_t ie_len,
5646 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005647{
5648 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005649 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005650 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005651 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005652
Dmitry Shmidt04949592012-07-19 12:16:46 -07005653 if (!locally_generated)
5654 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5655 ie_len);
5656
5657 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5658 wpa_s->current_ssid &&
5659 wpa_s->current_ssid->p2p_group &&
5660 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5661 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5662 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005663 if (wpas_p2p_group_delete(wpa_s,
5664 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5665 > 0)
5666 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005667 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005668
5669 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005670}
5671
5672
5673void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005674 u16 reason_code, const u8 *ie, size_t ie_len,
5675 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005676{
5677 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5678 return;
5679 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5680 return;
5681
Dmitry Shmidt04949592012-07-19 12:16:46 -07005682 if (!locally_generated)
5683 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5684 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005685}
5686
5687
5688void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5689{
5690 struct p2p_data *p2p = wpa_s->global->p2p;
5691
5692 if (p2p == NULL)
5693 return;
5694
5695 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5696 return;
5697
5698 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5699 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5700
5701 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5702 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5703
5704 if (wpa_s->wps &&
5705 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5706 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5707
5708 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5709 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5710
5711 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5712 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5713 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5714 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5715 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5716 }
5717
5718 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5719 p2p_set_sec_dev_types(p2p,
5720 (void *) wpa_s->conf->sec_device_type,
5721 wpa_s->conf->num_sec_device_types);
5722
5723 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5724 int i;
5725 p2p_remove_wps_vendor_extensions(p2p);
5726 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5727 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5728 continue;
5729 p2p_add_wps_vendor_extension(
5730 p2p, wpa_s->conf->wps_vendor_ext[i]);
5731 }
5732 }
5733
5734 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5735 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5736 char country[3];
5737 country[0] = wpa_s->conf->country[0];
5738 country[1] = wpa_s->conf->country[1];
5739 country[2] = 0x04;
5740 p2p_set_country(p2p, country);
5741 }
5742
5743 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5744 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5745 wpa_s->conf->p2p_ssid_postfix ?
5746 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5747 0);
5748 }
5749
5750 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5751 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005752
5753 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5754 u8 reg_class, channel;
5755 int ret;
5756 unsigned int r;
5757 if (wpa_s->conf->p2p_listen_reg_class &&
5758 wpa_s->conf->p2p_listen_channel) {
5759 reg_class = wpa_s->conf->p2p_listen_reg_class;
5760 channel = wpa_s->conf->p2p_listen_channel;
5761 } else {
5762 reg_class = 81;
5763 /*
5764 * Pick one of the social channels randomly as the
5765 * listen channel.
5766 */
5767 os_get_random((u8 *) &r, sizeof(r));
5768 channel = 1 + (r % 3) * 5;
5769 }
5770 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5771 if (ret)
5772 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5773 "failed: %d", ret);
5774 }
5775 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5776 u8 op_reg_class, op_channel, cfg_op_channel;
5777 int ret = 0;
5778 unsigned int r;
5779 if (wpa_s->conf->p2p_oper_reg_class &&
5780 wpa_s->conf->p2p_oper_channel) {
5781 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5782 op_channel = wpa_s->conf->p2p_oper_channel;
5783 cfg_op_channel = 1;
5784 } else {
5785 op_reg_class = 81;
5786 /*
5787 * Use random operation channel from (1, 6, 11)
5788 *if no other preference is indicated.
5789 */
5790 os_get_random((u8 *) &r, sizeof(r));
5791 op_channel = 1 + (r % 3) * 5;
5792 cfg_op_channel = 0;
5793 }
5794 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5795 cfg_op_channel);
5796 if (ret)
5797 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5798 "failed: %d", ret);
5799 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005800
5801 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5802 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5803 wpa_s->conf->p2p_pref_chan) < 0) {
5804 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5805 "update failed");
5806 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005807
5808 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
5809 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
5810 "update failed");
5811 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005813}
5814
5815
5816int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5817 int duration)
5818{
5819 if (!wpa_s->ap_iface)
5820 return -1;
5821 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5822 duration);
5823}
5824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005825
5826int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5827{
5828 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5829 return -1;
5830 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5831 return -1;
5832
5833 wpa_s->global->cross_connection = enabled;
5834 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5835
5836 if (!enabled) {
5837 struct wpa_supplicant *iface;
5838
5839 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5840 {
5841 if (iface->cross_connect_enabled == 0)
5842 continue;
5843
5844 iface->cross_connect_enabled = 0;
5845 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005846 wpa_msg_global(iface->parent, MSG_INFO,
5847 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5848 iface->ifname,
5849 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850 }
5851 }
5852
5853 return 0;
5854}
5855
5856
5857static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5858{
5859 struct wpa_supplicant *iface;
5860
5861 if (!uplink->global->cross_connection)
5862 return;
5863
5864 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5865 if (!iface->cross_connect_enabled)
5866 continue;
5867 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5868 0)
5869 continue;
5870 if (iface->ap_iface == NULL)
5871 continue;
5872 if (iface->cross_connect_in_use)
5873 continue;
5874
5875 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005876 wpa_msg_global(iface->parent, MSG_INFO,
5877 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5878 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005879 }
5880}
5881
5882
5883static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5884{
5885 struct wpa_supplicant *iface;
5886
5887 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5888 if (!iface->cross_connect_enabled)
5889 continue;
5890 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5891 0)
5892 continue;
5893 if (!iface->cross_connect_in_use)
5894 continue;
5895
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005896 wpa_msg_global(iface->parent, MSG_INFO,
5897 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5898 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005899 iface->cross_connect_in_use = 0;
5900 }
5901}
5902
5903
5904void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5905{
5906 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5907 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5908 wpa_s->cross_connect_disallowed)
5909 wpas_p2p_disable_cross_connect(wpa_s);
5910 else
5911 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005912 if (!wpa_s->ap_iface &&
5913 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5914 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005915}
5916
5917
5918void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5919{
5920 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005921 if (!wpa_s->ap_iface &&
5922 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5923 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005924 wpas_p2p_set_group_idle_timeout(wpa_s);
5925}
5926
5927
5928static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5929{
5930 struct wpa_supplicant *iface;
5931
5932 if (!wpa_s->global->cross_connection)
5933 return;
5934
5935 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5936 if (iface == wpa_s)
5937 continue;
5938 if (iface->drv_flags &
5939 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5940 continue;
5941 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5942 continue;
5943
5944 wpa_s->cross_connect_enabled = 1;
5945 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5946 sizeof(wpa_s->cross_connect_uplink));
5947 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5948 "%s to %s whenever uplink is available",
5949 wpa_s->ifname, wpa_s->cross_connect_uplink);
5950
5951 if (iface->ap_iface || iface->current_ssid == NULL ||
5952 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5953 iface->cross_connect_disallowed ||
5954 iface->wpa_state != WPA_COMPLETED)
5955 break;
5956
5957 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005958 wpa_msg_global(wpa_s->parent, MSG_INFO,
5959 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5960 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005961 break;
5962 }
5963}
5964
5965
5966int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5967{
5968 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5969 !wpa_s->p2p_in_provisioning)
5970 return 0; /* not P2P client operation */
5971
5972 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5973 "session overlap");
5974 if (wpa_s != wpa_s->parent)
5975 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005976 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005977 return 1;
5978}
5979
5980
5981void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5982{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005983 struct p2p_channels chan, cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984
5985 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5986 return;
5987
5988 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005989 os_memset(&cli_chan, 0, sizeof(cli_chan));
5990 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005991 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5992 "channel list");
5993 return;
5994 }
5995
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005996 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005997}
5998
5999
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006000static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6001 struct wpa_scan_results *scan_res)
6002{
6003 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6004}
6005
6006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006007int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6008{
6009 struct wpa_global *global = wpa_s->global;
6010 int found = 0;
6011 const u8 *peer;
6012
6013 if (global->p2p == NULL)
6014 return -1;
6015
6016 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6017
6018 if (wpa_s->pending_interface_name[0] &&
6019 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6020 found = 1;
6021
6022 peer = p2p_get_go_neg_peer(global->p2p);
6023 if (peer) {
6024 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6025 MACSTR, MAC2STR(peer));
6026 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006027 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006028 }
6029
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006030 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6031 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6032 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6033 found = 1;
6034 }
6035
6036 if (wpa_s->pending_pd_before_join) {
6037 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6038 wpa_s->pending_pd_before_join = 0;
6039 found = 1;
6040 }
6041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006042 wpas_p2p_stop_find(wpa_s);
6043
6044 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6045 if (wpa_s == global->p2p_group_formation &&
6046 (wpa_s->p2p_in_provisioning ||
6047 wpa_s->parent->pending_interface_type ==
6048 WPA_IF_P2P_CLIENT)) {
6049 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6050 "formation found - cancelling",
6051 wpa_s->ifname);
6052 found = 1;
6053 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6054 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006055 if (wpa_s->p2p_in_provisioning) {
6056 wpas_group_formation_completed(wpa_s, 0);
6057 break;
6058 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006059 wpas_p2p_group_delete(wpa_s,
6060 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006061 break;
6062 }
6063 }
6064
6065 if (!found) {
6066 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6067 return -1;
6068 }
6069
6070 return 0;
6071}
6072
6073
6074void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6075{
6076 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6077 return;
6078
6079 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6080 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006081 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006082}
6083
6084
6085void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6086 int freq_24, int freq_5, int freq_overall)
6087{
6088 struct p2p_data *p2p = wpa_s->global->p2p;
6089 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6090 return;
6091 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6092}
6093
6094
6095int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6096{
6097 u8 peer[ETH_ALEN];
6098 struct p2p_data *p2p = wpa_s->global->p2p;
6099
6100 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6101 return -1;
6102
6103 if (hwaddr_aton(addr, peer))
6104 return -1;
6105
6106 return p2p_unauthorize(p2p, peer);
6107}
6108
6109
6110/**
6111 * wpas_p2p_disconnect - Disconnect from a P2P Group
6112 * @wpa_s: Pointer to wpa_supplicant data
6113 * Returns: 0 on success, -1 on failure
6114 *
6115 * This can be used to disconnect from a group in which the local end is a P2P
6116 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6117 * virtual network interface was created for this group, that interface will be
6118 * removed. Otherwise, only the configured P2P group network will be removed
6119 * from the interface.
6120 */
6121int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6122{
6123
6124 if (wpa_s == NULL)
6125 return -1;
6126
Jouni Malinen2b89da82012-08-31 22:04:41 +03006127 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6128 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006129}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006130
6131
6132int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6133{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006134 int ret;
6135
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006136 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6137 return 0;
6138
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006139 ret = p2p_in_progress(wpa_s->global->p2p);
6140 if (ret == 0) {
6141 /*
6142 * Check whether there is an ongoing WPS provisioning step (or
6143 * other parts of group formation) on another interface since
6144 * p2p_in_progress() does not report this to avoid issues for
6145 * scans during such provisioning step.
6146 */
6147 if (wpa_s->global->p2p_group_formation &&
6148 wpa_s->global->p2p_group_formation != wpa_s) {
6149 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6150 "in group formation",
6151 wpa_s->global->p2p_group_formation->ifname);
6152 ret = 1;
6153 }
6154 }
6155
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006156 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6157 struct os_time now;
6158 os_get_time(&now);
6159 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
6160 P2P_MAX_INITIAL_CONN_WAIT_GO) {
6161 /* Wait for the first client has expired */
6162 wpa_s->global->p2p_go_wait_client.sec = 0;
6163 } else {
6164 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6165 ret = 1;
6166 }
6167 }
6168
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006169 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006170}
6171
6172
6173void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6174 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006175{
6176 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6177 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6178 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006179 /**
6180 * Remove the network by scheduling the group formation
6181 * timeout to happen immediately. The teardown code
6182 * needs to be scheduled to run asynch later so that we
6183 * don't delete data from under ourselves unexpectedly.
6184 * Calling wpas_p2p_group_formation_timeout directly
6185 * causes a series of crashes in WPS failure scenarios.
6186 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006187 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6188 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006189 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6190 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006191 }
6192}
6193
6194
6195struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006196 const u8 *addr, const u8 *ssid,
6197 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006198{
6199 struct wpa_ssid *s;
6200 size_t i;
6201
6202 for (s = wpa_s->conf->ssid; s; s = s->next) {
6203 if (s->disabled != 2)
6204 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006205 if (ssid &&
6206 (ssid_len != s->ssid_len ||
6207 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6208 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006209 if (addr == NULL) {
6210 if (s->mode == WPAS_MODE_P2P_GO)
6211 return s;
6212 continue;
6213 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006214 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6215 return s; /* peer is GO in the persistent group */
6216 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6217 continue;
6218 for (i = 0; i < s->num_p2p_clients; i++) {
6219 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6220 addr, ETH_ALEN) == 0)
6221 return s; /* peer is P2P client in persistent
6222 * group */
6223 }
6224 }
6225
6226 return NULL;
6227}
6228
6229
6230void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6231 const u8 *addr)
6232{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006233 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6234 wpa_s->parent, NULL) > 0) {
6235 /*
6236 * This can happen if WPS provisioning step is not terminated
6237 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6238 * peer was able to connect, there is no need to time out group
6239 * formation after this, though. In addition, this is used with
6240 * the initial connection wait on the GO as a separate formation
6241 * timeout and as such, expected to be hit after the initial WPS
6242 * provisioning step.
6243 */
6244 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6245 }
6246 if (!wpa_s->p2p_go_group_formation_completed) {
6247 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6248 wpa_s->p2p_go_group_formation_completed = 1;
6249 wpa_s->global->p2p_group_formation = NULL;
6250 wpa_s->p2p_in_provisioning = 0;
6251 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006252 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006253 if (addr == NULL)
6254 return;
6255 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6256}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006257
Dmitry Shmidt04949592012-07-19 12:16:46 -07006258
6259static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6260 int group_added)
6261{
6262 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006263 if (wpa_s->global->p2p_group_formation)
6264 group = wpa_s->global->p2p_group_formation;
6265 wpa_s = wpa_s->parent;
6266 offchannel_send_action_done(wpa_s);
6267 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006268 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006269 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6270 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6271 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6272 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6273 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006274 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006275 wpa_s->p2p_go_ht40,
6276 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006277}
6278
6279
6280int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6281{
6282 if (!wpa_s->p2p_fallback_to_go_neg ||
6283 wpa_s->p2p_in_provisioning <= 5)
6284 return 0;
6285
6286 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6287 return 0; /* peer operating as a GO */
6288
6289 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6290 "fallback to GO Negotiation");
6291 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6292
6293 return 1;
6294}
6295
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006296
6297unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6298{
6299 const char *rn, *rn2;
6300 struct wpa_supplicant *ifs;
6301
6302 if (wpa_s->wpa_state > WPA_SCANNING) {
6303 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6304 "concurrent operation",
6305 P2P_CONCURRENT_SEARCH_DELAY);
6306 return P2P_CONCURRENT_SEARCH_DELAY;
6307 }
6308
6309 if (!wpa_s->driver->get_radio_name)
6310 return 0;
6311 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
6312 if (rn == NULL || rn[0] == '\0')
6313 return 0;
6314
6315 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6316 if (ifs == wpa_s || !ifs->driver->get_radio_name)
6317 continue;
6318
6319 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
6320 if (!rn2 || os_strcmp(rn, rn2) != 0)
6321 continue;
6322 if (ifs->wpa_state > WPA_SCANNING) {
6323 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6324 "delay due to concurrent operation on "
6325 "interface %s",
6326 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6327 return P2P_CONCURRENT_SEARCH_DELAY;
6328 }
6329 }
6330
6331 return 0;
6332}
6333
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006334
6335void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6336{
6337 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6338 "pending anymore (sta_scan_pending=%d "
6339 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6340 wpa_s->global->p2p_cb_on_scan_complete);
6341 wpa_s->sta_scan_pending = 0;
6342
6343 if (!wpa_s->global->p2p_cb_on_scan_complete)
6344 return;
6345 wpa_s->global->p2p_cb_on_scan_complete = 0;
6346
6347 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6348 return;
6349
6350 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6351 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6352 "continued after successful connection");
6353 p2p_increase_search_delay(wpa_s->global->p2p,
6354 wpas_p2p_search_delay(wpa_s));
6355 }
6356}
6357
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006358
6359static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6360 struct wpa_ssid *s, const u8 *addr,
6361 int iface_addr)
6362{
6363 struct psk_list_entry *psk, *tmp;
6364 int changed = 0;
6365
6366 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6367 list) {
6368 if ((iface_addr && !psk->p2p &&
6369 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6370 (!iface_addr && psk->p2p &&
6371 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6372 wpa_dbg(wpa_s, MSG_DEBUG,
6373 "P2P: Remove persistent group PSK list entry for "
6374 MACSTR " p2p=%u",
6375 MAC2STR(psk->addr), psk->p2p);
6376 dl_list_del(&psk->list);
6377 os_free(psk);
6378 changed++;
6379 }
6380 }
6381
6382 return changed;
6383}
6384
6385
6386void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6387 const u8 *p2p_dev_addr,
6388 const u8 *psk, size_t psk_len)
6389{
6390 struct wpa_ssid *ssid = wpa_s->current_ssid;
6391 struct wpa_ssid *persistent;
6392 struct psk_list_entry *p;
6393
6394 if (psk_len != sizeof(p->psk))
6395 return;
6396
6397 if (p2p_dev_addr) {
6398 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6399 " p2p_dev_addr=" MACSTR,
6400 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6401 if (is_zero_ether_addr(p2p_dev_addr))
6402 p2p_dev_addr = NULL;
6403 } else {
6404 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6405 MAC2STR(mac_addr));
6406 }
6407
6408 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6409 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6410 /* To be added to persistent group once created */
6411 if (wpa_s->global->add_psk == NULL) {
6412 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6413 if (wpa_s->global->add_psk == NULL)
6414 return;
6415 }
6416 p = wpa_s->global->add_psk;
6417 if (p2p_dev_addr) {
6418 p->p2p = 1;
6419 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6420 } else {
6421 p->p2p = 0;
6422 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6423 }
6424 os_memcpy(p->psk, psk, psk_len);
6425 return;
6426 }
6427
6428 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6429 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6430 return;
6431 }
6432
6433 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6434 ssid->ssid_len);
6435 if (!persistent) {
6436 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6437 return;
6438 }
6439
6440 p = os_zalloc(sizeof(*p));
6441 if (p == NULL)
6442 return;
6443 if (p2p_dev_addr) {
6444 p->p2p = 1;
6445 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6446 } else {
6447 p->p2p = 0;
6448 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6449 }
6450 os_memcpy(p->psk, psk, psk_len);
6451
6452 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6453 struct psk_list_entry *last;
6454 last = dl_list_last(&persistent->psk_list,
6455 struct psk_list_entry, list);
6456 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6457 MACSTR " (p2p=%u) to make room for a new one",
6458 MAC2STR(last->addr), last->p2p);
6459 dl_list_del(&last->list);
6460 os_free(last);
6461 }
6462
6463 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6464 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6465 p2p_dev_addr == NULL);
6466 if (p2p_dev_addr) {
6467 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6468 MACSTR, MAC2STR(p2p_dev_addr));
6469 } else {
6470 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6471 MAC2STR(mac_addr));
6472 }
6473 dl_list_add(&persistent->psk_list, &p->list);
6474
6475#ifndef CONFIG_NO_CONFIG_WRITE
6476 if (wpa_s->parent->conf->update_config &&
6477 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6478 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6479#endif /* CONFIG_NO_CONFIG_WRITE */
6480}
6481
6482
6483static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6484 struct wpa_ssid *s, const u8 *addr,
6485 int iface_addr)
6486{
6487 int res;
6488
6489 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6490 if (res > 0) {
6491#ifndef CONFIG_NO_CONFIG_WRITE
6492 if (wpa_s->conf->update_config &&
6493 wpa_config_write(wpa_s->confname, wpa_s->conf))
6494 wpa_dbg(wpa_s, MSG_DEBUG,
6495 "P2P: Failed to update configuration");
6496#endif /* CONFIG_NO_CONFIG_WRITE */
6497 }
6498}
6499
6500
6501static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6502 const u8 *peer, int iface_addr)
6503{
6504 struct hostapd_data *hapd;
6505 struct hostapd_wpa_psk *psk, *prev, *rem;
6506 struct sta_info *sta;
6507
6508 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6509 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6510 return;
6511
6512 /* Remove per-station PSK entry */
6513 hapd = wpa_s->ap_iface->bss[0];
6514 prev = NULL;
6515 psk = hapd->conf->ssid.wpa_psk;
6516 while (psk) {
6517 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6518 (!iface_addr &&
6519 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6520 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6521 MACSTR " iface_addr=%d",
6522 MAC2STR(peer), iface_addr);
6523 if (prev)
6524 prev->next = psk->next;
6525 else
6526 hapd->conf->ssid.wpa_psk = psk->next;
6527 rem = psk;
6528 psk = psk->next;
6529 os_free(rem);
6530 } else {
6531 prev = psk;
6532 psk = psk->next;
6533 }
6534 }
6535
6536 /* Disconnect from group */
6537 if (iface_addr)
6538 sta = ap_get_sta(hapd, peer);
6539 else
6540 sta = ap_get_sta_p2p(hapd, peer);
6541 if (sta) {
6542 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6543 " (iface_addr=%d) from group",
6544 MAC2STR(peer), iface_addr);
6545 hostapd_drv_sta_deauth(hapd, sta->addr,
6546 WLAN_REASON_DEAUTH_LEAVING);
6547 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6548 }
6549}
6550
6551
6552void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6553 int iface_addr)
6554{
6555 struct wpa_ssid *s;
6556 struct wpa_supplicant *w;
6557
6558 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6559
6560 /* Remove from any persistent group */
6561 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6562 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6563 continue;
6564 if (!iface_addr)
6565 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6566 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6567 }
6568
6569 /* Remove from any operating group */
6570 for (w = wpa_s->global->ifaces; w; w = w->next)
6571 wpas_p2p_remove_client_go(w, peer, iface_addr);
6572}
6573
6574
6575static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6576{
6577 struct wpa_supplicant *wpa_s = eloop_ctx;
6578 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6579}
6580
6581
6582int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6583{
6584 struct wpa_ssid *ssid = wpa_s->current_ssid;
6585
6586 if (ssid == NULL || !ssid->p2p_group)
6587 return 0;
6588
6589 if (wpa_s->p2p_last_4way_hs_fail &&
6590 wpa_s->p2p_last_4way_hs_fail == ssid) {
6591 u8 go_dev_addr[ETH_ALEN];
6592 struct wpa_ssid *persistent;
6593
6594 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6595 ssid->ssid,
6596 ssid->ssid_len) <= 0) {
6597 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6598 goto disconnect;
6599 }
6600
6601 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6602 MACSTR, MAC2STR(go_dev_addr));
6603 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6604 ssid->ssid,
6605 ssid->ssid_len);
6606 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6607 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6608 goto disconnect;
6609 }
6610 wpa_msg_global(wpa_s->parent, MSG_INFO,
6611 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6612 persistent->id);
6613 disconnect:
6614 wpa_s->p2p_last_4way_hs_fail = NULL;
6615 /*
6616 * Remove the group from a timeout to avoid issues with caller
6617 * continuing to use the interface if this is on a P2P group
6618 * interface.
6619 */
6620 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6621 wpa_s, NULL);
6622 return 1;
6623 }
6624
6625 wpa_s->p2p_last_4way_hs_fail = ssid;
6626 return 0;
6627}
6628
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006629#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006630static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6631{
6632 struct wpa_supplicant *wpa_s = eloop_ctx;
6633
6634 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6635 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6636}
6637
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006638int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6639 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006640{
6641 struct wpa_supplicant *iface = NULL;
6642 struct p2p_data *p2p = wpa_s->global->p2p;
6643
6644 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006645 if ((iface->current_ssid) &&
6646 (iface->current_ssid->frequency != freq) &&
6647 ((iface->p2p_group_interface) ||
6648 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006649
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006650 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
6651 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
6652 /* Try to see whether we can move the GO. If it
6653 * is not possible, remove the GO interface
6654 */
6655 if (wpa_drv_switch_channel(iface, freq) == 0) {
6656 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
6657 iface->current_ssid->frequency = freq;
6658 continue;
6659 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006660 }
6661
6662 /* If GO cannot be moved or if the conflicting interface is a
6663 * P2P Client, remove the interface depending up on the connection
6664 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006665 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07006666 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006667 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006668 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006669 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006670 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
6671 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006672 /* If connection in progress is p2p connection, do not proceed for the connection */
6673 if (wpa_s == iface)
6674 return -1;
6675 else
6676 /* If connection in progress is STA connection, proceed for the connection */
6677 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006678 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006679 /* P2p connection has priority, disable the STA network*/
6680 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
6681 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6682 " id=%d", ssid->id);
6683 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6684 if (wpa_s == iface) {
6685 /* p2p connection is in progress, continue connecting...*/
6686 return 0;
6687 }
6688 else {
6689 /* STA connection is in progress, do not allow to continue */
6690 return -1;
6691 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006692 }
6693 }
6694 }
6695 return 0;
6696}
6697#endif