blob: 37f5ad3af8f7b03a03d26390a9c938c212fd8fbb [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
Dmitry Shmidt34af3062013-07-11 10:46:32 -070068#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
69
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070070enum p2p_group_removal_reason {
71 P2P_GROUP_REMOVAL_UNKNOWN,
72 P2P_GROUP_REMOVAL_SILENT,
73 P2P_GROUP_REMOVAL_FORMATION_FAILED,
74 P2P_GROUP_REMOVAL_REQUESTED,
75 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
76 P2P_GROUP_REMOVAL_UNAVAILABLE,
77 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
78#ifdef ANDROID_P2P
79 P2P_GROUP_REMOVAL_FREQ_CONFLICT
80#endif
81};
82
Jouni Malinendc7b7132012-09-14 12:53:47 -070083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
85static struct wpa_supplicant *
86wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
87 int go);
88static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070089static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
91static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070092 const u8 *dev_addr, enum p2p_wps_method wps_method,
93 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
95static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
96static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
97static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080098static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
99 void *timeout_ctx);
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700100#ifdef ANDROID_P2P
101static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
102#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700103static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
104 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800105static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106
107
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700108static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
109 int freq)
110{
111 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
112 return;
113 if (freq > 0 &&
114 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
115 wpa_s->parent->conf->p2p_ignore_shared_freq)
116 freq = 0;
117 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
118}
119
120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
122 struct wpa_scan_results *scan_res)
123{
124 size_t i;
125
126 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
127 return;
128
129 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
130 (int) scan_res->num);
131
132 for (i = 0; i < scan_res->num; i++) {
133 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800134 struct os_time time_tmp_age, entry_ts;
135 time_tmp_age.sec = bss->age / 1000;
136 time_tmp_age.usec = (bss->age % 1000) * 1000;
137 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800139 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140 (const u8 *) (bss + 1),
141 bss->ie_len) > 0)
142 break;
143 }
144
145 p2p_scan_res_handled(wpa_s->global->p2p);
146}
147
148
149static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
150 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700151 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152{
153 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700154 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155 struct wpa_driver_scan_params params;
156 int ret;
157 struct wpabuf *wps_ie, *ies;
158 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800159 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160
161 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
162 return -1;
163
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700164 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
165 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700166 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700167 wpas_p2p_in_progress(wpa_s) == 2) {
168 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
169 "pending station mode scan to be "
170 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700171 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700172 wpa_supplicant_req_scan(ifs, 0, 0);
173 return 1;
174 }
175 }
176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 os_memset(&params, 0, sizeof(params));
178
179 /* P2P Wildcard SSID */
180 params.num_ssids = 1;
181 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
182 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
183
184 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700185 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
186 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187 num_req_dev_types, req_dev_types);
188 if (wps_ie == NULL)
189 return -1;
190
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800191 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
192 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193 if (ies == NULL) {
194 wpabuf_free(wps_ie);
195 return -1;
196 }
197 wpabuf_put_buf(ies, wps_ie);
198 wpabuf_free(wps_ie);
199
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800200 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800202 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203 params.extra_ies = wpabuf_head(ies);
204 params.extra_ies_len = wpabuf_len(ies);
205
206 switch (type) {
207 case P2P_SCAN_SOCIAL:
208 params.freqs = social_channels;
209 break;
210 case P2P_SCAN_FULL:
211 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700212 case P2P_SCAN_SPECIFIC:
213 social_channels[0] = freq;
214 social_channels[1] = 0;
215 params.freqs = social_channels;
216 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 case P2P_SCAN_SOCIAL_PLUS_ONE:
218 social_channels[3] = freq;
219 params.freqs = social_channels;
220 break;
221 }
222
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800223 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224
225 wpabuf_free(ies);
226
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800227 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700228 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
229 if (ifs->scanning ||
230 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
231 wpa_s->global->p2p_cb_on_scan_complete = 1;
232 ret = 1;
233 break;
234 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800235 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700236 } else {
237 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700238 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700239 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241 return ret;
242}
243
244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
246{
247 switch (p2p_group_interface) {
248 case P2P_GROUP_INTERFACE_PENDING:
249 return WPA_IF_P2P_GROUP;
250 case P2P_GROUP_INTERFACE_GO:
251 return WPA_IF_P2P_GO;
252 case P2P_GROUP_INTERFACE_CLIENT:
253 return WPA_IF_P2P_CLIENT;
254 }
255
256 return WPA_IF_P2P_GROUP;
257}
258
259
260static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
261 const u8 *ssid,
262 size_t ssid_len, int *go)
263{
264 struct wpa_ssid *s;
265
266 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
267 for (s = wpa_s->conf->ssid; s; s = s->next) {
268 if (s->disabled != 0 || !s->p2p_group ||
269 s->ssid_len != ssid_len ||
270 os_memcmp(ssid, s->ssid, ssid_len) != 0)
271 continue;
272 if (s->mode == WPAS_MODE_P2P_GO &&
273 s != wpa_s->current_ssid)
274 continue;
275 if (go)
276 *go = s->mode == WPAS_MODE_P2P_GO;
277 return wpa_s;
278 }
279 }
280
281 return NULL;
282}
283
284
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700285static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
286 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287{
288 struct wpa_ssid *ssid;
289 char *gtype;
290 const char *reason;
291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 ssid = wpa_s->current_ssid;
293 if (ssid == NULL) {
294 /*
295 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700296 * pending P2P group interface waiting for provisioning or a
297 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298 */
299 ssid = wpa_s->conf->ssid;
300 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700301 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302 break;
303 ssid = ssid->next;
304 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300305 if (ssid == NULL &&
306 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
307 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700308 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
309 "not found");
310 return -1;
311 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312 }
313 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
314 gtype = "GO";
315 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
316 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
317 wpa_s->reassociate = 0;
318 wpa_s->disconnected = 1;
319 wpa_supplicant_deauthenticate(wpa_s,
320 WLAN_REASON_DEAUTH_LEAVING);
321 gtype = "client";
322 } else
323 gtype = "GO";
324 if (wpa_s->cross_connect_in_use) {
325 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700326 wpa_msg_global(wpa_s->parent, MSG_INFO,
327 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
328 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700330 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331 case P2P_GROUP_REMOVAL_REQUESTED:
332 reason = " reason=REQUESTED";
333 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700334 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
335 reason = " reason=FORMATION_FAILED";
336 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
338 reason = " reason=IDLE";
339 break;
340 case P2P_GROUP_REMOVAL_UNAVAILABLE:
341 reason = " reason=UNAVAILABLE";
342 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700343 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
344 reason = " reason=GO_ENDING_SESSION";
345 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700346#ifdef ANDROID_P2P
347 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
348 reason = " reason=FREQ_CONFLICT";
349 break;
350#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 default:
352 reason = "";
353 break;
354 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700355 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700356 wpa_msg_global(wpa_s->parent, MSG_INFO,
357 P2P_EVENT_GROUP_REMOVED "%s %s%s",
358 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700359 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700361#ifdef ANDROID_P2P
362 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
363#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700364 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
365 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800366 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700367 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800368 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
369 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700370 wpa_s->p2p_in_provisioning = 0;
371 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700372
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700373 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700374 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
375
376 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
377 struct wpa_global *global;
378 char *ifname;
379 enum wpa_driver_if_type type;
380 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
381 wpa_s->ifname);
382 global = wpa_s->global;
383 ifname = os_strdup(wpa_s->ifname);
384 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700385 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 wpa_s = global->ifaces;
387 if (wpa_s && ifname)
388 wpa_drv_if_remove(wpa_s, type, ifname);
389 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300390 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 }
392
393 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
394 if (ssid && (ssid->p2p_group ||
395 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
396 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
397 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700398 if (ssid == wpa_s->current_ssid) {
399 wpa_sm_set_config(wpa_s->wpa, NULL);
400 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700402 }
403 /*
404 * Networks objects created during any P2P activities are not
405 * exposed out as they might/will confuse certain non-P2P aware
406 * applications since these network objects won't behave like
407 * regular ones.
408 *
409 * Likewise, we don't send out network removed signals for such
410 * network objects.
411 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 wpa_config_remove_network(wpa_s->conf, id);
413 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800414 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700415 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 } else {
417 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
418 "found");
419 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700420 if (wpa_s->ap_iface)
421 wpa_supplicant_ap_deinit(wpa_s);
422 else
423 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700424
425 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426}
427
428
429static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
430 u8 *go_dev_addr,
431 const u8 *ssid, size_t ssid_len)
432{
433 struct wpa_bss *bss;
434 const u8 *bssid;
435 struct wpabuf *p2p;
436 u8 group_capab;
437 const u8 *addr;
438
439 if (wpa_s->go_params)
440 bssid = wpa_s->go_params->peer_interface_addr;
441 else
442 bssid = wpa_s->bssid;
443
444 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
445 if (bss == NULL) {
446 u8 iface_addr[ETH_ALEN];
447 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
448 iface_addr) == 0)
449 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
450 }
451 if (bss == NULL) {
452 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
453 "group is persistent - BSS " MACSTR " not found",
454 MAC2STR(bssid));
455 return 0;
456 }
457
458 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
459 if (p2p == NULL) {
460 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
461 "group is persistent - BSS " MACSTR
462 " did not include P2P IE", MAC2STR(bssid));
463 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
464 (u8 *) (bss + 1), bss->ie_len);
465 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
466 ((u8 *) bss + 1) + bss->ie_len,
467 bss->beacon_ie_len);
468 return 0;
469 }
470
471 group_capab = p2p_get_group_capab(p2p);
472 addr = p2p_get_go_dev_addr(p2p);
473 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
474 "group_capab=0x%x", group_capab);
475 if (addr) {
476 os_memcpy(go_dev_addr, addr, ETH_ALEN);
477 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
478 MAC2STR(addr));
479 } else
480 os_memset(go_dev_addr, 0, ETH_ALEN);
481 wpabuf_free(p2p);
482
483 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
484 "go_dev_addr=" MACSTR,
485 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
486
487 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
488}
489
490
Jouni Malinen75ecf522011-06-27 15:19:46 -0700491static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
492 struct wpa_ssid *ssid,
493 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494{
495 struct wpa_ssid *s;
496 int changed = 0;
497
498 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
499 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
500 for (s = wpa_s->conf->ssid; s; s = s->next) {
501 if (s->disabled == 2 &&
502 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
503 s->ssid_len == ssid->ssid_len &&
504 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
505 break;
506 }
507
508 if (s) {
509 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
510 "entry");
511 if (ssid->passphrase && !s->passphrase)
512 changed = 1;
513 else if (ssid->passphrase && s->passphrase &&
514 os_strcmp(ssid->passphrase, s->passphrase) != 0)
515 changed = 1;
516 } else {
517 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
518 "entry");
519 changed = 1;
520 s = wpa_config_add_network(wpa_s->conf);
521 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700522 return -1;
523
524 /*
525 * Instead of network_added we emit persistent_group_added
526 * notification. Also to keep the defense checks in
527 * persistent_group obj registration method, we set the
528 * relevant flags in s to designate it as a persistent group.
529 */
530 s->p2p_group = 1;
531 s->p2p_persistent_group = 1;
532 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 wpa_config_set_network_defaults(s);
534 }
535
536 s->p2p_group = 1;
537 s->p2p_persistent_group = 1;
538 s->disabled = 2;
539 s->bssid_set = 1;
540 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
541 s->mode = ssid->mode;
542 s->auth_alg = WPA_AUTH_ALG_OPEN;
543 s->key_mgmt = WPA_KEY_MGMT_PSK;
544 s->proto = WPA_PROTO_RSN;
545 s->pairwise_cipher = WPA_CIPHER_CCMP;
546 s->export_keys = 1;
547 if (ssid->passphrase) {
548 os_free(s->passphrase);
549 s->passphrase = os_strdup(ssid->passphrase);
550 }
551 if (ssid->psk_set) {
552 s->psk_set = 1;
553 os_memcpy(s->psk, ssid->psk, 32);
554 }
555 if (s->passphrase && !s->psk_set)
556 wpa_config_update_psk(s);
557 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
558 os_free(s->ssid);
559 s->ssid = os_malloc(ssid->ssid_len);
560 }
561 if (s->ssid) {
562 s->ssid_len = ssid->ssid_len;
563 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
564 }
565
566#ifndef CONFIG_NO_CONFIG_WRITE
567 if (changed && wpa_s->conf->update_config &&
568 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
569 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
570 }
571#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700572
573 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574}
575
576
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800577static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
578 const u8 *addr)
579{
580 struct wpa_ssid *ssid, *s;
581 u8 *n;
582 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700583 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800584
585 ssid = wpa_s->current_ssid;
586 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
587 !ssid->p2p_persistent_group)
588 return;
589
590 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
591 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
592 continue;
593
594 if (s->ssid_len == ssid->ssid_len &&
595 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
596 break;
597 }
598
599 if (s == NULL)
600 return;
601
602 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
603 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700604 ETH_ALEN) != 0)
605 continue;
606
607 if (i == s->num_p2p_clients - 1)
608 return; /* already the most recent entry */
609
610 /* move the entry to mark it most recent */
611 os_memmove(s->p2p_client_list + i * ETH_ALEN,
612 s->p2p_client_list + (i + 1) * ETH_ALEN,
613 (s->num_p2p_clients - i - 1) * ETH_ALEN);
614 os_memcpy(s->p2p_client_list +
615 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
616 found = 1;
617 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800618 }
619
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700620 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
621 n = os_realloc_array(s->p2p_client_list,
622 s->num_p2p_clients + 1, ETH_ALEN);
623 if (n == NULL)
624 return;
625 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
626 s->p2p_client_list = n;
627 s->num_p2p_clients++;
628 } else if (!found) {
629 /* Not enough room for an additional entry - drop the oldest
630 * entry */
631 os_memmove(s->p2p_client_list,
632 s->p2p_client_list + ETH_ALEN,
633 (s->num_p2p_clients - 1) * ETH_ALEN);
634 os_memcpy(s->p2p_client_list +
635 (s->num_p2p_clients - 1) * ETH_ALEN,
636 addr, ETH_ALEN);
637 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800638
639#ifndef CONFIG_NO_CONFIG_WRITE
640 if (wpa_s->parent->conf->update_config &&
641 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
642 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
643#endif /* CONFIG_NO_CONFIG_WRITE */
644}
645
646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700647static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
648 int success)
649{
650 struct wpa_ssid *ssid;
651 const char *ssid_txt;
652 int client;
653 int persistent;
654 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700655 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656
657 /*
658 * This callback is likely called for the main interface. Update wpa_s
659 * to use the group interface if a new interface was created for the
660 * group.
661 */
662 if (wpa_s->global->p2p_group_formation)
663 wpa_s = wpa_s->global->p2p_group_formation;
664 wpa_s->global->p2p_group_formation = NULL;
665 wpa_s->p2p_in_provisioning = 0;
666
667 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700668 wpa_msg_global(wpa_s->parent, MSG_INFO,
669 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700670 wpas_p2p_group_delete(wpa_s,
671 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672 return;
673 }
674
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700675 wpa_msg_global(wpa_s->parent, MSG_INFO,
676 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700677
678 ssid = wpa_s->current_ssid;
679 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
680 ssid->mode = WPAS_MODE_P2P_GO;
681 p2p_group_notif_formation_done(wpa_s->p2p_group);
682 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
683 }
684
685 persistent = 0;
686 if (ssid) {
687 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
688 client = ssid->mode == WPAS_MODE_INFRA;
689 if (ssid->mode == WPAS_MODE_P2P_GO) {
690 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700691 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
692 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 } else
694 persistent = wpas_p2p_persistent_group(wpa_s,
695 go_dev_addr,
696 ssid->ssid,
697 ssid->ssid_len);
698 } else {
699 ssid_txt = "";
700 client = wpa_s->p2p_group_interface ==
701 P2P_GROUP_INTERFACE_CLIENT;
702 os_memset(go_dev_addr, 0, ETH_ALEN);
703 }
704
705 wpa_s->show_group_started = 0;
706 if (client) {
707 /*
708 * Indicate event only after successfully completed 4-way
709 * handshake, i.e., when the interface is ready for data
710 * packets.
711 */
712 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700713#ifdef ANDROID_P2P
714 /* For client Second phase of Group formation (4-way handshake) can be still pending
715 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700716 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 -0700717 wpa_s->global->p2p_group_formation = wpa_s;
718#endif
719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
721 char psk[65];
722 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700723 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
724 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
725 MACSTR "%s",
726 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
727 MAC2STR(go_dev_addr),
728 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 wpas_p2p_cross_connect_setup(wpa_s);
730 wpas_p2p_set_group_idle_timeout(wpa_s);
731 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700732 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
733 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
734 "go_dev_addr=" MACSTR "%s",
735 wpa_s->ifname, ssid_txt,
736 ssid ? ssid->frequency : 0,
737 ssid && ssid->passphrase ? ssid->passphrase : "",
738 MAC2STR(go_dev_addr),
739 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 wpas_p2p_cross_connect_setup(wpa_s);
741 wpas_p2p_set_group_idle_timeout(wpa_s);
742 }
743
744 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700745 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
746 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800747 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700748 network_id = ssid->id;
749 if (!client)
750 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751}
752
753
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800754static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
755 unsigned int freq,
756 const u8 *dst, const u8 *src,
757 const u8 *bssid,
758 const u8 *data, size_t data_len,
759 enum offchannel_send_action_result
760 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800762 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
765 return;
766 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
767 return;
768
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800769 switch (result) {
770 case OFFCHANNEL_SEND_ACTION_SUCCESS:
771 res = P2P_SEND_ACTION_SUCCESS;
772 break;
773 case OFFCHANNEL_SEND_ACTION_NO_ACK:
774 res = P2P_SEND_ACTION_NO_ACK;
775 break;
776 case OFFCHANNEL_SEND_ACTION_FAILED:
777 res = P2P_SEND_ACTION_FAILED;
778 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 }
780
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800781 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800783 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
784 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800785 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800786 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
787 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800789 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
790 "during p2p_connect-auto");
791 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
792 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793 }
794}
795
796
797static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
798 const u8 *src, const u8 *bssid, const u8 *buf,
799 size_t len, unsigned int wait_time)
800{
801 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800802 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
803 wait_time,
804 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805}
806
807
808static void wpas_send_action_done(void *ctx)
809{
810 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800811 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812}
813
814
815static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
816 struct p2p_go_neg_results *params)
817{
818 if (wpa_s->go_params == NULL) {
819 wpa_s->go_params = os_malloc(sizeof(*params));
820 if (wpa_s->go_params == NULL)
821 return -1;
822 }
823 os_memcpy(wpa_s->go_params, params, sizeof(*params));
824 return 0;
825}
826
827
828static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
829 struct p2p_go_neg_results *res)
830{
831 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
832 MAC2STR(res->peer_interface_addr));
833 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
834 res->ssid, res->ssid_len);
835 wpa_supplicant_ap_deinit(wpa_s);
836 wpas_copy_go_neg_results(wpa_s, res);
837 if (res->wps_method == WPS_PBC)
838 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
839 else {
840 u16 dev_pw_id = DEV_PW_DEFAULT;
841 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
842 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
843 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
844 wpa_s->p2p_pin, 1, dev_pw_id);
845 }
846}
847
848
849static void p2p_go_configured(void *ctx, void *data)
850{
851 struct wpa_supplicant *wpa_s = ctx;
852 struct p2p_go_neg_results *params = data;
853 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700854 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855
856 ssid = wpa_s->current_ssid;
857 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
858 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
859 if (wpa_s->global->p2p_group_formation == wpa_s)
860 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800861 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700862 wpa_msg_global(wpa_s->parent, MSG_INFO,
863 P2P_EVENT_GROUP_STARTED
864 "%s GO ssid=\"%s\" freq=%d "
865 "passphrase=\"%s\" go_dev_addr=" MACSTR
866 "%s", wpa_s->ifname,
867 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
868 ssid->frequency, params->passphrase,
869 MAC2STR(wpa_s->global->p2p_dev_addr),
870 params->persistent_group ?
871 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800872 } else {
873 char psk[65];
874 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
875 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700876 wpa_msg_global(wpa_s->parent, MSG_INFO,
877 P2P_EVENT_GROUP_STARTED
878 "%s GO ssid=\"%s\" freq=%d psk=%s "
879 "go_dev_addr=" MACSTR "%s",
880 wpa_s->ifname,
881 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
882 ssid->frequency, psk,
883 MAC2STR(wpa_s->global->p2p_dev_addr),
884 params->persistent_group ?
885 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800886 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700889 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700891 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700892 if (network_id < 0)
893 network_id = ssid->id;
894 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 wpas_p2p_cross_connect_setup(wpa_s);
896 wpas_p2p_set_group_idle_timeout(wpa_s);
897 return;
898 }
899
900 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
901 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
902 params->peer_interface_addr)) {
903 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
904 "filtering");
905 return;
906 }
907 if (params->wps_method == WPS_PBC)
908 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800909 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 else if (wpa_s->p2p_pin[0])
911 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800912 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700913 os_free(wpa_s->go_params);
914 wpa_s->go_params = NULL;
915}
916
917
918static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
919 struct p2p_go_neg_results *params,
920 int group_formation)
921{
922 struct wpa_ssid *ssid;
923
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700924 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
925 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
926 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
927 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700929 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930
931 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700932 if (ssid == NULL) {
933 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700935 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800937 wpa_s->show_group_started = 0;
938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 wpa_config_set_network_defaults(ssid);
940 ssid->temporary = 1;
941 ssid->p2p_group = 1;
942 ssid->p2p_persistent_group = params->persistent_group;
943 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
944 WPAS_MODE_P2P_GO;
945 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700946 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 ssid->ssid = os_zalloc(params->ssid_len + 1);
948 if (ssid->ssid) {
949 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
950 ssid->ssid_len = params->ssid_len;
951 }
952 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
953 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
954 ssid->proto = WPA_PROTO_RSN;
955 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800956 if (os_strlen(params->passphrase) > 0) {
957 ssid->passphrase = os_strdup(params->passphrase);
958 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700959 wpa_msg_global(wpa_s, MSG_ERROR,
960 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800961 wpa_config_remove_network(wpa_s->conf, ssid->id);
962 return;
963 }
964 } else
965 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800966 ssid->psk_set = params->psk_set;
967 if (ssid->psk_set)
968 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800969 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800970 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700971 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972
973 wpa_s->ap_configured_cb = p2p_go_configured;
974 wpa_s->ap_configured_cb_ctx = wpa_s;
975 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700976 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 wpa_s->reassociate = 1;
978 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700979 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
980 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 wpa_supplicant_req_scan(wpa_s, 0, 0);
982}
983
984
985static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
986 const struct wpa_supplicant *src)
987{
988 struct wpa_config *d;
989 const struct wpa_config *s;
990
991 d = dst->conf;
992 s = src->conf;
993
994#define C(n) if (s->n) d->n = os_strdup(s->n)
995 C(device_name);
996 C(manufacturer);
997 C(model_name);
998 C(model_number);
999 C(serial_number);
1000 C(config_methods);
1001#undef C
1002
1003 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1004 os_memcpy(d->sec_device_type, s->sec_device_type,
1005 sizeof(d->sec_device_type));
1006 d->num_sec_device_types = s->num_sec_device_types;
1007
1008 d->p2p_group_idle = s->p2p_group_idle;
1009 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001010 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001011 d->max_num_sta = s->max_num_sta;
1012 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001013 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001014 d->beacon_int = s->beacon_int;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015}
1016
1017
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001018static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1019 char *ifname, size_t len)
1020{
1021 char *ifname_ptr = wpa_s->ifname;
1022
1023 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1024 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1025 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1026 }
1027
1028 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1029 if (os_strlen(ifname) >= IFNAMSIZ &&
1030 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1031 /* Try to avoid going over the IFNAMSIZ length limit */
1032 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1033 wpa_s->p2p_group_idx);
1034 }
1035}
1036
1037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1039 enum wpa_driver_if_type type)
1040{
1041 char ifname[120], force_ifname[120];
1042
1043 if (wpa_s->pending_interface_name[0]) {
1044 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1045 "- skip creation of a new one");
1046 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1047 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1048 "unknown?! ifname='%s'",
1049 wpa_s->pending_interface_name);
1050 return -1;
1051 }
1052 return 0;
1053 }
1054
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001055 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 force_ifname[0] = '\0';
1057
1058 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1059 ifname);
1060 wpa_s->p2p_group_idx++;
1061
1062 wpa_s->pending_interface_type = type;
1063 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1064 wpa_s->pending_interface_addr, NULL) < 0) {
1065 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1066 "interface");
1067 return -1;
1068 }
1069
1070 if (force_ifname[0]) {
1071 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1072 force_ifname);
1073 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1074 sizeof(wpa_s->pending_interface_name));
1075 } else
1076 os_strlcpy(wpa_s->pending_interface_name, ifname,
1077 sizeof(wpa_s->pending_interface_name));
1078 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1079 MACSTR, wpa_s->pending_interface_name,
1080 MAC2STR(wpa_s->pending_interface_addr));
1081
1082 return 0;
1083}
1084
1085
1086static void wpas_p2p_remove_pending_group_interface(
1087 struct wpa_supplicant *wpa_s)
1088{
1089 if (!wpa_s->pending_interface_name[0] ||
1090 is_zero_ether_addr(wpa_s->pending_interface_addr))
1091 return; /* No pending virtual interface */
1092
1093 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1094 wpa_s->pending_interface_name);
1095 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1096 wpa_s->pending_interface_name);
1097 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1098 wpa_s->pending_interface_name[0] = '\0';
1099}
1100
1101
1102static struct wpa_supplicant *
1103wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1104{
1105 struct wpa_interface iface;
1106 struct wpa_supplicant *group_wpa_s;
1107
1108 if (!wpa_s->pending_interface_name[0]) {
1109 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1110 if (!wpas_p2p_create_iface(wpa_s))
1111 return NULL;
1112 /*
1113 * Something has forced us to remove the pending interface; try
1114 * to create a new one and hope for the best that we will get
1115 * the same local address.
1116 */
1117 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1118 WPA_IF_P2P_CLIENT) < 0)
1119 return NULL;
1120 }
1121
1122 os_memset(&iface, 0, sizeof(iface));
1123 iface.ifname = wpa_s->pending_interface_name;
1124 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001125 if (wpa_s->conf->ctrl_interface == NULL &&
1126 wpa_s->parent != wpa_s &&
1127 wpa_s->p2p_mgmt &&
1128 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1129 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1130 else
1131 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 iface.driver_param = wpa_s->conf->driver_param;
1133 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1134 if (group_wpa_s == NULL) {
1135 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1136 "wpa_supplicant interface");
1137 return NULL;
1138 }
1139 wpa_s->pending_interface_name[0] = '\0';
1140 group_wpa_s->parent = wpa_s;
1141 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1142 P2P_GROUP_INTERFACE_CLIENT;
1143 wpa_s->global->p2p_group_formation = group_wpa_s;
1144
1145 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1146
1147 return group_wpa_s;
1148}
1149
1150
1151static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1152 void *timeout_ctx)
1153{
1154 struct wpa_supplicant *wpa_s = eloop_ctx;
1155 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001156 wpas_p2p_group_formation_failed(wpa_s);
1157}
1158
1159
1160void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1161{
1162 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1163 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 if (wpa_s->global->p2p)
1165 p2p_group_formation_failed(wpa_s->global->p2p);
1166 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1167 wpa_drv_p2p_group_formation_failed(wpa_s);
1168 wpas_group_formation_completed(wpa_s, 0);
1169}
1170
1171
1172void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1173{
1174 struct wpa_supplicant *wpa_s = ctx;
1175
1176 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1177 wpa_drv_cancel_remain_on_channel(wpa_s);
1178 wpa_s->off_channel_freq = 0;
1179 wpa_s->roc_waiting_drv_freq = 0;
1180 }
1181
1182 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001183 wpa_msg_global(wpa_s, MSG_INFO,
1184 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1185 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001186 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187 wpas_p2p_remove_pending_group_interface(wpa_s);
1188 return;
1189 }
1190
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001191 if (wpa_s->p2p_go_ht40)
1192 res->ht40 = 1;
1193
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001194 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001195 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196
Dmitry Shmidt04949592012-07-19 12:16:46 -07001197 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1198 struct wpa_ssid *ssid;
1199 ssid = wpa_config_get_network(wpa_s->conf,
1200 wpa_s->p2p_persistent_id);
1201 if (ssid && ssid->disabled == 2 &&
1202 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1203 size_t len = os_strlen(ssid->passphrase);
1204 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1205 "on requested persistent group");
1206 os_memcpy(res->passphrase, ssid->passphrase, len);
1207 res->passphrase[len] = '\0';
1208 }
1209 }
1210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 if (wpa_s->create_p2p_iface) {
1212 struct wpa_supplicant *group_wpa_s =
1213 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1214 if (group_wpa_s == NULL) {
1215 wpas_p2p_remove_pending_group_interface(wpa_s);
1216 return;
1217 }
1218 if (group_wpa_s != wpa_s) {
1219 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1220 sizeof(group_wpa_s->p2p_pin));
1221 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1222 }
1223 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1224 wpa_s->pending_interface_name[0] = '\0';
1225 group_wpa_s->p2p_in_provisioning = 1;
1226
1227 if (res->role_go)
1228 wpas_start_wps_go(group_wpa_s, res, 1);
1229 else
1230 wpas_start_wps_enrollee(group_wpa_s, res);
1231 } else {
1232 wpa_s->p2p_in_provisioning = 1;
1233 wpa_s->global->p2p_group_formation = wpa_s;
1234
1235 if (res->role_go)
1236 wpas_start_wps_go(wpa_s, res, 1);
1237 else
1238 wpas_start_wps_enrollee(ctx, res);
1239 }
1240
1241 wpa_s->p2p_long_listen = 0;
1242 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1243
1244 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1245 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1246 (res->peer_config_timeout % 100) * 10000,
1247 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1248}
1249
1250
1251void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1252{
1253 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001254 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1255 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256
1257 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1258}
1259
1260
1261void wpas_dev_found(void *ctx, const u8 *addr,
1262 const struct p2p_peer_info *info,
1263 int new_device)
1264{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001265#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 struct wpa_supplicant *wpa_s = ctx;
1267 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001268#define WFD_DEV_INFO_SIZE 9
1269 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001270 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001271#ifdef CONFIG_WIFI_DISPLAY
1272 if (info->wfd_subelems) {
1273 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1274 wpabuf_head(info->wfd_subelems),
1275 WFD_DEV_INFO_SIZE);
1276 }
1277#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001278 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1279 " p2p_dev_addr=" MACSTR
1280 " pri_dev_type=%s name='%s' config_methods=0x%x "
1281 "dev_capab=0x%x group_capab=0x%x%s%s",
1282 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1283 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1284 sizeof(devtype)),
1285 info->device_name, info->config_methods,
1286 info->dev_capab, info->group_capab,
1287 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001288#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289
1290 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1291}
1292
1293
1294static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1295{
1296 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001297
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001298 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1299 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1302}
1303
1304
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001305static void wpas_find_stopped(void *ctx)
1306{
1307 struct wpa_supplicant *wpa_s = ctx;
1308 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1309}
1310
1311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312static int wpas_start_listen(void *ctx, unsigned int freq,
1313 unsigned int duration,
1314 const struct wpabuf *probe_resp_ie)
1315{
1316 struct wpa_supplicant *wpa_s = ctx;
1317
1318 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1319
1320 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1321 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1322 "report received Probe Request frames");
1323 return -1;
1324 }
1325
1326 wpa_s->pending_listen_freq = freq;
1327 wpa_s->pending_listen_duration = duration;
1328
1329 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1330 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1331 "to remain on channel (%u MHz) for Listen "
1332 "state", freq);
1333 wpa_s->pending_listen_freq = 0;
1334 return -1;
1335 }
1336 wpa_s->off_channel_freq = 0;
1337 wpa_s->roc_waiting_drv_freq = freq;
1338
1339 return 0;
1340}
1341
1342
1343static void wpas_stop_listen(void *ctx)
1344{
1345 struct wpa_supplicant *wpa_s = ctx;
1346 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1347 wpa_drv_cancel_remain_on_channel(wpa_s);
1348 wpa_s->off_channel_freq = 0;
1349 wpa_s->roc_waiting_drv_freq = 0;
1350 }
1351 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1352 wpa_drv_probe_req_report(wpa_s, 0);
1353}
1354
1355
1356static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1357{
1358 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001359 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360}
1361
1362
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001363/*
1364 * DNS Header section is used only to calculate compression pointers, so the
1365 * contents of this data does not matter, but the length needs to be reserved
1366 * in the virtual packet.
1367 */
1368#define DNS_HEADER_LEN 12
1369
1370/*
1371 * 27-octet in-memory packet from P2P specification containing two implied
1372 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1373 */
1374#define P2P_SD_IN_MEMORY_LEN 27
1375
1376static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1377 u8 **spos, const u8 *end)
1378{
1379 while (*spos < end) {
1380 u8 val = ((*spos)[0] & 0xc0) >> 6;
1381 int len;
1382
1383 if (val == 1 || val == 2) {
1384 /* These are reserved values in RFC 1035 */
1385 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1386 "sequence starting with 0x%x", val);
1387 return -1;
1388 }
1389
1390 if (val == 3) {
1391 u16 offset;
1392 u8 *spos_tmp;
1393
1394 /* Offset */
1395 if (*spos + 2 > end) {
1396 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1397 "DNS offset field");
1398 return -1;
1399 }
1400
1401 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1402 if (offset >= *spos - start) {
1403 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1404 "pointer offset %u", offset);
1405 return -1;
1406 }
1407
1408 (*spos) += 2;
1409 spos_tmp = start + offset;
1410 return p2p_sd_dns_uncompress_label(upos, uend, start,
1411 &spos_tmp,
1412 *spos - 2);
1413 }
1414
1415 /* Label */
1416 len = (*spos)[0] & 0x3f;
1417 if (len == 0)
1418 return 0;
1419
1420 (*spos)++;
1421 if (*spos + len > end) {
1422 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1423 "sequence - no room for label with length "
1424 "%u", len);
1425 return -1;
1426 }
1427
1428 if (*upos + len + 2 > uend)
1429 return -2;
1430
1431 os_memcpy(*upos, *spos, len);
1432 *spos += len;
1433 *upos += len;
1434 (*upos)[0] = '.';
1435 (*upos)++;
1436 (*upos)[0] = '\0';
1437 }
1438
1439 return 0;
1440}
1441
1442
1443/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1444 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1445 * not large enough */
1446static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1447 size_t msg_len, size_t offset)
1448{
1449 /* 27-octet in-memory packet from P2P specification */
1450 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1451 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1452 u8 *tmp, *end, *spos;
1453 char *upos, *uend;
1454 int ret = 0;
1455
1456 if (buf_len < 2)
1457 return -1;
1458 if (offset > msg_len)
1459 return -1;
1460
1461 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1462 if (tmp == NULL)
1463 return -1;
1464 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1465 end = spos + msg_len;
1466 spos += offset;
1467
1468 os_memset(tmp, 0, DNS_HEADER_LEN);
1469 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1470 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1471
1472 upos = buf;
1473 uend = buf + buf_len;
1474
1475 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1476 if (ret) {
1477 os_free(tmp);
1478 return ret;
1479 }
1480
1481 if (upos == buf) {
1482 upos[0] = '.';
1483 upos[1] = '\0';
1484 } else if (upos[-1] == '.')
1485 upos[-1] = '\0';
1486
1487 os_free(tmp);
1488 return 0;
1489}
1490
1491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492static struct p2p_srv_bonjour *
1493wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1494 const struct wpabuf *query)
1495{
1496 struct p2p_srv_bonjour *bsrv;
1497 size_t len;
1498
1499 len = wpabuf_len(query);
1500 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1501 struct p2p_srv_bonjour, list) {
1502 if (len == wpabuf_len(bsrv->query) &&
1503 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1504 len) == 0)
1505 return bsrv;
1506 }
1507 return NULL;
1508}
1509
1510
1511static struct p2p_srv_upnp *
1512wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1513 const char *service)
1514{
1515 struct p2p_srv_upnp *usrv;
1516
1517 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1518 struct p2p_srv_upnp, list) {
1519 if (version == usrv->version &&
1520 os_strcmp(service, usrv->service) == 0)
1521 return usrv;
1522 }
1523 return NULL;
1524}
1525
1526
1527static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1528 u8 srv_trans_id)
1529{
1530 u8 *len_pos;
1531
1532 if (wpabuf_tailroom(resp) < 5)
1533 return;
1534
1535 /* Length (to be filled) */
1536 len_pos = wpabuf_put(resp, 2);
1537 wpabuf_put_u8(resp, srv_proto);
1538 wpabuf_put_u8(resp, srv_trans_id);
1539 /* Status Code */
1540 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1541 /* Response Data: empty */
1542 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1543}
1544
1545
1546static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1547 struct wpabuf *resp, u8 srv_trans_id)
1548{
1549 struct p2p_srv_bonjour *bsrv;
1550 u8 *len_pos;
1551
1552 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1553
1554 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1555 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1556 return;
1557 }
1558
1559 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1560 struct p2p_srv_bonjour, list) {
1561 if (wpabuf_tailroom(resp) <
1562 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1563 return;
1564 /* Length (to be filled) */
1565 len_pos = wpabuf_put(resp, 2);
1566 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1567 wpabuf_put_u8(resp, srv_trans_id);
1568 /* Status Code */
1569 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1570 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1571 wpabuf_head(bsrv->resp),
1572 wpabuf_len(bsrv->resp));
1573 /* Response Data */
1574 wpabuf_put_buf(resp, bsrv->query); /* Key */
1575 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1576 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1577 2);
1578 }
1579}
1580
1581
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001582static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1583 size_t query_len)
1584{
1585 char str_rx[256], str_srv[256];
1586
1587 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1588 return 0; /* Too short to include DNS Type and Version */
1589 if (os_memcmp(query + query_len - 3,
1590 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1591 3) != 0)
1592 return 0; /* Mismatch in DNS Type or Version */
1593 if (query_len == wpabuf_len(bsrv->query) &&
1594 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1595 return 1; /* Binary match */
1596
1597 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1598 0))
1599 return 0; /* Failed to uncompress query */
1600 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1601 wpabuf_head(bsrv->query),
1602 wpabuf_len(bsrv->query) - 3, 0))
1603 return 0; /* Failed to uncompress service */
1604
1605 return os_strcmp(str_rx, str_srv) == 0;
1606}
1607
1608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1610 struct wpabuf *resp, u8 srv_trans_id,
1611 const u8 *query, size_t query_len)
1612{
1613 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001614 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001615 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616
1617 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1618 query, query_len);
1619 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1620 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1621 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1622 srv_trans_id);
1623 return;
1624 }
1625
1626 if (query_len == 0) {
1627 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1628 return;
1629 }
1630
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001631 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1632 struct p2p_srv_bonjour, list) {
1633 if (!match_bonjour_query(bsrv, query, query_len))
1634 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001636 if (wpabuf_tailroom(resp) <
1637 5 + query_len + wpabuf_len(bsrv->resp))
1638 return;
1639
1640 matches++;
1641
1642 /* Length (to be filled) */
1643 len_pos = wpabuf_put(resp, 2);
1644 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1645 wpabuf_put_u8(resp, srv_trans_id);
1646
1647 /* Status Code */
1648 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1649 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1650 wpabuf_head(bsrv->resp),
1651 wpabuf_len(bsrv->resp));
1652
1653 /* Response Data */
1654 wpabuf_put_data(resp, query, query_len); /* Key */
1655 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1656
1657 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1658 }
1659
1660 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001661 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1662 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001663 if (wpabuf_tailroom(resp) < 5)
1664 return;
1665
1666 /* Length (to be filled) */
1667 len_pos = wpabuf_put(resp, 2);
1668 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1669 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670
1671 /* Status Code */
1672 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1673 /* Response Data: empty */
1674 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1675 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677}
1678
1679
1680static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1681 struct wpabuf *resp, u8 srv_trans_id)
1682{
1683 struct p2p_srv_upnp *usrv;
1684 u8 *len_pos;
1685
1686 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1687
1688 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1689 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1690 return;
1691 }
1692
1693 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1694 struct p2p_srv_upnp, list) {
1695 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1696 return;
1697
1698 /* Length (to be filled) */
1699 len_pos = wpabuf_put(resp, 2);
1700 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1701 wpabuf_put_u8(resp, srv_trans_id);
1702
1703 /* Status Code */
1704 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1705 /* Response Data */
1706 wpabuf_put_u8(resp, usrv->version);
1707 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1708 usrv->service);
1709 wpabuf_put_str(resp, usrv->service);
1710 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1711 2);
1712 }
1713}
1714
1715
1716static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1717 struct wpabuf *resp, u8 srv_trans_id,
1718 const u8 *query, size_t query_len)
1719{
1720 struct p2p_srv_upnp *usrv;
1721 u8 *len_pos;
1722 u8 version;
1723 char *str;
1724 int count = 0;
1725
1726 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1727 query, query_len);
1728
1729 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1730 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1731 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1732 srv_trans_id);
1733 return;
1734 }
1735
1736 if (query_len == 0) {
1737 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1738 return;
1739 }
1740
1741 if (wpabuf_tailroom(resp) < 5)
1742 return;
1743
1744 /* Length (to be filled) */
1745 len_pos = wpabuf_put(resp, 2);
1746 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1747 wpabuf_put_u8(resp, srv_trans_id);
1748
1749 version = query[0];
1750 str = os_malloc(query_len);
1751 if (str == NULL)
1752 return;
1753 os_memcpy(str, query + 1, query_len - 1);
1754 str[query_len - 1] = '\0';
1755
1756 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1757 struct p2p_srv_upnp, list) {
1758 if (version != usrv->version)
1759 continue;
1760
1761 if (os_strcmp(str, "ssdp:all") != 0 &&
1762 os_strstr(usrv->service, str) == NULL)
1763 continue;
1764
1765 if (wpabuf_tailroom(resp) < 2)
1766 break;
1767 if (count == 0) {
1768 /* Status Code */
1769 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1770 /* Response Data */
1771 wpabuf_put_u8(resp, version);
1772 } else
1773 wpabuf_put_u8(resp, ',');
1774
1775 count++;
1776
1777 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1778 usrv->service);
1779 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1780 break;
1781 wpabuf_put_str(resp, usrv->service);
1782 }
1783 os_free(str);
1784
1785 if (count == 0) {
1786 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1787 "available");
1788 /* Status Code */
1789 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1790 /* Response Data: empty */
1791 }
1792
1793 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1794}
1795
1796
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001797#ifdef CONFIG_WIFI_DISPLAY
1798static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1799 struct wpabuf *resp, u8 srv_trans_id,
1800 const u8 *query, size_t query_len)
1801{
1802 const u8 *pos;
1803 u8 role;
1804 u8 *len_pos;
1805
1806 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1807
1808 if (!wpa_s->global->wifi_display) {
1809 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1810 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1811 srv_trans_id);
1812 return;
1813 }
1814
1815 if (query_len < 1) {
1816 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1817 "Role");
1818 return;
1819 }
1820
1821 if (wpabuf_tailroom(resp) < 5)
1822 return;
1823
1824 pos = query;
1825 role = *pos++;
1826 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1827
1828 /* TODO: role specific handling */
1829
1830 /* Length (to be filled) */
1831 len_pos = wpabuf_put(resp, 2);
1832 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1833 wpabuf_put_u8(resp, srv_trans_id);
1834 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1835
1836 while (pos < query + query_len) {
1837 if (*pos < MAX_WFD_SUBELEMS &&
1838 wpa_s->global->wfd_subelem[*pos] &&
1839 wpabuf_tailroom(resp) >=
1840 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1841 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1842 "subelement %u", *pos);
1843 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1844 }
1845 pos++;
1846 }
1847
1848 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1849}
1850#endif /* CONFIG_WIFI_DISPLAY */
1851
1852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1854 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1855{
1856 struct wpa_supplicant *wpa_s = ctx;
1857 const u8 *pos = tlvs;
1858 const u8 *end = tlvs + tlvs_len;
1859 const u8 *tlv_end;
1860 u16 slen;
1861 struct wpabuf *resp;
1862 u8 srv_proto, srv_trans_id;
1863 size_t buf_len;
1864 char *buf;
1865
1866 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1867 tlvs, tlvs_len);
1868 buf_len = 2 * tlvs_len + 1;
1869 buf = os_malloc(buf_len);
1870 if (buf) {
1871 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1872 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1873 MACSTR " %u %u %s",
1874 freq, MAC2STR(sa), dialog_token, update_indic,
1875 buf);
1876 os_free(buf);
1877 }
1878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001879 if (wpa_s->p2p_sd_over_ctrl_iface) {
1880 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1881 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001883 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884
1885 resp = wpabuf_alloc(10000);
1886 if (resp == NULL)
1887 return;
1888
1889 while (pos + 1 < end) {
1890 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1891 slen = WPA_GET_LE16(pos);
1892 pos += 2;
1893 if (pos + slen > end || slen < 2) {
1894 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1895 "length");
1896 wpabuf_free(resp);
1897 return;
1898 }
1899 tlv_end = pos + slen;
1900
1901 srv_proto = *pos++;
1902 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1903 srv_proto);
1904 srv_trans_id = *pos++;
1905 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1906 srv_trans_id);
1907
1908 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1909 pos, tlv_end - pos);
1910
1911
1912 if (wpa_s->force_long_sd) {
1913 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1914 "response");
1915 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1916 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1917 goto done;
1918 }
1919
1920 switch (srv_proto) {
1921 case P2P_SERV_ALL_SERVICES:
1922 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1923 "for all services");
1924 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1925 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1926 wpa_printf(MSG_DEBUG, "P2P: No service "
1927 "discovery protocols available");
1928 wpas_sd_add_proto_not_avail(
1929 resp, P2P_SERV_ALL_SERVICES,
1930 srv_trans_id);
1931 break;
1932 }
1933 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1934 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1935 break;
1936 case P2P_SERV_BONJOUR:
1937 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1938 pos, tlv_end - pos);
1939 break;
1940 case P2P_SERV_UPNP:
1941 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1942 pos, tlv_end - pos);
1943 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001944#ifdef CONFIG_WIFI_DISPLAY
1945 case P2P_SERV_WIFI_DISPLAY:
1946 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1947 pos, tlv_end - pos);
1948 break;
1949#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001950 default:
1951 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1952 "protocol %u", srv_proto);
1953 wpas_sd_add_proto_not_avail(resp, srv_proto,
1954 srv_trans_id);
1955 break;
1956 }
1957
1958 pos = tlv_end;
1959 }
1960
1961done:
1962 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1963 update_indic, tlvs, tlvs_len);
1964
1965 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1966
1967 wpabuf_free(resp);
1968}
1969
1970
1971void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1972 const u8 *tlvs, size_t tlvs_len)
1973{
1974 struct wpa_supplicant *wpa_s = ctx;
1975 const u8 *pos = tlvs;
1976 const u8 *end = tlvs + tlvs_len;
1977 const u8 *tlv_end;
1978 u16 slen;
1979 size_t buf_len;
1980 char *buf;
1981
1982 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1983 tlvs, tlvs_len);
1984 if (tlvs_len > 1500) {
1985 /* TODO: better way for handling this */
1986 wpa_msg_ctrl(wpa_s, MSG_INFO,
1987 P2P_EVENT_SERV_DISC_RESP MACSTR
1988 " %u <long response: %u bytes>",
1989 MAC2STR(sa), update_indic,
1990 (unsigned int) tlvs_len);
1991 } else {
1992 buf_len = 2 * tlvs_len + 1;
1993 buf = os_malloc(buf_len);
1994 if (buf) {
1995 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1996 wpa_msg_ctrl(wpa_s, MSG_INFO,
1997 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1998 MAC2STR(sa), update_indic, buf);
1999 os_free(buf);
2000 }
2001 }
2002
2003 while (pos < end) {
2004 u8 srv_proto, srv_trans_id, status;
2005
2006 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2007 slen = WPA_GET_LE16(pos);
2008 pos += 2;
2009 if (pos + slen > end || slen < 3) {
2010 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2011 "length");
2012 return;
2013 }
2014 tlv_end = pos + slen;
2015
2016 srv_proto = *pos++;
2017 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2018 srv_proto);
2019 srv_trans_id = *pos++;
2020 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2021 srv_trans_id);
2022 status = *pos++;
2023 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2024 status);
2025
2026 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2027 pos, tlv_end - pos);
2028
2029 pos = tlv_end;
2030 }
2031
2032 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2033}
2034
2035
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002036u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2037 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038{
2039 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002040 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002042 return 0;
2043 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044}
2045
2046
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002047u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2048 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049{
2050 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002051 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052
2053 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2054 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002055 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2057 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2058 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2059 wpabuf_put_u8(tlvs, version);
2060 wpabuf_put_str(tlvs, query);
2061 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2062 wpabuf_free(tlvs);
2063 return ret;
2064}
2065
2066
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002067#ifdef CONFIG_WIFI_DISPLAY
2068
2069static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2070 const struct wpabuf *tlvs)
2071{
2072 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2073 return 0;
2074 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2075 return 0;
2076 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2077}
2078
2079
2080#define MAX_WFD_SD_SUBELEMS 20
2081
2082static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2083 const char *subelems)
2084{
2085 u8 *len;
2086 const char *pos;
2087 int val;
2088 int count = 0;
2089
2090 len = wpabuf_put(tlvs, 2);
2091 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2092 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2093
2094 wpabuf_put_u8(tlvs, role);
2095
2096 pos = subelems;
2097 while (*pos) {
2098 val = atoi(pos);
2099 if (val >= 0 && val < 256) {
2100 wpabuf_put_u8(tlvs, val);
2101 count++;
2102 if (count == MAX_WFD_SD_SUBELEMS)
2103 break;
2104 }
2105 pos = os_strchr(pos + 1, ',');
2106 if (pos == NULL)
2107 break;
2108 pos++;
2109 }
2110
2111 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2112}
2113
2114
2115u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2116 const u8 *dst, const char *role)
2117{
2118 struct wpabuf *tlvs;
2119 u64 ret;
2120 const char *subelems;
2121 u8 id = 1;
2122
2123 subelems = os_strchr(role, ' ');
2124 if (subelems == NULL)
2125 return 0;
2126 subelems++;
2127
2128 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2129 if (tlvs == NULL)
2130 return 0;
2131
2132 if (os_strstr(role, "[source]"))
2133 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2134 if (os_strstr(role, "[pri-sink]"))
2135 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2136 if (os_strstr(role, "[sec-sink]"))
2137 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2138 if (os_strstr(role, "[source+sink]"))
2139 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2140
2141 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2142 wpabuf_free(tlvs);
2143 return ret;
2144}
2145
2146#endif /* CONFIG_WIFI_DISPLAY */
2147
2148
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002149int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150{
2151 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002152 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2154 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002155 return p2p_sd_cancel_request(wpa_s->global->p2p,
2156 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157}
2158
2159
2160void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2161 const u8 *dst, u8 dialog_token,
2162 const struct wpabuf *resp_tlvs)
2163{
2164 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2165 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2166 resp_tlvs);
2167 return;
2168 }
2169 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2170 return;
2171 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2172 resp_tlvs);
2173}
2174
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002175#ifdef ANDROID_P2P
2176void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2177#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002179#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180{
2181 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2182 wpa_drv_p2p_service_update(wpa_s);
2183 return;
2184 }
2185 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002186#ifdef ANDROID_P2P
2187 p2p_sd_service_update(wpa_s->global->p2p, action);
2188#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002190#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191}
2192
2193
2194static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2195{
2196 dl_list_del(&bsrv->list);
2197 wpabuf_free(bsrv->query);
2198 wpabuf_free(bsrv->resp);
2199 os_free(bsrv);
2200}
2201
2202
2203static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2204{
2205 dl_list_del(&usrv->list);
2206 os_free(usrv->service);
2207 os_free(usrv);
2208}
2209
2210
2211void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2212{
2213 struct p2p_srv_bonjour *bsrv, *bn;
2214 struct p2p_srv_upnp *usrv, *un;
2215
2216 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2217 struct p2p_srv_bonjour, list)
2218 wpas_p2p_srv_bonjour_free(bsrv);
2219
2220 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2221 struct p2p_srv_upnp, list)
2222 wpas_p2p_srv_upnp_free(usrv);
2223
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002224#ifdef ANDROID_P2P
2225 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2226#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002228#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229}
2230
2231
2232int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2233 struct wpabuf *query, struct wpabuf *resp)
2234{
2235 struct p2p_srv_bonjour *bsrv;
2236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237 bsrv = os_zalloc(sizeof(*bsrv));
2238 if (bsrv == NULL)
2239 return -1;
2240 bsrv->query = query;
2241 bsrv->resp = resp;
2242 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2243
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002244#ifdef ANDROID_P2P
2245 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2246#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002248#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249 return 0;
2250}
2251
2252
2253int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2254 const struct wpabuf *query)
2255{
2256 struct p2p_srv_bonjour *bsrv;
2257
2258 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2259 if (bsrv == NULL)
2260 return -1;
2261 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002262#ifdef ANDROID_P2P
2263 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2264#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002265 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002266#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267 return 0;
2268}
2269
2270
2271int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2272 const char *service)
2273{
2274 struct p2p_srv_upnp *usrv;
2275
2276 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2277 return 0; /* Already listed */
2278 usrv = os_zalloc(sizeof(*usrv));
2279 if (usrv == NULL)
2280 return -1;
2281 usrv->version = version;
2282 usrv->service = os_strdup(service);
2283 if (usrv->service == NULL) {
2284 os_free(usrv);
2285 return -1;
2286 }
2287 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2288
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002289#ifdef ANDROID_P2P
2290 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2291#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002293#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 return 0;
2295}
2296
2297
2298int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2299 const char *service)
2300{
2301 struct p2p_srv_upnp *usrv;
2302
2303 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2304 if (usrv == NULL)
2305 return -1;
2306 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002307#ifdef ANDROID_P2P
2308 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2309#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002310 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002311#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002312 return 0;
2313}
2314
2315
2316static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2317 const u8 *peer, const char *params,
2318 unsigned int generated_pin)
2319{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002320 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2321 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322}
2323
2324
2325static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2326 const u8 *peer, const char *params)
2327{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002328 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2329 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002330}
2331
2332
2333void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2334 const u8 *dev_addr, const u8 *pri_dev_type,
2335 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002336 u8 dev_capab, u8 group_capab, const u8 *group_id,
2337 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338{
2339 struct wpa_supplicant *wpa_s = ctx;
2340 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002341 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 u8 empty_dev_type[8];
2343 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002344 struct wpa_supplicant *group = NULL;
2345
2346 if (group_id) {
2347 for (group = wpa_s->global->ifaces; group; group = group->next)
2348 {
2349 struct wpa_ssid *s = group->current_ssid;
2350 if (s != NULL &&
2351 s->mode == WPAS_MODE_P2P_GO &&
2352 group_id_len - ETH_ALEN == s->ssid_len &&
2353 os_memcmp(group_id + ETH_ALEN, s->ssid,
2354 s->ssid_len) == 0)
2355 break;
2356 }
2357 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358
2359 if (pri_dev_type == NULL) {
2360 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2361 pri_dev_type = empty_dev_type;
2362 }
2363 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2364 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002365 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 MAC2STR(dev_addr),
2367 wps_dev_type_bin2str(pri_dev_type, devtype,
2368 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002369 dev_name, supp_config_methods, dev_capab, group_capab,
2370 group ? " group=" : "",
2371 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 if (config_methods & WPS_CONFIG_DISPLAY) {
2375 generated_pin = wps_generate_pin();
2376 wpas_prov_disc_local_display(wpa_s, peer, params,
2377 generated_pin);
2378 } else if (config_methods & WPS_CONFIG_KEYPAD)
2379 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2380 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002381 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2382 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002383
2384 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2385 P2P_PROV_DISC_SUCCESS,
2386 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387}
2388
2389
2390void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2391{
2392 struct wpa_supplicant *wpa_s = ctx;
2393 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002394 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002396 if (wpa_s->pending_pd_before_join &&
2397 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2398 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2399 wpa_s->pending_pd_before_join = 0;
2400 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2401 "join-existing-group operation");
2402 wpas_p2p_join_start(wpa_s);
2403 return;
2404 }
2405
Dmitry Shmidt04949592012-07-19 12:16:46 -07002406 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2407 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2408 os_snprintf(params, sizeof(params), " peer_go=%d",
2409 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2410 else
2411 params[0] = '\0';
2412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002414 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415 else if (config_methods & WPS_CONFIG_KEYPAD) {
2416 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002417 wpas_prov_disc_local_display(wpa_s, peer, params,
2418 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002420 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2421 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002422
Jouni Malinen75ecf522011-06-27 15:19:46 -07002423 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2424 P2P_PROV_DISC_SUCCESS,
2425 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002426}
2427
2428
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002429static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2430 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002431{
2432 struct wpa_supplicant *wpa_s = ctx;
2433
Dmitry Shmidt04949592012-07-19 12:16:46 -07002434 if (wpa_s->p2p_fallback_to_go_neg) {
2435 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2436 "failed - fall back to GO Negotiation");
2437 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2438 return;
2439 }
2440
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002441 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002442 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002443 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2444 "join-existing-group operation (no ACK for PD "
2445 "Req attempts)");
2446 wpas_p2p_join_start(wpa_s);
2447 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002448 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002449
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002450 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2451 " p2p_dev_addr=" MACSTR " status=%d",
2452 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002453
Jouni Malinen75ecf522011-06-27 15:19:46 -07002454 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2455 status, 0, 0);
2456}
2457
2458
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002459static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2460{
2461 if (channels == NULL)
2462 return 1; /* Assume no restrictions */
2463 return p2p_channels_includes_freq(channels, freq);
2464
2465}
2466
2467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002468static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2469 const u8 *go_dev_addr, const u8 *ssid,
2470 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002471 int *force_freq, int persistent_group,
2472 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473{
2474 struct wpa_supplicant *wpa_s = ctx;
2475 struct wpa_ssid *s;
2476 u8 cur_bssid[ETH_ALEN];
2477 int res;
2478 struct wpa_supplicant *grp;
2479
2480 if (!persistent_group) {
2481 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2482 " to join an active group", MAC2STR(sa));
2483 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2484 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2485 == 0 ||
2486 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2487 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2488 "authorized invitation");
2489 goto accept_inv;
2490 }
2491 /*
2492 * Do not accept the invitation automatically; notify user and
2493 * request approval.
2494 */
2495 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2496 }
2497
2498 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2499 if (grp) {
2500 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2501 "running persistent group");
2502 if (*go)
2503 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2504 goto accept_inv;
2505 }
2506
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002507 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2508 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2509 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2510 "invitation to re-invoke a persistent group");
2511 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2513
2514 for (s = wpa_s->conf->ssid; s; s = s->next) {
2515 if (s->disabled == 2 &&
2516 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2517 s->ssid_len == ssid_len &&
2518 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2519 break;
2520 }
2521
2522 if (!s) {
2523 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2524 " requested reinvocation of an unknown group",
2525 MAC2STR(sa));
2526 return P2P_SC_FAIL_UNKNOWN_GROUP;
2527 }
2528
2529 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2530 *go = 1;
2531 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2532 wpa_printf(MSG_DEBUG, "P2P: The only available "
2533 "interface is already in use - reject "
2534 "invitation");
2535 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2536 }
2537 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2538 } else if (s->mode == WPAS_MODE_P2P_GO) {
2539 *go = 1;
2540 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2541 {
2542 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2543 "interface address for the group");
2544 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2545 }
2546 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2547 ETH_ALEN);
2548 }
2549
2550accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002551 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2554 wpa_s->assoc_freq) {
2555 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2556 "the channel we are already using");
2557 *force_freq = wpa_s->assoc_freq;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002558 wpas_p2p_set_own_freq_preference(wpa_s, wpa_s->assoc_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559 }
2560
2561 res = wpa_drv_shared_freq(wpa_s);
2562 if (res > 0) {
2563 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2564 "with the channel we are already using on a "
2565 "shared interface");
2566 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002567 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568 }
2569
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002570 if (*force_freq > 0 &&
2571 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2572 if (*go == 0) {
2573 /* We are the client */
2574 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2575 "running a GO but we are capable of MCC, "
2576 "figure out the best channel to use");
2577 *force_freq = 0;
2578 } else if (!freq_included(channels, *force_freq)) {
2579 /* We are the GO, and *force_freq is not in the
2580 * intersection */
2581 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2582 "in intersection but we are capable of MCC, "
2583 "figure out the best channel to use",
2584 *force_freq);
2585 *force_freq = 0;
2586 }
2587 }
2588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589 return P2P_SC_SUCCESS;
2590}
2591
2592
2593static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2594 const u8 *ssid, size_t ssid_len,
2595 const u8 *go_dev_addr, u8 status,
2596 int op_freq)
2597{
2598 struct wpa_supplicant *wpa_s = ctx;
2599 struct wpa_ssid *s;
2600
2601 for (s = wpa_s->conf->ssid; s; s = s->next) {
2602 if (s->disabled == 2 &&
2603 s->ssid_len == ssid_len &&
2604 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2605 break;
2606 }
2607
2608 if (status == P2P_SC_SUCCESS) {
2609 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2610 " was accepted; op_freq=%d MHz",
2611 MAC2STR(sa), op_freq);
2612 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002613 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002614 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002615 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002617 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002619 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620 }
2621 return;
2622 }
2623
2624 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2625 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2626 " was rejected (status %u)", MAC2STR(sa), status);
2627 return;
2628 }
2629
2630 if (!s) {
2631 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002632 wpa_msg_global(wpa_s, MSG_INFO,
2633 P2P_EVENT_INVITATION_RECEIVED
2634 "sa=" MACSTR " go_dev_addr=" MACSTR
2635 " bssid=" MACSTR " unknown-network",
2636 MAC2STR(sa), MAC2STR(go_dev_addr),
2637 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002639 wpa_msg_global(wpa_s, MSG_INFO,
2640 P2P_EVENT_INVITATION_RECEIVED
2641 "sa=" MACSTR " go_dev_addr=" MACSTR
2642 " unknown-network",
2643 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 }
2645 return;
2646 }
2647
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002648 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002649 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2650 "sa=" MACSTR " persistent=%d freq=%d",
2651 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002652 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002653 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2654 "sa=" MACSTR " persistent=%d",
2655 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002656 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657}
2658
2659
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002660static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2661 struct wpa_ssid *ssid,
2662 const u8 *peer)
2663{
2664 size_t i;
2665
2666 if (ssid == NULL)
2667 return;
2668
2669 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2670 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2671 ETH_ALEN) == 0)
2672 break;
2673 }
2674 if (i >= ssid->num_p2p_clients) {
2675 if (ssid->mode != WPAS_MODE_P2P_GO &&
2676 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2677 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2678 "due to invitation result", ssid->id);
2679 wpas_notify_network_removed(wpa_s, ssid);
2680 wpa_config_remove_network(wpa_s->conf, ssid->id);
2681 return;
2682 }
2683 return; /* Peer not found in client list */
2684 }
2685
2686 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2687 "group %d client list due to invitation result",
2688 MAC2STR(peer), ssid->id);
2689 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2690 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2691 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2692 ssid->num_p2p_clients--;
2693#ifndef CONFIG_NO_CONFIG_WRITE
2694 if (wpa_s->parent->conf->update_config &&
2695 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2696 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2697#endif /* CONFIG_NO_CONFIG_WRITE */
2698}
2699
2700
2701static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2702 const u8 *peer)
2703{
2704 struct wpa_ssid *ssid;
2705
2706 wpa_s = wpa_s->global->p2p_invite_group;
2707 if (wpa_s == NULL)
2708 return; /* No known invitation group */
2709 ssid = wpa_s->current_ssid;
2710 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2711 !ssid->p2p_persistent_group)
2712 return; /* Not operating as a GO in persistent group */
2713 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2714 ssid->ssid, ssid->ssid_len);
2715 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2716}
2717
2718
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002719static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002720 const struct p2p_channels *channels,
2721 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722{
2723 struct wpa_supplicant *wpa_s = ctx;
2724 struct wpa_ssid *ssid;
2725
2726 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002727 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2728 "status=%d " MACSTR,
2729 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002731 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2732 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733 }
2734 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2735
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002736 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2737 status, MAC2STR(peer));
2738 if (wpa_s->pending_invite_ssid_id == -1) {
2739 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2740 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002742 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002744 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2745 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2746 "invitation exchange to indicate readiness for "
2747 "re-invocation");
2748 }
2749
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002750 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002751 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2752 ssid = wpa_config_get_network(
2753 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2754 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2755 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756 wpas_p2p_remove_pending_group_interface(wpa_s);
2757 return;
2758 }
2759
2760 ssid = wpa_config_get_network(wpa_s->conf,
2761 wpa_s->pending_invite_ssid_id);
2762 if (ssid == NULL) {
2763 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2764 "data matching with invitation");
2765 return;
2766 }
2767
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002768 /*
2769 * The peer could have missed our ctrl::ack frame for Invitation
2770 * Response and continue retransmitting the frame. To reduce the
2771 * likelihood of the peer not getting successful TX status for the
2772 * Invitation Response frame, wait a short time here before starting
2773 * the persistent group so that we will remain on the current channel to
2774 * acknowledge any possible retransmission from the peer.
2775 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002776#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002777 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2778 "starting persistent group");
2779 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002780#else
2781 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2782 "starting persistent group");
2783 os_sleep(0, 100000);
2784#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002787 ssid->mode == WPAS_MODE_P2P_GO,
2788 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002789 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790}
2791
2792
Dmitry Shmidt04949592012-07-19 12:16:46 -07002793static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2794 unsigned int freq)
2795{
2796 unsigned int i;
2797
2798 if (global->p2p_disallow_freq == NULL)
2799 return 0;
2800
2801 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2802 if (freq >= global->p2p_disallow_freq[i].min &&
2803 freq <= global->p2p_disallow_freq[i].max)
2804 return 1;
2805 }
2806
2807 return 0;
2808}
2809
2810
2811static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2812{
2813 reg->channel[reg->channels] = chan;
2814 reg->channels++;
2815}
2816
2817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2819 struct p2p_channels *chan)
2820{
2821 int i, cla = 0;
2822
2823 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2824 "band");
2825
2826 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2827 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002828 chan->reg_class[cla].channels = 0;
2829 for (i = 0; i < 11; i++) {
2830 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2831 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2832 }
2833 if (chan->reg_class[cla].channels)
2834 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835
2836 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2837 "band");
2838
2839 /* Operating class 115 - 5 GHz, channels 36-48 */
2840 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002841 chan->reg_class[cla].channels = 0;
2842 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2843 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2844 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2845 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2846 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2847 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2848 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2849 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2850 if (chan->reg_class[cla].channels)
2851 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002852
2853 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2854 "band");
2855
2856 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2857 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002858 chan->reg_class[cla].channels = 0;
2859 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2860 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2861 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2862 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2863 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2864 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2865 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2866 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2867 if (chan->reg_class[cla].channels)
2868 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869
2870 chan->reg_classes = cla;
2871 return 0;
2872}
2873
2874
2875static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2876 u16 num_modes,
2877 enum hostapd_hw_mode mode)
2878{
2879 u16 i;
2880
2881 for (i = 0; i < num_modes; i++) {
2882 if (modes[i].mode == mode)
2883 return &modes[i];
2884 }
2885
2886 return NULL;
2887}
2888
2889
Dmitry Shmidt04949592012-07-19 12:16:46 -07002890static int has_channel(struct wpa_global *global,
2891 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892{
2893 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002894 unsigned int freq;
2895
2896 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2897 chan * 5;
2898 if (wpas_p2p_disallowed_freq(global, freq))
2899 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900
2901 for (i = 0; i < mode->num_channels; i++) {
2902 if (mode->channels[i].chan == chan) {
2903 if (flags)
2904 *flags = mode->channels[i].flag;
2905 return !(mode->channels[i].flag &
2906 (HOSTAPD_CHAN_DISABLED |
2907 HOSTAPD_CHAN_PASSIVE_SCAN |
2908 HOSTAPD_CHAN_NO_IBSS |
2909 HOSTAPD_CHAN_RADAR));
2910 }
2911 }
2912
2913 return 0;
2914}
2915
2916
2917struct p2p_oper_class_map {
2918 enum hostapd_hw_mode mode;
2919 u8 op_class;
2920 u8 min_chan;
2921 u8 max_chan;
2922 u8 inc;
2923 enum { BW20, BW40PLUS, BW40MINUS } bw;
2924};
2925
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002926static struct p2p_oper_class_map op_class[] = {
2927 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002928#if 0 /* Do not enable HT40 on 2 GHz for now */
2929 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2930 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2931#endif
2932 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2933 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2934 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2935 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2936 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2937 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2938 { -1, 0, 0, 0, 0, BW20 }
2939};
2940
2941
2942static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2943 struct hostapd_hw_modes *mode,
2944 u8 channel, u8 bw)
2945{
2946 int flag;
2947
2948 if (!has_channel(wpa_s->global, mode, channel, &flag))
2949 return -1;
2950 if (bw == BW40MINUS &&
2951 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2952 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2953 return 0;
2954 if (bw == BW40PLUS &&
2955 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2956 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2957 return 0;
2958 return 1;
2959}
2960
2961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2963 struct p2p_channels *chan)
2964{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002965 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002968 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2970 "of all supported channels; assume dualband "
2971 "support");
2972 return wpas_p2p_default_channels(wpa_s, chan);
2973 }
2974
2975 cla = 0;
2976
2977 for (op = 0; op_class[op].op_class; op++) {
2978 struct p2p_oper_class_map *o = &op_class[op];
2979 u8 ch;
2980 struct p2p_reg_class *reg = NULL;
2981
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002982 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002983 if (mode == NULL)
2984 continue;
2985 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002986 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002987 continue;
2988 if (reg == NULL) {
2989 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2990 "class %u", o->op_class);
2991 reg = &chan->reg_class[cla];
2992 cla++;
2993 reg->reg_class = o->op_class;
2994 }
2995 reg->channel[reg->channels] = ch;
2996 reg->channels++;
2997 }
2998 if (reg) {
2999 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3000 reg->channel, reg->channels);
3001 }
3002 }
3003
3004 chan->reg_classes = cla;
3005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 return 0;
3007}
3008
3009
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003010int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3011 struct hostapd_hw_modes *mode, u8 channel)
3012{
3013 int op, ret;
3014
3015 for (op = 0; op_class[op].op_class; op++) {
3016 struct p2p_oper_class_map *o = &op_class[op];
3017 u8 ch;
3018
3019 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3020 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3021 o->bw == BW20 || ch != channel)
3022 continue;
3023 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3024 if (ret < 0)
3025 continue;
3026 else if (ret > 0)
3027 return (o->bw == BW40MINUS) ? -1 : 1;
3028 else
3029 return 0;
3030 }
3031 }
3032 return 0;
3033}
3034
3035
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003036static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3037 size_t buf_len)
3038{
3039 struct wpa_supplicant *wpa_s = ctx;
3040
3041 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3042 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3043 break;
3044 }
3045 if (wpa_s == NULL)
3046 return -1;
3047
3048 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3049}
3050
3051
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003052static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3053{
3054 struct wpa_supplicant *wpa_s = ctx;
3055
3056 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3057 struct wpa_ssid *ssid = wpa_s->current_ssid;
3058 if (ssid == NULL)
3059 continue;
3060 if (ssid->mode != WPAS_MODE_INFRA)
3061 continue;
3062 if (wpa_s->wpa_state != WPA_COMPLETED &&
3063 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3064 continue;
3065 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3066 return 1;
3067 }
3068
3069 return 0;
3070}
3071
3072
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003073static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3074{
3075 struct wpa_supplicant *wpa_s = ctx;
3076 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3077}
3078
3079
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003080int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3081{
3082 struct wpa_interface iface;
3083 struct wpa_supplicant *p2pdev_wpa_s;
3084 char ifname[100];
3085 char force_name[100];
3086 int ret;
3087
3088 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3089 wpa_s->ifname);
3090 force_name[0] = '\0';
3091 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3092 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3093 force_name, wpa_s->pending_interface_addr, NULL);
3094 if (ret < 0) {
3095 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3096 return ret;
3097 }
3098 os_strlcpy(wpa_s->pending_interface_name, ifname,
3099 sizeof(wpa_s->pending_interface_name));
3100
3101 os_memset(&iface, 0, sizeof(iface));
3102 iface.p2p_mgmt = 1;
3103 iface.ifname = wpa_s->pending_interface_name;
3104 iface.driver = wpa_s->driver->name;
3105 iface.driver_param = wpa_s->conf->driver_param;
3106 iface.confname = wpa_s->confname;
3107 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3108 if (!p2pdev_wpa_s) {
3109 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3110 return -1;
3111 }
3112 p2pdev_wpa_s->parent = wpa_s;
3113
3114 wpa_s->pending_interface_name[0] = '\0';
3115 return 0;
3116}
3117
3118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003119/**
3120 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3121 * @global: Pointer to global data from wpa_supplicant_init()
3122 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3123 * Returns: 0 on success, -1 on failure
3124 */
3125int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3126{
3127 struct p2p_config p2p;
3128 unsigned int r;
3129 int i;
3130
3131 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3132 return 0;
3133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003134 if (global->p2p)
3135 return 0;
3136
3137 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3138 struct p2p_params params;
3139
3140 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3141 os_memset(&params, 0, sizeof(params));
3142 params.dev_name = wpa_s->conf->device_name;
3143 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3144 WPS_DEV_TYPE_LEN);
3145 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3146 os_memcpy(params.sec_dev_type,
3147 wpa_s->conf->sec_device_type,
3148 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3149
3150 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3151 return -1;
3152
3153 return 0;
3154 }
3155
3156 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003157 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003158 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003159 p2p.p2p_scan = wpas_p2p_scan;
3160 p2p.send_action = wpas_send_action;
3161 p2p.send_action_done = wpas_send_action_done;
3162 p2p.go_neg_completed = wpas_go_neg_completed;
3163 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3164 p2p.dev_found = wpas_dev_found;
3165 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003166 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003167 p2p.start_listen = wpas_start_listen;
3168 p2p.stop_listen = wpas_stop_listen;
3169 p2p.send_probe_resp = wpas_send_probe_resp;
3170 p2p.sd_request = wpas_sd_request;
3171 p2p.sd_response = wpas_sd_response;
3172 p2p.prov_disc_req = wpas_prov_disc_req;
3173 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003174 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003175 p2p.invitation_process = wpas_invitation_process;
3176 p2p.invitation_received = wpas_invitation_received;
3177 p2p.invitation_result = wpas_invitation_result;
3178 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003179 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003180
3181 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003182 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003183 p2p.dev_name = wpa_s->conf->device_name;
3184 p2p.manufacturer = wpa_s->conf->manufacturer;
3185 p2p.model_name = wpa_s->conf->model_name;
3186 p2p.model_number = wpa_s->conf->model_number;
3187 p2p.serial_number = wpa_s->conf->serial_number;
3188 if (wpa_s->wps) {
3189 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3190 p2p.config_methods = wpa_s->wps->config_methods;
3191 }
3192
3193 if (wpa_s->conf->p2p_listen_reg_class &&
3194 wpa_s->conf->p2p_listen_channel) {
3195 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3196 p2p.channel = wpa_s->conf->p2p_listen_channel;
3197 } else {
3198 p2p.reg_class = 81;
3199 /*
3200 * Pick one of the social channels randomly as the listen
3201 * channel.
3202 */
3203 os_get_random((u8 *) &r, sizeof(r));
3204 p2p.channel = 1 + (r % 3) * 5;
3205 }
3206 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3207
3208 if (wpa_s->conf->p2p_oper_reg_class &&
3209 wpa_s->conf->p2p_oper_channel) {
3210 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3211 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3212 p2p.cfg_op_channel = 1;
3213 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3214 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3215
3216 } else {
3217 p2p.op_reg_class = 81;
3218 /*
3219 * Use random operation channel from (1, 6, 11) if no other
3220 * preference is indicated.
3221 */
3222 os_get_random((u8 *) &r, sizeof(r));
3223 p2p.op_channel = 1 + (r % 3) * 5;
3224 p2p.cfg_op_channel = 0;
3225 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3226 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3227 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003228
3229 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3230 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3231 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3232 }
3233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3235 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3236 p2p.country[2] = 0x04;
3237 } else
3238 os_memcpy(p2p.country, "XX\x04", 3);
3239
3240 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3241 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3242 "channel list");
3243 return -1;
3244 }
3245
3246 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3247 WPS_DEV_TYPE_LEN);
3248
3249 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3250 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3251 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3252
3253 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3254 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3255
3256 p2p.max_peers = 100;
3257
3258 if (wpa_s->conf->p2p_ssid_postfix) {
3259 p2p.ssid_postfix_len =
3260 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3261 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3262 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3263 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3264 p2p.ssid_postfix_len);
3265 }
3266
3267 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3268
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003269 p2p.max_listen = wpa_s->max_remain_on_chan;
3270
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003271#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003272 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003273 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003274 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3275 } else {
3276 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003277 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003278 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3279 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003280#endif
3281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 global->p2p = p2p_init(&p2p);
3283 if (global->p2p == NULL)
3284 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003285 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286
3287 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3288 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3289 continue;
3290 p2p_add_wps_vendor_extension(
3291 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3292 }
3293
3294 return 0;
3295}
3296
3297
3298/**
3299 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3300 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3301 *
3302 * This function deinitialize per-interface P2P data.
3303 */
3304void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3305{
3306 if (wpa_s->driver && wpa_s->drv_priv)
3307 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003308
3309 if (wpa_s->go_params) {
3310 /* Clear any stored provisioning info */
3311 p2p_clear_provisioning_info(
3312 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003313 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003314 }
3315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316 os_free(wpa_s->go_params);
3317 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003318#ifdef ANDROID_P2P
3319 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3320#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3322 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3323 wpa_s->p2p_long_listen = 0;
3324 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3325 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3326 wpas_p2p_remove_pending_group_interface(wpa_s);
3327
3328 /* TODO: remove group interface from the driver if this wpa_s instance
3329 * is on top of a P2P group interface */
3330}
3331
3332
3333/**
3334 * wpas_p2p_deinit_global - Deinitialize global P2P module
3335 * @global: Pointer to global data from wpa_supplicant_init()
3336 *
3337 * This function deinitializes the global (per device) P2P module.
3338 */
3339void wpas_p2p_deinit_global(struct wpa_global *global)
3340{
3341 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003343 wpa_s = global->ifaces;
3344 if (wpa_s)
3345 wpas_p2p_service_flush(wpa_s);
3346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003347 if (global->p2p == NULL)
3348 return;
3349
3350 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003351 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3352 wpa_s = wpa_s->next;
3353 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003354 tmp = global->ifaces;
3355 while (tmp &&
3356 (tmp == wpa_s ||
3357 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3358 tmp = tmp->next;
3359 }
3360 if (tmp == NULL)
3361 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003362 /* Disconnect from the P2P group and deinit the interface */
3363 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364 }
3365
3366 /*
3367 * Deinit GO data on any possibly remaining interface (if main
3368 * interface is used as GO).
3369 */
3370 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3371 if (wpa_s->ap_iface)
3372 wpas_p2p_group_deinit(wpa_s);
3373 }
3374
3375 p2p_deinit(global->p2p);
3376 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003377 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378}
3379
3380
3381static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3382{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003383 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3384 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003385 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386 if (wpa_s->drv_flags &
3387 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3388 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3389 return 1; /* P2P group requires a new interface in every case
3390 */
3391 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3392 return 0; /* driver does not support concurrent operations */
3393 if (wpa_s->global->ifaces->next)
3394 return 1; /* more that one interface already in use */
3395 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3396 return 1; /* this interface is already in use */
3397 return 0;
3398}
3399
3400
3401static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3402 const u8 *peer_addr,
3403 enum p2p_wps_method wps_method,
3404 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003405 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003406 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003407{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003408 if (persistent_group && wpa_s->conf->persistent_reconnect)
3409 persistent_group = 2;
3410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003411 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3412 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3413 go_intent, own_interface_addr,
3414 force_freq, persistent_group);
3415 }
3416
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003417 /*
3418 * Increase GO config timeout if HT40 is used since it takes some time
3419 * to scan channels for coex purposes before the BSS can be started.
3420 */
3421 p2p_set_config_timeout(wpa_s->global->p2p,
3422 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003424 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3425 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003426 persistent_group, ssid ? ssid->ssid : NULL,
3427 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003428 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003429}
3430
3431
3432static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3433 const u8 *peer_addr,
3434 enum p2p_wps_method wps_method,
3435 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003436 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003437 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003438{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003439 if (persistent_group && wpa_s->conf->persistent_reconnect)
3440 persistent_group = 2;
3441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003442 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3443 return -1;
3444
3445 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3446 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003447 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003448 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003449}
3450
3451
3452static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3453{
3454 wpa_s->p2p_join_scan_count++;
3455 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3456 wpa_s->p2p_join_scan_count);
3457 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3458 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3459 " for join operationg - stop join attempt",
3460 MAC2STR(wpa_s->pending_join_iface_addr));
3461 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003462 if (wpa_s->p2p_auto_pd) {
3463 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003464 wpa_msg_global(wpa_s, MSG_INFO,
3465 P2P_EVENT_PROV_DISC_FAILURE
3466 " p2p_dev_addr=" MACSTR " status=N/A",
3467 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003468 return;
3469 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003470 wpa_msg_global(wpa_s->parent, MSG_INFO,
3471 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003472 }
3473}
3474
3475
Dmitry Shmidt04949592012-07-19 12:16:46 -07003476static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3477{
3478 struct wpa_supplicant *iface;
3479 int shared_freq;
3480 u8 bssid[ETH_ALEN];
3481
3482 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3483 return 0;
3484
3485 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3486 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3487 continue;
3488 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3489 continue;
3490 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3491 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3492 shared_freq = iface->current_ssid->frequency;
3493 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3494 shared_freq = iface->assoc_freq;
3495 else
3496 shared_freq = 0;
3497
3498 if (shared_freq && freq != shared_freq) {
3499 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3500 "connected on %d MHz - new connection on "
3501 "%d MHz", iface->ifname, shared_freq, freq);
3502 return 1;
3503 }
3504 }
3505
3506 shared_freq = wpa_drv_shared_freq(wpa_s);
3507 if (shared_freq > 0 && shared_freq != freq) {
3508 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3509 "virtual interface connected on %d MHz - new "
3510 "connection on %d MHz", shared_freq, freq);
3511 return 1;
3512 }
3513
3514 return 0;
3515}
3516
3517
3518static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3519 const u8 *peer_dev_addr)
3520{
3521 struct wpa_bss *bss;
3522 int updated;
3523
3524 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3525 if (bss == NULL)
3526 return -1;
3527 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3528 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3529 "last scan");
3530 return 0;
3531 }
3532
3533 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3534 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3535 "%ld.%06ld (%supdated in last scan)",
3536 bss->last_update.sec, bss->last_update.usec,
3537 updated ? "": "not ");
3538
3539 return updated;
3540}
3541
3542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003543static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3544 struct wpa_scan_results *scan_res)
3545{
3546 struct wpa_bss *bss;
3547 int freq;
3548 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3551
3552 if (wpa_s->global->p2p_disabled)
3553 return;
3554
Dmitry Shmidt04949592012-07-19 12:16:46 -07003555 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3556 scan_res ? (int) scan_res->num : -1,
3557 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003558
3559 if (scan_res)
3560 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3561
Dmitry Shmidt04949592012-07-19 12:16:46 -07003562 if (wpa_s->p2p_auto_pd) {
3563 int join = wpas_p2p_peer_go(wpa_s,
3564 wpa_s->pending_join_dev_addr);
3565 if (join == 0 &&
3566 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3567 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003568 bss = wpa_bss_get_bssid_latest(
3569 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003570 if (bss) {
3571 freq = bss->freq;
3572 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3573 "the peer " MACSTR " at %d MHz",
3574 wpa_s->auto_pd_scan_retry,
3575 MAC2STR(wpa_s->
3576 pending_join_dev_addr),
3577 freq);
3578 wpas_p2p_join_scan_req(wpa_s, freq);
3579 return;
3580 }
3581 }
3582
3583 if (join < 0)
3584 join = 0;
3585
3586 wpa_s->p2p_auto_pd = 0;
3587 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3588 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3589 MAC2STR(wpa_s->pending_join_dev_addr), join);
3590 if (p2p_prov_disc_req(wpa_s->global->p2p,
3591 wpa_s->pending_join_dev_addr,
3592 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003593 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003594 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003595 wpa_msg_global(wpa_s, MSG_INFO,
3596 P2P_EVENT_PROV_DISC_FAILURE
3597 " p2p_dev_addr=" MACSTR " status=N/A",
3598 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003599 }
3600 return;
3601 }
3602
3603 if (wpa_s->p2p_auto_join) {
3604 int join = wpas_p2p_peer_go(wpa_s,
3605 wpa_s->pending_join_dev_addr);
3606 if (join < 0) {
3607 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3608 "running a GO -> use GO Negotiation");
3609 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3610 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3611 wpa_s->p2p_persistent_group, 0, 0, 0,
3612 wpa_s->p2p_go_intent,
3613 wpa_s->p2p_connect_freq,
3614 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003615 wpa_s->p2p_pd_before_go_neg,
3616 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003617 return;
3618 }
3619
3620 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3621 "try to join the group", join ? "" :
3622 " in older scan");
3623 if (!join)
3624 wpa_s->p2p_fallback_to_go_neg = 1;
3625 }
3626
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003627 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3628 wpa_s->pending_join_iface_addr);
3629 if (freq < 0 &&
3630 p2p_get_interface_addr(wpa_s->global->p2p,
3631 wpa_s->pending_join_dev_addr,
3632 iface_addr) == 0 &&
3633 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3634 {
3635 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3636 "address for join from " MACSTR " to " MACSTR
3637 " based on newly discovered P2P peer entry",
3638 MAC2STR(wpa_s->pending_join_iface_addr),
3639 MAC2STR(iface_addr));
3640 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3641 ETH_ALEN);
3642
3643 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3644 wpa_s->pending_join_iface_addr);
3645 }
3646 if (freq >= 0) {
3647 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3648 "from P2P peer table: %d MHz", freq);
3649 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003650 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651 if (bss) {
3652 freq = bss->freq;
3653 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3654 "from BSS table: %d MHz", freq);
3655 }
3656 if (freq > 0) {
3657 u16 method;
3658
Dmitry Shmidt04949592012-07-19 12:16:46 -07003659 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003660 wpa_msg_global(wpa_s->parent, MSG_INFO,
3661 P2P_EVENT_GROUP_FORMATION_FAILURE
3662 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003663 return;
3664 }
3665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3667 "prior to joining an existing group (GO " MACSTR
3668 " freq=%u MHz)",
3669 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3670 wpa_s->pending_pd_before_join = 1;
3671
3672 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673 case WPS_PIN_DISPLAY:
3674 method = WPS_CONFIG_KEYPAD;
3675 break;
3676 case WPS_PIN_KEYPAD:
3677 method = WPS_CONFIG_DISPLAY;
3678 break;
3679 case WPS_PBC:
3680 method = WPS_CONFIG_PUSHBUTTON;
3681 break;
3682 default:
3683 method = 0;
3684 break;
3685 }
3686
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003687 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3688 wpa_s->pending_join_dev_addr) ==
3689 method)) {
3690 /*
3691 * We have already performed provision discovery for
3692 * joining the group. Proceed directly to join
3693 * operation without duplicated provision discovery. */
3694 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3695 "with " MACSTR " already done - proceed to "
3696 "join",
3697 MAC2STR(wpa_s->pending_join_dev_addr));
3698 wpa_s->pending_pd_before_join = 0;
3699 goto start;
3700 }
3701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003703 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003704 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003705 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3706 "Discovery Request before joining an "
3707 "existing group");
3708 wpa_s->pending_pd_before_join = 0;
3709 goto start;
3710 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003711 return;
3712 }
3713
3714 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3715 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3716 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3717 wpas_p2p_check_join_scan_limit(wpa_s);
3718 return;
3719
3720start:
3721 /* Start join operation immediately */
3722 wpas_p2p_join_start(wpa_s);
3723}
3724
3725
Dmitry Shmidt04949592012-07-19 12:16:46 -07003726static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003728 int ret;
3729 struct wpa_driver_scan_params params;
3730 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003731 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003732 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003733#ifdef ANDROID_P2P
3734 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003735
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003736 /* If freq is not provided, check the operating freq of the GO and do a
3737 * a directed scan to save time
3738 */
3739 if(!freq) {
3740 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3741 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3742 }
3743#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 os_memset(&params, 0, sizeof(params));
3745
3746 /* P2P Wildcard SSID */
3747 params.num_ssids = 1;
3748 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3749 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3750
3751 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003752 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3753 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3754 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003755 if (wps_ie == NULL) {
3756 wpas_p2p_scan_res_join(wpa_s, NULL);
3757 return;
3758 }
3759
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003760 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3761 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762 if (ies == NULL) {
3763 wpabuf_free(wps_ie);
3764 wpas_p2p_scan_res_join(wpa_s, NULL);
3765 return;
3766 }
3767 wpabuf_put_buf(ies, wps_ie);
3768 wpabuf_free(wps_ie);
3769
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003770 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003772 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773 params.extra_ies = wpabuf_head(ies);
3774 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003775 if (freq > 0) {
3776 freqs[0] = freq;
3777 params.freqs = freqs;
3778 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779
3780 /*
3781 * Run a scan to update BSS table and start Provision Discovery once
3782 * the new scan results become available.
3783 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003784 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003785 if (!ret) {
3786 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003787 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003788 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003789
3790 wpabuf_free(ies);
3791
3792 if (ret) {
3793 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3794 "try again later");
3795 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3796 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3797 wpas_p2p_check_join_scan_limit(wpa_s);
3798 }
3799}
3800
3801
Dmitry Shmidt04949592012-07-19 12:16:46 -07003802static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3803{
3804 struct wpa_supplicant *wpa_s = eloop_ctx;
3805 wpas_p2p_join_scan_req(wpa_s, 0);
3806}
3807
3808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003810 const u8 *dev_addr, enum p2p_wps_method wps_method,
3811 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003812{
3813 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003814 MACSTR " dev " MACSTR ")%s",
3815 MAC2STR(iface_addr), MAC2STR(dev_addr),
3816 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003817
Dmitry Shmidt04949592012-07-19 12:16:46 -07003818 wpa_s->p2p_auto_pd = 0;
3819 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3821 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3822 wpa_s->pending_join_wps_method = wps_method;
3823
3824 /* Make sure we are not running find during connection establishment */
3825 wpas_p2p_stop_find(wpa_s);
3826
3827 wpa_s->p2p_join_scan_count = 0;
3828 wpas_p2p_join_scan(wpa_s, NULL);
3829 return 0;
3830}
3831
3832
3833static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3834{
3835 struct wpa_supplicant *group;
3836 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003837 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838
3839 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3840 if (group == NULL)
3841 return -1;
3842 if (group != wpa_s) {
3843 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3844 sizeof(group->p2p_pin));
3845 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003846 } else {
3847 /*
3848 * Need to mark the current interface for p2p_group_formation
3849 * when a separate group interface is not used. This is needed
3850 * to allow p2p_cancel stop a pending p2p_connect-join.
3851 * wpas_p2p_init_group_interface() addresses this for the case
3852 * where a separate group interface is used.
3853 */
3854 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003855 }
3856
3857 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003858 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003859
3860 os_memset(&res, 0, sizeof(res));
3861 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3862 ETH_ALEN);
3863 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003864 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003865 if (bss) {
3866 res.freq = bss->freq;
3867 res.ssid_len = bss->ssid_len;
3868 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3869 }
3870
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003871 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3872 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3873 "starting client");
3874 wpa_drv_cancel_remain_on_channel(wpa_s);
3875 wpa_s->off_channel_freq = 0;
3876 wpa_s->roc_waiting_drv_freq = 0;
3877 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 wpas_start_wps_enrollee(group, &res);
3879
3880 /*
3881 * Allow a longer timeout for join-a-running-group than normal 15
3882 * second group formation timeout since the GO may not have authorized
3883 * our connection yet.
3884 */
3885 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3886 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3887 wpa_s, NULL);
3888
3889 return 0;
3890}
3891
3892
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003893static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3894 int *force_freq, int *pref_freq,
3895 int *oper_freq)
3896{
3897 if (freq > 0) {
3898 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3899 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3900 "(%u MHz) is not supported for P2P uses",
3901 freq);
3902 return -3;
3903 }
3904
3905 if (*oper_freq > 0 && freq != *oper_freq &&
3906 !(wpa_s->drv_flags &
3907 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3908 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3909 "on %u MHz while connected on another "
3910 "channel (%u MHz)", freq, *oper_freq);
3911 return -2;
3912 }
3913 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3914 "requested channel (%u MHz)", freq);
3915 *force_freq = freq;
3916 } else if (*oper_freq > 0 &&
3917 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3918 if (!(wpa_s->drv_flags &
3919 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3920 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3921 "while connected on non-P2P supported "
3922 "channel (%u MHz)", *oper_freq);
3923 return -2;
3924 }
3925 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3926 "(%u MHz) not available for P2P - try to use "
3927 "another channel", *oper_freq);
3928 *force_freq = 0;
3929 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3930 (wpa_s->drv_flags &
3931 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3932 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3933 "are already using (%u MHz) on another interface",
3934 *oper_freq);
3935 *pref_freq = *oper_freq;
3936 } else if (*oper_freq > 0) {
3937 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3938 "channel we are already using (%u MHz) on another "
3939 "interface", *oper_freq);
3940 *force_freq = *oper_freq;
3941 }
3942
3943 return 0;
3944}
3945
3946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947/**
3948 * wpas_p2p_connect - Request P2P Group Formation to be started
3949 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3950 * @peer_addr: Address of the peer P2P Device
3951 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3952 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003953 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 * @join: Whether to join an existing group (as a client) instead of starting
3955 * Group Owner negotiation; @peer_addr is BSSID in that case
3956 * @auth: Whether to only authorize the connection instead of doing that and
3957 * initiating Group Owner negotiation
3958 * @go_intent: GO Intent or -1 to use default
3959 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003960 * @persistent_id: Persistent group credentials to use for forcing GO
3961 * parameters or -1 to generate new values (SSID/passphrase)
3962 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3963 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003964 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003965 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3966 * failure, -2 on failure due to channel not currently available,
3967 * -3 if forced channel is not supported
3968 */
3969int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3970 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003971 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003972 int go_intent, int freq, int persistent_id, int pd,
3973 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003974{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003975 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 u8 bssid[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003977 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003979 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003980 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003981
3982 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3983 return -1;
3984
Dmitry Shmidt04949592012-07-19 12:16:46 -07003985 if (persistent_id >= 0) {
3986 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3987 if (ssid == NULL || ssid->disabled != 2 ||
3988 ssid->mode != WPAS_MODE_P2P_GO)
3989 return -1;
3990 }
3991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992 if (go_intent < 0)
3993 go_intent = wpa_s->conf->p2p_go_intent;
3994
3995 if (!auth)
3996 wpa_s->p2p_long_listen = 0;
3997
3998 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003999 wpa_s->p2p_persistent_group = !!persistent_group;
4000 wpa_s->p2p_persistent_id = persistent_id;
4001 wpa_s->p2p_go_intent = go_intent;
4002 wpa_s->p2p_connect_freq = freq;
4003 wpa_s->p2p_fallback_to_go_neg = 0;
4004 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004005 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004006
4007 if (pin)
4008 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4009 else if (wps_method == WPS_PIN_DISPLAY) {
4010 ret = wps_generate_pin();
4011 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4012 ret);
4013 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4014 wpa_s->p2p_pin);
4015 } else
4016 wpa_s->p2p_pin[0] = '\0';
4017
Dmitry Shmidt04949592012-07-19 12:16:46 -07004018 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004019 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4020 if (auth) {
4021 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4022 "connect a running group from " MACSTR,
4023 MAC2STR(peer_addr));
4024 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4025 return ret;
4026 }
4027 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4028 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4029 iface_addr) < 0) {
4030 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4031 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4032 dev_addr);
4033 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004034 if (auto_join) {
4035 os_get_time(&wpa_s->p2p_auto_started);
4036 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4037 "%ld.%06ld",
4038 wpa_s->p2p_auto_started.sec,
4039 wpa_s->p2p_auto_started.usec);
4040 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004041 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004042 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4043 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004044 return -1;
4045 return ret;
4046 }
4047
4048 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004049 wpa_s->assoc_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004050 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004051 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052 oper_freq = wpa_drv_shared_freq(wpa_s);
4053 if (oper_freq < 0)
4054 oper_freq = 0;
4055 }
4056
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004057 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4058 &oper_freq);
4059 if (res)
4060 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004061 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062
4063 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4064
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004065 if (wpa_s->create_p2p_iface) {
4066 /* Prepare to add a new interface for the group */
4067 iftype = WPA_IF_P2P_GROUP;
4068 if (go_intent == 15)
4069 iftype = WPA_IF_P2P_GO;
4070 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4071 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4072 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004074 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004076 if_addr = wpa_s->pending_interface_addr;
4077 } else
4078 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079
4080 if (auth) {
4081 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004082 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004083 force_freq, persistent_group, ssid,
4084 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085 return -1;
4086 return ret;
4087 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004088
4089 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4090 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004091 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004092 if (wpa_s->create_p2p_iface)
4093 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 return -1;
4095 }
4096 return ret;
4097}
4098
4099
4100/**
4101 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4102 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4103 * @freq: Frequency of the channel in MHz
4104 * @duration: Duration of the stay on the channel in milliseconds
4105 *
4106 * This callback is called when the driver indicates that it has started the
4107 * requested remain-on-channel duration.
4108 */
4109void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4110 unsigned int freq, unsigned int duration)
4111{
4112 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4113 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4115 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4116 wpa_s->pending_listen_duration);
4117 wpa_s->pending_listen_freq = 0;
4118 }
4119}
4120
4121
4122static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4123 unsigned int timeout)
4124{
4125 /* Limit maximum Listen state time based on driver limitation. */
4126 if (timeout > wpa_s->max_remain_on_chan)
4127 timeout = wpa_s->max_remain_on_chan;
4128
4129 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4130 return wpa_drv_p2p_listen(wpa_s, timeout);
4131
4132 return p2p_listen(wpa_s->global->p2p, timeout);
4133}
4134
4135
4136/**
4137 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4138 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4139 * @freq: Frequency of the channel in MHz
4140 *
4141 * This callback is called when the driver indicates that a remain-on-channel
4142 * operation has been completed, i.e., the duration on the requested channel
4143 * has timed out.
4144 */
4145void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4146 unsigned int freq)
4147{
4148 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4149 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004150 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4152 return;
4153 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4154 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004155 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004156 return;
4157 if (wpa_s->p2p_long_listen > 0)
4158 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4159 if (wpa_s->p2p_long_listen > 0) {
4160 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4161 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4162 }
4163}
4164
4165
4166/**
4167 * wpas_p2p_group_remove - Remove a P2P group
4168 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4169 * @ifname: Network interface name of the group interface or "*" to remove all
4170 * groups
4171 * Returns: 0 on success, -1 on failure
4172 *
4173 * This function is used to remove a P2P group. This can be used to disconnect
4174 * from a group in which the local end is a P2P Client or to end a P2P Group in
4175 * case the local end is the Group Owner. If a virtual network interface was
4176 * created for this group, that interface will be removed. Otherwise, only the
4177 * configured P2P group network will be removed from the interface.
4178 */
4179int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4180{
4181 struct wpa_global *global = wpa_s->global;
4182
4183 if (os_strcmp(ifname, "*") == 0) {
4184 struct wpa_supplicant *prev;
4185 wpa_s = global->ifaces;
4186 while (wpa_s) {
4187 prev = wpa_s;
4188 wpa_s = wpa_s->next;
4189 wpas_p2p_disconnect(prev);
4190 }
4191 return 0;
4192 }
4193
4194 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4195 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4196 break;
4197 }
4198
4199 return wpas_p2p_disconnect(wpa_s);
4200}
4201
4202
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004203static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4204{
4205 unsigned int r;
4206
4207 if (freq == 2) {
4208 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4209 "band");
4210 if (wpa_s->best_24_freq > 0 &&
4211 p2p_supported_freq(wpa_s->global->p2p,
4212 wpa_s->best_24_freq)) {
4213 freq = wpa_s->best_24_freq;
4214 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4215 "channel: %d MHz", freq);
4216 } else {
4217 os_get_random((u8 *) &r, sizeof(r));
4218 freq = 2412 + (r % 3) * 25;
4219 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4220 "channel: %d MHz", freq);
4221 }
4222 }
4223
4224 if (freq == 5) {
4225 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4226 "band");
4227 if (wpa_s->best_5_freq > 0 &&
4228 p2p_supported_freq(wpa_s->global->p2p,
4229 wpa_s->best_5_freq)) {
4230 freq = wpa_s->best_5_freq;
4231 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4232 "channel: %d MHz", freq);
4233 } else {
4234 os_get_random((u8 *) &r, sizeof(r));
4235 freq = 5180 + (r % 4) * 20;
4236 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4237 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4238 "5 GHz channel for P2P group");
4239 return -1;
4240 }
4241 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4242 "channel: %d MHz", freq);
4243 }
4244 }
4245
4246 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4247 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4248 "(%u MHz) is not supported for P2P uses",
4249 freq);
4250 return -1;
4251 }
4252
4253 return freq;
4254}
4255
4256
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004257static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4258 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004259 int freq, int ht40,
4260 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261{
4262 u8 bssid[ETH_ALEN];
4263 int res;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004264 unsigned int pref_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004265
4266 os_memset(params, 0, sizeof(*params));
4267 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004268 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004269 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004270 if (!freq_included(channels, freq)) {
4271 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4272 "accepted", freq);
4273 return -1;
4274 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004275 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4276 "frequency %d MHz", freq);
4277 params->freq = freq;
4278 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4279 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004280 wpa_s->conf->p2p_oper_channel <= 11 &&
4281 freq_included(channels,
4282 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4284 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4285 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004286 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4287 wpa_s->conf->p2p_oper_reg_class == 116 ||
4288 wpa_s->conf->p2p_oper_reg_class == 117 ||
4289 wpa_s->conf->p2p_oper_reg_class == 124 ||
4290 wpa_s->conf->p2p_oper_reg_class == 126 ||
4291 wpa_s->conf->p2p_oper_reg_class == 127) &&
4292 freq_included(channels,
4293 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4295 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4296 "frequency %d MHz", params->freq);
4297 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4298 wpa_s->best_overall_freq > 0 &&
4299 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004300 wpa_s->best_overall_freq) &&
4301 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 params->freq = wpa_s->best_overall_freq;
4303 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4304 "channel %d MHz", params->freq);
4305 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4306 wpa_s->best_24_freq > 0 &&
4307 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004308 wpa_s->best_24_freq) &&
4309 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310 params->freq = wpa_s->best_24_freq;
4311 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4312 "channel %d MHz", params->freq);
4313 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4314 wpa_s->best_5_freq > 0 &&
4315 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004316 wpa_s->best_5_freq) &&
4317 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318 params->freq = wpa_s->best_5_freq;
4319 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4320 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004321 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4322 channels))) {
4323 params->freq = pref_freq;
4324 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4325 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004327 int chan;
4328 for (chan = 0; chan < 11; chan++) {
4329 params->freq = 2412 + chan * 5;
4330 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004331 params->freq) &&
4332 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004333 break;
4334 }
4335 if (chan == 11) {
4336 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4337 "allowed");
4338 return -1;
4339 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4341 "known)", params->freq);
4342 }
4343
4344 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4345 wpa_s->assoc_freq && !freq) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004346 if (!p2p_supported_freq(wpa_s->global->p2p, wpa_s->assoc_freq)
4347 || !freq_included(channels, wpa_s->assoc_freq)) {
4348 if (wpa_s->drv_flags &
4349 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4350 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4351 "the channel we are already using "
4352 "(%u MHz) - allow multi-channel "
4353 "concurrency", wpa_s->assoc_freq);
4354 } else {
4355 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4356 "the channel we are already using "
4357 "(%u MHz)", wpa_s->assoc_freq);
4358 return -1;
4359 }
4360 } else {
4361 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we "
4362 "are already using (%u MHz)",
4363 wpa_s->assoc_freq);
4364 params->freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004365 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 }
4367
4368 res = wpa_drv_shared_freq(wpa_s);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004369 if (res > 0 && !freq &&
4370 (!p2p_supported_freq(wpa_s->global->p2p, res) ||
4371 !freq_included(channels, res))) {
4372 if (wpa_s->drv_flags &
4373 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4374 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4375 "channel we are already using on a shared "
4376 "interface (%u MHz) - allow multi-channel "
4377 "concurrency", res);
4378 } else {
4379 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4380 "channel we are already using on a shared "
4381 "interface (%u MHz)", res);
4382 return -1;
4383 }
4384 } else if (res > 0 && !freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004385 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4386 "already using on a shared interface");
4387 params->freq = res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004388 if (!freq_included(channels, params->freq)) {
4389 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4390 "accepted", params->freq);
4391 return -1;
4392 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004393 } else if (res > 0 && freq != res &&
4394 !(wpa_s->drv_flags &
4395 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4396 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4397 "while connected on another channel (%u MHz)",
4398 freq, res);
4399 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004400 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004401
4402 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004403}
4404
4405
4406static struct wpa_supplicant *
4407wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4408 int go)
4409{
4410 struct wpa_supplicant *group_wpa_s;
4411
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004412 if (!wpas_p2p_create_iface(wpa_s)) {
4413 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4414 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004415 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004416 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417
4418 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004419 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004420 wpa_msg_global(wpa_s, MSG_ERROR,
4421 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004422 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004423 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4425 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004426 wpa_msg_global(wpa_s, MSG_ERROR,
4427 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428 wpas_p2p_remove_pending_group_interface(wpa_s);
4429 return NULL;
4430 }
4431
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004432 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4433 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004434 return group_wpa_s;
4435}
4436
4437
4438/**
4439 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4440 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4441 * @persistent_group: Whether to create a persistent group
4442 * @freq: Frequency for the group or 0 to indicate no hardcoding
4443 * Returns: 0 on success, -1 on failure
4444 *
4445 * This function creates a new P2P group with the local end as the Group Owner,
4446 * i.e., without using Group Owner Negotiation.
4447 */
4448int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004449 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450{
4451 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004452
4453 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4454 return -1;
4455
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004456 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004457 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004458 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004459
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004460 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4461 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004463
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004464 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004465 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004466 if (params.freq &&
4467 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4468 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4469 "(%u MHz) is not supported for P2P uses",
4470 params.freq);
4471 return -1;
4472 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004473 p2p_go_params(wpa_s->global->p2p, &params);
4474 params.persistent_group = persistent_group;
4475
4476 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4477 if (wpa_s == NULL)
4478 return -1;
4479 wpas_start_wps_go(wpa_s, &params, 0);
4480
4481 return 0;
4482}
4483
4484
4485static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4486 struct wpa_ssid *params, int addr_allocated)
4487{
4488 struct wpa_ssid *ssid;
4489
4490 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4491 if (wpa_s == NULL)
4492 return -1;
4493
4494 wpa_supplicant_ap_deinit(wpa_s);
4495
4496 ssid = wpa_config_add_network(wpa_s->conf);
4497 if (ssid == NULL)
4498 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499 wpa_config_set_network_defaults(ssid);
4500 ssid->temporary = 1;
4501 ssid->proto = WPA_PROTO_RSN;
4502 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4503 ssid->group_cipher = WPA_CIPHER_CCMP;
4504 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4505 ssid->ssid = os_malloc(params->ssid_len);
4506 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004507 wpa_config_remove_network(wpa_s->conf, ssid->id);
4508 return -1;
4509 }
4510 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4511 ssid->ssid_len = params->ssid_len;
4512 ssid->p2p_group = 1;
4513 ssid->export_keys = 1;
4514 if (params->psk_set) {
4515 os_memcpy(ssid->psk, params->psk, 32);
4516 ssid->psk_set = 1;
4517 }
4518 if (params->passphrase)
4519 ssid->passphrase = os_strdup(params->passphrase);
4520
4521 wpa_supplicant_select_network(wpa_s, ssid);
4522
4523 wpa_s->show_group_started = 1;
4524
4525 return 0;
4526}
4527
4528
4529int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4530 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004531 int freq, int ht40,
4532 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004533{
4534 struct p2p_go_neg_results params;
4535 int go = 0;
4536
4537 if (ssid->disabled != 2 || ssid->ssid == NULL)
4538 return -1;
4539
4540 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4541 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4542 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4543 "already running");
4544 return 0;
4545 }
4546
4547 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004548 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004549
Dmitry Shmidt04949592012-07-19 12:16:46 -07004550 wpa_s->p2p_fallback_to_go_neg = 0;
4551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004552 if (ssid->mode == WPAS_MODE_INFRA)
4553 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4554
4555 if (ssid->mode != WPAS_MODE_P2P_GO)
4556 return -1;
4557
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004558 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4559 if (freq < 0)
4560 return -1;
4561
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004562 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004563 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564
4565 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004566 params.psk_set = ssid->psk_set;
4567 if (params.psk_set)
4568 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004569 if (ssid->passphrase) {
4570 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4571 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4572 "persistent group");
4573 return -1;
4574 }
4575 os_strlcpy(params.passphrase, ssid->passphrase,
4576 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004578 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4579 params.ssid_len = ssid->ssid_len;
4580 params.persistent_group = 1;
4581
4582 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4583 if (wpa_s == NULL)
4584 return -1;
4585
4586 wpas_start_wps_go(wpa_s, &params, 0);
4587
4588 return 0;
4589}
4590
4591
4592static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4593 struct wpabuf *proberesp_ies)
4594{
4595 struct wpa_supplicant *wpa_s = ctx;
4596 if (wpa_s->ap_iface) {
4597 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004598 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4599 wpabuf_free(beacon_ies);
4600 wpabuf_free(proberesp_ies);
4601 return;
4602 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004603 if (beacon_ies) {
4604 wpabuf_free(hapd->p2p_beacon_ie);
4605 hapd->p2p_beacon_ie = beacon_ies;
4606 }
4607 wpabuf_free(hapd->p2p_probe_resp_ie);
4608 hapd->p2p_probe_resp_ie = proberesp_ies;
4609 } else {
4610 wpabuf_free(beacon_ies);
4611 wpabuf_free(proberesp_ies);
4612 }
4613 wpa_supplicant_ap_update_beacon(wpa_s);
4614}
4615
4616
4617static void wpas_p2p_idle_update(void *ctx, int idle)
4618{
4619 struct wpa_supplicant *wpa_s = ctx;
4620 if (!wpa_s->ap_iface)
4621 return;
4622 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004623 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004624 wpas_p2p_set_group_idle_timeout(wpa_s);
4625 else
4626 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4627}
4628
4629
4630struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004631 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632{
4633 struct p2p_group *group;
4634 struct p2p_group_config *cfg;
4635
4636 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4637 return NULL;
4638 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4639 return NULL;
4640
4641 cfg = os_zalloc(sizeof(*cfg));
4642 if (cfg == NULL)
4643 return NULL;
4644
Dmitry Shmidt04949592012-07-19 12:16:46 -07004645 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004646 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004647 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004648 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004649 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4650 if (wpa_s->max_stations &&
4651 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4652 cfg->max_clients = wpa_s->max_stations;
4653 else
4654 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004655 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4656 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004657 cfg->cb_ctx = wpa_s;
4658 cfg->ie_update = wpas_p2p_ie_update;
4659 cfg->idle_update = wpas_p2p_idle_update;
4660
4661 group = p2p_group_init(wpa_s->global->p2p, cfg);
4662 if (group == NULL)
4663 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004664 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665 p2p_group_notif_formation_done(group);
4666 wpa_s->p2p_group = group;
4667 return group;
4668}
4669
4670
4671void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4672 int registrar)
4673{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004674 struct wpa_ssid *ssid = wpa_s->current_ssid;
4675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676 if (!wpa_s->p2p_in_provisioning) {
4677 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4678 "provisioning not in progress");
4679 return;
4680 }
4681
Dmitry Shmidt04949592012-07-19 12:16:46 -07004682 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4683 u8 go_dev_addr[ETH_ALEN];
4684 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4685 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4686 ssid->ssid_len);
4687 /* Clear any stored provisioning info */
4688 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4689 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004690
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004691 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4692 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004693 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4694 /*
4695 * Use a separate timeout for initial data connection to
4696 * complete to allow the group to be removed automatically if
4697 * something goes wrong in this step before the P2P group idle
4698 * timeout mechanism is taken into use.
4699 */
4700 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4701 wpas_p2p_group_formation_timeout,
4702 wpa_s->parent, NULL);
4703 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004704 if (wpa_s->global->p2p)
4705 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4706 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4707 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4708 wpas_group_formation_completed(wpa_s, 1);
4709}
4710
4711
Jouni Malinen75ecf522011-06-27 15:19:46 -07004712void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4713 struct wps_event_fail *fail)
4714{
4715 if (!wpa_s->p2p_in_provisioning) {
4716 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4717 "provisioning not in progress");
4718 return;
4719 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004720
4721 if (wpa_s->go_params) {
4722 p2p_clear_provisioning_info(
4723 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004724 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004725 }
4726
Jouni Malinen75ecf522011-06-27 15:19:46 -07004727 wpas_notify_p2p_wps_failed(wpa_s, fail);
4728}
4729
4730
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004731int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004732 const char *config_method,
4733 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004734{
4735 u16 config_methods;
4736
Dmitry Shmidt04949592012-07-19 12:16:46 -07004737 wpa_s->p2p_fallback_to_go_neg = 0;
4738 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004739 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004741 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004742 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004743 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4744 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004746 else {
4747 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004748 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004749 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750
Dmitry Shmidt04949592012-07-19 12:16:46 -07004751 if (use == WPAS_P2P_PD_AUTO) {
4752 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4753 wpa_s->pending_pd_config_methods = config_methods;
4754 wpa_s->p2p_auto_pd = 1;
4755 wpa_s->p2p_auto_join = 0;
4756 wpa_s->pending_pd_before_join = 0;
4757 wpa_s->auto_pd_scan_retry = 0;
4758 wpas_p2p_stop_find(wpa_s);
4759 wpa_s->p2p_join_scan_count = 0;
4760 os_get_time(&wpa_s->p2p_auto_started);
4761 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4762 wpa_s->p2p_auto_started.sec,
4763 wpa_s->p2p_auto_started.usec);
4764 wpas_p2p_join_scan(wpa_s, NULL);
4765 return 0;
4766 }
4767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004768 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4769 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004770 config_methods,
4771 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004772 }
4773
4774 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4775 return -1;
4776
4777 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004778 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004779 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004780}
4781
4782
4783int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4784 char *end)
4785{
4786 return p2p_scan_result_text(ies, ies_len, buf, end);
4787}
4788
4789
4790static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4791{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004792 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004793 return;
4794
4795 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4796 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004797 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798}
4799
4800
4801int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4802 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004803 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004804 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805{
4806 wpas_p2p_clear_pending_action_tx(wpa_s);
4807 wpa_s->p2p_long_listen = 0;
4808
4809 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4810 return wpa_drv_p2p_find(wpa_s, timeout, type);
4811
Dmitry Shmidt04949592012-07-19 12:16:46 -07004812 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4813 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 return -1;
4815
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004816 wpa_supplicant_cancel_sched_scan(wpa_s);
4817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004819 num_req_dev_types, req_dev_types, dev_id,
4820 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004821}
4822
4823
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004824static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825{
4826 wpas_p2p_clear_pending_action_tx(wpa_s);
4827 wpa_s->p2p_long_listen = 0;
4828 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4829 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004830 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004831
4832 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4833 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004834 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835 }
4836
4837 if (wpa_s->global->p2p)
4838 p2p_stop_find(wpa_s->global->p2p);
4839
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004840 return 0;
4841}
4842
4843
4844void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4845{
4846 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4847 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848 wpas_p2p_remove_pending_group_interface(wpa_s);
4849}
4850
4851
4852static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4853{
4854 struct wpa_supplicant *wpa_s = eloop_ctx;
4855 wpa_s->p2p_long_listen = 0;
4856}
4857
4858
4859int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4860{
4861 int res;
4862
4863 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4864 return -1;
4865
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004866 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 wpas_p2p_clear_pending_action_tx(wpa_s);
4868
4869 if (timeout == 0) {
4870 /*
4871 * This is a request for unlimited Listen state. However, at
4872 * least for now, this is mapped to a Listen state for one
4873 * hour.
4874 */
4875 timeout = 3600;
4876 }
4877 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4878 wpa_s->p2p_long_listen = 0;
4879
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004880 /*
4881 * Stop previous find/listen operation to avoid trying to request a new
4882 * remain-on-channel operation while the driver is still running the
4883 * previous one.
4884 */
4885 if (wpa_s->global->p2p)
4886 p2p_stop_find(wpa_s->global->p2p);
4887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4889 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4890 wpa_s->p2p_long_listen = timeout * 1000;
4891 eloop_register_timeout(timeout, 0,
4892 wpas_p2p_long_listen_timeout,
4893 wpa_s, NULL);
4894 }
4895
4896 return res;
4897}
4898
4899
4900int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4901 u8 *buf, size_t len, int p2p_group)
4902{
4903 struct wpabuf *p2p_ie;
4904 int ret;
4905
4906 if (wpa_s->global->p2p_disabled)
4907 return -1;
4908 if (wpa_s->global->p2p == NULL)
4909 return -1;
4910 if (bss == NULL)
4911 return -1;
4912
4913 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4914 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4915 p2p_group, p2p_ie);
4916 wpabuf_free(p2p_ie);
4917
4918 return ret;
4919}
4920
4921
4922int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004923 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004924 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004925{
4926 if (wpa_s->global->p2p_disabled)
4927 return 0;
4928 if (wpa_s->global->p2p == NULL)
4929 return 0;
4930
Dmitry Shmidt04949592012-07-19 12:16:46 -07004931 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4932 ie, ie_len)) {
4933 case P2P_PREQ_NOT_P2P:
4934 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4935 ssi_signal);
4936 /* fall through */
4937 case P2P_PREQ_MALFORMED:
4938 case P2P_PREQ_NOT_LISTEN:
4939 case P2P_PREQ_NOT_PROCESSED:
4940 default: /* make gcc happy */
4941 return 0;
4942 case P2P_PREQ_PROCESSED:
4943 return 1;
4944 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945}
4946
4947
4948void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4949 const u8 *sa, const u8 *bssid,
4950 u8 category, const u8 *data, size_t len, int freq)
4951{
4952 if (wpa_s->global->p2p_disabled)
4953 return;
4954 if (wpa_s->global->p2p == NULL)
4955 return;
4956
4957 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4958 freq);
4959}
4960
4961
4962void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4963{
4964 if (wpa_s->global->p2p_disabled)
4965 return;
4966 if (wpa_s->global->p2p == NULL)
4967 return;
4968
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004969 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004970}
4971
4972
4973void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4974{
4975 p2p_group_deinit(wpa_s->p2p_group);
4976 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004977
4978 wpa_s->ap_configured_cb = NULL;
4979 wpa_s->ap_configured_cb_ctx = NULL;
4980 wpa_s->ap_configured_cb_data = NULL;
4981 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004982}
4983
4984
4985int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4986{
4987 wpa_s->p2p_long_listen = 0;
4988
4989 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4990 return wpa_drv_p2p_reject(wpa_s, addr);
4991
4992 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4993 return -1;
4994
4995 return p2p_reject(wpa_s->global->p2p, addr);
4996}
4997
4998
4999/* Invite to reinvoke a persistent group */
5000int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005001 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005002 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003{
5004 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005005 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005006 int force_freq = 0, oper_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005007 int res;
5008
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005009 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005010 if (peer_addr)
5011 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5012 else
5013 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014
Jouni Malinen31be0a42012-08-31 21:20:51 +03005015 wpa_s->p2p_persistent_go_freq = freq;
5016 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017 if (ssid->mode == WPAS_MODE_P2P_GO) {
5018 role = P2P_INVITE_ROLE_GO;
5019 if (peer_addr == NULL) {
5020 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5021 "address in invitation command");
5022 return -1;
5023 }
5024 if (wpas_p2p_create_iface(wpa_s)) {
5025 if (wpas_p2p_add_group_interface(wpa_s,
5026 WPA_IF_P2P_GO) < 0) {
5027 wpa_printf(MSG_ERROR, "P2P: Failed to "
5028 "allocate a new interface for the "
5029 "group");
5030 return -1;
5031 }
5032 bssid = wpa_s->pending_interface_addr;
5033 } else
5034 bssid = wpa_s->own_addr;
5035 } else {
5036 role = P2P_INVITE_ROLE_CLIENT;
5037 peer_addr = ssid->bssid;
5038 }
5039 wpa_s->pending_invite_ssid_id = ssid->id;
5040
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005041 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
5042 wpa_s->assoc_freq) {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005043 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005044 if (bssid == NULL)
5045 bssid = bssid_buf;
5046 } else {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005047 oper_freq = wpa_drv_shared_freq(wpa_s);
5048 if (oper_freq < 0)
5049 oper_freq = 0;
5050 }
5051
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005052 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5053 &oper_freq);
5054 if (res)
5055 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005056
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5058 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5059 ssid->ssid, ssid->ssid_len,
5060 go_dev_addr, 1);
5061
5062 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5063 return -1;
5064
5065 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005066 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5067 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005068}
5069
5070
5071/* Invite to join an active group */
5072int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5073 const u8 *peer_addr, const u8 *go_dev_addr)
5074{
5075 struct wpa_global *global = wpa_s->global;
5076 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005077 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005078 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005079 int persistent;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005080 int force_freq = 0, oper_freq = 0, pref_freq = 0;
5081 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082
Jouni Malinen31be0a42012-08-31 21:20:51 +03005083 wpa_s->p2p_persistent_go_freq = 0;
5084 wpa_s->p2p_go_ht40 = 0;
5085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5087 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5088 break;
5089 }
5090 if (wpa_s == NULL) {
5091 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5092 return -1;
5093 }
5094
5095 ssid = wpa_s->current_ssid;
5096 if (ssid == NULL) {
5097 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5098 "invitation");
5099 return -1;
5100 }
5101
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005102 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005103 persistent = ssid->p2p_persistent_group &&
5104 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5105 ssid->ssid, ssid->ssid_len);
5106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005107 if (ssid->mode == WPAS_MODE_P2P_GO) {
5108 role = P2P_INVITE_ROLE_ACTIVE_GO;
5109 bssid = wpa_s->own_addr;
5110 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005111 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112 } else {
5113 role = P2P_INVITE_ROLE_CLIENT;
5114 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5115 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5116 "invite to current group");
5117 return -1;
5118 }
5119 bssid = wpa_s->bssid;
5120 if (go_dev_addr == NULL &&
5121 !is_zero_ether_addr(wpa_s->go_dev_addr))
5122 go_dev_addr = wpa_s->go_dev_addr;
5123 }
5124 wpa_s->parent->pending_invite_ssid_id = -1;
5125
5126 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5127 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5128 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005129 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005130
5131 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5132 return -1;
5133
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005134 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
5135 wpa_s->assoc_freq) {
5136 oper_freq = wpa_s->assoc_freq;
5137 if (bssid == NULL)
5138 bssid = bssid_buf;
5139 } else {
5140 oper_freq = wpa_drv_shared_freq(wpa_s);
5141 if (oper_freq < 0)
5142 oper_freq = 0;
5143 }
5144
5145 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
5146 &oper_freq);
5147 if (res)
5148 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005149 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005151 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005152 ssid->ssid, ssid->ssid_len, force_freq,
5153 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005154}
5155
5156
5157void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5158{
5159 struct wpa_ssid *ssid = wpa_s->current_ssid;
5160 const char *ssid_txt;
5161 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005162 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005163 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005164 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165
Dmitry Shmidt04949592012-07-19 12:16:46 -07005166 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5167 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5168 wpa_s->parent, NULL);
5169 }
5170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005171 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005172 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173
5174 wpa_s->show_group_started = 0;
5175
5176 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5177 os_memset(go_dev_addr, 0, ETH_ALEN);
5178 if (ssid->bssid_set)
5179 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5180 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5181 ssid->ssid_len);
5182 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5183
5184 if (wpa_s->global->p2p_group_formation == wpa_s)
5185 wpa_s->global->p2p_group_formation = NULL;
5186
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005187 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5188 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005189 if (ssid->passphrase == NULL && ssid->psk_set) {
5190 char psk[65];
5191 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005192 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5193 "%s client ssid=\"%s\" freq=%d psk=%s "
5194 "go_dev_addr=" MACSTR "%s",
5195 wpa_s->ifname, ssid_txt, freq, psk,
5196 MAC2STR(go_dev_addr),
5197 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005198 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005199 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5200 "%s client ssid=\"%s\" freq=%d "
5201 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5202 wpa_s->ifname, ssid_txt, freq,
5203 ssid->passphrase ? ssid->passphrase : "",
5204 MAC2STR(go_dev_addr),
5205 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005206 }
5207
5208 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005209 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5210 ssid, go_dev_addr);
5211 if (network_id < 0)
5212 network_id = ssid->id;
5213 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005214
5215done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005216 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005217}
5218
5219
5220int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5221 u32 interval1, u32 duration2, u32 interval2)
5222{
5223 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5224 return -1;
5225 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5226 return -1;
5227
5228 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5229 wpa_s->current_ssid == NULL ||
5230 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5231 return -1;
5232
5233 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5234 wpa_s->own_addr, wpa_s->assoc_freq,
5235 duration1, interval1, duration2, interval2);
5236}
5237
5238
5239int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5240 unsigned int interval)
5241{
5242 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5243 return -1;
5244
5245 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5246 return -1;
5247
5248 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5249}
5250
5251
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005252static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5253{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005254 if (wpa_s->current_ssid == NULL) {
5255 /*
5256 * current_ssid can be cleared when P2P client interface gets
5257 * disconnected, so assume this interface was used as P2P
5258 * client.
5259 */
5260 return 1;
5261 }
5262 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005263 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5264}
5265
5266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005267static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5268{
5269 struct wpa_supplicant *wpa_s = eloop_ctx;
5270
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005271 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005272 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5273 "disabled");
5274 return;
5275 }
5276
Dmitry Shmidt04949592012-07-19 12:16:46 -07005277 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5278 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005279 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005280}
5281
5282
5283static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5284{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005285 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005286
Dmitry Shmidt04949592012-07-19 12:16:46 -07005287 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5288 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005290 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5291 return;
5292
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005293 timeout = wpa_s->conf->p2p_group_idle;
5294 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5295 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5296 timeout = P2P_MAX_CLIENT_IDLE;
5297
5298 if (timeout == 0)
5299 return;
5300
Dmitry Shmidt04949592012-07-19 12:16:46 -07005301 if (timeout < 0) {
5302 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5303 timeout = 0; /* special client mode no-timeout */
5304 else
5305 return;
5306 }
5307
5308 if (wpa_s->p2p_in_provisioning) {
5309 /*
5310 * Use the normal group formation timeout during the
5311 * provisioning phase to avoid terminating this process too
5312 * early due to group idle timeout.
5313 */
5314 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5315 "during provisioning");
5316 return;
5317 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305318
Dmitry Shmidt04949592012-07-19 12:16:46 -07005319 if (wpa_s->show_group_started) {
5320 /*
5321 * Use the normal group formation timeout between the end of
5322 * the provisioning phase and completion of 4-way handshake to
5323 * avoid terminating this process too early due to group idle
5324 * timeout.
5325 */
5326 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5327 "while waiting for initial 4-way handshake to "
5328 "complete");
5329 return;
5330 }
5331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005332 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005333 timeout);
5334 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5335 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005336}
5337
5338
Jouni Malinen2b89da82012-08-31 22:04:41 +03005339/* Returns 1 if the interface was removed */
5340int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5341 u16 reason_code, const u8 *ie, size_t ie_len,
5342 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343{
5344 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005345 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005347 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005348
Dmitry Shmidt04949592012-07-19 12:16:46 -07005349 if (!locally_generated)
5350 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5351 ie_len);
5352
5353 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5354 wpa_s->current_ssid &&
5355 wpa_s->current_ssid->p2p_group &&
5356 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5357 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5358 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005359 if (wpas_p2p_group_delete(wpa_s,
5360 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5361 > 0)
5362 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005363 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005364
5365 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005366}
5367
5368
5369void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005370 u16 reason_code, const u8 *ie, size_t ie_len,
5371 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005372{
5373 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5374 return;
5375 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5376 return;
5377
Dmitry Shmidt04949592012-07-19 12:16:46 -07005378 if (!locally_generated)
5379 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5380 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005381}
5382
5383
5384void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5385{
5386 struct p2p_data *p2p = wpa_s->global->p2p;
5387
5388 if (p2p == NULL)
5389 return;
5390
5391 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5392 return;
5393
5394 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5395 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5396
5397 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5398 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5399
5400 if (wpa_s->wps &&
5401 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5402 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5403
5404 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5405 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5406
5407 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5408 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5409 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5410 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5411 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5412 }
5413
5414 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5415 p2p_set_sec_dev_types(p2p,
5416 (void *) wpa_s->conf->sec_device_type,
5417 wpa_s->conf->num_sec_device_types);
5418
5419 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5420 int i;
5421 p2p_remove_wps_vendor_extensions(p2p);
5422 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5423 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5424 continue;
5425 p2p_add_wps_vendor_extension(
5426 p2p, wpa_s->conf->wps_vendor_ext[i]);
5427 }
5428 }
5429
5430 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5431 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5432 char country[3];
5433 country[0] = wpa_s->conf->country[0];
5434 country[1] = wpa_s->conf->country[1];
5435 country[2] = 0x04;
5436 p2p_set_country(p2p, country);
5437 }
5438
5439 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5440 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5441 wpa_s->conf->p2p_ssid_postfix ?
5442 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5443 0);
5444 }
5445
5446 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5447 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005448
5449 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5450 u8 reg_class, channel;
5451 int ret;
5452 unsigned int r;
5453 if (wpa_s->conf->p2p_listen_reg_class &&
5454 wpa_s->conf->p2p_listen_channel) {
5455 reg_class = wpa_s->conf->p2p_listen_reg_class;
5456 channel = wpa_s->conf->p2p_listen_channel;
5457 } else {
5458 reg_class = 81;
5459 /*
5460 * Pick one of the social channels randomly as the
5461 * listen channel.
5462 */
5463 os_get_random((u8 *) &r, sizeof(r));
5464 channel = 1 + (r % 3) * 5;
5465 }
5466 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5467 if (ret)
5468 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5469 "failed: %d", ret);
5470 }
5471 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5472 u8 op_reg_class, op_channel, cfg_op_channel;
5473 int ret = 0;
5474 unsigned int r;
5475 if (wpa_s->conf->p2p_oper_reg_class &&
5476 wpa_s->conf->p2p_oper_channel) {
5477 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5478 op_channel = wpa_s->conf->p2p_oper_channel;
5479 cfg_op_channel = 1;
5480 } else {
5481 op_reg_class = 81;
5482 /*
5483 * Use random operation channel from (1, 6, 11)
5484 *if no other preference is indicated.
5485 */
5486 os_get_random((u8 *) &r, sizeof(r));
5487 op_channel = 1 + (r % 3) * 5;
5488 cfg_op_channel = 0;
5489 }
5490 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5491 cfg_op_channel);
5492 if (ret)
5493 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5494 "failed: %d", ret);
5495 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005496
5497 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5498 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5499 wpa_s->conf->p2p_pref_chan) < 0) {
5500 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5501 "update failed");
5502 }
5503 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005504}
5505
5506
5507int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5508 int duration)
5509{
5510 if (!wpa_s->ap_iface)
5511 return -1;
5512 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5513 duration);
5514}
5515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005516
5517int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5518{
5519 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5520 return -1;
5521 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5522 return -1;
5523
5524 wpa_s->global->cross_connection = enabled;
5525 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5526
5527 if (!enabled) {
5528 struct wpa_supplicant *iface;
5529
5530 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5531 {
5532 if (iface->cross_connect_enabled == 0)
5533 continue;
5534
5535 iface->cross_connect_enabled = 0;
5536 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005537 wpa_msg_global(iface->parent, MSG_INFO,
5538 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5539 iface->ifname,
5540 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005541 }
5542 }
5543
5544 return 0;
5545}
5546
5547
5548static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5549{
5550 struct wpa_supplicant *iface;
5551
5552 if (!uplink->global->cross_connection)
5553 return;
5554
5555 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5556 if (!iface->cross_connect_enabled)
5557 continue;
5558 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5559 0)
5560 continue;
5561 if (iface->ap_iface == NULL)
5562 continue;
5563 if (iface->cross_connect_in_use)
5564 continue;
5565
5566 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005567 wpa_msg_global(iface->parent, MSG_INFO,
5568 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5569 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005570 }
5571}
5572
5573
5574static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5575{
5576 struct wpa_supplicant *iface;
5577
5578 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5579 if (!iface->cross_connect_enabled)
5580 continue;
5581 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5582 0)
5583 continue;
5584 if (!iface->cross_connect_in_use)
5585 continue;
5586
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005587 wpa_msg_global(iface->parent, MSG_INFO,
5588 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5589 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005590 iface->cross_connect_in_use = 0;
5591 }
5592}
5593
5594
5595void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5596{
5597 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5598 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5599 wpa_s->cross_connect_disallowed)
5600 wpas_p2p_disable_cross_connect(wpa_s);
5601 else
5602 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005603 if (!wpa_s->ap_iface &&
5604 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5605 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005606}
5607
5608
5609void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5610{
5611 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005612 if (!wpa_s->ap_iface &&
5613 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5614 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005615 wpas_p2p_set_group_idle_timeout(wpa_s);
5616}
5617
5618
5619static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5620{
5621 struct wpa_supplicant *iface;
5622
5623 if (!wpa_s->global->cross_connection)
5624 return;
5625
5626 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5627 if (iface == wpa_s)
5628 continue;
5629 if (iface->drv_flags &
5630 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5631 continue;
5632 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5633 continue;
5634
5635 wpa_s->cross_connect_enabled = 1;
5636 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5637 sizeof(wpa_s->cross_connect_uplink));
5638 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5639 "%s to %s whenever uplink is available",
5640 wpa_s->ifname, wpa_s->cross_connect_uplink);
5641
5642 if (iface->ap_iface || iface->current_ssid == NULL ||
5643 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5644 iface->cross_connect_disallowed ||
5645 iface->wpa_state != WPA_COMPLETED)
5646 break;
5647
5648 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005649 wpa_msg_global(wpa_s->parent, MSG_INFO,
5650 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5651 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005652 break;
5653 }
5654}
5655
5656
5657int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5658{
5659 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5660 !wpa_s->p2p_in_provisioning)
5661 return 0; /* not P2P client operation */
5662
5663 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5664 "session overlap");
5665 if (wpa_s != wpa_s->parent)
5666 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005667 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005668 return 1;
5669}
5670
5671
5672void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5673{
5674 struct p2p_channels chan;
5675
5676 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5677 return;
5678
5679 os_memset(&chan, 0, sizeof(chan));
5680 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5681 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5682 "channel list");
5683 return;
5684 }
5685
5686 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5687}
5688
5689
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005690static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5691 struct wpa_scan_results *scan_res)
5692{
5693 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5694}
5695
5696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005697int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5698{
5699 struct wpa_global *global = wpa_s->global;
5700 int found = 0;
5701 const u8 *peer;
5702
5703 if (global->p2p == NULL)
5704 return -1;
5705
5706 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5707
5708 if (wpa_s->pending_interface_name[0] &&
5709 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5710 found = 1;
5711
5712 peer = p2p_get_go_neg_peer(global->p2p);
5713 if (peer) {
5714 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5715 MACSTR, MAC2STR(peer));
5716 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005717 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005718 }
5719
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005720 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5721 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5722 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5723 found = 1;
5724 }
5725
5726 if (wpa_s->pending_pd_before_join) {
5727 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5728 wpa_s->pending_pd_before_join = 0;
5729 found = 1;
5730 }
5731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005732 wpas_p2p_stop_find(wpa_s);
5733
5734 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5735 if (wpa_s == global->p2p_group_formation &&
5736 (wpa_s->p2p_in_provisioning ||
5737 wpa_s->parent->pending_interface_type ==
5738 WPA_IF_P2P_CLIENT)) {
5739 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5740 "formation found - cancelling",
5741 wpa_s->ifname);
5742 found = 1;
5743 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5744 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005745 if (wpa_s->p2p_in_provisioning) {
5746 wpas_group_formation_completed(wpa_s, 0);
5747 break;
5748 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005749 wpas_p2p_group_delete(wpa_s,
5750 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005751 break;
5752 }
5753 }
5754
5755 if (!found) {
5756 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5757 return -1;
5758 }
5759
5760 return 0;
5761}
5762
5763
5764void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5765{
5766 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5767 return;
5768
5769 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5770 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005771 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005772}
5773
5774
5775void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5776 int freq_24, int freq_5, int freq_overall)
5777{
5778 struct p2p_data *p2p = wpa_s->global->p2p;
5779 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5780 return;
5781 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5782}
5783
5784
5785int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5786{
5787 u8 peer[ETH_ALEN];
5788 struct p2p_data *p2p = wpa_s->global->p2p;
5789
5790 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5791 return -1;
5792
5793 if (hwaddr_aton(addr, peer))
5794 return -1;
5795
5796 return p2p_unauthorize(p2p, peer);
5797}
5798
5799
5800/**
5801 * wpas_p2p_disconnect - Disconnect from a P2P Group
5802 * @wpa_s: Pointer to wpa_supplicant data
5803 * Returns: 0 on success, -1 on failure
5804 *
5805 * This can be used to disconnect from a group in which the local end is a P2P
5806 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5807 * virtual network interface was created for this group, that interface will be
5808 * removed. Otherwise, only the configured P2P group network will be removed
5809 * from the interface.
5810 */
5811int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5812{
5813
5814 if (wpa_s == NULL)
5815 return -1;
5816
Jouni Malinen2b89da82012-08-31 22:04:41 +03005817 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5818 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005819}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005820
5821
5822int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5823{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005824 int ret;
5825
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005826 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5827 return 0;
5828
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005829 ret = p2p_in_progress(wpa_s->global->p2p);
5830 if (ret == 0) {
5831 /*
5832 * Check whether there is an ongoing WPS provisioning step (or
5833 * other parts of group formation) on another interface since
5834 * p2p_in_progress() does not report this to avoid issues for
5835 * scans during such provisioning step.
5836 */
5837 if (wpa_s->global->p2p_group_formation &&
5838 wpa_s->global->p2p_group_formation != wpa_s) {
5839 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5840 "in group formation",
5841 wpa_s->global->p2p_group_formation->ifname);
5842 ret = 1;
5843 }
5844 }
5845
5846 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005847}
5848
5849
5850void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5851 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005852{
5853 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5854 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5855 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005856 /**
5857 * Remove the network by scheduling the group formation
5858 * timeout to happen immediately. The teardown code
5859 * needs to be scheduled to run asynch later so that we
5860 * don't delete data from under ourselves unexpectedly.
5861 * Calling wpas_p2p_group_formation_timeout directly
5862 * causes a series of crashes in WPS failure scenarios.
5863 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005864 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5865 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005866 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5867 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005868 }
5869}
5870
5871
5872struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005873 const u8 *addr, const u8 *ssid,
5874 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005875{
5876 struct wpa_ssid *s;
5877 size_t i;
5878
5879 for (s = wpa_s->conf->ssid; s; s = s->next) {
5880 if (s->disabled != 2)
5881 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005882 if (ssid &&
5883 (ssid_len != s->ssid_len ||
5884 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5885 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005886 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5887 return s; /* peer is GO in the persistent group */
5888 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5889 continue;
5890 for (i = 0; i < s->num_p2p_clients; i++) {
5891 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5892 addr, ETH_ALEN) == 0)
5893 return s; /* peer is P2P client in persistent
5894 * group */
5895 }
5896 }
5897
5898 return NULL;
5899}
5900
5901
5902void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5903 const u8 *addr)
5904{
5905 if (addr == NULL)
5906 return;
5907 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5908}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005909
Dmitry Shmidt04949592012-07-19 12:16:46 -07005910
5911static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5912 int group_added)
5913{
5914 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005915 if (wpa_s->global->p2p_group_formation)
5916 group = wpa_s->global->p2p_group_formation;
5917 wpa_s = wpa_s->parent;
5918 offchannel_send_action_done(wpa_s);
5919 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005920 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005921 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5922 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5923 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5924 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5925 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005926 wpa_s->p2p_pd_before_go_neg,
5927 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005928}
5929
5930
5931int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5932{
5933 if (!wpa_s->p2p_fallback_to_go_neg ||
5934 wpa_s->p2p_in_provisioning <= 5)
5935 return 0;
5936
5937 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5938 return 0; /* peer operating as a GO */
5939
5940 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5941 "fallback to GO Negotiation");
5942 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5943
5944 return 1;
5945}
5946
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005947
5948unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5949{
5950 const char *rn, *rn2;
5951 struct wpa_supplicant *ifs;
5952
5953 if (wpa_s->wpa_state > WPA_SCANNING) {
5954 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5955 "concurrent operation",
5956 P2P_CONCURRENT_SEARCH_DELAY);
5957 return P2P_CONCURRENT_SEARCH_DELAY;
5958 }
5959
5960 if (!wpa_s->driver->get_radio_name)
5961 return 0;
5962 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5963 if (rn == NULL || rn[0] == '\0')
5964 return 0;
5965
5966 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5967 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5968 continue;
5969
5970 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5971 if (!rn2 || os_strcmp(rn, rn2) != 0)
5972 continue;
5973 if (ifs->wpa_state > WPA_SCANNING) {
5974 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5975 "delay due to concurrent operation on "
5976 "interface %s",
5977 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5978 return P2P_CONCURRENT_SEARCH_DELAY;
5979 }
5980 }
5981
5982 return 0;
5983}
5984
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005985
5986void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5987{
5988 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5989 "pending anymore (sta_scan_pending=%d "
5990 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5991 wpa_s->global->p2p_cb_on_scan_complete);
5992 wpa_s->sta_scan_pending = 0;
5993
5994 if (!wpa_s->global->p2p_cb_on_scan_complete)
5995 return;
5996 wpa_s->global->p2p_cb_on_scan_complete = 0;
5997
5998 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5999 return;
6000
6001 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6002 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6003 "continued after successful connection");
6004 p2p_increase_search_delay(wpa_s->global->p2p,
6005 wpas_p2p_search_delay(wpa_s));
6006 }
6007}
6008
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006009#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006010static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6011{
6012 struct wpa_supplicant *wpa_s = eloop_ctx;
6013
6014 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6015 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6016}
6017
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006018int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6019 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006020{
6021 struct wpa_supplicant *iface = NULL;
6022 struct p2p_data *p2p = wpa_s->global->p2p;
6023
6024 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006025 if ((iface->current_ssid) &&
6026 (iface->current_ssid->frequency != freq) &&
6027 ((iface->p2p_group_interface) ||
6028 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006029
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006030 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
6031 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
6032 /* Try to see whether we can move the GO. If it
6033 * is not possible, remove the GO interface
6034 */
6035 if (wpa_drv_switch_channel(iface, freq) == 0) {
6036 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
6037 iface->current_ssid->frequency = freq;
6038 continue;
6039 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006040 }
6041
6042 /* If GO cannot be moved or if the conflicting interface is a
6043 * P2P Client, remove the interface depending up on the connection
6044 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006045 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07006046 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006047 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006048 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006049 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006050 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
6051 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006052 /* If connection in progress is p2p connection, do not proceed for the connection */
6053 if (wpa_s == iface)
6054 return -1;
6055 else
6056 /* If connection in progress is STA connection, proceed for the connection */
6057 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006058 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006059 /* P2p connection has priority, disable the STA network*/
6060 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
6061 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6062 " id=%d", ssid->id);
6063 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6064 if (wpa_s == iface) {
6065 /* p2p connection is in progress, continue connecting...*/
6066 return 0;
6067 }
6068 else {
6069 /* STA connection is in progress, do not allow to continue */
6070 return -1;
6071 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006072 }
6073 }
6074 }
6075 return 0;
6076}
6077#endif