blob: 5fcaf292a2feaf85d67e4acc509ab21b788f5ae6 [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 Shmidt1cf45732013-04-29 17:36:45 -070098#ifdef ANDROID_P2P
99static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
100#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700101static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
102 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800103static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104
105
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700106static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
107 int freq)
108{
109 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
110 return;
111 if (freq > 0 &&
112 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
113 wpa_s->parent->conf->p2p_ignore_shared_freq)
114 freq = 0;
115 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
116}
117
118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
120 struct wpa_scan_results *scan_res)
121{
122 size_t i;
123
124 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
125 return;
126
127 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
128 (int) scan_res->num);
129
130 for (i = 0; i < scan_res->num; i++) {
131 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800132 struct os_time time_tmp_age, entry_ts;
133 time_tmp_age.sec = bss->age / 1000;
134 time_tmp_age.usec = (bss->age % 1000) * 1000;
135 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800137 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138 (const u8 *) (bss + 1),
139 bss->ie_len) > 0)
140 break;
141 }
142
143 p2p_scan_res_handled(wpa_s->global->p2p);
144}
145
146
147static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
148 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700149 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150{
151 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700152 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 struct wpa_driver_scan_params params;
154 int ret;
155 struct wpabuf *wps_ie, *ies;
156 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800157 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158
159 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
160 return -1;
161
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700162 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
163 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700164 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700165 wpas_p2p_in_progress(wpa_s) == 2) {
166 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
167 "pending station mode scan to be "
168 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700169 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700170 wpa_supplicant_req_scan(ifs, 0, 0);
171 return 1;
172 }
173 }
174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700175 os_memset(&params, 0, sizeof(params));
176
177 /* P2P Wildcard SSID */
178 params.num_ssids = 1;
179 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
180 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
181
182 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700183 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
184 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185 num_req_dev_types, req_dev_types);
186 if (wps_ie == NULL)
187 return -1;
188
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800189 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
190 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191 if (ies == NULL) {
192 wpabuf_free(wps_ie);
193 return -1;
194 }
195 wpabuf_put_buf(ies, wps_ie);
196 wpabuf_free(wps_ie);
197
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800198 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800200 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 params.extra_ies = wpabuf_head(ies);
202 params.extra_ies_len = wpabuf_len(ies);
203
204 switch (type) {
205 case P2P_SCAN_SOCIAL:
206 params.freqs = social_channels;
207 break;
208 case P2P_SCAN_FULL:
209 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700210 case P2P_SCAN_SPECIFIC:
211 social_channels[0] = freq;
212 social_channels[1] = 0;
213 params.freqs = social_channels;
214 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 case P2P_SCAN_SOCIAL_PLUS_ONE:
216 social_channels[3] = freq;
217 params.freqs = social_channels;
218 break;
219 }
220
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222
223 wpabuf_free(ies);
224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800225 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700226 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
227 if (ifs->scanning ||
228 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
229 wpa_s->global->p2p_cb_on_scan_complete = 1;
230 ret = 1;
231 break;
232 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700234 } else {
235 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700236 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700237 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239 return ret;
240}
241
242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
244{
245 switch (p2p_group_interface) {
246 case P2P_GROUP_INTERFACE_PENDING:
247 return WPA_IF_P2P_GROUP;
248 case P2P_GROUP_INTERFACE_GO:
249 return WPA_IF_P2P_GO;
250 case P2P_GROUP_INTERFACE_CLIENT:
251 return WPA_IF_P2P_CLIENT;
252 }
253
254 return WPA_IF_P2P_GROUP;
255}
256
257
258static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
259 const u8 *ssid,
260 size_t ssid_len, int *go)
261{
262 struct wpa_ssid *s;
263
264 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
265 for (s = wpa_s->conf->ssid; s; s = s->next) {
266 if (s->disabled != 0 || !s->p2p_group ||
267 s->ssid_len != ssid_len ||
268 os_memcmp(ssid, s->ssid, ssid_len) != 0)
269 continue;
270 if (s->mode == WPAS_MODE_P2P_GO &&
271 s != wpa_s->current_ssid)
272 continue;
273 if (go)
274 *go = s->mode == WPAS_MODE_P2P_GO;
275 return wpa_s;
276 }
277 }
278
279 return NULL;
280}
281
282
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700283static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
284 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285{
286 struct wpa_ssid *ssid;
287 char *gtype;
288 const char *reason;
289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 ssid = wpa_s->current_ssid;
291 if (ssid == NULL) {
292 /*
293 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700294 * pending P2P group interface waiting for provisioning or a
295 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296 */
297 ssid = wpa_s->conf->ssid;
298 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700299 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 break;
301 ssid = ssid->next;
302 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300303 if (ssid == NULL &&
304 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
305 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700306 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
307 "not found");
308 return -1;
309 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310 }
311 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
312 gtype = "GO";
313 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
314 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
315 wpa_s->reassociate = 0;
316 wpa_s->disconnected = 1;
317 wpa_supplicant_deauthenticate(wpa_s,
318 WLAN_REASON_DEAUTH_LEAVING);
319 gtype = "client";
320 } else
321 gtype = "GO";
322 if (wpa_s->cross_connect_in_use) {
323 wpa_s->cross_connect_in_use = 0;
324 wpa_msg(wpa_s->parent, MSG_INFO,
325 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
326 wpa_s->ifname, wpa_s->cross_connect_uplink);
327 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700328 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 case P2P_GROUP_REMOVAL_REQUESTED:
330 reason = " reason=REQUESTED";
331 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700332 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
333 reason = " reason=FORMATION_FAILED";
334 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
336 reason = " reason=IDLE";
337 break;
338 case P2P_GROUP_REMOVAL_UNAVAILABLE:
339 reason = " reason=UNAVAILABLE";
340 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700341 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
342 reason = " reason=GO_ENDING_SESSION";
343 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700344#ifdef ANDROID_P2P
345 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
346 reason = " reason=FREQ_CONFLICT";
347 break;
348#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 default:
350 reason = "";
351 break;
352 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700353 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700354 wpa_msg(wpa_s->parent, MSG_INFO,
355 P2P_EVENT_GROUP_REMOVED "%s %s%s",
356 wpa_s->ifname, gtype, reason);
357 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700359#ifdef ANDROID_P2P
360 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
361#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700362 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
363 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800364 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700365 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800366 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
367 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700368 wpa_s->p2p_in_provisioning = 0;
369 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700370
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700371 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
373
374 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
375 struct wpa_global *global;
376 char *ifname;
377 enum wpa_driver_if_type type;
378 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
379 wpa_s->ifname);
380 global = wpa_s->global;
381 ifname = os_strdup(wpa_s->ifname);
382 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700383 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 wpa_s = global->ifaces;
385 if (wpa_s && ifname)
386 wpa_drv_if_remove(wpa_s, type, ifname);
387 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300388 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 }
390
391 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
392 if (ssid && (ssid->p2p_group ||
393 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
394 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
395 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700396 if (ssid == wpa_s->current_ssid) {
397 wpa_sm_set_config(wpa_s->wpa, NULL);
398 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700400 }
401 /*
402 * Networks objects created during any P2P activities are not
403 * exposed out as they might/will confuse certain non-P2P aware
404 * applications since these network objects won't behave like
405 * regular ones.
406 *
407 * Likewise, we don't send out network removed signals for such
408 * network objects.
409 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410 wpa_config_remove_network(wpa_s->conf, id);
411 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800412 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700413 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 } else {
415 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
416 "found");
417 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700418 if (wpa_s->ap_iface)
419 wpa_supplicant_ap_deinit(wpa_s);
420 else
421 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700422
423 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424}
425
426
427static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
428 u8 *go_dev_addr,
429 const u8 *ssid, size_t ssid_len)
430{
431 struct wpa_bss *bss;
432 const u8 *bssid;
433 struct wpabuf *p2p;
434 u8 group_capab;
435 const u8 *addr;
436
437 if (wpa_s->go_params)
438 bssid = wpa_s->go_params->peer_interface_addr;
439 else
440 bssid = wpa_s->bssid;
441
442 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
443 if (bss == NULL) {
444 u8 iface_addr[ETH_ALEN];
445 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
446 iface_addr) == 0)
447 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
448 }
449 if (bss == NULL) {
450 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
451 "group is persistent - BSS " MACSTR " not found",
452 MAC2STR(bssid));
453 return 0;
454 }
455
456 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
457 if (p2p == NULL) {
458 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
459 "group is persistent - BSS " MACSTR
460 " did not include P2P IE", MAC2STR(bssid));
461 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
462 (u8 *) (bss + 1), bss->ie_len);
463 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
464 ((u8 *) bss + 1) + bss->ie_len,
465 bss->beacon_ie_len);
466 return 0;
467 }
468
469 group_capab = p2p_get_group_capab(p2p);
470 addr = p2p_get_go_dev_addr(p2p);
471 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
472 "group_capab=0x%x", group_capab);
473 if (addr) {
474 os_memcpy(go_dev_addr, addr, ETH_ALEN);
475 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
476 MAC2STR(addr));
477 } else
478 os_memset(go_dev_addr, 0, ETH_ALEN);
479 wpabuf_free(p2p);
480
481 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
482 "go_dev_addr=" MACSTR,
483 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
484
485 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
486}
487
488
Jouni Malinen75ecf522011-06-27 15:19:46 -0700489static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
490 struct wpa_ssid *ssid,
491 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700492{
493 struct wpa_ssid *s;
494 int changed = 0;
495
496 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
497 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
498 for (s = wpa_s->conf->ssid; s; s = s->next) {
499 if (s->disabled == 2 &&
500 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
501 s->ssid_len == ssid->ssid_len &&
502 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
503 break;
504 }
505
506 if (s) {
507 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
508 "entry");
509 if (ssid->passphrase && !s->passphrase)
510 changed = 1;
511 else if (ssid->passphrase && s->passphrase &&
512 os_strcmp(ssid->passphrase, s->passphrase) != 0)
513 changed = 1;
514 } else {
515 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
516 "entry");
517 changed = 1;
518 s = wpa_config_add_network(wpa_s->conf);
519 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700520 return -1;
521
522 /*
523 * Instead of network_added we emit persistent_group_added
524 * notification. Also to keep the defense checks in
525 * persistent_group obj registration method, we set the
526 * relevant flags in s to designate it as a persistent group.
527 */
528 s->p2p_group = 1;
529 s->p2p_persistent_group = 1;
530 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 wpa_config_set_network_defaults(s);
532 }
533
534 s->p2p_group = 1;
535 s->p2p_persistent_group = 1;
536 s->disabled = 2;
537 s->bssid_set = 1;
538 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
539 s->mode = ssid->mode;
540 s->auth_alg = WPA_AUTH_ALG_OPEN;
541 s->key_mgmt = WPA_KEY_MGMT_PSK;
542 s->proto = WPA_PROTO_RSN;
543 s->pairwise_cipher = WPA_CIPHER_CCMP;
544 s->export_keys = 1;
545 if (ssid->passphrase) {
546 os_free(s->passphrase);
547 s->passphrase = os_strdup(ssid->passphrase);
548 }
549 if (ssid->psk_set) {
550 s->psk_set = 1;
551 os_memcpy(s->psk, ssid->psk, 32);
552 }
553 if (s->passphrase && !s->psk_set)
554 wpa_config_update_psk(s);
555 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
556 os_free(s->ssid);
557 s->ssid = os_malloc(ssid->ssid_len);
558 }
559 if (s->ssid) {
560 s->ssid_len = ssid->ssid_len;
561 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
562 }
563
564#ifndef CONFIG_NO_CONFIG_WRITE
565 if (changed && wpa_s->conf->update_config &&
566 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
567 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
568 }
569#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700570
571 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572}
573
574
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800575static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
576 const u8 *addr)
577{
578 struct wpa_ssid *ssid, *s;
579 u8 *n;
580 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700581 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800582
583 ssid = wpa_s->current_ssid;
584 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
585 !ssid->p2p_persistent_group)
586 return;
587
588 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
589 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
590 continue;
591
592 if (s->ssid_len == ssid->ssid_len &&
593 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
594 break;
595 }
596
597 if (s == NULL)
598 return;
599
600 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
601 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700602 ETH_ALEN) != 0)
603 continue;
604
605 if (i == s->num_p2p_clients - 1)
606 return; /* already the most recent entry */
607
608 /* move the entry to mark it most recent */
609 os_memmove(s->p2p_client_list + i * ETH_ALEN,
610 s->p2p_client_list + (i + 1) * ETH_ALEN,
611 (s->num_p2p_clients - i - 1) * ETH_ALEN);
612 os_memcpy(s->p2p_client_list +
613 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
614 found = 1;
615 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800616 }
617
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700618 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
619 n = os_realloc_array(s->p2p_client_list,
620 s->num_p2p_clients + 1, ETH_ALEN);
621 if (n == NULL)
622 return;
623 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
624 s->p2p_client_list = n;
625 s->num_p2p_clients++;
626 } else if (!found) {
627 /* Not enough room for an additional entry - drop the oldest
628 * entry */
629 os_memmove(s->p2p_client_list,
630 s->p2p_client_list + ETH_ALEN,
631 (s->num_p2p_clients - 1) * ETH_ALEN);
632 os_memcpy(s->p2p_client_list +
633 (s->num_p2p_clients - 1) * ETH_ALEN,
634 addr, ETH_ALEN);
635 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800636
637#ifndef CONFIG_NO_CONFIG_WRITE
638 if (wpa_s->parent->conf->update_config &&
639 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
640 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
641#endif /* CONFIG_NO_CONFIG_WRITE */
642}
643
644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
646 int success)
647{
648 struct wpa_ssid *ssid;
649 const char *ssid_txt;
650 int client;
651 int persistent;
652 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700653 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654
655 /*
656 * This callback is likely called for the main interface. Update wpa_s
657 * to use the group interface if a new interface was created for the
658 * group.
659 */
660 if (wpa_s->global->p2p_group_formation)
661 wpa_s = wpa_s->global->p2p_group_formation;
662 wpa_s->global->p2p_group_formation = NULL;
663 wpa_s->p2p_in_provisioning = 0;
664
665 if (!success) {
666 wpa_msg(wpa_s->parent, MSG_INFO,
667 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700668 wpas_p2p_group_delete(wpa_s,
669 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670 return;
671 }
672
673 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
674
675 ssid = wpa_s->current_ssid;
676 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
677 ssid->mode = WPAS_MODE_P2P_GO;
678 p2p_group_notif_formation_done(wpa_s->p2p_group);
679 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
680 }
681
682 persistent = 0;
683 if (ssid) {
684 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
685 client = ssid->mode == WPAS_MODE_INFRA;
686 if (ssid->mode == WPAS_MODE_P2P_GO) {
687 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700688 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
689 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 } else
691 persistent = wpas_p2p_persistent_group(wpa_s,
692 go_dev_addr,
693 ssid->ssid,
694 ssid->ssid_len);
695 } else {
696 ssid_txt = "";
697 client = wpa_s->p2p_group_interface ==
698 P2P_GROUP_INTERFACE_CLIENT;
699 os_memset(go_dev_addr, 0, ETH_ALEN);
700 }
701
702 wpa_s->show_group_started = 0;
703 if (client) {
704 /*
705 * Indicate event only after successfully completed 4-way
706 * handshake, i.e., when the interface is ready for data
707 * packets.
708 */
709 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700710#ifdef ANDROID_P2P
711 /* For client Second phase of Group formation (4-way handshake) can be still pending
712 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700713 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 -0700714 wpa_s->global->p2p_group_formation = wpa_s;
715#endif
716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
718 char psk[65];
719 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
720 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
721 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
722 "%s",
723 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
724 MAC2STR(go_dev_addr),
725 persistent ? " [PERSISTENT]" : "");
726 wpas_p2p_cross_connect_setup(wpa_s);
727 wpas_p2p_set_group_idle_timeout(wpa_s);
728 } else {
729 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
730 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
731 "go_dev_addr=" MACSTR "%s",
732 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
733 ssid && ssid->passphrase ? ssid->passphrase : "",
734 MAC2STR(go_dev_addr),
735 persistent ? " [PERSISTENT]" : "");
736 wpas_p2p_cross_connect_setup(wpa_s);
737 wpas_p2p_set_group_idle_timeout(wpa_s);
738 }
739
740 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700741 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
742 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800743 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700744 network_id = ssid->id;
745 if (!client)
746 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747}
748
749
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800750static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
751 unsigned int freq,
752 const u8 *dst, const u8 *src,
753 const u8 *bssid,
754 const u8 *data, size_t data_len,
755 enum offchannel_send_action_result
756 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700757{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800758 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
761 return;
762 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
763 return;
764
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800765 switch (result) {
766 case OFFCHANNEL_SEND_ACTION_SUCCESS:
767 res = P2P_SEND_ACTION_SUCCESS;
768 break;
769 case OFFCHANNEL_SEND_ACTION_NO_ACK:
770 res = P2P_SEND_ACTION_NO_ACK;
771 break;
772 case OFFCHANNEL_SEND_ACTION_FAILED:
773 res = P2P_SEND_ACTION_FAILED;
774 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 }
776
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800779 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
780 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800781 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800782 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
783 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800785 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
786 "during p2p_connect-auto");
787 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
788 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789 }
790}
791
792
793static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
794 const u8 *src, const u8 *bssid, const u8 *buf,
795 size_t len, unsigned int wait_time)
796{
797 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
799 wait_time,
800 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801}
802
803
804static void wpas_send_action_done(void *ctx)
805{
806 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800807 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808}
809
810
811static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
812 struct p2p_go_neg_results *params)
813{
814 if (wpa_s->go_params == NULL) {
815 wpa_s->go_params = os_malloc(sizeof(*params));
816 if (wpa_s->go_params == NULL)
817 return -1;
818 }
819 os_memcpy(wpa_s->go_params, params, sizeof(*params));
820 return 0;
821}
822
823
824static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
825 struct p2p_go_neg_results *res)
826{
827 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
828 MAC2STR(res->peer_interface_addr));
829 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
830 res->ssid, res->ssid_len);
831 wpa_supplicant_ap_deinit(wpa_s);
832 wpas_copy_go_neg_results(wpa_s, res);
833 if (res->wps_method == WPS_PBC)
834 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
835 else {
836 u16 dev_pw_id = DEV_PW_DEFAULT;
837 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
838 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
839 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
840 wpa_s->p2p_pin, 1, dev_pw_id);
841 }
842}
843
844
845static void p2p_go_configured(void *ctx, void *data)
846{
847 struct wpa_supplicant *wpa_s = ctx;
848 struct p2p_go_neg_results *params = data;
849 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700850 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851
852 ssid = wpa_s->current_ssid;
853 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
854 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
855 if (wpa_s->global->p2p_group_formation == wpa_s)
856 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800857 if (os_strlen(params->passphrase) > 0) {
858 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
859 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
860 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
861 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
862 ssid->frequency, params->passphrase,
863 MAC2STR(wpa_s->global->p2p_dev_addr),
864 params->persistent_group ? " [PERSISTENT]" :
865 "");
866 } else {
867 char psk[65];
868 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
869 sizeof(params->psk));
870 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
871 "%s GO ssid=\"%s\" freq=%d psk=%s "
872 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
873 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
874 ssid->frequency, psk,
875 MAC2STR(wpa_s->global->p2p_dev_addr),
876 params->persistent_group ? " [PERSISTENT]" :
877 "");
878 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700881 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700883 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700884 if (network_id < 0)
885 network_id = ssid->id;
886 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 wpas_p2p_cross_connect_setup(wpa_s);
888 wpas_p2p_set_group_idle_timeout(wpa_s);
889 return;
890 }
891
892 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
893 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
894 params->peer_interface_addr)) {
895 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
896 "filtering");
897 return;
898 }
899 if (params->wps_method == WPS_PBC)
900 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800901 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 else if (wpa_s->p2p_pin[0])
903 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800904 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 os_free(wpa_s->go_params);
906 wpa_s->go_params = NULL;
907}
908
909
910static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
911 struct p2p_go_neg_results *params,
912 int group_formation)
913{
914 struct wpa_ssid *ssid;
915
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700916 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
917 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
918 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
919 "results");
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
923 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700924 if (ssid == NULL) {
925 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700927 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800929 wpa_s->show_group_started = 0;
930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 wpa_config_set_network_defaults(ssid);
932 ssid->temporary = 1;
933 ssid->p2p_group = 1;
934 ssid->p2p_persistent_group = params->persistent_group;
935 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
936 WPAS_MODE_P2P_GO;
937 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700938 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 ssid->ssid = os_zalloc(params->ssid_len + 1);
940 if (ssid->ssid) {
941 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
942 ssid->ssid_len = params->ssid_len;
943 }
944 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
945 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
946 ssid->proto = WPA_PROTO_RSN;
947 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800948 if (os_strlen(params->passphrase) > 0) {
949 ssid->passphrase = os_strdup(params->passphrase);
950 if (ssid->passphrase == NULL) {
951 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
952 "passphrase for GO");
953 wpa_config_remove_network(wpa_s->conf, ssid->id);
954 return;
955 }
956 } else
957 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800958 ssid->psk_set = params->psk_set;
959 if (ssid->psk_set)
960 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800961 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800962 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700963 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964
965 wpa_s->ap_configured_cb = p2p_go_configured;
966 wpa_s->ap_configured_cb_ctx = wpa_s;
967 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700968 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969 wpa_s->reassociate = 1;
970 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700971 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
972 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 wpa_supplicant_req_scan(wpa_s, 0, 0);
974}
975
976
977static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
978 const struct wpa_supplicant *src)
979{
980 struct wpa_config *d;
981 const struct wpa_config *s;
982
983 d = dst->conf;
984 s = src->conf;
985
986#define C(n) if (s->n) d->n = os_strdup(s->n)
987 C(device_name);
988 C(manufacturer);
989 C(model_name);
990 C(model_number);
991 C(serial_number);
992 C(config_methods);
993#undef C
994
995 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
996 os_memcpy(d->sec_device_type, s->sec_device_type,
997 sizeof(d->sec_device_type));
998 d->num_sec_device_types = s->num_sec_device_types;
999
1000 d->p2p_group_idle = s->p2p_group_idle;
1001 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001002 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001003 d->max_num_sta = s->max_num_sta;
1004 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001005 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006}
1007
1008
1009static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1010 enum wpa_driver_if_type type)
1011{
1012 char ifname[120], force_ifname[120];
1013
1014 if (wpa_s->pending_interface_name[0]) {
1015 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1016 "- skip creation of a new one");
1017 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1018 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1019 "unknown?! ifname='%s'",
1020 wpa_s->pending_interface_name);
1021 return -1;
1022 }
1023 return 0;
1024 }
1025
1026 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
1027 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001028 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1030 /* Try to avoid going over the IFNAMSIZ length limit */
1031 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1032 wpa_s->p2p_group_idx);
1033 }
1034 force_ifname[0] = '\0';
1035
1036 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1037 ifname);
1038 wpa_s->p2p_group_idx++;
1039
1040 wpa_s->pending_interface_type = type;
1041 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1042 wpa_s->pending_interface_addr, NULL) < 0) {
1043 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1044 "interface");
1045 return -1;
1046 }
1047
1048 if (force_ifname[0]) {
1049 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1050 force_ifname);
1051 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1052 sizeof(wpa_s->pending_interface_name));
1053 } else
1054 os_strlcpy(wpa_s->pending_interface_name, ifname,
1055 sizeof(wpa_s->pending_interface_name));
1056 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1057 MACSTR, wpa_s->pending_interface_name,
1058 MAC2STR(wpa_s->pending_interface_addr));
1059
1060 return 0;
1061}
1062
1063
1064static void wpas_p2p_remove_pending_group_interface(
1065 struct wpa_supplicant *wpa_s)
1066{
1067 if (!wpa_s->pending_interface_name[0] ||
1068 is_zero_ether_addr(wpa_s->pending_interface_addr))
1069 return; /* No pending virtual interface */
1070
1071 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1072 wpa_s->pending_interface_name);
1073 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1074 wpa_s->pending_interface_name);
1075 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1076 wpa_s->pending_interface_name[0] = '\0';
1077}
1078
1079
1080static struct wpa_supplicant *
1081wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1082{
1083 struct wpa_interface iface;
1084 struct wpa_supplicant *group_wpa_s;
1085
1086 if (!wpa_s->pending_interface_name[0]) {
1087 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1088 if (!wpas_p2p_create_iface(wpa_s))
1089 return NULL;
1090 /*
1091 * Something has forced us to remove the pending interface; try
1092 * to create a new one and hope for the best that we will get
1093 * the same local address.
1094 */
1095 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1096 WPA_IF_P2P_CLIENT) < 0)
1097 return NULL;
1098 }
1099
1100 os_memset(&iface, 0, sizeof(iface));
1101 iface.ifname = wpa_s->pending_interface_name;
1102 iface.driver = wpa_s->driver->name;
1103 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1104 iface.driver_param = wpa_s->conf->driver_param;
1105 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1106 if (group_wpa_s == NULL) {
1107 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1108 "wpa_supplicant interface");
1109 return NULL;
1110 }
1111 wpa_s->pending_interface_name[0] = '\0';
1112 group_wpa_s->parent = wpa_s;
1113 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1114 P2P_GROUP_INTERFACE_CLIENT;
1115 wpa_s->global->p2p_group_formation = group_wpa_s;
1116
1117 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1118
1119 return group_wpa_s;
1120}
1121
1122
1123static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1124 void *timeout_ctx)
1125{
1126 struct wpa_supplicant *wpa_s = eloop_ctx;
1127 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1128 if (wpa_s->global->p2p)
1129 p2p_group_formation_failed(wpa_s->global->p2p);
1130 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1131 wpa_drv_p2p_group_formation_failed(wpa_s);
1132 wpas_group_formation_completed(wpa_s, 0);
1133}
1134
1135
1136void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1137{
1138 struct wpa_supplicant *wpa_s = ctx;
1139
1140 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1141 wpa_drv_cancel_remain_on_channel(wpa_s);
1142 wpa_s->off_channel_freq = 0;
1143 wpa_s->roc_waiting_drv_freq = 0;
1144 }
1145
1146 if (res->status) {
1147 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1148 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001149 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 wpas_p2p_remove_pending_group_interface(wpa_s);
1151 return;
1152 }
1153
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001154 if (wpa_s->p2p_go_ht40)
1155 res->ht40 = 1;
1156
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001158 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159
Dmitry Shmidt04949592012-07-19 12:16:46 -07001160 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1161 struct wpa_ssid *ssid;
1162 ssid = wpa_config_get_network(wpa_s->conf,
1163 wpa_s->p2p_persistent_id);
1164 if (ssid && ssid->disabled == 2 &&
1165 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1166 size_t len = os_strlen(ssid->passphrase);
1167 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1168 "on requested persistent group");
1169 os_memcpy(res->passphrase, ssid->passphrase, len);
1170 res->passphrase[len] = '\0';
1171 }
1172 }
1173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 if (wpa_s->create_p2p_iface) {
1175 struct wpa_supplicant *group_wpa_s =
1176 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1177 if (group_wpa_s == NULL) {
1178 wpas_p2p_remove_pending_group_interface(wpa_s);
1179 return;
1180 }
1181 if (group_wpa_s != wpa_s) {
1182 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1183 sizeof(group_wpa_s->p2p_pin));
1184 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1185 }
1186 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1187 wpa_s->pending_interface_name[0] = '\0';
1188 group_wpa_s->p2p_in_provisioning = 1;
1189
1190 if (res->role_go)
1191 wpas_start_wps_go(group_wpa_s, res, 1);
1192 else
1193 wpas_start_wps_enrollee(group_wpa_s, res);
1194 } else {
1195 wpa_s->p2p_in_provisioning = 1;
1196 wpa_s->global->p2p_group_formation = wpa_s;
1197
1198 if (res->role_go)
1199 wpas_start_wps_go(wpa_s, res, 1);
1200 else
1201 wpas_start_wps_enrollee(ctx, res);
1202 }
1203
1204 wpa_s->p2p_long_listen = 0;
1205 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1206
1207 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1208 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1209 (res->peer_config_timeout % 100) * 10000,
1210 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1211}
1212
1213
1214void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1215{
1216 struct wpa_supplicant *wpa_s = ctx;
1217 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1218 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1219
1220 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1221}
1222
1223
1224void wpas_dev_found(void *ctx, const u8 *addr,
1225 const struct p2p_peer_info *info,
1226 int new_device)
1227{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 struct wpa_supplicant *wpa_s = ctx;
1230 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001231#define WFD_DEV_INFO_SIZE 9
1232 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001233 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001234#ifdef CONFIG_WIFI_DISPLAY
1235 if (info->wfd_subelems) {
1236 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1237 wpabuf_head(info->wfd_subelems),
1238 WFD_DEV_INFO_SIZE);
1239 }
1240#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1242 " p2p_dev_addr=" MACSTR
1243 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001244 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1246 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1247 sizeof(devtype)),
1248 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001249 info->dev_capab, info->group_capab,
1250 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001251#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252
1253 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1254}
1255
1256
1257static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1258{
1259 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001260
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001261 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1262 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1265}
1266
1267
1268static int wpas_start_listen(void *ctx, unsigned int freq,
1269 unsigned int duration,
1270 const struct wpabuf *probe_resp_ie)
1271{
1272 struct wpa_supplicant *wpa_s = ctx;
1273
1274 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1275
1276 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1277 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1278 "report received Probe Request frames");
1279 return -1;
1280 }
1281
1282 wpa_s->pending_listen_freq = freq;
1283 wpa_s->pending_listen_duration = duration;
1284
1285 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1286 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1287 "to remain on channel (%u MHz) for Listen "
1288 "state", freq);
1289 wpa_s->pending_listen_freq = 0;
1290 return -1;
1291 }
1292 wpa_s->off_channel_freq = 0;
1293 wpa_s->roc_waiting_drv_freq = freq;
1294
1295 return 0;
1296}
1297
1298
1299static void wpas_stop_listen(void *ctx)
1300{
1301 struct wpa_supplicant *wpa_s = ctx;
1302 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1303 wpa_drv_cancel_remain_on_channel(wpa_s);
1304 wpa_s->off_channel_freq = 0;
1305 wpa_s->roc_waiting_drv_freq = 0;
1306 }
1307 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1308 wpa_drv_probe_req_report(wpa_s, 0);
1309}
1310
1311
1312static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1313{
1314 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001315 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316}
1317
1318
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001319/*
1320 * DNS Header section is used only to calculate compression pointers, so the
1321 * contents of this data does not matter, but the length needs to be reserved
1322 * in the virtual packet.
1323 */
1324#define DNS_HEADER_LEN 12
1325
1326/*
1327 * 27-octet in-memory packet from P2P specification containing two implied
1328 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1329 */
1330#define P2P_SD_IN_MEMORY_LEN 27
1331
1332static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1333 u8 **spos, const u8 *end)
1334{
1335 while (*spos < end) {
1336 u8 val = ((*spos)[0] & 0xc0) >> 6;
1337 int len;
1338
1339 if (val == 1 || val == 2) {
1340 /* These are reserved values in RFC 1035 */
1341 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1342 "sequence starting with 0x%x", val);
1343 return -1;
1344 }
1345
1346 if (val == 3) {
1347 u16 offset;
1348 u8 *spos_tmp;
1349
1350 /* Offset */
1351 if (*spos + 2 > end) {
1352 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1353 "DNS offset field");
1354 return -1;
1355 }
1356
1357 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1358 if (offset >= *spos - start) {
1359 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1360 "pointer offset %u", offset);
1361 return -1;
1362 }
1363
1364 (*spos) += 2;
1365 spos_tmp = start + offset;
1366 return p2p_sd_dns_uncompress_label(upos, uend, start,
1367 &spos_tmp,
1368 *spos - 2);
1369 }
1370
1371 /* Label */
1372 len = (*spos)[0] & 0x3f;
1373 if (len == 0)
1374 return 0;
1375
1376 (*spos)++;
1377 if (*spos + len > end) {
1378 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1379 "sequence - no room for label with length "
1380 "%u", len);
1381 return -1;
1382 }
1383
1384 if (*upos + len + 2 > uend)
1385 return -2;
1386
1387 os_memcpy(*upos, *spos, len);
1388 *spos += len;
1389 *upos += len;
1390 (*upos)[0] = '.';
1391 (*upos)++;
1392 (*upos)[0] = '\0';
1393 }
1394
1395 return 0;
1396}
1397
1398
1399/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1400 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1401 * not large enough */
1402static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1403 size_t msg_len, size_t offset)
1404{
1405 /* 27-octet in-memory packet from P2P specification */
1406 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1407 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1408 u8 *tmp, *end, *spos;
1409 char *upos, *uend;
1410 int ret = 0;
1411
1412 if (buf_len < 2)
1413 return -1;
1414 if (offset > msg_len)
1415 return -1;
1416
1417 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1418 if (tmp == NULL)
1419 return -1;
1420 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1421 end = spos + msg_len;
1422 spos += offset;
1423
1424 os_memset(tmp, 0, DNS_HEADER_LEN);
1425 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1426 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1427
1428 upos = buf;
1429 uend = buf + buf_len;
1430
1431 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1432 if (ret) {
1433 os_free(tmp);
1434 return ret;
1435 }
1436
1437 if (upos == buf) {
1438 upos[0] = '.';
1439 upos[1] = '\0';
1440 } else if (upos[-1] == '.')
1441 upos[-1] = '\0';
1442
1443 os_free(tmp);
1444 return 0;
1445}
1446
1447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448static struct p2p_srv_bonjour *
1449wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1450 const struct wpabuf *query)
1451{
1452 struct p2p_srv_bonjour *bsrv;
1453 size_t len;
1454
1455 len = wpabuf_len(query);
1456 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1457 struct p2p_srv_bonjour, list) {
1458 if (len == wpabuf_len(bsrv->query) &&
1459 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1460 len) == 0)
1461 return bsrv;
1462 }
1463 return NULL;
1464}
1465
1466
1467static struct p2p_srv_upnp *
1468wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1469 const char *service)
1470{
1471 struct p2p_srv_upnp *usrv;
1472
1473 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1474 struct p2p_srv_upnp, list) {
1475 if (version == usrv->version &&
1476 os_strcmp(service, usrv->service) == 0)
1477 return usrv;
1478 }
1479 return NULL;
1480}
1481
1482
1483static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1484 u8 srv_trans_id)
1485{
1486 u8 *len_pos;
1487
1488 if (wpabuf_tailroom(resp) < 5)
1489 return;
1490
1491 /* Length (to be filled) */
1492 len_pos = wpabuf_put(resp, 2);
1493 wpabuf_put_u8(resp, srv_proto);
1494 wpabuf_put_u8(resp, srv_trans_id);
1495 /* Status Code */
1496 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1497 /* Response Data: empty */
1498 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1499}
1500
1501
1502static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1503 struct wpabuf *resp, u8 srv_trans_id)
1504{
1505 struct p2p_srv_bonjour *bsrv;
1506 u8 *len_pos;
1507
1508 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1509
1510 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1511 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1512 return;
1513 }
1514
1515 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1516 struct p2p_srv_bonjour, list) {
1517 if (wpabuf_tailroom(resp) <
1518 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1519 return;
1520 /* Length (to be filled) */
1521 len_pos = wpabuf_put(resp, 2);
1522 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1523 wpabuf_put_u8(resp, srv_trans_id);
1524 /* Status Code */
1525 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1526 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1527 wpabuf_head(bsrv->resp),
1528 wpabuf_len(bsrv->resp));
1529 /* Response Data */
1530 wpabuf_put_buf(resp, bsrv->query); /* Key */
1531 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1532 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1533 2);
1534 }
1535}
1536
1537
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001538static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1539 size_t query_len)
1540{
1541 char str_rx[256], str_srv[256];
1542
1543 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1544 return 0; /* Too short to include DNS Type and Version */
1545 if (os_memcmp(query + query_len - 3,
1546 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1547 3) != 0)
1548 return 0; /* Mismatch in DNS Type or Version */
1549 if (query_len == wpabuf_len(bsrv->query) &&
1550 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1551 return 1; /* Binary match */
1552
1553 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1554 0))
1555 return 0; /* Failed to uncompress query */
1556 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1557 wpabuf_head(bsrv->query),
1558 wpabuf_len(bsrv->query) - 3, 0))
1559 return 0; /* Failed to uncompress service */
1560
1561 return os_strcmp(str_rx, str_srv) == 0;
1562}
1563
1564
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001565static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1566 struct wpabuf *resp, u8 srv_trans_id,
1567 const u8 *query, size_t query_len)
1568{
1569 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001571 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572
1573 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1574 query, query_len);
1575 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1576 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1577 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1578 srv_trans_id);
1579 return;
1580 }
1581
1582 if (query_len == 0) {
1583 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1584 return;
1585 }
1586
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001587 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1588 struct p2p_srv_bonjour, list) {
1589 if (!match_bonjour_query(bsrv, query, query_len))
1590 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001592 if (wpabuf_tailroom(resp) <
1593 5 + query_len + wpabuf_len(bsrv->resp))
1594 return;
1595
1596 matches++;
1597
1598 /* Length (to be filled) */
1599 len_pos = wpabuf_put(resp, 2);
1600 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1601 wpabuf_put_u8(resp, srv_trans_id);
1602
1603 /* Status Code */
1604 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1605 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1606 wpabuf_head(bsrv->resp),
1607 wpabuf_len(bsrv->resp));
1608
1609 /* Response Data */
1610 wpabuf_put_data(resp, query, query_len); /* Key */
1611 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1612
1613 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1614 }
1615
1616 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1618 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001619 if (wpabuf_tailroom(resp) < 5)
1620 return;
1621
1622 /* Length (to be filled) */
1623 len_pos = wpabuf_put(resp, 2);
1624 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1625 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626
1627 /* Status Code */
1628 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1629 /* Response Data: empty */
1630 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1631 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633}
1634
1635
1636static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1637 struct wpabuf *resp, u8 srv_trans_id)
1638{
1639 struct p2p_srv_upnp *usrv;
1640 u8 *len_pos;
1641
1642 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1643
1644 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1645 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1646 return;
1647 }
1648
1649 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1650 struct p2p_srv_upnp, list) {
1651 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1652 return;
1653
1654 /* Length (to be filled) */
1655 len_pos = wpabuf_put(resp, 2);
1656 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1657 wpabuf_put_u8(resp, srv_trans_id);
1658
1659 /* Status Code */
1660 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1661 /* Response Data */
1662 wpabuf_put_u8(resp, usrv->version);
1663 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1664 usrv->service);
1665 wpabuf_put_str(resp, usrv->service);
1666 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1667 2);
1668 }
1669}
1670
1671
1672static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1673 struct wpabuf *resp, u8 srv_trans_id,
1674 const u8 *query, size_t query_len)
1675{
1676 struct p2p_srv_upnp *usrv;
1677 u8 *len_pos;
1678 u8 version;
1679 char *str;
1680 int count = 0;
1681
1682 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1683 query, query_len);
1684
1685 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1686 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1687 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1688 srv_trans_id);
1689 return;
1690 }
1691
1692 if (query_len == 0) {
1693 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1694 return;
1695 }
1696
1697 if (wpabuf_tailroom(resp) < 5)
1698 return;
1699
1700 /* Length (to be filled) */
1701 len_pos = wpabuf_put(resp, 2);
1702 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1703 wpabuf_put_u8(resp, srv_trans_id);
1704
1705 version = query[0];
1706 str = os_malloc(query_len);
1707 if (str == NULL)
1708 return;
1709 os_memcpy(str, query + 1, query_len - 1);
1710 str[query_len - 1] = '\0';
1711
1712 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1713 struct p2p_srv_upnp, list) {
1714 if (version != usrv->version)
1715 continue;
1716
1717 if (os_strcmp(str, "ssdp:all") != 0 &&
1718 os_strstr(usrv->service, str) == NULL)
1719 continue;
1720
1721 if (wpabuf_tailroom(resp) < 2)
1722 break;
1723 if (count == 0) {
1724 /* Status Code */
1725 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1726 /* Response Data */
1727 wpabuf_put_u8(resp, version);
1728 } else
1729 wpabuf_put_u8(resp, ',');
1730
1731 count++;
1732
1733 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1734 usrv->service);
1735 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1736 break;
1737 wpabuf_put_str(resp, usrv->service);
1738 }
1739 os_free(str);
1740
1741 if (count == 0) {
1742 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1743 "available");
1744 /* Status Code */
1745 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1746 /* Response Data: empty */
1747 }
1748
1749 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1750}
1751
1752
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001753#ifdef CONFIG_WIFI_DISPLAY
1754static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1755 struct wpabuf *resp, u8 srv_trans_id,
1756 const u8 *query, size_t query_len)
1757{
1758 const u8 *pos;
1759 u8 role;
1760 u8 *len_pos;
1761
1762 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1763
1764 if (!wpa_s->global->wifi_display) {
1765 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1766 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1767 srv_trans_id);
1768 return;
1769 }
1770
1771 if (query_len < 1) {
1772 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1773 "Role");
1774 return;
1775 }
1776
1777 if (wpabuf_tailroom(resp) < 5)
1778 return;
1779
1780 pos = query;
1781 role = *pos++;
1782 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1783
1784 /* TODO: role specific handling */
1785
1786 /* Length (to be filled) */
1787 len_pos = wpabuf_put(resp, 2);
1788 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1789 wpabuf_put_u8(resp, srv_trans_id);
1790 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1791
1792 while (pos < query + query_len) {
1793 if (*pos < MAX_WFD_SUBELEMS &&
1794 wpa_s->global->wfd_subelem[*pos] &&
1795 wpabuf_tailroom(resp) >=
1796 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1797 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1798 "subelement %u", *pos);
1799 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1800 }
1801 pos++;
1802 }
1803
1804 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1805}
1806#endif /* CONFIG_WIFI_DISPLAY */
1807
1808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1810 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1811{
1812 struct wpa_supplicant *wpa_s = ctx;
1813 const u8 *pos = tlvs;
1814 const u8 *end = tlvs + tlvs_len;
1815 const u8 *tlv_end;
1816 u16 slen;
1817 struct wpabuf *resp;
1818 u8 srv_proto, srv_trans_id;
1819 size_t buf_len;
1820 char *buf;
1821
1822 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1823 tlvs, tlvs_len);
1824 buf_len = 2 * tlvs_len + 1;
1825 buf = os_malloc(buf_len);
1826 if (buf) {
1827 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1828 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1829 MACSTR " %u %u %s",
1830 freq, MAC2STR(sa), dialog_token, update_indic,
1831 buf);
1832 os_free(buf);
1833 }
1834
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001835 if (wpa_s->p2p_sd_over_ctrl_iface) {
1836 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1837 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001839 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840
1841 resp = wpabuf_alloc(10000);
1842 if (resp == NULL)
1843 return;
1844
1845 while (pos + 1 < end) {
1846 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1847 slen = WPA_GET_LE16(pos);
1848 pos += 2;
1849 if (pos + slen > end || slen < 2) {
1850 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1851 "length");
1852 wpabuf_free(resp);
1853 return;
1854 }
1855 tlv_end = pos + slen;
1856
1857 srv_proto = *pos++;
1858 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1859 srv_proto);
1860 srv_trans_id = *pos++;
1861 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1862 srv_trans_id);
1863
1864 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1865 pos, tlv_end - pos);
1866
1867
1868 if (wpa_s->force_long_sd) {
1869 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1870 "response");
1871 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1872 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1873 goto done;
1874 }
1875
1876 switch (srv_proto) {
1877 case P2P_SERV_ALL_SERVICES:
1878 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1879 "for all services");
1880 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1881 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1882 wpa_printf(MSG_DEBUG, "P2P: No service "
1883 "discovery protocols available");
1884 wpas_sd_add_proto_not_avail(
1885 resp, P2P_SERV_ALL_SERVICES,
1886 srv_trans_id);
1887 break;
1888 }
1889 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1890 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1891 break;
1892 case P2P_SERV_BONJOUR:
1893 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1894 pos, tlv_end - pos);
1895 break;
1896 case P2P_SERV_UPNP:
1897 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1898 pos, tlv_end - pos);
1899 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001900#ifdef CONFIG_WIFI_DISPLAY
1901 case P2P_SERV_WIFI_DISPLAY:
1902 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1903 pos, tlv_end - pos);
1904 break;
1905#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906 default:
1907 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1908 "protocol %u", srv_proto);
1909 wpas_sd_add_proto_not_avail(resp, srv_proto,
1910 srv_trans_id);
1911 break;
1912 }
1913
1914 pos = tlv_end;
1915 }
1916
1917done:
1918 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1919 update_indic, tlvs, tlvs_len);
1920
1921 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1922
1923 wpabuf_free(resp);
1924}
1925
1926
1927void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1928 const u8 *tlvs, size_t tlvs_len)
1929{
1930 struct wpa_supplicant *wpa_s = ctx;
1931 const u8 *pos = tlvs;
1932 const u8 *end = tlvs + tlvs_len;
1933 const u8 *tlv_end;
1934 u16 slen;
1935 size_t buf_len;
1936 char *buf;
1937
1938 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1939 tlvs, tlvs_len);
1940 if (tlvs_len > 1500) {
1941 /* TODO: better way for handling this */
1942 wpa_msg_ctrl(wpa_s, MSG_INFO,
1943 P2P_EVENT_SERV_DISC_RESP MACSTR
1944 " %u <long response: %u bytes>",
1945 MAC2STR(sa), update_indic,
1946 (unsigned int) tlvs_len);
1947 } else {
1948 buf_len = 2 * tlvs_len + 1;
1949 buf = os_malloc(buf_len);
1950 if (buf) {
1951 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1952 wpa_msg_ctrl(wpa_s, MSG_INFO,
1953 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1954 MAC2STR(sa), update_indic, buf);
1955 os_free(buf);
1956 }
1957 }
1958
1959 while (pos < end) {
1960 u8 srv_proto, srv_trans_id, status;
1961
1962 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1963 slen = WPA_GET_LE16(pos);
1964 pos += 2;
1965 if (pos + slen > end || slen < 3) {
1966 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1967 "length");
1968 return;
1969 }
1970 tlv_end = pos + slen;
1971
1972 srv_proto = *pos++;
1973 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1974 srv_proto);
1975 srv_trans_id = *pos++;
1976 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1977 srv_trans_id);
1978 status = *pos++;
1979 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1980 status);
1981
1982 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1983 pos, tlv_end - pos);
1984
1985 pos = tlv_end;
1986 }
1987
1988 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1989}
1990
1991
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001992u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1993 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994{
1995 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001996 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001998 return 0;
1999 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002000}
2001
2002
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002003u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2004 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005{
2006 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002007 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008
2009 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2010 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002011 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2013 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2014 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2015 wpabuf_put_u8(tlvs, version);
2016 wpabuf_put_str(tlvs, query);
2017 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2018 wpabuf_free(tlvs);
2019 return ret;
2020}
2021
2022
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002023#ifdef CONFIG_WIFI_DISPLAY
2024
2025static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2026 const struct wpabuf *tlvs)
2027{
2028 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2029 return 0;
2030 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2031 return 0;
2032 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2033}
2034
2035
2036#define MAX_WFD_SD_SUBELEMS 20
2037
2038static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2039 const char *subelems)
2040{
2041 u8 *len;
2042 const char *pos;
2043 int val;
2044 int count = 0;
2045
2046 len = wpabuf_put(tlvs, 2);
2047 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2048 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2049
2050 wpabuf_put_u8(tlvs, role);
2051
2052 pos = subelems;
2053 while (*pos) {
2054 val = atoi(pos);
2055 if (val >= 0 && val < 256) {
2056 wpabuf_put_u8(tlvs, val);
2057 count++;
2058 if (count == MAX_WFD_SD_SUBELEMS)
2059 break;
2060 }
2061 pos = os_strchr(pos + 1, ',');
2062 if (pos == NULL)
2063 break;
2064 pos++;
2065 }
2066
2067 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2068}
2069
2070
2071u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2072 const u8 *dst, const char *role)
2073{
2074 struct wpabuf *tlvs;
2075 u64 ret;
2076 const char *subelems;
2077 u8 id = 1;
2078
2079 subelems = os_strchr(role, ' ');
2080 if (subelems == NULL)
2081 return 0;
2082 subelems++;
2083
2084 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2085 if (tlvs == NULL)
2086 return 0;
2087
2088 if (os_strstr(role, "[source]"))
2089 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2090 if (os_strstr(role, "[pri-sink]"))
2091 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2092 if (os_strstr(role, "[sec-sink]"))
2093 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2094 if (os_strstr(role, "[source+sink]"))
2095 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2096
2097 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2098 wpabuf_free(tlvs);
2099 return ret;
2100}
2101
2102#endif /* CONFIG_WIFI_DISPLAY */
2103
2104
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002105int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106{
2107 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002108 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2110 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002111 return p2p_sd_cancel_request(wpa_s->global->p2p,
2112 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113}
2114
2115
2116void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2117 const u8 *dst, u8 dialog_token,
2118 const struct wpabuf *resp_tlvs)
2119{
2120 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2121 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2122 resp_tlvs);
2123 return;
2124 }
2125 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2126 return;
2127 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2128 resp_tlvs);
2129}
2130
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002131#ifdef ANDROID_P2P
2132void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2133#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002135#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002136{
2137 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2138 wpa_drv_p2p_service_update(wpa_s);
2139 return;
2140 }
2141 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002142#ifdef ANDROID_P2P
2143 p2p_sd_service_update(wpa_s->global->p2p, action);
2144#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002146#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147}
2148
2149
2150static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2151{
2152 dl_list_del(&bsrv->list);
2153 wpabuf_free(bsrv->query);
2154 wpabuf_free(bsrv->resp);
2155 os_free(bsrv);
2156}
2157
2158
2159static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2160{
2161 dl_list_del(&usrv->list);
2162 os_free(usrv->service);
2163 os_free(usrv);
2164}
2165
2166
2167void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2168{
2169 struct p2p_srv_bonjour *bsrv, *bn;
2170 struct p2p_srv_upnp *usrv, *un;
2171
2172 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2173 struct p2p_srv_bonjour, list)
2174 wpas_p2p_srv_bonjour_free(bsrv);
2175
2176 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2177 struct p2p_srv_upnp, list)
2178 wpas_p2p_srv_upnp_free(usrv);
2179
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002180#ifdef ANDROID_P2P
2181 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2182#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002184#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185}
2186
2187
2188int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2189 struct wpabuf *query, struct wpabuf *resp)
2190{
2191 struct p2p_srv_bonjour *bsrv;
2192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 bsrv = os_zalloc(sizeof(*bsrv));
2194 if (bsrv == NULL)
2195 return -1;
2196 bsrv->query = query;
2197 bsrv->resp = resp;
2198 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2199
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002200#ifdef ANDROID_P2P
2201 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2202#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002204#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 return 0;
2206}
2207
2208
2209int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2210 const struct wpabuf *query)
2211{
2212 struct p2p_srv_bonjour *bsrv;
2213
2214 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2215 if (bsrv == NULL)
2216 return -1;
2217 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002218#ifdef ANDROID_P2P
2219 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2220#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002221 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002222#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 return 0;
2224}
2225
2226
2227int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2228 const char *service)
2229{
2230 struct p2p_srv_upnp *usrv;
2231
2232 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2233 return 0; /* Already listed */
2234 usrv = os_zalloc(sizeof(*usrv));
2235 if (usrv == NULL)
2236 return -1;
2237 usrv->version = version;
2238 usrv->service = os_strdup(service);
2239 if (usrv->service == NULL) {
2240 os_free(usrv);
2241 return -1;
2242 }
2243 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2244
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002245#ifdef ANDROID_P2P
2246 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2247#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002249#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250 return 0;
2251}
2252
2253
2254int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2255 const char *service)
2256{
2257 struct p2p_srv_upnp *usrv;
2258
2259 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2260 if (usrv == NULL)
2261 return -1;
2262 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002263#ifdef ANDROID_P2P
2264 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2265#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002267#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268 return 0;
2269}
2270
2271
2272static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2273 const u8 *peer, const char *params,
2274 unsigned int generated_pin)
2275{
2276 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2277 MAC2STR(peer), generated_pin, params);
2278}
2279
2280
2281static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2282 const u8 *peer, const char *params)
2283{
2284 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2285 MAC2STR(peer), params);
2286}
2287
2288
2289void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2290 const u8 *dev_addr, const u8 *pri_dev_type,
2291 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002292 u8 dev_capab, u8 group_capab, const u8 *group_id,
2293 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294{
2295 struct wpa_supplicant *wpa_s = ctx;
2296 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002297 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298 u8 empty_dev_type[8];
2299 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002300 struct wpa_supplicant *group = NULL;
2301
2302 if (group_id) {
2303 for (group = wpa_s->global->ifaces; group; group = group->next)
2304 {
2305 struct wpa_ssid *s = group->current_ssid;
2306 if (s != NULL &&
2307 s->mode == WPAS_MODE_P2P_GO &&
2308 group_id_len - ETH_ALEN == s->ssid_len &&
2309 os_memcmp(group_id + ETH_ALEN, s->ssid,
2310 s->ssid_len) == 0)
2311 break;
2312 }
2313 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314
2315 if (pri_dev_type == NULL) {
2316 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2317 pri_dev_type = empty_dev_type;
2318 }
2319 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2320 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002321 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322 MAC2STR(dev_addr),
2323 wps_dev_type_bin2str(pri_dev_type, devtype,
2324 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002325 dev_name, supp_config_methods, dev_capab, group_capab,
2326 group ? " group=" : "",
2327 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002330 if (config_methods & WPS_CONFIG_DISPLAY) {
2331 generated_pin = wps_generate_pin();
2332 wpas_prov_disc_local_display(wpa_s, peer, params,
2333 generated_pin);
2334 } else if (config_methods & WPS_CONFIG_KEYPAD)
2335 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2336 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2337 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2338 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002339
2340 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2341 P2P_PROV_DISC_SUCCESS,
2342 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343}
2344
2345
2346void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2347{
2348 struct wpa_supplicant *wpa_s = ctx;
2349 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002350 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002352 if (wpa_s->pending_pd_before_join &&
2353 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2354 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2355 wpa_s->pending_pd_before_join = 0;
2356 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2357 "join-existing-group operation");
2358 wpas_p2p_join_start(wpa_s);
2359 return;
2360 }
2361
Dmitry Shmidt04949592012-07-19 12:16:46 -07002362 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2363 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2364 os_snprintf(params, sizeof(params), " peer_go=%d",
2365 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2366 else
2367 params[0] = '\0';
2368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002370 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002371 else if (config_methods & WPS_CONFIG_KEYPAD) {
2372 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002373 wpas_prov_disc_local_display(wpa_s, peer, params,
2374 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002375 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002376 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2377 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378
Jouni Malinen75ecf522011-06-27 15:19:46 -07002379 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2380 P2P_PROV_DISC_SUCCESS,
2381 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382}
2383
2384
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002385static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2386 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002387{
2388 struct wpa_supplicant *wpa_s = ctx;
2389
Dmitry Shmidt04949592012-07-19 12:16:46 -07002390 if (wpa_s->p2p_fallback_to_go_neg) {
2391 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2392 "failed - fall back to GO Negotiation");
2393 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2394 return;
2395 }
2396
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002397 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002398 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002399 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2400 "join-existing-group operation (no ACK for PD "
2401 "Req attempts)");
2402 wpas_p2p_join_start(wpa_s);
2403 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002404 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002405
Dmitry Shmidt04949592012-07-19 12:16:46 -07002406 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2407 " p2p_dev_addr=" MACSTR " status=%d",
2408 MAC2STR(peer), status);
2409
Jouni Malinen75ecf522011-06-27 15:19:46 -07002410 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2411 status, 0, 0);
2412}
2413
2414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2416 const u8 *go_dev_addr, const u8 *ssid,
2417 size_t ssid_len, int *go, u8 *group_bssid,
2418 int *force_freq, int persistent_group)
2419{
2420 struct wpa_supplicant *wpa_s = ctx;
2421 struct wpa_ssid *s;
2422 u8 cur_bssid[ETH_ALEN];
2423 int res;
2424 struct wpa_supplicant *grp;
2425
2426 if (!persistent_group) {
2427 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2428 " to join an active group", MAC2STR(sa));
2429 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2430 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2431 == 0 ||
2432 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2433 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2434 "authorized invitation");
2435 goto accept_inv;
2436 }
2437 /*
2438 * Do not accept the invitation automatically; notify user and
2439 * request approval.
2440 */
2441 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2442 }
2443
2444 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2445 if (grp) {
2446 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2447 "running persistent group");
2448 if (*go)
2449 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2450 goto accept_inv;
2451 }
2452
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002453 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2454 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2455 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2456 "invitation to re-invoke a persistent group");
2457 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2459
2460 for (s = wpa_s->conf->ssid; s; s = s->next) {
2461 if (s->disabled == 2 &&
2462 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2463 s->ssid_len == ssid_len &&
2464 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2465 break;
2466 }
2467
2468 if (!s) {
2469 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2470 " requested reinvocation of an unknown group",
2471 MAC2STR(sa));
2472 return P2P_SC_FAIL_UNKNOWN_GROUP;
2473 }
2474
2475 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2476 *go = 1;
2477 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2478 wpa_printf(MSG_DEBUG, "P2P: The only available "
2479 "interface is already in use - reject "
2480 "invitation");
2481 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2482 }
2483 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2484 } else if (s->mode == WPAS_MODE_P2P_GO) {
2485 *go = 1;
2486 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2487 {
2488 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2489 "interface address for the group");
2490 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2491 }
2492 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2493 ETH_ALEN);
2494 }
2495
2496accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002497 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2498
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2500 wpa_s->assoc_freq) {
2501 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2502 "the channel we are already using");
2503 *force_freq = wpa_s->assoc_freq;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002504 wpas_p2p_set_own_freq_preference(wpa_s, wpa_s->assoc_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505 }
2506
2507 res = wpa_drv_shared_freq(wpa_s);
2508 if (res > 0) {
2509 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2510 "with the channel we are already using on a "
2511 "shared interface");
2512 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002513 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 }
2515
2516 return P2P_SC_SUCCESS;
2517}
2518
2519
2520static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2521 const u8 *ssid, size_t ssid_len,
2522 const u8 *go_dev_addr, u8 status,
2523 int op_freq)
2524{
2525 struct wpa_supplicant *wpa_s = ctx;
2526 struct wpa_ssid *s;
2527
2528 for (s = wpa_s->conf->ssid; s; s = s->next) {
2529 if (s->disabled == 2 &&
2530 s->ssid_len == ssid_len &&
2531 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2532 break;
2533 }
2534
2535 if (status == P2P_SC_SUCCESS) {
2536 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2537 " was accepted; op_freq=%d MHz",
2538 MAC2STR(sa), op_freq);
2539 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002540 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002542 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002544 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002546 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002547 }
2548 return;
2549 }
2550
2551 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2552 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2553 " was rejected (status %u)", MAC2STR(sa), status);
2554 return;
2555 }
2556
2557 if (!s) {
2558 if (bssid) {
2559 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2560 "sa=" MACSTR " go_dev_addr=" MACSTR
2561 " bssid=" MACSTR " unknown-network",
2562 MAC2STR(sa), MAC2STR(go_dev_addr),
2563 MAC2STR(bssid));
2564 } else {
2565 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2566 "sa=" MACSTR " go_dev_addr=" MACSTR
2567 " unknown-network",
2568 MAC2STR(sa), MAC2STR(go_dev_addr));
2569 }
2570 return;
2571 }
2572
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002573 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2574 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2575 MACSTR " persistent=%d freq=%d",
2576 MAC2STR(sa), s->id, op_freq);
2577 } else {
2578 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2579 MACSTR " persistent=%d", MAC2STR(sa), s->id);
2580 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581}
2582
2583
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002584static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2585 struct wpa_ssid *ssid,
2586 const u8 *peer)
2587{
2588 size_t i;
2589
2590 if (ssid == NULL)
2591 return;
2592
2593 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2594 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2595 ETH_ALEN) == 0)
2596 break;
2597 }
2598 if (i >= ssid->num_p2p_clients) {
2599 if (ssid->mode != WPAS_MODE_P2P_GO &&
2600 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2601 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2602 "due to invitation result", ssid->id);
2603 wpas_notify_network_removed(wpa_s, ssid);
2604 wpa_config_remove_network(wpa_s->conf, ssid->id);
2605 return;
2606 }
2607 return; /* Peer not found in client list */
2608 }
2609
2610 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2611 "group %d client list due to invitation result",
2612 MAC2STR(peer), ssid->id);
2613 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2614 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2615 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2616 ssid->num_p2p_clients--;
2617#ifndef CONFIG_NO_CONFIG_WRITE
2618 if (wpa_s->parent->conf->update_config &&
2619 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2620 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2621#endif /* CONFIG_NO_CONFIG_WRITE */
2622}
2623
2624
2625static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2626 const u8 *peer)
2627{
2628 struct wpa_ssid *ssid;
2629
2630 wpa_s = wpa_s->global->p2p_invite_group;
2631 if (wpa_s == NULL)
2632 return; /* No known invitation group */
2633 ssid = wpa_s->current_ssid;
2634 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2635 !ssid->p2p_persistent_group)
2636 return; /* Not operating as a GO in persistent group */
2637 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2638 ssid->ssid, ssid->ssid_len);
2639 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2640}
2641
2642
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002643static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002644 const struct p2p_channels *channels,
2645 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646{
2647 struct wpa_supplicant *wpa_s = ctx;
2648 struct wpa_ssid *ssid;
2649
2650 if (bssid) {
2651 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2652 "status=%d " MACSTR,
2653 status, MAC2STR(bssid));
2654 } else {
2655 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2656 "status=%d ", status);
2657 }
2658 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2659
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002660 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2661 status, MAC2STR(peer));
2662 if (wpa_s->pending_invite_ssid_id == -1) {
2663 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2664 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002665 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002666 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002668 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2669 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2670 "invitation exchange to indicate readiness for "
2671 "re-invocation");
2672 }
2673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002675 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2676 ssid = wpa_config_get_network(
2677 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2678 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2679 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002680 wpas_p2p_remove_pending_group_interface(wpa_s);
2681 return;
2682 }
2683
2684 ssid = wpa_config_get_network(wpa_s->conf,
2685 wpa_s->pending_invite_ssid_id);
2686 if (ssid == NULL) {
2687 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2688 "data matching with invitation");
2689 return;
2690 }
2691
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002692 /*
2693 * The peer could have missed our ctrl::ack frame for Invitation
2694 * Response and continue retransmitting the frame. To reduce the
2695 * likelihood of the peer not getting successful TX status for the
2696 * Invitation Response frame, wait a short time here before starting
2697 * the persistent group so that we will remain on the current channel to
2698 * acknowledge any possible retransmission from the peer.
2699 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002700#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002701 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2702 "starting persistent group");
2703 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002704#else
2705 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2706 "starting persistent group");
2707 os_sleep(0, 100000);
2708#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002711 ssid->mode == WPAS_MODE_P2P_GO,
2712 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002713 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002714}
2715
2716
Dmitry Shmidt04949592012-07-19 12:16:46 -07002717static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2718 unsigned int freq)
2719{
2720 unsigned int i;
2721
2722 if (global->p2p_disallow_freq == NULL)
2723 return 0;
2724
2725 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2726 if (freq >= global->p2p_disallow_freq[i].min &&
2727 freq <= global->p2p_disallow_freq[i].max)
2728 return 1;
2729 }
2730
2731 return 0;
2732}
2733
2734
2735static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2736{
2737 reg->channel[reg->channels] = chan;
2738 reg->channels++;
2739}
2740
2741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2743 struct p2p_channels *chan)
2744{
2745 int i, cla = 0;
2746
2747 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2748 "band");
2749
2750 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2751 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002752 chan->reg_class[cla].channels = 0;
2753 for (i = 0; i < 11; i++) {
2754 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2755 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2756 }
2757 if (chan->reg_class[cla].channels)
2758 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002759
2760 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2761 "band");
2762
2763 /* Operating class 115 - 5 GHz, channels 36-48 */
2764 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002765 chan->reg_class[cla].channels = 0;
2766 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2767 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2768 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2769 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2770 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2771 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2772 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2773 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2774 if (chan->reg_class[cla].channels)
2775 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002776
2777 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2778 "band");
2779
2780 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2781 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002782 chan->reg_class[cla].channels = 0;
2783 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2784 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2785 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2786 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2787 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2788 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2789 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2790 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2791 if (chan->reg_class[cla].channels)
2792 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793
2794 chan->reg_classes = cla;
2795 return 0;
2796}
2797
2798
2799static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2800 u16 num_modes,
2801 enum hostapd_hw_mode mode)
2802{
2803 u16 i;
2804
2805 for (i = 0; i < num_modes; i++) {
2806 if (modes[i].mode == mode)
2807 return &modes[i];
2808 }
2809
2810 return NULL;
2811}
2812
2813
Dmitry Shmidt04949592012-07-19 12:16:46 -07002814static int has_channel(struct wpa_global *global,
2815 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816{
2817 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002818 unsigned int freq;
2819
2820 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2821 chan * 5;
2822 if (wpas_p2p_disallowed_freq(global, freq))
2823 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824
2825 for (i = 0; i < mode->num_channels; i++) {
2826 if (mode->channels[i].chan == chan) {
2827 if (flags)
2828 *flags = mode->channels[i].flag;
2829 return !(mode->channels[i].flag &
2830 (HOSTAPD_CHAN_DISABLED |
2831 HOSTAPD_CHAN_PASSIVE_SCAN |
2832 HOSTAPD_CHAN_NO_IBSS |
2833 HOSTAPD_CHAN_RADAR));
2834 }
2835 }
2836
2837 return 0;
2838}
2839
2840
2841struct p2p_oper_class_map {
2842 enum hostapd_hw_mode mode;
2843 u8 op_class;
2844 u8 min_chan;
2845 u8 max_chan;
2846 u8 inc;
2847 enum { BW20, BW40PLUS, BW40MINUS } bw;
2848};
2849
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002850static struct p2p_oper_class_map op_class[] = {
2851 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002852#if 0 /* Do not enable HT40 on 2 GHz for now */
2853 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2854 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2855#endif
2856 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2857 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2858 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2859 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2860 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2861 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2862 { -1, 0, 0, 0, 0, BW20 }
2863};
2864
2865
2866static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2867 struct hostapd_hw_modes *mode,
2868 u8 channel, u8 bw)
2869{
2870 int flag;
2871
2872 if (!has_channel(wpa_s->global, mode, channel, &flag))
2873 return -1;
2874 if (bw == BW40MINUS &&
2875 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2876 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2877 return 0;
2878 if (bw == BW40PLUS &&
2879 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2880 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2881 return 0;
2882 return 1;
2883}
2884
2885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2887 struct p2p_channels *chan)
2888{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002889 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002890 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002892 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2894 "of all supported channels; assume dualband "
2895 "support");
2896 return wpas_p2p_default_channels(wpa_s, chan);
2897 }
2898
2899 cla = 0;
2900
2901 for (op = 0; op_class[op].op_class; op++) {
2902 struct p2p_oper_class_map *o = &op_class[op];
2903 u8 ch;
2904 struct p2p_reg_class *reg = NULL;
2905
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002906 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002907 if (mode == NULL)
2908 continue;
2909 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002910 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 continue;
2912 if (reg == NULL) {
2913 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2914 "class %u", o->op_class);
2915 reg = &chan->reg_class[cla];
2916 cla++;
2917 reg->reg_class = o->op_class;
2918 }
2919 reg->channel[reg->channels] = ch;
2920 reg->channels++;
2921 }
2922 if (reg) {
2923 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2924 reg->channel, reg->channels);
2925 }
2926 }
2927
2928 chan->reg_classes = cla;
2929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930 return 0;
2931}
2932
2933
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002934int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2935 struct hostapd_hw_modes *mode, u8 channel)
2936{
2937 int op, ret;
2938
2939 for (op = 0; op_class[op].op_class; op++) {
2940 struct p2p_oper_class_map *o = &op_class[op];
2941 u8 ch;
2942
2943 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2944 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2945 o->bw == BW20 || ch != channel)
2946 continue;
2947 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2948 if (ret < 0)
2949 continue;
2950 else if (ret > 0)
2951 return (o->bw == BW40MINUS) ? -1 : 1;
2952 else
2953 return 0;
2954 }
2955 }
2956 return 0;
2957}
2958
2959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2961 size_t buf_len)
2962{
2963 struct wpa_supplicant *wpa_s = ctx;
2964
2965 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2966 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2967 break;
2968 }
2969 if (wpa_s == NULL)
2970 return -1;
2971
2972 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2973}
2974
2975
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002976static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2977{
2978 struct wpa_supplicant *wpa_s = ctx;
2979
2980 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2981 struct wpa_ssid *ssid = wpa_s->current_ssid;
2982 if (ssid == NULL)
2983 continue;
2984 if (ssid->mode != WPAS_MODE_INFRA)
2985 continue;
2986 if (wpa_s->wpa_state != WPA_COMPLETED &&
2987 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2988 continue;
2989 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2990 return 1;
2991 }
2992
2993 return 0;
2994}
2995
2996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997/**
2998 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2999 * @global: Pointer to global data from wpa_supplicant_init()
3000 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3001 * Returns: 0 on success, -1 on failure
3002 */
3003int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3004{
3005 struct p2p_config p2p;
3006 unsigned int r;
3007 int i;
3008
3009 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3010 return 0;
3011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003012 if (global->p2p)
3013 return 0;
3014
3015 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3016 struct p2p_params params;
3017
3018 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3019 os_memset(&params, 0, sizeof(params));
3020 params.dev_name = wpa_s->conf->device_name;
3021 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3022 WPS_DEV_TYPE_LEN);
3023 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3024 os_memcpy(params.sec_dev_type,
3025 wpa_s->conf->sec_device_type,
3026 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3027
3028 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3029 return -1;
3030
3031 return 0;
3032 }
3033
3034 os_memset(&p2p, 0, sizeof(p2p));
3035 p2p.msg_ctx = wpa_s;
3036 p2p.cb_ctx = wpa_s;
3037 p2p.p2p_scan = wpas_p2p_scan;
3038 p2p.send_action = wpas_send_action;
3039 p2p.send_action_done = wpas_send_action_done;
3040 p2p.go_neg_completed = wpas_go_neg_completed;
3041 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3042 p2p.dev_found = wpas_dev_found;
3043 p2p.dev_lost = wpas_dev_lost;
3044 p2p.start_listen = wpas_start_listen;
3045 p2p.stop_listen = wpas_stop_listen;
3046 p2p.send_probe_resp = wpas_send_probe_resp;
3047 p2p.sd_request = wpas_sd_request;
3048 p2p.sd_response = wpas_sd_response;
3049 p2p.prov_disc_req = wpas_prov_disc_req;
3050 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003051 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052 p2p.invitation_process = wpas_invitation_process;
3053 p2p.invitation_received = wpas_invitation_received;
3054 p2p.invitation_result = wpas_invitation_result;
3055 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003056 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003057
3058 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003059 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060 p2p.dev_name = wpa_s->conf->device_name;
3061 p2p.manufacturer = wpa_s->conf->manufacturer;
3062 p2p.model_name = wpa_s->conf->model_name;
3063 p2p.model_number = wpa_s->conf->model_number;
3064 p2p.serial_number = wpa_s->conf->serial_number;
3065 if (wpa_s->wps) {
3066 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3067 p2p.config_methods = wpa_s->wps->config_methods;
3068 }
3069
3070 if (wpa_s->conf->p2p_listen_reg_class &&
3071 wpa_s->conf->p2p_listen_channel) {
3072 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3073 p2p.channel = wpa_s->conf->p2p_listen_channel;
3074 } else {
3075 p2p.reg_class = 81;
3076 /*
3077 * Pick one of the social channels randomly as the listen
3078 * channel.
3079 */
3080 os_get_random((u8 *) &r, sizeof(r));
3081 p2p.channel = 1 + (r % 3) * 5;
3082 }
3083 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3084
3085 if (wpa_s->conf->p2p_oper_reg_class &&
3086 wpa_s->conf->p2p_oper_channel) {
3087 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3088 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3089 p2p.cfg_op_channel = 1;
3090 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3091 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3092
3093 } else {
3094 p2p.op_reg_class = 81;
3095 /*
3096 * Use random operation channel from (1, 6, 11) if no other
3097 * preference is indicated.
3098 */
3099 os_get_random((u8 *) &r, sizeof(r));
3100 p2p.op_channel = 1 + (r % 3) * 5;
3101 p2p.cfg_op_channel = 0;
3102 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3103 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3104 }
3105 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3106 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3107 p2p.country[2] = 0x04;
3108 } else
3109 os_memcpy(p2p.country, "XX\x04", 3);
3110
3111 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3112 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3113 "channel list");
3114 return -1;
3115 }
3116
3117 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3118 WPS_DEV_TYPE_LEN);
3119
3120 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3121 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3122 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3123
3124 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3125 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3126
3127 p2p.max_peers = 100;
3128
3129 if (wpa_s->conf->p2p_ssid_postfix) {
3130 p2p.ssid_postfix_len =
3131 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3132 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3133 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3134 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3135 p2p.ssid_postfix_len);
3136 }
3137
3138 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3139
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003140 p2p.max_listen = wpa_s->max_remain_on_chan;
3141
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003142#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003143 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003144 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003145 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3146 } else {
3147 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003148 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003149 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3150 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003151#endif
3152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003153 global->p2p = p2p_init(&p2p);
3154 if (global->p2p == NULL)
3155 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003156 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003157
3158 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3159 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3160 continue;
3161 p2p_add_wps_vendor_extension(
3162 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3163 }
3164
3165 return 0;
3166}
3167
3168
3169/**
3170 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3171 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3172 *
3173 * This function deinitialize per-interface P2P data.
3174 */
3175void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3176{
3177 if (wpa_s->driver && wpa_s->drv_priv)
3178 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003179
3180 if (wpa_s->go_params) {
3181 /* Clear any stored provisioning info */
3182 p2p_clear_provisioning_info(
3183 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003184 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003185 }
3186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187 os_free(wpa_s->go_params);
3188 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003189#ifdef ANDROID_P2P
3190 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3191#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3193 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3194 wpa_s->p2p_long_listen = 0;
3195 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3196 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3197 wpas_p2p_remove_pending_group_interface(wpa_s);
3198
3199 /* TODO: remove group interface from the driver if this wpa_s instance
3200 * is on top of a P2P group interface */
3201}
3202
3203
3204/**
3205 * wpas_p2p_deinit_global - Deinitialize global P2P module
3206 * @global: Pointer to global data from wpa_supplicant_init()
3207 *
3208 * This function deinitializes the global (per device) P2P module.
3209 */
3210void wpas_p2p_deinit_global(struct wpa_global *global)
3211{
3212 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003214 wpa_s = global->ifaces;
3215 if (wpa_s)
3216 wpas_p2p_service_flush(wpa_s);
3217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003218 if (global->p2p == NULL)
3219 return;
3220
3221 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3223 wpa_s = wpa_s->next;
3224 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225 tmp = global->ifaces;
3226 while (tmp &&
3227 (tmp == wpa_s ||
3228 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3229 tmp = tmp->next;
3230 }
3231 if (tmp == NULL)
3232 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003233 /* Disconnect from the P2P group and deinit the interface */
3234 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235 }
3236
3237 /*
3238 * Deinit GO data on any possibly remaining interface (if main
3239 * interface is used as GO).
3240 */
3241 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3242 if (wpa_s->ap_iface)
3243 wpas_p2p_group_deinit(wpa_s);
3244 }
3245
3246 p2p_deinit(global->p2p);
3247 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003248 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249}
3250
3251
3252static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3253{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003254 if (wpa_s->conf->p2p_no_group_iface)
3255 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256 if (wpa_s->drv_flags &
3257 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3258 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3259 return 1; /* P2P group requires a new interface in every case
3260 */
3261 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3262 return 0; /* driver does not support concurrent operations */
3263 if (wpa_s->global->ifaces->next)
3264 return 1; /* more that one interface already in use */
3265 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3266 return 1; /* this interface is already in use */
3267 return 0;
3268}
3269
3270
3271static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3272 const u8 *peer_addr,
3273 enum p2p_wps_method wps_method,
3274 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003275 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003276 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003278 if (persistent_group && wpa_s->conf->persistent_reconnect)
3279 persistent_group = 2;
3280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003281 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3282 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3283 go_intent, own_interface_addr,
3284 force_freq, persistent_group);
3285 }
3286
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003287 /*
3288 * Increase GO config timeout if HT40 is used since it takes some time
3289 * to scan channels for coex purposes before the BSS can be started.
3290 */
3291 p2p_set_config_timeout(wpa_s->global->p2p,
3292 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003294 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3295 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003296 persistent_group, ssid ? ssid->ssid : NULL,
3297 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003298 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003299}
3300
3301
3302static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3303 const u8 *peer_addr,
3304 enum p2p_wps_method wps_method,
3305 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003306 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003307 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003308{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003309 if (persistent_group && wpa_s->conf->persistent_reconnect)
3310 persistent_group = 2;
3311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003312 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3313 return -1;
3314
3315 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3316 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003317 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003318 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003319}
3320
3321
3322static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3323{
3324 wpa_s->p2p_join_scan_count++;
3325 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3326 wpa_s->p2p_join_scan_count);
3327 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3328 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3329 " for join operationg - stop join attempt",
3330 MAC2STR(wpa_s->pending_join_iface_addr));
3331 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003332 if (wpa_s->p2p_auto_pd) {
3333 wpa_s->p2p_auto_pd = 0;
3334 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3335 " p2p_dev_addr=" MACSTR " status=N/A",
3336 MAC2STR(wpa_s->pending_join_dev_addr));
3337 return;
3338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339 wpa_msg(wpa_s->parent, MSG_INFO,
3340 P2P_EVENT_GROUP_FORMATION_FAILURE);
3341 }
3342}
3343
3344
Dmitry Shmidt04949592012-07-19 12:16:46 -07003345static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3346{
3347 struct wpa_supplicant *iface;
3348 int shared_freq;
3349 u8 bssid[ETH_ALEN];
3350
3351 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3352 return 0;
3353
3354 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3355 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3356 continue;
3357 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3358 continue;
3359 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3360 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3361 shared_freq = iface->current_ssid->frequency;
3362 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3363 shared_freq = iface->assoc_freq;
3364 else
3365 shared_freq = 0;
3366
3367 if (shared_freq && freq != shared_freq) {
3368 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3369 "connected on %d MHz - new connection on "
3370 "%d MHz", iface->ifname, shared_freq, freq);
3371 return 1;
3372 }
3373 }
3374
3375 shared_freq = wpa_drv_shared_freq(wpa_s);
3376 if (shared_freq > 0 && shared_freq != freq) {
3377 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3378 "virtual interface connected on %d MHz - new "
3379 "connection on %d MHz", shared_freq, freq);
3380 return 1;
3381 }
3382
3383 return 0;
3384}
3385
3386
3387static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3388 const u8 *peer_dev_addr)
3389{
3390 struct wpa_bss *bss;
3391 int updated;
3392
3393 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3394 if (bss == NULL)
3395 return -1;
3396 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3397 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3398 "last scan");
3399 return 0;
3400 }
3401
3402 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3403 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3404 "%ld.%06ld (%supdated in last scan)",
3405 bss->last_update.sec, bss->last_update.usec,
3406 updated ? "": "not ");
3407
3408 return updated;
3409}
3410
3411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003412static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3413 struct wpa_scan_results *scan_res)
3414{
3415 struct wpa_bss *bss;
3416 int freq;
3417 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3420
3421 if (wpa_s->global->p2p_disabled)
3422 return;
3423
Dmitry Shmidt04949592012-07-19 12:16:46 -07003424 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3425 scan_res ? (int) scan_res->num : -1,
3426 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427
3428 if (scan_res)
3429 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3430
Dmitry Shmidt04949592012-07-19 12:16:46 -07003431 if (wpa_s->p2p_auto_pd) {
3432 int join = wpas_p2p_peer_go(wpa_s,
3433 wpa_s->pending_join_dev_addr);
3434 if (join == 0 &&
3435 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3436 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003437 bss = wpa_bss_get_bssid_latest(
3438 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003439 if (bss) {
3440 freq = bss->freq;
3441 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3442 "the peer " MACSTR " at %d MHz",
3443 wpa_s->auto_pd_scan_retry,
3444 MAC2STR(wpa_s->
3445 pending_join_dev_addr),
3446 freq);
3447 wpas_p2p_join_scan_req(wpa_s, freq);
3448 return;
3449 }
3450 }
3451
3452 if (join < 0)
3453 join = 0;
3454
3455 wpa_s->p2p_auto_pd = 0;
3456 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3457 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3458 MAC2STR(wpa_s->pending_join_dev_addr), join);
3459 if (p2p_prov_disc_req(wpa_s->global->p2p,
3460 wpa_s->pending_join_dev_addr,
3461 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003462 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003463 wpa_s->p2p_auto_pd = 0;
3464 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3465 " p2p_dev_addr=" MACSTR " status=N/A",
3466 MAC2STR(wpa_s->pending_join_dev_addr));
3467 }
3468 return;
3469 }
3470
3471 if (wpa_s->p2p_auto_join) {
3472 int join = wpas_p2p_peer_go(wpa_s,
3473 wpa_s->pending_join_dev_addr);
3474 if (join < 0) {
3475 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3476 "running a GO -> use GO Negotiation");
3477 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3478 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3479 wpa_s->p2p_persistent_group, 0, 0, 0,
3480 wpa_s->p2p_go_intent,
3481 wpa_s->p2p_connect_freq,
3482 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003483 wpa_s->p2p_pd_before_go_neg,
3484 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003485 return;
3486 }
3487
3488 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3489 "try to join the group", join ? "" :
3490 " in older scan");
3491 if (!join)
3492 wpa_s->p2p_fallback_to_go_neg = 1;
3493 }
3494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3496 wpa_s->pending_join_iface_addr);
3497 if (freq < 0 &&
3498 p2p_get_interface_addr(wpa_s->global->p2p,
3499 wpa_s->pending_join_dev_addr,
3500 iface_addr) == 0 &&
3501 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3502 {
3503 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3504 "address for join from " MACSTR " to " MACSTR
3505 " based on newly discovered P2P peer entry",
3506 MAC2STR(wpa_s->pending_join_iface_addr),
3507 MAC2STR(iface_addr));
3508 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3509 ETH_ALEN);
3510
3511 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3512 wpa_s->pending_join_iface_addr);
3513 }
3514 if (freq >= 0) {
3515 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3516 "from P2P peer table: %d MHz", freq);
3517 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003518 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003519 if (bss) {
3520 freq = bss->freq;
3521 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3522 "from BSS table: %d MHz", freq);
3523 }
3524 if (freq > 0) {
3525 u16 method;
3526
Dmitry Shmidt04949592012-07-19 12:16:46 -07003527 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3528 wpa_msg(wpa_s->parent, MSG_INFO,
3529 P2P_EVENT_GROUP_FORMATION_FAILURE
3530 "reason=FREQ_CONFLICT");
3531 return;
3532 }
3533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3535 "prior to joining an existing group (GO " MACSTR
3536 " freq=%u MHz)",
3537 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3538 wpa_s->pending_pd_before_join = 1;
3539
3540 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003541 case WPS_PIN_DISPLAY:
3542 method = WPS_CONFIG_KEYPAD;
3543 break;
3544 case WPS_PIN_KEYPAD:
3545 method = WPS_CONFIG_DISPLAY;
3546 break;
3547 case WPS_PBC:
3548 method = WPS_CONFIG_PUSHBUTTON;
3549 break;
3550 default:
3551 method = 0;
3552 break;
3553 }
3554
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003555 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3556 wpa_s->pending_join_dev_addr) ==
3557 method)) {
3558 /*
3559 * We have already performed provision discovery for
3560 * joining the group. Proceed directly to join
3561 * operation without duplicated provision discovery. */
3562 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3563 "with " MACSTR " already done - proceed to "
3564 "join",
3565 MAC2STR(wpa_s->pending_join_dev_addr));
3566 wpa_s->pending_pd_before_join = 0;
3567 goto start;
3568 }
3569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003570 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003571 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003572 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003573 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3574 "Discovery Request before joining an "
3575 "existing group");
3576 wpa_s->pending_pd_before_join = 0;
3577 goto start;
3578 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 return;
3580 }
3581
3582 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3583 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3584 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3585 wpas_p2p_check_join_scan_limit(wpa_s);
3586 return;
3587
3588start:
3589 /* Start join operation immediately */
3590 wpas_p2p_join_start(wpa_s);
3591}
3592
3593
Dmitry Shmidt04949592012-07-19 12:16:46 -07003594static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003596 int ret;
3597 struct wpa_driver_scan_params params;
3598 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003599 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003600 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003601#ifdef ANDROID_P2P
3602 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003604 /* If freq is not provided, check the operating freq of the GO and do a
3605 * a directed scan to save time
3606 */
3607 if(!freq) {
3608 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3609 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3610 }
3611#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003612 os_memset(&params, 0, sizeof(params));
3613
3614 /* P2P Wildcard SSID */
3615 params.num_ssids = 1;
3616 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3617 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3618
3619 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003620 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3621 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3622 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 if (wps_ie == NULL) {
3624 wpas_p2p_scan_res_join(wpa_s, NULL);
3625 return;
3626 }
3627
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003628 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3629 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630 if (ies == NULL) {
3631 wpabuf_free(wps_ie);
3632 wpas_p2p_scan_res_join(wpa_s, NULL);
3633 return;
3634 }
3635 wpabuf_put_buf(ies, wps_ie);
3636 wpabuf_free(wps_ie);
3637
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003638 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003640 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003641 params.extra_ies = wpabuf_head(ies);
3642 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003643 if (freq > 0) {
3644 freqs[0] = freq;
3645 params.freqs = freqs;
3646 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003647
3648 /*
3649 * Run a scan to update BSS table and start Provision Discovery once
3650 * the new scan results become available.
3651 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003652 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003653 if (!ret) {
3654 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003655 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003656 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003657
3658 wpabuf_free(ies);
3659
3660 if (ret) {
3661 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3662 "try again later");
3663 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3664 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3665 wpas_p2p_check_join_scan_limit(wpa_s);
3666 }
3667}
3668
3669
Dmitry Shmidt04949592012-07-19 12:16:46 -07003670static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3671{
3672 struct wpa_supplicant *wpa_s = eloop_ctx;
3673 wpas_p2p_join_scan_req(wpa_s, 0);
3674}
3675
3676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003677static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003678 const u8 *dev_addr, enum p2p_wps_method wps_method,
3679 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680{
3681 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003682 MACSTR " dev " MACSTR ")%s",
3683 MAC2STR(iface_addr), MAC2STR(dev_addr),
3684 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003685
Dmitry Shmidt04949592012-07-19 12:16:46 -07003686 wpa_s->p2p_auto_pd = 0;
3687 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003688 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3689 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3690 wpa_s->pending_join_wps_method = wps_method;
3691
3692 /* Make sure we are not running find during connection establishment */
3693 wpas_p2p_stop_find(wpa_s);
3694
3695 wpa_s->p2p_join_scan_count = 0;
3696 wpas_p2p_join_scan(wpa_s, NULL);
3697 return 0;
3698}
3699
3700
3701static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3702{
3703 struct wpa_supplicant *group;
3704 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003705 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003706
3707 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3708 if (group == NULL)
3709 return -1;
3710 if (group != wpa_s) {
3711 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3712 sizeof(group->p2p_pin));
3713 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003714 } else {
3715 /*
3716 * Need to mark the current interface for p2p_group_formation
3717 * when a separate group interface is not used. This is needed
3718 * to allow p2p_cancel stop a pending p2p_connect-join.
3719 * wpas_p2p_init_group_interface() addresses this for the case
3720 * where a separate group interface is used.
3721 */
3722 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003723 }
3724
3725 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003726 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727
3728 os_memset(&res, 0, sizeof(res));
3729 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3730 ETH_ALEN);
3731 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003732 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003733 if (bss) {
3734 res.freq = bss->freq;
3735 res.ssid_len = bss->ssid_len;
3736 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3737 }
3738
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003739 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3740 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3741 "starting client");
3742 wpa_drv_cancel_remain_on_channel(wpa_s);
3743 wpa_s->off_channel_freq = 0;
3744 wpa_s->roc_waiting_drv_freq = 0;
3745 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746 wpas_start_wps_enrollee(group, &res);
3747
3748 /*
3749 * Allow a longer timeout for join-a-running-group than normal 15
3750 * second group formation timeout since the GO may not have authorized
3751 * our connection yet.
3752 */
3753 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3754 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3755 wpa_s, NULL);
3756
3757 return 0;
3758}
3759
3760
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003761static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3762 int *force_freq, int *pref_freq,
3763 int *oper_freq)
3764{
3765 if (freq > 0) {
3766 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3767 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3768 "(%u MHz) is not supported for P2P uses",
3769 freq);
3770 return -3;
3771 }
3772
3773 if (*oper_freq > 0 && freq != *oper_freq &&
3774 !(wpa_s->drv_flags &
3775 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3776 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3777 "on %u MHz while connected on another "
3778 "channel (%u MHz)", freq, *oper_freq);
3779 return -2;
3780 }
3781 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3782 "requested channel (%u MHz)", freq);
3783 *force_freq = freq;
3784 } else if (*oper_freq > 0 &&
3785 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3786 if (!(wpa_s->drv_flags &
3787 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3788 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3789 "while connected on non-P2P supported "
3790 "channel (%u MHz)", *oper_freq);
3791 return -2;
3792 }
3793 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3794 "(%u MHz) not available for P2P - try to use "
3795 "another channel", *oper_freq);
3796 *force_freq = 0;
3797 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3798 (wpa_s->drv_flags &
3799 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3800 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3801 "are already using (%u MHz) on another interface",
3802 *oper_freq);
3803 *pref_freq = *oper_freq;
3804 } else if (*oper_freq > 0) {
3805 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3806 "channel we are already using (%u MHz) on another "
3807 "interface", *oper_freq);
3808 *force_freq = *oper_freq;
3809 }
3810
3811 return 0;
3812}
3813
3814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003815/**
3816 * wpas_p2p_connect - Request P2P Group Formation to be started
3817 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3818 * @peer_addr: Address of the peer P2P Device
3819 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3820 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003821 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003822 * @join: Whether to join an existing group (as a client) instead of starting
3823 * Group Owner negotiation; @peer_addr is BSSID in that case
3824 * @auth: Whether to only authorize the connection instead of doing that and
3825 * initiating Group Owner negotiation
3826 * @go_intent: GO Intent or -1 to use default
3827 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003828 * @persistent_id: Persistent group credentials to use for forcing GO
3829 * parameters or -1 to generate new values (SSID/passphrase)
3830 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3831 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003832 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003833 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3834 * failure, -2 on failure due to channel not currently available,
3835 * -3 if forced channel is not supported
3836 */
3837int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3838 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003839 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003840 int go_intent, int freq, int persistent_id, int pd,
3841 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003842{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003843 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844 u8 bssid[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003845 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003846 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003847 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003848 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849
3850 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3851 return -1;
3852
Dmitry Shmidt04949592012-07-19 12:16:46 -07003853 if (persistent_id >= 0) {
3854 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3855 if (ssid == NULL || ssid->disabled != 2 ||
3856 ssid->mode != WPAS_MODE_P2P_GO)
3857 return -1;
3858 }
3859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003860 if (go_intent < 0)
3861 go_intent = wpa_s->conf->p2p_go_intent;
3862
3863 if (!auth)
3864 wpa_s->p2p_long_listen = 0;
3865
3866 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003867 wpa_s->p2p_persistent_group = !!persistent_group;
3868 wpa_s->p2p_persistent_id = persistent_id;
3869 wpa_s->p2p_go_intent = go_intent;
3870 wpa_s->p2p_connect_freq = freq;
3871 wpa_s->p2p_fallback_to_go_neg = 0;
3872 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003873 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003874
3875 if (pin)
3876 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3877 else if (wps_method == WPS_PIN_DISPLAY) {
3878 ret = wps_generate_pin();
3879 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3880 ret);
3881 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3882 wpa_s->p2p_pin);
3883 } else
3884 wpa_s->p2p_pin[0] = '\0';
3885
Dmitry Shmidt04949592012-07-19 12:16:46 -07003886 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003887 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3888 if (auth) {
3889 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3890 "connect a running group from " MACSTR,
3891 MAC2STR(peer_addr));
3892 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3893 return ret;
3894 }
3895 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3896 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3897 iface_addr) < 0) {
3898 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3899 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3900 dev_addr);
3901 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003902 if (auto_join) {
3903 os_get_time(&wpa_s->p2p_auto_started);
3904 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3905 "%ld.%06ld",
3906 wpa_s->p2p_auto_started.sec,
3907 wpa_s->p2p_auto_started.usec);
3908 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003909 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003910 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3911 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 return -1;
3913 return ret;
3914 }
3915
3916 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003917 wpa_s->assoc_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003919 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003920 oper_freq = wpa_drv_shared_freq(wpa_s);
3921 if (oper_freq < 0)
3922 oper_freq = 0;
3923 }
3924
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003925 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
3926 &oper_freq);
3927 if (res)
3928 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003929 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003930
3931 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3932
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003933 if (wpa_s->create_p2p_iface) {
3934 /* Prepare to add a new interface for the group */
3935 iftype = WPA_IF_P2P_GROUP;
3936 if (go_intent == 15)
3937 iftype = WPA_IF_P2P_GO;
3938 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3939 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3940 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003942 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003943
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003944 if_addr = wpa_s->pending_interface_addr;
3945 } else
3946 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947
3948 if (auth) {
3949 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003950 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003951 force_freq, persistent_group, ssid,
3952 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953 return -1;
3954 return ret;
3955 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003956
3957 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3958 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003959 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003960 if (wpa_s->create_p2p_iface)
3961 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003962 return -1;
3963 }
3964 return ret;
3965}
3966
3967
3968/**
3969 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3970 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3971 * @freq: Frequency of the channel in MHz
3972 * @duration: Duration of the stay on the channel in milliseconds
3973 *
3974 * This callback is called when the driver indicates that it has started the
3975 * requested remain-on-channel duration.
3976 */
3977void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3978 unsigned int freq, unsigned int duration)
3979{
3980 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3981 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3983 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3984 wpa_s->pending_listen_duration);
3985 wpa_s->pending_listen_freq = 0;
3986 }
3987}
3988
3989
3990static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3991 unsigned int timeout)
3992{
3993 /* Limit maximum Listen state time based on driver limitation. */
3994 if (timeout > wpa_s->max_remain_on_chan)
3995 timeout = wpa_s->max_remain_on_chan;
3996
3997 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3998 return wpa_drv_p2p_listen(wpa_s, timeout);
3999
4000 return p2p_listen(wpa_s->global->p2p, timeout);
4001}
4002
4003
4004/**
4005 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4006 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4007 * @freq: Frequency of the channel in MHz
4008 *
4009 * This callback is called when the driver indicates that a remain-on-channel
4010 * operation has been completed, i.e., the duration on the requested channel
4011 * has timed out.
4012 */
4013void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4014 unsigned int freq)
4015{
4016 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4017 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004018 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004019 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4020 return;
4021 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4022 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004023 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004024 return;
4025 if (wpa_s->p2p_long_listen > 0)
4026 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4027 if (wpa_s->p2p_long_listen > 0) {
4028 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4029 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4030 }
4031}
4032
4033
4034/**
4035 * wpas_p2p_group_remove - Remove a P2P group
4036 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4037 * @ifname: Network interface name of the group interface or "*" to remove all
4038 * groups
4039 * Returns: 0 on success, -1 on failure
4040 *
4041 * This function is used to remove a P2P group. This can be used to disconnect
4042 * from a group in which the local end is a P2P Client or to end a P2P Group in
4043 * case the local end is the Group Owner. If a virtual network interface was
4044 * created for this group, that interface will be removed. Otherwise, only the
4045 * configured P2P group network will be removed from the interface.
4046 */
4047int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4048{
4049 struct wpa_global *global = wpa_s->global;
4050
4051 if (os_strcmp(ifname, "*") == 0) {
4052 struct wpa_supplicant *prev;
4053 wpa_s = global->ifaces;
4054 while (wpa_s) {
4055 prev = wpa_s;
4056 wpa_s = wpa_s->next;
4057 wpas_p2p_disconnect(prev);
4058 }
4059 return 0;
4060 }
4061
4062 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4063 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4064 break;
4065 }
4066
4067 return wpas_p2p_disconnect(wpa_s);
4068}
4069
4070
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004071static int freq_included(const struct p2p_channels *channels, unsigned int freq)
4072{
4073 if (channels == NULL)
4074 return 1; /* Assume no restrictions */
4075 return p2p_channels_includes_freq(channels, freq);
4076}
4077
4078
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004079static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4080 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004081 int freq, int ht40,
4082 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004083{
4084 u8 bssid[ETH_ALEN];
4085 int res;
4086
4087 os_memset(params, 0, sizeof(*params));
4088 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004089 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004091 if (!freq_included(channels, freq)) {
4092 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4093 "accepted", freq);
4094 return -1;
4095 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004096 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4097 "frequency %d MHz", freq);
4098 params->freq = freq;
4099 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4100 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004101 wpa_s->conf->p2p_oper_channel <= 11 &&
4102 freq_included(channels,
4103 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4105 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4106 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004107 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4108 wpa_s->conf->p2p_oper_reg_class == 116 ||
4109 wpa_s->conf->p2p_oper_reg_class == 117 ||
4110 wpa_s->conf->p2p_oper_reg_class == 124 ||
4111 wpa_s->conf->p2p_oper_reg_class == 126 ||
4112 wpa_s->conf->p2p_oper_reg_class == 127) &&
4113 freq_included(channels,
4114 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004115 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4116 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4117 "frequency %d MHz", params->freq);
4118 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4119 wpa_s->best_overall_freq > 0 &&
4120 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004121 wpa_s->best_overall_freq) &&
4122 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123 params->freq = wpa_s->best_overall_freq;
4124 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4125 "channel %d MHz", params->freq);
4126 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4127 wpa_s->best_24_freq > 0 &&
4128 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004129 wpa_s->best_24_freq) &&
4130 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 params->freq = wpa_s->best_24_freq;
4132 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4133 "channel %d MHz", params->freq);
4134 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4135 wpa_s->best_5_freq > 0 &&
4136 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004137 wpa_s->best_5_freq) &&
4138 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139 params->freq = wpa_s->best_5_freq;
4140 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4141 "channel %d MHz", params->freq);
4142 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004143 int chan;
4144 for (chan = 0; chan < 11; chan++) {
4145 params->freq = 2412 + chan * 5;
4146 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004147 params->freq) &&
4148 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004149 break;
4150 }
4151 if (chan == 11) {
4152 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4153 "allowed");
4154 return -1;
4155 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004156 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4157 "known)", params->freq);
4158 }
4159
4160 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4161 wpa_s->assoc_freq && !freq) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004162 if (!p2p_supported_freq(wpa_s->global->p2p, wpa_s->assoc_freq)
4163 || !freq_included(channels, wpa_s->assoc_freq)) {
4164 if (wpa_s->drv_flags &
4165 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4166 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4167 "the channel we are already using "
4168 "(%u MHz) - allow multi-channel "
4169 "concurrency", wpa_s->assoc_freq);
4170 } else {
4171 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4172 "the channel we are already using "
4173 "(%u MHz)", wpa_s->assoc_freq);
4174 return -1;
4175 }
4176 } else {
4177 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we "
4178 "are already using (%u MHz)",
4179 wpa_s->assoc_freq);
4180 params->freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004181 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182 }
4183
4184 res = wpa_drv_shared_freq(wpa_s);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004185 if (res > 0 && !freq &&
4186 (!p2p_supported_freq(wpa_s->global->p2p, res) ||
4187 !freq_included(channels, res))) {
4188 if (wpa_s->drv_flags &
4189 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4190 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4191 "channel we are already using on a shared "
4192 "interface (%u MHz) - allow multi-channel "
4193 "concurrency", res);
4194 } else {
4195 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4196 "channel we are already using on a shared "
4197 "interface (%u MHz)", res);
4198 return -1;
4199 }
4200 } else if (res > 0 && !freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4202 "already using on a shared interface");
4203 params->freq = res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004204 if (!freq_included(channels, params->freq)) {
4205 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4206 "accepted", params->freq);
4207 return -1;
4208 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004209 } else if (res > 0 && freq != res &&
4210 !(wpa_s->drv_flags &
4211 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4212 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4213 "while connected on another channel (%u MHz)",
4214 freq, res);
4215 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004216 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004217
4218 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004219}
4220
4221
4222static struct wpa_supplicant *
4223wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4224 int go)
4225{
4226 struct wpa_supplicant *group_wpa_s;
4227
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004228 if (!wpas_p2p_create_iface(wpa_s)) {
4229 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4230 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004231 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004232 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004233
4234 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004235 WPA_IF_P2P_CLIENT) < 0) {
4236 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004237 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004238 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004239 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4240 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004241 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4242 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243 wpas_p2p_remove_pending_group_interface(wpa_s);
4244 return NULL;
4245 }
4246
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004247 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4248 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249 return group_wpa_s;
4250}
4251
4252
4253/**
4254 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4255 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4256 * @persistent_group: Whether to create a persistent group
4257 * @freq: Frequency for the group or 0 to indicate no hardcoding
4258 * Returns: 0 on success, -1 on failure
4259 *
4260 * This function creates a new P2P group with the local end as the Group Owner,
4261 * i.e., without using Group Owner Negotiation.
4262 */
4263int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004264 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004265{
4266 struct p2p_go_neg_results params;
4267 unsigned int r;
4268
4269 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4270 return -1;
4271
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004272 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004273 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004274 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276 if (freq == 2) {
4277 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4278 "band");
4279 if (wpa_s->best_24_freq > 0 &&
4280 p2p_supported_freq(wpa_s->global->p2p,
4281 wpa_s->best_24_freq)) {
4282 freq = wpa_s->best_24_freq;
4283 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4284 "channel: %d MHz", freq);
4285 } else {
4286 os_get_random((u8 *) &r, sizeof(r));
4287 freq = 2412 + (r % 3) * 25;
4288 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4289 "channel: %d MHz", freq);
4290 }
4291 }
4292
4293 if (freq == 5) {
4294 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4295 "band");
4296 if (wpa_s->best_5_freq > 0 &&
4297 p2p_supported_freq(wpa_s->global->p2p,
4298 wpa_s->best_5_freq)) {
4299 freq = wpa_s->best_5_freq;
4300 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4301 "channel: %d MHz", freq);
4302 } else {
4303 os_get_random((u8 *) &r, sizeof(r));
4304 freq = 5180 + (r % 4) * 20;
4305 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4306 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4307 "5 GHz channel for P2P group");
4308 return -1;
4309 }
4310 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4311 "channel: %d MHz", freq);
4312 }
4313 }
4314
4315 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4316 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4317 "(%u MHz) is not supported for P2P uses",
4318 freq);
4319 return -1;
4320 }
4321
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004322 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004323 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004324 if (params.freq &&
4325 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4326 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4327 "(%u MHz) is not supported for P2P uses",
4328 params.freq);
4329 return -1;
4330 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004331 p2p_go_params(wpa_s->global->p2p, &params);
4332 params.persistent_group = persistent_group;
4333
4334 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4335 if (wpa_s == NULL)
4336 return -1;
4337 wpas_start_wps_go(wpa_s, &params, 0);
4338
4339 return 0;
4340}
4341
4342
4343static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4344 struct wpa_ssid *params, int addr_allocated)
4345{
4346 struct wpa_ssid *ssid;
4347
4348 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4349 if (wpa_s == NULL)
4350 return -1;
4351
4352 wpa_supplicant_ap_deinit(wpa_s);
4353
4354 ssid = wpa_config_add_network(wpa_s->conf);
4355 if (ssid == NULL)
4356 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004357 wpa_config_set_network_defaults(ssid);
4358 ssid->temporary = 1;
4359 ssid->proto = WPA_PROTO_RSN;
4360 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4361 ssid->group_cipher = WPA_CIPHER_CCMP;
4362 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4363 ssid->ssid = os_malloc(params->ssid_len);
4364 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365 wpa_config_remove_network(wpa_s->conf, ssid->id);
4366 return -1;
4367 }
4368 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4369 ssid->ssid_len = params->ssid_len;
4370 ssid->p2p_group = 1;
4371 ssid->export_keys = 1;
4372 if (params->psk_set) {
4373 os_memcpy(ssid->psk, params->psk, 32);
4374 ssid->psk_set = 1;
4375 }
4376 if (params->passphrase)
4377 ssid->passphrase = os_strdup(params->passphrase);
4378
4379 wpa_supplicant_select_network(wpa_s, ssid);
4380
4381 wpa_s->show_group_started = 1;
4382
4383 return 0;
4384}
4385
4386
4387int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4388 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004389 int freq, int ht40,
4390 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391{
4392 struct p2p_go_neg_results params;
4393 int go = 0;
4394
4395 if (ssid->disabled != 2 || ssid->ssid == NULL)
4396 return -1;
4397
4398 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4399 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4400 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4401 "already running");
4402 return 0;
4403 }
4404
4405 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004406 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407
Dmitry Shmidt04949592012-07-19 12:16:46 -07004408 wpa_s->p2p_fallback_to_go_neg = 0;
4409
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004410 if (ssid->mode == WPAS_MODE_INFRA)
4411 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4412
4413 if (ssid->mode != WPAS_MODE_P2P_GO)
4414 return -1;
4415
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004416 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004417 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004418
4419 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004420 params.psk_set = ssid->psk_set;
4421 if (params.psk_set)
4422 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004423 if (ssid->passphrase) {
4424 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4425 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4426 "persistent group");
4427 return -1;
4428 }
4429 os_strlcpy(params.passphrase, ssid->passphrase,
4430 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4433 params.ssid_len = ssid->ssid_len;
4434 params.persistent_group = 1;
4435
4436 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4437 if (wpa_s == NULL)
4438 return -1;
4439
4440 wpas_start_wps_go(wpa_s, &params, 0);
4441
4442 return 0;
4443}
4444
4445
4446static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4447 struct wpabuf *proberesp_ies)
4448{
4449 struct wpa_supplicant *wpa_s = ctx;
4450 if (wpa_s->ap_iface) {
4451 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004452 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4453 wpabuf_free(beacon_ies);
4454 wpabuf_free(proberesp_ies);
4455 return;
4456 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004457 if (beacon_ies) {
4458 wpabuf_free(hapd->p2p_beacon_ie);
4459 hapd->p2p_beacon_ie = beacon_ies;
4460 }
4461 wpabuf_free(hapd->p2p_probe_resp_ie);
4462 hapd->p2p_probe_resp_ie = proberesp_ies;
4463 } else {
4464 wpabuf_free(beacon_ies);
4465 wpabuf_free(proberesp_ies);
4466 }
4467 wpa_supplicant_ap_update_beacon(wpa_s);
4468}
4469
4470
4471static void wpas_p2p_idle_update(void *ctx, int idle)
4472{
4473 struct wpa_supplicant *wpa_s = ctx;
4474 if (!wpa_s->ap_iface)
4475 return;
4476 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004477 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478 wpas_p2p_set_group_idle_timeout(wpa_s);
4479 else
4480 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4481}
4482
4483
4484struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004485 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004486{
4487 struct p2p_group *group;
4488 struct p2p_group_config *cfg;
4489
4490 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4491 return NULL;
4492 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4493 return NULL;
4494
4495 cfg = os_zalloc(sizeof(*cfg));
4496 if (cfg == NULL)
4497 return NULL;
4498
Dmitry Shmidt04949592012-07-19 12:16:46 -07004499 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004500 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004501 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004502 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4504 if (wpa_s->max_stations &&
4505 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4506 cfg->max_clients = wpa_s->max_stations;
4507 else
4508 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004509 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4510 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004511 cfg->cb_ctx = wpa_s;
4512 cfg->ie_update = wpas_p2p_ie_update;
4513 cfg->idle_update = wpas_p2p_idle_update;
4514
4515 group = p2p_group_init(wpa_s->global->p2p, cfg);
4516 if (group == NULL)
4517 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004518 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004519 p2p_group_notif_formation_done(group);
4520 wpa_s->p2p_group = group;
4521 return group;
4522}
4523
4524
4525void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4526 int registrar)
4527{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004528 struct wpa_ssid *ssid = wpa_s->current_ssid;
4529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004530 if (!wpa_s->p2p_in_provisioning) {
4531 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4532 "provisioning not in progress");
4533 return;
4534 }
4535
Dmitry Shmidt04949592012-07-19 12:16:46 -07004536 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4537 u8 go_dev_addr[ETH_ALEN];
4538 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4539 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4540 ssid->ssid_len);
4541 /* Clear any stored provisioning info */
4542 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4543 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004545 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4546 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004547 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4548 /*
4549 * Use a separate timeout for initial data connection to
4550 * complete to allow the group to be removed automatically if
4551 * something goes wrong in this step before the P2P group idle
4552 * timeout mechanism is taken into use.
4553 */
4554 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4555 wpas_p2p_group_formation_timeout,
4556 wpa_s->parent, NULL);
4557 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004558 if (wpa_s->global->p2p)
4559 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4560 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4561 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4562 wpas_group_formation_completed(wpa_s, 1);
4563}
4564
4565
Jouni Malinen75ecf522011-06-27 15:19:46 -07004566void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4567 struct wps_event_fail *fail)
4568{
4569 if (!wpa_s->p2p_in_provisioning) {
4570 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4571 "provisioning not in progress");
4572 return;
4573 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004574
4575 if (wpa_s->go_params) {
4576 p2p_clear_provisioning_info(
4577 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004578 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004579 }
4580
Jouni Malinen75ecf522011-06-27 15:19:46 -07004581 wpas_notify_p2p_wps_failed(wpa_s, fail);
4582}
4583
4584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004585int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004586 const char *config_method,
4587 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004588{
4589 u16 config_methods;
4590
Dmitry Shmidt04949592012-07-19 12:16:46 -07004591 wpa_s->p2p_fallback_to_go_neg = 0;
4592 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004593 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004594 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004595 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004596 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004597 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4598 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004599 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004600 else {
4601 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004603 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604
Dmitry Shmidt04949592012-07-19 12:16:46 -07004605 if (use == WPAS_P2P_PD_AUTO) {
4606 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4607 wpa_s->pending_pd_config_methods = config_methods;
4608 wpa_s->p2p_auto_pd = 1;
4609 wpa_s->p2p_auto_join = 0;
4610 wpa_s->pending_pd_before_join = 0;
4611 wpa_s->auto_pd_scan_retry = 0;
4612 wpas_p2p_stop_find(wpa_s);
4613 wpa_s->p2p_join_scan_count = 0;
4614 os_get_time(&wpa_s->p2p_auto_started);
4615 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4616 wpa_s->p2p_auto_started.sec,
4617 wpa_s->p2p_auto_started.usec);
4618 wpas_p2p_join_scan(wpa_s, NULL);
4619 return 0;
4620 }
4621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004622 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4623 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004624 config_methods,
4625 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626 }
4627
4628 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4629 return -1;
4630
4631 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004632 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004633 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004634}
4635
4636
4637int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4638 char *end)
4639{
4640 return p2p_scan_result_text(ies, ies_len, buf, end);
4641}
4642
4643
4644static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4645{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004646 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647 return;
4648
4649 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4650 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004651 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652}
4653
4654
4655int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4656 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004657 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004658 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004659{
4660 wpas_p2p_clear_pending_action_tx(wpa_s);
4661 wpa_s->p2p_long_listen = 0;
4662
4663 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4664 return wpa_drv_p2p_find(wpa_s, timeout, type);
4665
Dmitry Shmidt04949592012-07-19 12:16:46 -07004666 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4667 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668 return -1;
4669
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004670 wpa_supplicant_cancel_sched_scan(wpa_s);
4671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004673 num_req_dev_types, req_dev_types, dev_id,
4674 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004675}
4676
4677
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004678static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004679{
4680 wpas_p2p_clear_pending_action_tx(wpa_s);
4681 wpa_s->p2p_long_listen = 0;
4682 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4683 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004684 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004685
4686 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4687 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004688 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004689 }
4690
4691 if (wpa_s->global->p2p)
4692 p2p_stop_find(wpa_s->global->p2p);
4693
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004694 return 0;
4695}
4696
4697
4698void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4699{
4700 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4701 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702 wpas_p2p_remove_pending_group_interface(wpa_s);
4703}
4704
4705
4706static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4707{
4708 struct wpa_supplicant *wpa_s = eloop_ctx;
4709 wpa_s->p2p_long_listen = 0;
4710}
4711
4712
4713int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4714{
4715 int res;
4716
4717 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4718 return -1;
4719
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004720 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004721 wpas_p2p_clear_pending_action_tx(wpa_s);
4722
4723 if (timeout == 0) {
4724 /*
4725 * This is a request for unlimited Listen state. However, at
4726 * least for now, this is mapped to a Listen state for one
4727 * hour.
4728 */
4729 timeout = 3600;
4730 }
4731 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4732 wpa_s->p2p_long_listen = 0;
4733
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004734 /*
4735 * Stop previous find/listen operation to avoid trying to request a new
4736 * remain-on-channel operation while the driver is still running the
4737 * previous one.
4738 */
4739 if (wpa_s->global->p2p)
4740 p2p_stop_find(wpa_s->global->p2p);
4741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004742 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4743 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4744 wpa_s->p2p_long_listen = timeout * 1000;
4745 eloop_register_timeout(timeout, 0,
4746 wpas_p2p_long_listen_timeout,
4747 wpa_s, NULL);
4748 }
4749
4750 return res;
4751}
4752
4753
4754int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4755 u8 *buf, size_t len, int p2p_group)
4756{
4757 struct wpabuf *p2p_ie;
4758 int ret;
4759
4760 if (wpa_s->global->p2p_disabled)
4761 return -1;
4762 if (wpa_s->global->p2p == NULL)
4763 return -1;
4764 if (bss == NULL)
4765 return -1;
4766
4767 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4768 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4769 p2p_group, p2p_ie);
4770 wpabuf_free(p2p_ie);
4771
4772 return ret;
4773}
4774
4775
4776int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004777 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004778 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004779{
4780 if (wpa_s->global->p2p_disabled)
4781 return 0;
4782 if (wpa_s->global->p2p == NULL)
4783 return 0;
4784
Dmitry Shmidt04949592012-07-19 12:16:46 -07004785 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4786 ie, ie_len)) {
4787 case P2P_PREQ_NOT_P2P:
4788 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4789 ssi_signal);
4790 /* fall through */
4791 case P2P_PREQ_MALFORMED:
4792 case P2P_PREQ_NOT_LISTEN:
4793 case P2P_PREQ_NOT_PROCESSED:
4794 default: /* make gcc happy */
4795 return 0;
4796 case P2P_PREQ_PROCESSED:
4797 return 1;
4798 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004799}
4800
4801
4802void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4803 const u8 *sa, const u8 *bssid,
4804 u8 category, const u8 *data, size_t len, int freq)
4805{
4806 if (wpa_s->global->p2p_disabled)
4807 return;
4808 if (wpa_s->global->p2p == NULL)
4809 return;
4810
4811 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4812 freq);
4813}
4814
4815
4816void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4817{
4818 if (wpa_s->global->p2p_disabled)
4819 return;
4820 if (wpa_s->global->p2p == NULL)
4821 return;
4822
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004823 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824}
4825
4826
4827void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4828{
4829 p2p_group_deinit(wpa_s->p2p_group);
4830 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004831
4832 wpa_s->ap_configured_cb = NULL;
4833 wpa_s->ap_configured_cb_ctx = NULL;
4834 wpa_s->ap_configured_cb_data = NULL;
4835 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004836}
4837
4838
4839int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4840{
4841 wpa_s->p2p_long_listen = 0;
4842
4843 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4844 return wpa_drv_p2p_reject(wpa_s, addr);
4845
4846 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4847 return -1;
4848
4849 return p2p_reject(wpa_s->global->p2p, addr);
4850}
4851
4852
4853/* Invite to reinvoke a persistent group */
4854int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004855 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004856 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004857{
4858 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004859 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004860 int force_freq = 0, oper_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004861 int res;
4862
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004863 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004864 if (peer_addr)
4865 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4866 else
4867 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004868
Jouni Malinen31be0a42012-08-31 21:20:51 +03004869 wpa_s->p2p_persistent_go_freq = freq;
4870 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004871 if (ssid->mode == WPAS_MODE_P2P_GO) {
4872 role = P2P_INVITE_ROLE_GO;
4873 if (peer_addr == NULL) {
4874 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4875 "address in invitation command");
4876 return -1;
4877 }
4878 if (wpas_p2p_create_iface(wpa_s)) {
4879 if (wpas_p2p_add_group_interface(wpa_s,
4880 WPA_IF_P2P_GO) < 0) {
4881 wpa_printf(MSG_ERROR, "P2P: Failed to "
4882 "allocate a new interface for the "
4883 "group");
4884 return -1;
4885 }
4886 bssid = wpa_s->pending_interface_addr;
4887 } else
4888 bssid = wpa_s->own_addr;
4889 } else {
4890 role = P2P_INVITE_ROLE_CLIENT;
4891 peer_addr = ssid->bssid;
4892 }
4893 wpa_s->pending_invite_ssid_id = ssid->id;
4894
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004895 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4896 wpa_s->assoc_freq) {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004897 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004898 if (bssid == NULL)
4899 bssid = bssid_buf;
4900 } else {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004901 oper_freq = wpa_drv_shared_freq(wpa_s);
4902 if (oper_freq < 0)
4903 oper_freq = 0;
4904 }
4905
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004906 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4907 &oper_freq);
4908 if (res)
4909 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004911 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4912 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4913 ssid->ssid, ssid->ssid_len,
4914 go_dev_addr, 1);
4915
4916 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4917 return -1;
4918
4919 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004920 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4921 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922}
4923
4924
4925/* Invite to join an active group */
4926int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4927 const u8 *peer_addr, const u8 *go_dev_addr)
4928{
4929 struct wpa_global *global = wpa_s->global;
4930 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004931 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004933 int persistent;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004934 int force_freq = 0, oper_freq = 0, pref_freq = 0;
4935 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004936
Jouni Malinen31be0a42012-08-31 21:20:51 +03004937 wpa_s->p2p_persistent_go_freq = 0;
4938 wpa_s->p2p_go_ht40 = 0;
4939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4941 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4942 break;
4943 }
4944 if (wpa_s == NULL) {
4945 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4946 return -1;
4947 }
4948
4949 ssid = wpa_s->current_ssid;
4950 if (ssid == NULL) {
4951 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4952 "invitation");
4953 return -1;
4954 }
4955
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004956 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004957 persistent = ssid->p2p_persistent_group &&
4958 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4959 ssid->ssid, ssid->ssid_len);
4960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961 if (ssid->mode == WPAS_MODE_P2P_GO) {
4962 role = P2P_INVITE_ROLE_ACTIVE_GO;
4963 bssid = wpa_s->own_addr;
4964 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004965 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966 } else {
4967 role = P2P_INVITE_ROLE_CLIENT;
4968 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4969 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4970 "invite to current group");
4971 return -1;
4972 }
4973 bssid = wpa_s->bssid;
4974 if (go_dev_addr == NULL &&
4975 !is_zero_ether_addr(wpa_s->go_dev_addr))
4976 go_dev_addr = wpa_s->go_dev_addr;
4977 }
4978 wpa_s->parent->pending_invite_ssid_id = -1;
4979
4980 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4981 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4982 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004983 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004984
4985 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4986 return -1;
4987
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004988 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4989 wpa_s->assoc_freq) {
4990 oper_freq = wpa_s->assoc_freq;
4991 if (bssid == NULL)
4992 bssid = bssid_buf;
4993 } else {
4994 oper_freq = wpa_drv_shared_freq(wpa_s);
4995 if (oper_freq < 0)
4996 oper_freq = 0;
4997 }
4998
4999 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
5000 &oper_freq);
5001 if (res)
5002 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005003 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005006 ssid->ssid, ssid->ssid_len, force_freq,
5007 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008}
5009
5010
5011void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5012{
5013 struct wpa_ssid *ssid = wpa_s->current_ssid;
5014 const char *ssid_txt;
5015 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005016 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005018 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019
Dmitry Shmidt04949592012-07-19 12:16:46 -07005020 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5021 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5022 wpa_s->parent, NULL);
5023 }
5024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005026 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005027
5028 wpa_s->show_group_started = 0;
5029
5030 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5031 os_memset(go_dev_addr, 0, ETH_ALEN);
5032 if (ssid->bssid_set)
5033 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5034 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5035 ssid->ssid_len);
5036 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5037
5038 if (wpa_s->global->p2p_group_formation == wpa_s)
5039 wpa_s->global->p2p_group_formation = NULL;
5040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005041 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5042 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043 if (ssid->passphrase == NULL && ssid->psk_set) {
5044 char psk[65];
5045 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5046 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5047 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
5048 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005049 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005050 MAC2STR(go_dev_addr),
5051 persistent ? " [PERSISTENT]" : "");
5052 } else {
5053 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5054 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
5055 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005056 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057 ssid->passphrase ? ssid->passphrase : "",
5058 MAC2STR(go_dev_addr),
5059 persistent ? " [PERSISTENT]" : "");
5060 }
5061
5062 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005063 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5064 ssid, go_dev_addr);
5065 if (network_id < 0)
5066 network_id = ssid->id;
5067 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005068
5069done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005070 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005071}
5072
5073
5074int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5075 u32 interval1, u32 duration2, u32 interval2)
5076{
5077 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5078 return -1;
5079 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5080 return -1;
5081
5082 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5083 wpa_s->current_ssid == NULL ||
5084 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5085 return -1;
5086
5087 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5088 wpa_s->own_addr, wpa_s->assoc_freq,
5089 duration1, interval1, duration2, interval2);
5090}
5091
5092
5093int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5094 unsigned int interval)
5095{
5096 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5097 return -1;
5098
5099 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5100 return -1;
5101
5102 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5103}
5104
5105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005106static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5107{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005108 if (wpa_s->current_ssid == NULL) {
5109 /*
5110 * current_ssid can be cleared when P2P client interface gets
5111 * disconnected, so assume this interface was used as P2P
5112 * client.
5113 */
5114 return 1;
5115 }
5116 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005117 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5118}
5119
5120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5122{
5123 struct wpa_supplicant *wpa_s = eloop_ctx;
5124
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005125 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005126 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5127 "disabled");
5128 return;
5129 }
5130
Dmitry Shmidt04949592012-07-19 12:16:46 -07005131 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5132 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005133 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134}
5135
5136
5137static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5138{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005139 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140
Dmitry Shmidt04949592012-07-19 12:16:46 -07005141 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5142 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005144 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5145 return;
5146
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005147 timeout = wpa_s->conf->p2p_group_idle;
5148 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5149 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5150 timeout = P2P_MAX_CLIENT_IDLE;
5151
5152 if (timeout == 0)
5153 return;
5154
Dmitry Shmidt04949592012-07-19 12:16:46 -07005155 if (timeout < 0) {
5156 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5157 timeout = 0; /* special client mode no-timeout */
5158 else
5159 return;
5160 }
5161
5162 if (wpa_s->p2p_in_provisioning) {
5163 /*
5164 * Use the normal group formation timeout during the
5165 * provisioning phase to avoid terminating this process too
5166 * early due to group idle timeout.
5167 */
5168 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5169 "during provisioning");
5170 return;
5171 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305172
Dmitry Shmidt04949592012-07-19 12:16:46 -07005173 if (wpa_s->show_group_started) {
5174 /*
5175 * Use the normal group formation timeout between the end of
5176 * the provisioning phase and completion of 4-way handshake to
5177 * avoid terminating this process too early due to group idle
5178 * timeout.
5179 */
5180 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5181 "while waiting for initial 4-way handshake to "
5182 "complete");
5183 return;
5184 }
5185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005186 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005187 timeout);
5188 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5189 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005190}
5191
5192
Jouni Malinen2b89da82012-08-31 22:04:41 +03005193/* Returns 1 if the interface was removed */
5194int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5195 u16 reason_code, const u8 *ie, size_t ie_len,
5196 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005197{
5198 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005199 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005201 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005202
Dmitry Shmidt04949592012-07-19 12:16:46 -07005203 if (!locally_generated)
5204 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5205 ie_len);
5206
5207 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5208 wpa_s->current_ssid &&
5209 wpa_s->current_ssid->p2p_group &&
5210 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5211 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5212 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005213 if (wpas_p2p_group_delete(wpa_s,
5214 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5215 > 0)
5216 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005217 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005218
5219 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005220}
5221
5222
5223void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005224 u16 reason_code, const u8 *ie, size_t ie_len,
5225 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005226{
5227 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5228 return;
5229 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5230 return;
5231
Dmitry Shmidt04949592012-07-19 12:16:46 -07005232 if (!locally_generated)
5233 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5234 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235}
5236
5237
5238void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5239{
5240 struct p2p_data *p2p = wpa_s->global->p2p;
5241
5242 if (p2p == NULL)
5243 return;
5244
5245 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5246 return;
5247
5248 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5249 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5250
5251 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5252 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5253
5254 if (wpa_s->wps &&
5255 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5256 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5257
5258 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5259 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5260
5261 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5262 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5263 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5264 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5265 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5266 }
5267
5268 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5269 p2p_set_sec_dev_types(p2p,
5270 (void *) wpa_s->conf->sec_device_type,
5271 wpa_s->conf->num_sec_device_types);
5272
5273 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5274 int i;
5275 p2p_remove_wps_vendor_extensions(p2p);
5276 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5277 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5278 continue;
5279 p2p_add_wps_vendor_extension(
5280 p2p, wpa_s->conf->wps_vendor_ext[i]);
5281 }
5282 }
5283
5284 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5285 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5286 char country[3];
5287 country[0] = wpa_s->conf->country[0];
5288 country[1] = wpa_s->conf->country[1];
5289 country[2] = 0x04;
5290 p2p_set_country(p2p, country);
5291 }
5292
5293 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5294 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5295 wpa_s->conf->p2p_ssid_postfix ?
5296 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5297 0);
5298 }
5299
5300 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5301 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005302
5303 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5304 u8 reg_class, channel;
5305 int ret;
5306 unsigned int r;
5307 if (wpa_s->conf->p2p_listen_reg_class &&
5308 wpa_s->conf->p2p_listen_channel) {
5309 reg_class = wpa_s->conf->p2p_listen_reg_class;
5310 channel = wpa_s->conf->p2p_listen_channel;
5311 } else {
5312 reg_class = 81;
5313 /*
5314 * Pick one of the social channels randomly as the
5315 * listen channel.
5316 */
5317 os_get_random((u8 *) &r, sizeof(r));
5318 channel = 1 + (r % 3) * 5;
5319 }
5320 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5321 if (ret)
5322 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5323 "failed: %d", ret);
5324 }
5325 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5326 u8 op_reg_class, op_channel, cfg_op_channel;
5327 int ret = 0;
5328 unsigned int r;
5329 if (wpa_s->conf->p2p_oper_reg_class &&
5330 wpa_s->conf->p2p_oper_channel) {
5331 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5332 op_channel = wpa_s->conf->p2p_oper_channel;
5333 cfg_op_channel = 1;
5334 } else {
5335 op_reg_class = 81;
5336 /*
5337 * Use random operation channel from (1, 6, 11)
5338 *if no other preference is indicated.
5339 */
5340 os_get_random((u8 *) &r, sizeof(r));
5341 op_channel = 1 + (r % 3) * 5;
5342 cfg_op_channel = 0;
5343 }
5344 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5345 cfg_op_channel);
5346 if (ret)
5347 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5348 "failed: %d", ret);
5349 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005350
5351 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5352 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5353 wpa_s->conf->p2p_pref_chan) < 0) {
5354 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5355 "update failed");
5356 }
5357 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005358}
5359
5360
5361int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5362 int duration)
5363{
5364 if (!wpa_s->ap_iface)
5365 return -1;
5366 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5367 duration);
5368}
5369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005370
5371int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5372{
5373 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5374 return -1;
5375 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5376 return -1;
5377
5378 wpa_s->global->cross_connection = enabled;
5379 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5380
5381 if (!enabled) {
5382 struct wpa_supplicant *iface;
5383
5384 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5385 {
5386 if (iface->cross_connect_enabled == 0)
5387 continue;
5388
5389 iface->cross_connect_enabled = 0;
5390 iface->cross_connect_in_use = 0;
5391 wpa_msg(iface->parent, MSG_INFO,
5392 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5393 iface->ifname, iface->cross_connect_uplink);
5394 }
5395 }
5396
5397 return 0;
5398}
5399
5400
5401static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5402{
5403 struct wpa_supplicant *iface;
5404
5405 if (!uplink->global->cross_connection)
5406 return;
5407
5408 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5409 if (!iface->cross_connect_enabled)
5410 continue;
5411 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5412 0)
5413 continue;
5414 if (iface->ap_iface == NULL)
5415 continue;
5416 if (iface->cross_connect_in_use)
5417 continue;
5418
5419 iface->cross_connect_in_use = 1;
5420 wpa_msg(iface->parent, MSG_INFO,
5421 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5422 iface->ifname, iface->cross_connect_uplink);
5423 }
5424}
5425
5426
5427static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5428{
5429 struct wpa_supplicant *iface;
5430
5431 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5432 if (!iface->cross_connect_enabled)
5433 continue;
5434 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5435 0)
5436 continue;
5437 if (!iface->cross_connect_in_use)
5438 continue;
5439
5440 wpa_msg(iface->parent, MSG_INFO,
5441 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5442 iface->ifname, iface->cross_connect_uplink);
5443 iface->cross_connect_in_use = 0;
5444 }
5445}
5446
5447
5448void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5449{
5450 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5451 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5452 wpa_s->cross_connect_disallowed)
5453 wpas_p2p_disable_cross_connect(wpa_s);
5454 else
5455 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005456 if (!wpa_s->ap_iface &&
5457 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5458 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459}
5460
5461
5462void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5463{
5464 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005465 if (!wpa_s->ap_iface &&
5466 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5467 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468 wpas_p2p_set_group_idle_timeout(wpa_s);
5469}
5470
5471
5472static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5473{
5474 struct wpa_supplicant *iface;
5475
5476 if (!wpa_s->global->cross_connection)
5477 return;
5478
5479 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5480 if (iface == wpa_s)
5481 continue;
5482 if (iface->drv_flags &
5483 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5484 continue;
5485 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5486 continue;
5487
5488 wpa_s->cross_connect_enabled = 1;
5489 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5490 sizeof(wpa_s->cross_connect_uplink));
5491 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5492 "%s to %s whenever uplink is available",
5493 wpa_s->ifname, wpa_s->cross_connect_uplink);
5494
5495 if (iface->ap_iface || iface->current_ssid == NULL ||
5496 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5497 iface->cross_connect_disallowed ||
5498 iface->wpa_state != WPA_COMPLETED)
5499 break;
5500
5501 wpa_s->cross_connect_in_use = 1;
5502 wpa_msg(wpa_s->parent, MSG_INFO,
5503 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5504 wpa_s->ifname, wpa_s->cross_connect_uplink);
5505 break;
5506 }
5507}
5508
5509
5510int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5511{
5512 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5513 !wpa_s->p2p_in_provisioning)
5514 return 0; /* not P2P client operation */
5515
5516 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5517 "session overlap");
5518 if (wpa_s != wpa_s->parent)
5519 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5520
5521 if (wpa_s->global->p2p)
5522 p2p_group_formation_failed(wpa_s->global->p2p);
5523
5524 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5525 wpa_s->parent, NULL);
5526
5527 wpas_group_formation_completed(wpa_s, 0);
5528 return 1;
5529}
5530
5531
5532void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5533{
5534 struct p2p_channels chan;
5535
5536 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5537 return;
5538
5539 os_memset(&chan, 0, sizeof(chan));
5540 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5541 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5542 "channel list");
5543 return;
5544 }
5545
5546 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5547}
5548
5549
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005550static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5551 struct wpa_scan_results *scan_res)
5552{
5553 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5554}
5555
5556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005557int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5558{
5559 struct wpa_global *global = wpa_s->global;
5560 int found = 0;
5561 const u8 *peer;
5562
5563 if (global->p2p == NULL)
5564 return -1;
5565
5566 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5567
5568 if (wpa_s->pending_interface_name[0] &&
5569 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5570 found = 1;
5571
5572 peer = p2p_get_go_neg_peer(global->p2p);
5573 if (peer) {
5574 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5575 MACSTR, MAC2STR(peer));
5576 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005577 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005578 }
5579
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005580 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5581 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5582 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5583 found = 1;
5584 }
5585
5586 if (wpa_s->pending_pd_before_join) {
5587 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5588 wpa_s->pending_pd_before_join = 0;
5589 found = 1;
5590 }
5591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005592 wpas_p2p_stop_find(wpa_s);
5593
5594 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5595 if (wpa_s == global->p2p_group_formation &&
5596 (wpa_s->p2p_in_provisioning ||
5597 wpa_s->parent->pending_interface_type ==
5598 WPA_IF_P2P_CLIENT)) {
5599 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5600 "formation found - cancelling",
5601 wpa_s->ifname);
5602 found = 1;
5603 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5604 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005605 if (wpa_s->p2p_in_provisioning) {
5606 wpas_group_formation_completed(wpa_s, 0);
5607 break;
5608 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005609 wpas_p2p_group_delete(wpa_s,
5610 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611 break;
5612 }
5613 }
5614
5615 if (!found) {
5616 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5617 return -1;
5618 }
5619
5620 return 0;
5621}
5622
5623
5624void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5625{
5626 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5627 return;
5628
5629 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5630 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005631 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005632}
5633
5634
5635void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5636 int freq_24, int freq_5, int freq_overall)
5637{
5638 struct p2p_data *p2p = wpa_s->global->p2p;
5639 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5640 return;
5641 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5642}
5643
5644
5645int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5646{
5647 u8 peer[ETH_ALEN];
5648 struct p2p_data *p2p = wpa_s->global->p2p;
5649
5650 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5651 return -1;
5652
5653 if (hwaddr_aton(addr, peer))
5654 return -1;
5655
5656 return p2p_unauthorize(p2p, peer);
5657}
5658
5659
5660/**
5661 * wpas_p2p_disconnect - Disconnect from a P2P Group
5662 * @wpa_s: Pointer to wpa_supplicant data
5663 * Returns: 0 on success, -1 on failure
5664 *
5665 * This can be used to disconnect from a group in which the local end is a P2P
5666 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5667 * virtual network interface was created for this group, that interface will be
5668 * removed. Otherwise, only the configured P2P group network will be removed
5669 * from the interface.
5670 */
5671int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5672{
5673
5674 if (wpa_s == NULL)
5675 return -1;
5676
Jouni Malinen2b89da82012-08-31 22:04:41 +03005677 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5678 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005679}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005680
5681
5682int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5683{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005684 int ret;
5685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005686 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5687 return 0;
5688
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005689 ret = p2p_in_progress(wpa_s->global->p2p);
5690 if (ret == 0) {
5691 /*
5692 * Check whether there is an ongoing WPS provisioning step (or
5693 * other parts of group formation) on another interface since
5694 * p2p_in_progress() does not report this to avoid issues for
5695 * scans during such provisioning step.
5696 */
5697 if (wpa_s->global->p2p_group_formation &&
5698 wpa_s->global->p2p_group_formation != wpa_s) {
5699 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5700 "in group formation",
5701 wpa_s->global->p2p_group_formation->ifname);
5702 ret = 1;
5703 }
5704 }
5705
5706 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005707}
5708
5709
5710void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5711 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005712{
5713 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5714 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5715 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005716 /**
5717 * Remove the network by scheduling the group formation
5718 * timeout to happen immediately. The teardown code
5719 * needs to be scheduled to run asynch later so that we
5720 * don't delete data from under ourselves unexpectedly.
5721 * Calling wpas_p2p_group_formation_timeout directly
5722 * causes a series of crashes in WPS failure scenarios.
5723 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005724 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5725 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005726 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5727 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005728 }
5729}
5730
5731
5732struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005733 const u8 *addr, const u8 *ssid,
5734 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005735{
5736 struct wpa_ssid *s;
5737 size_t i;
5738
5739 for (s = wpa_s->conf->ssid; s; s = s->next) {
5740 if (s->disabled != 2)
5741 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005742 if (ssid &&
5743 (ssid_len != s->ssid_len ||
5744 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5745 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005746 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5747 return s; /* peer is GO in the persistent group */
5748 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5749 continue;
5750 for (i = 0; i < s->num_p2p_clients; i++) {
5751 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5752 addr, ETH_ALEN) == 0)
5753 return s; /* peer is P2P client in persistent
5754 * group */
5755 }
5756 }
5757
5758 return NULL;
5759}
5760
5761
5762void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5763 const u8 *addr)
5764{
5765 if (addr == NULL)
5766 return;
5767 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5768}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005769
Dmitry Shmidt04949592012-07-19 12:16:46 -07005770
5771static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5772 int group_added)
5773{
5774 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005775 if (wpa_s->global->p2p_group_formation)
5776 group = wpa_s->global->p2p_group_formation;
5777 wpa_s = wpa_s->parent;
5778 offchannel_send_action_done(wpa_s);
5779 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005780 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005781 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5782 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5783 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5784 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5785 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005786 wpa_s->p2p_pd_before_go_neg,
5787 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005788}
5789
5790
5791int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5792{
5793 if (!wpa_s->p2p_fallback_to_go_neg ||
5794 wpa_s->p2p_in_provisioning <= 5)
5795 return 0;
5796
5797 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5798 return 0; /* peer operating as a GO */
5799
5800 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5801 "fallback to GO Negotiation");
5802 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5803
5804 return 1;
5805}
5806
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005807
5808unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5809{
5810 const char *rn, *rn2;
5811 struct wpa_supplicant *ifs;
5812
5813 if (wpa_s->wpa_state > WPA_SCANNING) {
5814 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5815 "concurrent operation",
5816 P2P_CONCURRENT_SEARCH_DELAY);
5817 return P2P_CONCURRENT_SEARCH_DELAY;
5818 }
5819
5820 if (!wpa_s->driver->get_radio_name)
5821 return 0;
5822 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5823 if (rn == NULL || rn[0] == '\0')
5824 return 0;
5825
5826 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5827 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5828 continue;
5829
5830 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5831 if (!rn2 || os_strcmp(rn, rn2) != 0)
5832 continue;
5833 if (ifs->wpa_state > WPA_SCANNING) {
5834 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5835 "delay due to concurrent operation on "
5836 "interface %s",
5837 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5838 return P2P_CONCURRENT_SEARCH_DELAY;
5839 }
5840 }
5841
5842 return 0;
5843}
5844
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005845
5846void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5847{
5848 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5849 "pending anymore (sta_scan_pending=%d "
5850 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5851 wpa_s->global->p2p_cb_on_scan_complete);
5852 wpa_s->sta_scan_pending = 0;
5853
5854 if (!wpa_s->global->p2p_cb_on_scan_complete)
5855 return;
5856 wpa_s->global->p2p_cb_on_scan_complete = 0;
5857
5858 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5859 return;
5860
5861 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
5862 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
5863 "continued after successful connection");
5864 p2p_increase_search_delay(wpa_s->global->p2p,
5865 wpas_p2p_search_delay(wpa_s));
5866 }
5867}
5868
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005869#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07005870static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
5871{
5872 struct wpa_supplicant *wpa_s = eloop_ctx;
5873
5874 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
5875 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
5876}
5877
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005878int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5879 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005880{
5881 struct wpa_supplicant *iface = NULL;
5882 struct p2p_data *p2p = wpa_s->global->p2p;
5883
5884 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005885 if ((iface->current_ssid) &&
5886 (iface->current_ssid->frequency != freq) &&
5887 ((iface->p2p_group_interface) ||
5888 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005889
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005890 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5891 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5892 /* Try to see whether we can move the GO. If it
5893 * is not possible, remove the GO interface
5894 */
5895 if (wpa_drv_switch_channel(iface, freq) == 0) {
5896 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5897 iface->current_ssid->frequency = freq;
5898 continue;
5899 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005900 }
5901
5902 /* If GO cannot be moved or if the conflicting interface is a
5903 * P2P Client, remove the interface depending up on the connection
5904 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005905 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005906 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005907 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005908 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005909 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07005910 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
5911 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005912 /* If connection in progress is p2p connection, do not proceed for the connection */
5913 if (wpa_s == iface)
5914 return -1;
5915 else
5916 /* If connection in progress is STA connection, proceed for the connection */
5917 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005918 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005919 /* P2p connection has priority, disable the STA network*/
5920 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5921 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5922 " id=%d", ssid->id);
5923 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5924 if (wpa_s == iface) {
5925 /* p2p connection is in progress, continue connecting...*/
5926 return 0;
5927 }
5928 else {
5929 /* STA connection is in progress, do not allow to continue */
5930 return -1;
5931 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005932 }
5933 }
5934 }
5935 return 0;
5936}
5937#endif