blob: f9193aefbe6120e2534b547c6987cdf9bbc320b2 [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;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700324 wpa_msg_global(wpa_s->parent, MSG_INFO,
325 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
326 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 }
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 Shmidtb6e9aaf2013-05-20 14:49:44 -0700354 wpa_msg_global(wpa_s->parent, MSG_INFO,
355 P2P_EVENT_GROUP_REMOVED "%s %s%s",
356 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700357 }
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) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700666 wpa_msg_global(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
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700673 wpa_msg_global(wpa_s->parent, MSG_INFO,
674 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675
676 ssid = wpa_s->current_ssid;
677 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
678 ssid->mode = WPAS_MODE_P2P_GO;
679 p2p_group_notif_formation_done(wpa_s->p2p_group);
680 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
681 }
682
683 persistent = 0;
684 if (ssid) {
685 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
686 client = ssid->mode == WPAS_MODE_INFRA;
687 if (ssid->mode == WPAS_MODE_P2P_GO) {
688 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700689 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
690 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 } else
692 persistent = wpas_p2p_persistent_group(wpa_s,
693 go_dev_addr,
694 ssid->ssid,
695 ssid->ssid_len);
696 } else {
697 ssid_txt = "";
698 client = wpa_s->p2p_group_interface ==
699 P2P_GROUP_INTERFACE_CLIENT;
700 os_memset(go_dev_addr, 0, ETH_ALEN);
701 }
702
703 wpa_s->show_group_started = 0;
704 if (client) {
705 /*
706 * Indicate event only after successfully completed 4-way
707 * handshake, i.e., when the interface is ready for data
708 * packets.
709 */
710 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700711#ifdef ANDROID_P2P
712 /* For client Second phase of Group formation (4-way handshake) can be still pending
713 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700714 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 -0700715 wpa_s->global->p2p_group_formation = wpa_s;
716#endif
717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
719 char psk[65];
720 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700721 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
722 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
723 MACSTR "%s",
724 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
725 MAC2STR(go_dev_addr),
726 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727 wpas_p2p_cross_connect_setup(wpa_s);
728 wpas_p2p_set_group_idle_timeout(wpa_s);
729 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700730 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
731 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
732 "go_dev_addr=" MACSTR "%s",
733 wpa_s->ifname, ssid_txt,
734 ssid ? ssid->frequency : 0,
735 ssid && ssid->passphrase ? ssid->passphrase : "",
736 MAC2STR(go_dev_addr),
737 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 wpas_p2p_cross_connect_setup(wpa_s);
739 wpas_p2p_set_group_idle_timeout(wpa_s);
740 }
741
742 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700743 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
744 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800745 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700746 network_id = ssid->id;
747 if (!client)
748 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749}
750
751
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800752static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
753 unsigned int freq,
754 const u8 *dst, const u8 *src,
755 const u8 *bssid,
756 const u8 *data, size_t data_len,
757 enum offchannel_send_action_result
758 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800760 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
763 return;
764 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
765 return;
766
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800767 switch (result) {
768 case OFFCHANNEL_SEND_ACTION_SUCCESS:
769 res = P2P_SEND_ACTION_SUCCESS;
770 break;
771 case OFFCHANNEL_SEND_ACTION_NO_ACK:
772 res = P2P_SEND_ACTION_NO_ACK;
773 break;
774 case OFFCHANNEL_SEND_ACTION_FAILED:
775 res = P2P_SEND_ACTION_FAILED;
776 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777 }
778
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800779 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800781 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
782 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800783 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800784 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
785 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800787 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
788 "during p2p_connect-auto");
789 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
790 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700791 }
792}
793
794
795static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
796 const u8 *src, const u8 *bssid, const u8 *buf,
797 size_t len, unsigned int wait_time)
798{
799 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
801 wait_time,
802 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803}
804
805
806static void wpas_send_action_done(void *ctx)
807{
808 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800809 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810}
811
812
813static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
814 struct p2p_go_neg_results *params)
815{
816 if (wpa_s->go_params == NULL) {
817 wpa_s->go_params = os_malloc(sizeof(*params));
818 if (wpa_s->go_params == NULL)
819 return -1;
820 }
821 os_memcpy(wpa_s->go_params, params, sizeof(*params));
822 return 0;
823}
824
825
826static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
827 struct p2p_go_neg_results *res)
828{
829 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
830 MAC2STR(res->peer_interface_addr));
831 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
832 res->ssid, res->ssid_len);
833 wpa_supplicant_ap_deinit(wpa_s);
834 wpas_copy_go_neg_results(wpa_s, res);
835 if (res->wps_method == WPS_PBC)
836 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
837 else {
838 u16 dev_pw_id = DEV_PW_DEFAULT;
839 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
840 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
841 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
842 wpa_s->p2p_pin, 1, dev_pw_id);
843 }
844}
845
846
847static void p2p_go_configured(void *ctx, void *data)
848{
849 struct wpa_supplicant *wpa_s = ctx;
850 struct p2p_go_neg_results *params = data;
851 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700852 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700853
854 ssid = wpa_s->current_ssid;
855 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
856 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
857 if (wpa_s->global->p2p_group_formation == wpa_s)
858 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800859 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700860 wpa_msg_global(wpa_s->parent, MSG_INFO,
861 P2P_EVENT_GROUP_STARTED
862 "%s GO ssid=\"%s\" freq=%d "
863 "passphrase=\"%s\" go_dev_addr=" MACSTR
864 "%s", wpa_s->ifname,
865 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
866 ssid->frequency, params->passphrase,
867 MAC2STR(wpa_s->global->p2p_dev_addr),
868 params->persistent_group ?
869 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800870 } else {
871 char psk[65];
872 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
873 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700874 wpa_msg_global(wpa_s->parent, MSG_INFO,
875 P2P_EVENT_GROUP_STARTED
876 "%s GO ssid=\"%s\" freq=%d psk=%s "
877 "go_dev_addr=" MACSTR "%s",
878 wpa_s->ifname,
879 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
880 ssid->frequency, psk,
881 MAC2STR(wpa_s->global->p2p_dev_addr),
882 params->persistent_group ?
883 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800884 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700887 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700889 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700890 if (network_id < 0)
891 network_id = ssid->id;
892 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 wpas_p2p_cross_connect_setup(wpa_s);
894 wpas_p2p_set_group_idle_timeout(wpa_s);
895 return;
896 }
897
898 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
899 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
900 params->peer_interface_addr)) {
901 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
902 "filtering");
903 return;
904 }
905 if (params->wps_method == WPS_PBC)
906 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800907 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 else if (wpa_s->p2p_pin[0])
909 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800910 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 os_free(wpa_s->go_params);
912 wpa_s->go_params = NULL;
913}
914
915
916static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
917 struct p2p_go_neg_results *params,
918 int group_formation)
919{
920 struct wpa_ssid *ssid;
921
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700922 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
923 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
924 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
925 "results");
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
929 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700930 if (ssid == NULL) {
931 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800935 wpa_s->show_group_started = 0;
936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 wpa_config_set_network_defaults(ssid);
938 ssid->temporary = 1;
939 ssid->p2p_group = 1;
940 ssid->p2p_persistent_group = params->persistent_group;
941 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
942 WPAS_MODE_P2P_GO;
943 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700944 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 ssid->ssid = os_zalloc(params->ssid_len + 1);
946 if (ssid->ssid) {
947 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
948 ssid->ssid_len = params->ssid_len;
949 }
950 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
951 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
952 ssid->proto = WPA_PROTO_RSN;
953 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800954 if (os_strlen(params->passphrase) > 0) {
955 ssid->passphrase = os_strdup(params->passphrase);
956 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700957 wpa_msg_global(wpa_s, MSG_ERROR,
958 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800959 wpa_config_remove_network(wpa_s->conf, ssid->id);
960 return;
961 }
962 } else
963 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800964 ssid->psk_set = params->psk_set;
965 if (ssid->psk_set)
966 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800967 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800968 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700969 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970
971 wpa_s->ap_configured_cb = p2p_go_configured;
972 wpa_s->ap_configured_cb_ctx = wpa_s;
973 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700974 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 wpa_s->reassociate = 1;
976 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700977 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
978 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 wpa_supplicant_req_scan(wpa_s, 0, 0);
980}
981
982
983static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
984 const struct wpa_supplicant *src)
985{
986 struct wpa_config *d;
987 const struct wpa_config *s;
988
989 d = dst->conf;
990 s = src->conf;
991
992#define C(n) if (s->n) d->n = os_strdup(s->n)
993 C(device_name);
994 C(manufacturer);
995 C(model_name);
996 C(model_number);
997 C(serial_number);
998 C(config_methods);
999#undef C
1000
1001 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1002 os_memcpy(d->sec_device_type, s->sec_device_type,
1003 sizeof(d->sec_device_type));
1004 d->num_sec_device_types = s->num_sec_device_types;
1005
1006 d->p2p_group_idle = s->p2p_group_idle;
1007 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001008 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001009 d->max_num_sta = s->max_num_sta;
1010 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001011 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001012 d->beacon_int = s->beacon_int;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013}
1014
1015
1016static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1017 enum wpa_driver_if_type type)
1018{
1019 char ifname[120], force_ifname[120];
1020
1021 if (wpa_s->pending_interface_name[0]) {
1022 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1023 "- skip creation of a new one");
1024 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1025 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1026 "unknown?! ifname='%s'",
1027 wpa_s->pending_interface_name);
1028 return -1;
1029 }
1030 return 0;
1031 }
1032
1033 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
1034 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001035 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1037 /* Try to avoid going over the IFNAMSIZ length limit */
1038 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1039 wpa_s->p2p_group_idx);
1040 }
1041 force_ifname[0] = '\0';
1042
1043 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1044 ifname);
1045 wpa_s->p2p_group_idx++;
1046
1047 wpa_s->pending_interface_type = type;
1048 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1049 wpa_s->pending_interface_addr, NULL) < 0) {
1050 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1051 "interface");
1052 return -1;
1053 }
1054
1055 if (force_ifname[0]) {
1056 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1057 force_ifname);
1058 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1059 sizeof(wpa_s->pending_interface_name));
1060 } else
1061 os_strlcpy(wpa_s->pending_interface_name, ifname,
1062 sizeof(wpa_s->pending_interface_name));
1063 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1064 MACSTR, wpa_s->pending_interface_name,
1065 MAC2STR(wpa_s->pending_interface_addr));
1066
1067 return 0;
1068}
1069
1070
1071static void wpas_p2p_remove_pending_group_interface(
1072 struct wpa_supplicant *wpa_s)
1073{
1074 if (!wpa_s->pending_interface_name[0] ||
1075 is_zero_ether_addr(wpa_s->pending_interface_addr))
1076 return; /* No pending virtual interface */
1077
1078 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1079 wpa_s->pending_interface_name);
1080 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1081 wpa_s->pending_interface_name);
1082 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1083 wpa_s->pending_interface_name[0] = '\0';
1084}
1085
1086
1087static struct wpa_supplicant *
1088wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1089{
1090 struct wpa_interface iface;
1091 struct wpa_supplicant *group_wpa_s;
1092
1093 if (!wpa_s->pending_interface_name[0]) {
1094 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1095 if (!wpas_p2p_create_iface(wpa_s))
1096 return NULL;
1097 /*
1098 * Something has forced us to remove the pending interface; try
1099 * to create a new one and hope for the best that we will get
1100 * the same local address.
1101 */
1102 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1103 WPA_IF_P2P_CLIENT) < 0)
1104 return NULL;
1105 }
1106
1107 os_memset(&iface, 0, sizeof(iface));
1108 iface.ifname = wpa_s->pending_interface_name;
1109 iface.driver = wpa_s->driver->name;
1110 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1111 iface.driver_param = wpa_s->conf->driver_param;
1112 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1113 if (group_wpa_s == NULL) {
1114 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1115 "wpa_supplicant interface");
1116 return NULL;
1117 }
1118 wpa_s->pending_interface_name[0] = '\0';
1119 group_wpa_s->parent = wpa_s;
1120 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1121 P2P_GROUP_INTERFACE_CLIENT;
1122 wpa_s->global->p2p_group_formation = group_wpa_s;
1123
1124 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1125
1126 return group_wpa_s;
1127}
1128
1129
1130static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1131 void *timeout_ctx)
1132{
1133 struct wpa_supplicant *wpa_s = eloop_ctx;
1134 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1135 if (wpa_s->global->p2p)
1136 p2p_group_formation_failed(wpa_s->global->p2p);
1137 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1138 wpa_drv_p2p_group_formation_failed(wpa_s);
1139 wpas_group_formation_completed(wpa_s, 0);
1140}
1141
1142
1143void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1144{
1145 struct wpa_supplicant *wpa_s = ctx;
1146
1147 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1148 wpa_drv_cancel_remain_on_channel(wpa_s);
1149 wpa_s->off_channel_freq = 0;
1150 wpa_s->roc_waiting_drv_freq = 0;
1151 }
1152
1153 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001154 wpa_msg_global(wpa_s, MSG_INFO,
1155 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1156 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001157 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 wpas_p2p_remove_pending_group_interface(wpa_s);
1159 return;
1160 }
1161
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001162 if (wpa_s->p2p_go_ht40)
1163 res->ht40 = 1;
1164
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001165 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001166 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167
Dmitry Shmidt04949592012-07-19 12:16:46 -07001168 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1169 struct wpa_ssid *ssid;
1170 ssid = wpa_config_get_network(wpa_s->conf,
1171 wpa_s->p2p_persistent_id);
1172 if (ssid && ssid->disabled == 2 &&
1173 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1174 size_t len = os_strlen(ssid->passphrase);
1175 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1176 "on requested persistent group");
1177 os_memcpy(res->passphrase, ssid->passphrase, len);
1178 res->passphrase[len] = '\0';
1179 }
1180 }
1181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 if (wpa_s->create_p2p_iface) {
1183 struct wpa_supplicant *group_wpa_s =
1184 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1185 if (group_wpa_s == NULL) {
1186 wpas_p2p_remove_pending_group_interface(wpa_s);
1187 return;
1188 }
1189 if (group_wpa_s != wpa_s) {
1190 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1191 sizeof(group_wpa_s->p2p_pin));
1192 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1193 }
1194 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1195 wpa_s->pending_interface_name[0] = '\0';
1196 group_wpa_s->p2p_in_provisioning = 1;
1197
1198 if (res->role_go)
1199 wpas_start_wps_go(group_wpa_s, res, 1);
1200 else
1201 wpas_start_wps_enrollee(group_wpa_s, res);
1202 } else {
1203 wpa_s->p2p_in_provisioning = 1;
1204 wpa_s->global->p2p_group_formation = wpa_s;
1205
1206 if (res->role_go)
1207 wpas_start_wps_go(wpa_s, res, 1);
1208 else
1209 wpas_start_wps_enrollee(ctx, res);
1210 }
1211
1212 wpa_s->p2p_long_listen = 0;
1213 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1214
1215 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1216 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1217 (res->peer_config_timeout % 100) * 10000,
1218 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1219}
1220
1221
1222void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1223{
1224 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001225 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1226 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227
1228 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1229}
1230
1231
1232void wpas_dev_found(void *ctx, const u8 *addr,
1233 const struct p2p_peer_info *info,
1234 int new_device)
1235{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001236#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 struct wpa_supplicant *wpa_s = ctx;
1238 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001239#define WFD_DEV_INFO_SIZE 9
1240 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001241 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001242#ifdef CONFIG_WIFI_DISPLAY
1243 if (info->wfd_subelems) {
1244 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1245 wpabuf_head(info->wfd_subelems),
1246 WFD_DEV_INFO_SIZE);
1247 }
1248#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001249 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1250 " p2p_dev_addr=" MACSTR
1251 " pri_dev_type=%s name='%s' config_methods=0x%x "
1252 "dev_capab=0x%x group_capab=0x%x%s%s",
1253 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1254 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1255 sizeof(devtype)),
1256 info->device_name, info->config_methods,
1257 info->dev_capab, info->group_capab,
1258 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001259#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260
1261 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1262}
1263
1264
1265static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1266{
1267 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001268
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001269 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1270 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001272 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1273}
1274
1275
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001276static void wpas_find_stopped(void *ctx)
1277{
1278 struct wpa_supplicant *wpa_s = ctx;
1279 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1280}
1281
1282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283static int wpas_start_listen(void *ctx, unsigned int freq,
1284 unsigned int duration,
1285 const struct wpabuf *probe_resp_ie)
1286{
1287 struct wpa_supplicant *wpa_s = ctx;
1288
1289 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1290
1291 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1292 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1293 "report received Probe Request frames");
1294 return -1;
1295 }
1296
1297 wpa_s->pending_listen_freq = freq;
1298 wpa_s->pending_listen_duration = duration;
1299
1300 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1301 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1302 "to remain on channel (%u MHz) for Listen "
1303 "state", freq);
1304 wpa_s->pending_listen_freq = 0;
1305 return -1;
1306 }
1307 wpa_s->off_channel_freq = 0;
1308 wpa_s->roc_waiting_drv_freq = freq;
1309
1310 return 0;
1311}
1312
1313
1314static void wpas_stop_listen(void *ctx)
1315{
1316 struct wpa_supplicant *wpa_s = ctx;
1317 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1318 wpa_drv_cancel_remain_on_channel(wpa_s);
1319 wpa_s->off_channel_freq = 0;
1320 wpa_s->roc_waiting_drv_freq = 0;
1321 }
1322 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1323 wpa_drv_probe_req_report(wpa_s, 0);
1324}
1325
1326
1327static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1328{
1329 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001330 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331}
1332
1333
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001334/*
1335 * DNS Header section is used only to calculate compression pointers, so the
1336 * contents of this data does not matter, but the length needs to be reserved
1337 * in the virtual packet.
1338 */
1339#define DNS_HEADER_LEN 12
1340
1341/*
1342 * 27-octet in-memory packet from P2P specification containing two implied
1343 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1344 */
1345#define P2P_SD_IN_MEMORY_LEN 27
1346
1347static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1348 u8 **spos, const u8 *end)
1349{
1350 while (*spos < end) {
1351 u8 val = ((*spos)[0] & 0xc0) >> 6;
1352 int len;
1353
1354 if (val == 1 || val == 2) {
1355 /* These are reserved values in RFC 1035 */
1356 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1357 "sequence starting with 0x%x", val);
1358 return -1;
1359 }
1360
1361 if (val == 3) {
1362 u16 offset;
1363 u8 *spos_tmp;
1364
1365 /* Offset */
1366 if (*spos + 2 > end) {
1367 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1368 "DNS offset field");
1369 return -1;
1370 }
1371
1372 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1373 if (offset >= *spos - start) {
1374 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1375 "pointer offset %u", offset);
1376 return -1;
1377 }
1378
1379 (*spos) += 2;
1380 spos_tmp = start + offset;
1381 return p2p_sd_dns_uncompress_label(upos, uend, start,
1382 &spos_tmp,
1383 *spos - 2);
1384 }
1385
1386 /* Label */
1387 len = (*spos)[0] & 0x3f;
1388 if (len == 0)
1389 return 0;
1390
1391 (*spos)++;
1392 if (*spos + len > end) {
1393 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1394 "sequence - no room for label with length "
1395 "%u", len);
1396 return -1;
1397 }
1398
1399 if (*upos + len + 2 > uend)
1400 return -2;
1401
1402 os_memcpy(*upos, *spos, len);
1403 *spos += len;
1404 *upos += len;
1405 (*upos)[0] = '.';
1406 (*upos)++;
1407 (*upos)[0] = '\0';
1408 }
1409
1410 return 0;
1411}
1412
1413
1414/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1415 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1416 * not large enough */
1417static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1418 size_t msg_len, size_t offset)
1419{
1420 /* 27-octet in-memory packet from P2P specification */
1421 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1422 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1423 u8 *tmp, *end, *spos;
1424 char *upos, *uend;
1425 int ret = 0;
1426
1427 if (buf_len < 2)
1428 return -1;
1429 if (offset > msg_len)
1430 return -1;
1431
1432 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1433 if (tmp == NULL)
1434 return -1;
1435 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1436 end = spos + msg_len;
1437 spos += offset;
1438
1439 os_memset(tmp, 0, DNS_HEADER_LEN);
1440 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1441 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1442
1443 upos = buf;
1444 uend = buf + buf_len;
1445
1446 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1447 if (ret) {
1448 os_free(tmp);
1449 return ret;
1450 }
1451
1452 if (upos == buf) {
1453 upos[0] = '.';
1454 upos[1] = '\0';
1455 } else if (upos[-1] == '.')
1456 upos[-1] = '\0';
1457
1458 os_free(tmp);
1459 return 0;
1460}
1461
1462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001463static struct p2p_srv_bonjour *
1464wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1465 const struct wpabuf *query)
1466{
1467 struct p2p_srv_bonjour *bsrv;
1468 size_t len;
1469
1470 len = wpabuf_len(query);
1471 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1472 struct p2p_srv_bonjour, list) {
1473 if (len == wpabuf_len(bsrv->query) &&
1474 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1475 len) == 0)
1476 return bsrv;
1477 }
1478 return NULL;
1479}
1480
1481
1482static struct p2p_srv_upnp *
1483wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1484 const char *service)
1485{
1486 struct p2p_srv_upnp *usrv;
1487
1488 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1489 struct p2p_srv_upnp, list) {
1490 if (version == usrv->version &&
1491 os_strcmp(service, usrv->service) == 0)
1492 return usrv;
1493 }
1494 return NULL;
1495}
1496
1497
1498static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1499 u8 srv_trans_id)
1500{
1501 u8 *len_pos;
1502
1503 if (wpabuf_tailroom(resp) < 5)
1504 return;
1505
1506 /* Length (to be filled) */
1507 len_pos = wpabuf_put(resp, 2);
1508 wpabuf_put_u8(resp, srv_proto);
1509 wpabuf_put_u8(resp, srv_trans_id);
1510 /* Status Code */
1511 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1512 /* Response Data: empty */
1513 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1514}
1515
1516
1517static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1518 struct wpabuf *resp, u8 srv_trans_id)
1519{
1520 struct p2p_srv_bonjour *bsrv;
1521 u8 *len_pos;
1522
1523 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1524
1525 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1526 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1527 return;
1528 }
1529
1530 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1531 struct p2p_srv_bonjour, list) {
1532 if (wpabuf_tailroom(resp) <
1533 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1534 return;
1535 /* Length (to be filled) */
1536 len_pos = wpabuf_put(resp, 2);
1537 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1538 wpabuf_put_u8(resp, srv_trans_id);
1539 /* Status Code */
1540 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1541 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1542 wpabuf_head(bsrv->resp),
1543 wpabuf_len(bsrv->resp));
1544 /* Response Data */
1545 wpabuf_put_buf(resp, bsrv->query); /* Key */
1546 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1547 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1548 2);
1549 }
1550}
1551
1552
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001553static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1554 size_t query_len)
1555{
1556 char str_rx[256], str_srv[256];
1557
1558 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1559 return 0; /* Too short to include DNS Type and Version */
1560 if (os_memcmp(query + query_len - 3,
1561 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1562 3) != 0)
1563 return 0; /* Mismatch in DNS Type or Version */
1564 if (query_len == wpabuf_len(bsrv->query) &&
1565 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1566 return 1; /* Binary match */
1567
1568 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1569 0))
1570 return 0; /* Failed to uncompress query */
1571 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1572 wpabuf_head(bsrv->query),
1573 wpabuf_len(bsrv->query) - 3, 0))
1574 return 0; /* Failed to uncompress service */
1575
1576 return os_strcmp(str_rx, str_srv) == 0;
1577}
1578
1579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1581 struct wpabuf *resp, u8 srv_trans_id,
1582 const u8 *query, size_t query_len)
1583{
1584 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001586 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587
1588 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1589 query, query_len);
1590 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1591 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1592 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1593 srv_trans_id);
1594 return;
1595 }
1596
1597 if (query_len == 0) {
1598 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1599 return;
1600 }
1601
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001602 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1603 struct p2p_srv_bonjour, list) {
1604 if (!match_bonjour_query(bsrv, query, query_len))
1605 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001607 if (wpabuf_tailroom(resp) <
1608 5 + query_len + wpabuf_len(bsrv->resp))
1609 return;
1610
1611 matches++;
1612
1613 /* Length (to be filled) */
1614 len_pos = wpabuf_put(resp, 2);
1615 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1616 wpabuf_put_u8(resp, srv_trans_id);
1617
1618 /* Status Code */
1619 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1620 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1621 wpabuf_head(bsrv->resp),
1622 wpabuf_len(bsrv->resp));
1623
1624 /* Response Data */
1625 wpabuf_put_data(resp, query, query_len); /* Key */
1626 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1627
1628 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1629 }
1630
1631 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1633 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001634 if (wpabuf_tailroom(resp) < 5)
1635 return;
1636
1637 /* Length (to be filled) */
1638 len_pos = wpabuf_put(resp, 2);
1639 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1640 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641
1642 /* Status Code */
1643 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1644 /* Response Data: empty */
1645 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1646 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648}
1649
1650
1651static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1652 struct wpabuf *resp, u8 srv_trans_id)
1653{
1654 struct p2p_srv_upnp *usrv;
1655 u8 *len_pos;
1656
1657 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1658
1659 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1660 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1661 return;
1662 }
1663
1664 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1665 struct p2p_srv_upnp, list) {
1666 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1667 return;
1668
1669 /* Length (to be filled) */
1670 len_pos = wpabuf_put(resp, 2);
1671 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1672 wpabuf_put_u8(resp, srv_trans_id);
1673
1674 /* Status Code */
1675 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1676 /* Response Data */
1677 wpabuf_put_u8(resp, usrv->version);
1678 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1679 usrv->service);
1680 wpabuf_put_str(resp, usrv->service);
1681 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1682 2);
1683 }
1684}
1685
1686
1687static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1688 struct wpabuf *resp, u8 srv_trans_id,
1689 const u8 *query, size_t query_len)
1690{
1691 struct p2p_srv_upnp *usrv;
1692 u8 *len_pos;
1693 u8 version;
1694 char *str;
1695 int count = 0;
1696
1697 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1698 query, query_len);
1699
1700 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1701 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1702 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1703 srv_trans_id);
1704 return;
1705 }
1706
1707 if (query_len == 0) {
1708 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1709 return;
1710 }
1711
1712 if (wpabuf_tailroom(resp) < 5)
1713 return;
1714
1715 /* Length (to be filled) */
1716 len_pos = wpabuf_put(resp, 2);
1717 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1718 wpabuf_put_u8(resp, srv_trans_id);
1719
1720 version = query[0];
1721 str = os_malloc(query_len);
1722 if (str == NULL)
1723 return;
1724 os_memcpy(str, query + 1, query_len - 1);
1725 str[query_len - 1] = '\0';
1726
1727 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1728 struct p2p_srv_upnp, list) {
1729 if (version != usrv->version)
1730 continue;
1731
1732 if (os_strcmp(str, "ssdp:all") != 0 &&
1733 os_strstr(usrv->service, str) == NULL)
1734 continue;
1735
1736 if (wpabuf_tailroom(resp) < 2)
1737 break;
1738 if (count == 0) {
1739 /* Status Code */
1740 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1741 /* Response Data */
1742 wpabuf_put_u8(resp, version);
1743 } else
1744 wpabuf_put_u8(resp, ',');
1745
1746 count++;
1747
1748 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1749 usrv->service);
1750 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1751 break;
1752 wpabuf_put_str(resp, usrv->service);
1753 }
1754 os_free(str);
1755
1756 if (count == 0) {
1757 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1758 "available");
1759 /* Status Code */
1760 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1761 /* Response Data: empty */
1762 }
1763
1764 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1765}
1766
1767
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001768#ifdef CONFIG_WIFI_DISPLAY
1769static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1770 struct wpabuf *resp, u8 srv_trans_id,
1771 const u8 *query, size_t query_len)
1772{
1773 const u8 *pos;
1774 u8 role;
1775 u8 *len_pos;
1776
1777 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1778
1779 if (!wpa_s->global->wifi_display) {
1780 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1781 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1782 srv_trans_id);
1783 return;
1784 }
1785
1786 if (query_len < 1) {
1787 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1788 "Role");
1789 return;
1790 }
1791
1792 if (wpabuf_tailroom(resp) < 5)
1793 return;
1794
1795 pos = query;
1796 role = *pos++;
1797 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1798
1799 /* TODO: role specific handling */
1800
1801 /* Length (to be filled) */
1802 len_pos = wpabuf_put(resp, 2);
1803 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1804 wpabuf_put_u8(resp, srv_trans_id);
1805 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1806
1807 while (pos < query + query_len) {
1808 if (*pos < MAX_WFD_SUBELEMS &&
1809 wpa_s->global->wfd_subelem[*pos] &&
1810 wpabuf_tailroom(resp) >=
1811 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1812 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1813 "subelement %u", *pos);
1814 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1815 }
1816 pos++;
1817 }
1818
1819 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1820}
1821#endif /* CONFIG_WIFI_DISPLAY */
1822
1823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1825 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1826{
1827 struct wpa_supplicant *wpa_s = ctx;
1828 const u8 *pos = tlvs;
1829 const u8 *end = tlvs + tlvs_len;
1830 const u8 *tlv_end;
1831 u16 slen;
1832 struct wpabuf *resp;
1833 u8 srv_proto, srv_trans_id;
1834 size_t buf_len;
1835 char *buf;
1836
1837 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1838 tlvs, tlvs_len);
1839 buf_len = 2 * tlvs_len + 1;
1840 buf = os_malloc(buf_len);
1841 if (buf) {
1842 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1843 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1844 MACSTR " %u %u %s",
1845 freq, MAC2STR(sa), dialog_token, update_indic,
1846 buf);
1847 os_free(buf);
1848 }
1849
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850 if (wpa_s->p2p_sd_over_ctrl_iface) {
1851 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1852 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001854 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855
1856 resp = wpabuf_alloc(10000);
1857 if (resp == NULL)
1858 return;
1859
1860 while (pos + 1 < end) {
1861 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1862 slen = WPA_GET_LE16(pos);
1863 pos += 2;
1864 if (pos + slen > end || slen < 2) {
1865 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1866 "length");
1867 wpabuf_free(resp);
1868 return;
1869 }
1870 tlv_end = pos + slen;
1871
1872 srv_proto = *pos++;
1873 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1874 srv_proto);
1875 srv_trans_id = *pos++;
1876 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1877 srv_trans_id);
1878
1879 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1880 pos, tlv_end - pos);
1881
1882
1883 if (wpa_s->force_long_sd) {
1884 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1885 "response");
1886 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1887 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1888 goto done;
1889 }
1890
1891 switch (srv_proto) {
1892 case P2P_SERV_ALL_SERVICES:
1893 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1894 "for all services");
1895 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1896 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1897 wpa_printf(MSG_DEBUG, "P2P: No service "
1898 "discovery protocols available");
1899 wpas_sd_add_proto_not_avail(
1900 resp, P2P_SERV_ALL_SERVICES,
1901 srv_trans_id);
1902 break;
1903 }
1904 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1905 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1906 break;
1907 case P2P_SERV_BONJOUR:
1908 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1909 pos, tlv_end - pos);
1910 break;
1911 case P2P_SERV_UPNP:
1912 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1913 pos, tlv_end - pos);
1914 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001915#ifdef CONFIG_WIFI_DISPLAY
1916 case P2P_SERV_WIFI_DISPLAY:
1917 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1918 pos, tlv_end - pos);
1919 break;
1920#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 default:
1922 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1923 "protocol %u", srv_proto);
1924 wpas_sd_add_proto_not_avail(resp, srv_proto,
1925 srv_trans_id);
1926 break;
1927 }
1928
1929 pos = tlv_end;
1930 }
1931
1932done:
1933 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1934 update_indic, tlvs, tlvs_len);
1935
1936 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1937
1938 wpabuf_free(resp);
1939}
1940
1941
1942void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1943 const u8 *tlvs, size_t tlvs_len)
1944{
1945 struct wpa_supplicant *wpa_s = ctx;
1946 const u8 *pos = tlvs;
1947 const u8 *end = tlvs + tlvs_len;
1948 const u8 *tlv_end;
1949 u16 slen;
1950 size_t buf_len;
1951 char *buf;
1952
1953 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1954 tlvs, tlvs_len);
1955 if (tlvs_len > 1500) {
1956 /* TODO: better way for handling this */
1957 wpa_msg_ctrl(wpa_s, MSG_INFO,
1958 P2P_EVENT_SERV_DISC_RESP MACSTR
1959 " %u <long response: %u bytes>",
1960 MAC2STR(sa), update_indic,
1961 (unsigned int) tlvs_len);
1962 } else {
1963 buf_len = 2 * tlvs_len + 1;
1964 buf = os_malloc(buf_len);
1965 if (buf) {
1966 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1967 wpa_msg_ctrl(wpa_s, MSG_INFO,
1968 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1969 MAC2STR(sa), update_indic, buf);
1970 os_free(buf);
1971 }
1972 }
1973
1974 while (pos < end) {
1975 u8 srv_proto, srv_trans_id, status;
1976
1977 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1978 slen = WPA_GET_LE16(pos);
1979 pos += 2;
1980 if (pos + slen > end || slen < 3) {
1981 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1982 "length");
1983 return;
1984 }
1985 tlv_end = pos + slen;
1986
1987 srv_proto = *pos++;
1988 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1989 srv_proto);
1990 srv_trans_id = *pos++;
1991 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1992 srv_trans_id);
1993 status = *pos++;
1994 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1995 status);
1996
1997 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1998 pos, tlv_end - pos);
1999
2000 pos = tlv_end;
2001 }
2002
2003 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2004}
2005
2006
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002007u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2008 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009{
2010 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002011 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002013 return 0;
2014 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015}
2016
2017
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002018u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2019 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020{
2021 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002022 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023
2024 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2025 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002026 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2028 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2029 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2030 wpabuf_put_u8(tlvs, version);
2031 wpabuf_put_str(tlvs, query);
2032 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2033 wpabuf_free(tlvs);
2034 return ret;
2035}
2036
2037
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002038#ifdef CONFIG_WIFI_DISPLAY
2039
2040static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2041 const struct wpabuf *tlvs)
2042{
2043 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2044 return 0;
2045 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2046 return 0;
2047 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2048}
2049
2050
2051#define MAX_WFD_SD_SUBELEMS 20
2052
2053static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2054 const char *subelems)
2055{
2056 u8 *len;
2057 const char *pos;
2058 int val;
2059 int count = 0;
2060
2061 len = wpabuf_put(tlvs, 2);
2062 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2063 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2064
2065 wpabuf_put_u8(tlvs, role);
2066
2067 pos = subelems;
2068 while (*pos) {
2069 val = atoi(pos);
2070 if (val >= 0 && val < 256) {
2071 wpabuf_put_u8(tlvs, val);
2072 count++;
2073 if (count == MAX_WFD_SD_SUBELEMS)
2074 break;
2075 }
2076 pos = os_strchr(pos + 1, ',');
2077 if (pos == NULL)
2078 break;
2079 pos++;
2080 }
2081
2082 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2083}
2084
2085
2086u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2087 const u8 *dst, const char *role)
2088{
2089 struct wpabuf *tlvs;
2090 u64 ret;
2091 const char *subelems;
2092 u8 id = 1;
2093
2094 subelems = os_strchr(role, ' ');
2095 if (subelems == NULL)
2096 return 0;
2097 subelems++;
2098
2099 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2100 if (tlvs == NULL)
2101 return 0;
2102
2103 if (os_strstr(role, "[source]"))
2104 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2105 if (os_strstr(role, "[pri-sink]"))
2106 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2107 if (os_strstr(role, "[sec-sink]"))
2108 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2109 if (os_strstr(role, "[source+sink]"))
2110 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2111
2112 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2113 wpabuf_free(tlvs);
2114 return ret;
2115}
2116
2117#endif /* CONFIG_WIFI_DISPLAY */
2118
2119
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002120int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121{
2122 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002123 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2125 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002126 return p2p_sd_cancel_request(wpa_s->global->p2p,
2127 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128}
2129
2130
2131void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2132 const u8 *dst, u8 dialog_token,
2133 const struct wpabuf *resp_tlvs)
2134{
2135 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2136 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2137 resp_tlvs);
2138 return;
2139 }
2140 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2141 return;
2142 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2143 resp_tlvs);
2144}
2145
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002146#ifdef ANDROID_P2P
2147void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2148#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002150#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151{
2152 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2153 wpa_drv_p2p_service_update(wpa_s);
2154 return;
2155 }
2156 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002157#ifdef ANDROID_P2P
2158 p2p_sd_service_update(wpa_s->global->p2p, action);
2159#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002161#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162}
2163
2164
2165static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2166{
2167 dl_list_del(&bsrv->list);
2168 wpabuf_free(bsrv->query);
2169 wpabuf_free(bsrv->resp);
2170 os_free(bsrv);
2171}
2172
2173
2174static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2175{
2176 dl_list_del(&usrv->list);
2177 os_free(usrv->service);
2178 os_free(usrv);
2179}
2180
2181
2182void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2183{
2184 struct p2p_srv_bonjour *bsrv, *bn;
2185 struct p2p_srv_upnp *usrv, *un;
2186
2187 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2188 struct p2p_srv_bonjour, list)
2189 wpas_p2p_srv_bonjour_free(bsrv);
2190
2191 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2192 struct p2p_srv_upnp, list)
2193 wpas_p2p_srv_upnp_free(usrv);
2194
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002195#ifdef ANDROID_P2P
2196 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2197#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002198 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002199#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200}
2201
2202
2203int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2204 struct wpabuf *query, struct wpabuf *resp)
2205{
2206 struct p2p_srv_bonjour *bsrv;
2207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208 bsrv = os_zalloc(sizeof(*bsrv));
2209 if (bsrv == NULL)
2210 return -1;
2211 bsrv->query = query;
2212 bsrv->resp = resp;
2213 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2214
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002215#ifdef ANDROID_P2P
2216 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2217#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002218 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002219#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220 return 0;
2221}
2222
2223
2224int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2225 const struct wpabuf *query)
2226{
2227 struct p2p_srv_bonjour *bsrv;
2228
2229 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2230 if (bsrv == NULL)
2231 return -1;
2232 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002233#ifdef ANDROID_P2P
2234 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2235#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002237#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 return 0;
2239}
2240
2241
2242int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2243 const char *service)
2244{
2245 struct p2p_srv_upnp *usrv;
2246
2247 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2248 return 0; /* Already listed */
2249 usrv = os_zalloc(sizeof(*usrv));
2250 if (usrv == NULL)
2251 return -1;
2252 usrv->version = version;
2253 usrv->service = os_strdup(service);
2254 if (usrv->service == NULL) {
2255 os_free(usrv);
2256 return -1;
2257 }
2258 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2259
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002260#ifdef ANDROID_P2P
2261 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2262#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002264#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002265 return 0;
2266}
2267
2268
2269int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2270 const char *service)
2271{
2272 struct p2p_srv_upnp *usrv;
2273
2274 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2275 if (usrv == NULL)
2276 return -1;
2277 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002278#ifdef ANDROID_P2P
2279 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2280#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002282#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 return 0;
2284}
2285
2286
2287static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2288 const u8 *peer, const char *params,
2289 unsigned int generated_pin)
2290{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002291 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2292 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293}
2294
2295
2296static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2297 const u8 *peer, const char *params)
2298{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002299 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2300 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002301}
2302
2303
2304void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2305 const u8 *dev_addr, const u8 *pri_dev_type,
2306 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002307 u8 dev_capab, u8 group_capab, const u8 *group_id,
2308 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002309{
2310 struct wpa_supplicant *wpa_s = ctx;
2311 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002312 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 u8 empty_dev_type[8];
2314 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002315 struct wpa_supplicant *group = NULL;
2316
2317 if (group_id) {
2318 for (group = wpa_s->global->ifaces; group; group = group->next)
2319 {
2320 struct wpa_ssid *s = group->current_ssid;
2321 if (s != NULL &&
2322 s->mode == WPAS_MODE_P2P_GO &&
2323 group_id_len - ETH_ALEN == s->ssid_len &&
2324 os_memcmp(group_id + ETH_ALEN, s->ssid,
2325 s->ssid_len) == 0)
2326 break;
2327 }
2328 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329
2330 if (pri_dev_type == NULL) {
2331 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2332 pri_dev_type = empty_dev_type;
2333 }
2334 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2335 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002336 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337 MAC2STR(dev_addr),
2338 wps_dev_type_bin2str(pri_dev_type, devtype,
2339 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002340 dev_name, supp_config_methods, dev_capab, group_capab,
2341 group ? " group=" : "",
2342 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345 if (config_methods & WPS_CONFIG_DISPLAY) {
2346 generated_pin = wps_generate_pin();
2347 wpas_prov_disc_local_display(wpa_s, peer, params,
2348 generated_pin);
2349 } else if (config_methods & WPS_CONFIG_KEYPAD)
2350 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2351 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002352 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2353 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002354
2355 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2356 P2P_PROV_DISC_SUCCESS,
2357 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358}
2359
2360
2361void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2362{
2363 struct wpa_supplicant *wpa_s = ctx;
2364 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002365 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002367 if (wpa_s->pending_pd_before_join &&
2368 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2369 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2370 wpa_s->pending_pd_before_join = 0;
2371 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2372 "join-existing-group operation");
2373 wpas_p2p_join_start(wpa_s);
2374 return;
2375 }
2376
Dmitry Shmidt04949592012-07-19 12:16:46 -07002377 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2378 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2379 os_snprintf(params, sizeof(params), " peer_go=%d",
2380 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2381 else
2382 params[0] = '\0';
2383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002385 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002386 else if (config_methods & WPS_CONFIG_KEYPAD) {
2387 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002388 wpas_prov_disc_local_display(wpa_s, peer, params,
2389 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002390 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002391 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2392 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393
Jouni Malinen75ecf522011-06-27 15:19:46 -07002394 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2395 P2P_PROV_DISC_SUCCESS,
2396 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397}
2398
2399
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002400static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2401 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002402{
2403 struct wpa_supplicant *wpa_s = ctx;
2404
Dmitry Shmidt04949592012-07-19 12:16:46 -07002405 if (wpa_s->p2p_fallback_to_go_neg) {
2406 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2407 "failed - fall back to GO Negotiation");
2408 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2409 return;
2410 }
2411
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002412 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002413 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002414 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2415 "join-existing-group operation (no ACK for PD "
2416 "Req attempts)");
2417 wpas_p2p_join_start(wpa_s);
2418 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002419 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002420
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002421 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2422 " p2p_dev_addr=" MACSTR " status=%d",
2423 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002424
Jouni Malinen75ecf522011-06-27 15:19:46 -07002425 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2426 status, 0, 0);
2427}
2428
2429
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002430static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2431{
2432 if (channels == NULL)
2433 return 1; /* Assume no restrictions */
2434 return p2p_channels_includes_freq(channels, freq);
2435
2436}
2437
2438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2440 const u8 *go_dev_addr, const u8 *ssid,
2441 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002442 int *force_freq, int persistent_group,
2443 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444{
2445 struct wpa_supplicant *wpa_s = ctx;
2446 struct wpa_ssid *s;
2447 u8 cur_bssid[ETH_ALEN];
2448 int res;
2449 struct wpa_supplicant *grp;
2450
2451 if (!persistent_group) {
2452 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2453 " to join an active group", MAC2STR(sa));
2454 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2455 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2456 == 0 ||
2457 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2458 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2459 "authorized invitation");
2460 goto accept_inv;
2461 }
2462 /*
2463 * Do not accept the invitation automatically; notify user and
2464 * request approval.
2465 */
2466 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2467 }
2468
2469 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2470 if (grp) {
2471 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2472 "running persistent group");
2473 if (*go)
2474 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2475 goto accept_inv;
2476 }
2477
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002478 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2479 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2480 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2481 "invitation to re-invoke a persistent group");
2482 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002483 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2484
2485 for (s = wpa_s->conf->ssid; s; s = s->next) {
2486 if (s->disabled == 2 &&
2487 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2488 s->ssid_len == ssid_len &&
2489 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2490 break;
2491 }
2492
2493 if (!s) {
2494 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2495 " requested reinvocation of an unknown group",
2496 MAC2STR(sa));
2497 return P2P_SC_FAIL_UNKNOWN_GROUP;
2498 }
2499
2500 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2501 *go = 1;
2502 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2503 wpa_printf(MSG_DEBUG, "P2P: The only available "
2504 "interface is already in use - reject "
2505 "invitation");
2506 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2507 }
2508 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2509 } else if (s->mode == WPAS_MODE_P2P_GO) {
2510 *go = 1;
2511 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2512 {
2513 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2514 "interface address for the group");
2515 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2516 }
2517 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2518 ETH_ALEN);
2519 }
2520
2521accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002522 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2525 wpa_s->assoc_freq) {
2526 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2527 "the channel we are already using");
2528 *force_freq = wpa_s->assoc_freq;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002529 wpas_p2p_set_own_freq_preference(wpa_s, wpa_s->assoc_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 }
2531
2532 res = wpa_drv_shared_freq(wpa_s);
2533 if (res > 0) {
2534 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2535 "with the channel we are already using on a "
2536 "shared interface");
2537 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002538 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 }
2540
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002541 if (*force_freq > 0 &&
2542 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2543 if (*go == 0) {
2544 /* We are the client */
2545 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2546 "running a GO but we are capable of MCC, "
2547 "figure out the best channel to use");
2548 *force_freq = 0;
2549 } else if (!freq_included(channels, *force_freq)) {
2550 /* We are the GO, and *force_freq is not in the
2551 * intersection */
2552 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2553 "in intersection but we are capable of MCC, "
2554 "figure out the best channel to use",
2555 *force_freq);
2556 *force_freq = 0;
2557 }
2558 }
2559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002560 return P2P_SC_SUCCESS;
2561}
2562
2563
2564static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2565 const u8 *ssid, size_t ssid_len,
2566 const u8 *go_dev_addr, u8 status,
2567 int op_freq)
2568{
2569 struct wpa_supplicant *wpa_s = ctx;
2570 struct wpa_ssid *s;
2571
2572 for (s = wpa_s->conf->ssid; s; s = s->next) {
2573 if (s->disabled == 2 &&
2574 s->ssid_len == ssid_len &&
2575 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2576 break;
2577 }
2578
2579 if (status == P2P_SC_SUCCESS) {
2580 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2581 " was accepted; op_freq=%d MHz",
2582 MAC2STR(sa), op_freq);
2583 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002584 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002585 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002586 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002588 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002590 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002591 }
2592 return;
2593 }
2594
2595 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2596 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2597 " was rejected (status %u)", MAC2STR(sa), status);
2598 return;
2599 }
2600
2601 if (!s) {
2602 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002603 wpa_msg_global(wpa_s, MSG_INFO,
2604 P2P_EVENT_INVITATION_RECEIVED
2605 "sa=" MACSTR " go_dev_addr=" MACSTR
2606 " bssid=" MACSTR " unknown-network",
2607 MAC2STR(sa), MAC2STR(go_dev_addr),
2608 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002610 wpa_msg_global(wpa_s, MSG_INFO,
2611 P2P_EVENT_INVITATION_RECEIVED
2612 "sa=" MACSTR " go_dev_addr=" MACSTR
2613 " unknown-network",
2614 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002615 }
2616 return;
2617 }
2618
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002619 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002620 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2621 "sa=" MACSTR " persistent=%d freq=%d",
2622 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002623 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002624 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2625 "sa=" MACSTR " persistent=%d",
2626 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628}
2629
2630
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002631static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2632 struct wpa_ssid *ssid,
2633 const u8 *peer)
2634{
2635 size_t i;
2636
2637 if (ssid == NULL)
2638 return;
2639
2640 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2641 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2642 ETH_ALEN) == 0)
2643 break;
2644 }
2645 if (i >= ssid->num_p2p_clients) {
2646 if (ssid->mode != WPAS_MODE_P2P_GO &&
2647 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2648 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2649 "due to invitation result", ssid->id);
2650 wpas_notify_network_removed(wpa_s, ssid);
2651 wpa_config_remove_network(wpa_s->conf, ssid->id);
2652 return;
2653 }
2654 return; /* Peer not found in client list */
2655 }
2656
2657 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2658 "group %d client list due to invitation result",
2659 MAC2STR(peer), ssid->id);
2660 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2661 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2662 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2663 ssid->num_p2p_clients--;
2664#ifndef CONFIG_NO_CONFIG_WRITE
2665 if (wpa_s->parent->conf->update_config &&
2666 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2667 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2668#endif /* CONFIG_NO_CONFIG_WRITE */
2669}
2670
2671
2672static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2673 const u8 *peer)
2674{
2675 struct wpa_ssid *ssid;
2676
2677 wpa_s = wpa_s->global->p2p_invite_group;
2678 if (wpa_s == NULL)
2679 return; /* No known invitation group */
2680 ssid = wpa_s->current_ssid;
2681 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2682 !ssid->p2p_persistent_group)
2683 return; /* Not operating as a GO in persistent group */
2684 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2685 ssid->ssid, ssid->ssid_len);
2686 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2687}
2688
2689
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002690static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002691 const struct p2p_channels *channels,
2692 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002693{
2694 struct wpa_supplicant *wpa_s = ctx;
2695 struct wpa_ssid *ssid;
2696
2697 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002698 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2699 "status=%d " MACSTR,
2700 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002702 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2703 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704 }
2705 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2706
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002707 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2708 status, MAC2STR(peer));
2709 if (wpa_s->pending_invite_ssid_id == -1) {
2710 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2711 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002713 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002714
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002715 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2716 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2717 "invitation exchange to indicate readiness for "
2718 "re-invocation");
2719 }
2720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002721 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002722 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2723 ssid = wpa_config_get_network(
2724 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2725 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2726 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727 wpas_p2p_remove_pending_group_interface(wpa_s);
2728 return;
2729 }
2730
2731 ssid = wpa_config_get_network(wpa_s->conf,
2732 wpa_s->pending_invite_ssid_id);
2733 if (ssid == NULL) {
2734 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2735 "data matching with invitation");
2736 return;
2737 }
2738
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002739 /*
2740 * The peer could have missed our ctrl::ack frame for Invitation
2741 * Response and continue retransmitting the frame. To reduce the
2742 * likelihood of the peer not getting successful TX status for the
2743 * Invitation Response frame, wait a short time here before starting
2744 * the persistent group so that we will remain on the current channel to
2745 * acknowledge any possible retransmission from the peer.
2746 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002747#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002748 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2749 "starting persistent group");
2750 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002751#else
2752 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2753 "starting persistent group");
2754 os_sleep(0, 100000);
2755#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002757 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002758 ssid->mode == WPAS_MODE_P2P_GO,
2759 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002760 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761}
2762
2763
Dmitry Shmidt04949592012-07-19 12:16:46 -07002764static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2765 unsigned int freq)
2766{
2767 unsigned int i;
2768
2769 if (global->p2p_disallow_freq == NULL)
2770 return 0;
2771
2772 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2773 if (freq >= global->p2p_disallow_freq[i].min &&
2774 freq <= global->p2p_disallow_freq[i].max)
2775 return 1;
2776 }
2777
2778 return 0;
2779}
2780
2781
2782static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2783{
2784 reg->channel[reg->channels] = chan;
2785 reg->channels++;
2786}
2787
2788
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2790 struct p2p_channels *chan)
2791{
2792 int i, cla = 0;
2793
2794 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2795 "band");
2796
2797 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2798 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002799 chan->reg_class[cla].channels = 0;
2800 for (i = 0; i < 11; i++) {
2801 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2802 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2803 }
2804 if (chan->reg_class[cla].channels)
2805 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806
2807 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2808 "band");
2809
2810 /* Operating class 115 - 5 GHz, channels 36-48 */
2811 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002812 chan->reg_class[cla].channels = 0;
2813 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2814 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2815 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2816 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2817 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2818 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2819 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2820 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2821 if (chan->reg_class[cla].channels)
2822 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823
2824 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2825 "band");
2826
2827 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2828 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002829 chan->reg_class[cla].channels = 0;
2830 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2831 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2832 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2833 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2834 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2835 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2836 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2837 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2838 if (chan->reg_class[cla].channels)
2839 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002840
2841 chan->reg_classes = cla;
2842 return 0;
2843}
2844
2845
2846static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2847 u16 num_modes,
2848 enum hostapd_hw_mode mode)
2849{
2850 u16 i;
2851
2852 for (i = 0; i < num_modes; i++) {
2853 if (modes[i].mode == mode)
2854 return &modes[i];
2855 }
2856
2857 return NULL;
2858}
2859
2860
Dmitry Shmidt04949592012-07-19 12:16:46 -07002861static int has_channel(struct wpa_global *global,
2862 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863{
2864 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002865 unsigned int freq;
2866
2867 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2868 chan * 5;
2869 if (wpas_p2p_disallowed_freq(global, freq))
2870 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871
2872 for (i = 0; i < mode->num_channels; i++) {
2873 if (mode->channels[i].chan == chan) {
2874 if (flags)
2875 *flags = mode->channels[i].flag;
2876 return !(mode->channels[i].flag &
2877 (HOSTAPD_CHAN_DISABLED |
2878 HOSTAPD_CHAN_PASSIVE_SCAN |
2879 HOSTAPD_CHAN_NO_IBSS |
2880 HOSTAPD_CHAN_RADAR));
2881 }
2882 }
2883
2884 return 0;
2885}
2886
2887
2888struct p2p_oper_class_map {
2889 enum hostapd_hw_mode mode;
2890 u8 op_class;
2891 u8 min_chan;
2892 u8 max_chan;
2893 u8 inc;
2894 enum { BW20, BW40PLUS, BW40MINUS } bw;
2895};
2896
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002897static struct p2p_oper_class_map op_class[] = {
2898 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002899#if 0 /* Do not enable HT40 on 2 GHz for now */
2900 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2901 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2902#endif
2903 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2904 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2905 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2906 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2907 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2908 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2909 { -1, 0, 0, 0, 0, BW20 }
2910};
2911
2912
2913static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2914 struct hostapd_hw_modes *mode,
2915 u8 channel, u8 bw)
2916{
2917 int flag;
2918
2919 if (!has_channel(wpa_s->global, mode, channel, &flag))
2920 return -1;
2921 if (bw == BW40MINUS &&
2922 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2923 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2924 return 0;
2925 if (bw == BW40PLUS &&
2926 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2927 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2928 return 0;
2929 return 1;
2930}
2931
2932
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002933static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2934 struct p2p_channels *chan)
2935{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002936 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002937 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002939 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2941 "of all supported channels; assume dualband "
2942 "support");
2943 return wpas_p2p_default_channels(wpa_s, chan);
2944 }
2945
2946 cla = 0;
2947
2948 for (op = 0; op_class[op].op_class; op++) {
2949 struct p2p_oper_class_map *o = &op_class[op];
2950 u8 ch;
2951 struct p2p_reg_class *reg = NULL;
2952
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002953 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002954 if (mode == NULL)
2955 continue;
2956 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002957 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958 continue;
2959 if (reg == NULL) {
2960 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2961 "class %u", o->op_class);
2962 reg = &chan->reg_class[cla];
2963 cla++;
2964 reg->reg_class = o->op_class;
2965 }
2966 reg->channel[reg->channels] = ch;
2967 reg->channels++;
2968 }
2969 if (reg) {
2970 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2971 reg->channel, reg->channels);
2972 }
2973 }
2974
2975 chan->reg_classes = cla;
2976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977 return 0;
2978}
2979
2980
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002981int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2982 struct hostapd_hw_modes *mode, u8 channel)
2983{
2984 int op, ret;
2985
2986 for (op = 0; op_class[op].op_class; op++) {
2987 struct p2p_oper_class_map *o = &op_class[op];
2988 u8 ch;
2989
2990 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2991 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2992 o->bw == BW20 || ch != channel)
2993 continue;
2994 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2995 if (ret < 0)
2996 continue;
2997 else if (ret > 0)
2998 return (o->bw == BW40MINUS) ? -1 : 1;
2999 else
3000 return 0;
3001 }
3002 }
3003 return 0;
3004}
3005
3006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3008 size_t buf_len)
3009{
3010 struct wpa_supplicant *wpa_s = ctx;
3011
3012 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3013 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3014 break;
3015 }
3016 if (wpa_s == NULL)
3017 return -1;
3018
3019 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3020}
3021
3022
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003023static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3024{
3025 struct wpa_supplicant *wpa_s = ctx;
3026
3027 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3028 struct wpa_ssid *ssid = wpa_s->current_ssid;
3029 if (ssid == NULL)
3030 continue;
3031 if (ssid->mode != WPAS_MODE_INFRA)
3032 continue;
3033 if (wpa_s->wpa_state != WPA_COMPLETED &&
3034 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3035 continue;
3036 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3037 return 1;
3038 }
3039
3040 return 0;
3041}
3042
3043
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003044static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3045{
3046 struct wpa_supplicant *wpa_s = ctx;
3047 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3048}
3049
3050
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051/**
3052 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3053 * @global: Pointer to global data from wpa_supplicant_init()
3054 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3055 * Returns: 0 on success, -1 on failure
3056 */
3057int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3058{
3059 struct p2p_config p2p;
3060 unsigned int r;
3061 int i;
3062
3063 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3064 return 0;
3065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003066 if (global->p2p)
3067 return 0;
3068
3069 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3070 struct p2p_params params;
3071
3072 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3073 os_memset(&params, 0, sizeof(params));
3074 params.dev_name = wpa_s->conf->device_name;
3075 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3076 WPS_DEV_TYPE_LEN);
3077 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3078 os_memcpy(params.sec_dev_type,
3079 wpa_s->conf->sec_device_type,
3080 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3081
3082 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3083 return -1;
3084
3085 return 0;
3086 }
3087
3088 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003089 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003090 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091 p2p.p2p_scan = wpas_p2p_scan;
3092 p2p.send_action = wpas_send_action;
3093 p2p.send_action_done = wpas_send_action_done;
3094 p2p.go_neg_completed = wpas_go_neg_completed;
3095 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3096 p2p.dev_found = wpas_dev_found;
3097 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003098 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 p2p.start_listen = wpas_start_listen;
3100 p2p.stop_listen = wpas_stop_listen;
3101 p2p.send_probe_resp = wpas_send_probe_resp;
3102 p2p.sd_request = wpas_sd_request;
3103 p2p.sd_response = wpas_sd_response;
3104 p2p.prov_disc_req = wpas_prov_disc_req;
3105 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003106 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003107 p2p.invitation_process = wpas_invitation_process;
3108 p2p.invitation_received = wpas_invitation_received;
3109 p2p.invitation_result = wpas_invitation_result;
3110 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003111 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112
3113 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003114 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115 p2p.dev_name = wpa_s->conf->device_name;
3116 p2p.manufacturer = wpa_s->conf->manufacturer;
3117 p2p.model_name = wpa_s->conf->model_name;
3118 p2p.model_number = wpa_s->conf->model_number;
3119 p2p.serial_number = wpa_s->conf->serial_number;
3120 if (wpa_s->wps) {
3121 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3122 p2p.config_methods = wpa_s->wps->config_methods;
3123 }
3124
3125 if (wpa_s->conf->p2p_listen_reg_class &&
3126 wpa_s->conf->p2p_listen_channel) {
3127 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3128 p2p.channel = wpa_s->conf->p2p_listen_channel;
3129 } else {
3130 p2p.reg_class = 81;
3131 /*
3132 * Pick one of the social channels randomly as the listen
3133 * channel.
3134 */
3135 os_get_random((u8 *) &r, sizeof(r));
3136 p2p.channel = 1 + (r % 3) * 5;
3137 }
3138 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3139
3140 if (wpa_s->conf->p2p_oper_reg_class &&
3141 wpa_s->conf->p2p_oper_channel) {
3142 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3143 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3144 p2p.cfg_op_channel = 1;
3145 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3146 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3147
3148 } else {
3149 p2p.op_reg_class = 81;
3150 /*
3151 * Use random operation channel from (1, 6, 11) if no other
3152 * preference is indicated.
3153 */
3154 os_get_random((u8 *) &r, sizeof(r));
3155 p2p.op_channel = 1 + (r % 3) * 5;
3156 p2p.cfg_op_channel = 0;
3157 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3158 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3159 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003160
3161 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3162 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3163 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3164 }
3165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3167 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3168 p2p.country[2] = 0x04;
3169 } else
3170 os_memcpy(p2p.country, "XX\x04", 3);
3171
3172 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3173 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3174 "channel list");
3175 return -1;
3176 }
3177
3178 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3179 WPS_DEV_TYPE_LEN);
3180
3181 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3182 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3183 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3184
3185 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3186 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3187
3188 p2p.max_peers = 100;
3189
3190 if (wpa_s->conf->p2p_ssid_postfix) {
3191 p2p.ssid_postfix_len =
3192 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3193 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3194 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3195 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3196 p2p.ssid_postfix_len);
3197 }
3198
3199 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3200
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003201 p2p.max_listen = wpa_s->max_remain_on_chan;
3202
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003203#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003204 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003205 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003206 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3207 } else {
3208 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003209 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003210 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3211 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003212#endif
3213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 global->p2p = p2p_init(&p2p);
3215 if (global->p2p == NULL)
3216 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003217 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003218
3219 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3220 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3221 continue;
3222 p2p_add_wps_vendor_extension(
3223 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3224 }
3225
3226 return 0;
3227}
3228
3229
3230/**
3231 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3232 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3233 *
3234 * This function deinitialize per-interface P2P data.
3235 */
3236void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3237{
3238 if (wpa_s->driver && wpa_s->drv_priv)
3239 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003240
3241 if (wpa_s->go_params) {
3242 /* Clear any stored provisioning info */
3243 p2p_clear_provisioning_info(
3244 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003245 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003246 }
3247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248 os_free(wpa_s->go_params);
3249 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003250#ifdef ANDROID_P2P
3251 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3252#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3254 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3255 wpa_s->p2p_long_listen = 0;
3256 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3257 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3258 wpas_p2p_remove_pending_group_interface(wpa_s);
3259
3260 /* TODO: remove group interface from the driver if this wpa_s instance
3261 * is on top of a P2P group interface */
3262}
3263
3264
3265/**
3266 * wpas_p2p_deinit_global - Deinitialize global P2P module
3267 * @global: Pointer to global data from wpa_supplicant_init()
3268 *
3269 * This function deinitializes the global (per device) P2P module.
3270 */
3271void wpas_p2p_deinit_global(struct wpa_global *global)
3272{
3273 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003274
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003275 wpa_s = global->ifaces;
3276 if (wpa_s)
3277 wpas_p2p_service_flush(wpa_s);
3278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279 if (global->p2p == NULL)
3280 return;
3281
3282 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3284 wpa_s = wpa_s->next;
3285 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286 tmp = global->ifaces;
3287 while (tmp &&
3288 (tmp == wpa_s ||
3289 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3290 tmp = tmp->next;
3291 }
3292 if (tmp == NULL)
3293 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003294 /* Disconnect from the P2P group and deinit the interface */
3295 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296 }
3297
3298 /*
3299 * Deinit GO data on any possibly remaining interface (if main
3300 * interface is used as GO).
3301 */
3302 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3303 if (wpa_s->ap_iface)
3304 wpas_p2p_group_deinit(wpa_s);
3305 }
3306
3307 p2p_deinit(global->p2p);
3308 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003309 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310}
3311
3312
3313static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3314{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003315 if (wpa_s->conf->p2p_no_group_iface)
3316 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003317 if (wpa_s->drv_flags &
3318 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3319 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3320 return 1; /* P2P group requires a new interface in every case
3321 */
3322 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3323 return 0; /* driver does not support concurrent operations */
3324 if (wpa_s->global->ifaces->next)
3325 return 1; /* more that one interface already in use */
3326 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3327 return 1; /* this interface is already in use */
3328 return 0;
3329}
3330
3331
3332static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3333 const u8 *peer_addr,
3334 enum p2p_wps_method wps_method,
3335 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003336 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003337 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003339 if (persistent_group && wpa_s->conf->persistent_reconnect)
3340 persistent_group = 2;
3341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3343 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3344 go_intent, own_interface_addr,
3345 force_freq, persistent_group);
3346 }
3347
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003348 /*
3349 * Increase GO config timeout if HT40 is used since it takes some time
3350 * to scan channels for coex purposes before the BSS can be started.
3351 */
3352 p2p_set_config_timeout(wpa_s->global->p2p,
3353 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003355 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3356 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003357 persistent_group, ssid ? ssid->ssid : NULL,
3358 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003359 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003360}
3361
3362
3363static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3364 const u8 *peer_addr,
3365 enum p2p_wps_method wps_method,
3366 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003367 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003368 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003370 if (persistent_group && wpa_s->conf->persistent_reconnect)
3371 persistent_group = 2;
3372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3374 return -1;
3375
3376 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3377 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003378 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003379 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003380}
3381
3382
3383static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3384{
3385 wpa_s->p2p_join_scan_count++;
3386 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3387 wpa_s->p2p_join_scan_count);
3388 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3389 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3390 " for join operationg - stop join attempt",
3391 MAC2STR(wpa_s->pending_join_iface_addr));
3392 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003393 if (wpa_s->p2p_auto_pd) {
3394 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003395 wpa_msg_global(wpa_s, MSG_INFO,
3396 P2P_EVENT_PROV_DISC_FAILURE
3397 " p2p_dev_addr=" MACSTR " status=N/A",
3398 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003399 return;
3400 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003401 wpa_msg_global(wpa_s->parent, MSG_INFO,
3402 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003403 }
3404}
3405
3406
Dmitry Shmidt04949592012-07-19 12:16:46 -07003407static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3408{
3409 struct wpa_supplicant *iface;
3410 int shared_freq;
3411 u8 bssid[ETH_ALEN];
3412
3413 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3414 return 0;
3415
3416 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3417 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3418 continue;
3419 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3420 continue;
3421 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3422 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3423 shared_freq = iface->current_ssid->frequency;
3424 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3425 shared_freq = iface->assoc_freq;
3426 else
3427 shared_freq = 0;
3428
3429 if (shared_freq && freq != shared_freq) {
3430 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3431 "connected on %d MHz - new connection on "
3432 "%d MHz", iface->ifname, shared_freq, freq);
3433 return 1;
3434 }
3435 }
3436
3437 shared_freq = wpa_drv_shared_freq(wpa_s);
3438 if (shared_freq > 0 && shared_freq != freq) {
3439 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3440 "virtual interface connected on %d MHz - new "
3441 "connection on %d MHz", shared_freq, freq);
3442 return 1;
3443 }
3444
3445 return 0;
3446}
3447
3448
3449static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3450 const u8 *peer_dev_addr)
3451{
3452 struct wpa_bss *bss;
3453 int updated;
3454
3455 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3456 if (bss == NULL)
3457 return -1;
3458 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3459 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3460 "last scan");
3461 return 0;
3462 }
3463
3464 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3465 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3466 "%ld.%06ld (%supdated in last scan)",
3467 bss->last_update.sec, bss->last_update.usec,
3468 updated ? "": "not ");
3469
3470 return updated;
3471}
3472
3473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003474static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3475 struct wpa_scan_results *scan_res)
3476{
3477 struct wpa_bss *bss;
3478 int freq;
3479 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003480
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003481 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3482
3483 if (wpa_s->global->p2p_disabled)
3484 return;
3485
Dmitry Shmidt04949592012-07-19 12:16:46 -07003486 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3487 scan_res ? (int) scan_res->num : -1,
3488 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003489
3490 if (scan_res)
3491 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3492
Dmitry Shmidt04949592012-07-19 12:16:46 -07003493 if (wpa_s->p2p_auto_pd) {
3494 int join = wpas_p2p_peer_go(wpa_s,
3495 wpa_s->pending_join_dev_addr);
3496 if (join == 0 &&
3497 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3498 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003499 bss = wpa_bss_get_bssid_latest(
3500 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003501 if (bss) {
3502 freq = bss->freq;
3503 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3504 "the peer " MACSTR " at %d MHz",
3505 wpa_s->auto_pd_scan_retry,
3506 MAC2STR(wpa_s->
3507 pending_join_dev_addr),
3508 freq);
3509 wpas_p2p_join_scan_req(wpa_s, freq);
3510 return;
3511 }
3512 }
3513
3514 if (join < 0)
3515 join = 0;
3516
3517 wpa_s->p2p_auto_pd = 0;
3518 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3519 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3520 MAC2STR(wpa_s->pending_join_dev_addr), join);
3521 if (p2p_prov_disc_req(wpa_s->global->p2p,
3522 wpa_s->pending_join_dev_addr,
3523 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003524 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003525 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003526 wpa_msg_global(wpa_s, MSG_INFO,
3527 P2P_EVENT_PROV_DISC_FAILURE
3528 " p2p_dev_addr=" MACSTR " status=N/A",
3529 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003530 }
3531 return;
3532 }
3533
3534 if (wpa_s->p2p_auto_join) {
3535 int join = wpas_p2p_peer_go(wpa_s,
3536 wpa_s->pending_join_dev_addr);
3537 if (join < 0) {
3538 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3539 "running a GO -> use GO Negotiation");
3540 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3541 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3542 wpa_s->p2p_persistent_group, 0, 0, 0,
3543 wpa_s->p2p_go_intent,
3544 wpa_s->p2p_connect_freq,
3545 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003546 wpa_s->p2p_pd_before_go_neg,
3547 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003548 return;
3549 }
3550
3551 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3552 "try to join the group", join ? "" :
3553 " in older scan");
3554 if (!join)
3555 wpa_s->p2p_fallback_to_go_neg = 1;
3556 }
3557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003558 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3559 wpa_s->pending_join_iface_addr);
3560 if (freq < 0 &&
3561 p2p_get_interface_addr(wpa_s->global->p2p,
3562 wpa_s->pending_join_dev_addr,
3563 iface_addr) == 0 &&
3564 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3565 {
3566 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3567 "address for join from " MACSTR " to " MACSTR
3568 " based on newly discovered P2P peer entry",
3569 MAC2STR(wpa_s->pending_join_iface_addr),
3570 MAC2STR(iface_addr));
3571 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3572 ETH_ALEN);
3573
3574 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3575 wpa_s->pending_join_iface_addr);
3576 }
3577 if (freq >= 0) {
3578 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3579 "from P2P peer table: %d MHz", freq);
3580 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003581 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003582 if (bss) {
3583 freq = bss->freq;
3584 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3585 "from BSS table: %d MHz", freq);
3586 }
3587 if (freq > 0) {
3588 u16 method;
3589
Dmitry Shmidt04949592012-07-19 12:16:46 -07003590 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003591 wpa_msg_global(wpa_s->parent, MSG_INFO,
3592 P2P_EVENT_GROUP_FORMATION_FAILURE
3593 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003594 return;
3595 }
3596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003597 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3598 "prior to joining an existing group (GO " MACSTR
3599 " freq=%u MHz)",
3600 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3601 wpa_s->pending_pd_before_join = 1;
3602
3603 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604 case WPS_PIN_DISPLAY:
3605 method = WPS_CONFIG_KEYPAD;
3606 break;
3607 case WPS_PIN_KEYPAD:
3608 method = WPS_CONFIG_DISPLAY;
3609 break;
3610 case WPS_PBC:
3611 method = WPS_CONFIG_PUSHBUTTON;
3612 break;
3613 default:
3614 method = 0;
3615 break;
3616 }
3617
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003618 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3619 wpa_s->pending_join_dev_addr) ==
3620 method)) {
3621 /*
3622 * We have already performed provision discovery for
3623 * joining the group. Proceed directly to join
3624 * operation without duplicated provision discovery. */
3625 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3626 "with " MACSTR " already done - proceed to "
3627 "join",
3628 MAC2STR(wpa_s->pending_join_dev_addr));
3629 wpa_s->pending_pd_before_join = 0;
3630 goto start;
3631 }
3632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003634 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003635 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3637 "Discovery Request before joining an "
3638 "existing group");
3639 wpa_s->pending_pd_before_join = 0;
3640 goto start;
3641 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003642 return;
3643 }
3644
3645 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3646 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3647 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3648 wpas_p2p_check_join_scan_limit(wpa_s);
3649 return;
3650
3651start:
3652 /* Start join operation immediately */
3653 wpas_p2p_join_start(wpa_s);
3654}
3655
3656
Dmitry Shmidt04949592012-07-19 12:16:46 -07003657static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659 int ret;
3660 struct wpa_driver_scan_params params;
3661 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003662 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003663 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003664#ifdef ANDROID_P2P
3665 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003667 /* If freq is not provided, check the operating freq of the GO and do a
3668 * a directed scan to save time
3669 */
3670 if(!freq) {
3671 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3672 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3673 }
3674#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675 os_memset(&params, 0, sizeof(params));
3676
3677 /* P2P Wildcard SSID */
3678 params.num_ssids = 1;
3679 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3680 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3681
3682 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003683 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3684 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3685 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686 if (wps_ie == NULL) {
3687 wpas_p2p_scan_res_join(wpa_s, NULL);
3688 return;
3689 }
3690
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003691 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3692 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693 if (ies == NULL) {
3694 wpabuf_free(wps_ie);
3695 wpas_p2p_scan_res_join(wpa_s, NULL);
3696 return;
3697 }
3698 wpabuf_put_buf(ies, wps_ie);
3699 wpabuf_free(wps_ie);
3700
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003701 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003703 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003704 params.extra_ies = wpabuf_head(ies);
3705 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003706 if (freq > 0) {
3707 freqs[0] = freq;
3708 params.freqs = freqs;
3709 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003710
3711 /*
3712 * Run a scan to update BSS table and start Provision Discovery once
3713 * the new scan results become available.
3714 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003715 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003716 if (!ret) {
3717 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003718 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003719 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720
3721 wpabuf_free(ies);
3722
3723 if (ret) {
3724 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3725 "try again later");
3726 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3727 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3728 wpas_p2p_check_join_scan_limit(wpa_s);
3729 }
3730}
3731
3732
Dmitry Shmidt04949592012-07-19 12:16:46 -07003733static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3734{
3735 struct wpa_supplicant *wpa_s = eloop_ctx;
3736 wpas_p2p_join_scan_req(wpa_s, 0);
3737}
3738
3739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003741 const u8 *dev_addr, enum p2p_wps_method wps_method,
3742 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003743{
3744 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003745 MACSTR " dev " MACSTR ")%s",
3746 MAC2STR(iface_addr), MAC2STR(dev_addr),
3747 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748
Dmitry Shmidt04949592012-07-19 12:16:46 -07003749 wpa_s->p2p_auto_pd = 0;
3750 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3752 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3753 wpa_s->pending_join_wps_method = wps_method;
3754
3755 /* Make sure we are not running find during connection establishment */
3756 wpas_p2p_stop_find(wpa_s);
3757
3758 wpa_s->p2p_join_scan_count = 0;
3759 wpas_p2p_join_scan(wpa_s, NULL);
3760 return 0;
3761}
3762
3763
3764static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3765{
3766 struct wpa_supplicant *group;
3767 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003768 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003769
3770 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3771 if (group == NULL)
3772 return -1;
3773 if (group != wpa_s) {
3774 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3775 sizeof(group->p2p_pin));
3776 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003777 } else {
3778 /*
3779 * Need to mark the current interface for p2p_group_formation
3780 * when a separate group interface is not used. This is needed
3781 * to allow p2p_cancel stop a pending p2p_connect-join.
3782 * wpas_p2p_init_group_interface() addresses this for the case
3783 * where a separate group interface is used.
3784 */
3785 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786 }
3787
3788 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003789 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003790
3791 os_memset(&res, 0, sizeof(res));
3792 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3793 ETH_ALEN);
3794 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003795 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003796 if (bss) {
3797 res.freq = bss->freq;
3798 res.ssid_len = bss->ssid_len;
3799 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3800 }
3801
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003802 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3803 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3804 "starting client");
3805 wpa_drv_cancel_remain_on_channel(wpa_s);
3806 wpa_s->off_channel_freq = 0;
3807 wpa_s->roc_waiting_drv_freq = 0;
3808 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809 wpas_start_wps_enrollee(group, &res);
3810
3811 /*
3812 * Allow a longer timeout for join-a-running-group than normal 15
3813 * second group formation timeout since the GO may not have authorized
3814 * our connection yet.
3815 */
3816 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3817 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3818 wpa_s, NULL);
3819
3820 return 0;
3821}
3822
3823
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003824static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3825 int *force_freq, int *pref_freq,
3826 int *oper_freq)
3827{
3828 if (freq > 0) {
3829 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3830 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3831 "(%u MHz) is not supported for P2P uses",
3832 freq);
3833 return -3;
3834 }
3835
3836 if (*oper_freq > 0 && freq != *oper_freq &&
3837 !(wpa_s->drv_flags &
3838 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3839 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3840 "on %u MHz while connected on another "
3841 "channel (%u MHz)", freq, *oper_freq);
3842 return -2;
3843 }
3844 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3845 "requested channel (%u MHz)", freq);
3846 *force_freq = freq;
3847 } else if (*oper_freq > 0 &&
3848 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3849 if (!(wpa_s->drv_flags &
3850 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3851 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3852 "while connected on non-P2P supported "
3853 "channel (%u MHz)", *oper_freq);
3854 return -2;
3855 }
3856 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3857 "(%u MHz) not available for P2P - try to use "
3858 "another channel", *oper_freq);
3859 *force_freq = 0;
3860 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3861 (wpa_s->drv_flags &
3862 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3863 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3864 "are already using (%u MHz) on another interface",
3865 *oper_freq);
3866 *pref_freq = *oper_freq;
3867 } else if (*oper_freq > 0) {
3868 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3869 "channel we are already using (%u MHz) on another "
3870 "interface", *oper_freq);
3871 *force_freq = *oper_freq;
3872 }
3873
3874 return 0;
3875}
3876
3877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878/**
3879 * wpas_p2p_connect - Request P2P Group Formation to be started
3880 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3881 * @peer_addr: Address of the peer P2P Device
3882 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3883 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003884 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885 * @join: Whether to join an existing group (as a client) instead of starting
3886 * Group Owner negotiation; @peer_addr is BSSID in that case
3887 * @auth: Whether to only authorize the connection instead of doing that and
3888 * initiating Group Owner negotiation
3889 * @go_intent: GO Intent or -1 to use default
3890 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003891 * @persistent_id: Persistent group credentials to use for forcing GO
3892 * parameters or -1 to generate new values (SSID/passphrase)
3893 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3894 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003895 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003896 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3897 * failure, -2 on failure due to channel not currently available,
3898 * -3 if forced channel is not supported
3899 */
3900int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3901 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003902 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003903 int go_intent, int freq, int persistent_id, int pd,
3904 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003906 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003907 u8 bssid[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003908 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003910 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003911 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912
3913 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3914 return -1;
3915
Dmitry Shmidt04949592012-07-19 12:16:46 -07003916 if (persistent_id >= 0) {
3917 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3918 if (ssid == NULL || ssid->disabled != 2 ||
3919 ssid->mode != WPAS_MODE_P2P_GO)
3920 return -1;
3921 }
3922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003923 if (go_intent < 0)
3924 go_intent = wpa_s->conf->p2p_go_intent;
3925
3926 if (!auth)
3927 wpa_s->p2p_long_listen = 0;
3928
3929 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003930 wpa_s->p2p_persistent_group = !!persistent_group;
3931 wpa_s->p2p_persistent_id = persistent_id;
3932 wpa_s->p2p_go_intent = go_intent;
3933 wpa_s->p2p_connect_freq = freq;
3934 wpa_s->p2p_fallback_to_go_neg = 0;
3935 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003936 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937
3938 if (pin)
3939 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3940 else if (wps_method == WPS_PIN_DISPLAY) {
3941 ret = wps_generate_pin();
3942 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3943 ret);
3944 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3945 wpa_s->p2p_pin);
3946 } else
3947 wpa_s->p2p_pin[0] = '\0';
3948
Dmitry Shmidt04949592012-07-19 12:16:46 -07003949 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3951 if (auth) {
3952 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3953 "connect a running group from " MACSTR,
3954 MAC2STR(peer_addr));
3955 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3956 return ret;
3957 }
3958 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3959 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3960 iface_addr) < 0) {
3961 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3962 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3963 dev_addr);
3964 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003965 if (auto_join) {
3966 os_get_time(&wpa_s->p2p_auto_started);
3967 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3968 "%ld.%06ld",
3969 wpa_s->p2p_auto_started.sec,
3970 wpa_s->p2p_auto_started.usec);
3971 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003972 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003973 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3974 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003975 return -1;
3976 return ret;
3977 }
3978
3979 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003980 wpa_s->assoc_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003981 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003982 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003983 oper_freq = wpa_drv_shared_freq(wpa_s);
3984 if (oper_freq < 0)
3985 oper_freq = 0;
3986 }
3987
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003988 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
3989 &oper_freq);
3990 if (res)
3991 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003992 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993
3994 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3995
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003996 if (wpa_s->create_p2p_iface) {
3997 /* Prepare to add a new interface for the group */
3998 iftype = WPA_IF_P2P_GROUP;
3999 if (go_intent == 15)
4000 iftype = WPA_IF_P2P_GO;
4001 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4002 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4003 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004005 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004006
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004007 if_addr = wpa_s->pending_interface_addr;
4008 } else
4009 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004010
4011 if (auth) {
4012 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004013 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004014 force_freq, persistent_group, ssid,
4015 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016 return -1;
4017 return ret;
4018 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004019
4020 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4021 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004022 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004023 if (wpa_s->create_p2p_iface)
4024 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004025 return -1;
4026 }
4027 return ret;
4028}
4029
4030
4031/**
4032 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4033 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4034 * @freq: Frequency of the channel in MHz
4035 * @duration: Duration of the stay on the channel in milliseconds
4036 *
4037 * This callback is called when the driver indicates that it has started the
4038 * requested remain-on-channel duration.
4039 */
4040void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4041 unsigned int freq, unsigned int duration)
4042{
4043 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4044 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004045 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4046 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4047 wpa_s->pending_listen_duration);
4048 wpa_s->pending_listen_freq = 0;
4049 }
4050}
4051
4052
4053static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4054 unsigned int timeout)
4055{
4056 /* Limit maximum Listen state time based on driver limitation. */
4057 if (timeout > wpa_s->max_remain_on_chan)
4058 timeout = wpa_s->max_remain_on_chan;
4059
4060 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4061 return wpa_drv_p2p_listen(wpa_s, timeout);
4062
4063 return p2p_listen(wpa_s->global->p2p, timeout);
4064}
4065
4066
4067/**
4068 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4069 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4070 * @freq: Frequency of the channel in MHz
4071 *
4072 * This callback is called when the driver indicates that a remain-on-channel
4073 * operation has been completed, i.e., the duration on the requested channel
4074 * has timed out.
4075 */
4076void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4077 unsigned int freq)
4078{
4079 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4080 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004081 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4083 return;
4084 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4085 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004086 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 return;
4088 if (wpa_s->p2p_long_listen > 0)
4089 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4090 if (wpa_s->p2p_long_listen > 0) {
4091 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4092 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4093 }
4094}
4095
4096
4097/**
4098 * wpas_p2p_group_remove - Remove a P2P group
4099 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4100 * @ifname: Network interface name of the group interface or "*" to remove all
4101 * groups
4102 * Returns: 0 on success, -1 on failure
4103 *
4104 * This function is used to remove a P2P group. This can be used to disconnect
4105 * from a group in which the local end is a P2P Client or to end a P2P Group in
4106 * case the local end is the Group Owner. If a virtual network interface was
4107 * created for this group, that interface will be removed. Otherwise, only the
4108 * configured P2P group network will be removed from the interface.
4109 */
4110int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4111{
4112 struct wpa_global *global = wpa_s->global;
4113
4114 if (os_strcmp(ifname, "*") == 0) {
4115 struct wpa_supplicant *prev;
4116 wpa_s = global->ifaces;
4117 while (wpa_s) {
4118 prev = wpa_s;
4119 wpa_s = wpa_s->next;
4120 wpas_p2p_disconnect(prev);
4121 }
4122 return 0;
4123 }
4124
4125 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4126 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4127 break;
4128 }
4129
4130 return wpas_p2p_disconnect(wpa_s);
4131}
4132
4133
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004134static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4135{
4136 unsigned int r;
4137
4138 if (freq == 2) {
4139 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4140 "band");
4141 if (wpa_s->best_24_freq > 0 &&
4142 p2p_supported_freq(wpa_s->global->p2p,
4143 wpa_s->best_24_freq)) {
4144 freq = wpa_s->best_24_freq;
4145 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4146 "channel: %d MHz", freq);
4147 } else {
4148 os_get_random((u8 *) &r, sizeof(r));
4149 freq = 2412 + (r % 3) * 25;
4150 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4151 "channel: %d MHz", freq);
4152 }
4153 }
4154
4155 if (freq == 5) {
4156 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4157 "band");
4158 if (wpa_s->best_5_freq > 0 &&
4159 p2p_supported_freq(wpa_s->global->p2p,
4160 wpa_s->best_5_freq)) {
4161 freq = wpa_s->best_5_freq;
4162 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4163 "channel: %d MHz", freq);
4164 } else {
4165 os_get_random((u8 *) &r, sizeof(r));
4166 freq = 5180 + (r % 4) * 20;
4167 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4168 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4169 "5 GHz channel for P2P group");
4170 return -1;
4171 }
4172 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4173 "channel: %d MHz", freq);
4174 }
4175 }
4176
4177 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4178 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4179 "(%u MHz) is not supported for P2P uses",
4180 freq);
4181 return -1;
4182 }
4183
4184 return freq;
4185}
4186
4187
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004188static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4189 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004190 int freq, int ht40,
4191 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004192{
4193 u8 bssid[ETH_ALEN];
4194 int res;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004195 unsigned int pref_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004196
4197 os_memset(params, 0, sizeof(*params));
4198 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004199 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004200 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004201 if (!freq_included(channels, freq)) {
4202 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4203 "accepted", freq);
4204 return -1;
4205 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004206 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4207 "frequency %d MHz", freq);
4208 params->freq = freq;
4209 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4210 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004211 wpa_s->conf->p2p_oper_channel <= 11 &&
4212 freq_included(channels,
4213 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004214 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4215 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4216 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004217 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4218 wpa_s->conf->p2p_oper_reg_class == 116 ||
4219 wpa_s->conf->p2p_oper_reg_class == 117 ||
4220 wpa_s->conf->p2p_oper_reg_class == 124 ||
4221 wpa_s->conf->p2p_oper_reg_class == 126 ||
4222 wpa_s->conf->p2p_oper_reg_class == 127) &&
4223 freq_included(channels,
4224 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4226 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4227 "frequency %d MHz", params->freq);
4228 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4229 wpa_s->best_overall_freq > 0 &&
4230 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004231 wpa_s->best_overall_freq) &&
4232 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004233 params->freq = wpa_s->best_overall_freq;
4234 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4235 "channel %d MHz", params->freq);
4236 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4237 wpa_s->best_24_freq > 0 &&
4238 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004239 wpa_s->best_24_freq) &&
4240 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004241 params->freq = wpa_s->best_24_freq;
4242 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4243 "channel %d MHz", params->freq);
4244 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4245 wpa_s->best_5_freq > 0 &&
4246 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004247 wpa_s->best_5_freq) &&
4248 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249 params->freq = wpa_s->best_5_freq;
4250 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4251 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004252 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4253 channels))) {
4254 params->freq = pref_freq;
4255 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4256 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004258 int chan;
4259 for (chan = 0; chan < 11; chan++) {
4260 params->freq = 2412 + chan * 5;
4261 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004262 params->freq) &&
4263 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004264 break;
4265 }
4266 if (chan == 11) {
4267 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4268 "allowed");
4269 return -1;
4270 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004271 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4272 "known)", params->freq);
4273 }
4274
4275 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4276 wpa_s->assoc_freq && !freq) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004277 if (!p2p_supported_freq(wpa_s->global->p2p, wpa_s->assoc_freq)
4278 || !freq_included(channels, wpa_s->assoc_freq)) {
4279 if (wpa_s->drv_flags &
4280 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4281 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4282 "the channel we are already using "
4283 "(%u MHz) - allow multi-channel "
4284 "concurrency", wpa_s->assoc_freq);
4285 } else {
4286 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4287 "the channel we are already using "
4288 "(%u MHz)", wpa_s->assoc_freq);
4289 return -1;
4290 }
4291 } else {
4292 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we "
4293 "are already using (%u MHz)",
4294 wpa_s->assoc_freq);
4295 params->freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004296 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004297 }
4298
4299 res = wpa_drv_shared_freq(wpa_s);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004300 if (res > 0 && !freq &&
4301 (!p2p_supported_freq(wpa_s->global->p2p, res) ||
4302 !freq_included(channels, res))) {
4303 if (wpa_s->drv_flags &
4304 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4305 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4306 "channel we are already using on a shared "
4307 "interface (%u MHz) - allow multi-channel "
4308 "concurrency", res);
4309 } else {
4310 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4311 "channel we are already using on a shared "
4312 "interface (%u MHz)", res);
4313 return -1;
4314 }
4315 } else if (res > 0 && !freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004316 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4317 "already using on a shared interface");
4318 params->freq = res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004319 if (!freq_included(channels, params->freq)) {
4320 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4321 "accepted", params->freq);
4322 return -1;
4323 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004324 } else if (res > 0 && freq != res &&
4325 !(wpa_s->drv_flags &
4326 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4327 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4328 "while connected on another channel (%u MHz)",
4329 freq, res);
4330 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004331 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004332
4333 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334}
4335
4336
4337static struct wpa_supplicant *
4338wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4339 int go)
4340{
4341 struct wpa_supplicant *group_wpa_s;
4342
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004343 if (!wpas_p2p_create_iface(wpa_s)) {
4344 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4345 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004346 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004347 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004348
4349 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004350 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004351 wpa_msg_global(wpa_s, MSG_ERROR,
4352 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004353 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004354 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4356 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004357 wpa_msg_global(wpa_s, MSG_ERROR,
4358 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004359 wpas_p2p_remove_pending_group_interface(wpa_s);
4360 return NULL;
4361 }
4362
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004363 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4364 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365 return group_wpa_s;
4366}
4367
4368
4369/**
4370 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4371 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4372 * @persistent_group: Whether to create a persistent group
4373 * @freq: Frequency for the group or 0 to indicate no hardcoding
4374 * Returns: 0 on success, -1 on failure
4375 *
4376 * This function creates a new P2P group with the local end as the Group Owner,
4377 * i.e., without using Group Owner Negotiation.
4378 */
4379int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004380 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004381{
4382 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004383
4384 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4385 return -1;
4386
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004387 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004388 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004389 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004390
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004391 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4392 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004393 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004394
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004395 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004396 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004397 if (params.freq &&
4398 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4399 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4400 "(%u MHz) is not supported for P2P uses",
4401 params.freq);
4402 return -1;
4403 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404 p2p_go_params(wpa_s->global->p2p, &params);
4405 params.persistent_group = persistent_group;
4406
4407 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4408 if (wpa_s == NULL)
4409 return -1;
4410 wpas_start_wps_go(wpa_s, &params, 0);
4411
4412 return 0;
4413}
4414
4415
4416static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4417 struct wpa_ssid *params, int addr_allocated)
4418{
4419 struct wpa_ssid *ssid;
4420
4421 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4422 if (wpa_s == NULL)
4423 return -1;
4424
4425 wpa_supplicant_ap_deinit(wpa_s);
4426
4427 ssid = wpa_config_add_network(wpa_s->conf);
4428 if (ssid == NULL)
4429 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004430 wpa_config_set_network_defaults(ssid);
4431 ssid->temporary = 1;
4432 ssid->proto = WPA_PROTO_RSN;
4433 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4434 ssid->group_cipher = WPA_CIPHER_CCMP;
4435 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4436 ssid->ssid = os_malloc(params->ssid_len);
4437 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438 wpa_config_remove_network(wpa_s->conf, ssid->id);
4439 return -1;
4440 }
4441 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4442 ssid->ssid_len = params->ssid_len;
4443 ssid->p2p_group = 1;
4444 ssid->export_keys = 1;
4445 if (params->psk_set) {
4446 os_memcpy(ssid->psk, params->psk, 32);
4447 ssid->psk_set = 1;
4448 }
4449 if (params->passphrase)
4450 ssid->passphrase = os_strdup(params->passphrase);
4451
4452 wpa_supplicant_select_network(wpa_s, ssid);
4453
4454 wpa_s->show_group_started = 1;
4455
4456 return 0;
4457}
4458
4459
4460int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4461 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004462 int freq, int ht40,
4463 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004464{
4465 struct p2p_go_neg_results params;
4466 int go = 0;
4467
4468 if (ssid->disabled != 2 || ssid->ssid == NULL)
4469 return -1;
4470
4471 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4472 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4473 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4474 "already running");
4475 return 0;
4476 }
4477
4478 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004479 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004480
Dmitry Shmidt04949592012-07-19 12:16:46 -07004481 wpa_s->p2p_fallback_to_go_neg = 0;
4482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004483 if (ssid->mode == WPAS_MODE_INFRA)
4484 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4485
4486 if (ssid->mode != WPAS_MODE_P2P_GO)
4487 return -1;
4488
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004489 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4490 if (freq < 0)
4491 return -1;
4492
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004493 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004494 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004495
4496 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004497 params.psk_set = ssid->psk_set;
4498 if (params.psk_set)
4499 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004500 if (ssid->passphrase) {
4501 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4502 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4503 "persistent group");
4504 return -1;
4505 }
4506 os_strlcpy(params.passphrase, ssid->passphrase,
4507 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004509 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4510 params.ssid_len = ssid->ssid_len;
4511 params.persistent_group = 1;
4512
4513 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4514 if (wpa_s == NULL)
4515 return -1;
4516
4517 wpas_start_wps_go(wpa_s, &params, 0);
4518
4519 return 0;
4520}
4521
4522
4523static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4524 struct wpabuf *proberesp_ies)
4525{
4526 struct wpa_supplicant *wpa_s = ctx;
4527 if (wpa_s->ap_iface) {
4528 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004529 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4530 wpabuf_free(beacon_ies);
4531 wpabuf_free(proberesp_ies);
4532 return;
4533 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004534 if (beacon_ies) {
4535 wpabuf_free(hapd->p2p_beacon_ie);
4536 hapd->p2p_beacon_ie = beacon_ies;
4537 }
4538 wpabuf_free(hapd->p2p_probe_resp_ie);
4539 hapd->p2p_probe_resp_ie = proberesp_ies;
4540 } else {
4541 wpabuf_free(beacon_ies);
4542 wpabuf_free(proberesp_ies);
4543 }
4544 wpa_supplicant_ap_update_beacon(wpa_s);
4545}
4546
4547
4548static void wpas_p2p_idle_update(void *ctx, int idle)
4549{
4550 struct wpa_supplicant *wpa_s = ctx;
4551 if (!wpa_s->ap_iface)
4552 return;
4553 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004554 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004555 wpas_p2p_set_group_idle_timeout(wpa_s);
4556 else
4557 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4558}
4559
4560
4561struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004562 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563{
4564 struct p2p_group *group;
4565 struct p2p_group_config *cfg;
4566
4567 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4568 return NULL;
4569 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4570 return NULL;
4571
4572 cfg = os_zalloc(sizeof(*cfg));
4573 if (cfg == NULL)
4574 return NULL;
4575
Dmitry Shmidt04949592012-07-19 12:16:46 -07004576 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004577 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004578 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004579 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4581 if (wpa_s->max_stations &&
4582 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4583 cfg->max_clients = wpa_s->max_stations;
4584 else
4585 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004586 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4587 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004588 cfg->cb_ctx = wpa_s;
4589 cfg->ie_update = wpas_p2p_ie_update;
4590 cfg->idle_update = wpas_p2p_idle_update;
4591
4592 group = p2p_group_init(wpa_s->global->p2p, cfg);
4593 if (group == NULL)
4594 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004595 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004596 p2p_group_notif_formation_done(group);
4597 wpa_s->p2p_group = group;
4598 return group;
4599}
4600
4601
4602void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4603 int registrar)
4604{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004605 struct wpa_ssid *ssid = wpa_s->current_ssid;
4606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607 if (!wpa_s->p2p_in_provisioning) {
4608 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4609 "provisioning not in progress");
4610 return;
4611 }
4612
Dmitry Shmidt04949592012-07-19 12:16:46 -07004613 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4614 u8 go_dev_addr[ETH_ALEN];
4615 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4616 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4617 ssid->ssid_len);
4618 /* Clear any stored provisioning info */
4619 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4620 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004622 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4623 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004624 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4625 /*
4626 * Use a separate timeout for initial data connection to
4627 * complete to allow the group to be removed automatically if
4628 * something goes wrong in this step before the P2P group idle
4629 * timeout mechanism is taken into use.
4630 */
4631 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4632 wpas_p2p_group_formation_timeout,
4633 wpa_s->parent, NULL);
4634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635 if (wpa_s->global->p2p)
4636 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4637 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4638 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4639 wpas_group_formation_completed(wpa_s, 1);
4640}
4641
4642
Jouni Malinen75ecf522011-06-27 15:19:46 -07004643void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4644 struct wps_event_fail *fail)
4645{
4646 if (!wpa_s->p2p_in_provisioning) {
4647 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4648 "provisioning not in progress");
4649 return;
4650 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004651
4652 if (wpa_s->go_params) {
4653 p2p_clear_provisioning_info(
4654 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004655 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004656 }
4657
Jouni Malinen75ecf522011-06-27 15:19:46 -07004658 wpas_notify_p2p_wps_failed(wpa_s, fail);
4659}
4660
4661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004663 const char *config_method,
4664 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665{
4666 u16 config_methods;
4667
Dmitry Shmidt04949592012-07-19 12:16:46 -07004668 wpa_s->p2p_fallback_to_go_neg = 0;
4669 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004670 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004672 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004674 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4675 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004677 else {
4678 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004679 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004680 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004681
Dmitry Shmidt04949592012-07-19 12:16:46 -07004682 if (use == WPAS_P2P_PD_AUTO) {
4683 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4684 wpa_s->pending_pd_config_methods = config_methods;
4685 wpa_s->p2p_auto_pd = 1;
4686 wpa_s->p2p_auto_join = 0;
4687 wpa_s->pending_pd_before_join = 0;
4688 wpa_s->auto_pd_scan_retry = 0;
4689 wpas_p2p_stop_find(wpa_s);
4690 wpa_s->p2p_join_scan_count = 0;
4691 os_get_time(&wpa_s->p2p_auto_started);
4692 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4693 wpa_s->p2p_auto_started.sec,
4694 wpa_s->p2p_auto_started.usec);
4695 wpas_p2p_join_scan(wpa_s, NULL);
4696 return 0;
4697 }
4698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004699 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4700 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004701 config_methods,
4702 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703 }
4704
4705 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4706 return -1;
4707
4708 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004709 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004710 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004711}
4712
4713
4714int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4715 char *end)
4716{
4717 return p2p_scan_result_text(ies, ies_len, buf, end);
4718}
4719
4720
4721static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4722{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004723 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004724 return;
4725
4726 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4727 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004728 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729}
4730
4731
4732int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4733 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004734 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004735 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004736{
4737 wpas_p2p_clear_pending_action_tx(wpa_s);
4738 wpa_s->p2p_long_listen = 0;
4739
4740 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4741 return wpa_drv_p2p_find(wpa_s, timeout, type);
4742
Dmitry Shmidt04949592012-07-19 12:16:46 -07004743 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4744 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745 return -1;
4746
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004747 wpa_supplicant_cancel_sched_scan(wpa_s);
4748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004749 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004750 num_req_dev_types, req_dev_types, dev_id,
4751 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004752}
4753
4754
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004755static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756{
4757 wpas_p2p_clear_pending_action_tx(wpa_s);
4758 wpa_s->p2p_long_listen = 0;
4759 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4760 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004761 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762
4763 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4764 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004765 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766 }
4767
4768 if (wpa_s->global->p2p)
4769 p2p_stop_find(wpa_s->global->p2p);
4770
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004771 return 0;
4772}
4773
4774
4775void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4776{
4777 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4778 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004779 wpas_p2p_remove_pending_group_interface(wpa_s);
4780}
4781
4782
4783static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4784{
4785 struct wpa_supplicant *wpa_s = eloop_ctx;
4786 wpa_s->p2p_long_listen = 0;
4787}
4788
4789
4790int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4791{
4792 int res;
4793
4794 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4795 return -1;
4796
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004797 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798 wpas_p2p_clear_pending_action_tx(wpa_s);
4799
4800 if (timeout == 0) {
4801 /*
4802 * This is a request for unlimited Listen state. However, at
4803 * least for now, this is mapped to a Listen state for one
4804 * hour.
4805 */
4806 timeout = 3600;
4807 }
4808 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4809 wpa_s->p2p_long_listen = 0;
4810
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004811 /*
4812 * Stop previous find/listen operation to avoid trying to request a new
4813 * remain-on-channel operation while the driver is still running the
4814 * previous one.
4815 */
4816 if (wpa_s->global->p2p)
4817 p2p_stop_find(wpa_s->global->p2p);
4818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004819 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4820 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4821 wpa_s->p2p_long_listen = timeout * 1000;
4822 eloop_register_timeout(timeout, 0,
4823 wpas_p2p_long_listen_timeout,
4824 wpa_s, NULL);
4825 }
4826
4827 return res;
4828}
4829
4830
4831int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4832 u8 *buf, size_t len, int p2p_group)
4833{
4834 struct wpabuf *p2p_ie;
4835 int ret;
4836
4837 if (wpa_s->global->p2p_disabled)
4838 return -1;
4839 if (wpa_s->global->p2p == NULL)
4840 return -1;
4841 if (bss == NULL)
4842 return -1;
4843
4844 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4845 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4846 p2p_group, p2p_ie);
4847 wpabuf_free(p2p_ie);
4848
4849 return ret;
4850}
4851
4852
4853int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004854 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004855 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856{
4857 if (wpa_s->global->p2p_disabled)
4858 return 0;
4859 if (wpa_s->global->p2p == NULL)
4860 return 0;
4861
Dmitry Shmidt04949592012-07-19 12:16:46 -07004862 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4863 ie, ie_len)) {
4864 case P2P_PREQ_NOT_P2P:
4865 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4866 ssi_signal);
4867 /* fall through */
4868 case P2P_PREQ_MALFORMED:
4869 case P2P_PREQ_NOT_LISTEN:
4870 case P2P_PREQ_NOT_PROCESSED:
4871 default: /* make gcc happy */
4872 return 0;
4873 case P2P_PREQ_PROCESSED:
4874 return 1;
4875 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004876}
4877
4878
4879void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4880 const u8 *sa, const u8 *bssid,
4881 u8 category, const u8 *data, size_t len, int freq)
4882{
4883 if (wpa_s->global->p2p_disabled)
4884 return;
4885 if (wpa_s->global->p2p == NULL)
4886 return;
4887
4888 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4889 freq);
4890}
4891
4892
4893void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4894{
4895 if (wpa_s->global->p2p_disabled)
4896 return;
4897 if (wpa_s->global->p2p == NULL)
4898 return;
4899
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004900 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004901}
4902
4903
4904void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4905{
4906 p2p_group_deinit(wpa_s->p2p_group);
4907 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004908
4909 wpa_s->ap_configured_cb = NULL;
4910 wpa_s->ap_configured_cb_ctx = NULL;
4911 wpa_s->ap_configured_cb_data = NULL;
4912 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913}
4914
4915
4916int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4917{
4918 wpa_s->p2p_long_listen = 0;
4919
4920 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4921 return wpa_drv_p2p_reject(wpa_s, addr);
4922
4923 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4924 return -1;
4925
4926 return p2p_reject(wpa_s->global->p2p, addr);
4927}
4928
4929
4930/* Invite to reinvoke a persistent group */
4931int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004932 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004933 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004934{
4935 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004936 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004937 int force_freq = 0, oper_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004938 int res;
4939
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004940 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004941 if (peer_addr)
4942 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4943 else
4944 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945
Jouni Malinen31be0a42012-08-31 21:20:51 +03004946 wpa_s->p2p_persistent_go_freq = freq;
4947 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004948 if (ssid->mode == WPAS_MODE_P2P_GO) {
4949 role = P2P_INVITE_ROLE_GO;
4950 if (peer_addr == NULL) {
4951 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4952 "address in invitation command");
4953 return -1;
4954 }
4955 if (wpas_p2p_create_iface(wpa_s)) {
4956 if (wpas_p2p_add_group_interface(wpa_s,
4957 WPA_IF_P2P_GO) < 0) {
4958 wpa_printf(MSG_ERROR, "P2P: Failed to "
4959 "allocate a new interface for the "
4960 "group");
4961 return -1;
4962 }
4963 bssid = wpa_s->pending_interface_addr;
4964 } else
4965 bssid = wpa_s->own_addr;
4966 } else {
4967 role = P2P_INVITE_ROLE_CLIENT;
4968 peer_addr = ssid->bssid;
4969 }
4970 wpa_s->pending_invite_ssid_id = ssid->id;
4971
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004972 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4973 wpa_s->assoc_freq) {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004974 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004975 if (bssid == NULL)
4976 bssid = bssid_buf;
4977 } else {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004978 oper_freq = wpa_drv_shared_freq(wpa_s);
4979 if (oper_freq < 0)
4980 oper_freq = 0;
4981 }
4982
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004983 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4984 &oper_freq);
4985 if (res)
4986 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4989 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4990 ssid->ssid, ssid->ssid_len,
4991 go_dev_addr, 1);
4992
4993 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4994 return -1;
4995
4996 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004997 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4998 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004999}
5000
5001
5002/* Invite to join an active group */
5003int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5004 const u8 *peer_addr, const u8 *go_dev_addr)
5005{
5006 struct wpa_global *global = wpa_s->global;
5007 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005008 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005010 int persistent;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005011 int force_freq = 0, oper_freq = 0, pref_freq = 0;
5012 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005013
Jouni Malinen31be0a42012-08-31 21:20:51 +03005014 wpa_s->p2p_persistent_go_freq = 0;
5015 wpa_s->p2p_go_ht40 = 0;
5016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5018 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5019 break;
5020 }
5021 if (wpa_s == NULL) {
5022 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5023 return -1;
5024 }
5025
5026 ssid = wpa_s->current_ssid;
5027 if (ssid == NULL) {
5028 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5029 "invitation");
5030 return -1;
5031 }
5032
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005033 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005034 persistent = ssid->p2p_persistent_group &&
5035 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5036 ssid->ssid, ssid->ssid_len);
5037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038 if (ssid->mode == WPAS_MODE_P2P_GO) {
5039 role = P2P_INVITE_ROLE_ACTIVE_GO;
5040 bssid = wpa_s->own_addr;
5041 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005042 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043 } else {
5044 role = P2P_INVITE_ROLE_CLIENT;
5045 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5046 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5047 "invite to current group");
5048 return -1;
5049 }
5050 bssid = wpa_s->bssid;
5051 if (go_dev_addr == NULL &&
5052 !is_zero_ether_addr(wpa_s->go_dev_addr))
5053 go_dev_addr = wpa_s->go_dev_addr;
5054 }
5055 wpa_s->parent->pending_invite_ssid_id = -1;
5056
5057 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5058 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5059 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005060 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061
5062 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5063 return -1;
5064
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005065 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
5066 wpa_s->assoc_freq) {
5067 oper_freq = wpa_s->assoc_freq;
5068 if (bssid == NULL)
5069 bssid = bssid_buf;
5070 } else {
5071 oper_freq = wpa_drv_shared_freq(wpa_s);
5072 if (oper_freq < 0)
5073 oper_freq = 0;
5074 }
5075
5076 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
5077 &oper_freq);
5078 if (res)
5079 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005080 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005083 ssid->ssid, ssid->ssid_len, force_freq,
5084 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085}
5086
5087
5088void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5089{
5090 struct wpa_ssid *ssid = wpa_s->current_ssid;
5091 const char *ssid_txt;
5092 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005093 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005094 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005095 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005096
Dmitry Shmidt04949592012-07-19 12:16:46 -07005097 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5098 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5099 wpa_s->parent, NULL);
5100 }
5101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005103 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104
5105 wpa_s->show_group_started = 0;
5106
5107 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5108 os_memset(go_dev_addr, 0, ETH_ALEN);
5109 if (ssid->bssid_set)
5110 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5111 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5112 ssid->ssid_len);
5113 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5114
5115 if (wpa_s->global->p2p_group_formation == wpa_s)
5116 wpa_s->global->p2p_group_formation = NULL;
5117
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005118 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5119 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120 if (ssid->passphrase == NULL && ssid->psk_set) {
5121 char psk[65];
5122 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005123 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5124 "%s client ssid=\"%s\" freq=%d psk=%s "
5125 "go_dev_addr=" MACSTR "%s",
5126 wpa_s->ifname, ssid_txt, freq, psk,
5127 MAC2STR(go_dev_addr),
5128 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005129 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005130 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5131 "%s client ssid=\"%s\" freq=%d "
5132 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5133 wpa_s->ifname, ssid_txt, freq,
5134 ssid->passphrase ? ssid->passphrase : "",
5135 MAC2STR(go_dev_addr),
5136 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005137 }
5138
5139 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005140 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5141 ssid, go_dev_addr);
5142 if (network_id < 0)
5143 network_id = ssid->id;
5144 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005145
5146done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005147 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005148}
5149
5150
5151int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5152 u32 interval1, u32 duration2, u32 interval2)
5153{
5154 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5155 return -1;
5156 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5157 return -1;
5158
5159 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5160 wpa_s->current_ssid == NULL ||
5161 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5162 return -1;
5163
5164 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5165 wpa_s->own_addr, wpa_s->assoc_freq,
5166 duration1, interval1, duration2, interval2);
5167}
5168
5169
5170int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5171 unsigned int interval)
5172{
5173 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5174 return -1;
5175
5176 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5177 return -1;
5178
5179 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5180}
5181
5182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005183static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5184{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005185 if (wpa_s->current_ssid == NULL) {
5186 /*
5187 * current_ssid can be cleared when P2P client interface gets
5188 * disconnected, so assume this interface was used as P2P
5189 * client.
5190 */
5191 return 1;
5192 }
5193 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005194 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5195}
5196
5197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005198static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5199{
5200 struct wpa_supplicant *wpa_s = eloop_ctx;
5201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005202 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005203 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5204 "disabled");
5205 return;
5206 }
5207
Dmitry Shmidt04949592012-07-19 12:16:46 -07005208 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5209 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005210 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005211}
5212
5213
5214static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5215{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005216 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005217
Dmitry Shmidt04949592012-07-19 12:16:46 -07005218 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5219 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005221 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5222 return;
5223
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005224 timeout = wpa_s->conf->p2p_group_idle;
5225 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5226 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5227 timeout = P2P_MAX_CLIENT_IDLE;
5228
5229 if (timeout == 0)
5230 return;
5231
Dmitry Shmidt04949592012-07-19 12:16:46 -07005232 if (timeout < 0) {
5233 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5234 timeout = 0; /* special client mode no-timeout */
5235 else
5236 return;
5237 }
5238
5239 if (wpa_s->p2p_in_provisioning) {
5240 /*
5241 * Use the normal group formation timeout during the
5242 * provisioning phase to avoid terminating this process too
5243 * early due to group idle timeout.
5244 */
5245 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5246 "during provisioning");
5247 return;
5248 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305249
Dmitry Shmidt04949592012-07-19 12:16:46 -07005250 if (wpa_s->show_group_started) {
5251 /*
5252 * Use the normal group formation timeout between the end of
5253 * the provisioning phase and completion of 4-way handshake to
5254 * avoid terminating this process too early due to group idle
5255 * timeout.
5256 */
5257 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5258 "while waiting for initial 4-way handshake to "
5259 "complete");
5260 return;
5261 }
5262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005263 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005264 timeout);
5265 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5266 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005267}
5268
5269
Jouni Malinen2b89da82012-08-31 22:04:41 +03005270/* Returns 1 if the interface was removed */
5271int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5272 u16 reason_code, const u8 *ie, size_t ie_len,
5273 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005274{
5275 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005276 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005277 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005278 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005279
Dmitry Shmidt04949592012-07-19 12:16:46 -07005280 if (!locally_generated)
5281 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5282 ie_len);
5283
5284 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5285 wpa_s->current_ssid &&
5286 wpa_s->current_ssid->p2p_group &&
5287 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5288 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5289 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005290 if (wpas_p2p_group_delete(wpa_s,
5291 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5292 > 0)
5293 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005294 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005295
5296 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005297}
5298
5299
5300void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005301 u16 reason_code, const u8 *ie, size_t ie_len,
5302 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005303{
5304 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5305 return;
5306 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5307 return;
5308
Dmitry Shmidt04949592012-07-19 12:16:46 -07005309 if (!locally_generated)
5310 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5311 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005312}
5313
5314
5315void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5316{
5317 struct p2p_data *p2p = wpa_s->global->p2p;
5318
5319 if (p2p == NULL)
5320 return;
5321
5322 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5323 return;
5324
5325 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5326 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5327
5328 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5329 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5330
5331 if (wpa_s->wps &&
5332 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5333 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5334
5335 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5336 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5337
5338 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5339 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5340 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5341 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5342 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5343 }
5344
5345 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5346 p2p_set_sec_dev_types(p2p,
5347 (void *) wpa_s->conf->sec_device_type,
5348 wpa_s->conf->num_sec_device_types);
5349
5350 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5351 int i;
5352 p2p_remove_wps_vendor_extensions(p2p);
5353 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5354 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5355 continue;
5356 p2p_add_wps_vendor_extension(
5357 p2p, wpa_s->conf->wps_vendor_ext[i]);
5358 }
5359 }
5360
5361 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5362 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5363 char country[3];
5364 country[0] = wpa_s->conf->country[0];
5365 country[1] = wpa_s->conf->country[1];
5366 country[2] = 0x04;
5367 p2p_set_country(p2p, country);
5368 }
5369
5370 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5371 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5372 wpa_s->conf->p2p_ssid_postfix ?
5373 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5374 0);
5375 }
5376
5377 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5378 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005379
5380 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5381 u8 reg_class, channel;
5382 int ret;
5383 unsigned int r;
5384 if (wpa_s->conf->p2p_listen_reg_class &&
5385 wpa_s->conf->p2p_listen_channel) {
5386 reg_class = wpa_s->conf->p2p_listen_reg_class;
5387 channel = wpa_s->conf->p2p_listen_channel;
5388 } else {
5389 reg_class = 81;
5390 /*
5391 * Pick one of the social channels randomly as the
5392 * listen channel.
5393 */
5394 os_get_random((u8 *) &r, sizeof(r));
5395 channel = 1 + (r % 3) * 5;
5396 }
5397 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5398 if (ret)
5399 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5400 "failed: %d", ret);
5401 }
5402 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5403 u8 op_reg_class, op_channel, cfg_op_channel;
5404 int ret = 0;
5405 unsigned int r;
5406 if (wpa_s->conf->p2p_oper_reg_class &&
5407 wpa_s->conf->p2p_oper_channel) {
5408 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5409 op_channel = wpa_s->conf->p2p_oper_channel;
5410 cfg_op_channel = 1;
5411 } else {
5412 op_reg_class = 81;
5413 /*
5414 * Use random operation channel from (1, 6, 11)
5415 *if no other preference is indicated.
5416 */
5417 os_get_random((u8 *) &r, sizeof(r));
5418 op_channel = 1 + (r % 3) * 5;
5419 cfg_op_channel = 0;
5420 }
5421 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5422 cfg_op_channel);
5423 if (ret)
5424 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5425 "failed: %d", ret);
5426 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005427
5428 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5429 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5430 wpa_s->conf->p2p_pref_chan) < 0) {
5431 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5432 "update failed");
5433 }
5434 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435}
5436
5437
5438int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5439 int duration)
5440{
5441 if (!wpa_s->ap_iface)
5442 return -1;
5443 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5444 duration);
5445}
5446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447
5448int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5449{
5450 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5451 return -1;
5452 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5453 return -1;
5454
5455 wpa_s->global->cross_connection = enabled;
5456 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5457
5458 if (!enabled) {
5459 struct wpa_supplicant *iface;
5460
5461 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5462 {
5463 if (iface->cross_connect_enabled == 0)
5464 continue;
5465
5466 iface->cross_connect_enabled = 0;
5467 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005468 wpa_msg_global(iface->parent, MSG_INFO,
5469 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5470 iface->ifname,
5471 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472 }
5473 }
5474
5475 return 0;
5476}
5477
5478
5479static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5480{
5481 struct wpa_supplicant *iface;
5482
5483 if (!uplink->global->cross_connection)
5484 return;
5485
5486 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5487 if (!iface->cross_connect_enabled)
5488 continue;
5489 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5490 0)
5491 continue;
5492 if (iface->ap_iface == NULL)
5493 continue;
5494 if (iface->cross_connect_in_use)
5495 continue;
5496
5497 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005498 wpa_msg_global(iface->parent, MSG_INFO,
5499 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5500 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501 }
5502}
5503
5504
5505static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5506{
5507 struct wpa_supplicant *iface;
5508
5509 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5510 if (!iface->cross_connect_enabled)
5511 continue;
5512 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5513 0)
5514 continue;
5515 if (!iface->cross_connect_in_use)
5516 continue;
5517
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005518 wpa_msg_global(iface->parent, MSG_INFO,
5519 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5520 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521 iface->cross_connect_in_use = 0;
5522 }
5523}
5524
5525
5526void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5527{
5528 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5529 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5530 wpa_s->cross_connect_disallowed)
5531 wpas_p2p_disable_cross_connect(wpa_s);
5532 else
5533 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005534 if (!wpa_s->ap_iface &&
5535 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5536 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005537}
5538
5539
5540void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5541{
5542 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005543 if (!wpa_s->ap_iface &&
5544 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5545 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005546 wpas_p2p_set_group_idle_timeout(wpa_s);
5547}
5548
5549
5550static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5551{
5552 struct wpa_supplicant *iface;
5553
5554 if (!wpa_s->global->cross_connection)
5555 return;
5556
5557 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5558 if (iface == wpa_s)
5559 continue;
5560 if (iface->drv_flags &
5561 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5562 continue;
5563 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5564 continue;
5565
5566 wpa_s->cross_connect_enabled = 1;
5567 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5568 sizeof(wpa_s->cross_connect_uplink));
5569 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5570 "%s to %s whenever uplink is available",
5571 wpa_s->ifname, wpa_s->cross_connect_uplink);
5572
5573 if (iface->ap_iface || iface->current_ssid == NULL ||
5574 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5575 iface->cross_connect_disallowed ||
5576 iface->wpa_state != WPA_COMPLETED)
5577 break;
5578
5579 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005580 wpa_msg_global(wpa_s->parent, MSG_INFO,
5581 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5582 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005583 break;
5584 }
5585}
5586
5587
5588int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5589{
5590 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5591 !wpa_s->p2p_in_provisioning)
5592 return 0; /* not P2P client operation */
5593
5594 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5595 "session overlap");
5596 if (wpa_s != wpa_s->parent)
5597 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5598
5599 if (wpa_s->global->p2p)
5600 p2p_group_formation_failed(wpa_s->global->p2p);
5601
5602 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5603 wpa_s->parent, NULL);
5604
5605 wpas_group_formation_completed(wpa_s, 0);
5606 return 1;
5607}
5608
5609
5610void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5611{
5612 struct p2p_channels chan;
5613
5614 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5615 return;
5616
5617 os_memset(&chan, 0, sizeof(chan));
5618 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5619 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5620 "channel list");
5621 return;
5622 }
5623
5624 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5625}
5626
5627
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005628static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5629 struct wpa_scan_results *scan_res)
5630{
5631 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5632}
5633
5634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5636{
5637 struct wpa_global *global = wpa_s->global;
5638 int found = 0;
5639 const u8 *peer;
5640
5641 if (global->p2p == NULL)
5642 return -1;
5643
5644 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5645
5646 if (wpa_s->pending_interface_name[0] &&
5647 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5648 found = 1;
5649
5650 peer = p2p_get_go_neg_peer(global->p2p);
5651 if (peer) {
5652 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5653 MACSTR, MAC2STR(peer));
5654 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005655 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005656 }
5657
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005658 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5659 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5660 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5661 found = 1;
5662 }
5663
5664 if (wpa_s->pending_pd_before_join) {
5665 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5666 wpa_s->pending_pd_before_join = 0;
5667 found = 1;
5668 }
5669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005670 wpas_p2p_stop_find(wpa_s);
5671
5672 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5673 if (wpa_s == global->p2p_group_formation &&
5674 (wpa_s->p2p_in_provisioning ||
5675 wpa_s->parent->pending_interface_type ==
5676 WPA_IF_P2P_CLIENT)) {
5677 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5678 "formation found - cancelling",
5679 wpa_s->ifname);
5680 found = 1;
5681 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5682 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005683 if (wpa_s->p2p_in_provisioning) {
5684 wpas_group_formation_completed(wpa_s, 0);
5685 break;
5686 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005687 wpas_p2p_group_delete(wpa_s,
5688 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005689 break;
5690 }
5691 }
5692
5693 if (!found) {
5694 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5695 return -1;
5696 }
5697
5698 return 0;
5699}
5700
5701
5702void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5703{
5704 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5705 return;
5706
5707 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5708 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005709 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005710}
5711
5712
5713void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5714 int freq_24, int freq_5, int freq_overall)
5715{
5716 struct p2p_data *p2p = wpa_s->global->p2p;
5717 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5718 return;
5719 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5720}
5721
5722
5723int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5724{
5725 u8 peer[ETH_ALEN];
5726 struct p2p_data *p2p = wpa_s->global->p2p;
5727
5728 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5729 return -1;
5730
5731 if (hwaddr_aton(addr, peer))
5732 return -1;
5733
5734 return p2p_unauthorize(p2p, peer);
5735}
5736
5737
5738/**
5739 * wpas_p2p_disconnect - Disconnect from a P2P Group
5740 * @wpa_s: Pointer to wpa_supplicant data
5741 * Returns: 0 on success, -1 on failure
5742 *
5743 * This can be used to disconnect from a group in which the local end is a P2P
5744 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5745 * virtual network interface was created for this group, that interface will be
5746 * removed. Otherwise, only the configured P2P group network will be removed
5747 * from the interface.
5748 */
5749int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5750{
5751
5752 if (wpa_s == NULL)
5753 return -1;
5754
Jouni Malinen2b89da82012-08-31 22:04:41 +03005755 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5756 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005757}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005758
5759
5760int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5761{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005762 int ret;
5763
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005764 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5765 return 0;
5766
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005767 ret = p2p_in_progress(wpa_s->global->p2p);
5768 if (ret == 0) {
5769 /*
5770 * Check whether there is an ongoing WPS provisioning step (or
5771 * other parts of group formation) on another interface since
5772 * p2p_in_progress() does not report this to avoid issues for
5773 * scans during such provisioning step.
5774 */
5775 if (wpa_s->global->p2p_group_formation &&
5776 wpa_s->global->p2p_group_formation != wpa_s) {
5777 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5778 "in group formation",
5779 wpa_s->global->p2p_group_formation->ifname);
5780 ret = 1;
5781 }
5782 }
5783
5784 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005785}
5786
5787
5788void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5789 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005790{
5791 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5792 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5793 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005794 /**
5795 * Remove the network by scheduling the group formation
5796 * timeout to happen immediately. The teardown code
5797 * needs to be scheduled to run asynch later so that we
5798 * don't delete data from under ourselves unexpectedly.
5799 * Calling wpas_p2p_group_formation_timeout directly
5800 * causes a series of crashes in WPS failure scenarios.
5801 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005802 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5803 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005804 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5805 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005806 }
5807}
5808
5809
5810struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005811 const u8 *addr, const u8 *ssid,
5812 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005813{
5814 struct wpa_ssid *s;
5815 size_t i;
5816
5817 for (s = wpa_s->conf->ssid; s; s = s->next) {
5818 if (s->disabled != 2)
5819 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005820 if (ssid &&
5821 (ssid_len != s->ssid_len ||
5822 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5823 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005824 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5825 return s; /* peer is GO in the persistent group */
5826 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5827 continue;
5828 for (i = 0; i < s->num_p2p_clients; i++) {
5829 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5830 addr, ETH_ALEN) == 0)
5831 return s; /* peer is P2P client in persistent
5832 * group */
5833 }
5834 }
5835
5836 return NULL;
5837}
5838
5839
5840void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5841 const u8 *addr)
5842{
5843 if (addr == NULL)
5844 return;
5845 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5846}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005847
Dmitry Shmidt04949592012-07-19 12:16:46 -07005848
5849static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5850 int group_added)
5851{
5852 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005853 if (wpa_s->global->p2p_group_formation)
5854 group = wpa_s->global->p2p_group_formation;
5855 wpa_s = wpa_s->parent;
5856 offchannel_send_action_done(wpa_s);
5857 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005858 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005859 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5860 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5861 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5862 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5863 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005864 wpa_s->p2p_pd_before_go_neg,
5865 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005866}
5867
5868
5869int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5870{
5871 if (!wpa_s->p2p_fallback_to_go_neg ||
5872 wpa_s->p2p_in_provisioning <= 5)
5873 return 0;
5874
5875 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5876 return 0; /* peer operating as a GO */
5877
5878 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5879 "fallback to GO Negotiation");
5880 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5881
5882 return 1;
5883}
5884
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005885
5886unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5887{
5888 const char *rn, *rn2;
5889 struct wpa_supplicant *ifs;
5890
5891 if (wpa_s->wpa_state > WPA_SCANNING) {
5892 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5893 "concurrent operation",
5894 P2P_CONCURRENT_SEARCH_DELAY);
5895 return P2P_CONCURRENT_SEARCH_DELAY;
5896 }
5897
5898 if (!wpa_s->driver->get_radio_name)
5899 return 0;
5900 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5901 if (rn == NULL || rn[0] == '\0')
5902 return 0;
5903
5904 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5905 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5906 continue;
5907
5908 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5909 if (!rn2 || os_strcmp(rn, rn2) != 0)
5910 continue;
5911 if (ifs->wpa_state > WPA_SCANNING) {
5912 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5913 "delay due to concurrent operation on "
5914 "interface %s",
5915 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5916 return P2P_CONCURRENT_SEARCH_DELAY;
5917 }
5918 }
5919
5920 return 0;
5921}
5922
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005923
5924void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5925{
5926 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5927 "pending anymore (sta_scan_pending=%d "
5928 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5929 wpa_s->global->p2p_cb_on_scan_complete);
5930 wpa_s->sta_scan_pending = 0;
5931
5932 if (!wpa_s->global->p2p_cb_on_scan_complete)
5933 return;
5934 wpa_s->global->p2p_cb_on_scan_complete = 0;
5935
5936 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5937 return;
5938
5939 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
5940 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
5941 "continued after successful connection");
5942 p2p_increase_search_delay(wpa_s->global->p2p,
5943 wpas_p2p_search_delay(wpa_s));
5944 }
5945}
5946
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005947#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07005948static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
5949{
5950 struct wpa_supplicant *wpa_s = eloop_ctx;
5951
5952 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
5953 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
5954}
5955
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005956int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5957 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005958{
5959 struct wpa_supplicant *iface = NULL;
5960 struct p2p_data *p2p = wpa_s->global->p2p;
5961
5962 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005963 if ((iface->current_ssid) &&
5964 (iface->current_ssid->frequency != freq) &&
5965 ((iface->p2p_group_interface) ||
5966 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005967
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005968 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5969 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5970 /* Try to see whether we can move the GO. If it
5971 * is not possible, remove the GO interface
5972 */
5973 if (wpa_drv_switch_channel(iface, freq) == 0) {
5974 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5975 iface->current_ssid->frequency = freq;
5976 continue;
5977 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005978 }
5979
5980 /* If GO cannot be moved or if the conflicting interface is a
5981 * P2P Client, remove the interface depending up on the connection
5982 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005983 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005984 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005985 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005986 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005987 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07005988 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
5989 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005990 /* If connection in progress is p2p connection, do not proceed for the connection */
5991 if (wpa_s == iface)
5992 return -1;
5993 else
5994 /* If connection in progress is STA connection, proceed for the connection */
5995 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005996 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005997 /* P2p connection has priority, disable the STA network*/
5998 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5999 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6000 " id=%d", ssid->id);
6001 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6002 if (wpa_s == iface) {
6003 /* p2p connection is in progress, continue connecting...*/
6004 return 0;
6005 }
6006 else {
6007 /* STA connection is in progress, do not allow to continue */
6008 return -1;
6009 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006010 }
6011 }
6012 }
6013 return 0;
6014}
6015#endif