blob: a0a715710d20ada9718f1de09f68c76b91f35d3c [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "common/ieee802_11_common.h"
14#include "common/ieee802_11_defs.h"
15#include "common/wpa_ctrl.h"
16#include "wps/wps_i.h"
17#include "p2p/p2p.h"
18#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080019#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070020#include "ap/sta_info.h"
21#include "ap/ap_drv_ops.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070023#include "eapol_supp/eapol_supp_sm.h"
24#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "wpa_supplicant_i.h"
26#include "driver_i.h"
27#include "ap.h"
28#include "config_ssid.h"
29#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "notify.h"
31#include "scan.h"
32#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "wps_supplicant.h"
35#include "p2p_supplicant.h"
36
37
38/*
39 * How many times to try to scan to find the GO before giving up on join
40 * request.
41 */
42#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
43
Dmitry Shmidt04949592012-07-19 12:16:46 -070044#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
45
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080046#ifndef P2P_MAX_CLIENT_IDLE
47/*
48 * How many seconds to try to reconnect to the GO when connection in P2P client
49 * role has been lost.
50 */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070051#ifdef ANDROID_P2P
52#define P2P_MAX_CLIENT_IDLE 20
53#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054#define P2P_MAX_CLIENT_IDLE 10
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070055#endif /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056#endif /* P2P_MAX_CLIENT_IDLE */
57
Dmitry Shmidt04949592012-07-19 12:16:46 -070058#ifndef P2P_MAX_INITIAL_CONN_WAIT
59/*
60 * How many seconds to wait for initial 4-way handshake to get completed after
61 * WPS provisioning step.
62 */
63#define P2P_MAX_INITIAL_CONN_WAIT 10
64#endif /* P2P_MAX_INITIAL_CONN_WAIT */
65
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070066#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
67/*
68 * How many seconds to wait for initial 4-way handshake to get completed after
69 * WPS provisioning step on the GO. This controls the extra time the P2P
70 * operation is considered to be in progress (e.g., to delay other scans) after
71 * WPS provisioning has been completed on the GO during group formation.
72 */
73#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
74#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
75
Dmitry Shmidt56052862013-10-04 10:23:25 -070076#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
77/*
78 * How many seconds to wait for initial 4-way handshake to get completed after
79 * re-invocation of a persistent group on the GO when the client is expected
80 * to connect automatically (no user interaction).
81 */
82#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
83#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
84
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070085#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070086#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087#endif /* P2P_CONCURRENT_SEARCH_DELAY */
88
Dmitry Shmidt34af3062013-07-11 10:46:32 -070089#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
90
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070091enum p2p_group_removal_reason {
92 P2P_GROUP_REMOVAL_UNKNOWN,
93 P2P_GROUP_REMOVAL_SILENT,
94 P2P_GROUP_REMOVAL_FORMATION_FAILED,
95 P2P_GROUP_REMOVAL_REQUESTED,
96 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
97 P2P_GROUP_REMOVAL_UNAVAILABLE,
98 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070099 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700100#ifdef ANDROID_P2P
101 P2P_GROUP_REMOVAL_FREQ_CONFLICT
102#endif
103};
104
Jouni Malinendc7b7132012-09-14 12:53:47 -0700105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
107static struct wpa_supplicant *
108wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
109 int go);
110static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700111static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
113static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700114 const u8 *dev_addr, enum p2p_wps_method wps_method,
115 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
117static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
118static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
119static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800120static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
121 void *timeout_ctx);
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700122#ifdef ANDROID_P2P
123static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
124#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700125static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
126 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800127static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128
129
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700130/*
131 * Get the number of concurrent channels that the HW can operate, but that are
132 * currently not in use by any of the wpa_supplicant interfaces.
133 */
134static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
135{
136 int *freqs;
137 int num;
138
139 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
140 if (!freqs)
141 return -1;
142
143 num = get_shared_radio_freqs(wpa_s, freqs,
144 wpa_s->num_multichan_concurrent);
145 os_free(freqs);
146
147 return wpa_s->num_multichan_concurrent - num;
148}
149
150
151/*
152 * Get the frequencies that are currently in use by one or more of the virtual
153 * interfaces, and that are also valid for P2P operation.
154 */
155static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
156 int *p2p_freqs, unsigned int len)
157{
158 int *freqs;
159 unsigned int num, i, j;
160
161 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
162 if (!freqs)
163 return -1;
164
165 num = get_shared_radio_freqs(wpa_s, freqs,
166 wpa_s->num_multichan_concurrent);
167
168 os_memset(p2p_freqs, 0, sizeof(int) * len);
169
170 for (i = 0, j = 0; i < num && j < len; i++) {
171 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
172 p2p_freqs[j++] = freqs[i];
173 }
174
175 os_free(freqs);
176
177 return j;
178}
179
180
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700181static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
182 int freq)
183{
184 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
185 return;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700186 if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
187 wpas_p2p_num_unused_channels(wpa_s) > 0 &&
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700188 wpa_s->parent->conf->p2p_ignore_shared_freq)
189 freq = 0;
190 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
191}
192
193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
195 struct wpa_scan_results *scan_res)
196{
197 size_t i;
198
199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
200 return;
201
202 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
203 (int) scan_res->num);
204
205 for (i = 0; i < scan_res->num; i++) {
206 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800207 struct os_time time_tmp_age, entry_ts;
208 time_tmp_age.sec = bss->age / 1000;
209 time_tmp_age.usec = (bss->age % 1000) * 1000;
210 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800212 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213 (const u8 *) (bss + 1),
214 bss->ie_len) > 0)
215 break;
216 }
217
218 p2p_scan_res_handled(wpa_s->global->p2p);
219}
220
221
222static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
223 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700224 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225{
226 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700227 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 struct wpa_driver_scan_params params;
229 int ret;
230 struct wpabuf *wps_ie, *ies;
231 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800232 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233
234 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
235 return -1;
236
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700237 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
238 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700239 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700240 wpas_p2p_in_progress(wpa_s) == 2) {
241 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
242 "pending station mode scan to be "
243 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700244 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700245 wpa_supplicant_req_scan(ifs, 0, 0);
246 return 1;
247 }
248 }
249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 os_memset(&params, 0, sizeof(params));
251
252 /* P2P Wildcard SSID */
253 params.num_ssids = 1;
254 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
255 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
256
257 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700258 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
259 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260 num_req_dev_types, req_dev_types);
261 if (wps_ie == NULL)
262 return -1;
263
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800264 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
265 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 if (ies == NULL) {
267 wpabuf_free(wps_ie);
268 return -1;
269 }
270 wpabuf_put_buf(ies, wps_ie);
271 wpabuf_free(wps_ie);
272
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800273 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800275 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 params.extra_ies = wpabuf_head(ies);
277 params.extra_ies_len = wpabuf_len(ies);
278
279 switch (type) {
280 case P2P_SCAN_SOCIAL:
281 params.freqs = social_channels;
282 break;
283 case P2P_SCAN_FULL:
284 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 case P2P_SCAN_SOCIAL_PLUS_ONE:
286 social_channels[3] = freq;
287 params.freqs = social_channels;
288 break;
289 }
290
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292
293 wpabuf_free(ies);
294
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800295 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700296 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
297 if (ifs->scanning ||
298 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
299 wpa_s->global->p2p_cb_on_scan_complete = 1;
300 ret = 1;
301 break;
302 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700304 } else {
305 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700306 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700307 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 return ret;
310}
311
312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
314{
315 switch (p2p_group_interface) {
316 case P2P_GROUP_INTERFACE_PENDING:
317 return WPA_IF_P2P_GROUP;
318 case P2P_GROUP_INTERFACE_GO:
319 return WPA_IF_P2P_GO;
320 case P2P_GROUP_INTERFACE_CLIENT:
321 return WPA_IF_P2P_CLIENT;
322 }
323
324 return WPA_IF_P2P_GROUP;
325}
326
327
328static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
329 const u8 *ssid,
330 size_t ssid_len, int *go)
331{
332 struct wpa_ssid *s;
333
334 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
335 for (s = wpa_s->conf->ssid; s; s = s->next) {
336 if (s->disabled != 0 || !s->p2p_group ||
337 s->ssid_len != ssid_len ||
338 os_memcmp(ssid, s->ssid, ssid_len) != 0)
339 continue;
340 if (s->mode == WPAS_MODE_P2P_GO &&
341 s != wpa_s->current_ssid)
342 continue;
343 if (go)
344 *go = s->mode == WPAS_MODE_P2P_GO;
345 return wpa_s;
346 }
347 }
348
349 return NULL;
350}
351
352
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700353static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
354 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355{
356 struct wpa_ssid *ssid;
357 char *gtype;
358 const char *reason;
359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360 ssid = wpa_s->current_ssid;
361 if (ssid == NULL) {
362 /*
363 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700364 * pending P2P group interface waiting for provisioning or a
365 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366 */
367 ssid = wpa_s->conf->ssid;
368 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700369 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370 break;
371 ssid = ssid->next;
372 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300373 if (ssid == NULL &&
374 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
375 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700376 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
377 "not found");
378 return -1;
379 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 }
381 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
382 gtype = "GO";
383 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
384 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
385 wpa_s->reassociate = 0;
386 wpa_s->disconnected = 1;
387 wpa_supplicant_deauthenticate(wpa_s,
388 WLAN_REASON_DEAUTH_LEAVING);
389 gtype = "client";
390 } else
391 gtype = "GO";
392 if (wpa_s->cross_connect_in_use) {
393 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700394 wpa_msg_global(wpa_s->parent, MSG_INFO,
395 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
396 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700398 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 case P2P_GROUP_REMOVAL_REQUESTED:
400 reason = " reason=REQUESTED";
401 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700402 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
403 reason = " reason=FORMATION_FAILED";
404 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
406 reason = " reason=IDLE";
407 break;
408 case P2P_GROUP_REMOVAL_UNAVAILABLE:
409 reason = " reason=UNAVAILABLE";
410 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700411 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
412 reason = " reason=GO_ENDING_SESSION";
413 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700414 case P2P_GROUP_REMOVAL_PSK_FAILURE:
415 reason = " reason=PSK_FAILURE";
416 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700417#ifdef ANDROID_P2P
418 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
419 reason = " reason=FREQ_CONFLICT";
420 break;
421#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422 default:
423 reason = "";
424 break;
425 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700426 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700427 wpa_msg_global(wpa_s->parent, MSG_INFO,
428 P2P_EVENT_GROUP_REMOVED "%s %s%s",
429 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700430 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700432#ifdef ANDROID_P2P
433 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
434#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700435 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
436 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800437 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700438 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800439 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
440 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700441 wpa_s->p2p_in_provisioning = 0;
442 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700443
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700444 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
446
447 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
448 struct wpa_global *global;
449 char *ifname;
450 enum wpa_driver_if_type type;
451 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
452 wpa_s->ifname);
453 global = wpa_s->global;
454 ifname = os_strdup(wpa_s->ifname);
455 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700456 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 wpa_s = global->ifaces;
458 if (wpa_s && ifname)
459 wpa_drv_if_remove(wpa_s, type, ifname);
460 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300461 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 }
463
464 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
465 if (ssid && (ssid->p2p_group ||
466 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
467 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
468 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700469 if (ssid == wpa_s->current_ssid) {
470 wpa_sm_set_config(wpa_s->wpa, NULL);
471 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700473 }
474 /*
475 * Networks objects created during any P2P activities are not
476 * exposed out as they might/will confuse certain non-P2P aware
477 * applications since these network objects won't behave like
478 * regular ones.
479 *
480 * Likewise, we don't send out network removed signals for such
481 * network objects.
482 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 wpa_config_remove_network(wpa_s->conf, id);
484 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800485 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700486 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 } else {
488 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
489 "found");
490 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700491 if (wpa_s->ap_iface)
492 wpa_supplicant_ap_deinit(wpa_s);
493 else
494 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700495
496 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497}
498
499
500static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
501 u8 *go_dev_addr,
502 const u8 *ssid, size_t ssid_len)
503{
504 struct wpa_bss *bss;
505 const u8 *bssid;
506 struct wpabuf *p2p;
507 u8 group_capab;
508 const u8 *addr;
509
510 if (wpa_s->go_params)
511 bssid = wpa_s->go_params->peer_interface_addr;
512 else
513 bssid = wpa_s->bssid;
514
515 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
516 if (bss == NULL) {
517 u8 iface_addr[ETH_ALEN];
518 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
519 iface_addr) == 0)
520 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
521 }
522 if (bss == NULL) {
523 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
524 "group is persistent - BSS " MACSTR " not found",
525 MAC2STR(bssid));
526 return 0;
527 }
528
529 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
530 if (p2p == NULL) {
531 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
532 "group is persistent - BSS " MACSTR
533 " did not include P2P IE", MAC2STR(bssid));
534 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
535 (u8 *) (bss + 1), bss->ie_len);
536 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
537 ((u8 *) bss + 1) + bss->ie_len,
538 bss->beacon_ie_len);
539 return 0;
540 }
541
542 group_capab = p2p_get_group_capab(p2p);
543 addr = p2p_get_go_dev_addr(p2p);
544 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
545 "group_capab=0x%x", group_capab);
546 if (addr) {
547 os_memcpy(go_dev_addr, addr, ETH_ALEN);
548 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
549 MAC2STR(addr));
550 } else
551 os_memset(go_dev_addr, 0, ETH_ALEN);
552 wpabuf_free(p2p);
553
554 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
555 "go_dev_addr=" MACSTR,
556 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
557
558 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
559}
560
561
Jouni Malinen75ecf522011-06-27 15:19:46 -0700562static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
563 struct wpa_ssid *ssid,
564 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700565{
566 struct wpa_ssid *s;
567 int changed = 0;
568
569 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
570 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
571 for (s = wpa_s->conf->ssid; s; s = s->next) {
572 if (s->disabled == 2 &&
573 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
574 s->ssid_len == ssid->ssid_len &&
575 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
576 break;
577 }
578
579 if (s) {
580 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
581 "entry");
582 if (ssid->passphrase && !s->passphrase)
583 changed = 1;
584 else if (ssid->passphrase && s->passphrase &&
585 os_strcmp(ssid->passphrase, s->passphrase) != 0)
586 changed = 1;
587 } else {
588 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
589 "entry");
590 changed = 1;
591 s = wpa_config_add_network(wpa_s->conf);
592 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700593 return -1;
594
595 /*
596 * Instead of network_added we emit persistent_group_added
597 * notification. Also to keep the defense checks in
598 * persistent_group obj registration method, we set the
599 * relevant flags in s to designate it as a persistent group.
600 */
601 s->p2p_group = 1;
602 s->p2p_persistent_group = 1;
603 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 wpa_config_set_network_defaults(s);
605 }
606
607 s->p2p_group = 1;
608 s->p2p_persistent_group = 1;
609 s->disabled = 2;
610 s->bssid_set = 1;
611 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
612 s->mode = ssid->mode;
613 s->auth_alg = WPA_AUTH_ALG_OPEN;
614 s->key_mgmt = WPA_KEY_MGMT_PSK;
615 s->proto = WPA_PROTO_RSN;
616 s->pairwise_cipher = WPA_CIPHER_CCMP;
617 s->export_keys = 1;
618 if (ssid->passphrase) {
619 os_free(s->passphrase);
620 s->passphrase = os_strdup(ssid->passphrase);
621 }
622 if (ssid->psk_set) {
623 s->psk_set = 1;
624 os_memcpy(s->psk, ssid->psk, 32);
625 }
626 if (s->passphrase && !s->psk_set)
627 wpa_config_update_psk(s);
628 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
629 os_free(s->ssid);
630 s->ssid = os_malloc(ssid->ssid_len);
631 }
632 if (s->ssid) {
633 s->ssid_len = ssid->ssid_len;
634 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
635 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700636 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
637 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
638 wpa_s->global->add_psk = NULL;
639 changed = 1;
640 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641
642#ifndef CONFIG_NO_CONFIG_WRITE
643 if (changed && wpa_s->conf->update_config &&
644 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
645 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
646 }
647#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700648
649 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650}
651
652
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800653static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
654 const u8 *addr)
655{
656 struct wpa_ssid *ssid, *s;
657 u8 *n;
658 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700659 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800660
661 ssid = wpa_s->current_ssid;
662 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
663 !ssid->p2p_persistent_group)
664 return;
665
666 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
667 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
668 continue;
669
670 if (s->ssid_len == ssid->ssid_len &&
671 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
672 break;
673 }
674
675 if (s == NULL)
676 return;
677
678 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
679 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700680 ETH_ALEN) != 0)
681 continue;
682
683 if (i == s->num_p2p_clients - 1)
684 return; /* already the most recent entry */
685
686 /* move the entry to mark it most recent */
687 os_memmove(s->p2p_client_list + i * ETH_ALEN,
688 s->p2p_client_list + (i + 1) * ETH_ALEN,
689 (s->num_p2p_clients - i - 1) * ETH_ALEN);
690 os_memcpy(s->p2p_client_list +
691 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
692 found = 1;
693 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800694 }
695
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700696 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
697 n = os_realloc_array(s->p2p_client_list,
698 s->num_p2p_clients + 1, ETH_ALEN);
699 if (n == NULL)
700 return;
701 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
702 s->p2p_client_list = n;
703 s->num_p2p_clients++;
704 } else if (!found) {
705 /* Not enough room for an additional entry - drop the oldest
706 * entry */
707 os_memmove(s->p2p_client_list,
708 s->p2p_client_list + ETH_ALEN,
709 (s->num_p2p_clients - 1) * ETH_ALEN);
710 os_memcpy(s->p2p_client_list +
711 (s->num_p2p_clients - 1) * ETH_ALEN,
712 addr, ETH_ALEN);
713 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800714
715#ifndef CONFIG_NO_CONFIG_WRITE
716 if (wpa_s->parent->conf->update_config &&
717 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
718 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
719#endif /* CONFIG_NO_CONFIG_WRITE */
720}
721
722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
724 int success)
725{
726 struct wpa_ssid *ssid;
727 const char *ssid_txt;
728 int client;
729 int persistent;
730 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700731 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732
733 /*
734 * This callback is likely called for the main interface. Update wpa_s
735 * to use the group interface if a new interface was created for the
736 * group.
737 */
738 if (wpa_s->global->p2p_group_formation)
739 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700740 if (wpa_s->p2p_go_group_formation_completed) {
741 wpa_s->global->p2p_group_formation = NULL;
742 wpa_s->p2p_in_provisioning = 0;
743 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744
745 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700746 wpa_msg_global(wpa_s->parent, MSG_INFO,
747 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700748 wpas_p2p_group_delete(wpa_s,
749 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750 return;
751 }
752
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700753 wpa_msg_global(wpa_s->parent, MSG_INFO,
754 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755
756 ssid = wpa_s->current_ssid;
757 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
758 ssid->mode = WPAS_MODE_P2P_GO;
759 p2p_group_notif_formation_done(wpa_s->p2p_group);
760 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
761 }
762
763 persistent = 0;
764 if (ssid) {
765 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
766 client = ssid->mode == WPAS_MODE_INFRA;
767 if (ssid->mode == WPAS_MODE_P2P_GO) {
768 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700769 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
770 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 } else
772 persistent = wpas_p2p_persistent_group(wpa_s,
773 go_dev_addr,
774 ssid->ssid,
775 ssid->ssid_len);
776 } else {
777 ssid_txt = "";
778 client = wpa_s->p2p_group_interface ==
779 P2P_GROUP_INTERFACE_CLIENT;
780 os_memset(go_dev_addr, 0, ETH_ALEN);
781 }
782
783 wpa_s->show_group_started = 0;
784 if (client) {
785 /*
786 * Indicate event only after successfully completed 4-way
787 * handshake, i.e., when the interface is ready for data
788 * packets.
789 */
790 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700791#ifdef ANDROID_P2P
792 /* For client Second phase of Group formation (4-way handshake) can be still pending
793 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700794 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 -0700795 wpa_s->global->p2p_group_formation = wpa_s;
796#endif
797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700798 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
799 char psk[65];
800 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700801 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
802 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
803 MACSTR "%s",
804 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
805 MAC2STR(go_dev_addr),
806 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807 wpas_p2p_cross_connect_setup(wpa_s);
808 wpas_p2p_set_group_idle_timeout(wpa_s);
809 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700810 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
811 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
812 "go_dev_addr=" MACSTR "%s",
813 wpa_s->ifname, ssid_txt,
814 ssid ? ssid->frequency : 0,
815 ssid && ssid->passphrase ? ssid->passphrase : "",
816 MAC2STR(go_dev_addr),
817 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818 wpas_p2p_cross_connect_setup(wpa_s);
819 wpas_p2p_set_group_idle_timeout(wpa_s);
820 }
821
822 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700823 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
824 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700825 else {
826 os_free(wpa_s->global->add_psk);
827 wpa_s->global->add_psk = NULL;
828 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800829 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700830 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700831 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700832 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700833 os_get_time(&wpa_s->global->p2p_go_wait_client);
834 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835}
836
837
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800838static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
839 unsigned int freq,
840 const u8 *dst, const u8 *src,
841 const u8 *bssid,
842 const u8 *data, size_t data_len,
843 enum offchannel_send_action_result
844 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700845{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800846 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700848 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
849 return;
850 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
851 return;
852
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800853 switch (result) {
854 case OFFCHANNEL_SEND_ACTION_SUCCESS:
855 res = P2P_SEND_ACTION_SUCCESS;
856 break;
857 case OFFCHANNEL_SEND_ACTION_NO_ACK:
858 res = P2P_SEND_ACTION_NO_ACK;
859 break;
860 case OFFCHANNEL_SEND_ACTION_FAILED:
861 res = P2P_SEND_ACTION_FAILED;
862 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863 }
864
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800865 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800867 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
868 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800869 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800870 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
871 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800873 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
874 "during p2p_connect-auto");
875 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
876 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 }
878}
879
880
881static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
882 const u8 *src, const u8 *bssid, const u8 *buf,
883 size_t len, unsigned int wait_time)
884{
885 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800886 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
887 wait_time,
888 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889}
890
891
892static void wpas_send_action_done(void *ctx)
893{
894 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800895 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896}
897
898
899static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
900 struct p2p_go_neg_results *params)
901{
902 if (wpa_s->go_params == NULL) {
903 wpa_s->go_params = os_malloc(sizeof(*params));
904 if (wpa_s->go_params == NULL)
905 return -1;
906 }
907 os_memcpy(wpa_s->go_params, params, sizeof(*params));
908 return 0;
909}
910
911
912static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
913 struct p2p_go_neg_results *res)
914{
915 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
916 MAC2STR(res->peer_interface_addr));
917 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
918 res->ssid, res->ssid_len);
919 wpa_supplicant_ap_deinit(wpa_s);
920 wpas_copy_go_neg_results(wpa_s, res);
921 if (res->wps_method == WPS_PBC)
922 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
923 else {
924 u16 dev_pw_id = DEV_PW_DEFAULT;
925 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
926 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
927 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
928 wpa_s->p2p_pin, 1, dev_pw_id);
929 }
930}
931
932
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700933static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
934 struct wpa_ssid *ssid)
935{
936 struct wpa_ssid *persistent;
937 struct psk_list_entry *psk;
938 struct hostapd_data *hapd;
939
940 if (!wpa_s->ap_iface)
941 return;
942
943 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
944 ssid->ssid_len);
945 if (persistent == NULL)
946 return;
947
948 hapd = wpa_s->ap_iface->bss[0];
949
950 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
951 list) {
952 struct hostapd_wpa_psk *hpsk;
953
954 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
955 MACSTR " psk=%d",
956 MAC2STR(psk->addr), psk->p2p);
957 hpsk = os_zalloc(sizeof(*hpsk));
958 if (hpsk == NULL)
959 break;
960 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
961 if (psk->p2p)
962 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
963 else
964 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
965 hpsk->next = hapd->conf->ssid.wpa_psk;
966 hapd->conf->ssid.wpa_psk = hpsk;
967 }
968}
969
970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971static void p2p_go_configured(void *ctx, void *data)
972{
973 struct wpa_supplicant *wpa_s = ctx;
974 struct p2p_go_neg_results *params = data;
975 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700976 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977
978 ssid = wpa_s->current_ssid;
979 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
980 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
981 if (wpa_s->global->p2p_group_formation == wpa_s)
982 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800983 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700984 wpa_msg_global(wpa_s->parent, MSG_INFO,
985 P2P_EVENT_GROUP_STARTED
986 "%s GO ssid=\"%s\" freq=%d "
987 "passphrase=\"%s\" go_dev_addr=" MACSTR
988 "%s", wpa_s->ifname,
989 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
990 ssid->frequency, params->passphrase,
991 MAC2STR(wpa_s->global->p2p_dev_addr),
992 params->persistent_group ?
993 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800994 } else {
995 char psk[65];
996 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
997 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700998 wpa_msg_global(wpa_s->parent, MSG_INFO,
999 P2P_EVENT_GROUP_STARTED
1000 "%s GO ssid=\"%s\" freq=%d psk=%s "
1001 "go_dev_addr=" MACSTR "%s",
1002 wpa_s->ifname,
1003 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1004 ssid->frequency, psk,
1005 MAC2STR(wpa_s->global->p2p_dev_addr),
1006 params->persistent_group ?
1007 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001008 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001009
Dmitry Shmidt56052862013-10-04 10:23:25 -07001010 os_get_time(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001011 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001012 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001014 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001015 wpas_p2p_add_psk_list(wpa_s, ssid);
1016 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001017 if (network_id < 0)
1018 network_id = ssid->id;
1019 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 wpas_p2p_cross_connect_setup(wpa_s);
1021 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001022
1023 if (wpa_s->p2p_first_connection_timeout) {
1024 wpa_dbg(wpa_s, MSG_DEBUG,
1025 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1026 wpa_s->p2p_first_connection_timeout);
1027 wpa_s->p2p_go_group_formation_completed = 0;
1028 wpa_s->global->p2p_group_formation = wpa_s;
1029 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1030 wpa_s->parent, NULL);
1031 eloop_register_timeout(
1032 wpa_s->p2p_first_connection_timeout, 0,
1033 wpas_p2p_group_formation_timeout,
1034 wpa_s->parent, NULL);
1035 }
1036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001037 return;
1038 }
1039
1040 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1041 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1042 params->peer_interface_addr)) {
1043 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1044 "filtering");
1045 return;
1046 }
1047 if (params->wps_method == WPS_PBC)
1048 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001049 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 else if (wpa_s->p2p_pin[0])
1051 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001052 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 os_free(wpa_s->go_params);
1054 wpa_s->go_params = NULL;
1055}
1056
1057
1058static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1059 struct p2p_go_neg_results *params,
1060 int group_formation)
1061{
1062 struct wpa_ssid *ssid;
1063
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001064 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1065 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1066 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1067 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001069 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070
1071 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001072 if (ssid == NULL) {
1073 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001075 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001077 wpa_s->show_group_started = 0;
1078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079 wpa_config_set_network_defaults(ssid);
1080 ssid->temporary = 1;
1081 ssid->p2p_group = 1;
1082 ssid->p2p_persistent_group = params->persistent_group;
1083 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1084 WPAS_MODE_P2P_GO;
1085 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001086 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 ssid->ssid = os_zalloc(params->ssid_len + 1);
1088 if (ssid->ssid) {
1089 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1090 ssid->ssid_len = params->ssid_len;
1091 }
1092 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1093 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1094 ssid->proto = WPA_PROTO_RSN;
1095 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001096 if (os_strlen(params->passphrase) > 0) {
1097 ssid->passphrase = os_strdup(params->passphrase);
1098 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001099 wpa_msg_global(wpa_s, MSG_ERROR,
1100 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001101 wpa_config_remove_network(wpa_s->conf, ssid->id);
1102 return;
1103 }
1104 } else
1105 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001106 ssid->psk_set = params->psk_set;
1107 if (ssid->psk_set)
1108 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001109 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001110 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001111 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112
1113 wpa_s->ap_configured_cb = p2p_go_configured;
1114 wpa_s->ap_configured_cb_ctx = wpa_s;
1115 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001116 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 wpa_s->reassociate = 1;
1118 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001119 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1120 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121 wpa_supplicant_req_scan(wpa_s, 0, 0);
1122}
1123
1124
1125static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1126 const struct wpa_supplicant *src)
1127{
1128 struct wpa_config *d;
1129 const struct wpa_config *s;
1130
1131 d = dst->conf;
1132 s = src->conf;
1133
1134#define C(n) if (s->n) d->n = os_strdup(s->n)
1135 C(device_name);
1136 C(manufacturer);
1137 C(model_name);
1138 C(model_number);
1139 C(serial_number);
1140 C(config_methods);
1141#undef C
1142
1143 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1144 os_memcpy(d->sec_device_type, s->sec_device_type,
1145 sizeof(d->sec_device_type));
1146 d->num_sec_device_types = s->num_sec_device_types;
1147
1148 d->p2p_group_idle = s->p2p_group_idle;
1149 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001150 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001151 d->max_num_sta = s->max_num_sta;
1152 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001153 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001154 d->beacon_int = s->beacon_int;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001155 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156}
1157
1158
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001159static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1160 char *ifname, size_t len)
1161{
1162 char *ifname_ptr = wpa_s->ifname;
1163
1164 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1165 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1166 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1167 }
1168
1169 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1170 if (os_strlen(ifname) >= IFNAMSIZ &&
1171 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1172 /* Try to avoid going over the IFNAMSIZ length limit */
1173 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1174 wpa_s->p2p_group_idx);
1175 }
1176}
1177
1178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1180 enum wpa_driver_if_type type)
1181{
1182 char ifname[120], force_ifname[120];
1183
1184 if (wpa_s->pending_interface_name[0]) {
1185 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1186 "- skip creation of a new one");
1187 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1188 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1189 "unknown?! ifname='%s'",
1190 wpa_s->pending_interface_name);
1191 return -1;
1192 }
1193 return 0;
1194 }
1195
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001196 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 force_ifname[0] = '\0';
1198
1199 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1200 ifname);
1201 wpa_s->p2p_group_idx++;
1202
1203 wpa_s->pending_interface_type = type;
1204 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1205 wpa_s->pending_interface_addr, NULL) < 0) {
1206 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1207 "interface");
1208 return -1;
1209 }
1210
1211 if (force_ifname[0]) {
1212 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1213 force_ifname);
1214 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1215 sizeof(wpa_s->pending_interface_name));
1216 } else
1217 os_strlcpy(wpa_s->pending_interface_name, ifname,
1218 sizeof(wpa_s->pending_interface_name));
1219 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1220 MACSTR, wpa_s->pending_interface_name,
1221 MAC2STR(wpa_s->pending_interface_addr));
1222
1223 return 0;
1224}
1225
1226
1227static void wpas_p2p_remove_pending_group_interface(
1228 struct wpa_supplicant *wpa_s)
1229{
1230 if (!wpa_s->pending_interface_name[0] ||
1231 is_zero_ether_addr(wpa_s->pending_interface_addr))
1232 return; /* No pending virtual interface */
1233
1234 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1235 wpa_s->pending_interface_name);
1236 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1237 wpa_s->pending_interface_name);
1238 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1239 wpa_s->pending_interface_name[0] = '\0';
1240}
1241
1242
1243static struct wpa_supplicant *
1244wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1245{
1246 struct wpa_interface iface;
1247 struct wpa_supplicant *group_wpa_s;
1248
1249 if (!wpa_s->pending_interface_name[0]) {
1250 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1251 if (!wpas_p2p_create_iface(wpa_s))
1252 return NULL;
1253 /*
1254 * Something has forced us to remove the pending interface; try
1255 * to create a new one and hope for the best that we will get
1256 * the same local address.
1257 */
1258 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1259 WPA_IF_P2P_CLIENT) < 0)
1260 return NULL;
1261 }
1262
1263 os_memset(&iface, 0, sizeof(iface));
1264 iface.ifname = wpa_s->pending_interface_name;
1265 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001266 if (wpa_s->conf->ctrl_interface == NULL &&
1267 wpa_s->parent != wpa_s &&
1268 wpa_s->p2p_mgmt &&
1269 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1270 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1271 else
1272 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001273 iface.driver_param = wpa_s->conf->driver_param;
1274 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1275 if (group_wpa_s == NULL) {
1276 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1277 "wpa_supplicant interface");
1278 return NULL;
1279 }
1280 wpa_s->pending_interface_name[0] = '\0';
1281 group_wpa_s->parent = wpa_s;
1282 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1283 P2P_GROUP_INTERFACE_CLIENT;
1284 wpa_s->global->p2p_group_formation = group_wpa_s;
1285
1286 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1287
1288 return group_wpa_s;
1289}
1290
1291
1292static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1293 void *timeout_ctx)
1294{
1295 struct wpa_supplicant *wpa_s = eloop_ctx;
1296 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001297 wpas_p2p_group_formation_failed(wpa_s);
1298}
1299
1300
1301void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1302{
1303 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1304 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 if (wpa_s->global->p2p)
1306 p2p_group_formation_failed(wpa_s->global->p2p);
1307 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1308 wpa_drv_p2p_group_formation_failed(wpa_s);
1309 wpas_group_formation_completed(wpa_s, 0);
1310}
1311
1312
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001313void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1314{
1315 if (wpa_s->global->p2p_group_formation != wpa_s)
1316 return;
1317 /* Speed up group formation timeout since this cannot succeed */
1318 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1319 wpa_s->parent, NULL);
1320 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1321 wpa_s->parent, NULL);
1322}
1323
1324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1326{
1327 struct wpa_supplicant *wpa_s = ctx;
1328
1329 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1330 wpa_drv_cancel_remain_on_channel(wpa_s);
1331 wpa_s->off_channel_freq = 0;
1332 wpa_s->roc_waiting_drv_freq = 0;
1333 }
1334
1335 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001336 wpa_msg_global(wpa_s, MSG_INFO,
1337 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1338 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001339 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 wpas_p2p_remove_pending_group_interface(wpa_s);
1341 return;
1342 }
1343
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001344 if (wpa_s->p2p_go_ht40)
1345 res->ht40 = 1;
1346
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001347 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1348 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1349 " wps_method=%s",
1350 res->role_go ? "GO" : "client", res->freq, res->ht40,
1351 MAC2STR(res->peer_device_addr),
1352 MAC2STR(res->peer_interface_addr),
1353 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001354 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355
Dmitry Shmidt04949592012-07-19 12:16:46 -07001356 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1357 struct wpa_ssid *ssid;
1358 ssid = wpa_config_get_network(wpa_s->conf,
1359 wpa_s->p2p_persistent_id);
1360 if (ssid && ssid->disabled == 2 &&
1361 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1362 size_t len = os_strlen(ssid->passphrase);
1363 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1364 "on requested persistent group");
1365 os_memcpy(res->passphrase, ssid->passphrase, len);
1366 res->passphrase[len] = '\0';
1367 }
1368 }
1369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 if (wpa_s->create_p2p_iface) {
1371 struct wpa_supplicant *group_wpa_s =
1372 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1373 if (group_wpa_s == NULL) {
1374 wpas_p2p_remove_pending_group_interface(wpa_s);
1375 return;
1376 }
1377 if (group_wpa_s != wpa_s) {
1378 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1379 sizeof(group_wpa_s->p2p_pin));
1380 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1381 }
1382 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1383 wpa_s->pending_interface_name[0] = '\0';
1384 group_wpa_s->p2p_in_provisioning = 1;
1385
1386 if (res->role_go)
1387 wpas_start_wps_go(group_wpa_s, res, 1);
1388 else
1389 wpas_start_wps_enrollee(group_wpa_s, res);
1390 } else {
1391 wpa_s->p2p_in_provisioning = 1;
1392 wpa_s->global->p2p_group_formation = wpa_s;
1393
1394 if (res->role_go)
1395 wpas_start_wps_go(wpa_s, res, 1);
1396 else
1397 wpas_start_wps_enrollee(ctx, res);
1398 }
1399
1400 wpa_s->p2p_long_listen = 0;
1401 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1402
1403 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1404 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1405 (res->peer_config_timeout % 100) * 10000,
1406 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1407}
1408
1409
1410void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1411{
1412 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001413 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1414 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415
1416 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1417}
1418
1419
1420void wpas_dev_found(void *ctx, const u8 *addr,
1421 const struct p2p_peer_info *info,
1422 int new_device)
1423{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001424#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 struct wpa_supplicant *wpa_s = ctx;
1426 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001427#define WFD_DEV_INFO_SIZE 9
1428 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001429 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001430#ifdef CONFIG_WIFI_DISPLAY
1431 if (info->wfd_subelems) {
1432 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1433 wpabuf_head(info->wfd_subelems),
1434 WFD_DEV_INFO_SIZE);
1435 }
1436#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001437 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1438 " p2p_dev_addr=" MACSTR
1439 " pri_dev_type=%s name='%s' config_methods=0x%x "
1440 "dev_capab=0x%x group_capab=0x%x%s%s",
1441 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1442 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1443 sizeof(devtype)),
1444 info->device_name, info->config_methods,
1445 info->dev_capab, info->group_capab,
1446 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001447#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448
1449 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1450}
1451
1452
1453static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1454{
1455 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001456
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001457 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1458 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1461}
1462
1463
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001464static void wpas_find_stopped(void *ctx)
1465{
1466 struct wpa_supplicant *wpa_s = ctx;
1467 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1468}
1469
1470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471static int wpas_start_listen(void *ctx, unsigned int freq,
1472 unsigned int duration,
1473 const struct wpabuf *probe_resp_ie)
1474{
1475 struct wpa_supplicant *wpa_s = ctx;
1476
1477 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1478
1479 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1480 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1481 "report received Probe Request frames");
1482 return -1;
1483 }
1484
1485 wpa_s->pending_listen_freq = freq;
1486 wpa_s->pending_listen_duration = duration;
1487
1488 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1489 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1490 "to remain on channel (%u MHz) for Listen "
1491 "state", freq);
1492 wpa_s->pending_listen_freq = 0;
1493 return -1;
1494 }
1495 wpa_s->off_channel_freq = 0;
1496 wpa_s->roc_waiting_drv_freq = freq;
1497
1498 return 0;
1499}
1500
1501
1502static void wpas_stop_listen(void *ctx)
1503{
1504 struct wpa_supplicant *wpa_s = ctx;
1505 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1506 wpa_drv_cancel_remain_on_channel(wpa_s);
1507 wpa_s->off_channel_freq = 0;
1508 wpa_s->roc_waiting_drv_freq = 0;
1509 }
1510 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1511 wpa_drv_probe_req_report(wpa_s, 0);
1512}
1513
1514
1515static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1516{
1517 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001518 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001519}
1520
1521
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001522/*
1523 * DNS Header section is used only to calculate compression pointers, so the
1524 * contents of this data does not matter, but the length needs to be reserved
1525 * in the virtual packet.
1526 */
1527#define DNS_HEADER_LEN 12
1528
1529/*
1530 * 27-octet in-memory packet from P2P specification containing two implied
1531 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1532 */
1533#define P2P_SD_IN_MEMORY_LEN 27
1534
1535static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1536 u8 **spos, const u8 *end)
1537{
1538 while (*spos < end) {
1539 u8 val = ((*spos)[0] & 0xc0) >> 6;
1540 int len;
1541
1542 if (val == 1 || val == 2) {
1543 /* These are reserved values in RFC 1035 */
1544 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1545 "sequence starting with 0x%x", val);
1546 return -1;
1547 }
1548
1549 if (val == 3) {
1550 u16 offset;
1551 u8 *spos_tmp;
1552
1553 /* Offset */
1554 if (*spos + 2 > end) {
1555 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1556 "DNS offset field");
1557 return -1;
1558 }
1559
1560 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1561 if (offset >= *spos - start) {
1562 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1563 "pointer offset %u", offset);
1564 return -1;
1565 }
1566
1567 (*spos) += 2;
1568 spos_tmp = start + offset;
1569 return p2p_sd_dns_uncompress_label(upos, uend, start,
1570 &spos_tmp,
1571 *spos - 2);
1572 }
1573
1574 /* Label */
1575 len = (*spos)[0] & 0x3f;
1576 if (len == 0)
1577 return 0;
1578
1579 (*spos)++;
1580 if (*spos + len > end) {
1581 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1582 "sequence - no room for label with length "
1583 "%u", len);
1584 return -1;
1585 }
1586
1587 if (*upos + len + 2 > uend)
1588 return -2;
1589
1590 os_memcpy(*upos, *spos, len);
1591 *spos += len;
1592 *upos += len;
1593 (*upos)[0] = '.';
1594 (*upos)++;
1595 (*upos)[0] = '\0';
1596 }
1597
1598 return 0;
1599}
1600
1601
1602/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1603 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1604 * not large enough */
1605static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1606 size_t msg_len, size_t offset)
1607{
1608 /* 27-octet in-memory packet from P2P specification */
1609 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1610 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1611 u8 *tmp, *end, *spos;
1612 char *upos, *uend;
1613 int ret = 0;
1614
1615 if (buf_len < 2)
1616 return -1;
1617 if (offset > msg_len)
1618 return -1;
1619
1620 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1621 if (tmp == NULL)
1622 return -1;
1623 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1624 end = spos + msg_len;
1625 spos += offset;
1626
1627 os_memset(tmp, 0, DNS_HEADER_LEN);
1628 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1629 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1630
1631 upos = buf;
1632 uend = buf + buf_len;
1633
1634 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1635 if (ret) {
1636 os_free(tmp);
1637 return ret;
1638 }
1639
1640 if (upos == buf) {
1641 upos[0] = '.';
1642 upos[1] = '\0';
1643 } else if (upos[-1] == '.')
1644 upos[-1] = '\0';
1645
1646 os_free(tmp);
1647 return 0;
1648}
1649
1650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651static struct p2p_srv_bonjour *
1652wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1653 const struct wpabuf *query)
1654{
1655 struct p2p_srv_bonjour *bsrv;
1656 size_t len;
1657
1658 len = wpabuf_len(query);
1659 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1660 struct p2p_srv_bonjour, list) {
1661 if (len == wpabuf_len(bsrv->query) &&
1662 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1663 len) == 0)
1664 return bsrv;
1665 }
1666 return NULL;
1667}
1668
1669
1670static struct p2p_srv_upnp *
1671wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1672 const char *service)
1673{
1674 struct p2p_srv_upnp *usrv;
1675
1676 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1677 struct p2p_srv_upnp, list) {
1678 if (version == usrv->version &&
1679 os_strcmp(service, usrv->service) == 0)
1680 return usrv;
1681 }
1682 return NULL;
1683}
1684
1685
1686static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1687 u8 srv_trans_id)
1688{
1689 u8 *len_pos;
1690
1691 if (wpabuf_tailroom(resp) < 5)
1692 return;
1693
1694 /* Length (to be filled) */
1695 len_pos = wpabuf_put(resp, 2);
1696 wpabuf_put_u8(resp, srv_proto);
1697 wpabuf_put_u8(resp, srv_trans_id);
1698 /* Status Code */
1699 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1700 /* Response Data: empty */
1701 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1702}
1703
1704
1705static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1706 struct wpabuf *resp, u8 srv_trans_id)
1707{
1708 struct p2p_srv_bonjour *bsrv;
1709 u8 *len_pos;
1710
1711 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1712
1713 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1714 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1715 return;
1716 }
1717
1718 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1719 struct p2p_srv_bonjour, list) {
1720 if (wpabuf_tailroom(resp) <
1721 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1722 return;
1723 /* Length (to be filled) */
1724 len_pos = wpabuf_put(resp, 2);
1725 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1726 wpabuf_put_u8(resp, srv_trans_id);
1727 /* Status Code */
1728 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1729 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1730 wpabuf_head(bsrv->resp),
1731 wpabuf_len(bsrv->resp));
1732 /* Response Data */
1733 wpabuf_put_buf(resp, bsrv->query); /* Key */
1734 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1735 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1736 2);
1737 }
1738}
1739
1740
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001741static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1742 size_t query_len)
1743{
1744 char str_rx[256], str_srv[256];
1745
1746 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1747 return 0; /* Too short to include DNS Type and Version */
1748 if (os_memcmp(query + query_len - 3,
1749 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1750 3) != 0)
1751 return 0; /* Mismatch in DNS Type or Version */
1752 if (query_len == wpabuf_len(bsrv->query) &&
1753 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1754 return 1; /* Binary match */
1755
1756 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1757 0))
1758 return 0; /* Failed to uncompress query */
1759 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1760 wpabuf_head(bsrv->query),
1761 wpabuf_len(bsrv->query) - 3, 0))
1762 return 0; /* Failed to uncompress service */
1763
1764 return os_strcmp(str_rx, str_srv) == 0;
1765}
1766
1767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1769 struct wpabuf *resp, u8 srv_trans_id,
1770 const u8 *query, size_t query_len)
1771{
1772 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001774 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775
1776 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1777 query, query_len);
1778 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1779 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1780 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1781 srv_trans_id);
1782 return;
1783 }
1784
1785 if (query_len == 0) {
1786 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1787 return;
1788 }
1789
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001790 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1791 struct p2p_srv_bonjour, list) {
1792 if (!match_bonjour_query(bsrv, query, query_len))
1793 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001795 if (wpabuf_tailroom(resp) <
1796 5 + query_len + wpabuf_len(bsrv->resp))
1797 return;
1798
1799 matches++;
1800
1801 /* Length (to be filled) */
1802 len_pos = wpabuf_put(resp, 2);
1803 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1804 wpabuf_put_u8(resp, srv_trans_id);
1805
1806 /* Status Code */
1807 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1808 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1809 wpabuf_head(bsrv->resp),
1810 wpabuf_len(bsrv->resp));
1811
1812 /* Response Data */
1813 wpabuf_put_data(resp, query, query_len); /* Key */
1814 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1815
1816 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1817 }
1818
1819 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1821 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001822 if (wpabuf_tailroom(resp) < 5)
1823 return;
1824
1825 /* Length (to be filled) */
1826 len_pos = wpabuf_put(resp, 2);
1827 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1828 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829
1830 /* Status Code */
1831 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1832 /* Response Data: empty */
1833 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1834 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836}
1837
1838
1839static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1840 struct wpabuf *resp, u8 srv_trans_id)
1841{
1842 struct p2p_srv_upnp *usrv;
1843 u8 *len_pos;
1844
1845 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1846
1847 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1848 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1849 return;
1850 }
1851
1852 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1853 struct p2p_srv_upnp, list) {
1854 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1855 return;
1856
1857 /* Length (to be filled) */
1858 len_pos = wpabuf_put(resp, 2);
1859 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1860 wpabuf_put_u8(resp, srv_trans_id);
1861
1862 /* Status Code */
1863 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1864 /* Response Data */
1865 wpabuf_put_u8(resp, usrv->version);
1866 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1867 usrv->service);
1868 wpabuf_put_str(resp, usrv->service);
1869 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1870 2);
1871 }
1872}
1873
1874
1875static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1876 struct wpabuf *resp, u8 srv_trans_id,
1877 const u8 *query, size_t query_len)
1878{
1879 struct p2p_srv_upnp *usrv;
1880 u8 *len_pos;
1881 u8 version;
1882 char *str;
1883 int count = 0;
1884
1885 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1886 query, query_len);
1887
1888 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1889 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1890 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1891 srv_trans_id);
1892 return;
1893 }
1894
1895 if (query_len == 0) {
1896 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1897 return;
1898 }
1899
1900 if (wpabuf_tailroom(resp) < 5)
1901 return;
1902
1903 /* Length (to be filled) */
1904 len_pos = wpabuf_put(resp, 2);
1905 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1906 wpabuf_put_u8(resp, srv_trans_id);
1907
1908 version = query[0];
1909 str = os_malloc(query_len);
1910 if (str == NULL)
1911 return;
1912 os_memcpy(str, query + 1, query_len - 1);
1913 str[query_len - 1] = '\0';
1914
1915 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1916 struct p2p_srv_upnp, list) {
1917 if (version != usrv->version)
1918 continue;
1919
1920 if (os_strcmp(str, "ssdp:all") != 0 &&
1921 os_strstr(usrv->service, str) == NULL)
1922 continue;
1923
1924 if (wpabuf_tailroom(resp) < 2)
1925 break;
1926 if (count == 0) {
1927 /* Status Code */
1928 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1929 /* Response Data */
1930 wpabuf_put_u8(resp, version);
1931 } else
1932 wpabuf_put_u8(resp, ',');
1933
1934 count++;
1935
1936 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1937 usrv->service);
1938 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1939 break;
1940 wpabuf_put_str(resp, usrv->service);
1941 }
1942 os_free(str);
1943
1944 if (count == 0) {
1945 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1946 "available");
1947 /* Status Code */
1948 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1949 /* Response Data: empty */
1950 }
1951
1952 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1953}
1954
1955
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001956#ifdef CONFIG_WIFI_DISPLAY
1957static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1958 struct wpabuf *resp, u8 srv_trans_id,
1959 const u8 *query, size_t query_len)
1960{
1961 const u8 *pos;
1962 u8 role;
1963 u8 *len_pos;
1964
1965 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1966
1967 if (!wpa_s->global->wifi_display) {
1968 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1969 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1970 srv_trans_id);
1971 return;
1972 }
1973
1974 if (query_len < 1) {
1975 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1976 "Role");
1977 return;
1978 }
1979
1980 if (wpabuf_tailroom(resp) < 5)
1981 return;
1982
1983 pos = query;
1984 role = *pos++;
1985 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1986
1987 /* TODO: role specific handling */
1988
1989 /* Length (to be filled) */
1990 len_pos = wpabuf_put(resp, 2);
1991 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1992 wpabuf_put_u8(resp, srv_trans_id);
1993 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1994
1995 while (pos < query + query_len) {
1996 if (*pos < MAX_WFD_SUBELEMS &&
1997 wpa_s->global->wfd_subelem[*pos] &&
1998 wpabuf_tailroom(resp) >=
1999 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2000 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2001 "subelement %u", *pos);
2002 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2003 }
2004 pos++;
2005 }
2006
2007 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2008}
2009#endif /* CONFIG_WIFI_DISPLAY */
2010
2011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2013 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2014{
2015 struct wpa_supplicant *wpa_s = ctx;
2016 const u8 *pos = tlvs;
2017 const u8 *end = tlvs + tlvs_len;
2018 const u8 *tlv_end;
2019 u16 slen;
2020 struct wpabuf *resp;
2021 u8 srv_proto, srv_trans_id;
2022 size_t buf_len;
2023 char *buf;
2024
2025 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2026 tlvs, tlvs_len);
2027 buf_len = 2 * tlvs_len + 1;
2028 buf = os_malloc(buf_len);
2029 if (buf) {
2030 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2031 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2032 MACSTR " %u %u %s",
2033 freq, MAC2STR(sa), dialog_token, update_indic,
2034 buf);
2035 os_free(buf);
2036 }
2037
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002038 if (wpa_s->p2p_sd_over_ctrl_iface) {
2039 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2040 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002042 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043
2044 resp = wpabuf_alloc(10000);
2045 if (resp == NULL)
2046 return;
2047
2048 while (pos + 1 < end) {
2049 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2050 slen = WPA_GET_LE16(pos);
2051 pos += 2;
2052 if (pos + slen > end || slen < 2) {
2053 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2054 "length");
2055 wpabuf_free(resp);
2056 return;
2057 }
2058 tlv_end = pos + slen;
2059
2060 srv_proto = *pos++;
2061 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2062 srv_proto);
2063 srv_trans_id = *pos++;
2064 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2065 srv_trans_id);
2066
2067 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2068 pos, tlv_end - pos);
2069
2070
2071 if (wpa_s->force_long_sd) {
2072 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2073 "response");
2074 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2075 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2076 goto done;
2077 }
2078
2079 switch (srv_proto) {
2080 case P2P_SERV_ALL_SERVICES:
2081 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2082 "for all services");
2083 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2084 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2085 wpa_printf(MSG_DEBUG, "P2P: No service "
2086 "discovery protocols available");
2087 wpas_sd_add_proto_not_avail(
2088 resp, P2P_SERV_ALL_SERVICES,
2089 srv_trans_id);
2090 break;
2091 }
2092 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2093 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2094 break;
2095 case P2P_SERV_BONJOUR:
2096 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2097 pos, tlv_end - pos);
2098 break;
2099 case P2P_SERV_UPNP:
2100 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2101 pos, tlv_end - pos);
2102 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002103#ifdef CONFIG_WIFI_DISPLAY
2104 case P2P_SERV_WIFI_DISPLAY:
2105 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2106 pos, tlv_end - pos);
2107 break;
2108#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 default:
2110 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2111 "protocol %u", srv_proto);
2112 wpas_sd_add_proto_not_avail(resp, srv_proto,
2113 srv_trans_id);
2114 break;
2115 }
2116
2117 pos = tlv_end;
2118 }
2119
2120done:
2121 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2122 update_indic, tlvs, tlvs_len);
2123
2124 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2125
2126 wpabuf_free(resp);
2127}
2128
2129
2130void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2131 const u8 *tlvs, size_t tlvs_len)
2132{
2133 struct wpa_supplicant *wpa_s = ctx;
2134 const u8 *pos = tlvs;
2135 const u8 *end = tlvs + tlvs_len;
2136 const u8 *tlv_end;
2137 u16 slen;
2138 size_t buf_len;
2139 char *buf;
2140
2141 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2142 tlvs, tlvs_len);
2143 if (tlvs_len > 1500) {
2144 /* TODO: better way for handling this */
2145 wpa_msg_ctrl(wpa_s, MSG_INFO,
2146 P2P_EVENT_SERV_DISC_RESP MACSTR
2147 " %u <long response: %u bytes>",
2148 MAC2STR(sa), update_indic,
2149 (unsigned int) tlvs_len);
2150 } else {
2151 buf_len = 2 * tlvs_len + 1;
2152 buf = os_malloc(buf_len);
2153 if (buf) {
2154 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2155 wpa_msg_ctrl(wpa_s, MSG_INFO,
2156 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2157 MAC2STR(sa), update_indic, buf);
2158 os_free(buf);
2159 }
2160 }
2161
2162 while (pos < end) {
2163 u8 srv_proto, srv_trans_id, status;
2164
2165 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2166 slen = WPA_GET_LE16(pos);
2167 pos += 2;
2168 if (pos + slen > end || slen < 3) {
2169 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2170 "length");
2171 return;
2172 }
2173 tlv_end = pos + slen;
2174
2175 srv_proto = *pos++;
2176 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2177 srv_proto);
2178 srv_trans_id = *pos++;
2179 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2180 srv_trans_id);
2181 status = *pos++;
2182 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2183 status);
2184
2185 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2186 pos, tlv_end - pos);
2187
2188 pos = tlv_end;
2189 }
2190
2191 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2192}
2193
2194
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002195u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2196 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197{
2198 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002199 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002201 return 0;
2202 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203}
2204
2205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002206u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2207 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208{
2209 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002210 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211
2212 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2213 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002214 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2216 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2217 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2218 wpabuf_put_u8(tlvs, version);
2219 wpabuf_put_str(tlvs, query);
2220 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2221 wpabuf_free(tlvs);
2222 return ret;
2223}
2224
2225
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002226#ifdef CONFIG_WIFI_DISPLAY
2227
2228static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2229 const struct wpabuf *tlvs)
2230{
2231 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2232 return 0;
2233 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2234 return 0;
2235 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2236}
2237
2238
2239#define MAX_WFD_SD_SUBELEMS 20
2240
2241static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2242 const char *subelems)
2243{
2244 u8 *len;
2245 const char *pos;
2246 int val;
2247 int count = 0;
2248
2249 len = wpabuf_put(tlvs, 2);
2250 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2251 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2252
2253 wpabuf_put_u8(tlvs, role);
2254
2255 pos = subelems;
2256 while (*pos) {
2257 val = atoi(pos);
2258 if (val >= 0 && val < 256) {
2259 wpabuf_put_u8(tlvs, val);
2260 count++;
2261 if (count == MAX_WFD_SD_SUBELEMS)
2262 break;
2263 }
2264 pos = os_strchr(pos + 1, ',');
2265 if (pos == NULL)
2266 break;
2267 pos++;
2268 }
2269
2270 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2271}
2272
2273
2274u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2275 const u8 *dst, const char *role)
2276{
2277 struct wpabuf *tlvs;
2278 u64 ret;
2279 const char *subelems;
2280 u8 id = 1;
2281
2282 subelems = os_strchr(role, ' ');
2283 if (subelems == NULL)
2284 return 0;
2285 subelems++;
2286
2287 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2288 if (tlvs == NULL)
2289 return 0;
2290
2291 if (os_strstr(role, "[source]"))
2292 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2293 if (os_strstr(role, "[pri-sink]"))
2294 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2295 if (os_strstr(role, "[sec-sink]"))
2296 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2297 if (os_strstr(role, "[source+sink]"))
2298 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2299
2300 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2301 wpabuf_free(tlvs);
2302 return ret;
2303}
2304
2305#endif /* CONFIG_WIFI_DISPLAY */
2306
2307
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002308int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002309{
2310 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002311 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002312 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2313 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002314 return p2p_sd_cancel_request(wpa_s->global->p2p,
2315 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316}
2317
2318
2319void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2320 const u8 *dst, u8 dialog_token,
2321 const struct wpabuf *resp_tlvs)
2322{
2323 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2324 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2325 resp_tlvs);
2326 return;
2327 }
2328 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2329 return;
2330 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2331 resp_tlvs);
2332}
2333
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2336{
2337 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2338 wpa_drv_p2p_service_update(wpa_s);
2339 return;
2340 }
2341 if (wpa_s->global->p2p)
2342 p2p_sd_service_update(wpa_s->global->p2p);
2343}
2344
2345
2346static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2347{
2348 dl_list_del(&bsrv->list);
2349 wpabuf_free(bsrv->query);
2350 wpabuf_free(bsrv->resp);
2351 os_free(bsrv);
2352}
2353
2354
2355static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2356{
2357 dl_list_del(&usrv->list);
2358 os_free(usrv->service);
2359 os_free(usrv);
2360}
2361
2362
2363void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2364{
2365 struct p2p_srv_bonjour *bsrv, *bn;
2366 struct p2p_srv_upnp *usrv, *un;
2367
2368 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2369 struct p2p_srv_bonjour, list)
2370 wpas_p2p_srv_bonjour_free(bsrv);
2371
2372 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2373 struct p2p_srv_upnp, list)
2374 wpas_p2p_srv_upnp_free(usrv);
2375
2376 wpas_p2p_sd_service_update(wpa_s);
2377}
2378
2379
2380int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2381 struct wpabuf *query, struct wpabuf *resp)
2382{
2383 struct p2p_srv_bonjour *bsrv;
2384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385 bsrv = os_zalloc(sizeof(*bsrv));
2386 if (bsrv == NULL)
2387 return -1;
2388 bsrv->query = query;
2389 bsrv->resp = resp;
2390 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2391
2392 wpas_p2p_sd_service_update(wpa_s);
2393 return 0;
2394}
2395
2396
2397int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2398 const struct wpabuf *query)
2399{
2400 struct p2p_srv_bonjour *bsrv;
2401
2402 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2403 if (bsrv == NULL)
2404 return -1;
2405 wpas_p2p_srv_bonjour_free(bsrv);
2406 wpas_p2p_sd_service_update(wpa_s);
2407 return 0;
2408}
2409
2410
2411int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2412 const char *service)
2413{
2414 struct p2p_srv_upnp *usrv;
2415
2416 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2417 return 0; /* Already listed */
2418 usrv = os_zalloc(sizeof(*usrv));
2419 if (usrv == NULL)
2420 return -1;
2421 usrv->version = version;
2422 usrv->service = os_strdup(service);
2423 if (usrv->service == NULL) {
2424 os_free(usrv);
2425 return -1;
2426 }
2427 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2428
2429 wpas_p2p_sd_service_update(wpa_s);
2430 return 0;
2431}
2432
2433
2434int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2435 const char *service)
2436{
2437 struct p2p_srv_upnp *usrv;
2438
2439 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2440 if (usrv == NULL)
2441 return -1;
2442 wpas_p2p_srv_upnp_free(usrv);
2443 wpas_p2p_sd_service_update(wpa_s);
2444 return 0;
2445}
2446
2447
2448static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2449 const u8 *peer, const char *params,
2450 unsigned int generated_pin)
2451{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002452 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2453 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454}
2455
2456
2457static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2458 const u8 *peer, const char *params)
2459{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002460 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2461 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002462}
2463
2464
2465void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2466 const u8 *dev_addr, const u8 *pri_dev_type,
2467 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002468 u8 dev_capab, u8 group_capab, const u8 *group_id,
2469 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470{
2471 struct wpa_supplicant *wpa_s = ctx;
2472 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002473 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 u8 empty_dev_type[8];
2475 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002476 struct wpa_supplicant *group = NULL;
2477
2478 if (group_id) {
2479 for (group = wpa_s->global->ifaces; group; group = group->next)
2480 {
2481 struct wpa_ssid *s = group->current_ssid;
2482 if (s != NULL &&
2483 s->mode == WPAS_MODE_P2P_GO &&
2484 group_id_len - ETH_ALEN == s->ssid_len &&
2485 os_memcmp(group_id + ETH_ALEN, s->ssid,
2486 s->ssid_len) == 0)
2487 break;
2488 }
2489 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490
2491 if (pri_dev_type == NULL) {
2492 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2493 pri_dev_type = empty_dev_type;
2494 }
2495 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2496 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002497 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498 MAC2STR(dev_addr),
2499 wps_dev_type_bin2str(pri_dev_type, devtype,
2500 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002501 dev_name, supp_config_methods, dev_capab, group_capab,
2502 group ? " group=" : "",
2503 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506 if (config_methods & WPS_CONFIG_DISPLAY) {
2507 generated_pin = wps_generate_pin();
2508 wpas_prov_disc_local_display(wpa_s, peer, params,
2509 generated_pin);
2510 } else if (config_methods & WPS_CONFIG_KEYPAD)
2511 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2512 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002513 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2514 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002515
2516 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2517 P2P_PROV_DISC_SUCCESS,
2518 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002519}
2520
2521
2522void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2523{
2524 struct wpa_supplicant *wpa_s = ctx;
2525 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002526 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002528 if (wpa_s->pending_pd_before_join &&
2529 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2530 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2531 wpa_s->pending_pd_before_join = 0;
2532 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2533 "join-existing-group operation");
2534 wpas_p2p_join_start(wpa_s);
2535 return;
2536 }
2537
Dmitry Shmidt04949592012-07-19 12:16:46 -07002538 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2539 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2540 os_snprintf(params, sizeof(params), " peer_go=%d",
2541 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2542 else
2543 params[0] = '\0';
2544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002546 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002547 else if (config_methods & WPS_CONFIG_KEYPAD) {
2548 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002549 wpas_prov_disc_local_display(wpa_s, peer, params,
2550 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002551 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002552 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2553 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554
Jouni Malinen75ecf522011-06-27 15:19:46 -07002555 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2556 P2P_PROV_DISC_SUCCESS,
2557 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558}
2559
2560
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002561static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2562 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002563{
2564 struct wpa_supplicant *wpa_s = ctx;
2565
Dmitry Shmidt04949592012-07-19 12:16:46 -07002566 if (wpa_s->p2p_fallback_to_go_neg) {
2567 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2568 "failed - fall back to GO Negotiation");
2569 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2570 return;
2571 }
2572
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002573 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002574 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002575 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2576 "join-existing-group operation (no ACK for PD "
2577 "Req attempts)");
2578 wpas_p2p_join_start(wpa_s);
2579 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002580 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002581
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002582 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2583 " p2p_dev_addr=" MACSTR " status=%d",
2584 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002585
Jouni Malinen75ecf522011-06-27 15:19:46 -07002586 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2587 status, 0, 0);
2588}
2589
2590
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002591static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2592{
2593 if (channels == NULL)
2594 return 1; /* Assume no restrictions */
2595 return p2p_channels_includes_freq(channels, freq);
2596
2597}
2598
2599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2601 const u8 *go_dev_addr, const u8 *ssid,
2602 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002603 int *force_freq, int persistent_group,
2604 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002605{
2606 struct wpa_supplicant *wpa_s = ctx;
2607 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 int res;
2609 struct wpa_supplicant *grp;
2610
2611 if (!persistent_group) {
2612 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2613 " to join an active group", MAC2STR(sa));
2614 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2615 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2616 == 0 ||
2617 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2618 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2619 "authorized invitation");
2620 goto accept_inv;
2621 }
2622 /*
2623 * Do not accept the invitation automatically; notify user and
2624 * request approval.
2625 */
2626 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2627 }
2628
2629 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2630 if (grp) {
2631 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2632 "running persistent group");
2633 if (*go)
2634 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2635 goto accept_inv;
2636 }
2637
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002638 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2639 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2640 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2641 "invitation to re-invoke a persistent group");
2642 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2644
2645 for (s = wpa_s->conf->ssid; s; s = s->next) {
2646 if (s->disabled == 2 &&
2647 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2648 s->ssid_len == ssid_len &&
2649 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2650 break;
2651 }
2652
2653 if (!s) {
2654 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2655 " requested reinvocation of an unknown group",
2656 MAC2STR(sa));
2657 return P2P_SC_FAIL_UNKNOWN_GROUP;
2658 }
2659
2660 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2661 *go = 1;
2662 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2663 wpa_printf(MSG_DEBUG, "P2P: The only available "
2664 "interface is already in use - reject "
2665 "invitation");
2666 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2667 }
2668 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2669 } else if (s->mode == WPAS_MODE_P2P_GO) {
2670 *go = 1;
2671 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2672 {
2673 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2674 "interface address for the group");
2675 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2676 }
2677 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2678 ETH_ALEN);
2679 }
2680
2681accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002682 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2683
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002684 /* Get one of the frequencies currently in use */
2685 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2686 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 -07002687 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002688 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002689 }
2690
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002691 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2692 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002693 if (*go == 0) {
2694 /* We are the client */
2695 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2696 "running a GO but we are capable of MCC, "
2697 "figure out the best channel to use");
2698 *force_freq = 0;
2699 } else if (!freq_included(channels, *force_freq)) {
2700 /* We are the GO, and *force_freq is not in the
2701 * intersection */
2702 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2703 "in intersection but we are capable of MCC, "
2704 "figure out the best channel to use",
2705 *force_freq);
2706 *force_freq = 0;
2707 }
2708 }
2709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710 return P2P_SC_SUCCESS;
2711}
2712
2713
2714static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2715 const u8 *ssid, size_t ssid_len,
2716 const u8 *go_dev_addr, u8 status,
2717 int op_freq)
2718{
2719 struct wpa_supplicant *wpa_s = ctx;
2720 struct wpa_ssid *s;
2721
2722 for (s = wpa_s->conf->ssid; s; s = s->next) {
2723 if (s->disabled == 2 &&
2724 s->ssid_len == ssid_len &&
2725 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2726 break;
2727 }
2728
2729 if (status == P2P_SC_SUCCESS) {
2730 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2731 " was accepted; op_freq=%d MHz",
2732 MAC2STR(sa), op_freq);
2733 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002734 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735 wpas_p2p_group_add_persistent(
Dmitry Shmidt56052862013-10-04 10:23:25 -07002736 wpa_s, s, go, go ? op_freq : 0, 0, NULL,
2737 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002738 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002739 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002740 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002741 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742 }
2743 return;
2744 }
2745
2746 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2747 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2748 " was rejected (status %u)", MAC2STR(sa), status);
2749 return;
2750 }
2751
2752 if (!s) {
2753 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002754 wpa_msg_global(wpa_s, MSG_INFO,
2755 P2P_EVENT_INVITATION_RECEIVED
2756 "sa=" MACSTR " go_dev_addr=" MACSTR
2757 " bssid=" MACSTR " unknown-network",
2758 MAC2STR(sa), MAC2STR(go_dev_addr),
2759 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002761 wpa_msg_global(wpa_s, MSG_INFO,
2762 P2P_EVENT_INVITATION_RECEIVED
2763 "sa=" MACSTR " go_dev_addr=" MACSTR
2764 " unknown-network",
2765 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766 }
2767 return;
2768 }
2769
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002770 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002771 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2772 "sa=" MACSTR " persistent=%d freq=%d",
2773 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002774 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002775 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2776 "sa=" MACSTR " persistent=%d",
2777 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002778 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002779}
2780
2781
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002782static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2783 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002784 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002785{
2786 size_t i;
2787
2788 if (ssid == NULL)
2789 return;
2790
2791 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2792 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2793 ETH_ALEN) == 0)
2794 break;
2795 }
2796 if (i >= ssid->num_p2p_clients) {
2797 if (ssid->mode != WPAS_MODE_P2P_GO &&
2798 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2799 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2800 "due to invitation result", ssid->id);
2801 wpas_notify_network_removed(wpa_s, ssid);
2802 wpa_config_remove_network(wpa_s->conf, ssid->id);
2803 return;
2804 }
2805 return; /* Peer not found in client list */
2806 }
2807
2808 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002809 "group %d client list%s",
2810 MAC2STR(peer), ssid->id,
2811 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002812 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2813 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2814 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2815 ssid->num_p2p_clients--;
2816#ifndef CONFIG_NO_CONFIG_WRITE
2817 if (wpa_s->parent->conf->update_config &&
2818 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2819 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2820#endif /* CONFIG_NO_CONFIG_WRITE */
2821}
2822
2823
2824static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2825 const u8 *peer)
2826{
2827 struct wpa_ssid *ssid;
2828
2829 wpa_s = wpa_s->global->p2p_invite_group;
2830 if (wpa_s == NULL)
2831 return; /* No known invitation group */
2832 ssid = wpa_s->current_ssid;
2833 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2834 !ssid->p2p_persistent_group)
2835 return; /* Not operating as a GO in persistent group */
2836 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2837 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002838 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002839}
2840
2841
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002842static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002843 const struct p2p_channels *channels,
2844 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002845{
2846 struct wpa_supplicant *wpa_s = ctx;
2847 struct wpa_ssid *ssid;
2848
2849 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002850 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2851 "status=%d " MACSTR,
2852 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002854 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2855 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 }
2857 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2858
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002859 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2860 status, MAC2STR(peer));
2861 if (wpa_s->pending_invite_ssid_id == -1) {
2862 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2863 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002865 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002867 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2868 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2869 "invitation exchange to indicate readiness for "
2870 "re-invocation");
2871 }
2872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002874 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2875 ssid = wpa_config_get_network(
2876 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002877 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002878 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 wpas_p2p_remove_pending_group_interface(wpa_s);
2880 return;
2881 }
2882
2883 ssid = wpa_config_get_network(wpa_s->conf,
2884 wpa_s->pending_invite_ssid_id);
2885 if (ssid == NULL) {
2886 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2887 "data matching with invitation");
2888 return;
2889 }
2890
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002891 /*
2892 * The peer could have missed our ctrl::ack frame for Invitation
2893 * Response and continue retransmitting the frame. To reduce the
2894 * likelihood of the peer not getting successful TX status for the
2895 * Invitation Response frame, wait a short time here before starting
2896 * the persistent group so that we will remain on the current channel to
2897 * acknowledge any possible retransmission from the peer.
2898 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002899#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002900 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2901 "starting persistent group");
2902 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002903#else
2904 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2905 "starting persistent group");
2906 os_sleep(0, 100000);
2907#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002908
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002910 ssid->mode == WPAS_MODE_P2P_GO,
2911 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002912 wpa_s->p2p_go_ht40, channels,
2913 ssid->mode == WPAS_MODE_P2P_GO ?
2914 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2915 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916}
2917
2918
Dmitry Shmidt04949592012-07-19 12:16:46 -07002919static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2920 unsigned int freq)
2921{
2922 unsigned int i;
2923
2924 if (global->p2p_disallow_freq == NULL)
2925 return 0;
2926
2927 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2928 if (freq >= global->p2p_disallow_freq[i].min &&
2929 freq <= global->p2p_disallow_freq[i].max)
2930 return 1;
2931 }
2932
2933 return 0;
2934}
2935
2936
2937static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2938{
2939 reg->channel[reg->channels] = chan;
2940 reg->channels++;
2941}
2942
2943
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002944static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2945 struct p2p_channels *chan)
2946{
2947 int i, cla = 0;
2948
2949 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2950 "band");
2951
2952 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2953 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002954 chan->reg_class[cla].channels = 0;
2955 for (i = 0; i < 11; i++) {
2956 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2957 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2958 }
2959 if (chan->reg_class[cla].channels)
2960 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961
2962 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2963 "band");
2964
2965 /* Operating class 115 - 5 GHz, channels 36-48 */
2966 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002967 chan->reg_class[cla].channels = 0;
2968 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2969 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2970 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2971 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2972 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2973 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2974 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2975 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2976 if (chan->reg_class[cla].channels)
2977 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978
2979 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2980 "band");
2981
2982 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2983 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002984 chan->reg_class[cla].channels = 0;
2985 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2986 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2987 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2988 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2989 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2990 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2991 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2992 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2993 if (chan->reg_class[cla].channels)
2994 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995
2996 chan->reg_classes = cla;
2997 return 0;
2998}
2999
3000
3001static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3002 u16 num_modes,
3003 enum hostapd_hw_mode mode)
3004{
3005 u16 i;
3006
3007 for (i = 0; i < num_modes; i++) {
3008 if (modes[i].mode == mode)
3009 return &modes[i];
3010 }
3011
3012 return NULL;
3013}
3014
3015
Dmitry Shmidt04949592012-07-19 12:16:46 -07003016static int has_channel(struct wpa_global *global,
3017 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018{
3019 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003020 unsigned int freq;
3021
3022 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3023 chan * 5;
3024 if (wpas_p2p_disallowed_freq(global, freq))
3025 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026
3027 for (i = 0; i < mode->num_channels; i++) {
3028 if (mode->channels[i].chan == chan) {
3029 if (flags)
3030 *flags = mode->channels[i].flag;
3031 return !(mode->channels[i].flag &
3032 (HOSTAPD_CHAN_DISABLED |
3033 HOSTAPD_CHAN_PASSIVE_SCAN |
3034 HOSTAPD_CHAN_NO_IBSS |
3035 HOSTAPD_CHAN_RADAR));
3036 }
3037 }
3038
3039 return 0;
3040}
3041
3042
3043struct p2p_oper_class_map {
3044 enum hostapd_hw_mode mode;
3045 u8 op_class;
3046 u8 min_chan;
3047 u8 max_chan;
3048 u8 inc;
3049 enum { BW20, BW40PLUS, BW40MINUS } bw;
3050};
3051
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003052static struct p2p_oper_class_map op_class[] = {
3053 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003054#if 0 /* Do not enable HT40 on 2 GHz for now */
3055 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3056 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3057#endif
3058 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3059 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3060 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3061 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3062 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3063 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3064 { -1, 0, 0, 0, 0, BW20 }
3065};
3066
3067
3068static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3069 struct hostapd_hw_modes *mode,
3070 u8 channel, u8 bw)
3071{
3072 int flag;
3073
3074 if (!has_channel(wpa_s->global, mode, channel, &flag))
3075 return -1;
3076 if (bw == BW40MINUS &&
3077 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
3078 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
3079 return 0;
3080 if (bw == BW40PLUS &&
3081 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
3082 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
3083 return 0;
3084 return 1;
3085}
3086
3087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003088static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3089 struct p2p_channels *chan)
3090{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003091 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003094 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3096 "of all supported channels; assume dualband "
3097 "support");
3098 return wpas_p2p_default_channels(wpa_s, chan);
3099 }
3100
3101 cla = 0;
3102
3103 for (op = 0; op_class[op].op_class; op++) {
3104 struct p2p_oper_class_map *o = &op_class[op];
3105 u8 ch;
3106 struct p2p_reg_class *reg = NULL;
3107
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003108 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109 if (mode == NULL)
3110 continue;
3111 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003112 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113 continue;
3114 if (reg == NULL) {
3115 wpa_printf(MSG_DEBUG, "P2P: Add operating "
3116 "class %u", o->op_class);
3117 reg = &chan->reg_class[cla];
3118 cla++;
3119 reg->reg_class = o->op_class;
3120 }
3121 reg->channel[reg->channels] = ch;
3122 reg->channels++;
3123 }
3124 if (reg) {
3125 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3126 reg->channel, reg->channels);
3127 }
3128 }
3129
3130 chan->reg_classes = cla;
3131
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 return 0;
3133}
3134
3135
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003136int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3137 struct hostapd_hw_modes *mode, u8 channel)
3138{
3139 int op, ret;
3140
3141 for (op = 0; op_class[op].op_class; op++) {
3142 struct p2p_oper_class_map *o = &op_class[op];
3143 u8 ch;
3144
3145 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3146 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3147 o->bw == BW20 || ch != channel)
3148 continue;
3149 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3150 if (ret < 0)
3151 continue;
3152 else if (ret > 0)
3153 return (o->bw == BW40MINUS) ? -1 : 1;
3154 else
3155 return 0;
3156 }
3157 }
3158 return 0;
3159}
3160
3161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003162static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3163 size_t buf_len)
3164{
3165 struct wpa_supplicant *wpa_s = ctx;
3166
3167 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3168 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3169 break;
3170 }
3171 if (wpa_s == NULL)
3172 return -1;
3173
3174 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3175}
3176
3177
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003178static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3179{
3180 struct wpa_supplicant *wpa_s = ctx;
3181
3182 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3183 struct wpa_ssid *ssid = wpa_s->current_ssid;
3184 if (ssid == NULL)
3185 continue;
3186 if (ssid->mode != WPAS_MODE_INFRA)
3187 continue;
3188 if (wpa_s->wpa_state != WPA_COMPLETED &&
3189 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3190 continue;
3191 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3192 return 1;
3193 }
3194
3195 return 0;
3196}
3197
3198
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003199static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3200{
3201 struct wpa_supplicant *wpa_s = ctx;
3202 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3203}
3204
3205
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003206int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3207{
3208 struct wpa_interface iface;
3209 struct wpa_supplicant *p2pdev_wpa_s;
3210 char ifname[100];
3211 char force_name[100];
3212 int ret;
3213
3214 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3215 wpa_s->ifname);
3216 force_name[0] = '\0';
3217 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3218 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3219 force_name, wpa_s->pending_interface_addr, NULL);
3220 if (ret < 0) {
3221 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3222 return ret;
3223 }
3224 os_strlcpy(wpa_s->pending_interface_name, ifname,
3225 sizeof(wpa_s->pending_interface_name));
3226
3227 os_memset(&iface, 0, sizeof(iface));
3228 iface.p2p_mgmt = 1;
3229 iface.ifname = wpa_s->pending_interface_name;
3230 iface.driver = wpa_s->driver->name;
3231 iface.driver_param = wpa_s->conf->driver_param;
3232 iface.confname = wpa_s->confname;
3233 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3234 if (!p2pdev_wpa_s) {
3235 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3236 return -1;
3237 }
3238 p2pdev_wpa_s->parent = wpa_s;
3239
3240 wpa_s->pending_interface_name[0] = '\0';
3241 return 0;
3242}
3243
3244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245/**
3246 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3247 * @global: Pointer to global data from wpa_supplicant_init()
3248 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3249 * Returns: 0 on success, -1 on failure
3250 */
3251int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3252{
3253 struct p2p_config p2p;
3254 unsigned int r;
3255 int i;
3256
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003257 if (wpa_s->conf->p2p_disabled)
3258 return 0;
3259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003260 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3261 return 0;
3262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263 if (global->p2p)
3264 return 0;
3265
3266 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3267 struct p2p_params params;
3268
3269 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3270 os_memset(&params, 0, sizeof(params));
3271 params.dev_name = wpa_s->conf->device_name;
3272 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3273 WPS_DEV_TYPE_LEN);
3274 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3275 os_memcpy(params.sec_dev_type,
3276 wpa_s->conf->sec_device_type,
3277 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3278
3279 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3280 return -1;
3281
3282 return 0;
3283 }
3284
3285 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003287 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288 p2p.p2p_scan = wpas_p2p_scan;
3289 p2p.send_action = wpas_send_action;
3290 p2p.send_action_done = wpas_send_action_done;
3291 p2p.go_neg_completed = wpas_go_neg_completed;
3292 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3293 p2p.dev_found = wpas_dev_found;
3294 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003295 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296 p2p.start_listen = wpas_start_listen;
3297 p2p.stop_listen = wpas_stop_listen;
3298 p2p.send_probe_resp = wpas_send_probe_resp;
3299 p2p.sd_request = wpas_sd_request;
3300 p2p.sd_response = wpas_sd_response;
3301 p2p.prov_disc_req = wpas_prov_disc_req;
3302 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003303 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 p2p.invitation_process = wpas_invitation_process;
3305 p2p.invitation_received = wpas_invitation_received;
3306 p2p.invitation_result = wpas_invitation_result;
3307 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003308 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309
3310 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003311 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003312 p2p.dev_name = wpa_s->conf->device_name;
3313 p2p.manufacturer = wpa_s->conf->manufacturer;
3314 p2p.model_name = wpa_s->conf->model_name;
3315 p2p.model_number = wpa_s->conf->model_number;
3316 p2p.serial_number = wpa_s->conf->serial_number;
3317 if (wpa_s->wps) {
3318 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3319 p2p.config_methods = wpa_s->wps->config_methods;
3320 }
3321
3322 if (wpa_s->conf->p2p_listen_reg_class &&
3323 wpa_s->conf->p2p_listen_channel) {
3324 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3325 p2p.channel = wpa_s->conf->p2p_listen_channel;
3326 } else {
3327 p2p.reg_class = 81;
3328 /*
3329 * Pick one of the social channels randomly as the listen
3330 * channel.
3331 */
3332 os_get_random((u8 *) &r, sizeof(r));
3333 p2p.channel = 1 + (r % 3) * 5;
3334 }
3335 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3336
3337 if (wpa_s->conf->p2p_oper_reg_class &&
3338 wpa_s->conf->p2p_oper_channel) {
3339 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3340 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3341 p2p.cfg_op_channel = 1;
3342 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3343 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3344
3345 } else {
3346 p2p.op_reg_class = 81;
3347 /*
3348 * Use random operation channel from (1, 6, 11) if no other
3349 * preference is indicated.
3350 */
3351 os_get_random((u8 *) &r, sizeof(r));
3352 p2p.op_channel = 1 + (r % 3) * 5;
3353 p2p.cfg_op_channel = 0;
3354 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3355 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3356 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003357
3358 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3359 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3360 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3361 }
3362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3364 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3365 p2p.country[2] = 0x04;
3366 } else
3367 os_memcpy(p2p.country, "XX\x04", 3);
3368
3369 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3370 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3371 "channel list");
3372 return -1;
3373 }
3374
3375 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3376 WPS_DEV_TYPE_LEN);
3377
3378 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3379 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3380 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3381
3382 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3383 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3384
3385 p2p.max_peers = 100;
3386
3387 if (wpa_s->conf->p2p_ssid_postfix) {
3388 p2p.ssid_postfix_len =
3389 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3390 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3391 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3392 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3393 p2p.ssid_postfix_len);
3394 }
3395
3396 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3397
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003398 p2p.max_listen = wpa_s->max_remain_on_chan;
3399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003400 global->p2p = p2p_init(&p2p);
3401 if (global->p2p == NULL)
3402 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003403 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003404
3405 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3406 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3407 continue;
3408 p2p_add_wps_vendor_extension(
3409 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3410 }
3411
3412 return 0;
3413}
3414
3415
3416/**
3417 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3418 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3419 *
3420 * This function deinitialize per-interface P2P data.
3421 */
3422void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3423{
3424 if (wpa_s->driver && wpa_s->drv_priv)
3425 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003426
3427 if (wpa_s->go_params) {
3428 /* Clear any stored provisioning info */
3429 p2p_clear_provisioning_info(
3430 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003431 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003432 }
3433
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 os_free(wpa_s->go_params);
3435 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003436#ifdef ANDROID_P2P
3437 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3438#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003439 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3440 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3441 wpa_s->p2p_long_listen = 0;
3442 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3443 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3444 wpas_p2p_remove_pending_group_interface(wpa_s);
3445
3446 /* TODO: remove group interface from the driver if this wpa_s instance
3447 * is on top of a P2P group interface */
3448}
3449
3450
3451/**
3452 * wpas_p2p_deinit_global - Deinitialize global P2P module
3453 * @global: Pointer to global data from wpa_supplicant_init()
3454 *
3455 * This function deinitializes the global (per device) P2P module.
3456 */
3457void wpas_p2p_deinit_global(struct wpa_global *global)
3458{
3459 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003461 wpa_s = global->ifaces;
3462 if (wpa_s)
3463 wpas_p2p_service_flush(wpa_s);
3464
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465 if (global->p2p == NULL)
3466 return;
3467
3468 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003469 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3470 wpa_s = wpa_s->next;
3471 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003472 tmp = global->ifaces;
3473 while (tmp &&
3474 (tmp == wpa_s ||
3475 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3476 tmp = tmp->next;
3477 }
3478 if (tmp == NULL)
3479 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003480 /* Disconnect from the P2P group and deinit the interface */
3481 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482 }
3483
3484 /*
3485 * Deinit GO data on any possibly remaining interface (if main
3486 * interface is used as GO).
3487 */
3488 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3489 if (wpa_s->ap_iface)
3490 wpas_p2p_group_deinit(wpa_s);
3491 }
3492
3493 p2p_deinit(global->p2p);
3494 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003495 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003496}
3497
3498
3499static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3500{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003501 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3502 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003503 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504 if (wpa_s->drv_flags &
3505 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3506 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3507 return 1; /* P2P group requires a new interface in every case
3508 */
3509 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3510 return 0; /* driver does not support concurrent operations */
3511 if (wpa_s->global->ifaces->next)
3512 return 1; /* more that one interface already in use */
3513 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3514 return 1; /* this interface is already in use */
3515 return 0;
3516}
3517
3518
3519static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3520 const u8 *peer_addr,
3521 enum p2p_wps_method wps_method,
3522 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003523 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003524 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003526 if (persistent_group && wpa_s->conf->persistent_reconnect)
3527 persistent_group = 2;
3528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3530 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3531 go_intent, own_interface_addr,
3532 force_freq, persistent_group);
3533 }
3534
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003535 /*
3536 * Increase GO config timeout if HT40 is used since it takes some time
3537 * to scan channels for coex purposes before the BSS can be started.
3538 */
3539 p2p_set_config_timeout(wpa_s->global->p2p,
3540 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3543 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003544 persistent_group, ssid ? ssid->ssid : NULL,
3545 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003546 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547}
3548
3549
3550static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3551 const u8 *peer_addr,
3552 enum p2p_wps_method wps_method,
3553 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003554 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003555 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003556{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003557 if (persistent_group && wpa_s->conf->persistent_reconnect)
3558 persistent_group = 2;
3559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003560 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3561 return -1;
3562
3563 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3564 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003565 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003566 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567}
3568
3569
3570static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3571{
3572 wpa_s->p2p_join_scan_count++;
3573 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3574 wpa_s->p2p_join_scan_count);
3575 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3576 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3577 " for join operationg - stop join attempt",
3578 MAC2STR(wpa_s->pending_join_iface_addr));
3579 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003580 if (wpa_s->p2p_auto_pd) {
3581 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003582 wpa_msg_global(wpa_s, MSG_INFO,
3583 P2P_EVENT_PROV_DISC_FAILURE
3584 " p2p_dev_addr=" MACSTR " status=N/A",
3585 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003586 return;
3587 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003588 wpa_msg_global(wpa_s->parent, MSG_INFO,
3589 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590 }
3591}
3592
3593
Dmitry Shmidt04949592012-07-19 12:16:46 -07003594static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3595{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003596 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003597
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003598 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3599 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003600 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003601 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003602
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003603 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3604 if (!freqs)
3605 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003606
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003607 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3608 wpa_s->num_multichan_concurrent);
3609 if (num < 0) {
3610 res = 1;
3611 goto exit_free;
3612 }
3613
3614 for (i = 0; i < num; i++) {
3615 if (freqs[i] == freq) {
3616 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3617 freq);
3618 res = 0;
3619 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003620 }
3621 }
3622
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003623 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003624
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003625exit_free:
3626 os_free(freqs);
3627 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003628}
3629
3630
3631static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3632 const u8 *peer_dev_addr)
3633{
3634 struct wpa_bss *bss;
3635 int updated;
3636
3637 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3638 if (bss == NULL)
3639 return -1;
3640 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3641 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3642 "last scan");
3643 return 0;
3644 }
3645
3646 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3647 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3648 "%ld.%06ld (%supdated in last scan)",
3649 bss->last_update.sec, bss->last_update.usec,
3650 updated ? "": "not ");
3651
3652 return updated;
3653}
3654
3655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003656static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3657 struct wpa_scan_results *scan_res)
3658{
3659 struct wpa_bss *bss;
3660 int freq;
3661 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003663 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3664
3665 if (wpa_s->global->p2p_disabled)
3666 return;
3667
Dmitry Shmidt04949592012-07-19 12:16:46 -07003668 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3669 scan_res ? (int) scan_res->num : -1,
3670 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671
3672 if (scan_res)
3673 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3674
Dmitry Shmidt04949592012-07-19 12:16:46 -07003675 if (wpa_s->p2p_auto_pd) {
3676 int join = wpas_p2p_peer_go(wpa_s,
3677 wpa_s->pending_join_dev_addr);
3678 if (join == 0 &&
3679 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3680 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003681 bss = wpa_bss_get_bssid_latest(
3682 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003683 if (bss) {
3684 freq = bss->freq;
3685 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3686 "the peer " MACSTR " at %d MHz",
3687 wpa_s->auto_pd_scan_retry,
3688 MAC2STR(wpa_s->
3689 pending_join_dev_addr),
3690 freq);
3691 wpas_p2p_join_scan_req(wpa_s, freq);
3692 return;
3693 }
3694 }
3695
3696 if (join < 0)
3697 join = 0;
3698
3699 wpa_s->p2p_auto_pd = 0;
3700 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3701 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3702 MAC2STR(wpa_s->pending_join_dev_addr), join);
3703 if (p2p_prov_disc_req(wpa_s->global->p2p,
3704 wpa_s->pending_join_dev_addr,
3705 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003706 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003707 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003708 wpa_msg_global(wpa_s, MSG_INFO,
3709 P2P_EVENT_PROV_DISC_FAILURE
3710 " p2p_dev_addr=" MACSTR " status=N/A",
3711 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003712 }
3713 return;
3714 }
3715
3716 if (wpa_s->p2p_auto_join) {
3717 int join = wpas_p2p_peer_go(wpa_s,
3718 wpa_s->pending_join_dev_addr);
3719 if (join < 0) {
3720 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3721 "running a GO -> use GO Negotiation");
3722 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3723 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3724 wpa_s->p2p_persistent_group, 0, 0, 0,
3725 wpa_s->p2p_go_intent,
3726 wpa_s->p2p_connect_freq,
3727 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003728 wpa_s->p2p_pd_before_go_neg,
3729 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003730 return;
3731 }
3732
3733 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3734 "try to join the group", join ? "" :
3735 " in older scan");
3736 if (!join)
3737 wpa_s->p2p_fallback_to_go_neg = 1;
3738 }
3739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3741 wpa_s->pending_join_iface_addr);
3742 if (freq < 0 &&
3743 p2p_get_interface_addr(wpa_s->global->p2p,
3744 wpa_s->pending_join_dev_addr,
3745 iface_addr) == 0 &&
3746 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3747 {
3748 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3749 "address for join from " MACSTR " to " MACSTR
3750 " based on newly discovered P2P peer entry",
3751 MAC2STR(wpa_s->pending_join_iface_addr),
3752 MAC2STR(iface_addr));
3753 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3754 ETH_ALEN);
3755
3756 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3757 wpa_s->pending_join_iface_addr);
3758 }
3759 if (freq >= 0) {
3760 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3761 "from P2P peer table: %d MHz", freq);
3762 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003763 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003764 if (bss) {
3765 freq = bss->freq;
3766 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3767 "from BSS table: %d MHz", freq);
3768 }
3769 if (freq > 0) {
3770 u16 method;
3771
Dmitry Shmidt04949592012-07-19 12:16:46 -07003772 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003773 wpa_msg_global(wpa_s->parent, MSG_INFO,
3774 P2P_EVENT_GROUP_FORMATION_FAILURE
3775 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003776 return;
3777 }
3778
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3780 "prior to joining an existing group (GO " MACSTR
3781 " freq=%u MHz)",
3782 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3783 wpa_s->pending_pd_before_join = 1;
3784
3785 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786 case WPS_PIN_DISPLAY:
3787 method = WPS_CONFIG_KEYPAD;
3788 break;
3789 case WPS_PIN_KEYPAD:
3790 method = WPS_CONFIG_DISPLAY;
3791 break;
3792 case WPS_PBC:
3793 method = WPS_CONFIG_PUSHBUTTON;
3794 break;
3795 default:
3796 method = 0;
3797 break;
3798 }
3799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003800 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3801 wpa_s->pending_join_dev_addr) ==
3802 method)) {
3803 /*
3804 * We have already performed provision discovery for
3805 * joining the group. Proceed directly to join
3806 * operation without duplicated provision discovery. */
3807 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3808 "with " MACSTR " already done - proceed to "
3809 "join",
3810 MAC2STR(wpa_s->pending_join_dev_addr));
3811 wpa_s->pending_pd_before_join = 0;
3812 goto start;
3813 }
3814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003815 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003816 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003817 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003818 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3819 "Discovery Request before joining an "
3820 "existing group");
3821 wpa_s->pending_pd_before_join = 0;
3822 goto start;
3823 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824 return;
3825 }
3826
3827 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3828 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3829 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3830 wpas_p2p_check_join_scan_limit(wpa_s);
3831 return;
3832
3833start:
3834 /* Start join operation immediately */
3835 wpas_p2p_join_start(wpa_s);
3836}
3837
3838
Dmitry Shmidt04949592012-07-19 12:16:46 -07003839static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003840{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003841 int ret;
3842 struct wpa_driver_scan_params params;
3843 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003844 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003845 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003846#ifdef ANDROID_P2P
3847 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003848
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003849 /* If freq is not provided, check the operating freq of the GO and do a
3850 * a directed scan to save time
3851 */
3852 if(!freq) {
3853 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3854 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3855 }
3856#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003857 os_memset(&params, 0, sizeof(params));
3858
3859 /* P2P Wildcard SSID */
3860 params.num_ssids = 1;
3861 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3862 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3863
3864 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003865 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3866 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3867 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868 if (wps_ie == NULL) {
3869 wpas_p2p_scan_res_join(wpa_s, NULL);
3870 return;
3871 }
3872
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003873 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3874 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003875 if (ies == NULL) {
3876 wpabuf_free(wps_ie);
3877 wpas_p2p_scan_res_join(wpa_s, NULL);
3878 return;
3879 }
3880 wpabuf_put_buf(ies, wps_ie);
3881 wpabuf_free(wps_ie);
3882
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003883 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003885 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003886 params.extra_ies = wpabuf_head(ies);
3887 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003888 if (freq > 0) {
3889 freqs[0] = freq;
3890 params.freqs = freqs;
3891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003892
3893 /*
3894 * Run a scan to update BSS table and start Provision Discovery once
3895 * the new scan results become available.
3896 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003897 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003898 if (!ret) {
3899 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003900 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003901 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902
3903 wpabuf_free(ies);
3904
3905 if (ret) {
3906 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3907 "try again later");
3908 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3909 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3910 wpas_p2p_check_join_scan_limit(wpa_s);
3911 }
3912}
3913
3914
Dmitry Shmidt04949592012-07-19 12:16:46 -07003915static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3916{
3917 struct wpa_supplicant *wpa_s = eloop_ctx;
3918 wpas_p2p_join_scan_req(wpa_s, 0);
3919}
3920
3921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003922static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003923 const u8 *dev_addr, enum p2p_wps_method wps_method,
3924 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925{
3926 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003927 MACSTR " dev " MACSTR ")%s",
3928 MAC2STR(iface_addr), MAC2STR(dev_addr),
3929 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003930
Dmitry Shmidt04949592012-07-19 12:16:46 -07003931 wpa_s->p2p_auto_pd = 0;
3932 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3934 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3935 wpa_s->pending_join_wps_method = wps_method;
3936
3937 /* Make sure we are not running find during connection establishment */
3938 wpas_p2p_stop_find(wpa_s);
3939
3940 wpa_s->p2p_join_scan_count = 0;
3941 wpas_p2p_join_scan(wpa_s, NULL);
3942 return 0;
3943}
3944
3945
3946static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3947{
3948 struct wpa_supplicant *group;
3949 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003950 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003951
3952 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3953 if (group == NULL)
3954 return -1;
3955 if (group != wpa_s) {
3956 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3957 sizeof(group->p2p_pin));
3958 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003959 } else {
3960 /*
3961 * Need to mark the current interface for p2p_group_formation
3962 * when a separate group interface is not used. This is needed
3963 * to allow p2p_cancel stop a pending p2p_connect-join.
3964 * wpas_p2p_init_group_interface() addresses this for the case
3965 * where a separate group interface is used.
3966 */
3967 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 }
3969
3970 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003971 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972
3973 os_memset(&res, 0, sizeof(res));
3974 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3975 ETH_ALEN);
3976 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003977 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003978 if (bss) {
3979 res.freq = bss->freq;
3980 res.ssid_len = bss->ssid_len;
3981 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3982 }
3983
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003984 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3985 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3986 "starting client");
3987 wpa_drv_cancel_remain_on_channel(wpa_s);
3988 wpa_s->off_channel_freq = 0;
3989 wpa_s->roc_waiting_drv_freq = 0;
3990 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003991 wpas_start_wps_enrollee(group, &res);
3992
3993 /*
3994 * Allow a longer timeout for join-a-running-group than normal 15
3995 * second group formation timeout since the GO may not have authorized
3996 * our connection yet.
3997 */
3998 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3999 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4000 wpa_s, NULL);
4001
4002 return 0;
4003}
4004
4005
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004006static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004007 int *force_freq, int *pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004008{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004009 int *freqs, res;
4010 unsigned int freq_in_use = 0, num, i;
4011
4012 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4013 if (!freqs)
4014 return -1;
4015
4016 num = get_shared_radio_freqs(wpa_s, freqs,
4017 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004018 wpa_printf(MSG_DEBUG,
4019 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4020 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004021
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004022 if (freq > 0) {
4023 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4024 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4025 "(%u MHz) is not supported for P2P uses",
4026 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004027 res = -3;
4028 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004029 }
4030
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004031 for (i = 0; i < num; i++) {
4032 if (freqs[i] == freq)
4033 freq_in_use = 1;
4034 }
4035
4036 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4037 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4038 freq);
4039 res = -2;
4040 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004041 }
4042 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4043 "requested channel (%u MHz)", freq);
4044 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004045 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004046 }
4047
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004048 for (i = 0; i < num; i++) {
4049 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4050 continue;
4051
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004052#ifndef ANDROID_P2P
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004053 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4054 *force_freq);
4055 *force_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004056#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004057
4058 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4059 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004060 *pref_freq = freqs[i];
4061#ifdef ANDROID_P2P
4062 } else {
4063 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4064 *force_freq);
4065 *force_freq = freqs[i];
4066#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004067 }
4068 break;
4069 }
4070
4071 if (i == num) {
4072 if (num < wpa_s->num_multichan_concurrent) {
4073 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4074 *force_freq = 0;
4075 } else {
4076 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4077 res = -2;
4078 goto exit_free;
4079 }
4080 }
4081
4082exit_ok:
4083 res = 0;
4084exit_free:
4085 os_free(freqs);
4086 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004087}
4088
4089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090/**
4091 * wpas_p2p_connect - Request P2P Group Formation to be started
4092 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4093 * @peer_addr: Address of the peer P2P Device
4094 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4095 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004096 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097 * @join: Whether to join an existing group (as a client) instead of starting
4098 * Group Owner negotiation; @peer_addr is BSSID in that case
4099 * @auth: Whether to only authorize the connection instead of doing that and
4100 * initiating Group Owner negotiation
4101 * @go_intent: GO Intent or -1 to use default
4102 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004103 * @persistent_id: Persistent group credentials to use for forcing GO
4104 * parameters or -1 to generate new values (SSID/passphrase)
4105 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4106 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004107 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004108 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4109 * failure, -2 on failure due to channel not currently available,
4110 * -3 if forced channel is not supported
4111 */
4112int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4113 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004114 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004115 int go_intent, int freq, int persistent_id, int pd,
4116 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004118 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004119 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004121 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004122 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123
4124 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4125 return -1;
4126
Dmitry Shmidt04949592012-07-19 12:16:46 -07004127 if (persistent_id >= 0) {
4128 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4129 if (ssid == NULL || ssid->disabled != 2 ||
4130 ssid->mode != WPAS_MODE_P2P_GO)
4131 return -1;
4132 }
4133
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004134 os_free(wpa_s->global->add_psk);
4135 wpa_s->global->add_psk = NULL;
4136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004137 if (go_intent < 0)
4138 go_intent = wpa_s->conf->p2p_go_intent;
4139
4140 if (!auth)
4141 wpa_s->p2p_long_listen = 0;
4142
4143 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004144 wpa_s->p2p_persistent_group = !!persistent_group;
4145 wpa_s->p2p_persistent_id = persistent_id;
4146 wpa_s->p2p_go_intent = go_intent;
4147 wpa_s->p2p_connect_freq = freq;
4148 wpa_s->p2p_fallback_to_go_neg = 0;
4149 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004150 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151
4152 if (pin)
4153 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4154 else if (wps_method == WPS_PIN_DISPLAY) {
4155 ret = wps_generate_pin();
4156 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4157 ret);
4158 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4159 wpa_s->p2p_pin);
4160 } else
4161 wpa_s->p2p_pin[0] = '\0';
4162
Dmitry Shmidt04949592012-07-19 12:16:46 -07004163 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004164 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4165 if (auth) {
4166 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4167 "connect a running group from " MACSTR,
4168 MAC2STR(peer_addr));
4169 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4170 return ret;
4171 }
4172 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4173 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4174 iface_addr) < 0) {
4175 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4176 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4177 dev_addr);
4178 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004179 if (auto_join) {
4180 os_get_time(&wpa_s->p2p_auto_started);
4181 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4182 "%ld.%06ld",
4183 wpa_s->p2p_auto_started.sec,
4184 wpa_s->p2p_auto_started.usec);
4185 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004186 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004187 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4188 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004189 return -1;
4190 return ret;
4191 }
4192
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004193 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004194 if (res)
4195 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004196 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197
4198 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004200 if (wpa_s->create_p2p_iface) {
4201 /* Prepare to add a new interface for the group */
4202 iftype = WPA_IF_P2P_GROUP;
4203 if (go_intent == 15)
4204 iftype = WPA_IF_P2P_GO;
4205 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4206 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4207 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004208 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004209 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004210
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004211 if_addr = wpa_s->pending_interface_addr;
4212 } else
4213 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004214
4215 if (auth) {
4216 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004217 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004218 force_freq, persistent_group, ssid,
4219 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220 return -1;
4221 return ret;
4222 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004223
4224 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4225 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004226 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004227 if (wpa_s->create_p2p_iface)
4228 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004229 return -1;
4230 }
4231 return ret;
4232}
4233
4234
4235/**
4236 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4237 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4238 * @freq: Frequency of the channel in MHz
4239 * @duration: Duration of the stay on the channel in milliseconds
4240 *
4241 * This callback is called when the driver indicates that it has started the
4242 * requested remain-on-channel duration.
4243 */
4244void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4245 unsigned int freq, unsigned int duration)
4246{
4247 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4248 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4250 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4251 wpa_s->pending_listen_duration);
4252 wpa_s->pending_listen_freq = 0;
4253 }
4254}
4255
4256
4257static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4258 unsigned int timeout)
4259{
4260 /* Limit maximum Listen state time based on driver limitation. */
4261 if (timeout > wpa_s->max_remain_on_chan)
4262 timeout = wpa_s->max_remain_on_chan;
4263
4264 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4265 return wpa_drv_p2p_listen(wpa_s, timeout);
4266
4267 return p2p_listen(wpa_s->global->p2p, timeout);
4268}
4269
4270
4271/**
4272 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4273 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4274 * @freq: Frequency of the channel in MHz
4275 *
4276 * This callback is called when the driver indicates that a remain-on-channel
4277 * operation has been completed, i.e., the duration on the requested channel
4278 * has timed out.
4279 */
4280void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4281 unsigned int freq)
4282{
4283 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4284 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004285 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004286 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4287 return;
4288 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4289 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004290 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004291 return;
4292 if (wpa_s->p2p_long_listen > 0)
4293 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4294 if (wpa_s->p2p_long_listen > 0) {
4295 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4296 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004297 } else {
4298 /*
4299 * When listen duration is over, stop listen & update p2p_state
4300 * to IDLE.
4301 */
4302 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303 }
4304}
4305
4306
4307/**
4308 * wpas_p2p_group_remove - Remove a P2P group
4309 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4310 * @ifname: Network interface name of the group interface or "*" to remove all
4311 * groups
4312 * Returns: 0 on success, -1 on failure
4313 *
4314 * This function is used to remove a P2P group. This can be used to disconnect
4315 * from a group in which the local end is a P2P Client or to end a P2P Group in
4316 * case the local end is the Group Owner. If a virtual network interface was
4317 * created for this group, that interface will be removed. Otherwise, only the
4318 * configured P2P group network will be removed from the interface.
4319 */
4320int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4321{
4322 struct wpa_global *global = wpa_s->global;
4323
4324 if (os_strcmp(ifname, "*") == 0) {
4325 struct wpa_supplicant *prev;
4326 wpa_s = global->ifaces;
4327 while (wpa_s) {
4328 prev = wpa_s;
4329 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004330 if (prev->p2p_group_interface !=
4331 NOT_P2P_GROUP_INTERFACE ||
4332 (prev->current_ssid &&
4333 prev->current_ssid->p2p_group))
4334 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004335 }
4336 return 0;
4337 }
4338
4339 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4340 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4341 break;
4342 }
4343
4344 return wpas_p2p_disconnect(wpa_s);
4345}
4346
4347
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004348static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4349{
4350 unsigned int r;
4351
4352 if (freq == 2) {
4353 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4354 "band");
4355 if (wpa_s->best_24_freq > 0 &&
4356 p2p_supported_freq(wpa_s->global->p2p,
4357 wpa_s->best_24_freq)) {
4358 freq = wpa_s->best_24_freq;
4359 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4360 "channel: %d MHz", freq);
4361 } else {
4362 os_get_random((u8 *) &r, sizeof(r));
4363 freq = 2412 + (r % 3) * 25;
4364 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4365 "channel: %d MHz", freq);
4366 }
4367 }
4368
4369 if (freq == 5) {
4370 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4371 "band");
4372 if (wpa_s->best_5_freq > 0 &&
4373 p2p_supported_freq(wpa_s->global->p2p,
4374 wpa_s->best_5_freq)) {
4375 freq = wpa_s->best_5_freq;
4376 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4377 "channel: %d MHz", freq);
4378 } else {
4379 os_get_random((u8 *) &r, sizeof(r));
4380 freq = 5180 + (r % 4) * 20;
4381 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4382 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4383 "5 GHz channel for P2P group");
4384 return -1;
4385 }
4386 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4387 "channel: %d MHz", freq);
4388 }
4389 }
4390
4391 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4392 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4393 "(%u MHz) is not supported for P2P uses",
4394 freq);
4395 return -1;
4396 }
4397
4398 return freq;
4399}
4400
4401
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004402static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4403 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004404 int freq, int ht40,
4405 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004406{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004407 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004408 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004409 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004410
4411 os_memset(params, 0, sizeof(*params));
4412 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004413 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004414 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004415 if (!freq_included(channels, freq)) {
4416 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4417 "accepted", freq);
4418 return -1;
4419 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004420 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4421 "frequency %d MHz", freq);
4422 params->freq = freq;
4423 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4424 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004425 wpa_s->conf->p2p_oper_channel <= 11 &&
4426 freq_included(channels,
4427 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4429 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4430 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004431 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4432 wpa_s->conf->p2p_oper_reg_class == 116 ||
4433 wpa_s->conf->p2p_oper_reg_class == 117 ||
4434 wpa_s->conf->p2p_oper_reg_class == 124 ||
4435 wpa_s->conf->p2p_oper_reg_class == 126 ||
4436 wpa_s->conf->p2p_oper_reg_class == 127) &&
4437 freq_included(channels,
4438 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004439 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4440 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4441 "frequency %d MHz", params->freq);
4442 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4443 wpa_s->best_overall_freq > 0 &&
4444 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004445 wpa_s->best_overall_freq) &&
4446 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004447 params->freq = wpa_s->best_overall_freq;
4448 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4449 "channel %d MHz", params->freq);
4450 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4451 wpa_s->best_24_freq > 0 &&
4452 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004453 wpa_s->best_24_freq) &&
4454 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004455 params->freq = wpa_s->best_24_freq;
4456 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4457 "channel %d MHz", params->freq);
4458 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4459 wpa_s->best_5_freq > 0 &&
4460 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004461 wpa_s->best_5_freq) &&
4462 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004463 params->freq = wpa_s->best_5_freq;
4464 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4465 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004466 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4467 channels))) {
4468 params->freq = pref_freq;
4469 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4470 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004471 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004472 int chan;
4473 for (chan = 0; chan < 11; chan++) {
4474 params->freq = 2412 + chan * 5;
4475 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004476 params->freq) &&
4477 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004478 break;
4479 }
4480 if (chan == 11) {
4481 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4482 "allowed");
4483 return -1;
4484 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004485 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4486 "known)", params->freq);
4487 }
4488
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004489 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4490 if (!freqs)
4491 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004492
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004493 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4494 wpa_s->num_multichan_concurrent);
4495 if (res < 0) {
4496 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004497 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004499 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004500
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004501 if (!freq) {
4502 for (i = 0; i < num; i++) {
4503 if (freq_included(channels, freqs[i])) {
4504 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4505 freqs[i]);
4506 params->freq = freqs[i];
4507 break;
4508 }
4509 }
4510
4511 if (i == num) {
4512 if (num == wpa_s->num_multichan_concurrent) {
4513 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4514 os_free(freqs);
4515 return -1;
4516 } else {
4517 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4518 }
4519 }
4520 } else {
4521 for (i = 0; i < num; i++) {
4522 if (freqs[i] == freq)
4523 break;
4524 }
4525
4526 if (i == num) {
4527 if (num == wpa_s->num_multichan_concurrent) {
4528 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4529 os_free(freqs);
4530 return -1;
4531 } else {
4532 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4533 }
4534 }
4535 }
4536 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004537 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004538}
4539
4540
4541static struct wpa_supplicant *
4542wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4543 int go)
4544{
4545 struct wpa_supplicant *group_wpa_s;
4546
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004547 if (!wpas_p2p_create_iface(wpa_s)) {
4548 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4549 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004550 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004551 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004552 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553
4554 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004555 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004556 wpa_msg_global(wpa_s, MSG_ERROR,
4557 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004558 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004559 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004560 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4561 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004562 wpa_msg_global(wpa_s, MSG_ERROR,
4563 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564 wpas_p2p_remove_pending_group_interface(wpa_s);
4565 return NULL;
4566 }
4567
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004568 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4569 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004570 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571 return group_wpa_s;
4572}
4573
4574
4575/**
4576 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4577 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4578 * @persistent_group: Whether to create a persistent group
4579 * @freq: Frequency for the group or 0 to indicate no hardcoding
4580 * Returns: 0 on success, -1 on failure
4581 *
4582 * This function creates a new P2P group with the local end as the Group Owner,
4583 * i.e., without using Group Owner Negotiation.
4584 */
4585int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004586 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587{
4588 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589
4590 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4591 return -1;
4592
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004593 os_free(wpa_s->global->add_psk);
4594 wpa_s->global->add_psk = NULL;
4595
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004596 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004597 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004598 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004599
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004600 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4601 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004603
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004604 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004605 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004606 if (params.freq &&
4607 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4608 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4609 "(%u MHz) is not supported for P2P uses",
4610 params.freq);
4611 return -1;
4612 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004613 p2p_go_params(wpa_s->global->p2p, &params);
4614 params.persistent_group = persistent_group;
4615
4616 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4617 if (wpa_s == NULL)
4618 return -1;
4619 wpas_start_wps_go(wpa_s, &params, 0);
4620
4621 return 0;
4622}
4623
4624
4625static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4626 struct wpa_ssid *params, int addr_allocated)
4627{
4628 struct wpa_ssid *ssid;
4629
4630 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4631 if (wpa_s == NULL)
4632 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004633 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004634
4635 wpa_supplicant_ap_deinit(wpa_s);
4636
4637 ssid = wpa_config_add_network(wpa_s->conf);
4638 if (ssid == NULL)
4639 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640 wpa_config_set_network_defaults(ssid);
4641 ssid->temporary = 1;
4642 ssid->proto = WPA_PROTO_RSN;
4643 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4644 ssid->group_cipher = WPA_CIPHER_CCMP;
4645 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4646 ssid->ssid = os_malloc(params->ssid_len);
4647 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648 wpa_config_remove_network(wpa_s->conf, ssid->id);
4649 return -1;
4650 }
4651 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4652 ssid->ssid_len = params->ssid_len;
4653 ssid->p2p_group = 1;
4654 ssid->export_keys = 1;
4655 if (params->psk_set) {
4656 os_memcpy(ssid->psk, params->psk, 32);
4657 ssid->psk_set = 1;
4658 }
4659 if (params->passphrase)
4660 ssid->passphrase = os_strdup(params->passphrase);
4661
4662 wpa_supplicant_select_network(wpa_s, ssid);
4663
4664 wpa_s->show_group_started = 1;
4665
4666 return 0;
4667}
4668
4669
4670int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4671 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004672 int freq, int ht40,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004673 const struct p2p_channels *channels,
4674 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004675{
4676 struct p2p_go_neg_results params;
4677 int go = 0;
4678
4679 if (ssid->disabled != 2 || ssid->ssid == NULL)
4680 return -1;
4681
4682 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4683 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4684 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4685 "already running");
4686 return 0;
4687 }
4688
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004689 os_free(wpa_s->global->add_psk);
4690 wpa_s->global->add_psk = NULL;
4691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004693 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004694
Dmitry Shmidt04949592012-07-19 12:16:46 -07004695 wpa_s->p2p_fallback_to_go_neg = 0;
4696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004697 if (ssid->mode == WPAS_MODE_INFRA)
4698 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4699
4700 if (ssid->mode != WPAS_MODE_P2P_GO)
4701 return -1;
4702
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004703 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4704 if (freq < 0)
4705 return -1;
4706
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004707 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004708 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004709
4710 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004711 params.psk_set = ssid->psk_set;
4712 if (params.psk_set)
4713 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004714 if (ssid->passphrase) {
4715 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4716 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4717 "persistent group");
4718 return -1;
4719 }
4720 os_strlcpy(params.passphrase, ssid->passphrase,
4721 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4724 params.ssid_len = ssid->ssid_len;
4725 params.persistent_group = 1;
4726
4727 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4728 if (wpa_s == NULL)
4729 return -1;
4730
Dmitry Shmidt56052862013-10-04 10:23:25 -07004731 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004732 wpas_start_wps_go(wpa_s, &params, 0);
4733
4734 return 0;
4735}
4736
4737
4738static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4739 struct wpabuf *proberesp_ies)
4740{
4741 struct wpa_supplicant *wpa_s = ctx;
4742 if (wpa_s->ap_iface) {
4743 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004744 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4745 wpabuf_free(beacon_ies);
4746 wpabuf_free(proberesp_ies);
4747 return;
4748 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004749 if (beacon_ies) {
4750 wpabuf_free(hapd->p2p_beacon_ie);
4751 hapd->p2p_beacon_ie = beacon_ies;
4752 }
4753 wpabuf_free(hapd->p2p_probe_resp_ie);
4754 hapd->p2p_probe_resp_ie = proberesp_ies;
4755 } else {
4756 wpabuf_free(beacon_ies);
4757 wpabuf_free(proberesp_ies);
4758 }
4759 wpa_supplicant_ap_update_beacon(wpa_s);
4760}
4761
4762
4763static void wpas_p2p_idle_update(void *ctx, int idle)
4764{
4765 struct wpa_supplicant *wpa_s = ctx;
4766 if (!wpa_s->ap_iface)
4767 return;
4768 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004769 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770 wpas_p2p_set_group_idle_timeout(wpa_s);
4771 else
4772 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4773}
4774
4775
4776struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004777 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778{
4779 struct p2p_group *group;
4780 struct p2p_group_config *cfg;
4781
4782 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4783 return NULL;
4784 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4785 return NULL;
4786
4787 cfg = os_zalloc(sizeof(*cfg));
4788 if (cfg == NULL)
4789 return NULL;
4790
Dmitry Shmidt04949592012-07-19 12:16:46 -07004791 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004792 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004793 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004794 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4796 if (wpa_s->max_stations &&
4797 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4798 cfg->max_clients = wpa_s->max_stations;
4799 else
4800 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004801 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4802 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004803 cfg->cb_ctx = wpa_s;
4804 cfg->ie_update = wpas_p2p_ie_update;
4805 cfg->idle_update = wpas_p2p_idle_update;
4806
4807 group = p2p_group_init(wpa_s->global->p2p, cfg);
4808 if (group == NULL)
4809 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004810 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811 p2p_group_notif_formation_done(group);
4812 wpa_s->p2p_group = group;
4813 return group;
4814}
4815
4816
4817void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4818 int registrar)
4819{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004820 struct wpa_ssid *ssid = wpa_s->current_ssid;
4821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004822 if (!wpa_s->p2p_in_provisioning) {
4823 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4824 "provisioning not in progress");
4825 return;
4826 }
4827
Dmitry Shmidt04949592012-07-19 12:16:46 -07004828 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4829 u8 go_dev_addr[ETH_ALEN];
4830 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4831 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4832 ssid->ssid_len);
4833 /* Clear any stored provisioning info */
4834 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4835 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4838 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004839 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004840 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4841 /*
4842 * Use a separate timeout for initial data connection to
4843 * complete to allow the group to be removed automatically if
4844 * something goes wrong in this step before the P2P group idle
4845 * timeout mechanism is taken into use.
4846 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07004847 wpa_dbg(wpa_s, MSG_DEBUG,
4848 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4849 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004850 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4851 wpas_p2p_group_formation_timeout,
4852 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004853 } else if (ssid) {
4854 /*
4855 * Use a separate timeout for initial data connection to
4856 * complete to allow the group to be removed automatically if
4857 * the client does not complete data connection successfully.
4858 */
4859 wpa_dbg(wpa_s, MSG_DEBUG,
4860 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
4861 P2P_MAX_INITIAL_CONN_WAIT_GO);
4862 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
4863 wpas_p2p_group_formation_timeout,
4864 wpa_s->parent, NULL);
4865 /*
4866 * Complete group formation on first successful data connection
4867 */
4868 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004869 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870 if (wpa_s->global->p2p)
4871 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4872 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4873 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4874 wpas_group_formation_completed(wpa_s, 1);
4875}
4876
4877
Jouni Malinen75ecf522011-06-27 15:19:46 -07004878void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4879 struct wps_event_fail *fail)
4880{
4881 if (!wpa_s->p2p_in_provisioning) {
4882 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4883 "provisioning not in progress");
4884 return;
4885 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004886
4887 if (wpa_s->go_params) {
4888 p2p_clear_provisioning_info(
4889 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004890 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004891 }
4892
Jouni Malinen75ecf522011-06-27 15:19:46 -07004893 wpas_notify_p2p_wps_failed(wpa_s, fail);
4894}
4895
4896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004897int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004898 const char *config_method,
4899 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004900{
4901 u16 config_methods;
4902
Dmitry Shmidt04949592012-07-19 12:16:46 -07004903 wpa_s->p2p_fallback_to_go_neg = 0;
4904 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004905 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004906 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004907 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004909 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4910 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004911 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004912 else {
4913 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004914 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004915 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916
Dmitry Shmidt04949592012-07-19 12:16:46 -07004917 if (use == WPAS_P2P_PD_AUTO) {
4918 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4919 wpa_s->pending_pd_config_methods = config_methods;
4920 wpa_s->p2p_auto_pd = 1;
4921 wpa_s->p2p_auto_join = 0;
4922 wpa_s->pending_pd_before_join = 0;
4923 wpa_s->auto_pd_scan_retry = 0;
4924 wpas_p2p_stop_find(wpa_s);
4925 wpa_s->p2p_join_scan_count = 0;
4926 os_get_time(&wpa_s->p2p_auto_started);
4927 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4928 wpa_s->p2p_auto_started.sec,
4929 wpa_s->p2p_auto_started.usec);
4930 wpas_p2p_join_scan(wpa_s, NULL);
4931 return 0;
4932 }
4933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004934 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4935 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004936 config_methods,
4937 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004938 }
4939
4940 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4941 return -1;
4942
4943 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004944 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004945 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004946}
4947
4948
4949int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4950 char *end)
4951{
4952 return p2p_scan_result_text(ies, ies_len, buf, end);
4953}
4954
4955
4956static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4957{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004958 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959 return;
4960
4961 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4962 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004963 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004964}
4965
4966
4967int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4968 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004969 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004970 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004971{
4972 wpas_p2p_clear_pending_action_tx(wpa_s);
4973 wpa_s->p2p_long_listen = 0;
4974
4975 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4976 return wpa_drv_p2p_find(wpa_s, timeout, type);
4977
Dmitry Shmidt04949592012-07-19 12:16:46 -07004978 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4979 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004980 return -1;
4981
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004982 wpa_supplicant_cancel_sched_scan(wpa_s);
4983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004984 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004985 num_req_dev_types, req_dev_types, dev_id,
4986 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987}
4988
4989
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004990static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991{
4992 wpas_p2p_clear_pending_action_tx(wpa_s);
4993 wpa_s->p2p_long_listen = 0;
4994 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4995 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004996 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997
4998 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4999 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005000 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005001 }
5002
5003 if (wpa_s->global->p2p)
5004 p2p_stop_find(wpa_s->global->p2p);
5005
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005006 return 0;
5007}
5008
5009
5010void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5011{
5012 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5013 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 wpas_p2p_remove_pending_group_interface(wpa_s);
5015}
5016
5017
5018static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5019{
5020 struct wpa_supplicant *wpa_s = eloop_ctx;
5021 wpa_s->p2p_long_listen = 0;
5022}
5023
5024
5025int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5026{
5027 int res;
5028
5029 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5030 return -1;
5031
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005032 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005033 wpas_p2p_clear_pending_action_tx(wpa_s);
5034
5035 if (timeout == 0) {
5036 /*
5037 * This is a request for unlimited Listen state. However, at
5038 * least for now, this is mapped to a Listen state for one
5039 * hour.
5040 */
5041 timeout = 3600;
5042 }
5043 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5044 wpa_s->p2p_long_listen = 0;
5045
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005046 /*
5047 * Stop previous find/listen operation to avoid trying to request a new
5048 * remain-on-channel operation while the driver is still running the
5049 * previous one.
5050 */
5051 if (wpa_s->global->p2p)
5052 p2p_stop_find(wpa_s->global->p2p);
5053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005054 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5055 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5056 wpa_s->p2p_long_listen = timeout * 1000;
5057 eloop_register_timeout(timeout, 0,
5058 wpas_p2p_long_listen_timeout,
5059 wpa_s, NULL);
5060 }
5061
5062 return res;
5063}
5064
5065
5066int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5067 u8 *buf, size_t len, int p2p_group)
5068{
5069 struct wpabuf *p2p_ie;
5070 int ret;
5071
5072 if (wpa_s->global->p2p_disabled)
5073 return -1;
5074 if (wpa_s->global->p2p == NULL)
5075 return -1;
5076 if (bss == NULL)
5077 return -1;
5078
5079 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5080 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5081 p2p_group, p2p_ie);
5082 wpabuf_free(p2p_ie);
5083
5084 return ret;
5085}
5086
5087
5088int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005089 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005090 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005091{
5092 if (wpa_s->global->p2p_disabled)
5093 return 0;
5094 if (wpa_s->global->p2p == NULL)
5095 return 0;
5096
Dmitry Shmidt04949592012-07-19 12:16:46 -07005097 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5098 ie, ie_len)) {
5099 case P2P_PREQ_NOT_P2P:
5100 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5101 ssi_signal);
5102 /* fall through */
5103 case P2P_PREQ_MALFORMED:
5104 case P2P_PREQ_NOT_LISTEN:
5105 case P2P_PREQ_NOT_PROCESSED:
5106 default: /* make gcc happy */
5107 return 0;
5108 case P2P_PREQ_PROCESSED:
5109 return 1;
5110 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005111}
5112
5113
5114void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5115 const u8 *sa, const u8 *bssid,
5116 u8 category, const u8 *data, size_t len, int freq)
5117{
5118 if (wpa_s->global->p2p_disabled)
5119 return;
5120 if (wpa_s->global->p2p == NULL)
5121 return;
5122
5123 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5124 freq);
5125}
5126
5127
5128void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5129{
5130 if (wpa_s->global->p2p_disabled)
5131 return;
5132 if (wpa_s->global->p2p == NULL)
5133 return;
5134
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005135 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005136}
5137
5138
5139void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5140{
5141 p2p_group_deinit(wpa_s->p2p_group);
5142 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005143
5144 wpa_s->ap_configured_cb = NULL;
5145 wpa_s->ap_configured_cb_ctx = NULL;
5146 wpa_s->ap_configured_cb_data = NULL;
5147 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005148}
5149
5150
5151int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5152{
5153 wpa_s->p2p_long_listen = 0;
5154
5155 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5156 return wpa_drv_p2p_reject(wpa_s, addr);
5157
5158 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5159 return -1;
5160
5161 return p2p_reject(wpa_s->global->p2p, addr);
5162}
5163
5164
5165/* Invite to reinvoke a persistent group */
5166int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005167 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005168 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005169{
5170 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005171 u8 *bssid = NULL;
5172 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005173 int res;
5174
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005175 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005176 if (peer_addr)
5177 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5178 else
5179 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180
Jouni Malinen31be0a42012-08-31 21:20:51 +03005181 wpa_s->p2p_persistent_go_freq = freq;
5182 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005183 if (ssid->mode == WPAS_MODE_P2P_GO) {
5184 role = P2P_INVITE_ROLE_GO;
5185 if (peer_addr == NULL) {
5186 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5187 "address in invitation command");
5188 return -1;
5189 }
5190 if (wpas_p2p_create_iface(wpa_s)) {
5191 if (wpas_p2p_add_group_interface(wpa_s,
5192 WPA_IF_P2P_GO) < 0) {
5193 wpa_printf(MSG_ERROR, "P2P: Failed to "
5194 "allocate a new interface for the "
5195 "group");
5196 return -1;
5197 }
5198 bssid = wpa_s->pending_interface_addr;
5199 } else
5200 bssid = wpa_s->own_addr;
5201 } else {
5202 role = P2P_INVITE_ROLE_CLIENT;
5203 peer_addr = ssid->bssid;
5204 }
5205 wpa_s->pending_invite_ssid_id = ssid->id;
5206
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005207 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005208 if (res)
5209 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005211 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5212 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5213 ssid->ssid, ssid->ssid_len,
5214 go_dev_addr, 1);
5215
5216 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5217 return -1;
5218
5219 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005220 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5221 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005222}
5223
5224
5225/* Invite to join an active group */
5226int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5227 const u8 *peer_addr, const u8 *go_dev_addr)
5228{
5229 struct wpa_global *global = wpa_s->global;
5230 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005231 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005232 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005233 int persistent;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005234 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005235 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005236
Jouni Malinen31be0a42012-08-31 21:20:51 +03005237 wpa_s->p2p_persistent_go_freq = 0;
5238 wpa_s->p2p_go_ht40 = 0;
5239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005240 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5241 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5242 break;
5243 }
5244 if (wpa_s == NULL) {
5245 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5246 return -1;
5247 }
5248
5249 ssid = wpa_s->current_ssid;
5250 if (ssid == NULL) {
5251 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5252 "invitation");
5253 return -1;
5254 }
5255
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005256 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005257 persistent = ssid->p2p_persistent_group &&
5258 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5259 ssid->ssid, ssid->ssid_len);
5260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005261 if (ssid->mode == WPAS_MODE_P2P_GO) {
5262 role = P2P_INVITE_ROLE_ACTIVE_GO;
5263 bssid = wpa_s->own_addr;
5264 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005265 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005266 } else {
5267 role = P2P_INVITE_ROLE_CLIENT;
5268 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5269 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5270 "invite to current group");
5271 return -1;
5272 }
5273 bssid = wpa_s->bssid;
5274 if (go_dev_addr == NULL &&
5275 !is_zero_ether_addr(wpa_s->go_dev_addr))
5276 go_dev_addr = wpa_s->go_dev_addr;
5277 }
5278 wpa_s->parent->pending_invite_ssid_id = -1;
5279
5280 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5281 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5282 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005283 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005284
5285 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5286 return -1;
5287
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005288 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005289 if (res)
5290 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005291 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005293 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005294 ssid->ssid, ssid->ssid_len, force_freq,
5295 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005296}
5297
5298
5299void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5300{
5301 struct wpa_ssid *ssid = wpa_s->current_ssid;
5302 const char *ssid_txt;
5303 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005304 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005306 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005307
Dmitry Shmidt04949592012-07-19 12:16:46 -07005308 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5309 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5310 wpa_s->parent, NULL);
5311 }
5312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005313 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005314 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005315
5316 wpa_s->show_group_started = 0;
5317
5318 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5319 os_memset(go_dev_addr, 0, ETH_ALEN);
5320 if (ssid->bssid_set)
5321 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5322 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5323 ssid->ssid_len);
5324 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5325
5326 if (wpa_s->global->p2p_group_formation == wpa_s)
5327 wpa_s->global->p2p_group_formation = NULL;
5328
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005329 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5330 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005331 if (ssid->passphrase == NULL && ssid->psk_set) {
5332 char psk[65];
5333 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005334 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5335 "%s client ssid=\"%s\" freq=%d psk=%s "
5336 "go_dev_addr=" MACSTR "%s",
5337 wpa_s->ifname, ssid_txt, freq, psk,
5338 MAC2STR(go_dev_addr),
5339 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005340 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005341 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5342 "%s client ssid=\"%s\" freq=%d "
5343 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5344 wpa_s->ifname, ssid_txt, freq,
5345 ssid->passphrase ? ssid->passphrase : "",
5346 MAC2STR(go_dev_addr),
5347 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005348 }
5349
5350 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005351 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5352 ssid, go_dev_addr);
5353 if (network_id < 0)
5354 network_id = ssid->id;
5355 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005356
5357done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005358 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005359}
5360
5361
5362int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5363 u32 interval1, u32 duration2, u32 interval2)
5364{
5365 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5366 return -1;
5367 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5368 return -1;
5369
5370 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5371 wpa_s->current_ssid == NULL ||
5372 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5373 return -1;
5374
5375 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5376 wpa_s->own_addr, wpa_s->assoc_freq,
5377 duration1, interval1, duration2, interval2);
5378}
5379
5380
5381int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5382 unsigned int interval)
5383{
5384 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5385 return -1;
5386
5387 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5388 return -1;
5389
5390 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5391}
5392
5393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005394static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5395{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005396 if (wpa_s->current_ssid == NULL) {
5397 /*
5398 * current_ssid can be cleared when P2P client interface gets
5399 * disconnected, so assume this interface was used as P2P
5400 * client.
5401 */
5402 return 1;
5403 }
5404 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005405 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5406}
5407
5408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005409static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5410{
5411 struct wpa_supplicant *wpa_s = eloop_ctx;
5412
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005413 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005414 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5415 "disabled");
5416 return;
5417 }
5418
Dmitry Shmidt04949592012-07-19 12:16:46 -07005419 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5420 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005421 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005422}
5423
5424
5425static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5426{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005427 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005428
Dmitry Shmidt04949592012-07-19 12:16:46 -07005429 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5430 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5431
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005432 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5433 return;
5434
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005435 timeout = wpa_s->conf->p2p_group_idle;
5436 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5437 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5438 timeout = P2P_MAX_CLIENT_IDLE;
5439
5440 if (timeout == 0)
5441 return;
5442
Dmitry Shmidt04949592012-07-19 12:16:46 -07005443 if (timeout < 0) {
5444 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5445 timeout = 0; /* special client mode no-timeout */
5446 else
5447 return;
5448 }
5449
5450 if (wpa_s->p2p_in_provisioning) {
5451 /*
5452 * Use the normal group formation timeout during the
5453 * provisioning phase to avoid terminating this process too
5454 * early due to group idle timeout.
5455 */
5456 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5457 "during provisioning");
5458 return;
5459 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305460
Dmitry Shmidt04949592012-07-19 12:16:46 -07005461 if (wpa_s->show_group_started) {
5462 /*
5463 * Use the normal group formation timeout between the end of
5464 * the provisioning phase and completion of 4-way handshake to
5465 * avoid terminating this process too early due to group idle
5466 * timeout.
5467 */
5468 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5469 "while waiting for initial 4-way handshake to "
5470 "complete");
5471 return;
5472 }
5473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005474 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005475 timeout);
5476 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5477 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005478}
5479
5480
Jouni Malinen2b89da82012-08-31 22:04:41 +03005481/* Returns 1 if the interface was removed */
5482int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5483 u16 reason_code, const u8 *ie, size_t ie_len,
5484 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005485{
5486 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005487 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005488 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005489 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005490
Dmitry Shmidt04949592012-07-19 12:16:46 -07005491 if (!locally_generated)
5492 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5493 ie_len);
5494
5495 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5496 wpa_s->current_ssid &&
5497 wpa_s->current_ssid->p2p_group &&
5498 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5499 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5500 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005501 if (wpas_p2p_group_delete(wpa_s,
5502 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5503 > 0)
5504 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005505 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005506
5507 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508}
5509
5510
5511void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005512 u16 reason_code, const u8 *ie, size_t ie_len,
5513 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005514{
5515 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5516 return;
5517 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5518 return;
5519
Dmitry Shmidt04949592012-07-19 12:16:46 -07005520 if (!locally_generated)
5521 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5522 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005523}
5524
5525
5526void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5527{
5528 struct p2p_data *p2p = wpa_s->global->p2p;
5529
5530 if (p2p == NULL)
5531 return;
5532
5533 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5534 return;
5535
5536 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5537 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5538
5539 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5540 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5541
5542 if (wpa_s->wps &&
5543 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5544 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5545
5546 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5547 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5548
5549 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5550 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5551 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5552 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5553 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5554 }
5555
5556 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5557 p2p_set_sec_dev_types(p2p,
5558 (void *) wpa_s->conf->sec_device_type,
5559 wpa_s->conf->num_sec_device_types);
5560
5561 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5562 int i;
5563 p2p_remove_wps_vendor_extensions(p2p);
5564 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5565 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5566 continue;
5567 p2p_add_wps_vendor_extension(
5568 p2p, wpa_s->conf->wps_vendor_ext[i]);
5569 }
5570 }
5571
5572 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5573 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5574 char country[3];
5575 country[0] = wpa_s->conf->country[0];
5576 country[1] = wpa_s->conf->country[1];
5577 country[2] = 0x04;
5578 p2p_set_country(p2p, country);
5579 }
5580
5581 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5582 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5583 wpa_s->conf->p2p_ssid_postfix ?
5584 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5585 0);
5586 }
5587
5588 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5589 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005590
5591 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5592 u8 reg_class, channel;
5593 int ret;
5594 unsigned int r;
5595 if (wpa_s->conf->p2p_listen_reg_class &&
5596 wpa_s->conf->p2p_listen_channel) {
5597 reg_class = wpa_s->conf->p2p_listen_reg_class;
5598 channel = wpa_s->conf->p2p_listen_channel;
5599 } else {
5600 reg_class = 81;
5601 /*
5602 * Pick one of the social channels randomly as the
5603 * listen channel.
5604 */
5605 os_get_random((u8 *) &r, sizeof(r));
5606 channel = 1 + (r % 3) * 5;
5607 }
5608 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5609 if (ret)
5610 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5611 "failed: %d", ret);
5612 }
5613 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5614 u8 op_reg_class, op_channel, cfg_op_channel;
5615 int ret = 0;
5616 unsigned int r;
5617 if (wpa_s->conf->p2p_oper_reg_class &&
5618 wpa_s->conf->p2p_oper_channel) {
5619 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5620 op_channel = wpa_s->conf->p2p_oper_channel;
5621 cfg_op_channel = 1;
5622 } else {
5623 op_reg_class = 81;
5624 /*
5625 * Use random operation channel from (1, 6, 11)
5626 *if no other preference is indicated.
5627 */
5628 os_get_random((u8 *) &r, sizeof(r));
5629 op_channel = 1 + (r % 3) * 5;
5630 cfg_op_channel = 0;
5631 }
5632 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5633 cfg_op_channel);
5634 if (ret)
5635 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5636 "failed: %d", ret);
5637 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005638
5639 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5640 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5641 wpa_s->conf->p2p_pref_chan) < 0) {
5642 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5643 "update failed");
5644 }
5645 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005646}
5647
5648
5649int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5650 int duration)
5651{
5652 if (!wpa_s->ap_iface)
5653 return -1;
5654 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5655 duration);
5656}
5657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005658
5659int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5660{
5661 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5662 return -1;
5663 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5664 return -1;
5665
5666 wpa_s->global->cross_connection = enabled;
5667 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5668
5669 if (!enabled) {
5670 struct wpa_supplicant *iface;
5671
5672 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5673 {
5674 if (iface->cross_connect_enabled == 0)
5675 continue;
5676
5677 iface->cross_connect_enabled = 0;
5678 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005679 wpa_msg_global(iface->parent, MSG_INFO,
5680 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5681 iface->ifname,
5682 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005683 }
5684 }
5685
5686 return 0;
5687}
5688
5689
5690static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5691{
5692 struct wpa_supplicant *iface;
5693
5694 if (!uplink->global->cross_connection)
5695 return;
5696
5697 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5698 if (!iface->cross_connect_enabled)
5699 continue;
5700 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5701 0)
5702 continue;
5703 if (iface->ap_iface == NULL)
5704 continue;
5705 if (iface->cross_connect_in_use)
5706 continue;
5707
5708 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005709 wpa_msg_global(iface->parent, MSG_INFO,
5710 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5711 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005712 }
5713}
5714
5715
5716static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5717{
5718 struct wpa_supplicant *iface;
5719
5720 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5721 if (!iface->cross_connect_enabled)
5722 continue;
5723 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5724 0)
5725 continue;
5726 if (!iface->cross_connect_in_use)
5727 continue;
5728
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005729 wpa_msg_global(iface->parent, MSG_INFO,
5730 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5731 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005732 iface->cross_connect_in_use = 0;
5733 }
5734}
5735
5736
5737void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5738{
5739 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5740 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5741 wpa_s->cross_connect_disallowed)
5742 wpas_p2p_disable_cross_connect(wpa_s);
5743 else
5744 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005745 if (!wpa_s->ap_iface &&
5746 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5747 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005748}
5749
5750
5751void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5752{
5753 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005754 if (!wpa_s->ap_iface &&
5755 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5756 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005757 wpas_p2p_set_group_idle_timeout(wpa_s);
5758}
5759
5760
5761static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5762{
5763 struct wpa_supplicant *iface;
5764
5765 if (!wpa_s->global->cross_connection)
5766 return;
5767
5768 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5769 if (iface == wpa_s)
5770 continue;
5771 if (iface->drv_flags &
5772 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5773 continue;
5774 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5775 continue;
5776
5777 wpa_s->cross_connect_enabled = 1;
5778 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5779 sizeof(wpa_s->cross_connect_uplink));
5780 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5781 "%s to %s whenever uplink is available",
5782 wpa_s->ifname, wpa_s->cross_connect_uplink);
5783
5784 if (iface->ap_iface || iface->current_ssid == NULL ||
5785 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5786 iface->cross_connect_disallowed ||
5787 iface->wpa_state != WPA_COMPLETED)
5788 break;
5789
5790 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005791 wpa_msg_global(wpa_s->parent, MSG_INFO,
5792 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5793 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005794 break;
5795 }
5796}
5797
5798
5799int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5800{
5801 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5802 !wpa_s->p2p_in_provisioning)
5803 return 0; /* not P2P client operation */
5804
5805 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5806 "session overlap");
5807 if (wpa_s != wpa_s->parent)
5808 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005809 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005810 return 1;
5811}
5812
5813
5814void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5815{
5816 struct p2p_channels chan;
5817
5818 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5819 return;
5820
5821 os_memset(&chan, 0, sizeof(chan));
5822 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5823 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5824 "channel list");
5825 return;
5826 }
5827
5828 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5829}
5830
5831
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005832static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5833 struct wpa_scan_results *scan_res)
5834{
5835 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5836}
5837
5838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005839int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5840{
5841 struct wpa_global *global = wpa_s->global;
5842 int found = 0;
5843 const u8 *peer;
5844
5845 if (global->p2p == NULL)
5846 return -1;
5847
5848 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5849
5850 if (wpa_s->pending_interface_name[0] &&
5851 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5852 found = 1;
5853
5854 peer = p2p_get_go_neg_peer(global->p2p);
5855 if (peer) {
5856 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5857 MACSTR, MAC2STR(peer));
5858 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005859 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005860 }
5861
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005862 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5863 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5864 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5865 found = 1;
5866 }
5867
5868 if (wpa_s->pending_pd_before_join) {
5869 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5870 wpa_s->pending_pd_before_join = 0;
5871 found = 1;
5872 }
5873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005874 wpas_p2p_stop_find(wpa_s);
5875
5876 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5877 if (wpa_s == global->p2p_group_formation &&
5878 (wpa_s->p2p_in_provisioning ||
5879 wpa_s->parent->pending_interface_type ==
5880 WPA_IF_P2P_CLIENT)) {
5881 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5882 "formation found - cancelling",
5883 wpa_s->ifname);
5884 found = 1;
5885 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5886 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005887 if (wpa_s->p2p_in_provisioning) {
5888 wpas_group_formation_completed(wpa_s, 0);
5889 break;
5890 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005891 wpas_p2p_group_delete(wpa_s,
5892 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005893 break;
5894 }
5895 }
5896
5897 if (!found) {
5898 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5899 return -1;
5900 }
5901
5902 return 0;
5903}
5904
5905
5906void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5907{
5908 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5909 return;
5910
5911 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5912 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005913 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914}
5915
5916
5917void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5918 int freq_24, int freq_5, int freq_overall)
5919{
5920 struct p2p_data *p2p = wpa_s->global->p2p;
5921 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5922 return;
5923 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5924}
5925
5926
5927int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5928{
5929 u8 peer[ETH_ALEN];
5930 struct p2p_data *p2p = wpa_s->global->p2p;
5931
5932 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5933 return -1;
5934
5935 if (hwaddr_aton(addr, peer))
5936 return -1;
5937
5938 return p2p_unauthorize(p2p, peer);
5939}
5940
5941
5942/**
5943 * wpas_p2p_disconnect - Disconnect from a P2P Group
5944 * @wpa_s: Pointer to wpa_supplicant data
5945 * Returns: 0 on success, -1 on failure
5946 *
5947 * This can be used to disconnect from a group in which the local end is a P2P
5948 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5949 * virtual network interface was created for this group, that interface will be
5950 * removed. Otherwise, only the configured P2P group network will be removed
5951 * from the interface.
5952 */
5953int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5954{
5955
5956 if (wpa_s == NULL)
5957 return -1;
5958
Jouni Malinen2b89da82012-08-31 22:04:41 +03005959 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5960 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005961}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005962
5963
5964int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5965{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005966 int ret;
5967
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005968 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5969 return 0;
5970
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005971 ret = p2p_in_progress(wpa_s->global->p2p);
5972 if (ret == 0) {
5973 /*
5974 * Check whether there is an ongoing WPS provisioning step (or
5975 * other parts of group formation) on another interface since
5976 * p2p_in_progress() does not report this to avoid issues for
5977 * scans during such provisioning step.
5978 */
5979 if (wpa_s->global->p2p_group_formation &&
5980 wpa_s->global->p2p_group_formation != wpa_s) {
5981 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5982 "in group formation",
5983 wpa_s->global->p2p_group_formation->ifname);
5984 ret = 1;
5985 }
5986 }
5987
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07005988 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
5989 struct os_time now;
5990 os_get_time(&now);
5991 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
5992 P2P_MAX_INITIAL_CONN_WAIT_GO) {
5993 /* Wait for the first client has expired */
5994 wpa_s->global->p2p_go_wait_client.sec = 0;
5995 } else {
5996 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
5997 ret = 1;
5998 }
5999 }
6000
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006001 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006002}
6003
6004
6005void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6006 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006007{
6008 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6009 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6010 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006011 /**
6012 * Remove the network by scheduling the group formation
6013 * timeout to happen immediately. The teardown code
6014 * needs to be scheduled to run asynch later so that we
6015 * don't delete data from under ourselves unexpectedly.
6016 * Calling wpas_p2p_group_formation_timeout directly
6017 * causes a series of crashes in WPS failure scenarios.
6018 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006019 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6020 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006021 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6022 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006023 }
6024}
6025
6026
6027struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006028 const u8 *addr, const u8 *ssid,
6029 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006030{
6031 struct wpa_ssid *s;
6032 size_t i;
6033
6034 for (s = wpa_s->conf->ssid; s; s = s->next) {
6035 if (s->disabled != 2)
6036 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006037 if (ssid &&
6038 (ssid_len != s->ssid_len ||
6039 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6040 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006041 if (addr == NULL) {
6042 if (s->mode == WPAS_MODE_P2P_GO)
6043 return s;
6044 continue;
6045 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006046 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6047 return s; /* peer is GO in the persistent group */
6048 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6049 continue;
6050 for (i = 0; i < s->num_p2p_clients; i++) {
6051 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6052 addr, ETH_ALEN) == 0)
6053 return s; /* peer is P2P client in persistent
6054 * group */
6055 }
6056 }
6057
6058 return NULL;
6059}
6060
6061
6062void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6063 const u8 *addr)
6064{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006065 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6066 wpa_s->parent, NULL) > 0) {
6067 /*
6068 * This can happen if WPS provisioning step is not terminated
6069 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6070 * peer was able to connect, there is no need to time out group
6071 * formation after this, though. In addition, this is used with
6072 * the initial connection wait on the GO as a separate formation
6073 * timeout and as such, expected to be hit after the initial WPS
6074 * provisioning step.
6075 */
6076 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6077 }
6078 if (!wpa_s->p2p_go_group_formation_completed) {
6079 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6080 wpa_s->p2p_go_group_formation_completed = 1;
6081 wpa_s->global->p2p_group_formation = NULL;
6082 wpa_s->p2p_in_provisioning = 0;
6083 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006084 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006085 if (addr == NULL)
6086 return;
6087 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6088}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006089
Dmitry Shmidt04949592012-07-19 12:16:46 -07006090
6091static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6092 int group_added)
6093{
6094 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006095 if (wpa_s->global->p2p_group_formation)
6096 group = wpa_s->global->p2p_group_formation;
6097 wpa_s = wpa_s->parent;
6098 offchannel_send_action_done(wpa_s);
6099 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006100 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006101 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6102 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6103 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6104 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6105 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006106 wpa_s->p2p_pd_before_go_neg,
6107 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006108}
6109
6110
6111int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6112{
6113 if (!wpa_s->p2p_fallback_to_go_neg ||
6114 wpa_s->p2p_in_provisioning <= 5)
6115 return 0;
6116
6117 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6118 return 0; /* peer operating as a GO */
6119
6120 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6121 "fallback to GO Negotiation");
6122 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6123
6124 return 1;
6125}
6126
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006127
6128unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6129{
6130 const char *rn, *rn2;
6131 struct wpa_supplicant *ifs;
6132
6133 if (wpa_s->wpa_state > WPA_SCANNING) {
6134 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6135 "concurrent operation",
6136 P2P_CONCURRENT_SEARCH_DELAY);
6137 return P2P_CONCURRENT_SEARCH_DELAY;
6138 }
6139
6140 if (!wpa_s->driver->get_radio_name)
6141 return 0;
6142 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
6143 if (rn == NULL || rn[0] == '\0')
6144 return 0;
6145
6146 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6147 if (ifs == wpa_s || !ifs->driver->get_radio_name)
6148 continue;
6149
6150 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
6151 if (!rn2 || os_strcmp(rn, rn2) != 0)
6152 continue;
6153 if (ifs->wpa_state > WPA_SCANNING) {
6154 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6155 "delay due to concurrent operation on "
6156 "interface %s",
6157 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6158 return P2P_CONCURRENT_SEARCH_DELAY;
6159 }
6160 }
6161
6162 return 0;
6163}
6164
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006165
6166void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6167{
6168 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6169 "pending anymore (sta_scan_pending=%d "
6170 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6171 wpa_s->global->p2p_cb_on_scan_complete);
6172 wpa_s->sta_scan_pending = 0;
6173
6174 if (!wpa_s->global->p2p_cb_on_scan_complete)
6175 return;
6176 wpa_s->global->p2p_cb_on_scan_complete = 0;
6177
6178 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6179 return;
6180
6181 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6182 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6183 "continued after successful connection");
6184 p2p_increase_search_delay(wpa_s->global->p2p,
6185 wpas_p2p_search_delay(wpa_s));
6186 }
6187}
6188
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006189
6190static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6191 struct wpa_ssid *s, const u8 *addr,
6192 int iface_addr)
6193{
6194 struct psk_list_entry *psk, *tmp;
6195 int changed = 0;
6196
6197 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6198 list) {
6199 if ((iface_addr && !psk->p2p &&
6200 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6201 (!iface_addr && psk->p2p &&
6202 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6203 wpa_dbg(wpa_s, MSG_DEBUG,
6204 "P2P: Remove persistent group PSK list entry for "
6205 MACSTR " p2p=%u",
6206 MAC2STR(psk->addr), psk->p2p);
6207 dl_list_del(&psk->list);
6208 os_free(psk);
6209 changed++;
6210 }
6211 }
6212
6213 return changed;
6214}
6215
6216
6217void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6218 const u8 *p2p_dev_addr,
6219 const u8 *psk, size_t psk_len)
6220{
6221 struct wpa_ssid *ssid = wpa_s->current_ssid;
6222 struct wpa_ssid *persistent;
6223 struct psk_list_entry *p;
6224
6225 if (psk_len != sizeof(p->psk))
6226 return;
6227
6228 if (p2p_dev_addr) {
6229 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6230 " p2p_dev_addr=" MACSTR,
6231 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6232 if (is_zero_ether_addr(p2p_dev_addr))
6233 p2p_dev_addr = NULL;
6234 } else {
6235 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6236 MAC2STR(mac_addr));
6237 }
6238
6239 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6240 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6241 /* To be added to persistent group once created */
6242 if (wpa_s->global->add_psk == NULL) {
6243 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6244 if (wpa_s->global->add_psk == NULL)
6245 return;
6246 }
6247 p = wpa_s->global->add_psk;
6248 if (p2p_dev_addr) {
6249 p->p2p = 1;
6250 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6251 } else {
6252 p->p2p = 0;
6253 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6254 }
6255 os_memcpy(p->psk, psk, psk_len);
6256 return;
6257 }
6258
6259 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6260 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6261 return;
6262 }
6263
6264 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6265 ssid->ssid_len);
6266 if (!persistent) {
6267 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6268 return;
6269 }
6270
6271 p = os_zalloc(sizeof(*p));
6272 if (p == NULL)
6273 return;
6274 if (p2p_dev_addr) {
6275 p->p2p = 1;
6276 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6277 } else {
6278 p->p2p = 0;
6279 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6280 }
6281 os_memcpy(p->psk, psk, psk_len);
6282
6283 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6284 struct psk_list_entry *last;
6285 last = dl_list_last(&persistent->psk_list,
6286 struct psk_list_entry, list);
6287 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6288 MACSTR " (p2p=%u) to make room for a new one",
6289 MAC2STR(last->addr), last->p2p);
6290 dl_list_del(&last->list);
6291 os_free(last);
6292 }
6293
6294 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6295 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6296 p2p_dev_addr == NULL);
6297 if (p2p_dev_addr) {
6298 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6299 MACSTR, MAC2STR(p2p_dev_addr));
6300 } else {
6301 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6302 MAC2STR(mac_addr));
6303 }
6304 dl_list_add(&persistent->psk_list, &p->list);
6305
6306#ifndef CONFIG_NO_CONFIG_WRITE
6307 if (wpa_s->parent->conf->update_config &&
6308 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6309 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6310#endif /* CONFIG_NO_CONFIG_WRITE */
6311}
6312
6313
6314static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6315 struct wpa_ssid *s, const u8 *addr,
6316 int iface_addr)
6317{
6318 int res;
6319
6320 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6321 if (res > 0) {
6322#ifndef CONFIG_NO_CONFIG_WRITE
6323 if (wpa_s->conf->update_config &&
6324 wpa_config_write(wpa_s->confname, wpa_s->conf))
6325 wpa_dbg(wpa_s, MSG_DEBUG,
6326 "P2P: Failed to update configuration");
6327#endif /* CONFIG_NO_CONFIG_WRITE */
6328 }
6329}
6330
6331
6332static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6333 const u8 *peer, int iface_addr)
6334{
6335 struct hostapd_data *hapd;
6336 struct hostapd_wpa_psk *psk, *prev, *rem;
6337 struct sta_info *sta;
6338
6339 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6340 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6341 return;
6342
6343 /* Remove per-station PSK entry */
6344 hapd = wpa_s->ap_iface->bss[0];
6345 prev = NULL;
6346 psk = hapd->conf->ssid.wpa_psk;
6347 while (psk) {
6348 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6349 (!iface_addr &&
6350 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6351 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6352 MACSTR " iface_addr=%d",
6353 MAC2STR(peer), iface_addr);
6354 if (prev)
6355 prev->next = psk->next;
6356 else
6357 hapd->conf->ssid.wpa_psk = psk->next;
6358 rem = psk;
6359 psk = psk->next;
6360 os_free(rem);
6361 } else {
6362 prev = psk;
6363 psk = psk->next;
6364 }
6365 }
6366
6367 /* Disconnect from group */
6368 if (iface_addr)
6369 sta = ap_get_sta(hapd, peer);
6370 else
6371 sta = ap_get_sta_p2p(hapd, peer);
6372 if (sta) {
6373 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6374 " (iface_addr=%d) from group",
6375 MAC2STR(peer), iface_addr);
6376 hostapd_drv_sta_deauth(hapd, sta->addr,
6377 WLAN_REASON_DEAUTH_LEAVING);
6378 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6379 }
6380}
6381
6382
6383void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6384 int iface_addr)
6385{
6386 struct wpa_ssid *s;
6387 struct wpa_supplicant *w;
6388
6389 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6390
6391 /* Remove from any persistent group */
6392 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6393 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6394 continue;
6395 if (!iface_addr)
6396 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6397 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6398 }
6399
6400 /* Remove from any operating group */
6401 for (w = wpa_s->global->ifaces; w; w = w->next)
6402 wpas_p2p_remove_client_go(w, peer, iface_addr);
6403}
6404
6405
6406static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6407{
6408 struct wpa_supplicant *wpa_s = eloop_ctx;
6409 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6410}
6411
6412
6413int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6414{
6415 struct wpa_ssid *ssid = wpa_s->current_ssid;
6416
6417 if (ssid == NULL || !ssid->p2p_group)
6418 return 0;
6419
6420 if (wpa_s->p2p_last_4way_hs_fail &&
6421 wpa_s->p2p_last_4way_hs_fail == ssid) {
6422 u8 go_dev_addr[ETH_ALEN];
6423 struct wpa_ssid *persistent;
6424
6425 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6426 ssid->ssid,
6427 ssid->ssid_len) <= 0) {
6428 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6429 goto disconnect;
6430 }
6431
6432 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6433 MACSTR, MAC2STR(go_dev_addr));
6434 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6435 ssid->ssid,
6436 ssid->ssid_len);
6437 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6438 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6439 goto disconnect;
6440 }
6441 wpa_msg_global(wpa_s->parent, MSG_INFO,
6442 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6443 persistent->id);
6444 disconnect:
6445 wpa_s->p2p_last_4way_hs_fail = NULL;
6446 /*
6447 * Remove the group from a timeout to avoid issues with caller
6448 * continuing to use the interface if this is on a P2P group
6449 * interface.
6450 */
6451 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6452 wpa_s, NULL);
6453 return 1;
6454 }
6455
6456 wpa_s->p2p_last_4way_hs_fail = ssid;
6457 return 0;
6458}
6459
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006460#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006461static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6462{
6463 struct wpa_supplicant *wpa_s = eloop_ctx;
6464
6465 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6466 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6467}
6468
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006469int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6470 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006471{
6472 struct wpa_supplicant *iface = NULL;
6473 struct p2p_data *p2p = wpa_s->global->p2p;
6474
6475 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006476 if ((iface->current_ssid) &&
6477 (iface->current_ssid->frequency != freq) &&
6478 ((iface->p2p_group_interface) ||
6479 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006480
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006481 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
6482 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
6483 /* Try to see whether we can move the GO. If it
6484 * is not possible, remove the GO interface
6485 */
6486 if (wpa_drv_switch_channel(iface, freq) == 0) {
6487 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
6488 iface->current_ssid->frequency = freq;
6489 continue;
6490 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006491 }
6492
6493 /* If GO cannot be moved or if the conflicting interface is a
6494 * P2P Client, remove the interface depending up on the connection
6495 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006496 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07006497 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006498 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006499 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006500 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006501 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
6502 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006503 /* If connection in progress is p2p connection, do not proceed for the connection */
6504 if (wpa_s == iface)
6505 return -1;
6506 else
6507 /* If connection in progress is STA connection, proceed for the connection */
6508 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006509 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006510 /* P2p connection has priority, disable the STA network*/
6511 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
6512 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6513 " id=%d", ssid->id);
6514 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6515 if (wpa_s == iface) {
6516 /* p2p connection is in progress, continue connecting...*/
6517 return 0;
6518 }
6519 else {
6520 /* STA connection is in progress, do not allow to continue */
6521 return -1;
6522 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006523 }
6524 }
6525 }
6526 return 0;
6527}
6528#endif