blob: 6246dc0a9ddad3e01387c49d59f2adacb47ae1fa [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 &&
161 wpas_p2p_in_progress(wpa_s) == 2) {
162 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
163 "pending station mode scan to be "
164 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700165 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700166 wpa_supplicant_req_scan(ifs, 0, 0);
167 return 1;
168 }
169 }
170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171 os_memset(&params, 0, sizeof(params));
172
173 /* P2P Wildcard SSID */
174 params.num_ssids = 1;
175 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
176 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
177
178 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700179 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
180 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181 num_req_dev_types, req_dev_types);
182 if (wps_ie == NULL)
183 return -1;
184
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800185 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
186 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187 if (ies == NULL) {
188 wpabuf_free(wps_ie);
189 return -1;
190 }
191 wpabuf_put_buf(ies, wps_ie);
192 wpabuf_free(wps_ie);
193
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800194 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800196 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700197 params.extra_ies = wpabuf_head(ies);
198 params.extra_ies_len = wpabuf_len(ies);
199
200 switch (type) {
201 case P2P_SCAN_SOCIAL:
202 params.freqs = social_channels;
203 break;
204 case P2P_SCAN_FULL:
205 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700206 case P2P_SCAN_SPECIFIC:
207 social_channels[0] = freq;
208 social_channels[1] = 0;
209 params.freqs = social_channels;
210 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 case P2P_SCAN_SOCIAL_PLUS_ONE:
212 social_channels[3] = freq;
213 params.freqs = social_channels;
214 break;
215 }
216
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800217 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218
219 wpabuf_free(ies);
220
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700222 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
223 if (ifs->scanning ||
224 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
225 wpa_s->global->p2p_cb_on_scan_complete = 1;
226 ret = 1;
227 break;
228 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800229 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700230 } else
231 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233 return ret;
234}
235
236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
238{
239 switch (p2p_group_interface) {
240 case P2P_GROUP_INTERFACE_PENDING:
241 return WPA_IF_P2P_GROUP;
242 case P2P_GROUP_INTERFACE_GO:
243 return WPA_IF_P2P_GO;
244 case P2P_GROUP_INTERFACE_CLIENT:
245 return WPA_IF_P2P_CLIENT;
246 }
247
248 return WPA_IF_P2P_GROUP;
249}
250
251
252static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
253 const u8 *ssid,
254 size_t ssid_len, int *go)
255{
256 struct wpa_ssid *s;
257
258 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
259 for (s = wpa_s->conf->ssid; s; s = s->next) {
260 if (s->disabled != 0 || !s->p2p_group ||
261 s->ssid_len != ssid_len ||
262 os_memcmp(ssid, s->ssid, ssid_len) != 0)
263 continue;
264 if (s->mode == WPAS_MODE_P2P_GO &&
265 s != wpa_s->current_ssid)
266 continue;
267 if (go)
268 *go = s->mode == WPAS_MODE_P2P_GO;
269 return wpa_s;
270 }
271 }
272
273 return NULL;
274}
275
276
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700277static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
278 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279{
280 struct wpa_ssid *ssid;
281 char *gtype;
282 const char *reason;
283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 ssid = wpa_s->current_ssid;
285 if (ssid == NULL) {
286 /*
287 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700288 * pending P2P group interface waiting for provisioning or a
289 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 */
291 ssid = wpa_s->conf->ssid;
292 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700293 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294 break;
295 ssid = ssid->next;
296 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300297 if (ssid == NULL &&
298 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
299 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700300 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
301 "not found");
302 return -1;
303 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304 }
305 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
306 gtype = "GO";
307 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
308 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
309 wpa_s->reassociate = 0;
310 wpa_s->disconnected = 1;
311 wpa_supplicant_deauthenticate(wpa_s,
312 WLAN_REASON_DEAUTH_LEAVING);
313 gtype = "client";
314 } else
315 gtype = "GO";
316 if (wpa_s->cross_connect_in_use) {
317 wpa_s->cross_connect_in_use = 0;
318 wpa_msg(wpa_s->parent, MSG_INFO,
319 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
320 wpa_s->ifname, wpa_s->cross_connect_uplink);
321 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700322 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 case P2P_GROUP_REMOVAL_REQUESTED:
324 reason = " reason=REQUESTED";
325 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700326 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
327 reason = " reason=FORMATION_FAILED";
328 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
330 reason = " reason=IDLE";
331 break;
332 case P2P_GROUP_REMOVAL_UNAVAILABLE:
333 reason = " reason=UNAVAILABLE";
334 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700335 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
336 reason = " reason=GO_ENDING_SESSION";
337 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700338#ifdef ANDROID_P2P
339 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
340 reason = " reason=FREQ_CONFLICT";
341 break;
342#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 default:
344 reason = "";
345 break;
346 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700347 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700348 wpa_msg(wpa_s->parent, MSG_INFO,
349 P2P_EVENT_GROUP_REMOVED "%s %s%s",
350 wpa_s->ifname, gtype, reason);
351 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352
Dmitry Shmidt04949592012-07-19 12:16:46 -0700353 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
354 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800355 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700356 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800357 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
358 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700359 wpa_s->p2p_in_provisioning = 0;
360 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700361
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700362 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
364
365 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
366 struct wpa_global *global;
367 char *ifname;
368 enum wpa_driver_if_type type;
369 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
370 wpa_s->ifname);
371 global = wpa_s->global;
372 ifname = os_strdup(wpa_s->ifname);
373 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700374 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 wpa_s = global->ifaces;
376 if (wpa_s && ifname)
377 wpa_drv_if_remove(wpa_s, type, ifname);
378 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300379 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 }
381
382 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
383 if (ssid && (ssid->p2p_group ||
384 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
385 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
386 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700387 if (ssid == wpa_s->current_ssid) {
388 wpa_sm_set_config(wpa_s->wpa, NULL);
389 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700391 }
392 /*
393 * Networks objects created during any P2P activities are not
394 * exposed out as they might/will confuse certain non-P2P aware
395 * applications since these network objects won't behave like
396 * regular ones.
397 *
398 * Likewise, we don't send out network removed signals for such
399 * network objects.
400 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401 wpa_config_remove_network(wpa_s->conf, id);
402 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800403 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700404 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405 } else {
406 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
407 "found");
408 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700409 if (wpa_s->ap_iface)
410 wpa_supplicant_ap_deinit(wpa_s);
411 else
412 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700413
414 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415}
416
417
418static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
419 u8 *go_dev_addr,
420 const u8 *ssid, size_t ssid_len)
421{
422 struct wpa_bss *bss;
423 const u8 *bssid;
424 struct wpabuf *p2p;
425 u8 group_capab;
426 const u8 *addr;
427
428 if (wpa_s->go_params)
429 bssid = wpa_s->go_params->peer_interface_addr;
430 else
431 bssid = wpa_s->bssid;
432
433 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
434 if (bss == NULL) {
435 u8 iface_addr[ETH_ALEN];
436 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
437 iface_addr) == 0)
438 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
439 }
440 if (bss == NULL) {
441 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
442 "group is persistent - BSS " MACSTR " not found",
443 MAC2STR(bssid));
444 return 0;
445 }
446
447 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
448 if (p2p == NULL) {
449 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
450 "group is persistent - BSS " MACSTR
451 " did not include P2P IE", MAC2STR(bssid));
452 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
453 (u8 *) (bss + 1), bss->ie_len);
454 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
455 ((u8 *) bss + 1) + bss->ie_len,
456 bss->beacon_ie_len);
457 return 0;
458 }
459
460 group_capab = p2p_get_group_capab(p2p);
461 addr = p2p_get_go_dev_addr(p2p);
462 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
463 "group_capab=0x%x", group_capab);
464 if (addr) {
465 os_memcpy(go_dev_addr, addr, ETH_ALEN);
466 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
467 MAC2STR(addr));
468 } else
469 os_memset(go_dev_addr, 0, ETH_ALEN);
470 wpabuf_free(p2p);
471
472 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
473 "go_dev_addr=" MACSTR,
474 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
475
476 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
477}
478
479
Jouni Malinen75ecf522011-06-27 15:19:46 -0700480static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
481 struct wpa_ssid *ssid,
482 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483{
484 struct wpa_ssid *s;
485 int changed = 0;
486
487 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
488 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
489 for (s = wpa_s->conf->ssid; s; s = s->next) {
490 if (s->disabled == 2 &&
491 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
492 s->ssid_len == ssid->ssid_len &&
493 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
494 break;
495 }
496
497 if (s) {
498 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
499 "entry");
500 if (ssid->passphrase && !s->passphrase)
501 changed = 1;
502 else if (ssid->passphrase && s->passphrase &&
503 os_strcmp(ssid->passphrase, s->passphrase) != 0)
504 changed = 1;
505 } else {
506 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
507 "entry");
508 changed = 1;
509 s = wpa_config_add_network(wpa_s->conf);
510 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700511 return -1;
512
513 /*
514 * Instead of network_added we emit persistent_group_added
515 * notification. Also to keep the defense checks in
516 * persistent_group obj registration method, we set the
517 * relevant flags in s to designate it as a persistent group.
518 */
519 s->p2p_group = 1;
520 s->p2p_persistent_group = 1;
521 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522 wpa_config_set_network_defaults(s);
523 }
524
525 s->p2p_group = 1;
526 s->p2p_persistent_group = 1;
527 s->disabled = 2;
528 s->bssid_set = 1;
529 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
530 s->mode = ssid->mode;
531 s->auth_alg = WPA_AUTH_ALG_OPEN;
532 s->key_mgmt = WPA_KEY_MGMT_PSK;
533 s->proto = WPA_PROTO_RSN;
534 s->pairwise_cipher = WPA_CIPHER_CCMP;
535 s->export_keys = 1;
536 if (ssid->passphrase) {
537 os_free(s->passphrase);
538 s->passphrase = os_strdup(ssid->passphrase);
539 }
540 if (ssid->psk_set) {
541 s->psk_set = 1;
542 os_memcpy(s->psk, ssid->psk, 32);
543 }
544 if (s->passphrase && !s->psk_set)
545 wpa_config_update_psk(s);
546 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
547 os_free(s->ssid);
548 s->ssid = os_malloc(ssid->ssid_len);
549 }
550 if (s->ssid) {
551 s->ssid_len = ssid->ssid_len;
552 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
553 }
554
555#ifndef CONFIG_NO_CONFIG_WRITE
556 if (changed && wpa_s->conf->update_config &&
557 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
558 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
559 }
560#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700561
562 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563}
564
565
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800566static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
567 const u8 *addr)
568{
569 struct wpa_ssid *ssid, *s;
570 u8 *n;
571 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700572 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800573
574 ssid = wpa_s->current_ssid;
575 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
576 !ssid->p2p_persistent_group)
577 return;
578
579 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
580 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
581 continue;
582
583 if (s->ssid_len == ssid->ssid_len &&
584 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
585 break;
586 }
587
588 if (s == NULL)
589 return;
590
591 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
592 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700593 ETH_ALEN) != 0)
594 continue;
595
596 if (i == s->num_p2p_clients - 1)
597 return; /* already the most recent entry */
598
599 /* move the entry to mark it most recent */
600 os_memmove(s->p2p_client_list + i * ETH_ALEN,
601 s->p2p_client_list + (i + 1) * ETH_ALEN,
602 (s->num_p2p_clients - i - 1) * ETH_ALEN);
603 os_memcpy(s->p2p_client_list +
604 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
605 found = 1;
606 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800607 }
608
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700609 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
610 n = os_realloc_array(s->p2p_client_list,
611 s->num_p2p_clients + 1, ETH_ALEN);
612 if (n == NULL)
613 return;
614 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
615 s->p2p_client_list = n;
616 s->num_p2p_clients++;
617 } else if (!found) {
618 /* Not enough room for an additional entry - drop the oldest
619 * entry */
620 os_memmove(s->p2p_client_list,
621 s->p2p_client_list + ETH_ALEN,
622 (s->num_p2p_clients - 1) * ETH_ALEN);
623 os_memcpy(s->p2p_client_list +
624 (s->num_p2p_clients - 1) * ETH_ALEN,
625 addr, ETH_ALEN);
626 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800627
628#ifndef CONFIG_NO_CONFIG_WRITE
629 if (wpa_s->parent->conf->update_config &&
630 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
631 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
632#endif /* CONFIG_NO_CONFIG_WRITE */
633}
634
635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
637 int success)
638{
639 struct wpa_ssid *ssid;
640 const char *ssid_txt;
641 int client;
642 int persistent;
643 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700644 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645
646 /*
647 * This callback is likely called for the main interface. Update wpa_s
648 * to use the group interface if a new interface was created for the
649 * group.
650 */
651 if (wpa_s->global->p2p_group_formation)
652 wpa_s = wpa_s->global->p2p_group_formation;
653 wpa_s->global->p2p_group_formation = NULL;
654 wpa_s->p2p_in_provisioning = 0;
655
656 if (!success) {
657 wpa_msg(wpa_s->parent, MSG_INFO,
658 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700659 wpas_p2p_group_delete(wpa_s,
660 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661 return;
662 }
663
664 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
665
666 ssid = wpa_s->current_ssid;
667 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
668 ssid->mode = WPAS_MODE_P2P_GO;
669 p2p_group_notif_formation_done(wpa_s->p2p_group);
670 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
671 }
672
673 persistent = 0;
674 if (ssid) {
675 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
676 client = ssid->mode == WPAS_MODE_INFRA;
677 if (ssid->mode == WPAS_MODE_P2P_GO) {
678 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700679 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
680 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 } else
682 persistent = wpas_p2p_persistent_group(wpa_s,
683 go_dev_addr,
684 ssid->ssid,
685 ssid->ssid_len);
686 } else {
687 ssid_txt = "";
688 client = wpa_s->p2p_group_interface ==
689 P2P_GROUP_INTERFACE_CLIENT;
690 os_memset(go_dev_addr, 0, ETH_ALEN);
691 }
692
693 wpa_s->show_group_started = 0;
694 if (client) {
695 /*
696 * Indicate event only after successfully completed 4-way
697 * handshake, i.e., when the interface is ready for data
698 * packets.
699 */
700 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700701#ifdef ANDROID_P2P
702 /* For client Second phase of Group formation (4-way handshake) can be still pending
703 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700704 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 -0700705 wpa_s->global->p2p_group_formation = wpa_s;
706#endif
707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
709 char psk[65];
710 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
711 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
712 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
713 "%s",
714 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
715 MAC2STR(go_dev_addr),
716 persistent ? " [PERSISTENT]" : "");
717 wpas_p2p_cross_connect_setup(wpa_s);
718 wpas_p2p_set_group_idle_timeout(wpa_s);
719 } else {
720 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
721 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
722 "go_dev_addr=" MACSTR "%s",
723 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
724 ssid && ssid->passphrase ? ssid->passphrase : "",
725 MAC2STR(go_dev_addr),
726 persistent ? " [PERSISTENT]" : "");
727 wpas_p2p_cross_connect_setup(wpa_s);
728 wpas_p2p_set_group_idle_timeout(wpa_s);
729 }
730
731 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700732 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
733 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800734 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700735 network_id = ssid->id;
736 if (!client)
737 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738}
739
740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800741static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
742 unsigned int freq,
743 const u8 *dst, const u8 *src,
744 const u8 *bssid,
745 const u8 *data, size_t data_len,
746 enum offchannel_send_action_result
747 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800749 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
752 return;
753 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
754 return;
755
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800756 switch (result) {
757 case OFFCHANNEL_SEND_ACTION_SUCCESS:
758 res = P2P_SEND_ACTION_SUCCESS;
759 break;
760 case OFFCHANNEL_SEND_ACTION_NO_ACK:
761 res = P2P_SEND_ACTION_NO_ACK;
762 break;
763 case OFFCHANNEL_SEND_ACTION_FAILED:
764 res = P2P_SEND_ACTION_FAILED;
765 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766 }
767
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800768 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800770 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
771 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800772 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800773 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
774 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800776 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
777 "during p2p_connect-auto");
778 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
779 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 }
781}
782
783
784static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
785 const u8 *src, const u8 *bssid, const u8 *buf,
786 size_t len, unsigned int wait_time)
787{
788 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800789 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
790 wait_time,
791 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792}
793
794
795static void wpas_send_action_done(void *ctx)
796{
797 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799}
800
801
802static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
803 struct p2p_go_neg_results *params)
804{
805 if (wpa_s->go_params == NULL) {
806 wpa_s->go_params = os_malloc(sizeof(*params));
807 if (wpa_s->go_params == NULL)
808 return -1;
809 }
810 os_memcpy(wpa_s->go_params, params, sizeof(*params));
811 return 0;
812}
813
814
815static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
816 struct p2p_go_neg_results *res)
817{
818 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
819 MAC2STR(res->peer_interface_addr));
820 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
821 res->ssid, res->ssid_len);
822 wpa_supplicant_ap_deinit(wpa_s);
823 wpas_copy_go_neg_results(wpa_s, res);
824 if (res->wps_method == WPS_PBC)
825 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
826 else {
827 u16 dev_pw_id = DEV_PW_DEFAULT;
828 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
829 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
830 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
831 wpa_s->p2p_pin, 1, dev_pw_id);
832 }
833}
834
835
836static void p2p_go_configured(void *ctx, void *data)
837{
838 struct wpa_supplicant *wpa_s = ctx;
839 struct p2p_go_neg_results *params = data;
840 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700841 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842
843 ssid = wpa_s->current_ssid;
844 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
845 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
846 if (wpa_s->global->p2p_group_formation == wpa_s)
847 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800848 if (os_strlen(params->passphrase) > 0) {
849 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
850 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
851 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
852 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
853 ssid->frequency, params->passphrase,
854 MAC2STR(wpa_s->global->p2p_dev_addr),
855 params->persistent_group ? " [PERSISTENT]" :
856 "");
857 } else {
858 char psk[65];
859 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
860 sizeof(params->psk));
861 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
862 "%s GO ssid=\"%s\" freq=%d psk=%s "
863 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
864 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
865 ssid->frequency, psk,
866 MAC2STR(wpa_s->global->p2p_dev_addr),
867 params->persistent_group ? " [PERSISTENT]" :
868 "");
869 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700872 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700874 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700875 if (network_id < 0)
876 network_id = ssid->id;
877 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 wpas_p2p_cross_connect_setup(wpa_s);
879 wpas_p2p_set_group_idle_timeout(wpa_s);
880 return;
881 }
882
883 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
884 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
885 params->peer_interface_addr)) {
886 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
887 "filtering");
888 return;
889 }
890 if (params->wps_method == WPS_PBC)
891 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800892 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 else if (wpa_s->p2p_pin[0])
894 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800895 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 os_free(wpa_s->go_params);
897 wpa_s->go_params = NULL;
898}
899
900
901static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
902 struct p2p_go_neg_results *params,
903 int group_formation)
904{
905 struct wpa_ssid *ssid;
906
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700907 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
908 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
909 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
910 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700912 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700913
914 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700915 if (ssid == NULL) {
916 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700918 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800920 wpa_s->show_group_started = 0;
921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 wpa_config_set_network_defaults(ssid);
923 ssid->temporary = 1;
924 ssid->p2p_group = 1;
925 ssid->p2p_persistent_group = params->persistent_group;
926 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
927 WPAS_MODE_P2P_GO;
928 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700929 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 ssid->ssid = os_zalloc(params->ssid_len + 1);
931 if (ssid->ssid) {
932 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
933 ssid->ssid_len = params->ssid_len;
934 }
935 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
936 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
937 ssid->proto = WPA_PROTO_RSN;
938 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800939 if (os_strlen(params->passphrase) > 0) {
940 ssid->passphrase = os_strdup(params->passphrase);
941 if (ssid->passphrase == NULL) {
942 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
943 "passphrase for GO");
944 wpa_config_remove_network(wpa_s->conf, ssid->id);
945 return;
946 }
947 } else
948 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800949 ssid->psk_set = params->psk_set;
950 if (ssid->psk_set)
951 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800952 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800953 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700954 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955
956 wpa_s->ap_configured_cb = p2p_go_configured;
957 wpa_s->ap_configured_cb_ctx = wpa_s;
958 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700959 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 wpa_s->reassociate = 1;
961 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700962 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
963 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964 wpa_supplicant_req_scan(wpa_s, 0, 0);
965}
966
967
968static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
969 const struct wpa_supplicant *src)
970{
971 struct wpa_config *d;
972 const struct wpa_config *s;
973
974 d = dst->conf;
975 s = src->conf;
976
977#define C(n) if (s->n) d->n = os_strdup(s->n)
978 C(device_name);
979 C(manufacturer);
980 C(model_name);
981 C(model_number);
982 C(serial_number);
983 C(config_methods);
984#undef C
985
986 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
987 os_memcpy(d->sec_device_type, s->sec_device_type,
988 sizeof(d->sec_device_type));
989 d->num_sec_device_types = s->num_sec_device_types;
990
991 d->p2p_group_idle = s->p2p_group_idle;
992 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800993 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700994 d->max_num_sta = s->max_num_sta;
995 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700996}
997
998
999static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1000 enum wpa_driver_if_type type)
1001{
1002 char ifname[120], force_ifname[120];
1003
1004 if (wpa_s->pending_interface_name[0]) {
1005 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1006 "- skip creation of a new one");
1007 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1008 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1009 "unknown?! ifname='%s'",
1010 wpa_s->pending_interface_name);
1011 return -1;
1012 }
1013 return 0;
1014 }
1015
1016 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
1017 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001018 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1020 /* Try to avoid going over the IFNAMSIZ length limit */
1021 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1022 wpa_s->p2p_group_idx);
1023 }
1024 force_ifname[0] = '\0';
1025
1026 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1027 ifname);
1028 wpa_s->p2p_group_idx++;
1029
1030 wpa_s->pending_interface_type = type;
1031 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1032 wpa_s->pending_interface_addr, NULL) < 0) {
1033 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1034 "interface");
1035 return -1;
1036 }
1037
1038 if (force_ifname[0]) {
1039 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1040 force_ifname);
1041 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1042 sizeof(wpa_s->pending_interface_name));
1043 } else
1044 os_strlcpy(wpa_s->pending_interface_name, ifname,
1045 sizeof(wpa_s->pending_interface_name));
1046 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1047 MACSTR, wpa_s->pending_interface_name,
1048 MAC2STR(wpa_s->pending_interface_addr));
1049
1050 return 0;
1051}
1052
1053
1054static void wpas_p2p_remove_pending_group_interface(
1055 struct wpa_supplicant *wpa_s)
1056{
1057 if (!wpa_s->pending_interface_name[0] ||
1058 is_zero_ether_addr(wpa_s->pending_interface_addr))
1059 return; /* No pending virtual interface */
1060
1061 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1062 wpa_s->pending_interface_name);
1063 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1064 wpa_s->pending_interface_name);
1065 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1066 wpa_s->pending_interface_name[0] = '\0';
1067}
1068
1069
1070static struct wpa_supplicant *
1071wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1072{
1073 struct wpa_interface iface;
1074 struct wpa_supplicant *group_wpa_s;
1075
1076 if (!wpa_s->pending_interface_name[0]) {
1077 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1078 if (!wpas_p2p_create_iface(wpa_s))
1079 return NULL;
1080 /*
1081 * Something has forced us to remove the pending interface; try
1082 * to create a new one and hope for the best that we will get
1083 * the same local address.
1084 */
1085 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1086 WPA_IF_P2P_CLIENT) < 0)
1087 return NULL;
1088 }
1089
1090 os_memset(&iface, 0, sizeof(iface));
1091 iface.ifname = wpa_s->pending_interface_name;
1092 iface.driver = wpa_s->driver->name;
1093 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1094 iface.driver_param = wpa_s->conf->driver_param;
1095 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1096 if (group_wpa_s == NULL) {
1097 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1098 "wpa_supplicant interface");
1099 return NULL;
1100 }
1101 wpa_s->pending_interface_name[0] = '\0';
1102 group_wpa_s->parent = wpa_s;
1103 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1104 P2P_GROUP_INTERFACE_CLIENT;
1105 wpa_s->global->p2p_group_formation = group_wpa_s;
1106
1107 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1108
1109 return group_wpa_s;
1110}
1111
1112
1113static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1114 void *timeout_ctx)
1115{
1116 struct wpa_supplicant *wpa_s = eloop_ctx;
1117 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1118 if (wpa_s->global->p2p)
1119 p2p_group_formation_failed(wpa_s->global->p2p);
1120 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1121 wpa_drv_p2p_group_formation_failed(wpa_s);
1122 wpas_group_formation_completed(wpa_s, 0);
1123}
1124
1125
1126void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1127{
1128 struct wpa_supplicant *wpa_s = ctx;
1129
1130 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1131 wpa_drv_cancel_remain_on_channel(wpa_s);
1132 wpa_s->off_channel_freq = 0;
1133 wpa_s->roc_waiting_drv_freq = 0;
1134 }
1135
1136 if (res->status) {
1137 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1138 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001139 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 wpas_p2p_remove_pending_group_interface(wpa_s);
1141 return;
1142 }
1143
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001144 if (wpa_s->p2p_go_ht40)
1145 res->ht40 = 1;
1146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001148 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149
Dmitry Shmidt04949592012-07-19 12:16:46 -07001150 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1151 struct wpa_ssid *ssid;
1152 ssid = wpa_config_get_network(wpa_s->conf,
1153 wpa_s->p2p_persistent_id);
1154 if (ssid && ssid->disabled == 2 &&
1155 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1156 size_t len = os_strlen(ssid->passphrase);
1157 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1158 "on requested persistent group");
1159 os_memcpy(res->passphrase, ssid->passphrase, len);
1160 res->passphrase[len] = '\0';
1161 }
1162 }
1163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 if (wpa_s->create_p2p_iface) {
1165 struct wpa_supplicant *group_wpa_s =
1166 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1167 if (group_wpa_s == NULL) {
1168 wpas_p2p_remove_pending_group_interface(wpa_s);
1169 return;
1170 }
1171 if (group_wpa_s != wpa_s) {
1172 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1173 sizeof(group_wpa_s->p2p_pin));
1174 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1175 }
1176 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1177 wpa_s->pending_interface_name[0] = '\0';
1178 group_wpa_s->p2p_in_provisioning = 1;
1179
1180 if (res->role_go)
1181 wpas_start_wps_go(group_wpa_s, res, 1);
1182 else
1183 wpas_start_wps_enrollee(group_wpa_s, res);
1184 } else {
1185 wpa_s->p2p_in_provisioning = 1;
1186 wpa_s->global->p2p_group_formation = wpa_s;
1187
1188 if (res->role_go)
1189 wpas_start_wps_go(wpa_s, res, 1);
1190 else
1191 wpas_start_wps_enrollee(ctx, res);
1192 }
1193
1194 wpa_s->p2p_long_listen = 0;
1195 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1196
1197 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1198 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1199 (res->peer_config_timeout % 100) * 10000,
1200 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1201}
1202
1203
1204void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1205{
1206 struct wpa_supplicant *wpa_s = ctx;
1207 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1208 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1209
1210 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1211}
1212
1213
1214void wpas_dev_found(void *ctx, const u8 *addr,
1215 const struct p2p_peer_info *info,
1216 int new_device)
1217{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001218#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 struct wpa_supplicant *wpa_s = ctx;
1220 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001221#define WFD_DEV_INFO_SIZE 9
1222 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001223 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001224#ifdef CONFIG_WIFI_DISPLAY
1225 if (info->wfd_subelems) {
1226 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1227 wpabuf_head(info->wfd_subelems),
1228 WFD_DEV_INFO_SIZE);
1229 }
1230#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1232 " p2p_dev_addr=" MACSTR
1233 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001234 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1236 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1237 sizeof(devtype)),
1238 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001239 info->dev_capab, info->group_capab,
1240 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001241#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242
1243 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1244}
1245
1246
1247static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1248{
1249 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001250
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001251 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1252 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001254 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1255}
1256
1257
1258static int wpas_start_listen(void *ctx, unsigned int freq,
1259 unsigned int duration,
1260 const struct wpabuf *probe_resp_ie)
1261{
1262 struct wpa_supplicant *wpa_s = ctx;
1263
1264 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1265
1266 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1267 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1268 "report received Probe Request frames");
1269 return -1;
1270 }
1271
1272 wpa_s->pending_listen_freq = freq;
1273 wpa_s->pending_listen_duration = duration;
1274
1275 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1276 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1277 "to remain on channel (%u MHz) for Listen "
1278 "state", freq);
1279 wpa_s->pending_listen_freq = 0;
1280 return -1;
1281 }
1282 wpa_s->off_channel_freq = 0;
1283 wpa_s->roc_waiting_drv_freq = freq;
1284
1285 return 0;
1286}
1287
1288
1289static void wpas_stop_listen(void *ctx)
1290{
1291 struct wpa_supplicant *wpa_s = ctx;
1292 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1293 wpa_drv_cancel_remain_on_channel(wpa_s);
1294 wpa_s->off_channel_freq = 0;
1295 wpa_s->roc_waiting_drv_freq = 0;
1296 }
1297 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1298 wpa_drv_probe_req_report(wpa_s, 0);
1299}
1300
1301
1302static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1303{
1304 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001305 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306}
1307
1308
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001309/*
1310 * DNS Header section is used only to calculate compression pointers, so the
1311 * contents of this data does not matter, but the length needs to be reserved
1312 * in the virtual packet.
1313 */
1314#define DNS_HEADER_LEN 12
1315
1316/*
1317 * 27-octet in-memory packet from P2P specification containing two implied
1318 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1319 */
1320#define P2P_SD_IN_MEMORY_LEN 27
1321
1322static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1323 u8 **spos, const u8 *end)
1324{
1325 while (*spos < end) {
1326 u8 val = ((*spos)[0] & 0xc0) >> 6;
1327 int len;
1328
1329 if (val == 1 || val == 2) {
1330 /* These are reserved values in RFC 1035 */
1331 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1332 "sequence starting with 0x%x", val);
1333 return -1;
1334 }
1335
1336 if (val == 3) {
1337 u16 offset;
1338 u8 *spos_tmp;
1339
1340 /* Offset */
1341 if (*spos + 2 > end) {
1342 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1343 "DNS offset field");
1344 return -1;
1345 }
1346
1347 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1348 if (offset >= *spos - start) {
1349 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1350 "pointer offset %u", offset);
1351 return -1;
1352 }
1353
1354 (*spos) += 2;
1355 spos_tmp = start + offset;
1356 return p2p_sd_dns_uncompress_label(upos, uend, start,
1357 &spos_tmp,
1358 *spos - 2);
1359 }
1360
1361 /* Label */
1362 len = (*spos)[0] & 0x3f;
1363 if (len == 0)
1364 return 0;
1365
1366 (*spos)++;
1367 if (*spos + len > end) {
1368 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1369 "sequence - no room for label with length "
1370 "%u", len);
1371 return -1;
1372 }
1373
1374 if (*upos + len + 2 > uend)
1375 return -2;
1376
1377 os_memcpy(*upos, *spos, len);
1378 *spos += len;
1379 *upos += len;
1380 (*upos)[0] = '.';
1381 (*upos)++;
1382 (*upos)[0] = '\0';
1383 }
1384
1385 return 0;
1386}
1387
1388
1389/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1390 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1391 * not large enough */
1392static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1393 size_t msg_len, size_t offset)
1394{
1395 /* 27-octet in-memory packet from P2P specification */
1396 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1397 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1398 u8 *tmp, *end, *spos;
1399 char *upos, *uend;
1400 int ret = 0;
1401
1402 if (buf_len < 2)
1403 return -1;
1404 if (offset > msg_len)
1405 return -1;
1406
1407 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1408 if (tmp == NULL)
1409 return -1;
1410 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1411 end = spos + msg_len;
1412 spos += offset;
1413
1414 os_memset(tmp, 0, DNS_HEADER_LEN);
1415 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1416 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1417
1418 upos = buf;
1419 uend = buf + buf_len;
1420
1421 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1422 if (ret) {
1423 os_free(tmp);
1424 return ret;
1425 }
1426
1427 if (upos == buf) {
1428 upos[0] = '.';
1429 upos[1] = '\0';
1430 } else if (upos[-1] == '.')
1431 upos[-1] = '\0';
1432
1433 os_free(tmp);
1434 return 0;
1435}
1436
1437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438static struct p2p_srv_bonjour *
1439wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1440 const struct wpabuf *query)
1441{
1442 struct p2p_srv_bonjour *bsrv;
1443 size_t len;
1444
1445 len = wpabuf_len(query);
1446 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1447 struct p2p_srv_bonjour, list) {
1448 if (len == wpabuf_len(bsrv->query) &&
1449 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1450 len) == 0)
1451 return bsrv;
1452 }
1453 return NULL;
1454}
1455
1456
1457static struct p2p_srv_upnp *
1458wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1459 const char *service)
1460{
1461 struct p2p_srv_upnp *usrv;
1462
1463 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1464 struct p2p_srv_upnp, list) {
1465 if (version == usrv->version &&
1466 os_strcmp(service, usrv->service) == 0)
1467 return usrv;
1468 }
1469 return NULL;
1470}
1471
1472
1473static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1474 u8 srv_trans_id)
1475{
1476 u8 *len_pos;
1477
1478 if (wpabuf_tailroom(resp) < 5)
1479 return;
1480
1481 /* Length (to be filled) */
1482 len_pos = wpabuf_put(resp, 2);
1483 wpabuf_put_u8(resp, srv_proto);
1484 wpabuf_put_u8(resp, srv_trans_id);
1485 /* Status Code */
1486 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1487 /* Response Data: empty */
1488 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1489}
1490
1491
1492static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1493 struct wpabuf *resp, u8 srv_trans_id)
1494{
1495 struct p2p_srv_bonjour *bsrv;
1496 u8 *len_pos;
1497
1498 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1499
1500 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1501 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1502 return;
1503 }
1504
1505 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1506 struct p2p_srv_bonjour, list) {
1507 if (wpabuf_tailroom(resp) <
1508 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1509 return;
1510 /* Length (to be filled) */
1511 len_pos = wpabuf_put(resp, 2);
1512 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1513 wpabuf_put_u8(resp, srv_trans_id);
1514 /* Status Code */
1515 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1516 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1517 wpabuf_head(bsrv->resp),
1518 wpabuf_len(bsrv->resp));
1519 /* Response Data */
1520 wpabuf_put_buf(resp, bsrv->query); /* Key */
1521 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1522 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1523 2);
1524 }
1525}
1526
1527
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001528static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1529 size_t query_len)
1530{
1531 char str_rx[256], str_srv[256];
1532
1533 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1534 return 0; /* Too short to include DNS Type and Version */
1535 if (os_memcmp(query + query_len - 3,
1536 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1537 3) != 0)
1538 return 0; /* Mismatch in DNS Type or Version */
1539 if (query_len == wpabuf_len(bsrv->query) &&
1540 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1541 return 1; /* Binary match */
1542
1543 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1544 0))
1545 return 0; /* Failed to uncompress query */
1546 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1547 wpabuf_head(bsrv->query),
1548 wpabuf_len(bsrv->query) - 3, 0))
1549 return 0; /* Failed to uncompress service */
1550
1551 return os_strcmp(str_rx, str_srv) == 0;
1552}
1553
1554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1556 struct wpabuf *resp, u8 srv_trans_id,
1557 const u8 *query, size_t query_len)
1558{
1559 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001561 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001562
1563 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1564 query, query_len);
1565 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1566 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1567 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1568 srv_trans_id);
1569 return;
1570 }
1571
1572 if (query_len == 0) {
1573 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1574 return;
1575 }
1576
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001577 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1578 struct p2p_srv_bonjour, list) {
1579 if (!match_bonjour_query(bsrv, query, query_len))
1580 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001582 if (wpabuf_tailroom(resp) <
1583 5 + query_len + wpabuf_len(bsrv->resp))
1584 return;
1585
1586 matches++;
1587
1588 /* Length (to be filled) */
1589 len_pos = wpabuf_put(resp, 2);
1590 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1591 wpabuf_put_u8(resp, srv_trans_id);
1592
1593 /* Status Code */
1594 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1595 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1596 wpabuf_head(bsrv->resp),
1597 wpabuf_len(bsrv->resp));
1598
1599 /* Response Data */
1600 wpabuf_put_data(resp, query, query_len); /* Key */
1601 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1602
1603 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1604 }
1605
1606 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1608 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001609 if (wpabuf_tailroom(resp) < 5)
1610 return;
1611
1612 /* Length (to be filled) */
1613 len_pos = wpabuf_put(resp, 2);
1614 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1615 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616
1617 /* Status Code */
1618 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1619 /* Response Data: empty */
1620 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1621 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623}
1624
1625
1626static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1627 struct wpabuf *resp, u8 srv_trans_id)
1628{
1629 struct p2p_srv_upnp *usrv;
1630 u8 *len_pos;
1631
1632 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1633
1634 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1635 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1636 return;
1637 }
1638
1639 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1640 struct p2p_srv_upnp, list) {
1641 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1642 return;
1643
1644 /* Length (to be filled) */
1645 len_pos = wpabuf_put(resp, 2);
1646 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1647 wpabuf_put_u8(resp, srv_trans_id);
1648
1649 /* Status Code */
1650 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1651 /* Response Data */
1652 wpabuf_put_u8(resp, usrv->version);
1653 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1654 usrv->service);
1655 wpabuf_put_str(resp, usrv->service);
1656 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1657 2);
1658 }
1659}
1660
1661
1662static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1663 struct wpabuf *resp, u8 srv_trans_id,
1664 const u8 *query, size_t query_len)
1665{
1666 struct p2p_srv_upnp *usrv;
1667 u8 *len_pos;
1668 u8 version;
1669 char *str;
1670 int count = 0;
1671
1672 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1673 query, query_len);
1674
1675 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1676 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1677 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1678 srv_trans_id);
1679 return;
1680 }
1681
1682 if (query_len == 0) {
1683 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1684 return;
1685 }
1686
1687 if (wpabuf_tailroom(resp) < 5)
1688 return;
1689
1690 /* Length (to be filled) */
1691 len_pos = wpabuf_put(resp, 2);
1692 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1693 wpabuf_put_u8(resp, srv_trans_id);
1694
1695 version = query[0];
1696 str = os_malloc(query_len);
1697 if (str == NULL)
1698 return;
1699 os_memcpy(str, query + 1, query_len - 1);
1700 str[query_len - 1] = '\0';
1701
1702 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1703 struct p2p_srv_upnp, list) {
1704 if (version != usrv->version)
1705 continue;
1706
1707 if (os_strcmp(str, "ssdp:all") != 0 &&
1708 os_strstr(usrv->service, str) == NULL)
1709 continue;
1710
1711 if (wpabuf_tailroom(resp) < 2)
1712 break;
1713 if (count == 0) {
1714 /* Status Code */
1715 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1716 /* Response Data */
1717 wpabuf_put_u8(resp, version);
1718 } else
1719 wpabuf_put_u8(resp, ',');
1720
1721 count++;
1722
1723 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1724 usrv->service);
1725 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1726 break;
1727 wpabuf_put_str(resp, usrv->service);
1728 }
1729 os_free(str);
1730
1731 if (count == 0) {
1732 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1733 "available");
1734 /* Status Code */
1735 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1736 /* Response Data: empty */
1737 }
1738
1739 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1740}
1741
1742
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001743#ifdef CONFIG_WIFI_DISPLAY
1744static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1745 struct wpabuf *resp, u8 srv_trans_id,
1746 const u8 *query, size_t query_len)
1747{
1748 const u8 *pos;
1749 u8 role;
1750 u8 *len_pos;
1751
1752 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1753
1754 if (!wpa_s->global->wifi_display) {
1755 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1756 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1757 srv_trans_id);
1758 return;
1759 }
1760
1761 if (query_len < 1) {
1762 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1763 "Role");
1764 return;
1765 }
1766
1767 if (wpabuf_tailroom(resp) < 5)
1768 return;
1769
1770 pos = query;
1771 role = *pos++;
1772 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1773
1774 /* TODO: role specific handling */
1775
1776 /* Length (to be filled) */
1777 len_pos = wpabuf_put(resp, 2);
1778 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1779 wpabuf_put_u8(resp, srv_trans_id);
1780 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1781
1782 while (pos < query + query_len) {
1783 if (*pos < MAX_WFD_SUBELEMS &&
1784 wpa_s->global->wfd_subelem[*pos] &&
1785 wpabuf_tailroom(resp) >=
1786 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1787 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1788 "subelement %u", *pos);
1789 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1790 }
1791 pos++;
1792 }
1793
1794 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1795}
1796#endif /* CONFIG_WIFI_DISPLAY */
1797
1798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1800 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1801{
1802 struct wpa_supplicant *wpa_s = ctx;
1803 const u8 *pos = tlvs;
1804 const u8 *end = tlvs + tlvs_len;
1805 const u8 *tlv_end;
1806 u16 slen;
1807 struct wpabuf *resp;
1808 u8 srv_proto, srv_trans_id;
1809 size_t buf_len;
1810 char *buf;
1811
1812 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1813 tlvs, tlvs_len);
1814 buf_len = 2 * tlvs_len + 1;
1815 buf = os_malloc(buf_len);
1816 if (buf) {
1817 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1818 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1819 MACSTR " %u %u %s",
1820 freq, MAC2STR(sa), dialog_token, update_indic,
1821 buf);
1822 os_free(buf);
1823 }
1824
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001825 if (wpa_s->p2p_sd_over_ctrl_iface) {
1826 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1827 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001829 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830
1831 resp = wpabuf_alloc(10000);
1832 if (resp == NULL)
1833 return;
1834
1835 while (pos + 1 < end) {
1836 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1837 slen = WPA_GET_LE16(pos);
1838 pos += 2;
1839 if (pos + slen > end || slen < 2) {
1840 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1841 "length");
1842 wpabuf_free(resp);
1843 return;
1844 }
1845 tlv_end = pos + slen;
1846
1847 srv_proto = *pos++;
1848 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1849 srv_proto);
1850 srv_trans_id = *pos++;
1851 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1852 srv_trans_id);
1853
1854 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1855 pos, tlv_end - pos);
1856
1857
1858 if (wpa_s->force_long_sd) {
1859 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1860 "response");
1861 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1862 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1863 goto done;
1864 }
1865
1866 switch (srv_proto) {
1867 case P2P_SERV_ALL_SERVICES:
1868 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1869 "for all services");
1870 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1871 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1872 wpa_printf(MSG_DEBUG, "P2P: No service "
1873 "discovery protocols available");
1874 wpas_sd_add_proto_not_avail(
1875 resp, P2P_SERV_ALL_SERVICES,
1876 srv_trans_id);
1877 break;
1878 }
1879 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1880 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1881 break;
1882 case P2P_SERV_BONJOUR:
1883 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1884 pos, tlv_end - pos);
1885 break;
1886 case P2P_SERV_UPNP:
1887 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1888 pos, tlv_end - pos);
1889 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001890#ifdef CONFIG_WIFI_DISPLAY
1891 case P2P_SERV_WIFI_DISPLAY:
1892 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1893 pos, tlv_end - pos);
1894 break;
1895#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 default:
1897 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1898 "protocol %u", srv_proto);
1899 wpas_sd_add_proto_not_avail(resp, srv_proto,
1900 srv_trans_id);
1901 break;
1902 }
1903
1904 pos = tlv_end;
1905 }
1906
1907done:
1908 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1909 update_indic, tlvs, tlvs_len);
1910
1911 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1912
1913 wpabuf_free(resp);
1914}
1915
1916
1917void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1918 const u8 *tlvs, size_t tlvs_len)
1919{
1920 struct wpa_supplicant *wpa_s = ctx;
1921 const u8 *pos = tlvs;
1922 const u8 *end = tlvs + tlvs_len;
1923 const u8 *tlv_end;
1924 u16 slen;
1925 size_t buf_len;
1926 char *buf;
1927
1928 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1929 tlvs, tlvs_len);
1930 if (tlvs_len > 1500) {
1931 /* TODO: better way for handling this */
1932 wpa_msg_ctrl(wpa_s, MSG_INFO,
1933 P2P_EVENT_SERV_DISC_RESP MACSTR
1934 " %u <long response: %u bytes>",
1935 MAC2STR(sa), update_indic,
1936 (unsigned int) tlvs_len);
1937 } else {
1938 buf_len = 2 * tlvs_len + 1;
1939 buf = os_malloc(buf_len);
1940 if (buf) {
1941 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1942 wpa_msg_ctrl(wpa_s, MSG_INFO,
1943 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1944 MAC2STR(sa), update_indic, buf);
1945 os_free(buf);
1946 }
1947 }
1948
1949 while (pos < end) {
1950 u8 srv_proto, srv_trans_id, status;
1951
1952 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1953 slen = WPA_GET_LE16(pos);
1954 pos += 2;
1955 if (pos + slen > end || slen < 3) {
1956 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1957 "length");
1958 return;
1959 }
1960 tlv_end = pos + slen;
1961
1962 srv_proto = *pos++;
1963 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1964 srv_proto);
1965 srv_trans_id = *pos++;
1966 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1967 srv_trans_id);
1968 status = *pos++;
1969 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1970 status);
1971
1972 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1973 pos, tlv_end - pos);
1974
1975 pos = tlv_end;
1976 }
1977
1978 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1979}
1980
1981
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001982u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1983 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984{
1985 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001986 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001988 return 0;
1989 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990}
1991
1992
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001993u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1994 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995{
1996 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001997 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998
1999 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2000 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002001 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2003 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2004 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2005 wpabuf_put_u8(tlvs, version);
2006 wpabuf_put_str(tlvs, query);
2007 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2008 wpabuf_free(tlvs);
2009 return ret;
2010}
2011
2012
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002013#ifdef CONFIG_WIFI_DISPLAY
2014
2015static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2016 const struct wpabuf *tlvs)
2017{
2018 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2019 return 0;
2020 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2021 return 0;
2022 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2023}
2024
2025
2026#define MAX_WFD_SD_SUBELEMS 20
2027
2028static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2029 const char *subelems)
2030{
2031 u8 *len;
2032 const char *pos;
2033 int val;
2034 int count = 0;
2035
2036 len = wpabuf_put(tlvs, 2);
2037 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2038 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2039
2040 wpabuf_put_u8(tlvs, role);
2041
2042 pos = subelems;
2043 while (*pos) {
2044 val = atoi(pos);
2045 if (val >= 0 && val < 256) {
2046 wpabuf_put_u8(tlvs, val);
2047 count++;
2048 if (count == MAX_WFD_SD_SUBELEMS)
2049 break;
2050 }
2051 pos = os_strchr(pos + 1, ',');
2052 if (pos == NULL)
2053 break;
2054 pos++;
2055 }
2056
2057 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2058}
2059
2060
2061u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2062 const u8 *dst, const char *role)
2063{
2064 struct wpabuf *tlvs;
2065 u64 ret;
2066 const char *subelems;
2067 u8 id = 1;
2068
2069 subelems = os_strchr(role, ' ');
2070 if (subelems == NULL)
2071 return 0;
2072 subelems++;
2073
2074 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2075 if (tlvs == NULL)
2076 return 0;
2077
2078 if (os_strstr(role, "[source]"))
2079 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2080 if (os_strstr(role, "[pri-sink]"))
2081 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2082 if (os_strstr(role, "[sec-sink]"))
2083 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2084 if (os_strstr(role, "[source+sink]"))
2085 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2086
2087 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2088 wpabuf_free(tlvs);
2089 return ret;
2090}
2091
2092#endif /* CONFIG_WIFI_DISPLAY */
2093
2094
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002095int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096{
2097 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002098 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2100 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002101 return p2p_sd_cancel_request(wpa_s->global->p2p,
2102 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103}
2104
2105
2106void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2107 const u8 *dst, u8 dialog_token,
2108 const struct wpabuf *resp_tlvs)
2109{
2110 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2111 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2112 resp_tlvs);
2113 return;
2114 }
2115 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2116 return;
2117 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2118 resp_tlvs);
2119}
2120
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002121#ifdef ANDROID_P2P
2122void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2123#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002125#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126{
2127 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2128 wpa_drv_p2p_service_update(wpa_s);
2129 return;
2130 }
2131 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002132#ifdef ANDROID_P2P
2133 p2p_sd_service_update(wpa_s->global->p2p, action);
2134#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002136#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137}
2138
2139
2140static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2141{
2142 dl_list_del(&bsrv->list);
2143 wpabuf_free(bsrv->query);
2144 wpabuf_free(bsrv->resp);
2145 os_free(bsrv);
2146}
2147
2148
2149static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2150{
2151 dl_list_del(&usrv->list);
2152 os_free(usrv->service);
2153 os_free(usrv);
2154}
2155
2156
2157void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2158{
2159 struct p2p_srv_bonjour *bsrv, *bn;
2160 struct p2p_srv_upnp *usrv, *un;
2161
2162 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2163 struct p2p_srv_bonjour, list)
2164 wpas_p2p_srv_bonjour_free(bsrv);
2165
2166 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2167 struct p2p_srv_upnp, list)
2168 wpas_p2p_srv_upnp_free(usrv);
2169
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002170#ifdef ANDROID_P2P
2171 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2172#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002174#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175}
2176
2177
2178int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2179 struct wpabuf *query, struct wpabuf *resp)
2180{
2181 struct p2p_srv_bonjour *bsrv;
2182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 bsrv = os_zalloc(sizeof(*bsrv));
2184 if (bsrv == NULL)
2185 return -1;
2186 bsrv->query = query;
2187 bsrv->resp = resp;
2188 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2189
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002190#ifdef ANDROID_P2P
2191 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2192#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002194#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195 return 0;
2196}
2197
2198
2199int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2200 const struct wpabuf *query)
2201{
2202 struct p2p_srv_bonjour *bsrv;
2203
2204 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2205 if (bsrv == NULL)
2206 return -1;
2207 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002208#ifdef ANDROID_P2P
2209 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2210#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002212#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213 return 0;
2214}
2215
2216
2217int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2218 const char *service)
2219{
2220 struct p2p_srv_upnp *usrv;
2221
2222 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2223 return 0; /* Already listed */
2224 usrv = os_zalloc(sizeof(*usrv));
2225 if (usrv == NULL)
2226 return -1;
2227 usrv->version = version;
2228 usrv->service = os_strdup(service);
2229 if (usrv->service == NULL) {
2230 os_free(usrv);
2231 return -1;
2232 }
2233 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2234
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002235#ifdef ANDROID_P2P
2236 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2237#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002239#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002240 return 0;
2241}
2242
2243
2244int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2245 const char *service)
2246{
2247 struct p2p_srv_upnp *usrv;
2248
2249 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2250 if (usrv == NULL)
2251 return -1;
2252 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002253#ifdef ANDROID_P2P
2254 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2255#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002257#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258 return 0;
2259}
2260
2261
2262static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2263 const u8 *peer, const char *params,
2264 unsigned int generated_pin)
2265{
2266 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2267 MAC2STR(peer), generated_pin, params);
2268}
2269
2270
2271static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2272 const u8 *peer, const char *params)
2273{
2274 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2275 MAC2STR(peer), params);
2276}
2277
2278
2279void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2280 const u8 *dev_addr, const u8 *pri_dev_type,
2281 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002282 u8 dev_capab, u8 group_capab, const u8 *group_id,
2283 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284{
2285 struct wpa_supplicant *wpa_s = ctx;
2286 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002287 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 u8 empty_dev_type[8];
2289 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002290 struct wpa_supplicant *group = NULL;
2291
2292 if (group_id) {
2293 for (group = wpa_s->global->ifaces; group; group = group->next)
2294 {
2295 struct wpa_ssid *s = group->current_ssid;
2296 if (s != NULL &&
2297 s->mode == WPAS_MODE_P2P_GO &&
2298 group_id_len - ETH_ALEN == s->ssid_len &&
2299 os_memcmp(group_id + ETH_ALEN, s->ssid,
2300 s->ssid_len) == 0)
2301 break;
2302 }
2303 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002304
2305 if (pri_dev_type == NULL) {
2306 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2307 pri_dev_type = empty_dev_type;
2308 }
2309 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2310 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002311 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002312 MAC2STR(dev_addr),
2313 wps_dev_type_bin2str(pri_dev_type, devtype,
2314 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002315 dev_name, supp_config_methods, dev_capab, group_capab,
2316 group ? " group=" : "",
2317 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320 if (config_methods & WPS_CONFIG_DISPLAY) {
2321 generated_pin = wps_generate_pin();
2322 wpas_prov_disc_local_display(wpa_s, peer, params,
2323 generated_pin);
2324 } else if (config_methods & WPS_CONFIG_KEYPAD)
2325 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2326 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2327 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2328 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002329
2330 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2331 P2P_PROV_DISC_SUCCESS,
2332 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333}
2334
2335
2336void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2337{
2338 struct wpa_supplicant *wpa_s = ctx;
2339 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002340 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002342 if (wpa_s->pending_pd_before_join &&
2343 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2344 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2345 wpa_s->pending_pd_before_join = 0;
2346 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2347 "join-existing-group operation");
2348 wpas_p2p_join_start(wpa_s);
2349 return;
2350 }
2351
Dmitry Shmidt04949592012-07-19 12:16:46 -07002352 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2353 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2354 os_snprintf(params, sizeof(params), " peer_go=%d",
2355 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2356 else
2357 params[0] = '\0';
2358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002359 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002360 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361 else if (config_methods & WPS_CONFIG_KEYPAD) {
2362 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002363 wpas_prov_disc_local_display(wpa_s, peer, params,
2364 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002366 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2367 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368
Jouni Malinen75ecf522011-06-27 15:19:46 -07002369 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2370 P2P_PROV_DISC_SUCCESS,
2371 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372}
2373
2374
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002375static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2376 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002377{
2378 struct wpa_supplicant *wpa_s = ctx;
2379
Dmitry Shmidt04949592012-07-19 12:16:46 -07002380 if (wpa_s->p2p_fallback_to_go_neg) {
2381 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2382 "failed - fall back to GO Negotiation");
2383 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2384 return;
2385 }
2386
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002387 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002388 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002389 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2390 "join-existing-group operation (no ACK for PD "
2391 "Req attempts)");
2392 wpas_p2p_join_start(wpa_s);
2393 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002394 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002395
Dmitry Shmidt04949592012-07-19 12:16:46 -07002396 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2397 " p2p_dev_addr=" MACSTR " status=%d",
2398 MAC2STR(peer), status);
2399
Jouni Malinen75ecf522011-06-27 15:19:46 -07002400 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2401 status, 0, 0);
2402}
2403
2404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2406 const u8 *go_dev_addr, const u8 *ssid,
2407 size_t ssid_len, int *go, u8 *group_bssid,
2408 int *force_freq, int persistent_group)
2409{
2410 struct wpa_supplicant *wpa_s = ctx;
2411 struct wpa_ssid *s;
2412 u8 cur_bssid[ETH_ALEN];
2413 int res;
2414 struct wpa_supplicant *grp;
2415
2416 if (!persistent_group) {
2417 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2418 " to join an active group", MAC2STR(sa));
2419 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2420 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2421 == 0 ||
2422 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2423 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2424 "authorized invitation");
2425 goto accept_inv;
2426 }
2427 /*
2428 * Do not accept the invitation automatically; notify user and
2429 * request approval.
2430 */
2431 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2432 }
2433
2434 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2435 if (grp) {
2436 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2437 "running persistent group");
2438 if (*go)
2439 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2440 goto accept_inv;
2441 }
2442
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002443 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2444 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2445 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2446 "invitation to re-invoke a persistent group");
2447 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002448 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2449
2450 for (s = wpa_s->conf->ssid; s; s = s->next) {
2451 if (s->disabled == 2 &&
2452 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2453 s->ssid_len == ssid_len &&
2454 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2455 break;
2456 }
2457
2458 if (!s) {
2459 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2460 " requested reinvocation of an unknown group",
2461 MAC2STR(sa));
2462 return P2P_SC_FAIL_UNKNOWN_GROUP;
2463 }
2464
2465 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2466 *go = 1;
2467 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2468 wpa_printf(MSG_DEBUG, "P2P: The only available "
2469 "interface is already in use - reject "
2470 "invitation");
2471 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2472 }
2473 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2474 } else if (s->mode == WPAS_MODE_P2P_GO) {
2475 *go = 1;
2476 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2477 {
2478 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2479 "interface address for the group");
2480 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2481 }
2482 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2483 ETH_ALEN);
2484 }
2485
2486accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002487 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002489 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2490 wpa_s->assoc_freq) {
2491 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2492 "the channel we are already using");
2493 *force_freq = wpa_s->assoc_freq;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002494 wpas_p2p_set_own_freq_preference(wpa_s, wpa_s->assoc_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495 }
2496
2497 res = wpa_drv_shared_freq(wpa_s);
2498 if (res > 0) {
2499 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2500 "with the channel we are already using on a "
2501 "shared interface");
2502 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002503 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 }
2505
2506 return P2P_SC_SUCCESS;
2507}
2508
2509
2510static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2511 const u8 *ssid, size_t ssid_len,
2512 const u8 *go_dev_addr, u8 status,
2513 int op_freq)
2514{
2515 struct wpa_supplicant *wpa_s = ctx;
2516 struct wpa_ssid *s;
2517
2518 for (s = wpa_s->conf->ssid; s; s = s->next) {
2519 if (s->disabled == 2 &&
2520 s->ssid_len == ssid_len &&
2521 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2522 break;
2523 }
2524
2525 if (status == P2P_SC_SUCCESS) {
2526 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2527 " was accepted; op_freq=%d MHz",
2528 MAC2STR(sa), op_freq);
2529 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002530 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002532 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002534 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002536 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 }
2538 return;
2539 }
2540
2541 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2542 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2543 " was rejected (status %u)", MAC2STR(sa), status);
2544 return;
2545 }
2546
2547 if (!s) {
2548 if (bssid) {
2549 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2550 "sa=" MACSTR " go_dev_addr=" MACSTR
2551 " bssid=" MACSTR " unknown-network",
2552 MAC2STR(sa), MAC2STR(go_dev_addr),
2553 MAC2STR(bssid));
2554 } else {
2555 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2556 "sa=" MACSTR " go_dev_addr=" MACSTR
2557 " unknown-network",
2558 MAC2STR(sa), MAC2STR(go_dev_addr));
2559 }
2560 return;
2561 }
2562
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002563 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2564 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2565 MACSTR " persistent=%d freq=%d",
2566 MAC2STR(sa), s->id, op_freq);
2567 } else {
2568 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2569 MACSTR " persistent=%d", MAC2STR(sa), s->id);
2570 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571}
2572
2573
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002574static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2575 struct wpa_ssid *ssid,
2576 const u8 *peer)
2577{
2578 size_t i;
2579
2580 if (ssid == NULL)
2581 return;
2582
2583 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2584 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2585 ETH_ALEN) == 0)
2586 break;
2587 }
2588 if (i >= ssid->num_p2p_clients) {
2589 if (ssid->mode != WPAS_MODE_P2P_GO &&
2590 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2591 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2592 "due to invitation result", ssid->id);
2593 wpas_notify_network_removed(wpa_s, ssid);
2594 wpa_config_remove_network(wpa_s->conf, ssid->id);
2595 return;
2596 }
2597 return; /* Peer not found in client list */
2598 }
2599
2600 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2601 "group %d client list due to invitation result",
2602 MAC2STR(peer), ssid->id);
2603 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2604 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2605 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2606 ssid->num_p2p_clients--;
2607#ifndef CONFIG_NO_CONFIG_WRITE
2608 if (wpa_s->parent->conf->update_config &&
2609 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2610 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2611#endif /* CONFIG_NO_CONFIG_WRITE */
2612}
2613
2614
2615static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2616 const u8 *peer)
2617{
2618 struct wpa_ssid *ssid;
2619
2620 wpa_s = wpa_s->global->p2p_invite_group;
2621 if (wpa_s == NULL)
2622 return; /* No known invitation group */
2623 ssid = wpa_s->current_ssid;
2624 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2625 !ssid->p2p_persistent_group)
2626 return; /* Not operating as a GO in persistent group */
2627 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2628 ssid->ssid, ssid->ssid_len);
2629 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2630}
2631
2632
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002633static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002634 const struct p2p_channels *channels,
2635 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636{
2637 struct wpa_supplicant *wpa_s = ctx;
2638 struct wpa_ssid *ssid;
2639
2640 if (bssid) {
2641 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2642 "status=%d " MACSTR,
2643 status, MAC2STR(bssid));
2644 } else {
2645 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2646 "status=%d ", status);
2647 }
2648 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2649
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002650 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2651 status, MAC2STR(peer));
2652 if (wpa_s->pending_invite_ssid_id == -1) {
2653 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2654 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002655 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002656 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002658 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2659 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2660 "invitation exchange to indicate readiness for "
2661 "re-invocation");
2662 }
2663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002665 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2666 ssid = wpa_config_get_network(
2667 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2668 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2669 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670 wpas_p2p_remove_pending_group_interface(wpa_s);
2671 return;
2672 }
2673
2674 ssid = wpa_config_get_network(wpa_s->conf,
2675 wpa_s->pending_invite_ssid_id);
2676 if (ssid == NULL) {
2677 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2678 "data matching with invitation");
2679 return;
2680 }
2681
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002682 /*
2683 * The peer could have missed our ctrl::ack frame for Invitation
2684 * Response and continue retransmitting the frame. To reduce the
2685 * likelihood of the peer not getting successful TX status for the
2686 * Invitation Response frame, wait a short time here before starting
2687 * the persistent group so that we will remain on the current channel to
2688 * acknowledge any possible retransmission from the peer.
2689 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002690#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002691 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2692 "starting persistent group");
2693 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002694#else
2695 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2696 "starting persistent group");
2697 os_sleep(0, 100000);
2698#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002700 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002701 ssid->mode == WPAS_MODE_P2P_GO,
2702 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002703 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704}
2705
2706
Dmitry Shmidt04949592012-07-19 12:16:46 -07002707static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2708 unsigned int freq)
2709{
2710 unsigned int i;
2711
2712 if (global->p2p_disallow_freq == NULL)
2713 return 0;
2714
2715 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2716 if (freq >= global->p2p_disallow_freq[i].min &&
2717 freq <= global->p2p_disallow_freq[i].max)
2718 return 1;
2719 }
2720
2721 return 0;
2722}
2723
2724
2725static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2726{
2727 reg->channel[reg->channels] = chan;
2728 reg->channels++;
2729}
2730
2731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2733 struct p2p_channels *chan)
2734{
2735 int i, cla = 0;
2736
2737 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2738 "band");
2739
2740 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2741 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002742 chan->reg_class[cla].channels = 0;
2743 for (i = 0; i < 11; i++) {
2744 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2745 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2746 }
2747 if (chan->reg_class[cla].channels)
2748 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749
2750 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2751 "band");
2752
2753 /* Operating class 115 - 5 GHz, channels 36-48 */
2754 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002755 chan->reg_class[cla].channels = 0;
2756 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2757 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2758 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2759 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2760 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2761 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2762 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2763 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2764 if (chan->reg_class[cla].channels)
2765 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766
2767 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2768 "band");
2769
2770 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2771 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002772 chan->reg_class[cla].channels = 0;
2773 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2774 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2775 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2776 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2777 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2778 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2779 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2780 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2781 if (chan->reg_class[cla].channels)
2782 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783
2784 chan->reg_classes = cla;
2785 return 0;
2786}
2787
2788
2789static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2790 u16 num_modes,
2791 enum hostapd_hw_mode mode)
2792{
2793 u16 i;
2794
2795 for (i = 0; i < num_modes; i++) {
2796 if (modes[i].mode == mode)
2797 return &modes[i];
2798 }
2799
2800 return NULL;
2801}
2802
2803
Dmitry Shmidt04949592012-07-19 12:16:46 -07002804static int has_channel(struct wpa_global *global,
2805 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806{
2807 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002808 unsigned int freq;
2809
2810 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2811 chan * 5;
2812 if (wpas_p2p_disallowed_freq(global, freq))
2813 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814
2815 for (i = 0; i < mode->num_channels; i++) {
2816 if (mode->channels[i].chan == chan) {
2817 if (flags)
2818 *flags = mode->channels[i].flag;
2819 return !(mode->channels[i].flag &
2820 (HOSTAPD_CHAN_DISABLED |
2821 HOSTAPD_CHAN_PASSIVE_SCAN |
2822 HOSTAPD_CHAN_NO_IBSS |
2823 HOSTAPD_CHAN_RADAR));
2824 }
2825 }
2826
2827 return 0;
2828}
2829
2830
2831struct p2p_oper_class_map {
2832 enum hostapd_hw_mode mode;
2833 u8 op_class;
2834 u8 min_chan;
2835 u8 max_chan;
2836 u8 inc;
2837 enum { BW20, BW40PLUS, BW40MINUS } bw;
2838};
2839
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002840static struct p2p_oper_class_map op_class[] = {
2841 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002842#if 0 /* Do not enable HT40 on 2 GHz for now */
2843 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2844 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2845#endif
2846 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2847 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2848 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2849 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2850 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2851 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2852 { -1, 0, 0, 0, 0, BW20 }
2853};
2854
2855
2856static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2857 struct hostapd_hw_modes *mode,
2858 u8 channel, u8 bw)
2859{
2860 int flag;
2861
2862 if (!has_channel(wpa_s->global, mode, channel, &flag))
2863 return -1;
2864 if (bw == BW40MINUS &&
2865 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2866 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2867 return 0;
2868 if (bw == BW40PLUS &&
2869 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2870 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2871 return 0;
2872 return 1;
2873}
2874
2875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002876static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2877 struct p2p_channels *chan)
2878{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002879 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002882 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2884 "of all supported channels; assume dualband "
2885 "support");
2886 return wpas_p2p_default_channels(wpa_s, chan);
2887 }
2888
2889 cla = 0;
2890
2891 for (op = 0; op_class[op].op_class; op++) {
2892 struct p2p_oper_class_map *o = &op_class[op];
2893 u8 ch;
2894 struct p2p_reg_class *reg = NULL;
2895
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002896 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897 if (mode == NULL)
2898 continue;
2899 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002900 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 continue;
2902 if (reg == NULL) {
2903 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2904 "class %u", o->op_class);
2905 reg = &chan->reg_class[cla];
2906 cla++;
2907 reg->reg_class = o->op_class;
2908 }
2909 reg->channel[reg->channels] = ch;
2910 reg->channels++;
2911 }
2912 if (reg) {
2913 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2914 reg->channel, reg->channels);
2915 }
2916 }
2917
2918 chan->reg_classes = cla;
2919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920 return 0;
2921}
2922
2923
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002924int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2925 struct hostapd_hw_modes *mode, u8 channel)
2926{
2927 int op, ret;
2928
2929 for (op = 0; op_class[op].op_class; op++) {
2930 struct p2p_oper_class_map *o = &op_class[op];
2931 u8 ch;
2932
2933 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2934 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2935 o->bw == BW20 || ch != channel)
2936 continue;
2937 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2938 if (ret < 0)
2939 continue;
2940 else if (ret > 0)
2941 return (o->bw == BW40MINUS) ? -1 : 1;
2942 else
2943 return 0;
2944 }
2945 }
2946 return 0;
2947}
2948
2949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2951 size_t buf_len)
2952{
2953 struct wpa_supplicant *wpa_s = ctx;
2954
2955 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2956 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2957 break;
2958 }
2959 if (wpa_s == NULL)
2960 return -1;
2961
2962 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2963}
2964
2965
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002966static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2967{
2968 struct wpa_supplicant *wpa_s = ctx;
2969
2970 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2971 struct wpa_ssid *ssid = wpa_s->current_ssid;
2972 if (ssid == NULL)
2973 continue;
2974 if (ssid->mode != WPAS_MODE_INFRA)
2975 continue;
2976 if (wpa_s->wpa_state != WPA_COMPLETED &&
2977 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2978 continue;
2979 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2980 return 1;
2981 }
2982
2983 return 0;
2984}
2985
2986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002987/**
2988 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2989 * @global: Pointer to global data from wpa_supplicant_init()
2990 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2991 * Returns: 0 on success, -1 on failure
2992 */
2993int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2994{
2995 struct p2p_config p2p;
2996 unsigned int r;
2997 int i;
2998
2999 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3000 return 0;
3001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002 if (global->p2p)
3003 return 0;
3004
3005 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3006 struct p2p_params params;
3007
3008 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3009 os_memset(&params, 0, sizeof(params));
3010 params.dev_name = wpa_s->conf->device_name;
3011 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3012 WPS_DEV_TYPE_LEN);
3013 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3014 os_memcpy(params.sec_dev_type,
3015 wpa_s->conf->sec_device_type,
3016 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3017
3018 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3019 return -1;
3020
3021 return 0;
3022 }
3023
3024 os_memset(&p2p, 0, sizeof(p2p));
3025 p2p.msg_ctx = wpa_s;
3026 p2p.cb_ctx = wpa_s;
3027 p2p.p2p_scan = wpas_p2p_scan;
3028 p2p.send_action = wpas_send_action;
3029 p2p.send_action_done = wpas_send_action_done;
3030 p2p.go_neg_completed = wpas_go_neg_completed;
3031 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3032 p2p.dev_found = wpas_dev_found;
3033 p2p.dev_lost = wpas_dev_lost;
3034 p2p.start_listen = wpas_start_listen;
3035 p2p.stop_listen = wpas_stop_listen;
3036 p2p.send_probe_resp = wpas_send_probe_resp;
3037 p2p.sd_request = wpas_sd_request;
3038 p2p.sd_response = wpas_sd_response;
3039 p2p.prov_disc_req = wpas_prov_disc_req;
3040 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003041 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003042 p2p.invitation_process = wpas_invitation_process;
3043 p2p.invitation_received = wpas_invitation_received;
3044 p2p.invitation_result = wpas_invitation_result;
3045 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003046 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047
3048 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003049 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050 p2p.dev_name = wpa_s->conf->device_name;
3051 p2p.manufacturer = wpa_s->conf->manufacturer;
3052 p2p.model_name = wpa_s->conf->model_name;
3053 p2p.model_number = wpa_s->conf->model_number;
3054 p2p.serial_number = wpa_s->conf->serial_number;
3055 if (wpa_s->wps) {
3056 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3057 p2p.config_methods = wpa_s->wps->config_methods;
3058 }
3059
3060 if (wpa_s->conf->p2p_listen_reg_class &&
3061 wpa_s->conf->p2p_listen_channel) {
3062 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3063 p2p.channel = wpa_s->conf->p2p_listen_channel;
3064 } else {
3065 p2p.reg_class = 81;
3066 /*
3067 * Pick one of the social channels randomly as the listen
3068 * channel.
3069 */
3070 os_get_random((u8 *) &r, sizeof(r));
3071 p2p.channel = 1 + (r % 3) * 5;
3072 }
3073 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3074
3075 if (wpa_s->conf->p2p_oper_reg_class &&
3076 wpa_s->conf->p2p_oper_channel) {
3077 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3078 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3079 p2p.cfg_op_channel = 1;
3080 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3081 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3082
3083 } else {
3084 p2p.op_reg_class = 81;
3085 /*
3086 * Use random operation channel from (1, 6, 11) if no other
3087 * preference is indicated.
3088 */
3089 os_get_random((u8 *) &r, sizeof(r));
3090 p2p.op_channel = 1 + (r % 3) * 5;
3091 p2p.cfg_op_channel = 0;
3092 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3093 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3094 }
3095 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3096 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3097 p2p.country[2] = 0x04;
3098 } else
3099 os_memcpy(p2p.country, "XX\x04", 3);
3100
3101 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3102 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3103 "channel list");
3104 return -1;
3105 }
3106
3107 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3108 WPS_DEV_TYPE_LEN);
3109
3110 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3111 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3112 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3113
3114 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3115 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3116
3117 p2p.max_peers = 100;
3118
3119 if (wpa_s->conf->p2p_ssid_postfix) {
3120 p2p.ssid_postfix_len =
3121 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3122 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3123 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3124 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3125 p2p.ssid_postfix_len);
3126 }
3127
3128 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3129
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003130 p2p.max_listen = wpa_s->max_remain_on_chan;
3131
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003132#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003133 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003134 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003135 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3136 } else {
3137 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003138 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003139 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3140 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003141#endif
3142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 global->p2p = p2p_init(&p2p);
3144 if (global->p2p == NULL)
3145 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003146 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003147
3148 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3149 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3150 continue;
3151 p2p_add_wps_vendor_extension(
3152 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3153 }
3154
3155 return 0;
3156}
3157
3158
3159/**
3160 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3161 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3162 *
3163 * This function deinitialize per-interface P2P data.
3164 */
3165void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3166{
3167 if (wpa_s->driver && wpa_s->drv_priv)
3168 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003169
3170 if (wpa_s->go_params) {
3171 /* Clear any stored provisioning info */
3172 p2p_clear_provisioning_info(
3173 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003174 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003175 }
3176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003177 os_free(wpa_s->go_params);
3178 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3180 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3181 wpa_s->p2p_long_listen = 0;
3182 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3183 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3184 wpas_p2p_remove_pending_group_interface(wpa_s);
3185
3186 /* TODO: remove group interface from the driver if this wpa_s instance
3187 * is on top of a P2P group interface */
3188}
3189
3190
3191/**
3192 * wpas_p2p_deinit_global - Deinitialize global P2P module
3193 * @global: Pointer to global data from wpa_supplicant_init()
3194 *
3195 * This function deinitializes the global (per device) P2P module.
3196 */
3197void wpas_p2p_deinit_global(struct wpa_global *global)
3198{
3199 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003201 wpa_s = global->ifaces;
3202 if (wpa_s)
3203 wpas_p2p_service_flush(wpa_s);
3204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205 if (global->p2p == NULL)
3206 return;
3207
3208 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003209 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3210 wpa_s = wpa_s->next;
3211 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212 tmp = global->ifaces;
3213 while (tmp &&
3214 (tmp == wpa_s ||
3215 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3216 tmp = tmp->next;
3217 }
3218 if (tmp == NULL)
3219 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003220 /* Disconnect from the P2P group and deinit the interface */
3221 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222 }
3223
3224 /*
3225 * Deinit GO data on any possibly remaining interface (if main
3226 * interface is used as GO).
3227 */
3228 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3229 if (wpa_s->ap_iface)
3230 wpas_p2p_group_deinit(wpa_s);
3231 }
3232
3233 p2p_deinit(global->p2p);
3234 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003235 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003236}
3237
3238
3239static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3240{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003241 if (wpa_s->conf->p2p_no_group_iface)
3242 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 if (wpa_s->drv_flags &
3244 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3245 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3246 return 1; /* P2P group requires a new interface in every case
3247 */
3248 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3249 return 0; /* driver does not support concurrent operations */
3250 if (wpa_s->global->ifaces->next)
3251 return 1; /* more that one interface already in use */
3252 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3253 return 1; /* this interface is already in use */
3254 return 0;
3255}
3256
3257
3258static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3259 const u8 *peer_addr,
3260 enum p2p_wps_method wps_method,
3261 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003262 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003263 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003265 if (persistent_group && wpa_s->conf->persistent_reconnect)
3266 persistent_group = 2;
3267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3269 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3270 go_intent, own_interface_addr,
3271 force_freq, persistent_group);
3272 }
3273
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003274 /*
3275 * Increase GO config timeout if HT40 is used since it takes some time
3276 * to scan channels for coex purposes before the BSS can be started.
3277 */
3278 p2p_set_config_timeout(wpa_s->global->p2p,
3279 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003281 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3282 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003283 persistent_group, ssid ? ssid->ssid : NULL,
3284 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003285 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286}
3287
3288
3289static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3290 const u8 *peer_addr,
3291 enum p2p_wps_method wps_method,
3292 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003293 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003294 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003295{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003296 if (persistent_group && wpa_s->conf->persistent_reconnect)
3297 persistent_group = 2;
3298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003299 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3300 return -1;
3301
3302 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3303 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003304 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003305 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306}
3307
3308
3309static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3310{
3311 wpa_s->p2p_join_scan_count++;
3312 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3313 wpa_s->p2p_join_scan_count);
3314 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3315 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3316 " for join operationg - stop join attempt",
3317 MAC2STR(wpa_s->pending_join_iface_addr));
3318 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003319 if (wpa_s->p2p_auto_pd) {
3320 wpa_s->p2p_auto_pd = 0;
3321 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3322 " p2p_dev_addr=" MACSTR " status=N/A",
3323 MAC2STR(wpa_s->pending_join_dev_addr));
3324 return;
3325 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326 wpa_msg(wpa_s->parent, MSG_INFO,
3327 P2P_EVENT_GROUP_FORMATION_FAILURE);
3328 }
3329}
3330
3331
Dmitry Shmidt04949592012-07-19 12:16:46 -07003332static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3333{
3334 struct wpa_supplicant *iface;
3335 int shared_freq;
3336 u8 bssid[ETH_ALEN];
3337
3338 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3339 return 0;
3340
3341 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3342 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3343 continue;
3344 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3345 continue;
3346 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3347 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3348 shared_freq = iface->current_ssid->frequency;
3349 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3350 shared_freq = iface->assoc_freq;
3351 else
3352 shared_freq = 0;
3353
3354 if (shared_freq && freq != shared_freq) {
3355 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3356 "connected on %d MHz - new connection on "
3357 "%d MHz", iface->ifname, shared_freq, freq);
3358 return 1;
3359 }
3360 }
3361
3362 shared_freq = wpa_drv_shared_freq(wpa_s);
3363 if (shared_freq > 0 && shared_freq != freq) {
3364 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3365 "virtual interface connected on %d MHz - new "
3366 "connection on %d MHz", shared_freq, freq);
3367 return 1;
3368 }
3369
3370 return 0;
3371}
3372
3373
3374static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3375 const u8 *peer_dev_addr)
3376{
3377 struct wpa_bss *bss;
3378 int updated;
3379
3380 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3381 if (bss == NULL)
3382 return -1;
3383 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3384 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3385 "last scan");
3386 return 0;
3387 }
3388
3389 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3390 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3391 "%ld.%06ld (%supdated in last scan)",
3392 bss->last_update.sec, bss->last_update.usec,
3393 updated ? "": "not ");
3394
3395 return updated;
3396}
3397
3398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003399static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3400 struct wpa_scan_results *scan_res)
3401{
3402 struct wpa_bss *bss;
3403 int freq;
3404 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003406 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3407
3408 if (wpa_s->global->p2p_disabled)
3409 return;
3410
Dmitry Shmidt04949592012-07-19 12:16:46 -07003411 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3412 scan_res ? (int) scan_res->num : -1,
3413 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414
3415 if (scan_res)
3416 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3417
Dmitry Shmidt04949592012-07-19 12:16:46 -07003418 if (wpa_s->p2p_auto_pd) {
3419 int join = wpas_p2p_peer_go(wpa_s,
3420 wpa_s->pending_join_dev_addr);
3421 if (join == 0 &&
3422 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3423 wpa_s->auto_pd_scan_retry++;
3424 bss = wpa_bss_get_bssid(wpa_s,
3425 wpa_s->pending_join_dev_addr);
3426 if (bss) {
3427 freq = bss->freq;
3428 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3429 "the peer " MACSTR " at %d MHz",
3430 wpa_s->auto_pd_scan_retry,
3431 MAC2STR(wpa_s->
3432 pending_join_dev_addr),
3433 freq);
3434 wpas_p2p_join_scan_req(wpa_s, freq);
3435 return;
3436 }
3437 }
3438
3439 if (join < 0)
3440 join = 0;
3441
3442 wpa_s->p2p_auto_pd = 0;
3443 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3444 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3445 MAC2STR(wpa_s->pending_join_dev_addr), join);
3446 if (p2p_prov_disc_req(wpa_s->global->p2p,
3447 wpa_s->pending_join_dev_addr,
3448 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003449 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003450 wpa_s->p2p_auto_pd = 0;
3451 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3452 " p2p_dev_addr=" MACSTR " status=N/A",
3453 MAC2STR(wpa_s->pending_join_dev_addr));
3454 }
3455 return;
3456 }
3457
3458 if (wpa_s->p2p_auto_join) {
3459 int join = wpas_p2p_peer_go(wpa_s,
3460 wpa_s->pending_join_dev_addr);
3461 if (join < 0) {
3462 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3463 "running a GO -> use GO Negotiation");
3464 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3465 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3466 wpa_s->p2p_persistent_group, 0, 0, 0,
3467 wpa_s->p2p_go_intent,
3468 wpa_s->p2p_connect_freq,
3469 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003470 wpa_s->p2p_pd_before_go_neg,
3471 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003472 return;
3473 }
3474
3475 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3476 "try to join the group", join ? "" :
3477 " in older scan");
3478 if (!join)
3479 wpa_s->p2p_fallback_to_go_neg = 1;
3480 }
3481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3483 wpa_s->pending_join_iface_addr);
3484 if (freq < 0 &&
3485 p2p_get_interface_addr(wpa_s->global->p2p,
3486 wpa_s->pending_join_dev_addr,
3487 iface_addr) == 0 &&
3488 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3489 {
3490 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3491 "address for join from " MACSTR " to " MACSTR
3492 " based on newly discovered P2P peer entry",
3493 MAC2STR(wpa_s->pending_join_iface_addr),
3494 MAC2STR(iface_addr));
3495 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3496 ETH_ALEN);
3497
3498 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3499 wpa_s->pending_join_iface_addr);
3500 }
3501 if (freq >= 0) {
3502 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3503 "from P2P peer table: %d MHz", freq);
3504 }
3505 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3506 if (bss) {
3507 freq = bss->freq;
3508 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3509 "from BSS table: %d MHz", freq);
3510 }
3511 if (freq > 0) {
3512 u16 method;
3513
Dmitry Shmidt04949592012-07-19 12:16:46 -07003514 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3515 wpa_msg(wpa_s->parent, MSG_INFO,
3516 P2P_EVENT_GROUP_FORMATION_FAILURE
3517 "reason=FREQ_CONFLICT");
3518 return;
3519 }
3520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003521 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3522 "prior to joining an existing group (GO " MACSTR
3523 " freq=%u MHz)",
3524 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3525 wpa_s->pending_pd_before_join = 1;
3526
3527 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003528 case WPS_PIN_DISPLAY:
3529 method = WPS_CONFIG_KEYPAD;
3530 break;
3531 case WPS_PIN_KEYPAD:
3532 method = WPS_CONFIG_DISPLAY;
3533 break;
3534 case WPS_PBC:
3535 method = WPS_CONFIG_PUSHBUTTON;
3536 break;
3537 default:
3538 method = 0;
3539 break;
3540 }
3541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003542 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3543 wpa_s->pending_join_dev_addr) ==
3544 method)) {
3545 /*
3546 * We have already performed provision discovery for
3547 * joining the group. Proceed directly to join
3548 * operation without duplicated provision discovery. */
3549 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3550 "with " MACSTR " already done - proceed to "
3551 "join",
3552 MAC2STR(wpa_s->pending_join_dev_addr));
3553 wpa_s->pending_pd_before_join = 0;
3554 goto start;
3555 }
3556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003557 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003558 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003559 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003560 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3561 "Discovery Request before joining an "
3562 "existing group");
3563 wpa_s->pending_pd_before_join = 0;
3564 goto start;
3565 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566 return;
3567 }
3568
3569 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3570 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3571 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3572 wpas_p2p_check_join_scan_limit(wpa_s);
3573 return;
3574
3575start:
3576 /* Start join operation immediately */
3577 wpas_p2p_join_start(wpa_s);
3578}
3579
3580
Dmitry Shmidt04949592012-07-19 12:16:46 -07003581static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003582{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583 int ret;
3584 struct wpa_driver_scan_params params;
3585 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003586 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003587 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003588#ifdef ANDROID_P2P
3589 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003591 /* If freq is not provided, check the operating freq of the GO and do a
3592 * a directed scan to save time
3593 */
3594 if(!freq) {
3595 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3596 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3597 }
3598#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599 os_memset(&params, 0, sizeof(params));
3600
3601 /* P2P Wildcard SSID */
3602 params.num_ssids = 1;
3603 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3604 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3605
3606 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003607 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3608 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3609 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610 if (wps_ie == NULL) {
3611 wpas_p2p_scan_res_join(wpa_s, NULL);
3612 return;
3613 }
3614
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003615 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3616 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617 if (ies == NULL) {
3618 wpabuf_free(wps_ie);
3619 wpas_p2p_scan_res_join(wpa_s, NULL);
3620 return;
3621 }
3622 wpabuf_put_buf(ies, wps_ie);
3623 wpabuf_free(wps_ie);
3624
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003625 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003626
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003627 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628 params.extra_ies = wpabuf_head(ies);
3629 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003630 if (freq > 0) {
3631 freqs[0] = freq;
3632 params.freqs = freqs;
3633 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634
3635 /*
3636 * Run a scan to update BSS table and start Provision Discovery once
3637 * the new scan results become available.
3638 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003639 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003640 if (!ret)
3641 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003642
3643 wpabuf_free(ies);
3644
3645 if (ret) {
3646 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3647 "try again later");
3648 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3649 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3650 wpas_p2p_check_join_scan_limit(wpa_s);
3651 }
3652}
3653
3654
Dmitry Shmidt04949592012-07-19 12:16:46 -07003655static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3656{
3657 struct wpa_supplicant *wpa_s = eloop_ctx;
3658 wpas_p2p_join_scan_req(wpa_s, 0);
3659}
3660
3661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003663 const u8 *dev_addr, enum p2p_wps_method wps_method,
3664 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003665{
3666 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003667 MACSTR " dev " MACSTR ")%s",
3668 MAC2STR(iface_addr), MAC2STR(dev_addr),
3669 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670
Dmitry Shmidt04949592012-07-19 12:16:46 -07003671 wpa_s->p2p_auto_pd = 0;
3672 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3674 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3675 wpa_s->pending_join_wps_method = wps_method;
3676
3677 /* Make sure we are not running find during connection establishment */
3678 wpas_p2p_stop_find(wpa_s);
3679
3680 wpa_s->p2p_join_scan_count = 0;
3681 wpas_p2p_join_scan(wpa_s, NULL);
3682 return 0;
3683}
3684
3685
3686static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3687{
3688 struct wpa_supplicant *group;
3689 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003690 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691
3692 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3693 if (group == NULL)
3694 return -1;
3695 if (group != wpa_s) {
3696 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3697 sizeof(group->p2p_pin));
3698 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003699 } else {
3700 /*
3701 * Need to mark the current interface for p2p_group_formation
3702 * when a separate group interface is not used. This is needed
3703 * to allow p2p_cancel stop a pending p2p_connect-join.
3704 * wpas_p2p_init_group_interface() addresses this for the case
3705 * where a separate group interface is used.
3706 */
3707 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003708 }
3709
3710 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003711 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003712
3713 os_memset(&res, 0, sizeof(res));
3714 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3715 ETH_ALEN);
3716 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003717 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3718 if (bss) {
3719 res.freq = bss->freq;
3720 res.ssid_len = bss->ssid_len;
3721 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3722 }
3723
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003724 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3725 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3726 "starting client");
3727 wpa_drv_cancel_remain_on_channel(wpa_s);
3728 wpa_s->off_channel_freq = 0;
3729 wpa_s->roc_waiting_drv_freq = 0;
3730 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003731 wpas_start_wps_enrollee(group, &res);
3732
3733 /*
3734 * Allow a longer timeout for join-a-running-group than normal 15
3735 * second group formation timeout since the GO may not have authorized
3736 * our connection yet.
3737 */
3738 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3739 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3740 wpa_s, NULL);
3741
3742 return 0;
3743}
3744
3745
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003746static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3747 int *force_freq, int *pref_freq,
3748 int *oper_freq)
3749{
3750 if (freq > 0) {
3751 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3752 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3753 "(%u MHz) is not supported for P2P uses",
3754 freq);
3755 return -3;
3756 }
3757
3758 if (*oper_freq > 0 && freq != *oper_freq &&
3759 !(wpa_s->drv_flags &
3760 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3761 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3762 "on %u MHz while connected on another "
3763 "channel (%u MHz)", freq, *oper_freq);
3764 return -2;
3765 }
3766 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3767 "requested channel (%u MHz)", freq);
3768 *force_freq = freq;
3769 } else if (*oper_freq > 0 &&
3770 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3771 if (!(wpa_s->drv_flags &
3772 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3773 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3774 "while connected on non-P2P supported "
3775 "channel (%u MHz)", *oper_freq);
3776 return -2;
3777 }
3778 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3779 "(%u MHz) not available for P2P - try to use "
3780 "another channel", *oper_freq);
3781 *force_freq = 0;
3782 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3783 (wpa_s->drv_flags &
3784 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3785 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3786 "are already using (%u MHz) on another interface",
3787 *oper_freq);
3788 *pref_freq = *oper_freq;
3789 } else if (*oper_freq > 0) {
3790 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3791 "channel we are already using (%u MHz) on another "
3792 "interface", *oper_freq);
3793 *force_freq = *oper_freq;
3794 }
3795
3796 return 0;
3797}
3798
3799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800/**
3801 * wpas_p2p_connect - Request P2P Group Formation to be started
3802 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3803 * @peer_addr: Address of the peer P2P Device
3804 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3805 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003806 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807 * @join: Whether to join an existing group (as a client) instead of starting
3808 * Group Owner negotiation; @peer_addr is BSSID in that case
3809 * @auth: Whether to only authorize the connection instead of doing that and
3810 * initiating Group Owner negotiation
3811 * @go_intent: GO Intent or -1 to use default
3812 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003813 * @persistent_id: Persistent group credentials to use for forcing GO
3814 * parameters or -1 to generate new values (SSID/passphrase)
3815 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3816 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003817 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003818 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3819 * failure, -2 on failure due to channel not currently available,
3820 * -3 if forced channel is not supported
3821 */
3822int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3823 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003824 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003825 int go_intent, int freq, int persistent_id, int pd,
3826 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003828 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003829 u8 bssid[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003830 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003831 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003832 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003833 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003834
3835 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3836 return -1;
3837
Dmitry Shmidt04949592012-07-19 12:16:46 -07003838 if (persistent_id >= 0) {
3839 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3840 if (ssid == NULL || ssid->disabled != 2 ||
3841 ssid->mode != WPAS_MODE_P2P_GO)
3842 return -1;
3843 }
3844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845 if (go_intent < 0)
3846 go_intent = wpa_s->conf->p2p_go_intent;
3847
3848 if (!auth)
3849 wpa_s->p2p_long_listen = 0;
3850
3851 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003852 wpa_s->p2p_persistent_group = !!persistent_group;
3853 wpa_s->p2p_persistent_id = persistent_id;
3854 wpa_s->p2p_go_intent = go_intent;
3855 wpa_s->p2p_connect_freq = freq;
3856 wpa_s->p2p_fallback_to_go_neg = 0;
3857 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003858 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003859
3860 if (pin)
3861 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3862 else if (wps_method == WPS_PIN_DISPLAY) {
3863 ret = wps_generate_pin();
3864 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3865 ret);
3866 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3867 wpa_s->p2p_pin);
3868 } else
3869 wpa_s->p2p_pin[0] = '\0';
3870
Dmitry Shmidt04949592012-07-19 12:16:46 -07003871 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003872 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3873 if (auth) {
3874 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3875 "connect a running group from " MACSTR,
3876 MAC2STR(peer_addr));
3877 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3878 return ret;
3879 }
3880 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3881 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3882 iface_addr) < 0) {
3883 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3884 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3885 dev_addr);
3886 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003887 if (auto_join) {
3888 os_get_time(&wpa_s->p2p_auto_started);
3889 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3890 "%ld.%06ld",
3891 wpa_s->p2p_auto_started.sec,
3892 wpa_s->p2p_auto_started.usec);
3893 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003894 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003895 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3896 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897 return -1;
3898 return ret;
3899 }
3900
3901 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003902 wpa_s->assoc_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003903 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003904 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 oper_freq = wpa_drv_shared_freq(wpa_s);
3906 if (oper_freq < 0)
3907 oper_freq = 0;
3908 }
3909
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003910 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
3911 &oper_freq);
3912 if (res)
3913 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003914 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003915
3916 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3917
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003918 if (wpa_s->create_p2p_iface) {
3919 /* Prepare to add a new interface for the group */
3920 iftype = WPA_IF_P2P_GROUP;
3921 if (go_intent == 15)
3922 iftype = WPA_IF_P2P_GO;
3923 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3924 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3925 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003926 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003927 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003929 if_addr = wpa_s->pending_interface_addr;
3930 } else
3931 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932
3933 if (auth) {
3934 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003935 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003936 force_freq, persistent_group, ssid,
3937 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938 return -1;
3939 return ret;
3940 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003941
3942 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3943 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003944 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003945 if (wpa_s->create_p2p_iface)
3946 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 return -1;
3948 }
3949 return ret;
3950}
3951
3952
3953/**
3954 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3955 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3956 * @freq: Frequency of the channel in MHz
3957 * @duration: Duration of the stay on the channel in milliseconds
3958 *
3959 * This callback is called when the driver indicates that it has started the
3960 * requested remain-on-channel duration.
3961 */
3962void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3963 unsigned int freq, unsigned int duration)
3964{
3965 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3966 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3968 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3969 wpa_s->pending_listen_duration);
3970 wpa_s->pending_listen_freq = 0;
3971 }
3972}
3973
3974
3975static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3976 unsigned int timeout)
3977{
3978 /* Limit maximum Listen state time based on driver limitation. */
3979 if (timeout > wpa_s->max_remain_on_chan)
3980 timeout = wpa_s->max_remain_on_chan;
3981
3982 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3983 return wpa_drv_p2p_listen(wpa_s, timeout);
3984
3985 return p2p_listen(wpa_s->global->p2p, timeout);
3986}
3987
3988
3989/**
3990 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3991 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3992 * @freq: Frequency of the channel in MHz
3993 *
3994 * This callback is called when the driver indicates that a remain-on-channel
3995 * operation has been completed, i.e., the duration on the requested channel
3996 * has timed out.
3997 */
3998void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3999 unsigned int freq)
4000{
4001 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4002 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004003 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4005 return;
4006 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4007 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004008 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004009 return;
4010 if (wpa_s->p2p_long_listen > 0)
4011 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4012 if (wpa_s->p2p_long_listen > 0) {
4013 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4014 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4015 }
4016}
4017
4018
4019/**
4020 * wpas_p2p_group_remove - Remove a P2P group
4021 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4022 * @ifname: Network interface name of the group interface or "*" to remove all
4023 * groups
4024 * Returns: 0 on success, -1 on failure
4025 *
4026 * This function is used to remove a P2P group. This can be used to disconnect
4027 * from a group in which the local end is a P2P Client or to end a P2P Group in
4028 * case the local end is the Group Owner. If a virtual network interface was
4029 * created for this group, that interface will be removed. Otherwise, only the
4030 * configured P2P group network will be removed from the interface.
4031 */
4032int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4033{
4034 struct wpa_global *global = wpa_s->global;
4035
4036 if (os_strcmp(ifname, "*") == 0) {
4037 struct wpa_supplicant *prev;
4038 wpa_s = global->ifaces;
4039 while (wpa_s) {
4040 prev = wpa_s;
4041 wpa_s = wpa_s->next;
4042 wpas_p2p_disconnect(prev);
4043 }
4044 return 0;
4045 }
4046
4047 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4048 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4049 break;
4050 }
4051
4052 return wpas_p2p_disconnect(wpa_s);
4053}
4054
4055
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004056static int freq_included(const struct p2p_channels *channels, unsigned int freq)
4057{
4058 if (channels == NULL)
4059 return 1; /* Assume no restrictions */
4060 return p2p_channels_includes_freq(channels, freq);
4061}
4062
4063
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004064static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4065 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004066 int freq, int ht40,
4067 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004068{
4069 u8 bssid[ETH_ALEN];
4070 int res;
4071
4072 os_memset(params, 0, sizeof(*params));
4073 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004074 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004076 if (!freq_included(channels, freq)) {
4077 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4078 "accepted", freq);
4079 return -1;
4080 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4082 "frequency %d MHz", freq);
4083 params->freq = freq;
4084 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4085 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004086 wpa_s->conf->p2p_oper_channel <= 11 &&
4087 freq_included(channels,
4088 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4090 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4091 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004092 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4093 wpa_s->conf->p2p_oper_reg_class == 116 ||
4094 wpa_s->conf->p2p_oper_reg_class == 117 ||
4095 wpa_s->conf->p2p_oper_reg_class == 124 ||
4096 wpa_s->conf->p2p_oper_reg_class == 126 ||
4097 wpa_s->conf->p2p_oper_reg_class == 127) &&
4098 freq_included(channels,
4099 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004100 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4101 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4102 "frequency %d MHz", params->freq);
4103 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4104 wpa_s->best_overall_freq > 0 &&
4105 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004106 wpa_s->best_overall_freq) &&
4107 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004108 params->freq = wpa_s->best_overall_freq;
4109 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4110 "channel %d MHz", params->freq);
4111 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4112 wpa_s->best_24_freq > 0 &&
4113 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004114 wpa_s->best_24_freq) &&
4115 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116 params->freq = wpa_s->best_24_freq;
4117 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4118 "channel %d MHz", params->freq);
4119 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4120 wpa_s->best_5_freq > 0 &&
4121 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004122 wpa_s->best_5_freq) &&
4123 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124 params->freq = wpa_s->best_5_freq;
4125 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4126 "channel %d MHz", params->freq);
4127 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004128 int chan;
4129 for (chan = 0; chan < 11; chan++) {
4130 params->freq = 2412 + chan * 5;
4131 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004132 params->freq) &&
4133 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004134 break;
4135 }
4136 if (chan == 11) {
4137 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4138 "allowed");
4139 return -1;
4140 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004141 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4142 "known)", params->freq);
4143 }
4144
4145 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4146 wpa_s->assoc_freq && !freq) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004147 if (!p2p_supported_freq(wpa_s->global->p2p, wpa_s->assoc_freq)
4148 || !freq_included(channels, wpa_s->assoc_freq)) {
4149 if (wpa_s->drv_flags &
4150 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4151 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4152 "the channel we are already using "
4153 "(%u MHz) - allow multi-channel "
4154 "concurrency", wpa_s->assoc_freq);
4155 } else {
4156 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4157 "the channel we are already using "
4158 "(%u MHz)", wpa_s->assoc_freq);
4159 return -1;
4160 }
4161 } else {
4162 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we "
4163 "are already using (%u MHz)",
4164 wpa_s->assoc_freq);
4165 params->freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004166 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004167 }
4168
4169 res = wpa_drv_shared_freq(wpa_s);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004170 if (res > 0 && !freq &&
4171 (!p2p_supported_freq(wpa_s->global->p2p, res) ||
4172 !freq_included(channels, res))) {
4173 if (wpa_s->drv_flags &
4174 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4175 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4176 "channel we are already using on a shared "
4177 "interface (%u MHz) - allow multi-channel "
4178 "concurrency", res);
4179 } else {
4180 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4181 "channel we are already using on a shared "
4182 "interface (%u MHz)", res);
4183 return -1;
4184 }
4185 } else if (res > 0 && !freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004186 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4187 "already using on a shared interface");
4188 params->freq = res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004189 if (!freq_included(channels, params->freq)) {
4190 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4191 "accepted", params->freq);
4192 return -1;
4193 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004194 } else if (res > 0 && freq != res &&
4195 !(wpa_s->drv_flags &
4196 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4197 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4198 "while connected on another channel (%u MHz)",
4199 freq, res);
4200 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004202
4203 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004204}
4205
4206
4207static struct wpa_supplicant *
4208wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4209 int go)
4210{
4211 struct wpa_supplicant *group_wpa_s;
4212
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004213 if (!wpas_p2p_create_iface(wpa_s)) {
4214 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4215 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004216 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004217 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218
4219 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004220 WPA_IF_P2P_CLIENT) < 0) {
4221 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004222 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004223 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004224 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4225 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004226 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4227 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004228 wpas_p2p_remove_pending_group_interface(wpa_s);
4229 return NULL;
4230 }
4231
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004232 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4233 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234 return group_wpa_s;
4235}
4236
4237
4238/**
4239 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4240 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4241 * @persistent_group: Whether to create a persistent group
4242 * @freq: Frequency for the group or 0 to indicate no hardcoding
4243 * Returns: 0 on success, -1 on failure
4244 *
4245 * This function creates a new P2P group with the local end as the Group Owner,
4246 * i.e., without using Group Owner Negotiation.
4247 */
4248int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004249 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250{
4251 struct p2p_go_neg_results params;
4252 unsigned int r;
4253
4254 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4255 return -1;
4256
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004257 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004258 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004259 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261 if (freq == 2) {
4262 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4263 "band");
4264 if (wpa_s->best_24_freq > 0 &&
4265 p2p_supported_freq(wpa_s->global->p2p,
4266 wpa_s->best_24_freq)) {
4267 freq = wpa_s->best_24_freq;
4268 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4269 "channel: %d MHz", freq);
4270 } else {
4271 os_get_random((u8 *) &r, sizeof(r));
4272 freq = 2412 + (r % 3) * 25;
4273 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4274 "channel: %d MHz", freq);
4275 }
4276 }
4277
4278 if (freq == 5) {
4279 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4280 "band");
4281 if (wpa_s->best_5_freq > 0 &&
4282 p2p_supported_freq(wpa_s->global->p2p,
4283 wpa_s->best_5_freq)) {
4284 freq = wpa_s->best_5_freq;
4285 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4286 "channel: %d MHz", freq);
4287 } else {
4288 os_get_random((u8 *) &r, sizeof(r));
4289 freq = 5180 + (r % 4) * 20;
4290 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4291 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4292 "5 GHz channel for P2P group");
4293 return -1;
4294 }
4295 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4296 "channel: %d MHz", freq);
4297 }
4298 }
4299
4300 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4301 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4302 "(%u MHz) is not supported for P2P uses",
4303 freq);
4304 return -1;
4305 }
4306
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004307 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004308 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004309 if (params.freq &&
4310 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4311 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4312 "(%u MHz) is not supported for P2P uses",
4313 params.freq);
4314 return -1;
4315 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004316 p2p_go_params(wpa_s->global->p2p, &params);
4317 params.persistent_group = persistent_group;
4318
4319 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4320 if (wpa_s == NULL)
4321 return -1;
4322 wpas_start_wps_go(wpa_s, &params, 0);
4323
4324 return 0;
4325}
4326
4327
4328static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4329 struct wpa_ssid *params, int addr_allocated)
4330{
4331 struct wpa_ssid *ssid;
4332
4333 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4334 if (wpa_s == NULL)
4335 return -1;
4336
4337 wpa_supplicant_ap_deinit(wpa_s);
4338
4339 ssid = wpa_config_add_network(wpa_s->conf);
4340 if (ssid == NULL)
4341 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004342 wpa_config_set_network_defaults(ssid);
4343 ssid->temporary = 1;
4344 ssid->proto = WPA_PROTO_RSN;
4345 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4346 ssid->group_cipher = WPA_CIPHER_CCMP;
4347 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4348 ssid->ssid = os_malloc(params->ssid_len);
4349 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350 wpa_config_remove_network(wpa_s->conf, ssid->id);
4351 return -1;
4352 }
4353 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4354 ssid->ssid_len = params->ssid_len;
4355 ssid->p2p_group = 1;
4356 ssid->export_keys = 1;
4357 if (params->psk_set) {
4358 os_memcpy(ssid->psk, params->psk, 32);
4359 ssid->psk_set = 1;
4360 }
4361 if (params->passphrase)
4362 ssid->passphrase = os_strdup(params->passphrase);
4363
4364 wpa_supplicant_select_network(wpa_s, ssid);
4365
4366 wpa_s->show_group_started = 1;
4367
4368 return 0;
4369}
4370
4371
4372int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4373 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004374 int freq, int ht40,
4375 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004376{
4377 struct p2p_go_neg_results params;
4378 int go = 0;
4379
4380 if (ssid->disabled != 2 || ssid->ssid == NULL)
4381 return -1;
4382
4383 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4384 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4385 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4386 "already running");
4387 return 0;
4388 }
4389
4390 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004391 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004392
Dmitry Shmidt04949592012-07-19 12:16:46 -07004393 wpa_s->p2p_fallback_to_go_neg = 0;
4394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395 if (ssid->mode == WPAS_MODE_INFRA)
4396 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4397
4398 if (ssid->mode != WPAS_MODE_P2P_GO)
4399 return -1;
4400
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004401 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004402 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004403
4404 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004405 params.psk_set = ssid->psk_set;
4406 if (params.psk_set)
4407 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004408 if (ssid->passphrase) {
4409 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4410 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4411 "persistent group");
4412 return -1;
4413 }
4414 os_strlcpy(params.passphrase, ssid->passphrase,
4415 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004416 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4418 params.ssid_len = ssid->ssid_len;
4419 params.persistent_group = 1;
4420
4421 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4422 if (wpa_s == NULL)
4423 return -1;
4424
4425 wpas_start_wps_go(wpa_s, &params, 0);
4426
4427 return 0;
4428}
4429
4430
4431static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4432 struct wpabuf *proberesp_ies)
4433{
4434 struct wpa_supplicant *wpa_s = ctx;
4435 if (wpa_s->ap_iface) {
4436 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004437 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4438 wpabuf_free(beacon_ies);
4439 wpabuf_free(proberesp_ies);
4440 return;
4441 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004442 if (beacon_ies) {
4443 wpabuf_free(hapd->p2p_beacon_ie);
4444 hapd->p2p_beacon_ie = beacon_ies;
4445 }
4446 wpabuf_free(hapd->p2p_probe_resp_ie);
4447 hapd->p2p_probe_resp_ie = proberesp_ies;
4448 } else {
4449 wpabuf_free(beacon_ies);
4450 wpabuf_free(proberesp_ies);
4451 }
4452 wpa_supplicant_ap_update_beacon(wpa_s);
4453}
4454
4455
4456static void wpas_p2p_idle_update(void *ctx, int idle)
4457{
4458 struct wpa_supplicant *wpa_s = ctx;
4459 if (!wpa_s->ap_iface)
4460 return;
4461 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004462 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004463 wpas_p2p_set_group_idle_timeout(wpa_s);
4464 else
4465 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4466}
4467
4468
4469struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004470 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004471{
4472 struct p2p_group *group;
4473 struct p2p_group_config *cfg;
4474
4475 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4476 return NULL;
4477 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4478 return NULL;
4479
4480 cfg = os_zalloc(sizeof(*cfg));
4481 if (cfg == NULL)
4482 return NULL;
4483
Dmitry Shmidt04949592012-07-19 12:16:46 -07004484 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004485 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004486 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004487 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004488 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4489 if (wpa_s->max_stations &&
4490 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4491 cfg->max_clients = wpa_s->max_stations;
4492 else
4493 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004494 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4495 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004496 cfg->cb_ctx = wpa_s;
4497 cfg->ie_update = wpas_p2p_ie_update;
4498 cfg->idle_update = wpas_p2p_idle_update;
4499
4500 group = p2p_group_init(wpa_s->global->p2p, cfg);
4501 if (group == NULL)
4502 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004503 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 p2p_group_notif_formation_done(group);
4505 wpa_s->p2p_group = group;
4506 return group;
4507}
4508
4509
4510void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4511 int registrar)
4512{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004513 struct wpa_ssid *ssid = wpa_s->current_ssid;
4514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515 if (!wpa_s->p2p_in_provisioning) {
4516 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4517 "provisioning not in progress");
4518 return;
4519 }
4520
Dmitry Shmidt04949592012-07-19 12:16:46 -07004521 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4522 u8 go_dev_addr[ETH_ALEN];
4523 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4524 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4525 ssid->ssid_len);
4526 /* Clear any stored provisioning info */
4527 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4528 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004530 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4531 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004532 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4533 /*
4534 * Use a separate timeout for initial data connection to
4535 * complete to allow the group to be removed automatically if
4536 * something goes wrong in this step before the P2P group idle
4537 * timeout mechanism is taken into use.
4538 */
4539 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4540 wpas_p2p_group_formation_timeout,
4541 wpa_s->parent, NULL);
4542 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543 if (wpa_s->global->p2p)
4544 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4545 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4546 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4547 wpas_group_formation_completed(wpa_s, 1);
4548}
4549
4550
Jouni Malinen75ecf522011-06-27 15:19:46 -07004551void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4552 struct wps_event_fail *fail)
4553{
4554 if (!wpa_s->p2p_in_provisioning) {
4555 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4556 "provisioning not in progress");
4557 return;
4558 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004559
4560 if (wpa_s->go_params) {
4561 p2p_clear_provisioning_info(
4562 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004563 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004564 }
4565
Jouni Malinen75ecf522011-06-27 15:19:46 -07004566 wpas_notify_p2p_wps_failed(wpa_s, fail);
4567}
4568
4569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004571 const char *config_method,
4572 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573{
4574 u16 config_methods;
4575
Dmitry Shmidt04949592012-07-19 12:16:46 -07004576 wpa_s->p2p_fallback_to_go_neg = 0;
4577 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004578 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004579 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004580 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004581 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004582 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4583 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004585 else {
4586 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004588 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589
Dmitry Shmidt04949592012-07-19 12:16:46 -07004590 if (use == WPAS_P2P_PD_AUTO) {
4591 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4592 wpa_s->pending_pd_config_methods = config_methods;
4593 wpa_s->p2p_auto_pd = 1;
4594 wpa_s->p2p_auto_join = 0;
4595 wpa_s->pending_pd_before_join = 0;
4596 wpa_s->auto_pd_scan_retry = 0;
4597 wpas_p2p_stop_find(wpa_s);
4598 wpa_s->p2p_join_scan_count = 0;
4599 os_get_time(&wpa_s->p2p_auto_started);
4600 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4601 wpa_s->p2p_auto_started.sec,
4602 wpa_s->p2p_auto_started.usec);
4603 wpas_p2p_join_scan(wpa_s, NULL);
4604 return 0;
4605 }
4606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4608 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004609 config_methods,
4610 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 }
4612
4613 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4614 return -1;
4615
4616 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004617 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004618 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619}
4620
4621
4622int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4623 char *end)
4624{
4625 return p2p_scan_result_text(ies, ies_len, buf, end);
4626}
4627
4628
4629static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4630{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004631 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 return;
4633
4634 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4635 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004636 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004637}
4638
4639
4640int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4641 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004642 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004643 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004644{
4645 wpas_p2p_clear_pending_action_tx(wpa_s);
4646 wpa_s->p2p_long_listen = 0;
4647
4648 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4649 return wpa_drv_p2p_find(wpa_s, timeout, type);
4650
Dmitry Shmidt04949592012-07-19 12:16:46 -07004651 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4652 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004653 return -1;
4654
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004655 wpa_supplicant_cancel_sched_scan(wpa_s);
4656
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004657 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004658 num_req_dev_types, req_dev_types, dev_id,
4659 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004660}
4661
4662
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004663static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004664{
4665 wpas_p2p_clear_pending_action_tx(wpa_s);
4666 wpa_s->p2p_long_listen = 0;
4667 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4668 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004669 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670
4671 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4672 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004673 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 }
4675
4676 if (wpa_s->global->p2p)
4677 p2p_stop_find(wpa_s->global->p2p);
4678
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004679 return 0;
4680}
4681
4682
4683void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4684{
4685 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4686 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004687 wpas_p2p_remove_pending_group_interface(wpa_s);
4688}
4689
4690
4691static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4692{
4693 struct wpa_supplicant *wpa_s = eloop_ctx;
4694 wpa_s->p2p_long_listen = 0;
4695}
4696
4697
4698int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4699{
4700 int res;
4701
4702 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4703 return -1;
4704
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004705 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004706 wpas_p2p_clear_pending_action_tx(wpa_s);
4707
4708 if (timeout == 0) {
4709 /*
4710 * This is a request for unlimited Listen state. However, at
4711 * least for now, this is mapped to a Listen state for one
4712 * hour.
4713 */
4714 timeout = 3600;
4715 }
4716 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4717 wpa_s->p2p_long_listen = 0;
4718
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004719 /*
4720 * Stop previous find/listen operation to avoid trying to request a new
4721 * remain-on-channel operation while the driver is still running the
4722 * previous one.
4723 */
4724 if (wpa_s->global->p2p)
4725 p2p_stop_find(wpa_s->global->p2p);
4726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4728 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4729 wpa_s->p2p_long_listen = timeout * 1000;
4730 eloop_register_timeout(timeout, 0,
4731 wpas_p2p_long_listen_timeout,
4732 wpa_s, NULL);
4733 }
4734
4735 return res;
4736}
4737
4738
4739int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4740 u8 *buf, size_t len, int p2p_group)
4741{
4742 struct wpabuf *p2p_ie;
4743 int ret;
4744
4745 if (wpa_s->global->p2p_disabled)
4746 return -1;
4747 if (wpa_s->global->p2p == NULL)
4748 return -1;
4749 if (bss == NULL)
4750 return -1;
4751
4752 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4753 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4754 p2p_group, p2p_ie);
4755 wpabuf_free(p2p_ie);
4756
4757 return ret;
4758}
4759
4760
4761int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004762 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004763 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004764{
4765 if (wpa_s->global->p2p_disabled)
4766 return 0;
4767 if (wpa_s->global->p2p == NULL)
4768 return 0;
4769
Dmitry Shmidt04949592012-07-19 12:16:46 -07004770 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4771 ie, ie_len)) {
4772 case P2P_PREQ_NOT_P2P:
4773 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4774 ssi_signal);
4775 /* fall through */
4776 case P2P_PREQ_MALFORMED:
4777 case P2P_PREQ_NOT_LISTEN:
4778 case P2P_PREQ_NOT_PROCESSED:
4779 default: /* make gcc happy */
4780 return 0;
4781 case P2P_PREQ_PROCESSED:
4782 return 1;
4783 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004784}
4785
4786
4787void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4788 const u8 *sa, const u8 *bssid,
4789 u8 category, const u8 *data, size_t len, int freq)
4790{
4791 if (wpa_s->global->p2p_disabled)
4792 return;
4793 if (wpa_s->global->p2p == NULL)
4794 return;
4795
4796 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4797 freq);
4798}
4799
4800
4801void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4802{
4803 if (wpa_s->global->p2p_disabled)
4804 return;
4805 if (wpa_s->global->p2p == NULL)
4806 return;
4807
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004808 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809}
4810
4811
4812void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4813{
4814 p2p_group_deinit(wpa_s->p2p_group);
4815 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004816
4817 wpa_s->ap_configured_cb = NULL;
4818 wpa_s->ap_configured_cb_ctx = NULL;
4819 wpa_s->ap_configured_cb_data = NULL;
4820 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004821}
4822
4823
4824int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4825{
4826 wpa_s->p2p_long_listen = 0;
4827
4828 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4829 return wpa_drv_p2p_reject(wpa_s, addr);
4830
4831 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4832 return -1;
4833
4834 return p2p_reject(wpa_s->global->p2p, addr);
4835}
4836
4837
4838/* Invite to reinvoke a persistent group */
4839int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004840 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004841 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004842{
4843 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004844 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004845 int force_freq = 0, oper_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004846 int res;
4847
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004848 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004849 if (peer_addr)
4850 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4851 else
4852 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004853
Jouni Malinen31be0a42012-08-31 21:20:51 +03004854 wpa_s->p2p_persistent_go_freq = freq;
4855 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856 if (ssid->mode == WPAS_MODE_P2P_GO) {
4857 role = P2P_INVITE_ROLE_GO;
4858 if (peer_addr == NULL) {
4859 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4860 "address in invitation command");
4861 return -1;
4862 }
4863 if (wpas_p2p_create_iface(wpa_s)) {
4864 if (wpas_p2p_add_group_interface(wpa_s,
4865 WPA_IF_P2P_GO) < 0) {
4866 wpa_printf(MSG_ERROR, "P2P: Failed to "
4867 "allocate a new interface for the "
4868 "group");
4869 return -1;
4870 }
4871 bssid = wpa_s->pending_interface_addr;
4872 } else
4873 bssid = wpa_s->own_addr;
4874 } else {
4875 role = P2P_INVITE_ROLE_CLIENT;
4876 peer_addr = ssid->bssid;
4877 }
4878 wpa_s->pending_invite_ssid_id = ssid->id;
4879
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004880 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4881 wpa_s->assoc_freq) {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004882 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004883 if (bssid == NULL)
4884 bssid = bssid_buf;
4885 } else {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004886 oper_freq = wpa_drv_shared_freq(wpa_s);
4887 if (oper_freq < 0)
4888 oper_freq = 0;
4889 }
4890
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004891 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4892 &oper_freq);
4893 if (res)
4894 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4897 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4898 ssid->ssid, ssid->ssid_len,
4899 go_dev_addr, 1);
4900
4901 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4902 return -1;
4903
4904 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004905 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4906 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907}
4908
4909
4910/* Invite to join an active group */
4911int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4912 const u8 *peer_addr, const u8 *go_dev_addr)
4913{
4914 struct wpa_global *global = wpa_s->global;
4915 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004916 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004917 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004918 int persistent;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004919 int force_freq = 0, oper_freq = 0, pref_freq = 0;
4920 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921
Jouni Malinen31be0a42012-08-31 21:20:51 +03004922 wpa_s->p2p_persistent_go_freq = 0;
4923 wpa_s->p2p_go_ht40 = 0;
4924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004925 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4926 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4927 break;
4928 }
4929 if (wpa_s == NULL) {
4930 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4931 return -1;
4932 }
4933
4934 ssid = wpa_s->current_ssid;
4935 if (ssid == NULL) {
4936 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4937 "invitation");
4938 return -1;
4939 }
4940
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004941 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004942 persistent = ssid->p2p_persistent_group &&
4943 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4944 ssid->ssid, ssid->ssid_len);
4945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004946 if (ssid->mode == WPAS_MODE_P2P_GO) {
4947 role = P2P_INVITE_ROLE_ACTIVE_GO;
4948 bssid = wpa_s->own_addr;
4949 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004950 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951 } else {
4952 role = P2P_INVITE_ROLE_CLIENT;
4953 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4954 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4955 "invite to current group");
4956 return -1;
4957 }
4958 bssid = wpa_s->bssid;
4959 if (go_dev_addr == NULL &&
4960 !is_zero_ether_addr(wpa_s->go_dev_addr))
4961 go_dev_addr = wpa_s->go_dev_addr;
4962 }
4963 wpa_s->parent->pending_invite_ssid_id = -1;
4964
4965 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4966 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4967 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004968 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004969
4970 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4971 return -1;
4972
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004973 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4974 wpa_s->assoc_freq) {
4975 oper_freq = wpa_s->assoc_freq;
4976 if (bssid == NULL)
4977 bssid = bssid_buf;
4978 } else {
4979 oper_freq = wpa_drv_shared_freq(wpa_s);
4980 if (oper_freq < 0)
4981 oper_freq = 0;
4982 }
4983
4984 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
4985 &oper_freq);
4986 if (res)
4987 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004988 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004991 ssid->ssid, ssid->ssid_len, force_freq,
4992 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993}
4994
4995
4996void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4997{
4998 struct wpa_ssid *ssid = wpa_s->current_ssid;
4999 const char *ssid_txt;
5000 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005001 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005002 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005003 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004
Dmitry Shmidt04949592012-07-19 12:16:46 -07005005 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5006 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5007 wpa_s->parent, NULL);
5008 }
5009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005011 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005012
5013 wpa_s->show_group_started = 0;
5014
5015 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5016 os_memset(go_dev_addr, 0, ETH_ALEN);
5017 if (ssid->bssid_set)
5018 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5019 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5020 ssid->ssid_len);
5021 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5022
5023 if (wpa_s->global->p2p_group_formation == wpa_s)
5024 wpa_s->global->p2p_group_formation = NULL;
5025
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005026 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5027 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028 if (ssid->passphrase == NULL && ssid->psk_set) {
5029 char psk[65];
5030 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5031 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5032 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
5033 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005034 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005035 MAC2STR(go_dev_addr),
5036 persistent ? " [PERSISTENT]" : "");
5037 } else {
5038 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5039 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
5040 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005041 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005042 ssid->passphrase ? ssid->passphrase : "",
5043 MAC2STR(go_dev_addr),
5044 persistent ? " [PERSISTENT]" : "");
5045 }
5046
5047 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005048 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5049 ssid, go_dev_addr);
5050 if (network_id < 0)
5051 network_id = ssid->id;
5052 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005053
5054done:
Jouni Malinendc7b7132012-09-14 12:53:47 -07005055 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005056 wpa_s->global->p2p != NULL) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07005057 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005058 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
5059 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
5060 "continued after successful connection");
5061 p2p_increase_search_delay(
5062 wpa_s->global->p2p,
5063 wpas_p2p_search_delay(wpa_s));
5064 }
5065 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066}
5067
5068
5069int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5070 u32 interval1, u32 duration2, u32 interval2)
5071{
5072 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5073 return -1;
5074 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5075 return -1;
5076
5077 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5078 wpa_s->current_ssid == NULL ||
5079 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5080 return -1;
5081
5082 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5083 wpa_s->own_addr, wpa_s->assoc_freq,
5084 duration1, interval1, duration2, interval2);
5085}
5086
5087
5088int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5089 unsigned int interval)
5090{
5091 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5092 return -1;
5093
5094 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5095 return -1;
5096
5097 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5098}
5099
5100
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005101static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5102{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005103 if (wpa_s->current_ssid == NULL) {
5104 /*
5105 * current_ssid can be cleared when P2P client interface gets
5106 * disconnected, so assume this interface was used as P2P
5107 * client.
5108 */
5109 return 1;
5110 }
5111 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005112 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5113}
5114
5115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005116static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5117{
5118 struct wpa_supplicant *wpa_s = eloop_ctx;
5119
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005120 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5122 "disabled");
5123 return;
5124 }
5125
Dmitry Shmidt04949592012-07-19 12:16:46 -07005126 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5127 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005128 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005129}
5130
5131
5132static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5133{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005134 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135
Dmitry Shmidt04949592012-07-19 12:16:46 -07005136 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5137 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5140 return;
5141
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005142 timeout = wpa_s->conf->p2p_group_idle;
5143 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5144 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5145 timeout = P2P_MAX_CLIENT_IDLE;
5146
5147 if (timeout == 0)
5148 return;
5149
Dmitry Shmidt04949592012-07-19 12:16:46 -07005150 if (timeout < 0) {
5151 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5152 timeout = 0; /* special client mode no-timeout */
5153 else
5154 return;
5155 }
5156
5157 if (wpa_s->p2p_in_provisioning) {
5158 /*
5159 * Use the normal group formation timeout during the
5160 * provisioning phase to avoid terminating this process too
5161 * early due to group idle timeout.
5162 */
5163 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5164 "during provisioning");
5165 return;
5166 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07005167#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07005168 if (wpa_s->show_group_started) {
5169 /*
5170 * Use the normal group formation timeout between the end of
5171 * the provisioning phase and completion of 4-way handshake to
5172 * avoid terminating this process too early due to group idle
5173 * timeout.
5174 */
5175 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5176 "while waiting for initial 4-way handshake to "
5177 "complete");
5178 return;
5179 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07005180#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07005181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005182 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005183 timeout);
5184 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5185 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005186}
5187
5188
Jouni Malinen2b89da82012-08-31 22:04:41 +03005189/* Returns 1 if the interface was removed */
5190int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5191 u16 reason_code, const u8 *ie, size_t ie_len,
5192 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005193{
5194 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005195 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005196 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005197 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005198
Dmitry Shmidt04949592012-07-19 12:16:46 -07005199 if (!locally_generated)
5200 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5201 ie_len);
5202
5203 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5204 wpa_s->current_ssid &&
5205 wpa_s->current_ssid->p2p_group &&
5206 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5207 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5208 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005209 if (wpas_p2p_group_delete(wpa_s,
5210 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5211 > 0)
5212 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005213 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005214
5215 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005216}
5217
5218
5219void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005220 u16 reason_code, const u8 *ie, size_t ie_len,
5221 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005222{
5223 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5224 return;
5225 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5226 return;
5227
Dmitry Shmidt04949592012-07-19 12:16:46 -07005228 if (!locally_generated)
5229 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5230 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005231}
5232
5233
5234void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5235{
5236 struct p2p_data *p2p = wpa_s->global->p2p;
5237
5238 if (p2p == NULL)
5239 return;
5240
5241 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5242 return;
5243
5244 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5245 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5246
5247 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5248 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5249
5250 if (wpa_s->wps &&
5251 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5252 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5253
5254 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5255 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5256
5257 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5258 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5259 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5260 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5261 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5262 }
5263
5264 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5265 p2p_set_sec_dev_types(p2p,
5266 (void *) wpa_s->conf->sec_device_type,
5267 wpa_s->conf->num_sec_device_types);
5268
5269 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5270 int i;
5271 p2p_remove_wps_vendor_extensions(p2p);
5272 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5273 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5274 continue;
5275 p2p_add_wps_vendor_extension(
5276 p2p, wpa_s->conf->wps_vendor_ext[i]);
5277 }
5278 }
5279
5280 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5281 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5282 char country[3];
5283 country[0] = wpa_s->conf->country[0];
5284 country[1] = wpa_s->conf->country[1];
5285 country[2] = 0x04;
5286 p2p_set_country(p2p, country);
5287 }
5288
5289 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5290 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5291 wpa_s->conf->p2p_ssid_postfix ?
5292 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5293 0);
5294 }
5295
5296 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5297 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005298
5299 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5300 u8 reg_class, channel;
5301 int ret;
5302 unsigned int r;
5303 if (wpa_s->conf->p2p_listen_reg_class &&
5304 wpa_s->conf->p2p_listen_channel) {
5305 reg_class = wpa_s->conf->p2p_listen_reg_class;
5306 channel = wpa_s->conf->p2p_listen_channel;
5307 } else {
5308 reg_class = 81;
5309 /*
5310 * Pick one of the social channels randomly as the
5311 * listen channel.
5312 */
5313 os_get_random((u8 *) &r, sizeof(r));
5314 channel = 1 + (r % 3) * 5;
5315 }
5316 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5317 if (ret)
5318 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5319 "failed: %d", ret);
5320 }
5321 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5322 u8 op_reg_class, op_channel, cfg_op_channel;
5323 int ret = 0;
5324 unsigned int r;
5325 if (wpa_s->conf->p2p_oper_reg_class &&
5326 wpa_s->conf->p2p_oper_channel) {
5327 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5328 op_channel = wpa_s->conf->p2p_oper_channel;
5329 cfg_op_channel = 1;
5330 } else {
5331 op_reg_class = 81;
5332 /*
5333 * Use random operation channel from (1, 6, 11)
5334 *if no other preference is indicated.
5335 */
5336 os_get_random((u8 *) &r, sizeof(r));
5337 op_channel = 1 + (r % 3) * 5;
5338 cfg_op_channel = 0;
5339 }
5340 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5341 cfg_op_channel);
5342 if (ret)
5343 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5344 "failed: %d", ret);
5345 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005346
5347 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5348 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5349 wpa_s->conf->p2p_pref_chan) < 0) {
5350 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5351 "update failed");
5352 }
5353 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005354}
5355
5356
5357int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5358 int duration)
5359{
5360 if (!wpa_s->ap_iface)
5361 return -1;
5362 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5363 duration);
5364}
5365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005366
5367int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5368{
5369 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5370 return -1;
5371 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5372 return -1;
5373
5374 wpa_s->global->cross_connection = enabled;
5375 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5376
5377 if (!enabled) {
5378 struct wpa_supplicant *iface;
5379
5380 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5381 {
5382 if (iface->cross_connect_enabled == 0)
5383 continue;
5384
5385 iface->cross_connect_enabled = 0;
5386 iface->cross_connect_in_use = 0;
5387 wpa_msg(iface->parent, MSG_INFO,
5388 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5389 iface->ifname, iface->cross_connect_uplink);
5390 }
5391 }
5392
5393 return 0;
5394}
5395
5396
5397static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5398{
5399 struct wpa_supplicant *iface;
5400
5401 if (!uplink->global->cross_connection)
5402 return;
5403
5404 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5405 if (!iface->cross_connect_enabled)
5406 continue;
5407 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5408 0)
5409 continue;
5410 if (iface->ap_iface == NULL)
5411 continue;
5412 if (iface->cross_connect_in_use)
5413 continue;
5414
5415 iface->cross_connect_in_use = 1;
5416 wpa_msg(iface->parent, MSG_INFO,
5417 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5418 iface->ifname, iface->cross_connect_uplink);
5419 }
5420}
5421
5422
5423static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5424{
5425 struct wpa_supplicant *iface;
5426
5427 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5428 if (!iface->cross_connect_enabled)
5429 continue;
5430 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5431 0)
5432 continue;
5433 if (!iface->cross_connect_in_use)
5434 continue;
5435
5436 wpa_msg(iface->parent, MSG_INFO,
5437 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5438 iface->ifname, iface->cross_connect_uplink);
5439 iface->cross_connect_in_use = 0;
5440 }
5441}
5442
5443
5444void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5445{
5446 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5447 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5448 wpa_s->cross_connect_disallowed)
5449 wpas_p2p_disable_cross_connect(wpa_s);
5450 else
5451 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005452 if (!wpa_s->ap_iface &&
5453 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5454 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455}
5456
5457
5458void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5459{
5460 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005461 if (!wpa_s->ap_iface &&
5462 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5463 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005464 wpas_p2p_set_group_idle_timeout(wpa_s);
5465}
5466
5467
5468static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5469{
5470 struct wpa_supplicant *iface;
5471
5472 if (!wpa_s->global->cross_connection)
5473 return;
5474
5475 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5476 if (iface == wpa_s)
5477 continue;
5478 if (iface->drv_flags &
5479 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5480 continue;
5481 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5482 continue;
5483
5484 wpa_s->cross_connect_enabled = 1;
5485 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5486 sizeof(wpa_s->cross_connect_uplink));
5487 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5488 "%s to %s whenever uplink is available",
5489 wpa_s->ifname, wpa_s->cross_connect_uplink);
5490
5491 if (iface->ap_iface || iface->current_ssid == NULL ||
5492 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5493 iface->cross_connect_disallowed ||
5494 iface->wpa_state != WPA_COMPLETED)
5495 break;
5496
5497 wpa_s->cross_connect_in_use = 1;
5498 wpa_msg(wpa_s->parent, MSG_INFO,
5499 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5500 wpa_s->ifname, wpa_s->cross_connect_uplink);
5501 break;
5502 }
5503}
5504
5505
5506int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5507{
5508 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5509 !wpa_s->p2p_in_provisioning)
5510 return 0; /* not P2P client operation */
5511
5512 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5513 "session overlap");
5514 if (wpa_s != wpa_s->parent)
5515 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5516
5517 if (wpa_s->global->p2p)
5518 p2p_group_formation_failed(wpa_s->global->p2p);
5519
5520 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5521 wpa_s->parent, NULL);
5522
5523 wpas_group_formation_completed(wpa_s, 0);
5524 return 1;
5525}
5526
5527
5528void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5529{
5530 struct p2p_channels chan;
5531
5532 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5533 return;
5534
5535 os_memset(&chan, 0, sizeof(chan));
5536 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5537 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5538 "channel list");
5539 return;
5540 }
5541
5542 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5543}
5544
5545
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005546static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5547 struct wpa_scan_results *scan_res)
5548{
5549 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5550}
5551
5552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005553int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5554{
5555 struct wpa_global *global = wpa_s->global;
5556 int found = 0;
5557 const u8 *peer;
5558
5559 if (global->p2p == NULL)
5560 return -1;
5561
5562 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5563
5564 if (wpa_s->pending_interface_name[0] &&
5565 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5566 found = 1;
5567
5568 peer = p2p_get_go_neg_peer(global->p2p);
5569 if (peer) {
5570 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5571 MACSTR, MAC2STR(peer));
5572 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005573 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005574 }
5575
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005576 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5577 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5578 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5579 found = 1;
5580 }
5581
5582 if (wpa_s->pending_pd_before_join) {
5583 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5584 wpa_s->pending_pd_before_join = 0;
5585 found = 1;
5586 }
5587
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005588 wpas_p2p_stop_find(wpa_s);
5589
5590 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5591 if (wpa_s == global->p2p_group_formation &&
5592 (wpa_s->p2p_in_provisioning ||
5593 wpa_s->parent->pending_interface_type ==
5594 WPA_IF_P2P_CLIENT)) {
5595 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5596 "formation found - cancelling",
5597 wpa_s->ifname);
5598 found = 1;
5599 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5600 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005601 if (wpa_s->p2p_in_provisioning) {
5602 wpas_group_formation_completed(wpa_s, 0);
5603 break;
5604 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005605 wpas_p2p_group_delete(wpa_s,
5606 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607 break;
5608 }
5609 }
5610
5611 if (!found) {
5612 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5613 return -1;
5614 }
5615
5616 return 0;
5617}
5618
5619
5620void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5621{
5622 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5623 return;
5624
5625 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5626 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005627 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005628}
5629
5630
5631void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5632 int freq_24, int freq_5, int freq_overall)
5633{
5634 struct p2p_data *p2p = wpa_s->global->p2p;
5635 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5636 return;
5637 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5638}
5639
5640
5641int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5642{
5643 u8 peer[ETH_ALEN];
5644 struct p2p_data *p2p = wpa_s->global->p2p;
5645
5646 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5647 return -1;
5648
5649 if (hwaddr_aton(addr, peer))
5650 return -1;
5651
5652 return p2p_unauthorize(p2p, peer);
5653}
5654
5655
5656/**
5657 * wpas_p2p_disconnect - Disconnect from a P2P Group
5658 * @wpa_s: Pointer to wpa_supplicant data
5659 * Returns: 0 on success, -1 on failure
5660 *
5661 * This can be used to disconnect from a group in which the local end is a P2P
5662 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5663 * virtual network interface was created for this group, that interface will be
5664 * removed. Otherwise, only the configured P2P group network will be removed
5665 * from the interface.
5666 */
5667int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5668{
5669
5670 if (wpa_s == NULL)
5671 return -1;
5672
Jouni Malinen2b89da82012-08-31 22:04:41 +03005673 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5674 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005675}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005676
5677
5678int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5679{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005680 int ret;
5681
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005682 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5683 return 0;
5684
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005685 ret = p2p_in_progress(wpa_s->global->p2p);
5686 if (ret == 0) {
5687 /*
5688 * Check whether there is an ongoing WPS provisioning step (or
5689 * other parts of group formation) on another interface since
5690 * p2p_in_progress() does not report this to avoid issues for
5691 * scans during such provisioning step.
5692 */
5693 if (wpa_s->global->p2p_group_formation &&
5694 wpa_s->global->p2p_group_formation != wpa_s) {
5695 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5696 "in group formation",
5697 wpa_s->global->p2p_group_formation->ifname);
5698 ret = 1;
5699 }
5700 }
5701
5702 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005703}
5704
5705
5706void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5707 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005708{
5709 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5710 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5711 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005712 /**
5713 * Remove the network by scheduling the group formation
5714 * timeout to happen immediately. The teardown code
5715 * needs to be scheduled to run asynch later so that we
5716 * don't delete data from under ourselves unexpectedly.
5717 * Calling wpas_p2p_group_formation_timeout directly
5718 * causes a series of crashes in WPS failure scenarios.
5719 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005720 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5721 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005722 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5723 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005724 }
5725}
5726
5727
5728struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005729 const u8 *addr, const u8 *ssid,
5730 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005731{
5732 struct wpa_ssid *s;
5733 size_t i;
5734
5735 for (s = wpa_s->conf->ssid; s; s = s->next) {
5736 if (s->disabled != 2)
5737 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005738 if (ssid &&
5739 (ssid_len != s->ssid_len ||
5740 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5741 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005742 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5743 return s; /* peer is GO in the persistent group */
5744 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5745 continue;
5746 for (i = 0; i < s->num_p2p_clients; i++) {
5747 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5748 addr, ETH_ALEN) == 0)
5749 return s; /* peer is P2P client in persistent
5750 * group */
5751 }
5752 }
5753
5754 return NULL;
5755}
5756
5757
5758void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5759 const u8 *addr)
5760{
5761 if (addr == NULL)
5762 return;
5763 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5764}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005765
Dmitry Shmidt04949592012-07-19 12:16:46 -07005766
5767static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5768 int group_added)
5769{
5770 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005771 if (wpa_s->global->p2p_group_formation)
5772 group = wpa_s->global->p2p_group_formation;
5773 wpa_s = wpa_s->parent;
5774 offchannel_send_action_done(wpa_s);
5775 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005776 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005777 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5778 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5779 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5780 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5781 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005782 wpa_s->p2p_pd_before_go_neg,
5783 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005784}
5785
5786
5787int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5788{
5789 if (!wpa_s->p2p_fallback_to_go_neg ||
5790 wpa_s->p2p_in_provisioning <= 5)
5791 return 0;
5792
5793 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5794 return 0; /* peer operating as a GO */
5795
5796 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5797 "fallback to GO Negotiation");
5798 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5799
5800 return 1;
5801}
5802
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005803
5804unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5805{
5806 const char *rn, *rn2;
5807 struct wpa_supplicant *ifs;
5808
5809 if (wpa_s->wpa_state > WPA_SCANNING) {
5810 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5811 "concurrent operation",
5812 P2P_CONCURRENT_SEARCH_DELAY);
5813 return P2P_CONCURRENT_SEARCH_DELAY;
5814 }
5815
5816 if (!wpa_s->driver->get_radio_name)
5817 return 0;
5818 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5819 if (rn == NULL || rn[0] == '\0')
5820 return 0;
5821
5822 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5823 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5824 continue;
5825
5826 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5827 if (!rn2 || os_strcmp(rn, rn2) != 0)
5828 continue;
5829 if (ifs->wpa_state > WPA_SCANNING) {
5830 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5831 "delay due to concurrent operation on "
5832 "interface %s",
5833 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5834 return P2P_CONCURRENT_SEARCH_DELAY;
5835 }
5836 }
5837
5838 return 0;
5839}
5840
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005841#ifdef ANDROID_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005842int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5843 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005844{
5845 struct wpa_supplicant *iface = NULL;
5846 struct p2p_data *p2p = wpa_s->global->p2p;
5847
5848 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005849 if ((iface->current_ssid) &&
5850 (iface->current_ssid->frequency != freq) &&
5851 ((iface->p2p_group_interface) ||
5852 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005853
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005854 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5855 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5856 /* Try to see whether we can move the GO. If it
5857 * is not possible, remove the GO interface
5858 */
5859 if (wpa_drv_switch_channel(iface, freq) == 0) {
5860 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5861 iface->current_ssid->frequency = freq;
5862 continue;
5863 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005864 }
5865
5866 /* If GO cannot be moved or if the conflicting interface is a
5867 * P2P Client, remove the interface depending up on the connection
5868 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005869 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005870 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005871 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005872 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005873 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005874 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005875 /* If connection in progress is p2p connection, do not proceed for the connection */
5876 if (wpa_s == iface)
5877 return -1;
5878 else
5879 /* If connection in progress is STA connection, proceed for the connection */
5880 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005881 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005882 /* P2p connection has priority, disable the STA network*/
5883 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5884 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5885 " id=%d", ssid->id);
5886 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5887 if (wpa_s == iface) {
5888 /* p2p connection is in progress, continue connecting...*/
5889 return 0;
5890 }
5891 else {
5892 /* STA connection is in progress, do not allow to continue */
5893 return -1;
5894 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005895 }
5896 }
5897 }
5898 return 0;
5899}
5900#endif