blob: 7c19d0cc071457e52d9d130ff4271ad390759932 [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 Shmidt8d520ff2011-05-09 14:06:53 -07002430static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2431 const u8 *go_dev_addr, const u8 *ssid,
2432 size_t ssid_len, int *go, u8 *group_bssid,
2433 int *force_freq, int persistent_group)
2434{
2435 struct wpa_supplicant *wpa_s = ctx;
2436 struct wpa_ssid *s;
2437 u8 cur_bssid[ETH_ALEN];
2438 int res;
2439 struct wpa_supplicant *grp;
2440
2441 if (!persistent_group) {
2442 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2443 " to join an active group", MAC2STR(sa));
2444 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2445 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2446 == 0 ||
2447 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2448 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2449 "authorized invitation");
2450 goto accept_inv;
2451 }
2452 /*
2453 * Do not accept the invitation automatically; notify user and
2454 * request approval.
2455 */
2456 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2457 }
2458
2459 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2460 if (grp) {
2461 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2462 "running persistent group");
2463 if (*go)
2464 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2465 goto accept_inv;
2466 }
2467
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002468 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2469 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2470 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2471 "invitation to re-invoke a persistent group");
2472 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2474
2475 for (s = wpa_s->conf->ssid; s; s = s->next) {
2476 if (s->disabled == 2 &&
2477 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2478 s->ssid_len == ssid_len &&
2479 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2480 break;
2481 }
2482
2483 if (!s) {
2484 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2485 " requested reinvocation of an unknown group",
2486 MAC2STR(sa));
2487 return P2P_SC_FAIL_UNKNOWN_GROUP;
2488 }
2489
2490 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2491 *go = 1;
2492 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2493 wpa_printf(MSG_DEBUG, "P2P: The only available "
2494 "interface is already in use - reject "
2495 "invitation");
2496 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2497 }
2498 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2499 } else if (s->mode == WPAS_MODE_P2P_GO) {
2500 *go = 1;
2501 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2502 {
2503 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2504 "interface address for the group");
2505 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2506 }
2507 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2508 ETH_ALEN);
2509 }
2510
2511accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002512 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2515 wpa_s->assoc_freq) {
2516 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2517 "the channel we are already using");
2518 *force_freq = wpa_s->assoc_freq;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002519 wpas_p2p_set_own_freq_preference(wpa_s, wpa_s->assoc_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 }
2521
2522 res = wpa_drv_shared_freq(wpa_s);
2523 if (res > 0) {
2524 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2525 "with the channel we are already using on a "
2526 "shared interface");
2527 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002528 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529 }
2530
2531 return P2P_SC_SUCCESS;
2532}
2533
2534
2535static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2536 const u8 *ssid, size_t ssid_len,
2537 const u8 *go_dev_addr, u8 status,
2538 int op_freq)
2539{
2540 struct wpa_supplicant *wpa_s = ctx;
2541 struct wpa_ssid *s;
2542
2543 for (s = wpa_s->conf->ssid; s; s = s->next) {
2544 if (s->disabled == 2 &&
2545 s->ssid_len == ssid_len &&
2546 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2547 break;
2548 }
2549
2550 if (status == P2P_SC_SUCCESS) {
2551 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2552 " was accepted; op_freq=%d MHz",
2553 MAC2STR(sa), op_freq);
2554 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002555 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002557 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002559 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002560 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002561 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562 }
2563 return;
2564 }
2565
2566 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2567 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2568 " was rejected (status %u)", MAC2STR(sa), status);
2569 return;
2570 }
2571
2572 if (!s) {
2573 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002574 wpa_msg_global(wpa_s, MSG_INFO,
2575 P2P_EVENT_INVITATION_RECEIVED
2576 "sa=" MACSTR " go_dev_addr=" MACSTR
2577 " bssid=" MACSTR " unknown-network",
2578 MAC2STR(sa), MAC2STR(go_dev_addr),
2579 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002581 wpa_msg_global(wpa_s, MSG_INFO,
2582 P2P_EVENT_INVITATION_RECEIVED
2583 "sa=" MACSTR " go_dev_addr=" MACSTR
2584 " unknown-network",
2585 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002586 }
2587 return;
2588 }
2589
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002590 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002591 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2592 "sa=" MACSTR " persistent=%d freq=%d",
2593 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002594 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002595 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2596 "sa=" MACSTR " persistent=%d",
2597 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002598 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002599}
2600
2601
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002602static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2603 struct wpa_ssid *ssid,
2604 const u8 *peer)
2605{
2606 size_t i;
2607
2608 if (ssid == NULL)
2609 return;
2610
2611 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2612 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2613 ETH_ALEN) == 0)
2614 break;
2615 }
2616 if (i >= ssid->num_p2p_clients) {
2617 if (ssid->mode != WPAS_MODE_P2P_GO &&
2618 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2619 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2620 "due to invitation result", ssid->id);
2621 wpas_notify_network_removed(wpa_s, ssid);
2622 wpa_config_remove_network(wpa_s->conf, ssid->id);
2623 return;
2624 }
2625 return; /* Peer not found in client list */
2626 }
2627
2628 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2629 "group %d client list due to invitation result",
2630 MAC2STR(peer), ssid->id);
2631 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2632 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2633 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2634 ssid->num_p2p_clients--;
2635#ifndef CONFIG_NO_CONFIG_WRITE
2636 if (wpa_s->parent->conf->update_config &&
2637 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2638 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2639#endif /* CONFIG_NO_CONFIG_WRITE */
2640}
2641
2642
2643static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2644 const u8 *peer)
2645{
2646 struct wpa_ssid *ssid;
2647
2648 wpa_s = wpa_s->global->p2p_invite_group;
2649 if (wpa_s == NULL)
2650 return; /* No known invitation group */
2651 ssid = wpa_s->current_ssid;
2652 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2653 !ssid->p2p_persistent_group)
2654 return; /* Not operating as a GO in persistent group */
2655 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2656 ssid->ssid, ssid->ssid_len);
2657 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2658}
2659
2660
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002661static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002662 const struct p2p_channels *channels,
2663 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664{
2665 struct wpa_supplicant *wpa_s = ctx;
2666 struct wpa_ssid *ssid;
2667
2668 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002669 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2670 "status=%d " MACSTR,
2671 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002672 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002673 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2674 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675 }
2676 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2677
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002678 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2679 status, MAC2STR(peer));
2680 if (wpa_s->pending_invite_ssid_id == -1) {
2681 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2682 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002684 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002686 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2687 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2688 "invitation exchange to indicate readiness for "
2689 "re-invocation");
2690 }
2691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002693 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2694 ssid = wpa_config_get_network(
2695 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2696 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2697 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002698 wpas_p2p_remove_pending_group_interface(wpa_s);
2699 return;
2700 }
2701
2702 ssid = wpa_config_get_network(wpa_s->conf,
2703 wpa_s->pending_invite_ssid_id);
2704 if (ssid == NULL) {
2705 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2706 "data matching with invitation");
2707 return;
2708 }
2709
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002710 /*
2711 * The peer could have missed our ctrl::ack frame for Invitation
2712 * Response and continue retransmitting the frame. To reduce the
2713 * likelihood of the peer not getting successful TX status for the
2714 * Invitation Response frame, wait a short time here before starting
2715 * the persistent group so that we will remain on the current channel to
2716 * acknowledge any possible retransmission from the peer.
2717 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002718#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002719 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2720 "starting persistent group");
2721 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002722#else
2723 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2724 "starting persistent group");
2725 os_sleep(0, 100000);
2726#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002728 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002729 ssid->mode == WPAS_MODE_P2P_GO,
2730 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002731 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732}
2733
2734
Dmitry Shmidt04949592012-07-19 12:16:46 -07002735static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2736 unsigned int freq)
2737{
2738 unsigned int i;
2739
2740 if (global->p2p_disallow_freq == NULL)
2741 return 0;
2742
2743 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2744 if (freq >= global->p2p_disallow_freq[i].min &&
2745 freq <= global->p2p_disallow_freq[i].max)
2746 return 1;
2747 }
2748
2749 return 0;
2750}
2751
2752
2753static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2754{
2755 reg->channel[reg->channels] = chan;
2756 reg->channels++;
2757}
2758
2759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2761 struct p2p_channels *chan)
2762{
2763 int i, cla = 0;
2764
2765 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2766 "band");
2767
2768 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2769 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002770 chan->reg_class[cla].channels = 0;
2771 for (i = 0; i < 11; i++) {
2772 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2773 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2774 }
2775 if (chan->reg_class[cla].channels)
2776 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777
2778 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2779 "band");
2780
2781 /* Operating class 115 - 5 GHz, channels 36-48 */
2782 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002783 chan->reg_class[cla].channels = 0;
2784 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2785 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2786 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2787 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2788 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2789 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2790 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2791 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2792 if (chan->reg_class[cla].channels)
2793 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794
2795 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2796 "band");
2797
2798 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2799 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002800 chan->reg_class[cla].channels = 0;
2801 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2802 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2803 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2804 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2805 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2806 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2807 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2808 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2809 if (chan->reg_class[cla].channels)
2810 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811
2812 chan->reg_classes = cla;
2813 return 0;
2814}
2815
2816
2817static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2818 u16 num_modes,
2819 enum hostapd_hw_mode mode)
2820{
2821 u16 i;
2822
2823 for (i = 0; i < num_modes; i++) {
2824 if (modes[i].mode == mode)
2825 return &modes[i];
2826 }
2827
2828 return NULL;
2829}
2830
2831
Dmitry Shmidt04949592012-07-19 12:16:46 -07002832static int has_channel(struct wpa_global *global,
2833 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834{
2835 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002836 unsigned int freq;
2837
2838 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2839 chan * 5;
2840 if (wpas_p2p_disallowed_freq(global, freq))
2841 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842
2843 for (i = 0; i < mode->num_channels; i++) {
2844 if (mode->channels[i].chan == chan) {
2845 if (flags)
2846 *flags = mode->channels[i].flag;
2847 return !(mode->channels[i].flag &
2848 (HOSTAPD_CHAN_DISABLED |
2849 HOSTAPD_CHAN_PASSIVE_SCAN |
2850 HOSTAPD_CHAN_NO_IBSS |
2851 HOSTAPD_CHAN_RADAR));
2852 }
2853 }
2854
2855 return 0;
2856}
2857
2858
2859struct p2p_oper_class_map {
2860 enum hostapd_hw_mode mode;
2861 u8 op_class;
2862 u8 min_chan;
2863 u8 max_chan;
2864 u8 inc;
2865 enum { BW20, BW40PLUS, BW40MINUS } bw;
2866};
2867
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002868static struct p2p_oper_class_map op_class[] = {
2869 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002870#if 0 /* Do not enable HT40 on 2 GHz for now */
2871 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2872 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2873#endif
2874 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2875 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2876 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2877 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2878 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2879 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2880 { -1, 0, 0, 0, 0, BW20 }
2881};
2882
2883
2884static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2885 struct hostapd_hw_modes *mode,
2886 u8 channel, u8 bw)
2887{
2888 int flag;
2889
2890 if (!has_channel(wpa_s->global, mode, channel, &flag))
2891 return -1;
2892 if (bw == BW40MINUS &&
2893 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2894 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2895 return 0;
2896 if (bw == BW40PLUS &&
2897 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2898 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2899 return 0;
2900 return 1;
2901}
2902
2903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2905 struct p2p_channels *chan)
2906{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002907 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002910 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2912 "of all supported channels; assume dualband "
2913 "support");
2914 return wpas_p2p_default_channels(wpa_s, chan);
2915 }
2916
2917 cla = 0;
2918
2919 for (op = 0; op_class[op].op_class; op++) {
2920 struct p2p_oper_class_map *o = &op_class[op];
2921 u8 ch;
2922 struct p2p_reg_class *reg = NULL;
2923
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002924 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925 if (mode == NULL)
2926 continue;
2927 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002928 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 continue;
2930 if (reg == NULL) {
2931 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2932 "class %u", o->op_class);
2933 reg = &chan->reg_class[cla];
2934 cla++;
2935 reg->reg_class = o->op_class;
2936 }
2937 reg->channel[reg->channels] = ch;
2938 reg->channels++;
2939 }
2940 if (reg) {
2941 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2942 reg->channel, reg->channels);
2943 }
2944 }
2945
2946 chan->reg_classes = cla;
2947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002948 return 0;
2949}
2950
2951
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002952int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2953 struct hostapd_hw_modes *mode, u8 channel)
2954{
2955 int op, ret;
2956
2957 for (op = 0; op_class[op].op_class; op++) {
2958 struct p2p_oper_class_map *o = &op_class[op];
2959 u8 ch;
2960
2961 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2962 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2963 o->bw == BW20 || ch != channel)
2964 continue;
2965 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2966 if (ret < 0)
2967 continue;
2968 else if (ret > 0)
2969 return (o->bw == BW40MINUS) ? -1 : 1;
2970 else
2971 return 0;
2972 }
2973 }
2974 return 0;
2975}
2976
2977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2979 size_t buf_len)
2980{
2981 struct wpa_supplicant *wpa_s = ctx;
2982
2983 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2984 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2985 break;
2986 }
2987 if (wpa_s == NULL)
2988 return -1;
2989
2990 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2991}
2992
2993
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002994static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2995{
2996 struct wpa_supplicant *wpa_s = ctx;
2997
2998 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2999 struct wpa_ssid *ssid = wpa_s->current_ssid;
3000 if (ssid == NULL)
3001 continue;
3002 if (ssid->mode != WPAS_MODE_INFRA)
3003 continue;
3004 if (wpa_s->wpa_state != WPA_COMPLETED &&
3005 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3006 continue;
3007 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3008 return 1;
3009 }
3010
3011 return 0;
3012}
3013
3014
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003015static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3016{
3017 struct wpa_supplicant *wpa_s = ctx;
3018 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3019}
3020
3021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022/**
3023 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3024 * @global: Pointer to global data from wpa_supplicant_init()
3025 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3026 * Returns: 0 on success, -1 on failure
3027 */
3028int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3029{
3030 struct p2p_config p2p;
3031 unsigned int r;
3032 int i;
3033
3034 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3035 return 0;
3036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 if (global->p2p)
3038 return 0;
3039
3040 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3041 struct p2p_params params;
3042
3043 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3044 os_memset(&params, 0, sizeof(params));
3045 params.dev_name = wpa_s->conf->device_name;
3046 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3047 WPS_DEV_TYPE_LEN);
3048 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3049 os_memcpy(params.sec_dev_type,
3050 wpa_s->conf->sec_device_type,
3051 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3052
3053 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3054 return -1;
3055
3056 return 0;
3057 }
3058
3059 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003061 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003062 p2p.p2p_scan = wpas_p2p_scan;
3063 p2p.send_action = wpas_send_action;
3064 p2p.send_action_done = wpas_send_action_done;
3065 p2p.go_neg_completed = wpas_go_neg_completed;
3066 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3067 p2p.dev_found = wpas_dev_found;
3068 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003069 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070 p2p.start_listen = wpas_start_listen;
3071 p2p.stop_listen = wpas_stop_listen;
3072 p2p.send_probe_resp = wpas_send_probe_resp;
3073 p2p.sd_request = wpas_sd_request;
3074 p2p.sd_response = wpas_sd_response;
3075 p2p.prov_disc_req = wpas_prov_disc_req;
3076 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003077 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 p2p.invitation_process = wpas_invitation_process;
3079 p2p.invitation_received = wpas_invitation_received;
3080 p2p.invitation_result = wpas_invitation_result;
3081 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003082 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083
3084 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003085 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086 p2p.dev_name = wpa_s->conf->device_name;
3087 p2p.manufacturer = wpa_s->conf->manufacturer;
3088 p2p.model_name = wpa_s->conf->model_name;
3089 p2p.model_number = wpa_s->conf->model_number;
3090 p2p.serial_number = wpa_s->conf->serial_number;
3091 if (wpa_s->wps) {
3092 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3093 p2p.config_methods = wpa_s->wps->config_methods;
3094 }
3095
3096 if (wpa_s->conf->p2p_listen_reg_class &&
3097 wpa_s->conf->p2p_listen_channel) {
3098 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3099 p2p.channel = wpa_s->conf->p2p_listen_channel;
3100 } else {
3101 p2p.reg_class = 81;
3102 /*
3103 * Pick one of the social channels randomly as the listen
3104 * channel.
3105 */
3106 os_get_random((u8 *) &r, sizeof(r));
3107 p2p.channel = 1 + (r % 3) * 5;
3108 }
3109 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3110
3111 if (wpa_s->conf->p2p_oper_reg_class &&
3112 wpa_s->conf->p2p_oper_channel) {
3113 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3114 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3115 p2p.cfg_op_channel = 1;
3116 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3117 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3118
3119 } else {
3120 p2p.op_reg_class = 81;
3121 /*
3122 * Use random operation channel from (1, 6, 11) if no other
3123 * preference is indicated.
3124 */
3125 os_get_random((u8 *) &r, sizeof(r));
3126 p2p.op_channel = 1 + (r % 3) * 5;
3127 p2p.cfg_op_channel = 0;
3128 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3129 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3130 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003131
3132 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3133 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3134 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3135 }
3136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003137 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3138 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3139 p2p.country[2] = 0x04;
3140 } else
3141 os_memcpy(p2p.country, "XX\x04", 3);
3142
3143 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3144 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3145 "channel list");
3146 return -1;
3147 }
3148
3149 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3150 WPS_DEV_TYPE_LEN);
3151
3152 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3153 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3154 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3155
3156 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3157 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3158
3159 p2p.max_peers = 100;
3160
3161 if (wpa_s->conf->p2p_ssid_postfix) {
3162 p2p.ssid_postfix_len =
3163 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3164 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3165 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3166 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3167 p2p.ssid_postfix_len);
3168 }
3169
3170 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3171
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003172 p2p.max_listen = wpa_s->max_remain_on_chan;
3173
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003174#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003175 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003176 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003177 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3178 } else {
3179 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003180 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003181 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3182 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003183#endif
3184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003185 global->p2p = p2p_init(&p2p);
3186 if (global->p2p == NULL)
3187 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003188 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003189
3190 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3191 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3192 continue;
3193 p2p_add_wps_vendor_extension(
3194 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3195 }
3196
3197 return 0;
3198}
3199
3200
3201/**
3202 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3203 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3204 *
3205 * This function deinitialize per-interface P2P data.
3206 */
3207void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3208{
3209 if (wpa_s->driver && wpa_s->drv_priv)
3210 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003211
3212 if (wpa_s->go_params) {
3213 /* Clear any stored provisioning info */
3214 p2p_clear_provisioning_info(
3215 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003216 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003217 }
3218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219 os_free(wpa_s->go_params);
3220 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003221#ifdef ANDROID_P2P
3222 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3223#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3225 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3226 wpa_s->p2p_long_listen = 0;
3227 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3228 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3229 wpas_p2p_remove_pending_group_interface(wpa_s);
3230
3231 /* TODO: remove group interface from the driver if this wpa_s instance
3232 * is on top of a P2P group interface */
3233}
3234
3235
3236/**
3237 * wpas_p2p_deinit_global - Deinitialize global P2P module
3238 * @global: Pointer to global data from wpa_supplicant_init()
3239 *
3240 * This function deinitializes the global (per device) P2P module.
3241 */
3242void wpas_p2p_deinit_global(struct wpa_global *global)
3243{
3244 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003246 wpa_s = global->ifaces;
3247 if (wpa_s)
3248 wpas_p2p_service_flush(wpa_s);
3249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003250 if (global->p2p == NULL)
3251 return;
3252
3253 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003254 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3255 wpa_s = wpa_s->next;
3256 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 tmp = global->ifaces;
3258 while (tmp &&
3259 (tmp == wpa_s ||
3260 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3261 tmp = tmp->next;
3262 }
3263 if (tmp == NULL)
3264 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003265 /* Disconnect from the P2P group and deinit the interface */
3266 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267 }
3268
3269 /*
3270 * Deinit GO data on any possibly remaining interface (if main
3271 * interface is used as GO).
3272 */
3273 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3274 if (wpa_s->ap_iface)
3275 wpas_p2p_group_deinit(wpa_s);
3276 }
3277
3278 p2p_deinit(global->p2p);
3279 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003280 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003281}
3282
3283
3284static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3285{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003286 if (wpa_s->conf->p2p_no_group_iface)
3287 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288 if (wpa_s->drv_flags &
3289 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3290 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3291 return 1; /* P2P group requires a new interface in every case
3292 */
3293 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3294 return 0; /* driver does not support concurrent operations */
3295 if (wpa_s->global->ifaces->next)
3296 return 1; /* more that one interface already in use */
3297 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3298 return 1; /* this interface is already in use */
3299 return 0;
3300}
3301
3302
3303static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3304 const u8 *peer_addr,
3305 enum p2p_wps_method wps_method,
3306 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003307 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003308 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003310 if (persistent_group && wpa_s->conf->persistent_reconnect)
3311 persistent_group = 2;
3312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3314 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3315 go_intent, own_interface_addr,
3316 force_freq, persistent_group);
3317 }
3318
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003319 /*
3320 * Increase GO config timeout if HT40 is used since it takes some time
3321 * to scan channels for coex purposes before the BSS can be started.
3322 */
3323 p2p_set_config_timeout(wpa_s->global->p2p,
3324 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3327 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003328 persistent_group, ssid ? ssid->ssid : NULL,
3329 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003330 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331}
3332
3333
3334static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3335 const u8 *peer_addr,
3336 enum p2p_wps_method wps_method,
3337 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003338 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003339 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003341 if (persistent_group && wpa_s->conf->persistent_reconnect)
3342 persistent_group = 2;
3343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003344 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3345 return -1;
3346
3347 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3348 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003349 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003350 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003351}
3352
3353
3354static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3355{
3356 wpa_s->p2p_join_scan_count++;
3357 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3358 wpa_s->p2p_join_scan_count);
3359 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3360 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3361 " for join operationg - stop join attempt",
3362 MAC2STR(wpa_s->pending_join_iface_addr));
3363 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003364 if (wpa_s->p2p_auto_pd) {
3365 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003366 wpa_msg_global(wpa_s, MSG_INFO,
3367 P2P_EVENT_PROV_DISC_FAILURE
3368 " p2p_dev_addr=" MACSTR " status=N/A",
3369 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003370 return;
3371 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003372 wpa_msg_global(wpa_s->parent, MSG_INFO,
3373 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374 }
3375}
3376
3377
Dmitry Shmidt04949592012-07-19 12:16:46 -07003378static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3379{
3380 struct wpa_supplicant *iface;
3381 int shared_freq;
3382 u8 bssid[ETH_ALEN];
3383
3384 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3385 return 0;
3386
3387 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3388 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3389 continue;
3390 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3391 continue;
3392 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3393 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3394 shared_freq = iface->current_ssid->frequency;
3395 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3396 shared_freq = iface->assoc_freq;
3397 else
3398 shared_freq = 0;
3399
3400 if (shared_freq && freq != shared_freq) {
3401 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3402 "connected on %d MHz - new connection on "
3403 "%d MHz", iface->ifname, shared_freq, freq);
3404 return 1;
3405 }
3406 }
3407
3408 shared_freq = wpa_drv_shared_freq(wpa_s);
3409 if (shared_freq > 0 && shared_freq != freq) {
3410 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3411 "virtual interface connected on %d MHz - new "
3412 "connection on %d MHz", shared_freq, freq);
3413 return 1;
3414 }
3415
3416 return 0;
3417}
3418
3419
3420static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3421 const u8 *peer_dev_addr)
3422{
3423 struct wpa_bss *bss;
3424 int updated;
3425
3426 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3427 if (bss == NULL)
3428 return -1;
3429 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3430 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3431 "last scan");
3432 return 0;
3433 }
3434
3435 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3436 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3437 "%ld.%06ld (%supdated in last scan)",
3438 bss->last_update.sec, bss->last_update.usec,
3439 updated ? "": "not ");
3440
3441 return updated;
3442}
3443
3444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003445static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3446 struct wpa_scan_results *scan_res)
3447{
3448 struct wpa_bss *bss;
3449 int freq;
3450 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003452 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3453
3454 if (wpa_s->global->p2p_disabled)
3455 return;
3456
Dmitry Shmidt04949592012-07-19 12:16:46 -07003457 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3458 scan_res ? (int) scan_res->num : -1,
3459 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460
3461 if (scan_res)
3462 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3463
Dmitry Shmidt04949592012-07-19 12:16:46 -07003464 if (wpa_s->p2p_auto_pd) {
3465 int join = wpas_p2p_peer_go(wpa_s,
3466 wpa_s->pending_join_dev_addr);
3467 if (join == 0 &&
3468 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3469 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003470 bss = wpa_bss_get_bssid_latest(
3471 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003472 if (bss) {
3473 freq = bss->freq;
3474 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3475 "the peer " MACSTR " at %d MHz",
3476 wpa_s->auto_pd_scan_retry,
3477 MAC2STR(wpa_s->
3478 pending_join_dev_addr),
3479 freq);
3480 wpas_p2p_join_scan_req(wpa_s, freq);
3481 return;
3482 }
3483 }
3484
3485 if (join < 0)
3486 join = 0;
3487
3488 wpa_s->p2p_auto_pd = 0;
3489 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3490 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3491 MAC2STR(wpa_s->pending_join_dev_addr), join);
3492 if (p2p_prov_disc_req(wpa_s->global->p2p,
3493 wpa_s->pending_join_dev_addr,
3494 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003495 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003496 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003497 wpa_msg_global(wpa_s, MSG_INFO,
3498 P2P_EVENT_PROV_DISC_FAILURE
3499 " p2p_dev_addr=" MACSTR " status=N/A",
3500 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003501 }
3502 return;
3503 }
3504
3505 if (wpa_s->p2p_auto_join) {
3506 int join = wpas_p2p_peer_go(wpa_s,
3507 wpa_s->pending_join_dev_addr);
3508 if (join < 0) {
3509 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3510 "running a GO -> use GO Negotiation");
3511 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3512 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3513 wpa_s->p2p_persistent_group, 0, 0, 0,
3514 wpa_s->p2p_go_intent,
3515 wpa_s->p2p_connect_freq,
3516 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003517 wpa_s->p2p_pd_before_go_neg,
3518 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003519 return;
3520 }
3521
3522 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3523 "try to join the group", join ? "" :
3524 " in older scan");
3525 if (!join)
3526 wpa_s->p2p_fallback_to_go_neg = 1;
3527 }
3528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3530 wpa_s->pending_join_iface_addr);
3531 if (freq < 0 &&
3532 p2p_get_interface_addr(wpa_s->global->p2p,
3533 wpa_s->pending_join_dev_addr,
3534 iface_addr) == 0 &&
3535 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3536 {
3537 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3538 "address for join from " MACSTR " to " MACSTR
3539 " based on newly discovered P2P peer entry",
3540 MAC2STR(wpa_s->pending_join_iface_addr),
3541 MAC2STR(iface_addr));
3542 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3543 ETH_ALEN);
3544
3545 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3546 wpa_s->pending_join_iface_addr);
3547 }
3548 if (freq >= 0) {
3549 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3550 "from P2P peer table: %d MHz", freq);
3551 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003552 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003553 if (bss) {
3554 freq = bss->freq;
3555 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3556 "from BSS table: %d MHz", freq);
3557 }
3558 if (freq > 0) {
3559 u16 method;
3560
Dmitry Shmidt04949592012-07-19 12:16:46 -07003561 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003562 wpa_msg_global(wpa_s->parent, MSG_INFO,
3563 P2P_EVENT_GROUP_FORMATION_FAILURE
3564 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003565 return;
3566 }
3567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003568 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3569 "prior to joining an existing group (GO " MACSTR
3570 " freq=%u MHz)",
3571 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3572 wpa_s->pending_pd_before_join = 1;
3573
3574 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575 case WPS_PIN_DISPLAY:
3576 method = WPS_CONFIG_KEYPAD;
3577 break;
3578 case WPS_PIN_KEYPAD:
3579 method = WPS_CONFIG_DISPLAY;
3580 break;
3581 case WPS_PBC:
3582 method = WPS_CONFIG_PUSHBUTTON;
3583 break;
3584 default:
3585 method = 0;
3586 break;
3587 }
3588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003589 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3590 wpa_s->pending_join_dev_addr) ==
3591 method)) {
3592 /*
3593 * We have already performed provision discovery for
3594 * joining the group. Proceed directly to join
3595 * operation without duplicated provision discovery. */
3596 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3597 "with " MACSTR " already done - proceed to "
3598 "join",
3599 MAC2STR(wpa_s->pending_join_dev_addr));
3600 wpa_s->pending_pd_before_join = 0;
3601 goto start;
3602 }
3603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003605 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003606 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3608 "Discovery Request before joining an "
3609 "existing group");
3610 wpa_s->pending_pd_before_join = 0;
3611 goto start;
3612 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613 return;
3614 }
3615
3616 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3617 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3618 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3619 wpas_p2p_check_join_scan_limit(wpa_s);
3620 return;
3621
3622start:
3623 /* Start join operation immediately */
3624 wpas_p2p_join_start(wpa_s);
3625}
3626
3627
Dmitry Shmidt04949592012-07-19 12:16:46 -07003628static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630 int ret;
3631 struct wpa_driver_scan_params params;
3632 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003633 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003634 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003635#ifdef ANDROID_P2P
3636 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003638 /* If freq is not provided, check the operating freq of the GO and do a
3639 * a directed scan to save time
3640 */
3641 if(!freq) {
3642 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3643 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3644 }
3645#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003646 os_memset(&params, 0, sizeof(params));
3647
3648 /* P2P Wildcard SSID */
3649 params.num_ssids = 1;
3650 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3651 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3652
3653 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003654 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3655 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3656 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003657 if (wps_ie == NULL) {
3658 wpas_p2p_scan_res_join(wpa_s, NULL);
3659 return;
3660 }
3661
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003662 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3663 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664 if (ies == NULL) {
3665 wpabuf_free(wps_ie);
3666 wpas_p2p_scan_res_join(wpa_s, NULL);
3667 return;
3668 }
3669 wpabuf_put_buf(ies, wps_ie);
3670 wpabuf_free(wps_ie);
3671
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003672 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003674 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675 params.extra_ies = wpabuf_head(ies);
3676 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003677 if (freq > 0) {
3678 freqs[0] = freq;
3679 params.freqs = freqs;
3680 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003681
3682 /*
3683 * Run a scan to update BSS table and start Provision Discovery once
3684 * the new scan results become available.
3685 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003686 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003687 if (!ret) {
3688 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003689 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003690 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691
3692 wpabuf_free(ies);
3693
3694 if (ret) {
3695 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3696 "try again later");
3697 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3698 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3699 wpas_p2p_check_join_scan_limit(wpa_s);
3700 }
3701}
3702
3703
Dmitry Shmidt04949592012-07-19 12:16:46 -07003704static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3705{
3706 struct wpa_supplicant *wpa_s = eloop_ctx;
3707 wpas_p2p_join_scan_req(wpa_s, 0);
3708}
3709
3710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003711static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003712 const u8 *dev_addr, enum p2p_wps_method wps_method,
3713 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714{
3715 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003716 MACSTR " dev " MACSTR ")%s",
3717 MAC2STR(iface_addr), MAC2STR(dev_addr),
3718 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719
Dmitry Shmidt04949592012-07-19 12:16:46 -07003720 wpa_s->p2p_auto_pd = 0;
3721 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003722 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3723 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3724 wpa_s->pending_join_wps_method = wps_method;
3725
3726 /* Make sure we are not running find during connection establishment */
3727 wpas_p2p_stop_find(wpa_s);
3728
3729 wpa_s->p2p_join_scan_count = 0;
3730 wpas_p2p_join_scan(wpa_s, NULL);
3731 return 0;
3732}
3733
3734
3735static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3736{
3737 struct wpa_supplicant *group;
3738 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003739 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740
3741 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3742 if (group == NULL)
3743 return -1;
3744 if (group != wpa_s) {
3745 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3746 sizeof(group->p2p_pin));
3747 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003748 } else {
3749 /*
3750 * Need to mark the current interface for p2p_group_formation
3751 * when a separate group interface is not used. This is needed
3752 * to allow p2p_cancel stop a pending p2p_connect-join.
3753 * wpas_p2p_init_group_interface() addresses this for the case
3754 * where a separate group interface is used.
3755 */
3756 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003757 }
3758
3759 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003760 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003761
3762 os_memset(&res, 0, sizeof(res));
3763 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3764 ETH_ALEN);
3765 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003766 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003767 if (bss) {
3768 res.freq = bss->freq;
3769 res.ssid_len = bss->ssid_len;
3770 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3771 }
3772
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003773 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3774 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3775 "starting client");
3776 wpa_drv_cancel_remain_on_channel(wpa_s);
3777 wpa_s->off_channel_freq = 0;
3778 wpa_s->roc_waiting_drv_freq = 0;
3779 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003780 wpas_start_wps_enrollee(group, &res);
3781
3782 /*
3783 * Allow a longer timeout for join-a-running-group than normal 15
3784 * second group formation timeout since the GO may not have authorized
3785 * our connection yet.
3786 */
3787 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3788 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3789 wpa_s, NULL);
3790
3791 return 0;
3792}
3793
3794
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003795static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3796 int *force_freq, int *pref_freq,
3797 int *oper_freq)
3798{
3799 if (freq > 0) {
3800 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3801 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3802 "(%u MHz) is not supported for P2P uses",
3803 freq);
3804 return -3;
3805 }
3806
3807 if (*oper_freq > 0 && freq != *oper_freq &&
3808 !(wpa_s->drv_flags &
3809 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3810 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3811 "on %u MHz while connected on another "
3812 "channel (%u MHz)", freq, *oper_freq);
3813 return -2;
3814 }
3815 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3816 "requested channel (%u MHz)", freq);
3817 *force_freq = freq;
3818 } else if (*oper_freq > 0 &&
3819 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3820 if (!(wpa_s->drv_flags &
3821 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3822 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3823 "while connected on non-P2P supported "
3824 "channel (%u MHz)", *oper_freq);
3825 return -2;
3826 }
3827 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3828 "(%u MHz) not available for P2P - try to use "
3829 "another channel", *oper_freq);
3830 *force_freq = 0;
3831 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3832 (wpa_s->drv_flags &
3833 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3834 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3835 "are already using (%u MHz) on another interface",
3836 *oper_freq);
3837 *pref_freq = *oper_freq;
3838 } else if (*oper_freq > 0) {
3839 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3840 "channel we are already using (%u MHz) on another "
3841 "interface", *oper_freq);
3842 *force_freq = *oper_freq;
3843 }
3844
3845 return 0;
3846}
3847
3848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849/**
3850 * wpas_p2p_connect - Request P2P Group Formation to be started
3851 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3852 * @peer_addr: Address of the peer P2P Device
3853 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3854 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003855 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856 * @join: Whether to join an existing group (as a client) instead of starting
3857 * Group Owner negotiation; @peer_addr is BSSID in that case
3858 * @auth: Whether to only authorize the connection instead of doing that and
3859 * initiating Group Owner negotiation
3860 * @go_intent: GO Intent or -1 to use default
3861 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003862 * @persistent_id: Persistent group credentials to use for forcing GO
3863 * parameters or -1 to generate new values (SSID/passphrase)
3864 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3865 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003866 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3868 * failure, -2 on failure due to channel not currently available,
3869 * -3 if forced channel is not supported
3870 */
3871int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3872 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003873 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003874 int go_intent, int freq, int persistent_id, int pd,
3875 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003877 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 u8 bssid[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003879 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003880 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003881 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003882 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003883
3884 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3885 return -1;
3886
Dmitry Shmidt04949592012-07-19 12:16:46 -07003887 if (persistent_id >= 0) {
3888 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3889 if (ssid == NULL || ssid->disabled != 2 ||
3890 ssid->mode != WPAS_MODE_P2P_GO)
3891 return -1;
3892 }
3893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894 if (go_intent < 0)
3895 go_intent = wpa_s->conf->p2p_go_intent;
3896
3897 if (!auth)
3898 wpa_s->p2p_long_listen = 0;
3899
3900 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003901 wpa_s->p2p_persistent_group = !!persistent_group;
3902 wpa_s->p2p_persistent_id = persistent_id;
3903 wpa_s->p2p_go_intent = go_intent;
3904 wpa_s->p2p_connect_freq = freq;
3905 wpa_s->p2p_fallback_to_go_neg = 0;
3906 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003907 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003908
3909 if (pin)
3910 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3911 else if (wps_method == WPS_PIN_DISPLAY) {
3912 ret = wps_generate_pin();
3913 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3914 ret);
3915 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3916 wpa_s->p2p_pin);
3917 } else
3918 wpa_s->p2p_pin[0] = '\0';
3919
Dmitry Shmidt04949592012-07-19 12:16:46 -07003920 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3922 if (auth) {
3923 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3924 "connect a running group from " MACSTR,
3925 MAC2STR(peer_addr));
3926 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3927 return ret;
3928 }
3929 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3930 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3931 iface_addr) < 0) {
3932 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3933 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3934 dev_addr);
3935 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003936 if (auto_join) {
3937 os_get_time(&wpa_s->p2p_auto_started);
3938 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3939 "%ld.%06ld",
3940 wpa_s->p2p_auto_started.sec,
3941 wpa_s->p2p_auto_started.usec);
3942 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003943 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003944 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3945 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003946 return -1;
3947 return ret;
3948 }
3949
3950 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003951 wpa_s->assoc_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003953 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 oper_freq = wpa_drv_shared_freq(wpa_s);
3955 if (oper_freq < 0)
3956 oper_freq = 0;
3957 }
3958
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003959 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
3960 &oper_freq);
3961 if (res)
3962 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003963 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964
3965 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3966
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003967 if (wpa_s->create_p2p_iface) {
3968 /* Prepare to add a new interface for the group */
3969 iftype = WPA_IF_P2P_GROUP;
3970 if (go_intent == 15)
3971 iftype = WPA_IF_P2P_GO;
3972 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3973 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3974 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003975 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003976 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003977
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003978 if_addr = wpa_s->pending_interface_addr;
3979 } else
3980 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003981
3982 if (auth) {
3983 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003984 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003985 force_freq, persistent_group, ssid,
3986 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987 return -1;
3988 return ret;
3989 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003990
3991 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3992 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003993 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003994 if (wpa_s->create_p2p_iface)
3995 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996 return -1;
3997 }
3998 return ret;
3999}
4000
4001
4002/**
4003 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4004 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4005 * @freq: Frequency of the channel in MHz
4006 * @duration: Duration of the stay on the channel in milliseconds
4007 *
4008 * This callback is called when the driver indicates that it has started the
4009 * requested remain-on-channel duration.
4010 */
4011void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4012 unsigned int freq, unsigned int duration)
4013{
4014 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4015 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4017 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4018 wpa_s->pending_listen_duration);
4019 wpa_s->pending_listen_freq = 0;
4020 }
4021}
4022
4023
4024static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4025 unsigned int timeout)
4026{
4027 /* Limit maximum Listen state time based on driver limitation. */
4028 if (timeout > wpa_s->max_remain_on_chan)
4029 timeout = wpa_s->max_remain_on_chan;
4030
4031 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4032 return wpa_drv_p2p_listen(wpa_s, timeout);
4033
4034 return p2p_listen(wpa_s->global->p2p, timeout);
4035}
4036
4037
4038/**
4039 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4040 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4041 * @freq: Frequency of the channel in MHz
4042 *
4043 * This callback is called when the driver indicates that a remain-on-channel
4044 * operation has been completed, i.e., the duration on the requested channel
4045 * has timed out.
4046 */
4047void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4048 unsigned int freq)
4049{
4050 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4051 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004052 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004053 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4054 return;
4055 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4056 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004057 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004058 return;
4059 if (wpa_s->p2p_long_listen > 0)
4060 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4061 if (wpa_s->p2p_long_listen > 0) {
4062 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4063 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4064 }
4065}
4066
4067
4068/**
4069 * wpas_p2p_group_remove - Remove a P2P group
4070 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4071 * @ifname: Network interface name of the group interface or "*" to remove all
4072 * groups
4073 * Returns: 0 on success, -1 on failure
4074 *
4075 * This function is used to remove a P2P group. This can be used to disconnect
4076 * from a group in which the local end is a P2P Client or to end a P2P Group in
4077 * case the local end is the Group Owner. If a virtual network interface was
4078 * created for this group, that interface will be removed. Otherwise, only the
4079 * configured P2P group network will be removed from the interface.
4080 */
4081int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4082{
4083 struct wpa_global *global = wpa_s->global;
4084
4085 if (os_strcmp(ifname, "*") == 0) {
4086 struct wpa_supplicant *prev;
4087 wpa_s = global->ifaces;
4088 while (wpa_s) {
4089 prev = wpa_s;
4090 wpa_s = wpa_s->next;
4091 wpas_p2p_disconnect(prev);
4092 }
4093 return 0;
4094 }
4095
4096 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4097 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4098 break;
4099 }
4100
4101 return wpas_p2p_disconnect(wpa_s);
4102}
4103
4104
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004105static int freq_included(const struct p2p_channels *channels, unsigned int freq)
4106{
4107 if (channels == NULL)
4108 return 1; /* Assume no restrictions */
4109 return p2p_channels_includes_freq(channels, freq);
4110}
4111
4112
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004113static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4114 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004115 int freq, int ht40,
4116 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117{
4118 u8 bssid[ETH_ALEN];
4119 int res;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004120 unsigned int pref_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004121
4122 os_memset(params, 0, sizeof(*params));
4123 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004124 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004126 if (!freq_included(channels, freq)) {
4127 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4128 "accepted", freq);
4129 return -1;
4130 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4132 "frequency %d MHz", freq);
4133 params->freq = freq;
4134 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4135 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004136 wpa_s->conf->p2p_oper_channel <= 11 &&
4137 freq_included(channels,
4138 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4140 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4141 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004142 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4143 wpa_s->conf->p2p_oper_reg_class == 116 ||
4144 wpa_s->conf->p2p_oper_reg_class == 117 ||
4145 wpa_s->conf->p2p_oper_reg_class == 124 ||
4146 wpa_s->conf->p2p_oper_reg_class == 126 ||
4147 wpa_s->conf->p2p_oper_reg_class == 127) &&
4148 freq_included(channels,
4149 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004150 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4151 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4152 "frequency %d MHz", params->freq);
4153 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4154 wpa_s->best_overall_freq > 0 &&
4155 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004156 wpa_s->best_overall_freq) &&
4157 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004158 params->freq = wpa_s->best_overall_freq;
4159 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4160 "channel %d MHz", params->freq);
4161 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4162 wpa_s->best_24_freq > 0 &&
4163 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004164 wpa_s->best_24_freq) &&
4165 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004166 params->freq = wpa_s->best_24_freq;
4167 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4168 "channel %d MHz", params->freq);
4169 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4170 wpa_s->best_5_freq > 0 &&
4171 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004172 wpa_s->best_5_freq) &&
4173 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174 params->freq = wpa_s->best_5_freq;
4175 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4176 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004177 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4178 channels))) {
4179 params->freq = pref_freq;
4180 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4181 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004183 int chan;
4184 for (chan = 0; chan < 11; chan++) {
4185 params->freq = 2412 + chan * 5;
4186 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004187 params->freq) &&
4188 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004189 break;
4190 }
4191 if (chan == 11) {
4192 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4193 "allowed");
4194 return -1;
4195 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004196 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4197 "known)", params->freq);
4198 }
4199
4200 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4201 wpa_s->assoc_freq && !freq) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004202 if (!p2p_supported_freq(wpa_s->global->p2p, wpa_s->assoc_freq)
4203 || !freq_included(channels, wpa_s->assoc_freq)) {
4204 if (wpa_s->drv_flags &
4205 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4206 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4207 "the channel we are already using "
4208 "(%u MHz) - allow multi-channel "
4209 "concurrency", wpa_s->assoc_freq);
4210 } else {
4211 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4212 "the channel we are already using "
4213 "(%u MHz)", wpa_s->assoc_freq);
4214 return -1;
4215 }
4216 } else {
4217 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we "
4218 "are already using (%u MHz)",
4219 wpa_s->assoc_freq);
4220 params->freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004221 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004222 }
4223
4224 res = wpa_drv_shared_freq(wpa_s);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004225 if (res > 0 && !freq &&
4226 (!p2p_supported_freq(wpa_s->global->p2p, res) ||
4227 !freq_included(channels, res))) {
4228 if (wpa_s->drv_flags &
4229 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4230 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4231 "channel we are already using on a shared "
4232 "interface (%u MHz) - allow multi-channel "
4233 "concurrency", res);
4234 } else {
4235 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4236 "channel we are already using on a shared "
4237 "interface (%u MHz)", res);
4238 return -1;
4239 }
4240 } else if (res > 0 && !freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004241 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4242 "already using on a shared interface");
4243 params->freq = res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004244 if (!freq_included(channels, params->freq)) {
4245 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4246 "accepted", params->freq);
4247 return -1;
4248 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004249 } else if (res > 0 && freq != res &&
4250 !(wpa_s->drv_flags &
4251 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4252 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4253 "while connected on another channel (%u MHz)",
4254 freq, res);
4255 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004256 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004257
4258 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259}
4260
4261
4262static struct wpa_supplicant *
4263wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4264 int go)
4265{
4266 struct wpa_supplicant *group_wpa_s;
4267
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004268 if (!wpas_p2p_create_iface(wpa_s)) {
4269 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4270 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004271 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004272 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273
4274 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004275 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004276 wpa_msg_global(wpa_s, MSG_ERROR,
4277 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004278 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004279 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004280 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4281 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004282 wpa_msg_global(wpa_s, MSG_ERROR,
4283 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004284 wpas_p2p_remove_pending_group_interface(wpa_s);
4285 return NULL;
4286 }
4287
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004288 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4289 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004290 return group_wpa_s;
4291}
4292
4293
4294/**
4295 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4296 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4297 * @persistent_group: Whether to create a persistent group
4298 * @freq: Frequency for the group or 0 to indicate no hardcoding
4299 * Returns: 0 on success, -1 on failure
4300 *
4301 * This function creates a new P2P group with the local end as the Group Owner,
4302 * i.e., without using Group Owner Negotiation.
4303 */
4304int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004305 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004306{
4307 struct p2p_go_neg_results params;
4308 unsigned int r;
4309
4310 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4311 return -1;
4312
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004313 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004314 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004315 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 if (freq == 2) {
4318 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4319 "band");
4320 if (wpa_s->best_24_freq > 0 &&
4321 p2p_supported_freq(wpa_s->global->p2p,
4322 wpa_s->best_24_freq)) {
4323 freq = wpa_s->best_24_freq;
4324 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4325 "channel: %d MHz", freq);
4326 } else {
4327 os_get_random((u8 *) &r, sizeof(r));
4328 freq = 2412 + (r % 3) * 25;
4329 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4330 "channel: %d MHz", freq);
4331 }
4332 }
4333
4334 if (freq == 5) {
4335 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4336 "band");
4337 if (wpa_s->best_5_freq > 0 &&
4338 p2p_supported_freq(wpa_s->global->p2p,
4339 wpa_s->best_5_freq)) {
4340 freq = wpa_s->best_5_freq;
4341 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4342 "channel: %d MHz", freq);
4343 } else {
4344 os_get_random((u8 *) &r, sizeof(r));
4345 freq = 5180 + (r % 4) * 20;
4346 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4347 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4348 "5 GHz channel for P2P group");
4349 return -1;
4350 }
4351 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4352 "channel: %d MHz", freq);
4353 }
4354 }
4355
4356 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4357 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4358 "(%u MHz) is not supported for P2P uses",
4359 freq);
4360 return -1;
4361 }
4362
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004363 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004364 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004365 if (params.freq &&
4366 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4367 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4368 "(%u MHz) is not supported for P2P uses",
4369 params.freq);
4370 return -1;
4371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004372 p2p_go_params(wpa_s->global->p2p, &params);
4373 params.persistent_group = persistent_group;
4374
4375 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4376 if (wpa_s == NULL)
4377 return -1;
4378 wpas_start_wps_go(wpa_s, &params, 0);
4379
4380 return 0;
4381}
4382
4383
4384static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4385 struct wpa_ssid *params, int addr_allocated)
4386{
4387 struct wpa_ssid *ssid;
4388
4389 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4390 if (wpa_s == NULL)
4391 return -1;
4392
4393 wpa_supplicant_ap_deinit(wpa_s);
4394
4395 ssid = wpa_config_add_network(wpa_s->conf);
4396 if (ssid == NULL)
4397 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004398 wpa_config_set_network_defaults(ssid);
4399 ssid->temporary = 1;
4400 ssid->proto = WPA_PROTO_RSN;
4401 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4402 ssid->group_cipher = WPA_CIPHER_CCMP;
4403 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4404 ssid->ssid = os_malloc(params->ssid_len);
4405 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004406 wpa_config_remove_network(wpa_s->conf, ssid->id);
4407 return -1;
4408 }
4409 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4410 ssid->ssid_len = params->ssid_len;
4411 ssid->p2p_group = 1;
4412 ssid->export_keys = 1;
4413 if (params->psk_set) {
4414 os_memcpy(ssid->psk, params->psk, 32);
4415 ssid->psk_set = 1;
4416 }
4417 if (params->passphrase)
4418 ssid->passphrase = os_strdup(params->passphrase);
4419
4420 wpa_supplicant_select_network(wpa_s, ssid);
4421
4422 wpa_s->show_group_started = 1;
4423
4424 return 0;
4425}
4426
4427
4428int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4429 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004430 int freq, int ht40,
4431 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432{
4433 struct p2p_go_neg_results params;
4434 int go = 0;
4435
4436 if (ssid->disabled != 2 || ssid->ssid == NULL)
4437 return -1;
4438
4439 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4440 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4441 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4442 "already running");
4443 return 0;
4444 }
4445
4446 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004447 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448
Dmitry Shmidt04949592012-07-19 12:16:46 -07004449 wpa_s->p2p_fallback_to_go_neg = 0;
4450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004451 if (ssid->mode == WPAS_MODE_INFRA)
4452 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4453
4454 if (ssid->mode != WPAS_MODE_P2P_GO)
4455 return -1;
4456
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004457 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004458 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459
4460 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004461 params.psk_set = ssid->psk_set;
4462 if (params.psk_set)
4463 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004464 if (ssid->passphrase) {
4465 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4466 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4467 "persistent group");
4468 return -1;
4469 }
4470 os_strlcpy(params.passphrase, ssid->passphrase,
4471 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004473 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4474 params.ssid_len = ssid->ssid_len;
4475 params.persistent_group = 1;
4476
4477 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4478 if (wpa_s == NULL)
4479 return -1;
4480
4481 wpas_start_wps_go(wpa_s, &params, 0);
4482
4483 return 0;
4484}
4485
4486
4487static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4488 struct wpabuf *proberesp_ies)
4489{
4490 struct wpa_supplicant *wpa_s = ctx;
4491 if (wpa_s->ap_iface) {
4492 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004493 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4494 wpabuf_free(beacon_ies);
4495 wpabuf_free(proberesp_ies);
4496 return;
4497 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498 if (beacon_ies) {
4499 wpabuf_free(hapd->p2p_beacon_ie);
4500 hapd->p2p_beacon_ie = beacon_ies;
4501 }
4502 wpabuf_free(hapd->p2p_probe_resp_ie);
4503 hapd->p2p_probe_resp_ie = proberesp_ies;
4504 } else {
4505 wpabuf_free(beacon_ies);
4506 wpabuf_free(proberesp_ies);
4507 }
4508 wpa_supplicant_ap_update_beacon(wpa_s);
4509}
4510
4511
4512static void wpas_p2p_idle_update(void *ctx, int idle)
4513{
4514 struct wpa_supplicant *wpa_s = ctx;
4515 if (!wpa_s->ap_iface)
4516 return;
4517 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004518 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004519 wpas_p2p_set_group_idle_timeout(wpa_s);
4520 else
4521 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4522}
4523
4524
4525struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004526 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004527{
4528 struct p2p_group *group;
4529 struct p2p_group_config *cfg;
4530
4531 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4532 return NULL;
4533 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4534 return NULL;
4535
4536 cfg = os_zalloc(sizeof(*cfg));
4537 if (cfg == NULL)
4538 return NULL;
4539
Dmitry Shmidt04949592012-07-19 12:16:46 -07004540 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004541 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004542 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004543 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4545 if (wpa_s->max_stations &&
4546 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4547 cfg->max_clients = wpa_s->max_stations;
4548 else
4549 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004550 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4551 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004552 cfg->cb_ctx = wpa_s;
4553 cfg->ie_update = wpas_p2p_ie_update;
4554 cfg->idle_update = wpas_p2p_idle_update;
4555
4556 group = p2p_group_init(wpa_s->global->p2p, cfg);
4557 if (group == NULL)
4558 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004559 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004560 p2p_group_notif_formation_done(group);
4561 wpa_s->p2p_group = group;
4562 return group;
4563}
4564
4565
4566void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4567 int registrar)
4568{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004569 struct wpa_ssid *ssid = wpa_s->current_ssid;
4570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571 if (!wpa_s->p2p_in_provisioning) {
4572 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4573 "provisioning not in progress");
4574 return;
4575 }
4576
Dmitry Shmidt04949592012-07-19 12:16:46 -07004577 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4578 u8 go_dev_addr[ETH_ALEN];
4579 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4580 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4581 ssid->ssid_len);
4582 /* Clear any stored provisioning info */
4583 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4584 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4587 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004588 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4589 /*
4590 * Use a separate timeout for initial data connection to
4591 * complete to allow the group to be removed automatically if
4592 * something goes wrong in this step before the P2P group idle
4593 * timeout mechanism is taken into use.
4594 */
4595 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4596 wpas_p2p_group_formation_timeout,
4597 wpa_s->parent, NULL);
4598 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004599 if (wpa_s->global->p2p)
4600 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4601 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4602 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4603 wpas_group_formation_completed(wpa_s, 1);
4604}
4605
4606
Jouni Malinen75ecf522011-06-27 15:19:46 -07004607void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4608 struct wps_event_fail *fail)
4609{
4610 if (!wpa_s->p2p_in_provisioning) {
4611 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4612 "provisioning not in progress");
4613 return;
4614 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004615
4616 if (wpa_s->go_params) {
4617 p2p_clear_provisioning_info(
4618 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004619 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004620 }
4621
Jouni Malinen75ecf522011-06-27 15:19:46 -07004622 wpas_notify_p2p_wps_failed(wpa_s, fail);
4623}
4624
4625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004627 const char *config_method,
4628 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629{
4630 u16 config_methods;
4631
Dmitry Shmidt04949592012-07-19 12:16:46 -07004632 wpa_s->p2p_fallback_to_go_neg = 0;
4633 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004634 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004636 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004637 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004638 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4639 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004641 else {
4642 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004644 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645
Dmitry Shmidt04949592012-07-19 12:16:46 -07004646 if (use == WPAS_P2P_PD_AUTO) {
4647 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4648 wpa_s->pending_pd_config_methods = config_methods;
4649 wpa_s->p2p_auto_pd = 1;
4650 wpa_s->p2p_auto_join = 0;
4651 wpa_s->pending_pd_before_join = 0;
4652 wpa_s->auto_pd_scan_retry = 0;
4653 wpas_p2p_stop_find(wpa_s);
4654 wpa_s->p2p_join_scan_count = 0;
4655 os_get_time(&wpa_s->p2p_auto_started);
4656 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4657 wpa_s->p2p_auto_started.sec,
4658 wpa_s->p2p_auto_started.usec);
4659 wpas_p2p_join_scan(wpa_s, NULL);
4660 return 0;
4661 }
4662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004663 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4664 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004665 config_methods,
4666 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004667 }
4668
4669 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4670 return -1;
4671
4672 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004673 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004674 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004675}
4676
4677
4678int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4679 char *end)
4680{
4681 return p2p_scan_result_text(ies, ies_len, buf, end);
4682}
4683
4684
4685static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4686{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004687 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004688 return;
4689
4690 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4691 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004692 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004693}
4694
4695
4696int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4697 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004698 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004699 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004700{
4701 wpas_p2p_clear_pending_action_tx(wpa_s);
4702 wpa_s->p2p_long_listen = 0;
4703
4704 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4705 return wpa_drv_p2p_find(wpa_s, timeout, type);
4706
Dmitry Shmidt04949592012-07-19 12:16:46 -07004707 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4708 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004709 return -1;
4710
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004711 wpa_supplicant_cancel_sched_scan(wpa_s);
4712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004713 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004714 num_req_dev_types, req_dev_types, dev_id,
4715 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716}
4717
4718
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004719static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720{
4721 wpas_p2p_clear_pending_action_tx(wpa_s);
4722 wpa_s->p2p_long_listen = 0;
4723 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4724 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004725 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726
4727 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4728 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004729 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004730 }
4731
4732 if (wpa_s->global->p2p)
4733 p2p_stop_find(wpa_s->global->p2p);
4734
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004735 return 0;
4736}
4737
4738
4739void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4740{
4741 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4742 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743 wpas_p2p_remove_pending_group_interface(wpa_s);
4744}
4745
4746
4747static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4748{
4749 struct wpa_supplicant *wpa_s = eloop_ctx;
4750 wpa_s->p2p_long_listen = 0;
4751}
4752
4753
4754int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4755{
4756 int res;
4757
4758 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4759 return -1;
4760
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004761 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762 wpas_p2p_clear_pending_action_tx(wpa_s);
4763
4764 if (timeout == 0) {
4765 /*
4766 * This is a request for unlimited Listen state. However, at
4767 * least for now, this is mapped to a Listen state for one
4768 * hour.
4769 */
4770 timeout = 3600;
4771 }
4772 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4773 wpa_s->p2p_long_listen = 0;
4774
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004775 /*
4776 * Stop previous find/listen operation to avoid trying to request a new
4777 * remain-on-channel operation while the driver is still running the
4778 * previous one.
4779 */
4780 if (wpa_s->global->p2p)
4781 p2p_stop_find(wpa_s->global->p2p);
4782
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004783 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4784 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4785 wpa_s->p2p_long_listen = timeout * 1000;
4786 eloop_register_timeout(timeout, 0,
4787 wpas_p2p_long_listen_timeout,
4788 wpa_s, NULL);
4789 }
4790
4791 return res;
4792}
4793
4794
4795int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4796 u8 *buf, size_t len, int p2p_group)
4797{
4798 struct wpabuf *p2p_ie;
4799 int ret;
4800
4801 if (wpa_s->global->p2p_disabled)
4802 return -1;
4803 if (wpa_s->global->p2p == NULL)
4804 return -1;
4805 if (bss == NULL)
4806 return -1;
4807
4808 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4809 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4810 p2p_group, p2p_ie);
4811 wpabuf_free(p2p_ie);
4812
4813 return ret;
4814}
4815
4816
4817int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004818 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004819 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820{
4821 if (wpa_s->global->p2p_disabled)
4822 return 0;
4823 if (wpa_s->global->p2p == NULL)
4824 return 0;
4825
Dmitry Shmidt04949592012-07-19 12:16:46 -07004826 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4827 ie, ie_len)) {
4828 case P2P_PREQ_NOT_P2P:
4829 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4830 ssi_signal);
4831 /* fall through */
4832 case P2P_PREQ_MALFORMED:
4833 case P2P_PREQ_NOT_LISTEN:
4834 case P2P_PREQ_NOT_PROCESSED:
4835 default: /* make gcc happy */
4836 return 0;
4837 case P2P_PREQ_PROCESSED:
4838 return 1;
4839 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004840}
4841
4842
4843void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4844 const u8 *sa, const u8 *bssid,
4845 u8 category, const u8 *data, size_t len, int freq)
4846{
4847 if (wpa_s->global->p2p_disabled)
4848 return;
4849 if (wpa_s->global->p2p == NULL)
4850 return;
4851
4852 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4853 freq);
4854}
4855
4856
4857void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4858{
4859 if (wpa_s->global->p2p_disabled)
4860 return;
4861 if (wpa_s->global->p2p == NULL)
4862 return;
4863
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004864 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865}
4866
4867
4868void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4869{
4870 p2p_group_deinit(wpa_s->p2p_group);
4871 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004872
4873 wpa_s->ap_configured_cb = NULL;
4874 wpa_s->ap_configured_cb_ctx = NULL;
4875 wpa_s->ap_configured_cb_data = NULL;
4876 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004877}
4878
4879
4880int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4881{
4882 wpa_s->p2p_long_listen = 0;
4883
4884 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4885 return wpa_drv_p2p_reject(wpa_s, addr);
4886
4887 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4888 return -1;
4889
4890 return p2p_reject(wpa_s->global->p2p, addr);
4891}
4892
4893
4894/* Invite to reinvoke a persistent group */
4895int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004896 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004897 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004898{
4899 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004900 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004901 int force_freq = 0, oper_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004902 int res;
4903
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004904 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004905 if (peer_addr)
4906 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4907 else
4908 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909
Jouni Malinen31be0a42012-08-31 21:20:51 +03004910 wpa_s->p2p_persistent_go_freq = freq;
4911 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004912 if (ssid->mode == WPAS_MODE_P2P_GO) {
4913 role = P2P_INVITE_ROLE_GO;
4914 if (peer_addr == NULL) {
4915 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4916 "address in invitation command");
4917 return -1;
4918 }
4919 if (wpas_p2p_create_iface(wpa_s)) {
4920 if (wpas_p2p_add_group_interface(wpa_s,
4921 WPA_IF_P2P_GO) < 0) {
4922 wpa_printf(MSG_ERROR, "P2P: Failed to "
4923 "allocate a new interface for the "
4924 "group");
4925 return -1;
4926 }
4927 bssid = wpa_s->pending_interface_addr;
4928 } else
4929 bssid = wpa_s->own_addr;
4930 } else {
4931 role = P2P_INVITE_ROLE_CLIENT;
4932 peer_addr = ssid->bssid;
4933 }
4934 wpa_s->pending_invite_ssid_id = ssid->id;
4935
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004936 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4937 wpa_s->assoc_freq) {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004938 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004939 if (bssid == NULL)
4940 bssid = bssid_buf;
4941 } else {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004942 oper_freq = wpa_drv_shared_freq(wpa_s);
4943 if (oper_freq < 0)
4944 oper_freq = 0;
4945 }
4946
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004947 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4948 &oper_freq);
4949 if (res)
4950 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4953 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4954 ssid->ssid, ssid->ssid_len,
4955 go_dev_addr, 1);
4956
4957 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4958 return -1;
4959
4960 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004961 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4962 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004963}
4964
4965
4966/* Invite to join an active group */
4967int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4968 const u8 *peer_addr, const u8 *go_dev_addr)
4969{
4970 struct wpa_global *global = wpa_s->global;
4971 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004972 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004974 int persistent;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004975 int force_freq = 0, oper_freq = 0, pref_freq = 0;
4976 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004977
Jouni Malinen31be0a42012-08-31 21:20:51 +03004978 wpa_s->p2p_persistent_go_freq = 0;
4979 wpa_s->p2p_go_ht40 = 0;
4980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4982 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4983 break;
4984 }
4985 if (wpa_s == NULL) {
4986 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4987 return -1;
4988 }
4989
4990 ssid = wpa_s->current_ssid;
4991 if (ssid == NULL) {
4992 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4993 "invitation");
4994 return -1;
4995 }
4996
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004997 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004998 persistent = ssid->p2p_persistent_group &&
4999 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5000 ssid->ssid, ssid->ssid_len);
5001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005002 if (ssid->mode == WPAS_MODE_P2P_GO) {
5003 role = P2P_INVITE_ROLE_ACTIVE_GO;
5004 bssid = wpa_s->own_addr;
5005 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005006 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005007 } else {
5008 role = P2P_INVITE_ROLE_CLIENT;
5009 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5010 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5011 "invite to current group");
5012 return -1;
5013 }
5014 bssid = wpa_s->bssid;
5015 if (go_dev_addr == NULL &&
5016 !is_zero_ether_addr(wpa_s->go_dev_addr))
5017 go_dev_addr = wpa_s->go_dev_addr;
5018 }
5019 wpa_s->parent->pending_invite_ssid_id = -1;
5020
5021 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5022 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5023 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005024 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025
5026 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5027 return -1;
5028
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005029 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
5030 wpa_s->assoc_freq) {
5031 oper_freq = wpa_s->assoc_freq;
5032 if (bssid == NULL)
5033 bssid = bssid_buf;
5034 } else {
5035 oper_freq = wpa_drv_shared_freq(wpa_s);
5036 if (oper_freq < 0)
5037 oper_freq = 0;
5038 }
5039
5040 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
5041 &oper_freq);
5042 if (res)
5043 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005044 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005046 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005047 ssid->ssid, ssid->ssid_len, force_freq,
5048 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005049}
5050
5051
5052void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5053{
5054 struct wpa_ssid *ssid = wpa_s->current_ssid;
5055 const char *ssid_txt;
5056 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005057 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005058 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005059 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005060
Dmitry Shmidt04949592012-07-19 12:16:46 -07005061 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5062 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5063 wpa_s->parent, NULL);
5064 }
5065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005067 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005068
5069 wpa_s->show_group_started = 0;
5070
5071 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5072 os_memset(go_dev_addr, 0, ETH_ALEN);
5073 if (ssid->bssid_set)
5074 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5075 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5076 ssid->ssid_len);
5077 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5078
5079 if (wpa_s->global->p2p_group_formation == wpa_s)
5080 wpa_s->global->p2p_group_formation = NULL;
5081
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005082 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5083 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005084 if (ssid->passphrase == NULL && ssid->psk_set) {
5085 char psk[65];
5086 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005087 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5088 "%s client ssid=\"%s\" freq=%d psk=%s "
5089 "go_dev_addr=" MACSTR "%s",
5090 wpa_s->ifname, ssid_txt, freq, psk,
5091 MAC2STR(go_dev_addr),
5092 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005094 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5095 "%s client ssid=\"%s\" freq=%d "
5096 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5097 wpa_s->ifname, ssid_txt, freq,
5098 ssid->passphrase ? ssid->passphrase : "",
5099 MAC2STR(go_dev_addr),
5100 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101 }
5102
5103 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005104 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5105 ssid, go_dev_addr);
5106 if (network_id < 0)
5107 network_id = ssid->id;
5108 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005109
5110done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005111 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112}
5113
5114
5115int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5116 u32 interval1, u32 duration2, u32 interval2)
5117{
5118 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5119 return -1;
5120 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5121 return -1;
5122
5123 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5124 wpa_s->current_ssid == NULL ||
5125 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5126 return -1;
5127
5128 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5129 wpa_s->own_addr, wpa_s->assoc_freq,
5130 duration1, interval1, duration2, interval2);
5131}
5132
5133
5134int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5135 unsigned int interval)
5136{
5137 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5138 return -1;
5139
5140 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5141 return -1;
5142
5143 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5144}
5145
5146
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005147static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5148{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005149 if (wpa_s->current_ssid == NULL) {
5150 /*
5151 * current_ssid can be cleared when P2P client interface gets
5152 * disconnected, so assume this interface was used as P2P
5153 * client.
5154 */
5155 return 1;
5156 }
5157 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005158 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5159}
5160
5161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005162static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5163{
5164 struct wpa_supplicant *wpa_s = eloop_ctx;
5165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005166 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005167 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5168 "disabled");
5169 return;
5170 }
5171
Dmitry Shmidt04949592012-07-19 12:16:46 -07005172 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5173 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005174 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005175}
5176
5177
5178static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5179{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005180 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005181
Dmitry Shmidt04949592012-07-19 12:16:46 -07005182 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5183 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005185 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5186 return;
5187
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005188 timeout = wpa_s->conf->p2p_group_idle;
5189 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5190 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5191 timeout = P2P_MAX_CLIENT_IDLE;
5192
5193 if (timeout == 0)
5194 return;
5195
Dmitry Shmidt04949592012-07-19 12:16:46 -07005196 if (timeout < 0) {
5197 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5198 timeout = 0; /* special client mode no-timeout */
5199 else
5200 return;
5201 }
5202
5203 if (wpa_s->p2p_in_provisioning) {
5204 /*
5205 * Use the normal group formation timeout during the
5206 * provisioning phase to avoid terminating this process too
5207 * early due to group idle timeout.
5208 */
5209 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5210 "during provisioning");
5211 return;
5212 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305213
Dmitry Shmidt04949592012-07-19 12:16:46 -07005214 if (wpa_s->show_group_started) {
5215 /*
5216 * Use the normal group formation timeout between the end of
5217 * the provisioning phase and completion of 4-way handshake to
5218 * avoid terminating this process too early due to group idle
5219 * timeout.
5220 */
5221 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5222 "while waiting for initial 4-way handshake to "
5223 "complete");
5224 return;
5225 }
5226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005227 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005228 timeout);
5229 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5230 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005231}
5232
5233
Jouni Malinen2b89da82012-08-31 22:04:41 +03005234/* Returns 1 if the interface was removed */
5235int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5236 u16 reason_code, const u8 *ie, size_t ie_len,
5237 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005238{
5239 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005240 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005241 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005242 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005243
Dmitry Shmidt04949592012-07-19 12:16:46 -07005244 if (!locally_generated)
5245 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5246 ie_len);
5247
5248 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5249 wpa_s->current_ssid &&
5250 wpa_s->current_ssid->p2p_group &&
5251 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5252 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5253 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005254 if (wpas_p2p_group_delete(wpa_s,
5255 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5256 > 0)
5257 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005258 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005259
5260 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005261}
5262
5263
5264void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005265 u16 reason_code, const u8 *ie, size_t ie_len,
5266 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005267{
5268 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5269 return;
5270 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5271 return;
5272
Dmitry Shmidt04949592012-07-19 12:16:46 -07005273 if (!locally_generated)
5274 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5275 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005276}
5277
5278
5279void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5280{
5281 struct p2p_data *p2p = wpa_s->global->p2p;
5282
5283 if (p2p == NULL)
5284 return;
5285
5286 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5287 return;
5288
5289 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5290 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5291
5292 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5293 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5294
5295 if (wpa_s->wps &&
5296 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5297 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5298
5299 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5300 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5301
5302 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5303 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5304 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5305 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5306 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5307 }
5308
5309 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5310 p2p_set_sec_dev_types(p2p,
5311 (void *) wpa_s->conf->sec_device_type,
5312 wpa_s->conf->num_sec_device_types);
5313
5314 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5315 int i;
5316 p2p_remove_wps_vendor_extensions(p2p);
5317 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5318 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5319 continue;
5320 p2p_add_wps_vendor_extension(
5321 p2p, wpa_s->conf->wps_vendor_ext[i]);
5322 }
5323 }
5324
5325 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5326 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5327 char country[3];
5328 country[0] = wpa_s->conf->country[0];
5329 country[1] = wpa_s->conf->country[1];
5330 country[2] = 0x04;
5331 p2p_set_country(p2p, country);
5332 }
5333
5334 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5335 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5336 wpa_s->conf->p2p_ssid_postfix ?
5337 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5338 0);
5339 }
5340
5341 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5342 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005343
5344 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5345 u8 reg_class, channel;
5346 int ret;
5347 unsigned int r;
5348 if (wpa_s->conf->p2p_listen_reg_class &&
5349 wpa_s->conf->p2p_listen_channel) {
5350 reg_class = wpa_s->conf->p2p_listen_reg_class;
5351 channel = wpa_s->conf->p2p_listen_channel;
5352 } else {
5353 reg_class = 81;
5354 /*
5355 * Pick one of the social channels randomly as the
5356 * listen channel.
5357 */
5358 os_get_random((u8 *) &r, sizeof(r));
5359 channel = 1 + (r % 3) * 5;
5360 }
5361 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5362 if (ret)
5363 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5364 "failed: %d", ret);
5365 }
5366 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5367 u8 op_reg_class, op_channel, cfg_op_channel;
5368 int ret = 0;
5369 unsigned int r;
5370 if (wpa_s->conf->p2p_oper_reg_class &&
5371 wpa_s->conf->p2p_oper_channel) {
5372 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5373 op_channel = wpa_s->conf->p2p_oper_channel;
5374 cfg_op_channel = 1;
5375 } else {
5376 op_reg_class = 81;
5377 /*
5378 * Use random operation channel from (1, 6, 11)
5379 *if no other preference is indicated.
5380 */
5381 os_get_random((u8 *) &r, sizeof(r));
5382 op_channel = 1 + (r % 3) * 5;
5383 cfg_op_channel = 0;
5384 }
5385 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5386 cfg_op_channel);
5387 if (ret)
5388 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5389 "failed: %d", ret);
5390 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005391
5392 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5393 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5394 wpa_s->conf->p2p_pref_chan) < 0) {
5395 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5396 "update failed");
5397 }
5398 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005399}
5400
5401
5402int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5403 int duration)
5404{
5405 if (!wpa_s->ap_iface)
5406 return -1;
5407 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5408 duration);
5409}
5410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411
5412int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5413{
5414 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5415 return -1;
5416 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5417 return -1;
5418
5419 wpa_s->global->cross_connection = enabled;
5420 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5421
5422 if (!enabled) {
5423 struct wpa_supplicant *iface;
5424
5425 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5426 {
5427 if (iface->cross_connect_enabled == 0)
5428 continue;
5429
5430 iface->cross_connect_enabled = 0;
5431 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005432 wpa_msg_global(iface->parent, MSG_INFO,
5433 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5434 iface->ifname,
5435 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005436 }
5437 }
5438
5439 return 0;
5440}
5441
5442
5443static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5444{
5445 struct wpa_supplicant *iface;
5446
5447 if (!uplink->global->cross_connection)
5448 return;
5449
5450 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5451 if (!iface->cross_connect_enabled)
5452 continue;
5453 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5454 0)
5455 continue;
5456 if (iface->ap_iface == NULL)
5457 continue;
5458 if (iface->cross_connect_in_use)
5459 continue;
5460
5461 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005462 wpa_msg_global(iface->parent, MSG_INFO,
5463 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5464 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005465 }
5466}
5467
5468
5469static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5470{
5471 struct wpa_supplicant *iface;
5472
5473 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5474 if (!iface->cross_connect_enabled)
5475 continue;
5476 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5477 0)
5478 continue;
5479 if (!iface->cross_connect_in_use)
5480 continue;
5481
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005482 wpa_msg_global(iface->parent, MSG_INFO,
5483 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5484 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005485 iface->cross_connect_in_use = 0;
5486 }
5487}
5488
5489
5490void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5491{
5492 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5493 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5494 wpa_s->cross_connect_disallowed)
5495 wpas_p2p_disable_cross_connect(wpa_s);
5496 else
5497 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005498 if (!wpa_s->ap_iface &&
5499 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5500 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501}
5502
5503
5504void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5505{
5506 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005507 if (!wpa_s->ap_iface &&
5508 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5509 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510 wpas_p2p_set_group_idle_timeout(wpa_s);
5511}
5512
5513
5514static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5515{
5516 struct wpa_supplicant *iface;
5517
5518 if (!wpa_s->global->cross_connection)
5519 return;
5520
5521 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5522 if (iface == wpa_s)
5523 continue;
5524 if (iface->drv_flags &
5525 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5526 continue;
5527 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5528 continue;
5529
5530 wpa_s->cross_connect_enabled = 1;
5531 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5532 sizeof(wpa_s->cross_connect_uplink));
5533 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5534 "%s to %s whenever uplink is available",
5535 wpa_s->ifname, wpa_s->cross_connect_uplink);
5536
5537 if (iface->ap_iface || iface->current_ssid == NULL ||
5538 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5539 iface->cross_connect_disallowed ||
5540 iface->wpa_state != WPA_COMPLETED)
5541 break;
5542
5543 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005544 wpa_msg_global(wpa_s->parent, MSG_INFO,
5545 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5546 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547 break;
5548 }
5549}
5550
5551
5552int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5553{
5554 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5555 !wpa_s->p2p_in_provisioning)
5556 return 0; /* not P2P client operation */
5557
5558 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5559 "session overlap");
5560 if (wpa_s != wpa_s->parent)
5561 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5562
5563 if (wpa_s->global->p2p)
5564 p2p_group_formation_failed(wpa_s->global->p2p);
5565
5566 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5567 wpa_s->parent, NULL);
5568
5569 wpas_group_formation_completed(wpa_s, 0);
5570 return 1;
5571}
5572
5573
5574void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5575{
5576 struct p2p_channels chan;
5577
5578 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5579 return;
5580
5581 os_memset(&chan, 0, sizeof(chan));
5582 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5583 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5584 "channel list");
5585 return;
5586 }
5587
5588 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5589}
5590
5591
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005592static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5593 struct wpa_scan_results *scan_res)
5594{
5595 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5596}
5597
5598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005599int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5600{
5601 struct wpa_global *global = wpa_s->global;
5602 int found = 0;
5603 const u8 *peer;
5604
5605 if (global->p2p == NULL)
5606 return -1;
5607
5608 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5609
5610 if (wpa_s->pending_interface_name[0] &&
5611 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5612 found = 1;
5613
5614 peer = p2p_get_go_neg_peer(global->p2p);
5615 if (peer) {
5616 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5617 MACSTR, MAC2STR(peer));
5618 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005619 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005620 }
5621
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005622 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5623 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5624 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5625 found = 1;
5626 }
5627
5628 if (wpa_s->pending_pd_before_join) {
5629 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5630 wpa_s->pending_pd_before_join = 0;
5631 found = 1;
5632 }
5633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005634 wpas_p2p_stop_find(wpa_s);
5635
5636 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5637 if (wpa_s == global->p2p_group_formation &&
5638 (wpa_s->p2p_in_provisioning ||
5639 wpa_s->parent->pending_interface_type ==
5640 WPA_IF_P2P_CLIENT)) {
5641 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5642 "formation found - cancelling",
5643 wpa_s->ifname);
5644 found = 1;
5645 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5646 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005647 if (wpa_s->p2p_in_provisioning) {
5648 wpas_group_formation_completed(wpa_s, 0);
5649 break;
5650 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005651 wpas_p2p_group_delete(wpa_s,
5652 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005653 break;
5654 }
5655 }
5656
5657 if (!found) {
5658 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5659 return -1;
5660 }
5661
5662 return 0;
5663}
5664
5665
5666void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5667{
5668 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5669 return;
5670
5671 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5672 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005673 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674}
5675
5676
5677void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5678 int freq_24, int freq_5, int freq_overall)
5679{
5680 struct p2p_data *p2p = wpa_s->global->p2p;
5681 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5682 return;
5683 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5684}
5685
5686
5687int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5688{
5689 u8 peer[ETH_ALEN];
5690 struct p2p_data *p2p = wpa_s->global->p2p;
5691
5692 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5693 return -1;
5694
5695 if (hwaddr_aton(addr, peer))
5696 return -1;
5697
5698 return p2p_unauthorize(p2p, peer);
5699}
5700
5701
5702/**
5703 * wpas_p2p_disconnect - Disconnect from a P2P Group
5704 * @wpa_s: Pointer to wpa_supplicant data
5705 * Returns: 0 on success, -1 on failure
5706 *
5707 * This can be used to disconnect from a group in which the local end is a P2P
5708 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5709 * virtual network interface was created for this group, that interface will be
5710 * removed. Otherwise, only the configured P2P group network will be removed
5711 * from the interface.
5712 */
5713int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5714{
5715
5716 if (wpa_s == NULL)
5717 return -1;
5718
Jouni Malinen2b89da82012-08-31 22:04:41 +03005719 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5720 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005721}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005722
5723
5724int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5725{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005726 int ret;
5727
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005728 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5729 return 0;
5730
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005731 ret = p2p_in_progress(wpa_s->global->p2p);
5732 if (ret == 0) {
5733 /*
5734 * Check whether there is an ongoing WPS provisioning step (or
5735 * other parts of group formation) on another interface since
5736 * p2p_in_progress() does not report this to avoid issues for
5737 * scans during such provisioning step.
5738 */
5739 if (wpa_s->global->p2p_group_formation &&
5740 wpa_s->global->p2p_group_formation != wpa_s) {
5741 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5742 "in group formation",
5743 wpa_s->global->p2p_group_formation->ifname);
5744 ret = 1;
5745 }
5746 }
5747
5748 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005749}
5750
5751
5752void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5753 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005754{
5755 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5756 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5757 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005758 /**
5759 * Remove the network by scheduling the group formation
5760 * timeout to happen immediately. The teardown code
5761 * needs to be scheduled to run asynch later so that we
5762 * don't delete data from under ourselves unexpectedly.
5763 * Calling wpas_p2p_group_formation_timeout directly
5764 * causes a series of crashes in WPS failure scenarios.
5765 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005766 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5767 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005768 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5769 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005770 }
5771}
5772
5773
5774struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005775 const u8 *addr, const u8 *ssid,
5776 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005777{
5778 struct wpa_ssid *s;
5779 size_t i;
5780
5781 for (s = wpa_s->conf->ssid; s; s = s->next) {
5782 if (s->disabled != 2)
5783 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005784 if (ssid &&
5785 (ssid_len != s->ssid_len ||
5786 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5787 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005788 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5789 return s; /* peer is GO in the persistent group */
5790 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5791 continue;
5792 for (i = 0; i < s->num_p2p_clients; i++) {
5793 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5794 addr, ETH_ALEN) == 0)
5795 return s; /* peer is P2P client in persistent
5796 * group */
5797 }
5798 }
5799
5800 return NULL;
5801}
5802
5803
5804void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5805 const u8 *addr)
5806{
5807 if (addr == NULL)
5808 return;
5809 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5810}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005811
Dmitry Shmidt04949592012-07-19 12:16:46 -07005812
5813static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5814 int group_added)
5815{
5816 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005817 if (wpa_s->global->p2p_group_formation)
5818 group = wpa_s->global->p2p_group_formation;
5819 wpa_s = wpa_s->parent;
5820 offchannel_send_action_done(wpa_s);
5821 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005822 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005823 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5824 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5825 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5826 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5827 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005828 wpa_s->p2p_pd_before_go_neg,
5829 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005830}
5831
5832
5833int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5834{
5835 if (!wpa_s->p2p_fallback_to_go_neg ||
5836 wpa_s->p2p_in_provisioning <= 5)
5837 return 0;
5838
5839 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5840 return 0; /* peer operating as a GO */
5841
5842 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5843 "fallback to GO Negotiation");
5844 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5845
5846 return 1;
5847}
5848
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005849
5850unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5851{
5852 const char *rn, *rn2;
5853 struct wpa_supplicant *ifs;
5854
5855 if (wpa_s->wpa_state > WPA_SCANNING) {
5856 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5857 "concurrent operation",
5858 P2P_CONCURRENT_SEARCH_DELAY);
5859 return P2P_CONCURRENT_SEARCH_DELAY;
5860 }
5861
5862 if (!wpa_s->driver->get_radio_name)
5863 return 0;
5864 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5865 if (rn == NULL || rn[0] == '\0')
5866 return 0;
5867
5868 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5869 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5870 continue;
5871
5872 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5873 if (!rn2 || os_strcmp(rn, rn2) != 0)
5874 continue;
5875 if (ifs->wpa_state > WPA_SCANNING) {
5876 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5877 "delay due to concurrent operation on "
5878 "interface %s",
5879 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5880 return P2P_CONCURRENT_SEARCH_DELAY;
5881 }
5882 }
5883
5884 return 0;
5885}
5886
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005887
5888void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5889{
5890 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5891 "pending anymore (sta_scan_pending=%d "
5892 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5893 wpa_s->global->p2p_cb_on_scan_complete);
5894 wpa_s->sta_scan_pending = 0;
5895
5896 if (!wpa_s->global->p2p_cb_on_scan_complete)
5897 return;
5898 wpa_s->global->p2p_cb_on_scan_complete = 0;
5899
5900 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5901 return;
5902
5903 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
5904 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
5905 "continued after successful connection");
5906 p2p_increase_search_delay(wpa_s->global->p2p,
5907 wpas_p2p_search_delay(wpa_s));
5908 }
5909}
5910
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005911#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07005912static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
5913{
5914 struct wpa_supplicant *wpa_s = eloop_ctx;
5915
5916 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
5917 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
5918}
5919
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005920int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5921 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005922{
5923 struct wpa_supplicant *iface = NULL;
5924 struct p2p_data *p2p = wpa_s->global->p2p;
5925
5926 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005927 if ((iface->current_ssid) &&
5928 (iface->current_ssid->frequency != freq) &&
5929 ((iface->p2p_group_interface) ||
5930 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005931
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005932 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5933 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5934 /* Try to see whether we can move the GO. If it
5935 * is not possible, remove the GO interface
5936 */
5937 if (wpa_drv_switch_channel(iface, freq) == 0) {
5938 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5939 iface->current_ssid->frequency = freq;
5940 continue;
5941 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005942 }
5943
5944 /* If GO cannot be moved or if the conflicting interface is a
5945 * P2P Client, remove the interface depending up on the connection
5946 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005947 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005948 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005949 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005950 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005951 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07005952 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
5953 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005954 /* If connection in progress is p2p connection, do not proceed for the connection */
5955 if (wpa_s == iface)
5956 return -1;
5957 else
5958 /* If connection in progress is STA connection, proceed for the connection */
5959 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005960 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005961 /* P2p connection has priority, disable the STA network*/
5962 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5963 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5964 " id=%d", ssid->id);
5965 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5966 if (wpa_s == iface) {
5967 /* p2p connection is in progress, continue connecting...*/
5968 return 0;
5969 }
5970 else {
5971 /* STA connection is in progress, do not allow to continue */
5972 return -1;
5973 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005974 }
5975 }
5976 }
5977 return 0;
5978}
5979#endif