blob: 4b505b51b8449f800062680acc19a16faa2fa40a [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "common/ieee802_11_common.h"
14#include "common/ieee802_11_defs.h"
15#include "common/wpa_ctrl.h"
16#include "wps/wps_i.h"
17#include "p2p/p2p.h"
18#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080019#include "ap/ap_config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070021#include "eapol_supp/eapol_supp_sm.h"
22#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "wpa_supplicant_i.h"
24#include "driver_i.h"
25#include "ap.h"
26#include "config_ssid.h"
27#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "notify.h"
29#include "scan.h"
30#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "wps_supplicant.h"
33#include "p2p_supplicant.h"
34
35
36/*
37 * How many times to try to scan to find the GO before giving up on join
38 * request.
39 */
40#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
41
Dmitry Shmidt04949592012-07-19 12:16:46 -070042#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080044#ifndef P2P_MAX_CLIENT_IDLE
45/*
46 * How many seconds to try to reconnect to the GO when connection in P2P client
47 * role has been lost.
48 */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070049#ifdef ANDROID_P2P
50#define P2P_MAX_CLIENT_IDLE 20
51#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080052#define P2P_MAX_CLIENT_IDLE 10
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070053#endif /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054#endif /* P2P_MAX_CLIENT_IDLE */
55
Dmitry Shmidt04949592012-07-19 12:16:46 -070056#ifndef P2P_MAX_INITIAL_CONN_WAIT
57/*
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
60 */
61#define P2P_MAX_INITIAL_CONN_WAIT 10
62#endif /* P2P_MAX_INITIAL_CONN_WAIT */
63
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070064#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070065#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066#endif /* P2P_CONCURRENT_SEARCH_DELAY */
67
68enum p2p_group_removal_reason {
69 P2P_GROUP_REMOVAL_UNKNOWN,
70 P2P_GROUP_REMOVAL_SILENT,
71 P2P_GROUP_REMOVAL_FORMATION_FAILED,
72 P2P_GROUP_REMOVAL_REQUESTED,
73 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
74 P2P_GROUP_REMOVAL_UNAVAILABLE,
75 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
76#ifdef ANDROID_P2P
77 P2P_GROUP_REMOVAL_FREQ_CONFLICT
78#endif
79};
80
Jouni Malinendc7b7132012-09-14 12:53:47 -070081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
83static struct wpa_supplicant *
84wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
85 int go);
86static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070087static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
89static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070090 const u8 *dev_addr, enum p2p_wps_method wps_method,
91 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
93static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
94static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
95static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080096static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
97 void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -070098static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
99 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800100static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101
102
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700103static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
104 int freq)
105{
106 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
107 return;
108 if (freq > 0 &&
109 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
110 wpa_s->parent->conf->p2p_ignore_shared_freq)
111 freq = 0;
112 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
113}
114
115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
117 struct wpa_scan_results *scan_res)
118{
119 size_t i;
120
121 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
122 return;
123
124 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
125 (int) scan_res->num);
126
127 for (i = 0; i < scan_res->num; i++) {
128 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800129 struct os_time time_tmp_age, entry_ts;
130 time_tmp_age.sec = bss->age / 1000;
131 time_tmp_age.usec = (bss->age % 1000) * 1000;
132 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800134 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135 (const u8 *) (bss + 1),
136 bss->ie_len) > 0)
137 break;
138 }
139
140 p2p_scan_res_handled(wpa_s->global->p2p);
141}
142
143
144static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
145 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700146 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700147{
148 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700149 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150 struct wpa_driver_scan_params params;
151 int ret;
152 struct wpabuf *wps_ie, *ies;
153 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800154 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155
156 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
157 return -1;
158
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700159 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
160 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700161 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700162 wpas_p2p_in_progress(wpa_s) == 2) {
163 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
164 "pending station mode scan to be "
165 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700166 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700167 wpa_supplicant_req_scan(ifs, 0, 0);
168 return 1;
169 }
170 }
171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172 os_memset(&params, 0, sizeof(params));
173
174 /* P2P Wildcard SSID */
175 params.num_ssids = 1;
176 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
177 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
178
179 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700180 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
181 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 num_req_dev_types, req_dev_types);
183 if (wps_ie == NULL)
184 return -1;
185
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800186 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
187 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188 if (ies == NULL) {
189 wpabuf_free(wps_ie);
190 return -1;
191 }
192 wpabuf_put_buf(ies, wps_ie);
193 wpabuf_free(wps_ie);
194
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800195 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800197 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 params.extra_ies = wpabuf_head(ies);
199 params.extra_ies_len = wpabuf_len(ies);
200
201 switch (type) {
202 case P2P_SCAN_SOCIAL:
203 params.freqs = social_channels;
204 break;
205 case P2P_SCAN_FULL:
206 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700207 case P2P_SCAN_SPECIFIC:
208 social_channels[0] = freq;
209 social_channels[1] = 0;
210 params.freqs = social_channels;
211 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 case P2P_SCAN_SOCIAL_PLUS_ONE:
213 social_channels[3] = freq;
214 params.freqs = social_channels;
215 break;
216 }
217
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800218 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219
220 wpabuf_free(ies);
221
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800222 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700223 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
224 if (ifs->scanning ||
225 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
226 wpa_s->global->p2p_cb_on_scan_complete = 1;
227 ret = 1;
228 break;
229 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700231 } else {
232 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700233 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700234 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 return ret;
237}
238
239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
241{
242 switch (p2p_group_interface) {
243 case P2P_GROUP_INTERFACE_PENDING:
244 return WPA_IF_P2P_GROUP;
245 case P2P_GROUP_INTERFACE_GO:
246 return WPA_IF_P2P_GO;
247 case P2P_GROUP_INTERFACE_CLIENT:
248 return WPA_IF_P2P_CLIENT;
249 }
250
251 return WPA_IF_P2P_GROUP;
252}
253
254
255static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
256 const u8 *ssid,
257 size_t ssid_len, int *go)
258{
259 struct wpa_ssid *s;
260
261 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
262 for (s = wpa_s->conf->ssid; s; s = s->next) {
263 if (s->disabled != 0 || !s->p2p_group ||
264 s->ssid_len != ssid_len ||
265 os_memcmp(ssid, s->ssid, ssid_len) != 0)
266 continue;
267 if (s->mode == WPAS_MODE_P2P_GO &&
268 s != wpa_s->current_ssid)
269 continue;
270 if (go)
271 *go = s->mode == WPAS_MODE_P2P_GO;
272 return wpa_s;
273 }
274 }
275
276 return NULL;
277}
278
279
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700280static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
281 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282{
283 struct wpa_ssid *ssid;
284 char *gtype;
285 const char *reason;
286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 ssid = wpa_s->current_ssid;
288 if (ssid == NULL) {
289 /*
290 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700291 * pending P2P group interface waiting for provisioning or a
292 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 */
294 ssid = wpa_s->conf->ssid;
295 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700296 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 break;
298 ssid = ssid->next;
299 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300300 if (ssid == NULL &&
301 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
302 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700303 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
304 "not found");
305 return -1;
306 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 }
308 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
309 gtype = "GO";
310 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
311 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
312 wpa_s->reassociate = 0;
313 wpa_s->disconnected = 1;
314 wpa_supplicant_deauthenticate(wpa_s,
315 WLAN_REASON_DEAUTH_LEAVING);
316 gtype = "client";
317 } else
318 gtype = "GO";
319 if (wpa_s->cross_connect_in_use) {
320 wpa_s->cross_connect_in_use = 0;
321 wpa_msg(wpa_s->parent, MSG_INFO,
322 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
323 wpa_s->ifname, wpa_s->cross_connect_uplink);
324 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700325 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 case P2P_GROUP_REMOVAL_REQUESTED:
327 reason = " reason=REQUESTED";
328 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700329 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
330 reason = " reason=FORMATION_FAILED";
331 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
333 reason = " reason=IDLE";
334 break;
335 case P2P_GROUP_REMOVAL_UNAVAILABLE:
336 reason = " reason=UNAVAILABLE";
337 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700338 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
339 reason = " reason=GO_ENDING_SESSION";
340 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700341#ifdef ANDROID_P2P
342 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
343 reason = " reason=FREQ_CONFLICT";
344 break;
345#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346 default:
347 reason = "";
348 break;
349 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700350 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700351 wpa_msg(wpa_s->parent, MSG_INFO,
352 P2P_EVENT_GROUP_REMOVED "%s %s%s",
353 wpa_s->ifname, gtype, reason);
354 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355
Dmitry Shmidt04949592012-07-19 12:16:46 -0700356 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
357 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800358 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700359 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800360 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
361 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700362 wpa_s->p2p_in_provisioning = 0;
363 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700364
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700365 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
367
368 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
369 struct wpa_global *global;
370 char *ifname;
371 enum wpa_driver_if_type type;
372 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
373 wpa_s->ifname);
374 global = wpa_s->global;
375 ifname = os_strdup(wpa_s->ifname);
376 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700377 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378 wpa_s = global->ifaces;
379 if (wpa_s && ifname)
380 wpa_drv_if_remove(wpa_s, type, ifname);
381 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300382 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 }
384
385 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
386 if (ssid && (ssid->p2p_group ||
387 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
388 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
389 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700390 if (ssid == wpa_s->current_ssid) {
391 wpa_sm_set_config(wpa_s->wpa, NULL);
392 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700394 }
395 /*
396 * Networks objects created during any P2P activities are not
397 * exposed out as they might/will confuse certain non-P2P aware
398 * applications since these network objects won't behave like
399 * regular ones.
400 *
401 * Likewise, we don't send out network removed signals for such
402 * network objects.
403 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 wpa_config_remove_network(wpa_s->conf, id);
405 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800406 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700407 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 } else {
409 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
410 "found");
411 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700412 if (wpa_s->ap_iface)
413 wpa_supplicant_ap_deinit(wpa_s);
414 else
415 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700416
417 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418}
419
420
421static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
422 u8 *go_dev_addr,
423 const u8 *ssid, size_t ssid_len)
424{
425 struct wpa_bss *bss;
426 const u8 *bssid;
427 struct wpabuf *p2p;
428 u8 group_capab;
429 const u8 *addr;
430
431 if (wpa_s->go_params)
432 bssid = wpa_s->go_params->peer_interface_addr;
433 else
434 bssid = wpa_s->bssid;
435
436 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
437 if (bss == NULL) {
438 u8 iface_addr[ETH_ALEN];
439 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
440 iface_addr) == 0)
441 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
442 }
443 if (bss == NULL) {
444 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
445 "group is persistent - BSS " MACSTR " not found",
446 MAC2STR(bssid));
447 return 0;
448 }
449
450 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
451 if (p2p == NULL) {
452 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
453 "group is persistent - BSS " MACSTR
454 " did not include P2P IE", MAC2STR(bssid));
455 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
456 (u8 *) (bss + 1), bss->ie_len);
457 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
458 ((u8 *) bss + 1) + bss->ie_len,
459 bss->beacon_ie_len);
460 return 0;
461 }
462
463 group_capab = p2p_get_group_capab(p2p);
464 addr = p2p_get_go_dev_addr(p2p);
465 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
466 "group_capab=0x%x", group_capab);
467 if (addr) {
468 os_memcpy(go_dev_addr, addr, ETH_ALEN);
469 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
470 MAC2STR(addr));
471 } else
472 os_memset(go_dev_addr, 0, ETH_ALEN);
473 wpabuf_free(p2p);
474
475 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
476 "go_dev_addr=" MACSTR,
477 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
478
479 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
480}
481
482
Jouni Malinen75ecf522011-06-27 15:19:46 -0700483static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
484 struct wpa_ssid *ssid,
485 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486{
487 struct wpa_ssid *s;
488 int changed = 0;
489
490 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
491 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
492 for (s = wpa_s->conf->ssid; s; s = s->next) {
493 if (s->disabled == 2 &&
494 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
495 s->ssid_len == ssid->ssid_len &&
496 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
497 break;
498 }
499
500 if (s) {
501 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
502 "entry");
503 if (ssid->passphrase && !s->passphrase)
504 changed = 1;
505 else if (ssid->passphrase && s->passphrase &&
506 os_strcmp(ssid->passphrase, s->passphrase) != 0)
507 changed = 1;
508 } else {
509 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
510 "entry");
511 changed = 1;
512 s = wpa_config_add_network(wpa_s->conf);
513 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700514 return -1;
515
516 /*
517 * Instead of network_added we emit persistent_group_added
518 * notification. Also to keep the defense checks in
519 * persistent_group obj registration method, we set the
520 * relevant flags in s to designate it as a persistent group.
521 */
522 s->p2p_group = 1;
523 s->p2p_persistent_group = 1;
524 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 wpa_config_set_network_defaults(s);
526 }
527
528 s->p2p_group = 1;
529 s->p2p_persistent_group = 1;
530 s->disabled = 2;
531 s->bssid_set = 1;
532 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
533 s->mode = ssid->mode;
534 s->auth_alg = WPA_AUTH_ALG_OPEN;
535 s->key_mgmt = WPA_KEY_MGMT_PSK;
536 s->proto = WPA_PROTO_RSN;
537 s->pairwise_cipher = WPA_CIPHER_CCMP;
538 s->export_keys = 1;
539 if (ssid->passphrase) {
540 os_free(s->passphrase);
541 s->passphrase = os_strdup(ssid->passphrase);
542 }
543 if (ssid->psk_set) {
544 s->psk_set = 1;
545 os_memcpy(s->psk, ssid->psk, 32);
546 }
547 if (s->passphrase && !s->psk_set)
548 wpa_config_update_psk(s);
549 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
550 os_free(s->ssid);
551 s->ssid = os_malloc(ssid->ssid_len);
552 }
553 if (s->ssid) {
554 s->ssid_len = ssid->ssid_len;
555 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
556 }
557
558#ifndef CONFIG_NO_CONFIG_WRITE
559 if (changed && wpa_s->conf->update_config &&
560 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
561 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
562 }
563#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700564
565 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566}
567
568
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800569static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
570 const u8 *addr)
571{
572 struct wpa_ssid *ssid, *s;
573 u8 *n;
574 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700575 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800576
577 ssid = wpa_s->current_ssid;
578 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
579 !ssid->p2p_persistent_group)
580 return;
581
582 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
583 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
584 continue;
585
586 if (s->ssid_len == ssid->ssid_len &&
587 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
588 break;
589 }
590
591 if (s == NULL)
592 return;
593
594 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
595 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700596 ETH_ALEN) != 0)
597 continue;
598
599 if (i == s->num_p2p_clients - 1)
600 return; /* already the most recent entry */
601
602 /* move the entry to mark it most recent */
603 os_memmove(s->p2p_client_list + i * ETH_ALEN,
604 s->p2p_client_list + (i + 1) * ETH_ALEN,
605 (s->num_p2p_clients - i - 1) * ETH_ALEN);
606 os_memcpy(s->p2p_client_list +
607 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
608 found = 1;
609 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800610 }
611
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700612 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
613 n = os_realloc_array(s->p2p_client_list,
614 s->num_p2p_clients + 1, ETH_ALEN);
615 if (n == NULL)
616 return;
617 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
618 s->p2p_client_list = n;
619 s->num_p2p_clients++;
620 } else if (!found) {
621 /* Not enough room for an additional entry - drop the oldest
622 * entry */
623 os_memmove(s->p2p_client_list,
624 s->p2p_client_list + ETH_ALEN,
625 (s->num_p2p_clients - 1) * ETH_ALEN);
626 os_memcpy(s->p2p_client_list +
627 (s->num_p2p_clients - 1) * ETH_ALEN,
628 addr, ETH_ALEN);
629 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800630
631#ifndef CONFIG_NO_CONFIG_WRITE
632 if (wpa_s->parent->conf->update_config &&
633 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
634 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
635#endif /* CONFIG_NO_CONFIG_WRITE */
636}
637
638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700639static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
640 int success)
641{
642 struct wpa_ssid *ssid;
643 const char *ssid_txt;
644 int client;
645 int persistent;
646 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700647 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648
649 /*
650 * This callback is likely called for the main interface. Update wpa_s
651 * to use the group interface if a new interface was created for the
652 * group.
653 */
654 if (wpa_s->global->p2p_group_formation)
655 wpa_s = wpa_s->global->p2p_group_formation;
656 wpa_s->global->p2p_group_formation = NULL;
657 wpa_s->p2p_in_provisioning = 0;
658
659 if (!success) {
660 wpa_msg(wpa_s->parent, MSG_INFO,
661 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700662 wpas_p2p_group_delete(wpa_s,
663 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664 return;
665 }
666
667 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
668
669 ssid = wpa_s->current_ssid;
670 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
671 ssid->mode = WPAS_MODE_P2P_GO;
672 p2p_group_notif_formation_done(wpa_s->p2p_group);
673 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
674 }
675
676 persistent = 0;
677 if (ssid) {
678 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
679 client = ssid->mode == WPAS_MODE_INFRA;
680 if (ssid->mode == WPAS_MODE_P2P_GO) {
681 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700682 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
683 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 } else
685 persistent = wpas_p2p_persistent_group(wpa_s,
686 go_dev_addr,
687 ssid->ssid,
688 ssid->ssid_len);
689 } else {
690 ssid_txt = "";
691 client = wpa_s->p2p_group_interface ==
692 P2P_GROUP_INTERFACE_CLIENT;
693 os_memset(go_dev_addr, 0, ETH_ALEN);
694 }
695
696 wpa_s->show_group_started = 0;
697 if (client) {
698 /*
699 * Indicate event only after successfully completed 4-way
700 * handshake, i.e., when the interface is ready for data
701 * packets.
702 */
703 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700704#ifdef ANDROID_P2P
705 /* For client Second phase of Group formation (4-way handshake) can be still pending
706 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700707 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 -0700708 wpa_s->global->p2p_group_formation = wpa_s;
709#endif
710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
712 char psk[65];
713 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
714 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
715 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
716 "%s",
717 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
718 MAC2STR(go_dev_addr),
719 persistent ? " [PERSISTENT]" : "");
720 wpas_p2p_cross_connect_setup(wpa_s);
721 wpas_p2p_set_group_idle_timeout(wpa_s);
722 } else {
723 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
724 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
725 "go_dev_addr=" MACSTR "%s",
726 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
727 ssid && ssid->passphrase ? ssid->passphrase : "",
728 MAC2STR(go_dev_addr),
729 persistent ? " [PERSISTENT]" : "");
730 wpas_p2p_cross_connect_setup(wpa_s);
731 wpas_p2p_set_group_idle_timeout(wpa_s);
732 }
733
734 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700735 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
736 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800737 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700738 network_id = ssid->id;
739 if (!client)
740 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741}
742
743
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800744static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
745 unsigned int freq,
746 const u8 *dst, const u8 *src,
747 const u8 *bssid,
748 const u8 *data, size_t data_len,
749 enum offchannel_send_action_result
750 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800752 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
755 return;
756 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
757 return;
758
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800759 switch (result) {
760 case OFFCHANNEL_SEND_ACTION_SUCCESS:
761 res = P2P_SEND_ACTION_SUCCESS;
762 break;
763 case OFFCHANNEL_SEND_ACTION_NO_ACK:
764 res = P2P_SEND_ACTION_NO_ACK;
765 break;
766 case OFFCHANNEL_SEND_ACTION_FAILED:
767 res = P2P_SEND_ACTION_FAILED;
768 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769 }
770
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800771 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800773 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
774 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800775 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800776 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
777 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800779 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
780 "during p2p_connect-auto");
781 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
782 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783 }
784}
785
786
787static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
788 const u8 *src, const u8 *bssid, const u8 *buf,
789 size_t len, unsigned int wait_time)
790{
791 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
793 wait_time,
794 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795}
796
797
798static void wpas_send_action_done(void *ctx)
799{
800 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800801 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802}
803
804
805static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
806 struct p2p_go_neg_results *params)
807{
808 if (wpa_s->go_params == NULL) {
809 wpa_s->go_params = os_malloc(sizeof(*params));
810 if (wpa_s->go_params == NULL)
811 return -1;
812 }
813 os_memcpy(wpa_s->go_params, params, sizeof(*params));
814 return 0;
815}
816
817
818static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
819 struct p2p_go_neg_results *res)
820{
821 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
822 MAC2STR(res->peer_interface_addr));
823 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
824 res->ssid, res->ssid_len);
825 wpa_supplicant_ap_deinit(wpa_s);
826 wpas_copy_go_neg_results(wpa_s, res);
827 if (res->wps_method == WPS_PBC)
828 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
829 else {
830 u16 dev_pw_id = DEV_PW_DEFAULT;
831 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
832 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
833 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
834 wpa_s->p2p_pin, 1, dev_pw_id);
835 }
836}
837
838
839static void p2p_go_configured(void *ctx, void *data)
840{
841 struct wpa_supplicant *wpa_s = ctx;
842 struct p2p_go_neg_results *params = data;
843 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700844 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700845
846 ssid = wpa_s->current_ssid;
847 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
848 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
849 if (wpa_s->global->p2p_group_formation == wpa_s)
850 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800851 if (os_strlen(params->passphrase) > 0) {
852 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
853 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
854 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
855 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
856 ssid->frequency, params->passphrase,
857 MAC2STR(wpa_s->global->p2p_dev_addr),
858 params->persistent_group ? " [PERSISTENT]" :
859 "");
860 } else {
861 char psk[65];
862 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
863 sizeof(params->psk));
864 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
865 "%s GO ssid=\"%s\" freq=%d psk=%s "
866 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
867 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
868 ssid->frequency, psk,
869 MAC2STR(wpa_s->global->p2p_dev_addr),
870 params->persistent_group ? " [PERSISTENT]" :
871 "");
872 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700875 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700877 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700878 if (network_id < 0)
879 network_id = ssid->id;
880 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 wpas_p2p_cross_connect_setup(wpa_s);
882 wpas_p2p_set_group_idle_timeout(wpa_s);
883 return;
884 }
885
886 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
887 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
888 params->peer_interface_addr)) {
889 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
890 "filtering");
891 return;
892 }
893 if (params->wps_method == WPS_PBC)
894 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800895 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 else if (wpa_s->p2p_pin[0])
897 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800898 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 os_free(wpa_s->go_params);
900 wpa_s->go_params = NULL;
901}
902
903
904static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
905 struct p2p_go_neg_results *params,
906 int group_formation)
907{
908 struct wpa_ssid *ssid;
909
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700910 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
911 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
912 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
913 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700915 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916
917 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700918 if (ssid == NULL) {
919 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800923 wpa_s->show_group_started = 0;
924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 wpa_config_set_network_defaults(ssid);
926 ssid->temporary = 1;
927 ssid->p2p_group = 1;
928 ssid->p2p_persistent_group = params->persistent_group;
929 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
930 WPAS_MODE_P2P_GO;
931 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700932 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 ssid->ssid = os_zalloc(params->ssid_len + 1);
934 if (ssid->ssid) {
935 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
936 ssid->ssid_len = params->ssid_len;
937 }
938 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
939 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
940 ssid->proto = WPA_PROTO_RSN;
941 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800942 if (os_strlen(params->passphrase) > 0) {
943 ssid->passphrase = os_strdup(params->passphrase);
944 if (ssid->passphrase == NULL) {
945 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
946 "passphrase for GO");
947 wpa_config_remove_network(wpa_s->conf, ssid->id);
948 return;
949 }
950 } else
951 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800952 ssid->psk_set = params->psk_set;
953 if (ssid->psk_set)
954 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800955 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800956 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700957 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958
959 wpa_s->ap_configured_cb = p2p_go_configured;
960 wpa_s->ap_configured_cb_ctx = wpa_s;
961 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700962 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 wpa_s->reassociate = 1;
964 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700965 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
966 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 wpa_supplicant_req_scan(wpa_s, 0, 0);
968}
969
970
971static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
972 const struct wpa_supplicant *src)
973{
974 struct wpa_config *d;
975 const struct wpa_config *s;
976
977 d = dst->conf;
978 s = src->conf;
979
980#define C(n) if (s->n) d->n = os_strdup(s->n)
981 C(device_name);
982 C(manufacturer);
983 C(model_name);
984 C(model_number);
985 C(serial_number);
986 C(config_methods);
987#undef C
988
989 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
990 os_memcpy(d->sec_device_type, s->sec_device_type,
991 sizeof(d->sec_device_type));
992 d->num_sec_device_types = s->num_sec_device_types;
993
994 d->p2p_group_idle = s->p2p_group_idle;
995 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800996 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700997 d->max_num_sta = s->max_num_sta;
998 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700999 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000}
1001
1002
1003static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1004 enum wpa_driver_if_type type)
1005{
1006 char ifname[120], force_ifname[120];
1007
1008 if (wpa_s->pending_interface_name[0]) {
1009 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1010 "- skip creation of a new one");
1011 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1012 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1013 "unknown?! ifname='%s'",
1014 wpa_s->pending_interface_name);
1015 return -1;
1016 }
1017 return 0;
1018 }
1019
1020 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
1021 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001022 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1024 /* Try to avoid going over the IFNAMSIZ length limit */
1025 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1026 wpa_s->p2p_group_idx);
1027 }
1028 force_ifname[0] = '\0';
1029
1030 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1031 ifname);
1032 wpa_s->p2p_group_idx++;
1033
1034 wpa_s->pending_interface_type = type;
1035 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1036 wpa_s->pending_interface_addr, NULL) < 0) {
1037 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1038 "interface");
1039 return -1;
1040 }
1041
1042 if (force_ifname[0]) {
1043 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1044 force_ifname);
1045 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1046 sizeof(wpa_s->pending_interface_name));
1047 } else
1048 os_strlcpy(wpa_s->pending_interface_name, ifname,
1049 sizeof(wpa_s->pending_interface_name));
1050 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1051 MACSTR, wpa_s->pending_interface_name,
1052 MAC2STR(wpa_s->pending_interface_addr));
1053
1054 return 0;
1055}
1056
1057
1058static void wpas_p2p_remove_pending_group_interface(
1059 struct wpa_supplicant *wpa_s)
1060{
1061 if (!wpa_s->pending_interface_name[0] ||
1062 is_zero_ether_addr(wpa_s->pending_interface_addr))
1063 return; /* No pending virtual interface */
1064
1065 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1066 wpa_s->pending_interface_name);
1067 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1068 wpa_s->pending_interface_name);
1069 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1070 wpa_s->pending_interface_name[0] = '\0';
1071}
1072
1073
1074static struct wpa_supplicant *
1075wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1076{
1077 struct wpa_interface iface;
1078 struct wpa_supplicant *group_wpa_s;
1079
1080 if (!wpa_s->pending_interface_name[0]) {
1081 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1082 if (!wpas_p2p_create_iface(wpa_s))
1083 return NULL;
1084 /*
1085 * Something has forced us to remove the pending interface; try
1086 * to create a new one and hope for the best that we will get
1087 * the same local address.
1088 */
1089 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1090 WPA_IF_P2P_CLIENT) < 0)
1091 return NULL;
1092 }
1093
1094 os_memset(&iface, 0, sizeof(iface));
1095 iface.ifname = wpa_s->pending_interface_name;
1096 iface.driver = wpa_s->driver->name;
1097 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1098 iface.driver_param = wpa_s->conf->driver_param;
1099 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1100 if (group_wpa_s == NULL) {
1101 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1102 "wpa_supplicant interface");
1103 return NULL;
1104 }
1105 wpa_s->pending_interface_name[0] = '\0';
1106 group_wpa_s->parent = wpa_s;
1107 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1108 P2P_GROUP_INTERFACE_CLIENT;
1109 wpa_s->global->p2p_group_formation = group_wpa_s;
1110
1111 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1112
1113 return group_wpa_s;
1114}
1115
1116
1117static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1118 void *timeout_ctx)
1119{
1120 struct wpa_supplicant *wpa_s = eloop_ctx;
1121 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1122 if (wpa_s->global->p2p)
1123 p2p_group_formation_failed(wpa_s->global->p2p);
1124 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1125 wpa_drv_p2p_group_formation_failed(wpa_s);
1126 wpas_group_formation_completed(wpa_s, 0);
1127}
1128
1129
1130void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1131{
1132 struct wpa_supplicant *wpa_s = ctx;
1133
1134 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1135 wpa_drv_cancel_remain_on_channel(wpa_s);
1136 wpa_s->off_channel_freq = 0;
1137 wpa_s->roc_waiting_drv_freq = 0;
1138 }
1139
1140 if (res->status) {
1141 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1142 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001143 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 wpas_p2p_remove_pending_group_interface(wpa_s);
1145 return;
1146 }
1147
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001148 if (wpa_s->p2p_go_ht40)
1149 res->ht40 = 1;
1150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001152 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153
Dmitry Shmidt04949592012-07-19 12:16:46 -07001154 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1155 struct wpa_ssid *ssid;
1156 ssid = wpa_config_get_network(wpa_s->conf,
1157 wpa_s->p2p_persistent_id);
1158 if (ssid && ssid->disabled == 2 &&
1159 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1160 size_t len = os_strlen(ssid->passphrase);
1161 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1162 "on requested persistent group");
1163 os_memcpy(res->passphrase, ssid->passphrase, len);
1164 res->passphrase[len] = '\0';
1165 }
1166 }
1167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 if (wpa_s->create_p2p_iface) {
1169 struct wpa_supplicant *group_wpa_s =
1170 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1171 if (group_wpa_s == NULL) {
1172 wpas_p2p_remove_pending_group_interface(wpa_s);
1173 return;
1174 }
1175 if (group_wpa_s != wpa_s) {
1176 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1177 sizeof(group_wpa_s->p2p_pin));
1178 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1179 }
1180 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1181 wpa_s->pending_interface_name[0] = '\0';
1182 group_wpa_s->p2p_in_provisioning = 1;
1183
1184 if (res->role_go)
1185 wpas_start_wps_go(group_wpa_s, res, 1);
1186 else
1187 wpas_start_wps_enrollee(group_wpa_s, res);
1188 } else {
1189 wpa_s->p2p_in_provisioning = 1;
1190 wpa_s->global->p2p_group_formation = wpa_s;
1191
1192 if (res->role_go)
1193 wpas_start_wps_go(wpa_s, res, 1);
1194 else
1195 wpas_start_wps_enrollee(ctx, res);
1196 }
1197
1198 wpa_s->p2p_long_listen = 0;
1199 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1200
1201 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1202 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1203 (res->peer_config_timeout % 100) * 10000,
1204 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1205}
1206
1207
1208void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1209{
1210 struct wpa_supplicant *wpa_s = ctx;
1211 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1212 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1213
1214 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1215}
1216
1217
1218void wpas_dev_found(void *ctx, const u8 *addr,
1219 const struct p2p_peer_info *info,
1220 int new_device)
1221{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001222#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 struct wpa_supplicant *wpa_s = ctx;
1224 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001225#define WFD_DEV_INFO_SIZE 9
1226 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001227 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001228#ifdef CONFIG_WIFI_DISPLAY
1229 if (info->wfd_subelems) {
1230 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1231 wpabuf_head(info->wfd_subelems),
1232 WFD_DEV_INFO_SIZE);
1233 }
1234#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1236 " p2p_dev_addr=" MACSTR
1237 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001238 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1240 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1241 sizeof(devtype)),
1242 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001243 info->dev_capab, info->group_capab,
1244 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246
1247 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1248}
1249
1250
1251static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1252{
1253 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001255 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1256 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1259}
1260
1261
1262static int wpas_start_listen(void *ctx, unsigned int freq,
1263 unsigned int duration,
1264 const struct wpabuf *probe_resp_ie)
1265{
1266 struct wpa_supplicant *wpa_s = ctx;
1267
1268 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1269
1270 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1271 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1272 "report received Probe Request frames");
1273 return -1;
1274 }
1275
1276 wpa_s->pending_listen_freq = freq;
1277 wpa_s->pending_listen_duration = duration;
1278
1279 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1280 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1281 "to remain on channel (%u MHz) for Listen "
1282 "state", freq);
1283 wpa_s->pending_listen_freq = 0;
1284 return -1;
1285 }
1286 wpa_s->off_channel_freq = 0;
1287 wpa_s->roc_waiting_drv_freq = freq;
1288
1289 return 0;
1290}
1291
1292
1293static void wpas_stop_listen(void *ctx)
1294{
1295 struct wpa_supplicant *wpa_s = ctx;
1296 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1297 wpa_drv_cancel_remain_on_channel(wpa_s);
1298 wpa_s->off_channel_freq = 0;
1299 wpa_s->roc_waiting_drv_freq = 0;
1300 }
1301 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1302 wpa_drv_probe_req_report(wpa_s, 0);
1303}
1304
1305
1306static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1307{
1308 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001309 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310}
1311
1312
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001313/*
1314 * DNS Header section is used only to calculate compression pointers, so the
1315 * contents of this data does not matter, but the length needs to be reserved
1316 * in the virtual packet.
1317 */
1318#define DNS_HEADER_LEN 12
1319
1320/*
1321 * 27-octet in-memory packet from P2P specification containing two implied
1322 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1323 */
1324#define P2P_SD_IN_MEMORY_LEN 27
1325
1326static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1327 u8 **spos, const u8 *end)
1328{
1329 while (*spos < end) {
1330 u8 val = ((*spos)[0] & 0xc0) >> 6;
1331 int len;
1332
1333 if (val == 1 || val == 2) {
1334 /* These are reserved values in RFC 1035 */
1335 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1336 "sequence starting with 0x%x", val);
1337 return -1;
1338 }
1339
1340 if (val == 3) {
1341 u16 offset;
1342 u8 *spos_tmp;
1343
1344 /* Offset */
1345 if (*spos + 2 > end) {
1346 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1347 "DNS offset field");
1348 return -1;
1349 }
1350
1351 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1352 if (offset >= *spos - start) {
1353 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1354 "pointer offset %u", offset);
1355 return -1;
1356 }
1357
1358 (*spos) += 2;
1359 spos_tmp = start + offset;
1360 return p2p_sd_dns_uncompress_label(upos, uend, start,
1361 &spos_tmp,
1362 *spos - 2);
1363 }
1364
1365 /* Label */
1366 len = (*spos)[0] & 0x3f;
1367 if (len == 0)
1368 return 0;
1369
1370 (*spos)++;
1371 if (*spos + len > end) {
1372 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1373 "sequence - no room for label with length "
1374 "%u", len);
1375 return -1;
1376 }
1377
1378 if (*upos + len + 2 > uend)
1379 return -2;
1380
1381 os_memcpy(*upos, *spos, len);
1382 *spos += len;
1383 *upos += len;
1384 (*upos)[0] = '.';
1385 (*upos)++;
1386 (*upos)[0] = '\0';
1387 }
1388
1389 return 0;
1390}
1391
1392
1393/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1394 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1395 * not large enough */
1396static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1397 size_t msg_len, size_t offset)
1398{
1399 /* 27-octet in-memory packet from P2P specification */
1400 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1401 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1402 u8 *tmp, *end, *spos;
1403 char *upos, *uend;
1404 int ret = 0;
1405
1406 if (buf_len < 2)
1407 return -1;
1408 if (offset > msg_len)
1409 return -1;
1410
1411 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1412 if (tmp == NULL)
1413 return -1;
1414 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1415 end = spos + msg_len;
1416 spos += offset;
1417
1418 os_memset(tmp, 0, DNS_HEADER_LEN);
1419 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1420 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1421
1422 upos = buf;
1423 uend = buf + buf_len;
1424
1425 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1426 if (ret) {
1427 os_free(tmp);
1428 return ret;
1429 }
1430
1431 if (upos == buf) {
1432 upos[0] = '.';
1433 upos[1] = '\0';
1434 } else if (upos[-1] == '.')
1435 upos[-1] = '\0';
1436
1437 os_free(tmp);
1438 return 0;
1439}
1440
1441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442static struct p2p_srv_bonjour *
1443wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1444 const struct wpabuf *query)
1445{
1446 struct p2p_srv_bonjour *bsrv;
1447 size_t len;
1448
1449 len = wpabuf_len(query);
1450 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1451 struct p2p_srv_bonjour, list) {
1452 if (len == wpabuf_len(bsrv->query) &&
1453 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1454 len) == 0)
1455 return bsrv;
1456 }
1457 return NULL;
1458}
1459
1460
1461static struct p2p_srv_upnp *
1462wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1463 const char *service)
1464{
1465 struct p2p_srv_upnp *usrv;
1466
1467 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1468 struct p2p_srv_upnp, list) {
1469 if (version == usrv->version &&
1470 os_strcmp(service, usrv->service) == 0)
1471 return usrv;
1472 }
1473 return NULL;
1474}
1475
1476
1477static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1478 u8 srv_trans_id)
1479{
1480 u8 *len_pos;
1481
1482 if (wpabuf_tailroom(resp) < 5)
1483 return;
1484
1485 /* Length (to be filled) */
1486 len_pos = wpabuf_put(resp, 2);
1487 wpabuf_put_u8(resp, srv_proto);
1488 wpabuf_put_u8(resp, srv_trans_id);
1489 /* Status Code */
1490 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1491 /* Response Data: empty */
1492 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1493}
1494
1495
1496static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1497 struct wpabuf *resp, u8 srv_trans_id)
1498{
1499 struct p2p_srv_bonjour *bsrv;
1500 u8 *len_pos;
1501
1502 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1503
1504 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1505 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1506 return;
1507 }
1508
1509 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1510 struct p2p_srv_bonjour, list) {
1511 if (wpabuf_tailroom(resp) <
1512 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1513 return;
1514 /* Length (to be filled) */
1515 len_pos = wpabuf_put(resp, 2);
1516 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1517 wpabuf_put_u8(resp, srv_trans_id);
1518 /* Status Code */
1519 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1520 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1521 wpabuf_head(bsrv->resp),
1522 wpabuf_len(bsrv->resp));
1523 /* Response Data */
1524 wpabuf_put_buf(resp, bsrv->query); /* Key */
1525 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1526 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1527 2);
1528 }
1529}
1530
1531
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001532static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1533 size_t query_len)
1534{
1535 char str_rx[256], str_srv[256];
1536
1537 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1538 return 0; /* Too short to include DNS Type and Version */
1539 if (os_memcmp(query + query_len - 3,
1540 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1541 3) != 0)
1542 return 0; /* Mismatch in DNS Type or Version */
1543 if (query_len == wpabuf_len(bsrv->query) &&
1544 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1545 return 1; /* Binary match */
1546
1547 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1548 0))
1549 return 0; /* Failed to uncompress query */
1550 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1551 wpabuf_head(bsrv->query),
1552 wpabuf_len(bsrv->query) - 3, 0))
1553 return 0; /* Failed to uncompress service */
1554
1555 return os_strcmp(str_rx, str_srv) == 0;
1556}
1557
1558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1560 struct wpabuf *resp, u8 srv_trans_id,
1561 const u8 *query, size_t query_len)
1562{
1563 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001565 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566
1567 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1568 query, query_len);
1569 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1570 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1571 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1572 srv_trans_id);
1573 return;
1574 }
1575
1576 if (query_len == 0) {
1577 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1578 return;
1579 }
1580
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001581 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1582 struct p2p_srv_bonjour, list) {
1583 if (!match_bonjour_query(bsrv, query, query_len))
1584 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001586 if (wpabuf_tailroom(resp) <
1587 5 + query_len + wpabuf_len(bsrv->resp))
1588 return;
1589
1590 matches++;
1591
1592 /* Length (to be filled) */
1593 len_pos = wpabuf_put(resp, 2);
1594 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1595 wpabuf_put_u8(resp, srv_trans_id);
1596
1597 /* Status Code */
1598 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1599 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1600 wpabuf_head(bsrv->resp),
1601 wpabuf_len(bsrv->resp));
1602
1603 /* Response Data */
1604 wpabuf_put_data(resp, query, query_len); /* Key */
1605 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1606
1607 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1608 }
1609
1610 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1612 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001613 if (wpabuf_tailroom(resp) < 5)
1614 return;
1615
1616 /* Length (to be filled) */
1617 len_pos = wpabuf_put(resp, 2);
1618 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1619 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620
1621 /* Status Code */
1622 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1623 /* Response Data: empty */
1624 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1625 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627}
1628
1629
1630static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1631 struct wpabuf *resp, u8 srv_trans_id)
1632{
1633 struct p2p_srv_upnp *usrv;
1634 u8 *len_pos;
1635
1636 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1637
1638 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1639 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1640 return;
1641 }
1642
1643 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1644 struct p2p_srv_upnp, list) {
1645 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1646 return;
1647
1648 /* Length (to be filled) */
1649 len_pos = wpabuf_put(resp, 2);
1650 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1651 wpabuf_put_u8(resp, srv_trans_id);
1652
1653 /* Status Code */
1654 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1655 /* Response Data */
1656 wpabuf_put_u8(resp, usrv->version);
1657 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1658 usrv->service);
1659 wpabuf_put_str(resp, usrv->service);
1660 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1661 2);
1662 }
1663}
1664
1665
1666static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1667 struct wpabuf *resp, u8 srv_trans_id,
1668 const u8 *query, size_t query_len)
1669{
1670 struct p2p_srv_upnp *usrv;
1671 u8 *len_pos;
1672 u8 version;
1673 char *str;
1674 int count = 0;
1675
1676 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1677 query, query_len);
1678
1679 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1680 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1681 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1682 srv_trans_id);
1683 return;
1684 }
1685
1686 if (query_len == 0) {
1687 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1688 return;
1689 }
1690
1691 if (wpabuf_tailroom(resp) < 5)
1692 return;
1693
1694 /* Length (to be filled) */
1695 len_pos = wpabuf_put(resp, 2);
1696 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1697 wpabuf_put_u8(resp, srv_trans_id);
1698
1699 version = query[0];
1700 str = os_malloc(query_len);
1701 if (str == NULL)
1702 return;
1703 os_memcpy(str, query + 1, query_len - 1);
1704 str[query_len - 1] = '\0';
1705
1706 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1707 struct p2p_srv_upnp, list) {
1708 if (version != usrv->version)
1709 continue;
1710
1711 if (os_strcmp(str, "ssdp:all") != 0 &&
1712 os_strstr(usrv->service, str) == NULL)
1713 continue;
1714
1715 if (wpabuf_tailroom(resp) < 2)
1716 break;
1717 if (count == 0) {
1718 /* Status Code */
1719 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1720 /* Response Data */
1721 wpabuf_put_u8(resp, version);
1722 } else
1723 wpabuf_put_u8(resp, ',');
1724
1725 count++;
1726
1727 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1728 usrv->service);
1729 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1730 break;
1731 wpabuf_put_str(resp, usrv->service);
1732 }
1733 os_free(str);
1734
1735 if (count == 0) {
1736 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1737 "available");
1738 /* Status Code */
1739 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1740 /* Response Data: empty */
1741 }
1742
1743 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1744}
1745
1746
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001747#ifdef CONFIG_WIFI_DISPLAY
1748static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1749 struct wpabuf *resp, u8 srv_trans_id,
1750 const u8 *query, size_t query_len)
1751{
1752 const u8 *pos;
1753 u8 role;
1754 u8 *len_pos;
1755
1756 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1757
1758 if (!wpa_s->global->wifi_display) {
1759 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1760 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1761 srv_trans_id);
1762 return;
1763 }
1764
1765 if (query_len < 1) {
1766 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1767 "Role");
1768 return;
1769 }
1770
1771 if (wpabuf_tailroom(resp) < 5)
1772 return;
1773
1774 pos = query;
1775 role = *pos++;
1776 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1777
1778 /* TODO: role specific handling */
1779
1780 /* Length (to be filled) */
1781 len_pos = wpabuf_put(resp, 2);
1782 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1783 wpabuf_put_u8(resp, srv_trans_id);
1784 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1785
1786 while (pos < query + query_len) {
1787 if (*pos < MAX_WFD_SUBELEMS &&
1788 wpa_s->global->wfd_subelem[*pos] &&
1789 wpabuf_tailroom(resp) >=
1790 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1791 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1792 "subelement %u", *pos);
1793 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1794 }
1795 pos++;
1796 }
1797
1798 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1799}
1800#endif /* CONFIG_WIFI_DISPLAY */
1801
1802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1804 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1805{
1806 struct wpa_supplicant *wpa_s = ctx;
1807 const u8 *pos = tlvs;
1808 const u8 *end = tlvs + tlvs_len;
1809 const u8 *tlv_end;
1810 u16 slen;
1811 struct wpabuf *resp;
1812 u8 srv_proto, srv_trans_id;
1813 size_t buf_len;
1814 char *buf;
1815
1816 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1817 tlvs, tlvs_len);
1818 buf_len = 2 * tlvs_len + 1;
1819 buf = os_malloc(buf_len);
1820 if (buf) {
1821 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1822 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1823 MACSTR " %u %u %s",
1824 freq, MAC2STR(sa), dialog_token, update_indic,
1825 buf);
1826 os_free(buf);
1827 }
1828
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001829 if (wpa_s->p2p_sd_over_ctrl_iface) {
1830 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1831 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001832 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001833 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001834
1835 resp = wpabuf_alloc(10000);
1836 if (resp == NULL)
1837 return;
1838
1839 while (pos + 1 < end) {
1840 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1841 slen = WPA_GET_LE16(pos);
1842 pos += 2;
1843 if (pos + slen > end || slen < 2) {
1844 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1845 "length");
1846 wpabuf_free(resp);
1847 return;
1848 }
1849 tlv_end = pos + slen;
1850
1851 srv_proto = *pos++;
1852 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1853 srv_proto);
1854 srv_trans_id = *pos++;
1855 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1856 srv_trans_id);
1857
1858 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1859 pos, tlv_end - pos);
1860
1861
1862 if (wpa_s->force_long_sd) {
1863 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1864 "response");
1865 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1866 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1867 goto done;
1868 }
1869
1870 switch (srv_proto) {
1871 case P2P_SERV_ALL_SERVICES:
1872 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1873 "for all services");
1874 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1875 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1876 wpa_printf(MSG_DEBUG, "P2P: No service "
1877 "discovery protocols available");
1878 wpas_sd_add_proto_not_avail(
1879 resp, P2P_SERV_ALL_SERVICES,
1880 srv_trans_id);
1881 break;
1882 }
1883 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1884 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1885 break;
1886 case P2P_SERV_BONJOUR:
1887 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1888 pos, tlv_end - pos);
1889 break;
1890 case P2P_SERV_UPNP:
1891 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1892 pos, tlv_end - pos);
1893 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001894#ifdef CONFIG_WIFI_DISPLAY
1895 case P2P_SERV_WIFI_DISPLAY:
1896 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1897 pos, tlv_end - pos);
1898 break;
1899#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 default:
1901 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1902 "protocol %u", srv_proto);
1903 wpas_sd_add_proto_not_avail(resp, srv_proto,
1904 srv_trans_id);
1905 break;
1906 }
1907
1908 pos = tlv_end;
1909 }
1910
1911done:
1912 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1913 update_indic, tlvs, tlvs_len);
1914
1915 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1916
1917 wpabuf_free(resp);
1918}
1919
1920
1921void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1922 const u8 *tlvs, size_t tlvs_len)
1923{
1924 struct wpa_supplicant *wpa_s = ctx;
1925 const u8 *pos = tlvs;
1926 const u8 *end = tlvs + tlvs_len;
1927 const u8 *tlv_end;
1928 u16 slen;
1929 size_t buf_len;
1930 char *buf;
1931
1932 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1933 tlvs, tlvs_len);
1934 if (tlvs_len > 1500) {
1935 /* TODO: better way for handling this */
1936 wpa_msg_ctrl(wpa_s, MSG_INFO,
1937 P2P_EVENT_SERV_DISC_RESP MACSTR
1938 " %u <long response: %u bytes>",
1939 MAC2STR(sa), update_indic,
1940 (unsigned int) tlvs_len);
1941 } else {
1942 buf_len = 2 * tlvs_len + 1;
1943 buf = os_malloc(buf_len);
1944 if (buf) {
1945 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1946 wpa_msg_ctrl(wpa_s, MSG_INFO,
1947 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1948 MAC2STR(sa), update_indic, buf);
1949 os_free(buf);
1950 }
1951 }
1952
1953 while (pos < end) {
1954 u8 srv_proto, srv_trans_id, status;
1955
1956 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1957 slen = WPA_GET_LE16(pos);
1958 pos += 2;
1959 if (pos + slen > end || slen < 3) {
1960 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1961 "length");
1962 return;
1963 }
1964 tlv_end = pos + slen;
1965
1966 srv_proto = *pos++;
1967 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1968 srv_proto);
1969 srv_trans_id = *pos++;
1970 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1971 srv_trans_id);
1972 status = *pos++;
1973 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1974 status);
1975
1976 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1977 pos, tlv_end - pos);
1978
1979 pos = tlv_end;
1980 }
1981
1982 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1983}
1984
1985
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001986u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1987 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001988{
1989 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001990 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001992 return 0;
1993 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994}
1995
1996
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001997u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1998 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999{
2000 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002001 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002
2003 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2004 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002005 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002006 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2007 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2008 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2009 wpabuf_put_u8(tlvs, version);
2010 wpabuf_put_str(tlvs, query);
2011 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2012 wpabuf_free(tlvs);
2013 return ret;
2014}
2015
2016
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002017#ifdef CONFIG_WIFI_DISPLAY
2018
2019static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2020 const struct wpabuf *tlvs)
2021{
2022 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2023 return 0;
2024 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2025 return 0;
2026 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2027}
2028
2029
2030#define MAX_WFD_SD_SUBELEMS 20
2031
2032static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2033 const char *subelems)
2034{
2035 u8 *len;
2036 const char *pos;
2037 int val;
2038 int count = 0;
2039
2040 len = wpabuf_put(tlvs, 2);
2041 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2042 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2043
2044 wpabuf_put_u8(tlvs, role);
2045
2046 pos = subelems;
2047 while (*pos) {
2048 val = atoi(pos);
2049 if (val >= 0 && val < 256) {
2050 wpabuf_put_u8(tlvs, val);
2051 count++;
2052 if (count == MAX_WFD_SD_SUBELEMS)
2053 break;
2054 }
2055 pos = os_strchr(pos + 1, ',');
2056 if (pos == NULL)
2057 break;
2058 pos++;
2059 }
2060
2061 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2062}
2063
2064
2065u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2066 const u8 *dst, const char *role)
2067{
2068 struct wpabuf *tlvs;
2069 u64 ret;
2070 const char *subelems;
2071 u8 id = 1;
2072
2073 subelems = os_strchr(role, ' ');
2074 if (subelems == NULL)
2075 return 0;
2076 subelems++;
2077
2078 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2079 if (tlvs == NULL)
2080 return 0;
2081
2082 if (os_strstr(role, "[source]"))
2083 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2084 if (os_strstr(role, "[pri-sink]"))
2085 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2086 if (os_strstr(role, "[sec-sink]"))
2087 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2088 if (os_strstr(role, "[source+sink]"))
2089 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2090
2091 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2092 wpabuf_free(tlvs);
2093 return ret;
2094}
2095
2096#endif /* CONFIG_WIFI_DISPLAY */
2097
2098
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002099int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100{
2101 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2104 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002105 return p2p_sd_cancel_request(wpa_s->global->p2p,
2106 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107}
2108
2109
2110void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2111 const u8 *dst, u8 dialog_token,
2112 const struct wpabuf *resp_tlvs)
2113{
2114 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2115 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2116 resp_tlvs);
2117 return;
2118 }
2119 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2120 return;
2121 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2122 resp_tlvs);
2123}
2124
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002125#ifdef ANDROID_P2P
2126void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2127#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002129#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130{
2131 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2132 wpa_drv_p2p_service_update(wpa_s);
2133 return;
2134 }
2135 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002136#ifdef ANDROID_P2P
2137 p2p_sd_service_update(wpa_s->global->p2p, action);
2138#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002140#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141}
2142
2143
2144static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2145{
2146 dl_list_del(&bsrv->list);
2147 wpabuf_free(bsrv->query);
2148 wpabuf_free(bsrv->resp);
2149 os_free(bsrv);
2150}
2151
2152
2153static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2154{
2155 dl_list_del(&usrv->list);
2156 os_free(usrv->service);
2157 os_free(usrv);
2158}
2159
2160
2161void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2162{
2163 struct p2p_srv_bonjour *bsrv, *bn;
2164 struct p2p_srv_upnp *usrv, *un;
2165
2166 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2167 struct p2p_srv_bonjour, list)
2168 wpas_p2p_srv_bonjour_free(bsrv);
2169
2170 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2171 struct p2p_srv_upnp, list)
2172 wpas_p2p_srv_upnp_free(usrv);
2173
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002174#ifdef ANDROID_P2P
2175 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2176#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002178#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179}
2180
2181
2182int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2183 struct wpabuf *query, struct wpabuf *resp)
2184{
2185 struct p2p_srv_bonjour *bsrv;
2186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 bsrv = os_zalloc(sizeof(*bsrv));
2188 if (bsrv == NULL)
2189 return -1;
2190 bsrv->query = query;
2191 bsrv->resp = resp;
2192 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2193
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002194#ifdef ANDROID_P2P
2195 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2196#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002198#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 return 0;
2200}
2201
2202
2203int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2204 const struct wpabuf *query)
2205{
2206 struct p2p_srv_bonjour *bsrv;
2207
2208 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2209 if (bsrv == NULL)
2210 return -1;
2211 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002212#ifdef ANDROID_P2P
2213 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2214#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002216#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002217 return 0;
2218}
2219
2220
2221int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2222 const char *service)
2223{
2224 struct p2p_srv_upnp *usrv;
2225
2226 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2227 return 0; /* Already listed */
2228 usrv = os_zalloc(sizeof(*usrv));
2229 if (usrv == NULL)
2230 return -1;
2231 usrv->version = version;
2232 usrv->service = os_strdup(service);
2233 if (usrv->service == NULL) {
2234 os_free(usrv);
2235 return -1;
2236 }
2237 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2238
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002239#ifdef ANDROID_P2P
2240 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2241#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002243#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002244 return 0;
2245}
2246
2247
2248int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2249 const char *service)
2250{
2251 struct p2p_srv_upnp *usrv;
2252
2253 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2254 if (usrv == NULL)
2255 return -1;
2256 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002257#ifdef ANDROID_P2P
2258 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2259#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002261#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 return 0;
2263}
2264
2265
2266static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2267 const u8 *peer, const char *params,
2268 unsigned int generated_pin)
2269{
2270 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2271 MAC2STR(peer), generated_pin, params);
2272}
2273
2274
2275static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2276 const u8 *peer, const char *params)
2277{
2278 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2279 MAC2STR(peer), params);
2280}
2281
2282
2283void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2284 const u8 *dev_addr, const u8 *pri_dev_type,
2285 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002286 u8 dev_capab, u8 group_capab, const u8 *group_id,
2287 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288{
2289 struct wpa_supplicant *wpa_s = ctx;
2290 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002291 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 u8 empty_dev_type[8];
2293 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002294 struct wpa_supplicant *group = NULL;
2295
2296 if (group_id) {
2297 for (group = wpa_s->global->ifaces; group; group = group->next)
2298 {
2299 struct wpa_ssid *s = group->current_ssid;
2300 if (s != NULL &&
2301 s->mode == WPAS_MODE_P2P_GO &&
2302 group_id_len - ETH_ALEN == s->ssid_len &&
2303 os_memcmp(group_id + ETH_ALEN, s->ssid,
2304 s->ssid_len) == 0)
2305 break;
2306 }
2307 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308
2309 if (pri_dev_type == NULL) {
2310 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2311 pri_dev_type = empty_dev_type;
2312 }
2313 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2314 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002315 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316 MAC2STR(dev_addr),
2317 wps_dev_type_bin2str(pri_dev_type, devtype,
2318 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002319 dev_name, supp_config_methods, dev_capab, group_capab,
2320 group ? " group=" : "",
2321 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324 if (config_methods & WPS_CONFIG_DISPLAY) {
2325 generated_pin = wps_generate_pin();
2326 wpas_prov_disc_local_display(wpa_s, peer, params,
2327 generated_pin);
2328 } else if (config_methods & WPS_CONFIG_KEYPAD)
2329 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2330 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2331 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2332 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002333
2334 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2335 P2P_PROV_DISC_SUCCESS,
2336 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337}
2338
2339
2340void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2341{
2342 struct wpa_supplicant *wpa_s = ctx;
2343 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002344 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002346 if (wpa_s->pending_pd_before_join &&
2347 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2348 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2349 wpa_s->pending_pd_before_join = 0;
2350 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2351 "join-existing-group operation");
2352 wpas_p2p_join_start(wpa_s);
2353 return;
2354 }
2355
Dmitry Shmidt04949592012-07-19 12:16:46 -07002356 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2357 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2358 os_snprintf(params, sizeof(params), " peer_go=%d",
2359 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2360 else
2361 params[0] = '\0';
2362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002364 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365 else if (config_methods & WPS_CONFIG_KEYPAD) {
2366 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002367 wpas_prov_disc_local_display(wpa_s, peer, params,
2368 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002370 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2371 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372
Jouni Malinen75ecf522011-06-27 15:19:46 -07002373 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2374 P2P_PROV_DISC_SUCCESS,
2375 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002376}
2377
2378
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002379static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2380 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002381{
2382 struct wpa_supplicant *wpa_s = ctx;
2383
Dmitry Shmidt04949592012-07-19 12:16:46 -07002384 if (wpa_s->p2p_fallback_to_go_neg) {
2385 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2386 "failed - fall back to GO Negotiation");
2387 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2388 return;
2389 }
2390
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002391 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002392 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002393 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2394 "join-existing-group operation (no ACK for PD "
2395 "Req attempts)");
2396 wpas_p2p_join_start(wpa_s);
2397 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002398 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002399
Dmitry Shmidt04949592012-07-19 12:16:46 -07002400 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2401 " p2p_dev_addr=" MACSTR " status=%d",
2402 MAC2STR(peer), status);
2403
Jouni Malinen75ecf522011-06-27 15:19:46 -07002404 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2405 status, 0, 0);
2406}
2407
2408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2410 const u8 *go_dev_addr, const u8 *ssid,
2411 size_t ssid_len, int *go, u8 *group_bssid,
2412 int *force_freq, int persistent_group)
2413{
2414 struct wpa_supplicant *wpa_s = ctx;
2415 struct wpa_ssid *s;
2416 u8 cur_bssid[ETH_ALEN];
2417 int res;
2418 struct wpa_supplicant *grp;
2419
2420 if (!persistent_group) {
2421 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2422 " to join an active group", MAC2STR(sa));
2423 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2424 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2425 == 0 ||
2426 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2427 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2428 "authorized invitation");
2429 goto accept_inv;
2430 }
2431 /*
2432 * Do not accept the invitation automatically; notify user and
2433 * request approval.
2434 */
2435 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2436 }
2437
2438 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2439 if (grp) {
2440 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2441 "running persistent group");
2442 if (*go)
2443 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2444 goto accept_inv;
2445 }
2446
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002447 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2448 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2449 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2450 "invitation to re-invoke a persistent group");
2451 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2453
2454 for (s = wpa_s->conf->ssid; s; s = s->next) {
2455 if (s->disabled == 2 &&
2456 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2457 s->ssid_len == ssid_len &&
2458 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2459 break;
2460 }
2461
2462 if (!s) {
2463 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2464 " requested reinvocation of an unknown group",
2465 MAC2STR(sa));
2466 return P2P_SC_FAIL_UNKNOWN_GROUP;
2467 }
2468
2469 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2470 *go = 1;
2471 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2472 wpa_printf(MSG_DEBUG, "P2P: The only available "
2473 "interface is already in use - reject "
2474 "invitation");
2475 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2476 }
2477 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2478 } else if (s->mode == WPAS_MODE_P2P_GO) {
2479 *go = 1;
2480 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2481 {
2482 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2483 "interface address for the group");
2484 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2485 }
2486 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2487 ETH_ALEN);
2488 }
2489
2490accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002491 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2492
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2494 wpa_s->assoc_freq) {
2495 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2496 "the channel we are already using");
2497 *force_freq = wpa_s->assoc_freq;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002498 wpas_p2p_set_own_freq_preference(wpa_s, wpa_s->assoc_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499 }
2500
2501 res = wpa_drv_shared_freq(wpa_s);
2502 if (res > 0) {
2503 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2504 "with the channel we are already using on a "
2505 "shared interface");
2506 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002507 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508 }
2509
2510 return P2P_SC_SUCCESS;
2511}
2512
2513
2514static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2515 const u8 *ssid, size_t ssid_len,
2516 const u8 *go_dev_addr, u8 status,
2517 int op_freq)
2518{
2519 struct wpa_supplicant *wpa_s = ctx;
2520 struct wpa_ssid *s;
2521
2522 for (s = wpa_s->conf->ssid; s; s = s->next) {
2523 if (s->disabled == 2 &&
2524 s->ssid_len == ssid_len &&
2525 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2526 break;
2527 }
2528
2529 if (status == P2P_SC_SUCCESS) {
2530 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2531 " was accepted; op_freq=%d MHz",
2532 MAC2STR(sa), op_freq);
2533 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002534 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002536 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002538 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002540 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541 }
2542 return;
2543 }
2544
2545 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2546 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2547 " was rejected (status %u)", MAC2STR(sa), status);
2548 return;
2549 }
2550
2551 if (!s) {
2552 if (bssid) {
2553 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2554 "sa=" MACSTR " go_dev_addr=" MACSTR
2555 " bssid=" MACSTR " unknown-network",
2556 MAC2STR(sa), MAC2STR(go_dev_addr),
2557 MAC2STR(bssid));
2558 } else {
2559 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2560 "sa=" MACSTR " go_dev_addr=" MACSTR
2561 " unknown-network",
2562 MAC2STR(sa), MAC2STR(go_dev_addr));
2563 }
2564 return;
2565 }
2566
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002567 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2568 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2569 MACSTR " persistent=%d freq=%d",
2570 MAC2STR(sa), s->id, op_freq);
2571 } else {
2572 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2573 MACSTR " persistent=%d", MAC2STR(sa), s->id);
2574 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575}
2576
2577
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002578static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2579 struct wpa_ssid *ssid,
2580 const u8 *peer)
2581{
2582 size_t i;
2583
2584 if (ssid == NULL)
2585 return;
2586
2587 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2588 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2589 ETH_ALEN) == 0)
2590 break;
2591 }
2592 if (i >= ssid->num_p2p_clients) {
2593 if (ssid->mode != WPAS_MODE_P2P_GO &&
2594 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2595 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2596 "due to invitation result", ssid->id);
2597 wpas_notify_network_removed(wpa_s, ssid);
2598 wpa_config_remove_network(wpa_s->conf, ssid->id);
2599 return;
2600 }
2601 return; /* Peer not found in client list */
2602 }
2603
2604 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2605 "group %d client list due to invitation result",
2606 MAC2STR(peer), ssid->id);
2607 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2608 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2609 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2610 ssid->num_p2p_clients--;
2611#ifndef CONFIG_NO_CONFIG_WRITE
2612 if (wpa_s->parent->conf->update_config &&
2613 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2614 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2615#endif /* CONFIG_NO_CONFIG_WRITE */
2616}
2617
2618
2619static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2620 const u8 *peer)
2621{
2622 struct wpa_ssid *ssid;
2623
2624 wpa_s = wpa_s->global->p2p_invite_group;
2625 if (wpa_s == NULL)
2626 return; /* No known invitation group */
2627 ssid = wpa_s->current_ssid;
2628 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2629 !ssid->p2p_persistent_group)
2630 return; /* Not operating as a GO in persistent group */
2631 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2632 ssid->ssid, ssid->ssid_len);
2633 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2634}
2635
2636
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002637static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002638 const struct p2p_channels *channels,
2639 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640{
2641 struct wpa_supplicant *wpa_s = ctx;
2642 struct wpa_ssid *ssid;
2643
2644 if (bssid) {
2645 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2646 "status=%d " MACSTR,
2647 status, MAC2STR(bssid));
2648 } else {
2649 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2650 "status=%d ", status);
2651 }
2652 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2653
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002654 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2655 status, MAC2STR(peer));
2656 if (wpa_s->pending_invite_ssid_id == -1) {
2657 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2658 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002660 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002662 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2663 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2664 "invitation exchange to indicate readiness for "
2665 "re-invocation");
2666 }
2667
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002668 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002669 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2670 ssid = wpa_config_get_network(
2671 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2672 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2673 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674 wpas_p2p_remove_pending_group_interface(wpa_s);
2675 return;
2676 }
2677
2678 ssid = wpa_config_get_network(wpa_s->conf,
2679 wpa_s->pending_invite_ssid_id);
2680 if (ssid == NULL) {
2681 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2682 "data matching with invitation");
2683 return;
2684 }
2685
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002686 /*
2687 * The peer could have missed our ctrl::ack frame for Invitation
2688 * Response and continue retransmitting the frame. To reduce the
2689 * likelihood of the peer not getting successful TX status for the
2690 * Invitation Response frame, wait a short time here before starting
2691 * the persistent group so that we will remain on the current channel to
2692 * acknowledge any possible retransmission from the peer.
2693 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002694#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002695 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2696 "starting persistent group");
2697 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002698#else
2699 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2700 "starting persistent group");
2701 os_sleep(0, 100000);
2702#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002705 ssid->mode == WPAS_MODE_P2P_GO,
2706 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002707 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002708}
2709
2710
Dmitry Shmidt04949592012-07-19 12:16:46 -07002711static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2712 unsigned int freq)
2713{
2714 unsigned int i;
2715
2716 if (global->p2p_disallow_freq == NULL)
2717 return 0;
2718
2719 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2720 if (freq >= global->p2p_disallow_freq[i].min &&
2721 freq <= global->p2p_disallow_freq[i].max)
2722 return 1;
2723 }
2724
2725 return 0;
2726}
2727
2728
2729static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2730{
2731 reg->channel[reg->channels] = chan;
2732 reg->channels++;
2733}
2734
2735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002736static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2737 struct p2p_channels *chan)
2738{
2739 int i, cla = 0;
2740
2741 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2742 "band");
2743
2744 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2745 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002746 chan->reg_class[cla].channels = 0;
2747 for (i = 0; i < 11; i++) {
2748 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2749 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2750 }
2751 if (chan->reg_class[cla].channels)
2752 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002753
2754 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2755 "band");
2756
2757 /* Operating class 115 - 5 GHz, channels 36-48 */
2758 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002759 chan->reg_class[cla].channels = 0;
2760 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2761 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2762 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2763 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2764 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2765 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2766 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2767 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2768 if (chan->reg_class[cla].channels)
2769 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770
2771 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2772 "band");
2773
2774 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2775 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002776 chan->reg_class[cla].channels = 0;
2777 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2778 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2779 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2780 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2781 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2782 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2783 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2784 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2785 if (chan->reg_class[cla].channels)
2786 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002787
2788 chan->reg_classes = cla;
2789 return 0;
2790}
2791
2792
2793static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2794 u16 num_modes,
2795 enum hostapd_hw_mode mode)
2796{
2797 u16 i;
2798
2799 for (i = 0; i < num_modes; i++) {
2800 if (modes[i].mode == mode)
2801 return &modes[i];
2802 }
2803
2804 return NULL;
2805}
2806
2807
Dmitry Shmidt04949592012-07-19 12:16:46 -07002808static int has_channel(struct wpa_global *global,
2809 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810{
2811 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002812 unsigned int freq;
2813
2814 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2815 chan * 5;
2816 if (wpas_p2p_disallowed_freq(global, freq))
2817 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818
2819 for (i = 0; i < mode->num_channels; i++) {
2820 if (mode->channels[i].chan == chan) {
2821 if (flags)
2822 *flags = mode->channels[i].flag;
2823 return !(mode->channels[i].flag &
2824 (HOSTAPD_CHAN_DISABLED |
2825 HOSTAPD_CHAN_PASSIVE_SCAN |
2826 HOSTAPD_CHAN_NO_IBSS |
2827 HOSTAPD_CHAN_RADAR));
2828 }
2829 }
2830
2831 return 0;
2832}
2833
2834
2835struct p2p_oper_class_map {
2836 enum hostapd_hw_mode mode;
2837 u8 op_class;
2838 u8 min_chan;
2839 u8 max_chan;
2840 u8 inc;
2841 enum { BW20, BW40PLUS, BW40MINUS } bw;
2842};
2843
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002844static struct p2p_oper_class_map op_class[] = {
2845 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002846#if 0 /* Do not enable HT40 on 2 GHz for now */
2847 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2848 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2849#endif
2850 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2851 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2852 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2853 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2854 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2855 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2856 { -1, 0, 0, 0, 0, BW20 }
2857};
2858
2859
2860static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2861 struct hostapd_hw_modes *mode,
2862 u8 channel, u8 bw)
2863{
2864 int flag;
2865
2866 if (!has_channel(wpa_s->global, mode, channel, &flag))
2867 return -1;
2868 if (bw == BW40MINUS &&
2869 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2870 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2871 return 0;
2872 if (bw == BW40PLUS &&
2873 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2874 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2875 return 0;
2876 return 1;
2877}
2878
2879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2881 struct p2p_channels *chan)
2882{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002883 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002886 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2888 "of all supported channels; assume dualband "
2889 "support");
2890 return wpas_p2p_default_channels(wpa_s, chan);
2891 }
2892
2893 cla = 0;
2894
2895 for (op = 0; op_class[op].op_class; op++) {
2896 struct p2p_oper_class_map *o = &op_class[op];
2897 u8 ch;
2898 struct p2p_reg_class *reg = NULL;
2899
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002900 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 if (mode == NULL)
2902 continue;
2903 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002904 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905 continue;
2906 if (reg == NULL) {
2907 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2908 "class %u", o->op_class);
2909 reg = &chan->reg_class[cla];
2910 cla++;
2911 reg->reg_class = o->op_class;
2912 }
2913 reg->channel[reg->channels] = ch;
2914 reg->channels++;
2915 }
2916 if (reg) {
2917 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2918 reg->channel, reg->channels);
2919 }
2920 }
2921
2922 chan->reg_classes = cla;
2923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 return 0;
2925}
2926
2927
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002928int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2929 struct hostapd_hw_modes *mode, u8 channel)
2930{
2931 int op, ret;
2932
2933 for (op = 0; op_class[op].op_class; op++) {
2934 struct p2p_oper_class_map *o = &op_class[op];
2935 u8 ch;
2936
2937 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2938 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2939 o->bw == BW20 || ch != channel)
2940 continue;
2941 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2942 if (ret < 0)
2943 continue;
2944 else if (ret > 0)
2945 return (o->bw == BW40MINUS) ? -1 : 1;
2946 else
2947 return 0;
2948 }
2949 }
2950 return 0;
2951}
2952
2953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002954static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2955 size_t buf_len)
2956{
2957 struct wpa_supplicant *wpa_s = ctx;
2958
2959 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2960 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2961 break;
2962 }
2963 if (wpa_s == NULL)
2964 return -1;
2965
2966 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2967}
2968
2969
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002970static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2971{
2972 struct wpa_supplicant *wpa_s = ctx;
2973
2974 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2975 struct wpa_ssid *ssid = wpa_s->current_ssid;
2976 if (ssid == NULL)
2977 continue;
2978 if (ssid->mode != WPAS_MODE_INFRA)
2979 continue;
2980 if (wpa_s->wpa_state != WPA_COMPLETED &&
2981 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2982 continue;
2983 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2984 return 1;
2985 }
2986
2987 return 0;
2988}
2989
2990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991/**
2992 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2993 * @global: Pointer to global data from wpa_supplicant_init()
2994 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2995 * Returns: 0 on success, -1 on failure
2996 */
2997int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2998{
2999 struct p2p_config p2p;
3000 unsigned int r;
3001 int i;
3002
3003 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3004 return 0;
3005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 if (global->p2p)
3007 return 0;
3008
3009 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3010 struct p2p_params params;
3011
3012 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3013 os_memset(&params, 0, sizeof(params));
3014 params.dev_name = wpa_s->conf->device_name;
3015 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3016 WPS_DEV_TYPE_LEN);
3017 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3018 os_memcpy(params.sec_dev_type,
3019 wpa_s->conf->sec_device_type,
3020 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3021
3022 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3023 return -1;
3024
3025 return 0;
3026 }
3027
3028 os_memset(&p2p, 0, sizeof(p2p));
3029 p2p.msg_ctx = wpa_s;
3030 p2p.cb_ctx = wpa_s;
3031 p2p.p2p_scan = wpas_p2p_scan;
3032 p2p.send_action = wpas_send_action;
3033 p2p.send_action_done = wpas_send_action_done;
3034 p2p.go_neg_completed = wpas_go_neg_completed;
3035 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3036 p2p.dev_found = wpas_dev_found;
3037 p2p.dev_lost = wpas_dev_lost;
3038 p2p.start_listen = wpas_start_listen;
3039 p2p.stop_listen = wpas_stop_listen;
3040 p2p.send_probe_resp = wpas_send_probe_resp;
3041 p2p.sd_request = wpas_sd_request;
3042 p2p.sd_response = wpas_sd_response;
3043 p2p.prov_disc_req = wpas_prov_disc_req;
3044 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003045 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046 p2p.invitation_process = wpas_invitation_process;
3047 p2p.invitation_received = wpas_invitation_received;
3048 p2p.invitation_result = wpas_invitation_result;
3049 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003050 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051
3052 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003053 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003054 p2p.dev_name = wpa_s->conf->device_name;
3055 p2p.manufacturer = wpa_s->conf->manufacturer;
3056 p2p.model_name = wpa_s->conf->model_name;
3057 p2p.model_number = wpa_s->conf->model_number;
3058 p2p.serial_number = wpa_s->conf->serial_number;
3059 if (wpa_s->wps) {
3060 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3061 p2p.config_methods = wpa_s->wps->config_methods;
3062 }
3063
3064 if (wpa_s->conf->p2p_listen_reg_class &&
3065 wpa_s->conf->p2p_listen_channel) {
3066 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3067 p2p.channel = wpa_s->conf->p2p_listen_channel;
3068 } else {
3069 p2p.reg_class = 81;
3070 /*
3071 * Pick one of the social channels randomly as the listen
3072 * channel.
3073 */
3074 os_get_random((u8 *) &r, sizeof(r));
3075 p2p.channel = 1 + (r % 3) * 5;
3076 }
3077 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3078
3079 if (wpa_s->conf->p2p_oper_reg_class &&
3080 wpa_s->conf->p2p_oper_channel) {
3081 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3082 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3083 p2p.cfg_op_channel = 1;
3084 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3085 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3086
3087 } else {
3088 p2p.op_reg_class = 81;
3089 /*
3090 * Use random operation channel from (1, 6, 11) if no other
3091 * preference is indicated.
3092 */
3093 os_get_random((u8 *) &r, sizeof(r));
3094 p2p.op_channel = 1 + (r % 3) * 5;
3095 p2p.cfg_op_channel = 0;
3096 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3097 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3098 }
3099 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3100 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3101 p2p.country[2] = 0x04;
3102 } else
3103 os_memcpy(p2p.country, "XX\x04", 3);
3104
3105 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3106 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3107 "channel list");
3108 return -1;
3109 }
3110
3111 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3112 WPS_DEV_TYPE_LEN);
3113
3114 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3115 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3116 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3117
3118 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3119 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3120
3121 p2p.max_peers = 100;
3122
3123 if (wpa_s->conf->p2p_ssid_postfix) {
3124 p2p.ssid_postfix_len =
3125 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3126 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3127 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3128 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3129 p2p.ssid_postfix_len);
3130 }
3131
3132 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3133
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003134 p2p.max_listen = wpa_s->max_remain_on_chan;
3135
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003136#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003137 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003138 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003139 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3140 } else {
3141 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003142 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003143 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3144 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003145#endif
3146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003147 global->p2p = p2p_init(&p2p);
3148 if (global->p2p == NULL)
3149 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003150 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151
3152 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3153 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3154 continue;
3155 p2p_add_wps_vendor_extension(
3156 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3157 }
3158
3159 return 0;
3160}
3161
3162
3163/**
3164 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3165 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3166 *
3167 * This function deinitialize per-interface P2P data.
3168 */
3169void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3170{
3171 if (wpa_s->driver && wpa_s->drv_priv)
3172 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003173
3174 if (wpa_s->go_params) {
3175 /* Clear any stored provisioning info */
3176 p2p_clear_provisioning_info(
3177 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003178 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003179 }
3180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181 os_free(wpa_s->go_params);
3182 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003183 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3184 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3185 wpa_s->p2p_long_listen = 0;
3186 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3187 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3188 wpas_p2p_remove_pending_group_interface(wpa_s);
3189
3190 /* TODO: remove group interface from the driver if this wpa_s instance
3191 * is on top of a P2P group interface */
3192}
3193
3194
3195/**
3196 * wpas_p2p_deinit_global - Deinitialize global P2P module
3197 * @global: Pointer to global data from wpa_supplicant_init()
3198 *
3199 * This function deinitializes the global (per device) P2P module.
3200 */
3201void wpas_p2p_deinit_global(struct wpa_global *global)
3202{
3203 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003204
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003205 wpa_s = global->ifaces;
3206 if (wpa_s)
3207 wpas_p2p_service_flush(wpa_s);
3208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003209 if (global->p2p == NULL)
3210 return;
3211
3212 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3214 wpa_s = wpa_s->next;
3215 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216 tmp = global->ifaces;
3217 while (tmp &&
3218 (tmp == wpa_s ||
3219 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3220 tmp = tmp->next;
3221 }
3222 if (tmp == NULL)
3223 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003224 /* Disconnect from the P2P group and deinit the interface */
3225 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 }
3227
3228 /*
3229 * Deinit GO data on any possibly remaining interface (if main
3230 * interface is used as GO).
3231 */
3232 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3233 if (wpa_s->ap_iface)
3234 wpas_p2p_group_deinit(wpa_s);
3235 }
3236
3237 p2p_deinit(global->p2p);
3238 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003239 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003240}
3241
3242
3243static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3244{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003245 if (wpa_s->conf->p2p_no_group_iface)
3246 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247 if (wpa_s->drv_flags &
3248 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3249 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3250 return 1; /* P2P group requires a new interface in every case
3251 */
3252 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3253 return 0; /* driver does not support concurrent operations */
3254 if (wpa_s->global->ifaces->next)
3255 return 1; /* more that one interface already in use */
3256 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3257 return 1; /* this interface is already in use */
3258 return 0;
3259}
3260
3261
3262static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3263 const u8 *peer_addr,
3264 enum p2p_wps_method wps_method,
3265 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003266 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003267 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003269 if (persistent_group && wpa_s->conf->persistent_reconnect)
3270 persistent_group = 2;
3271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3273 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3274 go_intent, own_interface_addr,
3275 force_freq, persistent_group);
3276 }
3277
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003278 /*
3279 * Increase GO config timeout if HT40 is used since it takes some time
3280 * to scan channels for coex purposes before the BSS can be started.
3281 */
3282 p2p_set_config_timeout(wpa_s->global->p2p,
3283 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3286 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003287 persistent_group, ssid ? ssid->ssid : NULL,
3288 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003289 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290}
3291
3292
3293static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3294 const u8 *peer_addr,
3295 enum p2p_wps_method wps_method,
3296 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003297 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003298 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003299{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003300 if (persistent_group && wpa_s->conf->persistent_reconnect)
3301 persistent_group = 2;
3302
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3304 return -1;
3305
3306 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3307 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003308 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003309 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310}
3311
3312
3313static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3314{
3315 wpa_s->p2p_join_scan_count++;
3316 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3317 wpa_s->p2p_join_scan_count);
3318 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3319 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3320 " for join operationg - stop join attempt",
3321 MAC2STR(wpa_s->pending_join_iface_addr));
3322 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003323 if (wpa_s->p2p_auto_pd) {
3324 wpa_s->p2p_auto_pd = 0;
3325 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3326 " p2p_dev_addr=" MACSTR " status=N/A",
3327 MAC2STR(wpa_s->pending_join_dev_addr));
3328 return;
3329 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003330 wpa_msg(wpa_s->parent, MSG_INFO,
3331 P2P_EVENT_GROUP_FORMATION_FAILURE);
3332 }
3333}
3334
3335
Dmitry Shmidt04949592012-07-19 12:16:46 -07003336static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3337{
3338 struct wpa_supplicant *iface;
3339 int shared_freq;
3340 u8 bssid[ETH_ALEN];
3341
3342 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3343 return 0;
3344
3345 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3346 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3347 continue;
3348 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3349 continue;
3350 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3351 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3352 shared_freq = iface->current_ssid->frequency;
3353 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3354 shared_freq = iface->assoc_freq;
3355 else
3356 shared_freq = 0;
3357
3358 if (shared_freq && freq != shared_freq) {
3359 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3360 "connected on %d MHz - new connection on "
3361 "%d MHz", iface->ifname, shared_freq, freq);
3362 return 1;
3363 }
3364 }
3365
3366 shared_freq = wpa_drv_shared_freq(wpa_s);
3367 if (shared_freq > 0 && shared_freq != freq) {
3368 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3369 "virtual interface connected on %d MHz - new "
3370 "connection on %d MHz", shared_freq, freq);
3371 return 1;
3372 }
3373
3374 return 0;
3375}
3376
3377
3378static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3379 const u8 *peer_dev_addr)
3380{
3381 struct wpa_bss *bss;
3382 int updated;
3383
3384 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3385 if (bss == NULL)
3386 return -1;
3387 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3388 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3389 "last scan");
3390 return 0;
3391 }
3392
3393 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3394 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3395 "%ld.%06ld (%supdated in last scan)",
3396 bss->last_update.sec, bss->last_update.usec,
3397 updated ? "": "not ");
3398
3399 return updated;
3400}
3401
3402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003403static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3404 struct wpa_scan_results *scan_res)
3405{
3406 struct wpa_bss *bss;
3407 int freq;
3408 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003409
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003410 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3411
3412 if (wpa_s->global->p2p_disabled)
3413 return;
3414
Dmitry Shmidt04949592012-07-19 12:16:46 -07003415 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3416 scan_res ? (int) scan_res->num : -1,
3417 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418
3419 if (scan_res)
3420 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3421
Dmitry Shmidt04949592012-07-19 12:16:46 -07003422 if (wpa_s->p2p_auto_pd) {
3423 int join = wpas_p2p_peer_go(wpa_s,
3424 wpa_s->pending_join_dev_addr);
3425 if (join == 0 &&
3426 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3427 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003428 bss = wpa_bss_get_bssid_latest(
3429 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003430 if (bss) {
3431 freq = bss->freq;
3432 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3433 "the peer " MACSTR " at %d MHz",
3434 wpa_s->auto_pd_scan_retry,
3435 MAC2STR(wpa_s->
3436 pending_join_dev_addr),
3437 freq);
3438 wpas_p2p_join_scan_req(wpa_s, freq);
3439 return;
3440 }
3441 }
3442
3443 if (join < 0)
3444 join = 0;
3445
3446 wpa_s->p2p_auto_pd = 0;
3447 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3448 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3449 MAC2STR(wpa_s->pending_join_dev_addr), join);
3450 if (p2p_prov_disc_req(wpa_s->global->p2p,
3451 wpa_s->pending_join_dev_addr,
3452 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003453 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003454 wpa_s->p2p_auto_pd = 0;
3455 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3456 " p2p_dev_addr=" MACSTR " status=N/A",
3457 MAC2STR(wpa_s->pending_join_dev_addr));
3458 }
3459 return;
3460 }
3461
3462 if (wpa_s->p2p_auto_join) {
3463 int join = wpas_p2p_peer_go(wpa_s,
3464 wpa_s->pending_join_dev_addr);
3465 if (join < 0) {
3466 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3467 "running a GO -> use GO Negotiation");
3468 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3469 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3470 wpa_s->p2p_persistent_group, 0, 0, 0,
3471 wpa_s->p2p_go_intent,
3472 wpa_s->p2p_connect_freq,
3473 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003474 wpa_s->p2p_pd_before_go_neg,
3475 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003476 return;
3477 }
3478
3479 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3480 "try to join the group", join ? "" :
3481 " in older scan");
3482 if (!join)
3483 wpa_s->p2p_fallback_to_go_neg = 1;
3484 }
3485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003486 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3487 wpa_s->pending_join_iface_addr);
3488 if (freq < 0 &&
3489 p2p_get_interface_addr(wpa_s->global->p2p,
3490 wpa_s->pending_join_dev_addr,
3491 iface_addr) == 0 &&
3492 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3493 {
3494 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3495 "address for join from " MACSTR " to " MACSTR
3496 " based on newly discovered P2P peer entry",
3497 MAC2STR(wpa_s->pending_join_iface_addr),
3498 MAC2STR(iface_addr));
3499 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3500 ETH_ALEN);
3501
3502 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3503 wpa_s->pending_join_iface_addr);
3504 }
3505 if (freq >= 0) {
3506 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3507 "from P2P peer table: %d MHz", freq);
3508 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003509 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510 if (bss) {
3511 freq = bss->freq;
3512 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3513 "from BSS table: %d MHz", freq);
3514 }
3515 if (freq > 0) {
3516 u16 method;
3517
Dmitry Shmidt04949592012-07-19 12:16:46 -07003518 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3519 wpa_msg(wpa_s->parent, MSG_INFO,
3520 P2P_EVENT_GROUP_FORMATION_FAILURE
3521 "reason=FREQ_CONFLICT");
3522 return;
3523 }
3524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3526 "prior to joining an existing group (GO " MACSTR
3527 " freq=%u MHz)",
3528 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3529 wpa_s->pending_pd_before_join = 1;
3530
3531 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003532 case WPS_PIN_DISPLAY:
3533 method = WPS_CONFIG_KEYPAD;
3534 break;
3535 case WPS_PIN_KEYPAD:
3536 method = WPS_CONFIG_DISPLAY;
3537 break;
3538 case WPS_PBC:
3539 method = WPS_CONFIG_PUSHBUTTON;
3540 break;
3541 default:
3542 method = 0;
3543 break;
3544 }
3545
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003546 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3547 wpa_s->pending_join_dev_addr) ==
3548 method)) {
3549 /*
3550 * We have already performed provision discovery for
3551 * joining the group. Proceed directly to join
3552 * operation without duplicated provision discovery. */
3553 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3554 "with " MACSTR " already done - proceed to "
3555 "join",
3556 MAC2STR(wpa_s->pending_join_dev_addr));
3557 wpa_s->pending_pd_before_join = 0;
3558 goto start;
3559 }
3560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003562 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003563 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003564 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3565 "Discovery Request before joining an "
3566 "existing group");
3567 wpa_s->pending_pd_before_join = 0;
3568 goto start;
3569 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003570 return;
3571 }
3572
3573 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3574 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3575 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3576 wpas_p2p_check_join_scan_limit(wpa_s);
3577 return;
3578
3579start:
3580 /* Start join operation immediately */
3581 wpas_p2p_join_start(wpa_s);
3582}
3583
3584
Dmitry Shmidt04949592012-07-19 12:16:46 -07003585static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003587 int ret;
3588 struct wpa_driver_scan_params params;
3589 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003590 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003591 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003592#ifdef ANDROID_P2P
3593 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003595 /* If freq is not provided, check the operating freq of the GO and do a
3596 * a directed scan to save time
3597 */
3598 if(!freq) {
3599 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3600 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3601 }
3602#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603 os_memset(&params, 0, sizeof(params));
3604
3605 /* P2P Wildcard SSID */
3606 params.num_ssids = 1;
3607 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3608 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3609
3610 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003611 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3612 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3613 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614 if (wps_ie == NULL) {
3615 wpas_p2p_scan_res_join(wpa_s, NULL);
3616 return;
3617 }
3618
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003619 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3620 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003621 if (ies == NULL) {
3622 wpabuf_free(wps_ie);
3623 wpas_p2p_scan_res_join(wpa_s, NULL);
3624 return;
3625 }
3626 wpabuf_put_buf(ies, wps_ie);
3627 wpabuf_free(wps_ie);
3628
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003629 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003631 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003632 params.extra_ies = wpabuf_head(ies);
3633 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003634 if (freq > 0) {
3635 freqs[0] = freq;
3636 params.freqs = freqs;
3637 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003638
3639 /*
3640 * Run a scan to update BSS table and start Provision Discovery once
3641 * the new scan results become available.
3642 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003643 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003644 if (!ret) {
3645 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003646 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003647 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648
3649 wpabuf_free(ies);
3650
3651 if (ret) {
3652 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3653 "try again later");
3654 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3655 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3656 wpas_p2p_check_join_scan_limit(wpa_s);
3657 }
3658}
3659
3660
Dmitry Shmidt04949592012-07-19 12:16:46 -07003661static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3662{
3663 struct wpa_supplicant *wpa_s = eloop_ctx;
3664 wpas_p2p_join_scan_req(wpa_s, 0);
3665}
3666
3667
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003668static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003669 const u8 *dev_addr, enum p2p_wps_method wps_method,
3670 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671{
3672 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003673 MACSTR " dev " MACSTR ")%s",
3674 MAC2STR(iface_addr), MAC2STR(dev_addr),
3675 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003676
Dmitry Shmidt04949592012-07-19 12:16:46 -07003677 wpa_s->p2p_auto_pd = 0;
3678 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3680 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3681 wpa_s->pending_join_wps_method = wps_method;
3682
3683 /* Make sure we are not running find during connection establishment */
3684 wpas_p2p_stop_find(wpa_s);
3685
3686 wpa_s->p2p_join_scan_count = 0;
3687 wpas_p2p_join_scan(wpa_s, NULL);
3688 return 0;
3689}
3690
3691
3692static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3693{
3694 struct wpa_supplicant *group;
3695 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003696 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697
3698 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3699 if (group == NULL)
3700 return -1;
3701 if (group != wpa_s) {
3702 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3703 sizeof(group->p2p_pin));
3704 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003705 } else {
3706 /*
3707 * Need to mark the current interface for p2p_group_formation
3708 * when a separate group interface is not used. This is needed
3709 * to allow p2p_cancel stop a pending p2p_connect-join.
3710 * wpas_p2p_init_group_interface() addresses this for the case
3711 * where a separate group interface is used.
3712 */
3713 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714 }
3715
3716 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003717 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003718
3719 os_memset(&res, 0, sizeof(res));
3720 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3721 ETH_ALEN);
3722 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003723 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003724 if (bss) {
3725 res.freq = bss->freq;
3726 res.ssid_len = bss->ssid_len;
3727 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3728 }
3729
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003730 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3731 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3732 "starting client");
3733 wpa_drv_cancel_remain_on_channel(wpa_s);
3734 wpa_s->off_channel_freq = 0;
3735 wpa_s->roc_waiting_drv_freq = 0;
3736 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003737 wpas_start_wps_enrollee(group, &res);
3738
3739 /*
3740 * Allow a longer timeout for join-a-running-group than normal 15
3741 * second group formation timeout since the GO may not have authorized
3742 * our connection yet.
3743 */
3744 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3745 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3746 wpa_s, NULL);
3747
3748 return 0;
3749}
3750
3751
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003752static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3753 int *force_freq, int *pref_freq,
3754 int *oper_freq)
3755{
3756 if (freq > 0) {
3757 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3758 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3759 "(%u MHz) is not supported for P2P uses",
3760 freq);
3761 return -3;
3762 }
3763
3764 if (*oper_freq > 0 && freq != *oper_freq &&
3765 !(wpa_s->drv_flags &
3766 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3767 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3768 "on %u MHz while connected on another "
3769 "channel (%u MHz)", freq, *oper_freq);
3770 return -2;
3771 }
3772 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3773 "requested channel (%u MHz)", freq);
3774 *force_freq = freq;
3775 } else if (*oper_freq > 0 &&
3776 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3777 if (!(wpa_s->drv_flags &
3778 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3779 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3780 "while connected on non-P2P supported "
3781 "channel (%u MHz)", *oper_freq);
3782 return -2;
3783 }
3784 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3785 "(%u MHz) not available for P2P - try to use "
3786 "another channel", *oper_freq);
3787 *force_freq = 0;
3788 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3789 (wpa_s->drv_flags &
3790 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3791 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3792 "are already using (%u MHz) on another interface",
3793 *oper_freq);
3794 *pref_freq = *oper_freq;
3795 } else if (*oper_freq > 0) {
3796 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3797 "channel we are already using (%u MHz) on another "
3798 "interface", *oper_freq);
3799 *force_freq = *oper_freq;
3800 }
3801
3802 return 0;
3803}
3804
3805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806/**
3807 * wpas_p2p_connect - Request P2P Group Formation to be started
3808 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3809 * @peer_addr: Address of the peer P2P Device
3810 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3811 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003812 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003813 * @join: Whether to join an existing group (as a client) instead of starting
3814 * Group Owner negotiation; @peer_addr is BSSID in that case
3815 * @auth: Whether to only authorize the connection instead of doing that and
3816 * initiating Group Owner negotiation
3817 * @go_intent: GO Intent or -1 to use default
3818 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003819 * @persistent_id: Persistent group credentials to use for forcing GO
3820 * parameters or -1 to generate new values (SSID/passphrase)
3821 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3822 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003823 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3825 * failure, -2 on failure due to channel not currently available,
3826 * -3 if forced channel is not supported
3827 */
3828int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3829 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003830 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003831 int go_intent, int freq, int persistent_id, int pd,
3832 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003833{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003834 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835 u8 bssid[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003836 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003837 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003838 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003839 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003840
3841 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3842 return -1;
3843
Dmitry Shmidt04949592012-07-19 12:16:46 -07003844 if (persistent_id >= 0) {
3845 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3846 if (ssid == NULL || ssid->disabled != 2 ||
3847 ssid->mode != WPAS_MODE_P2P_GO)
3848 return -1;
3849 }
3850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003851 if (go_intent < 0)
3852 go_intent = wpa_s->conf->p2p_go_intent;
3853
3854 if (!auth)
3855 wpa_s->p2p_long_listen = 0;
3856
3857 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003858 wpa_s->p2p_persistent_group = !!persistent_group;
3859 wpa_s->p2p_persistent_id = persistent_id;
3860 wpa_s->p2p_go_intent = go_intent;
3861 wpa_s->p2p_connect_freq = freq;
3862 wpa_s->p2p_fallback_to_go_neg = 0;
3863 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003864 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003865
3866 if (pin)
3867 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3868 else if (wps_method == WPS_PIN_DISPLAY) {
3869 ret = wps_generate_pin();
3870 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3871 ret);
3872 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3873 wpa_s->p2p_pin);
3874 } else
3875 wpa_s->p2p_pin[0] = '\0';
3876
Dmitry Shmidt04949592012-07-19 12:16:46 -07003877 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3879 if (auth) {
3880 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3881 "connect a running group from " MACSTR,
3882 MAC2STR(peer_addr));
3883 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3884 return ret;
3885 }
3886 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3887 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3888 iface_addr) < 0) {
3889 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3890 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3891 dev_addr);
3892 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003893 if (auto_join) {
3894 os_get_time(&wpa_s->p2p_auto_started);
3895 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3896 "%ld.%06ld",
3897 wpa_s->p2p_auto_started.sec,
3898 wpa_s->p2p_auto_started.usec);
3899 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003900 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003901 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3902 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003903 return -1;
3904 return ret;
3905 }
3906
3907 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003908 wpa_s->assoc_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003910 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003911 oper_freq = wpa_drv_shared_freq(wpa_s);
3912 if (oper_freq < 0)
3913 oper_freq = 0;
3914 }
3915
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003916 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
3917 &oper_freq);
3918 if (res)
3919 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003920 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921
3922 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3923
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003924 if (wpa_s->create_p2p_iface) {
3925 /* Prepare to add a new interface for the group */
3926 iftype = WPA_IF_P2P_GROUP;
3927 if (go_intent == 15)
3928 iftype = WPA_IF_P2P_GO;
3929 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3930 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3931 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003934
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003935 if_addr = wpa_s->pending_interface_addr;
3936 } else
3937 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938
3939 if (auth) {
3940 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003941 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003942 force_freq, persistent_group, ssid,
3943 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 return -1;
3945 return ret;
3946 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003947
3948 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3949 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003950 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003951 if (wpa_s->create_p2p_iface)
3952 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953 return -1;
3954 }
3955 return ret;
3956}
3957
3958
3959/**
3960 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3961 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3962 * @freq: Frequency of the channel in MHz
3963 * @duration: Duration of the stay on the channel in milliseconds
3964 *
3965 * This callback is called when the driver indicates that it has started the
3966 * requested remain-on-channel duration.
3967 */
3968void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3969 unsigned int freq, unsigned int duration)
3970{
3971 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3972 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003973 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3974 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3975 wpa_s->pending_listen_duration);
3976 wpa_s->pending_listen_freq = 0;
3977 }
3978}
3979
3980
3981static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3982 unsigned int timeout)
3983{
3984 /* Limit maximum Listen state time based on driver limitation. */
3985 if (timeout > wpa_s->max_remain_on_chan)
3986 timeout = wpa_s->max_remain_on_chan;
3987
3988 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3989 return wpa_drv_p2p_listen(wpa_s, timeout);
3990
3991 return p2p_listen(wpa_s->global->p2p, timeout);
3992}
3993
3994
3995/**
3996 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3997 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3998 * @freq: Frequency of the channel in MHz
3999 *
4000 * This callback is called when the driver indicates that a remain-on-channel
4001 * operation has been completed, i.e., the duration on the requested channel
4002 * has timed out.
4003 */
4004void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4005 unsigned int freq)
4006{
4007 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4008 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004009 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004010 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4011 return;
4012 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4013 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004014 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004015 return;
4016 if (wpa_s->p2p_long_listen > 0)
4017 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4018 if (wpa_s->p2p_long_listen > 0) {
4019 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4020 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4021 }
4022}
4023
4024
4025/**
4026 * wpas_p2p_group_remove - Remove a P2P group
4027 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4028 * @ifname: Network interface name of the group interface or "*" to remove all
4029 * groups
4030 * Returns: 0 on success, -1 on failure
4031 *
4032 * This function is used to remove a P2P group. This can be used to disconnect
4033 * from a group in which the local end is a P2P Client or to end a P2P Group in
4034 * case the local end is the Group Owner. If a virtual network interface was
4035 * created for this group, that interface will be removed. Otherwise, only the
4036 * configured P2P group network will be removed from the interface.
4037 */
4038int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4039{
4040 struct wpa_global *global = wpa_s->global;
4041
4042 if (os_strcmp(ifname, "*") == 0) {
4043 struct wpa_supplicant *prev;
4044 wpa_s = global->ifaces;
4045 while (wpa_s) {
4046 prev = wpa_s;
4047 wpa_s = wpa_s->next;
4048 wpas_p2p_disconnect(prev);
4049 }
4050 return 0;
4051 }
4052
4053 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4054 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4055 break;
4056 }
4057
4058 return wpas_p2p_disconnect(wpa_s);
4059}
4060
4061
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004062static int freq_included(const struct p2p_channels *channels, unsigned int freq)
4063{
4064 if (channels == NULL)
4065 return 1; /* Assume no restrictions */
4066 return p2p_channels_includes_freq(channels, freq);
4067}
4068
4069
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004070static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4071 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004072 int freq, int ht40,
4073 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004074{
4075 u8 bssid[ETH_ALEN];
4076 int res;
4077
4078 os_memset(params, 0, sizeof(*params));
4079 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004080 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004082 if (!freq_included(channels, freq)) {
4083 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4084 "accepted", freq);
4085 return -1;
4086 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4088 "frequency %d MHz", freq);
4089 params->freq = freq;
4090 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4091 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004092 wpa_s->conf->p2p_oper_channel <= 11 &&
4093 freq_included(channels,
4094 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004095 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4096 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4097 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004098 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4099 wpa_s->conf->p2p_oper_reg_class == 116 ||
4100 wpa_s->conf->p2p_oper_reg_class == 117 ||
4101 wpa_s->conf->p2p_oper_reg_class == 124 ||
4102 wpa_s->conf->p2p_oper_reg_class == 126 ||
4103 wpa_s->conf->p2p_oper_reg_class == 127) &&
4104 freq_included(channels,
4105 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4107 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4108 "frequency %d MHz", params->freq);
4109 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4110 wpa_s->best_overall_freq > 0 &&
4111 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004112 wpa_s->best_overall_freq) &&
4113 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114 params->freq = wpa_s->best_overall_freq;
4115 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4116 "channel %d MHz", params->freq);
4117 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4118 wpa_s->best_24_freq > 0 &&
4119 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004120 wpa_s->best_24_freq) &&
4121 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004122 params->freq = wpa_s->best_24_freq;
4123 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4124 "channel %d MHz", params->freq);
4125 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4126 wpa_s->best_5_freq > 0 &&
4127 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004128 wpa_s->best_5_freq) &&
4129 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004130 params->freq = wpa_s->best_5_freq;
4131 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4132 "channel %d MHz", params->freq);
4133 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004134 int chan;
4135 for (chan = 0; chan < 11; chan++) {
4136 params->freq = 2412 + chan * 5;
4137 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004138 params->freq) &&
4139 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004140 break;
4141 }
4142 if (chan == 11) {
4143 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4144 "allowed");
4145 return -1;
4146 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004147 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4148 "known)", params->freq);
4149 }
4150
4151 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4152 wpa_s->assoc_freq && !freq) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004153 if (!p2p_supported_freq(wpa_s->global->p2p, wpa_s->assoc_freq)
4154 || !freq_included(channels, wpa_s->assoc_freq)) {
4155 if (wpa_s->drv_flags &
4156 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4157 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4158 "the channel we are already using "
4159 "(%u MHz) - allow multi-channel "
4160 "concurrency", wpa_s->assoc_freq);
4161 } else {
4162 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4163 "the channel we are already using "
4164 "(%u MHz)", wpa_s->assoc_freq);
4165 return -1;
4166 }
4167 } else {
4168 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we "
4169 "are already using (%u MHz)",
4170 wpa_s->assoc_freq);
4171 params->freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004172 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173 }
4174
4175 res = wpa_drv_shared_freq(wpa_s);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004176 if (res > 0 && !freq &&
4177 (!p2p_supported_freq(wpa_s->global->p2p, res) ||
4178 !freq_included(channels, res))) {
4179 if (wpa_s->drv_flags &
4180 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4181 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4182 "channel we are already using on a shared "
4183 "interface (%u MHz) - allow multi-channel "
4184 "concurrency", res);
4185 } else {
4186 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4187 "channel we are already using on a shared "
4188 "interface (%u MHz)", res);
4189 return -1;
4190 }
4191 } else if (res > 0 && !freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004192 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4193 "already using on a shared interface");
4194 params->freq = res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004195 if (!freq_included(channels, params->freq)) {
4196 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4197 "accepted", params->freq);
4198 return -1;
4199 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004200 } else if (res > 0 && freq != res &&
4201 !(wpa_s->drv_flags &
4202 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4203 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4204 "while connected on another channel (%u MHz)",
4205 freq, res);
4206 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004208
4209 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004210}
4211
4212
4213static struct wpa_supplicant *
4214wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4215 int go)
4216{
4217 struct wpa_supplicant *group_wpa_s;
4218
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004219 if (!wpas_p2p_create_iface(wpa_s)) {
4220 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4221 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004222 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004223 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004224
4225 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004226 WPA_IF_P2P_CLIENT) < 0) {
4227 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004228 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004229 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004230 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4231 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004232 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4233 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234 wpas_p2p_remove_pending_group_interface(wpa_s);
4235 return NULL;
4236 }
4237
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004238 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4239 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240 return group_wpa_s;
4241}
4242
4243
4244/**
4245 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4246 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4247 * @persistent_group: Whether to create a persistent group
4248 * @freq: Frequency for the group or 0 to indicate no hardcoding
4249 * Returns: 0 on success, -1 on failure
4250 *
4251 * This function creates a new P2P group with the local end as the Group Owner,
4252 * i.e., without using Group Owner Negotiation.
4253 */
4254int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004255 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004256{
4257 struct p2p_go_neg_results params;
4258 unsigned int r;
4259
4260 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4261 return -1;
4262
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004263 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004264 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004265 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004267 if (freq == 2) {
4268 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4269 "band");
4270 if (wpa_s->best_24_freq > 0 &&
4271 p2p_supported_freq(wpa_s->global->p2p,
4272 wpa_s->best_24_freq)) {
4273 freq = wpa_s->best_24_freq;
4274 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4275 "channel: %d MHz", freq);
4276 } else {
4277 os_get_random((u8 *) &r, sizeof(r));
4278 freq = 2412 + (r % 3) * 25;
4279 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4280 "channel: %d MHz", freq);
4281 }
4282 }
4283
4284 if (freq == 5) {
4285 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4286 "band");
4287 if (wpa_s->best_5_freq > 0 &&
4288 p2p_supported_freq(wpa_s->global->p2p,
4289 wpa_s->best_5_freq)) {
4290 freq = wpa_s->best_5_freq;
4291 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4292 "channel: %d MHz", freq);
4293 } else {
4294 os_get_random((u8 *) &r, sizeof(r));
4295 freq = 5180 + (r % 4) * 20;
4296 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4297 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4298 "5 GHz channel for P2P group");
4299 return -1;
4300 }
4301 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4302 "channel: %d MHz", freq);
4303 }
4304 }
4305
4306 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4307 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4308 "(%u MHz) is not supported for P2P uses",
4309 freq);
4310 return -1;
4311 }
4312
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004313 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004314 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004315 if (params.freq &&
4316 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4317 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4318 "(%u MHz) is not supported for P2P uses",
4319 params.freq);
4320 return -1;
4321 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004322 p2p_go_params(wpa_s->global->p2p, &params);
4323 params.persistent_group = persistent_group;
4324
4325 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4326 if (wpa_s == NULL)
4327 return -1;
4328 wpas_start_wps_go(wpa_s, &params, 0);
4329
4330 return 0;
4331}
4332
4333
4334static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4335 struct wpa_ssid *params, int addr_allocated)
4336{
4337 struct wpa_ssid *ssid;
4338
4339 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4340 if (wpa_s == NULL)
4341 return -1;
4342
4343 wpa_supplicant_ap_deinit(wpa_s);
4344
4345 ssid = wpa_config_add_network(wpa_s->conf);
4346 if (ssid == NULL)
4347 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004348 wpa_config_set_network_defaults(ssid);
4349 ssid->temporary = 1;
4350 ssid->proto = WPA_PROTO_RSN;
4351 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4352 ssid->group_cipher = WPA_CIPHER_CCMP;
4353 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4354 ssid->ssid = os_malloc(params->ssid_len);
4355 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004356 wpa_config_remove_network(wpa_s->conf, ssid->id);
4357 return -1;
4358 }
4359 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4360 ssid->ssid_len = params->ssid_len;
4361 ssid->p2p_group = 1;
4362 ssid->export_keys = 1;
4363 if (params->psk_set) {
4364 os_memcpy(ssid->psk, params->psk, 32);
4365 ssid->psk_set = 1;
4366 }
4367 if (params->passphrase)
4368 ssid->passphrase = os_strdup(params->passphrase);
4369
4370 wpa_supplicant_select_network(wpa_s, ssid);
4371
4372 wpa_s->show_group_started = 1;
4373
4374 return 0;
4375}
4376
4377
4378int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4379 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004380 int freq, int ht40,
4381 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382{
4383 struct p2p_go_neg_results params;
4384 int go = 0;
4385
4386 if (ssid->disabled != 2 || ssid->ssid == NULL)
4387 return -1;
4388
4389 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4390 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4391 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4392 "already running");
4393 return 0;
4394 }
4395
4396 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004397 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004398
Dmitry Shmidt04949592012-07-19 12:16:46 -07004399 wpa_s->p2p_fallback_to_go_neg = 0;
4400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004401 if (ssid->mode == WPAS_MODE_INFRA)
4402 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4403
4404 if (ssid->mode != WPAS_MODE_P2P_GO)
4405 return -1;
4406
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004407 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004408 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004409
4410 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004411 params.psk_set = ssid->psk_set;
4412 if (params.psk_set)
4413 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004414 if (ssid->passphrase) {
4415 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4416 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4417 "persistent group");
4418 return -1;
4419 }
4420 os_strlcpy(params.passphrase, ssid->passphrase,
4421 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004422 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004423 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4424 params.ssid_len = ssid->ssid_len;
4425 params.persistent_group = 1;
4426
4427 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4428 if (wpa_s == NULL)
4429 return -1;
4430
4431 wpas_start_wps_go(wpa_s, &params, 0);
4432
4433 return 0;
4434}
4435
4436
4437static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4438 struct wpabuf *proberesp_ies)
4439{
4440 struct wpa_supplicant *wpa_s = ctx;
4441 if (wpa_s->ap_iface) {
4442 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004443 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4444 wpabuf_free(beacon_ies);
4445 wpabuf_free(proberesp_ies);
4446 return;
4447 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 if (beacon_ies) {
4449 wpabuf_free(hapd->p2p_beacon_ie);
4450 hapd->p2p_beacon_ie = beacon_ies;
4451 }
4452 wpabuf_free(hapd->p2p_probe_resp_ie);
4453 hapd->p2p_probe_resp_ie = proberesp_ies;
4454 } else {
4455 wpabuf_free(beacon_ies);
4456 wpabuf_free(proberesp_ies);
4457 }
4458 wpa_supplicant_ap_update_beacon(wpa_s);
4459}
4460
4461
4462static void wpas_p2p_idle_update(void *ctx, int idle)
4463{
4464 struct wpa_supplicant *wpa_s = ctx;
4465 if (!wpa_s->ap_iface)
4466 return;
4467 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004468 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004469 wpas_p2p_set_group_idle_timeout(wpa_s);
4470 else
4471 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4472}
4473
4474
4475struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004476 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004477{
4478 struct p2p_group *group;
4479 struct p2p_group_config *cfg;
4480
4481 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4482 return NULL;
4483 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4484 return NULL;
4485
4486 cfg = os_zalloc(sizeof(*cfg));
4487 if (cfg == NULL)
4488 return NULL;
4489
Dmitry Shmidt04949592012-07-19 12:16:46 -07004490 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004491 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004492 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004493 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004494 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4495 if (wpa_s->max_stations &&
4496 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4497 cfg->max_clients = wpa_s->max_stations;
4498 else
4499 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004500 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4501 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004502 cfg->cb_ctx = wpa_s;
4503 cfg->ie_update = wpas_p2p_ie_update;
4504 cfg->idle_update = wpas_p2p_idle_update;
4505
4506 group = p2p_group_init(wpa_s->global->p2p, cfg);
4507 if (group == NULL)
4508 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004509 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510 p2p_group_notif_formation_done(group);
4511 wpa_s->p2p_group = group;
4512 return group;
4513}
4514
4515
4516void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4517 int registrar)
4518{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004519 struct wpa_ssid *ssid = wpa_s->current_ssid;
4520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004521 if (!wpa_s->p2p_in_provisioning) {
4522 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4523 "provisioning not in progress");
4524 return;
4525 }
4526
Dmitry Shmidt04949592012-07-19 12:16:46 -07004527 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4528 u8 go_dev_addr[ETH_ALEN];
4529 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4530 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4531 ssid->ssid_len);
4532 /* Clear any stored provisioning info */
4533 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4534 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004536 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4537 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004538 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4539 /*
4540 * Use a separate timeout for initial data connection to
4541 * complete to allow the group to be removed automatically if
4542 * something goes wrong in this step before the P2P group idle
4543 * timeout mechanism is taken into use.
4544 */
4545 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4546 wpas_p2p_group_formation_timeout,
4547 wpa_s->parent, NULL);
4548 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004549 if (wpa_s->global->p2p)
4550 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4551 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4552 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4553 wpas_group_formation_completed(wpa_s, 1);
4554}
4555
4556
Jouni Malinen75ecf522011-06-27 15:19:46 -07004557void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4558 struct wps_event_fail *fail)
4559{
4560 if (!wpa_s->p2p_in_provisioning) {
4561 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4562 "provisioning not in progress");
4563 return;
4564 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004565
4566 if (wpa_s->go_params) {
4567 p2p_clear_provisioning_info(
4568 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004569 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004570 }
4571
Jouni Malinen75ecf522011-06-27 15:19:46 -07004572 wpas_notify_p2p_wps_failed(wpa_s, fail);
4573}
4574
4575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004576int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004577 const char *config_method,
4578 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004579{
4580 u16 config_methods;
4581
Dmitry Shmidt04949592012-07-19 12:16:46 -07004582 wpa_s->p2p_fallback_to_go_neg = 0;
4583 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004584 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004585 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004586 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004588 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4589 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004590 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004591 else {
4592 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004594 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595
Dmitry Shmidt04949592012-07-19 12:16:46 -07004596 if (use == WPAS_P2P_PD_AUTO) {
4597 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4598 wpa_s->pending_pd_config_methods = config_methods;
4599 wpa_s->p2p_auto_pd = 1;
4600 wpa_s->p2p_auto_join = 0;
4601 wpa_s->pending_pd_before_join = 0;
4602 wpa_s->auto_pd_scan_retry = 0;
4603 wpas_p2p_stop_find(wpa_s);
4604 wpa_s->p2p_join_scan_count = 0;
4605 os_get_time(&wpa_s->p2p_auto_started);
4606 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4607 wpa_s->p2p_auto_started.sec,
4608 wpa_s->p2p_auto_started.usec);
4609 wpas_p2p_join_scan(wpa_s, NULL);
4610 return 0;
4611 }
4612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004613 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4614 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004615 config_methods,
4616 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617 }
4618
4619 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4620 return -1;
4621
4622 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004623 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004624 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625}
4626
4627
4628int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4629 char *end)
4630{
4631 return p2p_scan_result_text(ies, ies_len, buf, end);
4632}
4633
4634
4635static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4636{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004637 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004638 return;
4639
4640 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4641 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004642 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643}
4644
4645
4646int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4647 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004648 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004649 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004650{
4651 wpas_p2p_clear_pending_action_tx(wpa_s);
4652 wpa_s->p2p_long_listen = 0;
4653
4654 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4655 return wpa_drv_p2p_find(wpa_s, timeout, type);
4656
Dmitry Shmidt04949592012-07-19 12:16:46 -07004657 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4658 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004659 return -1;
4660
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004661 wpa_supplicant_cancel_sched_scan(wpa_s);
4662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004663 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004664 num_req_dev_types, req_dev_types, dev_id,
4665 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666}
4667
4668
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004669static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670{
4671 wpas_p2p_clear_pending_action_tx(wpa_s);
4672 wpa_s->p2p_long_listen = 0;
4673 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4674 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004675 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676
4677 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4678 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004679 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004680 }
4681
4682 if (wpa_s->global->p2p)
4683 p2p_stop_find(wpa_s->global->p2p);
4684
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004685 return 0;
4686}
4687
4688
4689void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4690{
4691 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4692 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004693 wpas_p2p_remove_pending_group_interface(wpa_s);
4694}
4695
4696
4697static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4698{
4699 struct wpa_supplicant *wpa_s = eloop_ctx;
4700 wpa_s->p2p_long_listen = 0;
4701}
4702
4703
4704int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4705{
4706 int res;
4707
4708 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4709 return -1;
4710
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004711 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004712 wpas_p2p_clear_pending_action_tx(wpa_s);
4713
4714 if (timeout == 0) {
4715 /*
4716 * This is a request for unlimited Listen state. However, at
4717 * least for now, this is mapped to a Listen state for one
4718 * hour.
4719 */
4720 timeout = 3600;
4721 }
4722 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4723 wpa_s->p2p_long_listen = 0;
4724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004725 /*
4726 * Stop previous find/listen operation to avoid trying to request a new
4727 * remain-on-channel operation while the driver is still running the
4728 * previous one.
4729 */
4730 if (wpa_s->global->p2p)
4731 p2p_stop_find(wpa_s->global->p2p);
4732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004733 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4734 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4735 wpa_s->p2p_long_listen = timeout * 1000;
4736 eloop_register_timeout(timeout, 0,
4737 wpas_p2p_long_listen_timeout,
4738 wpa_s, NULL);
4739 }
4740
4741 return res;
4742}
4743
4744
4745int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4746 u8 *buf, size_t len, int p2p_group)
4747{
4748 struct wpabuf *p2p_ie;
4749 int ret;
4750
4751 if (wpa_s->global->p2p_disabled)
4752 return -1;
4753 if (wpa_s->global->p2p == NULL)
4754 return -1;
4755 if (bss == NULL)
4756 return -1;
4757
4758 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4759 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4760 p2p_group, p2p_ie);
4761 wpabuf_free(p2p_ie);
4762
4763 return ret;
4764}
4765
4766
4767int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004768 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004769 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770{
4771 if (wpa_s->global->p2p_disabled)
4772 return 0;
4773 if (wpa_s->global->p2p == NULL)
4774 return 0;
4775
Dmitry Shmidt04949592012-07-19 12:16:46 -07004776 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4777 ie, ie_len)) {
4778 case P2P_PREQ_NOT_P2P:
4779 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4780 ssi_signal);
4781 /* fall through */
4782 case P2P_PREQ_MALFORMED:
4783 case P2P_PREQ_NOT_LISTEN:
4784 case P2P_PREQ_NOT_PROCESSED:
4785 default: /* make gcc happy */
4786 return 0;
4787 case P2P_PREQ_PROCESSED:
4788 return 1;
4789 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790}
4791
4792
4793void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4794 const u8 *sa, const u8 *bssid,
4795 u8 category, const u8 *data, size_t len, int freq)
4796{
4797 if (wpa_s->global->p2p_disabled)
4798 return;
4799 if (wpa_s->global->p2p == NULL)
4800 return;
4801
4802 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4803 freq);
4804}
4805
4806
4807void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4808{
4809 if (wpa_s->global->p2p_disabled)
4810 return;
4811 if (wpa_s->global->p2p == NULL)
4812 return;
4813
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004814 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004815}
4816
4817
4818void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4819{
4820 p2p_group_deinit(wpa_s->p2p_group);
4821 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004822
4823 wpa_s->ap_configured_cb = NULL;
4824 wpa_s->ap_configured_cb_ctx = NULL;
4825 wpa_s->ap_configured_cb_data = NULL;
4826 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827}
4828
4829
4830int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4831{
4832 wpa_s->p2p_long_listen = 0;
4833
4834 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4835 return wpa_drv_p2p_reject(wpa_s, addr);
4836
4837 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4838 return -1;
4839
4840 return p2p_reject(wpa_s->global->p2p, addr);
4841}
4842
4843
4844/* Invite to reinvoke a persistent group */
4845int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004846 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004847 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848{
4849 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004850 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004851 int force_freq = 0, oper_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004852 int res;
4853
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004854 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004855 if (peer_addr)
4856 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4857 else
4858 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004859
Jouni Malinen31be0a42012-08-31 21:20:51 +03004860 wpa_s->p2p_persistent_go_freq = freq;
4861 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004862 if (ssid->mode == WPAS_MODE_P2P_GO) {
4863 role = P2P_INVITE_ROLE_GO;
4864 if (peer_addr == NULL) {
4865 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4866 "address in invitation command");
4867 return -1;
4868 }
4869 if (wpas_p2p_create_iface(wpa_s)) {
4870 if (wpas_p2p_add_group_interface(wpa_s,
4871 WPA_IF_P2P_GO) < 0) {
4872 wpa_printf(MSG_ERROR, "P2P: Failed to "
4873 "allocate a new interface for the "
4874 "group");
4875 return -1;
4876 }
4877 bssid = wpa_s->pending_interface_addr;
4878 } else
4879 bssid = wpa_s->own_addr;
4880 } else {
4881 role = P2P_INVITE_ROLE_CLIENT;
4882 peer_addr = ssid->bssid;
4883 }
4884 wpa_s->pending_invite_ssid_id = ssid->id;
4885
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004886 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4887 wpa_s->assoc_freq) {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004888 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004889 if (bssid == NULL)
4890 bssid = bssid_buf;
4891 } else {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004892 oper_freq = wpa_drv_shared_freq(wpa_s);
4893 if (oper_freq < 0)
4894 oper_freq = 0;
4895 }
4896
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004897 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4898 &oper_freq);
4899 if (res)
4900 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004901
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4903 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4904 ssid->ssid, ssid->ssid_len,
4905 go_dev_addr, 1);
4906
4907 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4908 return -1;
4909
4910 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004911 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4912 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913}
4914
4915
4916/* Invite to join an active group */
4917int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4918 const u8 *peer_addr, const u8 *go_dev_addr)
4919{
4920 struct wpa_global *global = wpa_s->global;
4921 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004922 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004924 int persistent;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004925 int force_freq = 0, oper_freq = 0, pref_freq = 0;
4926 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004927
Jouni Malinen31be0a42012-08-31 21:20:51 +03004928 wpa_s->p2p_persistent_go_freq = 0;
4929 wpa_s->p2p_go_ht40 = 0;
4930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4932 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4933 break;
4934 }
4935 if (wpa_s == NULL) {
4936 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4937 return -1;
4938 }
4939
4940 ssid = wpa_s->current_ssid;
4941 if (ssid == NULL) {
4942 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4943 "invitation");
4944 return -1;
4945 }
4946
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004947 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004948 persistent = ssid->p2p_persistent_group &&
4949 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4950 ssid->ssid, ssid->ssid_len);
4951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 if (ssid->mode == WPAS_MODE_P2P_GO) {
4953 role = P2P_INVITE_ROLE_ACTIVE_GO;
4954 bssid = wpa_s->own_addr;
4955 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004956 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957 } else {
4958 role = P2P_INVITE_ROLE_CLIENT;
4959 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4960 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4961 "invite to current group");
4962 return -1;
4963 }
4964 bssid = wpa_s->bssid;
4965 if (go_dev_addr == NULL &&
4966 !is_zero_ether_addr(wpa_s->go_dev_addr))
4967 go_dev_addr = wpa_s->go_dev_addr;
4968 }
4969 wpa_s->parent->pending_invite_ssid_id = -1;
4970
4971 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4972 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4973 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004974 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975
4976 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4977 return -1;
4978
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004979 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4980 wpa_s->assoc_freq) {
4981 oper_freq = wpa_s->assoc_freq;
4982 if (bssid == NULL)
4983 bssid = bssid_buf;
4984 } else {
4985 oper_freq = wpa_drv_shared_freq(wpa_s);
4986 if (oper_freq < 0)
4987 oper_freq = 0;
4988 }
4989
4990 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
4991 &oper_freq);
4992 if (res)
4993 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004994 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004997 ssid->ssid, ssid->ssid_len, force_freq,
4998 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004999}
5000
5001
5002void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5003{
5004 struct wpa_ssid *ssid = wpa_s->current_ssid;
5005 const char *ssid_txt;
5006 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005007 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005009 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010
Dmitry Shmidt04949592012-07-19 12:16:46 -07005011 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5012 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5013 wpa_s->parent, NULL);
5014 }
5015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005016 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005017 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018
5019 wpa_s->show_group_started = 0;
5020
5021 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5022 os_memset(go_dev_addr, 0, ETH_ALEN);
5023 if (ssid->bssid_set)
5024 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5025 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5026 ssid->ssid_len);
5027 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5028
5029 if (wpa_s->global->p2p_group_formation == wpa_s)
5030 wpa_s->global->p2p_group_formation = NULL;
5031
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005032 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5033 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034 if (ssid->passphrase == NULL && ssid->psk_set) {
5035 char psk[65];
5036 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5037 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5038 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
5039 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005040 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005041 MAC2STR(go_dev_addr),
5042 persistent ? " [PERSISTENT]" : "");
5043 } else {
5044 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5045 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
5046 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005047 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005048 ssid->passphrase ? ssid->passphrase : "",
5049 MAC2STR(go_dev_addr),
5050 persistent ? " [PERSISTENT]" : "");
5051 }
5052
5053 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005054 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5055 ssid, go_dev_addr);
5056 if (network_id < 0)
5057 network_id = ssid->id;
5058 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005059
5060done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005061 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062}
5063
5064
5065int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5066 u32 interval1, u32 duration2, u32 interval2)
5067{
5068 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5069 return -1;
5070 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5071 return -1;
5072
5073 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5074 wpa_s->current_ssid == NULL ||
5075 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5076 return -1;
5077
5078 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5079 wpa_s->own_addr, wpa_s->assoc_freq,
5080 duration1, interval1, duration2, interval2);
5081}
5082
5083
5084int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5085 unsigned int interval)
5086{
5087 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5088 return -1;
5089
5090 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5091 return -1;
5092
5093 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5094}
5095
5096
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005097static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5098{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005099 if (wpa_s->current_ssid == NULL) {
5100 /*
5101 * current_ssid can be cleared when P2P client interface gets
5102 * disconnected, so assume this interface was used as P2P
5103 * client.
5104 */
5105 return 1;
5106 }
5107 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005108 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5109}
5110
5111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5113{
5114 struct wpa_supplicant *wpa_s = eloop_ctx;
5115
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005116 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005117 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5118 "disabled");
5119 return;
5120 }
5121
Dmitry Shmidt04949592012-07-19 12:16:46 -07005122 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5123 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005124 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125}
5126
5127
5128static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5129{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005130 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131
Dmitry Shmidt04949592012-07-19 12:16:46 -07005132 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5133 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5136 return;
5137
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005138 timeout = wpa_s->conf->p2p_group_idle;
5139 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5140 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5141 timeout = P2P_MAX_CLIENT_IDLE;
5142
5143 if (timeout == 0)
5144 return;
5145
Dmitry Shmidt04949592012-07-19 12:16:46 -07005146 if (timeout < 0) {
5147 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5148 timeout = 0; /* special client mode no-timeout */
5149 else
5150 return;
5151 }
5152
5153 if (wpa_s->p2p_in_provisioning) {
5154 /*
5155 * Use the normal group formation timeout during the
5156 * provisioning phase to avoid terminating this process too
5157 * early due to group idle timeout.
5158 */
5159 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5160 "during provisioning");
5161 return;
5162 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305163
Dmitry Shmidt04949592012-07-19 12:16:46 -07005164 if (wpa_s->show_group_started) {
5165 /*
5166 * Use the normal group formation timeout between the end of
5167 * the provisioning phase and completion of 4-way handshake to
5168 * avoid terminating this process too early due to group idle
5169 * timeout.
5170 */
5171 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5172 "while waiting for initial 4-way handshake to "
5173 "complete");
5174 return;
5175 }
5176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005177 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005178 timeout);
5179 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5180 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005181}
5182
5183
Jouni Malinen2b89da82012-08-31 22:04:41 +03005184/* Returns 1 if the interface was removed */
5185int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5186 u16 reason_code, const u8 *ie, size_t ie_len,
5187 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005188{
5189 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005190 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005191 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005192 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005193
Dmitry Shmidt04949592012-07-19 12:16:46 -07005194 if (!locally_generated)
5195 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5196 ie_len);
5197
5198 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5199 wpa_s->current_ssid &&
5200 wpa_s->current_ssid->p2p_group &&
5201 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5202 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5203 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005204 if (wpas_p2p_group_delete(wpa_s,
5205 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5206 > 0)
5207 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005208 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005209
5210 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005211}
5212
5213
5214void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005215 u16 reason_code, const u8 *ie, size_t ie_len,
5216 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005217{
5218 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5219 return;
5220 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5221 return;
5222
Dmitry Shmidt04949592012-07-19 12:16:46 -07005223 if (!locally_generated)
5224 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5225 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005226}
5227
5228
5229void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5230{
5231 struct p2p_data *p2p = wpa_s->global->p2p;
5232
5233 if (p2p == NULL)
5234 return;
5235
5236 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5237 return;
5238
5239 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5240 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5241
5242 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5243 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5244
5245 if (wpa_s->wps &&
5246 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5247 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5248
5249 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5250 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5251
5252 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5253 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5254 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5255 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5256 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5257 }
5258
5259 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5260 p2p_set_sec_dev_types(p2p,
5261 (void *) wpa_s->conf->sec_device_type,
5262 wpa_s->conf->num_sec_device_types);
5263
5264 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5265 int i;
5266 p2p_remove_wps_vendor_extensions(p2p);
5267 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5268 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5269 continue;
5270 p2p_add_wps_vendor_extension(
5271 p2p, wpa_s->conf->wps_vendor_ext[i]);
5272 }
5273 }
5274
5275 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5276 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5277 char country[3];
5278 country[0] = wpa_s->conf->country[0];
5279 country[1] = wpa_s->conf->country[1];
5280 country[2] = 0x04;
5281 p2p_set_country(p2p, country);
5282 }
5283
5284 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5285 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5286 wpa_s->conf->p2p_ssid_postfix ?
5287 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5288 0);
5289 }
5290
5291 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5292 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005293
5294 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5295 u8 reg_class, channel;
5296 int ret;
5297 unsigned int r;
5298 if (wpa_s->conf->p2p_listen_reg_class &&
5299 wpa_s->conf->p2p_listen_channel) {
5300 reg_class = wpa_s->conf->p2p_listen_reg_class;
5301 channel = wpa_s->conf->p2p_listen_channel;
5302 } else {
5303 reg_class = 81;
5304 /*
5305 * Pick one of the social channels randomly as the
5306 * listen channel.
5307 */
5308 os_get_random((u8 *) &r, sizeof(r));
5309 channel = 1 + (r % 3) * 5;
5310 }
5311 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5312 if (ret)
5313 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5314 "failed: %d", ret);
5315 }
5316 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5317 u8 op_reg_class, op_channel, cfg_op_channel;
5318 int ret = 0;
5319 unsigned int r;
5320 if (wpa_s->conf->p2p_oper_reg_class &&
5321 wpa_s->conf->p2p_oper_channel) {
5322 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5323 op_channel = wpa_s->conf->p2p_oper_channel;
5324 cfg_op_channel = 1;
5325 } else {
5326 op_reg_class = 81;
5327 /*
5328 * Use random operation channel from (1, 6, 11)
5329 *if no other preference is indicated.
5330 */
5331 os_get_random((u8 *) &r, sizeof(r));
5332 op_channel = 1 + (r % 3) * 5;
5333 cfg_op_channel = 0;
5334 }
5335 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5336 cfg_op_channel);
5337 if (ret)
5338 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5339 "failed: %d", ret);
5340 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005341
5342 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5343 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5344 wpa_s->conf->p2p_pref_chan) < 0) {
5345 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5346 "update failed");
5347 }
5348 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005349}
5350
5351
5352int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5353 int duration)
5354{
5355 if (!wpa_s->ap_iface)
5356 return -1;
5357 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5358 duration);
5359}
5360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005361
5362int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5363{
5364 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5365 return -1;
5366 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5367 return -1;
5368
5369 wpa_s->global->cross_connection = enabled;
5370 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5371
5372 if (!enabled) {
5373 struct wpa_supplicant *iface;
5374
5375 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5376 {
5377 if (iface->cross_connect_enabled == 0)
5378 continue;
5379
5380 iface->cross_connect_enabled = 0;
5381 iface->cross_connect_in_use = 0;
5382 wpa_msg(iface->parent, MSG_INFO,
5383 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5384 iface->ifname, iface->cross_connect_uplink);
5385 }
5386 }
5387
5388 return 0;
5389}
5390
5391
5392static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5393{
5394 struct wpa_supplicant *iface;
5395
5396 if (!uplink->global->cross_connection)
5397 return;
5398
5399 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5400 if (!iface->cross_connect_enabled)
5401 continue;
5402 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5403 0)
5404 continue;
5405 if (iface->ap_iface == NULL)
5406 continue;
5407 if (iface->cross_connect_in_use)
5408 continue;
5409
5410 iface->cross_connect_in_use = 1;
5411 wpa_msg(iface->parent, MSG_INFO,
5412 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5413 iface->ifname, iface->cross_connect_uplink);
5414 }
5415}
5416
5417
5418static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5419{
5420 struct wpa_supplicant *iface;
5421
5422 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5423 if (!iface->cross_connect_enabled)
5424 continue;
5425 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5426 0)
5427 continue;
5428 if (!iface->cross_connect_in_use)
5429 continue;
5430
5431 wpa_msg(iface->parent, MSG_INFO,
5432 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5433 iface->ifname, iface->cross_connect_uplink);
5434 iface->cross_connect_in_use = 0;
5435 }
5436}
5437
5438
5439void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5440{
5441 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5442 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5443 wpa_s->cross_connect_disallowed)
5444 wpas_p2p_disable_cross_connect(wpa_s);
5445 else
5446 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005447 if (!wpa_s->ap_iface &&
5448 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5449 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450}
5451
5452
5453void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5454{
5455 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005456 if (!wpa_s->ap_iface &&
5457 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5458 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459 wpas_p2p_set_group_idle_timeout(wpa_s);
5460}
5461
5462
5463static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5464{
5465 struct wpa_supplicant *iface;
5466
5467 if (!wpa_s->global->cross_connection)
5468 return;
5469
5470 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5471 if (iface == wpa_s)
5472 continue;
5473 if (iface->drv_flags &
5474 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5475 continue;
5476 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5477 continue;
5478
5479 wpa_s->cross_connect_enabled = 1;
5480 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5481 sizeof(wpa_s->cross_connect_uplink));
5482 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5483 "%s to %s whenever uplink is available",
5484 wpa_s->ifname, wpa_s->cross_connect_uplink);
5485
5486 if (iface->ap_iface || iface->current_ssid == NULL ||
5487 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5488 iface->cross_connect_disallowed ||
5489 iface->wpa_state != WPA_COMPLETED)
5490 break;
5491
5492 wpa_s->cross_connect_in_use = 1;
5493 wpa_msg(wpa_s->parent, MSG_INFO,
5494 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5495 wpa_s->ifname, wpa_s->cross_connect_uplink);
5496 break;
5497 }
5498}
5499
5500
5501int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5502{
5503 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5504 !wpa_s->p2p_in_provisioning)
5505 return 0; /* not P2P client operation */
5506
5507 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5508 "session overlap");
5509 if (wpa_s != wpa_s->parent)
5510 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5511
5512 if (wpa_s->global->p2p)
5513 p2p_group_formation_failed(wpa_s->global->p2p);
5514
5515 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5516 wpa_s->parent, NULL);
5517
5518 wpas_group_formation_completed(wpa_s, 0);
5519 return 1;
5520}
5521
5522
5523void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5524{
5525 struct p2p_channels chan;
5526
5527 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5528 return;
5529
5530 os_memset(&chan, 0, sizeof(chan));
5531 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5532 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5533 "channel list");
5534 return;
5535 }
5536
5537 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5538}
5539
5540
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005541static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5542 struct wpa_scan_results *scan_res)
5543{
5544 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5545}
5546
5547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005548int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5549{
5550 struct wpa_global *global = wpa_s->global;
5551 int found = 0;
5552 const u8 *peer;
5553
5554 if (global->p2p == NULL)
5555 return -1;
5556
5557 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5558
5559 if (wpa_s->pending_interface_name[0] &&
5560 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5561 found = 1;
5562
5563 peer = p2p_get_go_neg_peer(global->p2p);
5564 if (peer) {
5565 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5566 MACSTR, MAC2STR(peer));
5567 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005568 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005569 }
5570
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005571 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5572 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5573 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5574 found = 1;
5575 }
5576
5577 if (wpa_s->pending_pd_before_join) {
5578 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5579 wpa_s->pending_pd_before_join = 0;
5580 found = 1;
5581 }
5582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005583 wpas_p2p_stop_find(wpa_s);
5584
5585 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5586 if (wpa_s == global->p2p_group_formation &&
5587 (wpa_s->p2p_in_provisioning ||
5588 wpa_s->parent->pending_interface_type ==
5589 WPA_IF_P2P_CLIENT)) {
5590 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5591 "formation found - cancelling",
5592 wpa_s->ifname);
5593 found = 1;
5594 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5595 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005596 if (wpa_s->p2p_in_provisioning) {
5597 wpas_group_formation_completed(wpa_s, 0);
5598 break;
5599 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005600 wpas_p2p_group_delete(wpa_s,
5601 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005602 break;
5603 }
5604 }
5605
5606 if (!found) {
5607 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5608 return -1;
5609 }
5610
5611 return 0;
5612}
5613
5614
5615void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5616{
5617 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5618 return;
5619
5620 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5621 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005622 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005623}
5624
5625
5626void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5627 int freq_24, int freq_5, int freq_overall)
5628{
5629 struct p2p_data *p2p = wpa_s->global->p2p;
5630 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5631 return;
5632 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5633}
5634
5635
5636int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5637{
5638 u8 peer[ETH_ALEN];
5639 struct p2p_data *p2p = wpa_s->global->p2p;
5640
5641 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5642 return -1;
5643
5644 if (hwaddr_aton(addr, peer))
5645 return -1;
5646
5647 return p2p_unauthorize(p2p, peer);
5648}
5649
5650
5651/**
5652 * wpas_p2p_disconnect - Disconnect from a P2P Group
5653 * @wpa_s: Pointer to wpa_supplicant data
5654 * Returns: 0 on success, -1 on failure
5655 *
5656 * This can be used to disconnect from a group in which the local end is a P2P
5657 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5658 * virtual network interface was created for this group, that interface will be
5659 * removed. Otherwise, only the configured P2P group network will be removed
5660 * from the interface.
5661 */
5662int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5663{
5664
5665 if (wpa_s == NULL)
5666 return -1;
5667
Jouni Malinen2b89da82012-08-31 22:04:41 +03005668 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5669 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005670}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005671
5672
5673int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5674{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005675 int ret;
5676
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005677 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5678 return 0;
5679
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005680 ret = p2p_in_progress(wpa_s->global->p2p);
5681 if (ret == 0) {
5682 /*
5683 * Check whether there is an ongoing WPS provisioning step (or
5684 * other parts of group formation) on another interface since
5685 * p2p_in_progress() does not report this to avoid issues for
5686 * scans during such provisioning step.
5687 */
5688 if (wpa_s->global->p2p_group_formation &&
5689 wpa_s->global->p2p_group_formation != wpa_s) {
5690 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5691 "in group formation",
5692 wpa_s->global->p2p_group_formation->ifname);
5693 ret = 1;
5694 }
5695 }
5696
5697 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005698}
5699
5700
5701void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5702 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005703{
5704 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5705 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5706 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005707 /**
5708 * Remove the network by scheduling the group formation
5709 * timeout to happen immediately. The teardown code
5710 * needs to be scheduled to run asynch later so that we
5711 * don't delete data from under ourselves unexpectedly.
5712 * Calling wpas_p2p_group_formation_timeout directly
5713 * causes a series of crashes in WPS failure scenarios.
5714 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005715 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5716 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005717 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5718 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005719 }
5720}
5721
5722
5723struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005724 const u8 *addr, const u8 *ssid,
5725 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005726{
5727 struct wpa_ssid *s;
5728 size_t i;
5729
5730 for (s = wpa_s->conf->ssid; s; s = s->next) {
5731 if (s->disabled != 2)
5732 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005733 if (ssid &&
5734 (ssid_len != s->ssid_len ||
5735 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5736 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005737 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5738 return s; /* peer is GO in the persistent group */
5739 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5740 continue;
5741 for (i = 0; i < s->num_p2p_clients; i++) {
5742 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5743 addr, ETH_ALEN) == 0)
5744 return s; /* peer is P2P client in persistent
5745 * group */
5746 }
5747 }
5748
5749 return NULL;
5750}
5751
5752
5753void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5754 const u8 *addr)
5755{
5756 if (addr == NULL)
5757 return;
5758 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5759}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005760
Dmitry Shmidt04949592012-07-19 12:16:46 -07005761
5762static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5763 int group_added)
5764{
5765 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005766 if (wpa_s->global->p2p_group_formation)
5767 group = wpa_s->global->p2p_group_formation;
5768 wpa_s = wpa_s->parent;
5769 offchannel_send_action_done(wpa_s);
5770 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005771 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005772 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5773 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5774 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5775 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5776 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005777 wpa_s->p2p_pd_before_go_neg,
5778 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005779}
5780
5781
5782int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5783{
5784 if (!wpa_s->p2p_fallback_to_go_neg ||
5785 wpa_s->p2p_in_provisioning <= 5)
5786 return 0;
5787
5788 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5789 return 0; /* peer operating as a GO */
5790
5791 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5792 "fallback to GO Negotiation");
5793 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5794
5795 return 1;
5796}
5797
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005798
5799unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5800{
5801 const char *rn, *rn2;
5802 struct wpa_supplicant *ifs;
5803
5804 if (wpa_s->wpa_state > WPA_SCANNING) {
5805 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5806 "concurrent operation",
5807 P2P_CONCURRENT_SEARCH_DELAY);
5808 return P2P_CONCURRENT_SEARCH_DELAY;
5809 }
5810
5811 if (!wpa_s->driver->get_radio_name)
5812 return 0;
5813 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5814 if (rn == NULL || rn[0] == '\0')
5815 return 0;
5816
5817 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5818 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5819 continue;
5820
5821 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5822 if (!rn2 || os_strcmp(rn, rn2) != 0)
5823 continue;
5824 if (ifs->wpa_state > WPA_SCANNING) {
5825 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5826 "delay due to concurrent operation on "
5827 "interface %s",
5828 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5829 return P2P_CONCURRENT_SEARCH_DELAY;
5830 }
5831 }
5832
5833 return 0;
5834}
5835
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005836
5837void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5838{
5839 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5840 "pending anymore (sta_scan_pending=%d "
5841 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5842 wpa_s->global->p2p_cb_on_scan_complete);
5843 wpa_s->sta_scan_pending = 0;
5844
5845 if (!wpa_s->global->p2p_cb_on_scan_complete)
5846 return;
5847 wpa_s->global->p2p_cb_on_scan_complete = 0;
5848
5849 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5850 return;
5851
5852 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
5853 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
5854 "continued after successful connection");
5855 p2p_increase_search_delay(wpa_s->global->p2p,
5856 wpas_p2p_search_delay(wpa_s));
5857 }
5858}
5859
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005860#ifdef ANDROID_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005861int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5862 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005863{
5864 struct wpa_supplicant *iface = NULL;
5865 struct p2p_data *p2p = wpa_s->global->p2p;
5866
5867 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005868 if ((iface->current_ssid) &&
5869 (iface->current_ssid->frequency != freq) &&
5870 ((iface->p2p_group_interface) ||
5871 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005872
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005873 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5874 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5875 /* Try to see whether we can move the GO. If it
5876 * is not possible, remove the GO interface
5877 */
5878 if (wpa_drv_switch_channel(iface, freq) == 0) {
5879 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5880 iface->current_ssid->frequency = freq;
5881 continue;
5882 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005883 }
5884
5885 /* If GO cannot be moved or if the conflicting interface is a
5886 * P2P Client, remove the interface depending up on the connection
5887 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005888 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005889 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005890 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005891 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005892 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005893 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005894 /* If connection in progress is p2p connection, do not proceed for the connection */
5895 if (wpa_s == iface)
5896 return -1;
5897 else
5898 /* If connection in progress is STA connection, proceed for the connection */
5899 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005900 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005901 /* P2p connection has priority, disable the STA network*/
5902 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5903 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5904 " id=%d", ssid->id);
5905 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5906 if (wpa_s == iface) {
5907 /* p2p connection is in progress, continue connecting...*/
5908 return 0;
5909 }
5910 else {
5911 /* STA connection is in progress, do not allow to continue */
5912 return -1;
5913 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005914 }
5915 }
5916 }
5917 return 0;
5918}
5919#endif