blob: 76f50c7b06993e374609ff2c72b2374381284092 [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 Shmidt8d520ff2011-05-09 14:06:53 -070020#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070021#include "eapol_supp/eapol_supp_sm.h"
22#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "wpa_supplicant_i.h"
24#include "driver_i.h"
25#include "ap.h"
26#include "config_ssid.h"
27#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "notify.h"
29#include "scan.h"
30#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "wps_supplicant.h"
33#include "p2p_supplicant.h"
34
35
36/*
37 * How many times to try to scan to find the GO before giving up on join
38 * request.
39 */
40#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
41
Dmitry Shmidt04949592012-07-19 12:16:46 -070042#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080044#ifndef P2P_MAX_CLIENT_IDLE
45/*
46 * How many seconds to try to reconnect to the GO when connection in P2P client
47 * role has been lost.
48 */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070049#ifdef ANDROID_P2P
50#define P2P_MAX_CLIENT_IDLE 20
51#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080052#define P2P_MAX_CLIENT_IDLE 10
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070053#endif /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054#endif /* P2P_MAX_CLIENT_IDLE */
55
Dmitry Shmidt04949592012-07-19 12:16:46 -070056#ifndef P2P_MAX_INITIAL_CONN_WAIT
57/*
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
60 */
61#define P2P_MAX_INITIAL_CONN_WAIT 10
62#endif /* P2P_MAX_INITIAL_CONN_WAIT */
63
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070064#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070065#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066#endif /* P2P_CONCURRENT_SEARCH_DELAY */
67
68enum p2p_group_removal_reason {
69 P2P_GROUP_REMOVAL_UNKNOWN,
70 P2P_GROUP_REMOVAL_SILENT,
71 P2P_GROUP_REMOVAL_FORMATION_FAILED,
72 P2P_GROUP_REMOVAL_REQUESTED,
73 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
74 P2P_GROUP_REMOVAL_UNAVAILABLE,
75 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
76#ifdef ANDROID_P2P
77 P2P_GROUP_REMOVAL_FREQ_CONFLICT
78#endif
79};
80
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070081#ifdef ANDROID_P2P
82static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s);
83#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
85static struct wpa_supplicant *
86wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
87 int go);
88static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070089static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
91static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070092 const u8 *dev_addr, enum p2p_wps_method wps_method,
93 int auto_join);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080094static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
95 void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
97static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
98static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
99static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700100static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
101 int group_added);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -0700103#ifdef ANDROID_P2P
104static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s)
105{
106 struct wpa_supplicant *iface = NULL;
107
108 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
109 if(iface->scanning || iface->wpa_state == WPA_SCANNING) {
110 wpa_printf(MSG_DEBUG, "P2P: Scan in progress on %s,"
111 "defer P2P SEARCH", iface->ifname);
112 return 1;
113 }
114 }
115
116 return 0;
117}
118#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119
120static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
121 struct wpa_scan_results *scan_res)
122{
123 size_t i;
124
125 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
126 return;
127
128 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
129 (int) scan_res->num);
130
131 for (i = 0; i < scan_res->num; i++) {
132 struct wpa_scan_res *bss = scan_res->res[i];
133 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
134 bss->freq, bss->level,
135 (const u8 *) (bss + 1),
136 bss->ie_len) > 0)
137 break;
138 }
139
140 p2p_scan_res_handled(wpa_s->global->p2p);
141}
142
143
144static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
145 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700146 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700147{
148 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700149 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150 struct wpa_driver_scan_params params;
151 int ret;
152 struct wpabuf *wps_ie, *ies;
153 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800154 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155
156 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
157 return -1;
158
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700159 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
160 if (ifs->sta_scan_pending &&
161 wpas_p2p_in_progress(wpa_s) == 2) {
162 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
163 "pending station mode scan to be "
164 "completed on interface %s", ifs->ifname);
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700165#ifdef ANDROID_P2P
166 ifs->p2p_cb_on_scan_complete = 1;
167#else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700168 wpa_s->p2p_cb_on_scan_complete = 1;
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700169#endif
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700170 wpa_supplicant_req_scan(ifs, 0, 0);
171 return 1;
172 }
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700173#ifdef ANDROID_P2P
174 else if(ifs->scanning) {
175 wpa_printf(MSG_DEBUG, "Wait for the STA scan"
176 "to be "
177 "completed on interface %s", ifs->ifname);
178 ifs->p2p_cb_on_scan_complete = 1;
179 return 1;
180 }
181#endif
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700182 }
183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 os_memset(&params, 0, sizeof(params));
185
186 /* P2P Wildcard SSID */
187 params.num_ssids = 1;
188 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
189 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
190
191 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700192 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
193 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 num_req_dev_types, req_dev_types);
195 if (wps_ie == NULL)
196 return -1;
197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800198 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
199 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 if (ies == NULL) {
201 wpabuf_free(wps_ie);
202 return -1;
203 }
204 wpabuf_put_buf(ies, wps_ie);
205 wpabuf_free(wps_ie);
206
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800207 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800209 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210 params.extra_ies = wpabuf_head(ies);
211 params.extra_ies_len = wpabuf_len(ies);
212
213 switch (type) {
214 case P2P_SCAN_SOCIAL:
215 params.freqs = social_channels;
216 break;
217 case P2P_SCAN_FULL:
218 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700219 case P2P_SCAN_SPECIFIC:
220 social_channels[0] = freq;
221 social_channels[1] = 0;
222 params.freqs = social_channels;
223 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224 case P2P_SCAN_SOCIAL_PLUS_ONE:
225 social_channels[3] = freq;
226 params.freqs = social_channels;
227 break;
228 }
229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231
232 wpabuf_free(ies);
233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800234 if (ret) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700235 if (wpa_s->scanning ||
236 wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800237 wpa_s->p2p_cb_on_scan_complete = 1;
238 ret = 1;
239 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700240 } else
241 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 return ret;
244}
245
246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
248{
249 switch (p2p_group_interface) {
250 case P2P_GROUP_INTERFACE_PENDING:
251 return WPA_IF_P2P_GROUP;
252 case P2P_GROUP_INTERFACE_GO:
253 return WPA_IF_P2P_GO;
254 case P2P_GROUP_INTERFACE_CLIENT:
255 return WPA_IF_P2P_CLIENT;
256 }
257
258 return WPA_IF_P2P_GROUP;
259}
260
261
262static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
263 const u8 *ssid,
264 size_t ssid_len, int *go)
265{
266 struct wpa_ssid *s;
267
268 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
269 for (s = wpa_s->conf->ssid; s; s = s->next) {
270 if (s->disabled != 0 || !s->p2p_group ||
271 s->ssid_len != ssid_len ||
272 os_memcmp(ssid, s->ssid, ssid_len) != 0)
273 continue;
274 if (s->mode == WPAS_MODE_P2P_GO &&
275 s != wpa_s->current_ssid)
276 continue;
277 if (go)
278 *go = s->mode == WPAS_MODE_P2P_GO;
279 return wpa_s;
280 }
281 }
282
283 return NULL;
284}
285
286
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700287static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
288 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289{
290 struct wpa_ssid *ssid;
291 char *gtype;
292 const char *reason;
293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294 ssid = wpa_s->current_ssid;
295 if (ssid == NULL) {
296 /*
297 * The current SSID was not known, but there may still be a
298 * pending P2P group interface waiting for provisioning.
299 */
300 ssid = wpa_s->conf->ssid;
301 while (ssid) {
302 if (ssid->p2p_group &&
303 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
304 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
305 break;
306 ssid = ssid->next;
307 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300308 if (ssid == NULL &&
309 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
310 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700311 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
312 "not found");
313 return -1;
314 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315 }
316 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
317 gtype = "GO";
318 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
319 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
320 wpa_s->reassociate = 0;
321 wpa_s->disconnected = 1;
322 wpa_supplicant_deauthenticate(wpa_s,
323 WLAN_REASON_DEAUTH_LEAVING);
324 gtype = "client";
325 } else
326 gtype = "GO";
327 if (wpa_s->cross_connect_in_use) {
328 wpa_s->cross_connect_in_use = 0;
329 wpa_msg(wpa_s->parent, MSG_INFO,
330 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
331 wpa_s->ifname, wpa_s->cross_connect_uplink);
332 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700333 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334 case P2P_GROUP_REMOVAL_REQUESTED:
335 reason = " reason=REQUESTED";
336 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700337 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
338 reason = " reason=FORMATION_FAILED";
339 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
341 reason = " reason=IDLE";
342 break;
343 case P2P_GROUP_REMOVAL_UNAVAILABLE:
344 reason = " reason=UNAVAILABLE";
345 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700346 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
347 reason = " reason=GO_ENDING_SESSION";
348 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700349#ifdef ANDROID_P2P
350 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
351 reason = " reason=FREQ_CONFLICT";
352 break;
353#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 default:
355 reason = "";
356 break;
357 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700358 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700359 wpa_msg(wpa_s->parent, MSG_INFO,
360 P2P_EVENT_GROUP_REMOVED "%s %s%s",
361 wpa_s->ifname, gtype, reason);
362 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363
Dmitry Shmidt04949592012-07-19 12:16:46 -0700364 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
365 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
366
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700367 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
369
370 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
371 struct wpa_global *global;
372 char *ifname;
373 enum wpa_driver_if_type type;
374 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
375 wpa_s->ifname);
376 global = wpa_s->global;
377 ifname = os_strdup(wpa_s->ifname);
378 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700379 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 wpa_s = global->ifaces;
381 if (wpa_s && ifname)
382 wpa_drv_if_remove(wpa_s, type, ifname);
383 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300384 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 }
386
387 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
388 if (ssid && (ssid->p2p_group ||
389 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
390 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
391 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700392 if (ssid == wpa_s->current_ssid) {
393 wpa_sm_set_config(wpa_s->wpa, NULL);
394 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700396 }
397 /*
398 * Networks objects created during any P2P activities are not
399 * exposed out as they might/will confuse certain non-P2P aware
400 * applications since these network objects won't behave like
401 * regular ones.
402 *
403 * Likewise, we don't send out network removed signals for such
404 * network objects.
405 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406 wpa_config_remove_network(wpa_s->conf, id);
407 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800408 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 } else {
410 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
411 "found");
412 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700413 if (wpa_s->ap_iface)
414 wpa_supplicant_ap_deinit(wpa_s);
415 else
416 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700417
418 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419}
420
421
422static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
423 u8 *go_dev_addr,
424 const u8 *ssid, size_t ssid_len)
425{
426 struct wpa_bss *bss;
427 const u8 *bssid;
428 struct wpabuf *p2p;
429 u8 group_capab;
430 const u8 *addr;
431
432 if (wpa_s->go_params)
433 bssid = wpa_s->go_params->peer_interface_addr;
434 else
435 bssid = wpa_s->bssid;
436
437 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
438 if (bss == NULL) {
439 u8 iface_addr[ETH_ALEN];
440 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
441 iface_addr) == 0)
442 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
443 }
444 if (bss == NULL) {
445 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
446 "group is persistent - BSS " MACSTR " not found",
447 MAC2STR(bssid));
448 return 0;
449 }
450
451 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
452 if (p2p == NULL) {
453 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
454 "group is persistent - BSS " MACSTR
455 " did not include P2P IE", MAC2STR(bssid));
456 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
457 (u8 *) (bss + 1), bss->ie_len);
458 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
459 ((u8 *) bss + 1) + bss->ie_len,
460 bss->beacon_ie_len);
461 return 0;
462 }
463
464 group_capab = p2p_get_group_capab(p2p);
465 addr = p2p_get_go_dev_addr(p2p);
466 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
467 "group_capab=0x%x", group_capab);
468 if (addr) {
469 os_memcpy(go_dev_addr, addr, ETH_ALEN);
470 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
471 MAC2STR(addr));
472 } else
473 os_memset(go_dev_addr, 0, ETH_ALEN);
474 wpabuf_free(p2p);
475
476 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
477 "go_dev_addr=" MACSTR,
478 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
479
480 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
481}
482
483
Jouni Malinen75ecf522011-06-27 15:19:46 -0700484static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
485 struct wpa_ssid *ssid,
486 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487{
488 struct wpa_ssid *s;
489 int changed = 0;
490
491 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
492 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
493 for (s = wpa_s->conf->ssid; s; s = s->next) {
494 if (s->disabled == 2 &&
495 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
496 s->ssid_len == ssid->ssid_len &&
497 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
498 break;
499 }
500
501 if (s) {
502 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
503 "entry");
504 if (ssid->passphrase && !s->passphrase)
505 changed = 1;
506 else if (ssid->passphrase && s->passphrase &&
507 os_strcmp(ssid->passphrase, s->passphrase) != 0)
508 changed = 1;
509 } else {
510 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
511 "entry");
512 changed = 1;
513 s = wpa_config_add_network(wpa_s->conf);
514 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700515 return -1;
516
517 /*
518 * Instead of network_added we emit persistent_group_added
519 * notification. Also to keep the defense checks in
520 * persistent_group obj registration method, we set the
521 * relevant flags in s to designate it as a persistent group.
522 */
523 s->p2p_group = 1;
524 s->p2p_persistent_group = 1;
525 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526 wpa_config_set_network_defaults(s);
527 }
528
529 s->p2p_group = 1;
530 s->p2p_persistent_group = 1;
531 s->disabled = 2;
532 s->bssid_set = 1;
533 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
534 s->mode = ssid->mode;
535 s->auth_alg = WPA_AUTH_ALG_OPEN;
536 s->key_mgmt = WPA_KEY_MGMT_PSK;
537 s->proto = WPA_PROTO_RSN;
538 s->pairwise_cipher = WPA_CIPHER_CCMP;
539 s->export_keys = 1;
540 if (ssid->passphrase) {
541 os_free(s->passphrase);
542 s->passphrase = os_strdup(ssid->passphrase);
543 }
544 if (ssid->psk_set) {
545 s->psk_set = 1;
546 os_memcpy(s->psk, ssid->psk, 32);
547 }
548 if (s->passphrase && !s->psk_set)
549 wpa_config_update_psk(s);
550 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
551 os_free(s->ssid);
552 s->ssid = os_malloc(ssid->ssid_len);
553 }
554 if (s->ssid) {
555 s->ssid_len = ssid->ssid_len;
556 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
557 }
558
559#ifndef CONFIG_NO_CONFIG_WRITE
560 if (changed && wpa_s->conf->update_config &&
561 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
562 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
563 }
564#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700565
566 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567}
568
569
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800570static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
571 const u8 *addr)
572{
573 struct wpa_ssid *ssid, *s;
574 u8 *n;
575 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700576 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800577
578 ssid = wpa_s->current_ssid;
579 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
580 !ssid->p2p_persistent_group)
581 return;
582
583 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
584 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
585 continue;
586
587 if (s->ssid_len == ssid->ssid_len &&
588 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
589 break;
590 }
591
592 if (s == NULL)
593 return;
594
595 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
596 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700597 ETH_ALEN) != 0)
598 continue;
599
600 if (i == s->num_p2p_clients - 1)
601 return; /* already the most recent entry */
602
603 /* move the entry to mark it most recent */
604 os_memmove(s->p2p_client_list + i * ETH_ALEN,
605 s->p2p_client_list + (i + 1) * ETH_ALEN,
606 (s->num_p2p_clients - i - 1) * ETH_ALEN);
607 os_memcpy(s->p2p_client_list +
608 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
609 found = 1;
610 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800611 }
612
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700613 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
614 n = os_realloc_array(s->p2p_client_list,
615 s->num_p2p_clients + 1, ETH_ALEN);
616 if (n == NULL)
617 return;
618 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
619 s->p2p_client_list = n;
620 s->num_p2p_clients++;
621 } else if (!found) {
622 /* Not enough room for an additional entry - drop the oldest
623 * entry */
624 os_memmove(s->p2p_client_list,
625 s->p2p_client_list + ETH_ALEN,
626 (s->num_p2p_clients - 1) * ETH_ALEN);
627 os_memcpy(s->p2p_client_list +
628 (s->num_p2p_clients - 1) * ETH_ALEN,
629 addr, ETH_ALEN);
630 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800631
632#ifndef CONFIG_NO_CONFIG_WRITE
633 if (wpa_s->parent->conf->update_config &&
634 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
635 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
636#endif /* CONFIG_NO_CONFIG_WRITE */
637}
638
639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
641 int success)
642{
643 struct wpa_ssid *ssid;
644 const char *ssid_txt;
645 int client;
646 int persistent;
647 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700648 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649
650 /*
651 * This callback is likely called for the main interface. Update wpa_s
652 * to use the group interface if a new interface was created for the
653 * group.
654 */
655 if (wpa_s->global->p2p_group_formation)
656 wpa_s = wpa_s->global->p2p_group_formation;
657 wpa_s->global->p2p_group_formation = NULL;
658 wpa_s->p2p_in_provisioning = 0;
659
660 if (!success) {
661 wpa_msg(wpa_s->parent, MSG_INFO,
662 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700663 wpas_p2p_group_delete(wpa_s,
664 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665 return;
666 }
667
668 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
669
670 ssid = wpa_s->current_ssid;
671 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
672 ssid->mode = WPAS_MODE_P2P_GO;
673 p2p_group_notif_formation_done(wpa_s->p2p_group);
674 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
675 }
676
677 persistent = 0;
678 if (ssid) {
679 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
680 client = ssid->mode == WPAS_MODE_INFRA;
681 if (ssid->mode == WPAS_MODE_P2P_GO) {
682 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700683 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
684 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 } else
686 persistent = wpas_p2p_persistent_group(wpa_s,
687 go_dev_addr,
688 ssid->ssid,
689 ssid->ssid_len);
690 } else {
691 ssid_txt = "";
692 client = wpa_s->p2p_group_interface ==
693 P2P_GROUP_INTERFACE_CLIENT;
694 os_memset(go_dev_addr, 0, ETH_ALEN);
695 }
696
697 wpa_s->show_group_started = 0;
698 if (client) {
699 /*
700 * Indicate event only after successfully completed 4-way
701 * handshake, i.e., when the interface is ready for data
702 * packets.
703 */
704 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700705#ifdef ANDROID_P2P
706 /* For client Second phase of Group formation (4-way handshake) can be still pending
707 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700708 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 -0700709 wpa_s->global->p2p_group_formation = wpa_s;
710#endif
711
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700712 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
713 char psk[65];
714 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
715 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
716 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
717 "%s",
718 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
719 MAC2STR(go_dev_addr),
720 persistent ? " [PERSISTENT]" : "");
721 wpas_p2p_cross_connect_setup(wpa_s);
722 wpas_p2p_set_group_idle_timeout(wpa_s);
723 } else {
724 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
725 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
726 "go_dev_addr=" MACSTR "%s",
727 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
728 ssid && ssid->passphrase ? ssid->passphrase : "",
729 MAC2STR(go_dev_addr),
730 persistent ? " [PERSISTENT]" : "");
731 wpas_p2p_cross_connect_setup(wpa_s);
732 wpas_p2p_set_group_idle_timeout(wpa_s);
733 }
734
735 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700736 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
737 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800738 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700739 network_id = ssid->id;
740 if (!client)
741 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742}
743
744
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800745static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
746 unsigned int freq,
747 const u8 *dst, const u8 *src,
748 const u8 *bssid,
749 const u8 *data, size_t data_len,
750 enum offchannel_send_action_result
751 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800753 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
756 return;
757 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
758 return;
759
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800760 switch (result) {
761 case OFFCHANNEL_SEND_ACTION_SUCCESS:
762 res = P2P_SEND_ACTION_SUCCESS;
763 break;
764 case OFFCHANNEL_SEND_ACTION_NO_ACK:
765 res = P2P_SEND_ACTION_NO_ACK;
766 break;
767 case OFFCHANNEL_SEND_ACTION_FAILED:
768 res = P2P_SEND_ACTION_FAILED;
769 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 }
771
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800772 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800774 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
775 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800776 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
777 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700779 if (wpa_s->p2p_fallback_to_go_neg) {
780 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
781 "during p2p_connect-auto");
782 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
783 return;
784 }
785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800787 "join-existing-group operation (no ACK for PD "
788 "Req)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789 wpas_p2p_join_start(wpa_s);
790 }
791}
792
793
794static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
795 const u8 *src, const u8 *bssid, const u8 *buf,
796 size_t len, unsigned int wait_time)
797{
798 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800799 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
800 wait_time,
801 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802}
803
804
805static void wpas_send_action_done(void *ctx)
806{
807 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800808 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809}
810
811
812static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
813 struct p2p_go_neg_results *params)
814{
815 if (wpa_s->go_params == NULL) {
816 wpa_s->go_params = os_malloc(sizeof(*params));
817 if (wpa_s->go_params == NULL)
818 return -1;
819 }
820 os_memcpy(wpa_s->go_params, params, sizeof(*params));
821 return 0;
822}
823
824
825static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
826 struct p2p_go_neg_results *res)
827{
828 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
829 MAC2STR(res->peer_interface_addr));
830 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
831 res->ssid, res->ssid_len);
832 wpa_supplicant_ap_deinit(wpa_s);
833 wpas_copy_go_neg_results(wpa_s, res);
834 if (res->wps_method == WPS_PBC)
835 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
836 else {
837 u16 dev_pw_id = DEV_PW_DEFAULT;
838 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
839 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
840 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
841 wpa_s->p2p_pin, 1, dev_pw_id);
842 }
843}
844
845
846static void p2p_go_configured(void *ctx, void *data)
847{
848 struct wpa_supplicant *wpa_s = ctx;
849 struct p2p_go_neg_results *params = data;
850 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700851 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852
853 ssid = wpa_s->current_ssid;
854 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
855 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
856 if (wpa_s->global->p2p_group_formation == wpa_s)
857 wpa_s->global->p2p_group_formation = NULL;
858 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
859 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
860 "go_dev_addr=" MACSTR "%s",
861 wpa_s->ifname,
862 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
863 ssid->frequency,
864 params->passphrase ? params->passphrase : "",
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700865 MAC2STR(wpa_s->global->p2p_dev_addr),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 params->persistent_group ? " [PERSISTENT]" : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700869 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700871 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700872 if (network_id < 0)
873 network_id = ssid->id;
874 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 wpas_p2p_cross_connect_setup(wpa_s);
876 wpas_p2p_set_group_idle_timeout(wpa_s);
877 return;
878 }
879
880 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
881 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
882 params->peer_interface_addr)) {
883 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
884 "filtering");
885 return;
886 }
887 if (params->wps_method == WPS_PBC)
888 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800889 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 else if (wpa_s->p2p_pin[0])
891 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
892 wpa_s->p2p_pin, NULL, 0);
893 os_free(wpa_s->go_params);
894 wpa_s->go_params = NULL;
895}
896
897
898static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
899 struct p2p_go_neg_results *params,
900 int group_formation)
901{
902 struct wpa_ssid *ssid;
903
904 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
905 return;
906
907 ssid = wpa_config_add_network(wpa_s->conf);
908 if (ssid == NULL)
909 return;
910
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800911 wpa_s->show_group_started = 0;
912
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700913 wpa_config_set_network_defaults(ssid);
914 ssid->temporary = 1;
915 ssid->p2p_group = 1;
916 ssid->p2p_persistent_group = params->persistent_group;
917 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
918 WPAS_MODE_P2P_GO;
919 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700920 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 ssid->ssid = os_zalloc(params->ssid_len + 1);
922 if (ssid->ssid) {
923 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
924 ssid->ssid_len = params->ssid_len;
925 }
926 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
927 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
928 ssid->proto = WPA_PROTO_RSN;
929 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
930 ssid->passphrase = os_strdup(params->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700931 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932
933 wpa_s->ap_configured_cb = p2p_go_configured;
934 wpa_s->ap_configured_cb_ctx = wpa_s;
935 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700936 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 wpa_s->reassociate = 1;
938 wpa_s->disconnected = 0;
939 wpa_supplicant_req_scan(wpa_s, 0, 0);
940}
941
942
943static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
944 const struct wpa_supplicant *src)
945{
946 struct wpa_config *d;
947 const struct wpa_config *s;
948
949 d = dst->conf;
950 s = src->conf;
951
952#define C(n) if (s->n) d->n = os_strdup(s->n)
953 C(device_name);
954 C(manufacturer);
955 C(model_name);
956 C(model_number);
957 C(serial_number);
958 C(config_methods);
959#undef C
960
961 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
962 os_memcpy(d->sec_device_type, s->sec_device_type,
963 sizeof(d->sec_device_type));
964 d->num_sec_device_types = s->num_sec_device_types;
965
966 d->p2p_group_idle = s->p2p_group_idle;
967 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800968 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700969 d->max_num_sta = s->max_num_sta;
970 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971}
972
973
974static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
975 enum wpa_driver_if_type type)
976{
977 char ifname[120], force_ifname[120];
978
979 if (wpa_s->pending_interface_name[0]) {
980 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
981 "- skip creation of a new one");
982 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
983 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
984 "unknown?! ifname='%s'",
985 wpa_s->pending_interface_name);
986 return -1;
987 }
988 return 0;
989 }
990
991 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
992 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800993 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
995 /* Try to avoid going over the IFNAMSIZ length limit */
996 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
997 wpa_s->p2p_group_idx);
998 }
999 force_ifname[0] = '\0';
1000
1001 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1002 ifname);
1003 wpa_s->p2p_group_idx++;
1004
1005 wpa_s->pending_interface_type = type;
1006 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1007 wpa_s->pending_interface_addr, NULL) < 0) {
1008 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1009 "interface");
1010 return -1;
1011 }
1012
1013 if (force_ifname[0]) {
1014 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1015 force_ifname);
1016 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1017 sizeof(wpa_s->pending_interface_name));
1018 } else
1019 os_strlcpy(wpa_s->pending_interface_name, ifname,
1020 sizeof(wpa_s->pending_interface_name));
1021 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1022 MACSTR, wpa_s->pending_interface_name,
1023 MAC2STR(wpa_s->pending_interface_addr));
1024
1025 return 0;
1026}
1027
1028
1029static void wpas_p2p_remove_pending_group_interface(
1030 struct wpa_supplicant *wpa_s)
1031{
1032 if (!wpa_s->pending_interface_name[0] ||
1033 is_zero_ether_addr(wpa_s->pending_interface_addr))
1034 return; /* No pending virtual interface */
1035
1036 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1037 wpa_s->pending_interface_name);
1038 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1039 wpa_s->pending_interface_name);
1040 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1041 wpa_s->pending_interface_name[0] = '\0';
1042}
1043
1044
1045static struct wpa_supplicant *
1046wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1047{
1048 struct wpa_interface iface;
1049 struct wpa_supplicant *group_wpa_s;
1050
1051 if (!wpa_s->pending_interface_name[0]) {
1052 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1053 if (!wpas_p2p_create_iface(wpa_s))
1054 return NULL;
1055 /*
1056 * Something has forced us to remove the pending interface; try
1057 * to create a new one and hope for the best that we will get
1058 * the same local address.
1059 */
1060 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1061 WPA_IF_P2P_CLIENT) < 0)
1062 return NULL;
1063 }
1064
1065 os_memset(&iface, 0, sizeof(iface));
1066 iface.ifname = wpa_s->pending_interface_name;
1067 iface.driver = wpa_s->driver->name;
1068 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1069 iface.driver_param = wpa_s->conf->driver_param;
1070 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1071 if (group_wpa_s == NULL) {
1072 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1073 "wpa_supplicant interface");
1074 return NULL;
1075 }
1076 wpa_s->pending_interface_name[0] = '\0';
1077 group_wpa_s->parent = wpa_s;
1078 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1079 P2P_GROUP_INTERFACE_CLIENT;
1080 wpa_s->global->p2p_group_formation = group_wpa_s;
1081
1082 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1083
1084 return group_wpa_s;
1085}
1086
1087
1088static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1089 void *timeout_ctx)
1090{
1091 struct wpa_supplicant *wpa_s = eloop_ctx;
1092 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1093 if (wpa_s->global->p2p)
1094 p2p_group_formation_failed(wpa_s->global->p2p);
1095 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1096 wpa_drv_p2p_group_formation_failed(wpa_s);
1097 wpas_group_formation_completed(wpa_s, 0);
1098}
1099
1100
1101void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1102{
1103 struct wpa_supplicant *wpa_s = ctx;
1104
1105 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1106 wpa_drv_cancel_remain_on_channel(wpa_s);
1107 wpa_s->off_channel_freq = 0;
1108 wpa_s->roc_waiting_drv_freq = 0;
1109 }
1110
1111 if (res->status) {
1112 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1113 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001114 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 wpas_p2p_remove_pending_group_interface(wpa_s);
1116 return;
1117 }
1118
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001119 if (wpa_s->p2p_go_ht40)
1120 res->ht40 = 1;
1121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001123 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124
Dmitry Shmidt04949592012-07-19 12:16:46 -07001125 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1126 struct wpa_ssid *ssid;
1127 ssid = wpa_config_get_network(wpa_s->conf,
1128 wpa_s->p2p_persistent_id);
1129 if (ssid && ssid->disabled == 2 &&
1130 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1131 size_t len = os_strlen(ssid->passphrase);
1132 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1133 "on requested persistent group");
1134 os_memcpy(res->passphrase, ssid->passphrase, len);
1135 res->passphrase[len] = '\0';
1136 }
1137 }
1138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001139 if (wpa_s->create_p2p_iface) {
1140 struct wpa_supplicant *group_wpa_s =
1141 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1142 if (group_wpa_s == NULL) {
1143 wpas_p2p_remove_pending_group_interface(wpa_s);
1144 return;
1145 }
1146 if (group_wpa_s != wpa_s) {
1147 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1148 sizeof(group_wpa_s->p2p_pin));
1149 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1150 }
1151 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1152 wpa_s->pending_interface_name[0] = '\0';
1153 group_wpa_s->p2p_in_provisioning = 1;
1154
1155 if (res->role_go)
1156 wpas_start_wps_go(group_wpa_s, res, 1);
1157 else
1158 wpas_start_wps_enrollee(group_wpa_s, res);
1159 } else {
1160 wpa_s->p2p_in_provisioning = 1;
1161 wpa_s->global->p2p_group_formation = wpa_s;
1162
1163 if (res->role_go)
1164 wpas_start_wps_go(wpa_s, res, 1);
1165 else
1166 wpas_start_wps_enrollee(ctx, res);
1167 }
1168
1169 wpa_s->p2p_long_listen = 0;
1170 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1171
1172 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1173 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1174 (res->peer_config_timeout % 100) * 10000,
1175 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1176}
1177
1178
1179void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1180{
1181 struct wpa_supplicant *wpa_s = ctx;
1182 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1183 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1184
1185 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1186}
1187
1188
1189void wpas_dev_found(void *ctx, const u8 *addr,
1190 const struct p2p_peer_info *info,
1191 int new_device)
1192{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001193#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 struct wpa_supplicant *wpa_s = ctx;
1195 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001196#define WFD_DEV_INFO_SIZE 9
1197 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001198 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001199#ifdef CONFIG_WIFI_DISPLAY
1200 if (info->wfd_subelems) {
1201 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1202 wpabuf_head(info->wfd_subelems),
1203 WFD_DEV_INFO_SIZE);
1204 }
1205#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1207 " p2p_dev_addr=" MACSTR
1208 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001209 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1211 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1212 sizeof(devtype)),
1213 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001214 info->dev_capab, info->group_capab,
1215 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001216#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217
1218 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1219}
1220
1221
1222static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1223{
1224 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001225
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001226 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1227 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1230}
1231
1232
1233static int wpas_start_listen(void *ctx, unsigned int freq,
1234 unsigned int duration,
1235 const struct wpabuf *probe_resp_ie)
1236{
1237 struct wpa_supplicant *wpa_s = ctx;
1238
1239 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1240
1241 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1242 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1243 "report received Probe Request frames");
1244 return -1;
1245 }
1246
1247 wpa_s->pending_listen_freq = freq;
1248 wpa_s->pending_listen_duration = duration;
1249
1250 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1251 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1252 "to remain on channel (%u MHz) for Listen "
1253 "state", freq);
1254 wpa_s->pending_listen_freq = 0;
1255 return -1;
1256 }
1257 wpa_s->off_channel_freq = 0;
1258 wpa_s->roc_waiting_drv_freq = freq;
1259
1260 return 0;
1261}
1262
1263
1264static void wpas_stop_listen(void *ctx)
1265{
1266 struct wpa_supplicant *wpa_s = ctx;
1267 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1268 wpa_drv_cancel_remain_on_channel(wpa_s);
1269 wpa_s->off_channel_freq = 0;
1270 wpa_s->roc_waiting_drv_freq = 0;
1271 }
1272 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1273 wpa_drv_probe_req_report(wpa_s, 0);
1274}
1275
1276
1277static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1278{
1279 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001280 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281}
1282
1283
1284static struct p2p_srv_bonjour *
1285wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1286 const struct wpabuf *query)
1287{
1288 struct p2p_srv_bonjour *bsrv;
1289 size_t len;
1290
1291 len = wpabuf_len(query);
1292 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1293 struct p2p_srv_bonjour, list) {
1294 if (len == wpabuf_len(bsrv->query) &&
1295 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1296 len) == 0)
1297 return bsrv;
1298 }
1299 return NULL;
1300}
1301
1302
1303static struct p2p_srv_upnp *
1304wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1305 const char *service)
1306{
1307 struct p2p_srv_upnp *usrv;
1308
1309 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1310 struct p2p_srv_upnp, list) {
1311 if (version == usrv->version &&
1312 os_strcmp(service, usrv->service) == 0)
1313 return usrv;
1314 }
1315 return NULL;
1316}
1317
1318
1319static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1320 u8 srv_trans_id)
1321{
1322 u8 *len_pos;
1323
1324 if (wpabuf_tailroom(resp) < 5)
1325 return;
1326
1327 /* Length (to be filled) */
1328 len_pos = wpabuf_put(resp, 2);
1329 wpabuf_put_u8(resp, srv_proto);
1330 wpabuf_put_u8(resp, srv_trans_id);
1331 /* Status Code */
1332 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1333 /* Response Data: empty */
1334 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1335}
1336
1337
1338static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1339 struct wpabuf *resp, u8 srv_trans_id)
1340{
1341 struct p2p_srv_bonjour *bsrv;
1342 u8 *len_pos;
1343
1344 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1345
1346 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1347 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1348 return;
1349 }
1350
1351 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1352 struct p2p_srv_bonjour, list) {
1353 if (wpabuf_tailroom(resp) <
1354 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1355 return;
1356 /* Length (to be filled) */
1357 len_pos = wpabuf_put(resp, 2);
1358 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1359 wpabuf_put_u8(resp, srv_trans_id);
1360 /* Status Code */
1361 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1362 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1363 wpabuf_head(bsrv->resp),
1364 wpabuf_len(bsrv->resp));
1365 /* Response Data */
1366 wpabuf_put_buf(resp, bsrv->query); /* Key */
1367 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1368 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1369 2);
1370 }
1371}
1372
1373
1374static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1375 struct wpabuf *resp, u8 srv_trans_id,
1376 const u8 *query, size_t query_len)
1377{
1378 struct p2p_srv_bonjour *bsrv;
1379 struct wpabuf buf;
1380 u8 *len_pos;
1381
1382 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1383 query, query_len);
1384 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1385 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1386 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1387 srv_trans_id);
1388 return;
1389 }
1390
1391 if (query_len == 0) {
1392 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1393 return;
1394 }
1395
1396 if (wpabuf_tailroom(resp) < 5)
1397 return;
1398 /* Length (to be filled) */
1399 len_pos = wpabuf_put(resp, 2);
1400 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1401 wpabuf_put_u8(resp, srv_trans_id);
1402
1403 wpabuf_set(&buf, query, query_len);
1404 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1405 if (bsrv == NULL) {
1406 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1407 "available");
1408
1409 /* Status Code */
1410 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1411 /* Response Data: empty */
1412 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1413 2);
1414 return;
1415 }
1416
1417 /* Status Code */
1418 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1419 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1420 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1421
1422 if (wpabuf_tailroom(resp) >=
1423 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1424 /* Response Data */
1425 wpabuf_put_buf(resp, bsrv->query); /* Key */
1426 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1427 }
1428 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1429}
1430
1431
1432static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1433 struct wpabuf *resp, u8 srv_trans_id)
1434{
1435 struct p2p_srv_upnp *usrv;
1436 u8 *len_pos;
1437
1438 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1439
1440 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1441 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1442 return;
1443 }
1444
1445 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1446 struct p2p_srv_upnp, list) {
1447 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1448 return;
1449
1450 /* Length (to be filled) */
1451 len_pos = wpabuf_put(resp, 2);
1452 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1453 wpabuf_put_u8(resp, srv_trans_id);
1454
1455 /* Status Code */
1456 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1457 /* Response Data */
1458 wpabuf_put_u8(resp, usrv->version);
1459 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1460 usrv->service);
1461 wpabuf_put_str(resp, usrv->service);
1462 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1463 2);
1464 }
1465}
1466
1467
1468static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1469 struct wpabuf *resp, u8 srv_trans_id,
1470 const u8 *query, size_t query_len)
1471{
1472 struct p2p_srv_upnp *usrv;
1473 u8 *len_pos;
1474 u8 version;
1475 char *str;
1476 int count = 0;
1477
1478 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1479 query, query_len);
1480
1481 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1482 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1483 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1484 srv_trans_id);
1485 return;
1486 }
1487
1488 if (query_len == 0) {
1489 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1490 return;
1491 }
1492
1493 if (wpabuf_tailroom(resp) < 5)
1494 return;
1495
1496 /* Length (to be filled) */
1497 len_pos = wpabuf_put(resp, 2);
1498 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1499 wpabuf_put_u8(resp, srv_trans_id);
1500
1501 version = query[0];
1502 str = os_malloc(query_len);
1503 if (str == NULL)
1504 return;
1505 os_memcpy(str, query + 1, query_len - 1);
1506 str[query_len - 1] = '\0';
1507
1508 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1509 struct p2p_srv_upnp, list) {
1510 if (version != usrv->version)
1511 continue;
1512
1513 if (os_strcmp(str, "ssdp:all") != 0 &&
1514 os_strstr(usrv->service, str) == NULL)
1515 continue;
1516
1517 if (wpabuf_tailroom(resp) < 2)
1518 break;
1519 if (count == 0) {
1520 /* Status Code */
1521 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1522 /* Response Data */
1523 wpabuf_put_u8(resp, version);
1524 } else
1525 wpabuf_put_u8(resp, ',');
1526
1527 count++;
1528
1529 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1530 usrv->service);
1531 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1532 break;
1533 wpabuf_put_str(resp, usrv->service);
1534 }
1535 os_free(str);
1536
1537 if (count == 0) {
1538 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1539 "available");
1540 /* Status Code */
1541 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1542 /* Response Data: empty */
1543 }
1544
1545 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1546}
1547
1548
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001549#ifdef CONFIG_WIFI_DISPLAY
1550static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1551 struct wpabuf *resp, u8 srv_trans_id,
1552 const u8 *query, size_t query_len)
1553{
1554 const u8 *pos;
1555 u8 role;
1556 u8 *len_pos;
1557
1558 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1559
1560 if (!wpa_s->global->wifi_display) {
1561 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1562 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1563 srv_trans_id);
1564 return;
1565 }
1566
1567 if (query_len < 1) {
1568 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1569 "Role");
1570 return;
1571 }
1572
1573 if (wpabuf_tailroom(resp) < 5)
1574 return;
1575
1576 pos = query;
1577 role = *pos++;
1578 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1579
1580 /* TODO: role specific handling */
1581
1582 /* Length (to be filled) */
1583 len_pos = wpabuf_put(resp, 2);
1584 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1585 wpabuf_put_u8(resp, srv_trans_id);
1586 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1587
1588 while (pos < query + query_len) {
1589 if (*pos < MAX_WFD_SUBELEMS &&
1590 wpa_s->global->wfd_subelem[*pos] &&
1591 wpabuf_tailroom(resp) >=
1592 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1593 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1594 "subelement %u", *pos);
1595 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1596 }
1597 pos++;
1598 }
1599
1600 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1601}
1602#endif /* CONFIG_WIFI_DISPLAY */
1603
1604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1606 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1607{
1608 struct wpa_supplicant *wpa_s = ctx;
1609 const u8 *pos = tlvs;
1610 const u8 *end = tlvs + tlvs_len;
1611 const u8 *tlv_end;
1612 u16 slen;
1613 struct wpabuf *resp;
1614 u8 srv_proto, srv_trans_id;
1615 size_t buf_len;
1616 char *buf;
1617
1618 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1619 tlvs, tlvs_len);
1620 buf_len = 2 * tlvs_len + 1;
1621 buf = os_malloc(buf_len);
1622 if (buf) {
1623 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1624 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1625 MACSTR " %u %u %s",
1626 freq, MAC2STR(sa), dialog_token, update_indic,
1627 buf);
1628 os_free(buf);
1629 }
1630
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001631 if (wpa_s->p2p_sd_over_ctrl_iface) {
1632 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1633 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636
1637 resp = wpabuf_alloc(10000);
1638 if (resp == NULL)
1639 return;
1640
1641 while (pos + 1 < end) {
1642 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1643 slen = WPA_GET_LE16(pos);
1644 pos += 2;
1645 if (pos + slen > end || slen < 2) {
1646 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1647 "length");
1648 wpabuf_free(resp);
1649 return;
1650 }
1651 tlv_end = pos + slen;
1652
1653 srv_proto = *pos++;
1654 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1655 srv_proto);
1656 srv_trans_id = *pos++;
1657 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1658 srv_trans_id);
1659
1660 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1661 pos, tlv_end - pos);
1662
1663
1664 if (wpa_s->force_long_sd) {
1665 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1666 "response");
1667 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1668 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1669 goto done;
1670 }
1671
1672 switch (srv_proto) {
1673 case P2P_SERV_ALL_SERVICES:
1674 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1675 "for all services");
1676 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1677 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1678 wpa_printf(MSG_DEBUG, "P2P: No service "
1679 "discovery protocols available");
1680 wpas_sd_add_proto_not_avail(
1681 resp, P2P_SERV_ALL_SERVICES,
1682 srv_trans_id);
1683 break;
1684 }
1685 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1686 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1687 break;
1688 case P2P_SERV_BONJOUR:
1689 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1690 pos, tlv_end - pos);
1691 break;
1692 case P2P_SERV_UPNP:
1693 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1694 pos, tlv_end - pos);
1695 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001696#ifdef CONFIG_WIFI_DISPLAY
1697 case P2P_SERV_WIFI_DISPLAY:
1698 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1699 pos, tlv_end - pos);
1700 break;
1701#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 default:
1703 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1704 "protocol %u", srv_proto);
1705 wpas_sd_add_proto_not_avail(resp, srv_proto,
1706 srv_trans_id);
1707 break;
1708 }
1709
1710 pos = tlv_end;
1711 }
1712
1713done:
1714 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1715 update_indic, tlvs, tlvs_len);
1716
1717 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1718
1719 wpabuf_free(resp);
1720}
1721
1722
1723void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1724 const u8 *tlvs, size_t tlvs_len)
1725{
1726 struct wpa_supplicant *wpa_s = ctx;
1727 const u8 *pos = tlvs;
1728 const u8 *end = tlvs + tlvs_len;
1729 const u8 *tlv_end;
1730 u16 slen;
1731 size_t buf_len;
1732 char *buf;
1733
1734 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1735 tlvs, tlvs_len);
1736 if (tlvs_len > 1500) {
1737 /* TODO: better way for handling this */
1738 wpa_msg_ctrl(wpa_s, MSG_INFO,
1739 P2P_EVENT_SERV_DISC_RESP MACSTR
1740 " %u <long response: %u bytes>",
1741 MAC2STR(sa), update_indic,
1742 (unsigned int) tlvs_len);
1743 } else {
1744 buf_len = 2 * tlvs_len + 1;
1745 buf = os_malloc(buf_len);
1746 if (buf) {
1747 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1748 wpa_msg_ctrl(wpa_s, MSG_INFO,
1749 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1750 MAC2STR(sa), update_indic, buf);
1751 os_free(buf);
1752 }
1753 }
1754
1755 while (pos < end) {
1756 u8 srv_proto, srv_trans_id, status;
1757
1758 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1759 slen = WPA_GET_LE16(pos);
1760 pos += 2;
1761 if (pos + slen > end || slen < 3) {
1762 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1763 "length");
1764 return;
1765 }
1766 tlv_end = pos + slen;
1767
1768 srv_proto = *pos++;
1769 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1770 srv_proto);
1771 srv_trans_id = *pos++;
1772 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1773 srv_trans_id);
1774 status = *pos++;
1775 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1776 status);
1777
1778 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1779 pos, tlv_end - pos);
1780
1781 pos = tlv_end;
1782 }
1783
1784 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1785}
1786
1787
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001788u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1789 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790{
1791 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001792 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001793 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001794 return 0;
1795 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001796}
1797
1798
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001799u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1800 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801{
1802 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001803 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804
1805 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1806 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001807 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1809 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1810 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1811 wpabuf_put_u8(tlvs, version);
1812 wpabuf_put_str(tlvs, query);
1813 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1814 wpabuf_free(tlvs);
1815 return ret;
1816}
1817
1818
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001819#ifdef CONFIG_WIFI_DISPLAY
1820
1821static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1822 const struct wpabuf *tlvs)
1823{
1824 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1825 return 0;
1826 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1827 return 0;
1828 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1829}
1830
1831
1832#define MAX_WFD_SD_SUBELEMS 20
1833
1834static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1835 const char *subelems)
1836{
1837 u8 *len;
1838 const char *pos;
1839 int val;
1840 int count = 0;
1841
1842 len = wpabuf_put(tlvs, 2);
1843 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1844 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1845
1846 wpabuf_put_u8(tlvs, role);
1847
1848 pos = subelems;
1849 while (*pos) {
1850 val = atoi(pos);
1851 if (val >= 0 && val < 256) {
1852 wpabuf_put_u8(tlvs, val);
1853 count++;
1854 if (count == MAX_WFD_SD_SUBELEMS)
1855 break;
1856 }
1857 pos = os_strchr(pos + 1, ',');
1858 if (pos == NULL)
1859 break;
1860 pos++;
1861 }
1862
1863 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1864}
1865
1866
1867u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1868 const u8 *dst, const char *role)
1869{
1870 struct wpabuf *tlvs;
1871 u64 ret;
1872 const char *subelems;
1873 u8 id = 1;
1874
1875 subelems = os_strchr(role, ' ');
1876 if (subelems == NULL)
1877 return 0;
1878 subelems++;
1879
1880 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1881 if (tlvs == NULL)
1882 return 0;
1883
1884 if (os_strstr(role, "[source]"))
1885 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1886 if (os_strstr(role, "[pri-sink]"))
1887 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1888 if (os_strstr(role, "[sec-sink]"))
1889 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1890 if (os_strstr(role, "[source+sink]"))
1891 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1892
1893 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1894 wpabuf_free(tlvs);
1895 return ret;
1896}
1897
1898#endif /* CONFIG_WIFI_DISPLAY */
1899
1900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001901int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001902{
1903 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001904 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001905 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1906 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001907 return p2p_sd_cancel_request(wpa_s->global->p2p,
1908 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001909}
1910
1911
1912void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1913 const u8 *dst, u8 dialog_token,
1914 const struct wpabuf *resp_tlvs)
1915{
1916 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1917 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1918 resp_tlvs);
1919 return;
1920 }
1921 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1922 return;
1923 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1924 resp_tlvs);
1925}
1926
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001927#ifdef ANDROID_P2P
1928void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1929#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001931#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932{
1933 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1934 wpa_drv_p2p_service_update(wpa_s);
1935 return;
1936 }
1937 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001938#ifdef ANDROID_P2P
1939 p2p_sd_service_update(wpa_s->global->p2p, action);
1940#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001942#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943}
1944
1945
1946static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1947{
1948 dl_list_del(&bsrv->list);
1949 wpabuf_free(bsrv->query);
1950 wpabuf_free(bsrv->resp);
1951 os_free(bsrv);
1952}
1953
1954
1955static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1956{
1957 dl_list_del(&usrv->list);
1958 os_free(usrv->service);
1959 os_free(usrv);
1960}
1961
1962
1963void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1964{
1965 struct p2p_srv_bonjour *bsrv, *bn;
1966 struct p2p_srv_upnp *usrv, *un;
1967
1968 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1969 struct p2p_srv_bonjour, list)
1970 wpas_p2p_srv_bonjour_free(bsrv);
1971
1972 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1973 struct p2p_srv_upnp, list)
1974 wpas_p2p_srv_upnp_free(usrv);
1975
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001976#ifdef ANDROID_P2P
1977 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1978#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001980#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981}
1982
1983
1984int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1985 struct wpabuf *query, struct wpabuf *resp)
1986{
1987 struct p2p_srv_bonjour *bsrv;
1988
1989 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1990 if (bsrv) {
1991 wpabuf_free(query);
1992 wpabuf_free(bsrv->resp);
1993 bsrv->resp = resp;
1994 return 0;
1995 }
1996
1997 bsrv = os_zalloc(sizeof(*bsrv));
1998 if (bsrv == NULL)
1999 return -1;
2000 bsrv->query = query;
2001 bsrv->resp = resp;
2002 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2003
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002004#ifdef ANDROID_P2P
2005 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2006#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002007 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002008#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009 return 0;
2010}
2011
2012
2013int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2014 const struct wpabuf *query)
2015{
2016 struct p2p_srv_bonjour *bsrv;
2017
2018 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2019 if (bsrv == NULL)
2020 return -1;
2021 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002022#ifdef ANDROID_P2P
2023 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2024#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002026#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027 return 0;
2028}
2029
2030
2031int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2032 const char *service)
2033{
2034 struct p2p_srv_upnp *usrv;
2035
2036 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2037 return 0; /* Already listed */
2038 usrv = os_zalloc(sizeof(*usrv));
2039 if (usrv == NULL)
2040 return -1;
2041 usrv->version = version;
2042 usrv->service = os_strdup(service);
2043 if (usrv->service == NULL) {
2044 os_free(usrv);
2045 return -1;
2046 }
2047 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2048
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002049#ifdef ANDROID_P2P
2050 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2051#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002053#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054 return 0;
2055}
2056
2057
2058int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2059 const char *service)
2060{
2061 struct p2p_srv_upnp *usrv;
2062
2063 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2064 if (usrv == NULL)
2065 return -1;
2066 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002067#ifdef ANDROID_P2P
2068 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2069#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002071#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 return 0;
2073}
2074
2075
2076static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2077 const u8 *peer, const char *params,
2078 unsigned int generated_pin)
2079{
2080 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2081 MAC2STR(peer), generated_pin, params);
2082}
2083
2084
2085static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2086 const u8 *peer, const char *params)
2087{
2088 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2089 MAC2STR(peer), params);
2090}
2091
2092
2093void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2094 const u8 *dev_addr, const u8 *pri_dev_type,
2095 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002096 u8 dev_capab, u8 group_capab, const u8 *group_id,
2097 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098{
2099 struct wpa_supplicant *wpa_s = ctx;
2100 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002101 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 u8 empty_dev_type[8];
2103 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002104 struct wpa_supplicant *group = NULL;
2105
2106 if (group_id) {
2107 for (group = wpa_s->global->ifaces; group; group = group->next)
2108 {
2109 struct wpa_ssid *s = group->current_ssid;
2110 if (s != NULL &&
2111 s->mode == WPAS_MODE_P2P_GO &&
2112 group_id_len - ETH_ALEN == s->ssid_len &&
2113 os_memcmp(group_id + ETH_ALEN, s->ssid,
2114 s->ssid_len) == 0)
2115 break;
2116 }
2117 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118
2119 if (pri_dev_type == NULL) {
2120 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2121 pri_dev_type = empty_dev_type;
2122 }
2123 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2124 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002125 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126 MAC2STR(dev_addr),
2127 wps_dev_type_bin2str(pri_dev_type, devtype,
2128 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002129 dev_name, supp_config_methods, dev_capab, group_capab,
2130 group ? " group=" : "",
2131 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 if (config_methods & WPS_CONFIG_DISPLAY) {
2135 generated_pin = wps_generate_pin();
2136 wpas_prov_disc_local_display(wpa_s, peer, params,
2137 generated_pin);
2138 } else if (config_methods & WPS_CONFIG_KEYPAD)
2139 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2140 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2141 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2142 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002143
2144 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2145 P2P_PROV_DISC_SUCCESS,
2146 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147}
2148
2149
2150void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2151{
2152 struct wpa_supplicant *wpa_s = ctx;
2153 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002154 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002156 if (wpa_s->pending_pd_before_join &&
2157 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2158 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2159 wpa_s->pending_pd_before_join = 0;
2160 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2161 "join-existing-group operation");
2162 wpas_p2p_join_start(wpa_s);
2163 return;
2164 }
2165
Dmitry Shmidt04949592012-07-19 12:16:46 -07002166 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2167 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2168 os_snprintf(params, sizeof(params), " peer_go=%d",
2169 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2170 else
2171 params[0] = '\0';
2172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002174 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175 else if (config_methods & WPS_CONFIG_KEYPAD) {
2176 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002177 wpas_prov_disc_local_display(wpa_s, peer, params,
2178 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002180 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2181 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182
Jouni Malinen75ecf522011-06-27 15:19:46 -07002183 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2184 P2P_PROV_DISC_SUCCESS,
2185 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002186}
2187
2188
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002189static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2190 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002191{
2192 struct wpa_supplicant *wpa_s = ctx;
2193
Dmitry Shmidt04949592012-07-19 12:16:46 -07002194 if (wpa_s->p2p_fallback_to_go_neg) {
2195 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2196 "failed - fall back to GO Negotiation");
2197 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2198 return;
2199 }
2200
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002201#ifdef ANDROID_P2P
2202 /* If provision discovery failed it is safe to cancel the timer here and
2203 * also do not start the join */
2204 if (wpa_s->pending_pd_before_join &&
2205 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2206 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2207 wpa_s->pending_pd_before_join = 0;
2208 wpa_printf(MSG_DEBUG, "P2P: Do not Start pending "
2209 "join-existing-group operation");
2210 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2211 }
2212#endif /* ANDROID_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002213 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2214 " p2p_dev_addr=" MACSTR " status=%d",
2215 MAC2STR(peer), status);
2216
Jouni Malinen75ecf522011-06-27 15:19:46 -07002217 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2218 status, 0, 0);
2219}
2220
2221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2223 const u8 *go_dev_addr, const u8 *ssid,
2224 size_t ssid_len, int *go, u8 *group_bssid,
2225 int *force_freq, int persistent_group)
2226{
2227 struct wpa_supplicant *wpa_s = ctx;
2228 struct wpa_ssid *s;
2229 u8 cur_bssid[ETH_ALEN];
2230 int res;
2231 struct wpa_supplicant *grp;
2232
2233 if (!persistent_group) {
2234 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2235 " to join an active group", MAC2STR(sa));
2236 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2237 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2238 == 0 ||
2239 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2240 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2241 "authorized invitation");
2242 goto accept_inv;
2243 }
2244 /*
2245 * Do not accept the invitation automatically; notify user and
2246 * request approval.
2247 */
2248 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2249 }
2250
2251 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2252 if (grp) {
2253 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2254 "running persistent group");
2255 if (*go)
2256 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2257 goto accept_inv;
2258 }
2259
2260 if (!wpa_s->conf->persistent_reconnect)
2261 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2262
2263 for (s = wpa_s->conf->ssid; s; s = s->next) {
2264 if (s->disabled == 2 &&
2265 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2266 s->ssid_len == ssid_len &&
2267 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2268 break;
2269 }
2270
2271 if (!s) {
2272 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2273 " requested reinvocation of an unknown group",
2274 MAC2STR(sa));
2275 return P2P_SC_FAIL_UNKNOWN_GROUP;
2276 }
2277
2278 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2279 *go = 1;
2280 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2281 wpa_printf(MSG_DEBUG, "P2P: The only available "
2282 "interface is already in use - reject "
2283 "invitation");
2284 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2285 }
2286 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2287 } else if (s->mode == WPAS_MODE_P2P_GO) {
2288 *go = 1;
2289 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2290 {
2291 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2292 "interface address for the group");
2293 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2294 }
2295 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2296 ETH_ALEN);
2297 }
2298
2299accept_inv:
2300 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2301 wpa_s->assoc_freq) {
2302 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2303 "the channel we are already using");
2304 *force_freq = wpa_s->assoc_freq;
2305 }
2306
2307 res = wpa_drv_shared_freq(wpa_s);
2308 if (res > 0) {
2309 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2310 "with the channel we are already using on a "
2311 "shared interface");
2312 *force_freq = res;
2313 }
2314
2315 return P2P_SC_SUCCESS;
2316}
2317
2318
2319static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2320 const u8 *ssid, size_t ssid_len,
2321 const u8 *go_dev_addr, u8 status,
2322 int op_freq)
2323{
2324 struct wpa_supplicant *wpa_s = ctx;
2325 struct wpa_ssid *s;
2326
2327 for (s = wpa_s->conf->ssid; s; s = s->next) {
2328 if (s->disabled == 2 &&
2329 s->ssid_len == ssid_len &&
2330 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2331 break;
2332 }
2333
2334 if (status == P2P_SC_SUCCESS) {
2335 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2336 " was accepted; op_freq=%d MHz",
2337 MAC2STR(sa), op_freq);
2338 if (s) {
2339 wpas_p2p_group_add_persistent(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002340 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341 } else if (bssid) {
2342 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002343 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344 }
2345 return;
2346 }
2347
2348 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2349 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2350 " was rejected (status %u)", MAC2STR(sa), status);
2351 return;
2352 }
2353
2354 if (!s) {
2355 if (bssid) {
2356 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2357 "sa=" MACSTR " go_dev_addr=" MACSTR
2358 " bssid=" MACSTR " unknown-network",
2359 MAC2STR(sa), MAC2STR(go_dev_addr),
2360 MAC2STR(bssid));
2361 } else {
2362 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2363 "sa=" MACSTR " go_dev_addr=" MACSTR
2364 " unknown-network",
2365 MAC2STR(sa), MAC2STR(go_dev_addr));
2366 }
2367 return;
2368 }
2369
2370 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2371 " persistent=%d", MAC2STR(sa), s->id);
2372}
2373
2374
2375static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2376{
2377 struct wpa_supplicant *wpa_s = ctx;
2378 struct wpa_ssid *ssid;
2379
2380 if (bssid) {
2381 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2382 "status=%d " MACSTR,
2383 status, MAC2STR(bssid));
2384 } else {
2385 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2386 "status=%d ", status);
2387 }
2388 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2389
2390 if (wpa_s->pending_invite_ssid_id == -1)
2391 return; /* Invitation to active group */
2392
2393 if (status != P2P_SC_SUCCESS) {
2394 wpas_p2p_remove_pending_group_interface(wpa_s);
2395 return;
2396 }
2397
2398 ssid = wpa_config_get_network(wpa_s->conf,
2399 wpa_s->pending_invite_ssid_id);
2400 if (ssid == NULL) {
2401 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2402 "data matching with invitation");
2403 return;
2404 }
2405
2406 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002407 ssid->mode == WPAS_MODE_P2P_GO,
2408 wpa_s->p2p_persistent_go_freq,
2409 wpa_s->p2p_go_ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410}
2411
2412
Dmitry Shmidt04949592012-07-19 12:16:46 -07002413static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2414 unsigned int freq)
2415{
2416 unsigned int i;
2417
2418 if (global->p2p_disallow_freq == NULL)
2419 return 0;
2420
2421 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2422 if (freq >= global->p2p_disallow_freq[i].min &&
2423 freq <= global->p2p_disallow_freq[i].max)
2424 return 1;
2425 }
2426
2427 return 0;
2428}
2429
2430
2431static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2432{
2433 reg->channel[reg->channels] = chan;
2434 reg->channels++;
2435}
2436
2437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2439 struct p2p_channels *chan)
2440{
2441 int i, cla = 0;
2442
2443 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2444 "band");
2445
2446 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2447 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002448 chan->reg_class[cla].channels = 0;
2449 for (i = 0; i < 11; i++) {
2450 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2451 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2452 }
2453 if (chan->reg_class[cla].channels)
2454 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455
2456 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2457 "band");
2458
2459 /* Operating class 115 - 5 GHz, channels 36-48 */
2460 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002461 chan->reg_class[cla].channels = 0;
2462 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2463 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2464 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2465 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2466 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2467 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2468 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2469 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2470 if (chan->reg_class[cla].channels)
2471 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472
2473 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2474 "band");
2475
2476 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2477 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002478 chan->reg_class[cla].channels = 0;
2479 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2480 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2481 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2482 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2483 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2484 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2485 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2486 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2487 if (chan->reg_class[cla].channels)
2488 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002489
2490 chan->reg_classes = cla;
2491 return 0;
2492}
2493
2494
2495static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2496 u16 num_modes,
2497 enum hostapd_hw_mode mode)
2498{
2499 u16 i;
2500
2501 for (i = 0; i < num_modes; i++) {
2502 if (modes[i].mode == mode)
2503 return &modes[i];
2504 }
2505
2506 return NULL;
2507}
2508
2509
Dmitry Shmidt04949592012-07-19 12:16:46 -07002510static int has_channel(struct wpa_global *global,
2511 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512{
2513 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002514 unsigned int freq;
2515
2516 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2517 chan * 5;
2518 if (wpas_p2p_disallowed_freq(global, freq))
2519 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520
2521 for (i = 0; i < mode->num_channels; i++) {
2522 if (mode->channels[i].chan == chan) {
2523 if (flags)
2524 *flags = mode->channels[i].flag;
2525 return !(mode->channels[i].flag &
2526 (HOSTAPD_CHAN_DISABLED |
2527 HOSTAPD_CHAN_PASSIVE_SCAN |
2528 HOSTAPD_CHAN_NO_IBSS |
2529 HOSTAPD_CHAN_RADAR));
2530 }
2531 }
2532
2533 return 0;
2534}
2535
2536
2537struct p2p_oper_class_map {
2538 enum hostapd_hw_mode mode;
2539 u8 op_class;
2540 u8 min_chan;
2541 u8 max_chan;
2542 u8 inc;
2543 enum { BW20, BW40PLUS, BW40MINUS } bw;
2544};
2545
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002546static struct p2p_oper_class_map op_class[] = {
2547 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2548 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2549#if 0 /* Do not enable HT40 on 2 GHz for now */
2550 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2551 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2552#endif
2553 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2554 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2555 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2556 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2557 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2558 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2559 { -1, 0, 0, 0, 0, BW20 }
2560};
2561
2562
2563static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2564 struct hostapd_hw_modes *mode,
2565 u8 channel, u8 bw)
2566{
2567 int flag;
2568
2569 if (!has_channel(wpa_s->global, mode, channel, &flag))
2570 return -1;
2571 if (bw == BW40MINUS &&
2572 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2573 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2574 return 0;
2575 if (bw == BW40PLUS &&
2576 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2577 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2578 return 0;
2579 return 1;
2580}
2581
2582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2584 struct p2p_channels *chan)
2585{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002586 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002589 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2591 "of all supported channels; assume dualband "
2592 "support");
2593 return wpas_p2p_default_channels(wpa_s, chan);
2594 }
2595
2596 cla = 0;
2597
2598 for (op = 0; op_class[op].op_class; op++) {
2599 struct p2p_oper_class_map *o = &op_class[op];
2600 u8 ch;
2601 struct p2p_reg_class *reg = NULL;
2602
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002603 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002604 if (mode == NULL)
2605 continue;
2606 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002607 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 continue;
2609 if (reg == NULL) {
2610 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2611 "class %u", o->op_class);
2612 reg = &chan->reg_class[cla];
2613 cla++;
2614 reg->reg_class = o->op_class;
2615 }
2616 reg->channel[reg->channels] = ch;
2617 reg->channels++;
2618 }
2619 if (reg) {
2620 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2621 reg->channel, reg->channels);
2622 }
2623 }
2624
2625 chan->reg_classes = cla;
2626
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627 return 0;
2628}
2629
2630
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002631int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2632 struct hostapd_hw_modes *mode, u8 channel)
2633{
2634 int op, ret;
2635
2636 for (op = 0; op_class[op].op_class; op++) {
2637 struct p2p_oper_class_map *o = &op_class[op];
2638 u8 ch;
2639
2640 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2641 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2642 o->bw == BW20 || ch != channel)
2643 continue;
2644 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2645 if (ret < 0)
2646 continue;
2647 else if (ret > 0)
2648 return (o->bw == BW40MINUS) ? -1 : 1;
2649 else
2650 return 0;
2651 }
2652 }
2653 return 0;
2654}
2655
2656
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2658 size_t buf_len)
2659{
2660 struct wpa_supplicant *wpa_s = ctx;
2661
2662 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2663 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2664 break;
2665 }
2666 if (wpa_s == NULL)
2667 return -1;
2668
2669 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2670}
2671
2672
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002673static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2674{
2675 struct wpa_supplicant *wpa_s = ctx;
2676
2677 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2678 struct wpa_ssid *ssid = wpa_s->current_ssid;
2679 if (ssid == NULL)
2680 continue;
2681 if (ssid->mode != WPAS_MODE_INFRA)
2682 continue;
2683 if (wpa_s->wpa_state != WPA_COMPLETED &&
2684 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2685 continue;
2686 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2687 return 1;
2688 }
2689
2690 return 0;
2691}
2692
2693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002694/**
2695 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2696 * @global: Pointer to global data from wpa_supplicant_init()
2697 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2698 * Returns: 0 on success, -1 on failure
2699 */
2700int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2701{
2702 struct p2p_config p2p;
2703 unsigned int r;
2704 int i;
2705
2706 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2707 return 0;
2708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002709 if (global->p2p)
2710 return 0;
2711
2712 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2713 struct p2p_params params;
2714
2715 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2716 os_memset(&params, 0, sizeof(params));
2717 params.dev_name = wpa_s->conf->device_name;
2718 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2719 WPS_DEV_TYPE_LEN);
2720 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2721 os_memcpy(params.sec_dev_type,
2722 wpa_s->conf->sec_device_type,
2723 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2724
2725 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2726 return -1;
2727
2728 return 0;
2729 }
2730
2731 os_memset(&p2p, 0, sizeof(p2p));
2732 p2p.msg_ctx = wpa_s;
2733 p2p.cb_ctx = wpa_s;
2734 p2p.p2p_scan = wpas_p2p_scan;
2735 p2p.send_action = wpas_send_action;
2736 p2p.send_action_done = wpas_send_action_done;
2737 p2p.go_neg_completed = wpas_go_neg_completed;
2738 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2739 p2p.dev_found = wpas_dev_found;
2740 p2p.dev_lost = wpas_dev_lost;
2741 p2p.start_listen = wpas_start_listen;
2742 p2p.stop_listen = wpas_stop_listen;
2743 p2p.send_probe_resp = wpas_send_probe_resp;
2744 p2p.sd_request = wpas_sd_request;
2745 p2p.sd_response = wpas_sd_response;
2746 p2p.prov_disc_req = wpas_prov_disc_req;
2747 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002748 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749 p2p.invitation_process = wpas_invitation_process;
2750 p2p.invitation_received = wpas_invitation_received;
2751 p2p.invitation_result = wpas_invitation_result;
2752 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002753 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002754
2755 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002756 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002757 p2p.dev_name = wpa_s->conf->device_name;
2758 p2p.manufacturer = wpa_s->conf->manufacturer;
2759 p2p.model_name = wpa_s->conf->model_name;
2760 p2p.model_number = wpa_s->conf->model_number;
2761 p2p.serial_number = wpa_s->conf->serial_number;
2762 if (wpa_s->wps) {
2763 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2764 p2p.config_methods = wpa_s->wps->config_methods;
2765 }
2766
2767 if (wpa_s->conf->p2p_listen_reg_class &&
2768 wpa_s->conf->p2p_listen_channel) {
2769 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2770 p2p.channel = wpa_s->conf->p2p_listen_channel;
2771 } else {
2772 p2p.reg_class = 81;
2773 /*
2774 * Pick one of the social channels randomly as the listen
2775 * channel.
2776 */
2777 os_get_random((u8 *) &r, sizeof(r));
2778 p2p.channel = 1 + (r % 3) * 5;
2779 }
2780 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2781
2782 if (wpa_s->conf->p2p_oper_reg_class &&
2783 wpa_s->conf->p2p_oper_channel) {
2784 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2785 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2786 p2p.cfg_op_channel = 1;
2787 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2788 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2789
2790 } else {
2791 p2p.op_reg_class = 81;
2792 /*
2793 * Use random operation channel from (1, 6, 11) if no other
2794 * preference is indicated.
2795 */
2796 os_get_random((u8 *) &r, sizeof(r));
2797 p2p.op_channel = 1 + (r % 3) * 5;
2798 p2p.cfg_op_channel = 0;
2799 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2800 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2801 }
2802 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2803 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2804 p2p.country[2] = 0x04;
2805 } else
2806 os_memcpy(p2p.country, "XX\x04", 3);
2807
2808 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2809 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2810 "channel list");
2811 return -1;
2812 }
2813
2814 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2815 WPS_DEV_TYPE_LEN);
2816
2817 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2818 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2819 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2820
2821 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2822 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2823
2824 p2p.max_peers = 100;
2825
2826 if (wpa_s->conf->p2p_ssid_postfix) {
2827 p2p.ssid_postfix_len =
2828 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2829 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2830 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2831 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2832 p2p.ssid_postfix_len);
2833 }
2834
2835 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2836
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07002837#ifdef ANDROID_P2P
2838 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2839 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
2840 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)
2841 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
2842#endif
2843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844 global->p2p = p2p_init(&p2p);
2845 if (global->p2p == NULL)
2846 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002847 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848
2849 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2850 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2851 continue;
2852 p2p_add_wps_vendor_extension(
2853 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2854 }
2855
2856 return 0;
2857}
2858
2859
2860/**
2861 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2862 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2863 *
2864 * This function deinitialize per-interface P2P data.
2865 */
2866void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2867{
2868 if (wpa_s->driver && wpa_s->drv_priv)
2869 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002870
2871 if (wpa_s->go_params) {
2872 /* Clear any stored provisioning info */
2873 p2p_clear_provisioning_info(
2874 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002875 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002876 }
2877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002878 os_free(wpa_s->go_params);
2879 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2881 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002882 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883 wpa_s->p2p_long_listen = 0;
2884 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2885 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2886 wpas_p2p_remove_pending_group_interface(wpa_s);
2887
2888 /* TODO: remove group interface from the driver if this wpa_s instance
2889 * is on top of a P2P group interface */
2890}
2891
2892
2893/**
2894 * wpas_p2p_deinit_global - Deinitialize global P2P module
2895 * @global: Pointer to global data from wpa_supplicant_init()
2896 *
2897 * This function deinitializes the global (per device) P2P module.
2898 */
2899void wpas_p2p_deinit_global(struct wpa_global *global)
2900{
2901 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002903 wpa_s = global->ifaces;
2904 if (wpa_s)
2905 wpas_p2p_service_flush(wpa_s);
2906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002907 if (global->p2p == NULL)
2908 return;
2909
2910 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2912 wpa_s = wpa_s->next;
2913 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914 tmp = global->ifaces;
2915 while (tmp &&
2916 (tmp == wpa_s ||
2917 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2918 tmp = tmp->next;
2919 }
2920 if (tmp == NULL)
2921 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002922 /* Disconnect from the P2P group and deinit the interface */
2923 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 }
2925
2926 /*
2927 * Deinit GO data on any possibly remaining interface (if main
2928 * interface is used as GO).
2929 */
2930 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2931 if (wpa_s->ap_iface)
2932 wpas_p2p_group_deinit(wpa_s);
2933 }
2934
2935 p2p_deinit(global->p2p);
2936 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002937 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938}
2939
2940
2941static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2942{
2943 if (wpa_s->drv_flags &
2944 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2945 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2946 return 1; /* P2P group requires a new interface in every case
2947 */
2948 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2949 return 0; /* driver does not support concurrent operations */
2950 if (wpa_s->global->ifaces->next)
2951 return 1; /* more that one interface already in use */
2952 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2953 return 1; /* this interface is already in use */
2954 return 0;
2955}
2956
2957
2958static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2959 const u8 *peer_addr,
2960 enum p2p_wps_method wps_method,
2961 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002962 unsigned int force_freq, int persistent_group,
2963 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002964{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002965 if (persistent_group && wpa_s->conf->persistent_reconnect)
2966 persistent_group = 2;
2967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002968 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2969 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2970 go_intent, own_interface_addr,
2971 force_freq, persistent_group);
2972 }
2973
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002974 /*
2975 * Increase GO config timeout if HT40 is used since it takes some time
2976 * to scan channels for coex purposes before the BSS can be started.
2977 */
2978 p2p_set_config_timeout(wpa_s->global->p2p,
2979 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002981 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2982 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002983 persistent_group, ssid ? ssid->ssid : NULL,
2984 ssid ? ssid->ssid_len : 0,
2985 wpa_s->p2p_pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986}
2987
2988
2989static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2990 const u8 *peer_addr,
2991 enum p2p_wps_method wps_method,
2992 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002993 unsigned int force_freq, int persistent_group,
2994 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002996 if (persistent_group && wpa_s->conf->persistent_reconnect)
2997 persistent_group = 2;
2998
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3000 return -1;
3001
3002 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3003 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003004 persistent_group, ssid ? ssid->ssid : NULL,
3005 ssid ? ssid->ssid_len : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006}
3007
3008
3009static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3010{
3011 wpa_s->p2p_join_scan_count++;
3012 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3013 wpa_s->p2p_join_scan_count);
3014 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3015 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3016 " for join operationg - stop join attempt",
3017 MAC2STR(wpa_s->pending_join_iface_addr));
3018 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003019 if (wpa_s->p2p_auto_pd) {
3020 wpa_s->p2p_auto_pd = 0;
3021 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3022 " p2p_dev_addr=" MACSTR " status=N/A",
3023 MAC2STR(wpa_s->pending_join_dev_addr));
3024 return;
3025 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026 wpa_msg(wpa_s->parent, MSG_INFO,
3027 P2P_EVENT_GROUP_FORMATION_FAILURE);
3028 }
3029}
3030
3031
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003032static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
3033{
3034 struct wpa_supplicant *wpa_s = eloop_ctx;
3035 if (!wpa_s->pending_pd_before_join)
3036 return;
3037 /*
3038 * Provision Discovery Response may have been lost - try to connect
3039 * anyway since we do not need any information from this PD.
3040 */
3041 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
3042 "try to connect anyway");
3043 wpas_p2p_join_start(wpa_s);
3044}
3045
3046
Dmitry Shmidt04949592012-07-19 12:16:46 -07003047static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3048{
3049 struct wpa_supplicant *iface;
3050 int shared_freq;
3051 u8 bssid[ETH_ALEN];
3052
3053 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3054 return 0;
3055
3056 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3057 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3058 continue;
3059 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3060 continue;
3061 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3062 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3063 shared_freq = iface->current_ssid->frequency;
3064 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3065 shared_freq = iface->assoc_freq;
3066 else
3067 shared_freq = 0;
3068
3069 if (shared_freq && freq != shared_freq) {
3070 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3071 "connected on %d MHz - new connection on "
3072 "%d MHz", iface->ifname, shared_freq, freq);
3073 return 1;
3074 }
3075 }
3076
3077 shared_freq = wpa_drv_shared_freq(wpa_s);
3078 if (shared_freq > 0 && shared_freq != freq) {
3079 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3080 "virtual interface connected on %d MHz - new "
3081 "connection on %d MHz", shared_freq, freq);
3082 return 1;
3083 }
3084
3085 return 0;
3086}
3087
3088
3089static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3090 const u8 *peer_dev_addr)
3091{
3092 struct wpa_bss *bss;
3093 int updated;
3094
3095 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3096 if (bss == NULL)
3097 return -1;
3098 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3099 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3100 "last scan");
3101 return 0;
3102 }
3103
3104 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3105 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3106 "%ld.%06ld (%supdated in last scan)",
3107 bss->last_update.sec, bss->last_update.usec,
3108 updated ? "": "not ");
3109
3110 return updated;
3111}
3112
3113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3115 struct wpa_scan_results *scan_res)
3116{
3117 struct wpa_bss *bss;
3118 int freq;
3119 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003121 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3122
3123 if (wpa_s->global->p2p_disabled)
3124 return;
3125
Dmitry Shmidt04949592012-07-19 12:16:46 -07003126 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3127 scan_res ? (int) scan_res->num : -1,
3128 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003129
3130 if (scan_res)
3131 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3132
Dmitry Shmidt04949592012-07-19 12:16:46 -07003133 if (wpa_s->p2p_auto_pd) {
3134 int join = wpas_p2p_peer_go(wpa_s,
3135 wpa_s->pending_join_dev_addr);
3136 if (join == 0 &&
3137 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3138 wpa_s->auto_pd_scan_retry++;
3139 bss = wpa_bss_get_bssid(wpa_s,
3140 wpa_s->pending_join_dev_addr);
3141 if (bss) {
3142 freq = bss->freq;
3143 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3144 "the peer " MACSTR " at %d MHz",
3145 wpa_s->auto_pd_scan_retry,
3146 MAC2STR(wpa_s->
3147 pending_join_dev_addr),
3148 freq);
3149 wpas_p2p_join_scan_req(wpa_s, freq);
3150 return;
3151 }
3152 }
3153
3154 if (join < 0)
3155 join = 0;
3156
3157 wpa_s->p2p_auto_pd = 0;
3158 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3159 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3160 MAC2STR(wpa_s->pending_join_dev_addr), join);
3161 if (p2p_prov_disc_req(wpa_s->global->p2p,
3162 wpa_s->pending_join_dev_addr,
3163 wpa_s->pending_pd_config_methods, join,
3164 0) < 0) {
3165 wpa_s->p2p_auto_pd = 0;
3166 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3167 " p2p_dev_addr=" MACSTR " status=N/A",
3168 MAC2STR(wpa_s->pending_join_dev_addr));
3169 }
3170 return;
3171 }
3172
3173 if (wpa_s->p2p_auto_join) {
3174 int join = wpas_p2p_peer_go(wpa_s,
3175 wpa_s->pending_join_dev_addr);
3176 if (join < 0) {
3177 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3178 "running a GO -> use GO Negotiation");
3179 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3180 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3181 wpa_s->p2p_persistent_group, 0, 0, 0,
3182 wpa_s->p2p_go_intent,
3183 wpa_s->p2p_connect_freq,
3184 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003185 wpa_s->p2p_pd_before_go_neg,
3186 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003187 return;
3188 }
3189
3190 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3191 "try to join the group", join ? "" :
3192 " in older scan");
3193 if (!join)
3194 wpa_s->p2p_fallback_to_go_neg = 1;
3195 }
3196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3198 wpa_s->pending_join_iface_addr);
3199 if (freq < 0 &&
3200 p2p_get_interface_addr(wpa_s->global->p2p,
3201 wpa_s->pending_join_dev_addr,
3202 iface_addr) == 0 &&
3203 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3204 {
3205 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3206 "address for join from " MACSTR " to " MACSTR
3207 " based on newly discovered P2P peer entry",
3208 MAC2STR(wpa_s->pending_join_iface_addr),
3209 MAC2STR(iface_addr));
3210 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3211 ETH_ALEN);
3212
3213 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3214 wpa_s->pending_join_iface_addr);
3215 }
3216 if (freq >= 0) {
3217 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3218 "from P2P peer table: %d MHz", freq);
3219 }
3220 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3221 if (bss) {
3222 freq = bss->freq;
3223 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3224 "from BSS table: %d MHz", freq);
3225 }
3226 if (freq > 0) {
3227 u16 method;
3228
Dmitry Shmidt04949592012-07-19 12:16:46 -07003229 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3230 wpa_msg(wpa_s->parent, MSG_INFO,
3231 P2P_EVENT_GROUP_FORMATION_FAILURE
3232 "reason=FREQ_CONFLICT");
3233 return;
3234 }
3235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003236 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3237 "prior to joining an existing group (GO " MACSTR
3238 " freq=%u MHz)",
3239 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3240 wpa_s->pending_pd_before_join = 1;
3241
3242 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 case WPS_PIN_DISPLAY:
3244 method = WPS_CONFIG_KEYPAD;
3245 break;
3246 case WPS_PIN_KEYPAD:
3247 method = WPS_CONFIG_DISPLAY;
3248 break;
3249 case WPS_PBC:
3250 method = WPS_CONFIG_PUSHBUTTON;
3251 break;
3252 default:
3253 method = 0;
3254 break;
3255 }
3256
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003257 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3258 wpa_s->pending_join_dev_addr) ==
3259 method)) {
3260 /*
3261 * We have already performed provision discovery for
3262 * joining the group. Proceed directly to join
3263 * operation without duplicated provision discovery. */
3264 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3265 "with " MACSTR " already done - proceed to "
3266 "join",
3267 MAC2STR(wpa_s->pending_join_dev_addr));
3268 wpa_s->pending_pd_before_join = 0;
3269 goto start;
3270 }
3271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003273 wpa_s->pending_join_dev_addr, method, 1,
3274 freq) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3276 "Discovery Request before joining an "
3277 "existing group");
3278 wpa_s->pending_pd_before_join = 0;
3279 goto start;
3280 }
3281
3282 /*
3283 * Actual join operation will be started from the Action frame
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003284 * TX status callback (if no ACK is received) or when the
3285 * Provision Discovery Response is received. Use a short
3286 * timeout as a backup mechanism should the Provision Discovery
3287 * Response be lost for any reason.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003289 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3290 NULL);
3291 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3292 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 return;
3294 }
3295
3296 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3297 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3298 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3299 wpas_p2p_check_join_scan_limit(wpa_s);
3300 return;
3301
3302start:
3303 /* Start join operation immediately */
3304 wpas_p2p_join_start(wpa_s);
3305}
3306
3307
Dmitry Shmidt04949592012-07-19 12:16:46 -07003308static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310 int ret;
3311 struct wpa_driver_scan_params params;
3312 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003313 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003314 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003315#ifdef ANDROID_P2P
3316 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003317
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003318 /* If freq is not provided, check the operating freq of the GO and do a
3319 * a directed scan to save time
3320 */
3321 if(!freq) {
3322 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3323 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3324 }
3325#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326 os_memset(&params, 0, sizeof(params));
3327
3328 /* P2P Wildcard SSID */
3329 params.num_ssids = 1;
3330 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3331 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3332
3333 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003334 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3335 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3336 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337 if (wps_ie == NULL) {
3338 wpas_p2p_scan_res_join(wpa_s, NULL);
3339 return;
3340 }
3341
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003342 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3343 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003344 if (ies == NULL) {
3345 wpabuf_free(wps_ie);
3346 wpas_p2p_scan_res_join(wpa_s, NULL);
3347 return;
3348 }
3349 wpabuf_put_buf(ies, wps_ie);
3350 wpabuf_free(wps_ie);
3351
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003352 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003353
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003354 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003355 params.extra_ies = wpabuf_head(ies);
3356 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003357 if (freq > 0) {
3358 freqs[0] = freq;
3359 params.freqs = freqs;
3360 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003361
3362 /*
3363 * Run a scan to update BSS table and start Provision Discovery once
3364 * the new scan results become available.
3365 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003366 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003367 if (!ret)
3368 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369
3370 wpabuf_free(ies);
3371
3372 if (ret) {
3373 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3374 "try again later");
3375 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3376 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3377 wpas_p2p_check_join_scan_limit(wpa_s);
3378 }
3379}
3380
3381
Dmitry Shmidt04949592012-07-19 12:16:46 -07003382static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3383{
3384 struct wpa_supplicant *wpa_s = eloop_ctx;
3385 wpas_p2p_join_scan_req(wpa_s, 0);
3386}
3387
3388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003389static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003390 const u8 *dev_addr, enum p2p_wps_method wps_method,
3391 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003392{
3393 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003394 MACSTR " dev " MACSTR ")%s",
3395 MAC2STR(iface_addr), MAC2STR(dev_addr),
3396 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003397
Dmitry Shmidt04949592012-07-19 12:16:46 -07003398 wpa_s->p2p_auto_pd = 0;
3399 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003400 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3401 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3402 wpa_s->pending_join_wps_method = wps_method;
3403
3404 /* Make sure we are not running find during connection establishment */
3405 wpas_p2p_stop_find(wpa_s);
3406
3407 wpa_s->p2p_join_scan_count = 0;
3408 wpas_p2p_join_scan(wpa_s, NULL);
3409 return 0;
3410}
3411
3412
3413static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3414{
3415 struct wpa_supplicant *group;
3416 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003417 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003419 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3421 if (group == NULL)
3422 return -1;
3423 if (group != wpa_s) {
3424 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3425 sizeof(group->p2p_pin));
3426 group->p2p_wps_method = wpa_s->p2p_wps_method;
3427 }
3428
3429 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003430 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431
3432 os_memset(&res, 0, sizeof(res));
3433 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3434 ETH_ALEN);
3435 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003436 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3437 if (bss) {
3438 res.freq = bss->freq;
3439 res.ssid_len = bss->ssid_len;
3440 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3441 }
3442
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003443 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3444 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3445 "starting client");
3446 wpa_drv_cancel_remain_on_channel(wpa_s);
3447 wpa_s->off_channel_freq = 0;
3448 wpa_s->roc_waiting_drv_freq = 0;
3449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450 wpas_start_wps_enrollee(group, &res);
3451
3452 /*
3453 * Allow a longer timeout for join-a-running-group than normal 15
3454 * second group formation timeout since the GO may not have authorized
3455 * our connection yet.
3456 */
3457 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3458 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3459 wpa_s, NULL);
3460
3461 return 0;
3462}
3463
3464
3465/**
3466 * wpas_p2p_connect - Request P2P Group Formation to be started
3467 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3468 * @peer_addr: Address of the peer P2P Device
3469 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3470 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003471 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003472 * @join: Whether to join an existing group (as a client) instead of starting
3473 * Group Owner negotiation; @peer_addr is BSSID in that case
3474 * @auth: Whether to only authorize the connection instead of doing that and
3475 * initiating Group Owner negotiation
3476 * @go_intent: GO Intent or -1 to use default
3477 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003478 * @persistent_id: Persistent group credentials to use for forcing GO
3479 * parameters or -1 to generate new values (SSID/passphrase)
3480 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3481 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003482 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003483 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3484 * failure, -2 on failure due to channel not currently available,
3485 * -3 if forced channel is not supported
3486 */
3487int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3488 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003489 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003490 int go_intent, int freq, int persistent_id, int pd,
3491 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003492{
3493 int force_freq = 0, oper_freq = 0;
3494 u8 bssid[ETH_ALEN];
3495 int ret = 0;
3496 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003497 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003498 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003499
3500 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3501 return -1;
3502
Dmitry Shmidt04949592012-07-19 12:16:46 -07003503 if (persistent_id >= 0) {
3504 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3505 if (ssid == NULL || ssid->disabled != 2 ||
3506 ssid->mode != WPAS_MODE_P2P_GO)
3507 return -1;
3508 }
3509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510 if (go_intent < 0)
3511 go_intent = wpa_s->conf->p2p_go_intent;
3512
3513 if (!auth)
3514 wpa_s->p2p_long_listen = 0;
3515
3516 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003517 wpa_s->p2p_persistent_group = !!persistent_group;
3518 wpa_s->p2p_persistent_id = persistent_id;
3519 wpa_s->p2p_go_intent = go_intent;
3520 wpa_s->p2p_connect_freq = freq;
3521 wpa_s->p2p_fallback_to_go_neg = 0;
3522 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003523 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003524
3525 if (pin)
3526 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3527 else if (wps_method == WPS_PIN_DISPLAY) {
3528 ret = wps_generate_pin();
3529 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3530 ret);
3531 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3532 wpa_s->p2p_pin);
3533 } else
3534 wpa_s->p2p_pin[0] = '\0';
3535
Dmitry Shmidt04949592012-07-19 12:16:46 -07003536 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003537 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3538 if (auth) {
3539 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3540 "connect a running group from " MACSTR,
3541 MAC2STR(peer_addr));
3542 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3543 return ret;
3544 }
3545 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3546 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3547 iface_addr) < 0) {
3548 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3549 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3550 dev_addr);
3551 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003552 if (auto_join) {
3553 os_get_time(&wpa_s->p2p_auto_started);
3554 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3555 "%ld.%06ld",
3556 wpa_s->p2p_auto_started.sec,
3557 wpa_s->p2p_auto_started.usec);
3558 }
3559 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3560 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 return -1;
3562 return ret;
3563 }
3564
3565 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3566 wpa_s->assoc_freq)
3567 oper_freq = wpa_s->assoc_freq;
3568 else {
3569 oper_freq = wpa_drv_shared_freq(wpa_s);
3570 if (oper_freq < 0)
3571 oper_freq = 0;
3572 }
3573
3574 if (freq > 0) {
3575 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3576 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3577 "(%u MHz) is not supported for P2P uses",
3578 freq);
3579 return -3;
3580 }
3581
3582 if (oper_freq > 0 && freq != oper_freq &&
3583 !(wpa_s->drv_flags &
3584 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3585 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3586 "on %u MHz while connected on another "
3587 "channel (%u MHz)", freq, oper_freq);
3588 return -2;
3589 }
3590 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3591 "requested channel (%u MHz)", freq);
3592 force_freq = freq;
3593 } else if (oper_freq > 0 &&
3594 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3595 if (!(wpa_s->drv_flags &
3596 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3597 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3598 "while connected on non-P2P supported "
3599 "channel (%u MHz)", oper_freq);
3600 return -2;
3601 }
3602 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3603 "(%u MHz) not available for P2P - try to use "
3604 "another channel", oper_freq);
3605 force_freq = 0;
3606 } else if (oper_freq > 0) {
3607 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3608 "channel we are already using (%u MHz) on another "
3609 "interface", oper_freq);
3610 force_freq = oper_freq;
3611 }
3612
3613 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3614
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003615 if (wpa_s->create_p2p_iface) {
3616 /* Prepare to add a new interface for the group */
3617 iftype = WPA_IF_P2P_GROUP;
3618 if (go_intent == 15)
3619 iftype = WPA_IF_P2P_GO;
3620 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3621 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3622 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003624 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003626 if_addr = wpa_s->pending_interface_addr;
3627 } else
3628 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629
3630 if (auth) {
3631 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003632 go_intent, if_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003633 force_freq, persistent_group, ssid) <
3634 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003635 return -1;
3636 return ret;
3637 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003638
3639 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3640 go_intent, if_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003641 persistent_group, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003642 if (wpa_s->create_p2p_iface)
3643 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003644 return -1;
3645 }
3646 return ret;
3647}
3648
3649
3650/**
3651 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3652 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3653 * @freq: Frequency of the channel in MHz
3654 * @duration: Duration of the stay on the channel in milliseconds
3655 *
3656 * This callback is called when the driver indicates that it has started the
3657 * requested remain-on-channel duration.
3658 */
3659void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3660 unsigned int freq, unsigned int duration)
3661{
3662 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3663 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3665 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3666 wpa_s->pending_listen_duration);
3667 wpa_s->pending_listen_freq = 0;
3668 }
3669}
3670
3671
3672static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3673 unsigned int timeout)
3674{
3675 /* Limit maximum Listen state time based on driver limitation. */
3676 if (timeout > wpa_s->max_remain_on_chan)
3677 timeout = wpa_s->max_remain_on_chan;
3678
3679 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3680 return wpa_drv_p2p_listen(wpa_s, timeout);
3681
3682 return p2p_listen(wpa_s->global->p2p, timeout);
3683}
3684
3685
3686/**
3687 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3688 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3689 * @freq: Frequency of the channel in MHz
3690 *
3691 * This callback is called when the driver indicates that a remain-on-channel
3692 * operation has been completed, i.e., the duration on the requested channel
3693 * has timed out.
3694 */
3695void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3696 unsigned int freq)
3697{
3698 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3699 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3700 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003701 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3702 return;
3703 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3704 return; /* P2P module started a new operation */
3705 if (wpa_s->pending_action_tx)
3706 return;
3707 if (wpa_s->p2p_long_listen > 0)
3708 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3709 if (wpa_s->p2p_long_listen > 0) {
3710 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3711 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3712 }
3713}
3714
3715
3716/**
3717 * wpas_p2p_group_remove - Remove a P2P group
3718 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3719 * @ifname: Network interface name of the group interface or "*" to remove all
3720 * groups
3721 * Returns: 0 on success, -1 on failure
3722 *
3723 * This function is used to remove a P2P group. This can be used to disconnect
3724 * from a group in which the local end is a P2P Client or to end a P2P Group in
3725 * case the local end is the Group Owner. If a virtual network interface was
3726 * created for this group, that interface will be removed. Otherwise, only the
3727 * configured P2P group network will be removed from the interface.
3728 */
3729int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3730{
3731 struct wpa_global *global = wpa_s->global;
3732
3733 if (os_strcmp(ifname, "*") == 0) {
3734 struct wpa_supplicant *prev;
3735 wpa_s = global->ifaces;
3736 while (wpa_s) {
3737 prev = wpa_s;
3738 wpa_s = wpa_s->next;
3739 wpas_p2p_disconnect(prev);
3740 }
3741 return 0;
3742 }
3743
3744 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3745 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3746 break;
3747 }
3748
3749 return wpas_p2p_disconnect(wpa_s);
3750}
3751
3752
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003753static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3754 struct p2p_go_neg_results *params,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003755 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003756{
3757 u8 bssid[ETH_ALEN];
3758 int res;
3759
3760 os_memset(params, 0, sizeof(*params));
3761 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003762 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763 if (freq) {
3764 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3765 "frequency %d MHz", freq);
3766 params->freq = freq;
3767 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3768 wpa_s->conf->p2p_oper_channel >= 1 &&
3769 wpa_s->conf->p2p_oper_channel <= 11) {
3770 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3771 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3772 "frequency %d MHz", params->freq);
3773 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003774 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003775 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3776 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3777 "frequency %d MHz", params->freq);
3778 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3779 wpa_s->best_overall_freq > 0 &&
3780 p2p_supported_freq(wpa_s->global->p2p,
3781 wpa_s->best_overall_freq)) {
3782 params->freq = wpa_s->best_overall_freq;
3783 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3784 "channel %d MHz", params->freq);
3785 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3786 wpa_s->best_24_freq > 0 &&
3787 p2p_supported_freq(wpa_s->global->p2p,
3788 wpa_s->best_24_freq)) {
3789 params->freq = wpa_s->best_24_freq;
3790 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3791 "channel %d MHz", params->freq);
3792 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3793 wpa_s->best_5_freq > 0 &&
3794 p2p_supported_freq(wpa_s->global->p2p,
3795 wpa_s->best_5_freq)) {
3796 params->freq = wpa_s->best_5_freq;
3797 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3798 "channel %d MHz", params->freq);
3799 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003800 int chan;
3801 for (chan = 0; chan < 11; chan++) {
3802 params->freq = 2412 + chan * 5;
3803 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3804 params->freq))
3805 break;
3806 }
3807 if (chan == 11) {
3808 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3809 "allowed");
3810 return -1;
3811 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003812 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3813 "known)", params->freq);
3814 }
3815
3816 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3817 wpa_s->assoc_freq && !freq) {
3818 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3819 "already using");
3820 params->freq = wpa_s->assoc_freq;
3821 }
3822
3823 res = wpa_drv_shared_freq(wpa_s);
3824 if (res > 0 && !freq) {
3825 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3826 "already using on a shared interface");
3827 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003828 } else if (res > 0 && freq != res &&
3829 !(wpa_s->drv_flags &
3830 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3831 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3832 "while connected on another channel (%u MHz)",
3833 freq, res);
3834 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003836
3837 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838}
3839
3840
3841static struct wpa_supplicant *
3842wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3843 int go)
3844{
3845 struct wpa_supplicant *group_wpa_s;
3846
3847 if (!wpas_p2p_create_iface(wpa_s))
3848 return wpa_s;
3849
3850 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3851 WPA_IF_P2P_CLIENT) < 0)
3852 return NULL;
3853 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3854 if (group_wpa_s == NULL) {
3855 wpas_p2p_remove_pending_group_interface(wpa_s);
3856 return NULL;
3857 }
3858
3859 return group_wpa_s;
3860}
3861
3862
3863/**
3864 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3865 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3866 * @persistent_group: Whether to create a persistent group
3867 * @freq: Frequency for the group or 0 to indicate no hardcoding
3868 * Returns: 0 on success, -1 on failure
3869 *
3870 * This function creates a new P2P group with the local end as the Group Owner,
3871 * i.e., without using Group Owner Negotiation.
3872 */
3873int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003874 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003875{
3876 struct p2p_go_neg_results params;
3877 unsigned int r;
3878
3879 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3880 return -1;
3881
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003882 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003883 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003884 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003886 if (freq == 2) {
3887 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3888 "band");
3889 if (wpa_s->best_24_freq > 0 &&
3890 p2p_supported_freq(wpa_s->global->p2p,
3891 wpa_s->best_24_freq)) {
3892 freq = wpa_s->best_24_freq;
3893 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3894 "channel: %d MHz", freq);
3895 } else {
3896 os_get_random((u8 *) &r, sizeof(r));
3897 freq = 2412 + (r % 3) * 25;
3898 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3899 "channel: %d MHz", freq);
3900 }
3901 }
3902
3903 if (freq == 5) {
3904 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3905 "band");
3906 if (wpa_s->best_5_freq > 0 &&
3907 p2p_supported_freq(wpa_s->global->p2p,
3908 wpa_s->best_5_freq)) {
3909 freq = wpa_s->best_5_freq;
3910 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3911 "channel: %d MHz", freq);
3912 } else {
3913 os_get_random((u8 *) &r, sizeof(r));
3914 freq = 5180 + (r % 4) * 20;
3915 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3916 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3917 "5 GHz channel for P2P group");
3918 return -1;
3919 }
3920 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3921 "channel: %d MHz", freq);
3922 }
3923 }
3924
3925 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3926 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3927 "(%u MHz) is not supported for P2P uses",
3928 freq);
3929 return -1;
3930 }
3931
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003932 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003933 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003934 if (params.freq &&
3935 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3936 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3937 "(%u MHz) is not supported for P2P uses",
3938 params.freq);
3939 return -1;
3940 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 p2p_go_params(wpa_s->global->p2p, &params);
3942 params.persistent_group = persistent_group;
3943
3944 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3945 if (wpa_s == NULL)
3946 return -1;
3947 wpas_start_wps_go(wpa_s, &params, 0);
3948
3949 return 0;
3950}
3951
3952
3953static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3954 struct wpa_ssid *params, int addr_allocated)
3955{
3956 struct wpa_ssid *ssid;
3957
3958 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3959 if (wpa_s == NULL)
3960 return -1;
3961
3962 wpa_supplicant_ap_deinit(wpa_s);
3963
3964 ssid = wpa_config_add_network(wpa_s->conf);
3965 if (ssid == NULL)
3966 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967 wpa_config_set_network_defaults(ssid);
3968 ssid->temporary = 1;
3969 ssid->proto = WPA_PROTO_RSN;
3970 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3971 ssid->group_cipher = WPA_CIPHER_CCMP;
3972 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3973 ssid->ssid = os_malloc(params->ssid_len);
3974 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003975 wpa_config_remove_network(wpa_s->conf, ssid->id);
3976 return -1;
3977 }
3978 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3979 ssid->ssid_len = params->ssid_len;
3980 ssid->p2p_group = 1;
3981 ssid->export_keys = 1;
3982 if (params->psk_set) {
3983 os_memcpy(ssid->psk, params->psk, 32);
3984 ssid->psk_set = 1;
3985 }
3986 if (params->passphrase)
3987 ssid->passphrase = os_strdup(params->passphrase);
3988
3989 wpa_supplicant_select_network(wpa_s, ssid);
3990
3991 wpa_s->show_group_started = 1;
3992
3993 return 0;
3994}
3995
3996
3997int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3998 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003999 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000{
4001 struct p2p_go_neg_results params;
4002 int go = 0;
4003
4004 if (ssid->disabled != 2 || ssid->ssid == NULL)
4005 return -1;
4006
4007 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4008 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4009 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4010 "already running");
4011 return 0;
4012 }
4013
4014 /* Make sure we are not running find during connection establishment */
4015 wpas_p2p_stop_find(wpa_s);
4016
Dmitry Shmidt04949592012-07-19 12:16:46 -07004017 wpa_s->p2p_fallback_to_go_neg = 0;
4018
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004019 if (ssid->mode == WPAS_MODE_INFRA)
4020 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4021
4022 if (ssid->mode != WPAS_MODE_P2P_GO)
4023 return -1;
4024
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004025 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004026 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004027
4028 params.role_go = 1;
4029 if (ssid->passphrase == NULL ||
4030 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4031 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4032 "group");
4033 return -1;
4034 }
4035 os_strlcpy(params.passphrase, ssid->passphrase,
4036 sizeof(params.passphrase));
4037 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4038 params.ssid_len = ssid->ssid_len;
4039 params.persistent_group = 1;
4040
4041 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4042 if (wpa_s == NULL)
4043 return -1;
4044
4045 wpas_start_wps_go(wpa_s, &params, 0);
4046
4047 return 0;
4048}
4049
4050
4051static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4052 struct wpabuf *proberesp_ies)
4053{
4054 struct wpa_supplicant *wpa_s = ctx;
4055 if (wpa_s->ap_iface) {
4056 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004057 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4058 wpabuf_free(beacon_ies);
4059 wpabuf_free(proberesp_ies);
4060 return;
4061 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062 if (beacon_ies) {
4063 wpabuf_free(hapd->p2p_beacon_ie);
4064 hapd->p2p_beacon_ie = beacon_ies;
4065 }
4066 wpabuf_free(hapd->p2p_probe_resp_ie);
4067 hapd->p2p_probe_resp_ie = proberesp_ies;
4068 } else {
4069 wpabuf_free(beacon_ies);
4070 wpabuf_free(proberesp_ies);
4071 }
4072 wpa_supplicant_ap_update_beacon(wpa_s);
4073}
4074
4075
4076static void wpas_p2p_idle_update(void *ctx, int idle)
4077{
4078 struct wpa_supplicant *wpa_s = ctx;
4079 if (!wpa_s->ap_iface)
4080 return;
4081 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004082 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004083 wpas_p2p_set_group_idle_timeout(wpa_s);
4084 else
4085 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4086}
4087
4088
4089struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004090 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091{
4092 struct p2p_group *group;
4093 struct p2p_group_config *cfg;
4094
4095 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4096 return NULL;
4097 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4098 return NULL;
4099
4100 cfg = os_zalloc(sizeof(*cfg));
4101 if (cfg == NULL)
4102 return NULL;
4103
Dmitry Shmidt04949592012-07-19 12:16:46 -07004104 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004105 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004106 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004107 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004108 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4109 if (wpa_s->max_stations &&
4110 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4111 cfg->max_clients = wpa_s->max_stations;
4112 else
4113 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004114 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4115 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116 cfg->cb_ctx = wpa_s;
4117 cfg->ie_update = wpas_p2p_ie_update;
4118 cfg->idle_update = wpas_p2p_idle_update;
4119
4120 group = p2p_group_init(wpa_s->global->p2p, cfg);
4121 if (group == NULL)
4122 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004123 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124 p2p_group_notif_formation_done(group);
4125 wpa_s->p2p_group = group;
4126 return group;
4127}
4128
4129
4130void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4131 int registrar)
4132{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004133 struct wpa_ssid *ssid = wpa_s->current_ssid;
4134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135 if (!wpa_s->p2p_in_provisioning) {
4136 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4137 "provisioning not in progress");
4138 return;
4139 }
4140
Dmitry Shmidt04949592012-07-19 12:16:46 -07004141 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4142 u8 go_dev_addr[ETH_ALEN];
4143 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4144 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4145 ssid->ssid_len);
4146 /* Clear any stored provisioning info */
4147 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4148 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004150 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4151 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004152 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4153 /*
4154 * Use a separate timeout for initial data connection to
4155 * complete to allow the group to be removed automatically if
4156 * something goes wrong in this step before the P2P group idle
4157 * timeout mechanism is taken into use.
4158 */
4159 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4160 wpas_p2p_group_formation_timeout,
4161 wpa_s->parent, NULL);
4162 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004163 if (wpa_s->global->p2p)
4164 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4165 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4166 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4167 wpas_group_formation_completed(wpa_s, 1);
4168}
4169
4170
Jouni Malinen75ecf522011-06-27 15:19:46 -07004171void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4172 struct wps_event_fail *fail)
4173{
4174 if (!wpa_s->p2p_in_provisioning) {
4175 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4176 "provisioning not in progress");
4177 return;
4178 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004179
4180 if (wpa_s->go_params) {
4181 p2p_clear_provisioning_info(
4182 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004183 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004184 }
4185
Jouni Malinen75ecf522011-06-27 15:19:46 -07004186 wpas_notify_p2p_wps_failed(wpa_s, fail);
4187}
4188
4189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004190int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004191 const char *config_method,
4192 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004193{
4194 u16 config_methods;
4195
Dmitry Shmidt04949592012-07-19 12:16:46 -07004196 wpa_s->p2p_fallback_to_go_neg = 0;
4197 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004198 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004200 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004202 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4203 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004204 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004205 else {
4206 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004208 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209
Dmitry Shmidt04949592012-07-19 12:16:46 -07004210 if (use == WPAS_P2P_PD_AUTO) {
4211 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4212 wpa_s->pending_pd_config_methods = config_methods;
4213 wpa_s->p2p_auto_pd = 1;
4214 wpa_s->p2p_auto_join = 0;
4215 wpa_s->pending_pd_before_join = 0;
4216 wpa_s->auto_pd_scan_retry = 0;
4217 wpas_p2p_stop_find(wpa_s);
4218 wpa_s->p2p_join_scan_count = 0;
4219 os_get_time(&wpa_s->p2p_auto_started);
4220 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4221 wpa_s->p2p_auto_started.sec,
4222 wpa_s->p2p_auto_started.usec);
4223 wpas_p2p_join_scan(wpa_s, NULL);
4224 return 0;
4225 }
4226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4228 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004229 config_methods,
4230 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004231 }
4232
4233 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4234 return -1;
4235
4236 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004237 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4238 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004239}
4240
4241
4242int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4243 char *end)
4244{
4245 return p2p_scan_result_text(ies, ies_len, buf, end);
4246}
4247
4248
4249static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4250{
4251 if (!wpa_s->pending_action_tx)
4252 return;
4253
4254 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4255 "operation request");
4256 wpabuf_free(wpa_s->pending_action_tx);
4257 wpa_s->pending_action_tx = NULL;
4258}
4259
4260
4261int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4262 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004263 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004264 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004265{
4266 wpas_p2p_clear_pending_action_tx(wpa_s);
4267 wpa_s->p2p_long_listen = 0;
4268
4269 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4270 return wpa_drv_p2p_find(wpa_s, timeout, type);
4271
Dmitry Shmidt04949592012-07-19 12:16:46 -07004272 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4273 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004274 return -1;
4275
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004276 wpa_supplicant_cancel_sched_scan(wpa_s);
4277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004278 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004279 num_req_dev_types, req_dev_types, dev_id,
4280 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004281}
4282
4283
4284void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4285{
4286 wpas_p2p_clear_pending_action_tx(wpa_s);
4287 wpa_s->p2p_long_listen = 0;
4288 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4289 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004290 wpa_s->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004291
4292 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4293 wpa_drv_p2p_stop_find(wpa_s);
4294 return;
4295 }
4296
4297 if (wpa_s->global->p2p)
4298 p2p_stop_find(wpa_s->global->p2p);
4299
4300 wpas_p2p_remove_pending_group_interface(wpa_s);
4301}
4302
4303
4304static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4305{
4306 struct wpa_supplicant *wpa_s = eloop_ctx;
4307 wpa_s->p2p_long_listen = 0;
4308}
4309
4310
4311int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4312{
4313 int res;
4314
4315 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4316 return -1;
4317
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004318 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004319 wpas_p2p_clear_pending_action_tx(wpa_s);
4320
4321 if (timeout == 0) {
4322 /*
4323 * This is a request for unlimited Listen state. However, at
4324 * least for now, this is mapped to a Listen state for one
4325 * hour.
4326 */
4327 timeout = 3600;
4328 }
4329 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4330 wpa_s->p2p_long_listen = 0;
4331
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004332 /*
4333 * Stop previous find/listen operation to avoid trying to request a new
4334 * remain-on-channel operation while the driver is still running the
4335 * previous one.
4336 */
4337 if (wpa_s->global->p2p)
4338 p2p_stop_find(wpa_s->global->p2p);
4339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4341 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4342 wpa_s->p2p_long_listen = timeout * 1000;
4343 eloop_register_timeout(timeout, 0,
4344 wpas_p2p_long_listen_timeout,
4345 wpa_s, NULL);
4346 }
4347
4348 return res;
4349}
4350
4351
4352int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4353 u8 *buf, size_t len, int p2p_group)
4354{
4355 struct wpabuf *p2p_ie;
4356 int ret;
4357
4358 if (wpa_s->global->p2p_disabled)
4359 return -1;
4360 if (wpa_s->global->p2p == NULL)
4361 return -1;
4362 if (bss == NULL)
4363 return -1;
4364
4365 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4366 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4367 p2p_group, p2p_ie);
4368 wpabuf_free(p2p_ie);
4369
4370 return ret;
4371}
4372
4373
4374int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004375 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004376 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004377{
4378 if (wpa_s->global->p2p_disabled)
4379 return 0;
4380 if (wpa_s->global->p2p == NULL)
4381 return 0;
4382
Dmitry Shmidt04949592012-07-19 12:16:46 -07004383 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4384 ie, ie_len)) {
4385 case P2P_PREQ_NOT_P2P:
4386 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4387 ssi_signal);
4388 /* fall through */
4389 case P2P_PREQ_MALFORMED:
4390 case P2P_PREQ_NOT_LISTEN:
4391 case P2P_PREQ_NOT_PROCESSED:
4392 default: /* make gcc happy */
4393 return 0;
4394 case P2P_PREQ_PROCESSED:
4395 return 1;
4396 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004397}
4398
4399
4400void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4401 const u8 *sa, const u8 *bssid,
4402 u8 category, const u8 *data, size_t len, int freq)
4403{
4404 if (wpa_s->global->p2p_disabled)
4405 return;
4406 if (wpa_s->global->p2p == NULL)
4407 return;
4408
4409 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4410 freq);
4411}
4412
4413
4414void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4415{
4416 if (wpa_s->global->p2p_disabled)
4417 return;
4418 if (wpa_s->global->p2p == NULL)
4419 return;
4420
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004421 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004422}
4423
4424
4425void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4426{
4427 p2p_group_deinit(wpa_s->p2p_group);
4428 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004429
4430 wpa_s->ap_configured_cb = NULL;
4431 wpa_s->ap_configured_cb_ctx = NULL;
4432 wpa_s->ap_configured_cb_data = NULL;
4433 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004434}
4435
4436
4437int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4438{
4439 wpa_s->p2p_long_listen = 0;
4440
4441 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4442 return wpa_drv_p2p_reject(wpa_s, addr);
4443
4444 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4445 return -1;
4446
4447 return p2p_reject(wpa_s->global->p2p, addr);
4448}
4449
4450
4451/* Invite to reinvoke a persistent group */
4452int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004453 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4454 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004455{
4456 enum p2p_invite_role role;
4457 u8 *bssid = NULL;
4458
Jouni Malinen31be0a42012-08-31 21:20:51 +03004459 wpa_s->p2p_persistent_go_freq = freq;
4460 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004461 if (ssid->mode == WPAS_MODE_P2P_GO) {
4462 role = P2P_INVITE_ROLE_GO;
4463 if (peer_addr == NULL) {
4464 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4465 "address in invitation command");
4466 return -1;
4467 }
4468 if (wpas_p2p_create_iface(wpa_s)) {
4469 if (wpas_p2p_add_group_interface(wpa_s,
4470 WPA_IF_P2P_GO) < 0) {
4471 wpa_printf(MSG_ERROR, "P2P: Failed to "
4472 "allocate a new interface for the "
4473 "group");
4474 return -1;
4475 }
4476 bssid = wpa_s->pending_interface_addr;
4477 } else
4478 bssid = wpa_s->own_addr;
4479 } else {
4480 role = P2P_INVITE_ROLE_CLIENT;
4481 peer_addr = ssid->bssid;
4482 }
4483 wpa_s->pending_invite_ssid_id = ssid->id;
4484
4485 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4486 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4487 ssid->ssid, ssid->ssid_len,
4488 go_dev_addr, 1);
4489
4490 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4491 return -1;
4492
4493 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004494 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004495}
4496
4497
4498/* Invite to join an active group */
4499int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4500 const u8 *peer_addr, const u8 *go_dev_addr)
4501{
4502 struct wpa_global *global = wpa_s->global;
4503 enum p2p_invite_role role;
4504 u8 *bssid = NULL;
4505 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004506 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004507
Jouni Malinen31be0a42012-08-31 21:20:51 +03004508 wpa_s->p2p_persistent_go_freq = 0;
4509 wpa_s->p2p_go_ht40 = 0;
4510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004511 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4512 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4513 break;
4514 }
4515 if (wpa_s == NULL) {
4516 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4517 return -1;
4518 }
4519
4520 ssid = wpa_s->current_ssid;
4521 if (ssid == NULL) {
4522 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4523 "invitation");
4524 return -1;
4525 }
4526
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004527 persistent = ssid->p2p_persistent_group &&
4528 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4529 ssid->ssid, ssid->ssid_len);
4530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531 if (ssid->mode == WPAS_MODE_P2P_GO) {
4532 role = P2P_INVITE_ROLE_ACTIVE_GO;
4533 bssid = wpa_s->own_addr;
4534 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004535 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004536 } else {
4537 role = P2P_INVITE_ROLE_CLIENT;
4538 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4539 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4540 "invite to current group");
4541 return -1;
4542 }
4543 bssid = wpa_s->bssid;
4544 if (go_dev_addr == NULL &&
4545 !is_zero_ether_addr(wpa_s->go_dev_addr))
4546 go_dev_addr = wpa_s->go_dev_addr;
4547 }
4548 wpa_s->parent->pending_invite_ssid_id = -1;
4549
4550 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4551 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4552 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004553 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004554
4555 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4556 return -1;
4557
4558 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4559 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004560 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004561}
4562
4563
4564void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4565{
4566 struct wpa_ssid *ssid = wpa_s->current_ssid;
4567 const char *ssid_txt;
4568 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004569 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004571 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572
Dmitry Shmidt04949592012-07-19 12:16:46 -07004573 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4574 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4575 wpa_s->parent, NULL);
4576 }
4577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004578 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004579 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580
4581 wpa_s->show_group_started = 0;
4582
4583 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4584 os_memset(go_dev_addr, 0, ETH_ALEN);
4585 if (ssid->bssid_set)
4586 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4587 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4588 ssid->ssid_len);
4589 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4590
4591 if (wpa_s->global->p2p_group_formation == wpa_s)
4592 wpa_s->global->p2p_group_formation = NULL;
4593
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004594 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4595 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004596 if (ssid->passphrase == NULL && ssid->psk_set) {
4597 char psk[65];
4598 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4599 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4600 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4601 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004602 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004603 MAC2STR(go_dev_addr),
4604 persistent ? " [PERSISTENT]" : "");
4605 } else {
4606 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4607 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4608 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004609 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004610 ssid->passphrase ? ssid->passphrase : "",
4611 MAC2STR(go_dev_addr),
4612 persistent ? " [PERSISTENT]" : "");
4613 }
4614
4615 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004616 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4617 ssid, go_dev_addr);
4618 if (network_id < 0)
4619 network_id = ssid->id;
4620 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004621
4622done:
4623 if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4624 wpa_s->global->p2p != NULL) {
4625 wpa_s->p2p_cb_on_scan_complete = 0;
4626 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4627 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4628 "continued after successful connection");
4629 p2p_increase_search_delay(
4630 wpa_s->global->p2p,
4631 wpas_p2p_search_delay(wpa_s));
4632 }
4633 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004634}
4635
4636
4637int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4638 u32 interval1, u32 duration2, u32 interval2)
4639{
4640 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4641 return -1;
4642 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4643 return -1;
4644
4645 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4646 wpa_s->current_ssid == NULL ||
4647 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4648 return -1;
4649
4650 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4651 wpa_s->own_addr, wpa_s->assoc_freq,
4652 duration1, interval1, duration2, interval2);
4653}
4654
4655
4656int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4657 unsigned int interval)
4658{
4659 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4660 return -1;
4661
4662 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4663 return -1;
4664
4665 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4666}
4667
4668
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004669static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4670{
4671 return wpa_s->current_ssid != NULL &&
4672 wpa_s->current_ssid->p2p_group &&
4673 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4674}
4675
4676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004677static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4678{
4679 struct wpa_supplicant *wpa_s = eloop_ctx;
4680
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004681 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4683 "disabled");
4684 return;
4685 }
4686
Dmitry Shmidt04949592012-07-19 12:16:46 -07004687 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4688 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004689 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690}
4691
4692
4693static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4694{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004695 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004696
Dmitry Shmidt04949592012-07-19 12:16:46 -07004697 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4698 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004700 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4701 return;
4702
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004703 timeout = wpa_s->conf->p2p_group_idle;
4704 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4705 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4706 timeout = P2P_MAX_CLIENT_IDLE;
4707
4708 if (timeout == 0)
4709 return;
4710
Dmitry Shmidt04949592012-07-19 12:16:46 -07004711 if (timeout < 0) {
4712 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4713 timeout = 0; /* special client mode no-timeout */
4714 else
4715 return;
4716 }
4717
4718 if (wpa_s->p2p_in_provisioning) {
4719 /*
4720 * Use the normal group formation timeout during the
4721 * provisioning phase to avoid terminating this process too
4722 * early due to group idle timeout.
4723 */
4724 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4725 "during provisioning");
4726 return;
4727 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004728#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07004729 if (wpa_s->show_group_started) {
4730 /*
4731 * Use the normal group formation timeout between the end of
4732 * the provisioning phase and completion of 4-way handshake to
4733 * avoid terminating this process too early due to group idle
4734 * timeout.
4735 */
4736 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4737 "while waiting for initial 4-way handshake to "
4738 "complete");
4739 return;
4740 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004741#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07004742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004744 timeout);
4745 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4746 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004747}
4748
4749
Jouni Malinen2b89da82012-08-31 22:04:41 +03004750/* Returns 1 if the interface was removed */
4751int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4752 u16 reason_code, const u8 *ie, size_t ie_len,
4753 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004754{
4755 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004756 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004757 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004758 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759
Dmitry Shmidt04949592012-07-19 12:16:46 -07004760 if (!locally_generated)
4761 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4762 ie_len);
4763
4764 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4765 wpa_s->current_ssid &&
4766 wpa_s->current_ssid->p2p_group &&
4767 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4768 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4769 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03004770 if (wpas_p2p_group_delete(wpa_s,
4771 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4772 > 0)
4773 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004774 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03004775
4776 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004777}
4778
4779
4780void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004781 u16 reason_code, const u8 *ie, size_t ie_len,
4782 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004783{
4784 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4785 return;
4786 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4787 return;
4788
Dmitry Shmidt04949592012-07-19 12:16:46 -07004789 if (!locally_generated)
4790 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4791 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004792}
4793
4794
4795void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4796{
4797 struct p2p_data *p2p = wpa_s->global->p2p;
4798
4799 if (p2p == NULL)
4800 return;
4801
4802 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4803 return;
4804
4805 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4806 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4807
4808 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4809 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4810
4811 if (wpa_s->wps &&
4812 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4813 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4814
4815 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4816 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4817
4818 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4819 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4820 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4821 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4822 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4823 }
4824
4825 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4826 p2p_set_sec_dev_types(p2p,
4827 (void *) wpa_s->conf->sec_device_type,
4828 wpa_s->conf->num_sec_device_types);
4829
4830 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4831 int i;
4832 p2p_remove_wps_vendor_extensions(p2p);
4833 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4834 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4835 continue;
4836 p2p_add_wps_vendor_extension(
4837 p2p, wpa_s->conf->wps_vendor_ext[i]);
4838 }
4839 }
4840
4841 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4842 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4843 char country[3];
4844 country[0] = wpa_s->conf->country[0];
4845 country[1] = wpa_s->conf->country[1];
4846 country[2] = 0x04;
4847 p2p_set_country(p2p, country);
4848 }
4849
4850 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4851 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4852 wpa_s->conf->p2p_ssid_postfix ?
4853 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4854 0);
4855 }
4856
4857 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4858 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004859
4860 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4861 u8 reg_class, channel;
4862 int ret;
4863 unsigned int r;
4864 if (wpa_s->conf->p2p_listen_reg_class &&
4865 wpa_s->conf->p2p_listen_channel) {
4866 reg_class = wpa_s->conf->p2p_listen_reg_class;
4867 channel = wpa_s->conf->p2p_listen_channel;
4868 } else {
4869 reg_class = 81;
4870 /*
4871 * Pick one of the social channels randomly as the
4872 * listen channel.
4873 */
4874 os_get_random((u8 *) &r, sizeof(r));
4875 channel = 1 + (r % 3) * 5;
4876 }
4877 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4878 if (ret)
4879 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4880 "failed: %d", ret);
4881 }
4882 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4883 u8 op_reg_class, op_channel, cfg_op_channel;
4884 int ret = 0;
4885 unsigned int r;
4886 if (wpa_s->conf->p2p_oper_reg_class &&
4887 wpa_s->conf->p2p_oper_channel) {
4888 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4889 op_channel = wpa_s->conf->p2p_oper_channel;
4890 cfg_op_channel = 1;
4891 } else {
4892 op_reg_class = 81;
4893 /*
4894 * Use random operation channel from (1, 6, 11)
4895 *if no other preference is indicated.
4896 */
4897 os_get_random((u8 *) &r, sizeof(r));
4898 op_channel = 1 + (r % 3) * 5;
4899 cfg_op_channel = 0;
4900 }
4901 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4902 cfg_op_channel);
4903 if (ret)
4904 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4905 "failed: %d", ret);
4906 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004907
4908 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4909 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4910 wpa_s->conf->p2p_pref_chan) < 0) {
4911 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4912 "update failed");
4913 }
4914 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004915}
4916
4917
4918int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4919 int duration)
4920{
4921 if (!wpa_s->ap_iface)
4922 return -1;
4923 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4924 duration);
4925}
4926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004927
4928int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4929{
4930 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4931 return -1;
4932 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4933 return -1;
4934
4935 wpa_s->global->cross_connection = enabled;
4936 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4937
4938 if (!enabled) {
4939 struct wpa_supplicant *iface;
4940
4941 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4942 {
4943 if (iface->cross_connect_enabled == 0)
4944 continue;
4945
4946 iface->cross_connect_enabled = 0;
4947 iface->cross_connect_in_use = 0;
4948 wpa_msg(iface->parent, MSG_INFO,
4949 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4950 iface->ifname, iface->cross_connect_uplink);
4951 }
4952 }
4953
4954 return 0;
4955}
4956
4957
4958static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4959{
4960 struct wpa_supplicant *iface;
4961
4962 if (!uplink->global->cross_connection)
4963 return;
4964
4965 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4966 if (!iface->cross_connect_enabled)
4967 continue;
4968 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4969 0)
4970 continue;
4971 if (iface->ap_iface == NULL)
4972 continue;
4973 if (iface->cross_connect_in_use)
4974 continue;
4975
4976 iface->cross_connect_in_use = 1;
4977 wpa_msg(iface->parent, MSG_INFO,
4978 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4979 iface->ifname, iface->cross_connect_uplink);
4980 }
4981}
4982
4983
4984static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4985{
4986 struct wpa_supplicant *iface;
4987
4988 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4989 if (!iface->cross_connect_enabled)
4990 continue;
4991 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4992 0)
4993 continue;
4994 if (!iface->cross_connect_in_use)
4995 continue;
4996
4997 wpa_msg(iface->parent, MSG_INFO,
4998 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4999 iface->ifname, iface->cross_connect_uplink);
5000 iface->cross_connect_in_use = 0;
5001 }
5002}
5003
5004
5005void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5006{
5007 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5008 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5009 wpa_s->cross_connect_disallowed)
5010 wpas_p2p_disable_cross_connect(wpa_s);
5011 else
5012 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005013 if (!wpa_s->ap_iface &&
5014 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5015 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005016}
5017
5018
5019void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5020{
5021 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005022 if (!wpa_s->ap_iface &&
5023 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5024 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025 wpas_p2p_set_group_idle_timeout(wpa_s);
5026}
5027
5028
5029static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5030{
5031 struct wpa_supplicant *iface;
5032
5033 if (!wpa_s->global->cross_connection)
5034 return;
5035
5036 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5037 if (iface == wpa_s)
5038 continue;
5039 if (iface->drv_flags &
5040 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5041 continue;
5042 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5043 continue;
5044
5045 wpa_s->cross_connect_enabled = 1;
5046 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5047 sizeof(wpa_s->cross_connect_uplink));
5048 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5049 "%s to %s whenever uplink is available",
5050 wpa_s->ifname, wpa_s->cross_connect_uplink);
5051
5052 if (iface->ap_iface || iface->current_ssid == NULL ||
5053 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5054 iface->cross_connect_disallowed ||
5055 iface->wpa_state != WPA_COMPLETED)
5056 break;
5057
5058 wpa_s->cross_connect_in_use = 1;
5059 wpa_msg(wpa_s->parent, MSG_INFO,
5060 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5061 wpa_s->ifname, wpa_s->cross_connect_uplink);
5062 break;
5063 }
5064}
5065
5066
5067int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5068{
5069 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5070 !wpa_s->p2p_in_provisioning)
5071 return 0; /* not P2P client operation */
5072
5073 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5074 "session overlap");
5075 if (wpa_s != wpa_s->parent)
5076 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5077
5078 if (wpa_s->global->p2p)
5079 p2p_group_formation_failed(wpa_s->global->p2p);
5080
5081 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5082 wpa_s->parent, NULL);
5083
5084 wpas_group_formation_completed(wpa_s, 0);
5085 return 1;
5086}
5087
5088
5089void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5090{
5091 struct p2p_channels chan;
5092
5093 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5094 return;
5095
5096 os_memset(&chan, 0, sizeof(chan));
5097 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5098 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5099 "channel list");
5100 return;
5101 }
5102
5103 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5104}
5105
5106
5107int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5108{
5109 struct wpa_global *global = wpa_s->global;
5110 int found = 0;
5111 const u8 *peer;
5112
5113 if (global->p2p == NULL)
5114 return -1;
5115
5116 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5117
5118 if (wpa_s->pending_interface_name[0] &&
5119 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5120 found = 1;
5121
5122 peer = p2p_get_go_neg_peer(global->p2p);
5123 if (peer) {
5124 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5125 MACSTR, MAC2STR(peer));
5126 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005127 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005128 }
5129
5130 wpas_p2p_stop_find(wpa_s);
5131
5132 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5133 if (wpa_s == global->p2p_group_formation &&
5134 (wpa_s->p2p_in_provisioning ||
5135 wpa_s->parent->pending_interface_type ==
5136 WPA_IF_P2P_CLIENT)) {
5137 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5138 "formation found - cancelling",
5139 wpa_s->ifname);
5140 found = 1;
5141 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5142 wpa_s->parent, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005143 wpas_p2p_group_delete(wpa_s,
5144 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145 break;
5146 }
5147 }
5148
5149 if (!found) {
5150 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5151 return -1;
5152 }
5153
5154 return 0;
5155}
5156
5157
5158void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5159{
5160 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5161 return;
5162
5163 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5164 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005165 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005166}
5167
5168
5169void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5170 int freq_24, int freq_5, int freq_overall)
5171{
5172 struct p2p_data *p2p = wpa_s->global->p2p;
5173 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5174 return;
5175 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5176}
5177
5178
5179int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5180{
5181 u8 peer[ETH_ALEN];
5182 struct p2p_data *p2p = wpa_s->global->p2p;
5183
5184 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5185 return -1;
5186
5187 if (hwaddr_aton(addr, peer))
5188 return -1;
5189
5190 return p2p_unauthorize(p2p, peer);
5191}
5192
5193
5194/**
5195 * wpas_p2p_disconnect - Disconnect from a P2P Group
5196 * @wpa_s: Pointer to wpa_supplicant data
5197 * Returns: 0 on success, -1 on failure
5198 *
5199 * This can be used to disconnect from a group in which the local end is a P2P
5200 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5201 * virtual network interface was created for this group, that interface will be
5202 * removed. Otherwise, only the configured P2P group network will be removed
5203 * from the interface.
5204 */
5205int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5206{
5207
5208 if (wpa_s == NULL)
5209 return -1;
5210
Jouni Malinen2b89da82012-08-31 22:04:41 +03005211 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5212 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005213}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005214
5215
5216int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5217{
5218 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5219 return 0;
5220
5221 return p2p_in_progress(wpa_s->global->p2p);
5222}
5223
5224
5225void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5226 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005227{
5228 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5229 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5230 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005231 /**
5232 * Remove the network by scheduling the group formation
5233 * timeout to happen immediately. The teardown code
5234 * needs to be scheduled to run asynch later so that we
5235 * don't delete data from under ourselves unexpectedly.
5236 * Calling wpas_p2p_group_formation_timeout directly
5237 * causes a series of crashes in WPS failure scenarios.
5238 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005239 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5240 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005241 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5242 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005243 }
5244}
5245
5246
5247struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005248 const u8 *addr, const u8 *ssid,
5249 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005250{
5251 struct wpa_ssid *s;
5252 size_t i;
5253
5254 for (s = wpa_s->conf->ssid; s; s = s->next) {
5255 if (s->disabled != 2)
5256 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005257 if (ssid &&
5258 (ssid_len != s->ssid_len ||
5259 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5260 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005261 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5262 return s; /* peer is GO in the persistent group */
5263 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5264 continue;
5265 for (i = 0; i < s->num_p2p_clients; i++) {
5266 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5267 addr, ETH_ALEN) == 0)
5268 return s; /* peer is P2P client in persistent
5269 * group */
5270 }
5271 }
5272
5273 return NULL;
5274}
5275
5276
5277void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5278 const u8 *addr)
5279{
5280 if (addr == NULL)
5281 return;
5282 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5283}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005284
Dmitry Shmidt04949592012-07-19 12:16:46 -07005285
5286static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5287 int group_added)
5288{
5289 struct wpa_supplicant *group = wpa_s;
5290 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5291 if (wpa_s->global->p2p_group_formation)
5292 group = wpa_s->global->p2p_group_formation;
5293 wpa_s = wpa_s->parent;
5294 offchannel_send_action_done(wpa_s);
5295 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005296 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005297 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5298 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5299 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5300 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5301 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005302 wpa_s->p2p_pd_before_go_neg,
5303 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005304}
5305
5306
5307int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5308{
5309 if (!wpa_s->p2p_fallback_to_go_neg ||
5310 wpa_s->p2p_in_provisioning <= 5)
5311 return 0;
5312
5313 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5314 return 0; /* peer operating as a GO */
5315
5316 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5317 "fallback to GO Negotiation");
5318 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5319
5320 return 1;
5321}
5322
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005323
5324unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5325{
5326 const char *rn, *rn2;
5327 struct wpa_supplicant *ifs;
5328
5329 if (wpa_s->wpa_state > WPA_SCANNING) {
5330 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5331 "concurrent operation",
5332 P2P_CONCURRENT_SEARCH_DELAY);
5333 return P2P_CONCURRENT_SEARCH_DELAY;
5334 }
5335
5336 if (!wpa_s->driver->get_radio_name)
5337 return 0;
5338 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5339 if (rn == NULL || rn[0] == '\0')
5340 return 0;
5341
5342 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5343 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5344 continue;
5345
5346 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5347 if (!rn2 || os_strcmp(rn, rn2) != 0)
5348 continue;
5349 if (ifs->wpa_state > WPA_SCANNING) {
5350 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5351 "delay due to concurrent operation on "
5352 "interface %s",
5353 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5354 return P2P_CONCURRENT_SEARCH_DELAY;
5355 }
5356 }
5357
5358 return 0;
5359}
5360
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005361#ifdef ANDROID_P2P
5362int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
5363{
5364 struct wpa_supplicant *iface = NULL;
5365 struct p2p_data *p2p = wpa_s->global->p2p;
5366
5367 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5368 if((iface->p2p_group_interface) && (iface->current_ssid) &&
5369 (iface->current_ssid->frequency != freq)) {
5370
5371 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
5372 /* Try to see whether we can move the GO. If it
5373 * is not possible, remove the GO interface
5374 */
5375 if(wpa_drv_switch_channel(iface, freq) == 0) {
5376 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5377 iface->current_ssid->frequency = freq;
5378 continue;
5379 }
5380 }
5381
5382 /* If GO cannot be moved or if the conflicting interface is a
5383 * P2P Client, remove the interface depending up on the connection
5384 * priority */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005385 if(!wpas_is_p2p_prioritized(wpa_s)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005386 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005387 * P2P connection. So remove the interface */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005388 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5389 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005390 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005391 } else {
5392 /* Existing connection has the priority. Disable the newly
5393 * selected network and let the application know about it.
5394 */
5395 return -1;
5396 }
5397 }
5398 }
5399 return 0;
5400}
5401#endif