blob: e0395949dcbaf913005fb22783c04058e748b08c [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
Dmitry Shmidt34af3062013-07-11 10:46:32 -070068#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
69
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070070enum p2p_group_removal_reason {
71 P2P_GROUP_REMOVAL_UNKNOWN,
72 P2P_GROUP_REMOVAL_SILENT,
73 P2P_GROUP_REMOVAL_FORMATION_FAILED,
74 P2P_GROUP_REMOVAL_REQUESTED,
75 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
76 P2P_GROUP_REMOVAL_UNAVAILABLE,
77 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
78#ifdef ANDROID_P2P
79 P2P_GROUP_REMOVAL_FREQ_CONFLICT
80#endif
81};
82
Jouni Malinendc7b7132012-09-14 12:53:47 -070083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
85static struct wpa_supplicant *
86wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
87 int go);
88static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070089static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
91static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070092 const u8 *dev_addr, enum p2p_wps_method wps_method,
93 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
95static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
96static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
97static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080098static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
99 void *timeout_ctx);
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700100#ifdef ANDROID_P2P
101static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
102#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700103static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
104 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800105static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106
107
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700108/*
109 * Get the number of concurrent channels that the HW can operate, but that are
110 * currently not in use by any of the wpa_supplicant interfaces.
111 */
112static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
113{
114 int *freqs;
115 int num;
116
117 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
118 if (!freqs)
119 return -1;
120
121 num = get_shared_radio_freqs(wpa_s, freqs,
122 wpa_s->num_multichan_concurrent);
123 os_free(freqs);
124
125 return wpa_s->num_multichan_concurrent - num;
126}
127
128
129/*
130 * Get the frequencies that are currently in use by one or more of the virtual
131 * interfaces, and that are also valid for P2P operation.
132 */
133static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
134 int *p2p_freqs, unsigned int len)
135{
136 int *freqs;
137 unsigned int num, i, j;
138
139 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
140 if (!freqs)
141 return -1;
142
143 num = get_shared_radio_freqs(wpa_s, freqs,
144 wpa_s->num_multichan_concurrent);
145
146 os_memset(p2p_freqs, 0, sizeof(int) * len);
147
148 for (i = 0, j = 0; i < num && j < len; i++) {
149 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
150 p2p_freqs[j++] = freqs[i];
151 }
152
153 os_free(freqs);
154
155 return j;
156}
157
158
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700159static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
160 int freq)
161{
162 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
163 return;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700164 if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
165 wpas_p2p_num_unused_channels(wpa_s) > 0 &&
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700166 wpa_s->parent->conf->p2p_ignore_shared_freq)
167 freq = 0;
168 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
169}
170
171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
173 struct wpa_scan_results *scan_res)
174{
175 size_t i;
176
177 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
178 return;
179
180 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
181 (int) scan_res->num);
182
183 for (i = 0; i < scan_res->num; i++) {
184 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800185 struct os_time time_tmp_age, entry_ts;
186 time_tmp_age.sec = bss->age / 1000;
187 time_tmp_age.usec = (bss->age % 1000) * 1000;
188 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800190 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191 (const u8 *) (bss + 1),
192 bss->ie_len) > 0)
193 break;
194 }
195
196 p2p_scan_res_handled(wpa_s->global->p2p);
197}
198
199
200static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
201 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700202 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203{
204 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700205 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 struct wpa_driver_scan_params params;
207 int ret;
208 struct wpabuf *wps_ie, *ies;
209 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800210 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211
212 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
213 return -1;
214
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700215 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
216 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700217 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700218 wpas_p2p_in_progress(wpa_s) == 2) {
219 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
220 "pending station mode scan to be "
221 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700222 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700223 wpa_supplicant_req_scan(ifs, 0, 0);
224 return 1;
225 }
226 }
227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 os_memset(&params, 0, sizeof(params));
229
230 /* P2P Wildcard SSID */
231 params.num_ssids = 1;
232 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
233 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
234
235 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700236 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
237 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238 num_req_dev_types, req_dev_types);
239 if (wps_ie == NULL)
240 return -1;
241
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800242 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
243 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 if (ies == NULL) {
245 wpabuf_free(wps_ie);
246 return -1;
247 }
248 wpabuf_put_buf(ies, wps_ie);
249 wpabuf_free(wps_ie);
250
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800251 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800253 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 params.extra_ies = wpabuf_head(ies);
255 params.extra_ies_len = wpabuf_len(ies);
256
257 switch (type) {
258 case P2P_SCAN_SOCIAL:
259 params.freqs = social_channels;
260 break;
261 case P2P_SCAN_FULL:
262 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263 case P2P_SCAN_SOCIAL_PLUS_ONE:
264 social_channels[3] = freq;
265 params.freqs = social_channels;
266 break;
267 }
268
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800269 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270
271 wpabuf_free(ies);
272
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800273 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700274 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
275 if (ifs->scanning ||
276 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
277 wpa_s->global->p2p_cb_on_scan_complete = 1;
278 ret = 1;
279 break;
280 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800281 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700282 } else {
283 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700284 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700285 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 return ret;
288}
289
290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
292{
293 switch (p2p_group_interface) {
294 case P2P_GROUP_INTERFACE_PENDING:
295 return WPA_IF_P2P_GROUP;
296 case P2P_GROUP_INTERFACE_GO:
297 return WPA_IF_P2P_GO;
298 case P2P_GROUP_INTERFACE_CLIENT:
299 return WPA_IF_P2P_CLIENT;
300 }
301
302 return WPA_IF_P2P_GROUP;
303}
304
305
306static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
307 const u8 *ssid,
308 size_t ssid_len, int *go)
309{
310 struct wpa_ssid *s;
311
312 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
313 for (s = wpa_s->conf->ssid; s; s = s->next) {
314 if (s->disabled != 0 || !s->p2p_group ||
315 s->ssid_len != ssid_len ||
316 os_memcmp(ssid, s->ssid, ssid_len) != 0)
317 continue;
318 if (s->mode == WPAS_MODE_P2P_GO &&
319 s != wpa_s->current_ssid)
320 continue;
321 if (go)
322 *go = s->mode == WPAS_MODE_P2P_GO;
323 return wpa_s;
324 }
325 }
326
327 return NULL;
328}
329
330
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700331static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
332 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333{
334 struct wpa_ssid *ssid;
335 char *gtype;
336 const char *reason;
337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 ssid = wpa_s->current_ssid;
339 if (ssid == NULL) {
340 /*
341 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700342 * pending P2P group interface waiting for provisioning or a
343 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344 */
345 ssid = wpa_s->conf->ssid;
346 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700347 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 break;
349 ssid = ssid->next;
350 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300351 if (ssid == NULL &&
352 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
353 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700354 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
355 "not found");
356 return -1;
357 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358 }
359 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
360 gtype = "GO";
361 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
362 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
363 wpa_s->reassociate = 0;
364 wpa_s->disconnected = 1;
365 wpa_supplicant_deauthenticate(wpa_s,
366 WLAN_REASON_DEAUTH_LEAVING);
367 gtype = "client";
368 } else
369 gtype = "GO";
370 if (wpa_s->cross_connect_in_use) {
371 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700372 wpa_msg_global(wpa_s->parent, MSG_INFO,
373 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
374 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700376 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377 case P2P_GROUP_REMOVAL_REQUESTED:
378 reason = " reason=REQUESTED";
379 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700380 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
381 reason = " reason=FORMATION_FAILED";
382 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
384 reason = " reason=IDLE";
385 break;
386 case P2P_GROUP_REMOVAL_UNAVAILABLE:
387 reason = " reason=UNAVAILABLE";
388 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700389 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
390 reason = " reason=GO_ENDING_SESSION";
391 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700392#ifdef ANDROID_P2P
393 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
394 reason = " reason=FREQ_CONFLICT";
395 break;
396#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397 default:
398 reason = "";
399 break;
400 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700401 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700402 wpa_msg_global(wpa_s->parent, MSG_INFO,
403 P2P_EVENT_GROUP_REMOVED "%s %s%s",
404 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700405 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700407#ifdef ANDROID_P2P
408 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
409#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700410 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
411 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800412 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700413 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800414 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
415 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700416 wpa_s->p2p_in_provisioning = 0;
417 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700418
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700419 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
421
422 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
423 struct wpa_global *global;
424 char *ifname;
425 enum wpa_driver_if_type type;
426 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
427 wpa_s->ifname);
428 global = wpa_s->global;
429 ifname = os_strdup(wpa_s->ifname);
430 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700431 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432 wpa_s = global->ifaces;
433 if (wpa_s && ifname)
434 wpa_drv_if_remove(wpa_s, type, ifname);
435 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300436 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 }
438
439 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
440 if (ssid && (ssid->p2p_group ||
441 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
442 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
443 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700444 if (ssid == wpa_s->current_ssid) {
445 wpa_sm_set_config(wpa_s->wpa, NULL);
446 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700448 }
449 /*
450 * Networks objects created during any P2P activities are not
451 * exposed out as they might/will confuse certain non-P2P aware
452 * applications since these network objects won't behave like
453 * regular ones.
454 *
455 * Likewise, we don't send out network removed signals for such
456 * network objects.
457 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 wpa_config_remove_network(wpa_s->conf, id);
459 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800460 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700461 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 } else {
463 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
464 "found");
465 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700466 if (wpa_s->ap_iface)
467 wpa_supplicant_ap_deinit(wpa_s);
468 else
469 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700470
471 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472}
473
474
475static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
476 u8 *go_dev_addr,
477 const u8 *ssid, size_t ssid_len)
478{
479 struct wpa_bss *bss;
480 const u8 *bssid;
481 struct wpabuf *p2p;
482 u8 group_capab;
483 const u8 *addr;
484
485 if (wpa_s->go_params)
486 bssid = wpa_s->go_params->peer_interface_addr;
487 else
488 bssid = wpa_s->bssid;
489
490 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
491 if (bss == NULL) {
492 u8 iface_addr[ETH_ALEN];
493 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
494 iface_addr) == 0)
495 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
496 }
497 if (bss == NULL) {
498 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
499 "group is persistent - BSS " MACSTR " not found",
500 MAC2STR(bssid));
501 return 0;
502 }
503
504 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
505 if (p2p == NULL) {
506 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
507 "group is persistent - BSS " MACSTR
508 " did not include P2P IE", MAC2STR(bssid));
509 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
510 (u8 *) (bss + 1), bss->ie_len);
511 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
512 ((u8 *) bss + 1) + bss->ie_len,
513 bss->beacon_ie_len);
514 return 0;
515 }
516
517 group_capab = p2p_get_group_capab(p2p);
518 addr = p2p_get_go_dev_addr(p2p);
519 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
520 "group_capab=0x%x", group_capab);
521 if (addr) {
522 os_memcpy(go_dev_addr, addr, ETH_ALEN);
523 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
524 MAC2STR(addr));
525 } else
526 os_memset(go_dev_addr, 0, ETH_ALEN);
527 wpabuf_free(p2p);
528
529 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
530 "go_dev_addr=" MACSTR,
531 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
532
533 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
534}
535
536
Jouni Malinen75ecf522011-06-27 15:19:46 -0700537static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
538 struct wpa_ssid *ssid,
539 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540{
541 struct wpa_ssid *s;
542 int changed = 0;
543
544 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
545 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
546 for (s = wpa_s->conf->ssid; s; s = s->next) {
547 if (s->disabled == 2 &&
548 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
549 s->ssid_len == ssid->ssid_len &&
550 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
551 break;
552 }
553
554 if (s) {
555 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
556 "entry");
557 if (ssid->passphrase && !s->passphrase)
558 changed = 1;
559 else if (ssid->passphrase && s->passphrase &&
560 os_strcmp(ssid->passphrase, s->passphrase) != 0)
561 changed = 1;
562 } else {
563 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
564 "entry");
565 changed = 1;
566 s = wpa_config_add_network(wpa_s->conf);
567 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700568 return -1;
569
570 /*
571 * Instead of network_added we emit persistent_group_added
572 * notification. Also to keep the defense checks in
573 * persistent_group obj registration method, we set the
574 * relevant flags in s to designate it as a persistent group.
575 */
576 s->p2p_group = 1;
577 s->p2p_persistent_group = 1;
578 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579 wpa_config_set_network_defaults(s);
580 }
581
582 s->p2p_group = 1;
583 s->p2p_persistent_group = 1;
584 s->disabled = 2;
585 s->bssid_set = 1;
586 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
587 s->mode = ssid->mode;
588 s->auth_alg = WPA_AUTH_ALG_OPEN;
589 s->key_mgmt = WPA_KEY_MGMT_PSK;
590 s->proto = WPA_PROTO_RSN;
591 s->pairwise_cipher = WPA_CIPHER_CCMP;
592 s->export_keys = 1;
593 if (ssid->passphrase) {
594 os_free(s->passphrase);
595 s->passphrase = os_strdup(ssid->passphrase);
596 }
597 if (ssid->psk_set) {
598 s->psk_set = 1;
599 os_memcpy(s->psk, ssid->psk, 32);
600 }
601 if (s->passphrase && !s->psk_set)
602 wpa_config_update_psk(s);
603 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
604 os_free(s->ssid);
605 s->ssid = os_malloc(ssid->ssid_len);
606 }
607 if (s->ssid) {
608 s->ssid_len = ssid->ssid_len;
609 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
610 }
611
612#ifndef CONFIG_NO_CONFIG_WRITE
613 if (changed && wpa_s->conf->update_config &&
614 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
615 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
616 }
617#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700618
619 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620}
621
622
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800623static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
624 const u8 *addr)
625{
626 struct wpa_ssid *ssid, *s;
627 u8 *n;
628 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700629 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800630
631 ssid = wpa_s->current_ssid;
632 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
633 !ssid->p2p_persistent_group)
634 return;
635
636 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
637 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
638 continue;
639
640 if (s->ssid_len == ssid->ssid_len &&
641 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
642 break;
643 }
644
645 if (s == NULL)
646 return;
647
648 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
649 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700650 ETH_ALEN) != 0)
651 continue;
652
653 if (i == s->num_p2p_clients - 1)
654 return; /* already the most recent entry */
655
656 /* move the entry to mark it most recent */
657 os_memmove(s->p2p_client_list + i * ETH_ALEN,
658 s->p2p_client_list + (i + 1) * ETH_ALEN,
659 (s->num_p2p_clients - i - 1) * ETH_ALEN);
660 os_memcpy(s->p2p_client_list +
661 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
662 found = 1;
663 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800664 }
665
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700666 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
667 n = os_realloc_array(s->p2p_client_list,
668 s->num_p2p_clients + 1, ETH_ALEN);
669 if (n == NULL)
670 return;
671 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
672 s->p2p_client_list = n;
673 s->num_p2p_clients++;
674 } else if (!found) {
675 /* Not enough room for an additional entry - drop the oldest
676 * entry */
677 os_memmove(s->p2p_client_list,
678 s->p2p_client_list + ETH_ALEN,
679 (s->num_p2p_clients - 1) * ETH_ALEN);
680 os_memcpy(s->p2p_client_list +
681 (s->num_p2p_clients - 1) * ETH_ALEN,
682 addr, ETH_ALEN);
683 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800684
685#ifndef CONFIG_NO_CONFIG_WRITE
686 if (wpa_s->parent->conf->update_config &&
687 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
688 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
689#endif /* CONFIG_NO_CONFIG_WRITE */
690}
691
692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
694 int success)
695{
696 struct wpa_ssid *ssid;
697 const char *ssid_txt;
698 int client;
699 int persistent;
700 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700701 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702
703 /*
704 * This callback is likely called for the main interface. Update wpa_s
705 * to use the group interface if a new interface was created for the
706 * group.
707 */
708 if (wpa_s->global->p2p_group_formation)
709 wpa_s = wpa_s->global->p2p_group_formation;
710 wpa_s->global->p2p_group_formation = NULL;
711 wpa_s->p2p_in_provisioning = 0;
712
713 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700714 wpa_msg_global(wpa_s->parent, MSG_INFO,
715 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700716 wpas_p2p_group_delete(wpa_s,
717 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718 return;
719 }
720
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700721 wpa_msg_global(wpa_s->parent, MSG_INFO,
722 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723
724 ssid = wpa_s->current_ssid;
725 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
726 ssid->mode = WPAS_MODE_P2P_GO;
727 p2p_group_notif_formation_done(wpa_s->p2p_group);
728 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
729 }
730
731 persistent = 0;
732 if (ssid) {
733 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
734 client = ssid->mode == WPAS_MODE_INFRA;
735 if (ssid->mode == WPAS_MODE_P2P_GO) {
736 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700737 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
738 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739 } else
740 persistent = wpas_p2p_persistent_group(wpa_s,
741 go_dev_addr,
742 ssid->ssid,
743 ssid->ssid_len);
744 } else {
745 ssid_txt = "";
746 client = wpa_s->p2p_group_interface ==
747 P2P_GROUP_INTERFACE_CLIENT;
748 os_memset(go_dev_addr, 0, ETH_ALEN);
749 }
750
751 wpa_s->show_group_started = 0;
752 if (client) {
753 /*
754 * Indicate event only after successfully completed 4-way
755 * handshake, i.e., when the interface is ready for data
756 * packets.
757 */
758 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700759#ifdef ANDROID_P2P
760 /* For client Second phase of Group formation (4-way handshake) can be still pending
761 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700762 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 -0700763 wpa_s->global->p2p_group_formation = wpa_s;
764#endif
765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
767 char psk[65];
768 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700769 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
770 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
771 MACSTR "%s",
772 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
773 MAC2STR(go_dev_addr),
774 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 wpas_p2p_cross_connect_setup(wpa_s);
776 wpas_p2p_set_group_idle_timeout(wpa_s);
777 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700778 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
779 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
780 "go_dev_addr=" MACSTR "%s",
781 wpa_s->ifname, ssid_txt,
782 ssid ? ssid->frequency : 0,
783 ssid && ssid->passphrase ? ssid->passphrase : "",
784 MAC2STR(go_dev_addr),
785 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786 wpas_p2p_cross_connect_setup(wpa_s);
787 wpas_p2p_set_group_idle_timeout(wpa_s);
788 }
789
790 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700791 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
792 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800793 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700794 network_id = ssid->id;
795 if (!client)
796 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797}
798
799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
801 unsigned int freq,
802 const u8 *dst, const u8 *src,
803 const u8 *bssid,
804 const u8 *data, size_t data_len,
805 enum offchannel_send_action_result
806 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800808 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
811 return;
812 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
813 return;
814
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800815 switch (result) {
816 case OFFCHANNEL_SEND_ACTION_SUCCESS:
817 res = P2P_SEND_ACTION_SUCCESS;
818 break;
819 case OFFCHANNEL_SEND_ACTION_NO_ACK:
820 res = P2P_SEND_ACTION_NO_ACK;
821 break;
822 case OFFCHANNEL_SEND_ACTION_FAILED:
823 res = P2P_SEND_ACTION_FAILED;
824 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825 }
826
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800827 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800829 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
830 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800831 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800832 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
833 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800835 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
836 "during p2p_connect-auto");
837 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
838 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839 }
840}
841
842
843static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
844 const u8 *src, const u8 *bssid, const u8 *buf,
845 size_t len, unsigned int wait_time)
846{
847 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800848 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
849 wait_time,
850 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851}
852
853
854static void wpas_send_action_done(void *ctx)
855{
856 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800857 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858}
859
860
861static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
862 struct p2p_go_neg_results *params)
863{
864 if (wpa_s->go_params == NULL) {
865 wpa_s->go_params = os_malloc(sizeof(*params));
866 if (wpa_s->go_params == NULL)
867 return -1;
868 }
869 os_memcpy(wpa_s->go_params, params, sizeof(*params));
870 return 0;
871}
872
873
874static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
875 struct p2p_go_neg_results *res)
876{
877 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
878 MAC2STR(res->peer_interface_addr));
879 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
880 res->ssid, res->ssid_len);
881 wpa_supplicant_ap_deinit(wpa_s);
882 wpas_copy_go_neg_results(wpa_s, res);
883 if (res->wps_method == WPS_PBC)
884 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
885 else {
886 u16 dev_pw_id = DEV_PW_DEFAULT;
887 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
888 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
889 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
890 wpa_s->p2p_pin, 1, dev_pw_id);
891 }
892}
893
894
895static void p2p_go_configured(void *ctx, void *data)
896{
897 struct wpa_supplicant *wpa_s = ctx;
898 struct p2p_go_neg_results *params = data;
899 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700900 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901
902 ssid = wpa_s->current_ssid;
903 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
904 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
905 if (wpa_s->global->p2p_group_formation == wpa_s)
906 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800907 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700908 wpa_msg_global(wpa_s->parent, MSG_INFO,
909 P2P_EVENT_GROUP_STARTED
910 "%s GO ssid=\"%s\" freq=%d "
911 "passphrase=\"%s\" go_dev_addr=" MACSTR
912 "%s", wpa_s->ifname,
913 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
914 ssid->frequency, params->passphrase,
915 MAC2STR(wpa_s->global->p2p_dev_addr),
916 params->persistent_group ?
917 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800918 } else {
919 char psk[65];
920 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
921 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700922 wpa_msg_global(wpa_s->parent, MSG_INFO,
923 P2P_EVENT_GROUP_STARTED
924 "%s GO ssid=\"%s\" freq=%d psk=%s "
925 "go_dev_addr=" MACSTR "%s",
926 wpa_s->ifname,
927 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
928 ssid->frequency, psk,
929 MAC2STR(wpa_s->global->p2p_dev_addr),
930 params->persistent_group ?
931 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800932 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700935 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700937 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700938 if (network_id < 0)
939 network_id = ssid->id;
940 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 wpas_p2p_cross_connect_setup(wpa_s);
942 wpas_p2p_set_group_idle_timeout(wpa_s);
943 return;
944 }
945
946 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
947 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
948 params->peer_interface_addr)) {
949 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
950 "filtering");
951 return;
952 }
953 if (params->wps_method == WPS_PBC)
954 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800955 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 else if (wpa_s->p2p_pin[0])
957 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800958 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 os_free(wpa_s->go_params);
960 wpa_s->go_params = NULL;
961}
962
963
964static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
965 struct p2p_go_neg_results *params,
966 int group_formation)
967{
968 struct wpa_ssid *ssid;
969
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700970 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
971 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
972 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
973 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976
977 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700978 if (ssid == NULL) {
979 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700981 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800983 wpa_s->show_group_started = 0;
984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 wpa_config_set_network_defaults(ssid);
986 ssid->temporary = 1;
987 ssid->p2p_group = 1;
988 ssid->p2p_persistent_group = params->persistent_group;
989 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
990 WPAS_MODE_P2P_GO;
991 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700992 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993 ssid->ssid = os_zalloc(params->ssid_len + 1);
994 if (ssid->ssid) {
995 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
996 ssid->ssid_len = params->ssid_len;
997 }
998 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
999 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1000 ssid->proto = WPA_PROTO_RSN;
1001 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001002 if (os_strlen(params->passphrase) > 0) {
1003 ssid->passphrase = os_strdup(params->passphrase);
1004 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001005 wpa_msg_global(wpa_s, MSG_ERROR,
1006 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001007 wpa_config_remove_network(wpa_s->conf, ssid->id);
1008 return;
1009 }
1010 } else
1011 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001012 ssid->psk_set = params->psk_set;
1013 if (ssid->psk_set)
1014 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001015 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001016 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001017 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018
1019 wpa_s->ap_configured_cb = p2p_go_configured;
1020 wpa_s->ap_configured_cb_ctx = wpa_s;
1021 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001022 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 wpa_s->reassociate = 1;
1024 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001025 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1026 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027 wpa_supplicant_req_scan(wpa_s, 0, 0);
1028}
1029
1030
1031static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1032 const struct wpa_supplicant *src)
1033{
1034 struct wpa_config *d;
1035 const struct wpa_config *s;
1036
1037 d = dst->conf;
1038 s = src->conf;
1039
1040#define C(n) if (s->n) d->n = os_strdup(s->n)
1041 C(device_name);
1042 C(manufacturer);
1043 C(model_name);
1044 C(model_number);
1045 C(serial_number);
1046 C(config_methods);
1047#undef C
1048
1049 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1050 os_memcpy(d->sec_device_type, s->sec_device_type,
1051 sizeof(d->sec_device_type));
1052 d->num_sec_device_types = s->num_sec_device_types;
1053
1054 d->p2p_group_idle = s->p2p_group_idle;
1055 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001056 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001057 d->max_num_sta = s->max_num_sta;
1058 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001059 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001060 d->beacon_int = s->beacon_int;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061}
1062
1063
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001064static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1065 char *ifname, size_t len)
1066{
1067 char *ifname_ptr = wpa_s->ifname;
1068
1069 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1070 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1071 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1072 }
1073
1074 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1075 if (os_strlen(ifname) >= IFNAMSIZ &&
1076 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1077 /* Try to avoid going over the IFNAMSIZ length limit */
1078 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1079 wpa_s->p2p_group_idx);
1080 }
1081}
1082
1083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1085 enum wpa_driver_if_type type)
1086{
1087 char ifname[120], force_ifname[120];
1088
1089 if (wpa_s->pending_interface_name[0]) {
1090 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1091 "- skip creation of a new one");
1092 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1093 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1094 "unknown?! ifname='%s'",
1095 wpa_s->pending_interface_name);
1096 return -1;
1097 }
1098 return 0;
1099 }
1100
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001101 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 force_ifname[0] = '\0';
1103
1104 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1105 ifname);
1106 wpa_s->p2p_group_idx++;
1107
1108 wpa_s->pending_interface_type = type;
1109 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1110 wpa_s->pending_interface_addr, NULL) < 0) {
1111 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1112 "interface");
1113 return -1;
1114 }
1115
1116 if (force_ifname[0]) {
1117 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1118 force_ifname);
1119 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1120 sizeof(wpa_s->pending_interface_name));
1121 } else
1122 os_strlcpy(wpa_s->pending_interface_name, ifname,
1123 sizeof(wpa_s->pending_interface_name));
1124 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1125 MACSTR, wpa_s->pending_interface_name,
1126 MAC2STR(wpa_s->pending_interface_addr));
1127
1128 return 0;
1129}
1130
1131
1132static void wpas_p2p_remove_pending_group_interface(
1133 struct wpa_supplicant *wpa_s)
1134{
1135 if (!wpa_s->pending_interface_name[0] ||
1136 is_zero_ether_addr(wpa_s->pending_interface_addr))
1137 return; /* No pending virtual interface */
1138
1139 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1140 wpa_s->pending_interface_name);
1141 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1142 wpa_s->pending_interface_name);
1143 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1144 wpa_s->pending_interface_name[0] = '\0';
1145}
1146
1147
1148static struct wpa_supplicant *
1149wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1150{
1151 struct wpa_interface iface;
1152 struct wpa_supplicant *group_wpa_s;
1153
1154 if (!wpa_s->pending_interface_name[0]) {
1155 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1156 if (!wpas_p2p_create_iface(wpa_s))
1157 return NULL;
1158 /*
1159 * Something has forced us to remove the pending interface; try
1160 * to create a new one and hope for the best that we will get
1161 * the same local address.
1162 */
1163 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1164 WPA_IF_P2P_CLIENT) < 0)
1165 return NULL;
1166 }
1167
1168 os_memset(&iface, 0, sizeof(iface));
1169 iface.ifname = wpa_s->pending_interface_name;
1170 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001171 if (wpa_s->conf->ctrl_interface == NULL &&
1172 wpa_s->parent != wpa_s &&
1173 wpa_s->p2p_mgmt &&
1174 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1175 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1176 else
1177 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 iface.driver_param = wpa_s->conf->driver_param;
1179 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1180 if (group_wpa_s == NULL) {
1181 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1182 "wpa_supplicant interface");
1183 return NULL;
1184 }
1185 wpa_s->pending_interface_name[0] = '\0';
1186 group_wpa_s->parent = wpa_s;
1187 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1188 P2P_GROUP_INTERFACE_CLIENT;
1189 wpa_s->global->p2p_group_formation = group_wpa_s;
1190
1191 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1192
1193 return group_wpa_s;
1194}
1195
1196
1197static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1198 void *timeout_ctx)
1199{
1200 struct wpa_supplicant *wpa_s = eloop_ctx;
1201 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001202 wpas_p2p_group_formation_failed(wpa_s);
1203}
1204
1205
1206void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1207{
1208 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1209 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 if (wpa_s->global->p2p)
1211 p2p_group_formation_failed(wpa_s->global->p2p);
1212 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1213 wpa_drv_p2p_group_formation_failed(wpa_s);
1214 wpas_group_formation_completed(wpa_s, 0);
1215}
1216
1217
1218void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1219{
1220 struct wpa_supplicant *wpa_s = ctx;
1221
1222 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1223 wpa_drv_cancel_remain_on_channel(wpa_s);
1224 wpa_s->off_channel_freq = 0;
1225 wpa_s->roc_waiting_drv_freq = 0;
1226 }
1227
1228 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001229 wpa_msg_global(wpa_s, MSG_INFO,
1230 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1231 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001232 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 wpas_p2p_remove_pending_group_interface(wpa_s);
1234 return;
1235 }
1236
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001237 if (wpa_s->p2p_go_ht40)
1238 res->ht40 = 1;
1239
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001240 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001241 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242
Dmitry Shmidt04949592012-07-19 12:16:46 -07001243 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1244 struct wpa_ssid *ssid;
1245 ssid = wpa_config_get_network(wpa_s->conf,
1246 wpa_s->p2p_persistent_id);
1247 if (ssid && ssid->disabled == 2 &&
1248 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1249 size_t len = os_strlen(ssid->passphrase);
1250 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1251 "on requested persistent group");
1252 os_memcpy(res->passphrase, ssid->passphrase, len);
1253 res->passphrase[len] = '\0';
1254 }
1255 }
1256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001257 if (wpa_s->create_p2p_iface) {
1258 struct wpa_supplicant *group_wpa_s =
1259 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1260 if (group_wpa_s == NULL) {
1261 wpas_p2p_remove_pending_group_interface(wpa_s);
1262 return;
1263 }
1264 if (group_wpa_s != wpa_s) {
1265 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1266 sizeof(group_wpa_s->p2p_pin));
1267 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1268 }
1269 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1270 wpa_s->pending_interface_name[0] = '\0';
1271 group_wpa_s->p2p_in_provisioning = 1;
1272
1273 if (res->role_go)
1274 wpas_start_wps_go(group_wpa_s, res, 1);
1275 else
1276 wpas_start_wps_enrollee(group_wpa_s, res);
1277 } else {
1278 wpa_s->p2p_in_provisioning = 1;
1279 wpa_s->global->p2p_group_formation = wpa_s;
1280
1281 if (res->role_go)
1282 wpas_start_wps_go(wpa_s, res, 1);
1283 else
1284 wpas_start_wps_enrollee(ctx, res);
1285 }
1286
1287 wpa_s->p2p_long_listen = 0;
1288 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1289
1290 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1291 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1292 (res->peer_config_timeout % 100) * 10000,
1293 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1294}
1295
1296
1297void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1298{
1299 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001300 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1301 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302
1303 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1304}
1305
1306
1307void wpas_dev_found(void *ctx, const u8 *addr,
1308 const struct p2p_peer_info *info,
1309 int new_device)
1310{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001311#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 struct wpa_supplicant *wpa_s = ctx;
1313 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001314#define WFD_DEV_INFO_SIZE 9
1315 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001316 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001317#ifdef CONFIG_WIFI_DISPLAY
1318 if (info->wfd_subelems) {
1319 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1320 wpabuf_head(info->wfd_subelems),
1321 WFD_DEV_INFO_SIZE);
1322 }
1323#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001324 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1325 " p2p_dev_addr=" MACSTR
1326 " pri_dev_type=%s name='%s' config_methods=0x%x "
1327 "dev_capab=0x%x group_capab=0x%x%s%s",
1328 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1329 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1330 sizeof(devtype)),
1331 info->device_name, info->config_methods,
1332 info->dev_capab, info->group_capab,
1333 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001334#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335
1336 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1337}
1338
1339
1340static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1341{
1342 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001343
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001344 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1345 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1348}
1349
1350
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001351static void wpas_find_stopped(void *ctx)
1352{
1353 struct wpa_supplicant *wpa_s = ctx;
1354 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1355}
1356
1357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358static int wpas_start_listen(void *ctx, unsigned int freq,
1359 unsigned int duration,
1360 const struct wpabuf *probe_resp_ie)
1361{
1362 struct wpa_supplicant *wpa_s = ctx;
1363
1364 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1365
1366 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1367 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1368 "report received Probe Request frames");
1369 return -1;
1370 }
1371
1372 wpa_s->pending_listen_freq = freq;
1373 wpa_s->pending_listen_duration = duration;
1374
1375 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1376 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1377 "to remain on channel (%u MHz) for Listen "
1378 "state", freq);
1379 wpa_s->pending_listen_freq = 0;
1380 return -1;
1381 }
1382 wpa_s->off_channel_freq = 0;
1383 wpa_s->roc_waiting_drv_freq = freq;
1384
1385 return 0;
1386}
1387
1388
1389static void wpas_stop_listen(void *ctx)
1390{
1391 struct wpa_supplicant *wpa_s = ctx;
1392 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1393 wpa_drv_cancel_remain_on_channel(wpa_s);
1394 wpa_s->off_channel_freq = 0;
1395 wpa_s->roc_waiting_drv_freq = 0;
1396 }
1397 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1398 wpa_drv_probe_req_report(wpa_s, 0);
1399}
1400
1401
1402static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1403{
1404 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001405 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406}
1407
1408
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001409/*
1410 * DNS Header section is used only to calculate compression pointers, so the
1411 * contents of this data does not matter, but the length needs to be reserved
1412 * in the virtual packet.
1413 */
1414#define DNS_HEADER_LEN 12
1415
1416/*
1417 * 27-octet in-memory packet from P2P specification containing two implied
1418 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1419 */
1420#define P2P_SD_IN_MEMORY_LEN 27
1421
1422static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1423 u8 **spos, const u8 *end)
1424{
1425 while (*spos < end) {
1426 u8 val = ((*spos)[0] & 0xc0) >> 6;
1427 int len;
1428
1429 if (val == 1 || val == 2) {
1430 /* These are reserved values in RFC 1035 */
1431 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1432 "sequence starting with 0x%x", val);
1433 return -1;
1434 }
1435
1436 if (val == 3) {
1437 u16 offset;
1438 u8 *spos_tmp;
1439
1440 /* Offset */
1441 if (*spos + 2 > end) {
1442 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1443 "DNS offset field");
1444 return -1;
1445 }
1446
1447 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1448 if (offset >= *spos - start) {
1449 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1450 "pointer offset %u", offset);
1451 return -1;
1452 }
1453
1454 (*spos) += 2;
1455 spos_tmp = start + offset;
1456 return p2p_sd_dns_uncompress_label(upos, uend, start,
1457 &spos_tmp,
1458 *spos - 2);
1459 }
1460
1461 /* Label */
1462 len = (*spos)[0] & 0x3f;
1463 if (len == 0)
1464 return 0;
1465
1466 (*spos)++;
1467 if (*spos + len > end) {
1468 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1469 "sequence - no room for label with length "
1470 "%u", len);
1471 return -1;
1472 }
1473
1474 if (*upos + len + 2 > uend)
1475 return -2;
1476
1477 os_memcpy(*upos, *spos, len);
1478 *spos += len;
1479 *upos += len;
1480 (*upos)[0] = '.';
1481 (*upos)++;
1482 (*upos)[0] = '\0';
1483 }
1484
1485 return 0;
1486}
1487
1488
1489/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1490 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1491 * not large enough */
1492static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1493 size_t msg_len, size_t offset)
1494{
1495 /* 27-octet in-memory packet from P2P specification */
1496 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1497 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1498 u8 *tmp, *end, *spos;
1499 char *upos, *uend;
1500 int ret = 0;
1501
1502 if (buf_len < 2)
1503 return -1;
1504 if (offset > msg_len)
1505 return -1;
1506
1507 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1508 if (tmp == NULL)
1509 return -1;
1510 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1511 end = spos + msg_len;
1512 spos += offset;
1513
1514 os_memset(tmp, 0, DNS_HEADER_LEN);
1515 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1516 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1517
1518 upos = buf;
1519 uend = buf + buf_len;
1520
1521 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1522 if (ret) {
1523 os_free(tmp);
1524 return ret;
1525 }
1526
1527 if (upos == buf) {
1528 upos[0] = '.';
1529 upos[1] = '\0';
1530 } else if (upos[-1] == '.')
1531 upos[-1] = '\0';
1532
1533 os_free(tmp);
1534 return 0;
1535}
1536
1537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538static struct p2p_srv_bonjour *
1539wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1540 const struct wpabuf *query)
1541{
1542 struct p2p_srv_bonjour *bsrv;
1543 size_t len;
1544
1545 len = wpabuf_len(query);
1546 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1547 struct p2p_srv_bonjour, list) {
1548 if (len == wpabuf_len(bsrv->query) &&
1549 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1550 len) == 0)
1551 return bsrv;
1552 }
1553 return NULL;
1554}
1555
1556
1557static struct p2p_srv_upnp *
1558wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1559 const char *service)
1560{
1561 struct p2p_srv_upnp *usrv;
1562
1563 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1564 struct p2p_srv_upnp, list) {
1565 if (version == usrv->version &&
1566 os_strcmp(service, usrv->service) == 0)
1567 return usrv;
1568 }
1569 return NULL;
1570}
1571
1572
1573static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1574 u8 srv_trans_id)
1575{
1576 u8 *len_pos;
1577
1578 if (wpabuf_tailroom(resp) < 5)
1579 return;
1580
1581 /* Length (to be filled) */
1582 len_pos = wpabuf_put(resp, 2);
1583 wpabuf_put_u8(resp, srv_proto);
1584 wpabuf_put_u8(resp, srv_trans_id);
1585 /* Status Code */
1586 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1587 /* Response Data: empty */
1588 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1589}
1590
1591
1592static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1593 struct wpabuf *resp, u8 srv_trans_id)
1594{
1595 struct p2p_srv_bonjour *bsrv;
1596 u8 *len_pos;
1597
1598 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1599
1600 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1601 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1602 return;
1603 }
1604
1605 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1606 struct p2p_srv_bonjour, list) {
1607 if (wpabuf_tailroom(resp) <
1608 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1609 return;
1610 /* Length (to be filled) */
1611 len_pos = wpabuf_put(resp, 2);
1612 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1613 wpabuf_put_u8(resp, srv_trans_id);
1614 /* Status Code */
1615 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1616 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1617 wpabuf_head(bsrv->resp),
1618 wpabuf_len(bsrv->resp));
1619 /* Response Data */
1620 wpabuf_put_buf(resp, bsrv->query); /* Key */
1621 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1622 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1623 2);
1624 }
1625}
1626
1627
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001628static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1629 size_t query_len)
1630{
1631 char str_rx[256], str_srv[256];
1632
1633 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1634 return 0; /* Too short to include DNS Type and Version */
1635 if (os_memcmp(query + query_len - 3,
1636 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1637 3) != 0)
1638 return 0; /* Mismatch in DNS Type or Version */
1639 if (query_len == wpabuf_len(bsrv->query) &&
1640 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1641 return 1; /* Binary match */
1642
1643 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1644 0))
1645 return 0; /* Failed to uncompress query */
1646 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1647 wpabuf_head(bsrv->query),
1648 wpabuf_len(bsrv->query) - 3, 0))
1649 return 0; /* Failed to uncompress service */
1650
1651 return os_strcmp(str_rx, str_srv) == 0;
1652}
1653
1654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1656 struct wpabuf *resp, u8 srv_trans_id,
1657 const u8 *query, size_t query_len)
1658{
1659 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001661 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662
1663 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1664 query, query_len);
1665 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1666 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1667 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1668 srv_trans_id);
1669 return;
1670 }
1671
1672 if (query_len == 0) {
1673 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1674 return;
1675 }
1676
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001677 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1678 struct p2p_srv_bonjour, list) {
1679 if (!match_bonjour_query(bsrv, query, query_len))
1680 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001682 if (wpabuf_tailroom(resp) <
1683 5 + query_len + wpabuf_len(bsrv->resp))
1684 return;
1685
1686 matches++;
1687
1688 /* Length (to be filled) */
1689 len_pos = wpabuf_put(resp, 2);
1690 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1691 wpabuf_put_u8(resp, srv_trans_id);
1692
1693 /* Status Code */
1694 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1695 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1696 wpabuf_head(bsrv->resp),
1697 wpabuf_len(bsrv->resp));
1698
1699 /* Response Data */
1700 wpabuf_put_data(resp, query, query_len); /* Key */
1701 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1702
1703 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1704 }
1705
1706 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1708 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001709 if (wpabuf_tailroom(resp) < 5)
1710 return;
1711
1712 /* Length (to be filled) */
1713 len_pos = wpabuf_put(resp, 2);
1714 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1715 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001716
1717 /* Status Code */
1718 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1719 /* Response Data: empty */
1720 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1721 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723}
1724
1725
1726static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1727 struct wpabuf *resp, u8 srv_trans_id)
1728{
1729 struct p2p_srv_upnp *usrv;
1730 u8 *len_pos;
1731
1732 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1733
1734 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1735 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1736 return;
1737 }
1738
1739 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1740 struct p2p_srv_upnp, list) {
1741 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1742 return;
1743
1744 /* Length (to be filled) */
1745 len_pos = wpabuf_put(resp, 2);
1746 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1747 wpabuf_put_u8(resp, srv_trans_id);
1748
1749 /* Status Code */
1750 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1751 /* Response Data */
1752 wpabuf_put_u8(resp, usrv->version);
1753 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1754 usrv->service);
1755 wpabuf_put_str(resp, usrv->service);
1756 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1757 2);
1758 }
1759}
1760
1761
1762static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1763 struct wpabuf *resp, u8 srv_trans_id,
1764 const u8 *query, size_t query_len)
1765{
1766 struct p2p_srv_upnp *usrv;
1767 u8 *len_pos;
1768 u8 version;
1769 char *str;
1770 int count = 0;
1771
1772 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1773 query, query_len);
1774
1775 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1776 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1777 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1778 srv_trans_id);
1779 return;
1780 }
1781
1782 if (query_len == 0) {
1783 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1784 return;
1785 }
1786
1787 if (wpabuf_tailroom(resp) < 5)
1788 return;
1789
1790 /* Length (to be filled) */
1791 len_pos = wpabuf_put(resp, 2);
1792 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1793 wpabuf_put_u8(resp, srv_trans_id);
1794
1795 version = query[0];
1796 str = os_malloc(query_len);
1797 if (str == NULL)
1798 return;
1799 os_memcpy(str, query + 1, query_len - 1);
1800 str[query_len - 1] = '\0';
1801
1802 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1803 struct p2p_srv_upnp, list) {
1804 if (version != usrv->version)
1805 continue;
1806
1807 if (os_strcmp(str, "ssdp:all") != 0 &&
1808 os_strstr(usrv->service, str) == NULL)
1809 continue;
1810
1811 if (wpabuf_tailroom(resp) < 2)
1812 break;
1813 if (count == 0) {
1814 /* Status Code */
1815 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1816 /* Response Data */
1817 wpabuf_put_u8(resp, version);
1818 } else
1819 wpabuf_put_u8(resp, ',');
1820
1821 count++;
1822
1823 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1824 usrv->service);
1825 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1826 break;
1827 wpabuf_put_str(resp, usrv->service);
1828 }
1829 os_free(str);
1830
1831 if (count == 0) {
1832 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1833 "available");
1834 /* Status Code */
1835 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1836 /* Response Data: empty */
1837 }
1838
1839 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1840}
1841
1842
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001843#ifdef CONFIG_WIFI_DISPLAY
1844static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1845 struct wpabuf *resp, u8 srv_trans_id,
1846 const u8 *query, size_t query_len)
1847{
1848 const u8 *pos;
1849 u8 role;
1850 u8 *len_pos;
1851
1852 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1853
1854 if (!wpa_s->global->wifi_display) {
1855 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1856 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1857 srv_trans_id);
1858 return;
1859 }
1860
1861 if (query_len < 1) {
1862 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1863 "Role");
1864 return;
1865 }
1866
1867 if (wpabuf_tailroom(resp) < 5)
1868 return;
1869
1870 pos = query;
1871 role = *pos++;
1872 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1873
1874 /* TODO: role specific handling */
1875
1876 /* Length (to be filled) */
1877 len_pos = wpabuf_put(resp, 2);
1878 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1879 wpabuf_put_u8(resp, srv_trans_id);
1880 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1881
1882 while (pos < query + query_len) {
1883 if (*pos < MAX_WFD_SUBELEMS &&
1884 wpa_s->global->wfd_subelem[*pos] &&
1885 wpabuf_tailroom(resp) >=
1886 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1887 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1888 "subelement %u", *pos);
1889 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1890 }
1891 pos++;
1892 }
1893
1894 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1895}
1896#endif /* CONFIG_WIFI_DISPLAY */
1897
1898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1900 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1901{
1902 struct wpa_supplicant *wpa_s = ctx;
1903 const u8 *pos = tlvs;
1904 const u8 *end = tlvs + tlvs_len;
1905 const u8 *tlv_end;
1906 u16 slen;
1907 struct wpabuf *resp;
1908 u8 srv_proto, srv_trans_id;
1909 size_t buf_len;
1910 char *buf;
1911
1912 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1913 tlvs, tlvs_len);
1914 buf_len = 2 * tlvs_len + 1;
1915 buf = os_malloc(buf_len);
1916 if (buf) {
1917 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1918 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1919 MACSTR " %u %u %s",
1920 freq, MAC2STR(sa), dialog_token, update_indic,
1921 buf);
1922 os_free(buf);
1923 }
1924
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001925 if (wpa_s->p2p_sd_over_ctrl_iface) {
1926 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1927 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001929 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930
1931 resp = wpabuf_alloc(10000);
1932 if (resp == NULL)
1933 return;
1934
1935 while (pos + 1 < end) {
1936 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1937 slen = WPA_GET_LE16(pos);
1938 pos += 2;
1939 if (pos + slen > end || slen < 2) {
1940 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1941 "length");
1942 wpabuf_free(resp);
1943 return;
1944 }
1945 tlv_end = pos + slen;
1946
1947 srv_proto = *pos++;
1948 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1949 srv_proto);
1950 srv_trans_id = *pos++;
1951 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1952 srv_trans_id);
1953
1954 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1955 pos, tlv_end - pos);
1956
1957
1958 if (wpa_s->force_long_sd) {
1959 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1960 "response");
1961 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1962 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1963 goto done;
1964 }
1965
1966 switch (srv_proto) {
1967 case P2P_SERV_ALL_SERVICES:
1968 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1969 "for all services");
1970 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1971 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1972 wpa_printf(MSG_DEBUG, "P2P: No service "
1973 "discovery protocols available");
1974 wpas_sd_add_proto_not_avail(
1975 resp, P2P_SERV_ALL_SERVICES,
1976 srv_trans_id);
1977 break;
1978 }
1979 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1980 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1981 break;
1982 case P2P_SERV_BONJOUR:
1983 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1984 pos, tlv_end - pos);
1985 break;
1986 case P2P_SERV_UPNP:
1987 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1988 pos, tlv_end - pos);
1989 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001990#ifdef CONFIG_WIFI_DISPLAY
1991 case P2P_SERV_WIFI_DISPLAY:
1992 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1993 pos, tlv_end - pos);
1994 break;
1995#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996 default:
1997 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1998 "protocol %u", srv_proto);
1999 wpas_sd_add_proto_not_avail(resp, srv_proto,
2000 srv_trans_id);
2001 break;
2002 }
2003
2004 pos = tlv_end;
2005 }
2006
2007done:
2008 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2009 update_indic, tlvs, tlvs_len);
2010
2011 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2012
2013 wpabuf_free(resp);
2014}
2015
2016
2017void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2018 const u8 *tlvs, size_t tlvs_len)
2019{
2020 struct wpa_supplicant *wpa_s = ctx;
2021 const u8 *pos = tlvs;
2022 const u8 *end = tlvs + tlvs_len;
2023 const u8 *tlv_end;
2024 u16 slen;
2025 size_t buf_len;
2026 char *buf;
2027
2028 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2029 tlvs, tlvs_len);
2030 if (tlvs_len > 1500) {
2031 /* TODO: better way for handling this */
2032 wpa_msg_ctrl(wpa_s, MSG_INFO,
2033 P2P_EVENT_SERV_DISC_RESP MACSTR
2034 " %u <long response: %u bytes>",
2035 MAC2STR(sa), update_indic,
2036 (unsigned int) tlvs_len);
2037 } else {
2038 buf_len = 2 * tlvs_len + 1;
2039 buf = os_malloc(buf_len);
2040 if (buf) {
2041 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2042 wpa_msg_ctrl(wpa_s, MSG_INFO,
2043 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2044 MAC2STR(sa), update_indic, buf);
2045 os_free(buf);
2046 }
2047 }
2048
2049 while (pos < end) {
2050 u8 srv_proto, srv_trans_id, status;
2051
2052 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2053 slen = WPA_GET_LE16(pos);
2054 pos += 2;
2055 if (pos + slen > end || slen < 3) {
2056 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2057 "length");
2058 return;
2059 }
2060 tlv_end = pos + slen;
2061
2062 srv_proto = *pos++;
2063 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2064 srv_proto);
2065 srv_trans_id = *pos++;
2066 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2067 srv_trans_id);
2068 status = *pos++;
2069 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2070 status);
2071
2072 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2073 pos, tlv_end - pos);
2074
2075 pos = tlv_end;
2076 }
2077
2078 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2079}
2080
2081
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002082u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2083 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084{
2085 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002086 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002087 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002088 return 0;
2089 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090}
2091
2092
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002093u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2094 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095{
2096 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002097 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098
2099 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2100 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002101 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2103 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2104 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2105 wpabuf_put_u8(tlvs, version);
2106 wpabuf_put_str(tlvs, query);
2107 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2108 wpabuf_free(tlvs);
2109 return ret;
2110}
2111
2112
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002113#ifdef CONFIG_WIFI_DISPLAY
2114
2115static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2116 const struct wpabuf *tlvs)
2117{
2118 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2119 return 0;
2120 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2121 return 0;
2122 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2123}
2124
2125
2126#define MAX_WFD_SD_SUBELEMS 20
2127
2128static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2129 const char *subelems)
2130{
2131 u8 *len;
2132 const char *pos;
2133 int val;
2134 int count = 0;
2135
2136 len = wpabuf_put(tlvs, 2);
2137 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2138 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2139
2140 wpabuf_put_u8(tlvs, role);
2141
2142 pos = subelems;
2143 while (*pos) {
2144 val = atoi(pos);
2145 if (val >= 0 && val < 256) {
2146 wpabuf_put_u8(tlvs, val);
2147 count++;
2148 if (count == MAX_WFD_SD_SUBELEMS)
2149 break;
2150 }
2151 pos = os_strchr(pos + 1, ',');
2152 if (pos == NULL)
2153 break;
2154 pos++;
2155 }
2156
2157 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2158}
2159
2160
2161u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2162 const u8 *dst, const char *role)
2163{
2164 struct wpabuf *tlvs;
2165 u64 ret;
2166 const char *subelems;
2167 u8 id = 1;
2168
2169 subelems = os_strchr(role, ' ');
2170 if (subelems == NULL)
2171 return 0;
2172 subelems++;
2173
2174 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2175 if (tlvs == NULL)
2176 return 0;
2177
2178 if (os_strstr(role, "[source]"))
2179 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2180 if (os_strstr(role, "[pri-sink]"))
2181 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2182 if (os_strstr(role, "[sec-sink]"))
2183 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2184 if (os_strstr(role, "[source+sink]"))
2185 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2186
2187 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2188 wpabuf_free(tlvs);
2189 return ret;
2190}
2191
2192#endif /* CONFIG_WIFI_DISPLAY */
2193
2194
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002195int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196{
2197 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002198 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2200 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002201 return p2p_sd_cancel_request(wpa_s->global->p2p,
2202 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203}
2204
2205
2206void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2207 const u8 *dst, u8 dialog_token,
2208 const struct wpabuf *resp_tlvs)
2209{
2210 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2211 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2212 resp_tlvs);
2213 return;
2214 }
2215 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2216 return;
2217 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2218 resp_tlvs);
2219}
2220
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2223{
2224 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2225 wpa_drv_p2p_service_update(wpa_s);
2226 return;
2227 }
2228 if (wpa_s->global->p2p)
2229 p2p_sd_service_update(wpa_s->global->p2p);
2230}
2231
2232
2233static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2234{
2235 dl_list_del(&bsrv->list);
2236 wpabuf_free(bsrv->query);
2237 wpabuf_free(bsrv->resp);
2238 os_free(bsrv);
2239}
2240
2241
2242static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2243{
2244 dl_list_del(&usrv->list);
2245 os_free(usrv->service);
2246 os_free(usrv);
2247}
2248
2249
2250void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2251{
2252 struct p2p_srv_bonjour *bsrv, *bn;
2253 struct p2p_srv_upnp *usrv, *un;
2254
2255 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2256 struct p2p_srv_bonjour, list)
2257 wpas_p2p_srv_bonjour_free(bsrv);
2258
2259 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2260 struct p2p_srv_upnp, list)
2261 wpas_p2p_srv_upnp_free(usrv);
2262
2263 wpas_p2p_sd_service_update(wpa_s);
2264}
2265
2266
2267int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2268 struct wpabuf *query, struct wpabuf *resp)
2269{
2270 struct p2p_srv_bonjour *bsrv;
2271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272 bsrv = os_zalloc(sizeof(*bsrv));
2273 if (bsrv == NULL)
2274 return -1;
2275 bsrv->query = query;
2276 bsrv->resp = resp;
2277 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2278
2279 wpas_p2p_sd_service_update(wpa_s);
2280 return 0;
2281}
2282
2283
2284int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2285 const struct wpabuf *query)
2286{
2287 struct p2p_srv_bonjour *bsrv;
2288
2289 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2290 if (bsrv == NULL)
2291 return -1;
2292 wpas_p2p_srv_bonjour_free(bsrv);
2293 wpas_p2p_sd_service_update(wpa_s);
2294 return 0;
2295}
2296
2297
2298int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2299 const char *service)
2300{
2301 struct p2p_srv_upnp *usrv;
2302
2303 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2304 return 0; /* Already listed */
2305 usrv = os_zalloc(sizeof(*usrv));
2306 if (usrv == NULL)
2307 return -1;
2308 usrv->version = version;
2309 usrv->service = os_strdup(service);
2310 if (usrv->service == NULL) {
2311 os_free(usrv);
2312 return -1;
2313 }
2314 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2315
2316 wpas_p2p_sd_service_update(wpa_s);
2317 return 0;
2318}
2319
2320
2321int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2322 const char *service)
2323{
2324 struct p2p_srv_upnp *usrv;
2325
2326 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2327 if (usrv == NULL)
2328 return -1;
2329 wpas_p2p_srv_upnp_free(usrv);
2330 wpas_p2p_sd_service_update(wpa_s);
2331 return 0;
2332}
2333
2334
2335static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2336 const u8 *peer, const char *params,
2337 unsigned int generated_pin)
2338{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002339 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2340 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341}
2342
2343
2344static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2345 const u8 *peer, const char *params)
2346{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002347 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2348 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349}
2350
2351
2352void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2353 const u8 *dev_addr, const u8 *pri_dev_type,
2354 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002355 u8 dev_capab, u8 group_capab, const u8 *group_id,
2356 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357{
2358 struct wpa_supplicant *wpa_s = ctx;
2359 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002360 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361 u8 empty_dev_type[8];
2362 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002363 struct wpa_supplicant *group = NULL;
2364
2365 if (group_id) {
2366 for (group = wpa_s->global->ifaces; group; group = group->next)
2367 {
2368 struct wpa_ssid *s = group->current_ssid;
2369 if (s != NULL &&
2370 s->mode == WPAS_MODE_P2P_GO &&
2371 group_id_len - ETH_ALEN == s->ssid_len &&
2372 os_memcmp(group_id + ETH_ALEN, s->ssid,
2373 s->ssid_len) == 0)
2374 break;
2375 }
2376 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377
2378 if (pri_dev_type == NULL) {
2379 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2380 pri_dev_type = empty_dev_type;
2381 }
2382 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2383 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002384 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385 MAC2STR(dev_addr),
2386 wps_dev_type_bin2str(pri_dev_type, devtype,
2387 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002388 dev_name, supp_config_methods, dev_capab, group_capab,
2389 group ? " group=" : "",
2390 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 if (config_methods & WPS_CONFIG_DISPLAY) {
2394 generated_pin = wps_generate_pin();
2395 wpas_prov_disc_local_display(wpa_s, peer, params,
2396 generated_pin);
2397 } else if (config_methods & WPS_CONFIG_KEYPAD)
2398 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2399 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002400 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2401 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002402
2403 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2404 P2P_PROV_DISC_SUCCESS,
2405 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002406}
2407
2408
2409void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2410{
2411 struct wpa_supplicant *wpa_s = ctx;
2412 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002413 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002415 if (wpa_s->pending_pd_before_join &&
2416 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2417 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2418 wpa_s->pending_pd_before_join = 0;
2419 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2420 "join-existing-group operation");
2421 wpas_p2p_join_start(wpa_s);
2422 return;
2423 }
2424
Dmitry Shmidt04949592012-07-19 12:16:46 -07002425 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2426 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2427 os_snprintf(params, sizeof(params), " peer_go=%d",
2428 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2429 else
2430 params[0] = '\0';
2431
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002432 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002433 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434 else if (config_methods & WPS_CONFIG_KEYPAD) {
2435 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002436 wpas_prov_disc_local_display(wpa_s, peer, params,
2437 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002439 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2440 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002441
Jouni Malinen75ecf522011-06-27 15:19:46 -07002442 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2443 P2P_PROV_DISC_SUCCESS,
2444 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445}
2446
2447
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002448static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2449 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002450{
2451 struct wpa_supplicant *wpa_s = ctx;
2452
Dmitry Shmidt04949592012-07-19 12:16:46 -07002453 if (wpa_s->p2p_fallback_to_go_neg) {
2454 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2455 "failed - fall back to GO Negotiation");
2456 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2457 return;
2458 }
2459
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002460 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002461 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002462 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2463 "join-existing-group operation (no ACK for PD "
2464 "Req attempts)");
2465 wpas_p2p_join_start(wpa_s);
2466 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002467 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002468
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002469 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2470 " p2p_dev_addr=" MACSTR " status=%d",
2471 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002472
Jouni Malinen75ecf522011-06-27 15:19:46 -07002473 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2474 status, 0, 0);
2475}
2476
2477
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002478static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2479{
2480 if (channels == NULL)
2481 return 1; /* Assume no restrictions */
2482 return p2p_channels_includes_freq(channels, freq);
2483
2484}
2485
2486
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2488 const u8 *go_dev_addr, const u8 *ssid,
2489 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002490 int *force_freq, int persistent_group,
2491 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492{
2493 struct wpa_supplicant *wpa_s = ctx;
2494 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495 int res;
2496 struct wpa_supplicant *grp;
2497
2498 if (!persistent_group) {
2499 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2500 " to join an active group", MAC2STR(sa));
2501 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2502 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2503 == 0 ||
2504 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2505 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2506 "authorized invitation");
2507 goto accept_inv;
2508 }
2509 /*
2510 * Do not accept the invitation automatically; notify user and
2511 * request approval.
2512 */
2513 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2514 }
2515
2516 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2517 if (grp) {
2518 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2519 "running persistent group");
2520 if (*go)
2521 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2522 goto accept_inv;
2523 }
2524
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002525 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2526 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2527 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2528 "invitation to re-invoke a persistent group");
2529 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2531
2532 for (s = wpa_s->conf->ssid; s; s = s->next) {
2533 if (s->disabled == 2 &&
2534 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2535 s->ssid_len == ssid_len &&
2536 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2537 break;
2538 }
2539
2540 if (!s) {
2541 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2542 " requested reinvocation of an unknown group",
2543 MAC2STR(sa));
2544 return P2P_SC_FAIL_UNKNOWN_GROUP;
2545 }
2546
2547 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2548 *go = 1;
2549 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2550 wpa_printf(MSG_DEBUG, "P2P: The only available "
2551 "interface is already in use - reject "
2552 "invitation");
2553 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2554 }
2555 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2556 } else if (s->mode == WPAS_MODE_P2P_GO) {
2557 *go = 1;
2558 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2559 {
2560 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2561 "interface address for the group");
2562 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2563 }
2564 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2565 ETH_ALEN);
2566 }
2567
2568accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002569 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2570
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002571 /* Get one of the frequencies currently in use */
2572 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2573 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002575 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 }
2577
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002578 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2579 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002580 if (*go == 0) {
2581 /* We are the client */
2582 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2583 "running a GO but we are capable of MCC, "
2584 "figure out the best channel to use");
2585 *force_freq = 0;
2586 } else if (!freq_included(channels, *force_freq)) {
2587 /* We are the GO, and *force_freq is not in the
2588 * intersection */
2589 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2590 "in intersection but we are capable of MCC, "
2591 "figure out the best channel to use",
2592 *force_freq);
2593 *force_freq = 0;
2594 }
2595 }
2596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597 return P2P_SC_SUCCESS;
2598}
2599
2600
2601static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2602 const u8 *ssid, size_t ssid_len,
2603 const u8 *go_dev_addr, u8 status,
2604 int op_freq)
2605{
2606 struct wpa_supplicant *wpa_s = ctx;
2607 struct wpa_ssid *s;
2608
2609 for (s = wpa_s->conf->ssid; s; s = s->next) {
2610 if (s->disabled == 2 &&
2611 s->ssid_len == ssid_len &&
2612 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2613 break;
2614 }
2615
2616 if (status == P2P_SC_SUCCESS) {
2617 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2618 " was accepted; op_freq=%d MHz",
2619 MAC2STR(sa), op_freq);
2620 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002621 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002623 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002625 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002627 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628 }
2629 return;
2630 }
2631
2632 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2633 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2634 " was rejected (status %u)", MAC2STR(sa), status);
2635 return;
2636 }
2637
2638 if (!s) {
2639 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002640 wpa_msg_global(wpa_s, MSG_INFO,
2641 P2P_EVENT_INVITATION_RECEIVED
2642 "sa=" MACSTR " go_dev_addr=" MACSTR
2643 " bssid=" MACSTR " unknown-network",
2644 MAC2STR(sa), MAC2STR(go_dev_addr),
2645 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002647 wpa_msg_global(wpa_s, MSG_INFO,
2648 P2P_EVENT_INVITATION_RECEIVED
2649 "sa=" MACSTR " go_dev_addr=" MACSTR
2650 " unknown-network",
2651 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 }
2653 return;
2654 }
2655
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002656 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002657 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2658 "sa=" MACSTR " persistent=%d freq=%d",
2659 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002660 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002661 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2662 "sa=" MACSTR " persistent=%d",
2663 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002664 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002665}
2666
2667
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002668static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2669 struct wpa_ssid *ssid,
2670 const u8 *peer)
2671{
2672 size_t i;
2673
2674 if (ssid == NULL)
2675 return;
2676
2677 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2678 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2679 ETH_ALEN) == 0)
2680 break;
2681 }
2682 if (i >= ssid->num_p2p_clients) {
2683 if (ssid->mode != WPAS_MODE_P2P_GO &&
2684 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2685 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2686 "due to invitation result", ssid->id);
2687 wpas_notify_network_removed(wpa_s, ssid);
2688 wpa_config_remove_network(wpa_s->conf, ssid->id);
2689 return;
2690 }
2691 return; /* Peer not found in client list */
2692 }
2693
2694 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2695 "group %d client list due to invitation result",
2696 MAC2STR(peer), ssid->id);
2697 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2698 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2699 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2700 ssid->num_p2p_clients--;
2701#ifndef CONFIG_NO_CONFIG_WRITE
2702 if (wpa_s->parent->conf->update_config &&
2703 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2704 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2705#endif /* CONFIG_NO_CONFIG_WRITE */
2706}
2707
2708
2709static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2710 const u8 *peer)
2711{
2712 struct wpa_ssid *ssid;
2713
2714 wpa_s = wpa_s->global->p2p_invite_group;
2715 if (wpa_s == NULL)
2716 return; /* No known invitation group */
2717 ssid = wpa_s->current_ssid;
2718 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2719 !ssid->p2p_persistent_group)
2720 return; /* Not operating as a GO in persistent group */
2721 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2722 ssid->ssid, ssid->ssid_len);
2723 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2724}
2725
2726
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002727static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002728 const struct p2p_channels *channels,
2729 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730{
2731 struct wpa_supplicant *wpa_s = ctx;
2732 struct wpa_ssid *ssid;
2733
2734 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002735 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2736 "status=%d " MACSTR,
2737 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002738 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002739 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2740 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741 }
2742 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2743
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002744 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2745 status, MAC2STR(peer));
2746 if (wpa_s->pending_invite_ssid_id == -1) {
2747 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2748 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002750 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002752 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2753 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2754 "invitation exchange to indicate readiness for "
2755 "re-invocation");
2756 }
2757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002759 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2760 ssid = wpa_config_get_network(
2761 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2762 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2763 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 wpas_p2p_remove_pending_group_interface(wpa_s);
2765 return;
2766 }
2767
2768 ssid = wpa_config_get_network(wpa_s->conf,
2769 wpa_s->pending_invite_ssid_id);
2770 if (ssid == NULL) {
2771 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2772 "data matching with invitation");
2773 return;
2774 }
2775
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002776 /*
2777 * The peer could have missed our ctrl::ack frame for Invitation
2778 * Response and continue retransmitting the frame. To reduce the
2779 * likelihood of the peer not getting successful TX status for the
2780 * Invitation Response frame, wait a short time here before starting
2781 * the persistent group so that we will remain on the current channel to
2782 * acknowledge any possible retransmission from the peer.
2783 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002784#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002785 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2786 "starting persistent group");
2787 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002788#else
2789 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2790 "starting persistent group");
2791 os_sleep(0, 100000);
2792#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002795 ssid->mode == WPAS_MODE_P2P_GO,
2796 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002797 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798}
2799
2800
Dmitry Shmidt04949592012-07-19 12:16:46 -07002801static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2802 unsigned int freq)
2803{
2804 unsigned int i;
2805
2806 if (global->p2p_disallow_freq == NULL)
2807 return 0;
2808
2809 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2810 if (freq >= global->p2p_disallow_freq[i].min &&
2811 freq <= global->p2p_disallow_freq[i].max)
2812 return 1;
2813 }
2814
2815 return 0;
2816}
2817
2818
2819static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2820{
2821 reg->channel[reg->channels] = chan;
2822 reg->channels++;
2823}
2824
2825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2827 struct p2p_channels *chan)
2828{
2829 int i, cla = 0;
2830
2831 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2832 "band");
2833
2834 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2835 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002836 chan->reg_class[cla].channels = 0;
2837 for (i = 0; i < 11; i++) {
2838 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2839 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2840 }
2841 if (chan->reg_class[cla].channels)
2842 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843
2844 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2845 "band");
2846
2847 /* Operating class 115 - 5 GHz, channels 36-48 */
2848 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002849 chan->reg_class[cla].channels = 0;
2850 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2851 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2852 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2853 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2854 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2855 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2856 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2857 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2858 if (chan->reg_class[cla].channels)
2859 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860
2861 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2862 "band");
2863
2864 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2865 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002866 chan->reg_class[cla].channels = 0;
2867 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2868 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2869 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2870 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2871 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2872 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2873 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2874 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2875 if (chan->reg_class[cla].channels)
2876 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877
2878 chan->reg_classes = cla;
2879 return 0;
2880}
2881
2882
2883static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2884 u16 num_modes,
2885 enum hostapd_hw_mode mode)
2886{
2887 u16 i;
2888
2889 for (i = 0; i < num_modes; i++) {
2890 if (modes[i].mode == mode)
2891 return &modes[i];
2892 }
2893
2894 return NULL;
2895}
2896
2897
Dmitry Shmidt04949592012-07-19 12:16:46 -07002898static int has_channel(struct wpa_global *global,
2899 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900{
2901 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002902 unsigned int freq;
2903
2904 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2905 chan * 5;
2906 if (wpas_p2p_disallowed_freq(global, freq))
2907 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908
2909 for (i = 0; i < mode->num_channels; i++) {
2910 if (mode->channels[i].chan == chan) {
2911 if (flags)
2912 *flags = mode->channels[i].flag;
2913 return !(mode->channels[i].flag &
2914 (HOSTAPD_CHAN_DISABLED |
2915 HOSTAPD_CHAN_PASSIVE_SCAN |
2916 HOSTAPD_CHAN_NO_IBSS |
2917 HOSTAPD_CHAN_RADAR));
2918 }
2919 }
2920
2921 return 0;
2922}
2923
2924
2925struct p2p_oper_class_map {
2926 enum hostapd_hw_mode mode;
2927 u8 op_class;
2928 u8 min_chan;
2929 u8 max_chan;
2930 u8 inc;
2931 enum { BW20, BW40PLUS, BW40MINUS } bw;
2932};
2933
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002934static struct p2p_oper_class_map op_class[] = {
2935 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002936#if 0 /* Do not enable HT40 on 2 GHz for now */
2937 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2938 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2939#endif
2940 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2941 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2942 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2943 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2944 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2945 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2946 { -1, 0, 0, 0, 0, BW20 }
2947};
2948
2949
2950static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2951 struct hostapd_hw_modes *mode,
2952 u8 channel, u8 bw)
2953{
2954 int flag;
2955
2956 if (!has_channel(wpa_s->global, mode, channel, &flag))
2957 return -1;
2958 if (bw == BW40MINUS &&
2959 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2960 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2961 return 0;
2962 if (bw == BW40PLUS &&
2963 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2964 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2965 return 0;
2966 return 1;
2967}
2968
2969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002970static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2971 struct p2p_channels *chan)
2972{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002973 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002976 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2978 "of all supported channels; assume dualband "
2979 "support");
2980 return wpas_p2p_default_channels(wpa_s, chan);
2981 }
2982
2983 cla = 0;
2984
2985 for (op = 0; op_class[op].op_class; op++) {
2986 struct p2p_oper_class_map *o = &op_class[op];
2987 u8 ch;
2988 struct p2p_reg_class *reg = NULL;
2989
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002990 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991 if (mode == NULL)
2992 continue;
2993 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002994 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995 continue;
2996 if (reg == NULL) {
2997 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2998 "class %u", o->op_class);
2999 reg = &chan->reg_class[cla];
3000 cla++;
3001 reg->reg_class = o->op_class;
3002 }
3003 reg->channel[reg->channels] = ch;
3004 reg->channels++;
3005 }
3006 if (reg) {
3007 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3008 reg->channel, reg->channels);
3009 }
3010 }
3011
3012 chan->reg_classes = cla;
3013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014 return 0;
3015}
3016
3017
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003018int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3019 struct hostapd_hw_modes *mode, u8 channel)
3020{
3021 int op, ret;
3022
3023 for (op = 0; op_class[op].op_class; op++) {
3024 struct p2p_oper_class_map *o = &op_class[op];
3025 u8 ch;
3026
3027 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3028 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3029 o->bw == BW20 || ch != channel)
3030 continue;
3031 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3032 if (ret < 0)
3033 continue;
3034 else if (ret > 0)
3035 return (o->bw == BW40MINUS) ? -1 : 1;
3036 else
3037 return 0;
3038 }
3039 }
3040 return 0;
3041}
3042
3043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003044static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3045 size_t buf_len)
3046{
3047 struct wpa_supplicant *wpa_s = ctx;
3048
3049 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3050 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3051 break;
3052 }
3053 if (wpa_s == NULL)
3054 return -1;
3055
3056 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3057}
3058
3059
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003060static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3061{
3062 struct wpa_supplicant *wpa_s = ctx;
3063
3064 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3065 struct wpa_ssid *ssid = wpa_s->current_ssid;
3066 if (ssid == NULL)
3067 continue;
3068 if (ssid->mode != WPAS_MODE_INFRA)
3069 continue;
3070 if (wpa_s->wpa_state != WPA_COMPLETED &&
3071 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3072 continue;
3073 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3074 return 1;
3075 }
3076
3077 return 0;
3078}
3079
3080
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003081static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3082{
3083 struct wpa_supplicant *wpa_s = ctx;
3084 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3085}
3086
3087
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003088int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3089{
3090 struct wpa_interface iface;
3091 struct wpa_supplicant *p2pdev_wpa_s;
3092 char ifname[100];
3093 char force_name[100];
3094 int ret;
3095
3096 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3097 wpa_s->ifname);
3098 force_name[0] = '\0';
3099 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3100 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3101 force_name, wpa_s->pending_interface_addr, NULL);
3102 if (ret < 0) {
3103 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3104 return ret;
3105 }
3106 os_strlcpy(wpa_s->pending_interface_name, ifname,
3107 sizeof(wpa_s->pending_interface_name));
3108
3109 os_memset(&iface, 0, sizeof(iface));
3110 iface.p2p_mgmt = 1;
3111 iface.ifname = wpa_s->pending_interface_name;
3112 iface.driver = wpa_s->driver->name;
3113 iface.driver_param = wpa_s->conf->driver_param;
3114 iface.confname = wpa_s->confname;
3115 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3116 if (!p2pdev_wpa_s) {
3117 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3118 return -1;
3119 }
3120 p2pdev_wpa_s->parent = wpa_s;
3121
3122 wpa_s->pending_interface_name[0] = '\0';
3123 return 0;
3124}
3125
3126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003127/**
3128 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3129 * @global: Pointer to global data from wpa_supplicant_init()
3130 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3131 * Returns: 0 on success, -1 on failure
3132 */
3133int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3134{
3135 struct p2p_config p2p;
3136 unsigned int r;
3137 int i;
3138
3139 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3140 return 0;
3141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003142 if (global->p2p)
3143 return 0;
3144
3145 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3146 struct p2p_params params;
3147
3148 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3149 os_memset(&params, 0, sizeof(params));
3150 params.dev_name = wpa_s->conf->device_name;
3151 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3152 WPS_DEV_TYPE_LEN);
3153 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3154 os_memcpy(params.sec_dev_type,
3155 wpa_s->conf->sec_device_type,
3156 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3157
3158 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3159 return -1;
3160
3161 return 0;
3162 }
3163
3164 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003165 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003166 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003167 p2p.p2p_scan = wpas_p2p_scan;
3168 p2p.send_action = wpas_send_action;
3169 p2p.send_action_done = wpas_send_action_done;
3170 p2p.go_neg_completed = wpas_go_neg_completed;
3171 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3172 p2p.dev_found = wpas_dev_found;
3173 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003174 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003175 p2p.start_listen = wpas_start_listen;
3176 p2p.stop_listen = wpas_stop_listen;
3177 p2p.send_probe_resp = wpas_send_probe_resp;
3178 p2p.sd_request = wpas_sd_request;
3179 p2p.sd_response = wpas_sd_response;
3180 p2p.prov_disc_req = wpas_prov_disc_req;
3181 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003182 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003183 p2p.invitation_process = wpas_invitation_process;
3184 p2p.invitation_received = wpas_invitation_received;
3185 p2p.invitation_result = wpas_invitation_result;
3186 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003187 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003188
3189 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003190 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003191 p2p.dev_name = wpa_s->conf->device_name;
3192 p2p.manufacturer = wpa_s->conf->manufacturer;
3193 p2p.model_name = wpa_s->conf->model_name;
3194 p2p.model_number = wpa_s->conf->model_number;
3195 p2p.serial_number = wpa_s->conf->serial_number;
3196 if (wpa_s->wps) {
3197 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3198 p2p.config_methods = wpa_s->wps->config_methods;
3199 }
3200
3201 if (wpa_s->conf->p2p_listen_reg_class &&
3202 wpa_s->conf->p2p_listen_channel) {
3203 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3204 p2p.channel = wpa_s->conf->p2p_listen_channel;
3205 } else {
3206 p2p.reg_class = 81;
3207 /*
3208 * Pick one of the social channels randomly as the listen
3209 * channel.
3210 */
3211 os_get_random((u8 *) &r, sizeof(r));
3212 p2p.channel = 1 + (r % 3) * 5;
3213 }
3214 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3215
3216 if (wpa_s->conf->p2p_oper_reg_class &&
3217 wpa_s->conf->p2p_oper_channel) {
3218 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3219 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3220 p2p.cfg_op_channel = 1;
3221 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3222 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3223
3224 } else {
3225 p2p.op_reg_class = 81;
3226 /*
3227 * Use random operation channel from (1, 6, 11) if no other
3228 * preference is indicated.
3229 */
3230 os_get_random((u8 *) &r, sizeof(r));
3231 p2p.op_channel = 1 + (r % 3) * 5;
3232 p2p.cfg_op_channel = 0;
3233 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3234 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3235 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003236
3237 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3238 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3239 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3240 }
3241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003242 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3243 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3244 p2p.country[2] = 0x04;
3245 } else
3246 os_memcpy(p2p.country, "XX\x04", 3);
3247
3248 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3249 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3250 "channel list");
3251 return -1;
3252 }
3253
3254 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3255 WPS_DEV_TYPE_LEN);
3256
3257 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3258 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3259 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3260
3261 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3262 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3263
3264 p2p.max_peers = 100;
3265
3266 if (wpa_s->conf->p2p_ssid_postfix) {
3267 p2p.ssid_postfix_len =
3268 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3269 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3270 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3271 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3272 p2p.ssid_postfix_len);
3273 }
3274
3275 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3276
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003277 p2p.max_listen = wpa_s->max_remain_on_chan;
3278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279 global->p2p = p2p_init(&p2p);
3280 if (global->p2p == NULL)
3281 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003282 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283
3284 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3285 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3286 continue;
3287 p2p_add_wps_vendor_extension(
3288 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3289 }
3290
3291 return 0;
3292}
3293
3294
3295/**
3296 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3297 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3298 *
3299 * This function deinitialize per-interface P2P data.
3300 */
3301void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3302{
3303 if (wpa_s->driver && wpa_s->drv_priv)
3304 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003305
3306 if (wpa_s->go_params) {
3307 /* Clear any stored provisioning info */
3308 p2p_clear_provisioning_info(
3309 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003310 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003311 }
3312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 os_free(wpa_s->go_params);
3314 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003315#ifdef ANDROID_P2P
3316 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3317#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003318 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3319 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3320 wpa_s->p2p_long_listen = 0;
3321 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3322 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3323 wpas_p2p_remove_pending_group_interface(wpa_s);
3324
3325 /* TODO: remove group interface from the driver if this wpa_s instance
3326 * is on top of a P2P group interface */
3327}
3328
3329
3330/**
3331 * wpas_p2p_deinit_global - Deinitialize global P2P module
3332 * @global: Pointer to global data from wpa_supplicant_init()
3333 *
3334 * This function deinitializes the global (per device) P2P module.
3335 */
3336void wpas_p2p_deinit_global(struct wpa_global *global)
3337{
3338 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003340 wpa_s = global->ifaces;
3341 if (wpa_s)
3342 wpas_p2p_service_flush(wpa_s);
3343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003344 if (global->p2p == NULL)
3345 return;
3346
3347 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003348 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3349 wpa_s = wpa_s->next;
3350 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003351 tmp = global->ifaces;
3352 while (tmp &&
3353 (tmp == wpa_s ||
3354 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3355 tmp = tmp->next;
3356 }
3357 if (tmp == NULL)
3358 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003359 /* Disconnect from the P2P group and deinit the interface */
3360 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003361 }
3362
3363 /*
3364 * Deinit GO data on any possibly remaining interface (if main
3365 * interface is used as GO).
3366 */
3367 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3368 if (wpa_s->ap_iface)
3369 wpas_p2p_group_deinit(wpa_s);
3370 }
3371
3372 p2p_deinit(global->p2p);
3373 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003374 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003375}
3376
3377
3378static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3379{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003380 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3381 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003382 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383 if (wpa_s->drv_flags &
3384 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3385 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3386 return 1; /* P2P group requires a new interface in every case
3387 */
3388 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3389 return 0; /* driver does not support concurrent operations */
3390 if (wpa_s->global->ifaces->next)
3391 return 1; /* more that one interface already in use */
3392 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3393 return 1; /* this interface is already in use */
3394 return 0;
3395}
3396
3397
3398static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3399 const u8 *peer_addr,
3400 enum p2p_wps_method wps_method,
3401 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003402 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003403 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003404{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003405 if (persistent_group && wpa_s->conf->persistent_reconnect)
3406 persistent_group = 2;
3407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003408 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3409 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3410 go_intent, own_interface_addr,
3411 force_freq, persistent_group);
3412 }
3413
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003414 /*
3415 * Increase GO config timeout if HT40 is used since it takes some time
3416 * to scan channels for coex purposes before the BSS can be started.
3417 */
3418 p2p_set_config_timeout(wpa_s->global->p2p,
3419 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003421 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3422 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003423 persistent_group, ssid ? ssid->ssid : NULL,
3424 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003425 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426}
3427
3428
3429static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3430 const u8 *peer_addr,
3431 enum p2p_wps_method wps_method,
3432 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003433 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003434 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003435{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003436 if (persistent_group && wpa_s->conf->persistent_reconnect)
3437 persistent_group = 2;
3438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003439 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3440 return -1;
3441
3442 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3443 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003444 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003445 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446}
3447
3448
3449static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3450{
3451 wpa_s->p2p_join_scan_count++;
3452 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3453 wpa_s->p2p_join_scan_count);
3454 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3455 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3456 " for join operationg - stop join attempt",
3457 MAC2STR(wpa_s->pending_join_iface_addr));
3458 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003459 if (wpa_s->p2p_auto_pd) {
3460 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003461 wpa_msg_global(wpa_s, MSG_INFO,
3462 P2P_EVENT_PROV_DISC_FAILURE
3463 " p2p_dev_addr=" MACSTR " status=N/A",
3464 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003465 return;
3466 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003467 wpa_msg_global(wpa_s->parent, MSG_INFO,
3468 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003469 }
3470}
3471
3472
Dmitry Shmidt04949592012-07-19 12:16:46 -07003473static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3474{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003475 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003476
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003477 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3478 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003479 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003480 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003481
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003482 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3483 if (!freqs)
3484 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003485
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003486 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3487 wpa_s->num_multichan_concurrent);
3488 if (num < 0) {
3489 res = 1;
3490 goto exit_free;
3491 }
3492
3493 for (i = 0; i < num; i++) {
3494 if (freqs[i] == freq) {
3495 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3496 freq);
3497 res = 0;
3498 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003499 }
3500 }
3501
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003502 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003503
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003504exit_free:
3505 os_free(freqs);
3506 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003507}
3508
3509
3510static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3511 const u8 *peer_dev_addr)
3512{
3513 struct wpa_bss *bss;
3514 int updated;
3515
3516 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3517 if (bss == NULL)
3518 return -1;
3519 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3520 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3521 "last scan");
3522 return 0;
3523 }
3524
3525 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3526 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3527 "%ld.%06ld (%supdated in last scan)",
3528 bss->last_update.sec, bss->last_update.usec,
3529 updated ? "": "not ");
3530
3531 return updated;
3532}
3533
3534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003535static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3536 struct wpa_scan_results *scan_res)
3537{
3538 struct wpa_bss *bss;
3539 int freq;
3540 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3543
3544 if (wpa_s->global->p2p_disabled)
3545 return;
3546
Dmitry Shmidt04949592012-07-19 12:16:46 -07003547 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3548 scan_res ? (int) scan_res->num : -1,
3549 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550
3551 if (scan_res)
3552 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3553
Dmitry Shmidt04949592012-07-19 12:16:46 -07003554 if (wpa_s->p2p_auto_pd) {
3555 int join = wpas_p2p_peer_go(wpa_s,
3556 wpa_s->pending_join_dev_addr);
3557 if (join == 0 &&
3558 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3559 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003560 bss = wpa_bss_get_bssid_latest(
3561 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003562 if (bss) {
3563 freq = bss->freq;
3564 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3565 "the peer " MACSTR " at %d MHz",
3566 wpa_s->auto_pd_scan_retry,
3567 MAC2STR(wpa_s->
3568 pending_join_dev_addr),
3569 freq);
3570 wpas_p2p_join_scan_req(wpa_s, freq);
3571 return;
3572 }
3573 }
3574
3575 if (join < 0)
3576 join = 0;
3577
3578 wpa_s->p2p_auto_pd = 0;
3579 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3580 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3581 MAC2STR(wpa_s->pending_join_dev_addr), join);
3582 if (p2p_prov_disc_req(wpa_s->global->p2p,
3583 wpa_s->pending_join_dev_addr,
3584 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003585 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003586 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003587 wpa_msg_global(wpa_s, MSG_INFO,
3588 P2P_EVENT_PROV_DISC_FAILURE
3589 " p2p_dev_addr=" MACSTR " status=N/A",
3590 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003591 }
3592 return;
3593 }
3594
3595 if (wpa_s->p2p_auto_join) {
3596 int join = wpas_p2p_peer_go(wpa_s,
3597 wpa_s->pending_join_dev_addr);
3598 if (join < 0) {
3599 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3600 "running a GO -> use GO Negotiation");
3601 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3602 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3603 wpa_s->p2p_persistent_group, 0, 0, 0,
3604 wpa_s->p2p_go_intent,
3605 wpa_s->p2p_connect_freq,
3606 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003607 wpa_s->p2p_pd_before_go_neg,
3608 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003609 return;
3610 }
3611
3612 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3613 "try to join the group", join ? "" :
3614 " in older scan");
3615 if (!join)
3616 wpa_s->p2p_fallback_to_go_neg = 1;
3617 }
3618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3620 wpa_s->pending_join_iface_addr);
3621 if (freq < 0 &&
3622 p2p_get_interface_addr(wpa_s->global->p2p,
3623 wpa_s->pending_join_dev_addr,
3624 iface_addr) == 0 &&
3625 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3626 {
3627 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3628 "address for join from " MACSTR " to " MACSTR
3629 " based on newly discovered P2P peer entry",
3630 MAC2STR(wpa_s->pending_join_iface_addr),
3631 MAC2STR(iface_addr));
3632 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3633 ETH_ALEN);
3634
3635 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3636 wpa_s->pending_join_iface_addr);
3637 }
3638 if (freq >= 0) {
3639 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3640 "from P2P peer table: %d MHz", freq);
3641 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003642 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003643 if (bss) {
3644 freq = bss->freq;
3645 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3646 "from BSS table: %d MHz", freq);
3647 }
3648 if (freq > 0) {
3649 u16 method;
3650
Dmitry Shmidt04949592012-07-19 12:16:46 -07003651 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003652 wpa_msg_global(wpa_s->parent, MSG_INFO,
3653 P2P_EVENT_GROUP_FORMATION_FAILURE
3654 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003655 return;
3656 }
3657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3659 "prior to joining an existing group (GO " MACSTR
3660 " freq=%u MHz)",
3661 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3662 wpa_s->pending_pd_before_join = 1;
3663
3664 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003665 case WPS_PIN_DISPLAY:
3666 method = WPS_CONFIG_KEYPAD;
3667 break;
3668 case WPS_PIN_KEYPAD:
3669 method = WPS_CONFIG_DISPLAY;
3670 break;
3671 case WPS_PBC:
3672 method = WPS_CONFIG_PUSHBUTTON;
3673 break;
3674 default:
3675 method = 0;
3676 break;
3677 }
3678
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003679 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3680 wpa_s->pending_join_dev_addr) ==
3681 method)) {
3682 /*
3683 * We have already performed provision discovery for
3684 * joining the group. Proceed directly to join
3685 * operation without duplicated provision discovery. */
3686 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3687 "with " MACSTR " already done - proceed to "
3688 "join",
3689 MAC2STR(wpa_s->pending_join_dev_addr));
3690 wpa_s->pending_pd_before_join = 0;
3691 goto start;
3692 }
3693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003695 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003696 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3698 "Discovery Request before joining an "
3699 "existing group");
3700 wpa_s->pending_pd_before_join = 0;
3701 goto start;
3702 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003703 return;
3704 }
3705
3706 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3707 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3708 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3709 wpas_p2p_check_join_scan_limit(wpa_s);
3710 return;
3711
3712start:
3713 /* Start join operation immediately */
3714 wpas_p2p_join_start(wpa_s);
3715}
3716
3717
Dmitry Shmidt04949592012-07-19 12:16:46 -07003718static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720 int ret;
3721 struct wpa_driver_scan_params params;
3722 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003723 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003724 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003725#ifdef ANDROID_P2P
3726 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003728 /* If freq is not provided, check the operating freq of the GO and do a
3729 * a directed scan to save time
3730 */
3731 if(!freq) {
3732 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3733 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3734 }
3735#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736 os_memset(&params, 0, sizeof(params));
3737
3738 /* P2P Wildcard SSID */
3739 params.num_ssids = 1;
3740 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3741 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3742
3743 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003744 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3745 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3746 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747 if (wps_ie == NULL) {
3748 wpas_p2p_scan_res_join(wpa_s, NULL);
3749 return;
3750 }
3751
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003752 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3753 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 if (ies == NULL) {
3755 wpabuf_free(wps_ie);
3756 wpas_p2p_scan_res_join(wpa_s, NULL);
3757 return;
3758 }
3759 wpabuf_put_buf(ies, wps_ie);
3760 wpabuf_free(wps_ie);
3761
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003762 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003764 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003765 params.extra_ies = wpabuf_head(ies);
3766 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003767 if (freq > 0) {
3768 freqs[0] = freq;
3769 params.freqs = freqs;
3770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771
3772 /*
3773 * Run a scan to update BSS table and start Provision Discovery once
3774 * the new scan results become available.
3775 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003776 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003777 if (!ret) {
3778 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003779 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003780 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781
3782 wpabuf_free(ies);
3783
3784 if (ret) {
3785 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3786 "try again later");
3787 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3788 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3789 wpas_p2p_check_join_scan_limit(wpa_s);
3790 }
3791}
3792
3793
Dmitry Shmidt04949592012-07-19 12:16:46 -07003794static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3795{
3796 struct wpa_supplicant *wpa_s = eloop_ctx;
3797 wpas_p2p_join_scan_req(wpa_s, 0);
3798}
3799
3800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003802 const u8 *dev_addr, enum p2p_wps_method wps_method,
3803 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003804{
3805 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003806 MACSTR " dev " MACSTR ")%s",
3807 MAC2STR(iface_addr), MAC2STR(dev_addr),
3808 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809
Dmitry Shmidt04949592012-07-19 12:16:46 -07003810 wpa_s->p2p_auto_pd = 0;
3811 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003812 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3813 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3814 wpa_s->pending_join_wps_method = wps_method;
3815
3816 /* Make sure we are not running find during connection establishment */
3817 wpas_p2p_stop_find(wpa_s);
3818
3819 wpa_s->p2p_join_scan_count = 0;
3820 wpas_p2p_join_scan(wpa_s, NULL);
3821 return 0;
3822}
3823
3824
3825static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3826{
3827 struct wpa_supplicant *group;
3828 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003829 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003830
3831 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3832 if (group == NULL)
3833 return -1;
3834 if (group != wpa_s) {
3835 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3836 sizeof(group->p2p_pin));
3837 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003838 } else {
3839 /*
3840 * Need to mark the current interface for p2p_group_formation
3841 * when a separate group interface is not used. This is needed
3842 * to allow p2p_cancel stop a pending p2p_connect-join.
3843 * wpas_p2p_init_group_interface() addresses this for the case
3844 * where a separate group interface is used.
3845 */
3846 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003847 }
3848
3849 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003850 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003851
3852 os_memset(&res, 0, sizeof(res));
3853 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3854 ETH_ALEN);
3855 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003856 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003857 if (bss) {
3858 res.freq = bss->freq;
3859 res.ssid_len = bss->ssid_len;
3860 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3861 }
3862
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003863 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3864 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3865 "starting client");
3866 wpa_drv_cancel_remain_on_channel(wpa_s);
3867 wpa_s->off_channel_freq = 0;
3868 wpa_s->roc_waiting_drv_freq = 0;
3869 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003870 wpas_start_wps_enrollee(group, &res);
3871
3872 /*
3873 * Allow a longer timeout for join-a-running-group than normal 15
3874 * second group formation timeout since the GO may not have authorized
3875 * our connection yet.
3876 */
3877 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3878 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3879 wpa_s, NULL);
3880
3881 return 0;
3882}
3883
3884
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003885static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003886 int *force_freq, int *pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003887{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003888 int *freqs, res;
3889 unsigned int freq_in_use = 0, num, i;
3890
3891 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3892 if (!freqs)
3893 return -1;
3894
3895 num = get_shared_radio_freqs(wpa_s, freqs,
3896 wpa_s->num_multichan_concurrent);
3897
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003898 if (freq > 0) {
3899 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3900 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3901 "(%u MHz) is not supported for P2P uses",
3902 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003903 res = -3;
3904 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003905 }
3906
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003907 for (i = 0; i < num; i++) {
3908 if (freqs[i] == freq)
3909 freq_in_use = 1;
3910 }
3911
3912 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
3913 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
3914 freq);
3915 res = -2;
3916 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003917 }
3918 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3919 "requested channel (%u MHz)", freq);
3920 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003921 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003922 }
3923
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003924 for (i = 0; i < num; i++) {
3925 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
3926 continue;
3927
3928 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
3929 *force_freq);
3930 *force_freq = freqs[i];
3931
3932 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
3933 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
3934 *pref_freq = *force_freq;
3935 }
3936 break;
3937 }
3938
3939 if (i == num) {
3940 if (num < wpa_s->num_multichan_concurrent) {
3941 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
3942 *force_freq = 0;
3943 } else {
3944 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
3945 res = -2;
3946 goto exit_free;
3947 }
3948 }
3949
3950exit_ok:
3951 res = 0;
3952exit_free:
3953 os_free(freqs);
3954 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003955}
3956
3957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003958/**
3959 * wpas_p2p_connect - Request P2P Group Formation to be started
3960 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3961 * @peer_addr: Address of the peer P2P Device
3962 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3963 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003964 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003965 * @join: Whether to join an existing group (as a client) instead of starting
3966 * Group Owner negotiation; @peer_addr is BSSID in that case
3967 * @auth: Whether to only authorize the connection instead of doing that and
3968 * initiating Group Owner negotiation
3969 * @go_intent: GO Intent or -1 to use default
3970 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003971 * @persistent_id: Persistent group credentials to use for forcing GO
3972 * parameters or -1 to generate new values (SSID/passphrase)
3973 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3974 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003975 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3977 * failure, -2 on failure due to channel not currently available,
3978 * -3 if forced channel is not supported
3979 */
3980int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3981 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003982 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003983 int go_intent, int freq, int persistent_id, int pd,
3984 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003986 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003987 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003989 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003990 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003991
3992 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3993 return -1;
3994
Dmitry Shmidt04949592012-07-19 12:16:46 -07003995 if (persistent_id >= 0) {
3996 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3997 if (ssid == NULL || ssid->disabled != 2 ||
3998 ssid->mode != WPAS_MODE_P2P_GO)
3999 return -1;
4000 }
4001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004002 if (go_intent < 0)
4003 go_intent = wpa_s->conf->p2p_go_intent;
4004
4005 if (!auth)
4006 wpa_s->p2p_long_listen = 0;
4007
4008 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004009 wpa_s->p2p_persistent_group = !!persistent_group;
4010 wpa_s->p2p_persistent_id = persistent_id;
4011 wpa_s->p2p_go_intent = go_intent;
4012 wpa_s->p2p_connect_freq = freq;
4013 wpa_s->p2p_fallback_to_go_neg = 0;
4014 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004015 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016
4017 if (pin)
4018 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4019 else if (wps_method == WPS_PIN_DISPLAY) {
4020 ret = wps_generate_pin();
4021 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4022 ret);
4023 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4024 wpa_s->p2p_pin);
4025 } else
4026 wpa_s->p2p_pin[0] = '\0';
4027
Dmitry Shmidt04949592012-07-19 12:16:46 -07004028 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004029 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4030 if (auth) {
4031 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4032 "connect a running group from " MACSTR,
4033 MAC2STR(peer_addr));
4034 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4035 return ret;
4036 }
4037 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4038 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4039 iface_addr) < 0) {
4040 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4041 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4042 dev_addr);
4043 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004044 if (auto_join) {
4045 os_get_time(&wpa_s->p2p_auto_started);
4046 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4047 "%ld.%06ld",
4048 wpa_s->p2p_auto_started.sec,
4049 wpa_s->p2p_auto_started.usec);
4050 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004051 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004052 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4053 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004054 return -1;
4055 return ret;
4056 }
4057
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004058 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004059 if (res)
4060 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004061 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062
4063 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4064
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004065 if (wpa_s->create_p2p_iface) {
4066 /* Prepare to add a new interface for the group */
4067 iftype = WPA_IF_P2P_GROUP;
4068 if (go_intent == 15)
4069 iftype = WPA_IF_P2P_GO;
4070 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4071 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4072 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004074 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004076 if_addr = wpa_s->pending_interface_addr;
4077 } else
4078 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079
4080 if (auth) {
4081 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004082 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004083 force_freq, persistent_group, ssid,
4084 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085 return -1;
4086 return ret;
4087 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004088
4089 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4090 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004091 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004092 if (wpa_s->create_p2p_iface)
4093 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 return -1;
4095 }
4096 return ret;
4097}
4098
4099
4100/**
4101 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4102 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4103 * @freq: Frequency of the channel in MHz
4104 * @duration: Duration of the stay on the channel in milliseconds
4105 *
4106 * This callback is called when the driver indicates that it has started the
4107 * requested remain-on-channel duration.
4108 */
4109void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4110 unsigned int freq, unsigned int duration)
4111{
4112 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4113 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4115 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4116 wpa_s->pending_listen_duration);
4117 wpa_s->pending_listen_freq = 0;
4118 }
4119}
4120
4121
4122static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4123 unsigned int timeout)
4124{
4125 /* Limit maximum Listen state time based on driver limitation. */
4126 if (timeout > wpa_s->max_remain_on_chan)
4127 timeout = wpa_s->max_remain_on_chan;
4128
4129 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4130 return wpa_drv_p2p_listen(wpa_s, timeout);
4131
4132 return p2p_listen(wpa_s->global->p2p, timeout);
4133}
4134
4135
4136/**
4137 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4138 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4139 * @freq: Frequency of the channel in MHz
4140 *
4141 * This callback is called when the driver indicates that a remain-on-channel
4142 * operation has been completed, i.e., the duration on the requested channel
4143 * has timed out.
4144 */
4145void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4146 unsigned int freq)
4147{
4148 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4149 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004150 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4152 return;
4153 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4154 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004155 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004156 return;
4157 if (wpa_s->p2p_long_listen > 0)
4158 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4159 if (wpa_s->p2p_long_listen > 0) {
4160 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4161 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4162 }
4163}
4164
4165
4166/**
4167 * wpas_p2p_group_remove - Remove a P2P group
4168 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4169 * @ifname: Network interface name of the group interface or "*" to remove all
4170 * groups
4171 * Returns: 0 on success, -1 on failure
4172 *
4173 * This function is used to remove a P2P group. This can be used to disconnect
4174 * from a group in which the local end is a P2P Client or to end a P2P Group in
4175 * case the local end is the Group Owner. If a virtual network interface was
4176 * created for this group, that interface will be removed. Otherwise, only the
4177 * configured P2P group network will be removed from the interface.
4178 */
4179int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4180{
4181 struct wpa_global *global = wpa_s->global;
4182
4183 if (os_strcmp(ifname, "*") == 0) {
4184 struct wpa_supplicant *prev;
4185 wpa_s = global->ifaces;
4186 while (wpa_s) {
4187 prev = wpa_s;
4188 wpa_s = wpa_s->next;
4189 wpas_p2p_disconnect(prev);
4190 }
4191 return 0;
4192 }
4193
4194 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4195 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4196 break;
4197 }
4198
4199 return wpas_p2p_disconnect(wpa_s);
4200}
4201
4202
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004203static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4204{
4205 unsigned int r;
4206
4207 if (freq == 2) {
4208 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4209 "band");
4210 if (wpa_s->best_24_freq > 0 &&
4211 p2p_supported_freq(wpa_s->global->p2p,
4212 wpa_s->best_24_freq)) {
4213 freq = wpa_s->best_24_freq;
4214 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4215 "channel: %d MHz", freq);
4216 } else {
4217 os_get_random((u8 *) &r, sizeof(r));
4218 freq = 2412 + (r % 3) * 25;
4219 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4220 "channel: %d MHz", freq);
4221 }
4222 }
4223
4224 if (freq == 5) {
4225 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4226 "band");
4227 if (wpa_s->best_5_freq > 0 &&
4228 p2p_supported_freq(wpa_s->global->p2p,
4229 wpa_s->best_5_freq)) {
4230 freq = wpa_s->best_5_freq;
4231 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4232 "channel: %d MHz", freq);
4233 } else {
4234 os_get_random((u8 *) &r, sizeof(r));
4235 freq = 5180 + (r % 4) * 20;
4236 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4237 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4238 "5 GHz channel for P2P group");
4239 return -1;
4240 }
4241 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4242 "channel: %d MHz", freq);
4243 }
4244 }
4245
4246 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4247 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4248 "(%u MHz) is not supported for P2P uses",
4249 freq);
4250 return -1;
4251 }
4252
4253 return freq;
4254}
4255
4256
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004257static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4258 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004259 int freq, int ht40,
4260 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004262 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004263 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004264 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004265
4266 os_memset(params, 0, sizeof(*params));
4267 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004268 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004269 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004270 if (!freq_included(channels, freq)) {
4271 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4272 "accepted", freq);
4273 return -1;
4274 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004275 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4276 "frequency %d MHz", freq);
4277 params->freq = freq;
4278 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4279 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004280 wpa_s->conf->p2p_oper_channel <= 11 &&
4281 freq_included(channels,
4282 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4284 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4285 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004286 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4287 wpa_s->conf->p2p_oper_reg_class == 116 ||
4288 wpa_s->conf->p2p_oper_reg_class == 117 ||
4289 wpa_s->conf->p2p_oper_reg_class == 124 ||
4290 wpa_s->conf->p2p_oper_reg_class == 126 ||
4291 wpa_s->conf->p2p_oper_reg_class == 127) &&
4292 freq_included(channels,
4293 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4295 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4296 "frequency %d MHz", params->freq);
4297 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4298 wpa_s->best_overall_freq > 0 &&
4299 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004300 wpa_s->best_overall_freq) &&
4301 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 params->freq = wpa_s->best_overall_freq;
4303 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4304 "channel %d MHz", params->freq);
4305 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4306 wpa_s->best_24_freq > 0 &&
4307 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004308 wpa_s->best_24_freq) &&
4309 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310 params->freq = wpa_s->best_24_freq;
4311 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4312 "channel %d MHz", params->freq);
4313 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4314 wpa_s->best_5_freq > 0 &&
4315 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004316 wpa_s->best_5_freq) &&
4317 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318 params->freq = wpa_s->best_5_freq;
4319 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4320 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004321 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4322 channels))) {
4323 params->freq = pref_freq;
4324 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4325 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004327 int chan;
4328 for (chan = 0; chan < 11; chan++) {
4329 params->freq = 2412 + chan * 5;
4330 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004331 params->freq) &&
4332 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004333 break;
4334 }
4335 if (chan == 11) {
4336 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4337 "allowed");
4338 return -1;
4339 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4341 "known)", params->freq);
4342 }
4343
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004344 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4345 if (!freqs)
4346 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004348 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4349 wpa_s->num_multichan_concurrent);
4350 if (res < 0) {
4351 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004352 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004353 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004354 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004355
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004356 if (!freq) {
4357 for (i = 0; i < num; i++) {
4358 if (freq_included(channels, freqs[i])) {
4359 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4360 freqs[i]);
4361 params->freq = freqs[i];
4362 break;
4363 }
4364 }
4365
4366 if (i == num) {
4367 if (num == wpa_s->num_multichan_concurrent) {
4368 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4369 os_free(freqs);
4370 return -1;
4371 } else {
4372 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4373 }
4374 }
4375 } else {
4376 for (i = 0; i < num; i++) {
4377 if (freqs[i] == freq)
4378 break;
4379 }
4380
4381 if (i == num) {
4382 if (num == wpa_s->num_multichan_concurrent) {
4383 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4384 os_free(freqs);
4385 return -1;
4386 } else {
4387 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4388 }
4389 }
4390 }
4391 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004392 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004393}
4394
4395
4396static struct wpa_supplicant *
4397wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4398 int go)
4399{
4400 struct wpa_supplicant *group_wpa_s;
4401
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004402 if (!wpas_p2p_create_iface(wpa_s)) {
4403 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4404 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004406 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407
4408 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004409 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004410 wpa_msg_global(wpa_s, MSG_ERROR,
4411 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004412 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004413 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004414 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4415 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004416 wpa_msg_global(wpa_s, MSG_ERROR,
4417 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004418 wpas_p2p_remove_pending_group_interface(wpa_s);
4419 return NULL;
4420 }
4421
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004422 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4423 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424 return group_wpa_s;
4425}
4426
4427
4428/**
4429 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4430 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4431 * @persistent_group: Whether to create a persistent group
4432 * @freq: Frequency for the group or 0 to indicate no hardcoding
4433 * Returns: 0 on success, -1 on failure
4434 *
4435 * This function creates a new P2P group with the local end as the Group Owner,
4436 * i.e., without using Group Owner Negotiation.
4437 */
4438int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004439 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004440{
4441 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004442
4443 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4444 return -1;
4445
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004446 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004447 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004448 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004449
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004450 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4451 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004452 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004453
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004454 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004455 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004456 if (params.freq &&
4457 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4458 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4459 "(%u MHz) is not supported for P2P uses",
4460 params.freq);
4461 return -1;
4462 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004463 p2p_go_params(wpa_s->global->p2p, &params);
4464 params.persistent_group = persistent_group;
4465
4466 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4467 if (wpa_s == NULL)
4468 return -1;
4469 wpas_start_wps_go(wpa_s, &params, 0);
4470
4471 return 0;
4472}
4473
4474
4475static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4476 struct wpa_ssid *params, int addr_allocated)
4477{
4478 struct wpa_ssid *ssid;
4479
4480 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4481 if (wpa_s == NULL)
4482 return -1;
4483
4484 wpa_supplicant_ap_deinit(wpa_s);
4485
4486 ssid = wpa_config_add_network(wpa_s->conf);
4487 if (ssid == NULL)
4488 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489 wpa_config_set_network_defaults(ssid);
4490 ssid->temporary = 1;
4491 ssid->proto = WPA_PROTO_RSN;
4492 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4493 ssid->group_cipher = WPA_CIPHER_CCMP;
4494 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4495 ssid->ssid = os_malloc(params->ssid_len);
4496 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 wpa_config_remove_network(wpa_s->conf, ssid->id);
4498 return -1;
4499 }
4500 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4501 ssid->ssid_len = params->ssid_len;
4502 ssid->p2p_group = 1;
4503 ssid->export_keys = 1;
4504 if (params->psk_set) {
4505 os_memcpy(ssid->psk, params->psk, 32);
4506 ssid->psk_set = 1;
4507 }
4508 if (params->passphrase)
4509 ssid->passphrase = os_strdup(params->passphrase);
4510
4511 wpa_supplicant_select_network(wpa_s, ssid);
4512
4513 wpa_s->show_group_started = 1;
4514
4515 return 0;
4516}
4517
4518
4519int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4520 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004521 int freq, int ht40,
4522 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523{
4524 struct p2p_go_neg_results params;
4525 int go = 0;
4526
4527 if (ssid->disabled != 2 || ssid->ssid == NULL)
4528 return -1;
4529
4530 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4531 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4532 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4533 "already running");
4534 return 0;
4535 }
4536
4537 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004538 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004539
Dmitry Shmidt04949592012-07-19 12:16:46 -07004540 wpa_s->p2p_fallback_to_go_neg = 0;
4541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542 if (ssid->mode == WPAS_MODE_INFRA)
4543 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4544
4545 if (ssid->mode != WPAS_MODE_P2P_GO)
4546 return -1;
4547
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004548 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4549 if (freq < 0)
4550 return -1;
4551
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004552 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004553 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004554
4555 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004556 params.psk_set = ssid->psk_set;
4557 if (params.psk_set)
4558 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004559 if (ssid->passphrase) {
4560 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4561 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4562 "persistent group");
4563 return -1;
4564 }
4565 os_strlcpy(params.passphrase, ssid->passphrase,
4566 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004567 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4569 params.ssid_len = ssid->ssid_len;
4570 params.persistent_group = 1;
4571
4572 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4573 if (wpa_s == NULL)
4574 return -1;
4575
4576 wpas_start_wps_go(wpa_s, &params, 0);
4577
4578 return 0;
4579}
4580
4581
4582static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4583 struct wpabuf *proberesp_ies)
4584{
4585 struct wpa_supplicant *wpa_s = ctx;
4586 if (wpa_s->ap_iface) {
4587 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004588 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4589 wpabuf_free(beacon_ies);
4590 wpabuf_free(proberesp_ies);
4591 return;
4592 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593 if (beacon_ies) {
4594 wpabuf_free(hapd->p2p_beacon_ie);
4595 hapd->p2p_beacon_ie = beacon_ies;
4596 }
4597 wpabuf_free(hapd->p2p_probe_resp_ie);
4598 hapd->p2p_probe_resp_ie = proberesp_ies;
4599 } else {
4600 wpabuf_free(beacon_ies);
4601 wpabuf_free(proberesp_ies);
4602 }
4603 wpa_supplicant_ap_update_beacon(wpa_s);
4604}
4605
4606
4607static void wpas_p2p_idle_update(void *ctx, int idle)
4608{
4609 struct wpa_supplicant *wpa_s = ctx;
4610 if (!wpa_s->ap_iface)
4611 return;
4612 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004613 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004614 wpas_p2p_set_group_idle_timeout(wpa_s);
4615 else
4616 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4617}
4618
4619
4620struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004621 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004622{
4623 struct p2p_group *group;
4624 struct p2p_group_config *cfg;
4625
4626 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4627 return NULL;
4628 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4629 return NULL;
4630
4631 cfg = os_zalloc(sizeof(*cfg));
4632 if (cfg == NULL)
4633 return NULL;
4634
Dmitry Shmidt04949592012-07-19 12:16:46 -07004635 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004636 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004637 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004638 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4640 if (wpa_s->max_stations &&
4641 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4642 cfg->max_clients = wpa_s->max_stations;
4643 else
4644 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004645 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4646 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647 cfg->cb_ctx = wpa_s;
4648 cfg->ie_update = wpas_p2p_ie_update;
4649 cfg->idle_update = wpas_p2p_idle_update;
4650
4651 group = p2p_group_init(wpa_s->global->p2p, cfg);
4652 if (group == NULL)
4653 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004654 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655 p2p_group_notif_formation_done(group);
4656 wpa_s->p2p_group = group;
4657 return group;
4658}
4659
4660
4661void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4662 int registrar)
4663{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004664 struct wpa_ssid *ssid = wpa_s->current_ssid;
4665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666 if (!wpa_s->p2p_in_provisioning) {
4667 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4668 "provisioning not in progress");
4669 return;
4670 }
4671
Dmitry Shmidt04949592012-07-19 12:16:46 -07004672 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4673 u8 go_dev_addr[ETH_ALEN];
4674 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4675 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4676 ssid->ssid_len);
4677 /* Clear any stored provisioning info */
4678 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4679 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004681 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4682 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004683 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4684 /*
4685 * Use a separate timeout for initial data connection to
4686 * complete to allow the group to be removed automatically if
4687 * something goes wrong in this step before the P2P group idle
4688 * timeout mechanism is taken into use.
4689 */
4690 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4691 wpas_p2p_group_formation_timeout,
4692 wpa_s->parent, NULL);
4693 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004694 if (wpa_s->global->p2p)
4695 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4696 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4697 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4698 wpas_group_formation_completed(wpa_s, 1);
4699}
4700
4701
Jouni Malinen75ecf522011-06-27 15:19:46 -07004702void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4703 struct wps_event_fail *fail)
4704{
4705 if (!wpa_s->p2p_in_provisioning) {
4706 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4707 "provisioning not in progress");
4708 return;
4709 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004710
4711 if (wpa_s->go_params) {
4712 p2p_clear_provisioning_info(
4713 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004714 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004715 }
4716
Jouni Malinen75ecf522011-06-27 15:19:46 -07004717 wpas_notify_p2p_wps_failed(wpa_s, fail);
4718}
4719
4720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004721int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004722 const char *config_method,
4723 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004724{
4725 u16 config_methods;
4726
Dmitry Shmidt04949592012-07-19 12:16:46 -07004727 wpa_s->p2p_fallback_to_go_neg = 0;
4728 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004729 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004730 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004731 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004732 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004733 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4734 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004736 else {
4737 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004739 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740
Dmitry Shmidt04949592012-07-19 12:16:46 -07004741 if (use == WPAS_P2P_PD_AUTO) {
4742 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4743 wpa_s->pending_pd_config_methods = config_methods;
4744 wpa_s->p2p_auto_pd = 1;
4745 wpa_s->p2p_auto_join = 0;
4746 wpa_s->pending_pd_before_join = 0;
4747 wpa_s->auto_pd_scan_retry = 0;
4748 wpas_p2p_stop_find(wpa_s);
4749 wpa_s->p2p_join_scan_count = 0;
4750 os_get_time(&wpa_s->p2p_auto_started);
4751 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4752 wpa_s->p2p_auto_started.sec,
4753 wpa_s->p2p_auto_started.usec);
4754 wpas_p2p_join_scan(wpa_s, NULL);
4755 return 0;
4756 }
4757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4759 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004760 config_methods,
4761 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762 }
4763
4764 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4765 return -1;
4766
4767 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004768 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004769 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770}
4771
4772
4773int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4774 char *end)
4775{
4776 return p2p_scan_result_text(ies, ies_len, buf, end);
4777}
4778
4779
4780static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4781{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004782 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004783 return;
4784
4785 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4786 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004787 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004788}
4789
4790
4791int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4792 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004793 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004794 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795{
4796 wpas_p2p_clear_pending_action_tx(wpa_s);
4797 wpa_s->p2p_long_listen = 0;
4798
4799 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4800 return wpa_drv_p2p_find(wpa_s, timeout, type);
4801
Dmitry Shmidt04949592012-07-19 12:16:46 -07004802 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4803 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804 return -1;
4805
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004806 wpa_supplicant_cancel_sched_scan(wpa_s);
4807
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004808 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004809 num_req_dev_types, req_dev_types, dev_id,
4810 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811}
4812
4813
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004814static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004815{
4816 wpas_p2p_clear_pending_action_tx(wpa_s);
4817 wpa_s->p2p_long_listen = 0;
4818 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4819 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004820 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004821
4822 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4823 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004824 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825 }
4826
4827 if (wpa_s->global->p2p)
4828 p2p_stop_find(wpa_s->global->p2p);
4829
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004830 return 0;
4831}
4832
4833
4834void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4835{
4836 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4837 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004838 wpas_p2p_remove_pending_group_interface(wpa_s);
4839}
4840
4841
4842static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4843{
4844 struct wpa_supplicant *wpa_s = eloop_ctx;
4845 wpa_s->p2p_long_listen = 0;
4846}
4847
4848
4849int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4850{
4851 int res;
4852
4853 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4854 return -1;
4855
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004856 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004857 wpas_p2p_clear_pending_action_tx(wpa_s);
4858
4859 if (timeout == 0) {
4860 /*
4861 * This is a request for unlimited Listen state. However, at
4862 * least for now, this is mapped to a Listen state for one
4863 * hour.
4864 */
4865 timeout = 3600;
4866 }
4867 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4868 wpa_s->p2p_long_listen = 0;
4869
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004870 /*
4871 * Stop previous find/listen operation to avoid trying to request a new
4872 * remain-on-channel operation while the driver is still running the
4873 * previous one.
4874 */
4875 if (wpa_s->global->p2p)
4876 p2p_stop_find(wpa_s->global->p2p);
4877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004878 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4879 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4880 wpa_s->p2p_long_listen = timeout * 1000;
4881 eloop_register_timeout(timeout, 0,
4882 wpas_p2p_long_listen_timeout,
4883 wpa_s, NULL);
4884 }
4885
4886 return res;
4887}
4888
4889
4890int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4891 u8 *buf, size_t len, int p2p_group)
4892{
4893 struct wpabuf *p2p_ie;
4894 int ret;
4895
4896 if (wpa_s->global->p2p_disabled)
4897 return -1;
4898 if (wpa_s->global->p2p == NULL)
4899 return -1;
4900 if (bss == NULL)
4901 return -1;
4902
4903 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4904 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4905 p2p_group, p2p_ie);
4906 wpabuf_free(p2p_ie);
4907
4908 return ret;
4909}
4910
4911
4912int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004913 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004914 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004915{
4916 if (wpa_s->global->p2p_disabled)
4917 return 0;
4918 if (wpa_s->global->p2p == NULL)
4919 return 0;
4920
Dmitry Shmidt04949592012-07-19 12:16:46 -07004921 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4922 ie, ie_len)) {
4923 case P2P_PREQ_NOT_P2P:
4924 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4925 ssi_signal);
4926 /* fall through */
4927 case P2P_PREQ_MALFORMED:
4928 case P2P_PREQ_NOT_LISTEN:
4929 case P2P_PREQ_NOT_PROCESSED:
4930 default: /* make gcc happy */
4931 return 0;
4932 case P2P_PREQ_PROCESSED:
4933 return 1;
4934 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935}
4936
4937
4938void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4939 const u8 *sa, const u8 *bssid,
4940 u8 category, const u8 *data, size_t len, int freq)
4941{
4942 if (wpa_s->global->p2p_disabled)
4943 return;
4944 if (wpa_s->global->p2p == NULL)
4945 return;
4946
4947 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4948 freq);
4949}
4950
4951
4952void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4953{
4954 if (wpa_s->global->p2p_disabled)
4955 return;
4956 if (wpa_s->global->p2p == NULL)
4957 return;
4958
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004959 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960}
4961
4962
4963void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4964{
4965 p2p_group_deinit(wpa_s->p2p_group);
4966 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004967
4968 wpa_s->ap_configured_cb = NULL;
4969 wpa_s->ap_configured_cb_ctx = NULL;
4970 wpa_s->ap_configured_cb_data = NULL;
4971 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972}
4973
4974
4975int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4976{
4977 wpa_s->p2p_long_listen = 0;
4978
4979 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4980 return wpa_drv_p2p_reject(wpa_s, addr);
4981
4982 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4983 return -1;
4984
4985 return p2p_reject(wpa_s->global->p2p, addr);
4986}
4987
4988
4989/* Invite to reinvoke a persistent group */
4990int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004991 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004992 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993{
4994 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004995 u8 *bssid = NULL;
4996 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004997 int res;
4998
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004999 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005000 if (peer_addr)
5001 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5002 else
5003 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004
Jouni Malinen31be0a42012-08-31 21:20:51 +03005005 wpa_s->p2p_persistent_go_freq = freq;
5006 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005007 if (ssid->mode == WPAS_MODE_P2P_GO) {
5008 role = P2P_INVITE_ROLE_GO;
5009 if (peer_addr == NULL) {
5010 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5011 "address in invitation command");
5012 return -1;
5013 }
5014 if (wpas_p2p_create_iface(wpa_s)) {
5015 if (wpas_p2p_add_group_interface(wpa_s,
5016 WPA_IF_P2P_GO) < 0) {
5017 wpa_printf(MSG_ERROR, "P2P: Failed to "
5018 "allocate a new interface for the "
5019 "group");
5020 return -1;
5021 }
5022 bssid = wpa_s->pending_interface_addr;
5023 } else
5024 bssid = wpa_s->own_addr;
5025 } else {
5026 role = P2P_INVITE_ROLE_CLIENT;
5027 peer_addr = ssid->bssid;
5028 }
5029 wpa_s->pending_invite_ssid_id = ssid->id;
5030
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005031 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005032 if (res)
5033 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005035 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5036 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5037 ssid->ssid, ssid->ssid_len,
5038 go_dev_addr, 1);
5039
5040 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5041 return -1;
5042
5043 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005044 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5045 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005046}
5047
5048
5049/* Invite to join an active group */
5050int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5051 const u8 *peer_addr, const u8 *go_dev_addr)
5052{
5053 struct wpa_global *global = wpa_s->global;
5054 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005055 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005057 int persistent;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005058 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005059 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005060
Jouni Malinen31be0a42012-08-31 21:20:51 +03005061 wpa_s->p2p_persistent_go_freq = 0;
5062 wpa_s->p2p_go_ht40 = 0;
5063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5065 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5066 break;
5067 }
5068 if (wpa_s == NULL) {
5069 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5070 return -1;
5071 }
5072
5073 ssid = wpa_s->current_ssid;
5074 if (ssid == NULL) {
5075 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5076 "invitation");
5077 return -1;
5078 }
5079
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005080 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005081 persistent = ssid->p2p_persistent_group &&
5082 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5083 ssid->ssid, ssid->ssid_len);
5084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085 if (ssid->mode == WPAS_MODE_P2P_GO) {
5086 role = P2P_INVITE_ROLE_ACTIVE_GO;
5087 bssid = wpa_s->own_addr;
5088 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005089 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090 } else {
5091 role = P2P_INVITE_ROLE_CLIENT;
5092 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5093 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5094 "invite to current group");
5095 return -1;
5096 }
5097 bssid = wpa_s->bssid;
5098 if (go_dev_addr == NULL &&
5099 !is_zero_ether_addr(wpa_s->go_dev_addr))
5100 go_dev_addr = wpa_s->go_dev_addr;
5101 }
5102 wpa_s->parent->pending_invite_ssid_id = -1;
5103
5104 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5105 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5106 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005107 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005108
5109 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5110 return -1;
5111
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005112 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005113 if (res)
5114 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005115 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005117 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005118 ssid->ssid, ssid->ssid_len, force_freq,
5119 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120}
5121
5122
5123void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5124{
5125 struct wpa_ssid *ssid = wpa_s->current_ssid;
5126 const char *ssid_txt;
5127 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005128 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005129 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005130 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131
Dmitry Shmidt04949592012-07-19 12:16:46 -07005132 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5133 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5134 wpa_s->parent, NULL);
5135 }
5136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005137 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005138 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139
5140 wpa_s->show_group_started = 0;
5141
5142 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5143 os_memset(go_dev_addr, 0, ETH_ALEN);
5144 if (ssid->bssid_set)
5145 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5146 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5147 ssid->ssid_len);
5148 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5149
5150 if (wpa_s->global->p2p_group_formation == wpa_s)
5151 wpa_s->global->p2p_group_formation = NULL;
5152
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005153 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5154 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005155 if (ssid->passphrase == NULL && ssid->psk_set) {
5156 char psk[65];
5157 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005158 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5159 "%s client ssid=\"%s\" freq=%d psk=%s "
5160 "go_dev_addr=" MACSTR "%s",
5161 wpa_s->ifname, ssid_txt, freq, psk,
5162 MAC2STR(go_dev_addr),
5163 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005164 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005165 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5166 "%s client ssid=\"%s\" freq=%d "
5167 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5168 wpa_s->ifname, ssid_txt, freq,
5169 ssid->passphrase ? ssid->passphrase : "",
5170 MAC2STR(go_dev_addr),
5171 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005172 }
5173
5174 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005175 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5176 ssid, go_dev_addr);
5177 if (network_id < 0)
5178 network_id = ssid->id;
5179 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005180
5181done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005182 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005183}
5184
5185
5186int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5187 u32 interval1, u32 duration2, u32 interval2)
5188{
5189 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5190 return -1;
5191 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5192 return -1;
5193
5194 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5195 wpa_s->current_ssid == NULL ||
5196 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5197 return -1;
5198
5199 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5200 wpa_s->own_addr, wpa_s->assoc_freq,
5201 duration1, interval1, duration2, interval2);
5202}
5203
5204
5205int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5206 unsigned int interval)
5207{
5208 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5209 return -1;
5210
5211 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5212 return -1;
5213
5214 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5215}
5216
5217
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005218static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5219{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005220 if (wpa_s->current_ssid == NULL) {
5221 /*
5222 * current_ssid can be cleared when P2P client interface gets
5223 * disconnected, so assume this interface was used as P2P
5224 * client.
5225 */
5226 return 1;
5227 }
5228 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005229 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5230}
5231
5232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005233static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5234{
5235 struct wpa_supplicant *wpa_s = eloop_ctx;
5236
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005237 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005238 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5239 "disabled");
5240 return;
5241 }
5242
Dmitry Shmidt04949592012-07-19 12:16:46 -07005243 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5244 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005245 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005246}
5247
5248
5249static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5250{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005251 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005252
Dmitry Shmidt04949592012-07-19 12:16:46 -07005253 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5254 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005256 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5257 return;
5258
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005259 timeout = wpa_s->conf->p2p_group_idle;
5260 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5261 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5262 timeout = P2P_MAX_CLIENT_IDLE;
5263
5264 if (timeout == 0)
5265 return;
5266
Dmitry Shmidt04949592012-07-19 12:16:46 -07005267 if (timeout < 0) {
5268 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5269 timeout = 0; /* special client mode no-timeout */
5270 else
5271 return;
5272 }
5273
5274 if (wpa_s->p2p_in_provisioning) {
5275 /*
5276 * Use the normal group formation timeout during the
5277 * provisioning phase to avoid terminating this process too
5278 * early due to group idle timeout.
5279 */
5280 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5281 "during provisioning");
5282 return;
5283 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305284
Dmitry Shmidt04949592012-07-19 12:16:46 -07005285 if (wpa_s->show_group_started) {
5286 /*
5287 * Use the normal group formation timeout between the end of
5288 * the provisioning phase and completion of 4-way handshake to
5289 * avoid terminating this process too early due to group idle
5290 * timeout.
5291 */
5292 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5293 "while waiting for initial 4-way handshake to "
5294 "complete");
5295 return;
5296 }
5297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005298 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005299 timeout);
5300 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5301 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005302}
5303
5304
Jouni Malinen2b89da82012-08-31 22:04:41 +03005305/* Returns 1 if the interface was removed */
5306int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5307 u16 reason_code, const u8 *ie, size_t ie_len,
5308 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005309{
5310 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005311 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005312 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005313 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005314
Dmitry Shmidt04949592012-07-19 12:16:46 -07005315 if (!locally_generated)
5316 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5317 ie_len);
5318
5319 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5320 wpa_s->current_ssid &&
5321 wpa_s->current_ssid->p2p_group &&
5322 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5323 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5324 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005325 if (wpas_p2p_group_delete(wpa_s,
5326 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5327 > 0)
5328 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005329 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005330
5331 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005332}
5333
5334
5335void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005336 u16 reason_code, const u8 *ie, size_t ie_len,
5337 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338{
5339 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5340 return;
5341 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5342 return;
5343
Dmitry Shmidt04949592012-07-19 12:16:46 -07005344 if (!locally_generated)
5345 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5346 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005347}
5348
5349
5350void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5351{
5352 struct p2p_data *p2p = wpa_s->global->p2p;
5353
5354 if (p2p == NULL)
5355 return;
5356
5357 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5358 return;
5359
5360 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5361 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5362
5363 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5364 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5365
5366 if (wpa_s->wps &&
5367 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5368 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5369
5370 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5371 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5372
5373 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5374 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5375 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5376 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5377 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5378 }
5379
5380 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5381 p2p_set_sec_dev_types(p2p,
5382 (void *) wpa_s->conf->sec_device_type,
5383 wpa_s->conf->num_sec_device_types);
5384
5385 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5386 int i;
5387 p2p_remove_wps_vendor_extensions(p2p);
5388 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5389 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5390 continue;
5391 p2p_add_wps_vendor_extension(
5392 p2p, wpa_s->conf->wps_vendor_ext[i]);
5393 }
5394 }
5395
5396 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5397 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5398 char country[3];
5399 country[0] = wpa_s->conf->country[0];
5400 country[1] = wpa_s->conf->country[1];
5401 country[2] = 0x04;
5402 p2p_set_country(p2p, country);
5403 }
5404
5405 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5406 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5407 wpa_s->conf->p2p_ssid_postfix ?
5408 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5409 0);
5410 }
5411
5412 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5413 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005414
5415 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5416 u8 reg_class, channel;
5417 int ret;
5418 unsigned int r;
5419 if (wpa_s->conf->p2p_listen_reg_class &&
5420 wpa_s->conf->p2p_listen_channel) {
5421 reg_class = wpa_s->conf->p2p_listen_reg_class;
5422 channel = wpa_s->conf->p2p_listen_channel;
5423 } else {
5424 reg_class = 81;
5425 /*
5426 * Pick one of the social channels randomly as the
5427 * listen channel.
5428 */
5429 os_get_random((u8 *) &r, sizeof(r));
5430 channel = 1 + (r % 3) * 5;
5431 }
5432 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5433 if (ret)
5434 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5435 "failed: %d", ret);
5436 }
5437 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5438 u8 op_reg_class, op_channel, cfg_op_channel;
5439 int ret = 0;
5440 unsigned int r;
5441 if (wpa_s->conf->p2p_oper_reg_class &&
5442 wpa_s->conf->p2p_oper_channel) {
5443 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5444 op_channel = wpa_s->conf->p2p_oper_channel;
5445 cfg_op_channel = 1;
5446 } else {
5447 op_reg_class = 81;
5448 /*
5449 * Use random operation channel from (1, 6, 11)
5450 *if no other preference is indicated.
5451 */
5452 os_get_random((u8 *) &r, sizeof(r));
5453 op_channel = 1 + (r % 3) * 5;
5454 cfg_op_channel = 0;
5455 }
5456 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5457 cfg_op_channel);
5458 if (ret)
5459 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5460 "failed: %d", ret);
5461 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005462
5463 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5464 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5465 wpa_s->conf->p2p_pref_chan) < 0) {
5466 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5467 "update failed");
5468 }
5469 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005470}
5471
5472
5473int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5474 int duration)
5475{
5476 if (!wpa_s->ap_iface)
5477 return -1;
5478 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5479 duration);
5480}
5481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482
5483int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5484{
5485 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5486 return -1;
5487 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5488 return -1;
5489
5490 wpa_s->global->cross_connection = enabled;
5491 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5492
5493 if (!enabled) {
5494 struct wpa_supplicant *iface;
5495
5496 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5497 {
5498 if (iface->cross_connect_enabled == 0)
5499 continue;
5500
5501 iface->cross_connect_enabled = 0;
5502 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005503 wpa_msg_global(iface->parent, MSG_INFO,
5504 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5505 iface->ifname,
5506 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507 }
5508 }
5509
5510 return 0;
5511}
5512
5513
5514static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5515{
5516 struct wpa_supplicant *iface;
5517
5518 if (!uplink->global->cross_connection)
5519 return;
5520
5521 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5522 if (!iface->cross_connect_enabled)
5523 continue;
5524 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5525 0)
5526 continue;
5527 if (iface->ap_iface == NULL)
5528 continue;
5529 if (iface->cross_connect_in_use)
5530 continue;
5531
5532 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005533 wpa_msg_global(iface->parent, MSG_INFO,
5534 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5535 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005536 }
5537}
5538
5539
5540static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5541{
5542 struct wpa_supplicant *iface;
5543
5544 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5545 if (!iface->cross_connect_enabled)
5546 continue;
5547 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5548 0)
5549 continue;
5550 if (!iface->cross_connect_in_use)
5551 continue;
5552
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005553 wpa_msg_global(iface->parent, MSG_INFO,
5554 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5555 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005556 iface->cross_connect_in_use = 0;
5557 }
5558}
5559
5560
5561void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5562{
5563 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5564 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5565 wpa_s->cross_connect_disallowed)
5566 wpas_p2p_disable_cross_connect(wpa_s);
5567 else
5568 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005569 if (!wpa_s->ap_iface &&
5570 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5571 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005572}
5573
5574
5575void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5576{
5577 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005578 if (!wpa_s->ap_iface &&
5579 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5580 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005581 wpas_p2p_set_group_idle_timeout(wpa_s);
5582}
5583
5584
5585static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5586{
5587 struct wpa_supplicant *iface;
5588
5589 if (!wpa_s->global->cross_connection)
5590 return;
5591
5592 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5593 if (iface == wpa_s)
5594 continue;
5595 if (iface->drv_flags &
5596 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5597 continue;
5598 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5599 continue;
5600
5601 wpa_s->cross_connect_enabled = 1;
5602 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5603 sizeof(wpa_s->cross_connect_uplink));
5604 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5605 "%s to %s whenever uplink is available",
5606 wpa_s->ifname, wpa_s->cross_connect_uplink);
5607
5608 if (iface->ap_iface || iface->current_ssid == NULL ||
5609 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5610 iface->cross_connect_disallowed ||
5611 iface->wpa_state != WPA_COMPLETED)
5612 break;
5613
5614 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005615 wpa_msg_global(wpa_s->parent, MSG_INFO,
5616 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5617 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005618 break;
5619 }
5620}
5621
5622
5623int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5624{
5625 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5626 !wpa_s->p2p_in_provisioning)
5627 return 0; /* not P2P client operation */
5628
5629 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5630 "session overlap");
5631 if (wpa_s != wpa_s->parent)
5632 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005633 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005634 return 1;
5635}
5636
5637
5638void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5639{
5640 struct p2p_channels chan;
5641
5642 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5643 return;
5644
5645 os_memset(&chan, 0, sizeof(chan));
5646 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5647 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5648 "channel list");
5649 return;
5650 }
5651
5652 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5653}
5654
5655
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005656static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5657 struct wpa_scan_results *scan_res)
5658{
5659 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5660}
5661
5662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5664{
5665 struct wpa_global *global = wpa_s->global;
5666 int found = 0;
5667 const u8 *peer;
5668
5669 if (global->p2p == NULL)
5670 return -1;
5671
5672 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5673
5674 if (wpa_s->pending_interface_name[0] &&
5675 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5676 found = 1;
5677
5678 peer = p2p_get_go_neg_peer(global->p2p);
5679 if (peer) {
5680 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5681 MACSTR, MAC2STR(peer));
5682 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005683 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005684 }
5685
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005686 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5687 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5688 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5689 found = 1;
5690 }
5691
5692 if (wpa_s->pending_pd_before_join) {
5693 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5694 wpa_s->pending_pd_before_join = 0;
5695 found = 1;
5696 }
5697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005698 wpas_p2p_stop_find(wpa_s);
5699
5700 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5701 if (wpa_s == global->p2p_group_formation &&
5702 (wpa_s->p2p_in_provisioning ||
5703 wpa_s->parent->pending_interface_type ==
5704 WPA_IF_P2P_CLIENT)) {
5705 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5706 "formation found - cancelling",
5707 wpa_s->ifname);
5708 found = 1;
5709 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5710 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005711 if (wpa_s->p2p_in_provisioning) {
5712 wpas_group_formation_completed(wpa_s, 0);
5713 break;
5714 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005715 wpas_p2p_group_delete(wpa_s,
5716 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005717 break;
5718 }
5719 }
5720
5721 if (!found) {
5722 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5723 return -1;
5724 }
5725
5726 return 0;
5727}
5728
5729
5730void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5731{
5732 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5733 return;
5734
5735 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5736 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005737 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005738}
5739
5740
5741void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5742 int freq_24, int freq_5, int freq_overall)
5743{
5744 struct p2p_data *p2p = wpa_s->global->p2p;
5745 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5746 return;
5747 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5748}
5749
5750
5751int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5752{
5753 u8 peer[ETH_ALEN];
5754 struct p2p_data *p2p = wpa_s->global->p2p;
5755
5756 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5757 return -1;
5758
5759 if (hwaddr_aton(addr, peer))
5760 return -1;
5761
5762 return p2p_unauthorize(p2p, peer);
5763}
5764
5765
5766/**
5767 * wpas_p2p_disconnect - Disconnect from a P2P Group
5768 * @wpa_s: Pointer to wpa_supplicant data
5769 * Returns: 0 on success, -1 on failure
5770 *
5771 * This can be used to disconnect from a group in which the local end is a P2P
5772 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5773 * virtual network interface was created for this group, that interface will be
5774 * removed. Otherwise, only the configured P2P group network will be removed
5775 * from the interface.
5776 */
5777int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5778{
5779
5780 if (wpa_s == NULL)
5781 return -1;
5782
Jouni Malinen2b89da82012-08-31 22:04:41 +03005783 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5784 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005785}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005786
5787
5788int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5789{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005790 int ret;
5791
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005792 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5793 return 0;
5794
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005795 ret = p2p_in_progress(wpa_s->global->p2p);
5796 if (ret == 0) {
5797 /*
5798 * Check whether there is an ongoing WPS provisioning step (or
5799 * other parts of group formation) on another interface since
5800 * p2p_in_progress() does not report this to avoid issues for
5801 * scans during such provisioning step.
5802 */
5803 if (wpa_s->global->p2p_group_formation &&
5804 wpa_s->global->p2p_group_formation != wpa_s) {
5805 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5806 "in group formation",
5807 wpa_s->global->p2p_group_formation->ifname);
5808 ret = 1;
5809 }
5810 }
5811
5812 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005813}
5814
5815
5816void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5817 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005818{
5819 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5820 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5821 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005822 /**
5823 * Remove the network by scheduling the group formation
5824 * timeout to happen immediately. The teardown code
5825 * needs to be scheduled to run asynch later so that we
5826 * don't delete data from under ourselves unexpectedly.
5827 * Calling wpas_p2p_group_formation_timeout directly
5828 * causes a series of crashes in WPS failure scenarios.
5829 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005830 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5831 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005832 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5833 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005834 }
5835}
5836
5837
5838struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005839 const u8 *addr, const u8 *ssid,
5840 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005841{
5842 struct wpa_ssid *s;
5843 size_t i;
5844
5845 for (s = wpa_s->conf->ssid; s; s = s->next) {
5846 if (s->disabled != 2)
5847 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005848 if (ssid &&
5849 (ssid_len != s->ssid_len ||
5850 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5851 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005852 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5853 return s; /* peer is GO in the persistent group */
5854 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5855 continue;
5856 for (i = 0; i < s->num_p2p_clients; i++) {
5857 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5858 addr, ETH_ALEN) == 0)
5859 return s; /* peer is P2P client in persistent
5860 * group */
5861 }
5862 }
5863
5864 return NULL;
5865}
5866
5867
5868void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5869 const u8 *addr)
5870{
5871 if (addr == NULL)
5872 return;
5873 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5874}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005875
Dmitry Shmidt04949592012-07-19 12:16:46 -07005876
5877static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5878 int group_added)
5879{
5880 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005881 if (wpa_s->global->p2p_group_formation)
5882 group = wpa_s->global->p2p_group_formation;
5883 wpa_s = wpa_s->parent;
5884 offchannel_send_action_done(wpa_s);
5885 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005886 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005887 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5888 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5889 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5890 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5891 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005892 wpa_s->p2p_pd_before_go_neg,
5893 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005894}
5895
5896
5897int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5898{
5899 if (!wpa_s->p2p_fallback_to_go_neg ||
5900 wpa_s->p2p_in_provisioning <= 5)
5901 return 0;
5902
5903 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5904 return 0; /* peer operating as a GO */
5905
5906 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5907 "fallback to GO Negotiation");
5908 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5909
5910 return 1;
5911}
5912
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005913
5914unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5915{
5916 const char *rn, *rn2;
5917 struct wpa_supplicant *ifs;
5918
5919 if (wpa_s->wpa_state > WPA_SCANNING) {
5920 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5921 "concurrent operation",
5922 P2P_CONCURRENT_SEARCH_DELAY);
5923 return P2P_CONCURRENT_SEARCH_DELAY;
5924 }
5925
5926 if (!wpa_s->driver->get_radio_name)
5927 return 0;
5928 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5929 if (rn == NULL || rn[0] == '\0')
5930 return 0;
5931
5932 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5933 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5934 continue;
5935
5936 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5937 if (!rn2 || os_strcmp(rn, rn2) != 0)
5938 continue;
5939 if (ifs->wpa_state > WPA_SCANNING) {
5940 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5941 "delay due to concurrent operation on "
5942 "interface %s",
5943 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5944 return P2P_CONCURRENT_SEARCH_DELAY;
5945 }
5946 }
5947
5948 return 0;
5949}
5950
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005951
5952void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5953{
5954 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5955 "pending anymore (sta_scan_pending=%d "
5956 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5957 wpa_s->global->p2p_cb_on_scan_complete);
5958 wpa_s->sta_scan_pending = 0;
5959
5960 if (!wpa_s->global->p2p_cb_on_scan_complete)
5961 return;
5962 wpa_s->global->p2p_cb_on_scan_complete = 0;
5963
5964 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5965 return;
5966
5967 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
5968 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
5969 "continued after successful connection");
5970 p2p_increase_search_delay(wpa_s->global->p2p,
5971 wpas_p2p_search_delay(wpa_s));
5972 }
5973}
5974
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005975#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07005976static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
5977{
5978 struct wpa_supplicant *wpa_s = eloop_ctx;
5979
5980 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
5981 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
5982}
5983
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005984int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5985 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005986{
5987 struct wpa_supplicant *iface = NULL;
5988 struct p2p_data *p2p = wpa_s->global->p2p;
5989
5990 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005991 if ((iface->current_ssid) &&
5992 (iface->current_ssid->frequency != freq) &&
5993 ((iface->p2p_group_interface) ||
5994 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005995
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005996 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5997 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5998 /* Try to see whether we can move the GO. If it
5999 * is not possible, remove the GO interface
6000 */
6001 if (wpa_drv_switch_channel(iface, freq) == 0) {
6002 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
6003 iface->current_ssid->frequency = freq;
6004 continue;
6005 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006006 }
6007
6008 /* If GO cannot be moved or if the conflicting interface is a
6009 * P2P Client, remove the interface depending up on the connection
6010 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006011 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07006012 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006013 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006014 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006015 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006016 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
6017 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006018 /* If connection in progress is p2p connection, do not proceed for the connection */
6019 if (wpa_s == iface)
6020 return -1;
6021 else
6022 /* If connection in progress is STA connection, proceed for the connection */
6023 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006024 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006025 /* P2p connection has priority, disable the STA network*/
6026 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
6027 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6028 " id=%d", ssid->id);
6029 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6030 if (wpa_s == iface) {
6031 /* p2p connection is in progress, continue connecting...*/
6032 return 0;
6033 }
6034 else {
6035 /* STA connection is in progress, do not allow to continue */
6036 return -1;
6037 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006038 }
6039 }
6040 }
6041 return 0;
6042}
6043#endif