blob: 73d082e0b60f09b248726852881e9e7a499590c3 [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
65#ifdef ANDROID_P2P
66#define P2P_CONCURRENT_SEARCH_DELAY 0
67#else
68#define P2P_CONCURRENT_SEARCH_DELAY 500
69#endif
70#endif /* P2P_CONCURRENT_SEARCH_DELAY */
71
72enum p2p_group_removal_reason {
73 P2P_GROUP_REMOVAL_UNKNOWN,
74 P2P_GROUP_REMOVAL_SILENT,
75 P2P_GROUP_REMOVAL_FORMATION_FAILED,
76 P2P_GROUP_REMOVAL_REQUESTED,
77 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
78 P2P_GROUP_REMOVAL_UNAVAILABLE,
79 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
80#ifdef ANDROID_P2P
81 P2P_GROUP_REMOVAL_FREQ_CONFLICT
82#endif
83};
84
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070085#ifdef ANDROID_P2P
86static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s);
87#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
89static struct wpa_supplicant *
90wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
91 int go);
92static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070093static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
95static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070096 const u8 *dev_addr, enum p2p_wps_method wps_method,
97 int auto_join);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080098static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
99 void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
101static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
102static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
103static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700104static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
105 int group_added);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -0700107#ifdef ANDROID_P2P
108static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s)
109{
110 struct wpa_supplicant *iface = NULL;
111
112 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
113 if(iface->scanning || iface->wpa_state == WPA_SCANNING) {
114 wpa_printf(MSG_DEBUG, "P2P: Scan in progress on %s,"
115 "defer P2P SEARCH", iface->ifname);
116 return 1;
117 }
118 }
119
120 return 0;
121}
122#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123
124static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
125 struct wpa_scan_results *scan_res)
126{
127 size_t i;
128
129 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
130 return;
131
132 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
133 (int) scan_res->num);
134
135 for (i = 0; i < scan_res->num; i++) {
136 struct wpa_scan_res *bss = scan_res->res[i];
137 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
138 bss->freq, bss->level,
139 (const u8 *) (bss + 1),
140 bss->ie_len) > 0)
141 break;
142 }
143
144 p2p_scan_res_handled(wpa_s->global->p2p);
145}
146
147
148static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
149 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700150 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151{
152 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700153 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154 struct wpa_driver_scan_params params;
155 int ret;
156 struct wpabuf *wps_ie, *ies;
157 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800158 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159
160 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
161 return -1;
162
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700163 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
164 if (ifs->sta_scan_pending &&
165 wpas_p2p_in_progress(wpa_s) == 2) {
166 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
167 "pending station mode scan to be "
168 "completed on interface %s", ifs->ifname);
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700169#ifdef ANDROID_P2P
170 ifs->p2p_cb_on_scan_complete = 1;
171#else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700172 wpa_s->p2p_cb_on_scan_complete = 1;
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700173#endif
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700174 wpa_supplicant_req_scan(ifs, 0, 0);
175 return 1;
176 }
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700177#ifdef ANDROID_P2P
178 else if(ifs->scanning) {
179 wpa_printf(MSG_DEBUG, "Wait for the STA scan"
180 "to be "
181 "completed on interface %s", ifs->ifname);
182 ifs->p2p_cb_on_scan_complete = 1;
183 return 1;
184 }
185#endif
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700186 }
187
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188 os_memset(&params, 0, sizeof(params));
189
190 /* P2P Wildcard SSID */
191 params.num_ssids = 1;
192 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
193 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
194
195 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700196 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
197 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 num_req_dev_types, req_dev_types);
199 if (wps_ie == NULL)
200 return -1;
201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800202 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
203 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204 if (ies == NULL) {
205 wpabuf_free(wps_ie);
206 return -1;
207 }
208 wpabuf_put_buf(ies, wps_ie);
209 wpabuf_free(wps_ie);
210
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800211 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800213 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214 params.extra_ies = wpabuf_head(ies);
215 params.extra_ies_len = wpabuf_len(ies);
216
217 switch (type) {
218 case P2P_SCAN_SOCIAL:
219 params.freqs = social_channels;
220 break;
221 case P2P_SCAN_FULL:
222 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700223 case P2P_SCAN_SPECIFIC:
224 social_channels[0] = freq;
225 social_channels[1] = 0;
226 params.freqs = social_channels;
227 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 case P2P_SCAN_SOCIAL_PLUS_ONE:
229 social_channels[3] = freq;
230 params.freqs = social_channels;
231 break;
232 }
233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800234 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235
236 wpabuf_free(ies);
237
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238 if (ret) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700239 if (wpa_s->scanning ||
240 wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241 wpa_s->p2p_cb_on_scan_complete = 1;
242 ret = 1;
243 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700244 } else
245 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247 return ret;
248}
249
250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700251static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
252{
253 switch (p2p_group_interface) {
254 case P2P_GROUP_INTERFACE_PENDING:
255 return WPA_IF_P2P_GROUP;
256 case P2P_GROUP_INTERFACE_GO:
257 return WPA_IF_P2P_GO;
258 case P2P_GROUP_INTERFACE_CLIENT:
259 return WPA_IF_P2P_CLIENT;
260 }
261
262 return WPA_IF_P2P_GROUP;
263}
264
265
266static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
267 const u8 *ssid,
268 size_t ssid_len, int *go)
269{
270 struct wpa_ssid *s;
271
272 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
273 for (s = wpa_s->conf->ssid; s; s = s->next) {
274 if (s->disabled != 0 || !s->p2p_group ||
275 s->ssid_len != ssid_len ||
276 os_memcmp(ssid, s->ssid, ssid_len) != 0)
277 continue;
278 if (s->mode == WPAS_MODE_P2P_GO &&
279 s != wpa_s->current_ssid)
280 continue;
281 if (go)
282 *go = s->mode == WPAS_MODE_P2P_GO;
283 return wpa_s;
284 }
285 }
286
287 return NULL;
288}
289
290
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700291static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
292 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293{
294 struct wpa_ssid *ssid;
295 char *gtype;
296 const char *reason;
297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298 ssid = wpa_s->current_ssid;
299 if (ssid == NULL) {
300 /*
301 * The current SSID was not known, but there may still be a
302 * pending P2P group interface waiting for provisioning.
303 */
304 ssid = wpa_s->conf->ssid;
305 while (ssid) {
306 if (ssid->p2p_group &&
307 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
308 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
309 break;
310 ssid = ssid->next;
311 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300312 if (ssid == NULL &&
313 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
314 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700315 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
316 "not found");
317 return -1;
318 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 }
320 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
321 gtype = "GO";
322 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
323 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
324 wpa_s->reassociate = 0;
325 wpa_s->disconnected = 1;
326 wpa_supplicant_deauthenticate(wpa_s,
327 WLAN_REASON_DEAUTH_LEAVING);
328 gtype = "client";
329 } else
330 gtype = "GO";
331 if (wpa_s->cross_connect_in_use) {
332 wpa_s->cross_connect_in_use = 0;
333 wpa_msg(wpa_s->parent, MSG_INFO,
334 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
335 wpa_s->ifname, wpa_s->cross_connect_uplink);
336 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700337 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 case P2P_GROUP_REMOVAL_REQUESTED:
339 reason = " reason=REQUESTED";
340 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700341 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
342 reason = " reason=FORMATION_FAILED";
343 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
345 reason = " reason=IDLE";
346 break;
347 case P2P_GROUP_REMOVAL_UNAVAILABLE:
348 reason = " reason=UNAVAILABLE";
349 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700350 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
351 reason = " reason=GO_ENDING_SESSION";
352 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700353#ifdef ANDROID_P2P
354 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
355 reason = " reason=FREQ_CONFLICT";
356 break;
357#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358 default:
359 reason = "";
360 break;
361 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700362 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700363 wpa_msg(wpa_s->parent, MSG_INFO,
364 P2P_EVENT_GROUP_REMOVED "%s %s%s",
365 wpa_s->ifname, gtype, reason);
366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367
Dmitry Shmidt04949592012-07-19 12:16:46 -0700368 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
369 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
370
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700371 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
373
374 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
375 struct wpa_global *global;
376 char *ifname;
377 enum wpa_driver_if_type type;
378 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
379 wpa_s->ifname);
380 global = wpa_s->global;
381 ifname = os_strdup(wpa_s->ifname);
382 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700383 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 wpa_s = global->ifaces;
385 if (wpa_s && ifname)
386 wpa_drv_if_remove(wpa_s, type, ifname);
387 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300388 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 }
390
391 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
392 if (ssid && (ssid->p2p_group ||
393 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
394 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
395 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700396 if (ssid == wpa_s->current_ssid) {
397 wpa_sm_set_config(wpa_s->wpa, NULL);
398 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700400 }
401 /*
402 * Networks objects created during any P2P activities are not
403 * exposed out as they might/will confuse certain non-P2P aware
404 * applications since these network objects won't behave like
405 * regular ones.
406 *
407 * Likewise, we don't send out network removed signals for such
408 * network objects.
409 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410 wpa_config_remove_network(wpa_s->conf, id);
411 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800412 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413 } else {
414 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
415 "found");
416 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700417 if (wpa_s->ap_iface)
418 wpa_supplicant_ap_deinit(wpa_s);
419 else
420 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700421
422 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423}
424
425
426static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
427 u8 *go_dev_addr,
428 const u8 *ssid, size_t ssid_len)
429{
430 struct wpa_bss *bss;
431 const u8 *bssid;
432 struct wpabuf *p2p;
433 u8 group_capab;
434 const u8 *addr;
435
436 if (wpa_s->go_params)
437 bssid = wpa_s->go_params->peer_interface_addr;
438 else
439 bssid = wpa_s->bssid;
440
441 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
442 if (bss == NULL) {
443 u8 iface_addr[ETH_ALEN];
444 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
445 iface_addr) == 0)
446 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
447 }
448 if (bss == NULL) {
449 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
450 "group is persistent - BSS " MACSTR " not found",
451 MAC2STR(bssid));
452 return 0;
453 }
454
455 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
456 if (p2p == NULL) {
457 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
458 "group is persistent - BSS " MACSTR
459 " did not include P2P IE", MAC2STR(bssid));
460 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
461 (u8 *) (bss + 1), bss->ie_len);
462 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
463 ((u8 *) bss + 1) + bss->ie_len,
464 bss->beacon_ie_len);
465 return 0;
466 }
467
468 group_capab = p2p_get_group_capab(p2p);
469 addr = p2p_get_go_dev_addr(p2p);
470 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
471 "group_capab=0x%x", group_capab);
472 if (addr) {
473 os_memcpy(go_dev_addr, addr, ETH_ALEN);
474 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
475 MAC2STR(addr));
476 } else
477 os_memset(go_dev_addr, 0, ETH_ALEN);
478 wpabuf_free(p2p);
479
480 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
481 "go_dev_addr=" MACSTR,
482 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
483
484 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
485}
486
487
Jouni Malinen75ecf522011-06-27 15:19:46 -0700488static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
489 struct wpa_ssid *ssid,
490 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491{
492 struct wpa_ssid *s;
493 int changed = 0;
494
495 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
496 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
497 for (s = wpa_s->conf->ssid; s; s = s->next) {
498 if (s->disabled == 2 &&
499 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
500 s->ssid_len == ssid->ssid_len &&
501 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
502 break;
503 }
504
505 if (s) {
506 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
507 "entry");
508 if (ssid->passphrase && !s->passphrase)
509 changed = 1;
510 else if (ssid->passphrase && s->passphrase &&
511 os_strcmp(ssid->passphrase, s->passphrase) != 0)
512 changed = 1;
513 } else {
514 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
515 "entry");
516 changed = 1;
517 s = wpa_config_add_network(wpa_s->conf);
518 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700519 return -1;
520
521 /*
522 * Instead of network_added we emit persistent_group_added
523 * notification. Also to keep the defense checks in
524 * persistent_group obj registration method, we set the
525 * relevant flags in s to designate it as a persistent group.
526 */
527 s->p2p_group = 1;
528 s->p2p_persistent_group = 1;
529 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700530 wpa_config_set_network_defaults(s);
531 }
532
533 s->p2p_group = 1;
534 s->p2p_persistent_group = 1;
535 s->disabled = 2;
536 s->bssid_set = 1;
537 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
538 s->mode = ssid->mode;
539 s->auth_alg = WPA_AUTH_ALG_OPEN;
540 s->key_mgmt = WPA_KEY_MGMT_PSK;
541 s->proto = WPA_PROTO_RSN;
542 s->pairwise_cipher = WPA_CIPHER_CCMP;
543 s->export_keys = 1;
544 if (ssid->passphrase) {
545 os_free(s->passphrase);
546 s->passphrase = os_strdup(ssid->passphrase);
547 }
548 if (ssid->psk_set) {
549 s->psk_set = 1;
550 os_memcpy(s->psk, ssid->psk, 32);
551 }
552 if (s->passphrase && !s->psk_set)
553 wpa_config_update_psk(s);
554 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
555 os_free(s->ssid);
556 s->ssid = os_malloc(ssid->ssid_len);
557 }
558 if (s->ssid) {
559 s->ssid_len = ssid->ssid_len;
560 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
561 }
562
563#ifndef CONFIG_NO_CONFIG_WRITE
564 if (changed && wpa_s->conf->update_config &&
565 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
566 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
567 }
568#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700569
570 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700571}
572
573
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800574static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
575 const u8 *addr)
576{
577 struct wpa_ssid *ssid, *s;
578 u8 *n;
579 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700580 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800581
582 ssid = wpa_s->current_ssid;
583 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
584 !ssid->p2p_persistent_group)
585 return;
586
587 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
588 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
589 continue;
590
591 if (s->ssid_len == ssid->ssid_len &&
592 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
593 break;
594 }
595
596 if (s == NULL)
597 return;
598
599 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
600 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700601 ETH_ALEN) != 0)
602 continue;
603
604 if (i == s->num_p2p_clients - 1)
605 return; /* already the most recent entry */
606
607 /* move the entry to mark it most recent */
608 os_memmove(s->p2p_client_list + i * ETH_ALEN,
609 s->p2p_client_list + (i + 1) * ETH_ALEN,
610 (s->num_p2p_clients - i - 1) * ETH_ALEN);
611 os_memcpy(s->p2p_client_list +
612 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
613 found = 1;
614 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800615 }
616
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700617 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
618 n = os_realloc_array(s->p2p_client_list,
619 s->num_p2p_clients + 1, ETH_ALEN);
620 if (n == NULL)
621 return;
622 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
623 s->p2p_client_list = n;
624 s->num_p2p_clients++;
625 } else if (!found) {
626 /* Not enough room for an additional entry - drop the oldest
627 * entry */
628 os_memmove(s->p2p_client_list,
629 s->p2p_client_list + ETH_ALEN,
630 (s->num_p2p_clients - 1) * ETH_ALEN);
631 os_memcpy(s->p2p_client_list +
632 (s->num_p2p_clients - 1) * ETH_ALEN,
633 addr, ETH_ALEN);
634 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800635
636#ifndef CONFIG_NO_CONFIG_WRITE
637 if (wpa_s->parent->conf->update_config &&
638 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
639 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
640#endif /* CONFIG_NO_CONFIG_WRITE */
641}
642
643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
645 int success)
646{
647 struct wpa_ssid *ssid;
648 const char *ssid_txt;
649 int client;
650 int persistent;
651 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700652 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700653
654 /*
655 * This callback is likely called for the main interface. Update wpa_s
656 * to use the group interface if a new interface was created for the
657 * group.
658 */
659 if (wpa_s->global->p2p_group_formation)
660 wpa_s = wpa_s->global->p2p_group_formation;
661 wpa_s->global->p2p_group_formation = NULL;
662 wpa_s->p2p_in_provisioning = 0;
663
664 if (!success) {
665 wpa_msg(wpa_s->parent, MSG_INFO,
666 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700667 wpas_p2p_group_delete(wpa_s,
668 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 return;
670 }
671
672 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
673
674 ssid = wpa_s->current_ssid;
675 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
676 ssid->mode = WPAS_MODE_P2P_GO;
677 p2p_group_notif_formation_done(wpa_s->p2p_group);
678 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
679 }
680
681 persistent = 0;
682 if (ssid) {
683 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
684 client = ssid->mode == WPAS_MODE_INFRA;
685 if (ssid->mode == WPAS_MODE_P2P_GO) {
686 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700687 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
688 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700689 } else
690 persistent = wpas_p2p_persistent_group(wpa_s,
691 go_dev_addr,
692 ssid->ssid,
693 ssid->ssid_len);
694 } else {
695 ssid_txt = "";
696 client = wpa_s->p2p_group_interface ==
697 P2P_GROUP_INTERFACE_CLIENT;
698 os_memset(go_dev_addr, 0, ETH_ALEN);
699 }
700
701 wpa_s->show_group_started = 0;
702 if (client) {
703 /*
704 * Indicate event only after successfully completed 4-way
705 * handshake, i.e., when the interface is ready for data
706 * packets.
707 */
708 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700709#ifdef ANDROID_P2P
710 /* For client Second phase of Group formation (4-way handshake) can be still pending
711 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700712 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 -0700713 wpa_s->global->p2p_group_formation = wpa_s;
714#endif
715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
717 char psk[65];
718 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
719 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
720 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
721 "%s",
722 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
723 MAC2STR(go_dev_addr),
724 persistent ? " [PERSISTENT]" : "");
725 wpas_p2p_cross_connect_setup(wpa_s);
726 wpas_p2p_set_group_idle_timeout(wpa_s);
727 } else {
728 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
729 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
730 "go_dev_addr=" MACSTR "%s",
731 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
732 ssid && ssid->passphrase ? ssid->passphrase : "",
733 MAC2STR(go_dev_addr),
734 persistent ? " [PERSISTENT]" : "");
735 wpas_p2p_cross_connect_setup(wpa_s);
736 wpas_p2p_set_group_idle_timeout(wpa_s);
737 }
738
739 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700740 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
741 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800742 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700743 network_id = ssid->id;
744 if (!client)
745 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700746}
747
748
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800749static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
750 unsigned int freq,
751 const u8 *dst, const u8 *src,
752 const u8 *bssid,
753 const u8 *data, size_t data_len,
754 enum offchannel_send_action_result
755 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800757 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
760 return;
761 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
762 return;
763
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800764 switch (result) {
765 case OFFCHANNEL_SEND_ACTION_SUCCESS:
766 res = P2P_SEND_ACTION_SUCCESS;
767 break;
768 case OFFCHANNEL_SEND_ACTION_NO_ACK:
769 res = P2P_SEND_ACTION_NO_ACK;
770 break;
771 case OFFCHANNEL_SEND_ACTION_FAILED:
772 res = P2P_SEND_ACTION_FAILED;
773 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 }
775
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800776 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800778 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
779 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800780 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
781 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700783 if (wpa_s->p2p_fallback_to_go_neg) {
784 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
785 "during p2p_connect-auto");
786 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
787 return;
788 }
789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800791 "join-existing-group operation (no ACK for PD "
792 "Req)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793 wpas_p2p_join_start(wpa_s);
794 }
795}
796
797
798static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
799 const u8 *src, const u8 *bssid, const u8 *buf,
800 size_t len, unsigned int wait_time)
801{
802 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800803 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
804 wait_time,
805 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806}
807
808
809static void wpas_send_action_done(void *ctx)
810{
811 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800812 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813}
814
815
816static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
817 struct p2p_go_neg_results *params)
818{
819 if (wpa_s->go_params == NULL) {
820 wpa_s->go_params = os_malloc(sizeof(*params));
821 if (wpa_s->go_params == NULL)
822 return -1;
823 }
824 os_memcpy(wpa_s->go_params, params, sizeof(*params));
825 return 0;
826}
827
828
829static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
830 struct p2p_go_neg_results *res)
831{
832 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
833 MAC2STR(res->peer_interface_addr));
834 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
835 res->ssid, res->ssid_len);
836 wpa_supplicant_ap_deinit(wpa_s);
837 wpas_copy_go_neg_results(wpa_s, res);
838 if (res->wps_method == WPS_PBC)
839 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
840 else {
841 u16 dev_pw_id = DEV_PW_DEFAULT;
842 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
843 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
844 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
845 wpa_s->p2p_pin, 1, dev_pw_id);
846 }
847}
848
849
850static void p2p_go_configured(void *ctx, void *data)
851{
852 struct wpa_supplicant *wpa_s = ctx;
853 struct p2p_go_neg_results *params = data;
854 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700855 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700856
857 ssid = wpa_s->current_ssid;
858 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
859 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
860 if (wpa_s->global->p2p_group_formation == wpa_s)
861 wpa_s->global->p2p_group_formation = NULL;
862 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
863 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
864 "go_dev_addr=" MACSTR "%s",
865 wpa_s->ifname,
866 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
867 ssid->frequency,
868 params->passphrase ? params->passphrase : "",
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700869 MAC2STR(wpa_s->global->p2p_dev_addr),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 params->persistent_group ? " [PERSISTENT]" : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700873 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700875 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700876 if (network_id < 0)
877 network_id = ssid->id;
878 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879 wpas_p2p_cross_connect_setup(wpa_s);
880 wpas_p2p_set_group_idle_timeout(wpa_s);
881 return;
882 }
883
884 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
885 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
886 params->peer_interface_addr)) {
887 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
888 "filtering");
889 return;
890 }
891 if (params->wps_method == WPS_PBC)
892 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800893 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 else if (wpa_s->p2p_pin[0])
895 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
896 wpa_s->p2p_pin, NULL, 0);
897 os_free(wpa_s->go_params);
898 wpa_s->go_params = NULL;
899}
900
901
902static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
903 struct p2p_go_neg_results *params,
904 int group_formation)
905{
906 struct wpa_ssid *ssid;
907
908 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
909 return;
910
911 ssid = wpa_config_add_network(wpa_s->conf);
912 if (ssid == NULL)
913 return;
914
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800915 wpa_s->show_group_started = 0;
916
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 wpa_config_set_network_defaults(ssid);
918 ssid->temporary = 1;
919 ssid->p2p_group = 1;
920 ssid->p2p_persistent_group = params->persistent_group;
921 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
922 WPAS_MODE_P2P_GO;
923 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700924 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 ssid->ssid = os_zalloc(params->ssid_len + 1);
926 if (ssid->ssid) {
927 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
928 ssid->ssid_len = params->ssid_len;
929 }
930 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
931 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
932 ssid->proto = WPA_PROTO_RSN;
933 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
934 ssid->passphrase = os_strdup(params->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700935 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936
937 wpa_s->ap_configured_cb = p2p_go_configured;
938 wpa_s->ap_configured_cb_ctx = wpa_s;
939 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700940 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 wpa_s->reassociate = 1;
942 wpa_s->disconnected = 0;
943 wpa_supplicant_req_scan(wpa_s, 0, 0);
944}
945
946
947static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
948 const struct wpa_supplicant *src)
949{
950 struct wpa_config *d;
951 const struct wpa_config *s;
952
953 d = dst->conf;
954 s = src->conf;
955
956#define C(n) if (s->n) d->n = os_strdup(s->n)
957 C(device_name);
958 C(manufacturer);
959 C(model_name);
960 C(model_number);
961 C(serial_number);
962 C(config_methods);
963#undef C
964
965 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
966 os_memcpy(d->sec_device_type, s->sec_device_type,
967 sizeof(d->sec_device_type));
968 d->num_sec_device_types = s->num_sec_device_types;
969
970 d->p2p_group_idle = s->p2p_group_idle;
971 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800972 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700973 d->max_num_sta = s->max_num_sta;
974 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975}
976
977
978static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
979 enum wpa_driver_if_type type)
980{
981 char ifname[120], force_ifname[120];
982
983 if (wpa_s->pending_interface_name[0]) {
984 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
985 "- skip creation of a new one");
986 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
987 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
988 "unknown?! ifname='%s'",
989 wpa_s->pending_interface_name);
990 return -1;
991 }
992 return 0;
993 }
994
995 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
996 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800997 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
999 /* Try to avoid going over the IFNAMSIZ length limit */
1000 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1001 wpa_s->p2p_group_idx);
1002 }
1003 force_ifname[0] = '\0';
1004
1005 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1006 ifname);
1007 wpa_s->p2p_group_idx++;
1008
1009 wpa_s->pending_interface_type = type;
1010 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1011 wpa_s->pending_interface_addr, NULL) < 0) {
1012 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1013 "interface");
1014 return -1;
1015 }
1016
1017 if (force_ifname[0]) {
1018 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1019 force_ifname);
1020 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1021 sizeof(wpa_s->pending_interface_name));
1022 } else
1023 os_strlcpy(wpa_s->pending_interface_name, ifname,
1024 sizeof(wpa_s->pending_interface_name));
1025 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1026 MACSTR, wpa_s->pending_interface_name,
1027 MAC2STR(wpa_s->pending_interface_addr));
1028
1029 return 0;
1030}
1031
1032
1033static void wpas_p2p_remove_pending_group_interface(
1034 struct wpa_supplicant *wpa_s)
1035{
1036 if (!wpa_s->pending_interface_name[0] ||
1037 is_zero_ether_addr(wpa_s->pending_interface_addr))
1038 return; /* No pending virtual interface */
1039
1040 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1041 wpa_s->pending_interface_name);
1042 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1043 wpa_s->pending_interface_name);
1044 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1045 wpa_s->pending_interface_name[0] = '\0';
1046}
1047
1048
1049static struct wpa_supplicant *
1050wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1051{
1052 struct wpa_interface iface;
1053 struct wpa_supplicant *group_wpa_s;
1054
1055 if (!wpa_s->pending_interface_name[0]) {
1056 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1057 if (!wpas_p2p_create_iface(wpa_s))
1058 return NULL;
1059 /*
1060 * Something has forced us to remove the pending interface; try
1061 * to create a new one and hope for the best that we will get
1062 * the same local address.
1063 */
1064 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1065 WPA_IF_P2P_CLIENT) < 0)
1066 return NULL;
1067 }
1068
1069 os_memset(&iface, 0, sizeof(iface));
1070 iface.ifname = wpa_s->pending_interface_name;
1071 iface.driver = wpa_s->driver->name;
1072 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1073 iface.driver_param = wpa_s->conf->driver_param;
1074 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1075 if (group_wpa_s == NULL) {
1076 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1077 "wpa_supplicant interface");
1078 return NULL;
1079 }
1080 wpa_s->pending_interface_name[0] = '\0';
1081 group_wpa_s->parent = wpa_s;
1082 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1083 P2P_GROUP_INTERFACE_CLIENT;
1084 wpa_s->global->p2p_group_formation = group_wpa_s;
1085
1086 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1087
1088 return group_wpa_s;
1089}
1090
1091
1092static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1093 void *timeout_ctx)
1094{
1095 struct wpa_supplicant *wpa_s = eloop_ctx;
1096 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1097 if (wpa_s->global->p2p)
1098 p2p_group_formation_failed(wpa_s->global->p2p);
1099 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1100 wpa_drv_p2p_group_formation_failed(wpa_s);
1101 wpas_group_formation_completed(wpa_s, 0);
1102}
1103
1104
1105void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1106{
1107 struct wpa_supplicant *wpa_s = ctx;
1108
1109 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1110 wpa_drv_cancel_remain_on_channel(wpa_s);
1111 wpa_s->off_channel_freq = 0;
1112 wpa_s->roc_waiting_drv_freq = 0;
1113 }
1114
1115 if (res->status) {
1116 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1117 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001118 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001119 wpas_p2p_remove_pending_group_interface(wpa_s);
1120 return;
1121 }
1122
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001123 if (wpa_s->p2p_go_ht40)
1124 res->ht40 = 1;
1125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001127 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128
Dmitry Shmidt04949592012-07-19 12:16:46 -07001129 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1130 struct wpa_ssid *ssid;
1131 ssid = wpa_config_get_network(wpa_s->conf,
1132 wpa_s->p2p_persistent_id);
1133 if (ssid && ssid->disabled == 2 &&
1134 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1135 size_t len = os_strlen(ssid->passphrase);
1136 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1137 "on requested persistent group");
1138 os_memcpy(res->passphrase, ssid->passphrase, len);
1139 res->passphrase[len] = '\0';
1140 }
1141 }
1142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 if (wpa_s->create_p2p_iface) {
1144 struct wpa_supplicant *group_wpa_s =
1145 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1146 if (group_wpa_s == NULL) {
1147 wpas_p2p_remove_pending_group_interface(wpa_s);
1148 return;
1149 }
1150 if (group_wpa_s != wpa_s) {
1151 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1152 sizeof(group_wpa_s->p2p_pin));
1153 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1154 }
1155 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1156 wpa_s->pending_interface_name[0] = '\0';
1157 group_wpa_s->p2p_in_provisioning = 1;
1158
1159 if (res->role_go)
1160 wpas_start_wps_go(group_wpa_s, res, 1);
1161 else
1162 wpas_start_wps_enrollee(group_wpa_s, res);
1163 } else {
1164 wpa_s->p2p_in_provisioning = 1;
1165 wpa_s->global->p2p_group_formation = wpa_s;
1166
1167 if (res->role_go)
1168 wpas_start_wps_go(wpa_s, res, 1);
1169 else
1170 wpas_start_wps_enrollee(ctx, res);
1171 }
1172
1173 wpa_s->p2p_long_listen = 0;
1174 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1175
1176 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1177 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1178 (res->peer_config_timeout % 100) * 10000,
1179 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1180}
1181
1182
1183void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1184{
1185 struct wpa_supplicant *wpa_s = ctx;
1186 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1187 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1188
1189 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1190}
1191
1192
1193void wpas_dev_found(void *ctx, const u8 *addr,
1194 const struct p2p_peer_info *info,
1195 int new_device)
1196{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198 struct wpa_supplicant *wpa_s = ctx;
1199 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001200#define WFD_DEV_INFO_SIZE 9
1201 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
1202
1203#ifdef CONFIG_WIFI_DISPLAY
1204 if (info->wfd_subelems) {
1205 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1206 wpabuf_head(info->wfd_subelems),
1207 WFD_DEV_INFO_SIZE);
1208 }
1209#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210
1211 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1212 " p2p_dev_addr=" MACSTR
1213 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001214 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1216 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1217 sizeof(devtype)),
1218 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001219 info->dev_capab, info->group_capab,
1220 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001221#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222
1223 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1224}
1225
1226
1227static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1228{
1229 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001230
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001231 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1232 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1235}
1236
1237
1238static int wpas_start_listen(void *ctx, unsigned int freq,
1239 unsigned int duration,
1240 const struct wpabuf *probe_resp_ie)
1241{
1242 struct wpa_supplicant *wpa_s = ctx;
1243
1244 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1245
1246 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1247 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1248 "report received Probe Request frames");
1249 return -1;
1250 }
1251
1252 wpa_s->pending_listen_freq = freq;
1253 wpa_s->pending_listen_duration = duration;
1254
1255 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1256 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1257 "to remain on channel (%u MHz) for Listen "
1258 "state", freq);
1259 wpa_s->pending_listen_freq = 0;
1260 return -1;
1261 }
1262 wpa_s->off_channel_freq = 0;
1263 wpa_s->roc_waiting_drv_freq = freq;
1264
1265 return 0;
1266}
1267
1268
1269static void wpas_stop_listen(void *ctx)
1270{
1271 struct wpa_supplicant *wpa_s = ctx;
1272 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1273 wpa_drv_cancel_remain_on_channel(wpa_s);
1274 wpa_s->off_channel_freq = 0;
1275 wpa_s->roc_waiting_drv_freq = 0;
1276 }
1277 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1278 wpa_drv_probe_req_report(wpa_s, 0);
1279}
1280
1281
1282static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1283{
1284 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001285 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286}
1287
1288
1289static struct p2p_srv_bonjour *
1290wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1291 const struct wpabuf *query)
1292{
1293 struct p2p_srv_bonjour *bsrv;
1294 size_t len;
1295
1296 len = wpabuf_len(query);
1297 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1298 struct p2p_srv_bonjour, list) {
1299 if (len == wpabuf_len(bsrv->query) &&
1300 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1301 len) == 0)
1302 return bsrv;
1303 }
1304 return NULL;
1305}
1306
1307
1308static struct p2p_srv_upnp *
1309wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1310 const char *service)
1311{
1312 struct p2p_srv_upnp *usrv;
1313
1314 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1315 struct p2p_srv_upnp, list) {
1316 if (version == usrv->version &&
1317 os_strcmp(service, usrv->service) == 0)
1318 return usrv;
1319 }
1320 return NULL;
1321}
1322
1323
1324static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1325 u8 srv_trans_id)
1326{
1327 u8 *len_pos;
1328
1329 if (wpabuf_tailroom(resp) < 5)
1330 return;
1331
1332 /* Length (to be filled) */
1333 len_pos = wpabuf_put(resp, 2);
1334 wpabuf_put_u8(resp, srv_proto);
1335 wpabuf_put_u8(resp, srv_trans_id);
1336 /* Status Code */
1337 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1338 /* Response Data: empty */
1339 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1340}
1341
1342
1343static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1344 struct wpabuf *resp, u8 srv_trans_id)
1345{
1346 struct p2p_srv_bonjour *bsrv;
1347 u8 *len_pos;
1348
1349 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1350
1351 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1352 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1353 return;
1354 }
1355
1356 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1357 struct p2p_srv_bonjour, list) {
1358 if (wpabuf_tailroom(resp) <
1359 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1360 return;
1361 /* Length (to be filled) */
1362 len_pos = wpabuf_put(resp, 2);
1363 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1364 wpabuf_put_u8(resp, srv_trans_id);
1365 /* Status Code */
1366 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1367 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1368 wpabuf_head(bsrv->resp),
1369 wpabuf_len(bsrv->resp));
1370 /* Response Data */
1371 wpabuf_put_buf(resp, bsrv->query); /* Key */
1372 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1373 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1374 2);
1375 }
1376}
1377
1378
1379static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1380 struct wpabuf *resp, u8 srv_trans_id,
1381 const u8 *query, size_t query_len)
1382{
1383 struct p2p_srv_bonjour *bsrv;
1384 struct wpabuf buf;
1385 u8 *len_pos;
1386
1387 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1388 query, query_len);
1389 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1390 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1391 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1392 srv_trans_id);
1393 return;
1394 }
1395
1396 if (query_len == 0) {
1397 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1398 return;
1399 }
1400
1401 if (wpabuf_tailroom(resp) < 5)
1402 return;
1403 /* Length (to be filled) */
1404 len_pos = wpabuf_put(resp, 2);
1405 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1406 wpabuf_put_u8(resp, srv_trans_id);
1407
1408 wpabuf_set(&buf, query, query_len);
1409 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1410 if (bsrv == NULL) {
1411 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1412 "available");
1413
1414 /* Status Code */
1415 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1416 /* Response Data: empty */
1417 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1418 2);
1419 return;
1420 }
1421
1422 /* Status Code */
1423 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1424 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1425 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1426
1427 if (wpabuf_tailroom(resp) >=
1428 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1429 /* Response Data */
1430 wpabuf_put_buf(resp, bsrv->query); /* Key */
1431 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1432 }
1433 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1434}
1435
1436
1437static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1438 struct wpabuf *resp, u8 srv_trans_id)
1439{
1440 struct p2p_srv_upnp *usrv;
1441 u8 *len_pos;
1442
1443 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1444
1445 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1446 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1447 return;
1448 }
1449
1450 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1451 struct p2p_srv_upnp, list) {
1452 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1453 return;
1454
1455 /* Length (to be filled) */
1456 len_pos = wpabuf_put(resp, 2);
1457 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1458 wpabuf_put_u8(resp, srv_trans_id);
1459
1460 /* Status Code */
1461 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1462 /* Response Data */
1463 wpabuf_put_u8(resp, usrv->version);
1464 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1465 usrv->service);
1466 wpabuf_put_str(resp, usrv->service);
1467 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1468 2);
1469 }
1470}
1471
1472
1473static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1474 struct wpabuf *resp, u8 srv_trans_id,
1475 const u8 *query, size_t query_len)
1476{
1477 struct p2p_srv_upnp *usrv;
1478 u8 *len_pos;
1479 u8 version;
1480 char *str;
1481 int count = 0;
1482
1483 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1484 query, query_len);
1485
1486 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1487 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1488 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1489 srv_trans_id);
1490 return;
1491 }
1492
1493 if (query_len == 0) {
1494 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1495 return;
1496 }
1497
1498 if (wpabuf_tailroom(resp) < 5)
1499 return;
1500
1501 /* Length (to be filled) */
1502 len_pos = wpabuf_put(resp, 2);
1503 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1504 wpabuf_put_u8(resp, srv_trans_id);
1505
1506 version = query[0];
1507 str = os_malloc(query_len);
1508 if (str == NULL)
1509 return;
1510 os_memcpy(str, query + 1, query_len - 1);
1511 str[query_len - 1] = '\0';
1512
1513 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1514 struct p2p_srv_upnp, list) {
1515 if (version != usrv->version)
1516 continue;
1517
1518 if (os_strcmp(str, "ssdp:all") != 0 &&
1519 os_strstr(usrv->service, str) == NULL)
1520 continue;
1521
1522 if (wpabuf_tailroom(resp) < 2)
1523 break;
1524 if (count == 0) {
1525 /* Status Code */
1526 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1527 /* Response Data */
1528 wpabuf_put_u8(resp, version);
1529 } else
1530 wpabuf_put_u8(resp, ',');
1531
1532 count++;
1533
1534 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1535 usrv->service);
1536 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1537 break;
1538 wpabuf_put_str(resp, usrv->service);
1539 }
1540 os_free(str);
1541
1542 if (count == 0) {
1543 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1544 "available");
1545 /* Status Code */
1546 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1547 /* Response Data: empty */
1548 }
1549
1550 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1551}
1552
1553
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001554#ifdef CONFIG_WIFI_DISPLAY
1555static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1556 struct wpabuf *resp, u8 srv_trans_id,
1557 const u8 *query, size_t query_len)
1558{
1559 const u8 *pos;
1560 u8 role;
1561 u8 *len_pos;
1562
1563 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1564
1565 if (!wpa_s->global->wifi_display) {
1566 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1567 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1568 srv_trans_id);
1569 return;
1570 }
1571
1572 if (query_len < 1) {
1573 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1574 "Role");
1575 return;
1576 }
1577
1578 if (wpabuf_tailroom(resp) < 5)
1579 return;
1580
1581 pos = query;
1582 role = *pos++;
1583 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1584
1585 /* TODO: role specific handling */
1586
1587 /* Length (to be filled) */
1588 len_pos = wpabuf_put(resp, 2);
1589 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1590 wpabuf_put_u8(resp, srv_trans_id);
1591 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1592
1593 while (pos < query + query_len) {
1594 if (*pos < MAX_WFD_SUBELEMS &&
1595 wpa_s->global->wfd_subelem[*pos] &&
1596 wpabuf_tailroom(resp) >=
1597 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1598 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1599 "subelement %u", *pos);
1600 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1601 }
1602 pos++;
1603 }
1604
1605 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1606}
1607#endif /* CONFIG_WIFI_DISPLAY */
1608
1609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1611 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1612{
1613 struct wpa_supplicant *wpa_s = ctx;
1614 const u8 *pos = tlvs;
1615 const u8 *end = tlvs + tlvs_len;
1616 const u8 *tlv_end;
1617 u16 slen;
1618 struct wpabuf *resp;
1619 u8 srv_proto, srv_trans_id;
1620 size_t buf_len;
1621 char *buf;
1622
1623 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1624 tlvs, tlvs_len);
1625 buf_len = 2 * tlvs_len + 1;
1626 buf = os_malloc(buf_len);
1627 if (buf) {
1628 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1629 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1630 MACSTR " %u %u %s",
1631 freq, MAC2STR(sa), dialog_token, update_indic,
1632 buf);
1633 os_free(buf);
1634 }
1635
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001636 if (wpa_s->p2p_sd_over_ctrl_iface) {
1637 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1638 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001640 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641
1642 resp = wpabuf_alloc(10000);
1643 if (resp == NULL)
1644 return;
1645
1646 while (pos + 1 < end) {
1647 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1648 slen = WPA_GET_LE16(pos);
1649 pos += 2;
1650 if (pos + slen > end || slen < 2) {
1651 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1652 "length");
1653 wpabuf_free(resp);
1654 return;
1655 }
1656 tlv_end = pos + slen;
1657
1658 srv_proto = *pos++;
1659 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1660 srv_proto);
1661 srv_trans_id = *pos++;
1662 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1663 srv_trans_id);
1664
1665 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1666 pos, tlv_end - pos);
1667
1668
1669 if (wpa_s->force_long_sd) {
1670 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1671 "response");
1672 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1673 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1674 goto done;
1675 }
1676
1677 switch (srv_proto) {
1678 case P2P_SERV_ALL_SERVICES:
1679 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1680 "for all services");
1681 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1682 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1683 wpa_printf(MSG_DEBUG, "P2P: No service "
1684 "discovery protocols available");
1685 wpas_sd_add_proto_not_avail(
1686 resp, P2P_SERV_ALL_SERVICES,
1687 srv_trans_id);
1688 break;
1689 }
1690 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1691 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1692 break;
1693 case P2P_SERV_BONJOUR:
1694 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1695 pos, tlv_end - pos);
1696 break;
1697 case P2P_SERV_UPNP:
1698 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1699 pos, tlv_end - pos);
1700 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001701#ifdef CONFIG_WIFI_DISPLAY
1702 case P2P_SERV_WIFI_DISPLAY:
1703 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1704 pos, tlv_end - pos);
1705 break;
1706#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 default:
1708 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1709 "protocol %u", srv_proto);
1710 wpas_sd_add_proto_not_avail(resp, srv_proto,
1711 srv_trans_id);
1712 break;
1713 }
1714
1715 pos = tlv_end;
1716 }
1717
1718done:
1719 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1720 update_indic, tlvs, tlvs_len);
1721
1722 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1723
1724 wpabuf_free(resp);
1725}
1726
1727
1728void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1729 const u8 *tlvs, size_t tlvs_len)
1730{
1731 struct wpa_supplicant *wpa_s = ctx;
1732 const u8 *pos = tlvs;
1733 const u8 *end = tlvs + tlvs_len;
1734 const u8 *tlv_end;
1735 u16 slen;
1736 size_t buf_len;
1737 char *buf;
1738
1739 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1740 tlvs, tlvs_len);
1741 if (tlvs_len > 1500) {
1742 /* TODO: better way for handling this */
1743 wpa_msg_ctrl(wpa_s, MSG_INFO,
1744 P2P_EVENT_SERV_DISC_RESP MACSTR
1745 " %u <long response: %u bytes>",
1746 MAC2STR(sa), update_indic,
1747 (unsigned int) tlvs_len);
1748 } else {
1749 buf_len = 2 * tlvs_len + 1;
1750 buf = os_malloc(buf_len);
1751 if (buf) {
1752 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1753 wpa_msg_ctrl(wpa_s, MSG_INFO,
1754 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1755 MAC2STR(sa), update_indic, buf);
1756 os_free(buf);
1757 }
1758 }
1759
1760 while (pos < end) {
1761 u8 srv_proto, srv_trans_id, status;
1762
1763 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1764 slen = WPA_GET_LE16(pos);
1765 pos += 2;
1766 if (pos + slen > end || slen < 3) {
1767 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1768 "length");
1769 return;
1770 }
1771 tlv_end = pos + slen;
1772
1773 srv_proto = *pos++;
1774 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1775 srv_proto);
1776 srv_trans_id = *pos++;
1777 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1778 srv_trans_id);
1779 status = *pos++;
1780 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1781 status);
1782
1783 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1784 pos, tlv_end - pos);
1785
1786 pos = tlv_end;
1787 }
1788
1789 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1790}
1791
1792
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001793u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1794 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001795{
1796 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001797 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001799 return 0;
1800 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801}
1802
1803
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001804u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1805 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806{
1807 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001808 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809
1810 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1811 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001812 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1814 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1815 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1816 wpabuf_put_u8(tlvs, version);
1817 wpabuf_put_str(tlvs, query);
1818 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1819 wpabuf_free(tlvs);
1820 return ret;
1821}
1822
1823
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001824#ifdef CONFIG_WIFI_DISPLAY
1825
1826static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1827 const struct wpabuf *tlvs)
1828{
1829 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1830 return 0;
1831 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1832 return 0;
1833 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1834}
1835
1836
1837#define MAX_WFD_SD_SUBELEMS 20
1838
1839static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1840 const char *subelems)
1841{
1842 u8 *len;
1843 const char *pos;
1844 int val;
1845 int count = 0;
1846
1847 len = wpabuf_put(tlvs, 2);
1848 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1849 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1850
1851 wpabuf_put_u8(tlvs, role);
1852
1853 pos = subelems;
1854 while (*pos) {
1855 val = atoi(pos);
1856 if (val >= 0 && val < 256) {
1857 wpabuf_put_u8(tlvs, val);
1858 count++;
1859 if (count == MAX_WFD_SD_SUBELEMS)
1860 break;
1861 }
1862 pos = os_strchr(pos + 1, ',');
1863 if (pos == NULL)
1864 break;
1865 pos++;
1866 }
1867
1868 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1869}
1870
1871
1872u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1873 const u8 *dst, const char *role)
1874{
1875 struct wpabuf *tlvs;
1876 u64 ret;
1877 const char *subelems;
1878 u8 id = 1;
1879
1880 subelems = os_strchr(role, ' ');
1881 if (subelems == NULL)
1882 return 0;
1883 subelems++;
1884
1885 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1886 if (tlvs == NULL)
1887 return 0;
1888
1889 if (os_strstr(role, "[source]"))
1890 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1891 if (os_strstr(role, "[pri-sink]"))
1892 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1893 if (os_strstr(role, "[sec-sink]"))
1894 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1895 if (os_strstr(role, "[source+sink]"))
1896 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1897
1898 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1899 wpabuf_free(tlvs);
1900 return ret;
1901}
1902
1903#endif /* CONFIG_WIFI_DISPLAY */
1904
1905
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001906int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907{
1908 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001909 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001910 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1911 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001912 return p2p_sd_cancel_request(wpa_s->global->p2p,
1913 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001914}
1915
1916
1917void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1918 const u8 *dst, u8 dialog_token,
1919 const struct wpabuf *resp_tlvs)
1920{
1921 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1922 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1923 resp_tlvs);
1924 return;
1925 }
1926 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1927 return;
1928 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1929 resp_tlvs);
1930}
1931
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001932#ifdef ANDROID_P2P
1933void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1934#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001936#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937{
1938 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1939 wpa_drv_p2p_service_update(wpa_s);
1940 return;
1941 }
1942 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001943#ifdef ANDROID_P2P
1944 p2p_sd_service_update(wpa_s->global->p2p, action);
1945#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001946 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001947#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001948}
1949
1950
1951static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1952{
1953 dl_list_del(&bsrv->list);
1954 wpabuf_free(bsrv->query);
1955 wpabuf_free(bsrv->resp);
1956 os_free(bsrv);
1957}
1958
1959
1960static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1961{
1962 dl_list_del(&usrv->list);
1963 os_free(usrv->service);
1964 os_free(usrv);
1965}
1966
1967
1968void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1969{
1970 struct p2p_srv_bonjour *bsrv, *bn;
1971 struct p2p_srv_upnp *usrv, *un;
1972
1973 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1974 struct p2p_srv_bonjour, list)
1975 wpas_p2p_srv_bonjour_free(bsrv);
1976
1977 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1978 struct p2p_srv_upnp, list)
1979 wpas_p2p_srv_upnp_free(usrv);
1980
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001981#ifdef ANDROID_P2P
1982 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1983#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001985#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001986}
1987
1988
1989int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1990 struct wpabuf *query, struct wpabuf *resp)
1991{
1992 struct p2p_srv_bonjour *bsrv;
1993
1994 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1995 if (bsrv) {
1996 wpabuf_free(query);
1997 wpabuf_free(bsrv->resp);
1998 bsrv->resp = resp;
1999 return 0;
2000 }
2001
2002 bsrv = os_zalloc(sizeof(*bsrv));
2003 if (bsrv == NULL)
2004 return -1;
2005 bsrv->query = query;
2006 bsrv->resp = resp;
2007 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2008
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002009#ifdef ANDROID_P2P
2010 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2011#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002013#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014 return 0;
2015}
2016
2017
2018int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2019 const struct wpabuf *query)
2020{
2021 struct p2p_srv_bonjour *bsrv;
2022
2023 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2024 if (bsrv == NULL)
2025 return -1;
2026 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002027#ifdef ANDROID_P2P
2028 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2029#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002031#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032 return 0;
2033}
2034
2035
2036int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2037 const char *service)
2038{
2039 struct p2p_srv_upnp *usrv;
2040
2041 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2042 return 0; /* Already listed */
2043 usrv = os_zalloc(sizeof(*usrv));
2044 if (usrv == NULL)
2045 return -1;
2046 usrv->version = version;
2047 usrv->service = os_strdup(service);
2048 if (usrv->service == NULL) {
2049 os_free(usrv);
2050 return -1;
2051 }
2052 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2053
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002054#ifdef ANDROID_P2P
2055 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2056#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002058#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059 return 0;
2060}
2061
2062
2063int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2064 const char *service)
2065{
2066 struct p2p_srv_upnp *usrv;
2067
2068 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2069 if (usrv == NULL)
2070 return -1;
2071 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002072#ifdef ANDROID_P2P
2073 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2074#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002076#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077 return 0;
2078}
2079
2080
2081static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2082 const u8 *peer, const char *params,
2083 unsigned int generated_pin)
2084{
2085 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2086 MAC2STR(peer), generated_pin, params);
2087}
2088
2089
2090static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2091 const u8 *peer, const char *params)
2092{
2093 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2094 MAC2STR(peer), params);
2095}
2096
2097
2098void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2099 const u8 *dev_addr, const u8 *pri_dev_type,
2100 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002101 u8 dev_capab, u8 group_capab, const u8 *group_id,
2102 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103{
2104 struct wpa_supplicant *wpa_s = ctx;
2105 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002106 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 u8 empty_dev_type[8];
2108 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002109 struct wpa_supplicant *group = NULL;
2110
2111 if (group_id) {
2112 for (group = wpa_s->global->ifaces; group; group = group->next)
2113 {
2114 struct wpa_ssid *s = group->current_ssid;
2115 if (s != NULL &&
2116 s->mode == WPAS_MODE_P2P_GO &&
2117 group_id_len - ETH_ALEN == s->ssid_len &&
2118 os_memcmp(group_id + ETH_ALEN, s->ssid,
2119 s->ssid_len) == 0)
2120 break;
2121 }
2122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002123
2124 if (pri_dev_type == NULL) {
2125 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2126 pri_dev_type = empty_dev_type;
2127 }
2128 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2129 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002130 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002131 MAC2STR(dev_addr),
2132 wps_dev_type_bin2str(pri_dev_type, devtype,
2133 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002134 dev_name, supp_config_methods, dev_capab, group_capab,
2135 group ? " group=" : "",
2136 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 if (config_methods & WPS_CONFIG_DISPLAY) {
2140 generated_pin = wps_generate_pin();
2141 wpas_prov_disc_local_display(wpa_s, peer, params,
2142 generated_pin);
2143 } else if (config_methods & WPS_CONFIG_KEYPAD)
2144 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2145 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2146 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2147 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002148
2149 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2150 P2P_PROV_DISC_SUCCESS,
2151 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152}
2153
2154
2155void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2156{
2157 struct wpa_supplicant *wpa_s = ctx;
2158 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002159 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002161 if (wpa_s->pending_pd_before_join &&
2162 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2163 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2164 wpa_s->pending_pd_before_join = 0;
2165 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2166 "join-existing-group operation");
2167 wpas_p2p_join_start(wpa_s);
2168 return;
2169 }
2170
Dmitry Shmidt04949592012-07-19 12:16:46 -07002171 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2172 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2173 os_snprintf(params, sizeof(params), " peer_go=%d",
2174 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2175 else
2176 params[0] = '\0';
2177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002179 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180 else if (config_methods & WPS_CONFIG_KEYPAD) {
2181 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002182 wpas_prov_disc_local_display(wpa_s, peer, params,
2183 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002185 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2186 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187
Jouni Malinen75ecf522011-06-27 15:19:46 -07002188 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2189 P2P_PROV_DISC_SUCCESS,
2190 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191}
2192
2193
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002194static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2195 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002196{
2197 struct wpa_supplicant *wpa_s = ctx;
2198
Dmitry Shmidt04949592012-07-19 12:16:46 -07002199 if (wpa_s->p2p_fallback_to_go_neg) {
2200 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2201 "failed - fall back to GO Negotiation");
2202 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2203 return;
2204 }
2205
2206 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2207 " p2p_dev_addr=" MACSTR " status=%d",
2208 MAC2STR(peer), status);
2209
Jouni Malinen75ecf522011-06-27 15:19:46 -07002210 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2211 status, 0, 0);
2212}
2213
2214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2216 const u8 *go_dev_addr, const u8 *ssid,
2217 size_t ssid_len, int *go, u8 *group_bssid,
2218 int *force_freq, int persistent_group)
2219{
2220 struct wpa_supplicant *wpa_s = ctx;
2221 struct wpa_ssid *s;
2222 u8 cur_bssid[ETH_ALEN];
2223 int res;
2224 struct wpa_supplicant *grp;
2225
2226 if (!persistent_group) {
2227 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2228 " to join an active group", MAC2STR(sa));
2229 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2230 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2231 == 0 ||
2232 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2233 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2234 "authorized invitation");
2235 goto accept_inv;
2236 }
2237 /*
2238 * Do not accept the invitation automatically; notify user and
2239 * request approval.
2240 */
2241 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2242 }
2243
2244 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2245 if (grp) {
2246 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2247 "running persistent group");
2248 if (*go)
2249 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2250 goto accept_inv;
2251 }
2252
2253 if (!wpa_s->conf->persistent_reconnect)
2254 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2255
2256 for (s = wpa_s->conf->ssid; s; s = s->next) {
2257 if (s->disabled == 2 &&
2258 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2259 s->ssid_len == ssid_len &&
2260 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2261 break;
2262 }
2263
2264 if (!s) {
2265 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2266 " requested reinvocation of an unknown group",
2267 MAC2STR(sa));
2268 return P2P_SC_FAIL_UNKNOWN_GROUP;
2269 }
2270
2271 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2272 *go = 1;
2273 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2274 wpa_printf(MSG_DEBUG, "P2P: The only available "
2275 "interface is already in use - reject "
2276 "invitation");
2277 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2278 }
2279 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2280 } else if (s->mode == WPAS_MODE_P2P_GO) {
2281 *go = 1;
2282 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2283 {
2284 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2285 "interface address for the group");
2286 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2287 }
2288 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2289 ETH_ALEN);
2290 }
2291
2292accept_inv:
2293 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2294 wpa_s->assoc_freq) {
2295 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2296 "the channel we are already using");
2297 *force_freq = wpa_s->assoc_freq;
2298 }
2299
2300 res = wpa_drv_shared_freq(wpa_s);
2301 if (res > 0) {
2302 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2303 "with the channel we are already using on a "
2304 "shared interface");
2305 *force_freq = res;
2306 }
2307
2308 return P2P_SC_SUCCESS;
2309}
2310
2311
2312static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2313 const u8 *ssid, size_t ssid_len,
2314 const u8 *go_dev_addr, u8 status,
2315 int op_freq)
2316{
2317 struct wpa_supplicant *wpa_s = ctx;
2318 struct wpa_ssid *s;
2319
2320 for (s = wpa_s->conf->ssid; s; s = s->next) {
2321 if (s->disabled == 2 &&
2322 s->ssid_len == ssid_len &&
2323 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2324 break;
2325 }
2326
2327 if (status == P2P_SC_SUCCESS) {
2328 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2329 " was accepted; op_freq=%d MHz",
2330 MAC2STR(sa), op_freq);
2331 if (s) {
2332 wpas_p2p_group_add_persistent(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002333 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 } else if (bssid) {
2335 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002336 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337 }
2338 return;
2339 }
2340
2341 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2342 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2343 " was rejected (status %u)", MAC2STR(sa), status);
2344 return;
2345 }
2346
2347 if (!s) {
2348 if (bssid) {
2349 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2350 "sa=" MACSTR " go_dev_addr=" MACSTR
2351 " bssid=" MACSTR " unknown-network",
2352 MAC2STR(sa), MAC2STR(go_dev_addr),
2353 MAC2STR(bssid));
2354 } else {
2355 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2356 "sa=" MACSTR " go_dev_addr=" MACSTR
2357 " unknown-network",
2358 MAC2STR(sa), MAC2STR(go_dev_addr));
2359 }
2360 return;
2361 }
2362
2363 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2364 " persistent=%d", MAC2STR(sa), s->id);
2365}
2366
2367
2368static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2369{
2370 struct wpa_supplicant *wpa_s = ctx;
2371 struct wpa_ssid *ssid;
2372
2373 if (bssid) {
2374 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2375 "status=%d " MACSTR,
2376 status, MAC2STR(bssid));
2377 } else {
2378 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2379 "status=%d ", status);
2380 }
2381 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2382
2383 if (wpa_s->pending_invite_ssid_id == -1)
2384 return; /* Invitation to active group */
2385
2386 if (status != P2P_SC_SUCCESS) {
2387 wpas_p2p_remove_pending_group_interface(wpa_s);
2388 return;
2389 }
2390
2391 ssid = wpa_config_get_network(wpa_s->conf,
2392 wpa_s->pending_invite_ssid_id);
2393 if (ssid == NULL) {
2394 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2395 "data matching with invitation");
2396 return;
2397 }
2398
2399 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002400 ssid->mode == WPAS_MODE_P2P_GO,
2401 wpa_s->p2p_persistent_go_freq,
2402 wpa_s->p2p_go_ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403}
2404
2405
Dmitry Shmidt04949592012-07-19 12:16:46 -07002406static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2407 unsigned int freq)
2408{
2409 unsigned int i;
2410
2411 if (global->p2p_disallow_freq == NULL)
2412 return 0;
2413
2414 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2415 if (freq >= global->p2p_disallow_freq[i].min &&
2416 freq <= global->p2p_disallow_freq[i].max)
2417 return 1;
2418 }
2419
2420 return 0;
2421}
2422
2423
2424static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2425{
2426 reg->channel[reg->channels] = chan;
2427 reg->channels++;
2428}
2429
2430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002431static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2432 struct p2p_channels *chan)
2433{
2434 int i, cla = 0;
2435
2436 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2437 "band");
2438
2439 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2440 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002441 chan->reg_class[cla].channels = 0;
2442 for (i = 0; i < 11; i++) {
2443 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2444 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2445 }
2446 if (chan->reg_class[cla].channels)
2447 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002448
2449 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2450 "band");
2451
2452 /* Operating class 115 - 5 GHz, channels 36-48 */
2453 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002454 chan->reg_class[cla].channels = 0;
2455 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2456 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2457 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2458 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2459 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2460 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2461 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2462 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2463 if (chan->reg_class[cla].channels)
2464 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002465
2466 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2467 "band");
2468
2469 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2470 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002471 chan->reg_class[cla].channels = 0;
2472 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2473 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2474 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2475 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2476 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2477 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2478 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2479 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2480 if (chan->reg_class[cla].channels)
2481 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482
2483 chan->reg_classes = cla;
2484 return 0;
2485}
2486
2487
2488static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2489 u16 num_modes,
2490 enum hostapd_hw_mode mode)
2491{
2492 u16 i;
2493
2494 for (i = 0; i < num_modes; i++) {
2495 if (modes[i].mode == mode)
2496 return &modes[i];
2497 }
2498
2499 return NULL;
2500}
2501
2502
Dmitry Shmidt04949592012-07-19 12:16:46 -07002503static int has_channel(struct wpa_global *global,
2504 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505{
2506 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002507 unsigned int freq;
2508
2509 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2510 chan * 5;
2511 if (wpas_p2p_disallowed_freq(global, freq))
2512 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513
2514 for (i = 0; i < mode->num_channels; i++) {
2515 if (mode->channels[i].chan == chan) {
2516 if (flags)
2517 *flags = mode->channels[i].flag;
2518 return !(mode->channels[i].flag &
2519 (HOSTAPD_CHAN_DISABLED |
2520 HOSTAPD_CHAN_PASSIVE_SCAN |
2521 HOSTAPD_CHAN_NO_IBSS |
2522 HOSTAPD_CHAN_RADAR));
2523 }
2524 }
2525
2526 return 0;
2527}
2528
2529
2530struct p2p_oper_class_map {
2531 enum hostapd_hw_mode mode;
2532 u8 op_class;
2533 u8 min_chan;
2534 u8 max_chan;
2535 u8 inc;
2536 enum { BW20, BW40PLUS, BW40MINUS } bw;
2537};
2538
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002539static struct p2p_oper_class_map op_class[] = {
2540 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2541 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2542#if 0 /* Do not enable HT40 on 2 GHz for now */
2543 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2544 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2545#endif
2546 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2547 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2548 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2549 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2550 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2551 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2552 { -1, 0, 0, 0, 0, BW20 }
2553};
2554
2555
2556static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2557 struct hostapd_hw_modes *mode,
2558 u8 channel, u8 bw)
2559{
2560 int flag;
2561
2562 if (!has_channel(wpa_s->global, mode, channel, &flag))
2563 return -1;
2564 if (bw == BW40MINUS &&
2565 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2566 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2567 return 0;
2568 if (bw == BW40PLUS &&
2569 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2570 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2571 return 0;
2572 return 1;
2573}
2574
2575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2577 struct p2p_channels *chan)
2578{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002579 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002582 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2584 "of all supported channels; assume dualband "
2585 "support");
2586 return wpas_p2p_default_channels(wpa_s, chan);
2587 }
2588
2589 cla = 0;
2590
2591 for (op = 0; op_class[op].op_class; op++) {
2592 struct p2p_oper_class_map *o = &op_class[op];
2593 u8 ch;
2594 struct p2p_reg_class *reg = NULL;
2595
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002596 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597 if (mode == NULL)
2598 continue;
2599 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002600 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002601 continue;
2602 if (reg == NULL) {
2603 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2604 "class %u", o->op_class);
2605 reg = &chan->reg_class[cla];
2606 cla++;
2607 reg->reg_class = o->op_class;
2608 }
2609 reg->channel[reg->channels] = ch;
2610 reg->channels++;
2611 }
2612 if (reg) {
2613 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2614 reg->channel, reg->channels);
2615 }
2616 }
2617
2618 chan->reg_classes = cla;
2619
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620 return 0;
2621}
2622
2623
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002624int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2625 struct hostapd_hw_modes *mode, u8 channel)
2626{
2627 int op, ret;
2628
2629 for (op = 0; op_class[op].op_class; op++) {
2630 struct p2p_oper_class_map *o = &op_class[op];
2631 u8 ch;
2632
2633 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2634 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2635 o->bw == BW20 || ch != channel)
2636 continue;
2637 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2638 if (ret < 0)
2639 continue;
2640 else if (ret > 0)
2641 return (o->bw == BW40MINUS) ? -1 : 1;
2642 else
2643 return 0;
2644 }
2645 }
2646 return 0;
2647}
2648
2649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002650static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2651 size_t buf_len)
2652{
2653 struct wpa_supplicant *wpa_s = ctx;
2654
2655 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2656 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2657 break;
2658 }
2659 if (wpa_s == NULL)
2660 return -1;
2661
2662 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2663}
2664
2665
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002666static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2667{
2668 struct wpa_supplicant *wpa_s = ctx;
2669
2670 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2671 struct wpa_ssid *ssid = wpa_s->current_ssid;
2672 if (ssid == NULL)
2673 continue;
2674 if (ssid->mode != WPAS_MODE_INFRA)
2675 continue;
2676 if (wpa_s->wpa_state != WPA_COMPLETED &&
2677 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2678 continue;
2679 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2680 return 1;
2681 }
2682
2683 return 0;
2684}
2685
2686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002687/**
2688 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2689 * @global: Pointer to global data from wpa_supplicant_init()
2690 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2691 * Returns: 0 on success, -1 on failure
2692 */
2693int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2694{
2695 struct p2p_config p2p;
2696 unsigned int r;
2697 int i;
2698
2699 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2700 return 0;
2701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002702 if (global->p2p)
2703 return 0;
2704
2705 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2706 struct p2p_params params;
2707
2708 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2709 os_memset(&params, 0, sizeof(params));
2710 params.dev_name = wpa_s->conf->device_name;
2711 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2712 WPS_DEV_TYPE_LEN);
2713 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2714 os_memcpy(params.sec_dev_type,
2715 wpa_s->conf->sec_device_type,
2716 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2717
2718 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2719 return -1;
2720
2721 return 0;
2722 }
2723
2724 os_memset(&p2p, 0, sizeof(p2p));
2725 p2p.msg_ctx = wpa_s;
2726 p2p.cb_ctx = wpa_s;
2727 p2p.p2p_scan = wpas_p2p_scan;
2728 p2p.send_action = wpas_send_action;
2729 p2p.send_action_done = wpas_send_action_done;
2730 p2p.go_neg_completed = wpas_go_neg_completed;
2731 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2732 p2p.dev_found = wpas_dev_found;
2733 p2p.dev_lost = wpas_dev_lost;
2734 p2p.start_listen = wpas_start_listen;
2735 p2p.stop_listen = wpas_stop_listen;
2736 p2p.send_probe_resp = wpas_send_probe_resp;
2737 p2p.sd_request = wpas_sd_request;
2738 p2p.sd_response = wpas_sd_response;
2739 p2p.prov_disc_req = wpas_prov_disc_req;
2740 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002741 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742 p2p.invitation_process = wpas_invitation_process;
2743 p2p.invitation_received = wpas_invitation_received;
2744 p2p.invitation_result = wpas_invitation_result;
2745 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002746 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002747
2748 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002749 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002750 p2p.dev_name = wpa_s->conf->device_name;
2751 p2p.manufacturer = wpa_s->conf->manufacturer;
2752 p2p.model_name = wpa_s->conf->model_name;
2753 p2p.model_number = wpa_s->conf->model_number;
2754 p2p.serial_number = wpa_s->conf->serial_number;
2755 if (wpa_s->wps) {
2756 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2757 p2p.config_methods = wpa_s->wps->config_methods;
2758 }
2759
2760 if (wpa_s->conf->p2p_listen_reg_class &&
2761 wpa_s->conf->p2p_listen_channel) {
2762 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2763 p2p.channel = wpa_s->conf->p2p_listen_channel;
2764 } else {
2765 p2p.reg_class = 81;
2766 /*
2767 * Pick one of the social channels randomly as the listen
2768 * channel.
2769 */
2770 os_get_random((u8 *) &r, sizeof(r));
2771 p2p.channel = 1 + (r % 3) * 5;
2772 }
2773 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2774
2775 if (wpa_s->conf->p2p_oper_reg_class &&
2776 wpa_s->conf->p2p_oper_channel) {
2777 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2778 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2779 p2p.cfg_op_channel = 1;
2780 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2781 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2782
2783 } else {
2784 p2p.op_reg_class = 81;
2785 /*
2786 * Use random operation channel from (1, 6, 11) if no other
2787 * preference is indicated.
2788 */
2789 os_get_random((u8 *) &r, sizeof(r));
2790 p2p.op_channel = 1 + (r % 3) * 5;
2791 p2p.cfg_op_channel = 0;
2792 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2793 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2794 }
2795 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2796 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2797 p2p.country[2] = 0x04;
2798 } else
2799 os_memcpy(p2p.country, "XX\x04", 3);
2800
2801 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2802 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2803 "channel list");
2804 return -1;
2805 }
2806
2807 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2808 WPS_DEV_TYPE_LEN);
2809
2810 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2811 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2812 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2813
2814 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2815 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2816
2817 p2p.max_peers = 100;
2818
2819 if (wpa_s->conf->p2p_ssid_postfix) {
2820 p2p.ssid_postfix_len =
2821 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2822 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2823 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2824 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2825 p2p.ssid_postfix_len);
2826 }
2827
2828 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2829
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07002830#ifdef ANDROID_P2P
2831 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2832 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
2833 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)
2834 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
2835#endif
2836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837 global->p2p = p2p_init(&p2p);
2838 if (global->p2p == NULL)
2839 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002840 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841
2842 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2843 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2844 continue;
2845 p2p_add_wps_vendor_extension(
2846 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2847 }
2848
2849 return 0;
2850}
2851
2852
2853/**
2854 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2855 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2856 *
2857 * This function deinitialize per-interface P2P data.
2858 */
2859void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2860{
2861 if (wpa_s->driver && wpa_s->drv_priv)
2862 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002863
2864 if (wpa_s->go_params) {
2865 /* Clear any stored provisioning info */
2866 p2p_clear_provisioning_info(
2867 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002868 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002869 }
2870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871 os_free(wpa_s->go_params);
2872 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2874 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002875 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002876 wpa_s->p2p_long_listen = 0;
2877 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2878 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2879 wpas_p2p_remove_pending_group_interface(wpa_s);
2880
2881 /* TODO: remove group interface from the driver if this wpa_s instance
2882 * is on top of a P2P group interface */
2883}
2884
2885
2886/**
2887 * wpas_p2p_deinit_global - Deinitialize global P2P module
2888 * @global: Pointer to global data from wpa_supplicant_init()
2889 *
2890 * This function deinitializes the global (per device) P2P module.
2891 */
2892void wpas_p2p_deinit_global(struct wpa_global *global)
2893{
2894 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002896 wpa_s = global->ifaces;
2897 if (wpa_s)
2898 wpas_p2p_service_flush(wpa_s);
2899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900 if (global->p2p == NULL)
2901 return;
2902
2903 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2905 wpa_s = wpa_s->next;
2906 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002907 tmp = global->ifaces;
2908 while (tmp &&
2909 (tmp == wpa_s ||
2910 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2911 tmp = tmp->next;
2912 }
2913 if (tmp == NULL)
2914 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002915 /* Disconnect from the P2P group and deinit the interface */
2916 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002917 }
2918
2919 /*
2920 * Deinit GO data on any possibly remaining interface (if main
2921 * interface is used as GO).
2922 */
2923 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2924 if (wpa_s->ap_iface)
2925 wpas_p2p_group_deinit(wpa_s);
2926 }
2927
2928 p2p_deinit(global->p2p);
2929 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002930 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931}
2932
2933
2934static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2935{
2936 if (wpa_s->drv_flags &
2937 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2938 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2939 return 1; /* P2P group requires a new interface in every case
2940 */
2941 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2942 return 0; /* driver does not support concurrent operations */
2943 if (wpa_s->global->ifaces->next)
2944 return 1; /* more that one interface already in use */
2945 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2946 return 1; /* this interface is already in use */
2947 return 0;
2948}
2949
2950
2951static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2952 const u8 *peer_addr,
2953 enum p2p_wps_method wps_method,
2954 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002955 unsigned int force_freq, int persistent_group,
2956 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002957{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002958 if (persistent_group && wpa_s->conf->persistent_reconnect)
2959 persistent_group = 2;
2960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2962 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2963 go_intent, own_interface_addr,
2964 force_freq, persistent_group);
2965 }
2966
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002967 /*
2968 * Increase GO config timeout if HT40 is used since it takes some time
2969 * to scan channels for coex purposes before the BSS can be started.
2970 */
2971 p2p_set_config_timeout(wpa_s->global->p2p,
2972 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2975 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002976 persistent_group, ssid ? ssid->ssid : NULL,
2977 ssid ? ssid->ssid_len : 0,
2978 wpa_s->p2p_pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979}
2980
2981
2982static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2983 const u8 *peer_addr,
2984 enum p2p_wps_method wps_method,
2985 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002986 unsigned int force_freq, int persistent_group,
2987 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002989 if (persistent_group && wpa_s->conf->persistent_reconnect)
2990 persistent_group = 2;
2991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002992 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2993 return -1;
2994
2995 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2996 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002997 persistent_group, ssid ? ssid->ssid : NULL,
2998 ssid ? ssid->ssid_len : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999}
3000
3001
3002static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3003{
3004 wpa_s->p2p_join_scan_count++;
3005 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3006 wpa_s->p2p_join_scan_count);
3007 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3008 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3009 " for join operationg - stop join attempt",
3010 MAC2STR(wpa_s->pending_join_iface_addr));
3011 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003012 if (wpa_s->p2p_auto_pd) {
3013 wpa_s->p2p_auto_pd = 0;
3014 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3015 " p2p_dev_addr=" MACSTR " status=N/A",
3016 MAC2STR(wpa_s->pending_join_dev_addr));
3017 return;
3018 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003019 wpa_msg(wpa_s->parent, MSG_INFO,
3020 P2P_EVENT_GROUP_FORMATION_FAILURE);
3021 }
3022}
3023
3024
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003025static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
3026{
3027 struct wpa_supplicant *wpa_s = eloop_ctx;
3028 if (!wpa_s->pending_pd_before_join)
3029 return;
3030 /*
3031 * Provision Discovery Response may have been lost - try to connect
3032 * anyway since we do not need any information from this PD.
3033 */
3034 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
3035 "try to connect anyway");
3036 wpas_p2p_join_start(wpa_s);
3037}
3038
3039
Dmitry Shmidt04949592012-07-19 12:16:46 -07003040static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3041{
3042 struct wpa_supplicant *iface;
3043 int shared_freq;
3044 u8 bssid[ETH_ALEN];
3045
3046 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3047 return 0;
3048
3049 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3050 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3051 continue;
3052 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3053 continue;
3054 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3055 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3056 shared_freq = iface->current_ssid->frequency;
3057 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3058 shared_freq = iface->assoc_freq;
3059 else
3060 shared_freq = 0;
3061
3062 if (shared_freq && freq != shared_freq) {
3063 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3064 "connected on %d MHz - new connection on "
3065 "%d MHz", iface->ifname, shared_freq, freq);
3066 return 1;
3067 }
3068 }
3069
3070 shared_freq = wpa_drv_shared_freq(wpa_s);
3071 if (shared_freq > 0 && shared_freq != freq) {
3072 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3073 "virtual interface connected on %d MHz - new "
3074 "connection on %d MHz", shared_freq, freq);
3075 return 1;
3076 }
3077
3078 return 0;
3079}
3080
3081
3082static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3083 const u8 *peer_dev_addr)
3084{
3085 struct wpa_bss *bss;
3086 int updated;
3087
3088 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3089 if (bss == NULL)
3090 return -1;
3091 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3092 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3093 "last scan");
3094 return 0;
3095 }
3096
3097 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3098 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3099 "%ld.%06ld (%supdated in last scan)",
3100 bss->last_update.sec, bss->last_update.usec,
3101 updated ? "": "not ");
3102
3103 return updated;
3104}
3105
3106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003107static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3108 struct wpa_scan_results *scan_res)
3109{
3110 struct wpa_bss *bss;
3111 int freq;
3112 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3115
3116 if (wpa_s->global->p2p_disabled)
3117 return;
3118
Dmitry Shmidt04949592012-07-19 12:16:46 -07003119 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3120 scan_res ? (int) scan_res->num : -1,
3121 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122
3123 if (scan_res)
3124 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3125
Dmitry Shmidt04949592012-07-19 12:16:46 -07003126 if (wpa_s->p2p_auto_pd) {
3127 int join = wpas_p2p_peer_go(wpa_s,
3128 wpa_s->pending_join_dev_addr);
3129 if (join == 0 &&
3130 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3131 wpa_s->auto_pd_scan_retry++;
3132 bss = wpa_bss_get_bssid(wpa_s,
3133 wpa_s->pending_join_dev_addr);
3134 if (bss) {
3135 freq = bss->freq;
3136 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3137 "the peer " MACSTR " at %d MHz",
3138 wpa_s->auto_pd_scan_retry,
3139 MAC2STR(wpa_s->
3140 pending_join_dev_addr),
3141 freq);
3142 wpas_p2p_join_scan_req(wpa_s, freq);
3143 return;
3144 }
3145 }
3146
3147 if (join < 0)
3148 join = 0;
3149
3150 wpa_s->p2p_auto_pd = 0;
3151 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3152 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3153 MAC2STR(wpa_s->pending_join_dev_addr), join);
3154 if (p2p_prov_disc_req(wpa_s->global->p2p,
3155 wpa_s->pending_join_dev_addr,
3156 wpa_s->pending_pd_config_methods, join,
3157 0) < 0) {
3158 wpa_s->p2p_auto_pd = 0;
3159 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3160 " p2p_dev_addr=" MACSTR " status=N/A",
3161 MAC2STR(wpa_s->pending_join_dev_addr));
3162 }
3163 return;
3164 }
3165
3166 if (wpa_s->p2p_auto_join) {
3167 int join = wpas_p2p_peer_go(wpa_s,
3168 wpa_s->pending_join_dev_addr);
3169 if (join < 0) {
3170 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3171 "running a GO -> use GO Negotiation");
3172 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3173 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3174 wpa_s->p2p_persistent_group, 0, 0, 0,
3175 wpa_s->p2p_go_intent,
3176 wpa_s->p2p_connect_freq,
3177 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003178 wpa_s->p2p_pd_before_go_neg,
3179 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003180 return;
3181 }
3182
3183 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3184 "try to join the group", join ? "" :
3185 " in older scan");
3186 if (!join)
3187 wpa_s->p2p_fallback_to_go_neg = 1;
3188 }
3189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003190 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3191 wpa_s->pending_join_iface_addr);
3192 if (freq < 0 &&
3193 p2p_get_interface_addr(wpa_s->global->p2p,
3194 wpa_s->pending_join_dev_addr,
3195 iface_addr) == 0 &&
3196 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3197 {
3198 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3199 "address for join from " MACSTR " to " MACSTR
3200 " based on newly discovered P2P peer entry",
3201 MAC2STR(wpa_s->pending_join_iface_addr),
3202 MAC2STR(iface_addr));
3203 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3204 ETH_ALEN);
3205
3206 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3207 wpa_s->pending_join_iface_addr);
3208 }
3209 if (freq >= 0) {
3210 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3211 "from P2P peer table: %d MHz", freq);
3212 }
3213 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3214 if (bss) {
3215 freq = bss->freq;
3216 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3217 "from BSS table: %d MHz", freq);
3218 }
3219 if (freq > 0) {
3220 u16 method;
3221
Dmitry Shmidt04949592012-07-19 12:16:46 -07003222 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3223 wpa_msg(wpa_s->parent, MSG_INFO,
3224 P2P_EVENT_GROUP_FORMATION_FAILURE
3225 "reason=FREQ_CONFLICT");
3226 return;
3227 }
3228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3230 "prior to joining an existing group (GO " MACSTR
3231 " freq=%u MHz)",
3232 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3233 wpa_s->pending_pd_before_join = 1;
3234
3235 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003236 case WPS_PIN_DISPLAY:
3237 method = WPS_CONFIG_KEYPAD;
3238 break;
3239 case WPS_PIN_KEYPAD:
3240 method = WPS_CONFIG_DISPLAY;
3241 break;
3242 case WPS_PBC:
3243 method = WPS_CONFIG_PUSHBUTTON;
3244 break;
3245 default:
3246 method = 0;
3247 break;
3248 }
3249
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003250 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3251 wpa_s->pending_join_dev_addr) ==
3252 method)) {
3253 /*
3254 * We have already performed provision discovery for
3255 * joining the group. Proceed directly to join
3256 * operation without duplicated provision discovery. */
3257 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3258 "with " MACSTR " already done - proceed to "
3259 "join",
3260 MAC2STR(wpa_s->pending_join_dev_addr));
3261 wpa_s->pending_pd_before_join = 0;
3262 goto start;
3263 }
3264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003265 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003266 wpa_s->pending_join_dev_addr, method, 1,
3267 freq) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3269 "Discovery Request before joining an "
3270 "existing group");
3271 wpa_s->pending_pd_before_join = 0;
3272 goto start;
3273 }
3274
3275 /*
3276 * Actual join operation will be started from the Action frame
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003277 * TX status callback (if no ACK is received) or when the
3278 * Provision Discovery Response is received. Use a short
3279 * timeout as a backup mechanism should the Provision Discovery
3280 * Response be lost for any reason.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003281 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003282 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3283 NULL);
3284 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3285 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286 return;
3287 }
3288
3289 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3290 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3291 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3292 wpas_p2p_check_join_scan_limit(wpa_s);
3293 return;
3294
3295start:
3296 /* Start join operation immediately */
3297 wpas_p2p_join_start(wpa_s);
3298}
3299
3300
Dmitry Shmidt04949592012-07-19 12:16:46 -07003301static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303 int ret;
3304 struct wpa_driver_scan_params params;
3305 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003306 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003307 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003308#ifdef ANDROID_P2P
3309 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003311 /* If freq is not provided, check the operating freq of the GO and do a
3312 * a directed scan to save time
3313 */
3314 if(!freq) {
3315 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3316 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3317 }
3318#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003319 os_memset(&params, 0, sizeof(params));
3320
3321 /* P2P Wildcard SSID */
3322 params.num_ssids = 1;
3323 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3324 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3325
3326 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003327 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3328 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3329 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003330 if (wps_ie == NULL) {
3331 wpas_p2p_scan_res_join(wpa_s, NULL);
3332 return;
3333 }
3334
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003335 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3336 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337 if (ies == NULL) {
3338 wpabuf_free(wps_ie);
3339 wpas_p2p_scan_res_join(wpa_s, NULL);
3340 return;
3341 }
3342 wpabuf_put_buf(ies, wps_ie);
3343 wpabuf_free(wps_ie);
3344
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003345 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003346
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003347 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003348 params.extra_ies = wpabuf_head(ies);
3349 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003350 if (freq > 0) {
3351 freqs[0] = freq;
3352 params.freqs = freqs;
3353 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003354
3355 /*
3356 * Run a scan to update BSS table and start Provision Discovery once
3357 * the new scan results become available.
3358 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003359 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003360 if (!ret)
3361 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003362
3363 wpabuf_free(ies);
3364
3365 if (ret) {
3366 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3367 "try again later");
3368 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3369 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3370 wpas_p2p_check_join_scan_limit(wpa_s);
3371 }
3372}
3373
3374
Dmitry Shmidt04949592012-07-19 12:16:46 -07003375static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3376{
3377 struct wpa_supplicant *wpa_s = eloop_ctx;
3378 wpas_p2p_join_scan_req(wpa_s, 0);
3379}
3380
3381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003383 const u8 *dev_addr, enum p2p_wps_method wps_method,
3384 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385{
3386 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003387 MACSTR " dev " MACSTR ")%s",
3388 MAC2STR(iface_addr), MAC2STR(dev_addr),
3389 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390
Dmitry Shmidt04949592012-07-19 12:16:46 -07003391 wpa_s->p2p_auto_pd = 0;
3392 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3394 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3395 wpa_s->pending_join_wps_method = wps_method;
3396
3397 /* Make sure we are not running find during connection establishment */
3398 wpas_p2p_stop_find(wpa_s);
3399
3400 wpa_s->p2p_join_scan_count = 0;
3401 wpas_p2p_join_scan(wpa_s, NULL);
3402 return 0;
3403}
3404
3405
3406static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3407{
3408 struct wpa_supplicant *group;
3409 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003410 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003411
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003412 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3414 if (group == NULL)
3415 return -1;
3416 if (group != wpa_s) {
3417 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3418 sizeof(group->p2p_pin));
3419 group->p2p_wps_method = wpa_s->p2p_wps_method;
3420 }
3421
3422 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003423 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003424
3425 os_memset(&res, 0, sizeof(res));
3426 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3427 ETH_ALEN);
3428 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003429 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3430 if (bss) {
3431 res.freq = bss->freq;
3432 res.ssid_len = bss->ssid_len;
3433 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3434 }
3435
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003436 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3437 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3438 "starting client");
3439 wpa_drv_cancel_remain_on_channel(wpa_s);
3440 wpa_s->off_channel_freq = 0;
3441 wpa_s->roc_waiting_drv_freq = 0;
3442 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003443 wpas_start_wps_enrollee(group, &res);
3444
3445 /*
3446 * Allow a longer timeout for join-a-running-group than normal 15
3447 * second group formation timeout since the GO may not have authorized
3448 * our connection yet.
3449 */
3450 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3451 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3452 wpa_s, NULL);
3453
3454 return 0;
3455}
3456
3457
3458/**
3459 * wpas_p2p_connect - Request P2P Group Formation to be started
3460 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3461 * @peer_addr: Address of the peer P2P Device
3462 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3463 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003464 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465 * @join: Whether to join an existing group (as a client) instead of starting
3466 * Group Owner negotiation; @peer_addr is BSSID in that case
3467 * @auth: Whether to only authorize the connection instead of doing that and
3468 * initiating Group Owner negotiation
3469 * @go_intent: GO Intent or -1 to use default
3470 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003471 * @persistent_id: Persistent group credentials to use for forcing GO
3472 * parameters or -1 to generate new values (SSID/passphrase)
3473 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3474 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003475 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003476 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3477 * failure, -2 on failure due to channel not currently available,
3478 * -3 if forced channel is not supported
3479 */
3480int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3481 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003482 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003483 int go_intent, int freq, int persistent_id, int pd,
3484 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485{
3486 int force_freq = 0, oper_freq = 0;
3487 u8 bssid[ETH_ALEN];
3488 int ret = 0;
3489 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003490 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003491 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003492
3493 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3494 return -1;
3495
Dmitry Shmidt04949592012-07-19 12:16:46 -07003496 if (persistent_id >= 0) {
3497 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3498 if (ssid == NULL || ssid->disabled != 2 ||
3499 ssid->mode != WPAS_MODE_P2P_GO)
3500 return -1;
3501 }
3502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003503 if (go_intent < 0)
3504 go_intent = wpa_s->conf->p2p_go_intent;
3505
3506 if (!auth)
3507 wpa_s->p2p_long_listen = 0;
3508
3509 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003510 wpa_s->p2p_persistent_group = !!persistent_group;
3511 wpa_s->p2p_persistent_id = persistent_id;
3512 wpa_s->p2p_go_intent = go_intent;
3513 wpa_s->p2p_connect_freq = freq;
3514 wpa_s->p2p_fallback_to_go_neg = 0;
3515 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003516 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003517
3518 if (pin)
3519 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3520 else if (wps_method == WPS_PIN_DISPLAY) {
3521 ret = wps_generate_pin();
3522 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3523 ret);
3524 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3525 wpa_s->p2p_pin);
3526 } else
3527 wpa_s->p2p_pin[0] = '\0';
3528
Dmitry Shmidt04949592012-07-19 12:16:46 -07003529 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003530 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3531 if (auth) {
3532 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3533 "connect a running group from " MACSTR,
3534 MAC2STR(peer_addr));
3535 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3536 return ret;
3537 }
3538 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3539 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3540 iface_addr) < 0) {
3541 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3542 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3543 dev_addr);
3544 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003545 if (auto_join) {
3546 os_get_time(&wpa_s->p2p_auto_started);
3547 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3548 "%ld.%06ld",
3549 wpa_s->p2p_auto_started.sec,
3550 wpa_s->p2p_auto_started.usec);
3551 }
3552 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3553 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554 return -1;
3555 return ret;
3556 }
3557
3558 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3559 wpa_s->assoc_freq)
3560 oper_freq = wpa_s->assoc_freq;
3561 else {
3562 oper_freq = wpa_drv_shared_freq(wpa_s);
3563 if (oper_freq < 0)
3564 oper_freq = 0;
3565 }
3566
3567 if (freq > 0) {
3568 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3569 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3570 "(%u MHz) is not supported for P2P uses",
3571 freq);
3572 return -3;
3573 }
3574
3575 if (oper_freq > 0 && freq != oper_freq &&
3576 !(wpa_s->drv_flags &
3577 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3578 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3579 "on %u MHz while connected on another "
3580 "channel (%u MHz)", freq, oper_freq);
3581 return -2;
3582 }
3583 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3584 "requested channel (%u MHz)", freq);
3585 force_freq = freq;
3586 } else if (oper_freq > 0 &&
3587 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3588 if (!(wpa_s->drv_flags &
3589 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3590 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3591 "while connected on non-P2P supported "
3592 "channel (%u MHz)", oper_freq);
3593 return -2;
3594 }
3595 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3596 "(%u MHz) not available for P2P - try to use "
3597 "another channel", oper_freq);
3598 force_freq = 0;
3599 } else if (oper_freq > 0) {
3600 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3601 "channel we are already using (%u MHz) on another "
3602 "interface", oper_freq);
3603 force_freq = oper_freq;
3604 }
3605
3606 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003608 if (wpa_s->create_p2p_iface) {
3609 /* Prepare to add a new interface for the group */
3610 iftype = WPA_IF_P2P_GROUP;
3611 if (go_intent == 15)
3612 iftype = WPA_IF_P2P_GO;
3613 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3614 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3615 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003619 if_addr = wpa_s->pending_interface_addr;
3620 } else
3621 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622
3623 if (auth) {
3624 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003625 go_intent, if_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003626 force_freq, persistent_group, ssid) <
3627 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628 return -1;
3629 return ret;
3630 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003631
3632 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3633 go_intent, if_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003634 persistent_group, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003635 if (wpa_s->create_p2p_iface)
3636 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 return -1;
3638 }
3639 return ret;
3640}
3641
3642
3643/**
3644 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3645 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3646 * @freq: Frequency of the channel in MHz
3647 * @duration: Duration of the stay on the channel in milliseconds
3648 *
3649 * This callback is called when the driver indicates that it has started the
3650 * requested remain-on-channel duration.
3651 */
3652void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3653 unsigned int freq, unsigned int duration)
3654{
3655 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3656 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003657 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3658 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3659 wpa_s->pending_listen_duration);
3660 wpa_s->pending_listen_freq = 0;
3661 }
3662}
3663
3664
3665static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3666 unsigned int timeout)
3667{
3668 /* Limit maximum Listen state time based on driver limitation. */
3669 if (timeout > wpa_s->max_remain_on_chan)
3670 timeout = wpa_s->max_remain_on_chan;
3671
3672 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3673 return wpa_drv_p2p_listen(wpa_s, timeout);
3674
3675 return p2p_listen(wpa_s->global->p2p, timeout);
3676}
3677
3678
3679/**
3680 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3681 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3682 * @freq: Frequency of the channel in MHz
3683 *
3684 * This callback is called when the driver indicates that a remain-on-channel
3685 * operation has been completed, i.e., the duration on the requested channel
3686 * has timed out.
3687 */
3688void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3689 unsigned int freq)
3690{
3691 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3692 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3693 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3695 return;
3696 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3697 return; /* P2P module started a new operation */
3698 if (wpa_s->pending_action_tx)
3699 return;
3700 if (wpa_s->p2p_long_listen > 0)
3701 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3702 if (wpa_s->p2p_long_listen > 0) {
3703 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3704 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3705 }
3706}
3707
3708
3709/**
3710 * wpas_p2p_group_remove - Remove a P2P group
3711 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3712 * @ifname: Network interface name of the group interface or "*" to remove all
3713 * groups
3714 * Returns: 0 on success, -1 on failure
3715 *
3716 * This function is used to remove a P2P group. This can be used to disconnect
3717 * from a group in which the local end is a P2P Client or to end a P2P Group in
3718 * case the local end is the Group Owner. If a virtual network interface was
3719 * created for this group, that interface will be removed. Otherwise, only the
3720 * configured P2P group network will be removed from the interface.
3721 */
3722int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3723{
3724 struct wpa_global *global = wpa_s->global;
3725
3726 if (os_strcmp(ifname, "*") == 0) {
3727 struct wpa_supplicant *prev;
3728 wpa_s = global->ifaces;
3729 while (wpa_s) {
3730 prev = wpa_s;
3731 wpa_s = wpa_s->next;
3732 wpas_p2p_disconnect(prev);
3733 }
3734 return 0;
3735 }
3736
3737 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3738 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3739 break;
3740 }
3741
3742 return wpas_p2p_disconnect(wpa_s);
3743}
3744
3745
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003746static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3747 struct p2p_go_neg_results *params,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003748 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003749{
3750 u8 bssid[ETH_ALEN];
3751 int res;
3752
3753 os_memset(params, 0, sizeof(*params));
3754 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003755 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003756 if (freq) {
3757 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3758 "frequency %d MHz", freq);
3759 params->freq = freq;
3760 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3761 wpa_s->conf->p2p_oper_channel >= 1 &&
3762 wpa_s->conf->p2p_oper_channel <= 11) {
3763 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3764 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3765 "frequency %d MHz", params->freq);
3766 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003767 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003768 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3769 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3770 "frequency %d MHz", params->freq);
3771 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3772 wpa_s->best_overall_freq > 0 &&
3773 p2p_supported_freq(wpa_s->global->p2p,
3774 wpa_s->best_overall_freq)) {
3775 params->freq = wpa_s->best_overall_freq;
3776 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3777 "channel %d MHz", params->freq);
3778 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3779 wpa_s->best_24_freq > 0 &&
3780 p2p_supported_freq(wpa_s->global->p2p,
3781 wpa_s->best_24_freq)) {
3782 params->freq = wpa_s->best_24_freq;
3783 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3784 "channel %d MHz", params->freq);
3785 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3786 wpa_s->best_5_freq > 0 &&
3787 p2p_supported_freq(wpa_s->global->p2p,
3788 wpa_s->best_5_freq)) {
3789 params->freq = wpa_s->best_5_freq;
3790 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3791 "channel %d MHz", params->freq);
3792 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003793 int chan;
3794 for (chan = 0; chan < 11; chan++) {
3795 params->freq = 2412 + chan * 5;
3796 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3797 params->freq))
3798 break;
3799 }
3800 if (chan == 11) {
3801 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3802 "allowed");
3803 return -1;
3804 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003805 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3806 "known)", params->freq);
3807 }
3808
3809 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3810 wpa_s->assoc_freq && !freq) {
3811 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3812 "already using");
3813 params->freq = wpa_s->assoc_freq;
3814 }
3815
3816 res = wpa_drv_shared_freq(wpa_s);
3817 if (res > 0 && !freq) {
3818 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3819 "already using on a shared interface");
3820 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003821 } else if (res > 0 && freq != res &&
3822 !(wpa_s->drv_flags &
3823 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3824 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3825 "while connected on another channel (%u MHz)",
3826 freq, res);
3827 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003828 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003829
3830 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003831}
3832
3833
3834static struct wpa_supplicant *
3835wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3836 int go)
3837{
3838 struct wpa_supplicant *group_wpa_s;
3839
3840 if (!wpas_p2p_create_iface(wpa_s))
3841 return wpa_s;
3842
3843 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3844 WPA_IF_P2P_CLIENT) < 0)
3845 return NULL;
3846 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3847 if (group_wpa_s == NULL) {
3848 wpas_p2p_remove_pending_group_interface(wpa_s);
3849 return NULL;
3850 }
3851
3852 return group_wpa_s;
3853}
3854
3855
3856/**
3857 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3858 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3859 * @persistent_group: Whether to create a persistent group
3860 * @freq: Frequency for the group or 0 to indicate no hardcoding
3861 * Returns: 0 on success, -1 on failure
3862 *
3863 * This function creates a new P2P group with the local end as the Group Owner,
3864 * i.e., without using Group Owner Negotiation.
3865 */
3866int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003867 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868{
3869 struct p2p_go_neg_results params;
3870 unsigned int r;
3871
3872 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3873 return -1;
3874
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003875 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003876 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003877 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 if (freq == 2) {
3880 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3881 "band");
3882 if (wpa_s->best_24_freq > 0 &&
3883 p2p_supported_freq(wpa_s->global->p2p,
3884 wpa_s->best_24_freq)) {
3885 freq = wpa_s->best_24_freq;
3886 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3887 "channel: %d MHz", freq);
3888 } else {
3889 os_get_random((u8 *) &r, sizeof(r));
3890 freq = 2412 + (r % 3) * 25;
3891 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3892 "channel: %d MHz", freq);
3893 }
3894 }
3895
3896 if (freq == 5) {
3897 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3898 "band");
3899 if (wpa_s->best_5_freq > 0 &&
3900 p2p_supported_freq(wpa_s->global->p2p,
3901 wpa_s->best_5_freq)) {
3902 freq = wpa_s->best_5_freq;
3903 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3904 "channel: %d MHz", freq);
3905 } else {
3906 os_get_random((u8 *) &r, sizeof(r));
3907 freq = 5180 + (r % 4) * 20;
3908 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3909 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3910 "5 GHz channel for P2P group");
3911 return -1;
3912 }
3913 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3914 "channel: %d MHz", freq);
3915 }
3916 }
3917
3918 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3919 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3920 "(%u MHz) is not supported for P2P uses",
3921 freq);
3922 return -1;
3923 }
3924
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003925 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003926 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003927 if (params.freq &&
3928 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3929 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3930 "(%u MHz) is not supported for P2P uses",
3931 params.freq);
3932 return -1;
3933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003934 p2p_go_params(wpa_s->global->p2p, &params);
3935 params.persistent_group = persistent_group;
3936
3937 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3938 if (wpa_s == NULL)
3939 return -1;
3940 wpas_start_wps_go(wpa_s, &params, 0);
3941
3942 return 0;
3943}
3944
3945
3946static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3947 struct wpa_ssid *params, int addr_allocated)
3948{
3949 struct wpa_ssid *ssid;
3950
3951 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3952 if (wpa_s == NULL)
3953 return -1;
3954
3955 wpa_supplicant_ap_deinit(wpa_s);
3956
3957 ssid = wpa_config_add_network(wpa_s->conf);
3958 if (ssid == NULL)
3959 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960 wpa_config_set_network_defaults(ssid);
3961 ssid->temporary = 1;
3962 ssid->proto = WPA_PROTO_RSN;
3963 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3964 ssid->group_cipher = WPA_CIPHER_CCMP;
3965 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3966 ssid->ssid = os_malloc(params->ssid_len);
3967 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 wpa_config_remove_network(wpa_s->conf, ssid->id);
3969 return -1;
3970 }
3971 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3972 ssid->ssid_len = params->ssid_len;
3973 ssid->p2p_group = 1;
3974 ssid->export_keys = 1;
3975 if (params->psk_set) {
3976 os_memcpy(ssid->psk, params->psk, 32);
3977 ssid->psk_set = 1;
3978 }
3979 if (params->passphrase)
3980 ssid->passphrase = os_strdup(params->passphrase);
3981
3982 wpa_supplicant_select_network(wpa_s, ssid);
3983
3984 wpa_s->show_group_started = 1;
3985
3986 return 0;
3987}
3988
3989
3990int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3991 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003992 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993{
3994 struct p2p_go_neg_results params;
3995 int go = 0;
3996
3997 if (ssid->disabled != 2 || ssid->ssid == NULL)
3998 return -1;
3999
4000 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4001 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4002 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4003 "already running");
4004 return 0;
4005 }
4006
4007 /* Make sure we are not running find during connection establishment */
4008 wpas_p2p_stop_find(wpa_s);
4009
Dmitry Shmidt04949592012-07-19 12:16:46 -07004010 wpa_s->p2p_fallback_to_go_neg = 0;
4011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012 if (ssid->mode == WPAS_MODE_INFRA)
4013 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4014
4015 if (ssid->mode != WPAS_MODE_P2P_GO)
4016 return -1;
4017
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004018 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004019 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020
4021 params.role_go = 1;
4022 if (ssid->passphrase == NULL ||
4023 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4024 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4025 "group");
4026 return -1;
4027 }
4028 os_strlcpy(params.passphrase, ssid->passphrase,
4029 sizeof(params.passphrase));
4030 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4031 params.ssid_len = ssid->ssid_len;
4032 params.persistent_group = 1;
4033
4034 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4035 if (wpa_s == NULL)
4036 return -1;
4037
4038 wpas_start_wps_go(wpa_s, &params, 0);
4039
4040 return 0;
4041}
4042
4043
4044static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4045 struct wpabuf *proberesp_ies)
4046{
4047 struct wpa_supplicant *wpa_s = ctx;
4048 if (wpa_s->ap_iface) {
4049 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004050 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4051 wpabuf_free(beacon_ies);
4052 wpabuf_free(proberesp_ies);
4053 return;
4054 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 if (beacon_ies) {
4056 wpabuf_free(hapd->p2p_beacon_ie);
4057 hapd->p2p_beacon_ie = beacon_ies;
4058 }
4059 wpabuf_free(hapd->p2p_probe_resp_ie);
4060 hapd->p2p_probe_resp_ie = proberesp_ies;
4061 } else {
4062 wpabuf_free(beacon_ies);
4063 wpabuf_free(proberesp_ies);
4064 }
4065 wpa_supplicant_ap_update_beacon(wpa_s);
4066}
4067
4068
4069static void wpas_p2p_idle_update(void *ctx, int idle)
4070{
4071 struct wpa_supplicant *wpa_s = ctx;
4072 if (!wpa_s->ap_iface)
4073 return;
4074 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004075 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076 wpas_p2p_set_group_idle_timeout(wpa_s);
4077 else
4078 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4079}
4080
4081
4082struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004083 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004084{
4085 struct p2p_group *group;
4086 struct p2p_group_config *cfg;
4087
4088 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4089 return NULL;
4090 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4091 return NULL;
4092
4093 cfg = os_zalloc(sizeof(*cfg));
4094 if (cfg == NULL)
4095 return NULL;
4096
Dmitry Shmidt04949592012-07-19 12:16:46 -07004097 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004098 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004099 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004100 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004101 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4102 if (wpa_s->max_stations &&
4103 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4104 cfg->max_clients = wpa_s->max_stations;
4105 else
4106 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004107 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4108 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004109 cfg->cb_ctx = wpa_s;
4110 cfg->ie_update = wpas_p2p_ie_update;
4111 cfg->idle_update = wpas_p2p_idle_update;
4112
4113 group = p2p_group_init(wpa_s->global->p2p, cfg);
4114 if (group == NULL)
4115 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004116 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117 p2p_group_notif_formation_done(group);
4118 wpa_s->p2p_group = group;
4119 return group;
4120}
4121
4122
4123void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4124 int registrar)
4125{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004126 struct wpa_ssid *ssid = wpa_s->current_ssid;
4127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004128 if (!wpa_s->p2p_in_provisioning) {
4129 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4130 "provisioning not in progress");
4131 return;
4132 }
4133
Dmitry Shmidt04949592012-07-19 12:16:46 -07004134 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4135 u8 go_dev_addr[ETH_ALEN];
4136 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4137 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4138 ssid->ssid_len);
4139 /* Clear any stored provisioning info */
4140 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4141 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4144 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004145 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4146 /*
4147 * Use a separate timeout for initial data connection to
4148 * complete to allow the group to be removed automatically if
4149 * something goes wrong in this step before the P2P group idle
4150 * timeout mechanism is taken into use.
4151 */
4152 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4153 wpas_p2p_group_formation_timeout,
4154 wpa_s->parent, NULL);
4155 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004156 if (wpa_s->global->p2p)
4157 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4158 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4159 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4160 wpas_group_formation_completed(wpa_s, 1);
4161}
4162
4163
Jouni Malinen75ecf522011-06-27 15:19:46 -07004164void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4165 struct wps_event_fail *fail)
4166{
4167 if (!wpa_s->p2p_in_provisioning) {
4168 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4169 "provisioning not in progress");
4170 return;
4171 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004172
4173 if (wpa_s->go_params) {
4174 p2p_clear_provisioning_info(
4175 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004176 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004177 }
4178
Jouni Malinen75ecf522011-06-27 15:19:46 -07004179 wpas_notify_p2p_wps_failed(wpa_s, fail);
4180}
4181
4182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004183int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004184 const char *config_method,
4185 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004186{
4187 u16 config_methods;
4188
Dmitry Shmidt04949592012-07-19 12:16:46 -07004189 wpa_s->p2p_fallback_to_go_neg = 0;
4190 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004191 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004192 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004193 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004195 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4196 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004198 else {
4199 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004200 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004201 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004202
Dmitry Shmidt04949592012-07-19 12:16:46 -07004203 if (use == WPAS_P2P_PD_AUTO) {
4204 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4205 wpa_s->pending_pd_config_methods = config_methods;
4206 wpa_s->p2p_auto_pd = 1;
4207 wpa_s->p2p_auto_join = 0;
4208 wpa_s->pending_pd_before_join = 0;
4209 wpa_s->auto_pd_scan_retry = 0;
4210 wpas_p2p_stop_find(wpa_s);
4211 wpa_s->p2p_join_scan_count = 0;
4212 os_get_time(&wpa_s->p2p_auto_started);
4213 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4214 wpa_s->p2p_auto_started.sec,
4215 wpa_s->p2p_auto_started.usec);
4216 wpas_p2p_join_scan(wpa_s, NULL);
4217 return 0;
4218 }
4219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4221 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004222 config_methods,
4223 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004224 }
4225
4226 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4227 return -1;
4228
4229 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004230 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4231 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232}
4233
4234
4235int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4236 char *end)
4237{
4238 return p2p_scan_result_text(ies, ies_len, buf, end);
4239}
4240
4241
4242static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4243{
4244 if (!wpa_s->pending_action_tx)
4245 return;
4246
4247 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4248 "operation request");
4249 wpabuf_free(wpa_s->pending_action_tx);
4250 wpa_s->pending_action_tx = NULL;
4251}
4252
4253
4254int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4255 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004256 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004257 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004258{
4259 wpas_p2p_clear_pending_action_tx(wpa_s);
4260 wpa_s->p2p_long_listen = 0;
4261
4262 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4263 return wpa_drv_p2p_find(wpa_s, timeout, type);
4264
Dmitry Shmidt04949592012-07-19 12:16:46 -07004265 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4266 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004267 return -1;
4268
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004269 wpa_supplicant_cancel_sched_scan(wpa_s);
4270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004271 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004272 num_req_dev_types, req_dev_types, dev_id,
4273 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004274}
4275
4276
4277void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4278{
4279 wpas_p2p_clear_pending_action_tx(wpa_s);
4280 wpa_s->p2p_long_listen = 0;
4281 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4282 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004283 wpa_s->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004284
4285 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4286 wpa_drv_p2p_stop_find(wpa_s);
4287 return;
4288 }
4289
4290 if (wpa_s->global->p2p)
4291 p2p_stop_find(wpa_s->global->p2p);
4292
4293 wpas_p2p_remove_pending_group_interface(wpa_s);
4294}
4295
4296
4297static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4298{
4299 struct wpa_supplicant *wpa_s = eloop_ctx;
4300 wpa_s->p2p_long_listen = 0;
4301}
4302
4303
4304int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4305{
4306 int res;
4307
4308 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4309 return -1;
4310
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004311 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312 wpas_p2p_clear_pending_action_tx(wpa_s);
4313
4314 if (timeout == 0) {
4315 /*
4316 * This is a request for unlimited Listen state. However, at
4317 * least for now, this is mapped to a Listen state for one
4318 * hour.
4319 */
4320 timeout = 3600;
4321 }
4322 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4323 wpa_s->p2p_long_listen = 0;
4324
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004325 /*
4326 * Stop previous find/listen operation to avoid trying to request a new
4327 * remain-on-channel operation while the driver is still running the
4328 * previous one.
4329 */
4330 if (wpa_s->global->p2p)
4331 p2p_stop_find(wpa_s->global->p2p);
4332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004333 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4334 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4335 wpa_s->p2p_long_listen = timeout * 1000;
4336 eloop_register_timeout(timeout, 0,
4337 wpas_p2p_long_listen_timeout,
4338 wpa_s, NULL);
4339 }
4340
4341 return res;
4342}
4343
4344
4345int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4346 u8 *buf, size_t len, int p2p_group)
4347{
4348 struct wpabuf *p2p_ie;
4349 int ret;
4350
4351 if (wpa_s->global->p2p_disabled)
4352 return -1;
4353 if (wpa_s->global->p2p == NULL)
4354 return -1;
4355 if (bss == NULL)
4356 return -1;
4357
4358 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4359 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4360 p2p_group, p2p_ie);
4361 wpabuf_free(p2p_ie);
4362
4363 return ret;
4364}
4365
4366
4367int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004368 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004369 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370{
4371 if (wpa_s->global->p2p_disabled)
4372 return 0;
4373 if (wpa_s->global->p2p == NULL)
4374 return 0;
4375
Dmitry Shmidt04949592012-07-19 12:16:46 -07004376 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4377 ie, ie_len)) {
4378 case P2P_PREQ_NOT_P2P:
4379 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4380 ssi_signal);
4381 /* fall through */
4382 case P2P_PREQ_MALFORMED:
4383 case P2P_PREQ_NOT_LISTEN:
4384 case P2P_PREQ_NOT_PROCESSED:
4385 default: /* make gcc happy */
4386 return 0;
4387 case P2P_PREQ_PROCESSED:
4388 return 1;
4389 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004390}
4391
4392
4393void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4394 const u8 *sa, const u8 *bssid,
4395 u8 category, const u8 *data, size_t len, int freq)
4396{
4397 if (wpa_s->global->p2p_disabled)
4398 return;
4399 if (wpa_s->global->p2p == NULL)
4400 return;
4401
4402 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4403 freq);
4404}
4405
4406
4407void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4408{
4409 if (wpa_s->global->p2p_disabled)
4410 return;
4411 if (wpa_s->global->p2p == NULL)
4412 return;
4413
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004414 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004415}
4416
4417
4418void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4419{
4420 p2p_group_deinit(wpa_s->p2p_group);
4421 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004422
4423 wpa_s->ap_configured_cb = NULL;
4424 wpa_s->ap_configured_cb_ctx = NULL;
4425 wpa_s->ap_configured_cb_data = NULL;
4426 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004427}
4428
4429
4430int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4431{
4432 wpa_s->p2p_long_listen = 0;
4433
4434 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4435 return wpa_drv_p2p_reject(wpa_s, addr);
4436
4437 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4438 return -1;
4439
4440 return p2p_reject(wpa_s->global->p2p, addr);
4441}
4442
4443
4444/* Invite to reinvoke a persistent group */
4445int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004446 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4447 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448{
4449 enum p2p_invite_role role;
4450 u8 *bssid = NULL;
4451
Jouni Malinen31be0a42012-08-31 21:20:51 +03004452 wpa_s->p2p_persistent_go_freq = freq;
4453 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454 if (ssid->mode == WPAS_MODE_P2P_GO) {
4455 role = P2P_INVITE_ROLE_GO;
4456 if (peer_addr == NULL) {
4457 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4458 "address in invitation command");
4459 return -1;
4460 }
4461 if (wpas_p2p_create_iface(wpa_s)) {
4462 if (wpas_p2p_add_group_interface(wpa_s,
4463 WPA_IF_P2P_GO) < 0) {
4464 wpa_printf(MSG_ERROR, "P2P: Failed to "
4465 "allocate a new interface for the "
4466 "group");
4467 return -1;
4468 }
4469 bssid = wpa_s->pending_interface_addr;
4470 } else
4471 bssid = wpa_s->own_addr;
4472 } else {
4473 role = P2P_INVITE_ROLE_CLIENT;
4474 peer_addr = ssid->bssid;
4475 }
4476 wpa_s->pending_invite_ssid_id = ssid->id;
4477
4478 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4479 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4480 ssid->ssid, ssid->ssid_len,
4481 go_dev_addr, 1);
4482
4483 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4484 return -1;
4485
4486 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004487 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004488}
4489
4490
4491/* Invite to join an active group */
4492int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4493 const u8 *peer_addr, const u8 *go_dev_addr)
4494{
4495 struct wpa_global *global = wpa_s->global;
4496 enum p2p_invite_role role;
4497 u8 *bssid = NULL;
4498 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004499 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004500
Jouni Malinen31be0a42012-08-31 21:20:51 +03004501 wpa_s->p2p_persistent_go_freq = 0;
4502 wpa_s->p2p_go_ht40 = 0;
4503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4505 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4506 break;
4507 }
4508 if (wpa_s == NULL) {
4509 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4510 return -1;
4511 }
4512
4513 ssid = wpa_s->current_ssid;
4514 if (ssid == NULL) {
4515 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4516 "invitation");
4517 return -1;
4518 }
4519
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004520 persistent = ssid->p2p_persistent_group &&
4521 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4522 ssid->ssid, ssid->ssid_len);
4523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004524 if (ssid->mode == WPAS_MODE_P2P_GO) {
4525 role = P2P_INVITE_ROLE_ACTIVE_GO;
4526 bssid = wpa_s->own_addr;
4527 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004528 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004529 } else {
4530 role = P2P_INVITE_ROLE_CLIENT;
4531 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4532 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4533 "invite to current group");
4534 return -1;
4535 }
4536 bssid = wpa_s->bssid;
4537 if (go_dev_addr == NULL &&
4538 !is_zero_ether_addr(wpa_s->go_dev_addr))
4539 go_dev_addr = wpa_s->go_dev_addr;
4540 }
4541 wpa_s->parent->pending_invite_ssid_id = -1;
4542
4543 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4544 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4545 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004546 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547
4548 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4549 return -1;
4550
4551 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4552 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004553 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004554}
4555
4556
4557void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4558{
4559 struct wpa_ssid *ssid = wpa_s->current_ssid;
4560 const char *ssid_txt;
4561 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004562 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004564 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565
Dmitry Shmidt04949592012-07-19 12:16:46 -07004566 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4567 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4568 wpa_s->parent, NULL);
4569 }
4570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004572 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573
4574 wpa_s->show_group_started = 0;
4575
4576 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4577 os_memset(go_dev_addr, 0, ETH_ALEN);
4578 if (ssid->bssid_set)
4579 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4580 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4581 ssid->ssid_len);
4582 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4583
4584 if (wpa_s->global->p2p_group_formation == wpa_s)
4585 wpa_s->global->p2p_group_formation = NULL;
4586
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004587 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4588 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589 if (ssid->passphrase == NULL && ssid->psk_set) {
4590 char psk[65];
4591 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4592 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4593 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4594 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004595 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004596 MAC2STR(go_dev_addr),
4597 persistent ? " [PERSISTENT]" : "");
4598 } else {
4599 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4600 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4601 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004602 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004603 ssid->passphrase ? ssid->passphrase : "",
4604 MAC2STR(go_dev_addr),
4605 persistent ? " [PERSISTENT]" : "");
4606 }
4607
4608 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004609 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4610 ssid, go_dev_addr);
4611 if (network_id < 0)
4612 network_id = ssid->id;
4613 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004614
4615done:
4616 if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4617 wpa_s->global->p2p != NULL) {
4618 wpa_s->p2p_cb_on_scan_complete = 0;
4619 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4620 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4621 "continued after successful connection");
4622 p2p_increase_search_delay(
4623 wpa_s->global->p2p,
4624 wpas_p2p_search_delay(wpa_s));
4625 }
4626 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004627}
4628
4629
4630int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4631 u32 interval1, u32 duration2, u32 interval2)
4632{
4633 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4634 return -1;
4635 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4636 return -1;
4637
4638 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4639 wpa_s->current_ssid == NULL ||
4640 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4641 return -1;
4642
4643 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4644 wpa_s->own_addr, wpa_s->assoc_freq,
4645 duration1, interval1, duration2, interval2);
4646}
4647
4648
4649int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4650 unsigned int interval)
4651{
4652 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4653 return -1;
4654
4655 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4656 return -1;
4657
4658 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4659}
4660
4661
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004662static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4663{
4664 return wpa_s->current_ssid != NULL &&
4665 wpa_s->current_ssid->p2p_group &&
4666 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4667}
4668
4669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4671{
4672 struct wpa_supplicant *wpa_s = eloop_ctx;
4673
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004674 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004675 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4676 "disabled");
4677 return;
4678 }
4679
Dmitry Shmidt04949592012-07-19 12:16:46 -07004680 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4681 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004682 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683}
4684
4685
4686static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4687{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004688 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004689
Dmitry Shmidt04949592012-07-19 12:16:46 -07004690 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4691 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004693 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4694 return;
4695
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004696 timeout = wpa_s->conf->p2p_group_idle;
4697 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4698 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4699 timeout = P2P_MAX_CLIENT_IDLE;
4700
4701 if (timeout == 0)
4702 return;
4703
Dmitry Shmidt04949592012-07-19 12:16:46 -07004704 if (timeout < 0) {
4705 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4706 timeout = 0; /* special client mode no-timeout */
4707 else
4708 return;
4709 }
4710
4711 if (wpa_s->p2p_in_provisioning) {
4712 /*
4713 * Use the normal group formation timeout during the
4714 * provisioning phase to avoid terminating this process too
4715 * early due to group idle timeout.
4716 */
4717 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4718 "during provisioning");
4719 return;
4720 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004721#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07004722 if (wpa_s->show_group_started) {
4723 /*
4724 * Use the normal group formation timeout between the end of
4725 * the provisioning phase and completion of 4-way handshake to
4726 * avoid terminating this process too early due to group idle
4727 * timeout.
4728 */
4729 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4730 "while waiting for initial 4-way handshake to "
4731 "complete");
4732 return;
4733 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004734#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07004735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004736 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004737 timeout);
4738 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4739 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740}
4741
4742
Jouni Malinen2b89da82012-08-31 22:04:41 +03004743/* Returns 1 if the interface was removed */
4744int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4745 u16 reason_code, const u8 *ie, size_t ie_len,
4746 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004747{
4748 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004749 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004751 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004752
Dmitry Shmidt04949592012-07-19 12:16:46 -07004753 if (!locally_generated)
4754 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4755 ie_len);
4756
4757 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4758 wpa_s->current_ssid &&
4759 wpa_s->current_ssid->p2p_group &&
4760 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4761 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4762 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03004763 if (wpas_p2p_group_delete(wpa_s,
4764 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4765 > 0)
4766 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004767 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03004768
4769 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770}
4771
4772
4773void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004774 u16 reason_code, const u8 *ie, size_t ie_len,
4775 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004776{
4777 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4778 return;
4779 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4780 return;
4781
Dmitry Shmidt04949592012-07-19 12:16:46 -07004782 if (!locally_generated)
4783 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4784 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785}
4786
4787
4788void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4789{
4790 struct p2p_data *p2p = wpa_s->global->p2p;
4791
4792 if (p2p == NULL)
4793 return;
4794
4795 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4796 return;
4797
4798 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4799 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4800
4801 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4802 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4803
4804 if (wpa_s->wps &&
4805 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4806 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4807
4808 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4809 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4810
4811 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4812 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4813 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4814 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4815 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4816 }
4817
4818 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4819 p2p_set_sec_dev_types(p2p,
4820 (void *) wpa_s->conf->sec_device_type,
4821 wpa_s->conf->num_sec_device_types);
4822
4823 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4824 int i;
4825 p2p_remove_wps_vendor_extensions(p2p);
4826 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4827 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4828 continue;
4829 p2p_add_wps_vendor_extension(
4830 p2p, wpa_s->conf->wps_vendor_ext[i]);
4831 }
4832 }
4833
4834 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4835 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4836 char country[3];
4837 country[0] = wpa_s->conf->country[0];
4838 country[1] = wpa_s->conf->country[1];
4839 country[2] = 0x04;
4840 p2p_set_country(p2p, country);
4841 }
4842
4843 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4844 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4845 wpa_s->conf->p2p_ssid_postfix ?
4846 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4847 0);
4848 }
4849
4850 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4851 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004852
4853 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4854 u8 reg_class, channel;
4855 int ret;
4856 unsigned int r;
4857 if (wpa_s->conf->p2p_listen_reg_class &&
4858 wpa_s->conf->p2p_listen_channel) {
4859 reg_class = wpa_s->conf->p2p_listen_reg_class;
4860 channel = wpa_s->conf->p2p_listen_channel;
4861 } else {
4862 reg_class = 81;
4863 /*
4864 * Pick one of the social channels randomly as the
4865 * listen channel.
4866 */
4867 os_get_random((u8 *) &r, sizeof(r));
4868 channel = 1 + (r % 3) * 5;
4869 }
4870 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4871 if (ret)
4872 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4873 "failed: %d", ret);
4874 }
4875 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4876 u8 op_reg_class, op_channel, cfg_op_channel;
4877 int ret = 0;
4878 unsigned int r;
4879 if (wpa_s->conf->p2p_oper_reg_class &&
4880 wpa_s->conf->p2p_oper_channel) {
4881 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4882 op_channel = wpa_s->conf->p2p_oper_channel;
4883 cfg_op_channel = 1;
4884 } else {
4885 op_reg_class = 81;
4886 /*
4887 * Use random operation channel from (1, 6, 11)
4888 *if no other preference is indicated.
4889 */
4890 os_get_random((u8 *) &r, sizeof(r));
4891 op_channel = 1 + (r % 3) * 5;
4892 cfg_op_channel = 0;
4893 }
4894 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4895 cfg_op_channel);
4896 if (ret)
4897 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4898 "failed: %d", ret);
4899 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004900
4901 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4902 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4903 wpa_s->conf->p2p_pref_chan) < 0) {
4904 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4905 "update failed");
4906 }
4907 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908}
4909
4910
4911int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4912 int duration)
4913{
4914 if (!wpa_s->ap_iface)
4915 return -1;
4916 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4917 duration);
4918}
4919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920
4921int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4922{
4923 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4924 return -1;
4925 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4926 return -1;
4927
4928 wpa_s->global->cross_connection = enabled;
4929 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4930
4931 if (!enabled) {
4932 struct wpa_supplicant *iface;
4933
4934 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4935 {
4936 if (iface->cross_connect_enabled == 0)
4937 continue;
4938
4939 iface->cross_connect_enabled = 0;
4940 iface->cross_connect_in_use = 0;
4941 wpa_msg(iface->parent, MSG_INFO,
4942 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4943 iface->ifname, iface->cross_connect_uplink);
4944 }
4945 }
4946
4947 return 0;
4948}
4949
4950
4951static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4952{
4953 struct wpa_supplicant *iface;
4954
4955 if (!uplink->global->cross_connection)
4956 return;
4957
4958 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4959 if (!iface->cross_connect_enabled)
4960 continue;
4961 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4962 0)
4963 continue;
4964 if (iface->ap_iface == NULL)
4965 continue;
4966 if (iface->cross_connect_in_use)
4967 continue;
4968
4969 iface->cross_connect_in_use = 1;
4970 wpa_msg(iface->parent, MSG_INFO,
4971 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4972 iface->ifname, iface->cross_connect_uplink);
4973 }
4974}
4975
4976
4977static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4978{
4979 struct wpa_supplicant *iface;
4980
4981 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4982 if (!iface->cross_connect_enabled)
4983 continue;
4984 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4985 0)
4986 continue;
4987 if (!iface->cross_connect_in_use)
4988 continue;
4989
4990 wpa_msg(iface->parent, MSG_INFO,
4991 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4992 iface->ifname, iface->cross_connect_uplink);
4993 iface->cross_connect_in_use = 0;
4994 }
4995}
4996
4997
4998void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4999{
5000 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5001 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5002 wpa_s->cross_connect_disallowed)
5003 wpas_p2p_disable_cross_connect(wpa_s);
5004 else
5005 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005006 if (!wpa_s->ap_iface &&
5007 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5008 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009}
5010
5011
5012void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5013{
5014 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005015 if (!wpa_s->ap_iface &&
5016 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5017 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 wpas_p2p_set_group_idle_timeout(wpa_s);
5019}
5020
5021
5022static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5023{
5024 struct wpa_supplicant *iface;
5025
5026 if (!wpa_s->global->cross_connection)
5027 return;
5028
5029 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5030 if (iface == wpa_s)
5031 continue;
5032 if (iface->drv_flags &
5033 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5034 continue;
5035 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5036 continue;
5037
5038 wpa_s->cross_connect_enabled = 1;
5039 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5040 sizeof(wpa_s->cross_connect_uplink));
5041 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5042 "%s to %s whenever uplink is available",
5043 wpa_s->ifname, wpa_s->cross_connect_uplink);
5044
5045 if (iface->ap_iface || iface->current_ssid == NULL ||
5046 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5047 iface->cross_connect_disallowed ||
5048 iface->wpa_state != WPA_COMPLETED)
5049 break;
5050
5051 wpa_s->cross_connect_in_use = 1;
5052 wpa_msg(wpa_s->parent, MSG_INFO,
5053 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5054 wpa_s->ifname, wpa_s->cross_connect_uplink);
5055 break;
5056 }
5057}
5058
5059
5060int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5061{
5062 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5063 !wpa_s->p2p_in_provisioning)
5064 return 0; /* not P2P client operation */
5065
5066 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5067 "session overlap");
5068 if (wpa_s != wpa_s->parent)
5069 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5070
5071 if (wpa_s->global->p2p)
5072 p2p_group_formation_failed(wpa_s->global->p2p);
5073
5074 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5075 wpa_s->parent, NULL);
5076
5077 wpas_group_formation_completed(wpa_s, 0);
5078 return 1;
5079}
5080
5081
5082void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5083{
5084 struct p2p_channels chan;
5085
5086 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5087 return;
5088
5089 os_memset(&chan, 0, sizeof(chan));
5090 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5091 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5092 "channel list");
5093 return;
5094 }
5095
5096 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5097}
5098
5099
5100int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5101{
5102 struct wpa_global *global = wpa_s->global;
5103 int found = 0;
5104 const u8 *peer;
5105
5106 if (global->p2p == NULL)
5107 return -1;
5108
5109 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5110
5111 if (wpa_s->pending_interface_name[0] &&
5112 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5113 found = 1;
5114
5115 peer = p2p_get_go_neg_peer(global->p2p);
5116 if (peer) {
5117 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5118 MACSTR, MAC2STR(peer));
5119 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005120 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121 }
5122
5123 wpas_p2p_stop_find(wpa_s);
5124
5125 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5126 if (wpa_s == global->p2p_group_formation &&
5127 (wpa_s->p2p_in_provisioning ||
5128 wpa_s->parent->pending_interface_type ==
5129 WPA_IF_P2P_CLIENT)) {
5130 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5131 "formation found - cancelling",
5132 wpa_s->ifname);
5133 found = 1;
5134 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5135 wpa_s->parent, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005136 wpas_p2p_group_delete(wpa_s,
5137 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005138 break;
5139 }
5140 }
5141
5142 if (!found) {
5143 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5144 return -1;
5145 }
5146
5147 return 0;
5148}
5149
5150
5151void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5152{
5153 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5154 return;
5155
5156 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5157 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005158 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005159}
5160
5161
5162void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5163 int freq_24, int freq_5, int freq_overall)
5164{
5165 struct p2p_data *p2p = wpa_s->global->p2p;
5166 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5167 return;
5168 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5169}
5170
5171
5172int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5173{
5174 u8 peer[ETH_ALEN];
5175 struct p2p_data *p2p = wpa_s->global->p2p;
5176
5177 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5178 return -1;
5179
5180 if (hwaddr_aton(addr, peer))
5181 return -1;
5182
5183 return p2p_unauthorize(p2p, peer);
5184}
5185
5186
5187/**
5188 * wpas_p2p_disconnect - Disconnect from a P2P Group
5189 * @wpa_s: Pointer to wpa_supplicant data
5190 * Returns: 0 on success, -1 on failure
5191 *
5192 * This can be used to disconnect from a group in which the local end is a P2P
5193 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5194 * virtual network interface was created for this group, that interface will be
5195 * removed. Otherwise, only the configured P2P group network will be removed
5196 * from the interface.
5197 */
5198int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5199{
5200
5201 if (wpa_s == NULL)
5202 return -1;
5203
Jouni Malinen2b89da82012-08-31 22:04:41 +03005204 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5205 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005206}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005207
5208
5209int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5210{
5211 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5212 return 0;
5213
5214 return p2p_in_progress(wpa_s->global->p2p);
5215}
5216
5217
5218void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5219 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005220{
5221 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5222 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5223 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005224 /**
5225 * Remove the network by scheduling the group formation
5226 * timeout to happen immediately. The teardown code
5227 * needs to be scheduled to run asynch later so that we
5228 * don't delete data from under ourselves unexpectedly.
5229 * Calling wpas_p2p_group_formation_timeout directly
5230 * causes a series of crashes in WPS failure scenarios.
5231 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005232 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5233 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005234 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5235 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005236 }
5237}
5238
5239
5240struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005241 const u8 *addr, const u8 *ssid,
5242 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005243{
5244 struct wpa_ssid *s;
5245 size_t i;
5246
5247 for (s = wpa_s->conf->ssid; s; s = s->next) {
5248 if (s->disabled != 2)
5249 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005250 if (ssid &&
5251 (ssid_len != s->ssid_len ||
5252 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5253 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005254 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5255 return s; /* peer is GO in the persistent group */
5256 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5257 continue;
5258 for (i = 0; i < s->num_p2p_clients; i++) {
5259 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5260 addr, ETH_ALEN) == 0)
5261 return s; /* peer is P2P client in persistent
5262 * group */
5263 }
5264 }
5265
5266 return NULL;
5267}
5268
5269
5270void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5271 const u8 *addr)
5272{
5273 if (addr == NULL)
5274 return;
5275 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5276}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005277
Dmitry Shmidt04949592012-07-19 12:16:46 -07005278
5279static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5280 int group_added)
5281{
5282 struct wpa_supplicant *group = wpa_s;
5283 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5284 if (wpa_s->global->p2p_group_formation)
5285 group = wpa_s->global->p2p_group_formation;
5286 wpa_s = wpa_s->parent;
5287 offchannel_send_action_done(wpa_s);
5288 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005289 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005290 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5291 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5292 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5293 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5294 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005295 wpa_s->p2p_pd_before_go_neg,
5296 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005297}
5298
5299
5300int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5301{
5302 if (!wpa_s->p2p_fallback_to_go_neg ||
5303 wpa_s->p2p_in_provisioning <= 5)
5304 return 0;
5305
5306 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5307 return 0; /* peer operating as a GO */
5308
5309 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5310 "fallback to GO Negotiation");
5311 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5312
5313 return 1;
5314}
5315
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005316
5317unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5318{
5319 const char *rn, *rn2;
5320 struct wpa_supplicant *ifs;
5321
5322 if (wpa_s->wpa_state > WPA_SCANNING) {
5323 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5324 "concurrent operation",
5325 P2P_CONCURRENT_SEARCH_DELAY);
5326 return P2P_CONCURRENT_SEARCH_DELAY;
5327 }
5328
5329 if (!wpa_s->driver->get_radio_name)
5330 return 0;
5331 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5332 if (rn == NULL || rn[0] == '\0')
5333 return 0;
5334
5335 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5336 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5337 continue;
5338
5339 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5340 if (!rn2 || os_strcmp(rn, rn2) != 0)
5341 continue;
5342 if (ifs->wpa_state > WPA_SCANNING) {
5343 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5344 "delay due to concurrent operation on "
5345 "interface %s",
5346 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5347 return P2P_CONCURRENT_SEARCH_DELAY;
5348 }
5349 }
5350
5351 return 0;
5352}
5353
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005354#ifdef ANDROID_P2P
5355int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
5356{
5357 struct wpa_supplicant *iface = NULL;
5358 struct p2p_data *p2p = wpa_s->global->p2p;
5359
5360 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5361 if((iface->p2p_group_interface) && (iface->current_ssid) &&
5362 (iface->current_ssid->frequency != freq)) {
5363
5364 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
5365 /* Try to see whether we can move the GO. If it
5366 * is not possible, remove the GO interface
5367 */
5368 if(wpa_drv_switch_channel(iface, freq) == 0) {
5369 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5370 iface->current_ssid->frequency = freq;
5371 continue;
5372 }
5373 }
5374
5375 /* If GO cannot be moved or if the conflicting interface is a
5376 * P2P Client, remove the interface depending up on the connection
5377 * priority */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005378 if(!wpas_is_p2p_prioritized(wpa_s)) {
5379 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005380 * P2P connection. So remove the interface */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005381 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5382 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005383 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005384 } else {
5385 /* Existing connection has the priority. Disable the newly
5386 * selected network and let the application know about it.
5387 */
5388 return -1;
5389 }
5390 }
5391 }
5392 return 0;
5393}
5394#endif