blob: b760e7fefe809e45d62ccf3c1c0c36922b4b7146 [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 Shmidt51b6ea82013-05-08 10:42:09 -07001006 d->beacon_int = s->beacon_int;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007}
1008
1009
1010static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1011 enum wpa_driver_if_type type)
1012{
1013 char ifname[120], force_ifname[120];
1014
1015 if (wpa_s->pending_interface_name[0]) {
1016 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1017 "- skip creation of a new one");
1018 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1019 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1020 "unknown?! ifname='%s'",
1021 wpa_s->pending_interface_name);
1022 return -1;
1023 }
1024 return 0;
1025 }
1026
1027 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
1028 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001029 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1031 /* Try to avoid going over the IFNAMSIZ length limit */
1032 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1033 wpa_s->p2p_group_idx);
1034 }
1035 force_ifname[0] = '\0';
1036
1037 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1038 ifname);
1039 wpa_s->p2p_group_idx++;
1040
1041 wpa_s->pending_interface_type = type;
1042 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1043 wpa_s->pending_interface_addr, NULL) < 0) {
1044 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1045 "interface");
1046 return -1;
1047 }
1048
1049 if (force_ifname[0]) {
1050 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1051 force_ifname);
1052 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1053 sizeof(wpa_s->pending_interface_name));
1054 } else
1055 os_strlcpy(wpa_s->pending_interface_name, ifname,
1056 sizeof(wpa_s->pending_interface_name));
1057 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1058 MACSTR, wpa_s->pending_interface_name,
1059 MAC2STR(wpa_s->pending_interface_addr));
1060
1061 return 0;
1062}
1063
1064
1065static void wpas_p2p_remove_pending_group_interface(
1066 struct wpa_supplicant *wpa_s)
1067{
1068 if (!wpa_s->pending_interface_name[0] ||
1069 is_zero_ether_addr(wpa_s->pending_interface_addr))
1070 return; /* No pending virtual interface */
1071
1072 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1073 wpa_s->pending_interface_name);
1074 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1075 wpa_s->pending_interface_name);
1076 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1077 wpa_s->pending_interface_name[0] = '\0';
1078}
1079
1080
1081static struct wpa_supplicant *
1082wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1083{
1084 struct wpa_interface iface;
1085 struct wpa_supplicant *group_wpa_s;
1086
1087 if (!wpa_s->pending_interface_name[0]) {
1088 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1089 if (!wpas_p2p_create_iface(wpa_s))
1090 return NULL;
1091 /*
1092 * Something has forced us to remove the pending interface; try
1093 * to create a new one and hope for the best that we will get
1094 * the same local address.
1095 */
1096 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1097 WPA_IF_P2P_CLIENT) < 0)
1098 return NULL;
1099 }
1100
1101 os_memset(&iface, 0, sizeof(iface));
1102 iface.ifname = wpa_s->pending_interface_name;
1103 iface.driver = wpa_s->driver->name;
1104 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1105 iface.driver_param = wpa_s->conf->driver_param;
1106 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1107 if (group_wpa_s == NULL) {
1108 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1109 "wpa_supplicant interface");
1110 return NULL;
1111 }
1112 wpa_s->pending_interface_name[0] = '\0';
1113 group_wpa_s->parent = wpa_s;
1114 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1115 P2P_GROUP_INTERFACE_CLIENT;
1116 wpa_s->global->p2p_group_formation = group_wpa_s;
1117
1118 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1119
1120 return group_wpa_s;
1121}
1122
1123
1124static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1125 void *timeout_ctx)
1126{
1127 struct wpa_supplicant *wpa_s = eloop_ctx;
1128 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1129 if (wpa_s->global->p2p)
1130 p2p_group_formation_failed(wpa_s->global->p2p);
1131 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1132 wpa_drv_p2p_group_formation_failed(wpa_s);
1133 wpas_group_formation_completed(wpa_s, 0);
1134}
1135
1136
1137void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1138{
1139 struct wpa_supplicant *wpa_s = ctx;
1140
1141 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1142 wpa_drv_cancel_remain_on_channel(wpa_s);
1143 wpa_s->off_channel_freq = 0;
1144 wpa_s->roc_waiting_drv_freq = 0;
1145 }
1146
1147 if (res->status) {
1148 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1149 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001150 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 wpas_p2p_remove_pending_group_interface(wpa_s);
1152 return;
1153 }
1154
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001155 if (wpa_s->p2p_go_ht40)
1156 res->ht40 = 1;
1157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001159 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160
Dmitry Shmidt04949592012-07-19 12:16:46 -07001161 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1162 struct wpa_ssid *ssid;
1163 ssid = wpa_config_get_network(wpa_s->conf,
1164 wpa_s->p2p_persistent_id);
1165 if (ssid && ssid->disabled == 2 &&
1166 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1167 size_t len = os_strlen(ssid->passphrase);
1168 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1169 "on requested persistent group");
1170 os_memcpy(res->passphrase, ssid->passphrase, len);
1171 res->passphrase[len] = '\0';
1172 }
1173 }
1174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 if (wpa_s->create_p2p_iface) {
1176 struct wpa_supplicant *group_wpa_s =
1177 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1178 if (group_wpa_s == NULL) {
1179 wpas_p2p_remove_pending_group_interface(wpa_s);
1180 return;
1181 }
1182 if (group_wpa_s != wpa_s) {
1183 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1184 sizeof(group_wpa_s->p2p_pin));
1185 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1186 }
1187 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1188 wpa_s->pending_interface_name[0] = '\0';
1189 group_wpa_s->p2p_in_provisioning = 1;
1190
1191 if (res->role_go)
1192 wpas_start_wps_go(group_wpa_s, res, 1);
1193 else
1194 wpas_start_wps_enrollee(group_wpa_s, res);
1195 } else {
1196 wpa_s->p2p_in_provisioning = 1;
1197 wpa_s->global->p2p_group_formation = wpa_s;
1198
1199 if (res->role_go)
1200 wpas_start_wps_go(wpa_s, res, 1);
1201 else
1202 wpas_start_wps_enrollee(ctx, res);
1203 }
1204
1205 wpa_s->p2p_long_listen = 0;
1206 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1207
1208 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1209 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1210 (res->peer_config_timeout % 100) * 10000,
1211 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1212}
1213
1214
1215void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1216{
1217 struct wpa_supplicant *wpa_s = ctx;
1218 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1219 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1220
1221 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1222}
1223
1224
1225void wpas_dev_found(void *ctx, const u8 *addr,
1226 const struct p2p_peer_info *info,
1227 int new_device)
1228{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001229#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230 struct wpa_supplicant *wpa_s = ctx;
1231 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001232#define WFD_DEV_INFO_SIZE 9
1233 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001234 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001235#ifdef CONFIG_WIFI_DISPLAY
1236 if (info->wfd_subelems) {
1237 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1238 wpabuf_head(info->wfd_subelems),
1239 WFD_DEV_INFO_SIZE);
1240 }
1241#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1243 " p2p_dev_addr=" MACSTR
1244 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001245 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1247 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1248 sizeof(devtype)),
1249 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001250 info->dev_capab, info->group_capab,
1251 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001252#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253
1254 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1255}
1256
1257
1258static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1259{
1260 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001261
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001262 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1263 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1266}
1267
1268
1269static int wpas_start_listen(void *ctx, unsigned int freq,
1270 unsigned int duration,
1271 const struct wpabuf *probe_resp_ie)
1272{
1273 struct wpa_supplicant *wpa_s = ctx;
1274
1275 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1276
1277 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1278 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1279 "report received Probe Request frames");
1280 return -1;
1281 }
1282
1283 wpa_s->pending_listen_freq = freq;
1284 wpa_s->pending_listen_duration = duration;
1285
1286 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1287 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1288 "to remain on channel (%u MHz) for Listen "
1289 "state", freq);
1290 wpa_s->pending_listen_freq = 0;
1291 return -1;
1292 }
1293 wpa_s->off_channel_freq = 0;
1294 wpa_s->roc_waiting_drv_freq = freq;
1295
1296 return 0;
1297}
1298
1299
1300static void wpas_stop_listen(void *ctx)
1301{
1302 struct wpa_supplicant *wpa_s = ctx;
1303 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1304 wpa_drv_cancel_remain_on_channel(wpa_s);
1305 wpa_s->off_channel_freq = 0;
1306 wpa_s->roc_waiting_drv_freq = 0;
1307 }
1308 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1309 wpa_drv_probe_req_report(wpa_s, 0);
1310}
1311
1312
1313static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1314{
1315 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001316 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317}
1318
1319
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001320/*
1321 * DNS Header section is used only to calculate compression pointers, so the
1322 * contents of this data does not matter, but the length needs to be reserved
1323 * in the virtual packet.
1324 */
1325#define DNS_HEADER_LEN 12
1326
1327/*
1328 * 27-octet in-memory packet from P2P specification containing two implied
1329 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1330 */
1331#define P2P_SD_IN_MEMORY_LEN 27
1332
1333static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1334 u8 **spos, const u8 *end)
1335{
1336 while (*spos < end) {
1337 u8 val = ((*spos)[0] & 0xc0) >> 6;
1338 int len;
1339
1340 if (val == 1 || val == 2) {
1341 /* These are reserved values in RFC 1035 */
1342 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1343 "sequence starting with 0x%x", val);
1344 return -1;
1345 }
1346
1347 if (val == 3) {
1348 u16 offset;
1349 u8 *spos_tmp;
1350
1351 /* Offset */
1352 if (*spos + 2 > end) {
1353 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1354 "DNS offset field");
1355 return -1;
1356 }
1357
1358 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1359 if (offset >= *spos - start) {
1360 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1361 "pointer offset %u", offset);
1362 return -1;
1363 }
1364
1365 (*spos) += 2;
1366 spos_tmp = start + offset;
1367 return p2p_sd_dns_uncompress_label(upos, uend, start,
1368 &spos_tmp,
1369 *spos - 2);
1370 }
1371
1372 /* Label */
1373 len = (*spos)[0] & 0x3f;
1374 if (len == 0)
1375 return 0;
1376
1377 (*spos)++;
1378 if (*spos + len > end) {
1379 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1380 "sequence - no room for label with length "
1381 "%u", len);
1382 return -1;
1383 }
1384
1385 if (*upos + len + 2 > uend)
1386 return -2;
1387
1388 os_memcpy(*upos, *spos, len);
1389 *spos += len;
1390 *upos += len;
1391 (*upos)[0] = '.';
1392 (*upos)++;
1393 (*upos)[0] = '\0';
1394 }
1395
1396 return 0;
1397}
1398
1399
1400/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1401 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1402 * not large enough */
1403static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1404 size_t msg_len, size_t offset)
1405{
1406 /* 27-octet in-memory packet from P2P specification */
1407 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1408 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1409 u8 *tmp, *end, *spos;
1410 char *upos, *uend;
1411 int ret = 0;
1412
1413 if (buf_len < 2)
1414 return -1;
1415 if (offset > msg_len)
1416 return -1;
1417
1418 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1419 if (tmp == NULL)
1420 return -1;
1421 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1422 end = spos + msg_len;
1423 spos += offset;
1424
1425 os_memset(tmp, 0, DNS_HEADER_LEN);
1426 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1427 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1428
1429 upos = buf;
1430 uend = buf + buf_len;
1431
1432 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1433 if (ret) {
1434 os_free(tmp);
1435 return ret;
1436 }
1437
1438 if (upos == buf) {
1439 upos[0] = '.';
1440 upos[1] = '\0';
1441 } else if (upos[-1] == '.')
1442 upos[-1] = '\0';
1443
1444 os_free(tmp);
1445 return 0;
1446}
1447
1448
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449static struct p2p_srv_bonjour *
1450wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1451 const struct wpabuf *query)
1452{
1453 struct p2p_srv_bonjour *bsrv;
1454 size_t len;
1455
1456 len = wpabuf_len(query);
1457 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1458 struct p2p_srv_bonjour, list) {
1459 if (len == wpabuf_len(bsrv->query) &&
1460 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1461 len) == 0)
1462 return bsrv;
1463 }
1464 return NULL;
1465}
1466
1467
1468static struct p2p_srv_upnp *
1469wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1470 const char *service)
1471{
1472 struct p2p_srv_upnp *usrv;
1473
1474 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1475 struct p2p_srv_upnp, list) {
1476 if (version == usrv->version &&
1477 os_strcmp(service, usrv->service) == 0)
1478 return usrv;
1479 }
1480 return NULL;
1481}
1482
1483
1484static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1485 u8 srv_trans_id)
1486{
1487 u8 *len_pos;
1488
1489 if (wpabuf_tailroom(resp) < 5)
1490 return;
1491
1492 /* Length (to be filled) */
1493 len_pos = wpabuf_put(resp, 2);
1494 wpabuf_put_u8(resp, srv_proto);
1495 wpabuf_put_u8(resp, srv_trans_id);
1496 /* Status Code */
1497 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1498 /* Response Data: empty */
1499 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1500}
1501
1502
1503static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1504 struct wpabuf *resp, u8 srv_trans_id)
1505{
1506 struct p2p_srv_bonjour *bsrv;
1507 u8 *len_pos;
1508
1509 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1510
1511 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1512 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1513 return;
1514 }
1515
1516 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1517 struct p2p_srv_bonjour, list) {
1518 if (wpabuf_tailroom(resp) <
1519 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1520 return;
1521 /* Length (to be filled) */
1522 len_pos = wpabuf_put(resp, 2);
1523 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1524 wpabuf_put_u8(resp, srv_trans_id);
1525 /* Status Code */
1526 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1527 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1528 wpabuf_head(bsrv->resp),
1529 wpabuf_len(bsrv->resp));
1530 /* Response Data */
1531 wpabuf_put_buf(resp, bsrv->query); /* Key */
1532 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1533 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1534 2);
1535 }
1536}
1537
1538
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001539static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1540 size_t query_len)
1541{
1542 char str_rx[256], str_srv[256];
1543
1544 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1545 return 0; /* Too short to include DNS Type and Version */
1546 if (os_memcmp(query + query_len - 3,
1547 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1548 3) != 0)
1549 return 0; /* Mismatch in DNS Type or Version */
1550 if (query_len == wpabuf_len(bsrv->query) &&
1551 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1552 return 1; /* Binary match */
1553
1554 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1555 0))
1556 return 0; /* Failed to uncompress query */
1557 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1558 wpabuf_head(bsrv->query),
1559 wpabuf_len(bsrv->query) - 3, 0))
1560 return 0; /* Failed to uncompress service */
1561
1562 return os_strcmp(str_rx, str_srv) == 0;
1563}
1564
1565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1567 struct wpabuf *resp, u8 srv_trans_id,
1568 const u8 *query, size_t query_len)
1569{
1570 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001572 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573
1574 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1575 query, query_len);
1576 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1577 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1578 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1579 srv_trans_id);
1580 return;
1581 }
1582
1583 if (query_len == 0) {
1584 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1585 return;
1586 }
1587
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001588 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1589 struct p2p_srv_bonjour, list) {
1590 if (!match_bonjour_query(bsrv, query, query_len))
1591 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001593 if (wpabuf_tailroom(resp) <
1594 5 + query_len + wpabuf_len(bsrv->resp))
1595 return;
1596
1597 matches++;
1598
1599 /* Length (to be filled) */
1600 len_pos = wpabuf_put(resp, 2);
1601 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1602 wpabuf_put_u8(resp, srv_trans_id);
1603
1604 /* Status Code */
1605 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1606 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1607 wpabuf_head(bsrv->resp),
1608 wpabuf_len(bsrv->resp));
1609
1610 /* Response Data */
1611 wpabuf_put_data(resp, query, query_len); /* Key */
1612 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1613
1614 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1615 }
1616
1617 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001618 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1619 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001620 if (wpabuf_tailroom(resp) < 5)
1621 return;
1622
1623 /* Length (to be filled) */
1624 len_pos = wpabuf_put(resp, 2);
1625 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1626 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627
1628 /* Status Code */
1629 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1630 /* Response Data: empty */
1631 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1632 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634}
1635
1636
1637static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1638 struct wpabuf *resp, u8 srv_trans_id)
1639{
1640 struct p2p_srv_upnp *usrv;
1641 u8 *len_pos;
1642
1643 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1644
1645 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1646 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1647 return;
1648 }
1649
1650 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1651 struct p2p_srv_upnp, list) {
1652 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1653 return;
1654
1655 /* Length (to be filled) */
1656 len_pos = wpabuf_put(resp, 2);
1657 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1658 wpabuf_put_u8(resp, srv_trans_id);
1659
1660 /* Status Code */
1661 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1662 /* Response Data */
1663 wpabuf_put_u8(resp, usrv->version);
1664 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1665 usrv->service);
1666 wpabuf_put_str(resp, usrv->service);
1667 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1668 2);
1669 }
1670}
1671
1672
1673static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1674 struct wpabuf *resp, u8 srv_trans_id,
1675 const u8 *query, size_t query_len)
1676{
1677 struct p2p_srv_upnp *usrv;
1678 u8 *len_pos;
1679 u8 version;
1680 char *str;
1681 int count = 0;
1682
1683 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1684 query, query_len);
1685
1686 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1687 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1688 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1689 srv_trans_id);
1690 return;
1691 }
1692
1693 if (query_len == 0) {
1694 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1695 return;
1696 }
1697
1698 if (wpabuf_tailroom(resp) < 5)
1699 return;
1700
1701 /* Length (to be filled) */
1702 len_pos = wpabuf_put(resp, 2);
1703 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1704 wpabuf_put_u8(resp, srv_trans_id);
1705
1706 version = query[0];
1707 str = os_malloc(query_len);
1708 if (str == NULL)
1709 return;
1710 os_memcpy(str, query + 1, query_len - 1);
1711 str[query_len - 1] = '\0';
1712
1713 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1714 struct p2p_srv_upnp, list) {
1715 if (version != usrv->version)
1716 continue;
1717
1718 if (os_strcmp(str, "ssdp:all") != 0 &&
1719 os_strstr(usrv->service, str) == NULL)
1720 continue;
1721
1722 if (wpabuf_tailroom(resp) < 2)
1723 break;
1724 if (count == 0) {
1725 /* Status Code */
1726 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1727 /* Response Data */
1728 wpabuf_put_u8(resp, version);
1729 } else
1730 wpabuf_put_u8(resp, ',');
1731
1732 count++;
1733
1734 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1735 usrv->service);
1736 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1737 break;
1738 wpabuf_put_str(resp, usrv->service);
1739 }
1740 os_free(str);
1741
1742 if (count == 0) {
1743 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1744 "available");
1745 /* Status Code */
1746 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1747 /* Response Data: empty */
1748 }
1749
1750 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1751}
1752
1753
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001754#ifdef CONFIG_WIFI_DISPLAY
1755static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1756 struct wpabuf *resp, u8 srv_trans_id,
1757 const u8 *query, size_t query_len)
1758{
1759 const u8 *pos;
1760 u8 role;
1761 u8 *len_pos;
1762
1763 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1764
1765 if (!wpa_s->global->wifi_display) {
1766 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1767 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1768 srv_trans_id);
1769 return;
1770 }
1771
1772 if (query_len < 1) {
1773 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1774 "Role");
1775 return;
1776 }
1777
1778 if (wpabuf_tailroom(resp) < 5)
1779 return;
1780
1781 pos = query;
1782 role = *pos++;
1783 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1784
1785 /* TODO: role specific handling */
1786
1787 /* Length (to be filled) */
1788 len_pos = wpabuf_put(resp, 2);
1789 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1790 wpabuf_put_u8(resp, srv_trans_id);
1791 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1792
1793 while (pos < query + query_len) {
1794 if (*pos < MAX_WFD_SUBELEMS &&
1795 wpa_s->global->wfd_subelem[*pos] &&
1796 wpabuf_tailroom(resp) >=
1797 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1798 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1799 "subelement %u", *pos);
1800 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1801 }
1802 pos++;
1803 }
1804
1805 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1806}
1807#endif /* CONFIG_WIFI_DISPLAY */
1808
1809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1811 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1812{
1813 struct wpa_supplicant *wpa_s = ctx;
1814 const u8 *pos = tlvs;
1815 const u8 *end = tlvs + tlvs_len;
1816 const u8 *tlv_end;
1817 u16 slen;
1818 struct wpabuf *resp;
1819 u8 srv_proto, srv_trans_id;
1820 size_t buf_len;
1821 char *buf;
1822
1823 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1824 tlvs, tlvs_len);
1825 buf_len = 2 * tlvs_len + 1;
1826 buf = os_malloc(buf_len);
1827 if (buf) {
1828 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1829 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1830 MACSTR " %u %u %s",
1831 freq, MAC2STR(sa), dialog_token, update_indic,
1832 buf);
1833 os_free(buf);
1834 }
1835
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001836 if (wpa_s->p2p_sd_over_ctrl_iface) {
1837 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1838 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001840 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001841
1842 resp = wpabuf_alloc(10000);
1843 if (resp == NULL)
1844 return;
1845
1846 while (pos + 1 < end) {
1847 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1848 slen = WPA_GET_LE16(pos);
1849 pos += 2;
1850 if (pos + slen > end || slen < 2) {
1851 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1852 "length");
1853 wpabuf_free(resp);
1854 return;
1855 }
1856 tlv_end = pos + slen;
1857
1858 srv_proto = *pos++;
1859 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1860 srv_proto);
1861 srv_trans_id = *pos++;
1862 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1863 srv_trans_id);
1864
1865 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1866 pos, tlv_end - pos);
1867
1868
1869 if (wpa_s->force_long_sd) {
1870 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1871 "response");
1872 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1873 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1874 goto done;
1875 }
1876
1877 switch (srv_proto) {
1878 case P2P_SERV_ALL_SERVICES:
1879 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1880 "for all services");
1881 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1882 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1883 wpa_printf(MSG_DEBUG, "P2P: No service "
1884 "discovery protocols available");
1885 wpas_sd_add_proto_not_avail(
1886 resp, P2P_SERV_ALL_SERVICES,
1887 srv_trans_id);
1888 break;
1889 }
1890 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1891 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1892 break;
1893 case P2P_SERV_BONJOUR:
1894 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1895 pos, tlv_end - pos);
1896 break;
1897 case P2P_SERV_UPNP:
1898 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1899 pos, tlv_end - pos);
1900 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001901#ifdef CONFIG_WIFI_DISPLAY
1902 case P2P_SERV_WIFI_DISPLAY:
1903 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1904 pos, tlv_end - pos);
1905 break;
1906#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907 default:
1908 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1909 "protocol %u", srv_proto);
1910 wpas_sd_add_proto_not_avail(resp, srv_proto,
1911 srv_trans_id);
1912 break;
1913 }
1914
1915 pos = tlv_end;
1916 }
1917
1918done:
1919 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1920 update_indic, tlvs, tlvs_len);
1921
1922 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1923
1924 wpabuf_free(resp);
1925}
1926
1927
1928void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1929 const u8 *tlvs, size_t tlvs_len)
1930{
1931 struct wpa_supplicant *wpa_s = ctx;
1932 const u8 *pos = tlvs;
1933 const u8 *end = tlvs + tlvs_len;
1934 const u8 *tlv_end;
1935 u16 slen;
1936 size_t buf_len;
1937 char *buf;
1938
1939 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1940 tlvs, tlvs_len);
1941 if (tlvs_len > 1500) {
1942 /* TODO: better way for handling this */
1943 wpa_msg_ctrl(wpa_s, MSG_INFO,
1944 P2P_EVENT_SERV_DISC_RESP MACSTR
1945 " %u <long response: %u bytes>",
1946 MAC2STR(sa), update_indic,
1947 (unsigned int) tlvs_len);
1948 } else {
1949 buf_len = 2 * tlvs_len + 1;
1950 buf = os_malloc(buf_len);
1951 if (buf) {
1952 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1953 wpa_msg_ctrl(wpa_s, MSG_INFO,
1954 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1955 MAC2STR(sa), update_indic, buf);
1956 os_free(buf);
1957 }
1958 }
1959
1960 while (pos < end) {
1961 u8 srv_proto, srv_trans_id, status;
1962
1963 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1964 slen = WPA_GET_LE16(pos);
1965 pos += 2;
1966 if (pos + slen > end || slen < 3) {
1967 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1968 "length");
1969 return;
1970 }
1971 tlv_end = pos + slen;
1972
1973 srv_proto = *pos++;
1974 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1975 srv_proto);
1976 srv_trans_id = *pos++;
1977 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1978 srv_trans_id);
1979 status = *pos++;
1980 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1981 status);
1982
1983 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1984 pos, tlv_end - pos);
1985
1986 pos = tlv_end;
1987 }
1988
1989 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1990}
1991
1992
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001993u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1994 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995{
1996 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001997 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001999 return 0;
2000 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001}
2002
2003
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002004u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2005 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002006{
2007 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002008 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009
2010 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2011 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002012 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2014 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2015 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2016 wpabuf_put_u8(tlvs, version);
2017 wpabuf_put_str(tlvs, query);
2018 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2019 wpabuf_free(tlvs);
2020 return ret;
2021}
2022
2023
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002024#ifdef CONFIG_WIFI_DISPLAY
2025
2026static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2027 const struct wpabuf *tlvs)
2028{
2029 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2030 return 0;
2031 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2032 return 0;
2033 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2034}
2035
2036
2037#define MAX_WFD_SD_SUBELEMS 20
2038
2039static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2040 const char *subelems)
2041{
2042 u8 *len;
2043 const char *pos;
2044 int val;
2045 int count = 0;
2046
2047 len = wpabuf_put(tlvs, 2);
2048 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2049 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2050
2051 wpabuf_put_u8(tlvs, role);
2052
2053 pos = subelems;
2054 while (*pos) {
2055 val = atoi(pos);
2056 if (val >= 0 && val < 256) {
2057 wpabuf_put_u8(tlvs, val);
2058 count++;
2059 if (count == MAX_WFD_SD_SUBELEMS)
2060 break;
2061 }
2062 pos = os_strchr(pos + 1, ',');
2063 if (pos == NULL)
2064 break;
2065 pos++;
2066 }
2067
2068 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2069}
2070
2071
2072u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2073 const u8 *dst, const char *role)
2074{
2075 struct wpabuf *tlvs;
2076 u64 ret;
2077 const char *subelems;
2078 u8 id = 1;
2079
2080 subelems = os_strchr(role, ' ');
2081 if (subelems == NULL)
2082 return 0;
2083 subelems++;
2084
2085 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2086 if (tlvs == NULL)
2087 return 0;
2088
2089 if (os_strstr(role, "[source]"))
2090 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2091 if (os_strstr(role, "[pri-sink]"))
2092 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2093 if (os_strstr(role, "[sec-sink]"))
2094 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2095 if (os_strstr(role, "[source+sink]"))
2096 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2097
2098 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2099 wpabuf_free(tlvs);
2100 return ret;
2101}
2102
2103#endif /* CONFIG_WIFI_DISPLAY */
2104
2105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002106int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107{
2108 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002109 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2111 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112 return p2p_sd_cancel_request(wpa_s->global->p2p,
2113 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114}
2115
2116
2117void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2118 const u8 *dst, u8 dialog_token,
2119 const struct wpabuf *resp_tlvs)
2120{
2121 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2122 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2123 resp_tlvs);
2124 return;
2125 }
2126 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2127 return;
2128 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2129 resp_tlvs);
2130}
2131
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002132#ifdef ANDROID_P2P
2133void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2134#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002136#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137{
2138 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2139 wpa_drv_p2p_service_update(wpa_s);
2140 return;
2141 }
2142 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002143#ifdef ANDROID_P2P
2144 p2p_sd_service_update(wpa_s->global->p2p, action);
2145#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002147#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148}
2149
2150
2151static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2152{
2153 dl_list_del(&bsrv->list);
2154 wpabuf_free(bsrv->query);
2155 wpabuf_free(bsrv->resp);
2156 os_free(bsrv);
2157}
2158
2159
2160static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2161{
2162 dl_list_del(&usrv->list);
2163 os_free(usrv->service);
2164 os_free(usrv);
2165}
2166
2167
2168void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2169{
2170 struct p2p_srv_bonjour *bsrv, *bn;
2171 struct p2p_srv_upnp *usrv, *un;
2172
2173 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2174 struct p2p_srv_bonjour, list)
2175 wpas_p2p_srv_bonjour_free(bsrv);
2176
2177 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2178 struct p2p_srv_upnp, list)
2179 wpas_p2p_srv_upnp_free(usrv);
2180
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002181#ifdef ANDROID_P2P
2182 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2183#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002185#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002186}
2187
2188
2189int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2190 struct wpabuf *query, struct wpabuf *resp)
2191{
2192 struct p2p_srv_bonjour *bsrv;
2193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 bsrv = os_zalloc(sizeof(*bsrv));
2195 if (bsrv == NULL)
2196 return -1;
2197 bsrv->query = query;
2198 bsrv->resp = resp;
2199 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2200
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002201#ifdef ANDROID_P2P
2202 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2203#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002205#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002206 return 0;
2207}
2208
2209
2210int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2211 const struct wpabuf *query)
2212{
2213 struct p2p_srv_bonjour *bsrv;
2214
2215 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2216 if (bsrv == NULL)
2217 return -1;
2218 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002219#ifdef ANDROID_P2P
2220 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2221#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002223#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002224 return 0;
2225}
2226
2227
2228int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2229 const char *service)
2230{
2231 struct p2p_srv_upnp *usrv;
2232
2233 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2234 return 0; /* Already listed */
2235 usrv = os_zalloc(sizeof(*usrv));
2236 if (usrv == NULL)
2237 return -1;
2238 usrv->version = version;
2239 usrv->service = os_strdup(service);
2240 if (usrv->service == NULL) {
2241 os_free(usrv);
2242 return -1;
2243 }
2244 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2245
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002246#ifdef ANDROID_P2P
2247 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2248#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002250#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251 return 0;
2252}
2253
2254
2255int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2256 const char *service)
2257{
2258 struct p2p_srv_upnp *usrv;
2259
2260 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2261 if (usrv == NULL)
2262 return -1;
2263 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002264#ifdef ANDROID_P2P
2265 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2266#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002268#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 return 0;
2270}
2271
2272
2273static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2274 const u8 *peer, const char *params,
2275 unsigned int generated_pin)
2276{
2277 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2278 MAC2STR(peer), generated_pin, params);
2279}
2280
2281
2282static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2283 const u8 *peer, const char *params)
2284{
2285 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2286 MAC2STR(peer), params);
2287}
2288
2289
2290void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2291 const u8 *dev_addr, const u8 *pri_dev_type,
2292 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002293 u8 dev_capab, u8 group_capab, const u8 *group_id,
2294 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295{
2296 struct wpa_supplicant *wpa_s = ctx;
2297 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002298 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 u8 empty_dev_type[8];
2300 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002301 struct wpa_supplicant *group = NULL;
2302
2303 if (group_id) {
2304 for (group = wpa_s->global->ifaces; group; group = group->next)
2305 {
2306 struct wpa_ssid *s = group->current_ssid;
2307 if (s != NULL &&
2308 s->mode == WPAS_MODE_P2P_GO &&
2309 group_id_len - ETH_ALEN == s->ssid_len &&
2310 os_memcmp(group_id + ETH_ALEN, s->ssid,
2311 s->ssid_len) == 0)
2312 break;
2313 }
2314 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315
2316 if (pri_dev_type == NULL) {
2317 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2318 pri_dev_type = empty_dev_type;
2319 }
2320 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2321 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002322 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323 MAC2STR(dev_addr),
2324 wps_dev_type_bin2str(pri_dev_type, devtype,
2325 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002326 dev_name, supp_config_methods, dev_capab, group_capab,
2327 group ? " group=" : "",
2328 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331 if (config_methods & WPS_CONFIG_DISPLAY) {
2332 generated_pin = wps_generate_pin();
2333 wpas_prov_disc_local_display(wpa_s, peer, params,
2334 generated_pin);
2335 } else if (config_methods & WPS_CONFIG_KEYPAD)
2336 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2337 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2338 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2339 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002340
2341 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2342 P2P_PROV_DISC_SUCCESS,
2343 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344}
2345
2346
2347void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2348{
2349 struct wpa_supplicant *wpa_s = ctx;
2350 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002351 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002353 if (wpa_s->pending_pd_before_join &&
2354 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2355 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2356 wpa_s->pending_pd_before_join = 0;
2357 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2358 "join-existing-group operation");
2359 wpas_p2p_join_start(wpa_s);
2360 return;
2361 }
2362
Dmitry Shmidt04949592012-07-19 12:16:46 -07002363 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2364 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2365 os_snprintf(params, sizeof(params), " peer_go=%d",
2366 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2367 else
2368 params[0] = '\0';
2369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002370 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002371 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 else if (config_methods & WPS_CONFIG_KEYPAD) {
2373 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002374 wpas_prov_disc_local_display(wpa_s, peer, params,
2375 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002376 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002377 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2378 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379
Jouni Malinen75ecf522011-06-27 15:19:46 -07002380 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2381 P2P_PROV_DISC_SUCCESS,
2382 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383}
2384
2385
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2387 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002388{
2389 struct wpa_supplicant *wpa_s = ctx;
2390
Dmitry Shmidt04949592012-07-19 12:16:46 -07002391 if (wpa_s->p2p_fallback_to_go_neg) {
2392 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2393 "failed - fall back to GO Negotiation");
2394 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2395 return;
2396 }
2397
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002398 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002399 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002400 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2401 "join-existing-group operation (no ACK for PD "
2402 "Req attempts)");
2403 wpas_p2p_join_start(wpa_s);
2404 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002405 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002406
Dmitry Shmidt04949592012-07-19 12:16:46 -07002407 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2408 " p2p_dev_addr=" MACSTR " status=%d",
2409 MAC2STR(peer), status);
2410
Jouni Malinen75ecf522011-06-27 15:19:46 -07002411 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2412 status, 0, 0);
2413}
2414
2415
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2417 const u8 *go_dev_addr, const u8 *ssid,
2418 size_t ssid_len, int *go, u8 *group_bssid,
2419 int *force_freq, int persistent_group)
2420{
2421 struct wpa_supplicant *wpa_s = ctx;
2422 struct wpa_ssid *s;
2423 u8 cur_bssid[ETH_ALEN];
2424 int res;
2425 struct wpa_supplicant *grp;
2426
2427 if (!persistent_group) {
2428 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2429 " to join an active group", MAC2STR(sa));
2430 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2431 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2432 == 0 ||
2433 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2434 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2435 "authorized invitation");
2436 goto accept_inv;
2437 }
2438 /*
2439 * Do not accept the invitation automatically; notify user and
2440 * request approval.
2441 */
2442 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2443 }
2444
2445 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2446 if (grp) {
2447 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2448 "running persistent group");
2449 if (*go)
2450 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2451 goto accept_inv;
2452 }
2453
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002454 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2455 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2456 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2457 "invitation to re-invoke a persistent group");
2458 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002459 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2460
2461 for (s = wpa_s->conf->ssid; s; s = s->next) {
2462 if (s->disabled == 2 &&
2463 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2464 s->ssid_len == ssid_len &&
2465 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2466 break;
2467 }
2468
2469 if (!s) {
2470 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2471 " requested reinvocation of an unknown group",
2472 MAC2STR(sa));
2473 return P2P_SC_FAIL_UNKNOWN_GROUP;
2474 }
2475
2476 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2477 *go = 1;
2478 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2479 wpa_printf(MSG_DEBUG, "P2P: The only available "
2480 "interface is already in use - reject "
2481 "invitation");
2482 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2483 }
2484 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2485 } else if (s->mode == WPAS_MODE_P2P_GO) {
2486 *go = 1;
2487 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2488 {
2489 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2490 "interface address for the group");
2491 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2492 }
2493 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2494 ETH_ALEN);
2495 }
2496
2497accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002498 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2501 wpa_s->assoc_freq) {
2502 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2503 "the channel we are already using");
2504 *force_freq = wpa_s->assoc_freq;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002505 wpas_p2p_set_own_freq_preference(wpa_s, wpa_s->assoc_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506 }
2507
2508 res = wpa_drv_shared_freq(wpa_s);
2509 if (res > 0) {
2510 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2511 "with the channel we are already using on a "
2512 "shared interface");
2513 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002514 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515 }
2516
2517 return P2P_SC_SUCCESS;
2518}
2519
2520
2521static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2522 const u8 *ssid, size_t ssid_len,
2523 const u8 *go_dev_addr, u8 status,
2524 int op_freq)
2525{
2526 struct wpa_supplicant *wpa_s = ctx;
2527 struct wpa_ssid *s;
2528
2529 for (s = wpa_s->conf->ssid; s; s = s->next) {
2530 if (s->disabled == 2 &&
2531 s->ssid_len == ssid_len &&
2532 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2533 break;
2534 }
2535
2536 if (status == P2P_SC_SUCCESS) {
2537 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2538 " was accepted; op_freq=%d MHz",
2539 MAC2STR(sa), op_freq);
2540 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002541 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002543 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002545 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002547 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548 }
2549 return;
2550 }
2551
2552 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2553 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2554 " was rejected (status %u)", MAC2STR(sa), status);
2555 return;
2556 }
2557
2558 if (!s) {
2559 if (bssid) {
2560 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2561 "sa=" MACSTR " go_dev_addr=" MACSTR
2562 " bssid=" MACSTR " unknown-network",
2563 MAC2STR(sa), MAC2STR(go_dev_addr),
2564 MAC2STR(bssid));
2565 } else {
2566 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2567 "sa=" MACSTR " go_dev_addr=" MACSTR
2568 " unknown-network",
2569 MAC2STR(sa), MAC2STR(go_dev_addr));
2570 }
2571 return;
2572 }
2573
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002574 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2575 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2576 MACSTR " persistent=%d freq=%d",
2577 MAC2STR(sa), s->id, op_freq);
2578 } else {
2579 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2580 MACSTR " persistent=%d", MAC2STR(sa), s->id);
2581 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582}
2583
2584
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002585static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2586 struct wpa_ssid *ssid,
2587 const u8 *peer)
2588{
2589 size_t i;
2590
2591 if (ssid == NULL)
2592 return;
2593
2594 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2595 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2596 ETH_ALEN) == 0)
2597 break;
2598 }
2599 if (i >= ssid->num_p2p_clients) {
2600 if (ssid->mode != WPAS_MODE_P2P_GO &&
2601 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2602 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2603 "due to invitation result", ssid->id);
2604 wpas_notify_network_removed(wpa_s, ssid);
2605 wpa_config_remove_network(wpa_s->conf, ssid->id);
2606 return;
2607 }
2608 return; /* Peer not found in client list */
2609 }
2610
2611 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2612 "group %d client list due to invitation result",
2613 MAC2STR(peer), ssid->id);
2614 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2615 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2616 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2617 ssid->num_p2p_clients--;
2618#ifndef CONFIG_NO_CONFIG_WRITE
2619 if (wpa_s->parent->conf->update_config &&
2620 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2621 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2622#endif /* CONFIG_NO_CONFIG_WRITE */
2623}
2624
2625
2626static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2627 const u8 *peer)
2628{
2629 struct wpa_ssid *ssid;
2630
2631 wpa_s = wpa_s->global->p2p_invite_group;
2632 if (wpa_s == NULL)
2633 return; /* No known invitation group */
2634 ssid = wpa_s->current_ssid;
2635 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2636 !ssid->p2p_persistent_group)
2637 return; /* Not operating as a GO in persistent group */
2638 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2639 ssid->ssid, ssid->ssid_len);
2640 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2641}
2642
2643
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002644static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002645 const struct p2p_channels *channels,
2646 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647{
2648 struct wpa_supplicant *wpa_s = ctx;
2649 struct wpa_ssid *ssid;
2650
2651 if (bssid) {
2652 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2653 "status=%d " MACSTR,
2654 status, MAC2STR(bssid));
2655 } else {
2656 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2657 "status=%d ", status);
2658 }
2659 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2660
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002661 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2662 status, MAC2STR(peer));
2663 if (wpa_s->pending_invite_ssid_id == -1) {
2664 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2665 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002666 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002667 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002668
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002669 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2670 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2671 "invitation exchange to indicate readiness for "
2672 "re-invocation");
2673 }
2674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002676 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2677 ssid = wpa_config_get_network(
2678 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2679 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2680 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681 wpas_p2p_remove_pending_group_interface(wpa_s);
2682 return;
2683 }
2684
2685 ssid = wpa_config_get_network(wpa_s->conf,
2686 wpa_s->pending_invite_ssid_id);
2687 if (ssid == NULL) {
2688 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2689 "data matching with invitation");
2690 return;
2691 }
2692
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002693 /*
2694 * The peer could have missed our ctrl::ack frame for Invitation
2695 * Response and continue retransmitting the frame. To reduce the
2696 * likelihood of the peer not getting successful TX status for the
2697 * Invitation Response frame, wait a short time here before starting
2698 * the persistent group so that we will remain on the current channel to
2699 * acknowledge any possible retransmission from the peer.
2700 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002701#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002702 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2703 "starting persistent group");
2704 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002705#else
2706 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2707 "starting persistent group");
2708 os_sleep(0, 100000);
2709#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002712 ssid->mode == WPAS_MODE_P2P_GO,
2713 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002714 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715}
2716
2717
Dmitry Shmidt04949592012-07-19 12:16:46 -07002718static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2719 unsigned int freq)
2720{
2721 unsigned int i;
2722
2723 if (global->p2p_disallow_freq == NULL)
2724 return 0;
2725
2726 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2727 if (freq >= global->p2p_disallow_freq[i].min &&
2728 freq <= global->p2p_disallow_freq[i].max)
2729 return 1;
2730 }
2731
2732 return 0;
2733}
2734
2735
2736static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2737{
2738 reg->channel[reg->channels] = chan;
2739 reg->channels++;
2740}
2741
2742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2744 struct p2p_channels *chan)
2745{
2746 int i, cla = 0;
2747
2748 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2749 "band");
2750
2751 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2752 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002753 chan->reg_class[cla].channels = 0;
2754 for (i = 0; i < 11; i++) {
2755 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2756 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2757 }
2758 if (chan->reg_class[cla].channels)
2759 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760
2761 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2762 "band");
2763
2764 /* Operating class 115 - 5 GHz, channels 36-48 */
2765 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002766 chan->reg_class[cla].channels = 0;
2767 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2768 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2769 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2770 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2771 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2772 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2773 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2774 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2775 if (chan->reg_class[cla].channels)
2776 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777
2778 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2779 "band");
2780
2781 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2782 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002783 chan->reg_class[cla].channels = 0;
2784 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2785 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2786 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2787 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2788 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2789 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2790 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2791 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2792 if (chan->reg_class[cla].channels)
2793 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794
2795 chan->reg_classes = cla;
2796 return 0;
2797}
2798
2799
2800static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2801 u16 num_modes,
2802 enum hostapd_hw_mode mode)
2803{
2804 u16 i;
2805
2806 for (i = 0; i < num_modes; i++) {
2807 if (modes[i].mode == mode)
2808 return &modes[i];
2809 }
2810
2811 return NULL;
2812}
2813
2814
Dmitry Shmidt04949592012-07-19 12:16:46 -07002815static int has_channel(struct wpa_global *global,
2816 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817{
2818 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002819 unsigned int freq;
2820
2821 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2822 chan * 5;
2823 if (wpas_p2p_disallowed_freq(global, freq))
2824 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825
2826 for (i = 0; i < mode->num_channels; i++) {
2827 if (mode->channels[i].chan == chan) {
2828 if (flags)
2829 *flags = mode->channels[i].flag;
2830 return !(mode->channels[i].flag &
2831 (HOSTAPD_CHAN_DISABLED |
2832 HOSTAPD_CHAN_PASSIVE_SCAN |
2833 HOSTAPD_CHAN_NO_IBSS |
2834 HOSTAPD_CHAN_RADAR));
2835 }
2836 }
2837
2838 return 0;
2839}
2840
2841
2842struct p2p_oper_class_map {
2843 enum hostapd_hw_mode mode;
2844 u8 op_class;
2845 u8 min_chan;
2846 u8 max_chan;
2847 u8 inc;
2848 enum { BW20, BW40PLUS, BW40MINUS } bw;
2849};
2850
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002851static struct p2p_oper_class_map op_class[] = {
2852 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002853#if 0 /* Do not enable HT40 on 2 GHz for now */
2854 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2855 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2856#endif
2857 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2858 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2859 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2860 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2861 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2862 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2863 { -1, 0, 0, 0, 0, BW20 }
2864};
2865
2866
2867static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2868 struct hostapd_hw_modes *mode,
2869 u8 channel, u8 bw)
2870{
2871 int flag;
2872
2873 if (!has_channel(wpa_s->global, mode, channel, &flag))
2874 return -1;
2875 if (bw == BW40MINUS &&
2876 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2877 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2878 return 0;
2879 if (bw == BW40PLUS &&
2880 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2881 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2882 return 0;
2883 return 1;
2884}
2885
2886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2888 struct p2p_channels *chan)
2889{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002890 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002893 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2895 "of all supported channels; assume dualband "
2896 "support");
2897 return wpas_p2p_default_channels(wpa_s, chan);
2898 }
2899
2900 cla = 0;
2901
2902 for (op = 0; op_class[op].op_class; op++) {
2903 struct p2p_oper_class_map *o = &op_class[op];
2904 u8 ch;
2905 struct p2p_reg_class *reg = NULL;
2906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002907 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 if (mode == NULL)
2909 continue;
2910 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002911 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002912 continue;
2913 if (reg == NULL) {
2914 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2915 "class %u", o->op_class);
2916 reg = &chan->reg_class[cla];
2917 cla++;
2918 reg->reg_class = o->op_class;
2919 }
2920 reg->channel[reg->channels] = ch;
2921 reg->channels++;
2922 }
2923 if (reg) {
2924 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2925 reg->channel, reg->channels);
2926 }
2927 }
2928
2929 chan->reg_classes = cla;
2930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931 return 0;
2932}
2933
2934
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002935int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2936 struct hostapd_hw_modes *mode, u8 channel)
2937{
2938 int op, ret;
2939
2940 for (op = 0; op_class[op].op_class; op++) {
2941 struct p2p_oper_class_map *o = &op_class[op];
2942 u8 ch;
2943
2944 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2945 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2946 o->bw == BW20 || ch != channel)
2947 continue;
2948 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2949 if (ret < 0)
2950 continue;
2951 else if (ret > 0)
2952 return (o->bw == BW40MINUS) ? -1 : 1;
2953 else
2954 return 0;
2955 }
2956 }
2957 return 0;
2958}
2959
2960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2962 size_t buf_len)
2963{
2964 struct wpa_supplicant *wpa_s = ctx;
2965
2966 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2967 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2968 break;
2969 }
2970 if (wpa_s == NULL)
2971 return -1;
2972
2973 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2974}
2975
2976
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002977static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2978{
2979 struct wpa_supplicant *wpa_s = ctx;
2980
2981 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2982 struct wpa_ssid *ssid = wpa_s->current_ssid;
2983 if (ssid == NULL)
2984 continue;
2985 if (ssid->mode != WPAS_MODE_INFRA)
2986 continue;
2987 if (wpa_s->wpa_state != WPA_COMPLETED &&
2988 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2989 continue;
2990 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2991 return 1;
2992 }
2993
2994 return 0;
2995}
2996
2997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998/**
2999 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3000 * @global: Pointer to global data from wpa_supplicant_init()
3001 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3002 * Returns: 0 on success, -1 on failure
3003 */
3004int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3005{
3006 struct p2p_config p2p;
3007 unsigned int r;
3008 int i;
3009
3010 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3011 return 0;
3012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003013 if (global->p2p)
3014 return 0;
3015
3016 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3017 struct p2p_params params;
3018
3019 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3020 os_memset(&params, 0, sizeof(params));
3021 params.dev_name = wpa_s->conf->device_name;
3022 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3023 WPS_DEV_TYPE_LEN);
3024 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3025 os_memcpy(params.sec_dev_type,
3026 wpa_s->conf->sec_device_type,
3027 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3028
3029 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3030 return -1;
3031
3032 return 0;
3033 }
3034
3035 os_memset(&p2p, 0, sizeof(p2p));
3036 p2p.msg_ctx = wpa_s;
3037 p2p.cb_ctx = wpa_s;
3038 p2p.p2p_scan = wpas_p2p_scan;
3039 p2p.send_action = wpas_send_action;
3040 p2p.send_action_done = wpas_send_action_done;
3041 p2p.go_neg_completed = wpas_go_neg_completed;
3042 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3043 p2p.dev_found = wpas_dev_found;
3044 p2p.dev_lost = wpas_dev_lost;
3045 p2p.start_listen = wpas_start_listen;
3046 p2p.stop_listen = wpas_stop_listen;
3047 p2p.send_probe_resp = wpas_send_probe_resp;
3048 p2p.sd_request = wpas_sd_request;
3049 p2p.sd_response = wpas_sd_response;
3050 p2p.prov_disc_req = wpas_prov_disc_req;
3051 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003052 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 p2p.invitation_process = wpas_invitation_process;
3054 p2p.invitation_received = wpas_invitation_received;
3055 p2p.invitation_result = wpas_invitation_result;
3056 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003057 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058
3059 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003060 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 p2p.dev_name = wpa_s->conf->device_name;
3062 p2p.manufacturer = wpa_s->conf->manufacturer;
3063 p2p.model_name = wpa_s->conf->model_name;
3064 p2p.model_number = wpa_s->conf->model_number;
3065 p2p.serial_number = wpa_s->conf->serial_number;
3066 if (wpa_s->wps) {
3067 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3068 p2p.config_methods = wpa_s->wps->config_methods;
3069 }
3070
3071 if (wpa_s->conf->p2p_listen_reg_class &&
3072 wpa_s->conf->p2p_listen_channel) {
3073 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3074 p2p.channel = wpa_s->conf->p2p_listen_channel;
3075 } else {
3076 p2p.reg_class = 81;
3077 /*
3078 * Pick one of the social channels randomly as the listen
3079 * channel.
3080 */
3081 os_get_random((u8 *) &r, sizeof(r));
3082 p2p.channel = 1 + (r % 3) * 5;
3083 }
3084 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3085
3086 if (wpa_s->conf->p2p_oper_reg_class &&
3087 wpa_s->conf->p2p_oper_channel) {
3088 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3089 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3090 p2p.cfg_op_channel = 1;
3091 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3092 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3093
3094 } else {
3095 p2p.op_reg_class = 81;
3096 /*
3097 * Use random operation channel from (1, 6, 11) if no other
3098 * preference is indicated.
3099 */
3100 os_get_random((u8 *) &r, sizeof(r));
3101 p2p.op_channel = 1 + (r % 3) * 5;
3102 p2p.cfg_op_channel = 0;
3103 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3104 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3105 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003106
3107 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3108 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3109 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3110 }
3111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3113 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3114 p2p.country[2] = 0x04;
3115 } else
3116 os_memcpy(p2p.country, "XX\x04", 3);
3117
3118 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3119 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3120 "channel list");
3121 return -1;
3122 }
3123
3124 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3125 WPS_DEV_TYPE_LEN);
3126
3127 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3128 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3129 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3130
3131 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3132 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3133
3134 p2p.max_peers = 100;
3135
3136 if (wpa_s->conf->p2p_ssid_postfix) {
3137 p2p.ssid_postfix_len =
3138 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3139 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3140 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3141 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3142 p2p.ssid_postfix_len);
3143 }
3144
3145 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3146
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003147 p2p.max_listen = wpa_s->max_remain_on_chan;
3148
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003149#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003150 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003151 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003152 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3153 } else {
3154 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003155 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003156 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3157 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003158#endif
3159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003160 global->p2p = p2p_init(&p2p);
3161 if (global->p2p == NULL)
3162 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003163 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164
3165 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3166 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3167 continue;
3168 p2p_add_wps_vendor_extension(
3169 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3170 }
3171
3172 return 0;
3173}
3174
3175
3176/**
3177 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3178 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3179 *
3180 * This function deinitialize per-interface P2P data.
3181 */
3182void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3183{
3184 if (wpa_s->driver && wpa_s->drv_priv)
3185 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003186
3187 if (wpa_s->go_params) {
3188 /* Clear any stored provisioning info */
3189 p2p_clear_provisioning_info(
3190 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003191 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003192 }
3193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003194 os_free(wpa_s->go_params);
3195 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003196#ifdef ANDROID_P2P
3197 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3198#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3200 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3201 wpa_s->p2p_long_listen = 0;
3202 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3203 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3204 wpas_p2p_remove_pending_group_interface(wpa_s);
3205
3206 /* TODO: remove group interface from the driver if this wpa_s instance
3207 * is on top of a P2P group interface */
3208}
3209
3210
3211/**
3212 * wpas_p2p_deinit_global - Deinitialize global P2P module
3213 * @global: Pointer to global data from wpa_supplicant_init()
3214 *
3215 * This function deinitializes the global (per device) P2P module.
3216 */
3217void wpas_p2p_deinit_global(struct wpa_global *global)
3218{
3219 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003221 wpa_s = global->ifaces;
3222 if (wpa_s)
3223 wpas_p2p_service_flush(wpa_s);
3224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225 if (global->p2p == NULL)
3226 return;
3227
3228 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3230 wpa_s = wpa_s->next;
3231 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232 tmp = global->ifaces;
3233 while (tmp &&
3234 (tmp == wpa_s ||
3235 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3236 tmp = tmp->next;
3237 }
3238 if (tmp == NULL)
3239 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003240 /* Disconnect from the P2P group and deinit the interface */
3241 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003242 }
3243
3244 /*
3245 * Deinit GO data on any possibly remaining interface (if main
3246 * interface is used as GO).
3247 */
3248 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3249 if (wpa_s->ap_iface)
3250 wpas_p2p_group_deinit(wpa_s);
3251 }
3252
3253 p2p_deinit(global->p2p);
3254 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003255 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256}
3257
3258
3259static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3260{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003261 if (wpa_s->conf->p2p_no_group_iface)
3262 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263 if (wpa_s->drv_flags &
3264 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3265 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3266 return 1; /* P2P group requires a new interface in every case
3267 */
3268 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3269 return 0; /* driver does not support concurrent operations */
3270 if (wpa_s->global->ifaces->next)
3271 return 1; /* more that one interface already in use */
3272 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3273 return 1; /* this interface is already in use */
3274 return 0;
3275}
3276
3277
3278static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3279 const u8 *peer_addr,
3280 enum p2p_wps_method wps_method,
3281 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003282 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003283 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003284{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003285 if (persistent_group && wpa_s->conf->persistent_reconnect)
3286 persistent_group = 2;
3287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3289 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3290 go_intent, own_interface_addr,
3291 force_freq, persistent_group);
3292 }
3293
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003294 /*
3295 * Increase GO config timeout if HT40 is used since it takes some time
3296 * to scan channels for coex purposes before the BSS can be started.
3297 */
3298 p2p_set_config_timeout(wpa_s->global->p2p,
3299 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003301 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3302 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003303 persistent_group, ssid ? ssid->ssid : NULL,
3304 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003305 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306}
3307
3308
3309static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3310 const u8 *peer_addr,
3311 enum p2p_wps_method wps_method,
3312 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003313 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003314 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003315{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003316 if (persistent_group && wpa_s->conf->persistent_reconnect)
3317 persistent_group = 2;
3318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003319 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3320 return -1;
3321
3322 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3323 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003324 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003325 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326}
3327
3328
3329static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3330{
3331 wpa_s->p2p_join_scan_count++;
3332 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3333 wpa_s->p2p_join_scan_count);
3334 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3335 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3336 " for join operationg - stop join attempt",
3337 MAC2STR(wpa_s->pending_join_iface_addr));
3338 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003339 if (wpa_s->p2p_auto_pd) {
3340 wpa_s->p2p_auto_pd = 0;
3341 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3342 " p2p_dev_addr=" MACSTR " status=N/A",
3343 MAC2STR(wpa_s->pending_join_dev_addr));
3344 return;
3345 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003346 wpa_msg(wpa_s->parent, MSG_INFO,
3347 P2P_EVENT_GROUP_FORMATION_FAILURE);
3348 }
3349}
3350
3351
Dmitry Shmidt04949592012-07-19 12:16:46 -07003352static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3353{
3354 struct wpa_supplicant *iface;
3355 int shared_freq;
3356 u8 bssid[ETH_ALEN];
3357
3358 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3359 return 0;
3360
3361 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3362 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3363 continue;
3364 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3365 continue;
3366 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3367 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3368 shared_freq = iface->current_ssid->frequency;
3369 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3370 shared_freq = iface->assoc_freq;
3371 else
3372 shared_freq = 0;
3373
3374 if (shared_freq && freq != shared_freq) {
3375 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3376 "connected on %d MHz - new connection on "
3377 "%d MHz", iface->ifname, shared_freq, freq);
3378 return 1;
3379 }
3380 }
3381
3382 shared_freq = wpa_drv_shared_freq(wpa_s);
3383 if (shared_freq > 0 && shared_freq != freq) {
3384 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3385 "virtual interface connected on %d MHz - new "
3386 "connection on %d MHz", shared_freq, freq);
3387 return 1;
3388 }
3389
3390 return 0;
3391}
3392
3393
3394static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3395 const u8 *peer_dev_addr)
3396{
3397 struct wpa_bss *bss;
3398 int updated;
3399
3400 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3401 if (bss == NULL)
3402 return -1;
3403 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3404 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3405 "last scan");
3406 return 0;
3407 }
3408
3409 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3410 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3411 "%ld.%06ld (%supdated in last scan)",
3412 bss->last_update.sec, bss->last_update.usec,
3413 updated ? "": "not ");
3414
3415 return updated;
3416}
3417
3418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3420 struct wpa_scan_results *scan_res)
3421{
3422 struct wpa_bss *bss;
3423 int freq;
3424 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3427
3428 if (wpa_s->global->p2p_disabled)
3429 return;
3430
Dmitry Shmidt04949592012-07-19 12:16:46 -07003431 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3432 scan_res ? (int) scan_res->num : -1,
3433 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434
3435 if (scan_res)
3436 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3437
Dmitry Shmidt04949592012-07-19 12:16:46 -07003438 if (wpa_s->p2p_auto_pd) {
3439 int join = wpas_p2p_peer_go(wpa_s,
3440 wpa_s->pending_join_dev_addr);
3441 if (join == 0 &&
3442 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3443 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003444 bss = wpa_bss_get_bssid_latest(
3445 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003446 if (bss) {
3447 freq = bss->freq;
3448 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3449 "the peer " MACSTR " at %d MHz",
3450 wpa_s->auto_pd_scan_retry,
3451 MAC2STR(wpa_s->
3452 pending_join_dev_addr),
3453 freq);
3454 wpas_p2p_join_scan_req(wpa_s, freq);
3455 return;
3456 }
3457 }
3458
3459 if (join < 0)
3460 join = 0;
3461
3462 wpa_s->p2p_auto_pd = 0;
3463 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3464 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3465 MAC2STR(wpa_s->pending_join_dev_addr), join);
3466 if (p2p_prov_disc_req(wpa_s->global->p2p,
3467 wpa_s->pending_join_dev_addr,
3468 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003469 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003470 wpa_s->p2p_auto_pd = 0;
3471 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3472 " p2p_dev_addr=" MACSTR " status=N/A",
3473 MAC2STR(wpa_s->pending_join_dev_addr));
3474 }
3475 return;
3476 }
3477
3478 if (wpa_s->p2p_auto_join) {
3479 int join = wpas_p2p_peer_go(wpa_s,
3480 wpa_s->pending_join_dev_addr);
3481 if (join < 0) {
3482 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3483 "running a GO -> use GO Negotiation");
3484 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3485 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3486 wpa_s->p2p_persistent_group, 0, 0, 0,
3487 wpa_s->p2p_go_intent,
3488 wpa_s->p2p_connect_freq,
3489 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003490 wpa_s->p2p_pd_before_go_neg,
3491 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003492 return;
3493 }
3494
3495 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3496 "try to join the group", join ? "" :
3497 " in older scan");
3498 if (!join)
3499 wpa_s->p2p_fallback_to_go_neg = 1;
3500 }
3501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3503 wpa_s->pending_join_iface_addr);
3504 if (freq < 0 &&
3505 p2p_get_interface_addr(wpa_s->global->p2p,
3506 wpa_s->pending_join_dev_addr,
3507 iface_addr) == 0 &&
3508 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3509 {
3510 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3511 "address for join from " MACSTR " to " MACSTR
3512 " based on newly discovered P2P peer entry",
3513 MAC2STR(wpa_s->pending_join_iface_addr),
3514 MAC2STR(iface_addr));
3515 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3516 ETH_ALEN);
3517
3518 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3519 wpa_s->pending_join_iface_addr);
3520 }
3521 if (freq >= 0) {
3522 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3523 "from P2P peer table: %d MHz", freq);
3524 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003525 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526 if (bss) {
3527 freq = bss->freq;
3528 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3529 "from BSS table: %d MHz", freq);
3530 }
3531 if (freq > 0) {
3532 u16 method;
3533
Dmitry Shmidt04949592012-07-19 12:16:46 -07003534 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3535 wpa_msg(wpa_s->parent, MSG_INFO,
3536 P2P_EVENT_GROUP_FORMATION_FAILURE
3537 "reason=FREQ_CONFLICT");
3538 return;
3539 }
3540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003541 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3542 "prior to joining an existing group (GO " MACSTR
3543 " freq=%u MHz)",
3544 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3545 wpa_s->pending_pd_before_join = 1;
3546
3547 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003548 case WPS_PIN_DISPLAY:
3549 method = WPS_CONFIG_KEYPAD;
3550 break;
3551 case WPS_PIN_KEYPAD:
3552 method = WPS_CONFIG_DISPLAY;
3553 break;
3554 case WPS_PBC:
3555 method = WPS_CONFIG_PUSHBUTTON;
3556 break;
3557 default:
3558 method = 0;
3559 break;
3560 }
3561
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003562 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3563 wpa_s->pending_join_dev_addr) ==
3564 method)) {
3565 /*
3566 * We have already performed provision discovery for
3567 * joining the group. Proceed directly to join
3568 * operation without duplicated provision discovery. */
3569 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3570 "with " MACSTR " already done - proceed to "
3571 "join",
3572 MAC2STR(wpa_s->pending_join_dev_addr));
3573 wpa_s->pending_pd_before_join = 0;
3574 goto start;
3575 }
3576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003577 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003578 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003579 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3581 "Discovery Request before joining an "
3582 "existing group");
3583 wpa_s->pending_pd_before_join = 0;
3584 goto start;
3585 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 return;
3587 }
3588
3589 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3590 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3591 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3592 wpas_p2p_check_join_scan_limit(wpa_s);
3593 return;
3594
3595start:
3596 /* Start join operation immediately */
3597 wpas_p2p_join_start(wpa_s);
3598}
3599
3600
Dmitry Shmidt04949592012-07-19 12:16:46 -07003601static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603 int ret;
3604 struct wpa_driver_scan_params params;
3605 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003606 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003607 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003608#ifdef ANDROID_P2P
3609 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003611 /* If freq is not provided, check the operating freq of the GO and do a
3612 * a directed scan to save time
3613 */
3614 if(!freq) {
3615 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3616 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3617 }
3618#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 os_memset(&params, 0, sizeof(params));
3620
3621 /* P2P Wildcard SSID */
3622 params.num_ssids = 1;
3623 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3624 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3625
3626 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003627 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3628 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3629 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630 if (wps_ie == NULL) {
3631 wpas_p2p_scan_res_join(wpa_s, NULL);
3632 return;
3633 }
3634
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003635 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3636 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 if (ies == NULL) {
3638 wpabuf_free(wps_ie);
3639 wpas_p2p_scan_res_join(wpa_s, NULL);
3640 return;
3641 }
3642 wpabuf_put_buf(ies, wps_ie);
3643 wpabuf_free(wps_ie);
3644
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003645 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003646
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003647 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 params.extra_ies = wpabuf_head(ies);
3649 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003650 if (freq > 0) {
3651 freqs[0] = freq;
3652 params.freqs = freqs;
3653 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003654
3655 /*
3656 * Run a scan to update BSS table and start Provision Discovery once
3657 * the new scan results become available.
3658 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003659 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003660 if (!ret) {
3661 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003662 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003663 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664
3665 wpabuf_free(ies);
3666
3667 if (ret) {
3668 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3669 "try again later");
3670 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3671 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3672 wpas_p2p_check_join_scan_limit(wpa_s);
3673 }
3674}
3675
3676
Dmitry Shmidt04949592012-07-19 12:16:46 -07003677static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3678{
3679 struct wpa_supplicant *wpa_s = eloop_ctx;
3680 wpas_p2p_join_scan_req(wpa_s, 0);
3681}
3682
3683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003684static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003685 const u8 *dev_addr, enum p2p_wps_method wps_method,
3686 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003687{
3688 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003689 MACSTR " dev " MACSTR ")%s",
3690 MAC2STR(iface_addr), MAC2STR(dev_addr),
3691 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692
Dmitry Shmidt04949592012-07-19 12:16:46 -07003693 wpa_s->p2p_auto_pd = 0;
3694 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3696 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3697 wpa_s->pending_join_wps_method = wps_method;
3698
3699 /* Make sure we are not running find during connection establishment */
3700 wpas_p2p_stop_find(wpa_s);
3701
3702 wpa_s->p2p_join_scan_count = 0;
3703 wpas_p2p_join_scan(wpa_s, NULL);
3704 return 0;
3705}
3706
3707
3708static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3709{
3710 struct wpa_supplicant *group;
3711 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003712 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003713
3714 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3715 if (group == NULL)
3716 return -1;
3717 if (group != wpa_s) {
3718 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3719 sizeof(group->p2p_pin));
3720 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003721 } else {
3722 /*
3723 * Need to mark the current interface for p2p_group_formation
3724 * when a separate group interface is not used. This is needed
3725 * to allow p2p_cancel stop a pending p2p_connect-join.
3726 * wpas_p2p_init_group_interface() addresses this for the case
3727 * where a separate group interface is used.
3728 */
3729 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003730 }
3731
3732 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003733 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003734
3735 os_memset(&res, 0, sizeof(res));
3736 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3737 ETH_ALEN);
3738 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003739 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003740 if (bss) {
3741 res.freq = bss->freq;
3742 res.ssid_len = bss->ssid_len;
3743 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3744 }
3745
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003746 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3747 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3748 "starting client");
3749 wpa_drv_cancel_remain_on_channel(wpa_s);
3750 wpa_s->off_channel_freq = 0;
3751 wpa_s->roc_waiting_drv_freq = 0;
3752 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003753 wpas_start_wps_enrollee(group, &res);
3754
3755 /*
3756 * Allow a longer timeout for join-a-running-group than normal 15
3757 * second group formation timeout since the GO may not have authorized
3758 * our connection yet.
3759 */
3760 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3761 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3762 wpa_s, NULL);
3763
3764 return 0;
3765}
3766
3767
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003768static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3769 int *force_freq, int *pref_freq,
3770 int *oper_freq)
3771{
3772 if (freq > 0) {
3773 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3774 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3775 "(%u MHz) is not supported for P2P uses",
3776 freq);
3777 return -3;
3778 }
3779
3780 if (*oper_freq > 0 && freq != *oper_freq &&
3781 !(wpa_s->drv_flags &
3782 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3783 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3784 "on %u MHz while connected on another "
3785 "channel (%u MHz)", freq, *oper_freq);
3786 return -2;
3787 }
3788 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3789 "requested channel (%u MHz)", freq);
3790 *force_freq = freq;
3791 } else if (*oper_freq > 0 &&
3792 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3793 if (!(wpa_s->drv_flags &
3794 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3795 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3796 "while connected on non-P2P supported "
3797 "channel (%u MHz)", *oper_freq);
3798 return -2;
3799 }
3800 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3801 "(%u MHz) not available for P2P - try to use "
3802 "another channel", *oper_freq);
3803 *force_freq = 0;
3804 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3805 (wpa_s->drv_flags &
3806 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3807 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3808 "are already using (%u MHz) on another interface",
3809 *oper_freq);
3810 *pref_freq = *oper_freq;
3811 } else if (*oper_freq > 0) {
3812 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3813 "channel we are already using (%u MHz) on another "
3814 "interface", *oper_freq);
3815 *force_freq = *oper_freq;
3816 }
3817
3818 return 0;
3819}
3820
3821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003822/**
3823 * wpas_p2p_connect - Request P2P Group Formation to be started
3824 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3825 * @peer_addr: Address of the peer P2P Device
3826 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3827 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003828 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003829 * @join: Whether to join an existing group (as a client) instead of starting
3830 * Group Owner negotiation; @peer_addr is BSSID in that case
3831 * @auth: Whether to only authorize the connection instead of doing that and
3832 * initiating Group Owner negotiation
3833 * @go_intent: GO Intent or -1 to use default
3834 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003835 * @persistent_id: Persistent group credentials to use for forcing GO
3836 * parameters or -1 to generate new values (SSID/passphrase)
3837 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3838 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003839 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003840 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3841 * failure, -2 on failure due to channel not currently available,
3842 * -3 if forced channel is not supported
3843 */
3844int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3845 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003846 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003847 int go_intent, int freq, int persistent_id, int pd,
3848 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003850 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003851 u8 bssid[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003852 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003853 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003854 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003855 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856
3857 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3858 return -1;
3859
Dmitry Shmidt04949592012-07-19 12:16:46 -07003860 if (persistent_id >= 0) {
3861 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3862 if (ssid == NULL || ssid->disabled != 2 ||
3863 ssid->mode != WPAS_MODE_P2P_GO)
3864 return -1;
3865 }
3866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867 if (go_intent < 0)
3868 go_intent = wpa_s->conf->p2p_go_intent;
3869
3870 if (!auth)
3871 wpa_s->p2p_long_listen = 0;
3872
3873 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003874 wpa_s->p2p_persistent_group = !!persistent_group;
3875 wpa_s->p2p_persistent_id = persistent_id;
3876 wpa_s->p2p_go_intent = go_intent;
3877 wpa_s->p2p_connect_freq = freq;
3878 wpa_s->p2p_fallback_to_go_neg = 0;
3879 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003880 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003881
3882 if (pin)
3883 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3884 else if (wps_method == WPS_PIN_DISPLAY) {
3885 ret = wps_generate_pin();
3886 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3887 ret);
3888 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3889 wpa_s->p2p_pin);
3890 } else
3891 wpa_s->p2p_pin[0] = '\0';
3892
Dmitry Shmidt04949592012-07-19 12:16:46 -07003893 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3895 if (auth) {
3896 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3897 "connect a running group from " MACSTR,
3898 MAC2STR(peer_addr));
3899 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3900 return ret;
3901 }
3902 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3903 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3904 iface_addr) < 0) {
3905 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3906 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3907 dev_addr);
3908 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003909 if (auto_join) {
3910 os_get_time(&wpa_s->p2p_auto_started);
3911 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3912 "%ld.%06ld",
3913 wpa_s->p2p_auto_started.sec,
3914 wpa_s->p2p_auto_started.usec);
3915 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003916 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003917 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3918 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003919 return -1;
3920 return ret;
3921 }
3922
3923 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003924 wpa_s->assoc_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003926 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003927 oper_freq = wpa_drv_shared_freq(wpa_s);
3928 if (oper_freq < 0)
3929 oper_freq = 0;
3930 }
3931
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003932 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
3933 &oper_freq);
3934 if (res)
3935 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003936 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937
3938 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3939
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003940 if (wpa_s->create_p2p_iface) {
3941 /* Prepare to add a new interface for the group */
3942 iftype = WPA_IF_P2P_GROUP;
3943 if (go_intent == 15)
3944 iftype = WPA_IF_P2P_GO;
3945 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3946 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3947 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003949 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003951 if_addr = wpa_s->pending_interface_addr;
3952 } else
3953 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954
3955 if (auth) {
3956 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003957 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003958 force_freq, persistent_group, ssid,
3959 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960 return -1;
3961 return ret;
3962 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003963
3964 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3965 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003966 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003967 if (wpa_s->create_p2p_iface)
3968 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969 return -1;
3970 }
3971 return ret;
3972}
3973
3974
3975/**
3976 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3977 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3978 * @freq: Frequency of the channel in MHz
3979 * @duration: Duration of the stay on the channel in milliseconds
3980 *
3981 * This callback is called when the driver indicates that it has started the
3982 * requested remain-on-channel duration.
3983 */
3984void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3985 unsigned int freq, unsigned int duration)
3986{
3987 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3988 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003989 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3990 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3991 wpa_s->pending_listen_duration);
3992 wpa_s->pending_listen_freq = 0;
3993 }
3994}
3995
3996
3997static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3998 unsigned int timeout)
3999{
4000 /* Limit maximum Listen state time based on driver limitation. */
4001 if (timeout > wpa_s->max_remain_on_chan)
4002 timeout = wpa_s->max_remain_on_chan;
4003
4004 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4005 return wpa_drv_p2p_listen(wpa_s, timeout);
4006
4007 return p2p_listen(wpa_s->global->p2p, timeout);
4008}
4009
4010
4011/**
4012 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4013 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4014 * @freq: Frequency of the channel in MHz
4015 *
4016 * This callback is called when the driver indicates that a remain-on-channel
4017 * operation has been completed, i.e., the duration on the requested channel
4018 * has timed out.
4019 */
4020void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4021 unsigned int freq)
4022{
4023 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4024 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004025 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004026 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4027 return;
4028 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4029 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004030 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031 return;
4032 if (wpa_s->p2p_long_listen > 0)
4033 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4034 if (wpa_s->p2p_long_listen > 0) {
4035 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4036 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4037 }
4038}
4039
4040
4041/**
4042 * wpas_p2p_group_remove - Remove a P2P group
4043 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4044 * @ifname: Network interface name of the group interface or "*" to remove all
4045 * groups
4046 * Returns: 0 on success, -1 on failure
4047 *
4048 * This function is used to remove a P2P group. This can be used to disconnect
4049 * from a group in which the local end is a P2P Client or to end a P2P Group in
4050 * case the local end is the Group Owner. If a virtual network interface was
4051 * created for this group, that interface will be removed. Otherwise, only the
4052 * configured P2P group network will be removed from the interface.
4053 */
4054int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4055{
4056 struct wpa_global *global = wpa_s->global;
4057
4058 if (os_strcmp(ifname, "*") == 0) {
4059 struct wpa_supplicant *prev;
4060 wpa_s = global->ifaces;
4061 while (wpa_s) {
4062 prev = wpa_s;
4063 wpa_s = wpa_s->next;
4064 wpas_p2p_disconnect(prev);
4065 }
4066 return 0;
4067 }
4068
4069 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4070 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4071 break;
4072 }
4073
4074 return wpas_p2p_disconnect(wpa_s);
4075}
4076
4077
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004078static int freq_included(const struct p2p_channels *channels, unsigned int freq)
4079{
4080 if (channels == NULL)
4081 return 1; /* Assume no restrictions */
4082 return p2p_channels_includes_freq(channels, freq);
4083}
4084
4085
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004086static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4087 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004088 int freq, int ht40,
4089 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090{
4091 u8 bssid[ETH_ALEN];
4092 int res;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004093 unsigned int pref_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094
4095 os_memset(params, 0, sizeof(*params));
4096 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004097 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004098 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004099 if (!freq_included(channels, freq)) {
4100 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4101 "accepted", freq);
4102 return -1;
4103 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4105 "frequency %d MHz", freq);
4106 params->freq = freq;
4107 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4108 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004109 wpa_s->conf->p2p_oper_channel <= 11 &&
4110 freq_included(channels,
4111 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004112 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4113 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4114 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004115 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4116 wpa_s->conf->p2p_oper_reg_class == 116 ||
4117 wpa_s->conf->p2p_oper_reg_class == 117 ||
4118 wpa_s->conf->p2p_oper_reg_class == 124 ||
4119 wpa_s->conf->p2p_oper_reg_class == 126 ||
4120 wpa_s->conf->p2p_oper_reg_class == 127) &&
4121 freq_included(channels,
4122 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4124 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4125 "frequency %d MHz", params->freq);
4126 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4127 wpa_s->best_overall_freq > 0 &&
4128 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004129 wpa_s->best_overall_freq) &&
4130 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 params->freq = wpa_s->best_overall_freq;
4132 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4133 "channel %d MHz", params->freq);
4134 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4135 wpa_s->best_24_freq > 0 &&
4136 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004137 wpa_s->best_24_freq) &&
4138 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139 params->freq = wpa_s->best_24_freq;
4140 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4141 "channel %d MHz", params->freq);
4142 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4143 wpa_s->best_5_freq > 0 &&
4144 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004145 wpa_s->best_5_freq) &&
4146 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004147 params->freq = wpa_s->best_5_freq;
4148 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4149 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004150 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4151 channels))) {
4152 params->freq = pref_freq;
4153 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4154 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004155 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004156 int chan;
4157 for (chan = 0; chan < 11; chan++) {
4158 params->freq = 2412 + chan * 5;
4159 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004160 params->freq) &&
4161 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004162 break;
4163 }
4164 if (chan == 11) {
4165 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4166 "allowed");
4167 return -1;
4168 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004169 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4170 "known)", params->freq);
4171 }
4172
4173 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4174 wpa_s->assoc_freq && !freq) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004175 if (!p2p_supported_freq(wpa_s->global->p2p, wpa_s->assoc_freq)
4176 || !freq_included(channels, wpa_s->assoc_freq)) {
4177 if (wpa_s->drv_flags &
4178 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4179 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4180 "the channel we are already using "
4181 "(%u MHz) - allow multi-channel "
4182 "concurrency", wpa_s->assoc_freq);
4183 } else {
4184 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4185 "the channel we are already using "
4186 "(%u MHz)", wpa_s->assoc_freq);
4187 return -1;
4188 }
4189 } else {
4190 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we "
4191 "are already using (%u MHz)",
4192 wpa_s->assoc_freq);
4193 params->freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004194 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004195 }
4196
4197 res = wpa_drv_shared_freq(wpa_s);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004198 if (res > 0 && !freq &&
4199 (!p2p_supported_freq(wpa_s->global->p2p, res) ||
4200 !freq_included(channels, res))) {
4201 if (wpa_s->drv_flags &
4202 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4203 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4204 "channel we are already using on a shared "
4205 "interface (%u MHz) - allow multi-channel "
4206 "concurrency", res);
4207 } else {
4208 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4209 "channel we are already using on a shared "
4210 "interface (%u MHz)", res);
4211 return -1;
4212 }
4213 } else if (res > 0 && !freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004214 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4215 "already using on a shared interface");
4216 params->freq = res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004217 if (!freq_included(channels, params->freq)) {
4218 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4219 "accepted", params->freq);
4220 return -1;
4221 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004222 } else if (res > 0 && freq != res &&
4223 !(wpa_s->drv_flags &
4224 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4225 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4226 "while connected on another channel (%u MHz)",
4227 freq, res);
4228 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004229 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004230
4231 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232}
4233
4234
4235static struct wpa_supplicant *
4236wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4237 int go)
4238{
4239 struct wpa_supplicant *group_wpa_s;
4240
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004241 if (!wpas_p2p_create_iface(wpa_s)) {
4242 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4243 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004244 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004245 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004246
4247 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004248 WPA_IF_P2P_CLIENT) < 0) {
4249 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004251 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4253 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004254 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4255 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004256 wpas_p2p_remove_pending_group_interface(wpa_s);
4257 return NULL;
4258 }
4259
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004260 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4261 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004262 return group_wpa_s;
4263}
4264
4265
4266/**
4267 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4268 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4269 * @persistent_group: Whether to create a persistent group
4270 * @freq: Frequency for the group or 0 to indicate no hardcoding
4271 * Returns: 0 on success, -1 on failure
4272 *
4273 * This function creates a new P2P group with the local end as the Group Owner,
4274 * i.e., without using Group Owner Negotiation.
4275 */
4276int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004277 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004278{
4279 struct p2p_go_neg_results params;
4280 unsigned int r;
4281
4282 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4283 return -1;
4284
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004285 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004286 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004287 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004289 if (freq == 2) {
4290 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4291 "band");
4292 if (wpa_s->best_24_freq > 0 &&
4293 p2p_supported_freq(wpa_s->global->p2p,
4294 wpa_s->best_24_freq)) {
4295 freq = wpa_s->best_24_freq;
4296 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4297 "channel: %d MHz", freq);
4298 } else {
4299 os_get_random((u8 *) &r, sizeof(r));
4300 freq = 2412 + (r % 3) * 25;
4301 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4302 "channel: %d MHz", freq);
4303 }
4304 }
4305
4306 if (freq == 5) {
4307 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4308 "band");
4309 if (wpa_s->best_5_freq > 0 &&
4310 p2p_supported_freq(wpa_s->global->p2p,
4311 wpa_s->best_5_freq)) {
4312 freq = wpa_s->best_5_freq;
4313 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4314 "channel: %d MHz", freq);
4315 } else {
4316 os_get_random((u8 *) &r, sizeof(r));
4317 freq = 5180 + (r % 4) * 20;
4318 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4319 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4320 "5 GHz channel for P2P group");
4321 return -1;
4322 }
4323 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4324 "channel: %d MHz", freq);
4325 }
4326 }
4327
4328 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4329 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4330 "(%u MHz) is not supported for P2P uses",
4331 freq);
4332 return -1;
4333 }
4334
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004335 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004336 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004337 if (params.freq &&
4338 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4339 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4340 "(%u MHz) is not supported for P2P uses",
4341 params.freq);
4342 return -1;
4343 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344 p2p_go_params(wpa_s->global->p2p, &params);
4345 params.persistent_group = persistent_group;
4346
4347 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4348 if (wpa_s == NULL)
4349 return -1;
4350 wpas_start_wps_go(wpa_s, &params, 0);
4351
4352 return 0;
4353}
4354
4355
4356static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4357 struct wpa_ssid *params, int addr_allocated)
4358{
4359 struct wpa_ssid *ssid;
4360
4361 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4362 if (wpa_s == NULL)
4363 return -1;
4364
4365 wpa_supplicant_ap_deinit(wpa_s);
4366
4367 ssid = wpa_config_add_network(wpa_s->conf);
4368 if (ssid == NULL)
4369 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370 wpa_config_set_network_defaults(ssid);
4371 ssid->temporary = 1;
4372 ssid->proto = WPA_PROTO_RSN;
4373 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4374 ssid->group_cipher = WPA_CIPHER_CCMP;
4375 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4376 ssid->ssid = os_malloc(params->ssid_len);
4377 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004378 wpa_config_remove_network(wpa_s->conf, ssid->id);
4379 return -1;
4380 }
4381 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4382 ssid->ssid_len = params->ssid_len;
4383 ssid->p2p_group = 1;
4384 ssid->export_keys = 1;
4385 if (params->psk_set) {
4386 os_memcpy(ssid->psk, params->psk, 32);
4387 ssid->psk_set = 1;
4388 }
4389 if (params->passphrase)
4390 ssid->passphrase = os_strdup(params->passphrase);
4391
4392 wpa_supplicant_select_network(wpa_s, ssid);
4393
4394 wpa_s->show_group_started = 1;
4395
4396 return 0;
4397}
4398
4399
4400int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4401 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004402 int freq, int ht40,
4403 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404{
4405 struct p2p_go_neg_results params;
4406 int go = 0;
4407
4408 if (ssid->disabled != 2 || ssid->ssid == NULL)
4409 return -1;
4410
4411 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4412 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4413 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4414 "already running");
4415 return 0;
4416 }
4417
4418 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004419 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004420
Dmitry Shmidt04949592012-07-19 12:16:46 -07004421 wpa_s->p2p_fallback_to_go_neg = 0;
4422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004423 if (ssid->mode == WPAS_MODE_INFRA)
4424 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4425
4426 if (ssid->mode != WPAS_MODE_P2P_GO)
4427 return -1;
4428
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004429 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004430 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431
4432 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004433 params.psk_set = ssid->psk_set;
4434 if (params.psk_set)
4435 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004436 if (ssid->passphrase) {
4437 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4438 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4439 "persistent group");
4440 return -1;
4441 }
4442 os_strlcpy(params.passphrase, ssid->passphrase,
4443 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004445 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4446 params.ssid_len = ssid->ssid_len;
4447 params.persistent_group = 1;
4448
4449 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4450 if (wpa_s == NULL)
4451 return -1;
4452
4453 wpas_start_wps_go(wpa_s, &params, 0);
4454
4455 return 0;
4456}
4457
4458
4459static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4460 struct wpabuf *proberesp_ies)
4461{
4462 struct wpa_supplicant *wpa_s = ctx;
4463 if (wpa_s->ap_iface) {
4464 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004465 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4466 wpabuf_free(beacon_ies);
4467 wpabuf_free(proberesp_ies);
4468 return;
4469 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470 if (beacon_ies) {
4471 wpabuf_free(hapd->p2p_beacon_ie);
4472 hapd->p2p_beacon_ie = beacon_ies;
4473 }
4474 wpabuf_free(hapd->p2p_probe_resp_ie);
4475 hapd->p2p_probe_resp_ie = proberesp_ies;
4476 } else {
4477 wpabuf_free(beacon_ies);
4478 wpabuf_free(proberesp_ies);
4479 }
4480 wpa_supplicant_ap_update_beacon(wpa_s);
4481}
4482
4483
4484static void wpas_p2p_idle_update(void *ctx, int idle)
4485{
4486 struct wpa_supplicant *wpa_s = ctx;
4487 if (!wpa_s->ap_iface)
4488 return;
4489 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004490 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004491 wpas_p2p_set_group_idle_timeout(wpa_s);
4492 else
4493 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4494}
4495
4496
4497struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004498 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499{
4500 struct p2p_group *group;
4501 struct p2p_group_config *cfg;
4502
4503 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4504 return NULL;
4505 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4506 return NULL;
4507
4508 cfg = os_zalloc(sizeof(*cfg));
4509 if (cfg == NULL)
4510 return NULL;
4511
Dmitry Shmidt04949592012-07-19 12:16:46 -07004512 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004513 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004514 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004515 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004516 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4517 if (wpa_s->max_stations &&
4518 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4519 cfg->max_clients = wpa_s->max_stations;
4520 else
4521 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004522 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4523 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004524 cfg->cb_ctx = wpa_s;
4525 cfg->ie_update = wpas_p2p_ie_update;
4526 cfg->idle_update = wpas_p2p_idle_update;
4527
4528 group = p2p_group_init(wpa_s->global->p2p, cfg);
4529 if (group == NULL)
4530 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004531 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532 p2p_group_notif_formation_done(group);
4533 wpa_s->p2p_group = group;
4534 return group;
4535}
4536
4537
4538void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4539 int registrar)
4540{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004541 struct wpa_ssid *ssid = wpa_s->current_ssid;
4542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543 if (!wpa_s->p2p_in_provisioning) {
4544 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4545 "provisioning not in progress");
4546 return;
4547 }
4548
Dmitry Shmidt04949592012-07-19 12:16:46 -07004549 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4550 u8 go_dev_addr[ETH_ALEN];
4551 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4552 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4553 ssid->ssid_len);
4554 /* Clear any stored provisioning info */
4555 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4556 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004558 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4559 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004560 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4561 /*
4562 * Use a separate timeout for initial data connection to
4563 * complete to allow the group to be removed automatically if
4564 * something goes wrong in this step before the P2P group idle
4565 * timeout mechanism is taken into use.
4566 */
4567 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4568 wpas_p2p_group_formation_timeout,
4569 wpa_s->parent, NULL);
4570 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571 if (wpa_s->global->p2p)
4572 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4573 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4574 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4575 wpas_group_formation_completed(wpa_s, 1);
4576}
4577
4578
Jouni Malinen75ecf522011-06-27 15:19:46 -07004579void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4580 struct wps_event_fail *fail)
4581{
4582 if (!wpa_s->p2p_in_provisioning) {
4583 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4584 "provisioning not in progress");
4585 return;
4586 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004587
4588 if (wpa_s->go_params) {
4589 p2p_clear_provisioning_info(
4590 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004591 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004592 }
4593
Jouni Malinen75ecf522011-06-27 15:19:46 -07004594 wpas_notify_p2p_wps_failed(wpa_s, fail);
4595}
4596
4597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004598int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004599 const char *config_method,
4600 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601{
4602 u16 config_methods;
4603
Dmitry Shmidt04949592012-07-19 12:16:46 -07004604 wpa_s->p2p_fallback_to_go_neg = 0;
4605 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004606 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004608 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004609 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004610 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4611 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004613 else {
4614 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004616 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617
Dmitry Shmidt04949592012-07-19 12:16:46 -07004618 if (use == WPAS_P2P_PD_AUTO) {
4619 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4620 wpa_s->pending_pd_config_methods = config_methods;
4621 wpa_s->p2p_auto_pd = 1;
4622 wpa_s->p2p_auto_join = 0;
4623 wpa_s->pending_pd_before_join = 0;
4624 wpa_s->auto_pd_scan_retry = 0;
4625 wpas_p2p_stop_find(wpa_s);
4626 wpa_s->p2p_join_scan_count = 0;
4627 os_get_time(&wpa_s->p2p_auto_started);
4628 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4629 wpa_s->p2p_auto_started.sec,
4630 wpa_s->p2p_auto_started.usec);
4631 wpas_p2p_join_scan(wpa_s, NULL);
4632 return 0;
4633 }
4634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4636 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004637 config_methods,
4638 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639 }
4640
4641 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4642 return -1;
4643
4644 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004645 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004646 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647}
4648
4649
4650int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4651 char *end)
4652{
4653 return p2p_scan_result_text(ies, ies_len, buf, end);
4654}
4655
4656
4657static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4658{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004659 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004660 return;
4661
4662 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4663 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004664 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665}
4666
4667
4668int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4669 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004670 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004671 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672{
4673 wpas_p2p_clear_pending_action_tx(wpa_s);
4674 wpa_s->p2p_long_listen = 0;
4675
4676 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4677 return wpa_drv_p2p_find(wpa_s, timeout, type);
4678
Dmitry Shmidt04949592012-07-19 12:16:46 -07004679 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4680 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004681 return -1;
4682
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004683 wpa_supplicant_cancel_sched_scan(wpa_s);
4684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004685 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004686 num_req_dev_types, req_dev_types, dev_id,
4687 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004688}
4689
4690
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004691static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692{
4693 wpas_p2p_clear_pending_action_tx(wpa_s);
4694 wpa_s->p2p_long_listen = 0;
4695 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4696 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004697 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004698
4699 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4700 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004701 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702 }
4703
4704 if (wpa_s->global->p2p)
4705 p2p_stop_find(wpa_s->global->p2p);
4706
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004707 return 0;
4708}
4709
4710
4711void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4712{
4713 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4714 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004715 wpas_p2p_remove_pending_group_interface(wpa_s);
4716}
4717
4718
4719static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4720{
4721 struct wpa_supplicant *wpa_s = eloop_ctx;
4722 wpa_s->p2p_long_listen = 0;
4723}
4724
4725
4726int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4727{
4728 int res;
4729
4730 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4731 return -1;
4732
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004733 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004734 wpas_p2p_clear_pending_action_tx(wpa_s);
4735
4736 if (timeout == 0) {
4737 /*
4738 * This is a request for unlimited Listen state. However, at
4739 * least for now, this is mapped to a Listen state for one
4740 * hour.
4741 */
4742 timeout = 3600;
4743 }
4744 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4745 wpa_s->p2p_long_listen = 0;
4746
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004747 /*
4748 * Stop previous find/listen operation to avoid trying to request a new
4749 * remain-on-channel operation while the driver is still running the
4750 * previous one.
4751 */
4752 if (wpa_s->global->p2p)
4753 p2p_stop_find(wpa_s->global->p2p);
4754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004755 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4756 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4757 wpa_s->p2p_long_listen = timeout * 1000;
4758 eloop_register_timeout(timeout, 0,
4759 wpas_p2p_long_listen_timeout,
4760 wpa_s, NULL);
4761 }
4762
4763 return res;
4764}
4765
4766
4767int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4768 u8 *buf, size_t len, int p2p_group)
4769{
4770 struct wpabuf *p2p_ie;
4771 int ret;
4772
4773 if (wpa_s->global->p2p_disabled)
4774 return -1;
4775 if (wpa_s->global->p2p == NULL)
4776 return -1;
4777 if (bss == NULL)
4778 return -1;
4779
4780 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4781 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4782 p2p_group, p2p_ie);
4783 wpabuf_free(p2p_ie);
4784
4785 return ret;
4786}
4787
4788
4789int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004790 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004791 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004792{
4793 if (wpa_s->global->p2p_disabled)
4794 return 0;
4795 if (wpa_s->global->p2p == NULL)
4796 return 0;
4797
Dmitry Shmidt04949592012-07-19 12:16:46 -07004798 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4799 ie, ie_len)) {
4800 case P2P_PREQ_NOT_P2P:
4801 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4802 ssi_signal);
4803 /* fall through */
4804 case P2P_PREQ_MALFORMED:
4805 case P2P_PREQ_NOT_LISTEN:
4806 case P2P_PREQ_NOT_PROCESSED:
4807 default: /* make gcc happy */
4808 return 0;
4809 case P2P_PREQ_PROCESSED:
4810 return 1;
4811 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004812}
4813
4814
4815void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4816 const u8 *sa, const u8 *bssid,
4817 u8 category, const u8 *data, size_t len, int freq)
4818{
4819 if (wpa_s->global->p2p_disabled)
4820 return;
4821 if (wpa_s->global->p2p == NULL)
4822 return;
4823
4824 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4825 freq);
4826}
4827
4828
4829void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4830{
4831 if (wpa_s->global->p2p_disabled)
4832 return;
4833 if (wpa_s->global->p2p == NULL)
4834 return;
4835
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004836 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837}
4838
4839
4840void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4841{
4842 p2p_group_deinit(wpa_s->p2p_group);
4843 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004844
4845 wpa_s->ap_configured_cb = NULL;
4846 wpa_s->ap_configured_cb_ctx = NULL;
4847 wpa_s->ap_configured_cb_data = NULL;
4848 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004849}
4850
4851
4852int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4853{
4854 wpa_s->p2p_long_listen = 0;
4855
4856 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4857 return wpa_drv_p2p_reject(wpa_s, addr);
4858
4859 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4860 return -1;
4861
4862 return p2p_reject(wpa_s->global->p2p, addr);
4863}
4864
4865
4866/* Invite to reinvoke a persistent group */
4867int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004868 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004869 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870{
4871 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004872 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004873 int force_freq = 0, oper_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004874 int res;
4875
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004876 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004877 if (peer_addr)
4878 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4879 else
4880 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881
Jouni Malinen31be0a42012-08-31 21:20:51 +03004882 wpa_s->p2p_persistent_go_freq = freq;
4883 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 if (ssid->mode == WPAS_MODE_P2P_GO) {
4885 role = P2P_INVITE_ROLE_GO;
4886 if (peer_addr == NULL) {
4887 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4888 "address in invitation command");
4889 return -1;
4890 }
4891 if (wpas_p2p_create_iface(wpa_s)) {
4892 if (wpas_p2p_add_group_interface(wpa_s,
4893 WPA_IF_P2P_GO) < 0) {
4894 wpa_printf(MSG_ERROR, "P2P: Failed to "
4895 "allocate a new interface for the "
4896 "group");
4897 return -1;
4898 }
4899 bssid = wpa_s->pending_interface_addr;
4900 } else
4901 bssid = wpa_s->own_addr;
4902 } else {
4903 role = P2P_INVITE_ROLE_CLIENT;
4904 peer_addr = ssid->bssid;
4905 }
4906 wpa_s->pending_invite_ssid_id = ssid->id;
4907
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004908 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4909 wpa_s->assoc_freq) {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004910 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004911 if (bssid == NULL)
4912 bssid = bssid_buf;
4913 } else {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004914 oper_freq = wpa_drv_shared_freq(wpa_s);
4915 if (oper_freq < 0)
4916 oper_freq = 0;
4917 }
4918
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004919 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4920 &oper_freq);
4921 if (res)
4922 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4925 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4926 ssid->ssid, ssid->ssid_len,
4927 go_dev_addr, 1);
4928
4929 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4930 return -1;
4931
4932 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004933 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4934 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935}
4936
4937
4938/* Invite to join an active group */
4939int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4940 const u8 *peer_addr, const u8 *go_dev_addr)
4941{
4942 struct wpa_global *global = wpa_s->global;
4943 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004944 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004946 int persistent;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004947 int force_freq = 0, oper_freq = 0, pref_freq = 0;
4948 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004949
Jouni Malinen31be0a42012-08-31 21:20:51 +03004950 wpa_s->p2p_persistent_go_freq = 0;
4951 wpa_s->p2p_go_ht40 = 0;
4952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4954 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4955 break;
4956 }
4957 if (wpa_s == NULL) {
4958 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4959 return -1;
4960 }
4961
4962 ssid = wpa_s->current_ssid;
4963 if (ssid == NULL) {
4964 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4965 "invitation");
4966 return -1;
4967 }
4968
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004969 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004970 persistent = ssid->p2p_persistent_group &&
4971 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4972 ssid->ssid, ssid->ssid_len);
4973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004974 if (ssid->mode == WPAS_MODE_P2P_GO) {
4975 role = P2P_INVITE_ROLE_ACTIVE_GO;
4976 bssid = wpa_s->own_addr;
4977 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004978 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979 } else {
4980 role = P2P_INVITE_ROLE_CLIENT;
4981 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4982 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4983 "invite to current group");
4984 return -1;
4985 }
4986 bssid = wpa_s->bssid;
4987 if (go_dev_addr == NULL &&
4988 !is_zero_ether_addr(wpa_s->go_dev_addr))
4989 go_dev_addr = wpa_s->go_dev_addr;
4990 }
4991 wpa_s->parent->pending_invite_ssid_id = -1;
4992
4993 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4994 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4995 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004996 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997
4998 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4999 return -1;
5000
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005001 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
5002 wpa_s->assoc_freq) {
5003 oper_freq = wpa_s->assoc_freq;
5004 if (bssid == NULL)
5005 bssid = bssid_buf;
5006 } else {
5007 oper_freq = wpa_drv_shared_freq(wpa_s);
5008 if (oper_freq < 0)
5009 oper_freq = 0;
5010 }
5011
5012 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
5013 &oper_freq);
5014 if (res)
5015 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005016 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005019 ssid->ssid, ssid->ssid_len, force_freq,
5020 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021}
5022
5023
5024void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5025{
5026 struct wpa_ssid *ssid = wpa_s->current_ssid;
5027 const char *ssid_txt;
5028 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005029 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005031 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032
Dmitry Shmidt04949592012-07-19 12:16:46 -07005033 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5034 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5035 wpa_s->parent, NULL);
5036 }
5037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005039 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005040
5041 wpa_s->show_group_started = 0;
5042
5043 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5044 os_memset(go_dev_addr, 0, ETH_ALEN);
5045 if (ssid->bssid_set)
5046 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5047 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5048 ssid->ssid_len);
5049 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5050
5051 if (wpa_s->global->p2p_group_formation == wpa_s)
5052 wpa_s->global->p2p_group_formation = NULL;
5053
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005054 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5055 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056 if (ssid->passphrase == NULL && ssid->psk_set) {
5057 char psk[65];
5058 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5059 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5060 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
5061 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005062 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063 MAC2STR(go_dev_addr),
5064 persistent ? " [PERSISTENT]" : "");
5065 } else {
5066 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5067 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
5068 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005069 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005070 ssid->passphrase ? ssid->passphrase : "",
5071 MAC2STR(go_dev_addr),
5072 persistent ? " [PERSISTENT]" : "");
5073 }
5074
5075 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005076 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5077 ssid, go_dev_addr);
5078 if (network_id < 0)
5079 network_id = ssid->id;
5080 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005081
5082done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005083 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005084}
5085
5086
5087int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5088 u32 interval1, u32 duration2, u32 interval2)
5089{
5090 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5091 return -1;
5092 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5093 return -1;
5094
5095 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5096 wpa_s->current_ssid == NULL ||
5097 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5098 return -1;
5099
5100 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5101 wpa_s->own_addr, wpa_s->assoc_freq,
5102 duration1, interval1, duration2, interval2);
5103}
5104
5105
5106int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5107 unsigned int interval)
5108{
5109 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5110 return -1;
5111
5112 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5113 return -1;
5114
5115 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5116}
5117
5118
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005119static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5120{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005121 if (wpa_s->current_ssid == NULL) {
5122 /*
5123 * current_ssid can be cleared when P2P client interface gets
5124 * disconnected, so assume this interface was used as P2P
5125 * client.
5126 */
5127 return 1;
5128 }
5129 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005130 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5131}
5132
5133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5135{
5136 struct wpa_supplicant *wpa_s = eloop_ctx;
5137
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005138 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5140 "disabled");
5141 return;
5142 }
5143
Dmitry Shmidt04949592012-07-19 12:16:46 -07005144 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5145 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005146 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147}
5148
5149
5150static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5151{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005152 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005153
Dmitry Shmidt04949592012-07-19 12:16:46 -07005154 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5155 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5156
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005157 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5158 return;
5159
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005160 timeout = wpa_s->conf->p2p_group_idle;
5161 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5162 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5163 timeout = P2P_MAX_CLIENT_IDLE;
5164
5165 if (timeout == 0)
5166 return;
5167
Dmitry Shmidt04949592012-07-19 12:16:46 -07005168 if (timeout < 0) {
5169 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5170 timeout = 0; /* special client mode no-timeout */
5171 else
5172 return;
5173 }
5174
5175 if (wpa_s->p2p_in_provisioning) {
5176 /*
5177 * Use the normal group formation timeout during the
5178 * provisioning phase to avoid terminating this process too
5179 * early due to group idle timeout.
5180 */
5181 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5182 "during provisioning");
5183 return;
5184 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305185
Dmitry Shmidt04949592012-07-19 12:16:46 -07005186 if (wpa_s->show_group_started) {
5187 /*
5188 * Use the normal group formation timeout between the end of
5189 * the provisioning phase and completion of 4-way handshake to
5190 * avoid terminating this process too early due to group idle
5191 * timeout.
5192 */
5193 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5194 "while waiting for initial 4-way handshake to "
5195 "complete");
5196 return;
5197 }
5198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005199 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005200 timeout);
5201 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5202 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005203}
5204
5205
Jouni Malinen2b89da82012-08-31 22:04:41 +03005206/* Returns 1 if the interface was removed */
5207int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5208 u16 reason_code, const u8 *ie, size_t ie_len,
5209 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005210{
5211 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005212 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005213 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005214 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005215
Dmitry Shmidt04949592012-07-19 12:16:46 -07005216 if (!locally_generated)
5217 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5218 ie_len);
5219
5220 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5221 wpa_s->current_ssid &&
5222 wpa_s->current_ssid->p2p_group &&
5223 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5224 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5225 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005226 if (wpas_p2p_group_delete(wpa_s,
5227 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5228 > 0)
5229 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005230 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005231
5232 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005233}
5234
5235
5236void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005237 u16 reason_code, const u8 *ie, size_t ie_len,
5238 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005239{
5240 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5241 return;
5242 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5243 return;
5244
Dmitry Shmidt04949592012-07-19 12:16:46 -07005245 if (!locally_generated)
5246 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5247 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005248}
5249
5250
5251void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5252{
5253 struct p2p_data *p2p = wpa_s->global->p2p;
5254
5255 if (p2p == NULL)
5256 return;
5257
5258 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5259 return;
5260
5261 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5262 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5263
5264 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5265 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5266
5267 if (wpa_s->wps &&
5268 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5269 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5270
5271 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5272 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5273
5274 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5275 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5276 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5277 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5278 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5279 }
5280
5281 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5282 p2p_set_sec_dev_types(p2p,
5283 (void *) wpa_s->conf->sec_device_type,
5284 wpa_s->conf->num_sec_device_types);
5285
5286 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5287 int i;
5288 p2p_remove_wps_vendor_extensions(p2p);
5289 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5290 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5291 continue;
5292 p2p_add_wps_vendor_extension(
5293 p2p, wpa_s->conf->wps_vendor_ext[i]);
5294 }
5295 }
5296
5297 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5298 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5299 char country[3];
5300 country[0] = wpa_s->conf->country[0];
5301 country[1] = wpa_s->conf->country[1];
5302 country[2] = 0x04;
5303 p2p_set_country(p2p, country);
5304 }
5305
5306 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5307 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5308 wpa_s->conf->p2p_ssid_postfix ?
5309 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5310 0);
5311 }
5312
5313 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5314 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005315
5316 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5317 u8 reg_class, channel;
5318 int ret;
5319 unsigned int r;
5320 if (wpa_s->conf->p2p_listen_reg_class &&
5321 wpa_s->conf->p2p_listen_channel) {
5322 reg_class = wpa_s->conf->p2p_listen_reg_class;
5323 channel = wpa_s->conf->p2p_listen_channel;
5324 } else {
5325 reg_class = 81;
5326 /*
5327 * Pick one of the social channels randomly as the
5328 * listen channel.
5329 */
5330 os_get_random((u8 *) &r, sizeof(r));
5331 channel = 1 + (r % 3) * 5;
5332 }
5333 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5334 if (ret)
5335 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5336 "failed: %d", ret);
5337 }
5338 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5339 u8 op_reg_class, op_channel, cfg_op_channel;
5340 int ret = 0;
5341 unsigned int r;
5342 if (wpa_s->conf->p2p_oper_reg_class &&
5343 wpa_s->conf->p2p_oper_channel) {
5344 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5345 op_channel = wpa_s->conf->p2p_oper_channel;
5346 cfg_op_channel = 1;
5347 } else {
5348 op_reg_class = 81;
5349 /*
5350 * Use random operation channel from (1, 6, 11)
5351 *if no other preference is indicated.
5352 */
5353 os_get_random((u8 *) &r, sizeof(r));
5354 op_channel = 1 + (r % 3) * 5;
5355 cfg_op_channel = 0;
5356 }
5357 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5358 cfg_op_channel);
5359 if (ret)
5360 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5361 "failed: %d", ret);
5362 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005363
5364 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5365 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5366 wpa_s->conf->p2p_pref_chan) < 0) {
5367 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5368 "update failed");
5369 }
5370 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005371}
5372
5373
5374int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5375 int duration)
5376{
5377 if (!wpa_s->ap_iface)
5378 return -1;
5379 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5380 duration);
5381}
5382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383
5384int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5385{
5386 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5387 return -1;
5388 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5389 return -1;
5390
5391 wpa_s->global->cross_connection = enabled;
5392 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5393
5394 if (!enabled) {
5395 struct wpa_supplicant *iface;
5396
5397 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5398 {
5399 if (iface->cross_connect_enabled == 0)
5400 continue;
5401
5402 iface->cross_connect_enabled = 0;
5403 iface->cross_connect_in_use = 0;
5404 wpa_msg(iface->parent, MSG_INFO,
5405 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5406 iface->ifname, iface->cross_connect_uplink);
5407 }
5408 }
5409
5410 return 0;
5411}
5412
5413
5414static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5415{
5416 struct wpa_supplicant *iface;
5417
5418 if (!uplink->global->cross_connection)
5419 return;
5420
5421 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5422 if (!iface->cross_connect_enabled)
5423 continue;
5424 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5425 0)
5426 continue;
5427 if (iface->ap_iface == NULL)
5428 continue;
5429 if (iface->cross_connect_in_use)
5430 continue;
5431
5432 iface->cross_connect_in_use = 1;
5433 wpa_msg(iface->parent, MSG_INFO,
5434 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5435 iface->ifname, iface->cross_connect_uplink);
5436 }
5437}
5438
5439
5440static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5441{
5442 struct wpa_supplicant *iface;
5443
5444 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5445 if (!iface->cross_connect_enabled)
5446 continue;
5447 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5448 0)
5449 continue;
5450 if (!iface->cross_connect_in_use)
5451 continue;
5452
5453 wpa_msg(iface->parent, MSG_INFO,
5454 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5455 iface->ifname, iface->cross_connect_uplink);
5456 iface->cross_connect_in_use = 0;
5457 }
5458}
5459
5460
5461void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5462{
5463 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5464 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5465 wpa_s->cross_connect_disallowed)
5466 wpas_p2p_disable_cross_connect(wpa_s);
5467 else
5468 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005469 if (!wpa_s->ap_iface &&
5470 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5471 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472}
5473
5474
5475void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5476{
5477 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005478 if (!wpa_s->ap_iface &&
5479 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5480 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481 wpas_p2p_set_group_idle_timeout(wpa_s);
5482}
5483
5484
5485static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5486{
5487 struct wpa_supplicant *iface;
5488
5489 if (!wpa_s->global->cross_connection)
5490 return;
5491
5492 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5493 if (iface == wpa_s)
5494 continue;
5495 if (iface->drv_flags &
5496 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5497 continue;
5498 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5499 continue;
5500
5501 wpa_s->cross_connect_enabled = 1;
5502 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5503 sizeof(wpa_s->cross_connect_uplink));
5504 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5505 "%s to %s whenever uplink is available",
5506 wpa_s->ifname, wpa_s->cross_connect_uplink);
5507
5508 if (iface->ap_iface || iface->current_ssid == NULL ||
5509 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5510 iface->cross_connect_disallowed ||
5511 iface->wpa_state != WPA_COMPLETED)
5512 break;
5513
5514 wpa_s->cross_connect_in_use = 1;
5515 wpa_msg(wpa_s->parent, MSG_INFO,
5516 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5517 wpa_s->ifname, wpa_s->cross_connect_uplink);
5518 break;
5519 }
5520}
5521
5522
5523int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5524{
5525 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5526 !wpa_s->p2p_in_provisioning)
5527 return 0; /* not P2P client operation */
5528
5529 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5530 "session overlap");
5531 if (wpa_s != wpa_s->parent)
5532 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5533
5534 if (wpa_s->global->p2p)
5535 p2p_group_formation_failed(wpa_s->global->p2p);
5536
5537 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5538 wpa_s->parent, NULL);
5539
5540 wpas_group_formation_completed(wpa_s, 0);
5541 return 1;
5542}
5543
5544
5545void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5546{
5547 struct p2p_channels chan;
5548
5549 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5550 return;
5551
5552 os_memset(&chan, 0, sizeof(chan));
5553 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5554 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5555 "channel list");
5556 return;
5557 }
5558
5559 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5560}
5561
5562
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005563static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5564 struct wpa_scan_results *scan_res)
5565{
5566 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5567}
5568
5569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005570int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5571{
5572 struct wpa_global *global = wpa_s->global;
5573 int found = 0;
5574 const u8 *peer;
5575
5576 if (global->p2p == NULL)
5577 return -1;
5578
5579 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5580
5581 if (wpa_s->pending_interface_name[0] &&
5582 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5583 found = 1;
5584
5585 peer = p2p_get_go_neg_peer(global->p2p);
5586 if (peer) {
5587 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5588 MACSTR, MAC2STR(peer));
5589 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005590 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005591 }
5592
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005593 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5594 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5595 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5596 found = 1;
5597 }
5598
5599 if (wpa_s->pending_pd_before_join) {
5600 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5601 wpa_s->pending_pd_before_join = 0;
5602 found = 1;
5603 }
5604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005605 wpas_p2p_stop_find(wpa_s);
5606
5607 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5608 if (wpa_s == global->p2p_group_formation &&
5609 (wpa_s->p2p_in_provisioning ||
5610 wpa_s->parent->pending_interface_type ==
5611 WPA_IF_P2P_CLIENT)) {
5612 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5613 "formation found - cancelling",
5614 wpa_s->ifname);
5615 found = 1;
5616 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5617 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005618 if (wpa_s->p2p_in_provisioning) {
5619 wpas_group_formation_completed(wpa_s, 0);
5620 break;
5621 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005622 wpas_p2p_group_delete(wpa_s,
5623 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005624 break;
5625 }
5626 }
5627
5628 if (!found) {
5629 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5630 return -1;
5631 }
5632
5633 return 0;
5634}
5635
5636
5637void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5638{
5639 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5640 return;
5641
5642 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5643 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005644 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005645}
5646
5647
5648void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5649 int freq_24, int freq_5, int freq_overall)
5650{
5651 struct p2p_data *p2p = wpa_s->global->p2p;
5652 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5653 return;
5654 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5655}
5656
5657
5658int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5659{
5660 u8 peer[ETH_ALEN];
5661 struct p2p_data *p2p = wpa_s->global->p2p;
5662
5663 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5664 return -1;
5665
5666 if (hwaddr_aton(addr, peer))
5667 return -1;
5668
5669 return p2p_unauthorize(p2p, peer);
5670}
5671
5672
5673/**
5674 * wpas_p2p_disconnect - Disconnect from a P2P Group
5675 * @wpa_s: Pointer to wpa_supplicant data
5676 * Returns: 0 on success, -1 on failure
5677 *
5678 * This can be used to disconnect from a group in which the local end is a P2P
5679 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5680 * virtual network interface was created for this group, that interface will be
5681 * removed. Otherwise, only the configured P2P group network will be removed
5682 * from the interface.
5683 */
5684int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5685{
5686
5687 if (wpa_s == NULL)
5688 return -1;
5689
Jouni Malinen2b89da82012-08-31 22:04:41 +03005690 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5691 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005692}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005693
5694
5695int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5696{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005697 int ret;
5698
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005699 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5700 return 0;
5701
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005702 ret = p2p_in_progress(wpa_s->global->p2p);
5703 if (ret == 0) {
5704 /*
5705 * Check whether there is an ongoing WPS provisioning step (or
5706 * other parts of group formation) on another interface since
5707 * p2p_in_progress() does not report this to avoid issues for
5708 * scans during such provisioning step.
5709 */
5710 if (wpa_s->global->p2p_group_formation &&
5711 wpa_s->global->p2p_group_formation != wpa_s) {
5712 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5713 "in group formation",
5714 wpa_s->global->p2p_group_formation->ifname);
5715 ret = 1;
5716 }
5717 }
5718
5719 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005720}
5721
5722
5723void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5724 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005725{
5726 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5727 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5728 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005729 /**
5730 * Remove the network by scheduling the group formation
5731 * timeout to happen immediately. The teardown code
5732 * needs to be scheduled to run asynch later so that we
5733 * don't delete data from under ourselves unexpectedly.
5734 * Calling wpas_p2p_group_formation_timeout directly
5735 * causes a series of crashes in WPS failure scenarios.
5736 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005737 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5738 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005739 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5740 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005741 }
5742}
5743
5744
5745struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005746 const u8 *addr, const u8 *ssid,
5747 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005748{
5749 struct wpa_ssid *s;
5750 size_t i;
5751
5752 for (s = wpa_s->conf->ssid; s; s = s->next) {
5753 if (s->disabled != 2)
5754 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005755 if (ssid &&
5756 (ssid_len != s->ssid_len ||
5757 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5758 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005759 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5760 return s; /* peer is GO in the persistent group */
5761 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5762 continue;
5763 for (i = 0; i < s->num_p2p_clients; i++) {
5764 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5765 addr, ETH_ALEN) == 0)
5766 return s; /* peer is P2P client in persistent
5767 * group */
5768 }
5769 }
5770
5771 return NULL;
5772}
5773
5774
5775void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5776 const u8 *addr)
5777{
5778 if (addr == NULL)
5779 return;
5780 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5781}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005782
Dmitry Shmidt04949592012-07-19 12:16:46 -07005783
5784static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5785 int group_added)
5786{
5787 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005788 if (wpa_s->global->p2p_group_formation)
5789 group = wpa_s->global->p2p_group_formation;
5790 wpa_s = wpa_s->parent;
5791 offchannel_send_action_done(wpa_s);
5792 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005793 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005794 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5795 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5796 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5797 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5798 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005799 wpa_s->p2p_pd_before_go_neg,
5800 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005801}
5802
5803
5804int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5805{
5806 if (!wpa_s->p2p_fallback_to_go_neg ||
5807 wpa_s->p2p_in_provisioning <= 5)
5808 return 0;
5809
5810 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5811 return 0; /* peer operating as a GO */
5812
5813 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5814 "fallback to GO Negotiation");
5815 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5816
5817 return 1;
5818}
5819
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005820
5821unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5822{
5823 const char *rn, *rn2;
5824 struct wpa_supplicant *ifs;
5825
5826 if (wpa_s->wpa_state > WPA_SCANNING) {
5827 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5828 "concurrent operation",
5829 P2P_CONCURRENT_SEARCH_DELAY);
5830 return P2P_CONCURRENT_SEARCH_DELAY;
5831 }
5832
5833 if (!wpa_s->driver->get_radio_name)
5834 return 0;
5835 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5836 if (rn == NULL || rn[0] == '\0')
5837 return 0;
5838
5839 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5840 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5841 continue;
5842
5843 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5844 if (!rn2 || os_strcmp(rn, rn2) != 0)
5845 continue;
5846 if (ifs->wpa_state > WPA_SCANNING) {
5847 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5848 "delay due to concurrent operation on "
5849 "interface %s",
5850 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5851 return P2P_CONCURRENT_SEARCH_DELAY;
5852 }
5853 }
5854
5855 return 0;
5856}
5857
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005858
5859void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5860{
5861 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5862 "pending anymore (sta_scan_pending=%d "
5863 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5864 wpa_s->global->p2p_cb_on_scan_complete);
5865 wpa_s->sta_scan_pending = 0;
5866
5867 if (!wpa_s->global->p2p_cb_on_scan_complete)
5868 return;
5869 wpa_s->global->p2p_cb_on_scan_complete = 0;
5870
5871 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5872 return;
5873
5874 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
5875 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
5876 "continued after successful connection");
5877 p2p_increase_search_delay(wpa_s->global->p2p,
5878 wpas_p2p_search_delay(wpa_s));
5879 }
5880}
5881
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005882#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07005883static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
5884{
5885 struct wpa_supplicant *wpa_s = eloop_ctx;
5886
5887 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
5888 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
5889}
5890
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005891int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5892 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005893{
5894 struct wpa_supplicant *iface = NULL;
5895 struct p2p_data *p2p = wpa_s->global->p2p;
5896
5897 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005898 if ((iface->current_ssid) &&
5899 (iface->current_ssid->frequency != freq) &&
5900 ((iface->p2p_group_interface) ||
5901 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005902
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005903 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5904 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5905 /* Try to see whether we can move the GO. If it
5906 * is not possible, remove the GO interface
5907 */
5908 if (wpa_drv_switch_channel(iface, freq) == 0) {
5909 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5910 iface->current_ssid->frequency = freq;
5911 continue;
5912 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005913 }
5914
5915 /* If GO cannot be moved or if the conflicting interface is a
5916 * P2P Client, remove the interface depending up on the connection
5917 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005918 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005919 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005920 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005921 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005922 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07005923 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
5924 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005925 /* If connection in progress is p2p connection, do not proceed for the connection */
5926 if (wpa_s == iface)
5927 return -1;
5928 else
5929 /* If connection in progress is STA connection, proceed for the connection */
5930 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005931 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005932 /* P2p connection has priority, disable the STA network*/
5933 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5934 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5935 " id=%d", ssid->id);
5936 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5937 if (wpa_s == iface) {
5938 /* p2p connection is in progress, continue connecting...*/
5939 return 0;
5940 }
5941 else {
5942 /* STA connection is in progress, do not allow to continue */
5943 return -1;
5944 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005945 }
5946 }
5947 }
5948 return 0;
5949}
5950#endif