blob: 1b26ddfdd95bc5f620cbf4440d75baceb0704962 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "common/ieee802_11_common.h"
14#include "common/ieee802_11_defs.h"
15#include "common/wpa_ctrl.h"
16#include "wps/wps_i.h"
17#include "p2p/p2p.h"
18#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080019#include "ap/ap_config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070021#include "eapol_supp/eapol_supp_sm.h"
22#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "wpa_supplicant_i.h"
24#include "driver_i.h"
25#include "ap.h"
26#include "config_ssid.h"
27#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "notify.h"
29#include "scan.h"
30#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "wps_supplicant.h"
33#include "p2p_supplicant.h"
34
35
36/*
37 * How many times to try to scan to find the GO before giving up on join
38 * request.
39 */
40#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
41
Dmitry Shmidt04949592012-07-19 12:16:46 -070042#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080044#ifndef P2P_MAX_CLIENT_IDLE
45/*
46 * How many seconds to try to reconnect to the GO when connection in P2P client
47 * role has been lost.
48 */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070049#ifdef ANDROID_P2P
50#define P2P_MAX_CLIENT_IDLE 20
51#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080052#define P2P_MAX_CLIENT_IDLE 10
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070053#endif /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054#endif /* P2P_MAX_CLIENT_IDLE */
55
Dmitry Shmidt04949592012-07-19 12:16:46 -070056#ifndef P2P_MAX_INITIAL_CONN_WAIT
57/*
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
60 */
61#define P2P_MAX_INITIAL_CONN_WAIT 10
62#endif /* P2P_MAX_INITIAL_CONN_WAIT */
63
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070064#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070065#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066#endif /* P2P_CONCURRENT_SEARCH_DELAY */
67
68enum p2p_group_removal_reason {
69 P2P_GROUP_REMOVAL_UNKNOWN,
70 P2P_GROUP_REMOVAL_SILENT,
71 P2P_GROUP_REMOVAL_FORMATION_FAILED,
72 P2P_GROUP_REMOVAL_REQUESTED,
73 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
74 P2P_GROUP_REMOVAL_UNAVAILABLE,
75 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
76#ifdef ANDROID_P2P
77 P2P_GROUP_REMOVAL_FREQ_CONFLICT
78#endif
79};
80
Jouni Malinendc7b7132012-09-14 12:53:47 -070081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
83static struct wpa_supplicant *
84wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
85 int go);
86static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070087static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
89static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070090 const u8 *dev_addr, enum p2p_wps_method wps_method,
91 int auto_join);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080092static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
93 void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
95static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
96static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
97static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070098static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
99 int group_added);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100
101
102static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
103 struct wpa_scan_results *scan_res)
104{
105 size_t i;
106
107 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
108 return;
109
110 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
111 (int) scan_res->num);
112
113 for (i = 0; i < scan_res->num; i++) {
114 struct wpa_scan_res *bss = scan_res->res[i];
115 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
116 bss->freq, bss->level,
117 (const u8 *) (bss + 1),
118 bss->ie_len) > 0)
119 break;
120 }
121
122 p2p_scan_res_handled(wpa_s->global->p2p);
123}
124
125
126static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
127 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700128 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129{
130 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700131 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700132 struct wpa_driver_scan_params params;
133 int ret;
134 struct wpabuf *wps_ie, *ies;
135 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800136 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137
138 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
139 return -1;
140
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700141 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
142 if (ifs->sta_scan_pending &&
143 wpas_p2p_in_progress(wpa_s) == 2) {
144 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
145 "pending station mode scan to be "
146 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700147 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700148 wpa_supplicant_req_scan(ifs, 0, 0);
149 return 1;
150 }
151 }
152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 os_memset(&params, 0, sizeof(params));
154
155 /* P2P Wildcard SSID */
156 params.num_ssids = 1;
157 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
158 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
159
160 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700161 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
162 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 num_req_dev_types, req_dev_types);
164 if (wps_ie == NULL)
165 return -1;
166
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800167 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
168 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 if (ies == NULL) {
170 wpabuf_free(wps_ie);
171 return -1;
172 }
173 wpabuf_put_buf(ies, wps_ie);
174 wpabuf_free(wps_ie);
175
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800176 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800178 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179 params.extra_ies = wpabuf_head(ies);
180 params.extra_ies_len = wpabuf_len(ies);
181
182 switch (type) {
183 case P2P_SCAN_SOCIAL:
184 params.freqs = social_channels;
185 break;
186 case P2P_SCAN_FULL:
187 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700188 case P2P_SCAN_SPECIFIC:
189 social_channels[0] = freq;
190 social_channels[1] = 0;
191 params.freqs = social_channels;
192 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193 case P2P_SCAN_SOCIAL_PLUS_ONE:
194 social_channels[3] = freq;
195 params.freqs = social_channels;
196 break;
197 }
198
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800199 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200
201 wpabuf_free(ies);
202
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800203 if (ret) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700204 if (wpa_s->scanning ||
205 wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
Jouni Malinendc7b7132012-09-14 12:53:47 -0700206 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800207 ret = 1;
208 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700209 } else
210 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 return ret;
213}
214
215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
217{
218 switch (p2p_group_interface) {
219 case P2P_GROUP_INTERFACE_PENDING:
220 return WPA_IF_P2P_GROUP;
221 case P2P_GROUP_INTERFACE_GO:
222 return WPA_IF_P2P_GO;
223 case P2P_GROUP_INTERFACE_CLIENT:
224 return WPA_IF_P2P_CLIENT;
225 }
226
227 return WPA_IF_P2P_GROUP;
228}
229
230
231static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
232 const u8 *ssid,
233 size_t ssid_len, int *go)
234{
235 struct wpa_ssid *s;
236
237 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
238 for (s = wpa_s->conf->ssid; s; s = s->next) {
239 if (s->disabled != 0 || !s->p2p_group ||
240 s->ssid_len != ssid_len ||
241 os_memcmp(ssid, s->ssid, ssid_len) != 0)
242 continue;
243 if (s->mode == WPAS_MODE_P2P_GO &&
244 s != wpa_s->current_ssid)
245 continue;
246 if (go)
247 *go = s->mode == WPAS_MODE_P2P_GO;
248 return wpa_s;
249 }
250 }
251
252 return NULL;
253}
254
255
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700256static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
257 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258{
259 struct wpa_ssid *ssid;
260 char *gtype;
261 const char *reason;
262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263 ssid = wpa_s->current_ssid;
264 if (ssid == NULL) {
265 /*
266 * The current SSID was not known, but there may still be a
267 * pending P2P group interface waiting for provisioning.
268 */
269 ssid = wpa_s->conf->ssid;
270 while (ssid) {
271 if (ssid->p2p_group &&
272 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
273 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
274 break;
275 ssid = ssid->next;
276 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300277 if (ssid == NULL &&
278 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
279 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700280 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
281 "not found");
282 return -1;
283 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 }
285 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
286 gtype = "GO";
287 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
288 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
289 wpa_s->reassociate = 0;
290 wpa_s->disconnected = 1;
291 wpa_supplicant_deauthenticate(wpa_s,
292 WLAN_REASON_DEAUTH_LEAVING);
293 gtype = "client";
294 } else
295 gtype = "GO";
296 if (wpa_s->cross_connect_in_use) {
297 wpa_s->cross_connect_in_use = 0;
298 wpa_msg(wpa_s->parent, MSG_INFO,
299 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
300 wpa_s->ifname, wpa_s->cross_connect_uplink);
301 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700302 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 case P2P_GROUP_REMOVAL_REQUESTED:
304 reason = " reason=REQUESTED";
305 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700306 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
307 reason = " reason=FORMATION_FAILED";
308 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
310 reason = " reason=IDLE";
311 break;
312 case P2P_GROUP_REMOVAL_UNAVAILABLE:
313 reason = " reason=UNAVAILABLE";
314 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700315 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
316 reason = " reason=GO_ENDING_SESSION";
317 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700318#ifdef ANDROID_P2P
319 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
320 reason = " reason=FREQ_CONFLICT";
321 break;
322#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 default:
324 reason = "";
325 break;
326 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700327 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700328 wpa_msg(wpa_s->parent, MSG_INFO,
329 P2P_EVENT_GROUP_REMOVED "%s %s%s",
330 wpa_s->ifname, gtype, reason);
331 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332
Dmitry Shmidt04949592012-07-19 12:16:46 -0700333 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
334 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
335
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700336 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
338
339 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
340 struct wpa_global *global;
341 char *ifname;
342 enum wpa_driver_if_type type;
343 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
344 wpa_s->ifname);
345 global = wpa_s->global;
346 ifname = os_strdup(wpa_s->ifname);
347 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700348 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 wpa_s = global->ifaces;
350 if (wpa_s && ifname)
351 wpa_drv_if_remove(wpa_s, type, ifname);
352 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300353 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 }
355
356 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
357 if (ssid && (ssid->p2p_group ||
358 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
359 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
360 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700361 if (ssid == wpa_s->current_ssid) {
362 wpa_sm_set_config(wpa_s->wpa, NULL);
363 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700365 }
366 /*
367 * Networks objects created during any P2P activities are not
368 * exposed out as they might/will confuse certain non-P2P aware
369 * applications since these network objects won't behave like
370 * regular ones.
371 *
372 * Likewise, we don't send out network removed signals for such
373 * network objects.
374 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 wpa_config_remove_network(wpa_s->conf, id);
376 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800377 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378 } else {
379 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
380 "found");
381 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700382 if (wpa_s->ap_iface)
383 wpa_supplicant_ap_deinit(wpa_s);
384 else
385 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700386
387 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388}
389
390
391static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
392 u8 *go_dev_addr,
393 const u8 *ssid, size_t ssid_len)
394{
395 struct wpa_bss *bss;
396 const u8 *bssid;
397 struct wpabuf *p2p;
398 u8 group_capab;
399 const u8 *addr;
400
401 if (wpa_s->go_params)
402 bssid = wpa_s->go_params->peer_interface_addr;
403 else
404 bssid = wpa_s->bssid;
405
406 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
407 if (bss == NULL) {
408 u8 iface_addr[ETH_ALEN];
409 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
410 iface_addr) == 0)
411 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
412 }
413 if (bss == NULL) {
414 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
415 "group is persistent - BSS " MACSTR " not found",
416 MAC2STR(bssid));
417 return 0;
418 }
419
420 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
421 if (p2p == NULL) {
422 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
423 "group is persistent - BSS " MACSTR
424 " did not include P2P IE", MAC2STR(bssid));
425 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
426 (u8 *) (bss + 1), bss->ie_len);
427 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
428 ((u8 *) bss + 1) + bss->ie_len,
429 bss->beacon_ie_len);
430 return 0;
431 }
432
433 group_capab = p2p_get_group_capab(p2p);
434 addr = p2p_get_go_dev_addr(p2p);
435 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
436 "group_capab=0x%x", group_capab);
437 if (addr) {
438 os_memcpy(go_dev_addr, addr, ETH_ALEN);
439 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
440 MAC2STR(addr));
441 } else
442 os_memset(go_dev_addr, 0, ETH_ALEN);
443 wpabuf_free(p2p);
444
445 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
446 "go_dev_addr=" MACSTR,
447 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
448
449 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
450}
451
452
Jouni Malinen75ecf522011-06-27 15:19:46 -0700453static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
454 struct wpa_ssid *ssid,
455 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456{
457 struct wpa_ssid *s;
458 int changed = 0;
459
460 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
461 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
462 for (s = wpa_s->conf->ssid; s; s = s->next) {
463 if (s->disabled == 2 &&
464 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
465 s->ssid_len == ssid->ssid_len &&
466 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
467 break;
468 }
469
470 if (s) {
471 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
472 "entry");
473 if (ssid->passphrase && !s->passphrase)
474 changed = 1;
475 else if (ssid->passphrase && s->passphrase &&
476 os_strcmp(ssid->passphrase, s->passphrase) != 0)
477 changed = 1;
478 } else {
479 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
480 "entry");
481 changed = 1;
482 s = wpa_config_add_network(wpa_s->conf);
483 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700484 return -1;
485
486 /*
487 * Instead of network_added we emit persistent_group_added
488 * notification. Also to keep the defense checks in
489 * persistent_group obj registration method, we set the
490 * relevant flags in s to designate it as a persistent group.
491 */
492 s->p2p_group = 1;
493 s->p2p_persistent_group = 1;
494 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 wpa_config_set_network_defaults(s);
496 }
497
498 s->p2p_group = 1;
499 s->p2p_persistent_group = 1;
500 s->disabled = 2;
501 s->bssid_set = 1;
502 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
503 s->mode = ssid->mode;
504 s->auth_alg = WPA_AUTH_ALG_OPEN;
505 s->key_mgmt = WPA_KEY_MGMT_PSK;
506 s->proto = WPA_PROTO_RSN;
507 s->pairwise_cipher = WPA_CIPHER_CCMP;
508 s->export_keys = 1;
509 if (ssid->passphrase) {
510 os_free(s->passphrase);
511 s->passphrase = os_strdup(ssid->passphrase);
512 }
513 if (ssid->psk_set) {
514 s->psk_set = 1;
515 os_memcpy(s->psk, ssid->psk, 32);
516 }
517 if (s->passphrase && !s->psk_set)
518 wpa_config_update_psk(s);
519 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
520 os_free(s->ssid);
521 s->ssid = os_malloc(ssid->ssid_len);
522 }
523 if (s->ssid) {
524 s->ssid_len = ssid->ssid_len;
525 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
526 }
527
528#ifndef CONFIG_NO_CONFIG_WRITE
529 if (changed && wpa_s->conf->update_config &&
530 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
531 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
532 }
533#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700534
535 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536}
537
538
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800539static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
540 const u8 *addr)
541{
542 struct wpa_ssid *ssid, *s;
543 u8 *n;
544 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700545 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800546
547 ssid = wpa_s->current_ssid;
548 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
549 !ssid->p2p_persistent_group)
550 return;
551
552 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
553 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
554 continue;
555
556 if (s->ssid_len == ssid->ssid_len &&
557 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
558 break;
559 }
560
561 if (s == NULL)
562 return;
563
564 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
565 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700566 ETH_ALEN) != 0)
567 continue;
568
569 if (i == s->num_p2p_clients - 1)
570 return; /* already the most recent entry */
571
572 /* move the entry to mark it most recent */
573 os_memmove(s->p2p_client_list + i * ETH_ALEN,
574 s->p2p_client_list + (i + 1) * ETH_ALEN,
575 (s->num_p2p_clients - i - 1) * ETH_ALEN);
576 os_memcpy(s->p2p_client_list +
577 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
578 found = 1;
579 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800580 }
581
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700582 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
583 n = os_realloc_array(s->p2p_client_list,
584 s->num_p2p_clients + 1, ETH_ALEN);
585 if (n == NULL)
586 return;
587 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
588 s->p2p_client_list = n;
589 s->num_p2p_clients++;
590 } else if (!found) {
591 /* Not enough room for an additional entry - drop the oldest
592 * entry */
593 os_memmove(s->p2p_client_list,
594 s->p2p_client_list + ETH_ALEN,
595 (s->num_p2p_clients - 1) * ETH_ALEN);
596 os_memcpy(s->p2p_client_list +
597 (s->num_p2p_clients - 1) * ETH_ALEN,
598 addr, ETH_ALEN);
599 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800600
601#ifndef CONFIG_NO_CONFIG_WRITE
602 if (wpa_s->parent->conf->update_config &&
603 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
604 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
605#endif /* CONFIG_NO_CONFIG_WRITE */
606}
607
608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
610 int success)
611{
612 struct wpa_ssid *ssid;
613 const char *ssid_txt;
614 int client;
615 int persistent;
616 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700617 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618
619 /*
620 * This callback is likely called for the main interface. Update wpa_s
621 * to use the group interface if a new interface was created for the
622 * group.
623 */
624 if (wpa_s->global->p2p_group_formation)
625 wpa_s = wpa_s->global->p2p_group_formation;
626 wpa_s->global->p2p_group_formation = NULL;
627 wpa_s->p2p_in_provisioning = 0;
628
629 if (!success) {
630 wpa_msg(wpa_s->parent, MSG_INFO,
631 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700632 wpas_p2p_group_delete(wpa_s,
633 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 return;
635 }
636
637 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
638
639 ssid = wpa_s->current_ssid;
640 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
641 ssid->mode = WPAS_MODE_P2P_GO;
642 p2p_group_notif_formation_done(wpa_s->p2p_group);
643 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
644 }
645
646 persistent = 0;
647 if (ssid) {
648 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
649 client = ssid->mode == WPAS_MODE_INFRA;
650 if (ssid->mode == WPAS_MODE_P2P_GO) {
651 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700652 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
653 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 } else
655 persistent = wpas_p2p_persistent_group(wpa_s,
656 go_dev_addr,
657 ssid->ssid,
658 ssid->ssid_len);
659 } else {
660 ssid_txt = "";
661 client = wpa_s->p2p_group_interface ==
662 P2P_GROUP_INTERFACE_CLIENT;
663 os_memset(go_dev_addr, 0, ETH_ALEN);
664 }
665
666 wpa_s->show_group_started = 0;
667 if (client) {
668 /*
669 * Indicate event only after successfully completed 4-way
670 * handshake, i.e., when the interface is ready for data
671 * packets.
672 */
673 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700674#ifdef ANDROID_P2P
675 /* For client Second phase of Group formation (4-way handshake) can be still pending
676 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700677 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 -0700678 wpa_s->global->p2p_group_formation = wpa_s;
679#endif
680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
682 char psk[65];
683 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
684 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
685 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
686 "%s",
687 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
688 MAC2STR(go_dev_addr),
689 persistent ? " [PERSISTENT]" : "");
690 wpas_p2p_cross_connect_setup(wpa_s);
691 wpas_p2p_set_group_idle_timeout(wpa_s);
692 } else {
693 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
694 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
695 "go_dev_addr=" MACSTR "%s",
696 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
697 ssid && ssid->passphrase ? ssid->passphrase : "",
698 MAC2STR(go_dev_addr),
699 persistent ? " [PERSISTENT]" : "");
700 wpas_p2p_cross_connect_setup(wpa_s);
701 wpas_p2p_set_group_idle_timeout(wpa_s);
702 }
703
704 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700705 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
706 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800707 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700708 network_id = ssid->id;
709 if (!client)
710 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711}
712
713
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800714static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
715 unsigned int freq,
716 const u8 *dst, const u8 *src,
717 const u8 *bssid,
718 const u8 *data, size_t data_len,
719 enum offchannel_send_action_result
720 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800722 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
725 return;
726 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
727 return;
728
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800729 switch (result) {
730 case OFFCHANNEL_SEND_ACTION_SUCCESS:
731 res = P2P_SEND_ACTION_SUCCESS;
732 break;
733 case OFFCHANNEL_SEND_ACTION_NO_ACK:
734 res = P2P_SEND_ACTION_NO_ACK;
735 break;
736 case OFFCHANNEL_SEND_ACTION_FAILED:
737 res = P2P_SEND_ACTION_FAILED;
738 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739 }
740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800741 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800743 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
744 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800745 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
746 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700748 if (wpa_s->p2p_fallback_to_go_neg) {
749 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
750 "during p2p_connect-auto");
751 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
752 return;
753 }
754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800756 "join-existing-group operation (no ACK for PD "
757 "Req)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758 wpas_p2p_join_start(wpa_s);
759 }
760}
761
762
763static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
764 const u8 *src, const u8 *bssid, const u8 *buf,
765 size_t len, unsigned int wait_time)
766{
767 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800768 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
769 wait_time,
770 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771}
772
773
774static void wpas_send_action_done(void *ctx)
775{
776 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778}
779
780
781static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
782 struct p2p_go_neg_results *params)
783{
784 if (wpa_s->go_params == NULL) {
785 wpa_s->go_params = os_malloc(sizeof(*params));
786 if (wpa_s->go_params == NULL)
787 return -1;
788 }
789 os_memcpy(wpa_s->go_params, params, sizeof(*params));
790 return 0;
791}
792
793
794static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
795 struct p2p_go_neg_results *res)
796{
797 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
798 MAC2STR(res->peer_interface_addr));
799 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
800 res->ssid, res->ssid_len);
801 wpa_supplicant_ap_deinit(wpa_s);
802 wpas_copy_go_neg_results(wpa_s, res);
803 if (res->wps_method == WPS_PBC)
804 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
805 else {
806 u16 dev_pw_id = DEV_PW_DEFAULT;
807 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
808 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
809 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
810 wpa_s->p2p_pin, 1, dev_pw_id);
811 }
812}
813
814
815static void p2p_go_configured(void *ctx, void *data)
816{
817 struct wpa_supplicant *wpa_s = ctx;
818 struct p2p_go_neg_results *params = data;
819 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700820 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821
822 ssid = wpa_s->current_ssid;
823 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
824 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
825 if (wpa_s->global->p2p_group_formation == wpa_s)
826 wpa_s->global->p2p_group_formation = NULL;
827 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
828 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
829 "go_dev_addr=" MACSTR "%s",
830 wpa_s->ifname,
831 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
832 ssid->frequency,
833 params->passphrase ? params->passphrase : "",
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700834 MAC2STR(wpa_s->global->p2p_dev_addr),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835 params->persistent_group ? " [PERSISTENT]" : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700838 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700840 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700841 if (network_id < 0)
842 network_id = ssid->id;
843 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 wpas_p2p_cross_connect_setup(wpa_s);
845 wpas_p2p_set_group_idle_timeout(wpa_s);
846 return;
847 }
848
849 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
850 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
851 params->peer_interface_addr)) {
852 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
853 "filtering");
854 return;
855 }
856 if (params->wps_method == WPS_PBC)
857 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800858 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859 else if (wpa_s->p2p_pin[0])
860 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
861 wpa_s->p2p_pin, NULL, 0);
862 os_free(wpa_s->go_params);
863 wpa_s->go_params = NULL;
864}
865
866
867static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
868 struct p2p_go_neg_results *params,
869 int group_formation)
870{
871 struct wpa_ssid *ssid;
872
873 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
874 return;
875
876 ssid = wpa_config_add_network(wpa_s->conf);
877 if (ssid == NULL)
878 return;
879
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800880 wpa_s->show_group_started = 0;
881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 wpa_config_set_network_defaults(ssid);
883 ssid->temporary = 1;
884 ssid->p2p_group = 1;
885 ssid->p2p_persistent_group = params->persistent_group;
886 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
887 WPAS_MODE_P2P_GO;
888 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700889 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 ssid->ssid = os_zalloc(params->ssid_len + 1);
891 if (ssid->ssid) {
892 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
893 ssid->ssid_len = params->ssid_len;
894 }
895 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
896 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
897 ssid->proto = WPA_PROTO_RSN;
898 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
899 ssid->passphrase = os_strdup(params->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700900 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901
902 wpa_s->ap_configured_cb = p2p_go_configured;
903 wpa_s->ap_configured_cb_ctx = wpa_s;
904 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700905 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 wpa_s->reassociate = 1;
907 wpa_s->disconnected = 0;
908 wpa_supplicant_req_scan(wpa_s, 0, 0);
909}
910
911
912static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
913 const struct wpa_supplicant *src)
914{
915 struct wpa_config *d;
916 const struct wpa_config *s;
917
918 d = dst->conf;
919 s = src->conf;
920
921#define C(n) if (s->n) d->n = os_strdup(s->n)
922 C(device_name);
923 C(manufacturer);
924 C(model_name);
925 C(model_number);
926 C(serial_number);
927 C(config_methods);
928#undef C
929
930 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
931 os_memcpy(d->sec_device_type, s->sec_device_type,
932 sizeof(d->sec_device_type));
933 d->num_sec_device_types = s->num_sec_device_types;
934
935 d->p2p_group_idle = s->p2p_group_idle;
936 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800937 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700938 d->max_num_sta = s->max_num_sta;
939 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940}
941
942
943static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
944 enum wpa_driver_if_type type)
945{
946 char ifname[120], force_ifname[120];
947
948 if (wpa_s->pending_interface_name[0]) {
949 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
950 "- skip creation of a new one");
951 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
952 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
953 "unknown?! ifname='%s'",
954 wpa_s->pending_interface_name);
955 return -1;
956 }
957 return 0;
958 }
959
960 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
961 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800962 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
964 /* Try to avoid going over the IFNAMSIZ length limit */
965 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
966 wpa_s->p2p_group_idx);
967 }
968 force_ifname[0] = '\0';
969
970 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
971 ifname);
972 wpa_s->p2p_group_idx++;
973
974 wpa_s->pending_interface_type = type;
975 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
976 wpa_s->pending_interface_addr, NULL) < 0) {
977 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
978 "interface");
979 return -1;
980 }
981
982 if (force_ifname[0]) {
983 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
984 force_ifname);
985 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
986 sizeof(wpa_s->pending_interface_name));
987 } else
988 os_strlcpy(wpa_s->pending_interface_name, ifname,
989 sizeof(wpa_s->pending_interface_name));
990 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
991 MACSTR, wpa_s->pending_interface_name,
992 MAC2STR(wpa_s->pending_interface_addr));
993
994 return 0;
995}
996
997
998static void wpas_p2p_remove_pending_group_interface(
999 struct wpa_supplicant *wpa_s)
1000{
1001 if (!wpa_s->pending_interface_name[0] ||
1002 is_zero_ether_addr(wpa_s->pending_interface_addr))
1003 return; /* No pending virtual interface */
1004
1005 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1006 wpa_s->pending_interface_name);
1007 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1008 wpa_s->pending_interface_name);
1009 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1010 wpa_s->pending_interface_name[0] = '\0';
1011}
1012
1013
1014static struct wpa_supplicant *
1015wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1016{
1017 struct wpa_interface iface;
1018 struct wpa_supplicant *group_wpa_s;
1019
1020 if (!wpa_s->pending_interface_name[0]) {
1021 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1022 if (!wpas_p2p_create_iface(wpa_s))
1023 return NULL;
1024 /*
1025 * Something has forced us to remove the pending interface; try
1026 * to create a new one and hope for the best that we will get
1027 * the same local address.
1028 */
1029 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1030 WPA_IF_P2P_CLIENT) < 0)
1031 return NULL;
1032 }
1033
1034 os_memset(&iface, 0, sizeof(iface));
1035 iface.ifname = wpa_s->pending_interface_name;
1036 iface.driver = wpa_s->driver->name;
1037 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1038 iface.driver_param = wpa_s->conf->driver_param;
1039 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1040 if (group_wpa_s == NULL) {
1041 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1042 "wpa_supplicant interface");
1043 return NULL;
1044 }
1045 wpa_s->pending_interface_name[0] = '\0';
1046 group_wpa_s->parent = wpa_s;
1047 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1048 P2P_GROUP_INTERFACE_CLIENT;
1049 wpa_s->global->p2p_group_formation = group_wpa_s;
1050
1051 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1052
1053 return group_wpa_s;
1054}
1055
1056
1057static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1058 void *timeout_ctx)
1059{
1060 struct wpa_supplicant *wpa_s = eloop_ctx;
1061 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1062 if (wpa_s->global->p2p)
1063 p2p_group_formation_failed(wpa_s->global->p2p);
1064 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1065 wpa_drv_p2p_group_formation_failed(wpa_s);
1066 wpas_group_formation_completed(wpa_s, 0);
1067}
1068
1069
1070void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1071{
1072 struct wpa_supplicant *wpa_s = ctx;
1073
1074 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1075 wpa_drv_cancel_remain_on_channel(wpa_s);
1076 wpa_s->off_channel_freq = 0;
1077 wpa_s->roc_waiting_drv_freq = 0;
1078 }
1079
1080 if (res->status) {
1081 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1082 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001083 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084 wpas_p2p_remove_pending_group_interface(wpa_s);
1085 return;
1086 }
1087
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001088 if (wpa_s->p2p_go_ht40)
1089 res->ht40 = 1;
1090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001092 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093
Dmitry Shmidt04949592012-07-19 12:16:46 -07001094 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1095 struct wpa_ssid *ssid;
1096 ssid = wpa_config_get_network(wpa_s->conf,
1097 wpa_s->p2p_persistent_id);
1098 if (ssid && ssid->disabled == 2 &&
1099 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1100 size_t len = os_strlen(ssid->passphrase);
1101 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1102 "on requested persistent group");
1103 os_memcpy(res->passphrase, ssid->passphrase, len);
1104 res->passphrase[len] = '\0';
1105 }
1106 }
1107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 if (wpa_s->create_p2p_iface) {
1109 struct wpa_supplicant *group_wpa_s =
1110 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1111 if (group_wpa_s == NULL) {
1112 wpas_p2p_remove_pending_group_interface(wpa_s);
1113 return;
1114 }
1115 if (group_wpa_s != wpa_s) {
1116 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1117 sizeof(group_wpa_s->p2p_pin));
1118 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1119 }
1120 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1121 wpa_s->pending_interface_name[0] = '\0';
1122 group_wpa_s->p2p_in_provisioning = 1;
1123
1124 if (res->role_go)
1125 wpas_start_wps_go(group_wpa_s, res, 1);
1126 else
1127 wpas_start_wps_enrollee(group_wpa_s, res);
1128 } else {
1129 wpa_s->p2p_in_provisioning = 1;
1130 wpa_s->global->p2p_group_formation = wpa_s;
1131
1132 if (res->role_go)
1133 wpas_start_wps_go(wpa_s, res, 1);
1134 else
1135 wpas_start_wps_enrollee(ctx, res);
1136 }
1137
1138 wpa_s->p2p_long_listen = 0;
1139 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1140
1141 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1142 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1143 (res->peer_config_timeout % 100) * 10000,
1144 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1145}
1146
1147
1148void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1149{
1150 struct wpa_supplicant *wpa_s = ctx;
1151 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1152 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1153
1154 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1155}
1156
1157
1158void wpas_dev_found(void *ctx, const u8 *addr,
1159 const struct p2p_peer_info *info,
1160 int new_device)
1161{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001162#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 struct wpa_supplicant *wpa_s = ctx;
1164 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001165#define WFD_DEV_INFO_SIZE 9
1166 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001167 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001168#ifdef CONFIG_WIFI_DISPLAY
1169 if (info->wfd_subelems) {
1170 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1171 wpabuf_head(info->wfd_subelems),
1172 WFD_DEV_INFO_SIZE);
1173 }
1174#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1176 " p2p_dev_addr=" MACSTR
1177 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001178 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1180 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1181 sizeof(devtype)),
1182 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001183 info->dev_capab, info->group_capab,
1184 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001185#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186
1187 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1188}
1189
1190
1191static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1192{
1193 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001194
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001195 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1196 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1199}
1200
1201
1202static int wpas_start_listen(void *ctx, unsigned int freq,
1203 unsigned int duration,
1204 const struct wpabuf *probe_resp_ie)
1205{
1206 struct wpa_supplicant *wpa_s = ctx;
1207
1208 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1209
1210 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1211 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1212 "report received Probe Request frames");
1213 return -1;
1214 }
1215
1216 wpa_s->pending_listen_freq = freq;
1217 wpa_s->pending_listen_duration = duration;
1218
1219 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1220 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1221 "to remain on channel (%u MHz) for Listen "
1222 "state", freq);
1223 wpa_s->pending_listen_freq = 0;
1224 return -1;
1225 }
1226 wpa_s->off_channel_freq = 0;
1227 wpa_s->roc_waiting_drv_freq = freq;
1228
1229 return 0;
1230}
1231
1232
1233static void wpas_stop_listen(void *ctx)
1234{
1235 struct wpa_supplicant *wpa_s = ctx;
1236 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1237 wpa_drv_cancel_remain_on_channel(wpa_s);
1238 wpa_s->off_channel_freq = 0;
1239 wpa_s->roc_waiting_drv_freq = 0;
1240 }
1241 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1242 wpa_drv_probe_req_report(wpa_s, 0);
1243}
1244
1245
1246static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1247{
1248 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001249 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250}
1251
1252
1253static struct p2p_srv_bonjour *
1254wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1255 const struct wpabuf *query)
1256{
1257 struct p2p_srv_bonjour *bsrv;
1258 size_t len;
1259
1260 len = wpabuf_len(query);
1261 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1262 struct p2p_srv_bonjour, list) {
1263 if (len == wpabuf_len(bsrv->query) &&
1264 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1265 len) == 0)
1266 return bsrv;
1267 }
1268 return NULL;
1269}
1270
1271
1272static struct p2p_srv_upnp *
1273wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1274 const char *service)
1275{
1276 struct p2p_srv_upnp *usrv;
1277
1278 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1279 struct p2p_srv_upnp, list) {
1280 if (version == usrv->version &&
1281 os_strcmp(service, usrv->service) == 0)
1282 return usrv;
1283 }
1284 return NULL;
1285}
1286
1287
1288static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1289 u8 srv_trans_id)
1290{
1291 u8 *len_pos;
1292
1293 if (wpabuf_tailroom(resp) < 5)
1294 return;
1295
1296 /* Length (to be filled) */
1297 len_pos = wpabuf_put(resp, 2);
1298 wpabuf_put_u8(resp, srv_proto);
1299 wpabuf_put_u8(resp, srv_trans_id);
1300 /* Status Code */
1301 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1302 /* Response Data: empty */
1303 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1304}
1305
1306
1307static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1308 struct wpabuf *resp, u8 srv_trans_id)
1309{
1310 struct p2p_srv_bonjour *bsrv;
1311 u8 *len_pos;
1312
1313 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1314
1315 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1316 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1317 return;
1318 }
1319
1320 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1321 struct p2p_srv_bonjour, list) {
1322 if (wpabuf_tailroom(resp) <
1323 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1324 return;
1325 /* Length (to be filled) */
1326 len_pos = wpabuf_put(resp, 2);
1327 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1328 wpabuf_put_u8(resp, srv_trans_id);
1329 /* Status Code */
1330 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1331 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1332 wpabuf_head(bsrv->resp),
1333 wpabuf_len(bsrv->resp));
1334 /* Response Data */
1335 wpabuf_put_buf(resp, bsrv->query); /* Key */
1336 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1337 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1338 2);
1339 }
1340}
1341
1342
1343static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1344 struct wpabuf *resp, u8 srv_trans_id,
1345 const u8 *query, size_t query_len)
1346{
1347 struct p2p_srv_bonjour *bsrv;
1348 struct wpabuf buf;
1349 u8 *len_pos;
1350
1351 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1352 query, query_len);
1353 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1354 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1355 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1356 srv_trans_id);
1357 return;
1358 }
1359
1360 if (query_len == 0) {
1361 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1362 return;
1363 }
1364
1365 if (wpabuf_tailroom(resp) < 5)
1366 return;
1367 /* Length (to be filled) */
1368 len_pos = wpabuf_put(resp, 2);
1369 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1370 wpabuf_put_u8(resp, srv_trans_id);
1371
1372 wpabuf_set(&buf, query, query_len);
1373 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1374 if (bsrv == NULL) {
1375 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1376 "available");
1377
1378 /* Status Code */
1379 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1380 /* Response Data: empty */
1381 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1382 2);
1383 return;
1384 }
1385
1386 /* Status Code */
1387 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1388 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1389 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1390
1391 if (wpabuf_tailroom(resp) >=
1392 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1393 /* Response Data */
1394 wpabuf_put_buf(resp, bsrv->query); /* Key */
1395 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1396 }
1397 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1398}
1399
1400
1401static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1402 struct wpabuf *resp, u8 srv_trans_id)
1403{
1404 struct p2p_srv_upnp *usrv;
1405 u8 *len_pos;
1406
1407 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1408
1409 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1410 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1411 return;
1412 }
1413
1414 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1415 struct p2p_srv_upnp, list) {
1416 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1417 return;
1418
1419 /* Length (to be filled) */
1420 len_pos = wpabuf_put(resp, 2);
1421 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1422 wpabuf_put_u8(resp, srv_trans_id);
1423
1424 /* Status Code */
1425 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1426 /* Response Data */
1427 wpabuf_put_u8(resp, usrv->version);
1428 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1429 usrv->service);
1430 wpabuf_put_str(resp, usrv->service);
1431 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1432 2);
1433 }
1434}
1435
1436
1437static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1438 struct wpabuf *resp, u8 srv_trans_id,
1439 const u8 *query, size_t query_len)
1440{
1441 struct p2p_srv_upnp *usrv;
1442 u8 *len_pos;
1443 u8 version;
1444 char *str;
1445 int count = 0;
1446
1447 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1448 query, query_len);
1449
1450 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1451 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1452 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1453 srv_trans_id);
1454 return;
1455 }
1456
1457 if (query_len == 0) {
1458 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1459 return;
1460 }
1461
1462 if (wpabuf_tailroom(resp) < 5)
1463 return;
1464
1465 /* Length (to be filled) */
1466 len_pos = wpabuf_put(resp, 2);
1467 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1468 wpabuf_put_u8(resp, srv_trans_id);
1469
1470 version = query[0];
1471 str = os_malloc(query_len);
1472 if (str == NULL)
1473 return;
1474 os_memcpy(str, query + 1, query_len - 1);
1475 str[query_len - 1] = '\0';
1476
1477 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1478 struct p2p_srv_upnp, list) {
1479 if (version != usrv->version)
1480 continue;
1481
1482 if (os_strcmp(str, "ssdp:all") != 0 &&
1483 os_strstr(usrv->service, str) == NULL)
1484 continue;
1485
1486 if (wpabuf_tailroom(resp) < 2)
1487 break;
1488 if (count == 0) {
1489 /* Status Code */
1490 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1491 /* Response Data */
1492 wpabuf_put_u8(resp, version);
1493 } else
1494 wpabuf_put_u8(resp, ',');
1495
1496 count++;
1497
1498 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1499 usrv->service);
1500 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1501 break;
1502 wpabuf_put_str(resp, usrv->service);
1503 }
1504 os_free(str);
1505
1506 if (count == 0) {
1507 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1508 "available");
1509 /* Status Code */
1510 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1511 /* Response Data: empty */
1512 }
1513
1514 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1515}
1516
1517
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001518#ifdef CONFIG_WIFI_DISPLAY
1519static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1520 struct wpabuf *resp, u8 srv_trans_id,
1521 const u8 *query, size_t query_len)
1522{
1523 const u8 *pos;
1524 u8 role;
1525 u8 *len_pos;
1526
1527 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1528
1529 if (!wpa_s->global->wifi_display) {
1530 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1531 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1532 srv_trans_id);
1533 return;
1534 }
1535
1536 if (query_len < 1) {
1537 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1538 "Role");
1539 return;
1540 }
1541
1542 if (wpabuf_tailroom(resp) < 5)
1543 return;
1544
1545 pos = query;
1546 role = *pos++;
1547 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1548
1549 /* TODO: role specific handling */
1550
1551 /* Length (to be filled) */
1552 len_pos = wpabuf_put(resp, 2);
1553 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1554 wpabuf_put_u8(resp, srv_trans_id);
1555 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1556
1557 while (pos < query + query_len) {
1558 if (*pos < MAX_WFD_SUBELEMS &&
1559 wpa_s->global->wfd_subelem[*pos] &&
1560 wpabuf_tailroom(resp) >=
1561 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1562 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1563 "subelement %u", *pos);
1564 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1565 }
1566 pos++;
1567 }
1568
1569 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1570}
1571#endif /* CONFIG_WIFI_DISPLAY */
1572
1573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1575 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1576{
1577 struct wpa_supplicant *wpa_s = ctx;
1578 const u8 *pos = tlvs;
1579 const u8 *end = tlvs + tlvs_len;
1580 const u8 *tlv_end;
1581 u16 slen;
1582 struct wpabuf *resp;
1583 u8 srv_proto, srv_trans_id;
1584 size_t buf_len;
1585 char *buf;
1586
1587 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1588 tlvs, tlvs_len);
1589 buf_len = 2 * tlvs_len + 1;
1590 buf = os_malloc(buf_len);
1591 if (buf) {
1592 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1593 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1594 MACSTR " %u %u %s",
1595 freq, MAC2STR(sa), dialog_token, update_indic,
1596 buf);
1597 os_free(buf);
1598 }
1599
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001600 if (wpa_s->p2p_sd_over_ctrl_iface) {
1601 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1602 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001604 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605
1606 resp = wpabuf_alloc(10000);
1607 if (resp == NULL)
1608 return;
1609
1610 while (pos + 1 < end) {
1611 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1612 slen = WPA_GET_LE16(pos);
1613 pos += 2;
1614 if (pos + slen > end || slen < 2) {
1615 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1616 "length");
1617 wpabuf_free(resp);
1618 return;
1619 }
1620 tlv_end = pos + slen;
1621
1622 srv_proto = *pos++;
1623 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1624 srv_proto);
1625 srv_trans_id = *pos++;
1626 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1627 srv_trans_id);
1628
1629 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1630 pos, tlv_end - pos);
1631
1632
1633 if (wpa_s->force_long_sd) {
1634 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1635 "response");
1636 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1637 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1638 goto done;
1639 }
1640
1641 switch (srv_proto) {
1642 case P2P_SERV_ALL_SERVICES:
1643 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1644 "for all services");
1645 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1646 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1647 wpa_printf(MSG_DEBUG, "P2P: No service "
1648 "discovery protocols available");
1649 wpas_sd_add_proto_not_avail(
1650 resp, P2P_SERV_ALL_SERVICES,
1651 srv_trans_id);
1652 break;
1653 }
1654 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1655 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1656 break;
1657 case P2P_SERV_BONJOUR:
1658 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1659 pos, tlv_end - pos);
1660 break;
1661 case P2P_SERV_UPNP:
1662 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1663 pos, tlv_end - pos);
1664 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001665#ifdef CONFIG_WIFI_DISPLAY
1666 case P2P_SERV_WIFI_DISPLAY:
1667 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1668 pos, tlv_end - pos);
1669 break;
1670#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 default:
1672 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1673 "protocol %u", srv_proto);
1674 wpas_sd_add_proto_not_avail(resp, srv_proto,
1675 srv_trans_id);
1676 break;
1677 }
1678
1679 pos = tlv_end;
1680 }
1681
1682done:
1683 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1684 update_indic, tlvs, tlvs_len);
1685
1686 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1687
1688 wpabuf_free(resp);
1689}
1690
1691
1692void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1693 const u8 *tlvs, size_t tlvs_len)
1694{
1695 struct wpa_supplicant *wpa_s = ctx;
1696 const u8 *pos = tlvs;
1697 const u8 *end = tlvs + tlvs_len;
1698 const u8 *tlv_end;
1699 u16 slen;
1700 size_t buf_len;
1701 char *buf;
1702
1703 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1704 tlvs, tlvs_len);
1705 if (tlvs_len > 1500) {
1706 /* TODO: better way for handling this */
1707 wpa_msg_ctrl(wpa_s, MSG_INFO,
1708 P2P_EVENT_SERV_DISC_RESP MACSTR
1709 " %u <long response: %u bytes>",
1710 MAC2STR(sa), update_indic,
1711 (unsigned int) tlvs_len);
1712 } else {
1713 buf_len = 2 * tlvs_len + 1;
1714 buf = os_malloc(buf_len);
1715 if (buf) {
1716 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1717 wpa_msg_ctrl(wpa_s, MSG_INFO,
1718 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1719 MAC2STR(sa), update_indic, buf);
1720 os_free(buf);
1721 }
1722 }
1723
1724 while (pos < end) {
1725 u8 srv_proto, srv_trans_id, status;
1726
1727 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1728 slen = WPA_GET_LE16(pos);
1729 pos += 2;
1730 if (pos + slen > end || slen < 3) {
1731 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1732 "length");
1733 return;
1734 }
1735 tlv_end = pos + slen;
1736
1737 srv_proto = *pos++;
1738 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1739 srv_proto);
1740 srv_trans_id = *pos++;
1741 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1742 srv_trans_id);
1743 status = *pos++;
1744 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1745 status);
1746
1747 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1748 pos, tlv_end - pos);
1749
1750 pos = tlv_end;
1751 }
1752
1753 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1754}
1755
1756
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001757u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1758 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001759{
1760 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001761 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001763 return 0;
1764 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765}
1766
1767
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001768u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1769 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770{
1771 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001772 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773
1774 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1775 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001776 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1778 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1779 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1780 wpabuf_put_u8(tlvs, version);
1781 wpabuf_put_str(tlvs, query);
1782 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1783 wpabuf_free(tlvs);
1784 return ret;
1785}
1786
1787
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001788#ifdef CONFIG_WIFI_DISPLAY
1789
1790static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1791 const struct wpabuf *tlvs)
1792{
1793 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1794 return 0;
1795 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1796 return 0;
1797 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1798}
1799
1800
1801#define MAX_WFD_SD_SUBELEMS 20
1802
1803static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1804 const char *subelems)
1805{
1806 u8 *len;
1807 const char *pos;
1808 int val;
1809 int count = 0;
1810
1811 len = wpabuf_put(tlvs, 2);
1812 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1813 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1814
1815 wpabuf_put_u8(tlvs, role);
1816
1817 pos = subelems;
1818 while (*pos) {
1819 val = atoi(pos);
1820 if (val >= 0 && val < 256) {
1821 wpabuf_put_u8(tlvs, val);
1822 count++;
1823 if (count == MAX_WFD_SD_SUBELEMS)
1824 break;
1825 }
1826 pos = os_strchr(pos + 1, ',');
1827 if (pos == NULL)
1828 break;
1829 pos++;
1830 }
1831
1832 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1833}
1834
1835
1836u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1837 const u8 *dst, const char *role)
1838{
1839 struct wpabuf *tlvs;
1840 u64 ret;
1841 const char *subelems;
1842 u8 id = 1;
1843
1844 subelems = os_strchr(role, ' ');
1845 if (subelems == NULL)
1846 return 0;
1847 subelems++;
1848
1849 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1850 if (tlvs == NULL)
1851 return 0;
1852
1853 if (os_strstr(role, "[source]"))
1854 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1855 if (os_strstr(role, "[pri-sink]"))
1856 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1857 if (os_strstr(role, "[sec-sink]"))
1858 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1859 if (os_strstr(role, "[source+sink]"))
1860 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1861
1862 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1863 wpabuf_free(tlvs);
1864 return ret;
1865}
1866
1867#endif /* CONFIG_WIFI_DISPLAY */
1868
1869
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001870int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871{
1872 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001873 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1875 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001876 return p2p_sd_cancel_request(wpa_s->global->p2p,
1877 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878}
1879
1880
1881void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1882 const u8 *dst, u8 dialog_token,
1883 const struct wpabuf *resp_tlvs)
1884{
1885 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1886 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1887 resp_tlvs);
1888 return;
1889 }
1890 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1891 return;
1892 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1893 resp_tlvs);
1894}
1895
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001896#ifdef ANDROID_P2P
1897void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1898#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001900#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001901{
1902 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1903 wpa_drv_p2p_service_update(wpa_s);
1904 return;
1905 }
1906 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001907#ifdef ANDROID_P2P
1908 p2p_sd_service_update(wpa_s->global->p2p, action);
1909#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001910 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001911#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912}
1913
1914
1915static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1916{
1917 dl_list_del(&bsrv->list);
1918 wpabuf_free(bsrv->query);
1919 wpabuf_free(bsrv->resp);
1920 os_free(bsrv);
1921}
1922
1923
1924static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1925{
1926 dl_list_del(&usrv->list);
1927 os_free(usrv->service);
1928 os_free(usrv);
1929}
1930
1931
1932void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1933{
1934 struct p2p_srv_bonjour *bsrv, *bn;
1935 struct p2p_srv_upnp *usrv, *un;
1936
1937 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1938 struct p2p_srv_bonjour, list)
1939 wpas_p2p_srv_bonjour_free(bsrv);
1940
1941 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1942 struct p2p_srv_upnp, list)
1943 wpas_p2p_srv_upnp_free(usrv);
1944
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001945#ifdef ANDROID_P2P
1946 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1947#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001948 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001949#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001950}
1951
1952
1953int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1954 struct wpabuf *query, struct wpabuf *resp)
1955{
1956 struct p2p_srv_bonjour *bsrv;
1957
1958 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1959 if (bsrv) {
1960 wpabuf_free(query);
1961 wpabuf_free(bsrv->resp);
1962 bsrv->resp = resp;
1963 return 0;
1964 }
1965
1966 bsrv = os_zalloc(sizeof(*bsrv));
1967 if (bsrv == NULL)
1968 return -1;
1969 bsrv->query = query;
1970 bsrv->resp = resp;
1971 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1972
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001973#ifdef ANDROID_P2P
1974 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
1975#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001977#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978 return 0;
1979}
1980
1981
1982int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1983 const struct wpabuf *query)
1984{
1985 struct p2p_srv_bonjour *bsrv;
1986
1987 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1988 if (bsrv == NULL)
1989 return -1;
1990 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001991#ifdef ANDROID_P2P
1992 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
1993#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001995#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996 return 0;
1997}
1998
1999
2000int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2001 const char *service)
2002{
2003 struct p2p_srv_upnp *usrv;
2004
2005 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2006 return 0; /* Already listed */
2007 usrv = os_zalloc(sizeof(*usrv));
2008 if (usrv == NULL)
2009 return -1;
2010 usrv->version = version;
2011 usrv->service = os_strdup(service);
2012 if (usrv->service == NULL) {
2013 os_free(usrv);
2014 return -1;
2015 }
2016 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2017
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002018#ifdef ANDROID_P2P
2019 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2020#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002022#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023 return 0;
2024}
2025
2026
2027int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2028 const char *service)
2029{
2030 struct p2p_srv_upnp *usrv;
2031
2032 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2033 if (usrv == NULL)
2034 return -1;
2035 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002036#ifdef ANDROID_P2P
2037 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2038#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002040#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041 return 0;
2042}
2043
2044
2045static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2046 const u8 *peer, const char *params,
2047 unsigned int generated_pin)
2048{
2049 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2050 MAC2STR(peer), generated_pin, params);
2051}
2052
2053
2054static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2055 const u8 *peer, const char *params)
2056{
2057 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2058 MAC2STR(peer), params);
2059}
2060
2061
2062void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2063 const u8 *dev_addr, const u8 *pri_dev_type,
2064 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002065 u8 dev_capab, u8 group_capab, const u8 *group_id,
2066 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067{
2068 struct wpa_supplicant *wpa_s = ctx;
2069 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002070 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 u8 empty_dev_type[8];
2072 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073 struct wpa_supplicant *group = NULL;
2074
2075 if (group_id) {
2076 for (group = wpa_s->global->ifaces; group; group = group->next)
2077 {
2078 struct wpa_ssid *s = group->current_ssid;
2079 if (s != NULL &&
2080 s->mode == WPAS_MODE_P2P_GO &&
2081 group_id_len - ETH_ALEN == s->ssid_len &&
2082 os_memcmp(group_id + ETH_ALEN, s->ssid,
2083 s->ssid_len) == 0)
2084 break;
2085 }
2086 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002087
2088 if (pri_dev_type == NULL) {
2089 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2090 pri_dev_type = empty_dev_type;
2091 }
2092 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2093 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002094 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 MAC2STR(dev_addr),
2096 wps_dev_type_bin2str(pri_dev_type, devtype,
2097 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002098 dev_name, supp_config_methods, dev_capab, group_capab,
2099 group ? " group=" : "",
2100 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 if (config_methods & WPS_CONFIG_DISPLAY) {
2104 generated_pin = wps_generate_pin();
2105 wpas_prov_disc_local_display(wpa_s, peer, params,
2106 generated_pin);
2107 } else if (config_methods & WPS_CONFIG_KEYPAD)
2108 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2109 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2110 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2111 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002112
2113 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2114 P2P_PROV_DISC_SUCCESS,
2115 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116}
2117
2118
2119void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2120{
2121 struct wpa_supplicant *wpa_s = ctx;
2122 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002123 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002125 if (wpa_s->pending_pd_before_join &&
2126 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2127 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2128 wpa_s->pending_pd_before_join = 0;
2129 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2130 "join-existing-group operation");
2131 wpas_p2p_join_start(wpa_s);
2132 return;
2133 }
2134
Dmitry Shmidt04949592012-07-19 12:16:46 -07002135 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2136 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2137 os_snprintf(params, sizeof(params), " peer_go=%d",
2138 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2139 else
2140 params[0] = '\0';
2141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002143 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002144 else if (config_methods & WPS_CONFIG_KEYPAD) {
2145 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002146 wpas_prov_disc_local_display(wpa_s, peer, params,
2147 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002149 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2150 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151
Jouni Malinen75ecf522011-06-27 15:19:46 -07002152 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2153 P2P_PROV_DISC_SUCCESS,
2154 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155}
2156
2157
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002158static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2159 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002160{
2161 struct wpa_supplicant *wpa_s = ctx;
2162
Dmitry Shmidt04949592012-07-19 12:16:46 -07002163 if (wpa_s->p2p_fallback_to_go_neg) {
2164 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2165 "failed - fall back to GO Negotiation");
2166 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2167 return;
2168 }
2169
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002170#ifdef ANDROID_P2P
2171 /* If provision discovery failed it is safe to cancel the timer here and
2172 * also do not start the join */
2173 if (wpa_s->pending_pd_before_join &&
2174 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2175 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2176 wpa_s->pending_pd_before_join = 0;
2177 wpa_printf(MSG_DEBUG, "P2P: Do not Start pending "
2178 "join-existing-group operation");
2179 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2180 }
2181#endif /* ANDROID_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002182 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2183 " p2p_dev_addr=" MACSTR " status=%d",
2184 MAC2STR(peer), status);
2185
Jouni Malinen75ecf522011-06-27 15:19:46 -07002186 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2187 status, 0, 0);
2188}
2189
2190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2192 const u8 *go_dev_addr, const u8 *ssid,
2193 size_t ssid_len, int *go, u8 *group_bssid,
2194 int *force_freq, int persistent_group)
2195{
2196 struct wpa_supplicant *wpa_s = ctx;
2197 struct wpa_ssid *s;
2198 u8 cur_bssid[ETH_ALEN];
2199 int res;
2200 struct wpa_supplicant *grp;
2201
2202 if (!persistent_group) {
2203 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2204 " to join an active group", MAC2STR(sa));
2205 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2206 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2207 == 0 ||
2208 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2209 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2210 "authorized invitation");
2211 goto accept_inv;
2212 }
2213 /*
2214 * Do not accept the invitation automatically; notify user and
2215 * request approval.
2216 */
2217 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2218 }
2219
2220 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2221 if (grp) {
2222 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2223 "running persistent group");
2224 if (*go)
2225 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2226 goto accept_inv;
2227 }
2228
2229 if (!wpa_s->conf->persistent_reconnect)
2230 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2231
2232 for (s = wpa_s->conf->ssid; s; s = s->next) {
2233 if (s->disabled == 2 &&
2234 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2235 s->ssid_len == ssid_len &&
2236 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2237 break;
2238 }
2239
2240 if (!s) {
2241 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2242 " requested reinvocation of an unknown group",
2243 MAC2STR(sa));
2244 return P2P_SC_FAIL_UNKNOWN_GROUP;
2245 }
2246
2247 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2248 *go = 1;
2249 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2250 wpa_printf(MSG_DEBUG, "P2P: The only available "
2251 "interface is already in use - reject "
2252 "invitation");
2253 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2254 }
2255 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2256 } else if (s->mode == WPAS_MODE_P2P_GO) {
2257 *go = 1;
2258 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2259 {
2260 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2261 "interface address for the group");
2262 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2263 }
2264 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2265 ETH_ALEN);
2266 }
2267
2268accept_inv:
2269 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2270 wpa_s->assoc_freq) {
2271 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2272 "the channel we are already using");
2273 *force_freq = wpa_s->assoc_freq;
2274 }
2275
2276 res = wpa_drv_shared_freq(wpa_s);
2277 if (res > 0) {
2278 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2279 "with the channel we are already using on a "
2280 "shared interface");
2281 *force_freq = res;
2282 }
2283
2284 return P2P_SC_SUCCESS;
2285}
2286
2287
2288static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2289 const u8 *ssid, size_t ssid_len,
2290 const u8 *go_dev_addr, u8 status,
2291 int op_freq)
2292{
2293 struct wpa_supplicant *wpa_s = ctx;
2294 struct wpa_ssid *s;
2295
2296 for (s = wpa_s->conf->ssid; s; s = s->next) {
2297 if (s->disabled == 2 &&
2298 s->ssid_len == ssid_len &&
2299 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2300 break;
2301 }
2302
2303 if (status == P2P_SC_SUCCESS) {
2304 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2305 " was accepted; op_freq=%d MHz",
2306 MAC2STR(sa), op_freq);
2307 if (s) {
2308 wpas_p2p_group_add_persistent(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002309 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002310 } else if (bssid) {
2311 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002312 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 }
2314 return;
2315 }
2316
2317 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2318 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2319 " was rejected (status %u)", MAC2STR(sa), status);
2320 return;
2321 }
2322
2323 if (!s) {
2324 if (bssid) {
2325 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2326 "sa=" MACSTR " go_dev_addr=" MACSTR
2327 " bssid=" MACSTR " unknown-network",
2328 MAC2STR(sa), MAC2STR(go_dev_addr),
2329 MAC2STR(bssid));
2330 } else {
2331 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2332 "sa=" MACSTR " go_dev_addr=" MACSTR
2333 " unknown-network",
2334 MAC2STR(sa), MAC2STR(go_dev_addr));
2335 }
2336 return;
2337 }
2338
2339 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2340 " persistent=%d", MAC2STR(sa), s->id);
2341}
2342
2343
2344static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2345{
2346 struct wpa_supplicant *wpa_s = ctx;
2347 struct wpa_ssid *ssid;
2348
2349 if (bssid) {
2350 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2351 "status=%d " MACSTR,
2352 status, MAC2STR(bssid));
2353 } else {
2354 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2355 "status=%d ", status);
2356 }
2357 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2358
2359 if (wpa_s->pending_invite_ssid_id == -1)
2360 return; /* Invitation to active group */
2361
2362 if (status != P2P_SC_SUCCESS) {
2363 wpas_p2p_remove_pending_group_interface(wpa_s);
2364 return;
2365 }
2366
2367 ssid = wpa_config_get_network(wpa_s->conf,
2368 wpa_s->pending_invite_ssid_id);
2369 if (ssid == NULL) {
2370 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2371 "data matching with invitation");
2372 return;
2373 }
2374
2375 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002376 ssid->mode == WPAS_MODE_P2P_GO,
2377 wpa_s->p2p_persistent_go_freq,
2378 wpa_s->p2p_go_ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379}
2380
2381
Dmitry Shmidt04949592012-07-19 12:16:46 -07002382static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2383 unsigned int freq)
2384{
2385 unsigned int i;
2386
2387 if (global->p2p_disallow_freq == NULL)
2388 return 0;
2389
2390 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2391 if (freq >= global->p2p_disallow_freq[i].min &&
2392 freq <= global->p2p_disallow_freq[i].max)
2393 return 1;
2394 }
2395
2396 return 0;
2397}
2398
2399
2400static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2401{
2402 reg->channel[reg->channels] = chan;
2403 reg->channels++;
2404}
2405
2406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2408 struct p2p_channels *chan)
2409{
2410 int i, cla = 0;
2411
2412 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2413 "band");
2414
2415 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2416 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002417 chan->reg_class[cla].channels = 0;
2418 for (i = 0; i < 11; i++) {
2419 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2420 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2421 }
2422 if (chan->reg_class[cla].channels)
2423 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002424
2425 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2426 "band");
2427
2428 /* Operating class 115 - 5 GHz, channels 36-48 */
2429 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002430 chan->reg_class[cla].channels = 0;
2431 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2432 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2433 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2434 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2435 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2436 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2437 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2438 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2439 if (chan->reg_class[cla].channels)
2440 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002441
2442 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2443 "band");
2444
2445 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2446 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002447 chan->reg_class[cla].channels = 0;
2448 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2449 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2450 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2451 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2452 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2453 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2454 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2455 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2456 if (chan->reg_class[cla].channels)
2457 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458
2459 chan->reg_classes = cla;
2460 return 0;
2461}
2462
2463
2464static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2465 u16 num_modes,
2466 enum hostapd_hw_mode mode)
2467{
2468 u16 i;
2469
2470 for (i = 0; i < num_modes; i++) {
2471 if (modes[i].mode == mode)
2472 return &modes[i];
2473 }
2474
2475 return NULL;
2476}
2477
2478
Dmitry Shmidt04949592012-07-19 12:16:46 -07002479static int has_channel(struct wpa_global *global,
2480 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481{
2482 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002483 unsigned int freq;
2484
2485 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2486 chan * 5;
2487 if (wpas_p2p_disallowed_freq(global, freq))
2488 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002489
2490 for (i = 0; i < mode->num_channels; i++) {
2491 if (mode->channels[i].chan == chan) {
2492 if (flags)
2493 *flags = mode->channels[i].flag;
2494 return !(mode->channels[i].flag &
2495 (HOSTAPD_CHAN_DISABLED |
2496 HOSTAPD_CHAN_PASSIVE_SCAN |
2497 HOSTAPD_CHAN_NO_IBSS |
2498 HOSTAPD_CHAN_RADAR));
2499 }
2500 }
2501
2502 return 0;
2503}
2504
2505
2506struct p2p_oper_class_map {
2507 enum hostapd_hw_mode mode;
2508 u8 op_class;
2509 u8 min_chan;
2510 u8 max_chan;
2511 u8 inc;
2512 enum { BW20, BW40PLUS, BW40MINUS } bw;
2513};
2514
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002515static struct p2p_oper_class_map op_class[] = {
2516 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2517 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2518#if 0 /* Do not enable HT40 on 2 GHz for now */
2519 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2520 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2521#endif
2522 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2523 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2524 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2525 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2526 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2527 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2528 { -1, 0, 0, 0, 0, BW20 }
2529};
2530
2531
2532static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2533 struct hostapd_hw_modes *mode,
2534 u8 channel, u8 bw)
2535{
2536 int flag;
2537
2538 if (!has_channel(wpa_s->global, mode, channel, &flag))
2539 return -1;
2540 if (bw == BW40MINUS &&
2541 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2542 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2543 return 0;
2544 if (bw == BW40PLUS &&
2545 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2546 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2547 return 0;
2548 return 1;
2549}
2550
2551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2553 struct p2p_channels *chan)
2554{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002555 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002558 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2560 "of all supported channels; assume dualband "
2561 "support");
2562 return wpas_p2p_default_channels(wpa_s, chan);
2563 }
2564
2565 cla = 0;
2566
2567 for (op = 0; op_class[op].op_class; op++) {
2568 struct p2p_oper_class_map *o = &op_class[op];
2569 u8 ch;
2570 struct p2p_reg_class *reg = NULL;
2571
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002572 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 if (mode == NULL)
2574 continue;
2575 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002576 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 continue;
2578 if (reg == NULL) {
2579 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2580 "class %u", o->op_class);
2581 reg = &chan->reg_class[cla];
2582 cla++;
2583 reg->reg_class = o->op_class;
2584 }
2585 reg->channel[reg->channels] = ch;
2586 reg->channels++;
2587 }
2588 if (reg) {
2589 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2590 reg->channel, reg->channels);
2591 }
2592 }
2593
2594 chan->reg_classes = cla;
2595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596 return 0;
2597}
2598
2599
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002600int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2601 struct hostapd_hw_modes *mode, u8 channel)
2602{
2603 int op, ret;
2604
2605 for (op = 0; op_class[op].op_class; op++) {
2606 struct p2p_oper_class_map *o = &op_class[op];
2607 u8 ch;
2608
2609 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2610 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2611 o->bw == BW20 || ch != channel)
2612 continue;
2613 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2614 if (ret < 0)
2615 continue;
2616 else if (ret > 0)
2617 return (o->bw == BW40MINUS) ? -1 : 1;
2618 else
2619 return 0;
2620 }
2621 }
2622 return 0;
2623}
2624
2625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2627 size_t buf_len)
2628{
2629 struct wpa_supplicant *wpa_s = ctx;
2630
2631 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2632 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2633 break;
2634 }
2635 if (wpa_s == NULL)
2636 return -1;
2637
2638 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2639}
2640
2641
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002642static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2643{
2644 struct wpa_supplicant *wpa_s = ctx;
2645
2646 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2647 struct wpa_ssid *ssid = wpa_s->current_ssid;
2648 if (ssid == NULL)
2649 continue;
2650 if (ssid->mode != WPAS_MODE_INFRA)
2651 continue;
2652 if (wpa_s->wpa_state != WPA_COMPLETED &&
2653 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2654 continue;
2655 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2656 return 1;
2657 }
2658
2659 return 0;
2660}
2661
2662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663/**
2664 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2665 * @global: Pointer to global data from wpa_supplicant_init()
2666 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2667 * Returns: 0 on success, -1 on failure
2668 */
2669int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2670{
2671 struct p2p_config p2p;
2672 unsigned int r;
2673 int i;
2674
2675 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2676 return 0;
2677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678 if (global->p2p)
2679 return 0;
2680
2681 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2682 struct p2p_params params;
2683
2684 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2685 os_memset(&params, 0, sizeof(params));
2686 params.dev_name = wpa_s->conf->device_name;
2687 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2688 WPS_DEV_TYPE_LEN);
2689 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2690 os_memcpy(params.sec_dev_type,
2691 wpa_s->conf->sec_device_type,
2692 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2693
2694 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2695 return -1;
2696
2697 return 0;
2698 }
2699
2700 os_memset(&p2p, 0, sizeof(p2p));
2701 p2p.msg_ctx = wpa_s;
2702 p2p.cb_ctx = wpa_s;
2703 p2p.p2p_scan = wpas_p2p_scan;
2704 p2p.send_action = wpas_send_action;
2705 p2p.send_action_done = wpas_send_action_done;
2706 p2p.go_neg_completed = wpas_go_neg_completed;
2707 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2708 p2p.dev_found = wpas_dev_found;
2709 p2p.dev_lost = wpas_dev_lost;
2710 p2p.start_listen = wpas_start_listen;
2711 p2p.stop_listen = wpas_stop_listen;
2712 p2p.send_probe_resp = wpas_send_probe_resp;
2713 p2p.sd_request = wpas_sd_request;
2714 p2p.sd_response = wpas_sd_response;
2715 p2p.prov_disc_req = wpas_prov_disc_req;
2716 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002717 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002718 p2p.invitation_process = wpas_invitation_process;
2719 p2p.invitation_received = wpas_invitation_received;
2720 p2p.invitation_result = wpas_invitation_result;
2721 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002722 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002723
2724 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002725 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002726 p2p.dev_name = wpa_s->conf->device_name;
2727 p2p.manufacturer = wpa_s->conf->manufacturer;
2728 p2p.model_name = wpa_s->conf->model_name;
2729 p2p.model_number = wpa_s->conf->model_number;
2730 p2p.serial_number = wpa_s->conf->serial_number;
2731 if (wpa_s->wps) {
2732 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2733 p2p.config_methods = wpa_s->wps->config_methods;
2734 }
2735
2736 if (wpa_s->conf->p2p_listen_reg_class &&
2737 wpa_s->conf->p2p_listen_channel) {
2738 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2739 p2p.channel = wpa_s->conf->p2p_listen_channel;
2740 } else {
2741 p2p.reg_class = 81;
2742 /*
2743 * Pick one of the social channels randomly as the listen
2744 * channel.
2745 */
2746 os_get_random((u8 *) &r, sizeof(r));
2747 p2p.channel = 1 + (r % 3) * 5;
2748 }
2749 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2750
2751 if (wpa_s->conf->p2p_oper_reg_class &&
2752 wpa_s->conf->p2p_oper_channel) {
2753 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2754 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2755 p2p.cfg_op_channel = 1;
2756 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2757 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2758
2759 } else {
2760 p2p.op_reg_class = 81;
2761 /*
2762 * Use random operation channel from (1, 6, 11) if no other
2763 * preference is indicated.
2764 */
2765 os_get_random((u8 *) &r, sizeof(r));
2766 p2p.op_channel = 1 + (r % 3) * 5;
2767 p2p.cfg_op_channel = 0;
2768 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2769 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2770 }
2771 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2772 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2773 p2p.country[2] = 0x04;
2774 } else
2775 os_memcpy(p2p.country, "XX\x04", 3);
2776
2777 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2778 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2779 "channel list");
2780 return -1;
2781 }
2782
2783 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2784 WPS_DEV_TYPE_LEN);
2785
2786 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2787 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2788 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2789
2790 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2791 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2792
2793 p2p.max_peers = 100;
2794
2795 if (wpa_s->conf->p2p_ssid_postfix) {
2796 p2p.ssid_postfix_len =
2797 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2798 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2799 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2800 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2801 p2p.ssid_postfix_len);
2802 }
2803
2804 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2805
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07002806#ifdef ANDROID_P2P
2807 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2808 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
2809 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)
2810 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
2811#endif
2812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813 global->p2p = p2p_init(&p2p);
2814 if (global->p2p == NULL)
2815 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002816 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817
2818 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2819 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2820 continue;
2821 p2p_add_wps_vendor_extension(
2822 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2823 }
2824
2825 return 0;
2826}
2827
2828
2829/**
2830 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2831 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2832 *
2833 * This function deinitialize per-interface P2P data.
2834 */
2835void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2836{
2837 if (wpa_s->driver && wpa_s->drv_priv)
2838 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002839
2840 if (wpa_s->go_params) {
2841 /* Clear any stored provisioning info */
2842 p2p_clear_provisioning_info(
2843 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002844 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002845 }
2846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002847 os_free(wpa_s->go_params);
2848 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2850 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002851 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002852 wpa_s->p2p_long_listen = 0;
2853 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2854 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2855 wpas_p2p_remove_pending_group_interface(wpa_s);
2856
2857 /* TODO: remove group interface from the driver if this wpa_s instance
2858 * is on top of a P2P group interface */
2859}
2860
2861
2862/**
2863 * wpas_p2p_deinit_global - Deinitialize global P2P module
2864 * @global: Pointer to global data from wpa_supplicant_init()
2865 *
2866 * This function deinitializes the global (per device) P2P module.
2867 */
2868void wpas_p2p_deinit_global(struct wpa_global *global)
2869{
2870 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002872 wpa_s = global->ifaces;
2873 if (wpa_s)
2874 wpas_p2p_service_flush(wpa_s);
2875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002876 if (global->p2p == NULL)
2877 return;
2878
2879 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2881 wpa_s = wpa_s->next;
2882 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883 tmp = global->ifaces;
2884 while (tmp &&
2885 (tmp == wpa_s ||
2886 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2887 tmp = tmp->next;
2888 }
2889 if (tmp == NULL)
2890 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002891 /* Disconnect from the P2P group and deinit the interface */
2892 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 }
2894
2895 /*
2896 * Deinit GO data on any possibly remaining interface (if main
2897 * interface is used as GO).
2898 */
2899 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2900 if (wpa_s->ap_iface)
2901 wpas_p2p_group_deinit(wpa_s);
2902 }
2903
2904 p2p_deinit(global->p2p);
2905 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002906 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002907}
2908
2909
2910static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2911{
2912 if (wpa_s->drv_flags &
2913 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2914 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2915 return 1; /* P2P group requires a new interface in every case
2916 */
2917 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2918 return 0; /* driver does not support concurrent operations */
2919 if (wpa_s->global->ifaces->next)
2920 return 1; /* more that one interface already in use */
2921 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2922 return 1; /* this interface is already in use */
2923 return 0;
2924}
2925
2926
2927static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2928 const u8 *peer_addr,
2929 enum p2p_wps_method wps_method,
2930 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002931 unsigned int force_freq, int persistent_group,
2932 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002933{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002934 if (persistent_group && wpa_s->conf->persistent_reconnect)
2935 persistent_group = 2;
2936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002937 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2938 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2939 go_intent, own_interface_addr,
2940 force_freq, persistent_group);
2941 }
2942
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002943 /*
2944 * Increase GO config timeout if HT40 is used since it takes some time
2945 * to scan channels for coex purposes before the BSS can be started.
2946 */
2947 p2p_set_config_timeout(wpa_s->global->p2p,
2948 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2951 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002952 persistent_group, ssid ? ssid->ssid : NULL,
2953 ssid ? ssid->ssid_len : 0,
2954 wpa_s->p2p_pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002955}
2956
2957
2958static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2959 const u8 *peer_addr,
2960 enum p2p_wps_method wps_method,
2961 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002962 unsigned int force_freq, int persistent_group,
2963 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002964{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002965 if (persistent_group && wpa_s->conf->persistent_reconnect)
2966 persistent_group = 2;
2967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002968 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2969 return -1;
2970
2971 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2972 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002973 persistent_group, ssid ? ssid->ssid : NULL,
2974 ssid ? ssid->ssid_len : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975}
2976
2977
2978static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2979{
2980 wpa_s->p2p_join_scan_count++;
2981 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2982 wpa_s->p2p_join_scan_count);
2983 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2984 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2985 " for join operationg - stop join attempt",
2986 MAC2STR(wpa_s->pending_join_iface_addr));
2987 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002988 if (wpa_s->p2p_auto_pd) {
2989 wpa_s->p2p_auto_pd = 0;
2990 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2991 " p2p_dev_addr=" MACSTR " status=N/A",
2992 MAC2STR(wpa_s->pending_join_dev_addr));
2993 return;
2994 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995 wpa_msg(wpa_s->parent, MSG_INFO,
2996 P2P_EVENT_GROUP_FORMATION_FAILURE);
2997 }
2998}
2999
3000
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003001static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
3002{
3003 struct wpa_supplicant *wpa_s = eloop_ctx;
3004 if (!wpa_s->pending_pd_before_join)
3005 return;
3006 /*
3007 * Provision Discovery Response may have been lost - try to connect
3008 * anyway since we do not need any information from this PD.
3009 */
3010 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
3011 "try to connect anyway");
3012 wpas_p2p_join_start(wpa_s);
3013}
3014
3015
Dmitry Shmidt04949592012-07-19 12:16:46 -07003016static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3017{
3018 struct wpa_supplicant *iface;
3019 int shared_freq;
3020 u8 bssid[ETH_ALEN];
3021
3022 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3023 return 0;
3024
3025 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3026 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3027 continue;
3028 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3029 continue;
3030 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3031 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3032 shared_freq = iface->current_ssid->frequency;
3033 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3034 shared_freq = iface->assoc_freq;
3035 else
3036 shared_freq = 0;
3037
3038 if (shared_freq && freq != shared_freq) {
3039 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3040 "connected on %d MHz - new connection on "
3041 "%d MHz", iface->ifname, shared_freq, freq);
3042 return 1;
3043 }
3044 }
3045
3046 shared_freq = wpa_drv_shared_freq(wpa_s);
3047 if (shared_freq > 0 && shared_freq != freq) {
3048 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3049 "virtual interface connected on %d MHz - new "
3050 "connection on %d MHz", shared_freq, freq);
3051 return 1;
3052 }
3053
3054 return 0;
3055}
3056
3057
3058static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3059 const u8 *peer_dev_addr)
3060{
3061 struct wpa_bss *bss;
3062 int updated;
3063
3064 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3065 if (bss == NULL)
3066 return -1;
3067 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3068 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3069 "last scan");
3070 return 0;
3071 }
3072
3073 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3074 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3075 "%ld.%06ld (%supdated in last scan)",
3076 bss->last_update.sec, bss->last_update.usec,
3077 updated ? "": "not ");
3078
3079 return updated;
3080}
3081
3082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3084 struct wpa_scan_results *scan_res)
3085{
3086 struct wpa_bss *bss;
3087 int freq;
3088 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3091
3092 if (wpa_s->global->p2p_disabled)
3093 return;
3094
Dmitry Shmidt04949592012-07-19 12:16:46 -07003095 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3096 scan_res ? (int) scan_res->num : -1,
3097 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098
3099 if (scan_res)
3100 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3101
Dmitry Shmidt04949592012-07-19 12:16:46 -07003102 if (wpa_s->p2p_auto_pd) {
3103 int join = wpas_p2p_peer_go(wpa_s,
3104 wpa_s->pending_join_dev_addr);
3105 if (join == 0 &&
3106 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3107 wpa_s->auto_pd_scan_retry++;
3108 bss = wpa_bss_get_bssid(wpa_s,
3109 wpa_s->pending_join_dev_addr);
3110 if (bss) {
3111 freq = bss->freq;
3112 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3113 "the peer " MACSTR " at %d MHz",
3114 wpa_s->auto_pd_scan_retry,
3115 MAC2STR(wpa_s->
3116 pending_join_dev_addr),
3117 freq);
3118 wpas_p2p_join_scan_req(wpa_s, freq);
3119 return;
3120 }
3121 }
3122
3123 if (join < 0)
3124 join = 0;
3125
3126 wpa_s->p2p_auto_pd = 0;
3127 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3128 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3129 MAC2STR(wpa_s->pending_join_dev_addr), join);
3130 if (p2p_prov_disc_req(wpa_s->global->p2p,
3131 wpa_s->pending_join_dev_addr,
3132 wpa_s->pending_pd_config_methods, join,
3133 0) < 0) {
3134 wpa_s->p2p_auto_pd = 0;
3135 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3136 " p2p_dev_addr=" MACSTR " status=N/A",
3137 MAC2STR(wpa_s->pending_join_dev_addr));
3138 }
3139 return;
3140 }
3141
3142 if (wpa_s->p2p_auto_join) {
3143 int join = wpas_p2p_peer_go(wpa_s,
3144 wpa_s->pending_join_dev_addr);
3145 if (join < 0) {
3146 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3147 "running a GO -> use GO Negotiation");
3148 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3149 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3150 wpa_s->p2p_persistent_group, 0, 0, 0,
3151 wpa_s->p2p_go_intent,
3152 wpa_s->p2p_connect_freq,
3153 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003154 wpa_s->p2p_pd_before_go_neg,
3155 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003156 return;
3157 }
3158
3159 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3160 "try to join the group", join ? "" :
3161 " in older scan");
3162 if (!join)
3163 wpa_s->p2p_fallback_to_go_neg = 1;
3164 }
3165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3167 wpa_s->pending_join_iface_addr);
3168 if (freq < 0 &&
3169 p2p_get_interface_addr(wpa_s->global->p2p,
3170 wpa_s->pending_join_dev_addr,
3171 iface_addr) == 0 &&
3172 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3173 {
3174 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3175 "address for join from " MACSTR " to " MACSTR
3176 " based on newly discovered P2P peer entry",
3177 MAC2STR(wpa_s->pending_join_iface_addr),
3178 MAC2STR(iface_addr));
3179 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3180 ETH_ALEN);
3181
3182 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3183 wpa_s->pending_join_iface_addr);
3184 }
3185 if (freq >= 0) {
3186 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3187 "from P2P peer table: %d MHz", freq);
3188 }
3189 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3190 if (bss) {
3191 freq = bss->freq;
3192 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3193 "from BSS table: %d MHz", freq);
3194 }
3195 if (freq > 0) {
3196 u16 method;
3197
Dmitry Shmidt04949592012-07-19 12:16:46 -07003198 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3199 wpa_msg(wpa_s->parent, MSG_INFO,
3200 P2P_EVENT_GROUP_FORMATION_FAILURE
3201 "reason=FREQ_CONFLICT");
3202 return;
3203 }
3204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3206 "prior to joining an existing group (GO " MACSTR
3207 " freq=%u MHz)",
3208 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3209 wpa_s->pending_pd_before_join = 1;
3210
3211 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212 case WPS_PIN_DISPLAY:
3213 method = WPS_CONFIG_KEYPAD;
3214 break;
3215 case WPS_PIN_KEYPAD:
3216 method = WPS_CONFIG_DISPLAY;
3217 break;
3218 case WPS_PBC:
3219 method = WPS_CONFIG_PUSHBUTTON;
3220 break;
3221 default:
3222 method = 0;
3223 break;
3224 }
3225
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003226 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3227 wpa_s->pending_join_dev_addr) ==
3228 method)) {
3229 /*
3230 * We have already performed provision discovery for
3231 * joining the group. Proceed directly to join
3232 * operation without duplicated provision discovery. */
3233 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3234 "with " MACSTR " already done - proceed to "
3235 "join",
3236 MAC2STR(wpa_s->pending_join_dev_addr));
3237 wpa_s->pending_pd_before_join = 0;
3238 goto start;
3239 }
3240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003241 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003242 wpa_s->pending_join_dev_addr, method, 1,
3243 freq) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003244 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3245 "Discovery Request before joining an "
3246 "existing group");
3247 wpa_s->pending_pd_before_join = 0;
3248 goto start;
3249 }
3250
3251 /*
3252 * Actual join operation will be started from the Action frame
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003253 * TX status callback (if no ACK is received) or when the
3254 * Provision Discovery Response is received. Use a short
3255 * timeout as a backup mechanism should the Provision Discovery
3256 * Response be lost for any reason.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003258 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3259 NULL);
3260 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3261 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262 return;
3263 }
3264
3265 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3266 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3267 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3268 wpas_p2p_check_join_scan_limit(wpa_s);
3269 return;
3270
3271start:
3272 /* Start join operation immediately */
3273 wpas_p2p_join_start(wpa_s);
3274}
3275
3276
Dmitry Shmidt04949592012-07-19 12:16:46 -07003277static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279 int ret;
3280 struct wpa_driver_scan_params params;
3281 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003282 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003283 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003284#ifdef ANDROID_P2P
3285 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003287 /* If freq is not provided, check the operating freq of the GO and do a
3288 * a directed scan to save time
3289 */
3290 if(!freq) {
3291 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3292 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3293 }
3294#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003295 os_memset(&params, 0, sizeof(params));
3296
3297 /* P2P Wildcard SSID */
3298 params.num_ssids = 1;
3299 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3300 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3301
3302 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003303 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3304 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3305 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306 if (wps_ie == NULL) {
3307 wpas_p2p_scan_res_join(wpa_s, NULL);
3308 return;
3309 }
3310
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003311 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3312 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 if (ies == NULL) {
3314 wpabuf_free(wps_ie);
3315 wpas_p2p_scan_res_join(wpa_s, NULL);
3316 return;
3317 }
3318 wpabuf_put_buf(ies, wps_ie);
3319 wpabuf_free(wps_ie);
3320
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003321 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003322
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003323 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324 params.extra_ies = wpabuf_head(ies);
3325 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003326 if (freq > 0) {
3327 freqs[0] = freq;
3328 params.freqs = freqs;
3329 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003330
3331 /*
3332 * Run a scan to update BSS table and start Provision Discovery once
3333 * the new scan results become available.
3334 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003335 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003336 if (!ret)
3337 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338
3339 wpabuf_free(ies);
3340
3341 if (ret) {
3342 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3343 "try again later");
3344 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3345 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3346 wpas_p2p_check_join_scan_limit(wpa_s);
3347 }
3348}
3349
3350
Dmitry Shmidt04949592012-07-19 12:16:46 -07003351static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3352{
3353 struct wpa_supplicant *wpa_s = eloop_ctx;
3354 wpas_p2p_join_scan_req(wpa_s, 0);
3355}
3356
3357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003358static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003359 const u8 *dev_addr, enum p2p_wps_method wps_method,
3360 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003361{
3362 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003363 MACSTR " dev " MACSTR ")%s",
3364 MAC2STR(iface_addr), MAC2STR(dev_addr),
3365 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366
Dmitry Shmidt04949592012-07-19 12:16:46 -07003367 wpa_s->p2p_auto_pd = 0;
3368 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3370 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3371 wpa_s->pending_join_wps_method = wps_method;
3372
3373 /* Make sure we are not running find during connection establishment */
3374 wpas_p2p_stop_find(wpa_s);
3375
3376 wpa_s->p2p_join_scan_count = 0;
3377 wpas_p2p_join_scan(wpa_s, NULL);
3378 return 0;
3379}
3380
3381
3382static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3383{
3384 struct wpa_supplicant *group;
3385 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003386 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003388 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003389 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3390 if (group == NULL)
3391 return -1;
3392 if (group != wpa_s) {
3393 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3394 sizeof(group->p2p_pin));
3395 group->p2p_wps_method = wpa_s->p2p_wps_method;
3396 }
3397
3398 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003399 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003400
3401 os_memset(&res, 0, sizeof(res));
3402 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3403 ETH_ALEN);
3404 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003405 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3406 if (bss) {
3407 res.freq = bss->freq;
3408 res.ssid_len = bss->ssid_len;
3409 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3410 }
3411
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003412 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3413 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3414 "starting client");
3415 wpa_drv_cancel_remain_on_channel(wpa_s);
3416 wpa_s->off_channel_freq = 0;
3417 wpa_s->roc_waiting_drv_freq = 0;
3418 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419 wpas_start_wps_enrollee(group, &res);
3420
3421 /*
3422 * Allow a longer timeout for join-a-running-group than normal 15
3423 * second group formation timeout since the GO may not have authorized
3424 * our connection yet.
3425 */
3426 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3427 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3428 wpa_s, NULL);
3429
3430 return 0;
3431}
3432
3433
3434/**
3435 * wpas_p2p_connect - Request P2P Group Formation to be started
3436 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3437 * @peer_addr: Address of the peer P2P Device
3438 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3439 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003440 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 * @join: Whether to join an existing group (as a client) instead of starting
3442 * Group Owner negotiation; @peer_addr is BSSID in that case
3443 * @auth: Whether to only authorize the connection instead of doing that and
3444 * initiating Group Owner negotiation
3445 * @go_intent: GO Intent or -1 to use default
3446 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003447 * @persistent_id: Persistent group credentials to use for forcing GO
3448 * parameters or -1 to generate new values (SSID/passphrase)
3449 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3450 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003451 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003452 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3453 * failure, -2 on failure due to channel not currently available,
3454 * -3 if forced channel is not supported
3455 */
3456int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3457 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003458 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003459 int go_intent, int freq, int persistent_id, int pd,
3460 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003461{
3462 int force_freq = 0, oper_freq = 0;
3463 u8 bssid[ETH_ALEN];
3464 int ret = 0;
3465 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003466 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003467 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003468
3469 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3470 return -1;
3471
Dmitry Shmidt04949592012-07-19 12:16:46 -07003472 if (persistent_id >= 0) {
3473 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3474 if (ssid == NULL || ssid->disabled != 2 ||
3475 ssid->mode != WPAS_MODE_P2P_GO)
3476 return -1;
3477 }
3478
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003479 if (go_intent < 0)
3480 go_intent = wpa_s->conf->p2p_go_intent;
3481
3482 if (!auth)
3483 wpa_s->p2p_long_listen = 0;
3484
3485 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003486 wpa_s->p2p_persistent_group = !!persistent_group;
3487 wpa_s->p2p_persistent_id = persistent_id;
3488 wpa_s->p2p_go_intent = go_intent;
3489 wpa_s->p2p_connect_freq = freq;
3490 wpa_s->p2p_fallback_to_go_neg = 0;
3491 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003492 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003493
3494 if (pin)
3495 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3496 else if (wps_method == WPS_PIN_DISPLAY) {
3497 ret = wps_generate_pin();
3498 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3499 ret);
3500 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3501 wpa_s->p2p_pin);
3502 } else
3503 wpa_s->p2p_pin[0] = '\0';
3504
Dmitry Shmidt04949592012-07-19 12:16:46 -07003505 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003506 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3507 if (auth) {
3508 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3509 "connect a running group from " MACSTR,
3510 MAC2STR(peer_addr));
3511 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3512 return ret;
3513 }
3514 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3515 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3516 iface_addr) < 0) {
3517 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3518 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3519 dev_addr);
3520 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003521 if (auto_join) {
3522 os_get_time(&wpa_s->p2p_auto_started);
3523 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3524 "%ld.%06ld",
3525 wpa_s->p2p_auto_started.sec,
3526 wpa_s->p2p_auto_started.usec);
3527 }
3528 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3529 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003530 return -1;
3531 return ret;
3532 }
3533
3534 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3535 wpa_s->assoc_freq)
3536 oper_freq = wpa_s->assoc_freq;
3537 else {
3538 oper_freq = wpa_drv_shared_freq(wpa_s);
3539 if (oper_freq < 0)
3540 oper_freq = 0;
3541 }
3542
3543 if (freq > 0) {
3544 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3545 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3546 "(%u MHz) is not supported for P2P uses",
3547 freq);
3548 return -3;
3549 }
3550
3551 if (oper_freq > 0 && freq != oper_freq &&
3552 !(wpa_s->drv_flags &
3553 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3554 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3555 "on %u MHz while connected on another "
3556 "channel (%u MHz)", freq, oper_freq);
3557 return -2;
3558 }
3559 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3560 "requested channel (%u MHz)", freq);
3561 force_freq = freq;
3562 } else if (oper_freq > 0 &&
3563 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3564 if (!(wpa_s->drv_flags &
3565 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3566 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3567 "while connected on non-P2P supported "
3568 "channel (%u MHz)", oper_freq);
3569 return -2;
3570 }
3571 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3572 "(%u MHz) not available for P2P - try to use "
3573 "another channel", oper_freq);
3574 force_freq = 0;
3575 } else if (oper_freq > 0) {
3576 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3577 "channel we are already using (%u MHz) on another "
3578 "interface", oper_freq);
3579 force_freq = oper_freq;
3580 }
3581
3582 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3583
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003584 if (wpa_s->create_p2p_iface) {
3585 /* Prepare to add a new interface for the group */
3586 iftype = WPA_IF_P2P_GROUP;
3587 if (go_intent == 15)
3588 iftype = WPA_IF_P2P_GO;
3589 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3590 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3591 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003592 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003593 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003595 if_addr = wpa_s->pending_interface_addr;
3596 } else
3597 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003598
3599 if (auth) {
3600 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003601 go_intent, if_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003602 force_freq, persistent_group, ssid) <
3603 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604 return -1;
3605 return ret;
3606 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003607
3608 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3609 go_intent, if_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003610 persistent_group, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003611 if (wpa_s->create_p2p_iface)
3612 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613 return -1;
3614 }
3615 return ret;
3616}
3617
3618
3619/**
3620 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3621 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3622 * @freq: Frequency of the channel in MHz
3623 * @duration: Duration of the stay on the channel in milliseconds
3624 *
3625 * This callback is called when the driver indicates that it has started the
3626 * requested remain-on-channel duration.
3627 */
3628void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3629 unsigned int freq, unsigned int duration)
3630{
3631 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3632 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3634 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3635 wpa_s->pending_listen_duration);
3636 wpa_s->pending_listen_freq = 0;
3637 }
3638}
3639
3640
3641static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3642 unsigned int timeout)
3643{
3644 /* Limit maximum Listen state time based on driver limitation. */
3645 if (timeout > wpa_s->max_remain_on_chan)
3646 timeout = wpa_s->max_remain_on_chan;
3647
3648 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3649 return wpa_drv_p2p_listen(wpa_s, timeout);
3650
3651 return p2p_listen(wpa_s->global->p2p, timeout);
3652}
3653
3654
3655/**
3656 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3657 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3658 * @freq: Frequency of the channel in MHz
3659 *
3660 * This callback is called when the driver indicates that a remain-on-channel
3661 * operation has been completed, i.e., the duration on the requested channel
3662 * has timed out.
3663 */
3664void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3665 unsigned int freq)
3666{
3667 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3668 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3669 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3671 return;
3672 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3673 return; /* P2P module started a new operation */
3674 if (wpa_s->pending_action_tx)
3675 return;
3676 if (wpa_s->p2p_long_listen > 0)
3677 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3678 if (wpa_s->p2p_long_listen > 0) {
3679 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3680 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3681 }
3682}
3683
3684
3685/**
3686 * wpas_p2p_group_remove - Remove a P2P group
3687 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3688 * @ifname: Network interface name of the group interface or "*" to remove all
3689 * groups
3690 * Returns: 0 on success, -1 on failure
3691 *
3692 * This function is used to remove a P2P group. This can be used to disconnect
3693 * from a group in which the local end is a P2P Client or to end a P2P Group in
3694 * case the local end is the Group Owner. If a virtual network interface was
3695 * created for this group, that interface will be removed. Otherwise, only the
3696 * configured P2P group network will be removed from the interface.
3697 */
3698int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3699{
3700 struct wpa_global *global = wpa_s->global;
3701
3702 if (os_strcmp(ifname, "*") == 0) {
3703 struct wpa_supplicant *prev;
3704 wpa_s = global->ifaces;
3705 while (wpa_s) {
3706 prev = wpa_s;
3707 wpa_s = wpa_s->next;
3708 wpas_p2p_disconnect(prev);
3709 }
3710 return 0;
3711 }
3712
3713 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3714 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3715 break;
3716 }
3717
3718 return wpas_p2p_disconnect(wpa_s);
3719}
3720
3721
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003722static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3723 struct p2p_go_neg_results *params,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003724 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003725{
3726 u8 bssid[ETH_ALEN];
3727 int res;
3728
3729 os_memset(params, 0, sizeof(*params));
3730 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003731 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732 if (freq) {
3733 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3734 "frequency %d MHz", freq);
3735 params->freq = freq;
3736 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3737 wpa_s->conf->p2p_oper_channel >= 1 &&
3738 wpa_s->conf->p2p_oper_channel <= 11) {
3739 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3740 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3741 "frequency %d MHz", params->freq);
3742 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003743 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3745 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3746 "frequency %d MHz", params->freq);
3747 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3748 wpa_s->best_overall_freq > 0 &&
3749 p2p_supported_freq(wpa_s->global->p2p,
3750 wpa_s->best_overall_freq)) {
3751 params->freq = wpa_s->best_overall_freq;
3752 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3753 "channel %d MHz", params->freq);
3754 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3755 wpa_s->best_24_freq > 0 &&
3756 p2p_supported_freq(wpa_s->global->p2p,
3757 wpa_s->best_24_freq)) {
3758 params->freq = wpa_s->best_24_freq;
3759 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3760 "channel %d MHz", params->freq);
3761 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3762 wpa_s->best_5_freq > 0 &&
3763 p2p_supported_freq(wpa_s->global->p2p,
3764 wpa_s->best_5_freq)) {
3765 params->freq = wpa_s->best_5_freq;
3766 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3767 "channel %d MHz", params->freq);
3768 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003769 int chan;
3770 for (chan = 0; chan < 11; chan++) {
3771 params->freq = 2412 + chan * 5;
3772 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3773 params->freq))
3774 break;
3775 }
3776 if (chan == 11) {
3777 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3778 "allowed");
3779 return -1;
3780 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3782 "known)", params->freq);
3783 }
3784
3785 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3786 wpa_s->assoc_freq && !freq) {
3787 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3788 "already using");
3789 params->freq = wpa_s->assoc_freq;
3790 }
3791
3792 res = wpa_drv_shared_freq(wpa_s);
3793 if (res > 0 && !freq) {
3794 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3795 "already using on a shared interface");
3796 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003797 } else if (res > 0 && freq != res &&
3798 !(wpa_s->drv_flags &
3799 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3800 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3801 "while connected on another channel (%u MHz)",
3802 freq, res);
3803 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003804 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003805
3806 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807}
3808
3809
3810static struct wpa_supplicant *
3811wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3812 int go)
3813{
3814 struct wpa_supplicant *group_wpa_s;
3815
3816 if (!wpas_p2p_create_iface(wpa_s))
3817 return wpa_s;
3818
3819 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3820 WPA_IF_P2P_CLIENT) < 0)
3821 return NULL;
3822 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3823 if (group_wpa_s == NULL) {
3824 wpas_p2p_remove_pending_group_interface(wpa_s);
3825 return NULL;
3826 }
3827
3828 return group_wpa_s;
3829}
3830
3831
3832/**
3833 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3834 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3835 * @persistent_group: Whether to create a persistent group
3836 * @freq: Frequency for the group or 0 to indicate no hardcoding
3837 * Returns: 0 on success, -1 on failure
3838 *
3839 * This function creates a new P2P group with the local end as the Group Owner,
3840 * i.e., without using Group Owner Negotiation.
3841 */
3842int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003843 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844{
3845 struct p2p_go_neg_results params;
3846 unsigned int r;
3847
3848 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3849 return -1;
3850
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003851 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003852 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003853 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003855 if (freq == 2) {
3856 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3857 "band");
3858 if (wpa_s->best_24_freq > 0 &&
3859 p2p_supported_freq(wpa_s->global->p2p,
3860 wpa_s->best_24_freq)) {
3861 freq = wpa_s->best_24_freq;
3862 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3863 "channel: %d MHz", freq);
3864 } else {
3865 os_get_random((u8 *) &r, sizeof(r));
3866 freq = 2412 + (r % 3) * 25;
3867 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3868 "channel: %d MHz", freq);
3869 }
3870 }
3871
3872 if (freq == 5) {
3873 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3874 "band");
3875 if (wpa_s->best_5_freq > 0 &&
3876 p2p_supported_freq(wpa_s->global->p2p,
3877 wpa_s->best_5_freq)) {
3878 freq = wpa_s->best_5_freq;
3879 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3880 "channel: %d MHz", freq);
3881 } else {
3882 os_get_random((u8 *) &r, sizeof(r));
3883 freq = 5180 + (r % 4) * 20;
3884 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3885 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3886 "5 GHz channel for P2P group");
3887 return -1;
3888 }
3889 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3890 "channel: %d MHz", freq);
3891 }
3892 }
3893
3894 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3895 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3896 "(%u MHz) is not supported for P2P uses",
3897 freq);
3898 return -1;
3899 }
3900
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003901 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003902 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003903 if (params.freq &&
3904 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3905 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3906 "(%u MHz) is not supported for P2P uses",
3907 params.freq);
3908 return -1;
3909 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003910 p2p_go_params(wpa_s->global->p2p, &params);
3911 params.persistent_group = persistent_group;
3912
3913 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3914 if (wpa_s == NULL)
3915 return -1;
3916 wpas_start_wps_go(wpa_s, &params, 0);
3917
3918 return 0;
3919}
3920
3921
3922static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3923 struct wpa_ssid *params, int addr_allocated)
3924{
3925 struct wpa_ssid *ssid;
3926
3927 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3928 if (wpa_s == NULL)
3929 return -1;
3930
3931 wpa_supplicant_ap_deinit(wpa_s);
3932
3933 ssid = wpa_config_add_network(wpa_s->conf);
3934 if (ssid == NULL)
3935 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936 wpa_config_set_network_defaults(ssid);
3937 ssid->temporary = 1;
3938 ssid->proto = WPA_PROTO_RSN;
3939 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3940 ssid->group_cipher = WPA_CIPHER_CCMP;
3941 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3942 ssid->ssid = os_malloc(params->ssid_len);
3943 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 wpa_config_remove_network(wpa_s->conf, ssid->id);
3945 return -1;
3946 }
3947 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3948 ssid->ssid_len = params->ssid_len;
3949 ssid->p2p_group = 1;
3950 ssid->export_keys = 1;
3951 if (params->psk_set) {
3952 os_memcpy(ssid->psk, params->psk, 32);
3953 ssid->psk_set = 1;
3954 }
3955 if (params->passphrase)
3956 ssid->passphrase = os_strdup(params->passphrase);
3957
3958 wpa_supplicant_select_network(wpa_s, ssid);
3959
3960 wpa_s->show_group_started = 1;
3961
3962 return 0;
3963}
3964
3965
3966int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3967 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003968 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969{
3970 struct p2p_go_neg_results params;
3971 int go = 0;
3972
3973 if (ssid->disabled != 2 || ssid->ssid == NULL)
3974 return -1;
3975
3976 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3977 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3978 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3979 "already running");
3980 return 0;
3981 }
3982
3983 /* Make sure we are not running find during connection establishment */
3984 wpas_p2p_stop_find(wpa_s);
3985
Dmitry Shmidt04949592012-07-19 12:16:46 -07003986 wpa_s->p2p_fallback_to_go_neg = 0;
3987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988 if (ssid->mode == WPAS_MODE_INFRA)
3989 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3990
3991 if (ssid->mode != WPAS_MODE_P2P_GO)
3992 return -1;
3993
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003994 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003995 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996
3997 params.role_go = 1;
3998 if (ssid->passphrase == NULL ||
3999 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4000 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4001 "group");
4002 return -1;
4003 }
4004 os_strlcpy(params.passphrase, ssid->passphrase,
4005 sizeof(params.passphrase));
4006 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4007 params.ssid_len = ssid->ssid_len;
4008 params.persistent_group = 1;
4009
4010 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4011 if (wpa_s == NULL)
4012 return -1;
4013
4014 wpas_start_wps_go(wpa_s, &params, 0);
4015
4016 return 0;
4017}
4018
4019
4020static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4021 struct wpabuf *proberesp_ies)
4022{
4023 struct wpa_supplicant *wpa_s = ctx;
4024 if (wpa_s->ap_iface) {
4025 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004026 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4027 wpabuf_free(beacon_ies);
4028 wpabuf_free(proberesp_ies);
4029 return;
4030 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031 if (beacon_ies) {
4032 wpabuf_free(hapd->p2p_beacon_ie);
4033 hapd->p2p_beacon_ie = beacon_ies;
4034 }
4035 wpabuf_free(hapd->p2p_probe_resp_ie);
4036 hapd->p2p_probe_resp_ie = proberesp_ies;
4037 } else {
4038 wpabuf_free(beacon_ies);
4039 wpabuf_free(proberesp_ies);
4040 }
4041 wpa_supplicant_ap_update_beacon(wpa_s);
4042}
4043
4044
4045static void wpas_p2p_idle_update(void *ctx, int idle)
4046{
4047 struct wpa_supplicant *wpa_s = ctx;
4048 if (!wpa_s->ap_iface)
4049 return;
4050 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004051 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052 wpas_p2p_set_group_idle_timeout(wpa_s);
4053 else
4054 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4055}
4056
4057
4058struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004059 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060{
4061 struct p2p_group *group;
4062 struct p2p_group_config *cfg;
4063
4064 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4065 return NULL;
4066 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4067 return NULL;
4068
4069 cfg = os_zalloc(sizeof(*cfg));
4070 if (cfg == NULL)
4071 return NULL;
4072
Dmitry Shmidt04949592012-07-19 12:16:46 -07004073 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004074 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004075 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004076 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4078 if (wpa_s->max_stations &&
4079 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4080 cfg->max_clients = wpa_s->max_stations;
4081 else
4082 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004083 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4084 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085 cfg->cb_ctx = wpa_s;
4086 cfg->ie_update = wpas_p2p_ie_update;
4087 cfg->idle_update = wpas_p2p_idle_update;
4088
4089 group = p2p_group_init(wpa_s->global->p2p, cfg);
4090 if (group == NULL)
4091 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004092 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093 p2p_group_notif_formation_done(group);
4094 wpa_s->p2p_group = group;
4095 return group;
4096}
4097
4098
4099void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4100 int registrar)
4101{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004102 struct wpa_ssid *ssid = wpa_s->current_ssid;
4103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104 if (!wpa_s->p2p_in_provisioning) {
4105 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4106 "provisioning not in progress");
4107 return;
4108 }
4109
Dmitry Shmidt04949592012-07-19 12:16:46 -07004110 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4111 u8 go_dev_addr[ETH_ALEN];
4112 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4113 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4114 ssid->ssid_len);
4115 /* Clear any stored provisioning info */
4116 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4117 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004119 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4120 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004121 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4122 /*
4123 * Use a separate timeout for initial data connection to
4124 * complete to allow the group to be removed automatically if
4125 * something goes wrong in this step before the P2P group idle
4126 * timeout mechanism is taken into use.
4127 */
4128 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4129 wpas_p2p_group_formation_timeout,
4130 wpa_s->parent, NULL);
4131 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004132 if (wpa_s->global->p2p)
4133 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4134 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4135 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4136 wpas_group_formation_completed(wpa_s, 1);
4137}
4138
4139
Jouni Malinen75ecf522011-06-27 15:19:46 -07004140void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4141 struct wps_event_fail *fail)
4142{
4143 if (!wpa_s->p2p_in_provisioning) {
4144 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4145 "provisioning not in progress");
4146 return;
4147 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004148
4149 if (wpa_s->go_params) {
4150 p2p_clear_provisioning_info(
4151 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004152 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004153 }
4154
Jouni Malinen75ecf522011-06-27 15:19:46 -07004155 wpas_notify_p2p_wps_failed(wpa_s, fail);
4156}
4157
4158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004159int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004160 const char *config_method,
4161 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162{
4163 u16 config_methods;
4164
Dmitry Shmidt04949592012-07-19 12:16:46 -07004165 wpa_s->p2p_fallback_to_go_neg = 0;
4166 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004167 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004168 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004169 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004170 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004171 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4172 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004174 else {
4175 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004177 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178
Dmitry Shmidt04949592012-07-19 12:16:46 -07004179 if (use == WPAS_P2P_PD_AUTO) {
4180 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4181 wpa_s->pending_pd_config_methods = config_methods;
4182 wpa_s->p2p_auto_pd = 1;
4183 wpa_s->p2p_auto_join = 0;
4184 wpa_s->pending_pd_before_join = 0;
4185 wpa_s->auto_pd_scan_retry = 0;
4186 wpas_p2p_stop_find(wpa_s);
4187 wpa_s->p2p_join_scan_count = 0;
4188 os_get_time(&wpa_s->p2p_auto_started);
4189 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4190 wpa_s->p2p_auto_started.sec,
4191 wpa_s->p2p_auto_started.usec);
4192 wpas_p2p_join_scan(wpa_s, NULL);
4193 return 0;
4194 }
4195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004196 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4197 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004198 config_methods,
4199 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004200 }
4201
4202 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4203 return -1;
4204
4205 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004206 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4207 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004208}
4209
4210
4211int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4212 char *end)
4213{
4214 return p2p_scan_result_text(ies, ies_len, buf, end);
4215}
4216
4217
4218static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4219{
4220 if (!wpa_s->pending_action_tx)
4221 return;
4222
4223 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4224 "operation request");
4225 wpabuf_free(wpa_s->pending_action_tx);
4226 wpa_s->pending_action_tx = NULL;
4227}
4228
4229
4230int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4231 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004232 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004233 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234{
4235 wpas_p2p_clear_pending_action_tx(wpa_s);
4236 wpa_s->p2p_long_listen = 0;
4237
4238 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4239 return wpa_drv_p2p_find(wpa_s, timeout, type);
4240
Dmitry Shmidt04949592012-07-19 12:16:46 -07004241 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4242 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243 return -1;
4244
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004245 wpa_supplicant_cancel_sched_scan(wpa_s);
4246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004247 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004248 num_req_dev_types, req_dev_types, dev_id,
4249 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250}
4251
4252
4253void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4254{
4255 wpas_p2p_clear_pending_action_tx(wpa_s);
4256 wpa_s->p2p_long_listen = 0;
4257 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4258 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004259 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004260
4261 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4262 wpa_drv_p2p_stop_find(wpa_s);
4263 return;
4264 }
4265
4266 if (wpa_s->global->p2p)
4267 p2p_stop_find(wpa_s->global->p2p);
4268
4269 wpas_p2p_remove_pending_group_interface(wpa_s);
4270}
4271
4272
4273static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4274{
4275 struct wpa_supplicant *wpa_s = eloop_ctx;
4276 wpa_s->p2p_long_listen = 0;
4277}
4278
4279
4280int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4281{
4282 int res;
4283
4284 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4285 return -1;
4286
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004287 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288 wpas_p2p_clear_pending_action_tx(wpa_s);
4289
4290 if (timeout == 0) {
4291 /*
4292 * This is a request for unlimited Listen state. However, at
4293 * least for now, this is mapped to a Listen state for one
4294 * hour.
4295 */
4296 timeout = 3600;
4297 }
4298 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4299 wpa_s->p2p_long_listen = 0;
4300
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004301 /*
4302 * Stop previous find/listen operation to avoid trying to request a new
4303 * remain-on-channel operation while the driver is still running the
4304 * previous one.
4305 */
4306 if (wpa_s->global->p2p)
4307 p2p_stop_find(wpa_s->global->p2p);
4308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4310 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4311 wpa_s->p2p_long_listen = timeout * 1000;
4312 eloop_register_timeout(timeout, 0,
4313 wpas_p2p_long_listen_timeout,
4314 wpa_s, NULL);
4315 }
4316
4317 return res;
4318}
4319
4320
4321int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4322 u8 *buf, size_t len, int p2p_group)
4323{
4324 struct wpabuf *p2p_ie;
4325 int ret;
4326
4327 if (wpa_s->global->p2p_disabled)
4328 return -1;
4329 if (wpa_s->global->p2p == NULL)
4330 return -1;
4331 if (bss == NULL)
4332 return -1;
4333
4334 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4335 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4336 p2p_group, p2p_ie);
4337 wpabuf_free(p2p_ie);
4338
4339 return ret;
4340}
4341
4342
4343int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004344 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004345 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004346{
4347 if (wpa_s->global->p2p_disabled)
4348 return 0;
4349 if (wpa_s->global->p2p == NULL)
4350 return 0;
4351
Dmitry Shmidt04949592012-07-19 12:16:46 -07004352 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4353 ie, ie_len)) {
4354 case P2P_PREQ_NOT_P2P:
4355 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4356 ssi_signal);
4357 /* fall through */
4358 case P2P_PREQ_MALFORMED:
4359 case P2P_PREQ_NOT_LISTEN:
4360 case P2P_PREQ_NOT_PROCESSED:
4361 default: /* make gcc happy */
4362 return 0;
4363 case P2P_PREQ_PROCESSED:
4364 return 1;
4365 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366}
4367
4368
4369void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4370 const u8 *sa, const u8 *bssid,
4371 u8 category, const u8 *data, size_t len, int freq)
4372{
4373 if (wpa_s->global->p2p_disabled)
4374 return;
4375 if (wpa_s->global->p2p == NULL)
4376 return;
4377
4378 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4379 freq);
4380}
4381
4382
4383void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4384{
4385 if (wpa_s->global->p2p_disabled)
4386 return;
4387 if (wpa_s->global->p2p == NULL)
4388 return;
4389
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004390 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391}
4392
4393
4394void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4395{
4396 p2p_group_deinit(wpa_s->p2p_group);
4397 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004398
4399 wpa_s->ap_configured_cb = NULL;
4400 wpa_s->ap_configured_cb_ctx = NULL;
4401 wpa_s->ap_configured_cb_data = NULL;
4402 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004403}
4404
4405
4406int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4407{
4408 wpa_s->p2p_long_listen = 0;
4409
4410 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4411 return wpa_drv_p2p_reject(wpa_s, addr);
4412
4413 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4414 return -1;
4415
4416 return p2p_reject(wpa_s->global->p2p, addr);
4417}
4418
4419
4420/* Invite to reinvoke a persistent group */
4421int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004422 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4423 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424{
4425 enum p2p_invite_role role;
4426 u8 *bssid = NULL;
4427
Jouni Malinen31be0a42012-08-31 21:20:51 +03004428 wpa_s->p2p_persistent_go_freq = freq;
4429 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004430 if (ssid->mode == WPAS_MODE_P2P_GO) {
4431 role = P2P_INVITE_ROLE_GO;
4432 if (peer_addr == NULL) {
4433 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4434 "address in invitation command");
4435 return -1;
4436 }
4437 if (wpas_p2p_create_iface(wpa_s)) {
4438 if (wpas_p2p_add_group_interface(wpa_s,
4439 WPA_IF_P2P_GO) < 0) {
4440 wpa_printf(MSG_ERROR, "P2P: Failed to "
4441 "allocate a new interface for the "
4442 "group");
4443 return -1;
4444 }
4445 bssid = wpa_s->pending_interface_addr;
4446 } else
4447 bssid = wpa_s->own_addr;
4448 } else {
4449 role = P2P_INVITE_ROLE_CLIENT;
4450 peer_addr = ssid->bssid;
4451 }
4452 wpa_s->pending_invite_ssid_id = ssid->id;
4453
4454 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4455 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4456 ssid->ssid, ssid->ssid_len,
4457 go_dev_addr, 1);
4458
4459 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4460 return -1;
4461
4462 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004463 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004464}
4465
4466
4467/* Invite to join an active group */
4468int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4469 const u8 *peer_addr, const u8 *go_dev_addr)
4470{
4471 struct wpa_global *global = wpa_s->global;
4472 enum p2p_invite_role role;
4473 u8 *bssid = NULL;
4474 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004475 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004476
Jouni Malinen31be0a42012-08-31 21:20:51 +03004477 wpa_s->p2p_persistent_go_freq = 0;
4478 wpa_s->p2p_go_ht40 = 0;
4479
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004480 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4481 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4482 break;
4483 }
4484 if (wpa_s == NULL) {
4485 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4486 return -1;
4487 }
4488
4489 ssid = wpa_s->current_ssid;
4490 if (ssid == NULL) {
4491 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4492 "invitation");
4493 return -1;
4494 }
4495
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004496 persistent = ssid->p2p_persistent_group &&
4497 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4498 ssid->ssid, ssid->ssid_len);
4499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004500 if (ssid->mode == WPAS_MODE_P2P_GO) {
4501 role = P2P_INVITE_ROLE_ACTIVE_GO;
4502 bssid = wpa_s->own_addr;
4503 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004504 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 } else {
4506 role = P2P_INVITE_ROLE_CLIENT;
4507 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4508 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4509 "invite to current group");
4510 return -1;
4511 }
4512 bssid = wpa_s->bssid;
4513 if (go_dev_addr == NULL &&
4514 !is_zero_ether_addr(wpa_s->go_dev_addr))
4515 go_dev_addr = wpa_s->go_dev_addr;
4516 }
4517 wpa_s->parent->pending_invite_ssid_id = -1;
4518
4519 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4520 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4521 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004522 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523
4524 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4525 return -1;
4526
4527 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4528 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004529 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004530}
4531
4532
4533void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4534{
4535 struct wpa_ssid *ssid = wpa_s->current_ssid;
4536 const char *ssid_txt;
4537 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004538 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004539 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004540 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004541
Dmitry Shmidt04949592012-07-19 12:16:46 -07004542 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4543 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4544 wpa_s->parent, NULL);
4545 }
4546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004548 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004549
4550 wpa_s->show_group_started = 0;
4551
4552 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4553 os_memset(go_dev_addr, 0, ETH_ALEN);
4554 if (ssid->bssid_set)
4555 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4556 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4557 ssid->ssid_len);
4558 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4559
4560 if (wpa_s->global->p2p_group_formation == wpa_s)
4561 wpa_s->global->p2p_group_formation = NULL;
4562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004563 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4564 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565 if (ssid->passphrase == NULL && ssid->psk_set) {
4566 char psk[65];
4567 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4568 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4569 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4570 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004571 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 MAC2STR(go_dev_addr),
4573 persistent ? " [PERSISTENT]" : "");
4574 } else {
4575 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4576 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4577 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004578 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004579 ssid->passphrase ? ssid->passphrase : "",
4580 MAC2STR(go_dev_addr),
4581 persistent ? " [PERSISTENT]" : "");
4582 }
4583
4584 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004585 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4586 ssid, go_dev_addr);
4587 if (network_id < 0)
4588 network_id = ssid->id;
4589 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004590
4591done:
Jouni Malinendc7b7132012-09-14 12:53:47 -07004592 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004593 wpa_s->global->p2p != NULL) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07004594 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004595 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4596 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4597 "continued after successful connection");
4598 p2p_increase_search_delay(
4599 wpa_s->global->p2p,
4600 wpas_p2p_search_delay(wpa_s));
4601 }
4602 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004603}
4604
4605
4606int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4607 u32 interval1, u32 duration2, u32 interval2)
4608{
4609 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4610 return -1;
4611 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4612 return -1;
4613
4614 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4615 wpa_s->current_ssid == NULL ||
4616 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4617 return -1;
4618
4619 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4620 wpa_s->own_addr, wpa_s->assoc_freq,
4621 duration1, interval1, duration2, interval2);
4622}
4623
4624
4625int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4626 unsigned int interval)
4627{
4628 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4629 return -1;
4630
4631 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4632 return -1;
4633
4634 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4635}
4636
4637
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004638static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4639{
4640 return wpa_s->current_ssid != NULL &&
4641 wpa_s->current_ssid->p2p_group &&
4642 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4643}
4644
4645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4647{
4648 struct wpa_supplicant *wpa_s = eloop_ctx;
4649
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004650 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4652 "disabled");
4653 return;
4654 }
4655
Dmitry Shmidt04949592012-07-19 12:16:46 -07004656 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4657 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004658 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004659}
4660
4661
4662static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4663{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004664 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665
Dmitry Shmidt04949592012-07-19 12:16:46 -07004666 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4667 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4670 return;
4671
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004672 timeout = wpa_s->conf->p2p_group_idle;
4673 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4674 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4675 timeout = P2P_MAX_CLIENT_IDLE;
4676
4677 if (timeout == 0)
4678 return;
4679
Dmitry Shmidt04949592012-07-19 12:16:46 -07004680 if (timeout < 0) {
4681 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4682 timeout = 0; /* special client mode no-timeout */
4683 else
4684 return;
4685 }
4686
4687 if (wpa_s->p2p_in_provisioning) {
4688 /*
4689 * Use the normal group formation timeout during the
4690 * provisioning phase to avoid terminating this process too
4691 * early due to group idle timeout.
4692 */
4693 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4694 "during provisioning");
4695 return;
4696 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004697#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07004698 if (wpa_s->show_group_started) {
4699 /*
4700 * Use the normal group formation timeout between the end of
4701 * the provisioning phase and completion of 4-way handshake to
4702 * avoid terminating this process too early due to group idle
4703 * timeout.
4704 */
4705 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4706 "while waiting for initial 4-way handshake to "
4707 "complete");
4708 return;
4709 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004710#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07004711
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004712 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004713 timeout);
4714 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4715 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716}
4717
4718
Jouni Malinen2b89da82012-08-31 22:04:41 +03004719/* Returns 1 if the interface was removed */
4720int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4721 u16 reason_code, const u8 *ie, size_t ie_len,
4722 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723{
4724 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004725 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004727 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004728
Dmitry Shmidt04949592012-07-19 12:16:46 -07004729 if (!locally_generated)
4730 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4731 ie_len);
4732
4733 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4734 wpa_s->current_ssid &&
4735 wpa_s->current_ssid->p2p_group &&
4736 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4737 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4738 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03004739 if (wpas_p2p_group_delete(wpa_s,
4740 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4741 > 0)
4742 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004743 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03004744
4745 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004746}
4747
4748
4749void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004750 u16 reason_code, const u8 *ie, size_t ie_len,
4751 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004752{
4753 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4754 return;
4755 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4756 return;
4757
Dmitry Shmidt04949592012-07-19 12:16:46 -07004758 if (!locally_generated)
4759 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4760 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004761}
4762
4763
4764void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4765{
4766 struct p2p_data *p2p = wpa_s->global->p2p;
4767
4768 if (p2p == NULL)
4769 return;
4770
4771 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4772 return;
4773
4774 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4775 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4776
4777 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4778 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4779
4780 if (wpa_s->wps &&
4781 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4782 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4783
4784 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4785 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4786
4787 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4788 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4789 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4790 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4791 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4792 }
4793
4794 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4795 p2p_set_sec_dev_types(p2p,
4796 (void *) wpa_s->conf->sec_device_type,
4797 wpa_s->conf->num_sec_device_types);
4798
4799 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4800 int i;
4801 p2p_remove_wps_vendor_extensions(p2p);
4802 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4803 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4804 continue;
4805 p2p_add_wps_vendor_extension(
4806 p2p, wpa_s->conf->wps_vendor_ext[i]);
4807 }
4808 }
4809
4810 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4811 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4812 char country[3];
4813 country[0] = wpa_s->conf->country[0];
4814 country[1] = wpa_s->conf->country[1];
4815 country[2] = 0x04;
4816 p2p_set_country(p2p, country);
4817 }
4818
4819 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4820 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4821 wpa_s->conf->p2p_ssid_postfix ?
4822 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4823 0);
4824 }
4825
4826 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4827 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004828
4829 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4830 u8 reg_class, channel;
4831 int ret;
4832 unsigned int r;
4833 if (wpa_s->conf->p2p_listen_reg_class &&
4834 wpa_s->conf->p2p_listen_channel) {
4835 reg_class = wpa_s->conf->p2p_listen_reg_class;
4836 channel = wpa_s->conf->p2p_listen_channel;
4837 } else {
4838 reg_class = 81;
4839 /*
4840 * Pick one of the social channels randomly as the
4841 * listen channel.
4842 */
4843 os_get_random((u8 *) &r, sizeof(r));
4844 channel = 1 + (r % 3) * 5;
4845 }
4846 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4847 if (ret)
4848 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4849 "failed: %d", ret);
4850 }
4851 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4852 u8 op_reg_class, op_channel, cfg_op_channel;
4853 int ret = 0;
4854 unsigned int r;
4855 if (wpa_s->conf->p2p_oper_reg_class &&
4856 wpa_s->conf->p2p_oper_channel) {
4857 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4858 op_channel = wpa_s->conf->p2p_oper_channel;
4859 cfg_op_channel = 1;
4860 } else {
4861 op_reg_class = 81;
4862 /*
4863 * Use random operation channel from (1, 6, 11)
4864 *if no other preference is indicated.
4865 */
4866 os_get_random((u8 *) &r, sizeof(r));
4867 op_channel = 1 + (r % 3) * 5;
4868 cfg_op_channel = 0;
4869 }
4870 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4871 cfg_op_channel);
4872 if (ret)
4873 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4874 "failed: %d", ret);
4875 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004876
4877 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4878 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4879 wpa_s->conf->p2p_pref_chan) < 0) {
4880 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4881 "update failed");
4882 }
4883 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884}
4885
4886
4887int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4888 int duration)
4889{
4890 if (!wpa_s->ap_iface)
4891 return -1;
4892 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4893 duration);
4894}
4895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896
4897int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4898{
4899 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4900 return -1;
4901 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4902 return -1;
4903
4904 wpa_s->global->cross_connection = enabled;
4905 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4906
4907 if (!enabled) {
4908 struct wpa_supplicant *iface;
4909
4910 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4911 {
4912 if (iface->cross_connect_enabled == 0)
4913 continue;
4914
4915 iface->cross_connect_enabled = 0;
4916 iface->cross_connect_in_use = 0;
4917 wpa_msg(iface->parent, MSG_INFO,
4918 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4919 iface->ifname, iface->cross_connect_uplink);
4920 }
4921 }
4922
4923 return 0;
4924}
4925
4926
4927static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4928{
4929 struct wpa_supplicant *iface;
4930
4931 if (!uplink->global->cross_connection)
4932 return;
4933
4934 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4935 if (!iface->cross_connect_enabled)
4936 continue;
4937 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4938 0)
4939 continue;
4940 if (iface->ap_iface == NULL)
4941 continue;
4942 if (iface->cross_connect_in_use)
4943 continue;
4944
4945 iface->cross_connect_in_use = 1;
4946 wpa_msg(iface->parent, MSG_INFO,
4947 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4948 iface->ifname, iface->cross_connect_uplink);
4949 }
4950}
4951
4952
4953static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4954{
4955 struct wpa_supplicant *iface;
4956
4957 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4958 if (!iface->cross_connect_enabled)
4959 continue;
4960 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4961 0)
4962 continue;
4963 if (!iface->cross_connect_in_use)
4964 continue;
4965
4966 wpa_msg(iface->parent, MSG_INFO,
4967 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4968 iface->ifname, iface->cross_connect_uplink);
4969 iface->cross_connect_in_use = 0;
4970 }
4971}
4972
4973
4974void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4975{
4976 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4977 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4978 wpa_s->cross_connect_disallowed)
4979 wpas_p2p_disable_cross_connect(wpa_s);
4980 else
4981 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004982 if (!wpa_s->ap_iface &&
4983 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4984 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004985}
4986
4987
4988void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4989{
4990 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004991 if (!wpa_s->ap_iface &&
4992 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4993 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004994 wpas_p2p_set_group_idle_timeout(wpa_s);
4995}
4996
4997
4998static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4999{
5000 struct wpa_supplicant *iface;
5001
5002 if (!wpa_s->global->cross_connection)
5003 return;
5004
5005 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5006 if (iface == wpa_s)
5007 continue;
5008 if (iface->drv_flags &
5009 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5010 continue;
5011 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5012 continue;
5013
5014 wpa_s->cross_connect_enabled = 1;
5015 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5016 sizeof(wpa_s->cross_connect_uplink));
5017 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5018 "%s to %s whenever uplink is available",
5019 wpa_s->ifname, wpa_s->cross_connect_uplink);
5020
5021 if (iface->ap_iface || iface->current_ssid == NULL ||
5022 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5023 iface->cross_connect_disallowed ||
5024 iface->wpa_state != WPA_COMPLETED)
5025 break;
5026
5027 wpa_s->cross_connect_in_use = 1;
5028 wpa_msg(wpa_s->parent, MSG_INFO,
5029 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5030 wpa_s->ifname, wpa_s->cross_connect_uplink);
5031 break;
5032 }
5033}
5034
5035
5036int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5037{
5038 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5039 !wpa_s->p2p_in_provisioning)
5040 return 0; /* not P2P client operation */
5041
5042 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5043 "session overlap");
5044 if (wpa_s != wpa_s->parent)
5045 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5046
5047 if (wpa_s->global->p2p)
5048 p2p_group_formation_failed(wpa_s->global->p2p);
5049
5050 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5051 wpa_s->parent, NULL);
5052
5053 wpas_group_formation_completed(wpa_s, 0);
5054 return 1;
5055}
5056
5057
5058void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5059{
5060 struct p2p_channels chan;
5061
5062 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5063 return;
5064
5065 os_memset(&chan, 0, sizeof(chan));
5066 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5067 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5068 "channel list");
5069 return;
5070 }
5071
5072 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5073}
5074
5075
5076int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5077{
5078 struct wpa_global *global = wpa_s->global;
5079 int found = 0;
5080 const u8 *peer;
5081
5082 if (global->p2p == NULL)
5083 return -1;
5084
5085 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5086
5087 if (wpa_s->pending_interface_name[0] &&
5088 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5089 found = 1;
5090
5091 peer = p2p_get_go_neg_peer(global->p2p);
5092 if (peer) {
5093 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5094 MACSTR, MAC2STR(peer));
5095 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005096 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005097 }
5098
5099 wpas_p2p_stop_find(wpa_s);
5100
5101 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5102 if (wpa_s == global->p2p_group_formation &&
5103 (wpa_s->p2p_in_provisioning ||
5104 wpa_s->parent->pending_interface_type ==
5105 WPA_IF_P2P_CLIENT)) {
5106 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5107 "formation found - cancelling",
5108 wpa_s->ifname);
5109 found = 1;
5110 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5111 wpa_s->parent, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005112 wpas_p2p_group_delete(wpa_s,
5113 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114 break;
5115 }
5116 }
5117
5118 if (!found) {
5119 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5120 return -1;
5121 }
5122
5123 return 0;
5124}
5125
5126
5127void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5128{
5129 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5130 return;
5131
5132 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5133 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005134 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135}
5136
5137
5138void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5139 int freq_24, int freq_5, int freq_overall)
5140{
5141 struct p2p_data *p2p = wpa_s->global->p2p;
5142 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5143 return;
5144 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5145}
5146
5147
5148int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5149{
5150 u8 peer[ETH_ALEN];
5151 struct p2p_data *p2p = wpa_s->global->p2p;
5152
5153 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5154 return -1;
5155
5156 if (hwaddr_aton(addr, peer))
5157 return -1;
5158
5159 return p2p_unauthorize(p2p, peer);
5160}
5161
5162
5163/**
5164 * wpas_p2p_disconnect - Disconnect from a P2P Group
5165 * @wpa_s: Pointer to wpa_supplicant data
5166 * Returns: 0 on success, -1 on failure
5167 *
5168 * This can be used to disconnect from a group in which the local end is a P2P
5169 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5170 * virtual network interface was created for this group, that interface will be
5171 * removed. Otherwise, only the configured P2P group network will be removed
5172 * from the interface.
5173 */
5174int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5175{
5176
5177 if (wpa_s == NULL)
5178 return -1;
5179
Jouni Malinen2b89da82012-08-31 22:04:41 +03005180 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5181 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005182}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005183
5184
5185int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5186{
5187 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5188 return 0;
5189
5190 return p2p_in_progress(wpa_s->global->p2p);
5191}
5192
5193
5194void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5195 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005196{
5197 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5198 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5199 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005200 /**
5201 * Remove the network by scheduling the group formation
5202 * timeout to happen immediately. The teardown code
5203 * needs to be scheduled to run asynch later so that we
5204 * don't delete data from under ourselves unexpectedly.
5205 * Calling wpas_p2p_group_formation_timeout directly
5206 * causes a series of crashes in WPS failure scenarios.
5207 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005208 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5209 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005210 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5211 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005212 }
5213}
5214
5215
5216struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005217 const u8 *addr, const u8 *ssid,
5218 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005219{
5220 struct wpa_ssid *s;
5221 size_t i;
5222
5223 for (s = wpa_s->conf->ssid; s; s = s->next) {
5224 if (s->disabled != 2)
5225 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005226 if (ssid &&
5227 (ssid_len != s->ssid_len ||
5228 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5229 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005230 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5231 return s; /* peer is GO in the persistent group */
5232 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5233 continue;
5234 for (i = 0; i < s->num_p2p_clients; i++) {
5235 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5236 addr, ETH_ALEN) == 0)
5237 return s; /* peer is P2P client in persistent
5238 * group */
5239 }
5240 }
5241
5242 return NULL;
5243}
5244
5245
5246void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5247 const u8 *addr)
5248{
5249 if (addr == NULL)
5250 return;
5251 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5252}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005253
Dmitry Shmidt04949592012-07-19 12:16:46 -07005254
5255static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5256 int group_added)
5257{
5258 struct wpa_supplicant *group = wpa_s;
5259 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5260 if (wpa_s->global->p2p_group_formation)
5261 group = wpa_s->global->p2p_group_formation;
5262 wpa_s = wpa_s->parent;
5263 offchannel_send_action_done(wpa_s);
5264 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005265 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005266 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5267 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5268 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5269 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5270 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005271 wpa_s->p2p_pd_before_go_neg,
5272 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005273}
5274
5275
5276int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5277{
5278 if (!wpa_s->p2p_fallback_to_go_neg ||
5279 wpa_s->p2p_in_provisioning <= 5)
5280 return 0;
5281
5282 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5283 return 0; /* peer operating as a GO */
5284
5285 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5286 "fallback to GO Negotiation");
5287 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5288
5289 return 1;
5290}
5291
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005292
5293unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5294{
5295 const char *rn, *rn2;
5296 struct wpa_supplicant *ifs;
5297
5298 if (wpa_s->wpa_state > WPA_SCANNING) {
5299 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5300 "concurrent operation",
5301 P2P_CONCURRENT_SEARCH_DELAY);
5302 return P2P_CONCURRENT_SEARCH_DELAY;
5303 }
5304
5305 if (!wpa_s->driver->get_radio_name)
5306 return 0;
5307 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5308 if (rn == NULL || rn[0] == '\0')
5309 return 0;
5310
5311 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5312 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5313 continue;
5314
5315 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5316 if (!rn2 || os_strcmp(rn, rn2) != 0)
5317 continue;
5318 if (ifs->wpa_state > WPA_SCANNING) {
5319 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5320 "delay due to concurrent operation on "
5321 "interface %s",
5322 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5323 return P2P_CONCURRENT_SEARCH_DELAY;
5324 }
5325 }
5326
5327 return 0;
5328}
5329
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005330#ifdef ANDROID_P2P
5331int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
5332{
5333 struct wpa_supplicant *iface = NULL;
5334 struct p2p_data *p2p = wpa_s->global->p2p;
5335
5336 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5337 if((iface->p2p_group_interface) && (iface->current_ssid) &&
5338 (iface->current_ssid->frequency != freq)) {
5339
5340 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
5341 /* Try to see whether we can move the GO. If it
5342 * is not possible, remove the GO interface
5343 */
5344 if(wpa_drv_switch_channel(iface, freq) == 0) {
5345 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5346 iface->current_ssid->frequency = freq;
5347 continue;
5348 }
5349 }
5350
5351 /* If GO cannot be moved or if the conflicting interface is a
5352 * P2P Client, remove the interface depending up on the connection
5353 * priority */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005354 if(!wpas_is_p2p_prioritized(wpa_s)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005355 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005356 * P2P connection. So remove the interface */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005357 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5358 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005359 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005360 } else {
5361 /* Existing connection has the priority. Disable the newly
5362 * selected network and let the application know about it.
5363 */
5364 return -1;
5365 }
5366 }
5367 }
5368 return 0;
5369}
5370#endif