blob: c6856e28bbcd9690b62a2d28954749d80e5e51b2 [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 Shmidt8d520ff2011-05-09 14:06:53 -070092static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
93static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
94static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
95static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080096static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
97 void *timeout_ctx);
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 Shmidtd5e49232012-12-03 15:08:10 -0800100static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101
102
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700103static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
104 int freq)
105{
106 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
107 return;
108 if (freq > 0 &&
109 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
110 wpa_s->parent->conf->p2p_ignore_shared_freq)
111 freq = 0;
112 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
113}
114
115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
117 struct wpa_scan_results *scan_res)
118{
119 size_t i;
120
121 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
122 return;
123
124 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
125 (int) scan_res->num);
126
127 for (i = 0; i < scan_res->num; i++) {
128 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800129 struct os_time time_tmp_age, entry_ts;
130 time_tmp_age.sec = bss->age / 1000;
131 time_tmp_age.usec = (bss->age % 1000) * 1000;
132 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800134 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135 (const u8 *) (bss + 1),
136 bss->ie_len) > 0)
137 break;
138 }
139
140 p2p_scan_res_handled(wpa_s->global->p2p);
141}
142
143
144static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
145 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700146 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700147{
148 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700149 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150 struct wpa_driver_scan_params params;
151 int ret;
152 struct wpabuf *wps_ie, *ies;
153 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800154 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155
156 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
157 return -1;
158
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700159 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
160 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700161 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700162 wpas_p2p_in_progress(wpa_s) == 2) {
163 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
164 "pending station mode scan to be "
165 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700166 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700167 wpa_supplicant_req_scan(ifs, 0, 0);
168 return 1;
169 }
170 }
171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172 os_memset(&params, 0, sizeof(params));
173
174 /* P2P Wildcard SSID */
175 params.num_ssids = 1;
176 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
177 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
178
179 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700180 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
181 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 num_req_dev_types, req_dev_types);
183 if (wps_ie == NULL)
184 return -1;
185
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800186 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
187 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188 if (ies == NULL) {
189 wpabuf_free(wps_ie);
190 return -1;
191 }
192 wpabuf_put_buf(ies, wps_ie);
193 wpabuf_free(wps_ie);
194
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800195 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800197 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 params.extra_ies = wpabuf_head(ies);
199 params.extra_ies_len = wpabuf_len(ies);
200
201 switch (type) {
202 case P2P_SCAN_SOCIAL:
203 params.freqs = social_channels;
204 break;
205 case P2P_SCAN_FULL:
206 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700207 case P2P_SCAN_SPECIFIC:
208 social_channels[0] = freq;
209 social_channels[1] = 0;
210 params.freqs = social_channels;
211 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 case P2P_SCAN_SOCIAL_PLUS_ONE:
213 social_channels[3] = freq;
214 params.freqs = social_channels;
215 break;
216 }
217
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800218 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219
220 wpabuf_free(ies);
221
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800222 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700223 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
224 if (ifs->scanning ||
225 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
226 wpa_s->global->p2p_cb_on_scan_complete = 1;
227 ret = 1;
228 break;
229 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700231 } else
232 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 return ret;
235}
236
237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
239{
240 switch (p2p_group_interface) {
241 case P2P_GROUP_INTERFACE_PENDING:
242 return WPA_IF_P2P_GROUP;
243 case P2P_GROUP_INTERFACE_GO:
244 return WPA_IF_P2P_GO;
245 case P2P_GROUP_INTERFACE_CLIENT:
246 return WPA_IF_P2P_CLIENT;
247 }
248
249 return WPA_IF_P2P_GROUP;
250}
251
252
253static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
254 const u8 *ssid,
255 size_t ssid_len, int *go)
256{
257 struct wpa_ssid *s;
258
259 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
260 for (s = wpa_s->conf->ssid; s; s = s->next) {
261 if (s->disabled != 0 || !s->p2p_group ||
262 s->ssid_len != ssid_len ||
263 os_memcmp(ssid, s->ssid, ssid_len) != 0)
264 continue;
265 if (s->mode == WPAS_MODE_P2P_GO &&
266 s != wpa_s->current_ssid)
267 continue;
268 if (go)
269 *go = s->mode == WPAS_MODE_P2P_GO;
270 return wpa_s;
271 }
272 }
273
274 return NULL;
275}
276
277
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700278static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
279 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280{
281 struct wpa_ssid *ssid;
282 char *gtype;
283 const char *reason;
284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 ssid = wpa_s->current_ssid;
286 if (ssid == NULL) {
287 /*
288 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700289 * pending P2P group interface waiting for provisioning or a
290 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 */
292 ssid = wpa_s->conf->ssid;
293 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700294 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 break;
296 ssid = ssid->next;
297 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300298 if (ssid == NULL &&
299 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
300 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700301 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
302 "not found");
303 return -1;
304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305 }
306 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
307 gtype = "GO";
308 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
309 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
310 wpa_s->reassociate = 0;
311 wpa_s->disconnected = 1;
312 wpa_supplicant_deauthenticate(wpa_s,
313 WLAN_REASON_DEAUTH_LEAVING);
314 gtype = "client";
315 } else
316 gtype = "GO";
317 if (wpa_s->cross_connect_in_use) {
318 wpa_s->cross_connect_in_use = 0;
319 wpa_msg(wpa_s->parent, MSG_INFO,
320 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
321 wpa_s->ifname, wpa_s->cross_connect_uplink);
322 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700323 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 case P2P_GROUP_REMOVAL_REQUESTED:
325 reason = " reason=REQUESTED";
326 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700327 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
328 reason = " reason=FORMATION_FAILED";
329 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
331 reason = " reason=IDLE";
332 break;
333 case P2P_GROUP_REMOVAL_UNAVAILABLE:
334 reason = " reason=UNAVAILABLE";
335 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700336 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
337 reason = " reason=GO_ENDING_SESSION";
338 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700339#ifdef ANDROID_P2P
340 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
341 reason = " reason=FREQ_CONFLICT";
342 break;
343#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344 default:
345 reason = "";
346 break;
347 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700348 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700349 wpa_msg(wpa_s->parent, MSG_INFO,
350 P2P_EVENT_GROUP_REMOVED "%s %s%s",
351 wpa_s->ifname, gtype, reason);
352 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353
Dmitry Shmidt04949592012-07-19 12:16:46 -0700354 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
355 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800356 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700357 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800358 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
359 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700360 wpa_s->p2p_in_provisioning = 0;
361 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700362
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700363 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
365
366 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
367 struct wpa_global *global;
368 char *ifname;
369 enum wpa_driver_if_type type;
370 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
371 wpa_s->ifname);
372 global = wpa_s->global;
373 ifname = os_strdup(wpa_s->ifname);
374 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700375 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 wpa_s = global->ifaces;
377 if (wpa_s && ifname)
378 wpa_drv_if_remove(wpa_s, type, ifname);
379 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300380 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 }
382
383 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
384 if (ssid && (ssid->p2p_group ||
385 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
386 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
387 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700388 if (ssid == wpa_s->current_ssid) {
389 wpa_sm_set_config(wpa_s->wpa, NULL);
390 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700392 }
393 /*
394 * Networks objects created during any P2P activities are not
395 * exposed out as they might/will confuse certain non-P2P aware
396 * applications since these network objects won't behave like
397 * regular ones.
398 *
399 * Likewise, we don't send out network removed signals for such
400 * network objects.
401 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402 wpa_config_remove_network(wpa_s->conf, id);
403 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800404 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700405 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406 } else {
407 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
408 "found");
409 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700410 if (wpa_s->ap_iface)
411 wpa_supplicant_ap_deinit(wpa_s);
412 else
413 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700414
415 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416}
417
418
419static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
420 u8 *go_dev_addr,
421 const u8 *ssid, size_t ssid_len)
422{
423 struct wpa_bss *bss;
424 const u8 *bssid;
425 struct wpabuf *p2p;
426 u8 group_capab;
427 const u8 *addr;
428
429 if (wpa_s->go_params)
430 bssid = wpa_s->go_params->peer_interface_addr;
431 else
432 bssid = wpa_s->bssid;
433
434 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
435 if (bss == NULL) {
436 u8 iface_addr[ETH_ALEN];
437 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
438 iface_addr) == 0)
439 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
440 }
441 if (bss == NULL) {
442 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
443 "group is persistent - BSS " MACSTR " not found",
444 MAC2STR(bssid));
445 return 0;
446 }
447
448 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
449 if (p2p == NULL) {
450 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
451 "group is persistent - BSS " MACSTR
452 " did not include P2P IE", MAC2STR(bssid));
453 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
454 (u8 *) (bss + 1), bss->ie_len);
455 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
456 ((u8 *) bss + 1) + bss->ie_len,
457 bss->beacon_ie_len);
458 return 0;
459 }
460
461 group_capab = p2p_get_group_capab(p2p);
462 addr = p2p_get_go_dev_addr(p2p);
463 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
464 "group_capab=0x%x", group_capab);
465 if (addr) {
466 os_memcpy(go_dev_addr, addr, ETH_ALEN);
467 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
468 MAC2STR(addr));
469 } else
470 os_memset(go_dev_addr, 0, ETH_ALEN);
471 wpabuf_free(p2p);
472
473 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
474 "go_dev_addr=" MACSTR,
475 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
476
477 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
478}
479
480
Jouni Malinen75ecf522011-06-27 15:19:46 -0700481static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
482 struct wpa_ssid *ssid,
483 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484{
485 struct wpa_ssid *s;
486 int changed = 0;
487
488 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
489 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
490 for (s = wpa_s->conf->ssid; s; s = s->next) {
491 if (s->disabled == 2 &&
492 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
493 s->ssid_len == ssid->ssid_len &&
494 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
495 break;
496 }
497
498 if (s) {
499 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
500 "entry");
501 if (ssid->passphrase && !s->passphrase)
502 changed = 1;
503 else if (ssid->passphrase && s->passphrase &&
504 os_strcmp(ssid->passphrase, s->passphrase) != 0)
505 changed = 1;
506 } else {
507 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
508 "entry");
509 changed = 1;
510 s = wpa_config_add_network(wpa_s->conf);
511 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700512 return -1;
513
514 /*
515 * Instead of network_added we emit persistent_group_added
516 * notification. Also to keep the defense checks in
517 * persistent_group obj registration method, we set the
518 * relevant flags in s to designate it as a persistent group.
519 */
520 s->p2p_group = 1;
521 s->p2p_persistent_group = 1;
522 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 wpa_config_set_network_defaults(s);
524 }
525
526 s->p2p_group = 1;
527 s->p2p_persistent_group = 1;
528 s->disabled = 2;
529 s->bssid_set = 1;
530 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
531 s->mode = ssid->mode;
532 s->auth_alg = WPA_AUTH_ALG_OPEN;
533 s->key_mgmt = WPA_KEY_MGMT_PSK;
534 s->proto = WPA_PROTO_RSN;
535 s->pairwise_cipher = WPA_CIPHER_CCMP;
536 s->export_keys = 1;
537 if (ssid->passphrase) {
538 os_free(s->passphrase);
539 s->passphrase = os_strdup(ssid->passphrase);
540 }
541 if (ssid->psk_set) {
542 s->psk_set = 1;
543 os_memcpy(s->psk, ssid->psk, 32);
544 }
545 if (s->passphrase && !s->psk_set)
546 wpa_config_update_psk(s);
547 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
548 os_free(s->ssid);
549 s->ssid = os_malloc(ssid->ssid_len);
550 }
551 if (s->ssid) {
552 s->ssid_len = ssid->ssid_len;
553 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
554 }
555
556#ifndef CONFIG_NO_CONFIG_WRITE
557 if (changed && wpa_s->conf->update_config &&
558 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
559 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
560 }
561#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700562
563 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564}
565
566
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800567static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
568 const u8 *addr)
569{
570 struct wpa_ssid *ssid, *s;
571 u8 *n;
572 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700573 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800574
575 ssid = wpa_s->current_ssid;
576 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
577 !ssid->p2p_persistent_group)
578 return;
579
580 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
581 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
582 continue;
583
584 if (s->ssid_len == ssid->ssid_len &&
585 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
586 break;
587 }
588
589 if (s == NULL)
590 return;
591
592 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
593 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700594 ETH_ALEN) != 0)
595 continue;
596
597 if (i == s->num_p2p_clients - 1)
598 return; /* already the most recent entry */
599
600 /* move the entry to mark it most recent */
601 os_memmove(s->p2p_client_list + i * ETH_ALEN,
602 s->p2p_client_list + (i + 1) * ETH_ALEN,
603 (s->num_p2p_clients - i - 1) * ETH_ALEN);
604 os_memcpy(s->p2p_client_list +
605 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
606 found = 1;
607 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800608 }
609
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700610 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
611 n = os_realloc_array(s->p2p_client_list,
612 s->num_p2p_clients + 1, ETH_ALEN);
613 if (n == NULL)
614 return;
615 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
616 s->p2p_client_list = n;
617 s->num_p2p_clients++;
618 } else if (!found) {
619 /* Not enough room for an additional entry - drop the oldest
620 * entry */
621 os_memmove(s->p2p_client_list,
622 s->p2p_client_list + ETH_ALEN,
623 (s->num_p2p_clients - 1) * ETH_ALEN);
624 os_memcpy(s->p2p_client_list +
625 (s->num_p2p_clients - 1) * ETH_ALEN,
626 addr, ETH_ALEN);
627 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800628
629#ifndef CONFIG_NO_CONFIG_WRITE
630 if (wpa_s->parent->conf->update_config &&
631 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
632 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
633#endif /* CONFIG_NO_CONFIG_WRITE */
634}
635
636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
638 int success)
639{
640 struct wpa_ssid *ssid;
641 const char *ssid_txt;
642 int client;
643 int persistent;
644 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700645 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646
647 /*
648 * This callback is likely called for the main interface. Update wpa_s
649 * to use the group interface if a new interface was created for the
650 * group.
651 */
652 if (wpa_s->global->p2p_group_formation)
653 wpa_s = wpa_s->global->p2p_group_formation;
654 wpa_s->global->p2p_group_formation = NULL;
655 wpa_s->p2p_in_provisioning = 0;
656
657 if (!success) {
658 wpa_msg(wpa_s->parent, MSG_INFO,
659 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700660 wpas_p2p_group_delete(wpa_s,
661 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662 return;
663 }
664
665 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
666
667 ssid = wpa_s->current_ssid;
668 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
669 ssid->mode = WPAS_MODE_P2P_GO;
670 p2p_group_notif_formation_done(wpa_s->p2p_group);
671 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
672 }
673
674 persistent = 0;
675 if (ssid) {
676 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
677 client = ssid->mode == WPAS_MODE_INFRA;
678 if (ssid->mode == WPAS_MODE_P2P_GO) {
679 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700680 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
681 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 } else
683 persistent = wpas_p2p_persistent_group(wpa_s,
684 go_dev_addr,
685 ssid->ssid,
686 ssid->ssid_len);
687 } else {
688 ssid_txt = "";
689 client = wpa_s->p2p_group_interface ==
690 P2P_GROUP_INTERFACE_CLIENT;
691 os_memset(go_dev_addr, 0, ETH_ALEN);
692 }
693
694 wpa_s->show_group_started = 0;
695 if (client) {
696 /*
697 * Indicate event only after successfully completed 4-way
698 * handshake, i.e., when the interface is ready for data
699 * packets.
700 */
701 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700702#ifdef ANDROID_P2P
703 /* For client Second phase of Group formation (4-way handshake) can be still pending
704 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700705 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 -0700706 wpa_s->global->p2p_group_formation = wpa_s;
707#endif
708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
710 char psk[65];
711 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
712 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
713 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
714 "%s",
715 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
716 MAC2STR(go_dev_addr),
717 persistent ? " [PERSISTENT]" : "");
718 wpas_p2p_cross_connect_setup(wpa_s);
719 wpas_p2p_set_group_idle_timeout(wpa_s);
720 } else {
721 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
722 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
723 "go_dev_addr=" MACSTR "%s",
724 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
725 ssid && ssid->passphrase ? ssid->passphrase : "",
726 MAC2STR(go_dev_addr),
727 persistent ? " [PERSISTENT]" : "");
728 wpas_p2p_cross_connect_setup(wpa_s);
729 wpas_p2p_set_group_idle_timeout(wpa_s);
730 }
731
732 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700733 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
734 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800735 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700736 network_id = ssid->id;
737 if (!client)
738 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739}
740
741
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800742static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
743 unsigned int freq,
744 const u8 *dst, const u8 *src,
745 const u8 *bssid,
746 const u8 *data, size_t data_len,
747 enum offchannel_send_action_result
748 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800750 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
753 return;
754 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
755 return;
756
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800757 switch (result) {
758 case OFFCHANNEL_SEND_ACTION_SUCCESS:
759 res = P2P_SEND_ACTION_SUCCESS;
760 break;
761 case OFFCHANNEL_SEND_ACTION_NO_ACK:
762 res = P2P_SEND_ACTION_NO_ACK;
763 break;
764 case OFFCHANNEL_SEND_ACTION_FAILED:
765 res = P2P_SEND_ACTION_FAILED;
766 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 }
768
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800769 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800771 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
772 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800773 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800774 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
775 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800777 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
778 "during p2p_connect-auto");
779 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
780 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700781 }
782}
783
784
785static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
786 const u8 *src, const u8 *bssid, const u8 *buf,
787 size_t len, unsigned int wait_time)
788{
789 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800790 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
791 wait_time,
792 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793}
794
795
796static void wpas_send_action_done(void *ctx)
797{
798 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800799 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800}
801
802
803static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
804 struct p2p_go_neg_results *params)
805{
806 if (wpa_s->go_params == NULL) {
807 wpa_s->go_params = os_malloc(sizeof(*params));
808 if (wpa_s->go_params == NULL)
809 return -1;
810 }
811 os_memcpy(wpa_s->go_params, params, sizeof(*params));
812 return 0;
813}
814
815
816static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
817 struct p2p_go_neg_results *res)
818{
819 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
820 MAC2STR(res->peer_interface_addr));
821 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
822 res->ssid, res->ssid_len);
823 wpa_supplicant_ap_deinit(wpa_s);
824 wpas_copy_go_neg_results(wpa_s, res);
825 if (res->wps_method == WPS_PBC)
826 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
827 else {
828 u16 dev_pw_id = DEV_PW_DEFAULT;
829 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
830 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
831 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
832 wpa_s->p2p_pin, 1, dev_pw_id);
833 }
834}
835
836
837static void p2p_go_configured(void *ctx, void *data)
838{
839 struct wpa_supplicant *wpa_s = ctx;
840 struct p2p_go_neg_results *params = data;
841 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700842 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700843
844 ssid = wpa_s->current_ssid;
845 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
846 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
847 if (wpa_s->global->p2p_group_formation == wpa_s)
848 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800849 if (os_strlen(params->passphrase) > 0) {
850 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
851 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
852 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
853 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
854 ssid->frequency, params->passphrase,
855 MAC2STR(wpa_s->global->p2p_dev_addr),
856 params->persistent_group ? " [PERSISTENT]" :
857 "");
858 } else {
859 char psk[65];
860 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
861 sizeof(params->psk));
862 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
863 "%s GO ssid=\"%s\" freq=%d psk=%s "
864 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
865 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
866 ssid->frequency, psk,
867 MAC2STR(wpa_s->global->p2p_dev_addr),
868 params->persistent_group ? " [PERSISTENT]" :
869 "");
870 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700873 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700875 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700876 if (network_id < 0)
877 network_id = ssid->id;
878 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879 wpas_p2p_cross_connect_setup(wpa_s);
880 wpas_p2p_set_group_idle_timeout(wpa_s);
881 return;
882 }
883
884 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
885 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
886 params->peer_interface_addr)) {
887 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
888 "filtering");
889 return;
890 }
891 if (params->wps_method == WPS_PBC)
892 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800893 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 else if (wpa_s->p2p_pin[0])
895 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800896 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 os_free(wpa_s->go_params);
898 wpa_s->go_params = NULL;
899}
900
901
902static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
903 struct p2p_go_neg_results *params,
904 int group_formation)
905{
906 struct wpa_ssid *ssid;
907
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700908 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
909 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
910 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
911 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700913 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914
915 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700916 if (ssid == NULL) {
917 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700919 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800921 wpa_s->show_group_started = 0;
922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 wpa_config_set_network_defaults(ssid);
924 ssid->temporary = 1;
925 ssid->p2p_group = 1;
926 ssid->p2p_persistent_group = params->persistent_group;
927 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
928 WPAS_MODE_P2P_GO;
929 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700930 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 ssid->ssid = os_zalloc(params->ssid_len + 1);
932 if (ssid->ssid) {
933 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
934 ssid->ssid_len = params->ssid_len;
935 }
936 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
937 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
938 ssid->proto = WPA_PROTO_RSN;
939 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800940 if (os_strlen(params->passphrase) > 0) {
941 ssid->passphrase = os_strdup(params->passphrase);
942 if (ssid->passphrase == NULL) {
943 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
944 "passphrase for GO");
945 wpa_config_remove_network(wpa_s->conf, ssid->id);
946 return;
947 }
948 } else
949 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800950 ssid->psk_set = params->psk_set;
951 if (ssid->psk_set)
952 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800953 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800954 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700955 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956
957 wpa_s->ap_configured_cb = p2p_go_configured;
958 wpa_s->ap_configured_cb_ctx = wpa_s;
959 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700960 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 wpa_s->reassociate = 1;
962 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700963 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
964 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 wpa_supplicant_req_scan(wpa_s, 0, 0);
966}
967
968
969static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
970 const struct wpa_supplicant *src)
971{
972 struct wpa_config *d;
973 const struct wpa_config *s;
974
975 d = dst->conf;
976 s = src->conf;
977
978#define C(n) if (s->n) d->n = os_strdup(s->n)
979 C(device_name);
980 C(manufacturer);
981 C(model_name);
982 C(model_number);
983 C(serial_number);
984 C(config_methods);
985#undef C
986
987 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
988 os_memcpy(d->sec_device_type, s->sec_device_type,
989 sizeof(d->sec_device_type));
990 d->num_sec_device_types = s->num_sec_device_types;
991
992 d->p2p_group_idle = s->p2p_group_idle;
993 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800994 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700995 d->max_num_sta = s->max_num_sta;
996 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997}
998
999
1000static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1001 enum wpa_driver_if_type type)
1002{
1003 char ifname[120], force_ifname[120];
1004
1005 if (wpa_s->pending_interface_name[0]) {
1006 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1007 "- skip creation of a new one");
1008 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1009 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1010 "unknown?! ifname='%s'",
1011 wpa_s->pending_interface_name);
1012 return -1;
1013 }
1014 return 0;
1015 }
1016
1017 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
1018 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001019 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1021 /* Try to avoid going over the IFNAMSIZ length limit */
1022 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1023 wpa_s->p2p_group_idx);
1024 }
1025 force_ifname[0] = '\0';
1026
1027 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1028 ifname);
1029 wpa_s->p2p_group_idx++;
1030
1031 wpa_s->pending_interface_type = type;
1032 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1033 wpa_s->pending_interface_addr, NULL) < 0) {
1034 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1035 "interface");
1036 return -1;
1037 }
1038
1039 if (force_ifname[0]) {
1040 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1041 force_ifname);
1042 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1043 sizeof(wpa_s->pending_interface_name));
1044 } else
1045 os_strlcpy(wpa_s->pending_interface_name, ifname,
1046 sizeof(wpa_s->pending_interface_name));
1047 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1048 MACSTR, wpa_s->pending_interface_name,
1049 MAC2STR(wpa_s->pending_interface_addr));
1050
1051 return 0;
1052}
1053
1054
1055static void wpas_p2p_remove_pending_group_interface(
1056 struct wpa_supplicant *wpa_s)
1057{
1058 if (!wpa_s->pending_interface_name[0] ||
1059 is_zero_ether_addr(wpa_s->pending_interface_addr))
1060 return; /* No pending virtual interface */
1061
1062 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1063 wpa_s->pending_interface_name);
1064 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1065 wpa_s->pending_interface_name);
1066 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1067 wpa_s->pending_interface_name[0] = '\0';
1068}
1069
1070
1071static struct wpa_supplicant *
1072wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1073{
1074 struct wpa_interface iface;
1075 struct wpa_supplicant *group_wpa_s;
1076
1077 if (!wpa_s->pending_interface_name[0]) {
1078 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1079 if (!wpas_p2p_create_iface(wpa_s))
1080 return NULL;
1081 /*
1082 * Something has forced us to remove the pending interface; try
1083 * to create a new one and hope for the best that we will get
1084 * the same local address.
1085 */
1086 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1087 WPA_IF_P2P_CLIENT) < 0)
1088 return NULL;
1089 }
1090
1091 os_memset(&iface, 0, sizeof(iface));
1092 iface.ifname = wpa_s->pending_interface_name;
1093 iface.driver = wpa_s->driver->name;
1094 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1095 iface.driver_param = wpa_s->conf->driver_param;
1096 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1097 if (group_wpa_s == NULL) {
1098 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1099 "wpa_supplicant interface");
1100 return NULL;
1101 }
1102 wpa_s->pending_interface_name[0] = '\0';
1103 group_wpa_s->parent = wpa_s;
1104 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1105 P2P_GROUP_INTERFACE_CLIENT;
1106 wpa_s->global->p2p_group_formation = group_wpa_s;
1107
1108 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1109
1110 return group_wpa_s;
1111}
1112
1113
1114static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1115 void *timeout_ctx)
1116{
1117 struct wpa_supplicant *wpa_s = eloop_ctx;
1118 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1119 if (wpa_s->global->p2p)
1120 p2p_group_formation_failed(wpa_s->global->p2p);
1121 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1122 wpa_drv_p2p_group_formation_failed(wpa_s);
1123 wpas_group_formation_completed(wpa_s, 0);
1124}
1125
1126
1127void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1128{
1129 struct wpa_supplicant *wpa_s = ctx;
1130
1131 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1132 wpa_drv_cancel_remain_on_channel(wpa_s);
1133 wpa_s->off_channel_freq = 0;
1134 wpa_s->roc_waiting_drv_freq = 0;
1135 }
1136
1137 if (res->status) {
1138 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1139 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001140 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 wpas_p2p_remove_pending_group_interface(wpa_s);
1142 return;
1143 }
1144
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001145 if (wpa_s->p2p_go_ht40)
1146 res->ht40 = 1;
1147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001149 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150
Dmitry Shmidt04949592012-07-19 12:16:46 -07001151 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1152 struct wpa_ssid *ssid;
1153 ssid = wpa_config_get_network(wpa_s->conf,
1154 wpa_s->p2p_persistent_id);
1155 if (ssid && ssid->disabled == 2 &&
1156 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1157 size_t len = os_strlen(ssid->passphrase);
1158 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1159 "on requested persistent group");
1160 os_memcpy(res->passphrase, ssid->passphrase, len);
1161 res->passphrase[len] = '\0';
1162 }
1163 }
1164
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 if (wpa_s->create_p2p_iface) {
1166 struct wpa_supplicant *group_wpa_s =
1167 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1168 if (group_wpa_s == NULL) {
1169 wpas_p2p_remove_pending_group_interface(wpa_s);
1170 return;
1171 }
1172 if (group_wpa_s != wpa_s) {
1173 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1174 sizeof(group_wpa_s->p2p_pin));
1175 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1176 }
1177 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1178 wpa_s->pending_interface_name[0] = '\0';
1179 group_wpa_s->p2p_in_provisioning = 1;
1180
1181 if (res->role_go)
1182 wpas_start_wps_go(group_wpa_s, res, 1);
1183 else
1184 wpas_start_wps_enrollee(group_wpa_s, res);
1185 } else {
1186 wpa_s->p2p_in_provisioning = 1;
1187 wpa_s->global->p2p_group_formation = wpa_s;
1188
1189 if (res->role_go)
1190 wpas_start_wps_go(wpa_s, res, 1);
1191 else
1192 wpas_start_wps_enrollee(ctx, res);
1193 }
1194
1195 wpa_s->p2p_long_listen = 0;
1196 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1197
1198 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1199 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1200 (res->peer_config_timeout % 100) * 10000,
1201 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1202}
1203
1204
1205void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1206{
1207 struct wpa_supplicant *wpa_s = ctx;
1208 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1209 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1210
1211 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1212}
1213
1214
1215void wpas_dev_found(void *ctx, const u8 *addr,
1216 const struct p2p_peer_info *info,
1217 int new_device)
1218{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001219#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 struct wpa_supplicant *wpa_s = ctx;
1221 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001222#define WFD_DEV_INFO_SIZE 9
1223 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001224 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001225#ifdef CONFIG_WIFI_DISPLAY
1226 if (info->wfd_subelems) {
1227 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1228 wpabuf_head(info->wfd_subelems),
1229 WFD_DEV_INFO_SIZE);
1230 }
1231#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1233 " p2p_dev_addr=" MACSTR
1234 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001235 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1237 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1238 sizeof(devtype)),
1239 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001240 info->dev_capab, info->group_capab,
1241 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001242#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243
1244 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1245}
1246
1247
1248static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1249{
1250 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001251
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001252 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1253 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1256}
1257
1258
1259static int wpas_start_listen(void *ctx, unsigned int freq,
1260 unsigned int duration,
1261 const struct wpabuf *probe_resp_ie)
1262{
1263 struct wpa_supplicant *wpa_s = ctx;
1264
1265 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1266
1267 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1268 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1269 "report received Probe Request frames");
1270 return -1;
1271 }
1272
1273 wpa_s->pending_listen_freq = freq;
1274 wpa_s->pending_listen_duration = duration;
1275
1276 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1277 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1278 "to remain on channel (%u MHz) for Listen "
1279 "state", freq);
1280 wpa_s->pending_listen_freq = 0;
1281 return -1;
1282 }
1283 wpa_s->off_channel_freq = 0;
1284 wpa_s->roc_waiting_drv_freq = freq;
1285
1286 return 0;
1287}
1288
1289
1290static void wpas_stop_listen(void *ctx)
1291{
1292 struct wpa_supplicant *wpa_s = ctx;
1293 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1294 wpa_drv_cancel_remain_on_channel(wpa_s);
1295 wpa_s->off_channel_freq = 0;
1296 wpa_s->roc_waiting_drv_freq = 0;
1297 }
1298 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1299 wpa_drv_probe_req_report(wpa_s, 0);
1300}
1301
1302
1303static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1304{
1305 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001306 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307}
1308
1309
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001310/*
1311 * DNS Header section is used only to calculate compression pointers, so the
1312 * contents of this data does not matter, but the length needs to be reserved
1313 * in the virtual packet.
1314 */
1315#define DNS_HEADER_LEN 12
1316
1317/*
1318 * 27-octet in-memory packet from P2P specification containing two implied
1319 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1320 */
1321#define P2P_SD_IN_MEMORY_LEN 27
1322
1323static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1324 u8 **spos, const u8 *end)
1325{
1326 while (*spos < end) {
1327 u8 val = ((*spos)[0] & 0xc0) >> 6;
1328 int len;
1329
1330 if (val == 1 || val == 2) {
1331 /* These are reserved values in RFC 1035 */
1332 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1333 "sequence starting with 0x%x", val);
1334 return -1;
1335 }
1336
1337 if (val == 3) {
1338 u16 offset;
1339 u8 *spos_tmp;
1340
1341 /* Offset */
1342 if (*spos + 2 > end) {
1343 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1344 "DNS offset field");
1345 return -1;
1346 }
1347
1348 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1349 if (offset >= *spos - start) {
1350 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1351 "pointer offset %u", offset);
1352 return -1;
1353 }
1354
1355 (*spos) += 2;
1356 spos_tmp = start + offset;
1357 return p2p_sd_dns_uncompress_label(upos, uend, start,
1358 &spos_tmp,
1359 *spos - 2);
1360 }
1361
1362 /* Label */
1363 len = (*spos)[0] & 0x3f;
1364 if (len == 0)
1365 return 0;
1366
1367 (*spos)++;
1368 if (*spos + len > end) {
1369 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1370 "sequence - no room for label with length "
1371 "%u", len);
1372 return -1;
1373 }
1374
1375 if (*upos + len + 2 > uend)
1376 return -2;
1377
1378 os_memcpy(*upos, *spos, len);
1379 *spos += len;
1380 *upos += len;
1381 (*upos)[0] = '.';
1382 (*upos)++;
1383 (*upos)[0] = '\0';
1384 }
1385
1386 return 0;
1387}
1388
1389
1390/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1391 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1392 * not large enough */
1393static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1394 size_t msg_len, size_t offset)
1395{
1396 /* 27-octet in-memory packet from P2P specification */
1397 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1398 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1399 u8 *tmp, *end, *spos;
1400 char *upos, *uend;
1401 int ret = 0;
1402
1403 if (buf_len < 2)
1404 return -1;
1405 if (offset > msg_len)
1406 return -1;
1407
1408 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1409 if (tmp == NULL)
1410 return -1;
1411 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1412 end = spos + msg_len;
1413 spos += offset;
1414
1415 os_memset(tmp, 0, DNS_HEADER_LEN);
1416 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1417 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1418
1419 upos = buf;
1420 uend = buf + buf_len;
1421
1422 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1423 if (ret) {
1424 os_free(tmp);
1425 return ret;
1426 }
1427
1428 if (upos == buf) {
1429 upos[0] = '.';
1430 upos[1] = '\0';
1431 } else if (upos[-1] == '.')
1432 upos[-1] = '\0';
1433
1434 os_free(tmp);
1435 return 0;
1436}
1437
1438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001439static struct p2p_srv_bonjour *
1440wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1441 const struct wpabuf *query)
1442{
1443 struct p2p_srv_bonjour *bsrv;
1444 size_t len;
1445
1446 len = wpabuf_len(query);
1447 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1448 struct p2p_srv_bonjour, list) {
1449 if (len == wpabuf_len(bsrv->query) &&
1450 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1451 len) == 0)
1452 return bsrv;
1453 }
1454 return NULL;
1455}
1456
1457
1458static struct p2p_srv_upnp *
1459wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1460 const char *service)
1461{
1462 struct p2p_srv_upnp *usrv;
1463
1464 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1465 struct p2p_srv_upnp, list) {
1466 if (version == usrv->version &&
1467 os_strcmp(service, usrv->service) == 0)
1468 return usrv;
1469 }
1470 return NULL;
1471}
1472
1473
1474static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1475 u8 srv_trans_id)
1476{
1477 u8 *len_pos;
1478
1479 if (wpabuf_tailroom(resp) < 5)
1480 return;
1481
1482 /* Length (to be filled) */
1483 len_pos = wpabuf_put(resp, 2);
1484 wpabuf_put_u8(resp, srv_proto);
1485 wpabuf_put_u8(resp, srv_trans_id);
1486 /* Status Code */
1487 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1488 /* Response Data: empty */
1489 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1490}
1491
1492
1493static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1494 struct wpabuf *resp, u8 srv_trans_id)
1495{
1496 struct p2p_srv_bonjour *bsrv;
1497 u8 *len_pos;
1498
1499 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1500
1501 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1502 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1503 return;
1504 }
1505
1506 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1507 struct p2p_srv_bonjour, list) {
1508 if (wpabuf_tailroom(resp) <
1509 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1510 return;
1511 /* Length (to be filled) */
1512 len_pos = wpabuf_put(resp, 2);
1513 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1514 wpabuf_put_u8(resp, srv_trans_id);
1515 /* Status Code */
1516 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1517 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1518 wpabuf_head(bsrv->resp),
1519 wpabuf_len(bsrv->resp));
1520 /* Response Data */
1521 wpabuf_put_buf(resp, bsrv->query); /* Key */
1522 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1523 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1524 2);
1525 }
1526}
1527
1528
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001529static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1530 size_t query_len)
1531{
1532 char str_rx[256], str_srv[256];
1533
1534 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1535 return 0; /* Too short to include DNS Type and Version */
1536 if (os_memcmp(query + query_len - 3,
1537 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1538 3) != 0)
1539 return 0; /* Mismatch in DNS Type or Version */
1540 if (query_len == wpabuf_len(bsrv->query) &&
1541 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1542 return 1; /* Binary match */
1543
1544 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1545 0))
1546 return 0; /* Failed to uncompress query */
1547 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1548 wpabuf_head(bsrv->query),
1549 wpabuf_len(bsrv->query) - 3, 0))
1550 return 0; /* Failed to uncompress service */
1551
1552 return os_strcmp(str_rx, str_srv) == 0;
1553}
1554
1555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1557 struct wpabuf *resp, u8 srv_trans_id,
1558 const u8 *query, size_t query_len)
1559{
1560 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001562 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001563
1564 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1565 query, query_len);
1566 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1567 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1568 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1569 srv_trans_id);
1570 return;
1571 }
1572
1573 if (query_len == 0) {
1574 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1575 return;
1576 }
1577
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001578 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1579 struct p2p_srv_bonjour, list) {
1580 if (!match_bonjour_query(bsrv, query, query_len))
1581 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001583 if (wpabuf_tailroom(resp) <
1584 5 + query_len + wpabuf_len(bsrv->resp))
1585 return;
1586
1587 matches++;
1588
1589 /* Length (to be filled) */
1590 len_pos = wpabuf_put(resp, 2);
1591 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1592 wpabuf_put_u8(resp, srv_trans_id);
1593
1594 /* Status Code */
1595 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1596 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1597 wpabuf_head(bsrv->resp),
1598 wpabuf_len(bsrv->resp));
1599
1600 /* Response Data */
1601 wpabuf_put_data(resp, query, query_len); /* Key */
1602 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1603
1604 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1605 }
1606
1607 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1609 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001610 if (wpabuf_tailroom(resp) < 5)
1611 return;
1612
1613 /* Length (to be filled) */
1614 len_pos = wpabuf_put(resp, 2);
1615 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1616 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617
1618 /* Status Code */
1619 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1620 /* Response Data: empty */
1621 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1622 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624}
1625
1626
1627static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1628 struct wpabuf *resp, u8 srv_trans_id)
1629{
1630 struct p2p_srv_upnp *usrv;
1631 u8 *len_pos;
1632
1633 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1634
1635 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1636 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1637 return;
1638 }
1639
1640 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1641 struct p2p_srv_upnp, list) {
1642 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1643 return;
1644
1645 /* Length (to be filled) */
1646 len_pos = wpabuf_put(resp, 2);
1647 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1648 wpabuf_put_u8(resp, srv_trans_id);
1649
1650 /* Status Code */
1651 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1652 /* Response Data */
1653 wpabuf_put_u8(resp, usrv->version);
1654 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1655 usrv->service);
1656 wpabuf_put_str(resp, usrv->service);
1657 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1658 2);
1659 }
1660}
1661
1662
1663static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1664 struct wpabuf *resp, u8 srv_trans_id,
1665 const u8 *query, size_t query_len)
1666{
1667 struct p2p_srv_upnp *usrv;
1668 u8 *len_pos;
1669 u8 version;
1670 char *str;
1671 int count = 0;
1672
1673 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1674 query, query_len);
1675
1676 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1677 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1678 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1679 srv_trans_id);
1680 return;
1681 }
1682
1683 if (query_len == 0) {
1684 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1685 return;
1686 }
1687
1688 if (wpabuf_tailroom(resp) < 5)
1689 return;
1690
1691 /* Length (to be filled) */
1692 len_pos = wpabuf_put(resp, 2);
1693 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1694 wpabuf_put_u8(resp, srv_trans_id);
1695
1696 version = query[0];
1697 str = os_malloc(query_len);
1698 if (str == NULL)
1699 return;
1700 os_memcpy(str, query + 1, query_len - 1);
1701 str[query_len - 1] = '\0';
1702
1703 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1704 struct p2p_srv_upnp, list) {
1705 if (version != usrv->version)
1706 continue;
1707
1708 if (os_strcmp(str, "ssdp:all") != 0 &&
1709 os_strstr(usrv->service, str) == NULL)
1710 continue;
1711
1712 if (wpabuf_tailroom(resp) < 2)
1713 break;
1714 if (count == 0) {
1715 /* Status Code */
1716 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1717 /* Response Data */
1718 wpabuf_put_u8(resp, version);
1719 } else
1720 wpabuf_put_u8(resp, ',');
1721
1722 count++;
1723
1724 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1725 usrv->service);
1726 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1727 break;
1728 wpabuf_put_str(resp, usrv->service);
1729 }
1730 os_free(str);
1731
1732 if (count == 0) {
1733 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1734 "available");
1735 /* Status Code */
1736 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1737 /* Response Data: empty */
1738 }
1739
1740 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1741}
1742
1743
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001744#ifdef CONFIG_WIFI_DISPLAY
1745static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1746 struct wpabuf *resp, u8 srv_trans_id,
1747 const u8 *query, size_t query_len)
1748{
1749 const u8 *pos;
1750 u8 role;
1751 u8 *len_pos;
1752
1753 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1754
1755 if (!wpa_s->global->wifi_display) {
1756 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1757 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1758 srv_trans_id);
1759 return;
1760 }
1761
1762 if (query_len < 1) {
1763 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1764 "Role");
1765 return;
1766 }
1767
1768 if (wpabuf_tailroom(resp) < 5)
1769 return;
1770
1771 pos = query;
1772 role = *pos++;
1773 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1774
1775 /* TODO: role specific handling */
1776
1777 /* Length (to be filled) */
1778 len_pos = wpabuf_put(resp, 2);
1779 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1780 wpabuf_put_u8(resp, srv_trans_id);
1781 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1782
1783 while (pos < query + query_len) {
1784 if (*pos < MAX_WFD_SUBELEMS &&
1785 wpa_s->global->wfd_subelem[*pos] &&
1786 wpabuf_tailroom(resp) >=
1787 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1788 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1789 "subelement %u", *pos);
1790 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1791 }
1792 pos++;
1793 }
1794
1795 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1796}
1797#endif /* CONFIG_WIFI_DISPLAY */
1798
1799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1801 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1802{
1803 struct wpa_supplicant *wpa_s = ctx;
1804 const u8 *pos = tlvs;
1805 const u8 *end = tlvs + tlvs_len;
1806 const u8 *tlv_end;
1807 u16 slen;
1808 struct wpabuf *resp;
1809 u8 srv_proto, srv_trans_id;
1810 size_t buf_len;
1811 char *buf;
1812
1813 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1814 tlvs, tlvs_len);
1815 buf_len = 2 * tlvs_len + 1;
1816 buf = os_malloc(buf_len);
1817 if (buf) {
1818 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1819 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1820 MACSTR " %u %u %s",
1821 freq, MAC2STR(sa), dialog_token, update_indic,
1822 buf);
1823 os_free(buf);
1824 }
1825
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001826 if (wpa_s->p2p_sd_over_ctrl_iface) {
1827 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1828 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001830 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831
1832 resp = wpabuf_alloc(10000);
1833 if (resp == NULL)
1834 return;
1835
1836 while (pos + 1 < end) {
1837 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1838 slen = WPA_GET_LE16(pos);
1839 pos += 2;
1840 if (pos + slen > end || slen < 2) {
1841 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1842 "length");
1843 wpabuf_free(resp);
1844 return;
1845 }
1846 tlv_end = pos + slen;
1847
1848 srv_proto = *pos++;
1849 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1850 srv_proto);
1851 srv_trans_id = *pos++;
1852 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1853 srv_trans_id);
1854
1855 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1856 pos, tlv_end - pos);
1857
1858
1859 if (wpa_s->force_long_sd) {
1860 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1861 "response");
1862 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1863 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1864 goto done;
1865 }
1866
1867 switch (srv_proto) {
1868 case P2P_SERV_ALL_SERVICES:
1869 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1870 "for all services");
1871 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1872 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1873 wpa_printf(MSG_DEBUG, "P2P: No service "
1874 "discovery protocols available");
1875 wpas_sd_add_proto_not_avail(
1876 resp, P2P_SERV_ALL_SERVICES,
1877 srv_trans_id);
1878 break;
1879 }
1880 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1881 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1882 break;
1883 case P2P_SERV_BONJOUR:
1884 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1885 pos, tlv_end - pos);
1886 break;
1887 case P2P_SERV_UPNP:
1888 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1889 pos, tlv_end - pos);
1890 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001891#ifdef CONFIG_WIFI_DISPLAY
1892 case P2P_SERV_WIFI_DISPLAY:
1893 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1894 pos, tlv_end - pos);
1895 break;
1896#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001897 default:
1898 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1899 "protocol %u", srv_proto);
1900 wpas_sd_add_proto_not_avail(resp, srv_proto,
1901 srv_trans_id);
1902 break;
1903 }
1904
1905 pos = tlv_end;
1906 }
1907
1908done:
1909 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1910 update_indic, tlvs, tlvs_len);
1911
1912 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1913
1914 wpabuf_free(resp);
1915}
1916
1917
1918void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1919 const u8 *tlvs, size_t tlvs_len)
1920{
1921 struct wpa_supplicant *wpa_s = ctx;
1922 const u8 *pos = tlvs;
1923 const u8 *end = tlvs + tlvs_len;
1924 const u8 *tlv_end;
1925 u16 slen;
1926 size_t buf_len;
1927 char *buf;
1928
1929 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1930 tlvs, tlvs_len);
1931 if (tlvs_len > 1500) {
1932 /* TODO: better way for handling this */
1933 wpa_msg_ctrl(wpa_s, MSG_INFO,
1934 P2P_EVENT_SERV_DISC_RESP MACSTR
1935 " %u <long response: %u bytes>",
1936 MAC2STR(sa), update_indic,
1937 (unsigned int) tlvs_len);
1938 } else {
1939 buf_len = 2 * tlvs_len + 1;
1940 buf = os_malloc(buf_len);
1941 if (buf) {
1942 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1943 wpa_msg_ctrl(wpa_s, MSG_INFO,
1944 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1945 MAC2STR(sa), update_indic, buf);
1946 os_free(buf);
1947 }
1948 }
1949
1950 while (pos < end) {
1951 u8 srv_proto, srv_trans_id, status;
1952
1953 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1954 slen = WPA_GET_LE16(pos);
1955 pos += 2;
1956 if (pos + slen > end || slen < 3) {
1957 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1958 "length");
1959 return;
1960 }
1961 tlv_end = pos + slen;
1962
1963 srv_proto = *pos++;
1964 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1965 srv_proto);
1966 srv_trans_id = *pos++;
1967 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1968 srv_trans_id);
1969 status = *pos++;
1970 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1971 status);
1972
1973 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1974 pos, tlv_end - pos);
1975
1976 pos = tlv_end;
1977 }
1978
1979 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1980}
1981
1982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001983u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1984 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985{
1986 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001987 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001988 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001989 return 0;
1990 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991}
1992
1993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001994u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1995 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996{
1997 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001998 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999
2000 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2001 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002002 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2004 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2005 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2006 wpabuf_put_u8(tlvs, version);
2007 wpabuf_put_str(tlvs, query);
2008 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2009 wpabuf_free(tlvs);
2010 return ret;
2011}
2012
2013
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002014#ifdef CONFIG_WIFI_DISPLAY
2015
2016static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2017 const struct wpabuf *tlvs)
2018{
2019 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2020 return 0;
2021 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2022 return 0;
2023 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2024}
2025
2026
2027#define MAX_WFD_SD_SUBELEMS 20
2028
2029static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2030 const char *subelems)
2031{
2032 u8 *len;
2033 const char *pos;
2034 int val;
2035 int count = 0;
2036
2037 len = wpabuf_put(tlvs, 2);
2038 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2039 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2040
2041 wpabuf_put_u8(tlvs, role);
2042
2043 pos = subelems;
2044 while (*pos) {
2045 val = atoi(pos);
2046 if (val >= 0 && val < 256) {
2047 wpabuf_put_u8(tlvs, val);
2048 count++;
2049 if (count == MAX_WFD_SD_SUBELEMS)
2050 break;
2051 }
2052 pos = os_strchr(pos + 1, ',');
2053 if (pos == NULL)
2054 break;
2055 pos++;
2056 }
2057
2058 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2059}
2060
2061
2062u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2063 const u8 *dst, const char *role)
2064{
2065 struct wpabuf *tlvs;
2066 u64 ret;
2067 const char *subelems;
2068 u8 id = 1;
2069
2070 subelems = os_strchr(role, ' ');
2071 if (subelems == NULL)
2072 return 0;
2073 subelems++;
2074
2075 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2076 if (tlvs == NULL)
2077 return 0;
2078
2079 if (os_strstr(role, "[source]"))
2080 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2081 if (os_strstr(role, "[pri-sink]"))
2082 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2083 if (os_strstr(role, "[sec-sink]"))
2084 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2085 if (os_strstr(role, "[source+sink]"))
2086 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2087
2088 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2089 wpabuf_free(tlvs);
2090 return ret;
2091}
2092
2093#endif /* CONFIG_WIFI_DISPLAY */
2094
2095
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002096int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097{
2098 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002099 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2101 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102 return p2p_sd_cancel_request(wpa_s->global->p2p,
2103 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104}
2105
2106
2107void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2108 const u8 *dst, u8 dialog_token,
2109 const struct wpabuf *resp_tlvs)
2110{
2111 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2112 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2113 resp_tlvs);
2114 return;
2115 }
2116 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2117 return;
2118 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2119 resp_tlvs);
2120}
2121
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002122#ifdef ANDROID_P2P
2123void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2124#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002126#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127{
2128 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2129 wpa_drv_p2p_service_update(wpa_s);
2130 return;
2131 }
2132 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002133#ifdef ANDROID_P2P
2134 p2p_sd_service_update(wpa_s->global->p2p, action);
2135#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002136 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002137#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138}
2139
2140
2141static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2142{
2143 dl_list_del(&bsrv->list);
2144 wpabuf_free(bsrv->query);
2145 wpabuf_free(bsrv->resp);
2146 os_free(bsrv);
2147}
2148
2149
2150static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2151{
2152 dl_list_del(&usrv->list);
2153 os_free(usrv->service);
2154 os_free(usrv);
2155}
2156
2157
2158void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2159{
2160 struct p2p_srv_bonjour *bsrv, *bn;
2161 struct p2p_srv_upnp *usrv, *un;
2162
2163 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2164 struct p2p_srv_bonjour, list)
2165 wpas_p2p_srv_bonjour_free(bsrv);
2166
2167 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2168 struct p2p_srv_upnp, list)
2169 wpas_p2p_srv_upnp_free(usrv);
2170
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002171#ifdef ANDROID_P2P
2172 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2173#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002175#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002176}
2177
2178
2179int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2180 struct wpabuf *query, struct wpabuf *resp)
2181{
2182 struct p2p_srv_bonjour *bsrv;
2183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 bsrv = os_zalloc(sizeof(*bsrv));
2185 if (bsrv == NULL)
2186 return -1;
2187 bsrv->query = query;
2188 bsrv->resp = resp;
2189 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2190
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002191#ifdef ANDROID_P2P
2192 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2193#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002195#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196 return 0;
2197}
2198
2199
2200int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2201 const struct wpabuf *query)
2202{
2203 struct p2p_srv_bonjour *bsrv;
2204
2205 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2206 if (bsrv == NULL)
2207 return -1;
2208 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002209#ifdef ANDROID_P2P
2210 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2211#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002212 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002213#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002214 return 0;
2215}
2216
2217
2218int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2219 const char *service)
2220{
2221 struct p2p_srv_upnp *usrv;
2222
2223 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2224 return 0; /* Already listed */
2225 usrv = os_zalloc(sizeof(*usrv));
2226 if (usrv == NULL)
2227 return -1;
2228 usrv->version = version;
2229 usrv->service = os_strdup(service);
2230 if (usrv->service == NULL) {
2231 os_free(usrv);
2232 return -1;
2233 }
2234 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2235
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002236#ifdef ANDROID_P2P
2237 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2238#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002240#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 return 0;
2242}
2243
2244
2245int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2246 const char *service)
2247{
2248 struct p2p_srv_upnp *usrv;
2249
2250 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2251 if (usrv == NULL)
2252 return -1;
2253 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002254#ifdef ANDROID_P2P
2255 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2256#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002258#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259 return 0;
2260}
2261
2262
2263static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2264 const u8 *peer, const char *params,
2265 unsigned int generated_pin)
2266{
2267 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2268 MAC2STR(peer), generated_pin, params);
2269}
2270
2271
2272static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2273 const u8 *peer, const char *params)
2274{
2275 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2276 MAC2STR(peer), params);
2277}
2278
2279
2280void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2281 const u8 *dev_addr, const u8 *pri_dev_type,
2282 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002283 u8 dev_capab, u8 group_capab, const u8 *group_id,
2284 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285{
2286 struct wpa_supplicant *wpa_s = ctx;
2287 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002288 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 u8 empty_dev_type[8];
2290 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002291 struct wpa_supplicant *group = NULL;
2292
2293 if (group_id) {
2294 for (group = wpa_s->global->ifaces; group; group = group->next)
2295 {
2296 struct wpa_ssid *s = group->current_ssid;
2297 if (s != NULL &&
2298 s->mode == WPAS_MODE_P2P_GO &&
2299 group_id_len - ETH_ALEN == s->ssid_len &&
2300 os_memcmp(group_id + ETH_ALEN, s->ssid,
2301 s->ssid_len) == 0)
2302 break;
2303 }
2304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002305
2306 if (pri_dev_type == NULL) {
2307 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2308 pri_dev_type = empty_dev_type;
2309 }
2310 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2311 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002312 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 MAC2STR(dev_addr),
2314 wps_dev_type_bin2str(pri_dev_type, devtype,
2315 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002316 dev_name, supp_config_methods, dev_capab, group_capab,
2317 group ? " group=" : "",
2318 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002319 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 if (config_methods & WPS_CONFIG_DISPLAY) {
2322 generated_pin = wps_generate_pin();
2323 wpas_prov_disc_local_display(wpa_s, peer, params,
2324 generated_pin);
2325 } else if (config_methods & WPS_CONFIG_KEYPAD)
2326 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2327 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2328 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2329 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002330
2331 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2332 P2P_PROV_DISC_SUCCESS,
2333 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334}
2335
2336
2337void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2338{
2339 struct wpa_supplicant *wpa_s = ctx;
2340 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002341 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002343 if (wpa_s->pending_pd_before_join &&
2344 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2345 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2346 wpa_s->pending_pd_before_join = 0;
2347 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2348 "join-existing-group operation");
2349 wpas_p2p_join_start(wpa_s);
2350 return;
2351 }
2352
Dmitry Shmidt04949592012-07-19 12:16:46 -07002353 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2354 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2355 os_snprintf(params, sizeof(params), " peer_go=%d",
2356 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2357 else
2358 params[0] = '\0';
2359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002360 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002361 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362 else if (config_methods & WPS_CONFIG_KEYPAD) {
2363 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002364 wpas_prov_disc_local_display(wpa_s, peer, params,
2365 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002367 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2368 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369
Jouni Malinen75ecf522011-06-27 15:19:46 -07002370 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2371 P2P_PROV_DISC_SUCCESS,
2372 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373}
2374
2375
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002376static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2377 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002378{
2379 struct wpa_supplicant *wpa_s = ctx;
2380
Dmitry Shmidt04949592012-07-19 12:16:46 -07002381 if (wpa_s->p2p_fallback_to_go_neg) {
2382 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2383 "failed - fall back to GO Negotiation");
2384 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2385 return;
2386 }
2387
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002388 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002389 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002390 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2391 "join-existing-group operation (no ACK for PD "
2392 "Req attempts)");
2393 wpas_p2p_join_start(wpa_s);
2394 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002395 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002396
Dmitry Shmidt04949592012-07-19 12:16:46 -07002397 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2398 " p2p_dev_addr=" MACSTR " status=%d",
2399 MAC2STR(peer), status);
2400
Jouni Malinen75ecf522011-06-27 15:19:46 -07002401 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2402 status, 0, 0);
2403}
2404
2405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002406static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2407 const u8 *go_dev_addr, const u8 *ssid,
2408 size_t ssid_len, int *go, u8 *group_bssid,
2409 int *force_freq, int persistent_group)
2410{
2411 struct wpa_supplicant *wpa_s = ctx;
2412 struct wpa_ssid *s;
2413 u8 cur_bssid[ETH_ALEN];
2414 int res;
2415 struct wpa_supplicant *grp;
2416
2417 if (!persistent_group) {
2418 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2419 " to join an active group", MAC2STR(sa));
2420 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2421 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2422 == 0 ||
2423 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2424 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2425 "authorized invitation");
2426 goto accept_inv;
2427 }
2428 /*
2429 * Do not accept the invitation automatically; notify user and
2430 * request approval.
2431 */
2432 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2433 }
2434
2435 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2436 if (grp) {
2437 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2438 "running persistent group");
2439 if (*go)
2440 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2441 goto accept_inv;
2442 }
2443
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002444 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2445 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2446 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2447 "invitation to re-invoke a persistent group");
2448 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2450
2451 for (s = wpa_s->conf->ssid; s; s = s->next) {
2452 if (s->disabled == 2 &&
2453 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2454 s->ssid_len == ssid_len &&
2455 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2456 break;
2457 }
2458
2459 if (!s) {
2460 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2461 " requested reinvocation of an unknown group",
2462 MAC2STR(sa));
2463 return P2P_SC_FAIL_UNKNOWN_GROUP;
2464 }
2465
2466 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2467 *go = 1;
2468 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2469 wpa_printf(MSG_DEBUG, "P2P: The only available "
2470 "interface is already in use - reject "
2471 "invitation");
2472 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2473 }
2474 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2475 } else if (s->mode == WPAS_MODE_P2P_GO) {
2476 *go = 1;
2477 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2478 {
2479 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2480 "interface address for the group");
2481 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2482 }
2483 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2484 ETH_ALEN);
2485 }
2486
2487accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002488 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2491 wpa_s->assoc_freq) {
2492 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2493 "the channel we are already using");
2494 *force_freq = wpa_s->assoc_freq;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002495 wpas_p2p_set_own_freq_preference(wpa_s, wpa_s->assoc_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496 }
2497
2498 res = wpa_drv_shared_freq(wpa_s);
2499 if (res > 0) {
2500 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2501 "with the channel we are already using on a "
2502 "shared interface");
2503 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002504 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505 }
2506
2507 return P2P_SC_SUCCESS;
2508}
2509
2510
2511static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2512 const u8 *ssid, size_t ssid_len,
2513 const u8 *go_dev_addr, u8 status,
2514 int op_freq)
2515{
2516 struct wpa_supplicant *wpa_s = ctx;
2517 struct wpa_ssid *s;
2518
2519 for (s = wpa_s->conf->ssid; s; s = s->next) {
2520 if (s->disabled == 2 &&
2521 s->ssid_len == ssid_len &&
2522 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2523 break;
2524 }
2525
2526 if (status == P2P_SC_SUCCESS) {
2527 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2528 " was accepted; op_freq=%d MHz",
2529 MAC2STR(sa), op_freq);
2530 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002531 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002533 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002535 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002537 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 }
2539 return;
2540 }
2541
2542 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2543 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2544 " was rejected (status %u)", MAC2STR(sa), status);
2545 return;
2546 }
2547
2548 if (!s) {
2549 if (bssid) {
2550 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2551 "sa=" MACSTR " go_dev_addr=" MACSTR
2552 " bssid=" MACSTR " unknown-network",
2553 MAC2STR(sa), MAC2STR(go_dev_addr),
2554 MAC2STR(bssid));
2555 } else {
2556 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2557 "sa=" MACSTR " go_dev_addr=" MACSTR
2558 " unknown-network",
2559 MAC2STR(sa), MAC2STR(go_dev_addr));
2560 }
2561 return;
2562 }
2563
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002564 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2565 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2566 MACSTR " persistent=%d freq=%d",
2567 MAC2STR(sa), s->id, op_freq);
2568 } else {
2569 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2570 MACSTR " persistent=%d", MAC2STR(sa), s->id);
2571 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572}
2573
2574
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002575static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2576 struct wpa_ssid *ssid,
2577 const u8 *peer)
2578{
2579 size_t i;
2580
2581 if (ssid == NULL)
2582 return;
2583
2584 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2585 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2586 ETH_ALEN) == 0)
2587 break;
2588 }
2589 if (i >= ssid->num_p2p_clients) {
2590 if (ssid->mode != WPAS_MODE_P2P_GO &&
2591 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2592 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2593 "due to invitation result", ssid->id);
2594 wpas_notify_network_removed(wpa_s, ssid);
2595 wpa_config_remove_network(wpa_s->conf, ssid->id);
2596 return;
2597 }
2598 return; /* Peer not found in client list */
2599 }
2600
2601 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2602 "group %d client list due to invitation result",
2603 MAC2STR(peer), ssid->id);
2604 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2605 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2606 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2607 ssid->num_p2p_clients--;
2608#ifndef CONFIG_NO_CONFIG_WRITE
2609 if (wpa_s->parent->conf->update_config &&
2610 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2611 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2612#endif /* CONFIG_NO_CONFIG_WRITE */
2613}
2614
2615
2616static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2617 const u8 *peer)
2618{
2619 struct wpa_ssid *ssid;
2620
2621 wpa_s = wpa_s->global->p2p_invite_group;
2622 if (wpa_s == NULL)
2623 return; /* No known invitation group */
2624 ssid = wpa_s->current_ssid;
2625 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2626 !ssid->p2p_persistent_group)
2627 return; /* Not operating as a GO in persistent group */
2628 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2629 ssid->ssid, ssid->ssid_len);
2630 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2631}
2632
2633
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002634static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002635 const struct p2p_channels *channels,
2636 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637{
2638 struct wpa_supplicant *wpa_s = ctx;
2639 struct wpa_ssid *ssid;
2640
2641 if (bssid) {
2642 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2643 "status=%d " MACSTR,
2644 status, MAC2STR(bssid));
2645 } else {
2646 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2647 "status=%d ", status);
2648 }
2649 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2650
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002651 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2652 status, MAC2STR(peer));
2653 if (wpa_s->pending_invite_ssid_id == -1) {
2654 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2655 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002657 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002659 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2660 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2661 "invitation exchange to indicate readiness for "
2662 "re-invocation");
2663 }
2664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002665 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002666 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2667 ssid = wpa_config_get_network(
2668 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2669 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2670 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 wpas_p2p_remove_pending_group_interface(wpa_s);
2672 return;
2673 }
2674
2675 ssid = wpa_config_get_network(wpa_s->conf,
2676 wpa_s->pending_invite_ssid_id);
2677 if (ssid == NULL) {
2678 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2679 "data matching with invitation");
2680 return;
2681 }
2682
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002683 /*
2684 * The peer could have missed our ctrl::ack frame for Invitation
2685 * Response and continue retransmitting the frame. To reduce the
2686 * likelihood of the peer not getting successful TX status for the
2687 * Invitation Response frame, wait a short time here before starting
2688 * the persistent group so that we will remain on the current channel to
2689 * acknowledge any possible retransmission from the peer.
2690 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002691#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002692 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2693 "starting persistent group");
2694 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002695#else
2696 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2697 "starting persistent group");
2698 os_sleep(0, 100000);
2699#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002700
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002702 ssid->mode == WPAS_MODE_P2P_GO,
2703 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002704 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705}
2706
2707
Dmitry Shmidt04949592012-07-19 12:16:46 -07002708static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2709 unsigned int freq)
2710{
2711 unsigned int i;
2712
2713 if (global->p2p_disallow_freq == NULL)
2714 return 0;
2715
2716 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2717 if (freq >= global->p2p_disallow_freq[i].min &&
2718 freq <= global->p2p_disallow_freq[i].max)
2719 return 1;
2720 }
2721
2722 return 0;
2723}
2724
2725
2726static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2727{
2728 reg->channel[reg->channels] = chan;
2729 reg->channels++;
2730}
2731
2732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2734 struct p2p_channels *chan)
2735{
2736 int i, cla = 0;
2737
2738 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2739 "band");
2740
2741 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2742 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002743 chan->reg_class[cla].channels = 0;
2744 for (i = 0; i < 11; i++) {
2745 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2746 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2747 }
2748 if (chan->reg_class[cla].channels)
2749 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002750
2751 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2752 "band");
2753
2754 /* Operating class 115 - 5 GHz, channels 36-48 */
2755 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002756 chan->reg_class[cla].channels = 0;
2757 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2758 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2759 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2760 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2761 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2762 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2763 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2764 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2765 if (chan->reg_class[cla].channels)
2766 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767
2768 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2769 "band");
2770
2771 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2772 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002773 chan->reg_class[cla].channels = 0;
2774 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2775 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2776 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2777 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2778 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2779 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2780 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2781 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2782 if (chan->reg_class[cla].channels)
2783 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002784
2785 chan->reg_classes = cla;
2786 return 0;
2787}
2788
2789
2790static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2791 u16 num_modes,
2792 enum hostapd_hw_mode mode)
2793{
2794 u16 i;
2795
2796 for (i = 0; i < num_modes; i++) {
2797 if (modes[i].mode == mode)
2798 return &modes[i];
2799 }
2800
2801 return NULL;
2802}
2803
2804
Dmitry Shmidt04949592012-07-19 12:16:46 -07002805static int has_channel(struct wpa_global *global,
2806 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807{
2808 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002809 unsigned int freq;
2810
2811 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2812 chan * 5;
2813 if (wpas_p2p_disallowed_freq(global, freq))
2814 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815
2816 for (i = 0; i < mode->num_channels; i++) {
2817 if (mode->channels[i].chan == chan) {
2818 if (flags)
2819 *flags = mode->channels[i].flag;
2820 return !(mode->channels[i].flag &
2821 (HOSTAPD_CHAN_DISABLED |
2822 HOSTAPD_CHAN_PASSIVE_SCAN |
2823 HOSTAPD_CHAN_NO_IBSS |
2824 HOSTAPD_CHAN_RADAR));
2825 }
2826 }
2827
2828 return 0;
2829}
2830
2831
2832struct p2p_oper_class_map {
2833 enum hostapd_hw_mode mode;
2834 u8 op_class;
2835 u8 min_chan;
2836 u8 max_chan;
2837 u8 inc;
2838 enum { BW20, BW40PLUS, BW40MINUS } bw;
2839};
2840
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002841static struct p2p_oper_class_map op_class[] = {
2842 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002843#if 0 /* Do not enable HT40 on 2 GHz for now */
2844 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2845 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2846#endif
2847 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2848 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2849 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2850 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2851 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2852 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2853 { -1, 0, 0, 0, 0, BW20 }
2854};
2855
2856
2857static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2858 struct hostapd_hw_modes *mode,
2859 u8 channel, u8 bw)
2860{
2861 int flag;
2862
2863 if (!has_channel(wpa_s->global, mode, channel, &flag))
2864 return -1;
2865 if (bw == BW40MINUS &&
2866 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2867 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2868 return 0;
2869 if (bw == BW40PLUS &&
2870 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2871 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2872 return 0;
2873 return 1;
2874}
2875
2876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2878 struct p2p_channels *chan)
2879{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002880 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002883 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2885 "of all supported channels; assume dualband "
2886 "support");
2887 return wpas_p2p_default_channels(wpa_s, chan);
2888 }
2889
2890 cla = 0;
2891
2892 for (op = 0; op_class[op].op_class; op++) {
2893 struct p2p_oper_class_map *o = &op_class[op];
2894 u8 ch;
2895 struct p2p_reg_class *reg = NULL;
2896
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002897 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898 if (mode == NULL)
2899 continue;
2900 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002901 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902 continue;
2903 if (reg == NULL) {
2904 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2905 "class %u", o->op_class);
2906 reg = &chan->reg_class[cla];
2907 cla++;
2908 reg->reg_class = o->op_class;
2909 }
2910 reg->channel[reg->channels] = ch;
2911 reg->channels++;
2912 }
2913 if (reg) {
2914 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2915 reg->channel, reg->channels);
2916 }
2917 }
2918
2919 chan->reg_classes = cla;
2920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921 return 0;
2922}
2923
2924
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002925int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2926 struct hostapd_hw_modes *mode, u8 channel)
2927{
2928 int op, ret;
2929
2930 for (op = 0; op_class[op].op_class; op++) {
2931 struct p2p_oper_class_map *o = &op_class[op];
2932 u8 ch;
2933
2934 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2935 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2936 o->bw == BW20 || ch != channel)
2937 continue;
2938 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2939 if (ret < 0)
2940 continue;
2941 else if (ret > 0)
2942 return (o->bw == BW40MINUS) ? -1 : 1;
2943 else
2944 return 0;
2945 }
2946 }
2947 return 0;
2948}
2949
2950
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002951static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2952 size_t buf_len)
2953{
2954 struct wpa_supplicant *wpa_s = ctx;
2955
2956 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2957 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2958 break;
2959 }
2960 if (wpa_s == NULL)
2961 return -1;
2962
2963 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2964}
2965
2966
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002967static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2968{
2969 struct wpa_supplicant *wpa_s = ctx;
2970
2971 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2972 struct wpa_ssid *ssid = wpa_s->current_ssid;
2973 if (ssid == NULL)
2974 continue;
2975 if (ssid->mode != WPAS_MODE_INFRA)
2976 continue;
2977 if (wpa_s->wpa_state != WPA_COMPLETED &&
2978 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2979 continue;
2980 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2981 return 1;
2982 }
2983
2984 return 0;
2985}
2986
2987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988/**
2989 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2990 * @global: Pointer to global data from wpa_supplicant_init()
2991 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2992 * Returns: 0 on success, -1 on failure
2993 */
2994int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2995{
2996 struct p2p_config p2p;
2997 unsigned int r;
2998 int i;
2999
3000 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3001 return 0;
3002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003 if (global->p2p)
3004 return 0;
3005
3006 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3007 struct p2p_params params;
3008
3009 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3010 os_memset(&params, 0, sizeof(params));
3011 params.dev_name = wpa_s->conf->device_name;
3012 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3013 WPS_DEV_TYPE_LEN);
3014 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3015 os_memcpy(params.sec_dev_type,
3016 wpa_s->conf->sec_device_type,
3017 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3018
3019 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3020 return -1;
3021
3022 return 0;
3023 }
3024
3025 os_memset(&p2p, 0, sizeof(p2p));
3026 p2p.msg_ctx = wpa_s;
3027 p2p.cb_ctx = wpa_s;
3028 p2p.p2p_scan = wpas_p2p_scan;
3029 p2p.send_action = wpas_send_action;
3030 p2p.send_action_done = wpas_send_action_done;
3031 p2p.go_neg_completed = wpas_go_neg_completed;
3032 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3033 p2p.dev_found = wpas_dev_found;
3034 p2p.dev_lost = wpas_dev_lost;
3035 p2p.start_listen = wpas_start_listen;
3036 p2p.stop_listen = wpas_stop_listen;
3037 p2p.send_probe_resp = wpas_send_probe_resp;
3038 p2p.sd_request = wpas_sd_request;
3039 p2p.sd_response = wpas_sd_response;
3040 p2p.prov_disc_req = wpas_prov_disc_req;
3041 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003042 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003043 p2p.invitation_process = wpas_invitation_process;
3044 p2p.invitation_received = wpas_invitation_received;
3045 p2p.invitation_result = wpas_invitation_result;
3046 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003047 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048
3049 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003050 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051 p2p.dev_name = wpa_s->conf->device_name;
3052 p2p.manufacturer = wpa_s->conf->manufacturer;
3053 p2p.model_name = wpa_s->conf->model_name;
3054 p2p.model_number = wpa_s->conf->model_number;
3055 p2p.serial_number = wpa_s->conf->serial_number;
3056 if (wpa_s->wps) {
3057 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3058 p2p.config_methods = wpa_s->wps->config_methods;
3059 }
3060
3061 if (wpa_s->conf->p2p_listen_reg_class &&
3062 wpa_s->conf->p2p_listen_channel) {
3063 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3064 p2p.channel = wpa_s->conf->p2p_listen_channel;
3065 } else {
3066 p2p.reg_class = 81;
3067 /*
3068 * Pick one of the social channels randomly as the listen
3069 * channel.
3070 */
3071 os_get_random((u8 *) &r, sizeof(r));
3072 p2p.channel = 1 + (r % 3) * 5;
3073 }
3074 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3075
3076 if (wpa_s->conf->p2p_oper_reg_class &&
3077 wpa_s->conf->p2p_oper_channel) {
3078 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3079 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3080 p2p.cfg_op_channel = 1;
3081 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3082 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3083
3084 } else {
3085 p2p.op_reg_class = 81;
3086 /*
3087 * Use random operation channel from (1, 6, 11) if no other
3088 * preference is indicated.
3089 */
3090 os_get_random((u8 *) &r, sizeof(r));
3091 p2p.op_channel = 1 + (r % 3) * 5;
3092 p2p.cfg_op_channel = 0;
3093 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3094 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3095 }
3096 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3097 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3098 p2p.country[2] = 0x04;
3099 } else
3100 os_memcpy(p2p.country, "XX\x04", 3);
3101
3102 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3103 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3104 "channel list");
3105 return -1;
3106 }
3107
3108 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3109 WPS_DEV_TYPE_LEN);
3110
3111 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3112 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3113 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3114
3115 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3116 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3117
3118 p2p.max_peers = 100;
3119
3120 if (wpa_s->conf->p2p_ssid_postfix) {
3121 p2p.ssid_postfix_len =
3122 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3123 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3124 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3125 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3126 p2p.ssid_postfix_len);
3127 }
3128
3129 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3130
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003131 p2p.max_listen = wpa_s->max_remain_on_chan;
3132
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003133#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003134 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003135 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003136 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3137 } else {
3138 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003139 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003140 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3141 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003142#endif
3143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144 global->p2p = p2p_init(&p2p);
3145 if (global->p2p == NULL)
3146 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003147 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003148
3149 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3150 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3151 continue;
3152 p2p_add_wps_vendor_extension(
3153 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3154 }
3155
3156 return 0;
3157}
3158
3159
3160/**
3161 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3162 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3163 *
3164 * This function deinitialize per-interface P2P data.
3165 */
3166void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3167{
3168 if (wpa_s->driver && wpa_s->drv_priv)
3169 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003170
3171 if (wpa_s->go_params) {
3172 /* Clear any stored provisioning info */
3173 p2p_clear_provisioning_info(
3174 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003175 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003176 }
3177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003178 os_free(wpa_s->go_params);
3179 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003180 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3181 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3182 wpa_s->p2p_long_listen = 0;
3183 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3184 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3185 wpas_p2p_remove_pending_group_interface(wpa_s);
3186
3187 /* TODO: remove group interface from the driver if this wpa_s instance
3188 * is on top of a P2P group interface */
3189}
3190
3191
3192/**
3193 * wpas_p2p_deinit_global - Deinitialize global P2P module
3194 * @global: Pointer to global data from wpa_supplicant_init()
3195 *
3196 * This function deinitializes the global (per device) P2P module.
3197 */
3198void wpas_p2p_deinit_global(struct wpa_global *global)
3199{
3200 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003201
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003202 wpa_s = global->ifaces;
3203 if (wpa_s)
3204 wpas_p2p_service_flush(wpa_s);
3205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206 if (global->p2p == NULL)
3207 return;
3208
3209 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3211 wpa_s = wpa_s->next;
3212 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213 tmp = global->ifaces;
3214 while (tmp &&
3215 (tmp == wpa_s ||
3216 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3217 tmp = tmp->next;
3218 }
3219 if (tmp == NULL)
3220 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003221 /* Disconnect from the P2P group and deinit the interface */
3222 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223 }
3224
3225 /*
3226 * Deinit GO data on any possibly remaining interface (if main
3227 * interface is used as GO).
3228 */
3229 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3230 if (wpa_s->ap_iface)
3231 wpas_p2p_group_deinit(wpa_s);
3232 }
3233
3234 p2p_deinit(global->p2p);
3235 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003236 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237}
3238
3239
3240static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3241{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003242 if (wpa_s->conf->p2p_no_group_iface)
3243 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003244 if (wpa_s->drv_flags &
3245 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3246 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3247 return 1; /* P2P group requires a new interface in every case
3248 */
3249 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3250 return 0; /* driver does not support concurrent operations */
3251 if (wpa_s->global->ifaces->next)
3252 return 1; /* more that one interface already in use */
3253 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3254 return 1; /* this interface is already in use */
3255 return 0;
3256}
3257
3258
3259static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3260 const u8 *peer_addr,
3261 enum p2p_wps_method wps_method,
3262 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003263 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003264 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003265{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003266 if (persistent_group && wpa_s->conf->persistent_reconnect)
3267 persistent_group = 2;
3268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3270 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3271 go_intent, own_interface_addr,
3272 force_freq, persistent_group);
3273 }
3274
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003275 /*
3276 * Increase GO config timeout if HT40 is used since it takes some time
3277 * to scan channels for coex purposes before the BSS can be started.
3278 */
3279 p2p_set_config_timeout(wpa_s->global->p2p,
3280 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3283 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003284 persistent_group, ssid ? ssid->ssid : NULL,
3285 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003286 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003287}
3288
3289
3290static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3291 const u8 *peer_addr,
3292 enum p2p_wps_method wps_method,
3293 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003294 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003295 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003297 if (persistent_group && wpa_s->conf->persistent_reconnect)
3298 persistent_group = 2;
3299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3301 return -1;
3302
3303 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3304 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003305 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003306 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307}
3308
3309
3310static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3311{
3312 wpa_s->p2p_join_scan_count++;
3313 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3314 wpa_s->p2p_join_scan_count);
3315 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3316 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3317 " for join operationg - stop join attempt",
3318 MAC2STR(wpa_s->pending_join_iface_addr));
3319 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003320 if (wpa_s->p2p_auto_pd) {
3321 wpa_s->p2p_auto_pd = 0;
3322 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3323 " p2p_dev_addr=" MACSTR " status=N/A",
3324 MAC2STR(wpa_s->pending_join_dev_addr));
3325 return;
3326 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003327 wpa_msg(wpa_s->parent, MSG_INFO,
3328 P2P_EVENT_GROUP_FORMATION_FAILURE);
3329 }
3330}
3331
3332
Dmitry Shmidt04949592012-07-19 12:16:46 -07003333static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3334{
3335 struct wpa_supplicant *iface;
3336 int shared_freq;
3337 u8 bssid[ETH_ALEN];
3338
3339 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3340 return 0;
3341
3342 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3343 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3344 continue;
3345 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3346 continue;
3347 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3348 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3349 shared_freq = iface->current_ssid->frequency;
3350 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3351 shared_freq = iface->assoc_freq;
3352 else
3353 shared_freq = 0;
3354
3355 if (shared_freq && freq != shared_freq) {
3356 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3357 "connected on %d MHz - new connection on "
3358 "%d MHz", iface->ifname, shared_freq, freq);
3359 return 1;
3360 }
3361 }
3362
3363 shared_freq = wpa_drv_shared_freq(wpa_s);
3364 if (shared_freq > 0 && shared_freq != freq) {
3365 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3366 "virtual interface connected on %d MHz - new "
3367 "connection on %d MHz", shared_freq, freq);
3368 return 1;
3369 }
3370
3371 return 0;
3372}
3373
3374
3375static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3376 const u8 *peer_dev_addr)
3377{
3378 struct wpa_bss *bss;
3379 int updated;
3380
3381 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3382 if (bss == NULL)
3383 return -1;
3384 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3385 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3386 "last scan");
3387 return 0;
3388 }
3389
3390 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3391 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3392 "%ld.%06ld (%supdated in last scan)",
3393 bss->last_update.sec, bss->last_update.usec,
3394 updated ? "": "not ");
3395
3396 return updated;
3397}
3398
3399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003400static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3401 struct wpa_scan_results *scan_res)
3402{
3403 struct wpa_bss *bss;
3404 int freq;
3405 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003407 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3408
3409 if (wpa_s->global->p2p_disabled)
3410 return;
3411
Dmitry Shmidt04949592012-07-19 12:16:46 -07003412 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3413 scan_res ? (int) scan_res->num : -1,
3414 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415
3416 if (scan_res)
3417 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3418
Dmitry Shmidt04949592012-07-19 12:16:46 -07003419 if (wpa_s->p2p_auto_pd) {
3420 int join = wpas_p2p_peer_go(wpa_s,
3421 wpa_s->pending_join_dev_addr);
3422 if (join == 0 &&
3423 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3424 wpa_s->auto_pd_scan_retry++;
3425 bss = wpa_bss_get_bssid(wpa_s,
3426 wpa_s->pending_join_dev_addr);
3427 if (bss) {
3428 freq = bss->freq;
3429 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3430 "the peer " MACSTR " at %d MHz",
3431 wpa_s->auto_pd_scan_retry,
3432 MAC2STR(wpa_s->
3433 pending_join_dev_addr),
3434 freq);
3435 wpas_p2p_join_scan_req(wpa_s, freq);
3436 return;
3437 }
3438 }
3439
3440 if (join < 0)
3441 join = 0;
3442
3443 wpa_s->p2p_auto_pd = 0;
3444 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3445 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3446 MAC2STR(wpa_s->pending_join_dev_addr), join);
3447 if (p2p_prov_disc_req(wpa_s->global->p2p,
3448 wpa_s->pending_join_dev_addr,
3449 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003450 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003451 wpa_s->p2p_auto_pd = 0;
3452 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3453 " p2p_dev_addr=" MACSTR " status=N/A",
3454 MAC2STR(wpa_s->pending_join_dev_addr));
3455 }
3456 return;
3457 }
3458
3459 if (wpa_s->p2p_auto_join) {
3460 int join = wpas_p2p_peer_go(wpa_s,
3461 wpa_s->pending_join_dev_addr);
3462 if (join < 0) {
3463 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3464 "running a GO -> use GO Negotiation");
3465 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3466 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3467 wpa_s->p2p_persistent_group, 0, 0, 0,
3468 wpa_s->p2p_go_intent,
3469 wpa_s->p2p_connect_freq,
3470 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003471 wpa_s->p2p_pd_before_go_neg,
3472 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003473 return;
3474 }
3475
3476 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3477 "try to join the group", join ? "" :
3478 " in older scan");
3479 if (!join)
3480 wpa_s->p2p_fallback_to_go_neg = 1;
3481 }
3482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003483 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3484 wpa_s->pending_join_iface_addr);
3485 if (freq < 0 &&
3486 p2p_get_interface_addr(wpa_s->global->p2p,
3487 wpa_s->pending_join_dev_addr,
3488 iface_addr) == 0 &&
3489 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3490 {
3491 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3492 "address for join from " MACSTR " to " MACSTR
3493 " based on newly discovered P2P peer entry",
3494 MAC2STR(wpa_s->pending_join_iface_addr),
3495 MAC2STR(iface_addr));
3496 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3497 ETH_ALEN);
3498
3499 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3500 wpa_s->pending_join_iface_addr);
3501 }
3502 if (freq >= 0) {
3503 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3504 "from P2P peer table: %d MHz", freq);
3505 }
3506 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3507 if (bss) {
3508 freq = bss->freq;
3509 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3510 "from BSS table: %d MHz", freq);
3511 }
3512 if (freq > 0) {
3513 u16 method;
3514
Dmitry Shmidt04949592012-07-19 12:16:46 -07003515 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3516 wpa_msg(wpa_s->parent, MSG_INFO,
3517 P2P_EVENT_GROUP_FORMATION_FAILURE
3518 "reason=FREQ_CONFLICT");
3519 return;
3520 }
3521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003522 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3523 "prior to joining an existing group (GO " MACSTR
3524 " freq=%u MHz)",
3525 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3526 wpa_s->pending_pd_before_join = 1;
3527
3528 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529 case WPS_PIN_DISPLAY:
3530 method = WPS_CONFIG_KEYPAD;
3531 break;
3532 case WPS_PIN_KEYPAD:
3533 method = WPS_CONFIG_DISPLAY;
3534 break;
3535 case WPS_PBC:
3536 method = WPS_CONFIG_PUSHBUTTON;
3537 break;
3538 default:
3539 method = 0;
3540 break;
3541 }
3542
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003543 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3544 wpa_s->pending_join_dev_addr) ==
3545 method)) {
3546 /*
3547 * We have already performed provision discovery for
3548 * joining the group. Proceed directly to join
3549 * operation without duplicated provision discovery. */
3550 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3551 "with " MACSTR " already done - proceed to "
3552 "join",
3553 MAC2STR(wpa_s->pending_join_dev_addr));
3554 wpa_s->pending_pd_before_join = 0;
3555 goto start;
3556 }
3557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003558 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003559 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003560 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3562 "Discovery Request before joining an "
3563 "existing group");
3564 wpa_s->pending_pd_before_join = 0;
3565 goto start;
3566 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567 return;
3568 }
3569
3570 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3571 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3572 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3573 wpas_p2p_check_join_scan_limit(wpa_s);
3574 return;
3575
3576start:
3577 /* Start join operation immediately */
3578 wpas_p2p_join_start(wpa_s);
3579}
3580
3581
Dmitry Shmidt04949592012-07-19 12:16:46 -07003582static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584 int ret;
3585 struct wpa_driver_scan_params params;
3586 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003587 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003588 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003589#ifdef ANDROID_P2P
3590 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003591
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003592 /* If freq is not provided, check the operating freq of the GO and do a
3593 * a directed scan to save time
3594 */
3595 if(!freq) {
3596 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3597 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3598 }
3599#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600 os_memset(&params, 0, sizeof(params));
3601
3602 /* P2P Wildcard SSID */
3603 params.num_ssids = 1;
3604 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3605 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3606
3607 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003608 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3609 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3610 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 if (wps_ie == NULL) {
3612 wpas_p2p_scan_res_join(wpa_s, NULL);
3613 return;
3614 }
3615
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003616 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3617 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618 if (ies == NULL) {
3619 wpabuf_free(wps_ie);
3620 wpas_p2p_scan_res_join(wpa_s, NULL);
3621 return;
3622 }
3623 wpabuf_put_buf(ies, wps_ie);
3624 wpabuf_free(wps_ie);
3625
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003626 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003627
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003628 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 params.extra_ies = wpabuf_head(ies);
3630 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003631 if (freq > 0) {
3632 freqs[0] = freq;
3633 params.freqs = freqs;
3634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003635
3636 /*
3637 * Run a scan to update BSS table and start Provision Discovery once
3638 * the new scan results become available.
3639 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003640 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003641 if (!ret)
3642 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003643
3644 wpabuf_free(ies);
3645
3646 if (ret) {
3647 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3648 "try again later");
3649 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3650 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3651 wpas_p2p_check_join_scan_limit(wpa_s);
3652 }
3653}
3654
3655
Dmitry Shmidt04949592012-07-19 12:16:46 -07003656static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3657{
3658 struct wpa_supplicant *wpa_s = eloop_ctx;
3659 wpas_p2p_join_scan_req(wpa_s, 0);
3660}
3661
3662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003663static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003664 const u8 *dev_addr, enum p2p_wps_method wps_method,
3665 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666{
3667 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003668 MACSTR " dev " MACSTR ")%s",
3669 MAC2STR(iface_addr), MAC2STR(dev_addr),
3670 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671
Dmitry Shmidt04949592012-07-19 12:16:46 -07003672 wpa_s->p2p_auto_pd = 0;
3673 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003674 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3675 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3676 wpa_s->pending_join_wps_method = wps_method;
3677
3678 /* Make sure we are not running find during connection establishment */
3679 wpas_p2p_stop_find(wpa_s);
3680
3681 wpa_s->p2p_join_scan_count = 0;
3682 wpas_p2p_join_scan(wpa_s, NULL);
3683 return 0;
3684}
3685
3686
3687static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3688{
3689 struct wpa_supplicant *group;
3690 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003691 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692
3693 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3694 if (group == NULL)
3695 return -1;
3696 if (group != wpa_s) {
3697 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3698 sizeof(group->p2p_pin));
3699 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003700 } else {
3701 /*
3702 * Need to mark the current interface for p2p_group_formation
3703 * when a separate group interface is not used. This is needed
3704 * to allow p2p_cancel stop a pending p2p_connect-join.
3705 * wpas_p2p_init_group_interface() addresses this for the case
3706 * where a separate group interface is used.
3707 */
3708 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709 }
3710
3711 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003712 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003713
3714 os_memset(&res, 0, sizeof(res));
3715 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3716 ETH_ALEN);
3717 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003718 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3719 if (bss) {
3720 res.freq = bss->freq;
3721 res.ssid_len = bss->ssid_len;
3722 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3723 }
3724
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003725 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3726 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3727 "starting client");
3728 wpa_drv_cancel_remain_on_channel(wpa_s);
3729 wpa_s->off_channel_freq = 0;
3730 wpa_s->roc_waiting_drv_freq = 0;
3731 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732 wpas_start_wps_enrollee(group, &res);
3733
3734 /*
3735 * Allow a longer timeout for join-a-running-group than normal 15
3736 * second group formation timeout since the GO may not have authorized
3737 * our connection yet.
3738 */
3739 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3740 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3741 wpa_s, NULL);
3742
3743 return 0;
3744}
3745
3746
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003747static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3748 int *force_freq, int *pref_freq,
3749 int *oper_freq)
3750{
3751 if (freq > 0) {
3752 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3753 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3754 "(%u MHz) is not supported for P2P uses",
3755 freq);
3756 return -3;
3757 }
3758
3759 if (*oper_freq > 0 && freq != *oper_freq &&
3760 !(wpa_s->drv_flags &
3761 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3762 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3763 "on %u MHz while connected on another "
3764 "channel (%u MHz)", freq, *oper_freq);
3765 return -2;
3766 }
3767 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3768 "requested channel (%u MHz)", freq);
3769 *force_freq = freq;
3770 } else if (*oper_freq > 0 &&
3771 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3772 if (!(wpa_s->drv_flags &
3773 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3774 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3775 "while connected on non-P2P supported "
3776 "channel (%u MHz)", *oper_freq);
3777 return -2;
3778 }
3779 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3780 "(%u MHz) not available for P2P - try to use "
3781 "another channel", *oper_freq);
3782 *force_freq = 0;
3783 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3784 (wpa_s->drv_flags &
3785 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3786 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3787 "are already using (%u MHz) on another interface",
3788 *oper_freq);
3789 *pref_freq = *oper_freq;
3790 } else if (*oper_freq > 0) {
3791 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3792 "channel we are already using (%u MHz) on another "
3793 "interface", *oper_freq);
3794 *force_freq = *oper_freq;
3795 }
3796
3797 return 0;
3798}
3799
3800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801/**
3802 * wpas_p2p_connect - Request P2P Group Formation to be started
3803 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3804 * @peer_addr: Address of the peer P2P Device
3805 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3806 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003807 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003808 * @join: Whether to join an existing group (as a client) instead of starting
3809 * Group Owner negotiation; @peer_addr is BSSID in that case
3810 * @auth: Whether to only authorize the connection instead of doing that and
3811 * initiating Group Owner negotiation
3812 * @go_intent: GO Intent or -1 to use default
3813 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003814 * @persistent_id: Persistent group credentials to use for forcing GO
3815 * parameters or -1 to generate new values (SSID/passphrase)
3816 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3817 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003818 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003819 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3820 * failure, -2 on failure due to channel not currently available,
3821 * -3 if forced channel is not supported
3822 */
3823int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3824 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003825 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003826 int go_intent, int freq, int persistent_id, int pd,
3827 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003828{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003829 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003830 u8 bssid[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003831 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003833 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003834 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835
3836 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3837 return -1;
3838
Dmitry Shmidt04949592012-07-19 12:16:46 -07003839 if (persistent_id >= 0) {
3840 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3841 if (ssid == NULL || ssid->disabled != 2 ||
3842 ssid->mode != WPAS_MODE_P2P_GO)
3843 return -1;
3844 }
3845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003846 if (go_intent < 0)
3847 go_intent = wpa_s->conf->p2p_go_intent;
3848
3849 if (!auth)
3850 wpa_s->p2p_long_listen = 0;
3851
3852 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003853 wpa_s->p2p_persistent_group = !!persistent_group;
3854 wpa_s->p2p_persistent_id = persistent_id;
3855 wpa_s->p2p_go_intent = go_intent;
3856 wpa_s->p2p_connect_freq = freq;
3857 wpa_s->p2p_fallback_to_go_neg = 0;
3858 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003859 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003860
3861 if (pin)
3862 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3863 else if (wps_method == WPS_PIN_DISPLAY) {
3864 ret = wps_generate_pin();
3865 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3866 ret);
3867 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3868 wpa_s->p2p_pin);
3869 } else
3870 wpa_s->p2p_pin[0] = '\0';
3871
Dmitry Shmidt04949592012-07-19 12:16:46 -07003872 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003873 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3874 if (auth) {
3875 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3876 "connect a running group from " MACSTR,
3877 MAC2STR(peer_addr));
3878 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3879 return ret;
3880 }
3881 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3882 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3883 iface_addr) < 0) {
3884 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3885 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3886 dev_addr);
3887 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003888 if (auto_join) {
3889 os_get_time(&wpa_s->p2p_auto_started);
3890 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3891 "%ld.%06ld",
3892 wpa_s->p2p_auto_started.sec,
3893 wpa_s->p2p_auto_started.usec);
3894 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003895 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003896 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3897 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898 return -1;
3899 return ret;
3900 }
3901
3902 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003903 wpa_s->assoc_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003905 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003906 oper_freq = wpa_drv_shared_freq(wpa_s);
3907 if (oper_freq < 0)
3908 oper_freq = 0;
3909 }
3910
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003911 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
3912 &oper_freq);
3913 if (res)
3914 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003915 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003916
3917 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3918
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003919 if (wpa_s->create_p2p_iface) {
3920 /* Prepare to add a new interface for the group */
3921 iftype = WPA_IF_P2P_GROUP;
3922 if (go_intent == 15)
3923 iftype = WPA_IF_P2P_GO;
3924 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3925 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3926 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003927 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003929
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003930 if_addr = wpa_s->pending_interface_addr;
3931 } else
3932 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933
3934 if (auth) {
3935 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003936 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003937 force_freq, persistent_group, ssid,
3938 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939 return -1;
3940 return ret;
3941 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003942
3943 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3944 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003945 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003946 if (wpa_s->create_p2p_iface)
3947 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948 return -1;
3949 }
3950 return ret;
3951}
3952
3953
3954/**
3955 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3956 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3957 * @freq: Frequency of the channel in MHz
3958 * @duration: Duration of the stay on the channel in milliseconds
3959 *
3960 * This callback is called when the driver indicates that it has started the
3961 * requested remain-on-channel duration.
3962 */
3963void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3964 unsigned int freq, unsigned int duration)
3965{
3966 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3967 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3969 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3970 wpa_s->pending_listen_duration);
3971 wpa_s->pending_listen_freq = 0;
3972 }
3973}
3974
3975
3976static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3977 unsigned int timeout)
3978{
3979 /* Limit maximum Listen state time based on driver limitation. */
3980 if (timeout > wpa_s->max_remain_on_chan)
3981 timeout = wpa_s->max_remain_on_chan;
3982
3983 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3984 return wpa_drv_p2p_listen(wpa_s, timeout);
3985
3986 return p2p_listen(wpa_s->global->p2p, timeout);
3987}
3988
3989
3990/**
3991 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3992 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3993 * @freq: Frequency of the channel in MHz
3994 *
3995 * This callback is called when the driver indicates that a remain-on-channel
3996 * operation has been completed, i.e., the duration on the requested channel
3997 * has timed out.
3998 */
3999void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4000 unsigned int freq)
4001{
4002 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4003 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004004 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004005 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4006 return;
4007 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4008 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004009 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004010 return;
4011 if (wpa_s->p2p_long_listen > 0)
4012 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4013 if (wpa_s->p2p_long_listen > 0) {
4014 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4015 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4016 }
4017}
4018
4019
4020/**
4021 * wpas_p2p_group_remove - Remove a P2P group
4022 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4023 * @ifname: Network interface name of the group interface or "*" to remove all
4024 * groups
4025 * Returns: 0 on success, -1 on failure
4026 *
4027 * This function is used to remove a P2P group. This can be used to disconnect
4028 * from a group in which the local end is a P2P Client or to end a P2P Group in
4029 * case the local end is the Group Owner. If a virtual network interface was
4030 * created for this group, that interface will be removed. Otherwise, only the
4031 * configured P2P group network will be removed from the interface.
4032 */
4033int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4034{
4035 struct wpa_global *global = wpa_s->global;
4036
4037 if (os_strcmp(ifname, "*") == 0) {
4038 struct wpa_supplicant *prev;
4039 wpa_s = global->ifaces;
4040 while (wpa_s) {
4041 prev = wpa_s;
4042 wpa_s = wpa_s->next;
4043 wpas_p2p_disconnect(prev);
4044 }
4045 return 0;
4046 }
4047
4048 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4049 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4050 break;
4051 }
4052
4053 return wpas_p2p_disconnect(wpa_s);
4054}
4055
4056
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004057static int freq_included(const struct p2p_channels *channels, unsigned int freq)
4058{
4059 if (channels == NULL)
4060 return 1; /* Assume no restrictions */
4061 return p2p_channels_includes_freq(channels, freq);
4062}
4063
4064
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004065static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4066 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004067 int freq, int ht40,
4068 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069{
4070 u8 bssid[ETH_ALEN];
4071 int res;
4072
4073 os_memset(params, 0, sizeof(*params));
4074 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004075 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004077 if (!freq_included(channels, freq)) {
4078 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4079 "accepted", freq);
4080 return -1;
4081 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4083 "frequency %d MHz", freq);
4084 params->freq = freq;
4085 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4086 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004087 wpa_s->conf->p2p_oper_channel <= 11 &&
4088 freq_included(channels,
4089 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4091 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4092 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004093 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4094 wpa_s->conf->p2p_oper_reg_class == 116 ||
4095 wpa_s->conf->p2p_oper_reg_class == 117 ||
4096 wpa_s->conf->p2p_oper_reg_class == 124 ||
4097 wpa_s->conf->p2p_oper_reg_class == 126 ||
4098 wpa_s->conf->p2p_oper_reg_class == 127) &&
4099 freq_included(channels,
4100 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004101 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4102 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4103 "frequency %d MHz", params->freq);
4104 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4105 wpa_s->best_overall_freq > 0 &&
4106 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004107 wpa_s->best_overall_freq) &&
4108 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004109 params->freq = wpa_s->best_overall_freq;
4110 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4111 "channel %d MHz", params->freq);
4112 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4113 wpa_s->best_24_freq > 0 &&
4114 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004115 wpa_s->best_24_freq) &&
4116 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117 params->freq = wpa_s->best_24_freq;
4118 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4119 "channel %d MHz", params->freq);
4120 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4121 wpa_s->best_5_freq > 0 &&
4122 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004123 wpa_s->best_5_freq) &&
4124 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125 params->freq = wpa_s->best_5_freq;
4126 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4127 "channel %d MHz", params->freq);
4128 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004129 int chan;
4130 for (chan = 0; chan < 11; chan++) {
4131 params->freq = 2412 + chan * 5;
4132 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004133 params->freq) &&
4134 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004135 break;
4136 }
4137 if (chan == 11) {
4138 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4139 "allowed");
4140 return -1;
4141 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004142 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4143 "known)", params->freq);
4144 }
4145
4146 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4147 wpa_s->assoc_freq && !freq) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004148 if (!p2p_supported_freq(wpa_s->global->p2p, wpa_s->assoc_freq)
4149 || !freq_included(channels, wpa_s->assoc_freq)) {
4150 if (wpa_s->drv_flags &
4151 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4152 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4153 "the channel we are already using "
4154 "(%u MHz) - allow multi-channel "
4155 "concurrency", wpa_s->assoc_freq);
4156 } else {
4157 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4158 "the channel we are already using "
4159 "(%u MHz)", wpa_s->assoc_freq);
4160 return -1;
4161 }
4162 } else {
4163 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we "
4164 "are already using (%u MHz)",
4165 wpa_s->assoc_freq);
4166 params->freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004167 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004168 }
4169
4170 res = wpa_drv_shared_freq(wpa_s);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004171 if (res > 0 && !freq &&
4172 (!p2p_supported_freq(wpa_s->global->p2p, res) ||
4173 !freq_included(channels, res))) {
4174 if (wpa_s->drv_flags &
4175 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4176 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4177 "channel we are already using on a shared "
4178 "interface (%u MHz) - allow multi-channel "
4179 "concurrency", res);
4180 } else {
4181 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4182 "channel we are already using on a shared "
4183 "interface (%u MHz)", res);
4184 return -1;
4185 }
4186 } else if (res > 0 && !freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004187 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4188 "already using on a shared interface");
4189 params->freq = res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004190 if (!freq_included(channels, params->freq)) {
4191 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4192 "accepted", params->freq);
4193 return -1;
4194 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004195 } else if (res > 0 && freq != res &&
4196 !(wpa_s->drv_flags &
4197 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4198 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4199 "while connected on another channel (%u MHz)",
4200 freq, res);
4201 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004202 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004203
4204 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004205}
4206
4207
4208static struct wpa_supplicant *
4209wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4210 int go)
4211{
4212 struct wpa_supplicant *group_wpa_s;
4213
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004214 if (!wpas_p2p_create_iface(wpa_s)) {
4215 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4216 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004217 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004218 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004219
4220 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004221 WPA_IF_P2P_CLIENT) < 0) {
4222 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004223 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004224 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4226 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004227 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4228 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004229 wpas_p2p_remove_pending_group_interface(wpa_s);
4230 return NULL;
4231 }
4232
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004233 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4234 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004235 return group_wpa_s;
4236}
4237
4238
4239/**
4240 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4241 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4242 * @persistent_group: Whether to create a persistent group
4243 * @freq: Frequency for the group or 0 to indicate no hardcoding
4244 * Returns: 0 on success, -1 on failure
4245 *
4246 * This function creates a new P2P group with the local end as the Group Owner,
4247 * i.e., without using Group Owner Negotiation.
4248 */
4249int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004250 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251{
4252 struct p2p_go_neg_results params;
4253 unsigned int r;
4254
4255 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4256 return -1;
4257
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004258 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004259 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004260 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004262 if (freq == 2) {
4263 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4264 "band");
4265 if (wpa_s->best_24_freq > 0 &&
4266 p2p_supported_freq(wpa_s->global->p2p,
4267 wpa_s->best_24_freq)) {
4268 freq = wpa_s->best_24_freq;
4269 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4270 "channel: %d MHz", freq);
4271 } else {
4272 os_get_random((u8 *) &r, sizeof(r));
4273 freq = 2412 + (r % 3) * 25;
4274 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4275 "channel: %d MHz", freq);
4276 }
4277 }
4278
4279 if (freq == 5) {
4280 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4281 "band");
4282 if (wpa_s->best_5_freq > 0 &&
4283 p2p_supported_freq(wpa_s->global->p2p,
4284 wpa_s->best_5_freq)) {
4285 freq = wpa_s->best_5_freq;
4286 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4287 "channel: %d MHz", freq);
4288 } else {
4289 os_get_random((u8 *) &r, sizeof(r));
4290 freq = 5180 + (r % 4) * 20;
4291 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4292 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4293 "5 GHz channel for P2P group");
4294 return -1;
4295 }
4296 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4297 "channel: %d MHz", freq);
4298 }
4299 }
4300
4301 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4302 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4303 "(%u MHz) is not supported for P2P uses",
4304 freq);
4305 return -1;
4306 }
4307
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004308 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004309 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004310 if (params.freq &&
4311 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4312 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4313 "(%u MHz) is not supported for P2P uses",
4314 params.freq);
4315 return -1;
4316 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 p2p_go_params(wpa_s->global->p2p, &params);
4318 params.persistent_group = persistent_group;
4319
4320 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4321 if (wpa_s == NULL)
4322 return -1;
4323 wpas_start_wps_go(wpa_s, &params, 0);
4324
4325 return 0;
4326}
4327
4328
4329static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4330 struct wpa_ssid *params, int addr_allocated)
4331{
4332 struct wpa_ssid *ssid;
4333
4334 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4335 if (wpa_s == NULL)
4336 return -1;
4337
4338 wpa_supplicant_ap_deinit(wpa_s);
4339
4340 ssid = wpa_config_add_network(wpa_s->conf);
4341 if (ssid == NULL)
4342 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004343 wpa_config_set_network_defaults(ssid);
4344 ssid->temporary = 1;
4345 ssid->proto = WPA_PROTO_RSN;
4346 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4347 ssid->group_cipher = WPA_CIPHER_CCMP;
4348 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4349 ssid->ssid = os_malloc(params->ssid_len);
4350 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004351 wpa_config_remove_network(wpa_s->conf, ssid->id);
4352 return -1;
4353 }
4354 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4355 ssid->ssid_len = params->ssid_len;
4356 ssid->p2p_group = 1;
4357 ssid->export_keys = 1;
4358 if (params->psk_set) {
4359 os_memcpy(ssid->psk, params->psk, 32);
4360 ssid->psk_set = 1;
4361 }
4362 if (params->passphrase)
4363 ssid->passphrase = os_strdup(params->passphrase);
4364
4365 wpa_supplicant_select_network(wpa_s, ssid);
4366
4367 wpa_s->show_group_started = 1;
4368
4369 return 0;
4370}
4371
4372
4373int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4374 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004375 int freq, int ht40,
4376 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004377{
4378 struct p2p_go_neg_results params;
4379 int go = 0;
4380
4381 if (ssid->disabled != 2 || ssid->ssid == NULL)
4382 return -1;
4383
4384 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4385 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4386 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4387 "already running");
4388 return 0;
4389 }
4390
4391 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004392 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004393
Dmitry Shmidt04949592012-07-19 12:16:46 -07004394 wpa_s->p2p_fallback_to_go_neg = 0;
4395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004396 if (ssid->mode == WPAS_MODE_INFRA)
4397 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4398
4399 if (ssid->mode != WPAS_MODE_P2P_GO)
4400 return -1;
4401
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004402 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004403 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404
4405 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004406 params.psk_set = ssid->psk_set;
4407 if (params.psk_set)
4408 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004409 if (ssid->passphrase) {
4410 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4411 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4412 "persistent group");
4413 return -1;
4414 }
4415 os_strlcpy(params.passphrase, ssid->passphrase,
4416 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004418 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4419 params.ssid_len = ssid->ssid_len;
4420 params.persistent_group = 1;
4421
4422 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4423 if (wpa_s == NULL)
4424 return -1;
4425
4426 wpas_start_wps_go(wpa_s, &params, 0);
4427
4428 return 0;
4429}
4430
4431
4432static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4433 struct wpabuf *proberesp_ies)
4434{
4435 struct wpa_supplicant *wpa_s = ctx;
4436 if (wpa_s->ap_iface) {
4437 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004438 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4439 wpabuf_free(beacon_ies);
4440 wpabuf_free(proberesp_ies);
4441 return;
4442 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004443 if (beacon_ies) {
4444 wpabuf_free(hapd->p2p_beacon_ie);
4445 hapd->p2p_beacon_ie = beacon_ies;
4446 }
4447 wpabuf_free(hapd->p2p_probe_resp_ie);
4448 hapd->p2p_probe_resp_ie = proberesp_ies;
4449 } else {
4450 wpabuf_free(beacon_ies);
4451 wpabuf_free(proberesp_ies);
4452 }
4453 wpa_supplicant_ap_update_beacon(wpa_s);
4454}
4455
4456
4457static void wpas_p2p_idle_update(void *ctx, int idle)
4458{
4459 struct wpa_supplicant *wpa_s = ctx;
4460 if (!wpa_s->ap_iface)
4461 return;
4462 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004463 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004464 wpas_p2p_set_group_idle_timeout(wpa_s);
4465 else
4466 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4467}
4468
4469
4470struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004471 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472{
4473 struct p2p_group *group;
4474 struct p2p_group_config *cfg;
4475
4476 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4477 return NULL;
4478 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4479 return NULL;
4480
4481 cfg = os_zalloc(sizeof(*cfg));
4482 if (cfg == NULL)
4483 return NULL;
4484
Dmitry Shmidt04949592012-07-19 12:16:46 -07004485 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004486 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004487 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004488 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4490 if (wpa_s->max_stations &&
4491 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4492 cfg->max_clients = wpa_s->max_stations;
4493 else
4494 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004495 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4496 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 cfg->cb_ctx = wpa_s;
4498 cfg->ie_update = wpas_p2p_ie_update;
4499 cfg->idle_update = wpas_p2p_idle_update;
4500
4501 group = p2p_group_init(wpa_s->global->p2p, cfg);
4502 if (group == NULL)
4503 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004504 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 p2p_group_notif_formation_done(group);
4506 wpa_s->p2p_group = group;
4507 return group;
4508}
4509
4510
4511void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4512 int registrar)
4513{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004514 struct wpa_ssid *ssid = wpa_s->current_ssid;
4515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004516 if (!wpa_s->p2p_in_provisioning) {
4517 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4518 "provisioning not in progress");
4519 return;
4520 }
4521
Dmitry Shmidt04949592012-07-19 12:16:46 -07004522 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4523 u8 go_dev_addr[ETH_ALEN];
4524 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4525 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4526 ssid->ssid_len);
4527 /* Clear any stored provisioning info */
4528 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4529 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4532 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004533 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4534 /*
4535 * Use a separate timeout for initial data connection to
4536 * complete to allow the group to be removed automatically if
4537 * something goes wrong in this step before the P2P group idle
4538 * timeout mechanism is taken into use.
4539 */
4540 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4541 wpas_p2p_group_formation_timeout,
4542 wpa_s->parent, NULL);
4543 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544 if (wpa_s->global->p2p)
4545 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4546 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4547 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4548 wpas_group_formation_completed(wpa_s, 1);
4549}
4550
4551
Jouni Malinen75ecf522011-06-27 15:19:46 -07004552void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4553 struct wps_event_fail *fail)
4554{
4555 if (!wpa_s->p2p_in_provisioning) {
4556 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4557 "provisioning not in progress");
4558 return;
4559 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004560
4561 if (wpa_s->go_params) {
4562 p2p_clear_provisioning_info(
4563 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004564 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004565 }
4566
Jouni Malinen75ecf522011-06-27 15:19:46 -07004567 wpas_notify_p2p_wps_failed(wpa_s, fail);
4568}
4569
4570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004572 const char *config_method,
4573 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004574{
4575 u16 config_methods;
4576
Dmitry Shmidt04949592012-07-19 12:16:46 -07004577 wpa_s->p2p_fallback_to_go_neg = 0;
4578 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004579 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004581 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004583 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4584 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004585 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004586 else {
4587 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004588 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004589 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004590
Dmitry Shmidt04949592012-07-19 12:16:46 -07004591 if (use == WPAS_P2P_PD_AUTO) {
4592 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4593 wpa_s->pending_pd_config_methods = config_methods;
4594 wpa_s->p2p_auto_pd = 1;
4595 wpa_s->p2p_auto_join = 0;
4596 wpa_s->pending_pd_before_join = 0;
4597 wpa_s->auto_pd_scan_retry = 0;
4598 wpas_p2p_stop_find(wpa_s);
4599 wpa_s->p2p_join_scan_count = 0;
4600 os_get_time(&wpa_s->p2p_auto_started);
4601 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4602 wpa_s->p2p_auto_started.sec,
4603 wpa_s->p2p_auto_started.usec);
4604 wpas_p2p_join_scan(wpa_s, NULL);
4605 return 0;
4606 }
4607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4609 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004610 config_methods,
4611 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612 }
4613
4614 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4615 return -1;
4616
4617 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004618 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004619 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620}
4621
4622
4623int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4624 char *end)
4625{
4626 return p2p_scan_result_text(ies, ies_len, buf, end);
4627}
4628
4629
4630static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4631{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004632 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633 return;
4634
4635 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4636 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004637 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004638}
4639
4640
4641int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4642 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004643 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004644 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645{
4646 wpas_p2p_clear_pending_action_tx(wpa_s);
4647 wpa_s->p2p_long_listen = 0;
4648
4649 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4650 return wpa_drv_p2p_find(wpa_s, timeout, type);
4651
Dmitry Shmidt04949592012-07-19 12:16:46 -07004652 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4653 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004654 return -1;
4655
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004656 wpa_supplicant_cancel_sched_scan(wpa_s);
4657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004659 num_req_dev_types, req_dev_types, dev_id,
4660 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004661}
4662
4663
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004664static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665{
4666 wpas_p2p_clear_pending_action_tx(wpa_s);
4667 wpa_s->p2p_long_listen = 0;
4668 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4669 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004670 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671
4672 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4673 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004674 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004675 }
4676
4677 if (wpa_s->global->p2p)
4678 p2p_stop_find(wpa_s->global->p2p);
4679
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004680 return 0;
4681}
4682
4683
4684void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4685{
4686 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4687 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004688 wpas_p2p_remove_pending_group_interface(wpa_s);
4689}
4690
4691
4692static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4693{
4694 struct wpa_supplicant *wpa_s = eloop_ctx;
4695 wpa_s->p2p_long_listen = 0;
4696}
4697
4698
4699int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4700{
4701 int res;
4702
4703 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4704 return -1;
4705
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004706 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707 wpas_p2p_clear_pending_action_tx(wpa_s);
4708
4709 if (timeout == 0) {
4710 /*
4711 * This is a request for unlimited Listen state. However, at
4712 * least for now, this is mapped to a Listen state for one
4713 * hour.
4714 */
4715 timeout = 3600;
4716 }
4717 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4718 wpa_s->p2p_long_listen = 0;
4719
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004720 /*
4721 * Stop previous find/listen operation to avoid trying to request a new
4722 * remain-on-channel operation while the driver is still running the
4723 * previous one.
4724 */
4725 if (wpa_s->global->p2p)
4726 p2p_stop_find(wpa_s->global->p2p);
4727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004728 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4729 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4730 wpa_s->p2p_long_listen = timeout * 1000;
4731 eloop_register_timeout(timeout, 0,
4732 wpas_p2p_long_listen_timeout,
4733 wpa_s, NULL);
4734 }
4735
4736 return res;
4737}
4738
4739
4740int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4741 u8 *buf, size_t len, int p2p_group)
4742{
4743 struct wpabuf *p2p_ie;
4744 int ret;
4745
4746 if (wpa_s->global->p2p_disabled)
4747 return -1;
4748 if (wpa_s->global->p2p == NULL)
4749 return -1;
4750 if (bss == NULL)
4751 return -1;
4752
4753 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4754 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4755 p2p_group, p2p_ie);
4756 wpabuf_free(p2p_ie);
4757
4758 return ret;
4759}
4760
4761
4762int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004763 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004764 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004765{
4766 if (wpa_s->global->p2p_disabled)
4767 return 0;
4768 if (wpa_s->global->p2p == NULL)
4769 return 0;
4770
Dmitry Shmidt04949592012-07-19 12:16:46 -07004771 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4772 ie, ie_len)) {
4773 case P2P_PREQ_NOT_P2P:
4774 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4775 ssi_signal);
4776 /* fall through */
4777 case P2P_PREQ_MALFORMED:
4778 case P2P_PREQ_NOT_LISTEN:
4779 case P2P_PREQ_NOT_PROCESSED:
4780 default: /* make gcc happy */
4781 return 0;
4782 case P2P_PREQ_PROCESSED:
4783 return 1;
4784 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785}
4786
4787
4788void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4789 const u8 *sa, const u8 *bssid,
4790 u8 category, const u8 *data, size_t len, int freq)
4791{
4792 if (wpa_s->global->p2p_disabled)
4793 return;
4794 if (wpa_s->global->p2p == NULL)
4795 return;
4796
4797 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4798 freq);
4799}
4800
4801
4802void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4803{
4804 if (wpa_s->global->p2p_disabled)
4805 return;
4806 if (wpa_s->global->p2p == NULL)
4807 return;
4808
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004809 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004810}
4811
4812
4813void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4814{
4815 p2p_group_deinit(wpa_s->p2p_group);
4816 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004817
4818 wpa_s->ap_configured_cb = NULL;
4819 wpa_s->ap_configured_cb_ctx = NULL;
4820 wpa_s->ap_configured_cb_data = NULL;
4821 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004822}
4823
4824
4825int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4826{
4827 wpa_s->p2p_long_listen = 0;
4828
4829 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4830 return wpa_drv_p2p_reject(wpa_s, addr);
4831
4832 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4833 return -1;
4834
4835 return p2p_reject(wpa_s->global->p2p, addr);
4836}
4837
4838
4839/* Invite to reinvoke a persistent group */
4840int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004841 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004842 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843{
4844 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004845 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004846 int force_freq = 0, oper_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004847 int res;
4848
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004849 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004850 if (peer_addr)
4851 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4852 else
4853 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854
Jouni Malinen31be0a42012-08-31 21:20:51 +03004855 wpa_s->p2p_persistent_go_freq = freq;
4856 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004857 if (ssid->mode == WPAS_MODE_P2P_GO) {
4858 role = P2P_INVITE_ROLE_GO;
4859 if (peer_addr == NULL) {
4860 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4861 "address in invitation command");
4862 return -1;
4863 }
4864 if (wpas_p2p_create_iface(wpa_s)) {
4865 if (wpas_p2p_add_group_interface(wpa_s,
4866 WPA_IF_P2P_GO) < 0) {
4867 wpa_printf(MSG_ERROR, "P2P: Failed to "
4868 "allocate a new interface for the "
4869 "group");
4870 return -1;
4871 }
4872 bssid = wpa_s->pending_interface_addr;
4873 } else
4874 bssid = wpa_s->own_addr;
4875 } else {
4876 role = P2P_INVITE_ROLE_CLIENT;
4877 peer_addr = ssid->bssid;
4878 }
4879 wpa_s->pending_invite_ssid_id = ssid->id;
4880
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004881 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4882 wpa_s->assoc_freq) {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004883 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004884 if (bssid == NULL)
4885 bssid = bssid_buf;
4886 } else {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004887 oper_freq = wpa_drv_shared_freq(wpa_s);
4888 if (oper_freq < 0)
4889 oper_freq = 0;
4890 }
4891
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004892 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4893 &oper_freq);
4894 if (res)
4895 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004897 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4898 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4899 ssid->ssid, ssid->ssid_len,
4900 go_dev_addr, 1);
4901
4902 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4903 return -1;
4904
4905 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004906 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4907 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908}
4909
4910
4911/* Invite to join an active group */
4912int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4913 const u8 *peer_addr, const u8 *go_dev_addr)
4914{
4915 struct wpa_global *global = wpa_s->global;
4916 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004917 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004918 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004919 int persistent;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004920 int force_freq = 0, oper_freq = 0, pref_freq = 0;
4921 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922
Jouni Malinen31be0a42012-08-31 21:20:51 +03004923 wpa_s->p2p_persistent_go_freq = 0;
4924 wpa_s->p2p_go_ht40 = 0;
4925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4927 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4928 break;
4929 }
4930 if (wpa_s == NULL) {
4931 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4932 return -1;
4933 }
4934
4935 ssid = wpa_s->current_ssid;
4936 if (ssid == NULL) {
4937 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4938 "invitation");
4939 return -1;
4940 }
4941
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004942 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004943 persistent = ssid->p2p_persistent_group &&
4944 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4945 ssid->ssid, ssid->ssid_len);
4946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947 if (ssid->mode == WPAS_MODE_P2P_GO) {
4948 role = P2P_INVITE_ROLE_ACTIVE_GO;
4949 bssid = wpa_s->own_addr;
4950 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004951 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 } else {
4953 role = P2P_INVITE_ROLE_CLIENT;
4954 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4955 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4956 "invite to current group");
4957 return -1;
4958 }
4959 bssid = wpa_s->bssid;
4960 if (go_dev_addr == NULL &&
4961 !is_zero_ether_addr(wpa_s->go_dev_addr))
4962 go_dev_addr = wpa_s->go_dev_addr;
4963 }
4964 wpa_s->parent->pending_invite_ssid_id = -1;
4965
4966 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4967 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4968 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004969 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004970
4971 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4972 return -1;
4973
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004974 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4975 wpa_s->assoc_freq) {
4976 oper_freq = wpa_s->assoc_freq;
4977 if (bssid == NULL)
4978 bssid = bssid_buf;
4979 } else {
4980 oper_freq = wpa_drv_shared_freq(wpa_s);
4981 if (oper_freq < 0)
4982 oper_freq = 0;
4983 }
4984
4985 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
4986 &oper_freq);
4987 if (res)
4988 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004989 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004992 ssid->ssid, ssid->ssid_len, force_freq,
4993 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004994}
4995
4996
4997void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4998{
4999 struct wpa_ssid *ssid = wpa_s->current_ssid;
5000 const char *ssid_txt;
5001 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005002 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005004 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005
Dmitry Shmidt04949592012-07-19 12:16:46 -07005006 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5007 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5008 wpa_s->parent, NULL);
5009 }
5010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005011 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005012 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005013
5014 wpa_s->show_group_started = 0;
5015
5016 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5017 os_memset(go_dev_addr, 0, ETH_ALEN);
5018 if (ssid->bssid_set)
5019 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5020 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5021 ssid->ssid_len);
5022 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5023
5024 if (wpa_s->global->p2p_group_formation == wpa_s)
5025 wpa_s->global->p2p_group_formation = NULL;
5026
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005027 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5028 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005029 if (ssid->passphrase == NULL && ssid->psk_set) {
5030 char psk[65];
5031 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5032 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5033 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
5034 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005035 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005036 MAC2STR(go_dev_addr),
5037 persistent ? " [PERSISTENT]" : "");
5038 } else {
5039 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5040 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
5041 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005042 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043 ssid->passphrase ? ssid->passphrase : "",
5044 MAC2STR(go_dev_addr),
5045 persistent ? " [PERSISTENT]" : "");
5046 }
5047
5048 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005049 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5050 ssid, go_dev_addr);
5051 if (network_id < 0)
5052 network_id = ssid->id;
5053 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005054
5055done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005056 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057}
5058
5059
5060int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5061 u32 interval1, u32 duration2, u32 interval2)
5062{
5063 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5064 return -1;
5065 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5066 return -1;
5067
5068 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5069 wpa_s->current_ssid == NULL ||
5070 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5071 return -1;
5072
5073 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5074 wpa_s->own_addr, wpa_s->assoc_freq,
5075 duration1, interval1, duration2, interval2);
5076}
5077
5078
5079int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5080 unsigned int interval)
5081{
5082 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5083 return -1;
5084
5085 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5086 return -1;
5087
5088 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5089}
5090
5091
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005092static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5093{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005094 if (wpa_s->current_ssid == NULL) {
5095 /*
5096 * current_ssid can be cleared when P2P client interface gets
5097 * disconnected, so assume this interface was used as P2P
5098 * client.
5099 */
5100 return 1;
5101 }
5102 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005103 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5104}
5105
5106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005107static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5108{
5109 struct wpa_supplicant *wpa_s = eloop_ctx;
5110
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005111 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5113 "disabled");
5114 return;
5115 }
5116
Dmitry Shmidt04949592012-07-19 12:16:46 -07005117 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5118 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005119 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120}
5121
5122
5123static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5124{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005125 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005126
Dmitry Shmidt04949592012-07-19 12:16:46 -07005127 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5128 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005130 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5131 return;
5132
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005133 timeout = wpa_s->conf->p2p_group_idle;
5134 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5135 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5136 timeout = P2P_MAX_CLIENT_IDLE;
5137
5138 if (timeout == 0)
5139 return;
5140
Dmitry Shmidt04949592012-07-19 12:16:46 -07005141 if (timeout < 0) {
5142 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5143 timeout = 0; /* special client mode no-timeout */
5144 else
5145 return;
5146 }
5147
5148 if (wpa_s->p2p_in_provisioning) {
5149 /*
5150 * Use the normal group formation timeout during the
5151 * provisioning phase to avoid terminating this process too
5152 * early due to group idle timeout.
5153 */
5154 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5155 "during provisioning");
5156 return;
5157 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07005158#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07005159 if (wpa_s->show_group_started) {
5160 /*
5161 * Use the normal group formation timeout between the end of
5162 * the provisioning phase and completion of 4-way handshake to
5163 * avoid terminating this process too early due to group idle
5164 * timeout.
5165 */
5166 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5167 "while waiting for initial 4-way handshake to "
5168 "complete");
5169 return;
5170 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07005171#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07005172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005174 timeout);
5175 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5176 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005177}
5178
5179
Jouni Malinen2b89da82012-08-31 22:04:41 +03005180/* Returns 1 if the interface was removed */
5181int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5182 u16 reason_code, const u8 *ie, size_t ie_len,
5183 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005184{
5185 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005186 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005187 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005188 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005189
Dmitry Shmidt04949592012-07-19 12:16:46 -07005190 if (!locally_generated)
5191 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5192 ie_len);
5193
5194 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5195 wpa_s->current_ssid &&
5196 wpa_s->current_ssid->p2p_group &&
5197 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5198 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5199 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005200 if (wpas_p2p_group_delete(wpa_s,
5201 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5202 > 0)
5203 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005204 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005205
5206 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005207}
5208
5209
5210void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005211 u16 reason_code, const u8 *ie, size_t ie_len,
5212 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005213{
5214 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5215 return;
5216 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5217 return;
5218
Dmitry Shmidt04949592012-07-19 12:16:46 -07005219 if (!locally_generated)
5220 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5221 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005222}
5223
5224
5225void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5226{
5227 struct p2p_data *p2p = wpa_s->global->p2p;
5228
5229 if (p2p == NULL)
5230 return;
5231
5232 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5233 return;
5234
5235 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5236 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5237
5238 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5239 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5240
5241 if (wpa_s->wps &&
5242 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5243 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5244
5245 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5246 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5247
5248 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5249 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5250 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5251 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5252 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5253 }
5254
5255 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5256 p2p_set_sec_dev_types(p2p,
5257 (void *) wpa_s->conf->sec_device_type,
5258 wpa_s->conf->num_sec_device_types);
5259
5260 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5261 int i;
5262 p2p_remove_wps_vendor_extensions(p2p);
5263 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5264 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5265 continue;
5266 p2p_add_wps_vendor_extension(
5267 p2p, wpa_s->conf->wps_vendor_ext[i]);
5268 }
5269 }
5270
5271 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5272 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5273 char country[3];
5274 country[0] = wpa_s->conf->country[0];
5275 country[1] = wpa_s->conf->country[1];
5276 country[2] = 0x04;
5277 p2p_set_country(p2p, country);
5278 }
5279
5280 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5281 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5282 wpa_s->conf->p2p_ssid_postfix ?
5283 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5284 0);
5285 }
5286
5287 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5288 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005289
5290 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5291 u8 reg_class, channel;
5292 int ret;
5293 unsigned int r;
5294 if (wpa_s->conf->p2p_listen_reg_class &&
5295 wpa_s->conf->p2p_listen_channel) {
5296 reg_class = wpa_s->conf->p2p_listen_reg_class;
5297 channel = wpa_s->conf->p2p_listen_channel;
5298 } else {
5299 reg_class = 81;
5300 /*
5301 * Pick one of the social channels randomly as the
5302 * listen channel.
5303 */
5304 os_get_random((u8 *) &r, sizeof(r));
5305 channel = 1 + (r % 3) * 5;
5306 }
5307 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5308 if (ret)
5309 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5310 "failed: %d", ret);
5311 }
5312 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5313 u8 op_reg_class, op_channel, cfg_op_channel;
5314 int ret = 0;
5315 unsigned int r;
5316 if (wpa_s->conf->p2p_oper_reg_class &&
5317 wpa_s->conf->p2p_oper_channel) {
5318 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5319 op_channel = wpa_s->conf->p2p_oper_channel;
5320 cfg_op_channel = 1;
5321 } else {
5322 op_reg_class = 81;
5323 /*
5324 * Use random operation channel from (1, 6, 11)
5325 *if no other preference is indicated.
5326 */
5327 os_get_random((u8 *) &r, sizeof(r));
5328 op_channel = 1 + (r % 3) * 5;
5329 cfg_op_channel = 0;
5330 }
5331 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5332 cfg_op_channel);
5333 if (ret)
5334 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5335 "failed: %d", ret);
5336 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005337
5338 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5339 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5340 wpa_s->conf->p2p_pref_chan) < 0) {
5341 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5342 "update failed");
5343 }
5344 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005345}
5346
5347
5348int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5349 int duration)
5350{
5351 if (!wpa_s->ap_iface)
5352 return -1;
5353 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5354 duration);
5355}
5356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005357
5358int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5359{
5360 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5361 return -1;
5362 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5363 return -1;
5364
5365 wpa_s->global->cross_connection = enabled;
5366 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5367
5368 if (!enabled) {
5369 struct wpa_supplicant *iface;
5370
5371 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5372 {
5373 if (iface->cross_connect_enabled == 0)
5374 continue;
5375
5376 iface->cross_connect_enabled = 0;
5377 iface->cross_connect_in_use = 0;
5378 wpa_msg(iface->parent, MSG_INFO,
5379 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5380 iface->ifname, iface->cross_connect_uplink);
5381 }
5382 }
5383
5384 return 0;
5385}
5386
5387
5388static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5389{
5390 struct wpa_supplicant *iface;
5391
5392 if (!uplink->global->cross_connection)
5393 return;
5394
5395 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5396 if (!iface->cross_connect_enabled)
5397 continue;
5398 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5399 0)
5400 continue;
5401 if (iface->ap_iface == NULL)
5402 continue;
5403 if (iface->cross_connect_in_use)
5404 continue;
5405
5406 iface->cross_connect_in_use = 1;
5407 wpa_msg(iface->parent, MSG_INFO,
5408 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5409 iface->ifname, iface->cross_connect_uplink);
5410 }
5411}
5412
5413
5414static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5415{
5416 struct wpa_supplicant *iface;
5417
5418 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5419 if (!iface->cross_connect_enabled)
5420 continue;
5421 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5422 0)
5423 continue;
5424 if (!iface->cross_connect_in_use)
5425 continue;
5426
5427 wpa_msg(iface->parent, MSG_INFO,
5428 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5429 iface->ifname, iface->cross_connect_uplink);
5430 iface->cross_connect_in_use = 0;
5431 }
5432}
5433
5434
5435void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5436{
5437 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5438 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5439 wpa_s->cross_connect_disallowed)
5440 wpas_p2p_disable_cross_connect(wpa_s);
5441 else
5442 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005443 if (!wpa_s->ap_iface &&
5444 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5445 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005446}
5447
5448
5449void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5450{
5451 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005452 if (!wpa_s->ap_iface &&
5453 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5454 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455 wpas_p2p_set_group_idle_timeout(wpa_s);
5456}
5457
5458
5459static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5460{
5461 struct wpa_supplicant *iface;
5462
5463 if (!wpa_s->global->cross_connection)
5464 return;
5465
5466 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5467 if (iface == wpa_s)
5468 continue;
5469 if (iface->drv_flags &
5470 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5471 continue;
5472 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5473 continue;
5474
5475 wpa_s->cross_connect_enabled = 1;
5476 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5477 sizeof(wpa_s->cross_connect_uplink));
5478 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5479 "%s to %s whenever uplink is available",
5480 wpa_s->ifname, wpa_s->cross_connect_uplink);
5481
5482 if (iface->ap_iface || iface->current_ssid == NULL ||
5483 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5484 iface->cross_connect_disallowed ||
5485 iface->wpa_state != WPA_COMPLETED)
5486 break;
5487
5488 wpa_s->cross_connect_in_use = 1;
5489 wpa_msg(wpa_s->parent, MSG_INFO,
5490 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5491 wpa_s->ifname, wpa_s->cross_connect_uplink);
5492 break;
5493 }
5494}
5495
5496
5497int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5498{
5499 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5500 !wpa_s->p2p_in_provisioning)
5501 return 0; /* not P2P client operation */
5502
5503 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5504 "session overlap");
5505 if (wpa_s != wpa_s->parent)
5506 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5507
5508 if (wpa_s->global->p2p)
5509 p2p_group_formation_failed(wpa_s->global->p2p);
5510
5511 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5512 wpa_s->parent, NULL);
5513
5514 wpas_group_formation_completed(wpa_s, 0);
5515 return 1;
5516}
5517
5518
5519void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5520{
5521 struct p2p_channels chan;
5522
5523 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5524 return;
5525
5526 os_memset(&chan, 0, sizeof(chan));
5527 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5528 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5529 "channel list");
5530 return;
5531 }
5532
5533 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5534}
5535
5536
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005537static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5538 struct wpa_scan_results *scan_res)
5539{
5540 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5541}
5542
5543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005544int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5545{
5546 struct wpa_global *global = wpa_s->global;
5547 int found = 0;
5548 const u8 *peer;
5549
5550 if (global->p2p == NULL)
5551 return -1;
5552
5553 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5554
5555 if (wpa_s->pending_interface_name[0] &&
5556 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5557 found = 1;
5558
5559 peer = p2p_get_go_neg_peer(global->p2p);
5560 if (peer) {
5561 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5562 MACSTR, MAC2STR(peer));
5563 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005564 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005565 }
5566
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005567 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5568 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5569 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5570 found = 1;
5571 }
5572
5573 if (wpa_s->pending_pd_before_join) {
5574 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5575 wpa_s->pending_pd_before_join = 0;
5576 found = 1;
5577 }
5578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005579 wpas_p2p_stop_find(wpa_s);
5580
5581 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5582 if (wpa_s == global->p2p_group_formation &&
5583 (wpa_s->p2p_in_provisioning ||
5584 wpa_s->parent->pending_interface_type ==
5585 WPA_IF_P2P_CLIENT)) {
5586 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5587 "formation found - cancelling",
5588 wpa_s->ifname);
5589 found = 1;
5590 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5591 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005592 if (wpa_s->p2p_in_provisioning) {
5593 wpas_group_formation_completed(wpa_s, 0);
5594 break;
5595 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005596 wpas_p2p_group_delete(wpa_s,
5597 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005598 break;
5599 }
5600 }
5601
5602 if (!found) {
5603 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5604 return -1;
5605 }
5606
5607 return 0;
5608}
5609
5610
5611void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5612{
5613 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5614 return;
5615
5616 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5617 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005618 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619}
5620
5621
5622void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5623 int freq_24, int freq_5, int freq_overall)
5624{
5625 struct p2p_data *p2p = wpa_s->global->p2p;
5626 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5627 return;
5628 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5629}
5630
5631
5632int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5633{
5634 u8 peer[ETH_ALEN];
5635 struct p2p_data *p2p = wpa_s->global->p2p;
5636
5637 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5638 return -1;
5639
5640 if (hwaddr_aton(addr, peer))
5641 return -1;
5642
5643 return p2p_unauthorize(p2p, peer);
5644}
5645
5646
5647/**
5648 * wpas_p2p_disconnect - Disconnect from a P2P Group
5649 * @wpa_s: Pointer to wpa_supplicant data
5650 * Returns: 0 on success, -1 on failure
5651 *
5652 * This can be used to disconnect from a group in which the local end is a P2P
5653 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5654 * virtual network interface was created for this group, that interface will be
5655 * removed. Otherwise, only the configured P2P group network will be removed
5656 * from the interface.
5657 */
5658int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5659{
5660
5661 if (wpa_s == NULL)
5662 return -1;
5663
Jouni Malinen2b89da82012-08-31 22:04:41 +03005664 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5665 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005666}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005667
5668
5669int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5670{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005671 int ret;
5672
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005673 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5674 return 0;
5675
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005676 ret = p2p_in_progress(wpa_s->global->p2p);
5677 if (ret == 0) {
5678 /*
5679 * Check whether there is an ongoing WPS provisioning step (or
5680 * other parts of group formation) on another interface since
5681 * p2p_in_progress() does not report this to avoid issues for
5682 * scans during such provisioning step.
5683 */
5684 if (wpa_s->global->p2p_group_formation &&
5685 wpa_s->global->p2p_group_formation != wpa_s) {
5686 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5687 "in group formation",
5688 wpa_s->global->p2p_group_formation->ifname);
5689 ret = 1;
5690 }
5691 }
5692
5693 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005694}
5695
5696
5697void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5698 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005699{
5700 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5701 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5702 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005703 /**
5704 * Remove the network by scheduling the group formation
5705 * timeout to happen immediately. The teardown code
5706 * needs to be scheduled to run asynch later so that we
5707 * don't delete data from under ourselves unexpectedly.
5708 * Calling wpas_p2p_group_formation_timeout directly
5709 * causes a series of crashes in WPS failure scenarios.
5710 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005711 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5712 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005713 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5714 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005715 }
5716}
5717
5718
5719struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005720 const u8 *addr, const u8 *ssid,
5721 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005722{
5723 struct wpa_ssid *s;
5724 size_t i;
5725
5726 for (s = wpa_s->conf->ssid; s; s = s->next) {
5727 if (s->disabled != 2)
5728 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005729 if (ssid &&
5730 (ssid_len != s->ssid_len ||
5731 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5732 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005733 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5734 return s; /* peer is GO in the persistent group */
5735 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5736 continue;
5737 for (i = 0; i < s->num_p2p_clients; i++) {
5738 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5739 addr, ETH_ALEN) == 0)
5740 return s; /* peer is P2P client in persistent
5741 * group */
5742 }
5743 }
5744
5745 return NULL;
5746}
5747
5748
5749void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5750 const u8 *addr)
5751{
5752 if (addr == NULL)
5753 return;
5754 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5755}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005756
Dmitry Shmidt04949592012-07-19 12:16:46 -07005757
5758static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5759 int group_added)
5760{
5761 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005762 if (wpa_s->global->p2p_group_formation)
5763 group = wpa_s->global->p2p_group_formation;
5764 wpa_s = wpa_s->parent;
5765 offchannel_send_action_done(wpa_s);
5766 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005767 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005768 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5769 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5770 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5771 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5772 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005773 wpa_s->p2p_pd_before_go_neg,
5774 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005775}
5776
5777
5778int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5779{
5780 if (!wpa_s->p2p_fallback_to_go_neg ||
5781 wpa_s->p2p_in_provisioning <= 5)
5782 return 0;
5783
5784 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5785 return 0; /* peer operating as a GO */
5786
5787 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5788 "fallback to GO Negotiation");
5789 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5790
5791 return 1;
5792}
5793
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005794
5795unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5796{
5797 const char *rn, *rn2;
5798 struct wpa_supplicant *ifs;
5799
5800 if (wpa_s->wpa_state > WPA_SCANNING) {
5801 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5802 "concurrent operation",
5803 P2P_CONCURRENT_SEARCH_DELAY);
5804 return P2P_CONCURRENT_SEARCH_DELAY;
5805 }
5806
5807 if (!wpa_s->driver->get_radio_name)
5808 return 0;
5809 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5810 if (rn == NULL || rn[0] == '\0')
5811 return 0;
5812
5813 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5814 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5815 continue;
5816
5817 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5818 if (!rn2 || os_strcmp(rn, rn2) != 0)
5819 continue;
5820 if (ifs->wpa_state > WPA_SCANNING) {
5821 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5822 "delay due to concurrent operation on "
5823 "interface %s",
5824 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5825 return P2P_CONCURRENT_SEARCH_DELAY;
5826 }
5827 }
5828
5829 return 0;
5830}
5831
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005832
5833void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5834{
5835 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5836 "pending anymore (sta_scan_pending=%d "
5837 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5838 wpa_s->global->p2p_cb_on_scan_complete);
5839 wpa_s->sta_scan_pending = 0;
5840
5841 if (!wpa_s->global->p2p_cb_on_scan_complete)
5842 return;
5843 wpa_s->global->p2p_cb_on_scan_complete = 0;
5844
5845 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5846 return;
5847
5848 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
5849 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
5850 "continued after successful connection");
5851 p2p_increase_search_delay(wpa_s->global->p2p,
5852 wpas_p2p_search_delay(wpa_s));
5853 }
5854}
5855
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005856#ifdef ANDROID_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005857int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5858 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005859{
5860 struct wpa_supplicant *iface = NULL;
5861 struct p2p_data *p2p = wpa_s->global->p2p;
5862
5863 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005864 if ((iface->current_ssid) &&
5865 (iface->current_ssid->frequency != freq) &&
5866 ((iface->p2p_group_interface) ||
5867 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005868
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005869 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5870 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5871 /* Try to see whether we can move the GO. If it
5872 * is not possible, remove the GO interface
5873 */
5874 if (wpa_drv_switch_channel(iface, freq) == 0) {
5875 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5876 iface->current_ssid->frequency = freq;
5877 continue;
5878 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005879 }
5880
5881 /* If GO cannot be moved or if the conflicting interface is a
5882 * P2P Client, remove the interface depending up on the connection
5883 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005884 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005885 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005886 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005887 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005888 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005889 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005890 /* If connection in progress is p2p connection, do not proceed for the connection */
5891 if (wpa_s == iface)
5892 return -1;
5893 else
5894 /* If connection in progress is STA connection, proceed for the connection */
5895 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005896 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005897 /* P2p connection has priority, disable the STA network*/
5898 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5899 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5900 " id=%d", ssid->id);
5901 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5902 if (wpa_s == iface) {
5903 /* p2p connection is in progress, continue connecting...*/
5904 return 0;
5905 }
5906 else {
5907 /* STA connection is in progress, do not allow to continue */
5908 return -1;
5909 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005910 }
5911 }
5912 }
5913 return 0;
5914}
5915#endif