blob: 7afdfa5a22788bc761c822e9b7ca80a2a9f3f9b8 [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];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001202 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001203
1204#ifdef CONFIG_WIFI_DISPLAY
1205 if (info->wfd_subelems) {
1206 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1207 wpabuf_head(info->wfd_subelems),
1208 WFD_DEV_INFO_SIZE);
1209 }
1210#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211
1212 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1213 " p2p_dev_addr=" MACSTR
1214 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001215 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1217 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1218 sizeof(devtype)),
1219 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001220 info->dev_capab, info->group_capab,
1221 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001222#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223
1224 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1225}
1226
1227
1228static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1229{
1230 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001231
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001232 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1233 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001234
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1236}
1237
1238
1239static int wpas_start_listen(void *ctx, unsigned int freq,
1240 unsigned int duration,
1241 const struct wpabuf *probe_resp_ie)
1242{
1243 struct wpa_supplicant *wpa_s = ctx;
1244
1245 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1246
1247 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1248 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1249 "report received Probe Request frames");
1250 return -1;
1251 }
1252
1253 wpa_s->pending_listen_freq = freq;
1254 wpa_s->pending_listen_duration = duration;
1255
1256 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1257 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1258 "to remain on channel (%u MHz) for Listen "
1259 "state", freq);
1260 wpa_s->pending_listen_freq = 0;
1261 return -1;
1262 }
1263 wpa_s->off_channel_freq = 0;
1264 wpa_s->roc_waiting_drv_freq = freq;
1265
1266 return 0;
1267}
1268
1269
1270static void wpas_stop_listen(void *ctx)
1271{
1272 struct wpa_supplicant *wpa_s = ctx;
1273 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1274 wpa_drv_cancel_remain_on_channel(wpa_s);
1275 wpa_s->off_channel_freq = 0;
1276 wpa_s->roc_waiting_drv_freq = 0;
1277 }
1278 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1279 wpa_drv_probe_req_report(wpa_s, 0);
1280}
1281
1282
1283static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1284{
1285 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001286 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287}
1288
1289
1290static struct p2p_srv_bonjour *
1291wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1292 const struct wpabuf *query)
1293{
1294 struct p2p_srv_bonjour *bsrv;
1295 size_t len;
1296
1297 len = wpabuf_len(query);
1298 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1299 struct p2p_srv_bonjour, list) {
1300 if (len == wpabuf_len(bsrv->query) &&
1301 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1302 len) == 0)
1303 return bsrv;
1304 }
1305 return NULL;
1306}
1307
1308
1309static struct p2p_srv_upnp *
1310wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1311 const char *service)
1312{
1313 struct p2p_srv_upnp *usrv;
1314
1315 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1316 struct p2p_srv_upnp, list) {
1317 if (version == usrv->version &&
1318 os_strcmp(service, usrv->service) == 0)
1319 return usrv;
1320 }
1321 return NULL;
1322}
1323
1324
1325static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1326 u8 srv_trans_id)
1327{
1328 u8 *len_pos;
1329
1330 if (wpabuf_tailroom(resp) < 5)
1331 return;
1332
1333 /* Length (to be filled) */
1334 len_pos = wpabuf_put(resp, 2);
1335 wpabuf_put_u8(resp, srv_proto);
1336 wpabuf_put_u8(resp, srv_trans_id);
1337 /* Status Code */
1338 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1339 /* Response Data: empty */
1340 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1341}
1342
1343
1344static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1345 struct wpabuf *resp, u8 srv_trans_id)
1346{
1347 struct p2p_srv_bonjour *bsrv;
1348 u8 *len_pos;
1349
1350 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1351
1352 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1353 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1354 return;
1355 }
1356
1357 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1358 struct p2p_srv_bonjour, list) {
1359 if (wpabuf_tailroom(resp) <
1360 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1361 return;
1362 /* Length (to be filled) */
1363 len_pos = wpabuf_put(resp, 2);
1364 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1365 wpabuf_put_u8(resp, srv_trans_id);
1366 /* Status Code */
1367 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1368 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1369 wpabuf_head(bsrv->resp),
1370 wpabuf_len(bsrv->resp));
1371 /* Response Data */
1372 wpabuf_put_buf(resp, bsrv->query); /* Key */
1373 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1374 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1375 2);
1376 }
1377}
1378
1379
1380static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1381 struct wpabuf *resp, u8 srv_trans_id,
1382 const u8 *query, size_t query_len)
1383{
1384 struct p2p_srv_bonjour *bsrv;
1385 struct wpabuf buf;
1386 u8 *len_pos;
1387
1388 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1389 query, query_len);
1390 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1391 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1392 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1393 srv_trans_id);
1394 return;
1395 }
1396
1397 if (query_len == 0) {
1398 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1399 return;
1400 }
1401
1402 if (wpabuf_tailroom(resp) < 5)
1403 return;
1404 /* Length (to be filled) */
1405 len_pos = wpabuf_put(resp, 2);
1406 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1407 wpabuf_put_u8(resp, srv_trans_id);
1408
1409 wpabuf_set(&buf, query, query_len);
1410 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1411 if (bsrv == NULL) {
1412 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1413 "available");
1414
1415 /* Status Code */
1416 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1417 /* Response Data: empty */
1418 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1419 2);
1420 return;
1421 }
1422
1423 /* Status Code */
1424 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1425 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1426 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1427
1428 if (wpabuf_tailroom(resp) >=
1429 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1430 /* Response Data */
1431 wpabuf_put_buf(resp, bsrv->query); /* Key */
1432 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1433 }
1434 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1435}
1436
1437
1438static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1439 struct wpabuf *resp, u8 srv_trans_id)
1440{
1441 struct p2p_srv_upnp *usrv;
1442 u8 *len_pos;
1443
1444 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1445
1446 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1447 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1448 return;
1449 }
1450
1451 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1452 struct p2p_srv_upnp, list) {
1453 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1454 return;
1455
1456 /* Length (to be filled) */
1457 len_pos = wpabuf_put(resp, 2);
1458 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1459 wpabuf_put_u8(resp, srv_trans_id);
1460
1461 /* Status Code */
1462 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1463 /* Response Data */
1464 wpabuf_put_u8(resp, usrv->version);
1465 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1466 usrv->service);
1467 wpabuf_put_str(resp, usrv->service);
1468 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1469 2);
1470 }
1471}
1472
1473
1474static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1475 struct wpabuf *resp, u8 srv_trans_id,
1476 const u8 *query, size_t query_len)
1477{
1478 struct p2p_srv_upnp *usrv;
1479 u8 *len_pos;
1480 u8 version;
1481 char *str;
1482 int count = 0;
1483
1484 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1485 query, query_len);
1486
1487 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1488 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1489 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1490 srv_trans_id);
1491 return;
1492 }
1493
1494 if (query_len == 0) {
1495 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1496 return;
1497 }
1498
1499 if (wpabuf_tailroom(resp) < 5)
1500 return;
1501
1502 /* Length (to be filled) */
1503 len_pos = wpabuf_put(resp, 2);
1504 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1505 wpabuf_put_u8(resp, srv_trans_id);
1506
1507 version = query[0];
1508 str = os_malloc(query_len);
1509 if (str == NULL)
1510 return;
1511 os_memcpy(str, query + 1, query_len - 1);
1512 str[query_len - 1] = '\0';
1513
1514 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1515 struct p2p_srv_upnp, list) {
1516 if (version != usrv->version)
1517 continue;
1518
1519 if (os_strcmp(str, "ssdp:all") != 0 &&
1520 os_strstr(usrv->service, str) == NULL)
1521 continue;
1522
1523 if (wpabuf_tailroom(resp) < 2)
1524 break;
1525 if (count == 0) {
1526 /* Status Code */
1527 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1528 /* Response Data */
1529 wpabuf_put_u8(resp, version);
1530 } else
1531 wpabuf_put_u8(resp, ',');
1532
1533 count++;
1534
1535 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1536 usrv->service);
1537 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1538 break;
1539 wpabuf_put_str(resp, usrv->service);
1540 }
1541 os_free(str);
1542
1543 if (count == 0) {
1544 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1545 "available");
1546 /* Status Code */
1547 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1548 /* Response Data: empty */
1549 }
1550
1551 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1552}
1553
1554
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001555#ifdef CONFIG_WIFI_DISPLAY
1556static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1557 struct wpabuf *resp, u8 srv_trans_id,
1558 const u8 *query, size_t query_len)
1559{
1560 const u8 *pos;
1561 u8 role;
1562 u8 *len_pos;
1563
1564 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1565
1566 if (!wpa_s->global->wifi_display) {
1567 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1568 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1569 srv_trans_id);
1570 return;
1571 }
1572
1573 if (query_len < 1) {
1574 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1575 "Role");
1576 return;
1577 }
1578
1579 if (wpabuf_tailroom(resp) < 5)
1580 return;
1581
1582 pos = query;
1583 role = *pos++;
1584 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1585
1586 /* TODO: role specific handling */
1587
1588 /* Length (to be filled) */
1589 len_pos = wpabuf_put(resp, 2);
1590 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1591 wpabuf_put_u8(resp, srv_trans_id);
1592 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1593
1594 while (pos < query + query_len) {
1595 if (*pos < MAX_WFD_SUBELEMS &&
1596 wpa_s->global->wfd_subelem[*pos] &&
1597 wpabuf_tailroom(resp) >=
1598 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1599 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1600 "subelement %u", *pos);
1601 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1602 }
1603 pos++;
1604 }
1605
1606 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1607}
1608#endif /* CONFIG_WIFI_DISPLAY */
1609
1610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1612 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1613{
1614 struct wpa_supplicant *wpa_s = ctx;
1615 const u8 *pos = tlvs;
1616 const u8 *end = tlvs + tlvs_len;
1617 const u8 *tlv_end;
1618 u16 slen;
1619 struct wpabuf *resp;
1620 u8 srv_proto, srv_trans_id;
1621 size_t buf_len;
1622 char *buf;
1623
1624 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1625 tlvs, tlvs_len);
1626 buf_len = 2 * tlvs_len + 1;
1627 buf = os_malloc(buf_len);
1628 if (buf) {
1629 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1630 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1631 MACSTR " %u %u %s",
1632 freq, MAC2STR(sa), dialog_token, update_indic,
1633 buf);
1634 os_free(buf);
1635 }
1636
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001637 if (wpa_s->p2p_sd_over_ctrl_iface) {
1638 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1639 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001641 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642
1643 resp = wpabuf_alloc(10000);
1644 if (resp == NULL)
1645 return;
1646
1647 while (pos + 1 < end) {
1648 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1649 slen = WPA_GET_LE16(pos);
1650 pos += 2;
1651 if (pos + slen > end || slen < 2) {
1652 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1653 "length");
1654 wpabuf_free(resp);
1655 return;
1656 }
1657 tlv_end = pos + slen;
1658
1659 srv_proto = *pos++;
1660 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1661 srv_proto);
1662 srv_trans_id = *pos++;
1663 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1664 srv_trans_id);
1665
1666 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1667 pos, tlv_end - pos);
1668
1669
1670 if (wpa_s->force_long_sd) {
1671 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1672 "response");
1673 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1674 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1675 goto done;
1676 }
1677
1678 switch (srv_proto) {
1679 case P2P_SERV_ALL_SERVICES:
1680 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1681 "for all services");
1682 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1683 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1684 wpa_printf(MSG_DEBUG, "P2P: No service "
1685 "discovery protocols available");
1686 wpas_sd_add_proto_not_avail(
1687 resp, P2P_SERV_ALL_SERVICES,
1688 srv_trans_id);
1689 break;
1690 }
1691 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1692 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1693 break;
1694 case P2P_SERV_BONJOUR:
1695 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1696 pos, tlv_end - pos);
1697 break;
1698 case P2P_SERV_UPNP:
1699 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1700 pos, tlv_end - pos);
1701 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001702#ifdef CONFIG_WIFI_DISPLAY
1703 case P2P_SERV_WIFI_DISPLAY:
1704 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1705 pos, tlv_end - pos);
1706 break;
1707#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 default:
1709 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1710 "protocol %u", srv_proto);
1711 wpas_sd_add_proto_not_avail(resp, srv_proto,
1712 srv_trans_id);
1713 break;
1714 }
1715
1716 pos = tlv_end;
1717 }
1718
1719done:
1720 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1721 update_indic, tlvs, tlvs_len);
1722
1723 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1724
1725 wpabuf_free(resp);
1726}
1727
1728
1729void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1730 const u8 *tlvs, size_t tlvs_len)
1731{
1732 struct wpa_supplicant *wpa_s = ctx;
1733 const u8 *pos = tlvs;
1734 const u8 *end = tlvs + tlvs_len;
1735 const u8 *tlv_end;
1736 u16 slen;
1737 size_t buf_len;
1738 char *buf;
1739
1740 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1741 tlvs, tlvs_len);
1742 if (tlvs_len > 1500) {
1743 /* TODO: better way for handling this */
1744 wpa_msg_ctrl(wpa_s, MSG_INFO,
1745 P2P_EVENT_SERV_DISC_RESP MACSTR
1746 " %u <long response: %u bytes>",
1747 MAC2STR(sa), update_indic,
1748 (unsigned int) tlvs_len);
1749 } else {
1750 buf_len = 2 * tlvs_len + 1;
1751 buf = os_malloc(buf_len);
1752 if (buf) {
1753 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1754 wpa_msg_ctrl(wpa_s, MSG_INFO,
1755 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1756 MAC2STR(sa), update_indic, buf);
1757 os_free(buf);
1758 }
1759 }
1760
1761 while (pos < end) {
1762 u8 srv_proto, srv_trans_id, status;
1763
1764 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1765 slen = WPA_GET_LE16(pos);
1766 pos += 2;
1767 if (pos + slen > end || slen < 3) {
1768 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1769 "length");
1770 return;
1771 }
1772 tlv_end = pos + slen;
1773
1774 srv_proto = *pos++;
1775 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1776 srv_proto);
1777 srv_trans_id = *pos++;
1778 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1779 srv_trans_id);
1780 status = *pos++;
1781 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1782 status);
1783
1784 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1785 pos, tlv_end - pos);
1786
1787 pos = tlv_end;
1788 }
1789
1790 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1791}
1792
1793
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001794u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1795 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001796{
1797 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001798 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001800 return 0;
1801 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001802}
1803
1804
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001805u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1806 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807{
1808 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001809 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810
1811 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1812 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001813 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001814 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1815 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1816 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1817 wpabuf_put_u8(tlvs, version);
1818 wpabuf_put_str(tlvs, query);
1819 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1820 wpabuf_free(tlvs);
1821 return ret;
1822}
1823
1824
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001825#ifdef CONFIG_WIFI_DISPLAY
1826
1827static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1828 const struct wpabuf *tlvs)
1829{
1830 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1831 return 0;
1832 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1833 return 0;
1834 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1835}
1836
1837
1838#define MAX_WFD_SD_SUBELEMS 20
1839
1840static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1841 const char *subelems)
1842{
1843 u8 *len;
1844 const char *pos;
1845 int val;
1846 int count = 0;
1847
1848 len = wpabuf_put(tlvs, 2);
1849 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1850 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1851
1852 wpabuf_put_u8(tlvs, role);
1853
1854 pos = subelems;
1855 while (*pos) {
1856 val = atoi(pos);
1857 if (val >= 0 && val < 256) {
1858 wpabuf_put_u8(tlvs, val);
1859 count++;
1860 if (count == MAX_WFD_SD_SUBELEMS)
1861 break;
1862 }
1863 pos = os_strchr(pos + 1, ',');
1864 if (pos == NULL)
1865 break;
1866 pos++;
1867 }
1868
1869 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1870}
1871
1872
1873u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1874 const u8 *dst, const char *role)
1875{
1876 struct wpabuf *tlvs;
1877 u64 ret;
1878 const char *subelems;
1879 u8 id = 1;
1880
1881 subelems = os_strchr(role, ' ');
1882 if (subelems == NULL)
1883 return 0;
1884 subelems++;
1885
1886 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1887 if (tlvs == NULL)
1888 return 0;
1889
1890 if (os_strstr(role, "[source]"))
1891 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1892 if (os_strstr(role, "[pri-sink]"))
1893 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1894 if (os_strstr(role, "[sec-sink]"))
1895 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1896 if (os_strstr(role, "[source+sink]"))
1897 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1898
1899 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1900 wpabuf_free(tlvs);
1901 return ret;
1902}
1903
1904#endif /* CONFIG_WIFI_DISPLAY */
1905
1906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001907int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908{
1909 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001910 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1912 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001913 return p2p_sd_cancel_request(wpa_s->global->p2p,
1914 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915}
1916
1917
1918void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1919 const u8 *dst, u8 dialog_token,
1920 const struct wpabuf *resp_tlvs)
1921{
1922 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1923 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1924 resp_tlvs);
1925 return;
1926 }
1927 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1928 return;
1929 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1930 resp_tlvs);
1931}
1932
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001933#ifdef ANDROID_P2P
1934void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1935#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001937#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001938{
1939 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1940 wpa_drv_p2p_service_update(wpa_s);
1941 return;
1942 }
1943 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001944#ifdef ANDROID_P2P
1945 p2p_sd_service_update(wpa_s->global->p2p, action);
1946#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001948#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949}
1950
1951
1952static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1953{
1954 dl_list_del(&bsrv->list);
1955 wpabuf_free(bsrv->query);
1956 wpabuf_free(bsrv->resp);
1957 os_free(bsrv);
1958}
1959
1960
1961static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1962{
1963 dl_list_del(&usrv->list);
1964 os_free(usrv->service);
1965 os_free(usrv);
1966}
1967
1968
1969void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1970{
1971 struct p2p_srv_bonjour *bsrv, *bn;
1972 struct p2p_srv_upnp *usrv, *un;
1973
1974 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1975 struct p2p_srv_bonjour, list)
1976 wpas_p2p_srv_bonjour_free(bsrv);
1977
1978 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1979 struct p2p_srv_upnp, list)
1980 wpas_p2p_srv_upnp_free(usrv);
1981
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001982#ifdef ANDROID_P2P
1983 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1984#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001986#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987}
1988
1989
1990int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1991 struct wpabuf *query, struct wpabuf *resp)
1992{
1993 struct p2p_srv_bonjour *bsrv;
1994
1995 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1996 if (bsrv) {
1997 wpabuf_free(query);
1998 wpabuf_free(bsrv->resp);
1999 bsrv->resp = resp;
2000 return 0;
2001 }
2002
2003 bsrv = os_zalloc(sizeof(*bsrv));
2004 if (bsrv == NULL)
2005 return -1;
2006 bsrv->query = query;
2007 bsrv->resp = resp;
2008 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2009
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002010#ifdef ANDROID_P2P
2011 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2012#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002014#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 return 0;
2016}
2017
2018
2019int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2020 const struct wpabuf *query)
2021{
2022 struct p2p_srv_bonjour *bsrv;
2023
2024 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2025 if (bsrv == NULL)
2026 return -1;
2027 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002028#ifdef ANDROID_P2P
2029 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2030#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002032#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 return 0;
2034}
2035
2036
2037int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2038 const char *service)
2039{
2040 struct p2p_srv_upnp *usrv;
2041
2042 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2043 return 0; /* Already listed */
2044 usrv = os_zalloc(sizeof(*usrv));
2045 if (usrv == NULL)
2046 return -1;
2047 usrv->version = version;
2048 usrv->service = os_strdup(service);
2049 if (usrv->service == NULL) {
2050 os_free(usrv);
2051 return -1;
2052 }
2053 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2054
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002055#ifdef ANDROID_P2P
2056 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2057#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002059#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060 return 0;
2061}
2062
2063
2064int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2065 const char *service)
2066{
2067 struct p2p_srv_upnp *usrv;
2068
2069 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2070 if (usrv == NULL)
2071 return -1;
2072 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002073#ifdef ANDROID_P2P
2074 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2075#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002077#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 return 0;
2079}
2080
2081
2082static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2083 const u8 *peer, const char *params,
2084 unsigned int generated_pin)
2085{
2086 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2087 MAC2STR(peer), generated_pin, params);
2088}
2089
2090
2091static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2092 const u8 *peer, const char *params)
2093{
2094 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2095 MAC2STR(peer), params);
2096}
2097
2098
2099void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2100 const u8 *dev_addr, const u8 *pri_dev_type,
2101 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102 u8 dev_capab, u8 group_capab, const u8 *group_id,
2103 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104{
2105 struct wpa_supplicant *wpa_s = ctx;
2106 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002107 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108 u8 empty_dev_type[8];
2109 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002110 struct wpa_supplicant *group = NULL;
2111
2112 if (group_id) {
2113 for (group = wpa_s->global->ifaces; group; group = group->next)
2114 {
2115 struct wpa_ssid *s = group->current_ssid;
2116 if (s != NULL &&
2117 s->mode == WPAS_MODE_P2P_GO &&
2118 group_id_len - ETH_ALEN == s->ssid_len &&
2119 os_memcmp(group_id + ETH_ALEN, s->ssid,
2120 s->ssid_len) == 0)
2121 break;
2122 }
2123 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124
2125 if (pri_dev_type == NULL) {
2126 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2127 pri_dev_type = empty_dev_type;
2128 }
2129 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2130 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002131 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132 MAC2STR(dev_addr),
2133 wps_dev_type_bin2str(pri_dev_type, devtype,
2134 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002135 dev_name, supp_config_methods, dev_capab, group_capab,
2136 group ? " group=" : "",
2137 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140 if (config_methods & WPS_CONFIG_DISPLAY) {
2141 generated_pin = wps_generate_pin();
2142 wpas_prov_disc_local_display(wpa_s, peer, params,
2143 generated_pin);
2144 } else if (config_methods & WPS_CONFIG_KEYPAD)
2145 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2146 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2147 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2148 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002149
2150 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2151 P2P_PROV_DISC_SUCCESS,
2152 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153}
2154
2155
2156void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2157{
2158 struct wpa_supplicant *wpa_s = ctx;
2159 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002160 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002162 if (wpa_s->pending_pd_before_join &&
2163 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2164 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2165 wpa_s->pending_pd_before_join = 0;
2166 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2167 "join-existing-group operation");
2168 wpas_p2p_join_start(wpa_s);
2169 return;
2170 }
2171
Dmitry Shmidt04949592012-07-19 12:16:46 -07002172 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2173 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2174 os_snprintf(params, sizeof(params), " peer_go=%d",
2175 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2176 else
2177 params[0] = '\0';
2178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002180 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 else if (config_methods & WPS_CONFIG_KEYPAD) {
2182 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002183 wpas_prov_disc_local_display(wpa_s, peer, params,
2184 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002186 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2187 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188
Jouni Malinen75ecf522011-06-27 15:19:46 -07002189 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2190 P2P_PROV_DISC_SUCCESS,
2191 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192}
2193
2194
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002195static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2196 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002197{
2198 struct wpa_supplicant *wpa_s = ctx;
2199
Dmitry Shmidt04949592012-07-19 12:16:46 -07002200 if (wpa_s->p2p_fallback_to_go_neg) {
2201 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2202 "failed - fall back to GO Negotiation");
2203 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2204 return;
2205 }
2206
2207 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2208 " p2p_dev_addr=" MACSTR " status=%d",
2209 MAC2STR(peer), status);
2210
Jouni Malinen75ecf522011-06-27 15:19:46 -07002211 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2212 status, 0, 0);
2213}
2214
2215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002216static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2217 const u8 *go_dev_addr, const u8 *ssid,
2218 size_t ssid_len, int *go, u8 *group_bssid,
2219 int *force_freq, int persistent_group)
2220{
2221 struct wpa_supplicant *wpa_s = ctx;
2222 struct wpa_ssid *s;
2223 u8 cur_bssid[ETH_ALEN];
2224 int res;
2225 struct wpa_supplicant *grp;
2226
2227 if (!persistent_group) {
2228 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2229 " to join an active group", MAC2STR(sa));
2230 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2231 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2232 == 0 ||
2233 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2234 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2235 "authorized invitation");
2236 goto accept_inv;
2237 }
2238 /*
2239 * Do not accept the invitation automatically; notify user and
2240 * request approval.
2241 */
2242 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2243 }
2244
2245 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2246 if (grp) {
2247 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2248 "running persistent group");
2249 if (*go)
2250 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2251 goto accept_inv;
2252 }
2253
2254 if (!wpa_s->conf->persistent_reconnect)
2255 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2256
2257 for (s = wpa_s->conf->ssid; s; s = s->next) {
2258 if (s->disabled == 2 &&
2259 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2260 s->ssid_len == ssid_len &&
2261 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2262 break;
2263 }
2264
2265 if (!s) {
2266 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2267 " requested reinvocation of an unknown group",
2268 MAC2STR(sa));
2269 return P2P_SC_FAIL_UNKNOWN_GROUP;
2270 }
2271
2272 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2273 *go = 1;
2274 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2275 wpa_printf(MSG_DEBUG, "P2P: The only available "
2276 "interface is already in use - reject "
2277 "invitation");
2278 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2279 }
2280 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2281 } else if (s->mode == WPAS_MODE_P2P_GO) {
2282 *go = 1;
2283 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2284 {
2285 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2286 "interface address for the group");
2287 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2288 }
2289 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2290 ETH_ALEN);
2291 }
2292
2293accept_inv:
2294 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2295 wpa_s->assoc_freq) {
2296 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2297 "the channel we are already using");
2298 *force_freq = wpa_s->assoc_freq;
2299 }
2300
2301 res = wpa_drv_shared_freq(wpa_s);
2302 if (res > 0) {
2303 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2304 "with the channel we are already using on a "
2305 "shared interface");
2306 *force_freq = res;
2307 }
2308
2309 return P2P_SC_SUCCESS;
2310}
2311
2312
2313static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2314 const u8 *ssid, size_t ssid_len,
2315 const u8 *go_dev_addr, u8 status,
2316 int op_freq)
2317{
2318 struct wpa_supplicant *wpa_s = ctx;
2319 struct wpa_ssid *s;
2320
2321 for (s = wpa_s->conf->ssid; s; s = s->next) {
2322 if (s->disabled == 2 &&
2323 s->ssid_len == ssid_len &&
2324 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2325 break;
2326 }
2327
2328 if (status == P2P_SC_SUCCESS) {
2329 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2330 " was accepted; op_freq=%d MHz",
2331 MAC2STR(sa), op_freq);
2332 if (s) {
2333 wpas_p2p_group_add_persistent(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002334 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335 } else if (bssid) {
2336 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002337 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338 }
2339 return;
2340 }
2341
2342 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2343 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2344 " was rejected (status %u)", MAC2STR(sa), status);
2345 return;
2346 }
2347
2348 if (!s) {
2349 if (bssid) {
2350 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2351 "sa=" MACSTR " go_dev_addr=" MACSTR
2352 " bssid=" MACSTR " unknown-network",
2353 MAC2STR(sa), MAC2STR(go_dev_addr),
2354 MAC2STR(bssid));
2355 } else {
2356 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2357 "sa=" MACSTR " go_dev_addr=" MACSTR
2358 " unknown-network",
2359 MAC2STR(sa), MAC2STR(go_dev_addr));
2360 }
2361 return;
2362 }
2363
2364 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2365 " persistent=%d", MAC2STR(sa), s->id);
2366}
2367
2368
2369static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2370{
2371 struct wpa_supplicant *wpa_s = ctx;
2372 struct wpa_ssid *ssid;
2373
2374 if (bssid) {
2375 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2376 "status=%d " MACSTR,
2377 status, MAC2STR(bssid));
2378 } else {
2379 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2380 "status=%d ", status);
2381 }
2382 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2383
2384 if (wpa_s->pending_invite_ssid_id == -1)
2385 return; /* Invitation to active group */
2386
2387 if (status != P2P_SC_SUCCESS) {
2388 wpas_p2p_remove_pending_group_interface(wpa_s);
2389 return;
2390 }
2391
2392 ssid = wpa_config_get_network(wpa_s->conf,
2393 wpa_s->pending_invite_ssid_id);
2394 if (ssid == NULL) {
2395 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2396 "data matching with invitation");
2397 return;
2398 }
2399
2400 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002401 ssid->mode == WPAS_MODE_P2P_GO,
2402 wpa_s->p2p_persistent_go_freq,
2403 wpa_s->p2p_go_ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404}
2405
2406
Dmitry Shmidt04949592012-07-19 12:16:46 -07002407static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2408 unsigned int freq)
2409{
2410 unsigned int i;
2411
2412 if (global->p2p_disallow_freq == NULL)
2413 return 0;
2414
2415 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2416 if (freq >= global->p2p_disallow_freq[i].min &&
2417 freq <= global->p2p_disallow_freq[i].max)
2418 return 1;
2419 }
2420
2421 return 0;
2422}
2423
2424
2425static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2426{
2427 reg->channel[reg->channels] = chan;
2428 reg->channels++;
2429}
2430
2431
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002432static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2433 struct p2p_channels *chan)
2434{
2435 int i, cla = 0;
2436
2437 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2438 "band");
2439
2440 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2441 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002442 chan->reg_class[cla].channels = 0;
2443 for (i = 0; i < 11; i++) {
2444 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2445 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2446 }
2447 if (chan->reg_class[cla].channels)
2448 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449
2450 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2451 "band");
2452
2453 /* Operating class 115 - 5 GHz, channels 36-48 */
2454 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002455 chan->reg_class[cla].channels = 0;
2456 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2457 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2458 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2459 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2460 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2461 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2462 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2463 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2464 if (chan->reg_class[cla].channels)
2465 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466
2467 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2468 "band");
2469
2470 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2471 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002472 chan->reg_class[cla].channels = 0;
2473 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2474 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2475 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2476 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2477 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2478 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2479 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2480 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2481 if (chan->reg_class[cla].channels)
2482 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002483
2484 chan->reg_classes = cla;
2485 return 0;
2486}
2487
2488
2489static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2490 u16 num_modes,
2491 enum hostapd_hw_mode mode)
2492{
2493 u16 i;
2494
2495 for (i = 0; i < num_modes; i++) {
2496 if (modes[i].mode == mode)
2497 return &modes[i];
2498 }
2499
2500 return NULL;
2501}
2502
2503
Dmitry Shmidt04949592012-07-19 12:16:46 -07002504static int has_channel(struct wpa_global *global,
2505 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506{
2507 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002508 unsigned int freq;
2509
2510 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2511 chan * 5;
2512 if (wpas_p2p_disallowed_freq(global, freq))
2513 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514
2515 for (i = 0; i < mode->num_channels; i++) {
2516 if (mode->channels[i].chan == chan) {
2517 if (flags)
2518 *flags = mode->channels[i].flag;
2519 return !(mode->channels[i].flag &
2520 (HOSTAPD_CHAN_DISABLED |
2521 HOSTAPD_CHAN_PASSIVE_SCAN |
2522 HOSTAPD_CHAN_NO_IBSS |
2523 HOSTAPD_CHAN_RADAR));
2524 }
2525 }
2526
2527 return 0;
2528}
2529
2530
2531struct p2p_oper_class_map {
2532 enum hostapd_hw_mode mode;
2533 u8 op_class;
2534 u8 min_chan;
2535 u8 max_chan;
2536 u8 inc;
2537 enum { BW20, BW40PLUS, BW40MINUS } bw;
2538};
2539
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002540static struct p2p_oper_class_map op_class[] = {
2541 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2542 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2543#if 0 /* Do not enable HT40 on 2 GHz for now */
2544 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2545 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2546#endif
2547 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2548 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2549 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2550 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2551 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2552 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2553 { -1, 0, 0, 0, 0, BW20 }
2554};
2555
2556
2557static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2558 struct hostapd_hw_modes *mode,
2559 u8 channel, u8 bw)
2560{
2561 int flag;
2562
2563 if (!has_channel(wpa_s->global, mode, channel, &flag))
2564 return -1;
2565 if (bw == BW40MINUS &&
2566 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2567 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2568 return 0;
2569 if (bw == BW40PLUS &&
2570 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2571 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2572 return 0;
2573 return 1;
2574}
2575
2576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2578 struct p2p_channels *chan)
2579{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002580 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002583 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2585 "of all supported channels; assume dualband "
2586 "support");
2587 return wpas_p2p_default_channels(wpa_s, chan);
2588 }
2589
2590 cla = 0;
2591
2592 for (op = 0; op_class[op].op_class; op++) {
2593 struct p2p_oper_class_map *o = &op_class[op];
2594 u8 ch;
2595 struct p2p_reg_class *reg = NULL;
2596
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002597 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598 if (mode == NULL)
2599 continue;
2600 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002601 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602 continue;
2603 if (reg == NULL) {
2604 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2605 "class %u", o->op_class);
2606 reg = &chan->reg_class[cla];
2607 cla++;
2608 reg->reg_class = o->op_class;
2609 }
2610 reg->channel[reg->channels] = ch;
2611 reg->channels++;
2612 }
2613 if (reg) {
2614 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2615 reg->channel, reg->channels);
2616 }
2617 }
2618
2619 chan->reg_classes = cla;
2620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621 return 0;
2622}
2623
2624
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002625int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2626 struct hostapd_hw_modes *mode, u8 channel)
2627{
2628 int op, ret;
2629
2630 for (op = 0; op_class[op].op_class; op++) {
2631 struct p2p_oper_class_map *o = &op_class[op];
2632 u8 ch;
2633
2634 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2635 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2636 o->bw == BW20 || ch != channel)
2637 continue;
2638 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2639 if (ret < 0)
2640 continue;
2641 else if (ret > 0)
2642 return (o->bw == BW40MINUS) ? -1 : 1;
2643 else
2644 return 0;
2645 }
2646 }
2647 return 0;
2648}
2649
2650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2652 size_t buf_len)
2653{
2654 struct wpa_supplicant *wpa_s = ctx;
2655
2656 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2657 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2658 break;
2659 }
2660 if (wpa_s == NULL)
2661 return -1;
2662
2663 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2664}
2665
2666
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002667static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2668{
2669 struct wpa_supplicant *wpa_s = ctx;
2670
2671 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2672 struct wpa_ssid *ssid = wpa_s->current_ssid;
2673 if (ssid == NULL)
2674 continue;
2675 if (ssid->mode != WPAS_MODE_INFRA)
2676 continue;
2677 if (wpa_s->wpa_state != WPA_COMPLETED &&
2678 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2679 continue;
2680 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2681 return 1;
2682 }
2683
2684 return 0;
2685}
2686
2687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002688/**
2689 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2690 * @global: Pointer to global data from wpa_supplicant_init()
2691 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2692 * Returns: 0 on success, -1 on failure
2693 */
2694int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2695{
2696 struct p2p_config p2p;
2697 unsigned int r;
2698 int i;
2699
2700 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2701 return 0;
2702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703 if (global->p2p)
2704 return 0;
2705
2706 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2707 struct p2p_params params;
2708
2709 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2710 os_memset(&params, 0, sizeof(params));
2711 params.dev_name = wpa_s->conf->device_name;
2712 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2713 WPS_DEV_TYPE_LEN);
2714 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2715 os_memcpy(params.sec_dev_type,
2716 wpa_s->conf->sec_device_type,
2717 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2718
2719 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2720 return -1;
2721
2722 return 0;
2723 }
2724
2725 os_memset(&p2p, 0, sizeof(p2p));
2726 p2p.msg_ctx = wpa_s;
2727 p2p.cb_ctx = wpa_s;
2728 p2p.p2p_scan = wpas_p2p_scan;
2729 p2p.send_action = wpas_send_action;
2730 p2p.send_action_done = wpas_send_action_done;
2731 p2p.go_neg_completed = wpas_go_neg_completed;
2732 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2733 p2p.dev_found = wpas_dev_found;
2734 p2p.dev_lost = wpas_dev_lost;
2735 p2p.start_listen = wpas_start_listen;
2736 p2p.stop_listen = wpas_stop_listen;
2737 p2p.send_probe_resp = wpas_send_probe_resp;
2738 p2p.sd_request = wpas_sd_request;
2739 p2p.sd_response = wpas_sd_response;
2740 p2p.prov_disc_req = wpas_prov_disc_req;
2741 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002742 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743 p2p.invitation_process = wpas_invitation_process;
2744 p2p.invitation_received = wpas_invitation_received;
2745 p2p.invitation_result = wpas_invitation_result;
2746 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002747 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002748
2749 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002750 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751 p2p.dev_name = wpa_s->conf->device_name;
2752 p2p.manufacturer = wpa_s->conf->manufacturer;
2753 p2p.model_name = wpa_s->conf->model_name;
2754 p2p.model_number = wpa_s->conf->model_number;
2755 p2p.serial_number = wpa_s->conf->serial_number;
2756 if (wpa_s->wps) {
2757 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2758 p2p.config_methods = wpa_s->wps->config_methods;
2759 }
2760
2761 if (wpa_s->conf->p2p_listen_reg_class &&
2762 wpa_s->conf->p2p_listen_channel) {
2763 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2764 p2p.channel = wpa_s->conf->p2p_listen_channel;
2765 } else {
2766 p2p.reg_class = 81;
2767 /*
2768 * Pick one of the social channels randomly as the listen
2769 * channel.
2770 */
2771 os_get_random((u8 *) &r, sizeof(r));
2772 p2p.channel = 1 + (r % 3) * 5;
2773 }
2774 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2775
2776 if (wpa_s->conf->p2p_oper_reg_class &&
2777 wpa_s->conf->p2p_oper_channel) {
2778 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2779 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2780 p2p.cfg_op_channel = 1;
2781 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2782 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2783
2784 } else {
2785 p2p.op_reg_class = 81;
2786 /*
2787 * Use random operation channel from (1, 6, 11) if no other
2788 * preference is indicated.
2789 */
2790 os_get_random((u8 *) &r, sizeof(r));
2791 p2p.op_channel = 1 + (r % 3) * 5;
2792 p2p.cfg_op_channel = 0;
2793 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2794 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2795 }
2796 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2797 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2798 p2p.country[2] = 0x04;
2799 } else
2800 os_memcpy(p2p.country, "XX\x04", 3);
2801
2802 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2803 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2804 "channel list");
2805 return -1;
2806 }
2807
2808 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2809 WPS_DEV_TYPE_LEN);
2810
2811 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2812 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2813 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2814
2815 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2816 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2817
2818 p2p.max_peers = 100;
2819
2820 if (wpa_s->conf->p2p_ssid_postfix) {
2821 p2p.ssid_postfix_len =
2822 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2823 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2824 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2825 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2826 p2p.ssid_postfix_len);
2827 }
2828
2829 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2830
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07002831#ifdef ANDROID_P2P
2832 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2833 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
2834 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)
2835 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
2836#endif
2837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002838 global->p2p = p2p_init(&p2p);
2839 if (global->p2p == NULL)
2840 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002841 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842
2843 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2844 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2845 continue;
2846 p2p_add_wps_vendor_extension(
2847 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2848 }
2849
2850 return 0;
2851}
2852
2853
2854/**
2855 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2856 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2857 *
2858 * This function deinitialize per-interface P2P data.
2859 */
2860void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2861{
2862 if (wpa_s->driver && wpa_s->drv_priv)
2863 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002864
2865 if (wpa_s->go_params) {
2866 /* Clear any stored provisioning info */
2867 p2p_clear_provisioning_info(
2868 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002869 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002870 }
2871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872 os_free(wpa_s->go_params);
2873 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2875 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002876 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877 wpa_s->p2p_long_listen = 0;
2878 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2879 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2880 wpas_p2p_remove_pending_group_interface(wpa_s);
2881
2882 /* TODO: remove group interface from the driver if this wpa_s instance
2883 * is on top of a P2P group interface */
2884}
2885
2886
2887/**
2888 * wpas_p2p_deinit_global - Deinitialize global P2P module
2889 * @global: Pointer to global data from wpa_supplicant_init()
2890 *
2891 * This function deinitializes the global (per device) P2P module.
2892 */
2893void wpas_p2p_deinit_global(struct wpa_global *global)
2894{
2895 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002897 wpa_s = global->ifaces;
2898 if (wpa_s)
2899 wpas_p2p_service_flush(wpa_s);
2900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 if (global->p2p == NULL)
2902 return;
2903
2904 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2906 wpa_s = wpa_s->next;
2907 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 tmp = global->ifaces;
2909 while (tmp &&
2910 (tmp == wpa_s ||
2911 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2912 tmp = tmp->next;
2913 }
2914 if (tmp == NULL)
2915 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002916 /* Disconnect from the P2P group and deinit the interface */
2917 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918 }
2919
2920 /*
2921 * Deinit GO data on any possibly remaining interface (if main
2922 * interface is used as GO).
2923 */
2924 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2925 if (wpa_s->ap_iface)
2926 wpas_p2p_group_deinit(wpa_s);
2927 }
2928
2929 p2p_deinit(global->p2p);
2930 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002931 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932}
2933
2934
2935static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2936{
2937 if (wpa_s->drv_flags &
2938 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2939 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2940 return 1; /* P2P group requires a new interface in every case
2941 */
2942 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2943 return 0; /* driver does not support concurrent operations */
2944 if (wpa_s->global->ifaces->next)
2945 return 1; /* more that one interface already in use */
2946 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2947 return 1; /* this interface is already in use */
2948 return 0;
2949}
2950
2951
2952static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2953 const u8 *peer_addr,
2954 enum p2p_wps_method wps_method,
2955 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002956 unsigned int force_freq, int persistent_group,
2957 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002959 if (persistent_group && wpa_s->conf->persistent_reconnect)
2960 persistent_group = 2;
2961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2963 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2964 go_intent, own_interface_addr,
2965 force_freq, persistent_group);
2966 }
2967
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002968 /*
2969 * Increase GO config timeout if HT40 is used since it takes some time
2970 * to scan channels for coex purposes before the BSS can be started.
2971 */
2972 p2p_set_config_timeout(wpa_s->global->p2p,
2973 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2976 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002977 persistent_group, ssid ? ssid->ssid : NULL,
2978 ssid ? ssid->ssid_len : 0,
2979 wpa_s->p2p_pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980}
2981
2982
2983static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2984 const u8 *peer_addr,
2985 enum p2p_wps_method wps_method,
2986 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002987 unsigned int force_freq, int persistent_group,
2988 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002990 if (persistent_group && wpa_s->conf->persistent_reconnect)
2991 persistent_group = 2;
2992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2994 return -1;
2995
2996 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2997 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002998 persistent_group, ssid ? ssid->ssid : NULL,
2999 ssid ? ssid->ssid_len : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000}
3001
3002
3003static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3004{
3005 wpa_s->p2p_join_scan_count++;
3006 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3007 wpa_s->p2p_join_scan_count);
3008 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3009 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3010 " for join operationg - stop join attempt",
3011 MAC2STR(wpa_s->pending_join_iface_addr));
3012 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003013 if (wpa_s->p2p_auto_pd) {
3014 wpa_s->p2p_auto_pd = 0;
3015 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3016 " p2p_dev_addr=" MACSTR " status=N/A",
3017 MAC2STR(wpa_s->pending_join_dev_addr));
3018 return;
3019 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 wpa_msg(wpa_s->parent, MSG_INFO,
3021 P2P_EVENT_GROUP_FORMATION_FAILURE);
3022 }
3023}
3024
3025
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003026static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
3027{
3028 struct wpa_supplicant *wpa_s = eloop_ctx;
3029 if (!wpa_s->pending_pd_before_join)
3030 return;
3031 /*
3032 * Provision Discovery Response may have been lost - try to connect
3033 * anyway since we do not need any information from this PD.
3034 */
3035 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
3036 "try to connect anyway");
3037 wpas_p2p_join_start(wpa_s);
3038}
3039
3040
Dmitry Shmidt04949592012-07-19 12:16:46 -07003041static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3042{
3043 struct wpa_supplicant *iface;
3044 int shared_freq;
3045 u8 bssid[ETH_ALEN];
3046
3047 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3048 return 0;
3049
3050 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3051 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3052 continue;
3053 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3054 continue;
3055 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3056 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3057 shared_freq = iface->current_ssid->frequency;
3058 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3059 shared_freq = iface->assoc_freq;
3060 else
3061 shared_freq = 0;
3062
3063 if (shared_freq && freq != shared_freq) {
3064 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3065 "connected on %d MHz - new connection on "
3066 "%d MHz", iface->ifname, shared_freq, freq);
3067 return 1;
3068 }
3069 }
3070
3071 shared_freq = wpa_drv_shared_freq(wpa_s);
3072 if (shared_freq > 0 && shared_freq != freq) {
3073 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3074 "virtual interface connected on %d MHz - new "
3075 "connection on %d MHz", shared_freq, freq);
3076 return 1;
3077 }
3078
3079 return 0;
3080}
3081
3082
3083static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3084 const u8 *peer_dev_addr)
3085{
3086 struct wpa_bss *bss;
3087 int updated;
3088
3089 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3090 if (bss == NULL)
3091 return -1;
3092 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3093 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3094 "last scan");
3095 return 0;
3096 }
3097
3098 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3099 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3100 "%ld.%06ld (%supdated in last scan)",
3101 bss->last_update.sec, bss->last_update.usec,
3102 updated ? "": "not ");
3103
3104 return updated;
3105}
3106
3107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003108static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3109 struct wpa_scan_results *scan_res)
3110{
3111 struct wpa_bss *bss;
3112 int freq;
3113 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3116
3117 if (wpa_s->global->p2p_disabled)
3118 return;
3119
Dmitry Shmidt04949592012-07-19 12:16:46 -07003120 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3121 scan_res ? (int) scan_res->num : -1,
3122 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003123
3124 if (scan_res)
3125 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3126
Dmitry Shmidt04949592012-07-19 12:16:46 -07003127 if (wpa_s->p2p_auto_pd) {
3128 int join = wpas_p2p_peer_go(wpa_s,
3129 wpa_s->pending_join_dev_addr);
3130 if (join == 0 &&
3131 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3132 wpa_s->auto_pd_scan_retry++;
3133 bss = wpa_bss_get_bssid(wpa_s,
3134 wpa_s->pending_join_dev_addr);
3135 if (bss) {
3136 freq = bss->freq;
3137 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3138 "the peer " MACSTR " at %d MHz",
3139 wpa_s->auto_pd_scan_retry,
3140 MAC2STR(wpa_s->
3141 pending_join_dev_addr),
3142 freq);
3143 wpas_p2p_join_scan_req(wpa_s, freq);
3144 return;
3145 }
3146 }
3147
3148 if (join < 0)
3149 join = 0;
3150
3151 wpa_s->p2p_auto_pd = 0;
3152 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3153 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3154 MAC2STR(wpa_s->pending_join_dev_addr), join);
3155 if (p2p_prov_disc_req(wpa_s->global->p2p,
3156 wpa_s->pending_join_dev_addr,
3157 wpa_s->pending_pd_config_methods, join,
3158 0) < 0) {
3159 wpa_s->p2p_auto_pd = 0;
3160 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3161 " p2p_dev_addr=" MACSTR " status=N/A",
3162 MAC2STR(wpa_s->pending_join_dev_addr));
3163 }
3164 return;
3165 }
3166
3167 if (wpa_s->p2p_auto_join) {
3168 int join = wpas_p2p_peer_go(wpa_s,
3169 wpa_s->pending_join_dev_addr);
3170 if (join < 0) {
3171 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3172 "running a GO -> use GO Negotiation");
3173 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3174 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3175 wpa_s->p2p_persistent_group, 0, 0, 0,
3176 wpa_s->p2p_go_intent,
3177 wpa_s->p2p_connect_freq,
3178 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003179 wpa_s->p2p_pd_before_go_neg,
3180 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003181 return;
3182 }
3183
3184 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3185 "try to join the group", join ? "" :
3186 " in older scan");
3187 if (!join)
3188 wpa_s->p2p_fallback_to_go_neg = 1;
3189 }
3190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003191 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3192 wpa_s->pending_join_iface_addr);
3193 if (freq < 0 &&
3194 p2p_get_interface_addr(wpa_s->global->p2p,
3195 wpa_s->pending_join_dev_addr,
3196 iface_addr) == 0 &&
3197 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3198 {
3199 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3200 "address for join from " MACSTR " to " MACSTR
3201 " based on newly discovered P2P peer entry",
3202 MAC2STR(wpa_s->pending_join_iface_addr),
3203 MAC2STR(iface_addr));
3204 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3205 ETH_ALEN);
3206
3207 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3208 wpa_s->pending_join_iface_addr);
3209 }
3210 if (freq >= 0) {
3211 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3212 "from P2P peer table: %d MHz", freq);
3213 }
3214 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3215 if (bss) {
3216 freq = bss->freq;
3217 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3218 "from BSS table: %d MHz", freq);
3219 }
3220 if (freq > 0) {
3221 u16 method;
3222
Dmitry Shmidt04949592012-07-19 12:16:46 -07003223 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3224 wpa_msg(wpa_s->parent, MSG_INFO,
3225 P2P_EVENT_GROUP_FORMATION_FAILURE
3226 "reason=FREQ_CONFLICT");
3227 return;
3228 }
3229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003230 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3231 "prior to joining an existing group (GO " MACSTR
3232 " freq=%u MHz)",
3233 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3234 wpa_s->pending_pd_before_join = 1;
3235
3236 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237 case WPS_PIN_DISPLAY:
3238 method = WPS_CONFIG_KEYPAD;
3239 break;
3240 case WPS_PIN_KEYPAD:
3241 method = WPS_CONFIG_DISPLAY;
3242 break;
3243 case WPS_PBC:
3244 method = WPS_CONFIG_PUSHBUTTON;
3245 break;
3246 default:
3247 method = 0;
3248 break;
3249 }
3250
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003251 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3252 wpa_s->pending_join_dev_addr) ==
3253 method)) {
3254 /*
3255 * We have already performed provision discovery for
3256 * joining the group. Proceed directly to join
3257 * operation without duplicated provision discovery. */
3258 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3259 "with " MACSTR " already done - proceed to "
3260 "join",
3261 MAC2STR(wpa_s->pending_join_dev_addr));
3262 wpa_s->pending_pd_before_join = 0;
3263 goto start;
3264 }
3265
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003267 wpa_s->pending_join_dev_addr, method, 1,
3268 freq) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3270 "Discovery Request before joining an "
3271 "existing group");
3272 wpa_s->pending_pd_before_join = 0;
3273 goto start;
3274 }
3275
3276 /*
3277 * Actual join operation will be started from the Action frame
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003278 * TX status callback (if no ACK is received) or when the
3279 * Provision Discovery Response is received. Use a short
3280 * timeout as a backup mechanism should the Provision Discovery
3281 * Response be lost for any reason.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003283 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3284 NULL);
3285 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3286 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003287 return;
3288 }
3289
3290 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3291 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3292 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3293 wpas_p2p_check_join_scan_limit(wpa_s);
3294 return;
3295
3296start:
3297 /* Start join operation immediately */
3298 wpas_p2p_join_start(wpa_s);
3299}
3300
3301
Dmitry Shmidt04949592012-07-19 12:16:46 -07003302static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 int ret;
3305 struct wpa_driver_scan_params params;
3306 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003307 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003308 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003309#ifdef ANDROID_P2P
3310 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003312 /* If freq is not provided, check the operating freq of the GO and do a
3313 * a directed scan to save time
3314 */
3315 if(!freq) {
3316 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3317 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3318 }
3319#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320 os_memset(&params, 0, sizeof(params));
3321
3322 /* P2P Wildcard SSID */
3323 params.num_ssids = 1;
3324 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3325 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3326
3327 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003328 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3329 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3330 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331 if (wps_ie == NULL) {
3332 wpas_p2p_scan_res_join(wpa_s, NULL);
3333 return;
3334 }
3335
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003336 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3337 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338 if (ies == NULL) {
3339 wpabuf_free(wps_ie);
3340 wpas_p2p_scan_res_join(wpa_s, NULL);
3341 return;
3342 }
3343 wpabuf_put_buf(ies, wps_ie);
3344 wpabuf_free(wps_ie);
3345
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003346 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003347
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003348 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003349 params.extra_ies = wpabuf_head(ies);
3350 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003351 if (freq > 0) {
3352 freqs[0] = freq;
3353 params.freqs = freqs;
3354 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003355
3356 /*
3357 * Run a scan to update BSS table and start Provision Discovery once
3358 * the new scan results become available.
3359 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003360 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003361 if (!ret)
3362 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363
3364 wpabuf_free(ies);
3365
3366 if (ret) {
3367 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3368 "try again later");
3369 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3370 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3371 wpas_p2p_check_join_scan_limit(wpa_s);
3372 }
3373}
3374
3375
Dmitry Shmidt04949592012-07-19 12:16:46 -07003376static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3377{
3378 struct wpa_supplicant *wpa_s = eloop_ctx;
3379 wpas_p2p_join_scan_req(wpa_s, 0);
3380}
3381
3382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003384 const u8 *dev_addr, enum p2p_wps_method wps_method,
3385 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386{
3387 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003388 MACSTR " dev " MACSTR ")%s",
3389 MAC2STR(iface_addr), MAC2STR(dev_addr),
3390 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391
Dmitry Shmidt04949592012-07-19 12:16:46 -07003392 wpa_s->p2p_auto_pd = 0;
3393 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3395 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3396 wpa_s->pending_join_wps_method = wps_method;
3397
3398 /* Make sure we are not running find during connection establishment */
3399 wpas_p2p_stop_find(wpa_s);
3400
3401 wpa_s->p2p_join_scan_count = 0;
3402 wpas_p2p_join_scan(wpa_s, NULL);
3403 return 0;
3404}
3405
3406
3407static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3408{
3409 struct wpa_supplicant *group;
3410 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003411 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003412
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003413 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3415 if (group == NULL)
3416 return -1;
3417 if (group != wpa_s) {
3418 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3419 sizeof(group->p2p_pin));
3420 group->p2p_wps_method = wpa_s->p2p_wps_method;
3421 }
3422
3423 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003424 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003425
3426 os_memset(&res, 0, sizeof(res));
3427 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3428 ETH_ALEN);
3429 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003430 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3431 if (bss) {
3432 res.freq = bss->freq;
3433 res.ssid_len = bss->ssid_len;
3434 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3435 }
3436
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003437 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3438 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3439 "starting client");
3440 wpa_drv_cancel_remain_on_channel(wpa_s);
3441 wpa_s->off_channel_freq = 0;
3442 wpa_s->roc_waiting_drv_freq = 0;
3443 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444 wpas_start_wps_enrollee(group, &res);
3445
3446 /*
3447 * Allow a longer timeout for join-a-running-group than normal 15
3448 * second group formation timeout since the GO may not have authorized
3449 * our connection yet.
3450 */
3451 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3452 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3453 wpa_s, NULL);
3454
3455 return 0;
3456}
3457
3458
3459/**
3460 * wpas_p2p_connect - Request P2P Group Formation to be started
3461 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3462 * @peer_addr: Address of the peer P2P Device
3463 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3464 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003465 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003466 * @join: Whether to join an existing group (as a client) instead of starting
3467 * Group Owner negotiation; @peer_addr is BSSID in that case
3468 * @auth: Whether to only authorize the connection instead of doing that and
3469 * initiating Group Owner negotiation
3470 * @go_intent: GO Intent or -1 to use default
3471 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003472 * @persistent_id: Persistent group credentials to use for forcing GO
3473 * parameters or -1 to generate new values (SSID/passphrase)
3474 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3475 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003476 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003477 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3478 * failure, -2 on failure due to channel not currently available,
3479 * -3 if forced channel is not supported
3480 */
3481int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3482 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003483 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003484 int go_intent, int freq, int persistent_id, int pd,
3485 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003486{
3487 int force_freq = 0, oper_freq = 0;
3488 u8 bssid[ETH_ALEN];
3489 int ret = 0;
3490 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003491 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003492 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003493
3494 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3495 return -1;
3496
Dmitry Shmidt04949592012-07-19 12:16:46 -07003497 if (persistent_id >= 0) {
3498 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3499 if (ssid == NULL || ssid->disabled != 2 ||
3500 ssid->mode != WPAS_MODE_P2P_GO)
3501 return -1;
3502 }
3503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504 if (go_intent < 0)
3505 go_intent = wpa_s->conf->p2p_go_intent;
3506
3507 if (!auth)
3508 wpa_s->p2p_long_listen = 0;
3509
3510 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003511 wpa_s->p2p_persistent_group = !!persistent_group;
3512 wpa_s->p2p_persistent_id = persistent_id;
3513 wpa_s->p2p_go_intent = go_intent;
3514 wpa_s->p2p_connect_freq = freq;
3515 wpa_s->p2p_fallback_to_go_neg = 0;
3516 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003517 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003518
3519 if (pin)
3520 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3521 else if (wps_method == WPS_PIN_DISPLAY) {
3522 ret = wps_generate_pin();
3523 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3524 ret);
3525 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3526 wpa_s->p2p_pin);
3527 } else
3528 wpa_s->p2p_pin[0] = '\0';
3529
Dmitry Shmidt04949592012-07-19 12:16:46 -07003530 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3532 if (auth) {
3533 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3534 "connect a running group from " MACSTR,
3535 MAC2STR(peer_addr));
3536 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3537 return ret;
3538 }
3539 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3540 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3541 iface_addr) < 0) {
3542 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3543 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3544 dev_addr);
3545 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003546 if (auto_join) {
3547 os_get_time(&wpa_s->p2p_auto_started);
3548 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3549 "%ld.%06ld",
3550 wpa_s->p2p_auto_started.sec,
3551 wpa_s->p2p_auto_started.usec);
3552 }
3553 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3554 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003555 return -1;
3556 return ret;
3557 }
3558
3559 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3560 wpa_s->assoc_freq)
3561 oper_freq = wpa_s->assoc_freq;
3562 else {
3563 oper_freq = wpa_drv_shared_freq(wpa_s);
3564 if (oper_freq < 0)
3565 oper_freq = 0;
3566 }
3567
3568 if (freq > 0) {
3569 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3570 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3571 "(%u MHz) is not supported for P2P uses",
3572 freq);
3573 return -3;
3574 }
3575
3576 if (oper_freq > 0 && freq != oper_freq &&
3577 !(wpa_s->drv_flags &
3578 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3579 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3580 "on %u MHz while connected on another "
3581 "channel (%u MHz)", freq, oper_freq);
3582 return -2;
3583 }
3584 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3585 "requested channel (%u MHz)", freq);
3586 force_freq = freq;
3587 } else if (oper_freq > 0 &&
3588 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3589 if (!(wpa_s->drv_flags &
3590 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3591 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3592 "while connected on non-P2P supported "
3593 "channel (%u MHz)", oper_freq);
3594 return -2;
3595 }
3596 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3597 "(%u MHz) not available for P2P - try to use "
3598 "another channel", oper_freq);
3599 force_freq = 0;
3600 } else if (oper_freq > 0) {
3601 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3602 "channel we are already using (%u MHz) on another "
3603 "interface", oper_freq);
3604 force_freq = oper_freq;
3605 }
3606
3607 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3608
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003609 if (wpa_s->create_p2p_iface) {
3610 /* Prepare to add a new interface for the group */
3611 iftype = WPA_IF_P2P_GROUP;
3612 if (go_intent == 15)
3613 iftype = WPA_IF_P2P_GO;
3614 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3615 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3616 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003618 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003620 if_addr = wpa_s->pending_interface_addr;
3621 } else
3622 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623
3624 if (auth) {
3625 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003626 go_intent, if_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003627 force_freq, persistent_group, ssid) <
3628 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 return -1;
3630 return ret;
3631 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003632
3633 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3634 go_intent, if_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003635 persistent_group, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003636 if (wpa_s->create_p2p_iface)
3637 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003638 return -1;
3639 }
3640 return ret;
3641}
3642
3643
3644/**
3645 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3646 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3647 * @freq: Frequency of the channel in MHz
3648 * @duration: Duration of the stay on the channel in milliseconds
3649 *
3650 * This callback is called when the driver indicates that it has started the
3651 * requested remain-on-channel duration.
3652 */
3653void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3654 unsigned int freq, unsigned int duration)
3655{
3656 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3657 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3659 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3660 wpa_s->pending_listen_duration);
3661 wpa_s->pending_listen_freq = 0;
3662 }
3663}
3664
3665
3666static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3667 unsigned int timeout)
3668{
3669 /* Limit maximum Listen state time based on driver limitation. */
3670 if (timeout > wpa_s->max_remain_on_chan)
3671 timeout = wpa_s->max_remain_on_chan;
3672
3673 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3674 return wpa_drv_p2p_listen(wpa_s, timeout);
3675
3676 return p2p_listen(wpa_s->global->p2p, timeout);
3677}
3678
3679
3680/**
3681 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3682 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3683 * @freq: Frequency of the channel in MHz
3684 *
3685 * This callback is called when the driver indicates that a remain-on-channel
3686 * operation has been completed, i.e., the duration on the requested channel
3687 * has timed out.
3688 */
3689void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3690 unsigned int freq)
3691{
3692 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3693 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3694 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3696 return;
3697 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3698 return; /* P2P module started a new operation */
3699 if (wpa_s->pending_action_tx)
3700 return;
3701 if (wpa_s->p2p_long_listen > 0)
3702 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3703 if (wpa_s->p2p_long_listen > 0) {
3704 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3705 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3706 }
3707}
3708
3709
3710/**
3711 * wpas_p2p_group_remove - Remove a P2P group
3712 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3713 * @ifname: Network interface name of the group interface or "*" to remove all
3714 * groups
3715 * Returns: 0 on success, -1 on failure
3716 *
3717 * This function is used to remove a P2P group. This can be used to disconnect
3718 * from a group in which the local end is a P2P Client or to end a P2P Group in
3719 * case the local end is the Group Owner. If a virtual network interface was
3720 * created for this group, that interface will be removed. Otherwise, only the
3721 * configured P2P group network will be removed from the interface.
3722 */
3723int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3724{
3725 struct wpa_global *global = wpa_s->global;
3726
3727 if (os_strcmp(ifname, "*") == 0) {
3728 struct wpa_supplicant *prev;
3729 wpa_s = global->ifaces;
3730 while (wpa_s) {
3731 prev = wpa_s;
3732 wpa_s = wpa_s->next;
3733 wpas_p2p_disconnect(prev);
3734 }
3735 return 0;
3736 }
3737
3738 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3739 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3740 break;
3741 }
3742
3743 return wpas_p2p_disconnect(wpa_s);
3744}
3745
3746
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003747static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3748 struct p2p_go_neg_results *params,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003749 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003750{
3751 u8 bssid[ETH_ALEN];
3752 int res;
3753
3754 os_memset(params, 0, sizeof(*params));
3755 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003756 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003757 if (freq) {
3758 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3759 "frequency %d MHz", freq);
3760 params->freq = freq;
3761 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3762 wpa_s->conf->p2p_oper_channel >= 1 &&
3763 wpa_s->conf->p2p_oper_channel <= 11) {
3764 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3765 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3766 "frequency %d MHz", params->freq);
3767 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003768 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003769 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3770 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3771 "frequency %d MHz", params->freq);
3772 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3773 wpa_s->best_overall_freq > 0 &&
3774 p2p_supported_freq(wpa_s->global->p2p,
3775 wpa_s->best_overall_freq)) {
3776 params->freq = wpa_s->best_overall_freq;
3777 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3778 "channel %d MHz", params->freq);
3779 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3780 wpa_s->best_24_freq > 0 &&
3781 p2p_supported_freq(wpa_s->global->p2p,
3782 wpa_s->best_24_freq)) {
3783 params->freq = wpa_s->best_24_freq;
3784 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3785 "channel %d MHz", params->freq);
3786 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3787 wpa_s->best_5_freq > 0 &&
3788 p2p_supported_freq(wpa_s->global->p2p,
3789 wpa_s->best_5_freq)) {
3790 params->freq = wpa_s->best_5_freq;
3791 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3792 "channel %d MHz", params->freq);
3793 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003794 int chan;
3795 for (chan = 0; chan < 11; chan++) {
3796 params->freq = 2412 + chan * 5;
3797 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3798 params->freq))
3799 break;
3800 }
3801 if (chan == 11) {
3802 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3803 "allowed");
3804 return -1;
3805 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3807 "known)", params->freq);
3808 }
3809
3810 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3811 wpa_s->assoc_freq && !freq) {
3812 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3813 "already using");
3814 params->freq = wpa_s->assoc_freq;
3815 }
3816
3817 res = wpa_drv_shared_freq(wpa_s);
3818 if (res > 0 && !freq) {
3819 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3820 "already using on a shared interface");
3821 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003822 } else if (res > 0 && freq != res &&
3823 !(wpa_s->drv_flags &
3824 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3825 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3826 "while connected on another channel (%u MHz)",
3827 freq, res);
3828 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003829 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003830
3831 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832}
3833
3834
3835static struct wpa_supplicant *
3836wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3837 int go)
3838{
3839 struct wpa_supplicant *group_wpa_s;
3840
3841 if (!wpas_p2p_create_iface(wpa_s))
3842 return wpa_s;
3843
3844 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3845 WPA_IF_P2P_CLIENT) < 0)
3846 return NULL;
3847 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3848 if (group_wpa_s == NULL) {
3849 wpas_p2p_remove_pending_group_interface(wpa_s);
3850 return NULL;
3851 }
3852
3853 return group_wpa_s;
3854}
3855
3856
3857/**
3858 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3859 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3860 * @persistent_group: Whether to create a persistent group
3861 * @freq: Frequency for the group or 0 to indicate no hardcoding
3862 * Returns: 0 on success, -1 on failure
3863 *
3864 * This function creates a new P2P group with the local end as the Group Owner,
3865 * i.e., without using Group Owner Negotiation.
3866 */
3867int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003868 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003869{
3870 struct p2p_go_neg_results params;
3871 unsigned int r;
3872
3873 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3874 return -1;
3875
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003876 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003877 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003878 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003880 if (freq == 2) {
3881 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3882 "band");
3883 if (wpa_s->best_24_freq > 0 &&
3884 p2p_supported_freq(wpa_s->global->p2p,
3885 wpa_s->best_24_freq)) {
3886 freq = wpa_s->best_24_freq;
3887 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3888 "channel: %d MHz", freq);
3889 } else {
3890 os_get_random((u8 *) &r, sizeof(r));
3891 freq = 2412 + (r % 3) * 25;
3892 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3893 "channel: %d MHz", freq);
3894 }
3895 }
3896
3897 if (freq == 5) {
3898 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3899 "band");
3900 if (wpa_s->best_5_freq > 0 &&
3901 p2p_supported_freq(wpa_s->global->p2p,
3902 wpa_s->best_5_freq)) {
3903 freq = wpa_s->best_5_freq;
3904 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3905 "channel: %d MHz", freq);
3906 } else {
3907 os_get_random((u8 *) &r, sizeof(r));
3908 freq = 5180 + (r % 4) * 20;
3909 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3910 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3911 "5 GHz channel for P2P group");
3912 return -1;
3913 }
3914 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3915 "channel: %d MHz", freq);
3916 }
3917 }
3918
3919 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3920 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3921 "(%u MHz) is not supported for P2P uses",
3922 freq);
3923 return -1;
3924 }
3925
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003926 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003927 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003928 if (params.freq &&
3929 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3930 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3931 "(%u MHz) is not supported for P2P uses",
3932 params.freq);
3933 return -1;
3934 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003935 p2p_go_params(wpa_s->global->p2p, &params);
3936 params.persistent_group = persistent_group;
3937
3938 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3939 if (wpa_s == NULL)
3940 return -1;
3941 wpas_start_wps_go(wpa_s, &params, 0);
3942
3943 return 0;
3944}
3945
3946
3947static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3948 struct wpa_ssid *params, int addr_allocated)
3949{
3950 struct wpa_ssid *ssid;
3951
3952 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3953 if (wpa_s == NULL)
3954 return -1;
3955
3956 wpa_supplicant_ap_deinit(wpa_s);
3957
3958 ssid = wpa_config_add_network(wpa_s->conf);
3959 if (ssid == NULL)
3960 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003961 wpa_config_set_network_defaults(ssid);
3962 ssid->temporary = 1;
3963 ssid->proto = WPA_PROTO_RSN;
3964 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3965 ssid->group_cipher = WPA_CIPHER_CCMP;
3966 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3967 ssid->ssid = os_malloc(params->ssid_len);
3968 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969 wpa_config_remove_network(wpa_s->conf, ssid->id);
3970 return -1;
3971 }
3972 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3973 ssid->ssid_len = params->ssid_len;
3974 ssid->p2p_group = 1;
3975 ssid->export_keys = 1;
3976 if (params->psk_set) {
3977 os_memcpy(ssid->psk, params->psk, 32);
3978 ssid->psk_set = 1;
3979 }
3980 if (params->passphrase)
3981 ssid->passphrase = os_strdup(params->passphrase);
3982
3983 wpa_supplicant_select_network(wpa_s, ssid);
3984
3985 wpa_s->show_group_started = 1;
3986
3987 return 0;
3988}
3989
3990
3991int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3992 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003993 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994{
3995 struct p2p_go_neg_results params;
3996 int go = 0;
3997
3998 if (ssid->disabled != 2 || ssid->ssid == NULL)
3999 return -1;
4000
4001 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4002 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4003 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4004 "already running");
4005 return 0;
4006 }
4007
4008 /* Make sure we are not running find during connection establishment */
4009 wpas_p2p_stop_find(wpa_s);
4010
Dmitry Shmidt04949592012-07-19 12:16:46 -07004011 wpa_s->p2p_fallback_to_go_neg = 0;
4012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004013 if (ssid->mode == WPAS_MODE_INFRA)
4014 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4015
4016 if (ssid->mode != WPAS_MODE_P2P_GO)
4017 return -1;
4018
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004019 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004020 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021
4022 params.role_go = 1;
4023 if (ssid->passphrase == NULL ||
4024 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4025 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4026 "group");
4027 return -1;
4028 }
4029 os_strlcpy(params.passphrase, ssid->passphrase,
4030 sizeof(params.passphrase));
4031 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4032 params.ssid_len = ssid->ssid_len;
4033 params.persistent_group = 1;
4034
4035 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4036 if (wpa_s == NULL)
4037 return -1;
4038
4039 wpas_start_wps_go(wpa_s, &params, 0);
4040
4041 return 0;
4042}
4043
4044
4045static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4046 struct wpabuf *proberesp_ies)
4047{
4048 struct wpa_supplicant *wpa_s = ctx;
4049 if (wpa_s->ap_iface) {
4050 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004051 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4052 wpabuf_free(beacon_ies);
4053 wpabuf_free(proberesp_ies);
4054 return;
4055 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004056 if (beacon_ies) {
4057 wpabuf_free(hapd->p2p_beacon_ie);
4058 hapd->p2p_beacon_ie = beacon_ies;
4059 }
4060 wpabuf_free(hapd->p2p_probe_resp_ie);
4061 hapd->p2p_probe_resp_ie = proberesp_ies;
4062 } else {
4063 wpabuf_free(beacon_ies);
4064 wpabuf_free(proberesp_ies);
4065 }
4066 wpa_supplicant_ap_update_beacon(wpa_s);
4067}
4068
4069
4070static void wpas_p2p_idle_update(void *ctx, int idle)
4071{
4072 struct wpa_supplicant *wpa_s = ctx;
4073 if (!wpa_s->ap_iface)
4074 return;
4075 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004076 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 wpas_p2p_set_group_idle_timeout(wpa_s);
4078 else
4079 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4080}
4081
4082
4083struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004084 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085{
4086 struct p2p_group *group;
4087 struct p2p_group_config *cfg;
4088
4089 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4090 return NULL;
4091 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4092 return NULL;
4093
4094 cfg = os_zalloc(sizeof(*cfg));
4095 if (cfg == NULL)
4096 return NULL;
4097
Dmitry Shmidt04949592012-07-19 12:16:46 -07004098 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004099 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004100 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004101 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4103 if (wpa_s->max_stations &&
4104 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4105 cfg->max_clients = wpa_s->max_stations;
4106 else
4107 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004108 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4109 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110 cfg->cb_ctx = wpa_s;
4111 cfg->ie_update = wpas_p2p_ie_update;
4112 cfg->idle_update = wpas_p2p_idle_update;
4113
4114 group = p2p_group_init(wpa_s->global->p2p, cfg);
4115 if (group == NULL)
4116 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004117 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004118 p2p_group_notif_formation_done(group);
4119 wpa_s->p2p_group = group;
4120 return group;
4121}
4122
4123
4124void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4125 int registrar)
4126{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004127 struct wpa_ssid *ssid = wpa_s->current_ssid;
4128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129 if (!wpa_s->p2p_in_provisioning) {
4130 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4131 "provisioning not in progress");
4132 return;
4133 }
4134
Dmitry Shmidt04949592012-07-19 12:16:46 -07004135 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4136 u8 go_dev_addr[ETH_ALEN];
4137 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4138 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4139 ssid->ssid_len);
4140 /* Clear any stored provisioning info */
4141 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4142 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4145 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004146 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4147 /*
4148 * Use a separate timeout for initial data connection to
4149 * complete to allow the group to be removed automatically if
4150 * something goes wrong in this step before the P2P group idle
4151 * timeout mechanism is taken into use.
4152 */
4153 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4154 wpas_p2p_group_formation_timeout,
4155 wpa_s->parent, NULL);
4156 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157 if (wpa_s->global->p2p)
4158 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4159 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4160 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4161 wpas_group_formation_completed(wpa_s, 1);
4162}
4163
4164
Jouni Malinen75ecf522011-06-27 15:19:46 -07004165void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4166 struct wps_event_fail *fail)
4167{
4168 if (!wpa_s->p2p_in_provisioning) {
4169 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4170 "provisioning not in progress");
4171 return;
4172 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004173
4174 if (wpa_s->go_params) {
4175 p2p_clear_provisioning_info(
4176 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004177 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004178 }
4179
Jouni Malinen75ecf522011-06-27 15:19:46 -07004180 wpas_notify_p2p_wps_failed(wpa_s, fail);
4181}
4182
4183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004184int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004185 const char *config_method,
4186 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004187{
4188 u16 config_methods;
4189
Dmitry Shmidt04949592012-07-19 12:16:46 -07004190 wpa_s->p2p_fallback_to_go_neg = 0;
4191 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004192 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004193 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004194 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004195 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004196 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4197 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004199 else {
4200 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004202 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004203
Dmitry Shmidt04949592012-07-19 12:16:46 -07004204 if (use == WPAS_P2P_PD_AUTO) {
4205 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4206 wpa_s->pending_pd_config_methods = config_methods;
4207 wpa_s->p2p_auto_pd = 1;
4208 wpa_s->p2p_auto_join = 0;
4209 wpa_s->pending_pd_before_join = 0;
4210 wpa_s->auto_pd_scan_retry = 0;
4211 wpas_p2p_stop_find(wpa_s);
4212 wpa_s->p2p_join_scan_count = 0;
4213 os_get_time(&wpa_s->p2p_auto_started);
4214 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4215 wpa_s->p2p_auto_started.sec,
4216 wpa_s->p2p_auto_started.usec);
4217 wpas_p2p_join_scan(wpa_s, NULL);
4218 return 0;
4219 }
4220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004221 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4222 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004223 config_methods,
4224 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225 }
4226
4227 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4228 return -1;
4229
4230 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004231 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4232 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004233}
4234
4235
4236int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4237 char *end)
4238{
4239 return p2p_scan_result_text(ies, ies_len, buf, end);
4240}
4241
4242
4243static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4244{
4245 if (!wpa_s->pending_action_tx)
4246 return;
4247
4248 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4249 "operation request");
4250 wpabuf_free(wpa_s->pending_action_tx);
4251 wpa_s->pending_action_tx = NULL;
4252}
4253
4254
4255int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4256 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004257 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004258 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259{
4260 wpas_p2p_clear_pending_action_tx(wpa_s);
4261 wpa_s->p2p_long_listen = 0;
4262
4263 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4264 return wpa_drv_p2p_find(wpa_s, timeout, type);
4265
Dmitry Shmidt04949592012-07-19 12:16:46 -07004266 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4267 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004268 return -1;
4269
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004270 wpa_supplicant_cancel_sched_scan(wpa_s);
4271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004272 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004273 num_req_dev_types, req_dev_types, dev_id,
4274 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004275}
4276
4277
4278void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4279{
4280 wpas_p2p_clear_pending_action_tx(wpa_s);
4281 wpa_s->p2p_long_listen = 0;
4282 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4283 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004284 wpa_s->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004285
4286 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4287 wpa_drv_p2p_stop_find(wpa_s);
4288 return;
4289 }
4290
4291 if (wpa_s->global->p2p)
4292 p2p_stop_find(wpa_s->global->p2p);
4293
4294 wpas_p2p_remove_pending_group_interface(wpa_s);
4295}
4296
4297
4298static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4299{
4300 struct wpa_supplicant *wpa_s = eloop_ctx;
4301 wpa_s->p2p_long_listen = 0;
4302}
4303
4304
4305int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4306{
4307 int res;
4308
4309 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4310 return -1;
4311
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004312 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004313 wpas_p2p_clear_pending_action_tx(wpa_s);
4314
4315 if (timeout == 0) {
4316 /*
4317 * This is a request for unlimited Listen state. However, at
4318 * least for now, this is mapped to a Listen state for one
4319 * hour.
4320 */
4321 timeout = 3600;
4322 }
4323 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4324 wpa_s->p2p_long_listen = 0;
4325
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004326 /*
4327 * Stop previous find/listen operation to avoid trying to request a new
4328 * remain-on-channel operation while the driver is still running the
4329 * previous one.
4330 */
4331 if (wpa_s->global->p2p)
4332 p2p_stop_find(wpa_s->global->p2p);
4333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4335 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4336 wpa_s->p2p_long_listen = timeout * 1000;
4337 eloop_register_timeout(timeout, 0,
4338 wpas_p2p_long_listen_timeout,
4339 wpa_s, NULL);
4340 }
4341
4342 return res;
4343}
4344
4345
4346int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4347 u8 *buf, size_t len, int p2p_group)
4348{
4349 struct wpabuf *p2p_ie;
4350 int ret;
4351
4352 if (wpa_s->global->p2p_disabled)
4353 return -1;
4354 if (wpa_s->global->p2p == NULL)
4355 return -1;
4356 if (bss == NULL)
4357 return -1;
4358
4359 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4360 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4361 p2p_group, p2p_ie);
4362 wpabuf_free(p2p_ie);
4363
4364 return ret;
4365}
4366
4367
4368int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004369 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004370 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004371{
4372 if (wpa_s->global->p2p_disabled)
4373 return 0;
4374 if (wpa_s->global->p2p == NULL)
4375 return 0;
4376
Dmitry Shmidt04949592012-07-19 12:16:46 -07004377 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4378 ie, ie_len)) {
4379 case P2P_PREQ_NOT_P2P:
4380 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4381 ssi_signal);
4382 /* fall through */
4383 case P2P_PREQ_MALFORMED:
4384 case P2P_PREQ_NOT_LISTEN:
4385 case P2P_PREQ_NOT_PROCESSED:
4386 default: /* make gcc happy */
4387 return 0;
4388 case P2P_PREQ_PROCESSED:
4389 return 1;
4390 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391}
4392
4393
4394void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4395 const u8 *sa, const u8 *bssid,
4396 u8 category, const u8 *data, size_t len, int freq)
4397{
4398 if (wpa_s->global->p2p_disabled)
4399 return;
4400 if (wpa_s->global->p2p == NULL)
4401 return;
4402
4403 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4404 freq);
4405}
4406
4407
4408void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4409{
4410 if (wpa_s->global->p2p_disabled)
4411 return;
4412 if (wpa_s->global->p2p == NULL)
4413 return;
4414
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004415 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004416}
4417
4418
4419void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4420{
4421 p2p_group_deinit(wpa_s->p2p_group);
4422 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004423
4424 wpa_s->ap_configured_cb = NULL;
4425 wpa_s->ap_configured_cb_ctx = NULL;
4426 wpa_s->ap_configured_cb_data = NULL;
4427 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428}
4429
4430
4431int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4432{
4433 wpa_s->p2p_long_listen = 0;
4434
4435 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4436 return wpa_drv_p2p_reject(wpa_s, addr);
4437
4438 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4439 return -1;
4440
4441 return p2p_reject(wpa_s->global->p2p, addr);
4442}
4443
4444
4445/* Invite to reinvoke a persistent group */
4446int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004447 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4448 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004449{
4450 enum p2p_invite_role role;
4451 u8 *bssid = NULL;
4452
Jouni Malinen31be0a42012-08-31 21:20:51 +03004453 wpa_s->p2p_persistent_go_freq = freq;
4454 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004455 if (ssid->mode == WPAS_MODE_P2P_GO) {
4456 role = P2P_INVITE_ROLE_GO;
4457 if (peer_addr == NULL) {
4458 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4459 "address in invitation command");
4460 return -1;
4461 }
4462 if (wpas_p2p_create_iface(wpa_s)) {
4463 if (wpas_p2p_add_group_interface(wpa_s,
4464 WPA_IF_P2P_GO) < 0) {
4465 wpa_printf(MSG_ERROR, "P2P: Failed to "
4466 "allocate a new interface for the "
4467 "group");
4468 return -1;
4469 }
4470 bssid = wpa_s->pending_interface_addr;
4471 } else
4472 bssid = wpa_s->own_addr;
4473 } else {
4474 role = P2P_INVITE_ROLE_CLIENT;
4475 peer_addr = ssid->bssid;
4476 }
4477 wpa_s->pending_invite_ssid_id = ssid->id;
4478
4479 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4480 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4481 ssid->ssid, ssid->ssid_len,
4482 go_dev_addr, 1);
4483
4484 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4485 return -1;
4486
4487 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004488 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489}
4490
4491
4492/* Invite to join an active group */
4493int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4494 const u8 *peer_addr, const u8 *go_dev_addr)
4495{
4496 struct wpa_global *global = wpa_s->global;
4497 enum p2p_invite_role role;
4498 u8 *bssid = NULL;
4499 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004500 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501
Jouni Malinen31be0a42012-08-31 21:20:51 +03004502 wpa_s->p2p_persistent_go_freq = 0;
4503 wpa_s->p2p_go_ht40 = 0;
4504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4506 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4507 break;
4508 }
4509 if (wpa_s == NULL) {
4510 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4511 return -1;
4512 }
4513
4514 ssid = wpa_s->current_ssid;
4515 if (ssid == NULL) {
4516 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4517 "invitation");
4518 return -1;
4519 }
4520
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004521 persistent = ssid->p2p_persistent_group &&
4522 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4523 ssid->ssid, ssid->ssid_len);
4524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004525 if (ssid->mode == WPAS_MODE_P2P_GO) {
4526 role = P2P_INVITE_ROLE_ACTIVE_GO;
4527 bssid = wpa_s->own_addr;
4528 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004529 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004530 } else {
4531 role = P2P_INVITE_ROLE_CLIENT;
4532 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4533 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4534 "invite to current group");
4535 return -1;
4536 }
4537 bssid = wpa_s->bssid;
4538 if (go_dev_addr == NULL &&
4539 !is_zero_ether_addr(wpa_s->go_dev_addr))
4540 go_dev_addr = wpa_s->go_dev_addr;
4541 }
4542 wpa_s->parent->pending_invite_ssid_id = -1;
4543
4544 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4545 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4546 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004547 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548
4549 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4550 return -1;
4551
4552 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4553 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004554 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004555}
4556
4557
4558void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4559{
4560 struct wpa_ssid *ssid = wpa_s->current_ssid;
4561 const char *ssid_txt;
4562 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004563 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004565 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566
Dmitry Shmidt04949592012-07-19 12:16:46 -07004567 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4568 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4569 wpa_s->parent, NULL);
4570 }
4571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004573 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004574
4575 wpa_s->show_group_started = 0;
4576
4577 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4578 os_memset(go_dev_addr, 0, ETH_ALEN);
4579 if (ssid->bssid_set)
4580 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4581 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4582 ssid->ssid_len);
4583 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4584
4585 if (wpa_s->global->p2p_group_formation == wpa_s)
4586 wpa_s->global->p2p_group_formation = NULL;
4587
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004588 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4589 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004590 if (ssid->passphrase == NULL && ssid->psk_set) {
4591 char psk[65];
4592 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4593 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4594 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4595 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004596 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004597 MAC2STR(go_dev_addr),
4598 persistent ? " [PERSISTENT]" : "");
4599 } else {
4600 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4601 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4602 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004603 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604 ssid->passphrase ? ssid->passphrase : "",
4605 MAC2STR(go_dev_addr),
4606 persistent ? " [PERSISTENT]" : "");
4607 }
4608
4609 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004610 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4611 ssid, go_dev_addr);
4612 if (network_id < 0)
4613 network_id = ssid->id;
4614 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004615
4616done:
4617 if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4618 wpa_s->global->p2p != NULL) {
4619 wpa_s->p2p_cb_on_scan_complete = 0;
4620 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4621 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4622 "continued after successful connection");
4623 p2p_increase_search_delay(
4624 wpa_s->global->p2p,
4625 wpas_p2p_search_delay(wpa_s));
4626 }
4627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628}
4629
4630
4631int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4632 u32 interval1, u32 duration2, u32 interval2)
4633{
4634 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4635 return -1;
4636 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4637 return -1;
4638
4639 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4640 wpa_s->current_ssid == NULL ||
4641 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4642 return -1;
4643
4644 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4645 wpa_s->own_addr, wpa_s->assoc_freq,
4646 duration1, interval1, duration2, interval2);
4647}
4648
4649
4650int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4651 unsigned int interval)
4652{
4653 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4654 return -1;
4655
4656 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4657 return -1;
4658
4659 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4660}
4661
4662
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004663static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4664{
4665 return wpa_s->current_ssid != NULL &&
4666 wpa_s->current_ssid->p2p_group &&
4667 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4668}
4669
4670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4672{
4673 struct wpa_supplicant *wpa_s = eloop_ctx;
4674
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004675 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4677 "disabled");
4678 return;
4679 }
4680
Dmitry Shmidt04949592012-07-19 12:16:46 -07004681 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4682 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004683 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004684}
4685
4686
4687static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4688{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004689 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690
Dmitry Shmidt04949592012-07-19 12:16:46 -07004691 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4692 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004694 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4695 return;
4696
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004697 timeout = wpa_s->conf->p2p_group_idle;
4698 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4699 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4700 timeout = P2P_MAX_CLIENT_IDLE;
4701
4702 if (timeout == 0)
4703 return;
4704
Dmitry Shmidt04949592012-07-19 12:16:46 -07004705 if (timeout < 0) {
4706 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4707 timeout = 0; /* special client mode no-timeout */
4708 else
4709 return;
4710 }
4711
4712 if (wpa_s->p2p_in_provisioning) {
4713 /*
4714 * Use the normal group formation timeout during the
4715 * provisioning phase to avoid terminating this process too
4716 * early due to group idle timeout.
4717 */
4718 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4719 "during provisioning");
4720 return;
4721 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004722#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07004723 if (wpa_s->show_group_started) {
4724 /*
4725 * Use the normal group formation timeout between the end of
4726 * the provisioning phase and completion of 4-way handshake to
4727 * avoid terminating this process too early due to group idle
4728 * timeout.
4729 */
4730 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4731 "while waiting for initial 4-way handshake to "
4732 "complete");
4733 return;
4734 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004735#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07004736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004738 timeout);
4739 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4740 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004741}
4742
4743
Jouni Malinen2b89da82012-08-31 22:04:41 +03004744/* Returns 1 if the interface was removed */
4745int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4746 u16 reason_code, const u8 *ie, size_t ie_len,
4747 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004748{
4749 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004750 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004751 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004752 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753
Dmitry Shmidt04949592012-07-19 12:16:46 -07004754 if (!locally_generated)
4755 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4756 ie_len);
4757
4758 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4759 wpa_s->current_ssid &&
4760 wpa_s->current_ssid->p2p_group &&
4761 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4762 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4763 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03004764 if (wpas_p2p_group_delete(wpa_s,
4765 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4766 > 0)
4767 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004768 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03004769
4770 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004771}
4772
4773
4774void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004775 u16 reason_code, const u8 *ie, size_t ie_len,
4776 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004777{
4778 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4779 return;
4780 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4781 return;
4782
Dmitry Shmidt04949592012-07-19 12:16:46 -07004783 if (!locally_generated)
4784 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4785 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004786}
4787
4788
4789void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4790{
4791 struct p2p_data *p2p = wpa_s->global->p2p;
4792
4793 if (p2p == NULL)
4794 return;
4795
4796 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4797 return;
4798
4799 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4800 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4801
4802 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4803 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4804
4805 if (wpa_s->wps &&
4806 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4807 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4808
4809 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4810 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4811
4812 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4813 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4814 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4815 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4816 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4817 }
4818
4819 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4820 p2p_set_sec_dev_types(p2p,
4821 (void *) wpa_s->conf->sec_device_type,
4822 wpa_s->conf->num_sec_device_types);
4823
4824 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4825 int i;
4826 p2p_remove_wps_vendor_extensions(p2p);
4827 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4828 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4829 continue;
4830 p2p_add_wps_vendor_extension(
4831 p2p, wpa_s->conf->wps_vendor_ext[i]);
4832 }
4833 }
4834
4835 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4836 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4837 char country[3];
4838 country[0] = wpa_s->conf->country[0];
4839 country[1] = wpa_s->conf->country[1];
4840 country[2] = 0x04;
4841 p2p_set_country(p2p, country);
4842 }
4843
4844 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4845 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4846 wpa_s->conf->p2p_ssid_postfix ?
4847 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4848 0);
4849 }
4850
4851 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4852 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004853
4854 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4855 u8 reg_class, channel;
4856 int ret;
4857 unsigned int r;
4858 if (wpa_s->conf->p2p_listen_reg_class &&
4859 wpa_s->conf->p2p_listen_channel) {
4860 reg_class = wpa_s->conf->p2p_listen_reg_class;
4861 channel = wpa_s->conf->p2p_listen_channel;
4862 } else {
4863 reg_class = 81;
4864 /*
4865 * Pick one of the social channels randomly as the
4866 * listen channel.
4867 */
4868 os_get_random((u8 *) &r, sizeof(r));
4869 channel = 1 + (r % 3) * 5;
4870 }
4871 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4872 if (ret)
4873 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4874 "failed: %d", ret);
4875 }
4876 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4877 u8 op_reg_class, op_channel, cfg_op_channel;
4878 int ret = 0;
4879 unsigned int r;
4880 if (wpa_s->conf->p2p_oper_reg_class &&
4881 wpa_s->conf->p2p_oper_channel) {
4882 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4883 op_channel = wpa_s->conf->p2p_oper_channel;
4884 cfg_op_channel = 1;
4885 } else {
4886 op_reg_class = 81;
4887 /*
4888 * Use random operation channel from (1, 6, 11)
4889 *if no other preference is indicated.
4890 */
4891 os_get_random((u8 *) &r, sizeof(r));
4892 op_channel = 1 + (r % 3) * 5;
4893 cfg_op_channel = 0;
4894 }
4895 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4896 cfg_op_channel);
4897 if (ret)
4898 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4899 "failed: %d", ret);
4900 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004901
4902 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4903 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4904 wpa_s->conf->p2p_pref_chan) < 0) {
4905 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4906 "update failed");
4907 }
4908 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909}
4910
4911
4912int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4913 int duration)
4914{
4915 if (!wpa_s->ap_iface)
4916 return -1;
4917 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4918 duration);
4919}
4920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921
4922int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4923{
4924 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4925 return -1;
4926 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4927 return -1;
4928
4929 wpa_s->global->cross_connection = enabled;
4930 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4931
4932 if (!enabled) {
4933 struct wpa_supplicant *iface;
4934
4935 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4936 {
4937 if (iface->cross_connect_enabled == 0)
4938 continue;
4939
4940 iface->cross_connect_enabled = 0;
4941 iface->cross_connect_in_use = 0;
4942 wpa_msg(iface->parent, MSG_INFO,
4943 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4944 iface->ifname, iface->cross_connect_uplink);
4945 }
4946 }
4947
4948 return 0;
4949}
4950
4951
4952static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4953{
4954 struct wpa_supplicant *iface;
4955
4956 if (!uplink->global->cross_connection)
4957 return;
4958
4959 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4960 if (!iface->cross_connect_enabled)
4961 continue;
4962 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4963 0)
4964 continue;
4965 if (iface->ap_iface == NULL)
4966 continue;
4967 if (iface->cross_connect_in_use)
4968 continue;
4969
4970 iface->cross_connect_in_use = 1;
4971 wpa_msg(iface->parent, MSG_INFO,
4972 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4973 iface->ifname, iface->cross_connect_uplink);
4974 }
4975}
4976
4977
4978static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4979{
4980 struct wpa_supplicant *iface;
4981
4982 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4983 if (!iface->cross_connect_enabled)
4984 continue;
4985 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4986 0)
4987 continue;
4988 if (!iface->cross_connect_in_use)
4989 continue;
4990
4991 wpa_msg(iface->parent, MSG_INFO,
4992 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4993 iface->ifname, iface->cross_connect_uplink);
4994 iface->cross_connect_in_use = 0;
4995 }
4996}
4997
4998
4999void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5000{
5001 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5002 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5003 wpa_s->cross_connect_disallowed)
5004 wpas_p2p_disable_cross_connect(wpa_s);
5005 else
5006 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005007 if (!wpa_s->ap_iface &&
5008 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5009 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010}
5011
5012
5013void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5014{
5015 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005016 if (!wpa_s->ap_iface &&
5017 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5018 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019 wpas_p2p_set_group_idle_timeout(wpa_s);
5020}
5021
5022
5023static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5024{
5025 struct wpa_supplicant *iface;
5026
5027 if (!wpa_s->global->cross_connection)
5028 return;
5029
5030 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5031 if (iface == wpa_s)
5032 continue;
5033 if (iface->drv_flags &
5034 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5035 continue;
5036 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5037 continue;
5038
5039 wpa_s->cross_connect_enabled = 1;
5040 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5041 sizeof(wpa_s->cross_connect_uplink));
5042 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5043 "%s to %s whenever uplink is available",
5044 wpa_s->ifname, wpa_s->cross_connect_uplink);
5045
5046 if (iface->ap_iface || iface->current_ssid == NULL ||
5047 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5048 iface->cross_connect_disallowed ||
5049 iface->wpa_state != WPA_COMPLETED)
5050 break;
5051
5052 wpa_s->cross_connect_in_use = 1;
5053 wpa_msg(wpa_s->parent, MSG_INFO,
5054 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5055 wpa_s->ifname, wpa_s->cross_connect_uplink);
5056 break;
5057 }
5058}
5059
5060
5061int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5062{
5063 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5064 !wpa_s->p2p_in_provisioning)
5065 return 0; /* not P2P client operation */
5066
5067 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5068 "session overlap");
5069 if (wpa_s != wpa_s->parent)
5070 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5071
5072 if (wpa_s->global->p2p)
5073 p2p_group_formation_failed(wpa_s->global->p2p);
5074
5075 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5076 wpa_s->parent, NULL);
5077
5078 wpas_group_formation_completed(wpa_s, 0);
5079 return 1;
5080}
5081
5082
5083void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5084{
5085 struct p2p_channels chan;
5086
5087 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5088 return;
5089
5090 os_memset(&chan, 0, sizeof(chan));
5091 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5092 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5093 "channel list");
5094 return;
5095 }
5096
5097 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5098}
5099
5100
5101int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5102{
5103 struct wpa_global *global = wpa_s->global;
5104 int found = 0;
5105 const u8 *peer;
5106
5107 if (global->p2p == NULL)
5108 return -1;
5109
5110 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5111
5112 if (wpa_s->pending_interface_name[0] &&
5113 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5114 found = 1;
5115
5116 peer = p2p_get_go_neg_peer(global->p2p);
5117 if (peer) {
5118 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5119 MACSTR, MAC2STR(peer));
5120 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005121 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122 }
5123
5124 wpas_p2p_stop_find(wpa_s);
5125
5126 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5127 if (wpa_s == global->p2p_group_formation &&
5128 (wpa_s->p2p_in_provisioning ||
5129 wpa_s->parent->pending_interface_type ==
5130 WPA_IF_P2P_CLIENT)) {
5131 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5132 "formation found - cancelling",
5133 wpa_s->ifname);
5134 found = 1;
5135 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5136 wpa_s->parent, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005137 wpas_p2p_group_delete(wpa_s,
5138 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139 break;
5140 }
5141 }
5142
5143 if (!found) {
5144 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5145 return -1;
5146 }
5147
5148 return 0;
5149}
5150
5151
5152void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5153{
5154 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5155 return;
5156
5157 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5158 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005159 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005160}
5161
5162
5163void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5164 int freq_24, int freq_5, int freq_overall)
5165{
5166 struct p2p_data *p2p = wpa_s->global->p2p;
5167 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5168 return;
5169 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5170}
5171
5172
5173int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5174{
5175 u8 peer[ETH_ALEN];
5176 struct p2p_data *p2p = wpa_s->global->p2p;
5177
5178 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5179 return -1;
5180
5181 if (hwaddr_aton(addr, peer))
5182 return -1;
5183
5184 return p2p_unauthorize(p2p, peer);
5185}
5186
5187
5188/**
5189 * wpas_p2p_disconnect - Disconnect from a P2P Group
5190 * @wpa_s: Pointer to wpa_supplicant data
5191 * Returns: 0 on success, -1 on failure
5192 *
5193 * This can be used to disconnect from a group in which the local end is a P2P
5194 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5195 * virtual network interface was created for this group, that interface will be
5196 * removed. Otherwise, only the configured P2P group network will be removed
5197 * from the interface.
5198 */
5199int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5200{
5201
5202 if (wpa_s == NULL)
5203 return -1;
5204
Jouni Malinen2b89da82012-08-31 22:04:41 +03005205 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5206 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005207}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005208
5209
5210int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5211{
5212 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5213 return 0;
5214
5215 return p2p_in_progress(wpa_s->global->p2p);
5216}
5217
5218
5219void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5220 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005221{
5222 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5223 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5224 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005225 /**
5226 * Remove the network by scheduling the group formation
5227 * timeout to happen immediately. The teardown code
5228 * needs to be scheduled to run asynch later so that we
5229 * don't delete data from under ourselves unexpectedly.
5230 * Calling wpas_p2p_group_formation_timeout directly
5231 * causes a series of crashes in WPS failure scenarios.
5232 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005233 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5234 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005235 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5236 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005237 }
5238}
5239
5240
5241struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005242 const u8 *addr, const u8 *ssid,
5243 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005244{
5245 struct wpa_ssid *s;
5246 size_t i;
5247
5248 for (s = wpa_s->conf->ssid; s; s = s->next) {
5249 if (s->disabled != 2)
5250 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005251 if (ssid &&
5252 (ssid_len != s->ssid_len ||
5253 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5254 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005255 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5256 return s; /* peer is GO in the persistent group */
5257 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5258 continue;
5259 for (i = 0; i < s->num_p2p_clients; i++) {
5260 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5261 addr, ETH_ALEN) == 0)
5262 return s; /* peer is P2P client in persistent
5263 * group */
5264 }
5265 }
5266
5267 return NULL;
5268}
5269
5270
5271void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5272 const u8 *addr)
5273{
5274 if (addr == NULL)
5275 return;
5276 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5277}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005278
Dmitry Shmidt04949592012-07-19 12:16:46 -07005279
5280static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5281 int group_added)
5282{
5283 struct wpa_supplicant *group = wpa_s;
5284 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5285 if (wpa_s->global->p2p_group_formation)
5286 group = wpa_s->global->p2p_group_formation;
5287 wpa_s = wpa_s->parent;
5288 offchannel_send_action_done(wpa_s);
5289 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005290 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005291 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5292 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5293 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5294 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5295 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005296 wpa_s->p2p_pd_before_go_neg,
5297 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005298}
5299
5300
5301int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5302{
5303 if (!wpa_s->p2p_fallback_to_go_neg ||
5304 wpa_s->p2p_in_provisioning <= 5)
5305 return 0;
5306
5307 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5308 return 0; /* peer operating as a GO */
5309
5310 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5311 "fallback to GO Negotiation");
5312 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5313
5314 return 1;
5315}
5316
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005317
5318unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5319{
5320 const char *rn, *rn2;
5321 struct wpa_supplicant *ifs;
5322
5323 if (wpa_s->wpa_state > WPA_SCANNING) {
5324 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5325 "concurrent operation",
5326 P2P_CONCURRENT_SEARCH_DELAY);
5327 return P2P_CONCURRENT_SEARCH_DELAY;
5328 }
5329
5330 if (!wpa_s->driver->get_radio_name)
5331 return 0;
5332 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5333 if (rn == NULL || rn[0] == '\0')
5334 return 0;
5335
5336 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5337 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5338 continue;
5339
5340 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5341 if (!rn2 || os_strcmp(rn, rn2) != 0)
5342 continue;
5343 if (ifs->wpa_state > WPA_SCANNING) {
5344 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5345 "delay due to concurrent operation on "
5346 "interface %s",
5347 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5348 return P2P_CONCURRENT_SEARCH_DELAY;
5349 }
5350 }
5351
5352 return 0;
5353}
5354
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005355#ifdef ANDROID_P2P
5356int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
5357{
5358 struct wpa_supplicant *iface = NULL;
5359 struct p2p_data *p2p = wpa_s->global->p2p;
5360
5361 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5362 if((iface->p2p_group_interface) && (iface->current_ssid) &&
5363 (iface->current_ssid->frequency != freq)) {
5364
5365 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
5366 /* Try to see whether we can move the GO. If it
5367 * is not possible, remove the GO interface
5368 */
5369 if(wpa_drv_switch_channel(iface, freq) == 0) {
5370 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5371 iface->current_ssid->frequency = freq;
5372 continue;
5373 }
5374 }
5375
5376 /* If GO cannot be moved or if the conflicting interface is a
5377 * P2P Client, remove the interface depending up on the connection
5378 * priority */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005379 if(!wpas_is_p2p_prioritized(wpa_s)) {
5380 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005381 * P2P connection. So remove the interface */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005382 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5383 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005384 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005385 } else {
5386 /* Existing connection has the priority. Disable the newly
5387 * selected network and let the application know about it.
5388 */
5389 return -1;
5390 }
5391 }
5392 }
5393 return 0;
5394}
5395#endif