blob: e6a88e63dfbff51128eed1d531bd02300ec4bcd0 [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 Shmidt391c59f2013-09-03 12:16:28 -070020#include "ap/sta_info.h"
21#include "ap/ap_drv_ops.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070023#include "eapol_supp/eapol_supp_sm.h"
24#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "wpa_supplicant_i.h"
26#include "driver_i.h"
27#include "ap.h"
28#include "config_ssid.h"
29#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "notify.h"
31#include "scan.h"
32#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "wps_supplicant.h"
35#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080036#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037
38
39/*
40 * How many times to try to scan to find the GO before giving up on join
41 * request.
42 */
43#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
44
Dmitry Shmidt04949592012-07-19 12:16:46 -070045#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
46
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080047#ifndef P2P_MAX_CLIENT_IDLE
48/*
49 * How many seconds to try to reconnect to the GO when connection in P2P client
50 * role has been lost.
51 */
52#define P2P_MAX_CLIENT_IDLE 10
53#endif /* P2P_MAX_CLIENT_IDLE */
54
Dmitry Shmidt04949592012-07-19 12:16:46 -070055#ifndef P2P_MAX_INITIAL_CONN_WAIT
56/*
57 * How many seconds to wait for initial 4-way handshake to get completed after
58 * WPS provisioning step.
59 */
60#define P2P_MAX_INITIAL_CONN_WAIT 10
61#endif /* P2P_MAX_INITIAL_CONN_WAIT */
62
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070063#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
64/*
65 * How many seconds to wait for initial 4-way handshake to get completed after
66 * WPS provisioning step on the GO. This controls the extra time the P2P
67 * operation is considered to be in progress (e.g., to delay other scans) after
68 * WPS provisioning has been completed on the GO during group formation.
69 */
70#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
71#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
72
Dmitry Shmidt56052862013-10-04 10:23:25 -070073#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
74/*
75 * How many seconds to wait for initial 4-way handshake to get completed after
76 * re-invocation of a persistent group on the GO when the client is expected
77 * to connect automatically (no user interaction).
78 */
79#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
80#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
81
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070082#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070083#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070084#endif /* P2P_CONCURRENT_SEARCH_DELAY */
85
Dmitry Shmidt34af3062013-07-11 10:46:32 -070086#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
87
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070088enum p2p_group_removal_reason {
89 P2P_GROUP_REMOVAL_UNKNOWN,
90 P2P_GROUP_REMOVAL_SILENT,
91 P2P_GROUP_REMOVAL_FORMATION_FAILED,
92 P2P_GROUP_REMOVAL_REQUESTED,
93 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
94 P2P_GROUP_REMOVAL_UNAVAILABLE,
95 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080096 P2P_GROUP_REMOVAL_PSK_FAILURE,
97 P2P_GROUP_REMOVAL_FREQ_CONFLICT
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070098};
99
Jouni Malinendc7b7132012-09-14 12:53:47 -0700100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
102static struct wpa_supplicant *
103wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
104 int go);
105static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700106static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
108static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700109 const u8 *dev_addr, enum p2p_wps_method wps_method,
110 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
112static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
113static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
114static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800115static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
116 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800117static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700118static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
119 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800120static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121
122
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700123/*
124 * Get the number of concurrent channels that the HW can operate, but that are
125 * currently not in use by any of the wpa_supplicant interfaces.
126 */
127static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
128{
129 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800130 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700131
132 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
133 if (!freqs)
134 return -1;
135
136 num = get_shared_radio_freqs(wpa_s, freqs,
137 wpa_s->num_multichan_concurrent);
138 os_free(freqs);
139
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800140 unused = wpa_s->num_multichan_concurrent - num;
141 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
142 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700143}
144
145
146/*
147 * Get the frequencies that are currently in use by one or more of the virtual
148 * interfaces, and that are also valid for P2P operation.
149 */
150static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
151 int *p2p_freqs, unsigned int len)
152{
153 int *freqs;
154 unsigned int num, i, j;
155
156 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
157 if (!freqs)
158 return -1;
159
160 num = get_shared_radio_freqs(wpa_s, freqs,
161 wpa_s->num_multichan_concurrent);
162
163 os_memset(p2p_freqs, 0, sizeof(int) * len);
164
165 for (i = 0, j = 0; i < num && j < len; i++) {
166 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
167 p2p_freqs[j++] = freqs[i];
168 }
169
170 os_free(freqs);
171
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800172 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
173
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700174 return j;
175}
176
177
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700178static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
179 int freq)
180{
181 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
182 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800183 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
184 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
185 wpas_p2p_num_unused_channels(wpa_s) > 0) {
186 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
187 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700188 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800189 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700190 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
191}
192
193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
195 struct wpa_scan_results *scan_res)
196{
197 size_t i;
198
199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
200 return;
201
202 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
203 (int) scan_res->num);
204
205 for (i = 0; i < scan_res->num; i++) {
206 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800207 struct os_time time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700208 const u8 *ies;
209 size_t ies_len;
210
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800211 time_tmp_age.sec = bss->age / 1000;
212 time_tmp_age.usec = (bss->age % 1000) * 1000;
213 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700214
215 ies = (const u8 *) (bss + 1);
216 ies_len = bss->ie_len;
217 if (bss->beacon_ie_len > 0 &&
218 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
219 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
220 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
221 MACSTR, MAC2STR(bss->bssid));
222 ies = ies + ies_len;
223 ies_len = bss->beacon_ie_len;
224 }
225
226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800228 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700229 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 break;
231 }
232
233 p2p_scan_res_handled(wpa_s->global->p2p);
234}
235
236
237static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
238 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700239 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240{
241 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700242 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 struct wpa_driver_scan_params params;
244 int ret;
245 struct wpabuf *wps_ie, *ies;
246 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248
249 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
250 return -1;
251
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700252 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
253 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700254 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700255 wpas_p2p_in_progress(wpa_s) == 2) {
256 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
257 "pending station mode scan to be "
258 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700259 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700260 wpa_supplicant_req_scan(ifs, 0, 0);
261 return 1;
262 }
263 }
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 os_memset(&params, 0, sizeof(params));
266
267 /* P2P Wildcard SSID */
268 params.num_ssids = 1;
269 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
270 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
271
272 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700273 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
274 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 num_req_dev_types, req_dev_types);
276 if (wps_ie == NULL)
277 return -1;
278
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800279 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
280 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 if (ies == NULL) {
282 wpabuf_free(wps_ie);
283 return -1;
284 }
285 wpabuf_put_buf(ies, wps_ie);
286 wpabuf_free(wps_ie);
287
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800288 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800290 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 params.extra_ies = wpabuf_head(ies);
292 params.extra_ies_len = wpabuf_len(ies);
293
294 switch (type) {
295 case P2P_SCAN_SOCIAL:
296 params.freqs = social_channels;
297 break;
298 case P2P_SCAN_FULL:
299 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 case P2P_SCAN_SOCIAL_PLUS_ONE:
301 social_channels[3] = freq;
302 params.freqs = social_channels;
303 break;
304 }
305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307
308 wpabuf_free(ies);
309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800310 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700311 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
312 if (ifs->scanning ||
313 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
314 wpa_s->global->p2p_cb_on_scan_complete = 1;
315 ret = 1;
316 break;
317 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800318 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700319 } else {
320 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700321 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700322 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 return ret;
325}
326
327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
329{
330 switch (p2p_group_interface) {
331 case P2P_GROUP_INTERFACE_PENDING:
332 return WPA_IF_P2P_GROUP;
333 case P2P_GROUP_INTERFACE_GO:
334 return WPA_IF_P2P_GO;
335 case P2P_GROUP_INTERFACE_CLIENT:
336 return WPA_IF_P2P_CLIENT;
337 }
338
339 return WPA_IF_P2P_GROUP;
340}
341
342
343static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
344 const u8 *ssid,
345 size_t ssid_len, int *go)
346{
347 struct wpa_ssid *s;
348
349 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
350 for (s = wpa_s->conf->ssid; s; s = s->next) {
351 if (s->disabled != 0 || !s->p2p_group ||
352 s->ssid_len != ssid_len ||
353 os_memcmp(ssid, s->ssid, ssid_len) != 0)
354 continue;
355 if (s->mode == WPAS_MODE_P2P_GO &&
356 s != wpa_s->current_ssid)
357 continue;
358 if (go)
359 *go = s->mode == WPAS_MODE_P2P_GO;
360 return wpa_s;
361 }
362 }
363
364 return NULL;
365}
366
367
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700368static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
369 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370{
371 struct wpa_ssid *ssid;
372 char *gtype;
373 const char *reason;
374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 ssid = wpa_s->current_ssid;
376 if (ssid == NULL) {
377 /*
378 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700379 * pending P2P group interface waiting for provisioning or a
380 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 */
382 ssid = wpa_s->conf->ssid;
383 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700384 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 break;
386 ssid = ssid->next;
387 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300388 if (ssid == NULL &&
389 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
390 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700391 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
392 "not found");
393 return -1;
394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395 }
396 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
397 gtype = "GO";
398 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
399 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
400 wpa_s->reassociate = 0;
401 wpa_s->disconnected = 1;
402 wpa_supplicant_deauthenticate(wpa_s,
403 WLAN_REASON_DEAUTH_LEAVING);
404 gtype = "client";
405 } else
406 gtype = "GO";
407 if (wpa_s->cross_connect_in_use) {
408 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700409 wpa_msg_global(wpa_s->parent, MSG_INFO,
410 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
411 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700413 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 case P2P_GROUP_REMOVAL_REQUESTED:
415 reason = " reason=REQUESTED";
416 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700417 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
418 reason = " reason=FORMATION_FAILED";
419 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
421 reason = " reason=IDLE";
422 break;
423 case P2P_GROUP_REMOVAL_UNAVAILABLE:
424 reason = " reason=UNAVAILABLE";
425 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700426 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
427 reason = " reason=GO_ENDING_SESSION";
428 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700429 case P2P_GROUP_REMOVAL_PSK_FAILURE:
430 reason = " reason=PSK_FAILURE";
431 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800432 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
433 reason = " reason=FREQ_CONFLICT";
434 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 default:
436 reason = "";
437 break;
438 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700439 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700440 wpa_msg_global(wpa_s->parent, MSG_INFO,
441 P2P_EVENT_GROUP_REMOVED "%s %s%s",
442 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700443 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800445 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
446 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700447 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
448 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800449 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700450 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800451 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
452 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700453 wpa_s->p2p_in_provisioning = 0;
454 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700455
Dmitry Shmidt96571392013-10-14 12:54:46 -0700456 /*
457 * Make sure wait for the first client does not remain active after the
458 * group has been removed.
459 */
460 wpa_s->global->p2p_go_wait_client.sec = 0;
461
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700462 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
464
465 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
466 struct wpa_global *global;
467 char *ifname;
468 enum wpa_driver_if_type type;
469 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
470 wpa_s->ifname);
471 global = wpa_s->global;
472 ifname = os_strdup(wpa_s->ifname);
473 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700474 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 wpa_s = global->ifaces;
476 if (wpa_s && ifname)
477 wpa_drv_if_remove(wpa_s, type, ifname);
478 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300479 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480 }
481
Dmitry Shmidt96571392013-10-14 12:54:46 -0700482 if (!wpa_s->p2p_go_group_formation_completed) {
483 wpa_s->global->p2p_group_formation = NULL;
484 wpa_s->p2p_in_provisioning = 0;
485 }
486
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800487 wpa_s->show_group_started = 0;
488 os_free(wpa_s->go_params);
489 wpa_s->go_params = NULL;
490
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
492 if (ssid && (ssid->p2p_group ||
493 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
494 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
495 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700496 if (ssid == wpa_s->current_ssid) {
497 wpa_sm_set_config(wpa_s->wpa, NULL);
498 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700499 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700500 }
501 /*
502 * Networks objects created during any P2P activities are not
503 * exposed out as they might/will confuse certain non-P2P aware
504 * applications since these network objects won't behave like
505 * regular ones.
506 *
507 * Likewise, we don't send out network removed signals for such
508 * network objects.
509 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700510 wpa_config_remove_network(wpa_s->conf, id);
511 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800512 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700513 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514 } else {
515 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
516 "found");
517 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700518 if (wpa_s->ap_iface)
519 wpa_supplicant_ap_deinit(wpa_s);
520 else
521 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700522
523 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524}
525
526
527static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
528 u8 *go_dev_addr,
529 const u8 *ssid, size_t ssid_len)
530{
531 struct wpa_bss *bss;
532 const u8 *bssid;
533 struct wpabuf *p2p;
534 u8 group_capab;
535 const u8 *addr;
536
537 if (wpa_s->go_params)
538 bssid = wpa_s->go_params->peer_interface_addr;
539 else
540 bssid = wpa_s->bssid;
541
542 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
543 if (bss == NULL) {
544 u8 iface_addr[ETH_ALEN];
545 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
546 iface_addr) == 0)
547 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
548 }
549 if (bss == NULL) {
550 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
551 "group is persistent - BSS " MACSTR " not found",
552 MAC2STR(bssid));
553 return 0;
554 }
555
556 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700557 if (p2p == NULL)
558 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
559 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560 if (p2p == NULL) {
561 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
562 "group is persistent - BSS " MACSTR
563 " did not include P2P IE", MAC2STR(bssid));
564 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
565 (u8 *) (bss + 1), bss->ie_len);
566 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
567 ((u8 *) bss + 1) + bss->ie_len,
568 bss->beacon_ie_len);
569 return 0;
570 }
571
572 group_capab = p2p_get_group_capab(p2p);
573 addr = p2p_get_go_dev_addr(p2p);
574 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
575 "group_capab=0x%x", group_capab);
576 if (addr) {
577 os_memcpy(go_dev_addr, addr, ETH_ALEN);
578 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
579 MAC2STR(addr));
580 } else
581 os_memset(go_dev_addr, 0, ETH_ALEN);
582 wpabuf_free(p2p);
583
584 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
585 "go_dev_addr=" MACSTR,
586 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
587
588 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
589}
590
591
Jouni Malinen75ecf522011-06-27 15:19:46 -0700592static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
593 struct wpa_ssid *ssid,
594 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595{
596 struct wpa_ssid *s;
597 int changed = 0;
598
599 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
600 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
601 for (s = wpa_s->conf->ssid; s; s = s->next) {
602 if (s->disabled == 2 &&
603 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
604 s->ssid_len == ssid->ssid_len &&
605 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
606 break;
607 }
608
609 if (s) {
610 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
611 "entry");
612 if (ssid->passphrase && !s->passphrase)
613 changed = 1;
614 else if (ssid->passphrase && s->passphrase &&
615 os_strcmp(ssid->passphrase, s->passphrase) != 0)
616 changed = 1;
617 } else {
618 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
619 "entry");
620 changed = 1;
621 s = wpa_config_add_network(wpa_s->conf);
622 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700623 return -1;
624
625 /*
626 * Instead of network_added we emit persistent_group_added
627 * notification. Also to keep the defense checks in
628 * persistent_group obj registration method, we set the
629 * relevant flags in s to designate it as a persistent group.
630 */
631 s->p2p_group = 1;
632 s->p2p_persistent_group = 1;
633 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 wpa_config_set_network_defaults(s);
635 }
636
637 s->p2p_group = 1;
638 s->p2p_persistent_group = 1;
639 s->disabled = 2;
640 s->bssid_set = 1;
641 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
642 s->mode = ssid->mode;
643 s->auth_alg = WPA_AUTH_ALG_OPEN;
644 s->key_mgmt = WPA_KEY_MGMT_PSK;
645 s->proto = WPA_PROTO_RSN;
646 s->pairwise_cipher = WPA_CIPHER_CCMP;
647 s->export_keys = 1;
648 if (ssid->passphrase) {
649 os_free(s->passphrase);
650 s->passphrase = os_strdup(ssid->passphrase);
651 }
652 if (ssid->psk_set) {
653 s->psk_set = 1;
654 os_memcpy(s->psk, ssid->psk, 32);
655 }
656 if (s->passphrase && !s->psk_set)
657 wpa_config_update_psk(s);
658 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
659 os_free(s->ssid);
660 s->ssid = os_malloc(ssid->ssid_len);
661 }
662 if (s->ssid) {
663 s->ssid_len = ssid->ssid_len;
664 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
665 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700666 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
667 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
668 wpa_s->global->add_psk = NULL;
669 changed = 1;
670 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671
672#ifndef CONFIG_NO_CONFIG_WRITE
673 if (changed && wpa_s->conf->update_config &&
674 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
675 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
676 }
677#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700678
679 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680}
681
682
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800683static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
684 const u8 *addr)
685{
686 struct wpa_ssid *ssid, *s;
687 u8 *n;
688 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700689 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800690
691 ssid = wpa_s->current_ssid;
692 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
693 !ssid->p2p_persistent_group)
694 return;
695
696 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
697 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
698 continue;
699
700 if (s->ssid_len == ssid->ssid_len &&
701 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
702 break;
703 }
704
705 if (s == NULL)
706 return;
707
708 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
709 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700710 ETH_ALEN) != 0)
711 continue;
712
713 if (i == s->num_p2p_clients - 1)
714 return; /* already the most recent entry */
715
716 /* move the entry to mark it most recent */
717 os_memmove(s->p2p_client_list + i * ETH_ALEN,
718 s->p2p_client_list + (i + 1) * ETH_ALEN,
719 (s->num_p2p_clients - i - 1) * ETH_ALEN);
720 os_memcpy(s->p2p_client_list +
721 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
722 found = 1;
723 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800724 }
725
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700726 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
727 n = os_realloc_array(s->p2p_client_list,
728 s->num_p2p_clients + 1, ETH_ALEN);
729 if (n == NULL)
730 return;
731 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
732 s->p2p_client_list = n;
733 s->num_p2p_clients++;
734 } else if (!found) {
735 /* Not enough room for an additional entry - drop the oldest
736 * entry */
737 os_memmove(s->p2p_client_list,
738 s->p2p_client_list + ETH_ALEN,
739 (s->num_p2p_clients - 1) * ETH_ALEN);
740 os_memcpy(s->p2p_client_list +
741 (s->num_p2p_clients - 1) * ETH_ALEN,
742 addr, ETH_ALEN);
743 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800744
745#ifndef CONFIG_NO_CONFIG_WRITE
746 if (wpa_s->parent->conf->update_config &&
747 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
748 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
749#endif /* CONFIG_NO_CONFIG_WRITE */
750}
751
752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
754 int success)
755{
756 struct wpa_ssid *ssid;
757 const char *ssid_txt;
758 int client;
759 int persistent;
760 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700761 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762
763 /*
764 * This callback is likely called for the main interface. Update wpa_s
765 * to use the group interface if a new interface was created for the
766 * group.
767 */
768 if (wpa_s->global->p2p_group_formation)
769 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700770 if (wpa_s->p2p_go_group_formation_completed) {
771 wpa_s->global->p2p_group_formation = NULL;
772 wpa_s->p2p_in_provisioning = 0;
773 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774
775 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700776 wpa_msg_global(wpa_s->parent, MSG_INFO,
777 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700778 wpas_p2p_group_delete(wpa_s,
779 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 return;
781 }
782
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700783 wpa_msg_global(wpa_s->parent, MSG_INFO,
784 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785
786 ssid = wpa_s->current_ssid;
787 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
788 ssid->mode = WPAS_MODE_P2P_GO;
789 p2p_group_notif_formation_done(wpa_s->p2p_group);
790 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
791 }
792
793 persistent = 0;
794 if (ssid) {
795 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
796 client = ssid->mode == WPAS_MODE_INFRA;
797 if (ssid->mode == WPAS_MODE_P2P_GO) {
798 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700799 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
800 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801 } else
802 persistent = wpas_p2p_persistent_group(wpa_s,
803 go_dev_addr,
804 ssid->ssid,
805 ssid->ssid_len);
806 } else {
807 ssid_txt = "";
808 client = wpa_s->p2p_group_interface ==
809 P2P_GROUP_INTERFACE_CLIENT;
810 os_memset(go_dev_addr, 0, ETH_ALEN);
811 }
812
813 wpa_s->show_group_started = 0;
814 if (client) {
815 /*
816 * Indicate event only after successfully completed 4-way
817 * handshake, i.e., when the interface is ready for data
818 * packets.
819 */
820 wpa_s->show_group_started = 1;
821 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
822 char psk[65];
823 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700824 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
825 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
826 MACSTR "%s",
827 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
828 MAC2STR(go_dev_addr),
829 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830 wpas_p2p_cross_connect_setup(wpa_s);
831 wpas_p2p_set_group_idle_timeout(wpa_s);
832 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700833 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
834 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
835 "go_dev_addr=" MACSTR "%s",
836 wpa_s->ifname, ssid_txt,
837 ssid ? ssid->frequency : 0,
838 ssid && ssid->passphrase ? ssid->passphrase : "",
839 MAC2STR(go_dev_addr),
840 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 wpas_p2p_cross_connect_setup(wpa_s);
842 wpas_p2p_set_group_idle_timeout(wpa_s);
843 }
844
845 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700846 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
847 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700848 else {
849 os_free(wpa_s->global->add_psk);
850 wpa_s->global->add_psk = NULL;
851 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800852 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700853 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700854 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700855 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700856 os_get_time(&wpa_s->global->p2p_go_wait_client);
857 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858}
859
860
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800861static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
862 unsigned int freq,
863 const u8 *dst, const u8 *src,
864 const u8 *bssid,
865 const u8 *data, size_t data_len,
866 enum offchannel_send_action_result
867 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800869 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
872 return;
873 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
874 return;
875
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800876 switch (result) {
877 case OFFCHANNEL_SEND_ACTION_SUCCESS:
878 res = P2P_SEND_ACTION_SUCCESS;
879 break;
880 case OFFCHANNEL_SEND_ACTION_NO_ACK:
881 res = P2P_SEND_ACTION_NO_ACK;
882 break;
883 case OFFCHANNEL_SEND_ACTION_FAILED:
884 res = P2P_SEND_ACTION_FAILED;
885 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 }
887
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800888 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800890 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
891 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800892 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800893 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
894 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800896 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
897 "during p2p_connect-auto");
898 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
899 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 }
901}
902
903
904static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
905 const u8 *src, const u8 *bssid, const u8 *buf,
906 size_t len, unsigned int wait_time)
907{
908 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800909 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
910 wait_time,
911 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912}
913
914
915static void wpas_send_action_done(void *ctx)
916{
917 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800918 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919}
920
921
922static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
923 struct p2p_go_neg_results *params)
924{
925 if (wpa_s->go_params == NULL) {
926 wpa_s->go_params = os_malloc(sizeof(*params));
927 if (wpa_s->go_params == NULL)
928 return -1;
929 }
930 os_memcpy(wpa_s->go_params, params, sizeof(*params));
931 return 0;
932}
933
934
935static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
936 struct p2p_go_neg_results *res)
937{
938 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
939 MAC2STR(res->peer_interface_addr));
940 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
941 res->ssid, res->ssid_len);
942 wpa_supplicant_ap_deinit(wpa_s);
943 wpas_copy_go_neg_results(wpa_s, res);
944 if (res->wps_method == WPS_PBC)
945 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
946 else {
947 u16 dev_pw_id = DEV_PW_DEFAULT;
948 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
949 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
950 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
951 wpa_s->p2p_pin, 1, dev_pw_id);
952 }
953}
954
955
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700956static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
957 struct wpa_ssid *ssid)
958{
959 struct wpa_ssid *persistent;
960 struct psk_list_entry *psk;
961 struct hostapd_data *hapd;
962
963 if (!wpa_s->ap_iface)
964 return;
965
966 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
967 ssid->ssid_len);
968 if (persistent == NULL)
969 return;
970
971 hapd = wpa_s->ap_iface->bss[0];
972
973 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
974 list) {
975 struct hostapd_wpa_psk *hpsk;
976
977 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
978 MACSTR " psk=%d",
979 MAC2STR(psk->addr), psk->p2p);
980 hpsk = os_zalloc(sizeof(*hpsk));
981 if (hpsk == NULL)
982 break;
983 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
984 if (psk->p2p)
985 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
986 else
987 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
988 hpsk->next = hapd->conf->ssid.wpa_psk;
989 hapd->conf->ssid.wpa_psk = hpsk;
990 }
991}
992
993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994static void p2p_go_configured(void *ctx, void *data)
995{
996 struct wpa_supplicant *wpa_s = ctx;
997 struct p2p_go_neg_results *params = data;
998 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700999 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000
1001 ssid = wpa_s->current_ssid;
1002 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1003 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1004 if (wpa_s->global->p2p_group_formation == wpa_s)
1005 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001006 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001007 wpa_msg_global(wpa_s->parent, MSG_INFO,
1008 P2P_EVENT_GROUP_STARTED
1009 "%s GO ssid=\"%s\" freq=%d "
1010 "passphrase=\"%s\" go_dev_addr=" MACSTR
1011 "%s", wpa_s->ifname,
1012 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1013 ssid->frequency, params->passphrase,
1014 MAC2STR(wpa_s->global->p2p_dev_addr),
1015 params->persistent_group ?
1016 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001017 } else {
1018 char psk[65];
1019 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1020 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001021 wpa_msg_global(wpa_s->parent, MSG_INFO,
1022 P2P_EVENT_GROUP_STARTED
1023 "%s GO ssid=\"%s\" freq=%d psk=%s "
1024 "go_dev_addr=" MACSTR "%s",
1025 wpa_s->ifname,
1026 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1027 ssid->frequency, psk,
1028 MAC2STR(wpa_s->global->p2p_dev_addr),
1029 params->persistent_group ?
1030 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001031 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001032
Dmitry Shmidt56052862013-10-04 10:23:25 -07001033 os_get_time(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001034 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001035 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001037 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001038 wpas_p2p_add_psk_list(wpa_s, ssid);
1039 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001040 if (network_id < 0)
1041 network_id = ssid->id;
1042 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 wpas_p2p_cross_connect_setup(wpa_s);
1044 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001045
1046 if (wpa_s->p2p_first_connection_timeout) {
1047 wpa_dbg(wpa_s, MSG_DEBUG,
1048 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1049 wpa_s->p2p_first_connection_timeout);
1050 wpa_s->p2p_go_group_formation_completed = 0;
1051 wpa_s->global->p2p_group_formation = wpa_s;
1052 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1053 wpa_s->parent, NULL);
1054 eloop_register_timeout(
1055 wpa_s->p2p_first_connection_timeout, 0,
1056 wpas_p2p_group_formation_timeout,
1057 wpa_s->parent, NULL);
1058 }
1059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 return;
1061 }
1062
1063 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1064 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1065 params->peer_interface_addr)) {
1066 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1067 "filtering");
1068 return;
1069 }
1070 if (params->wps_method == WPS_PBC)
1071 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001072 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 else if (wpa_s->p2p_pin[0])
1074 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001075 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 os_free(wpa_s->go_params);
1077 wpa_s->go_params = NULL;
1078}
1079
1080
1081static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1082 struct p2p_go_neg_results *params,
1083 int group_formation)
1084{
1085 struct wpa_ssid *ssid;
1086
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001087 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1088 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1089 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1090 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001092 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093
1094 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001095 if (ssid == NULL) {
1096 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001098 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001100 wpa_s->show_group_started = 0;
1101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 wpa_config_set_network_defaults(ssid);
1103 ssid->temporary = 1;
1104 ssid->p2p_group = 1;
1105 ssid->p2p_persistent_group = params->persistent_group;
1106 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1107 WPAS_MODE_P2P_GO;
1108 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001109 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001110 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 ssid->ssid = os_zalloc(params->ssid_len + 1);
1112 if (ssid->ssid) {
1113 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1114 ssid->ssid_len = params->ssid_len;
1115 }
1116 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1117 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1118 ssid->proto = WPA_PROTO_RSN;
1119 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001120 if (os_strlen(params->passphrase) > 0) {
1121 ssid->passphrase = os_strdup(params->passphrase);
1122 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001123 wpa_msg_global(wpa_s, MSG_ERROR,
1124 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001125 wpa_config_remove_network(wpa_s->conf, ssid->id);
1126 return;
1127 }
1128 } else
1129 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001130 ssid->psk_set = params->psk_set;
1131 if (ssid->psk_set)
1132 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001133 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001134 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001135 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136
1137 wpa_s->ap_configured_cb = p2p_go_configured;
1138 wpa_s->ap_configured_cb_ctx = wpa_s;
1139 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001140 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 wpa_s->reassociate = 1;
1142 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001143 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1144 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 wpa_supplicant_req_scan(wpa_s, 0, 0);
1146}
1147
1148
1149static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1150 const struct wpa_supplicant *src)
1151{
1152 struct wpa_config *d;
1153 const struct wpa_config *s;
1154
1155 d = dst->conf;
1156 s = src->conf;
1157
1158#define C(n) if (s->n) d->n = os_strdup(s->n)
1159 C(device_name);
1160 C(manufacturer);
1161 C(model_name);
1162 C(model_number);
1163 C(serial_number);
1164 C(config_methods);
1165#undef C
1166
1167 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1168 os_memcpy(d->sec_device_type, s->sec_device_type,
1169 sizeof(d->sec_device_type));
1170 d->num_sec_device_types = s->num_sec_device_types;
1171
1172 d->p2p_group_idle = s->p2p_group_idle;
1173 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001174 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001175 d->max_num_sta = s->max_num_sta;
1176 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001177 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001178 d->beacon_int = s->beacon_int;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001179 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001180 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181}
1182
1183
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001184static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1185 char *ifname, size_t len)
1186{
1187 char *ifname_ptr = wpa_s->ifname;
1188
1189 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1190 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1191 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1192 }
1193
1194 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1195 if (os_strlen(ifname) >= IFNAMSIZ &&
1196 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1197 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001198 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001199 }
1200}
1201
1202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1204 enum wpa_driver_if_type type)
1205{
1206 char ifname[120], force_ifname[120];
1207
1208 if (wpa_s->pending_interface_name[0]) {
1209 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1210 "- skip creation of a new one");
1211 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1212 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1213 "unknown?! ifname='%s'",
1214 wpa_s->pending_interface_name);
1215 return -1;
1216 }
1217 return 0;
1218 }
1219
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001220 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221 force_ifname[0] = '\0';
1222
1223 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1224 ifname);
1225 wpa_s->p2p_group_idx++;
1226
1227 wpa_s->pending_interface_type = type;
1228 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1229 wpa_s->pending_interface_addr, NULL) < 0) {
1230 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1231 "interface");
1232 return -1;
1233 }
1234
1235 if (force_ifname[0]) {
1236 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1237 force_ifname);
1238 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1239 sizeof(wpa_s->pending_interface_name));
1240 } else
1241 os_strlcpy(wpa_s->pending_interface_name, ifname,
1242 sizeof(wpa_s->pending_interface_name));
1243 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1244 MACSTR, wpa_s->pending_interface_name,
1245 MAC2STR(wpa_s->pending_interface_addr));
1246
1247 return 0;
1248}
1249
1250
1251static void wpas_p2p_remove_pending_group_interface(
1252 struct wpa_supplicant *wpa_s)
1253{
1254 if (!wpa_s->pending_interface_name[0] ||
1255 is_zero_ether_addr(wpa_s->pending_interface_addr))
1256 return; /* No pending virtual interface */
1257
1258 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1259 wpa_s->pending_interface_name);
1260 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1261 wpa_s->pending_interface_name);
1262 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1263 wpa_s->pending_interface_name[0] = '\0';
1264}
1265
1266
1267static struct wpa_supplicant *
1268wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1269{
1270 struct wpa_interface iface;
1271 struct wpa_supplicant *group_wpa_s;
1272
1273 if (!wpa_s->pending_interface_name[0]) {
1274 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1275 if (!wpas_p2p_create_iface(wpa_s))
1276 return NULL;
1277 /*
1278 * Something has forced us to remove the pending interface; try
1279 * to create a new one and hope for the best that we will get
1280 * the same local address.
1281 */
1282 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1283 WPA_IF_P2P_CLIENT) < 0)
1284 return NULL;
1285 }
1286
1287 os_memset(&iface, 0, sizeof(iface));
1288 iface.ifname = wpa_s->pending_interface_name;
1289 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001290 if (wpa_s->conf->ctrl_interface == NULL &&
1291 wpa_s->parent != wpa_s &&
1292 wpa_s->p2p_mgmt &&
1293 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1294 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1295 else
1296 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 iface.driver_param = wpa_s->conf->driver_param;
1298 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1299 if (group_wpa_s == NULL) {
1300 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1301 "wpa_supplicant interface");
1302 return NULL;
1303 }
1304 wpa_s->pending_interface_name[0] = '\0';
1305 group_wpa_s->parent = wpa_s;
1306 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1307 P2P_GROUP_INTERFACE_CLIENT;
1308 wpa_s->global->p2p_group_formation = group_wpa_s;
1309
1310 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1311
1312 return group_wpa_s;
1313}
1314
1315
1316static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1317 void *timeout_ctx)
1318{
1319 struct wpa_supplicant *wpa_s = eloop_ctx;
1320 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001321 wpas_p2p_group_formation_failed(wpa_s);
1322}
1323
1324
1325void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1326{
1327 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1328 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 if (wpa_s->global->p2p)
1330 p2p_group_formation_failed(wpa_s->global->p2p);
1331 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1332 wpa_drv_p2p_group_formation_failed(wpa_s);
1333 wpas_group_formation_completed(wpa_s, 0);
1334}
1335
1336
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001337void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1338{
1339 if (wpa_s->global->p2p_group_formation != wpa_s)
1340 return;
1341 /* Speed up group formation timeout since this cannot succeed */
1342 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1343 wpa_s->parent, NULL);
1344 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1345 wpa_s->parent, NULL);
1346}
1347
1348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1350{
1351 struct wpa_supplicant *wpa_s = ctx;
1352
1353 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1354 wpa_drv_cancel_remain_on_channel(wpa_s);
1355 wpa_s->off_channel_freq = 0;
1356 wpa_s->roc_waiting_drv_freq = 0;
1357 }
1358
1359 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001360 wpa_msg_global(wpa_s, MSG_INFO,
1361 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1362 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001363 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364 wpas_p2p_remove_pending_group_interface(wpa_s);
1365 return;
1366 }
1367
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001368 if (wpa_s->p2p_go_ht40)
1369 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001370 if (wpa_s->p2p_go_vht)
1371 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001372
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001373 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1374 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1375 " wps_method=%s",
1376 res->role_go ? "GO" : "client", res->freq, res->ht40,
1377 MAC2STR(res->peer_device_addr),
1378 MAC2STR(res->peer_interface_addr),
1379 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001380 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001381
Dmitry Shmidt04949592012-07-19 12:16:46 -07001382 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1383 struct wpa_ssid *ssid;
1384 ssid = wpa_config_get_network(wpa_s->conf,
1385 wpa_s->p2p_persistent_id);
1386 if (ssid && ssid->disabled == 2 &&
1387 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1388 size_t len = os_strlen(ssid->passphrase);
1389 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1390 "on requested persistent group");
1391 os_memcpy(res->passphrase, ssid->passphrase, len);
1392 res->passphrase[len] = '\0';
1393 }
1394 }
1395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 if (wpa_s->create_p2p_iface) {
1397 struct wpa_supplicant *group_wpa_s =
1398 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1399 if (group_wpa_s == NULL) {
1400 wpas_p2p_remove_pending_group_interface(wpa_s);
1401 return;
1402 }
1403 if (group_wpa_s != wpa_s) {
1404 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1405 sizeof(group_wpa_s->p2p_pin));
1406 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1407 }
1408 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1409 wpa_s->pending_interface_name[0] = '\0';
1410 group_wpa_s->p2p_in_provisioning = 1;
1411
1412 if (res->role_go)
1413 wpas_start_wps_go(group_wpa_s, res, 1);
1414 else
1415 wpas_start_wps_enrollee(group_wpa_s, res);
1416 } else {
1417 wpa_s->p2p_in_provisioning = 1;
1418 wpa_s->global->p2p_group_formation = wpa_s;
1419
1420 if (res->role_go)
1421 wpas_start_wps_go(wpa_s, res, 1);
1422 else
1423 wpas_start_wps_enrollee(ctx, res);
1424 }
1425
1426 wpa_s->p2p_long_listen = 0;
1427 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1428
1429 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1430 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1431 (res->peer_config_timeout % 100) * 10000,
1432 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1433}
1434
1435
1436void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1437{
1438 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001439 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1440 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441
1442 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1443}
1444
1445
1446void wpas_dev_found(void *ctx, const u8 *addr,
1447 const struct p2p_peer_info *info,
1448 int new_device)
1449{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001450#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 struct wpa_supplicant *wpa_s = ctx;
1452 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001453 char *wfd_dev_info_hex = NULL;
1454
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001455#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001456 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1457 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001458#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001459
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001460 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1461 " p2p_dev_addr=" MACSTR
1462 " pri_dev_type=%s name='%s' config_methods=0x%x "
1463 "dev_capab=0x%x group_capab=0x%x%s%s",
1464 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1465 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1466 sizeof(devtype)),
1467 info->device_name, info->config_methods,
1468 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001469 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1470 wfd_dev_info_hex ? wfd_dev_info_hex : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001471#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001473 os_free(wfd_dev_info_hex);
1474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1476}
1477
1478
1479static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1480{
1481 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001482
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001483 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1484 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1487}
1488
1489
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001490static void wpas_find_stopped(void *ctx)
1491{
1492 struct wpa_supplicant *wpa_s = ctx;
1493 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1494}
1495
1496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497static int wpas_start_listen(void *ctx, unsigned int freq,
1498 unsigned int duration,
1499 const struct wpabuf *probe_resp_ie)
1500{
1501 struct wpa_supplicant *wpa_s = ctx;
1502
1503 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1504
1505 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1506 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1507 "report received Probe Request frames");
1508 return -1;
1509 }
1510
1511 wpa_s->pending_listen_freq = freq;
1512 wpa_s->pending_listen_duration = duration;
1513
1514 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1515 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1516 "to remain on channel (%u MHz) for Listen "
1517 "state", freq);
1518 wpa_s->pending_listen_freq = 0;
1519 return -1;
1520 }
1521 wpa_s->off_channel_freq = 0;
1522 wpa_s->roc_waiting_drv_freq = freq;
1523
1524 return 0;
1525}
1526
1527
1528static void wpas_stop_listen(void *ctx)
1529{
1530 struct wpa_supplicant *wpa_s = ctx;
1531 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1532 wpa_drv_cancel_remain_on_channel(wpa_s);
1533 wpa_s->off_channel_freq = 0;
1534 wpa_s->roc_waiting_drv_freq = 0;
1535 }
1536 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1537 wpa_drv_probe_req_report(wpa_s, 0);
1538}
1539
1540
1541static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1542{
1543 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001544 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001545}
1546
1547
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001548/*
1549 * DNS Header section is used only to calculate compression pointers, so the
1550 * contents of this data does not matter, but the length needs to be reserved
1551 * in the virtual packet.
1552 */
1553#define DNS_HEADER_LEN 12
1554
1555/*
1556 * 27-octet in-memory packet from P2P specification containing two implied
1557 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1558 */
1559#define P2P_SD_IN_MEMORY_LEN 27
1560
1561static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1562 u8 **spos, const u8 *end)
1563{
1564 while (*spos < end) {
1565 u8 val = ((*spos)[0] & 0xc0) >> 6;
1566 int len;
1567
1568 if (val == 1 || val == 2) {
1569 /* These are reserved values in RFC 1035 */
1570 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1571 "sequence starting with 0x%x", val);
1572 return -1;
1573 }
1574
1575 if (val == 3) {
1576 u16 offset;
1577 u8 *spos_tmp;
1578
1579 /* Offset */
1580 if (*spos + 2 > end) {
1581 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1582 "DNS offset field");
1583 return -1;
1584 }
1585
1586 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1587 if (offset >= *spos - start) {
1588 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1589 "pointer offset %u", offset);
1590 return -1;
1591 }
1592
1593 (*spos) += 2;
1594 spos_tmp = start + offset;
1595 return p2p_sd_dns_uncompress_label(upos, uend, start,
1596 &spos_tmp,
1597 *spos - 2);
1598 }
1599
1600 /* Label */
1601 len = (*spos)[0] & 0x3f;
1602 if (len == 0)
1603 return 0;
1604
1605 (*spos)++;
1606 if (*spos + len > end) {
1607 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1608 "sequence - no room for label with length "
1609 "%u", len);
1610 return -1;
1611 }
1612
1613 if (*upos + len + 2 > uend)
1614 return -2;
1615
1616 os_memcpy(*upos, *spos, len);
1617 *spos += len;
1618 *upos += len;
1619 (*upos)[0] = '.';
1620 (*upos)++;
1621 (*upos)[0] = '\0';
1622 }
1623
1624 return 0;
1625}
1626
1627
1628/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1629 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1630 * not large enough */
1631static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1632 size_t msg_len, size_t offset)
1633{
1634 /* 27-octet in-memory packet from P2P specification */
1635 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1636 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1637 u8 *tmp, *end, *spos;
1638 char *upos, *uend;
1639 int ret = 0;
1640
1641 if (buf_len < 2)
1642 return -1;
1643 if (offset > msg_len)
1644 return -1;
1645
1646 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1647 if (tmp == NULL)
1648 return -1;
1649 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1650 end = spos + msg_len;
1651 spos += offset;
1652
1653 os_memset(tmp, 0, DNS_HEADER_LEN);
1654 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1655 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1656
1657 upos = buf;
1658 uend = buf + buf_len;
1659
1660 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1661 if (ret) {
1662 os_free(tmp);
1663 return ret;
1664 }
1665
1666 if (upos == buf) {
1667 upos[0] = '.';
1668 upos[1] = '\0';
1669 } else if (upos[-1] == '.')
1670 upos[-1] = '\0';
1671
1672 os_free(tmp);
1673 return 0;
1674}
1675
1676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677static struct p2p_srv_bonjour *
1678wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1679 const struct wpabuf *query)
1680{
1681 struct p2p_srv_bonjour *bsrv;
1682 size_t len;
1683
1684 len = wpabuf_len(query);
1685 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1686 struct p2p_srv_bonjour, list) {
1687 if (len == wpabuf_len(bsrv->query) &&
1688 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1689 len) == 0)
1690 return bsrv;
1691 }
1692 return NULL;
1693}
1694
1695
1696static struct p2p_srv_upnp *
1697wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1698 const char *service)
1699{
1700 struct p2p_srv_upnp *usrv;
1701
1702 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1703 struct p2p_srv_upnp, list) {
1704 if (version == usrv->version &&
1705 os_strcmp(service, usrv->service) == 0)
1706 return usrv;
1707 }
1708 return NULL;
1709}
1710
1711
1712static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1713 u8 srv_trans_id)
1714{
1715 u8 *len_pos;
1716
1717 if (wpabuf_tailroom(resp) < 5)
1718 return;
1719
1720 /* Length (to be filled) */
1721 len_pos = wpabuf_put(resp, 2);
1722 wpabuf_put_u8(resp, srv_proto);
1723 wpabuf_put_u8(resp, srv_trans_id);
1724 /* Status Code */
1725 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1726 /* Response Data: empty */
1727 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1728}
1729
1730
1731static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1732 struct wpabuf *resp, u8 srv_trans_id)
1733{
1734 struct p2p_srv_bonjour *bsrv;
1735 u8 *len_pos;
1736
1737 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1738
1739 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1740 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1741 return;
1742 }
1743
1744 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1745 struct p2p_srv_bonjour, list) {
1746 if (wpabuf_tailroom(resp) <
1747 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1748 return;
1749 /* Length (to be filled) */
1750 len_pos = wpabuf_put(resp, 2);
1751 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1752 wpabuf_put_u8(resp, srv_trans_id);
1753 /* Status Code */
1754 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1755 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1756 wpabuf_head(bsrv->resp),
1757 wpabuf_len(bsrv->resp));
1758 /* Response Data */
1759 wpabuf_put_buf(resp, bsrv->query); /* Key */
1760 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1761 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1762 2);
1763 }
1764}
1765
1766
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001767static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1768 size_t query_len)
1769{
1770 char str_rx[256], str_srv[256];
1771
1772 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1773 return 0; /* Too short to include DNS Type and Version */
1774 if (os_memcmp(query + query_len - 3,
1775 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1776 3) != 0)
1777 return 0; /* Mismatch in DNS Type or Version */
1778 if (query_len == wpabuf_len(bsrv->query) &&
1779 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1780 return 1; /* Binary match */
1781
1782 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1783 0))
1784 return 0; /* Failed to uncompress query */
1785 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1786 wpabuf_head(bsrv->query),
1787 wpabuf_len(bsrv->query) - 3, 0))
1788 return 0; /* Failed to uncompress service */
1789
1790 return os_strcmp(str_rx, str_srv) == 0;
1791}
1792
1793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1795 struct wpabuf *resp, u8 srv_trans_id,
1796 const u8 *query, size_t query_len)
1797{
1798 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001800 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801
1802 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1803 query, query_len);
1804 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1805 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1806 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1807 srv_trans_id);
1808 return;
1809 }
1810
1811 if (query_len == 0) {
1812 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1813 return;
1814 }
1815
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001816 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1817 struct p2p_srv_bonjour, list) {
1818 if (!match_bonjour_query(bsrv, query, query_len))
1819 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001821 if (wpabuf_tailroom(resp) <
1822 5 + query_len + wpabuf_len(bsrv->resp))
1823 return;
1824
1825 matches++;
1826
1827 /* Length (to be filled) */
1828 len_pos = wpabuf_put(resp, 2);
1829 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1830 wpabuf_put_u8(resp, srv_trans_id);
1831
1832 /* Status Code */
1833 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1834 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1835 wpabuf_head(bsrv->resp),
1836 wpabuf_len(bsrv->resp));
1837
1838 /* Response Data */
1839 wpabuf_put_data(resp, query, query_len); /* Key */
1840 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1841
1842 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1843 }
1844
1845 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1847 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001848 if (wpabuf_tailroom(resp) < 5)
1849 return;
1850
1851 /* Length (to be filled) */
1852 len_pos = wpabuf_put(resp, 2);
1853 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1854 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855
1856 /* Status Code */
1857 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1858 /* Response Data: empty */
1859 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1860 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001861 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862}
1863
1864
1865static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1866 struct wpabuf *resp, u8 srv_trans_id)
1867{
1868 struct p2p_srv_upnp *usrv;
1869 u8 *len_pos;
1870
1871 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1872
1873 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1874 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1875 return;
1876 }
1877
1878 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1879 struct p2p_srv_upnp, list) {
1880 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1881 return;
1882
1883 /* Length (to be filled) */
1884 len_pos = wpabuf_put(resp, 2);
1885 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1886 wpabuf_put_u8(resp, srv_trans_id);
1887
1888 /* Status Code */
1889 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1890 /* Response Data */
1891 wpabuf_put_u8(resp, usrv->version);
1892 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1893 usrv->service);
1894 wpabuf_put_str(resp, usrv->service);
1895 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1896 2);
1897 }
1898}
1899
1900
1901static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1902 struct wpabuf *resp, u8 srv_trans_id,
1903 const u8 *query, size_t query_len)
1904{
1905 struct p2p_srv_upnp *usrv;
1906 u8 *len_pos;
1907 u8 version;
1908 char *str;
1909 int count = 0;
1910
1911 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1912 query, query_len);
1913
1914 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1915 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1916 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1917 srv_trans_id);
1918 return;
1919 }
1920
1921 if (query_len == 0) {
1922 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1923 return;
1924 }
1925
1926 if (wpabuf_tailroom(resp) < 5)
1927 return;
1928
1929 /* Length (to be filled) */
1930 len_pos = wpabuf_put(resp, 2);
1931 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1932 wpabuf_put_u8(resp, srv_trans_id);
1933
1934 version = query[0];
1935 str = os_malloc(query_len);
1936 if (str == NULL)
1937 return;
1938 os_memcpy(str, query + 1, query_len - 1);
1939 str[query_len - 1] = '\0';
1940
1941 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1942 struct p2p_srv_upnp, list) {
1943 if (version != usrv->version)
1944 continue;
1945
1946 if (os_strcmp(str, "ssdp:all") != 0 &&
1947 os_strstr(usrv->service, str) == NULL)
1948 continue;
1949
1950 if (wpabuf_tailroom(resp) < 2)
1951 break;
1952 if (count == 0) {
1953 /* Status Code */
1954 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1955 /* Response Data */
1956 wpabuf_put_u8(resp, version);
1957 } else
1958 wpabuf_put_u8(resp, ',');
1959
1960 count++;
1961
1962 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1963 usrv->service);
1964 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1965 break;
1966 wpabuf_put_str(resp, usrv->service);
1967 }
1968 os_free(str);
1969
1970 if (count == 0) {
1971 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1972 "available");
1973 /* Status Code */
1974 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1975 /* Response Data: empty */
1976 }
1977
1978 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1979}
1980
1981
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001982#ifdef CONFIG_WIFI_DISPLAY
1983static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1984 struct wpabuf *resp, u8 srv_trans_id,
1985 const u8 *query, size_t query_len)
1986{
1987 const u8 *pos;
1988 u8 role;
1989 u8 *len_pos;
1990
1991 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1992
1993 if (!wpa_s->global->wifi_display) {
1994 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1995 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1996 srv_trans_id);
1997 return;
1998 }
1999
2000 if (query_len < 1) {
2001 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2002 "Role");
2003 return;
2004 }
2005
2006 if (wpabuf_tailroom(resp) < 5)
2007 return;
2008
2009 pos = query;
2010 role = *pos++;
2011 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2012
2013 /* TODO: role specific handling */
2014
2015 /* Length (to be filled) */
2016 len_pos = wpabuf_put(resp, 2);
2017 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2018 wpabuf_put_u8(resp, srv_trans_id);
2019 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2020
2021 while (pos < query + query_len) {
2022 if (*pos < MAX_WFD_SUBELEMS &&
2023 wpa_s->global->wfd_subelem[*pos] &&
2024 wpabuf_tailroom(resp) >=
2025 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2026 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2027 "subelement %u", *pos);
2028 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2029 }
2030 pos++;
2031 }
2032
2033 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2034}
2035#endif /* CONFIG_WIFI_DISPLAY */
2036
2037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2039 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2040{
2041 struct wpa_supplicant *wpa_s = ctx;
2042 const u8 *pos = tlvs;
2043 const u8 *end = tlvs + tlvs_len;
2044 const u8 *tlv_end;
2045 u16 slen;
2046 struct wpabuf *resp;
2047 u8 srv_proto, srv_trans_id;
2048 size_t buf_len;
2049 char *buf;
2050
2051 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2052 tlvs, tlvs_len);
2053 buf_len = 2 * tlvs_len + 1;
2054 buf = os_malloc(buf_len);
2055 if (buf) {
2056 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2057 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2058 MACSTR " %u %u %s",
2059 freq, MAC2STR(sa), dialog_token, update_indic,
2060 buf);
2061 os_free(buf);
2062 }
2063
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002064 if (wpa_s->p2p_sd_over_ctrl_iface) {
2065 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2066 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002068 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069
2070 resp = wpabuf_alloc(10000);
2071 if (resp == NULL)
2072 return;
2073
2074 while (pos + 1 < end) {
2075 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2076 slen = WPA_GET_LE16(pos);
2077 pos += 2;
2078 if (pos + slen > end || slen < 2) {
2079 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2080 "length");
2081 wpabuf_free(resp);
2082 return;
2083 }
2084 tlv_end = pos + slen;
2085
2086 srv_proto = *pos++;
2087 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2088 srv_proto);
2089 srv_trans_id = *pos++;
2090 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2091 srv_trans_id);
2092
2093 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2094 pos, tlv_end - pos);
2095
2096
2097 if (wpa_s->force_long_sd) {
2098 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2099 "response");
2100 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2101 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2102 goto done;
2103 }
2104
2105 switch (srv_proto) {
2106 case P2P_SERV_ALL_SERVICES:
2107 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2108 "for all services");
2109 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2110 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2111 wpa_printf(MSG_DEBUG, "P2P: No service "
2112 "discovery protocols available");
2113 wpas_sd_add_proto_not_avail(
2114 resp, P2P_SERV_ALL_SERVICES,
2115 srv_trans_id);
2116 break;
2117 }
2118 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2119 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2120 break;
2121 case P2P_SERV_BONJOUR:
2122 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2123 pos, tlv_end - pos);
2124 break;
2125 case P2P_SERV_UPNP:
2126 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2127 pos, tlv_end - pos);
2128 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002129#ifdef CONFIG_WIFI_DISPLAY
2130 case P2P_SERV_WIFI_DISPLAY:
2131 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2132 pos, tlv_end - pos);
2133 break;
2134#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 default:
2136 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2137 "protocol %u", srv_proto);
2138 wpas_sd_add_proto_not_avail(resp, srv_proto,
2139 srv_trans_id);
2140 break;
2141 }
2142
2143 pos = tlv_end;
2144 }
2145
2146done:
2147 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2148 update_indic, tlvs, tlvs_len);
2149
2150 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2151
2152 wpabuf_free(resp);
2153}
2154
2155
2156void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2157 const u8 *tlvs, size_t tlvs_len)
2158{
2159 struct wpa_supplicant *wpa_s = ctx;
2160 const u8 *pos = tlvs;
2161 const u8 *end = tlvs + tlvs_len;
2162 const u8 *tlv_end;
2163 u16 slen;
2164 size_t buf_len;
2165 char *buf;
2166
2167 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2168 tlvs, tlvs_len);
2169 if (tlvs_len > 1500) {
2170 /* TODO: better way for handling this */
2171 wpa_msg_ctrl(wpa_s, MSG_INFO,
2172 P2P_EVENT_SERV_DISC_RESP MACSTR
2173 " %u <long response: %u bytes>",
2174 MAC2STR(sa), update_indic,
2175 (unsigned int) tlvs_len);
2176 } else {
2177 buf_len = 2 * tlvs_len + 1;
2178 buf = os_malloc(buf_len);
2179 if (buf) {
2180 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2181 wpa_msg_ctrl(wpa_s, MSG_INFO,
2182 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2183 MAC2STR(sa), update_indic, buf);
2184 os_free(buf);
2185 }
2186 }
2187
2188 while (pos < end) {
2189 u8 srv_proto, srv_trans_id, status;
2190
2191 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2192 slen = WPA_GET_LE16(pos);
2193 pos += 2;
2194 if (pos + slen > end || slen < 3) {
2195 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2196 "length");
2197 return;
2198 }
2199 tlv_end = pos + slen;
2200
2201 srv_proto = *pos++;
2202 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2203 srv_proto);
2204 srv_trans_id = *pos++;
2205 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2206 srv_trans_id);
2207 status = *pos++;
2208 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2209 status);
2210
2211 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2212 pos, tlv_end - pos);
2213
2214 pos = tlv_end;
2215 }
2216
2217 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2218}
2219
2220
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002221u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2222 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223{
2224 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002225 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002226 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002227 return 0;
2228 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229}
2230
2231
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002232u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2233 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002234{
2235 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002236 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237
2238 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2239 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002240 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2242 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2243 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2244 wpabuf_put_u8(tlvs, version);
2245 wpabuf_put_str(tlvs, query);
2246 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2247 wpabuf_free(tlvs);
2248 return ret;
2249}
2250
2251
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002252#ifdef CONFIG_WIFI_DISPLAY
2253
2254static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2255 const struct wpabuf *tlvs)
2256{
2257 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2258 return 0;
2259 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2260 return 0;
2261 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2262}
2263
2264
2265#define MAX_WFD_SD_SUBELEMS 20
2266
2267static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2268 const char *subelems)
2269{
2270 u8 *len;
2271 const char *pos;
2272 int val;
2273 int count = 0;
2274
2275 len = wpabuf_put(tlvs, 2);
2276 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2277 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2278
2279 wpabuf_put_u8(tlvs, role);
2280
2281 pos = subelems;
2282 while (*pos) {
2283 val = atoi(pos);
2284 if (val >= 0 && val < 256) {
2285 wpabuf_put_u8(tlvs, val);
2286 count++;
2287 if (count == MAX_WFD_SD_SUBELEMS)
2288 break;
2289 }
2290 pos = os_strchr(pos + 1, ',');
2291 if (pos == NULL)
2292 break;
2293 pos++;
2294 }
2295
2296 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2297}
2298
2299
2300u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2301 const u8 *dst, const char *role)
2302{
2303 struct wpabuf *tlvs;
2304 u64 ret;
2305 const char *subelems;
2306 u8 id = 1;
2307
2308 subelems = os_strchr(role, ' ');
2309 if (subelems == NULL)
2310 return 0;
2311 subelems++;
2312
2313 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2314 if (tlvs == NULL)
2315 return 0;
2316
2317 if (os_strstr(role, "[source]"))
2318 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2319 if (os_strstr(role, "[pri-sink]"))
2320 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2321 if (os_strstr(role, "[sec-sink]"))
2322 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2323 if (os_strstr(role, "[source+sink]"))
2324 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2325
2326 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2327 wpabuf_free(tlvs);
2328 return ret;
2329}
2330
2331#endif /* CONFIG_WIFI_DISPLAY */
2332
2333
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002334int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335{
2336 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002337 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2339 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002340 return p2p_sd_cancel_request(wpa_s->global->p2p,
2341 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342}
2343
2344
2345void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2346 const u8 *dst, u8 dialog_token,
2347 const struct wpabuf *resp_tlvs)
2348{
2349 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2350 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2351 resp_tlvs);
2352 return;
2353 }
2354 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2355 return;
2356 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2357 resp_tlvs);
2358}
2359
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2362{
2363 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2364 wpa_drv_p2p_service_update(wpa_s);
2365 return;
2366 }
2367 if (wpa_s->global->p2p)
2368 p2p_sd_service_update(wpa_s->global->p2p);
2369}
2370
2371
2372static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2373{
2374 dl_list_del(&bsrv->list);
2375 wpabuf_free(bsrv->query);
2376 wpabuf_free(bsrv->resp);
2377 os_free(bsrv);
2378}
2379
2380
2381static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2382{
2383 dl_list_del(&usrv->list);
2384 os_free(usrv->service);
2385 os_free(usrv);
2386}
2387
2388
2389void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2390{
2391 struct p2p_srv_bonjour *bsrv, *bn;
2392 struct p2p_srv_upnp *usrv, *un;
2393
2394 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2395 struct p2p_srv_bonjour, list)
2396 wpas_p2p_srv_bonjour_free(bsrv);
2397
2398 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2399 struct p2p_srv_upnp, list)
2400 wpas_p2p_srv_upnp_free(usrv);
2401
2402 wpas_p2p_sd_service_update(wpa_s);
2403}
2404
2405
2406int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2407 struct wpabuf *query, struct wpabuf *resp)
2408{
2409 struct p2p_srv_bonjour *bsrv;
2410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411 bsrv = os_zalloc(sizeof(*bsrv));
2412 if (bsrv == NULL)
2413 return -1;
2414 bsrv->query = query;
2415 bsrv->resp = resp;
2416 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2417
2418 wpas_p2p_sd_service_update(wpa_s);
2419 return 0;
2420}
2421
2422
2423int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2424 const struct wpabuf *query)
2425{
2426 struct p2p_srv_bonjour *bsrv;
2427
2428 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2429 if (bsrv == NULL)
2430 return -1;
2431 wpas_p2p_srv_bonjour_free(bsrv);
2432 wpas_p2p_sd_service_update(wpa_s);
2433 return 0;
2434}
2435
2436
2437int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2438 const char *service)
2439{
2440 struct p2p_srv_upnp *usrv;
2441
2442 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2443 return 0; /* Already listed */
2444 usrv = os_zalloc(sizeof(*usrv));
2445 if (usrv == NULL)
2446 return -1;
2447 usrv->version = version;
2448 usrv->service = os_strdup(service);
2449 if (usrv->service == NULL) {
2450 os_free(usrv);
2451 return -1;
2452 }
2453 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2454
2455 wpas_p2p_sd_service_update(wpa_s);
2456 return 0;
2457}
2458
2459
2460int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2461 const char *service)
2462{
2463 struct p2p_srv_upnp *usrv;
2464
2465 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2466 if (usrv == NULL)
2467 return -1;
2468 wpas_p2p_srv_upnp_free(usrv);
2469 wpas_p2p_sd_service_update(wpa_s);
2470 return 0;
2471}
2472
2473
2474static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2475 const u8 *peer, const char *params,
2476 unsigned int generated_pin)
2477{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002478 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2479 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480}
2481
2482
2483static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2484 const u8 *peer, const char *params)
2485{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002486 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2487 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488}
2489
2490
2491void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2492 const u8 *dev_addr, const u8 *pri_dev_type,
2493 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002494 u8 dev_capab, u8 group_capab, const u8 *group_id,
2495 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496{
2497 struct wpa_supplicant *wpa_s = ctx;
2498 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002499 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 u8 empty_dev_type[8];
2501 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002502 struct wpa_supplicant *group = NULL;
2503
2504 if (group_id) {
2505 for (group = wpa_s->global->ifaces; group; group = group->next)
2506 {
2507 struct wpa_ssid *s = group->current_ssid;
2508 if (s != NULL &&
2509 s->mode == WPAS_MODE_P2P_GO &&
2510 group_id_len - ETH_ALEN == s->ssid_len &&
2511 os_memcmp(group_id + ETH_ALEN, s->ssid,
2512 s->ssid_len) == 0)
2513 break;
2514 }
2515 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516
2517 if (pri_dev_type == NULL) {
2518 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2519 pri_dev_type = empty_dev_type;
2520 }
2521 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2522 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002523 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 MAC2STR(dev_addr),
2525 wps_dev_type_bin2str(pri_dev_type, devtype,
2526 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002527 dev_name, supp_config_methods, dev_capab, group_capab,
2528 group ? " group=" : "",
2529 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 if (config_methods & WPS_CONFIG_DISPLAY) {
2533 generated_pin = wps_generate_pin();
2534 wpas_prov_disc_local_display(wpa_s, peer, params,
2535 generated_pin);
2536 } else if (config_methods & WPS_CONFIG_KEYPAD)
2537 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2538 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002539 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2540 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002541
2542 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2543 P2P_PROV_DISC_SUCCESS,
2544 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545}
2546
2547
2548void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2549{
2550 struct wpa_supplicant *wpa_s = ctx;
2551 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002552 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002554 if (wpa_s->pending_pd_before_join &&
2555 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2556 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2557 wpa_s->pending_pd_before_join = 0;
2558 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2559 "join-existing-group operation");
2560 wpas_p2p_join_start(wpa_s);
2561 return;
2562 }
2563
Dmitry Shmidt04949592012-07-19 12:16:46 -07002564 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2565 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2566 os_snprintf(params, sizeof(params), " peer_go=%d",
2567 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2568 else
2569 params[0] = '\0';
2570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002572 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 else if (config_methods & WPS_CONFIG_KEYPAD) {
2574 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002575 wpas_prov_disc_local_display(wpa_s, peer, params,
2576 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002578 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2579 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580
Jouni Malinen75ecf522011-06-27 15:19:46 -07002581 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2582 P2P_PROV_DISC_SUCCESS,
2583 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584}
2585
2586
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002587static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2588 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002589{
2590 struct wpa_supplicant *wpa_s = ctx;
2591
Dmitry Shmidt04949592012-07-19 12:16:46 -07002592 if (wpa_s->p2p_fallback_to_go_neg) {
2593 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2594 "failed - fall back to GO Negotiation");
2595 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2596 return;
2597 }
2598
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002599 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002600 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002601 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2602 "join-existing-group operation (no ACK for PD "
2603 "Req attempts)");
2604 wpas_p2p_join_start(wpa_s);
2605 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002606 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002607
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002608 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2609 " p2p_dev_addr=" MACSTR " status=%d",
2610 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002611
Jouni Malinen75ecf522011-06-27 15:19:46 -07002612 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2613 status, 0, 0);
2614}
2615
2616
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002617static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2618{
2619 if (channels == NULL)
2620 return 1; /* Assume no restrictions */
2621 return p2p_channels_includes_freq(channels, freq);
2622
2623}
2624
2625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2627 const u8 *go_dev_addr, const u8 *ssid,
2628 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002629 int *force_freq, int persistent_group,
2630 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631{
2632 struct wpa_supplicant *wpa_s = ctx;
2633 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634 int res;
2635 struct wpa_supplicant *grp;
2636
2637 if (!persistent_group) {
2638 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2639 " to join an active group", MAC2STR(sa));
2640 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2641 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2642 == 0 ||
2643 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2644 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2645 "authorized invitation");
2646 goto accept_inv;
2647 }
2648 /*
2649 * Do not accept the invitation automatically; notify user and
2650 * request approval.
2651 */
2652 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2653 }
2654
2655 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2656 if (grp) {
2657 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2658 "running persistent group");
2659 if (*go)
2660 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2661 goto accept_inv;
2662 }
2663
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002664 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2665 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2666 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2667 "invitation to re-invoke a persistent group");
2668 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2670
2671 for (s = wpa_s->conf->ssid; s; s = s->next) {
2672 if (s->disabled == 2 &&
2673 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2674 s->ssid_len == ssid_len &&
2675 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2676 break;
2677 }
2678
2679 if (!s) {
2680 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2681 " requested reinvocation of an unknown group",
2682 MAC2STR(sa));
2683 return P2P_SC_FAIL_UNKNOWN_GROUP;
2684 }
2685
2686 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2687 *go = 1;
2688 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2689 wpa_printf(MSG_DEBUG, "P2P: The only available "
2690 "interface is already in use - reject "
2691 "invitation");
2692 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2693 }
2694 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2695 } else if (s->mode == WPAS_MODE_P2P_GO) {
2696 *go = 1;
2697 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2698 {
2699 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2700 "interface address for the group");
2701 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2702 }
2703 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2704 ETH_ALEN);
2705 }
2706
2707accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002708 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2709
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002710 /* Get one of the frequencies currently in use */
2711 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002712 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002713 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002714
2715 if (wpa_s->num_multichan_concurrent < 2 ||
2716 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2717 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2718 *force_freq = res;
2719 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002720 }
2721
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002722 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2723 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002724 if (*go == 0) {
2725 /* We are the client */
2726 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2727 "running a GO but we are capable of MCC, "
2728 "figure out the best channel to use");
2729 *force_freq = 0;
2730 } else if (!freq_included(channels, *force_freq)) {
2731 /* We are the GO, and *force_freq is not in the
2732 * intersection */
2733 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2734 "in intersection but we are capable of MCC, "
2735 "figure out the best channel to use",
2736 *force_freq);
2737 *force_freq = 0;
2738 }
2739 }
2740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741 return P2P_SC_SUCCESS;
2742}
2743
2744
2745static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2746 const u8 *ssid, size_t ssid_len,
2747 const u8 *go_dev_addr, u8 status,
2748 int op_freq)
2749{
2750 struct wpa_supplicant *wpa_s = ctx;
2751 struct wpa_ssid *s;
2752
2753 for (s = wpa_s->conf->ssid; s; s = s->next) {
2754 if (s->disabled == 2 &&
2755 s->ssid_len == ssid_len &&
2756 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2757 break;
2758 }
2759
2760 if (status == P2P_SC_SUCCESS) {
2761 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2762 " was accepted; op_freq=%d MHz",
2763 MAC2STR(sa), op_freq);
2764 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002765 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766 wpas_p2p_group_add_persistent(
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002767 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002768 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002770 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002772 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 }
2774 return;
2775 }
2776
2777 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2778 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2779 " was rejected (status %u)", MAC2STR(sa), status);
2780 return;
2781 }
2782
2783 if (!s) {
2784 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002785 wpa_msg_global(wpa_s, MSG_INFO,
2786 P2P_EVENT_INVITATION_RECEIVED
2787 "sa=" MACSTR " go_dev_addr=" MACSTR
2788 " bssid=" MACSTR " unknown-network",
2789 MAC2STR(sa), MAC2STR(go_dev_addr),
2790 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002792 wpa_msg_global(wpa_s, MSG_INFO,
2793 P2P_EVENT_INVITATION_RECEIVED
2794 "sa=" MACSTR " go_dev_addr=" MACSTR
2795 " unknown-network",
2796 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002797 }
2798 return;
2799 }
2800
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002801 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002802 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2803 "sa=" MACSTR " persistent=%d freq=%d",
2804 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002805 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002806 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2807 "sa=" MACSTR " persistent=%d",
2808 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002809 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810}
2811
2812
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002813static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2814 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002815 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002816{
2817 size_t i;
2818
2819 if (ssid == NULL)
2820 return;
2821
2822 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2823 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2824 ETH_ALEN) == 0)
2825 break;
2826 }
2827 if (i >= ssid->num_p2p_clients) {
2828 if (ssid->mode != WPAS_MODE_P2P_GO &&
2829 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2830 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2831 "due to invitation result", ssid->id);
2832 wpas_notify_network_removed(wpa_s, ssid);
2833 wpa_config_remove_network(wpa_s->conf, ssid->id);
2834 return;
2835 }
2836 return; /* Peer not found in client list */
2837 }
2838
2839 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002840 "group %d client list%s",
2841 MAC2STR(peer), ssid->id,
2842 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002843 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2844 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2845 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2846 ssid->num_p2p_clients--;
2847#ifndef CONFIG_NO_CONFIG_WRITE
2848 if (wpa_s->parent->conf->update_config &&
2849 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2850 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2851#endif /* CONFIG_NO_CONFIG_WRITE */
2852}
2853
2854
2855static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2856 const u8 *peer)
2857{
2858 struct wpa_ssid *ssid;
2859
2860 wpa_s = wpa_s->global->p2p_invite_group;
2861 if (wpa_s == NULL)
2862 return; /* No known invitation group */
2863 ssid = wpa_s->current_ssid;
2864 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2865 !ssid->p2p_persistent_group)
2866 return; /* Not operating as a GO in persistent group */
2867 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2868 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002869 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002870}
2871
2872
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002873static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002874 const struct p2p_channels *channels,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002875 const u8 *peer, int neg_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002876{
2877 struct wpa_supplicant *wpa_s = ctx;
2878 struct wpa_ssid *ssid;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002879 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880
2881 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002882 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2883 "status=%d " MACSTR,
2884 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002886 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2887 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888 }
2889 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2890
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002891 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2892 status, MAC2STR(peer));
2893 if (wpa_s->pending_invite_ssid_id == -1) {
2894 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2895 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002899 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2900 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2901 "invitation exchange to indicate readiness for "
2902 "re-invocation");
2903 }
2904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002906 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2907 ssid = wpa_config_get_network(
2908 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002909 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 wpas_p2p_remove_pending_group_interface(wpa_s);
2912 return;
2913 }
2914
2915 ssid = wpa_config_get_network(wpa_s->conf,
2916 wpa_s->pending_invite_ssid_id);
2917 if (ssid == NULL) {
2918 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2919 "data matching with invitation");
2920 return;
2921 }
2922
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002923 /*
2924 * The peer could have missed our ctrl::ack frame for Invitation
2925 * Response and continue retransmitting the frame. To reduce the
2926 * likelihood of the peer not getting successful TX status for the
2927 * Invitation Response frame, wait a short time here before starting
2928 * the persistent group so that we will remain on the current channel to
2929 * acknowledge any possible retransmission from the peer.
2930 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002931#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002932 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2933 "starting persistent group");
2934 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002935#else
2936 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2937 "starting persistent group");
2938 os_sleep(0, 100000);
2939#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002940
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002941 freq = wpa_s->p2p_persistent_go_freq;
2942 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2943 freq_included(channels, neg_freq)) {
2944 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
2945 neg_freq);
2946 freq = neg_freq;
2947 }
2948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002949 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002950 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002951 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002952 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2953 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002954 ssid->mode == WPAS_MODE_P2P_GO ?
2955 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2956 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002957}
2958
2959
Dmitry Shmidt04949592012-07-19 12:16:46 -07002960static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2961 unsigned int freq)
2962{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002963 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002964}
2965
2966
2967static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2968{
2969 reg->channel[reg->channels] = chan;
2970 reg->channels++;
2971}
2972
2973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002975 struct p2p_channels *chan,
2976 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977{
2978 int i, cla = 0;
2979
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002980 os_memset(cli_chan, 0, sizeof(*cli_chan));
2981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2983 "band");
2984
2985 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2986 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002987 chan->reg_class[cla].channels = 0;
2988 for (i = 0; i < 11; i++) {
2989 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2990 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2991 }
2992 if (chan->reg_class[cla].channels)
2993 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002994
2995 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2996 "band");
2997
2998 /* Operating class 115 - 5 GHz, channels 36-48 */
2999 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003000 chan->reg_class[cla].channels = 0;
3001 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3002 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3003 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3004 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3005 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3006 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3007 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3008 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3009 if (chan->reg_class[cla].channels)
3010 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011
3012 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3013 "band");
3014
3015 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3016 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003017 chan->reg_class[cla].channels = 0;
3018 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3019 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3020 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3021 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3022 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3023 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3024 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3025 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3026 if (chan->reg_class[cla].channels)
3027 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028
3029 chan->reg_classes = cla;
3030 return 0;
3031}
3032
3033
3034static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3035 u16 num_modes,
3036 enum hostapd_hw_mode mode)
3037{
3038 u16 i;
3039
3040 for (i = 0; i < num_modes; i++) {
3041 if (modes[i].mode == mode)
3042 return &modes[i];
3043 }
3044
3045 return NULL;
3046}
3047
3048
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003049enum chan_allowed {
3050 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3051};
3052
Dmitry Shmidt04949592012-07-19 12:16:46 -07003053static int has_channel(struct wpa_global *global,
3054 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055{
3056 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003057 unsigned int freq;
3058
3059 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3060 chan * 5;
3061 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003062 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063
3064 for (i = 0; i < mode->num_channels; i++) {
3065 if (mode->channels[i].chan == chan) {
3066 if (flags)
3067 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003068 if (mode->channels[i].flag &
3069 (HOSTAPD_CHAN_DISABLED |
3070 HOSTAPD_CHAN_RADAR))
3071 return NOT_ALLOWED;
3072 if (mode->channels[i].flag &
3073 (HOSTAPD_CHAN_PASSIVE_SCAN |
3074 HOSTAPD_CHAN_NO_IBSS))
3075 return PASSIVE_ONLY;
3076 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003077 }
3078 }
3079
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003080 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081}
3082
3083
3084struct p2p_oper_class_map {
3085 enum hostapd_hw_mode mode;
3086 u8 op_class;
3087 u8 min_chan;
3088 u8 max_chan;
3089 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003090 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091};
3092
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003093static struct p2p_oper_class_map op_class[] = {
3094 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003095#if 0 /* Do not enable HT40 on 2 GHz for now */
3096 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3097 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3098#endif
3099 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3100 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3101 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3102 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3103 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3104 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003105
3106 /*
3107 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3108 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3109 * 80 MHz, but currently use the following definition for simplicity
3110 * (these center frequencies are not actual channels, which makes
3111 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3112 * removing invalid channels.
3113 */
3114 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003115 { -1, 0, 0, 0, 0, BW20 }
3116};
3117
3118
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003119static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3120 struct hostapd_hw_modes *mode,
3121 u8 channel)
3122{
3123 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3124 unsigned int i;
3125
3126 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3127 return 0;
3128
3129 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3130 /*
3131 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3132 * so the center channel is 6 channels away from the start/end.
3133 */
3134 if (channel >= center_channels[i] - 6 &&
3135 channel <= center_channels[i] + 6)
3136 return center_channels[i];
3137
3138 return 0;
3139}
3140
3141
3142static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3143 struct hostapd_hw_modes *mode,
3144 u8 channel, u8 bw)
3145{
3146 u8 center_chan;
3147 int i, flags;
3148 enum chan_allowed res, ret = ALLOWED;
3149
3150 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3151 if (!center_chan)
3152 return NOT_ALLOWED;
3153 if (center_chan >= 58 && center_chan <= 138)
3154 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3155
3156 /* check all the channels are available */
3157 for (i = 0; i < 4; i++) {
3158 int adj_chan = center_chan - 6 + i * 4;
3159
3160 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3161 if (res == NOT_ALLOWED)
3162 return NOT_ALLOWED;
3163 if (res == PASSIVE_ONLY)
3164 ret = PASSIVE_ONLY;
3165
3166 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3167 return NOT_ALLOWED;
3168 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3169 return NOT_ALLOWED;
3170 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3171 return NOT_ALLOWED;
3172 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3173 return NOT_ALLOWED;
3174 }
3175
3176 return ret;
3177}
3178
3179
3180static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3181 struct hostapd_hw_modes *mode,
3182 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003183{
3184 int flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003185 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003186
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003187 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3188 if (bw == BW40MINUS) {
3189 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3190 return NOT_ALLOWED;
3191 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3192 } else if (bw == BW40PLUS) {
3193 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3194 return NOT_ALLOWED;
3195 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3196 } else if (bw == BW80) {
3197 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3198 }
3199
3200 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3201 return NOT_ALLOWED;
3202 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3203 return PASSIVE_ONLY;
3204 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003205}
3206
3207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003208static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003209 struct p2p_channels *chan,
3210 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003212 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003213 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003215 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3217 "of all supported channels; assume dualband "
3218 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003219 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 }
3221
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003222 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223
3224 for (op = 0; op_class[op].op_class; op++) {
3225 struct p2p_oper_class_map *o = &op_class[op];
3226 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003227 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003229 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003230 if (mode == NULL)
3231 continue;
3232 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003233 enum chan_allowed res;
3234 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3235 if (res == ALLOWED) {
3236 if (reg == NULL) {
3237 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3238 o->op_class);
3239 reg = &chan->reg_class[cla];
3240 cla++;
3241 reg->reg_class = o->op_class;
3242 }
3243 reg->channel[reg->channels] = ch;
3244 reg->channels++;
3245 } else if (res == PASSIVE_ONLY &&
3246 wpa_s->conf->p2p_add_cli_chan) {
3247 if (cli_reg == NULL) {
3248 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3249 o->op_class);
3250 cli_reg = &cli_chan->reg_class[cli_cla];
3251 cli_cla++;
3252 cli_reg->reg_class = o->op_class;
3253 }
3254 cli_reg->channel[cli_reg->channels] = ch;
3255 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 }
3258 if (reg) {
3259 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3260 reg->channel, reg->channels);
3261 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003262 if (cli_reg) {
3263 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3264 cli_reg->channel, cli_reg->channels);
3265 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 }
3267
3268 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003269 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271 return 0;
3272}
3273
3274
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003275int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3276 struct hostapd_hw_modes *mode, u8 channel)
3277{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003278 int op;
3279 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003280
3281 for (op = 0; op_class[op].op_class; op++) {
3282 struct p2p_oper_class_map *o = &op_class[op];
3283 u8 ch;
3284
3285 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3286 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3287 o->bw == BW20 || ch != channel)
3288 continue;
3289 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003290 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003291 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003292 }
3293 }
3294 return 0;
3295}
3296
3297
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003298int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3299 struct hostapd_hw_modes *mode, u8 channel)
3300{
3301 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3302 return 0;
3303
3304 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3305}
3306
3307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003308static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3309 size_t buf_len)
3310{
3311 struct wpa_supplicant *wpa_s = ctx;
3312
3313 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3314 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3315 break;
3316 }
3317 if (wpa_s == NULL)
3318 return -1;
3319
3320 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3321}
3322
3323
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003324static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3325{
3326 struct wpa_supplicant *wpa_s = ctx;
3327
3328 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3329 struct wpa_ssid *ssid = wpa_s->current_ssid;
3330 if (ssid == NULL)
3331 continue;
3332 if (ssid->mode != WPAS_MODE_INFRA)
3333 continue;
3334 if (wpa_s->wpa_state != WPA_COMPLETED &&
3335 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3336 continue;
3337 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3338 return 1;
3339 }
3340
3341 return 0;
3342}
3343
3344
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003345static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3346{
3347 struct wpa_supplicant *wpa_s = ctx;
3348 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3349}
3350
3351
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003352int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3353{
3354 struct wpa_interface iface;
3355 struct wpa_supplicant *p2pdev_wpa_s;
3356 char ifname[100];
3357 char force_name[100];
3358 int ret;
3359
3360 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3361 wpa_s->ifname);
3362 force_name[0] = '\0';
3363 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3364 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3365 force_name, wpa_s->pending_interface_addr, NULL);
3366 if (ret < 0) {
3367 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3368 return ret;
3369 }
3370 os_strlcpy(wpa_s->pending_interface_name, ifname,
3371 sizeof(wpa_s->pending_interface_name));
3372
3373 os_memset(&iface, 0, sizeof(iface));
3374 iface.p2p_mgmt = 1;
3375 iface.ifname = wpa_s->pending_interface_name;
3376 iface.driver = wpa_s->driver->name;
3377 iface.driver_param = wpa_s->conf->driver_param;
3378 iface.confname = wpa_s->confname;
3379 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3380 if (!p2pdev_wpa_s) {
3381 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3382 return -1;
3383 }
3384 p2pdev_wpa_s->parent = wpa_s;
3385
3386 wpa_s->pending_interface_name[0] = '\0';
3387 return 0;
3388}
3389
3390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391/**
3392 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3393 * @global: Pointer to global data from wpa_supplicant_init()
3394 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3395 * Returns: 0 on success, -1 on failure
3396 */
3397int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3398{
3399 struct p2p_config p2p;
3400 unsigned int r;
3401 int i;
3402
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003403 if (wpa_s->conf->p2p_disabled)
3404 return 0;
3405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003406 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3407 return 0;
3408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003409 if (global->p2p)
3410 return 0;
3411
3412 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3413 struct p2p_params params;
3414
3415 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3416 os_memset(&params, 0, sizeof(params));
3417 params.dev_name = wpa_s->conf->device_name;
3418 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3419 WPS_DEV_TYPE_LEN);
3420 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3421 os_memcpy(params.sec_dev_type,
3422 wpa_s->conf->sec_device_type,
3423 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3424
3425 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3426 return -1;
3427
3428 return 0;
3429 }
3430
3431 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003432 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003433 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 p2p.p2p_scan = wpas_p2p_scan;
3435 p2p.send_action = wpas_send_action;
3436 p2p.send_action_done = wpas_send_action_done;
3437 p2p.go_neg_completed = wpas_go_neg_completed;
3438 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3439 p2p.dev_found = wpas_dev_found;
3440 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003441 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003442 p2p.start_listen = wpas_start_listen;
3443 p2p.stop_listen = wpas_stop_listen;
3444 p2p.send_probe_resp = wpas_send_probe_resp;
3445 p2p.sd_request = wpas_sd_request;
3446 p2p.sd_response = wpas_sd_response;
3447 p2p.prov_disc_req = wpas_prov_disc_req;
3448 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003449 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450 p2p.invitation_process = wpas_invitation_process;
3451 p2p.invitation_received = wpas_invitation_received;
3452 p2p.invitation_result = wpas_invitation_result;
3453 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003454 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455
3456 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003457 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003458 p2p.dev_name = wpa_s->conf->device_name;
3459 p2p.manufacturer = wpa_s->conf->manufacturer;
3460 p2p.model_name = wpa_s->conf->model_name;
3461 p2p.model_number = wpa_s->conf->model_number;
3462 p2p.serial_number = wpa_s->conf->serial_number;
3463 if (wpa_s->wps) {
3464 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3465 p2p.config_methods = wpa_s->wps->config_methods;
3466 }
3467
3468 if (wpa_s->conf->p2p_listen_reg_class &&
3469 wpa_s->conf->p2p_listen_channel) {
3470 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3471 p2p.channel = wpa_s->conf->p2p_listen_channel;
3472 } else {
3473 p2p.reg_class = 81;
3474 /*
3475 * Pick one of the social channels randomly as the listen
3476 * channel.
3477 */
3478 os_get_random((u8 *) &r, sizeof(r));
3479 p2p.channel = 1 + (r % 3) * 5;
3480 }
3481 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3482
3483 if (wpa_s->conf->p2p_oper_reg_class &&
3484 wpa_s->conf->p2p_oper_channel) {
3485 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3486 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3487 p2p.cfg_op_channel = 1;
3488 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3489 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3490
3491 } else {
3492 p2p.op_reg_class = 81;
3493 /*
3494 * Use random operation channel from (1, 6, 11) if no other
3495 * preference is indicated.
3496 */
3497 os_get_random((u8 *) &r, sizeof(r));
3498 p2p.op_channel = 1 + (r % 3) * 5;
3499 p2p.cfg_op_channel = 0;
3500 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3501 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3502 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003503
3504 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3505 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3506 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3507 }
3508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3510 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3511 p2p.country[2] = 0x04;
3512 } else
3513 os_memcpy(p2p.country, "XX\x04", 3);
3514
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003515 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003516 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3517 "channel list");
3518 return -1;
3519 }
3520
3521 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3522 WPS_DEV_TYPE_LEN);
3523
3524 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3525 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3526 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3527
3528 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3529 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3530
3531 p2p.max_peers = 100;
3532
3533 if (wpa_s->conf->p2p_ssid_postfix) {
3534 p2p.ssid_postfix_len =
3535 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3536 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3537 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3538 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3539 p2p.ssid_postfix_len);
3540 }
3541
3542 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3543
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003544 p2p.max_listen = wpa_s->max_remain_on_chan;
3545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546 global->p2p = p2p_init(&p2p);
3547 if (global->p2p == NULL)
3548 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003549 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550
3551 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3552 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3553 continue;
3554 p2p_add_wps_vendor_extension(
3555 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3556 }
3557
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003558 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003560 return 0;
3561}
3562
3563
3564/**
3565 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3566 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3567 *
3568 * This function deinitialize per-interface P2P data.
3569 */
3570void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3571{
3572 if (wpa_s->driver && wpa_s->drv_priv)
3573 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003574
3575 if (wpa_s->go_params) {
3576 /* Clear any stored provisioning info */
3577 p2p_clear_provisioning_info(
3578 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003579 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003580 }
3581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003582 os_free(wpa_s->go_params);
3583 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3585 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3586 wpa_s->p2p_long_listen = 0;
3587 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3588 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3589 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003590 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003591
3592 /* TODO: remove group interface from the driver if this wpa_s instance
3593 * is on top of a P2P group interface */
3594}
3595
3596
3597/**
3598 * wpas_p2p_deinit_global - Deinitialize global P2P module
3599 * @global: Pointer to global data from wpa_supplicant_init()
3600 *
3601 * This function deinitializes the global (per device) P2P module.
3602 */
3603void wpas_p2p_deinit_global(struct wpa_global *global)
3604{
3605 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003607 wpa_s = global->ifaces;
3608 if (wpa_s)
3609 wpas_p2p_service_flush(wpa_s);
3610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 if (global->p2p == NULL)
3612 return;
3613
3614 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003615 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3616 wpa_s = wpa_s->next;
3617 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618 tmp = global->ifaces;
3619 while (tmp &&
3620 (tmp == wpa_s ||
3621 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3622 tmp = tmp->next;
3623 }
3624 if (tmp == NULL)
3625 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003626 /* Disconnect from the P2P group and deinit the interface */
3627 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628 }
3629
3630 /*
3631 * Deinit GO data on any possibly remaining interface (if main
3632 * interface is used as GO).
3633 */
3634 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3635 if (wpa_s->ap_iface)
3636 wpas_p2p_group_deinit(wpa_s);
3637 }
3638
3639 p2p_deinit(global->p2p);
3640 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003641 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003642}
3643
3644
3645static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3646{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003647 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3648 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003649 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003650 if (wpa_s->drv_flags &
3651 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3652 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3653 return 1; /* P2P group requires a new interface in every case
3654 */
3655 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3656 return 0; /* driver does not support concurrent operations */
3657 if (wpa_s->global->ifaces->next)
3658 return 1; /* more that one interface already in use */
3659 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3660 return 1; /* this interface is already in use */
3661 return 0;
3662}
3663
3664
3665static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3666 const u8 *peer_addr,
3667 enum p2p_wps_method wps_method,
3668 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003669 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003670 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003672 if (persistent_group && wpa_s->conf->persistent_reconnect)
3673 persistent_group = 2;
3674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3676 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3677 go_intent, own_interface_addr,
3678 force_freq, persistent_group);
3679 }
3680
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003681 /*
3682 * Increase GO config timeout if HT40 is used since it takes some time
3683 * to scan channels for coex purposes before the BSS can be started.
3684 */
3685 p2p_set_config_timeout(wpa_s->global->p2p,
3686 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003688 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3689 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003690 persistent_group, ssid ? ssid->ssid : NULL,
3691 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003692 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693}
3694
3695
3696static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3697 const u8 *peer_addr,
3698 enum p2p_wps_method wps_method,
3699 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003700 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003701 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003703 if (persistent_group && wpa_s->conf->persistent_reconnect)
3704 persistent_group = 2;
3705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003706 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3707 return -1;
3708
3709 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3710 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003711 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003712 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003713}
3714
3715
3716static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3717{
3718 wpa_s->p2p_join_scan_count++;
3719 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3720 wpa_s->p2p_join_scan_count);
3721 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3722 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3723 " for join operationg - stop join attempt",
3724 MAC2STR(wpa_s->pending_join_iface_addr));
3725 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003726 if (wpa_s->p2p_auto_pd) {
3727 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003728 wpa_msg_global(wpa_s, MSG_INFO,
3729 P2P_EVENT_PROV_DISC_FAILURE
3730 " p2p_dev_addr=" MACSTR " status=N/A",
3731 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003732 return;
3733 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003734 wpa_msg_global(wpa_s->parent, MSG_INFO,
3735 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736 }
3737}
3738
3739
Dmitry Shmidt04949592012-07-19 12:16:46 -07003740static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3741{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003742 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003743
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003744 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3745 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003746 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003747 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003748
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003749 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3750 if (!freqs)
3751 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003752
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003753 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3754 wpa_s->num_multichan_concurrent);
3755 if (num < 0) {
3756 res = 1;
3757 goto exit_free;
3758 }
3759
3760 for (i = 0; i < num; i++) {
3761 if (freqs[i] == freq) {
3762 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3763 freq);
3764 res = 0;
3765 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003766 }
3767 }
3768
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003769 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003770
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003771exit_free:
3772 os_free(freqs);
3773 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003774}
3775
3776
3777static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3778 const u8 *peer_dev_addr)
3779{
3780 struct wpa_bss *bss;
3781 int updated;
3782
3783 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3784 if (bss == NULL)
3785 return -1;
3786 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3787 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3788 "last scan");
3789 return 0;
3790 }
3791
3792 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3793 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3794 "%ld.%06ld (%supdated in last scan)",
3795 bss->last_update.sec, bss->last_update.usec,
3796 updated ? "": "not ");
3797
3798 return updated;
3799}
3800
3801
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003802static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3803 struct wpa_scan_results *scan_res)
3804{
3805 struct wpa_bss *bss;
3806 int freq;
3807 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3810
3811 if (wpa_s->global->p2p_disabled)
3812 return;
3813
Dmitry Shmidt04949592012-07-19 12:16:46 -07003814 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3815 scan_res ? (int) scan_res->num : -1,
3816 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003817
3818 if (scan_res)
3819 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3820
Dmitry Shmidt04949592012-07-19 12:16:46 -07003821 if (wpa_s->p2p_auto_pd) {
3822 int join = wpas_p2p_peer_go(wpa_s,
3823 wpa_s->pending_join_dev_addr);
3824 if (join == 0 &&
3825 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3826 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003827 bss = wpa_bss_get_bssid_latest(
3828 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003829 if (bss) {
3830 freq = bss->freq;
3831 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3832 "the peer " MACSTR " at %d MHz",
3833 wpa_s->auto_pd_scan_retry,
3834 MAC2STR(wpa_s->
3835 pending_join_dev_addr),
3836 freq);
3837 wpas_p2p_join_scan_req(wpa_s, freq);
3838 return;
3839 }
3840 }
3841
3842 if (join < 0)
3843 join = 0;
3844
3845 wpa_s->p2p_auto_pd = 0;
3846 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3847 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3848 MAC2STR(wpa_s->pending_join_dev_addr), join);
3849 if (p2p_prov_disc_req(wpa_s->global->p2p,
3850 wpa_s->pending_join_dev_addr,
3851 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003852 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003853 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003854 wpa_msg_global(wpa_s, MSG_INFO,
3855 P2P_EVENT_PROV_DISC_FAILURE
3856 " p2p_dev_addr=" MACSTR " status=N/A",
3857 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003858 }
3859 return;
3860 }
3861
3862 if (wpa_s->p2p_auto_join) {
3863 int join = wpas_p2p_peer_go(wpa_s,
3864 wpa_s->pending_join_dev_addr);
3865 if (join < 0) {
3866 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3867 "running a GO -> use GO Negotiation");
3868 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3869 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3870 wpa_s->p2p_persistent_group, 0, 0, 0,
3871 wpa_s->p2p_go_intent,
3872 wpa_s->p2p_connect_freq,
3873 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003874 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003875 wpa_s->p2p_go_ht40,
3876 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003877 return;
3878 }
3879
3880 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3881 "try to join the group", join ? "" :
3882 " in older scan");
3883 if (!join)
3884 wpa_s->p2p_fallback_to_go_neg = 1;
3885 }
3886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003887 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3888 wpa_s->pending_join_iface_addr);
3889 if (freq < 0 &&
3890 p2p_get_interface_addr(wpa_s->global->p2p,
3891 wpa_s->pending_join_dev_addr,
3892 iface_addr) == 0 &&
3893 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3894 {
3895 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3896 "address for join from " MACSTR " to " MACSTR
3897 " based on newly discovered P2P peer entry",
3898 MAC2STR(wpa_s->pending_join_iface_addr),
3899 MAC2STR(iface_addr));
3900 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3901 ETH_ALEN);
3902
3903 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3904 wpa_s->pending_join_iface_addr);
3905 }
3906 if (freq >= 0) {
3907 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3908 "from P2P peer table: %d MHz", freq);
3909 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003910 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003911 if (bss) {
3912 freq = bss->freq;
3913 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07003914 "from BSS table: %d MHz (SSID %s)", freq,
3915 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003916 }
3917 if (freq > 0) {
3918 u16 method;
3919
Dmitry Shmidt04949592012-07-19 12:16:46 -07003920 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003921 wpa_msg_global(wpa_s->parent, MSG_INFO,
3922 P2P_EVENT_GROUP_FORMATION_FAILURE
3923 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003924 return;
3925 }
3926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003927 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3928 "prior to joining an existing group (GO " MACSTR
3929 " freq=%u MHz)",
3930 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3931 wpa_s->pending_pd_before_join = 1;
3932
3933 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003934 case WPS_PIN_DISPLAY:
3935 method = WPS_CONFIG_KEYPAD;
3936 break;
3937 case WPS_PIN_KEYPAD:
3938 method = WPS_CONFIG_DISPLAY;
3939 break;
3940 case WPS_PBC:
3941 method = WPS_CONFIG_PUSHBUTTON;
3942 break;
3943 default:
3944 method = 0;
3945 break;
3946 }
3947
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003948 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3949 wpa_s->pending_join_dev_addr) ==
3950 method)) {
3951 /*
3952 * We have already performed provision discovery for
3953 * joining the group. Proceed directly to join
3954 * operation without duplicated provision discovery. */
3955 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3956 "with " MACSTR " already done - proceed to "
3957 "join",
3958 MAC2STR(wpa_s->pending_join_dev_addr));
3959 wpa_s->pending_pd_before_join = 0;
3960 goto start;
3961 }
3962
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003964 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003965 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3967 "Discovery Request before joining an "
3968 "existing group");
3969 wpa_s->pending_pd_before_join = 0;
3970 goto start;
3971 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972 return;
3973 }
3974
3975 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3976 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3977 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3978 wpas_p2p_check_join_scan_limit(wpa_s);
3979 return;
3980
3981start:
3982 /* Start join operation immediately */
3983 wpas_p2p_join_start(wpa_s);
3984}
3985
3986
Dmitry Shmidt04949592012-07-19 12:16:46 -07003987static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003989 int ret;
3990 struct wpa_driver_scan_params params;
3991 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003992 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003993 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003994#ifdef ANDROID_P2P
3995 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003997 /* If freq is not provided, check the operating freq of the GO and do a
3998 * a directed scan to save time
3999 */
4000 if(!freq) {
4001 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4002 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
4003 }
4004#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004005 os_memset(&params, 0, sizeof(params));
4006
4007 /* P2P Wildcard SSID */
4008 params.num_ssids = 1;
4009 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4010 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4011
4012 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004013 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4014 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4015 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016 if (wps_ie == NULL) {
4017 wpas_p2p_scan_res_join(wpa_s, NULL);
4018 return;
4019 }
4020
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004021 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4022 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023 if (ies == NULL) {
4024 wpabuf_free(wps_ie);
4025 wpas_p2p_scan_res_join(wpa_s, NULL);
4026 return;
4027 }
4028 wpabuf_put_buf(ies, wps_ie);
4029 wpabuf_free(wps_ie);
4030
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004031 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004032
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004033 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034 params.extra_ies = wpabuf_head(ies);
4035 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004036 if (freq > 0) {
4037 freqs[0] = freq;
4038 params.freqs = freqs;
4039 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004040
4041 /*
4042 * Run a scan to update BSS table and start Provision Discovery once
4043 * the new scan results become available.
4044 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004045 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004046 if (!ret) {
4047 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004048 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004049 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004050
4051 wpabuf_free(ies);
4052
4053 if (ret) {
4054 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4055 "try again later");
4056 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4057 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4058 wpas_p2p_check_join_scan_limit(wpa_s);
4059 }
4060}
4061
4062
Dmitry Shmidt04949592012-07-19 12:16:46 -07004063static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4064{
4065 struct wpa_supplicant *wpa_s = eloop_ctx;
4066 wpas_p2p_join_scan_req(wpa_s, 0);
4067}
4068
4069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004070static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004071 const u8 *dev_addr, enum p2p_wps_method wps_method,
4072 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073{
4074 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004075 MACSTR " dev " MACSTR ")%s",
4076 MAC2STR(iface_addr), MAC2STR(dev_addr),
4077 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004078
Dmitry Shmidt04949592012-07-19 12:16:46 -07004079 wpa_s->p2p_auto_pd = 0;
4080 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4082 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4083 wpa_s->pending_join_wps_method = wps_method;
4084
4085 /* Make sure we are not running find during connection establishment */
4086 wpas_p2p_stop_find(wpa_s);
4087
4088 wpa_s->p2p_join_scan_count = 0;
4089 wpas_p2p_join_scan(wpa_s, NULL);
4090 return 0;
4091}
4092
4093
4094static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4095{
4096 struct wpa_supplicant *group;
4097 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004098 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004099
4100 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4101 if (group == NULL)
4102 return -1;
4103 if (group != wpa_s) {
4104 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4105 sizeof(group->p2p_pin));
4106 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004107 } else {
4108 /*
4109 * Need to mark the current interface for p2p_group_formation
4110 * when a separate group interface is not used. This is needed
4111 * to allow p2p_cancel stop a pending p2p_connect-join.
4112 * wpas_p2p_init_group_interface() addresses this for the case
4113 * where a separate group interface is used.
4114 */
4115 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116 }
4117
4118 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004119 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120
4121 os_memset(&res, 0, sizeof(res));
4122 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4123 ETH_ALEN);
4124 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004125 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004126 if (bss) {
4127 res.freq = bss->freq;
4128 res.ssid_len = bss->ssid_len;
4129 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07004130 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4131 "from BSS table: %d MHz (SSID %s)", bss->freq,
4132 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004133 }
4134
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004135 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4136 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4137 "starting client");
4138 wpa_drv_cancel_remain_on_channel(wpa_s);
4139 wpa_s->off_channel_freq = 0;
4140 wpa_s->roc_waiting_drv_freq = 0;
4141 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004142 wpas_start_wps_enrollee(group, &res);
4143
4144 /*
4145 * Allow a longer timeout for join-a-running-group than normal 15
4146 * second group formation timeout since the GO may not have authorized
4147 * our connection yet.
4148 */
4149 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4150 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4151 wpa_s, NULL);
4152
4153 return 0;
4154}
4155
4156
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004157static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004158 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004159{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004160 int *freqs, res;
4161 unsigned int freq_in_use = 0, num, i;
4162
4163 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4164 if (!freqs)
4165 return -1;
4166
4167 num = get_shared_radio_freqs(wpa_s, freqs,
4168 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004169 wpa_printf(MSG_DEBUG,
4170 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4171 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004172
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004173 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004174 int ret;
4175 if (go)
4176 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4177 else
4178 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4179 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004180 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4181 "(%u MHz) is not supported for P2P uses",
4182 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004183 res = -3;
4184 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004185 }
4186
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004187 for (i = 0; i < num; i++) {
4188 if (freqs[i] == freq)
4189 freq_in_use = 1;
4190 }
4191
4192 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4193 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4194 freq);
4195 res = -2;
4196 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004197 }
4198 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4199 "requested channel (%u MHz)", freq);
4200 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004201 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004202 }
4203
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004204 for (i = 0; i < num; i++) {
4205 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4206 continue;
4207
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004208 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004209 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4210 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004211 *pref_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004212 } else {
4213 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004214 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004215 *force_freq = freqs[i];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004216 }
4217 break;
4218 }
4219
4220 if (i == num) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004221 if (num < wpa_s->num_multichan_concurrent && num > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004222 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4223 *force_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004224 } else if (num < wpa_s->num_multichan_concurrent) {
4225 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4226 *force_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004227 } else {
4228 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4229 res = -2;
4230 goto exit_free;
4231 }
4232 }
4233
4234exit_ok:
4235 res = 0;
4236exit_free:
4237 os_free(freqs);
4238 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004239}
4240
4241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004242/**
4243 * wpas_p2p_connect - Request P2P Group Formation to be started
4244 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4245 * @peer_addr: Address of the peer P2P Device
4246 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4247 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004248 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249 * @join: Whether to join an existing group (as a client) instead of starting
4250 * Group Owner negotiation; @peer_addr is BSSID in that case
4251 * @auth: Whether to only authorize the connection instead of doing that and
4252 * initiating Group Owner negotiation
4253 * @go_intent: GO Intent or -1 to use default
4254 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004255 * @persistent_id: Persistent group credentials to use for forcing GO
4256 * parameters or -1 to generate new values (SSID/passphrase)
4257 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4258 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004259 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004260 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4262 * failure, -2 on failure due to channel not currently available,
4263 * -3 if forced channel is not supported
4264 */
4265int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4266 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004267 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004268 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004269 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004271 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004272 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004274 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004275 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276
4277 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4278 return -1;
4279
Dmitry Shmidt04949592012-07-19 12:16:46 -07004280 if (persistent_id >= 0) {
4281 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4282 if (ssid == NULL || ssid->disabled != 2 ||
4283 ssid->mode != WPAS_MODE_P2P_GO)
4284 return -1;
4285 }
4286
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004287 os_free(wpa_s->global->add_psk);
4288 wpa_s->global->add_psk = NULL;
4289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004290 if (go_intent < 0)
4291 go_intent = wpa_s->conf->p2p_go_intent;
4292
4293 if (!auth)
4294 wpa_s->p2p_long_listen = 0;
4295
4296 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004297 wpa_s->p2p_persistent_group = !!persistent_group;
4298 wpa_s->p2p_persistent_id = persistent_id;
4299 wpa_s->p2p_go_intent = go_intent;
4300 wpa_s->p2p_connect_freq = freq;
4301 wpa_s->p2p_fallback_to_go_neg = 0;
4302 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004303 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004304 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004305
4306 if (pin)
4307 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4308 else if (wps_method == WPS_PIN_DISPLAY) {
4309 ret = wps_generate_pin();
4310 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4311 ret);
4312 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4313 wpa_s->p2p_pin);
4314 } else
4315 wpa_s->p2p_pin[0] = '\0';
4316
Dmitry Shmidt04949592012-07-19 12:16:46 -07004317 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4319 if (auth) {
4320 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4321 "connect a running group from " MACSTR,
4322 MAC2STR(peer_addr));
4323 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4324 return ret;
4325 }
4326 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4327 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4328 iface_addr) < 0) {
4329 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4330 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4331 dev_addr);
4332 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004333 if (auto_join) {
4334 os_get_time(&wpa_s->p2p_auto_started);
4335 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4336 "%ld.%06ld",
4337 wpa_s->p2p_auto_started.sec,
4338 wpa_s->p2p_auto_started.usec);
4339 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004340 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004341 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4342 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004343 return -1;
4344 return ret;
4345 }
4346
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004347 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4348 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004349 if (res)
4350 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004351 wpas_p2p_set_own_freq_preference(wpa_s,
4352 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004353
4354 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4355
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004356 if (wpa_s->create_p2p_iface) {
4357 /* Prepare to add a new interface for the group */
4358 iftype = WPA_IF_P2P_GROUP;
4359 if (go_intent == 15)
4360 iftype = WPA_IF_P2P_GO;
4361 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4362 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4363 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004364 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004365 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004367 if_addr = wpa_s->pending_interface_addr;
4368 } else
4369 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370
4371 if (auth) {
4372 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004373 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004374 force_freq, persistent_group, ssid,
4375 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004376 return -1;
4377 return ret;
4378 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004379
4380 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4381 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004382 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004383 if (wpa_s->create_p2p_iface)
4384 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004385 return -1;
4386 }
4387 return ret;
4388}
4389
4390
4391/**
4392 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4393 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4394 * @freq: Frequency of the channel in MHz
4395 * @duration: Duration of the stay on the channel in milliseconds
4396 *
4397 * This callback is called when the driver indicates that it has started the
4398 * requested remain-on-channel duration.
4399 */
4400void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4401 unsigned int freq, unsigned int duration)
4402{
4403 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4404 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4406 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4407 wpa_s->pending_listen_duration);
4408 wpa_s->pending_listen_freq = 0;
4409 }
4410}
4411
4412
4413static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4414 unsigned int timeout)
4415{
4416 /* Limit maximum Listen state time based on driver limitation. */
4417 if (timeout > wpa_s->max_remain_on_chan)
4418 timeout = wpa_s->max_remain_on_chan;
4419
4420 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4421 return wpa_drv_p2p_listen(wpa_s, timeout);
4422
4423 return p2p_listen(wpa_s->global->p2p, timeout);
4424}
4425
4426
4427/**
4428 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4429 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4430 * @freq: Frequency of the channel in MHz
4431 *
4432 * This callback is called when the driver indicates that a remain-on-channel
4433 * operation has been completed, i.e., the duration on the requested channel
4434 * has timed out.
4435 */
4436void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4437 unsigned int freq)
4438{
4439 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4440 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004441 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004442 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4443 return;
4444 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4445 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004446 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004447 return;
4448 if (wpa_s->p2p_long_listen > 0)
4449 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4450 if (wpa_s->p2p_long_listen > 0) {
4451 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4452 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004453 } else {
4454 /*
4455 * When listen duration is over, stop listen & update p2p_state
4456 * to IDLE.
4457 */
4458 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459 }
4460}
4461
4462
4463/**
4464 * wpas_p2p_group_remove - Remove a P2P group
4465 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4466 * @ifname: Network interface name of the group interface or "*" to remove all
4467 * groups
4468 * Returns: 0 on success, -1 on failure
4469 *
4470 * This function is used to remove a P2P group. This can be used to disconnect
4471 * from a group in which the local end is a P2P Client or to end a P2P Group in
4472 * case the local end is the Group Owner. If a virtual network interface was
4473 * created for this group, that interface will be removed. Otherwise, only the
4474 * configured P2P group network will be removed from the interface.
4475 */
4476int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4477{
4478 struct wpa_global *global = wpa_s->global;
4479
4480 if (os_strcmp(ifname, "*") == 0) {
4481 struct wpa_supplicant *prev;
4482 wpa_s = global->ifaces;
4483 while (wpa_s) {
4484 prev = wpa_s;
4485 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004486 if (prev->p2p_group_interface !=
4487 NOT_P2P_GROUP_INTERFACE ||
4488 (prev->current_ssid &&
4489 prev->current_ssid->p2p_group))
4490 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004491 }
4492 return 0;
4493 }
4494
4495 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4496 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4497 break;
4498 }
4499
4500 return wpas_p2p_disconnect(wpa_s);
4501}
4502
4503
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004504static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4505{
4506 unsigned int r;
4507
4508 if (freq == 2) {
4509 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4510 "band");
4511 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004512 p2p_supported_freq_go(wpa_s->global->p2p,
4513 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004514 freq = wpa_s->best_24_freq;
4515 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4516 "channel: %d MHz", freq);
4517 } else {
4518 os_get_random((u8 *) &r, sizeof(r));
4519 freq = 2412 + (r % 3) * 25;
4520 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4521 "channel: %d MHz", freq);
4522 }
4523 }
4524
4525 if (freq == 5) {
4526 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4527 "band");
4528 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004529 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004530 wpa_s->best_5_freq)) {
4531 freq = wpa_s->best_5_freq;
4532 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4533 "channel: %d MHz", freq);
4534 } else {
4535 os_get_random((u8 *) &r, sizeof(r));
4536 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004537 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004538 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4539 "5 GHz channel for P2P group");
4540 return -1;
4541 }
4542 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4543 "channel: %d MHz", freq);
4544 }
4545 }
4546
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004547 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004548 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4549 "(%u MHz) is not supported for P2P uses",
4550 freq);
4551 return -1;
4552 }
4553
4554 return freq;
4555}
4556
4557
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004558static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4559 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004560 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004561 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004563 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004564 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004565 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566
4567 os_memset(params, 0, sizeof(*params));
4568 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004569 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004570 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004572 if (!freq_included(channels, freq)) {
4573 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4574 "accepted", freq);
4575 return -1;
4576 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4578 "frequency %d MHz", freq);
4579 params->freq = freq;
4580 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4581 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004582 wpa_s->conf->p2p_oper_channel <= 11 &&
4583 freq_included(channels,
4584 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004585 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4586 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4587 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004588 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4589 wpa_s->conf->p2p_oper_reg_class == 116 ||
4590 wpa_s->conf->p2p_oper_reg_class == 117 ||
4591 wpa_s->conf->p2p_oper_reg_class == 124 ||
4592 wpa_s->conf->p2p_oper_reg_class == 126 ||
4593 wpa_s->conf->p2p_oper_reg_class == 127) &&
4594 freq_included(channels,
4595 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004596 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4597 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4598 "frequency %d MHz", params->freq);
4599 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4600 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004601 p2p_supported_freq_go(wpa_s->global->p2p,
4602 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004603 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604 params->freq = wpa_s->best_overall_freq;
4605 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4606 "channel %d MHz", params->freq);
4607 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4608 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004609 p2p_supported_freq_go(wpa_s->global->p2p,
4610 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004611 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612 params->freq = wpa_s->best_24_freq;
4613 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4614 "channel %d MHz", params->freq);
4615 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4616 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004617 p2p_supported_freq_go(wpa_s->global->p2p,
4618 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004619 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620 params->freq = wpa_s->best_5_freq;
4621 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4622 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004623 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4624 channels))) {
4625 params->freq = pref_freq;
4626 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4627 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004629 int chan;
4630 for (chan = 0; chan < 11; chan++) {
4631 params->freq = 2412 + chan * 5;
4632 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004633 params->freq) &&
4634 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004635 break;
4636 }
4637 if (chan == 11) {
4638 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4639 "allowed");
4640 return -1;
4641 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004642 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4643 "known)", params->freq);
4644 }
4645
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004646 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4647 if (!freqs)
4648 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004649
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004650 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4651 wpa_s->num_multichan_concurrent);
4652 if (res < 0) {
4653 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004654 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004656 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004657
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004658 for (i = 0; i < num; i++) {
4659 if (freq && freqs[i] == freq)
4660 break;
4661 if (!freq && freq_included(channels, freqs[i])) {
4662 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4663 freqs[i]);
4664 params->freq = freqs[i];
4665 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004666 }
4667 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004668
4669 if (i == num) {
4670 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
4671 if (freq)
4672 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4673 else
4674 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4675 os_free(freqs);
4676 return -1;
4677 } else if (num == 0) {
4678 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
4679 } else {
4680 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4681 }
4682 }
4683
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004684 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004685 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004686}
4687
4688
4689static struct wpa_supplicant *
4690wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4691 int go)
4692{
4693 struct wpa_supplicant *group_wpa_s;
4694
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004695 if (!wpas_p2p_create_iface(wpa_s)) {
4696 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4697 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004698 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004699 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004700 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004701
4702 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004703 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004704 wpa_msg_global(wpa_s, MSG_ERROR,
4705 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004706 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004707 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004708 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4709 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004710 wpa_msg_global(wpa_s, MSG_ERROR,
4711 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004712 wpas_p2p_remove_pending_group_interface(wpa_s);
4713 return NULL;
4714 }
4715
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004716 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4717 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004718 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719 return group_wpa_s;
4720}
4721
4722
4723/**
4724 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4725 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4726 * @persistent_group: Whether to create a persistent group
4727 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004728 * @ht40: Start GO with 40 MHz channel width
4729 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004730 * Returns: 0 on success, -1 on failure
4731 *
4732 * This function creates a new P2P group with the local end as the Group Owner,
4733 * i.e., without using Group Owner Negotiation.
4734 */
4735int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004736 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737{
4738 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739
4740 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4741 return -1;
4742
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004743 os_free(wpa_s->global->add_psk);
4744 wpa_s->global->add_psk = NULL;
4745
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004746 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004747 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004748 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004749
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004750 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4751 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004752 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004754 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004755 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004756 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004757 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004758 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4759 "(%u MHz) is not supported for P2P uses",
4760 params.freq);
4761 return -1;
4762 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004763 p2p_go_params(wpa_s->global->p2p, &params);
4764 params.persistent_group = persistent_group;
4765
4766 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4767 if (wpa_s == NULL)
4768 return -1;
4769 wpas_start_wps_go(wpa_s, &params, 0);
4770
4771 return 0;
4772}
4773
4774
4775static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4776 struct wpa_ssid *params, int addr_allocated)
4777{
4778 struct wpa_ssid *ssid;
4779
4780 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4781 if (wpa_s == NULL)
4782 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004783 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004784
4785 wpa_supplicant_ap_deinit(wpa_s);
4786
4787 ssid = wpa_config_add_network(wpa_s->conf);
4788 if (ssid == NULL)
4789 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790 wpa_config_set_network_defaults(ssid);
4791 ssid->temporary = 1;
4792 ssid->proto = WPA_PROTO_RSN;
4793 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4794 ssid->group_cipher = WPA_CIPHER_CCMP;
4795 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4796 ssid->ssid = os_malloc(params->ssid_len);
4797 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798 wpa_config_remove_network(wpa_s->conf, ssid->id);
4799 return -1;
4800 }
4801 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4802 ssid->ssid_len = params->ssid_len;
4803 ssid->p2p_group = 1;
4804 ssid->export_keys = 1;
4805 if (params->psk_set) {
4806 os_memcpy(ssid->psk, params->psk, 32);
4807 ssid->psk_set = 1;
4808 }
4809 if (params->passphrase)
4810 ssid->passphrase = os_strdup(params->passphrase);
4811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004812 wpa_s->show_group_started = 1;
4813
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08004814 wpa_supplicant_select_network(wpa_s, ssid);
4815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816 return 0;
4817}
4818
4819
4820int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4821 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004822 int freq, int ht40, int vht,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004823 const struct p2p_channels *channels,
4824 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825{
4826 struct p2p_go_neg_results params;
4827 int go = 0;
4828
4829 if (ssid->disabled != 2 || ssid->ssid == NULL)
4830 return -1;
4831
4832 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4833 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4834 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4835 "already running");
4836 return 0;
4837 }
4838
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004839 os_free(wpa_s->global->add_psk);
4840 wpa_s->global->add_psk = NULL;
4841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004842 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004843 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004844
Dmitry Shmidt04949592012-07-19 12:16:46 -07004845 wpa_s->p2p_fallback_to_go_neg = 0;
4846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004847 if (ssid->mode == WPAS_MODE_INFRA)
4848 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4849
4850 if (ssid->mode != WPAS_MODE_P2P_GO)
4851 return -1;
4852
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004853 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4854 if (freq < 0)
4855 return -1;
4856
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004857 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004858 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004859
4860 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004861 params.psk_set = ssid->psk_set;
4862 if (params.psk_set)
4863 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004864 if (ssid->passphrase) {
4865 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4866 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4867 "persistent group");
4868 return -1;
4869 }
4870 os_strlcpy(params.passphrase, ssid->passphrase,
4871 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004873 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4874 params.ssid_len = ssid->ssid_len;
4875 params.persistent_group = 1;
4876
4877 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4878 if (wpa_s == NULL)
4879 return -1;
4880
Dmitry Shmidt56052862013-10-04 10:23:25 -07004881 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004882 wpas_start_wps_go(wpa_s, &params, 0);
4883
4884 return 0;
4885}
4886
4887
4888static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4889 struct wpabuf *proberesp_ies)
4890{
4891 struct wpa_supplicant *wpa_s = ctx;
4892 if (wpa_s->ap_iface) {
4893 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004894 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4895 wpabuf_free(beacon_ies);
4896 wpabuf_free(proberesp_ies);
4897 return;
4898 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004899 if (beacon_ies) {
4900 wpabuf_free(hapd->p2p_beacon_ie);
4901 hapd->p2p_beacon_ie = beacon_ies;
4902 }
4903 wpabuf_free(hapd->p2p_probe_resp_ie);
4904 hapd->p2p_probe_resp_ie = proberesp_ies;
4905 } else {
4906 wpabuf_free(beacon_ies);
4907 wpabuf_free(proberesp_ies);
4908 }
4909 wpa_supplicant_ap_update_beacon(wpa_s);
4910}
4911
4912
4913static void wpas_p2p_idle_update(void *ctx, int idle)
4914{
4915 struct wpa_supplicant *wpa_s = ctx;
4916 if (!wpa_s->ap_iface)
4917 return;
4918 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004919 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920 wpas_p2p_set_group_idle_timeout(wpa_s);
4921 else
4922 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4923}
4924
4925
4926struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004927 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928{
4929 struct p2p_group *group;
4930 struct p2p_group_config *cfg;
4931
4932 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4933 return NULL;
4934 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4935 return NULL;
4936
4937 cfg = os_zalloc(sizeof(*cfg));
4938 if (cfg == NULL)
4939 return NULL;
4940
Dmitry Shmidt04949592012-07-19 12:16:46 -07004941 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004942 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004943 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004944 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4946 if (wpa_s->max_stations &&
4947 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4948 cfg->max_clients = wpa_s->max_stations;
4949 else
4950 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004951 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4952 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953 cfg->cb_ctx = wpa_s;
4954 cfg->ie_update = wpas_p2p_ie_update;
4955 cfg->idle_update = wpas_p2p_idle_update;
4956
4957 group = p2p_group_init(wpa_s->global->p2p, cfg);
4958 if (group == NULL)
4959 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004960 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961 p2p_group_notif_formation_done(group);
4962 wpa_s->p2p_group = group;
4963 return group;
4964}
4965
4966
4967void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4968 int registrar)
4969{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004970 struct wpa_ssid *ssid = wpa_s->current_ssid;
4971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 if (!wpa_s->p2p_in_provisioning) {
4973 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4974 "provisioning not in progress");
4975 return;
4976 }
4977
Dmitry Shmidt04949592012-07-19 12:16:46 -07004978 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4979 u8 go_dev_addr[ETH_ALEN];
4980 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4981 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4982 ssid->ssid_len);
4983 /* Clear any stored provisioning info */
4984 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4985 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4988 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004989 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004990 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4991 /*
4992 * Use a separate timeout for initial data connection to
4993 * complete to allow the group to be removed automatically if
4994 * something goes wrong in this step before the P2P group idle
4995 * timeout mechanism is taken into use.
4996 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07004997 wpa_dbg(wpa_s, MSG_DEBUG,
4998 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4999 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005000 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5001 wpas_p2p_group_formation_timeout,
5002 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005003 } else if (ssid) {
5004 /*
5005 * Use a separate timeout for initial data connection to
5006 * complete to allow the group to be removed automatically if
5007 * the client does not complete data connection successfully.
5008 */
5009 wpa_dbg(wpa_s, MSG_DEBUG,
5010 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5011 P2P_MAX_INITIAL_CONN_WAIT_GO);
5012 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5013 wpas_p2p_group_formation_timeout,
5014 wpa_s->parent, NULL);
5015 /*
5016 * Complete group formation on first successful data connection
5017 */
5018 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005019 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005020 if (wpa_s->global->p2p)
5021 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5022 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5023 wpa_drv_wps_success_cb(wpa_s, peer_addr);
5024 wpas_group_formation_completed(wpa_s, 1);
5025}
5026
5027
Jouni Malinen75ecf522011-06-27 15:19:46 -07005028void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5029 struct wps_event_fail *fail)
5030{
5031 if (!wpa_s->p2p_in_provisioning) {
5032 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5033 "provisioning not in progress");
5034 return;
5035 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005036
5037 if (wpa_s->go_params) {
5038 p2p_clear_provisioning_info(
5039 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005040 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005041 }
5042
Jouni Malinen75ecf522011-06-27 15:19:46 -07005043 wpas_notify_p2p_wps_failed(wpa_s, fail);
5044}
5045
5046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005047int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005048 const char *config_method,
5049 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005050{
5051 u16 config_methods;
5052
Dmitry Shmidt04949592012-07-19 12:16:46 -07005053 wpa_s->p2p_fallback_to_go_neg = 0;
5054 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005055 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005057 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005058 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005059 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5060 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005062 else {
5063 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005065 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066
Dmitry Shmidt04949592012-07-19 12:16:46 -07005067 if (use == WPAS_P2P_PD_AUTO) {
5068 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5069 wpa_s->pending_pd_config_methods = config_methods;
5070 wpa_s->p2p_auto_pd = 1;
5071 wpa_s->p2p_auto_join = 0;
5072 wpa_s->pending_pd_before_join = 0;
5073 wpa_s->auto_pd_scan_retry = 0;
5074 wpas_p2p_stop_find(wpa_s);
5075 wpa_s->p2p_join_scan_count = 0;
5076 os_get_time(&wpa_s->p2p_auto_started);
5077 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5078 wpa_s->p2p_auto_started.sec,
5079 wpa_s->p2p_auto_started.usec);
5080 wpas_p2p_join_scan(wpa_s, NULL);
5081 return 0;
5082 }
5083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005084 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5085 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005086 config_methods,
5087 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088 }
5089
5090 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5091 return -1;
5092
5093 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005094 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005095 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005096}
5097
5098
5099int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5100 char *end)
5101{
5102 return p2p_scan_result_text(ies, ies_len, buf, end);
5103}
5104
5105
5106static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5107{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005108 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005109 return;
5110
5111 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5112 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005113 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114}
5115
5116
5117int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5118 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005119 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005120 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121{
5122 wpas_p2p_clear_pending_action_tx(wpa_s);
5123 wpa_s->p2p_long_listen = 0;
5124
5125 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5126 return wpa_drv_p2p_find(wpa_s, timeout, type);
5127
Dmitry Shmidt04949592012-07-19 12:16:46 -07005128 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5129 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005130 return -1;
5131
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005132 wpa_supplicant_cancel_sched_scan(wpa_s);
5133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005135 num_req_dev_types, req_dev_types, dev_id,
5136 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005137}
5138
5139
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005140static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005141{
5142 wpas_p2p_clear_pending_action_tx(wpa_s);
5143 wpa_s->p2p_long_listen = 0;
5144 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5145 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07005146 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147
5148 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5149 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005150 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005151 }
5152
5153 if (wpa_s->global->p2p)
5154 p2p_stop_find(wpa_s->global->p2p);
5155
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005156 return 0;
5157}
5158
5159
5160void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5161{
5162 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5163 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005164 wpas_p2p_remove_pending_group_interface(wpa_s);
5165}
5166
5167
5168static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5169{
5170 struct wpa_supplicant *wpa_s = eloop_ctx;
5171 wpa_s->p2p_long_listen = 0;
5172}
5173
5174
5175int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5176{
5177 int res;
5178
5179 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5180 return -1;
5181
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005182 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005183 wpas_p2p_clear_pending_action_tx(wpa_s);
5184
5185 if (timeout == 0) {
5186 /*
5187 * This is a request for unlimited Listen state. However, at
5188 * least for now, this is mapped to a Listen state for one
5189 * hour.
5190 */
5191 timeout = 3600;
5192 }
5193 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5194 wpa_s->p2p_long_listen = 0;
5195
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005196 /*
5197 * Stop previous find/listen operation to avoid trying to request a new
5198 * remain-on-channel operation while the driver is still running the
5199 * previous one.
5200 */
5201 if (wpa_s->global->p2p)
5202 p2p_stop_find(wpa_s->global->p2p);
5203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005204 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5205 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5206 wpa_s->p2p_long_listen = timeout * 1000;
5207 eloop_register_timeout(timeout, 0,
5208 wpas_p2p_long_listen_timeout,
5209 wpa_s, NULL);
5210 }
5211
5212 return res;
5213}
5214
5215
5216int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5217 u8 *buf, size_t len, int p2p_group)
5218{
5219 struct wpabuf *p2p_ie;
5220 int ret;
5221
5222 if (wpa_s->global->p2p_disabled)
5223 return -1;
5224 if (wpa_s->global->p2p == NULL)
5225 return -1;
5226 if (bss == NULL)
5227 return -1;
5228
5229 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5230 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5231 p2p_group, p2p_ie);
5232 wpabuf_free(p2p_ie);
5233
5234 return ret;
5235}
5236
5237
5238int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005239 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005240 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005241{
5242 if (wpa_s->global->p2p_disabled)
5243 return 0;
5244 if (wpa_s->global->p2p == NULL)
5245 return 0;
5246
Dmitry Shmidt04949592012-07-19 12:16:46 -07005247 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5248 ie, ie_len)) {
5249 case P2P_PREQ_NOT_P2P:
5250 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5251 ssi_signal);
5252 /* fall through */
5253 case P2P_PREQ_MALFORMED:
5254 case P2P_PREQ_NOT_LISTEN:
5255 case P2P_PREQ_NOT_PROCESSED:
5256 default: /* make gcc happy */
5257 return 0;
5258 case P2P_PREQ_PROCESSED:
5259 return 1;
5260 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005261}
5262
5263
5264void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5265 const u8 *sa, const u8 *bssid,
5266 u8 category, const u8 *data, size_t len, int freq)
5267{
5268 if (wpa_s->global->p2p_disabled)
5269 return;
5270 if (wpa_s->global->p2p == NULL)
5271 return;
5272
5273 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5274 freq);
5275}
5276
5277
5278void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5279{
5280 if (wpa_s->global->p2p_disabled)
5281 return;
5282 if (wpa_s->global->p2p == NULL)
5283 return;
5284
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005285 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005286}
5287
5288
5289void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5290{
5291 p2p_group_deinit(wpa_s->p2p_group);
5292 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005293
5294 wpa_s->ap_configured_cb = NULL;
5295 wpa_s->ap_configured_cb_ctx = NULL;
5296 wpa_s->ap_configured_cb_data = NULL;
5297 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005298}
5299
5300
5301int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5302{
5303 wpa_s->p2p_long_listen = 0;
5304
5305 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5306 return wpa_drv_p2p_reject(wpa_s, addr);
5307
5308 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5309 return -1;
5310
5311 return p2p_reject(wpa_s->global->p2p, addr);
5312}
5313
5314
5315/* Invite to reinvoke a persistent group */
5316int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005317 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005318 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005319{
5320 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005321 u8 *bssid = NULL;
5322 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005323 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005324 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005325
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005326 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005327 if (peer_addr)
5328 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5329 else
5330 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005331
Jouni Malinen31be0a42012-08-31 21:20:51 +03005332 wpa_s->p2p_persistent_go_freq = freq;
5333 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005334 if (ssid->mode == WPAS_MODE_P2P_GO) {
5335 role = P2P_INVITE_ROLE_GO;
5336 if (peer_addr == NULL) {
5337 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5338 "address in invitation command");
5339 return -1;
5340 }
5341 if (wpas_p2p_create_iface(wpa_s)) {
5342 if (wpas_p2p_add_group_interface(wpa_s,
5343 WPA_IF_P2P_GO) < 0) {
5344 wpa_printf(MSG_ERROR, "P2P: Failed to "
5345 "allocate a new interface for the "
5346 "group");
5347 return -1;
5348 }
5349 bssid = wpa_s->pending_interface_addr;
5350 } else
5351 bssid = wpa_s->own_addr;
5352 } else {
5353 role = P2P_INVITE_ROLE_CLIENT;
5354 peer_addr = ssid->bssid;
5355 }
5356 wpa_s->pending_invite_ssid_id = ssid->id;
5357
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005358 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5359 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005360 if (res)
5361 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005363 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5364 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5365 ssid->ssid, ssid->ssid_len,
5366 go_dev_addr, 1);
5367
5368 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5369 return -1;
5370
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005371 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5372 no_pref_freq_given && pref_freq > 0 &&
5373 wpa_s->num_multichan_concurrent > 1 &&
5374 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5375 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5376 pref_freq);
5377 pref_freq = 0;
5378 }
5379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005380 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005381 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5382 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383}
5384
5385
5386/* Invite to join an active group */
5387int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5388 const u8 *peer_addr, const u8 *go_dev_addr)
5389{
5390 struct wpa_global *global = wpa_s->global;
5391 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005392 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005393 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005394 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005395 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005396 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005397
Jouni Malinen31be0a42012-08-31 21:20:51 +03005398 wpa_s->p2p_persistent_go_freq = 0;
5399 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005400 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005402 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5403 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5404 break;
5405 }
5406 if (wpa_s == NULL) {
5407 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5408 return -1;
5409 }
5410
5411 ssid = wpa_s->current_ssid;
5412 if (ssid == NULL) {
5413 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5414 "invitation");
5415 return -1;
5416 }
5417
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005418 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005419 persistent = ssid->p2p_persistent_group &&
5420 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5421 ssid->ssid, ssid->ssid_len);
5422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005423 if (ssid->mode == WPAS_MODE_P2P_GO) {
5424 role = P2P_INVITE_ROLE_ACTIVE_GO;
5425 bssid = wpa_s->own_addr;
5426 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005427 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005428 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005429 } else {
5430 role = P2P_INVITE_ROLE_CLIENT;
5431 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5432 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5433 "invite to current group");
5434 return -1;
5435 }
5436 bssid = wpa_s->bssid;
5437 if (go_dev_addr == NULL &&
5438 !is_zero_ether_addr(wpa_s->go_dev_addr))
5439 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005440 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5441 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442 }
5443 wpa_s->parent->pending_invite_ssid_id = -1;
5444
5445 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5446 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5447 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005448 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005449
5450 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5451 return -1;
5452
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005453 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5454 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005455 if (res)
5456 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005457 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005460 ssid->ssid, ssid->ssid_len, force_freq,
5461 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462}
5463
5464
5465void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5466{
5467 struct wpa_ssid *ssid = wpa_s->current_ssid;
5468 const char *ssid_txt;
5469 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005470 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005471 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005472 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473
Dmitry Shmidt04949592012-07-19 12:16:46 -07005474 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5475 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5476 wpa_s->parent, NULL);
5477 }
5478
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005479 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005480 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481
5482 wpa_s->show_group_started = 0;
5483
5484 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5485 os_memset(go_dev_addr, 0, ETH_ALEN);
5486 if (ssid->bssid_set)
5487 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5488 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5489 ssid->ssid_len);
5490 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5491
5492 if (wpa_s->global->p2p_group_formation == wpa_s)
5493 wpa_s->global->p2p_group_formation = NULL;
5494
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005495 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5496 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005497 if (ssid->passphrase == NULL && ssid->psk_set) {
5498 char psk[65];
5499 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005500 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5501 "%s client ssid=\"%s\" freq=%d psk=%s "
5502 "go_dev_addr=" MACSTR "%s",
5503 wpa_s->ifname, ssid_txt, freq, psk,
5504 MAC2STR(go_dev_addr),
5505 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005506 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005507 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5508 "%s client ssid=\"%s\" freq=%d "
5509 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5510 wpa_s->ifname, ssid_txt, freq,
5511 ssid->passphrase ? ssid->passphrase : "",
5512 MAC2STR(go_dev_addr),
5513 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005514 }
5515
5516 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005517 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5518 ssid, go_dev_addr);
5519 if (network_id < 0)
5520 network_id = ssid->id;
5521 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005522
5523done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005524 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005525}
5526
5527
5528int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5529 u32 interval1, u32 duration2, u32 interval2)
5530{
5531 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5532 return -1;
5533 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5534 return -1;
5535
5536 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5537 wpa_s->current_ssid == NULL ||
5538 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5539 return -1;
5540
5541 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5542 wpa_s->own_addr, wpa_s->assoc_freq,
5543 duration1, interval1, duration2, interval2);
5544}
5545
5546
5547int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5548 unsigned int interval)
5549{
5550 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5551 return -1;
5552
5553 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5554 return -1;
5555
5556 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5557}
5558
5559
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005560static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5561{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005562 if (wpa_s->current_ssid == NULL) {
5563 /*
5564 * current_ssid can be cleared when P2P client interface gets
5565 * disconnected, so assume this interface was used as P2P
5566 * client.
5567 */
5568 return 1;
5569 }
5570 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005571 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5572}
5573
5574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005575static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5576{
5577 struct wpa_supplicant *wpa_s = eloop_ctx;
5578
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005579 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5581 "disabled");
5582 return;
5583 }
5584
Dmitry Shmidt04949592012-07-19 12:16:46 -07005585 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5586 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005587 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005588}
5589
5590
5591static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5592{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005593 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005594
Dmitry Shmidt04949592012-07-19 12:16:46 -07005595 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5596 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005598 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5599 return;
5600
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005601 timeout = wpa_s->conf->p2p_group_idle;
5602 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5603 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5604 timeout = P2P_MAX_CLIENT_IDLE;
5605
5606 if (timeout == 0)
5607 return;
5608
Dmitry Shmidt04949592012-07-19 12:16:46 -07005609 if (timeout < 0) {
5610 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5611 timeout = 0; /* special client mode no-timeout */
5612 else
5613 return;
5614 }
5615
5616 if (wpa_s->p2p_in_provisioning) {
5617 /*
5618 * Use the normal group formation timeout during the
5619 * provisioning phase to avoid terminating this process too
5620 * early due to group idle timeout.
5621 */
5622 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5623 "during provisioning");
5624 return;
5625 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305626
Dmitry Shmidt04949592012-07-19 12:16:46 -07005627 if (wpa_s->show_group_started) {
5628 /*
5629 * Use the normal group formation timeout between the end of
5630 * the provisioning phase and completion of 4-way handshake to
5631 * avoid terminating this process too early due to group idle
5632 * timeout.
5633 */
5634 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5635 "while waiting for initial 4-way handshake to "
5636 "complete");
5637 return;
5638 }
5639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005640 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005641 timeout);
5642 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5643 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005644}
5645
5646
Jouni Malinen2b89da82012-08-31 22:04:41 +03005647/* Returns 1 if the interface was removed */
5648int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5649 u16 reason_code, const u8 *ie, size_t ie_len,
5650 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005651{
5652 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005653 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005654 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005655 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005656
Dmitry Shmidt04949592012-07-19 12:16:46 -07005657 if (!locally_generated)
5658 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5659 ie_len);
5660
5661 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5662 wpa_s->current_ssid &&
5663 wpa_s->current_ssid->p2p_group &&
5664 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5665 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5666 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005667 if (wpas_p2p_group_delete(wpa_s,
5668 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5669 > 0)
5670 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005671 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005672
5673 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674}
5675
5676
5677void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005678 u16 reason_code, const u8 *ie, size_t ie_len,
5679 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680{
5681 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5682 return;
5683 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5684 return;
5685
Dmitry Shmidt04949592012-07-19 12:16:46 -07005686 if (!locally_generated)
5687 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5688 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005689}
5690
5691
5692void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5693{
5694 struct p2p_data *p2p = wpa_s->global->p2p;
5695
5696 if (p2p == NULL)
5697 return;
5698
5699 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5700 return;
5701
5702 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5703 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5704
5705 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5706 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5707
5708 if (wpa_s->wps &&
5709 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5710 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5711
5712 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5713 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5714
5715 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5716 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5717 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5718 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5719 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5720 }
5721
5722 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5723 p2p_set_sec_dev_types(p2p,
5724 (void *) wpa_s->conf->sec_device_type,
5725 wpa_s->conf->num_sec_device_types);
5726
5727 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5728 int i;
5729 p2p_remove_wps_vendor_extensions(p2p);
5730 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5731 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5732 continue;
5733 p2p_add_wps_vendor_extension(
5734 p2p, wpa_s->conf->wps_vendor_ext[i]);
5735 }
5736 }
5737
5738 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5739 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5740 char country[3];
5741 country[0] = wpa_s->conf->country[0];
5742 country[1] = wpa_s->conf->country[1];
5743 country[2] = 0x04;
5744 p2p_set_country(p2p, country);
5745 }
5746
5747 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5748 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5749 wpa_s->conf->p2p_ssid_postfix ?
5750 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5751 0);
5752 }
5753
5754 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5755 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005756
5757 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5758 u8 reg_class, channel;
5759 int ret;
5760 unsigned int r;
5761 if (wpa_s->conf->p2p_listen_reg_class &&
5762 wpa_s->conf->p2p_listen_channel) {
5763 reg_class = wpa_s->conf->p2p_listen_reg_class;
5764 channel = wpa_s->conf->p2p_listen_channel;
5765 } else {
5766 reg_class = 81;
5767 /*
5768 * Pick one of the social channels randomly as the
5769 * listen channel.
5770 */
5771 os_get_random((u8 *) &r, sizeof(r));
5772 channel = 1 + (r % 3) * 5;
5773 }
5774 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5775 if (ret)
5776 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5777 "failed: %d", ret);
5778 }
5779 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5780 u8 op_reg_class, op_channel, cfg_op_channel;
5781 int ret = 0;
5782 unsigned int r;
5783 if (wpa_s->conf->p2p_oper_reg_class &&
5784 wpa_s->conf->p2p_oper_channel) {
5785 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5786 op_channel = wpa_s->conf->p2p_oper_channel;
5787 cfg_op_channel = 1;
5788 } else {
5789 op_reg_class = 81;
5790 /*
5791 * Use random operation channel from (1, 6, 11)
5792 *if no other preference is indicated.
5793 */
5794 os_get_random((u8 *) &r, sizeof(r));
5795 op_channel = 1 + (r % 3) * 5;
5796 cfg_op_channel = 0;
5797 }
5798 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5799 cfg_op_channel);
5800 if (ret)
5801 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5802 "failed: %d", ret);
5803 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005804
5805 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5806 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5807 wpa_s->conf->p2p_pref_chan) < 0) {
5808 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5809 "update failed");
5810 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005811
5812 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
5813 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
5814 "update failed");
5815 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005816 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005817}
5818
5819
5820int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5821 int duration)
5822{
5823 if (!wpa_s->ap_iface)
5824 return -1;
5825 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5826 duration);
5827}
5828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005829
5830int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5831{
5832 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5833 return -1;
5834 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5835 return -1;
5836
5837 wpa_s->global->cross_connection = enabled;
5838 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5839
5840 if (!enabled) {
5841 struct wpa_supplicant *iface;
5842
5843 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5844 {
5845 if (iface->cross_connect_enabled == 0)
5846 continue;
5847
5848 iface->cross_connect_enabled = 0;
5849 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005850 wpa_msg_global(iface->parent, MSG_INFO,
5851 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5852 iface->ifname,
5853 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005854 }
5855 }
5856
5857 return 0;
5858}
5859
5860
5861static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5862{
5863 struct wpa_supplicant *iface;
5864
5865 if (!uplink->global->cross_connection)
5866 return;
5867
5868 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5869 if (!iface->cross_connect_enabled)
5870 continue;
5871 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5872 0)
5873 continue;
5874 if (iface->ap_iface == NULL)
5875 continue;
5876 if (iface->cross_connect_in_use)
5877 continue;
5878
5879 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005880 wpa_msg_global(iface->parent, MSG_INFO,
5881 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5882 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005883 }
5884}
5885
5886
5887static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5888{
5889 struct wpa_supplicant *iface;
5890
5891 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5892 if (!iface->cross_connect_enabled)
5893 continue;
5894 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5895 0)
5896 continue;
5897 if (!iface->cross_connect_in_use)
5898 continue;
5899
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005900 wpa_msg_global(iface->parent, MSG_INFO,
5901 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5902 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005903 iface->cross_connect_in_use = 0;
5904 }
5905}
5906
5907
5908void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5909{
5910 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5911 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5912 wpa_s->cross_connect_disallowed)
5913 wpas_p2p_disable_cross_connect(wpa_s);
5914 else
5915 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005916 if (!wpa_s->ap_iface &&
5917 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5918 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005919}
5920
5921
5922void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5923{
5924 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005925 if (!wpa_s->ap_iface &&
5926 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5927 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005928 wpas_p2p_set_group_idle_timeout(wpa_s);
5929}
5930
5931
5932static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5933{
5934 struct wpa_supplicant *iface;
5935
5936 if (!wpa_s->global->cross_connection)
5937 return;
5938
5939 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5940 if (iface == wpa_s)
5941 continue;
5942 if (iface->drv_flags &
5943 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5944 continue;
5945 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5946 continue;
5947
5948 wpa_s->cross_connect_enabled = 1;
5949 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5950 sizeof(wpa_s->cross_connect_uplink));
5951 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5952 "%s to %s whenever uplink is available",
5953 wpa_s->ifname, wpa_s->cross_connect_uplink);
5954
5955 if (iface->ap_iface || iface->current_ssid == NULL ||
5956 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5957 iface->cross_connect_disallowed ||
5958 iface->wpa_state != WPA_COMPLETED)
5959 break;
5960
5961 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005962 wpa_msg_global(wpa_s->parent, MSG_INFO,
5963 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5964 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005965 break;
5966 }
5967}
5968
5969
5970int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5971{
5972 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5973 !wpa_s->p2p_in_provisioning)
5974 return 0; /* not P2P client operation */
5975
5976 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5977 "session overlap");
5978 if (wpa_s != wpa_s->parent)
5979 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005980 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005981 return 1;
5982}
5983
5984
5985void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5986{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005987 struct p2p_channels chan, cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005988
5989 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5990 return;
5991
5992 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005993 os_memset(&cli_chan, 0, sizeof(cli_chan));
5994 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005995 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5996 "channel list");
5997 return;
5998 }
5999
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006000 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006001}
6002
6003
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006004static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6005 struct wpa_scan_results *scan_res)
6006{
6007 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6008}
6009
6010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006011int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6012{
6013 struct wpa_global *global = wpa_s->global;
6014 int found = 0;
6015 const u8 *peer;
6016
6017 if (global->p2p == NULL)
6018 return -1;
6019
6020 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6021
6022 if (wpa_s->pending_interface_name[0] &&
6023 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6024 found = 1;
6025
6026 peer = p2p_get_go_neg_peer(global->p2p);
6027 if (peer) {
6028 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6029 MACSTR, MAC2STR(peer));
6030 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006031 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032 }
6033
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006034 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6035 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6036 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6037 found = 1;
6038 }
6039
6040 if (wpa_s->pending_pd_before_join) {
6041 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6042 wpa_s->pending_pd_before_join = 0;
6043 found = 1;
6044 }
6045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046 wpas_p2p_stop_find(wpa_s);
6047
6048 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6049 if (wpa_s == global->p2p_group_formation &&
6050 (wpa_s->p2p_in_provisioning ||
6051 wpa_s->parent->pending_interface_type ==
6052 WPA_IF_P2P_CLIENT)) {
6053 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6054 "formation found - cancelling",
6055 wpa_s->ifname);
6056 found = 1;
6057 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6058 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006059 if (wpa_s->p2p_in_provisioning) {
6060 wpas_group_formation_completed(wpa_s, 0);
6061 break;
6062 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006063 wpas_p2p_group_delete(wpa_s,
6064 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065 break;
6066 }
6067 }
6068
6069 if (!found) {
6070 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6071 return -1;
6072 }
6073
6074 return 0;
6075}
6076
6077
6078void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6079{
6080 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6081 return;
6082
6083 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6084 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006085 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006086}
6087
6088
6089void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6090 int freq_24, int freq_5, int freq_overall)
6091{
6092 struct p2p_data *p2p = wpa_s->global->p2p;
6093 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6094 return;
6095 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6096}
6097
6098
6099int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6100{
6101 u8 peer[ETH_ALEN];
6102 struct p2p_data *p2p = wpa_s->global->p2p;
6103
6104 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6105 return -1;
6106
6107 if (hwaddr_aton(addr, peer))
6108 return -1;
6109
6110 return p2p_unauthorize(p2p, peer);
6111}
6112
6113
6114/**
6115 * wpas_p2p_disconnect - Disconnect from a P2P Group
6116 * @wpa_s: Pointer to wpa_supplicant data
6117 * Returns: 0 on success, -1 on failure
6118 *
6119 * This can be used to disconnect from a group in which the local end is a P2P
6120 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6121 * virtual network interface was created for this group, that interface will be
6122 * removed. Otherwise, only the configured P2P group network will be removed
6123 * from the interface.
6124 */
6125int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6126{
6127
6128 if (wpa_s == NULL)
6129 return -1;
6130
Jouni Malinen2b89da82012-08-31 22:04:41 +03006131 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6132 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006133}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006134
6135
6136int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6137{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006138 int ret;
6139
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006140 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6141 return 0;
6142
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006143 ret = p2p_in_progress(wpa_s->global->p2p);
6144 if (ret == 0) {
6145 /*
6146 * Check whether there is an ongoing WPS provisioning step (or
6147 * other parts of group formation) on another interface since
6148 * p2p_in_progress() does not report this to avoid issues for
6149 * scans during such provisioning step.
6150 */
6151 if (wpa_s->global->p2p_group_formation &&
6152 wpa_s->global->p2p_group_formation != wpa_s) {
6153 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6154 "in group formation",
6155 wpa_s->global->p2p_group_formation->ifname);
6156 ret = 1;
6157 }
6158 }
6159
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006160 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6161 struct os_time now;
6162 os_get_time(&now);
6163 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
6164 P2P_MAX_INITIAL_CONN_WAIT_GO) {
6165 /* Wait for the first client has expired */
6166 wpa_s->global->p2p_go_wait_client.sec = 0;
6167 } else {
6168 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6169 ret = 1;
6170 }
6171 }
6172
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006173 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006174}
6175
6176
6177void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6178 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006179{
6180 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6181 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6182 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006183 /**
6184 * Remove the network by scheduling the group formation
6185 * timeout to happen immediately. The teardown code
6186 * needs to be scheduled to run asynch later so that we
6187 * don't delete data from under ourselves unexpectedly.
6188 * Calling wpas_p2p_group_formation_timeout directly
6189 * causes a series of crashes in WPS failure scenarios.
6190 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006191 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6192 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006193 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6194 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006195 }
6196}
6197
6198
6199struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006200 const u8 *addr, const u8 *ssid,
6201 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006202{
6203 struct wpa_ssid *s;
6204 size_t i;
6205
6206 for (s = wpa_s->conf->ssid; s; s = s->next) {
6207 if (s->disabled != 2)
6208 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006209 if (ssid &&
6210 (ssid_len != s->ssid_len ||
6211 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6212 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006213 if (addr == NULL) {
6214 if (s->mode == WPAS_MODE_P2P_GO)
6215 return s;
6216 continue;
6217 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006218 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6219 return s; /* peer is GO in the persistent group */
6220 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6221 continue;
6222 for (i = 0; i < s->num_p2p_clients; i++) {
6223 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6224 addr, ETH_ALEN) == 0)
6225 return s; /* peer is P2P client in persistent
6226 * group */
6227 }
6228 }
6229
6230 return NULL;
6231}
6232
6233
6234void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6235 const u8 *addr)
6236{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006237 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6238 wpa_s->parent, NULL) > 0) {
6239 /*
6240 * This can happen if WPS provisioning step is not terminated
6241 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6242 * peer was able to connect, there is no need to time out group
6243 * formation after this, though. In addition, this is used with
6244 * the initial connection wait on the GO as a separate formation
6245 * timeout and as such, expected to be hit after the initial WPS
6246 * provisioning step.
6247 */
6248 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6249 }
6250 if (!wpa_s->p2p_go_group_formation_completed) {
6251 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6252 wpa_s->p2p_go_group_formation_completed = 1;
6253 wpa_s->global->p2p_group_formation = NULL;
6254 wpa_s->p2p_in_provisioning = 0;
6255 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006256 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006257 if (addr == NULL)
6258 return;
6259 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6260}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006261
Dmitry Shmidt04949592012-07-19 12:16:46 -07006262
6263static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6264 int group_added)
6265{
6266 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006267 if (wpa_s->global->p2p_group_formation)
6268 group = wpa_s->global->p2p_group_formation;
6269 wpa_s = wpa_s->parent;
6270 offchannel_send_action_done(wpa_s);
6271 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006272 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006273 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6274 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6275 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6276 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6277 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006278 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006279 wpa_s->p2p_go_ht40,
6280 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006281}
6282
6283
6284int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6285{
6286 if (!wpa_s->p2p_fallback_to_go_neg ||
6287 wpa_s->p2p_in_provisioning <= 5)
6288 return 0;
6289
6290 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6291 return 0; /* peer operating as a GO */
6292
6293 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6294 "fallback to GO Negotiation");
6295 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6296
6297 return 1;
6298}
6299
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006300
6301unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6302{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006303 struct wpa_supplicant *ifs;
6304
6305 if (wpa_s->wpa_state > WPA_SCANNING) {
6306 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6307 "concurrent operation",
6308 P2P_CONCURRENT_SEARCH_DELAY);
6309 return P2P_CONCURRENT_SEARCH_DELAY;
6310 }
6311
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006312 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6313 radio_list) {
6314 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006315 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6316 "delay due to concurrent operation on "
6317 "interface %s",
6318 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6319 return P2P_CONCURRENT_SEARCH_DELAY;
6320 }
6321 }
6322
6323 return 0;
6324}
6325
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006326
6327void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6328{
6329 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6330 "pending anymore (sta_scan_pending=%d "
6331 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6332 wpa_s->global->p2p_cb_on_scan_complete);
6333 wpa_s->sta_scan_pending = 0;
6334
6335 if (!wpa_s->global->p2p_cb_on_scan_complete)
6336 return;
6337 wpa_s->global->p2p_cb_on_scan_complete = 0;
6338
6339 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6340 return;
6341
6342 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6343 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6344 "continued after successful connection");
6345 p2p_increase_search_delay(wpa_s->global->p2p,
6346 wpas_p2p_search_delay(wpa_s));
6347 }
6348}
6349
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006350
6351static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6352 struct wpa_ssid *s, const u8 *addr,
6353 int iface_addr)
6354{
6355 struct psk_list_entry *psk, *tmp;
6356 int changed = 0;
6357
6358 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6359 list) {
6360 if ((iface_addr && !psk->p2p &&
6361 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6362 (!iface_addr && psk->p2p &&
6363 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6364 wpa_dbg(wpa_s, MSG_DEBUG,
6365 "P2P: Remove persistent group PSK list entry for "
6366 MACSTR " p2p=%u",
6367 MAC2STR(psk->addr), psk->p2p);
6368 dl_list_del(&psk->list);
6369 os_free(psk);
6370 changed++;
6371 }
6372 }
6373
6374 return changed;
6375}
6376
6377
6378void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6379 const u8 *p2p_dev_addr,
6380 const u8 *psk, size_t psk_len)
6381{
6382 struct wpa_ssid *ssid = wpa_s->current_ssid;
6383 struct wpa_ssid *persistent;
6384 struct psk_list_entry *p;
6385
6386 if (psk_len != sizeof(p->psk))
6387 return;
6388
6389 if (p2p_dev_addr) {
6390 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6391 " p2p_dev_addr=" MACSTR,
6392 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6393 if (is_zero_ether_addr(p2p_dev_addr))
6394 p2p_dev_addr = NULL;
6395 } else {
6396 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6397 MAC2STR(mac_addr));
6398 }
6399
6400 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6401 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6402 /* To be added to persistent group once created */
6403 if (wpa_s->global->add_psk == NULL) {
6404 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6405 if (wpa_s->global->add_psk == NULL)
6406 return;
6407 }
6408 p = wpa_s->global->add_psk;
6409 if (p2p_dev_addr) {
6410 p->p2p = 1;
6411 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6412 } else {
6413 p->p2p = 0;
6414 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6415 }
6416 os_memcpy(p->psk, psk, psk_len);
6417 return;
6418 }
6419
6420 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6421 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6422 return;
6423 }
6424
6425 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6426 ssid->ssid_len);
6427 if (!persistent) {
6428 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6429 return;
6430 }
6431
6432 p = os_zalloc(sizeof(*p));
6433 if (p == NULL)
6434 return;
6435 if (p2p_dev_addr) {
6436 p->p2p = 1;
6437 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6438 } else {
6439 p->p2p = 0;
6440 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6441 }
6442 os_memcpy(p->psk, psk, psk_len);
6443
6444 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6445 struct psk_list_entry *last;
6446 last = dl_list_last(&persistent->psk_list,
6447 struct psk_list_entry, list);
6448 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6449 MACSTR " (p2p=%u) to make room for a new one",
6450 MAC2STR(last->addr), last->p2p);
6451 dl_list_del(&last->list);
6452 os_free(last);
6453 }
6454
6455 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6456 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6457 p2p_dev_addr == NULL);
6458 if (p2p_dev_addr) {
6459 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6460 MACSTR, MAC2STR(p2p_dev_addr));
6461 } else {
6462 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6463 MAC2STR(mac_addr));
6464 }
6465 dl_list_add(&persistent->psk_list, &p->list);
6466
6467#ifndef CONFIG_NO_CONFIG_WRITE
6468 if (wpa_s->parent->conf->update_config &&
6469 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6470 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6471#endif /* CONFIG_NO_CONFIG_WRITE */
6472}
6473
6474
6475static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6476 struct wpa_ssid *s, const u8 *addr,
6477 int iface_addr)
6478{
6479 int res;
6480
6481 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6482 if (res > 0) {
6483#ifndef CONFIG_NO_CONFIG_WRITE
6484 if (wpa_s->conf->update_config &&
6485 wpa_config_write(wpa_s->confname, wpa_s->conf))
6486 wpa_dbg(wpa_s, MSG_DEBUG,
6487 "P2P: Failed to update configuration");
6488#endif /* CONFIG_NO_CONFIG_WRITE */
6489 }
6490}
6491
6492
6493static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6494 const u8 *peer, int iface_addr)
6495{
6496 struct hostapd_data *hapd;
6497 struct hostapd_wpa_psk *psk, *prev, *rem;
6498 struct sta_info *sta;
6499
6500 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6501 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6502 return;
6503
6504 /* Remove per-station PSK entry */
6505 hapd = wpa_s->ap_iface->bss[0];
6506 prev = NULL;
6507 psk = hapd->conf->ssid.wpa_psk;
6508 while (psk) {
6509 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6510 (!iface_addr &&
6511 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6512 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6513 MACSTR " iface_addr=%d",
6514 MAC2STR(peer), iface_addr);
6515 if (prev)
6516 prev->next = psk->next;
6517 else
6518 hapd->conf->ssid.wpa_psk = psk->next;
6519 rem = psk;
6520 psk = psk->next;
6521 os_free(rem);
6522 } else {
6523 prev = psk;
6524 psk = psk->next;
6525 }
6526 }
6527
6528 /* Disconnect from group */
6529 if (iface_addr)
6530 sta = ap_get_sta(hapd, peer);
6531 else
6532 sta = ap_get_sta_p2p(hapd, peer);
6533 if (sta) {
6534 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6535 " (iface_addr=%d) from group",
6536 MAC2STR(peer), iface_addr);
6537 hostapd_drv_sta_deauth(hapd, sta->addr,
6538 WLAN_REASON_DEAUTH_LEAVING);
6539 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6540 }
6541}
6542
6543
6544void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6545 int iface_addr)
6546{
6547 struct wpa_ssid *s;
6548 struct wpa_supplicant *w;
6549
6550 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6551
6552 /* Remove from any persistent group */
6553 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6554 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6555 continue;
6556 if (!iface_addr)
6557 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6558 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6559 }
6560
6561 /* Remove from any operating group */
6562 for (w = wpa_s->global->ifaces; w; w = w->next)
6563 wpas_p2p_remove_client_go(w, peer, iface_addr);
6564}
6565
6566
6567static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6568{
6569 struct wpa_supplicant *wpa_s = eloop_ctx;
6570 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6571}
6572
6573
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08006574static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6575{
6576 struct wpa_supplicant *wpa_s = eloop_ctx;
6577
6578 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6579 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6580}
6581
6582
6583int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6584 struct wpa_ssid *ssid)
6585{
6586 struct wpa_supplicant *iface;
6587
6588 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6589 if (!iface->current_ssid ||
6590 iface->current_ssid->frequency == freq ||
6591 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6592 !iface->current_ssid->p2p_group))
6593 continue;
6594
6595 /* Remove the connection with least priority */
6596 if (!wpas_is_p2p_prioritized(iface)) {
6597 /* STA connection has priority over existing
6598 * P2P connection, so remove the interface. */
6599 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6600 eloop_register_timeout(0, 0,
6601 wpas_p2p_group_freq_conflict,
6602 iface, NULL);
6603 /* If connection in progress is P2P connection, do not
6604 * proceed for the connection. */
6605 if (wpa_s == iface)
6606 return -1;
6607 else
6608 return 0;
6609 } else {
6610 /* P2P connection has priority, disable the STA network
6611 */
6612 wpa_supplicant_disable_network(wpa_s->global->ifaces,
6613 ssid);
6614 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6615 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6616 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6617 ETH_ALEN);
6618 /* If P2P connection is in progress, continue
6619 * connecting...*/
6620 if (wpa_s == iface)
6621 return 0;
6622 else
6623 return -1;
6624 }
6625 }
6626
6627 return 0;
6628}
6629
6630
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006631int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6632{
6633 struct wpa_ssid *ssid = wpa_s->current_ssid;
6634
6635 if (ssid == NULL || !ssid->p2p_group)
6636 return 0;
6637
6638 if (wpa_s->p2p_last_4way_hs_fail &&
6639 wpa_s->p2p_last_4way_hs_fail == ssid) {
6640 u8 go_dev_addr[ETH_ALEN];
6641 struct wpa_ssid *persistent;
6642
6643 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6644 ssid->ssid,
6645 ssid->ssid_len) <= 0) {
6646 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6647 goto disconnect;
6648 }
6649
6650 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6651 MACSTR, MAC2STR(go_dev_addr));
6652 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6653 ssid->ssid,
6654 ssid->ssid_len);
6655 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6656 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6657 goto disconnect;
6658 }
6659 wpa_msg_global(wpa_s->parent, MSG_INFO,
6660 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6661 persistent->id);
6662 disconnect:
6663 wpa_s->p2p_last_4way_hs_fail = NULL;
6664 /*
6665 * Remove the group from a timeout to avoid issues with caller
6666 * continuing to use the interface if this is on a P2P group
6667 * interface.
6668 */
6669 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6670 wpa_s, NULL);
6671 return 1;
6672 }
6673
6674 wpa_s->p2p_last_4way_hs_fail = ssid;
6675 return 0;
6676}