blob: 21d99f1077a14329f3edc934e4cc32cc094b2974 [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"
36
37
38/*
39 * How many times to try to scan to find the GO before giving up on join
40 * request.
41 */
42#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
43
Dmitry Shmidt04949592012-07-19 12:16:46 -070044#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
45
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080046#ifndef P2P_MAX_CLIENT_IDLE
47/*
48 * How many seconds to try to reconnect to the GO when connection in P2P client
49 * role has been lost.
50 */
51#define P2P_MAX_CLIENT_IDLE 10
52#endif /* P2P_MAX_CLIENT_IDLE */
53
Dmitry Shmidt04949592012-07-19 12:16:46 -070054#ifndef P2P_MAX_INITIAL_CONN_WAIT
55/*
56 * How many seconds to wait for initial 4-way handshake to get completed after
57 * WPS provisioning step.
58 */
59#define P2P_MAX_INITIAL_CONN_WAIT 10
60#endif /* P2P_MAX_INITIAL_CONN_WAIT */
61
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070062#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
63/*
64 * How many seconds to wait for initial 4-way handshake to get completed after
65 * WPS provisioning step on the GO. This controls the extra time the P2P
66 * operation is considered to be in progress (e.g., to delay other scans) after
67 * WPS provisioning has been completed on the GO during group formation.
68 */
69#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
70#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
71
Dmitry Shmidt56052862013-10-04 10:23:25 -070072#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
73/*
74 * How many seconds to wait for initial 4-way handshake to get completed after
75 * re-invocation of a persistent group on the GO when the client is expected
76 * to connect automatically (no user interaction).
77 */
78#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
79#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
80
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070081#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070082#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070083#endif /* P2P_CONCURRENT_SEARCH_DELAY */
84
Dmitry Shmidt34af3062013-07-11 10:46:32 -070085#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
86
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087enum p2p_group_removal_reason {
88 P2P_GROUP_REMOVAL_UNKNOWN,
89 P2P_GROUP_REMOVAL_SILENT,
90 P2P_GROUP_REMOVAL_FORMATION_FAILED,
91 P2P_GROUP_REMOVAL_REQUESTED,
92 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
93 P2P_GROUP_REMOVAL_UNAVAILABLE,
94 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidta6163552013-12-05 14:25:21 -080095 P2P_GROUP_REMOVAL_PSK_FAILURE
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070096};
97
Jouni Malinendc7b7132012-09-14 12:53:47 -070098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
100static struct wpa_supplicant *
101wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
102 int go);
103static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700104static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
106static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700107 const u8 *dev_addr, enum p2p_wps_method wps_method,
108 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
110static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
111static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
112static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800113static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
114 void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700115static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
116 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800117static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118
119
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700120/*
121 * Get the number of concurrent channels that the HW can operate, but that are
122 * currently not in use by any of the wpa_supplicant interfaces.
123 */
124static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
125{
126 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800127 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700128
129 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
130 if (!freqs)
131 return -1;
132
133 num = get_shared_radio_freqs(wpa_s, freqs,
134 wpa_s->num_multichan_concurrent);
135 os_free(freqs);
136
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800137 unused = wpa_s->num_multichan_concurrent - num;
138 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
139 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700140}
141
142
143/*
144 * Get the frequencies that are currently in use by one or more of the virtual
145 * interfaces, and that are also valid for P2P operation.
146 */
147static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
148 int *p2p_freqs, unsigned int len)
149{
150 int *freqs;
151 unsigned int num, i, j;
152
153 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
154 if (!freqs)
155 return -1;
156
157 num = get_shared_radio_freqs(wpa_s, freqs,
158 wpa_s->num_multichan_concurrent);
159
160 os_memset(p2p_freqs, 0, sizeof(int) * len);
161
162 for (i = 0, j = 0; i < num && j < len; i++) {
163 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
164 p2p_freqs[j++] = freqs[i];
165 }
166
167 os_free(freqs);
168
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800169 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
170
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700171 return j;
172}
173
174
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700175static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
176 int freq)
177{
178 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
179 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800180 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
181 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
182 wpas_p2p_num_unused_channels(wpa_s) > 0) {
183 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
184 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700185 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800186 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700187 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
188}
189
190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
192 struct wpa_scan_results *scan_res)
193{
194 size_t i;
195
196 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
197 return;
198
199 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
200 (int) scan_res->num);
201
202 for (i = 0; i < scan_res->num; i++) {
203 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800204 struct os_time time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700205 const u8 *ies;
206 size_t ies_len;
207
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800208 time_tmp_age.sec = bss->age / 1000;
209 time_tmp_age.usec = (bss->age % 1000) * 1000;
210 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700211
212 ies = (const u8 *) (bss + 1);
213 ies_len = bss->ie_len;
214 if (bss->beacon_ie_len > 0 &&
215 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
216 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
217 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
218 MACSTR, MAC2STR(bss->bssid));
219 ies = ies + ies_len;
220 ies_len = bss->beacon_ie_len;
221 }
222
223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800225 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700226 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 break;
228 }
229
230 p2p_scan_res_handled(wpa_s->global->p2p);
231}
232
233
234static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
235 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700236 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237{
238 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700239 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240 struct wpa_driver_scan_params params;
241 int ret;
242 struct wpabuf *wps_ie, *ies;
243 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800244 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245
246 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
247 return -1;
248
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700249 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
250 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700251 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700252 wpas_p2p_in_progress(wpa_s) == 2) {
253 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
254 "pending station mode scan to be "
255 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700256 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700257 wpa_supplicant_req_scan(ifs, 0, 0);
258 return 1;
259 }
260 }
261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 os_memset(&params, 0, sizeof(params));
263
264 /* P2P Wildcard SSID */
265 params.num_ssids = 1;
266 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
267 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
268
269 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700270 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
271 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 num_req_dev_types, req_dev_types);
273 if (wps_ie == NULL)
274 return -1;
275
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800276 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
277 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278 if (ies == NULL) {
279 wpabuf_free(wps_ie);
280 return -1;
281 }
282 wpabuf_put_buf(ies, wps_ie);
283 wpabuf_free(wps_ie);
284
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800285 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800287 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 params.extra_ies = wpabuf_head(ies);
289 params.extra_ies_len = wpabuf_len(ies);
290
291 switch (type) {
292 case P2P_SCAN_SOCIAL:
293 params.freqs = social_channels;
294 break;
295 case P2P_SCAN_FULL:
296 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 case P2P_SCAN_SOCIAL_PLUS_ONE:
298 social_channels[3] = freq;
299 params.freqs = social_channels;
300 break;
301 }
302
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304
305 wpabuf_free(ies);
306
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800307 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700308 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
309 if (ifs->scanning ||
310 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
311 wpa_s->global->p2p_cb_on_scan_complete = 1;
312 ret = 1;
313 break;
314 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800315 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700316 } else {
317 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700318 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700319 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 return ret;
322}
323
324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
326{
327 switch (p2p_group_interface) {
328 case P2P_GROUP_INTERFACE_PENDING:
329 return WPA_IF_P2P_GROUP;
330 case P2P_GROUP_INTERFACE_GO:
331 return WPA_IF_P2P_GO;
332 case P2P_GROUP_INTERFACE_CLIENT:
333 return WPA_IF_P2P_CLIENT;
334 }
335
336 return WPA_IF_P2P_GROUP;
337}
338
339
340static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
341 const u8 *ssid,
342 size_t ssid_len, int *go)
343{
344 struct wpa_ssid *s;
345
346 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
347 for (s = wpa_s->conf->ssid; s; s = s->next) {
348 if (s->disabled != 0 || !s->p2p_group ||
349 s->ssid_len != ssid_len ||
350 os_memcmp(ssid, s->ssid, ssid_len) != 0)
351 continue;
352 if (s->mode == WPAS_MODE_P2P_GO &&
353 s != wpa_s->current_ssid)
354 continue;
355 if (go)
356 *go = s->mode == WPAS_MODE_P2P_GO;
357 return wpa_s;
358 }
359 }
360
361 return NULL;
362}
363
364
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700365static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
366 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367{
368 struct wpa_ssid *ssid;
369 char *gtype;
370 const char *reason;
371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 ssid = wpa_s->current_ssid;
373 if (ssid == NULL) {
374 /*
375 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700376 * pending P2P group interface waiting for provisioning or a
377 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378 */
379 ssid = wpa_s->conf->ssid;
380 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700381 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 break;
383 ssid = ssid->next;
384 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300385 if (ssid == NULL &&
386 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
387 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700388 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
389 "not found");
390 return -1;
391 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 }
393 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
394 gtype = "GO";
395 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
396 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
397 wpa_s->reassociate = 0;
398 wpa_s->disconnected = 1;
399 wpa_supplicant_deauthenticate(wpa_s,
400 WLAN_REASON_DEAUTH_LEAVING);
401 gtype = "client";
402 } else
403 gtype = "GO";
404 if (wpa_s->cross_connect_in_use) {
405 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700406 wpa_msg_global(wpa_s->parent, MSG_INFO,
407 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
408 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700410 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700411 case P2P_GROUP_REMOVAL_REQUESTED:
412 reason = " reason=REQUESTED";
413 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700414 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
415 reason = " reason=FORMATION_FAILED";
416 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
418 reason = " reason=IDLE";
419 break;
420 case P2P_GROUP_REMOVAL_UNAVAILABLE:
421 reason = " reason=UNAVAILABLE";
422 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700423 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
424 reason = " reason=GO_ENDING_SESSION";
425 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700426 case P2P_GROUP_REMOVAL_PSK_FAILURE:
427 reason = " reason=PSK_FAILURE";
428 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 default:
430 reason = "";
431 break;
432 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700433 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700434 wpa_msg_global(wpa_s->parent, MSG_INFO,
435 P2P_EVENT_GROUP_REMOVED "%s %s%s",
436 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700437 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438
Dmitry Shmidt04949592012-07-19 12:16:46 -0700439 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
440 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800441 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700442 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800443 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
444 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700445 wpa_s->p2p_in_provisioning = 0;
446 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700447
Dmitry Shmidt96571392013-10-14 12:54:46 -0700448 /*
449 * Make sure wait for the first client does not remain active after the
450 * group has been removed.
451 */
452 wpa_s->global->p2p_go_wait_client.sec = 0;
453
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700454 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
456
457 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
458 struct wpa_global *global;
459 char *ifname;
460 enum wpa_driver_if_type type;
461 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
462 wpa_s->ifname);
463 global = wpa_s->global;
464 ifname = os_strdup(wpa_s->ifname);
465 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700466 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 wpa_s = global->ifaces;
468 if (wpa_s && ifname)
469 wpa_drv_if_remove(wpa_s, type, ifname);
470 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300471 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472 }
473
Dmitry Shmidt96571392013-10-14 12:54:46 -0700474 if (!wpa_s->p2p_go_group_formation_completed) {
475 wpa_s->global->p2p_group_formation = NULL;
476 wpa_s->p2p_in_provisioning = 0;
477 }
478
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800479 wpa_s->show_group_started = 0;
480 os_free(wpa_s->go_params);
481 wpa_s->go_params = NULL;
482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
484 if (ssid && (ssid->p2p_group ||
485 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
486 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
487 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700488 if (ssid == wpa_s->current_ssid) {
489 wpa_sm_set_config(wpa_s->wpa, NULL);
490 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700492 }
493 /*
494 * Networks objects created during any P2P activities are not
495 * exposed out as they might/will confuse certain non-P2P aware
496 * applications since these network objects won't behave like
497 * regular ones.
498 *
499 * Likewise, we don't send out network removed signals for such
500 * network objects.
501 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 wpa_config_remove_network(wpa_s->conf, id);
503 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800504 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700505 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506 } else {
507 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
508 "found");
509 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700510 if (wpa_s->ap_iface)
511 wpa_supplicant_ap_deinit(wpa_s);
512 else
513 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700514
515 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700516}
517
518
519static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
520 u8 *go_dev_addr,
521 const u8 *ssid, size_t ssid_len)
522{
523 struct wpa_bss *bss;
524 const u8 *bssid;
525 struct wpabuf *p2p;
526 u8 group_capab;
527 const u8 *addr;
528
529 if (wpa_s->go_params)
530 bssid = wpa_s->go_params->peer_interface_addr;
531 else
532 bssid = wpa_s->bssid;
533
534 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
535 if (bss == NULL) {
536 u8 iface_addr[ETH_ALEN];
537 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
538 iface_addr) == 0)
539 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
540 }
541 if (bss == NULL) {
542 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
543 "group is persistent - BSS " MACSTR " not found",
544 MAC2STR(bssid));
545 return 0;
546 }
547
548 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700549 if (p2p == NULL)
550 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
551 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552 if (p2p == NULL) {
553 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
554 "group is persistent - BSS " MACSTR
555 " did not include P2P IE", MAC2STR(bssid));
556 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
557 (u8 *) (bss + 1), bss->ie_len);
558 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
559 ((u8 *) bss + 1) + bss->ie_len,
560 bss->beacon_ie_len);
561 return 0;
562 }
563
564 group_capab = p2p_get_group_capab(p2p);
565 addr = p2p_get_go_dev_addr(p2p);
566 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
567 "group_capab=0x%x", group_capab);
568 if (addr) {
569 os_memcpy(go_dev_addr, addr, ETH_ALEN);
570 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
571 MAC2STR(addr));
572 } else
573 os_memset(go_dev_addr, 0, ETH_ALEN);
574 wpabuf_free(p2p);
575
576 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
577 "go_dev_addr=" MACSTR,
578 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
579
580 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
581}
582
583
Jouni Malinen75ecf522011-06-27 15:19:46 -0700584static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
585 struct wpa_ssid *ssid,
586 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587{
588 struct wpa_ssid *s;
589 int changed = 0;
590
591 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
592 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
593 for (s = wpa_s->conf->ssid; s; s = s->next) {
594 if (s->disabled == 2 &&
595 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
596 s->ssid_len == ssid->ssid_len &&
597 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
598 break;
599 }
600
601 if (s) {
602 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
603 "entry");
604 if (ssid->passphrase && !s->passphrase)
605 changed = 1;
606 else if (ssid->passphrase && s->passphrase &&
607 os_strcmp(ssid->passphrase, s->passphrase) != 0)
608 changed = 1;
609 } else {
610 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
611 "entry");
612 changed = 1;
613 s = wpa_config_add_network(wpa_s->conf);
614 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700615 return -1;
616
617 /*
618 * Instead of network_added we emit persistent_group_added
619 * notification. Also to keep the defense checks in
620 * persistent_group obj registration method, we set the
621 * relevant flags in s to designate it as a persistent group.
622 */
623 s->p2p_group = 1;
624 s->p2p_persistent_group = 1;
625 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626 wpa_config_set_network_defaults(s);
627 }
628
629 s->p2p_group = 1;
630 s->p2p_persistent_group = 1;
631 s->disabled = 2;
632 s->bssid_set = 1;
633 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
634 s->mode = ssid->mode;
635 s->auth_alg = WPA_AUTH_ALG_OPEN;
636 s->key_mgmt = WPA_KEY_MGMT_PSK;
637 s->proto = WPA_PROTO_RSN;
638 s->pairwise_cipher = WPA_CIPHER_CCMP;
639 s->export_keys = 1;
640 if (ssid->passphrase) {
641 os_free(s->passphrase);
642 s->passphrase = os_strdup(ssid->passphrase);
643 }
644 if (ssid->psk_set) {
645 s->psk_set = 1;
646 os_memcpy(s->psk, ssid->psk, 32);
647 }
648 if (s->passphrase && !s->psk_set)
649 wpa_config_update_psk(s);
650 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
651 os_free(s->ssid);
652 s->ssid = os_malloc(ssid->ssid_len);
653 }
654 if (s->ssid) {
655 s->ssid_len = ssid->ssid_len;
656 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
657 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700658 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
659 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
660 wpa_s->global->add_psk = NULL;
661 changed = 1;
662 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663
664#ifndef CONFIG_NO_CONFIG_WRITE
665 if (changed && wpa_s->conf->update_config &&
666 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
667 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
668 }
669#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700670
671 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672}
673
674
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800675static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
676 const u8 *addr)
677{
678 struct wpa_ssid *ssid, *s;
679 u8 *n;
680 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700681 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800682
683 ssid = wpa_s->current_ssid;
684 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
685 !ssid->p2p_persistent_group)
686 return;
687
688 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
689 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
690 continue;
691
692 if (s->ssid_len == ssid->ssid_len &&
693 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
694 break;
695 }
696
697 if (s == NULL)
698 return;
699
700 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
701 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700702 ETH_ALEN) != 0)
703 continue;
704
705 if (i == s->num_p2p_clients - 1)
706 return; /* already the most recent entry */
707
708 /* move the entry to mark it most recent */
709 os_memmove(s->p2p_client_list + i * ETH_ALEN,
710 s->p2p_client_list + (i + 1) * ETH_ALEN,
711 (s->num_p2p_clients - i - 1) * ETH_ALEN);
712 os_memcpy(s->p2p_client_list +
713 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
714 found = 1;
715 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800716 }
717
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700718 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
719 n = os_realloc_array(s->p2p_client_list,
720 s->num_p2p_clients + 1, ETH_ALEN);
721 if (n == NULL)
722 return;
723 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
724 s->p2p_client_list = n;
725 s->num_p2p_clients++;
726 } else if (!found) {
727 /* Not enough room for an additional entry - drop the oldest
728 * entry */
729 os_memmove(s->p2p_client_list,
730 s->p2p_client_list + ETH_ALEN,
731 (s->num_p2p_clients - 1) * ETH_ALEN);
732 os_memcpy(s->p2p_client_list +
733 (s->num_p2p_clients - 1) * ETH_ALEN,
734 addr, ETH_ALEN);
735 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800736
737#ifndef CONFIG_NO_CONFIG_WRITE
738 if (wpa_s->parent->conf->update_config &&
739 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
740 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
741#endif /* CONFIG_NO_CONFIG_WRITE */
742}
743
744
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700745static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
746 int success)
747{
748 struct wpa_ssid *ssid;
749 const char *ssid_txt;
750 int client;
751 int persistent;
752 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700753 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754
755 /*
756 * This callback is likely called for the main interface. Update wpa_s
757 * to use the group interface if a new interface was created for the
758 * group.
759 */
760 if (wpa_s->global->p2p_group_formation)
761 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700762 if (wpa_s->p2p_go_group_formation_completed) {
763 wpa_s->global->p2p_group_formation = NULL;
764 wpa_s->p2p_in_provisioning = 0;
765 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766
767 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700768 wpa_msg_global(wpa_s->parent, MSG_INFO,
769 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700770 wpas_p2p_group_delete(wpa_s,
771 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772 return;
773 }
774
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700775 wpa_msg_global(wpa_s->parent, MSG_INFO,
776 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777
778 ssid = wpa_s->current_ssid;
779 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
780 ssid->mode = WPAS_MODE_P2P_GO;
781 p2p_group_notif_formation_done(wpa_s->p2p_group);
782 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
783 }
784
785 persistent = 0;
786 if (ssid) {
787 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
788 client = ssid->mode == WPAS_MODE_INFRA;
789 if (ssid->mode == WPAS_MODE_P2P_GO) {
790 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700791 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
792 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793 } else
794 persistent = wpas_p2p_persistent_group(wpa_s,
795 go_dev_addr,
796 ssid->ssid,
797 ssid->ssid_len);
798 } else {
799 ssid_txt = "";
800 client = wpa_s->p2p_group_interface ==
801 P2P_GROUP_INTERFACE_CLIENT;
802 os_memset(go_dev_addr, 0, ETH_ALEN);
803 }
804
805 wpa_s->show_group_started = 0;
806 if (client) {
807 /*
808 * Indicate event only after successfully completed 4-way
809 * handshake, i.e., when the interface is ready for data
810 * packets.
811 */
812 wpa_s->show_group_started = 1;
813 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
814 char psk[65];
815 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700816 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
817 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
818 MACSTR "%s",
819 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
820 MAC2STR(go_dev_addr),
821 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 wpas_p2p_cross_connect_setup(wpa_s);
823 wpas_p2p_set_group_idle_timeout(wpa_s);
824 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700825 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
826 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
827 "go_dev_addr=" MACSTR "%s",
828 wpa_s->ifname, ssid_txt,
829 ssid ? ssid->frequency : 0,
830 ssid && ssid->passphrase ? ssid->passphrase : "",
831 MAC2STR(go_dev_addr),
832 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 wpas_p2p_cross_connect_setup(wpa_s);
834 wpas_p2p_set_group_idle_timeout(wpa_s);
835 }
836
837 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700838 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
839 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700840 else {
841 os_free(wpa_s->global->add_psk);
842 wpa_s->global->add_psk = NULL;
843 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800844 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700845 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700846 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700847 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700848 os_get_time(&wpa_s->global->p2p_go_wait_client);
849 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850}
851
852
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800853static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
854 unsigned int freq,
855 const u8 *dst, const u8 *src,
856 const u8 *bssid,
857 const u8 *data, size_t data_len,
858 enum offchannel_send_action_result
859 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800861 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
864 return;
865 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
866 return;
867
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800868 switch (result) {
869 case OFFCHANNEL_SEND_ACTION_SUCCESS:
870 res = P2P_SEND_ACTION_SUCCESS;
871 break;
872 case OFFCHANNEL_SEND_ACTION_NO_ACK:
873 res = P2P_SEND_ACTION_NO_ACK;
874 break;
875 case OFFCHANNEL_SEND_ACTION_FAILED:
876 res = P2P_SEND_ACTION_FAILED;
877 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 }
879
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800880 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800882 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
883 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800884 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800885 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
886 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800888 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
889 "during p2p_connect-auto");
890 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
891 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 }
893}
894
895
896static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
897 const u8 *src, const u8 *bssid, const u8 *buf,
898 size_t len, unsigned int wait_time)
899{
900 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800901 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
902 wait_time,
903 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904}
905
906
907static void wpas_send_action_done(void *ctx)
908{
909 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800910 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911}
912
913
914static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
915 struct p2p_go_neg_results *params)
916{
917 if (wpa_s->go_params == NULL) {
918 wpa_s->go_params = os_malloc(sizeof(*params));
919 if (wpa_s->go_params == NULL)
920 return -1;
921 }
922 os_memcpy(wpa_s->go_params, params, sizeof(*params));
923 return 0;
924}
925
926
927static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
928 struct p2p_go_neg_results *res)
929{
930 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
931 MAC2STR(res->peer_interface_addr));
932 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
933 res->ssid, res->ssid_len);
934 wpa_supplicant_ap_deinit(wpa_s);
935 wpas_copy_go_neg_results(wpa_s, res);
936 if (res->wps_method == WPS_PBC)
937 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
938 else {
939 u16 dev_pw_id = DEV_PW_DEFAULT;
940 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
941 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
942 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
943 wpa_s->p2p_pin, 1, dev_pw_id);
944 }
945}
946
947
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700948static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
949 struct wpa_ssid *ssid)
950{
951 struct wpa_ssid *persistent;
952 struct psk_list_entry *psk;
953 struct hostapd_data *hapd;
954
955 if (!wpa_s->ap_iface)
956 return;
957
958 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
959 ssid->ssid_len);
960 if (persistent == NULL)
961 return;
962
963 hapd = wpa_s->ap_iface->bss[0];
964
965 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
966 list) {
967 struct hostapd_wpa_psk *hpsk;
968
969 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
970 MACSTR " psk=%d",
971 MAC2STR(psk->addr), psk->p2p);
972 hpsk = os_zalloc(sizeof(*hpsk));
973 if (hpsk == NULL)
974 break;
975 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
976 if (psk->p2p)
977 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
978 else
979 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
980 hpsk->next = hapd->conf->ssid.wpa_psk;
981 hapd->conf->ssid.wpa_psk = hpsk;
982 }
983}
984
985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986static void p2p_go_configured(void *ctx, void *data)
987{
988 struct wpa_supplicant *wpa_s = ctx;
989 struct p2p_go_neg_results *params = data;
990 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700991 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992
993 ssid = wpa_s->current_ssid;
994 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
995 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
996 if (wpa_s->global->p2p_group_formation == wpa_s)
997 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800998 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700999 wpa_msg_global(wpa_s->parent, MSG_INFO,
1000 P2P_EVENT_GROUP_STARTED
1001 "%s GO ssid=\"%s\" freq=%d "
1002 "passphrase=\"%s\" go_dev_addr=" MACSTR
1003 "%s", wpa_s->ifname,
1004 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1005 ssid->frequency, params->passphrase,
1006 MAC2STR(wpa_s->global->p2p_dev_addr),
1007 params->persistent_group ?
1008 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001009 } else {
1010 char psk[65];
1011 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1012 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001013 wpa_msg_global(wpa_s->parent, MSG_INFO,
1014 P2P_EVENT_GROUP_STARTED
1015 "%s GO ssid=\"%s\" freq=%d psk=%s "
1016 "go_dev_addr=" MACSTR "%s",
1017 wpa_s->ifname,
1018 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1019 ssid->frequency, psk,
1020 MAC2STR(wpa_s->global->p2p_dev_addr),
1021 params->persistent_group ?
1022 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001023 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001024
Dmitry Shmidt56052862013-10-04 10:23:25 -07001025 os_get_time(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001026 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001027 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001029 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001030 wpas_p2p_add_psk_list(wpa_s, ssid);
1031 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001032 if (network_id < 0)
1033 network_id = ssid->id;
1034 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 wpas_p2p_cross_connect_setup(wpa_s);
1036 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001037
1038 if (wpa_s->p2p_first_connection_timeout) {
1039 wpa_dbg(wpa_s, MSG_DEBUG,
1040 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1041 wpa_s->p2p_first_connection_timeout);
1042 wpa_s->p2p_go_group_formation_completed = 0;
1043 wpa_s->global->p2p_group_formation = wpa_s;
1044 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1045 wpa_s->parent, NULL);
1046 eloop_register_timeout(
1047 wpa_s->p2p_first_connection_timeout, 0,
1048 wpas_p2p_group_formation_timeout,
1049 wpa_s->parent, NULL);
1050 }
1051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 return;
1053 }
1054
1055 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1056 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1057 params->peer_interface_addr)) {
1058 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1059 "filtering");
1060 return;
1061 }
1062 if (params->wps_method == WPS_PBC)
1063 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001064 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 else if (wpa_s->p2p_pin[0])
1066 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001067 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 os_free(wpa_s->go_params);
1069 wpa_s->go_params = NULL;
1070}
1071
1072
1073static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1074 struct p2p_go_neg_results *params,
1075 int group_formation)
1076{
1077 struct wpa_ssid *ssid;
1078
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001079 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1080 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1081 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1082 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001084 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085
1086 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001087 if (ssid == NULL) {
1088 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001090 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001092 wpa_s->show_group_started = 0;
1093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 wpa_config_set_network_defaults(ssid);
1095 ssid->temporary = 1;
1096 ssid->p2p_group = 1;
1097 ssid->p2p_persistent_group = params->persistent_group;
1098 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1099 WPAS_MODE_P2P_GO;
1100 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001101 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001102 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 ssid->ssid = os_zalloc(params->ssid_len + 1);
1104 if (ssid->ssid) {
1105 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1106 ssid->ssid_len = params->ssid_len;
1107 }
1108 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1109 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1110 ssid->proto = WPA_PROTO_RSN;
1111 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001112 if (os_strlen(params->passphrase) > 0) {
1113 ssid->passphrase = os_strdup(params->passphrase);
1114 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001115 wpa_msg_global(wpa_s, MSG_ERROR,
1116 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001117 wpa_config_remove_network(wpa_s->conf, ssid->id);
1118 return;
1119 }
1120 } else
1121 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001122 ssid->psk_set = params->psk_set;
1123 if (ssid->psk_set)
1124 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001125 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001126 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001127 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128
1129 wpa_s->ap_configured_cb = p2p_go_configured;
1130 wpa_s->ap_configured_cb_ctx = wpa_s;
1131 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001132 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 wpa_s->reassociate = 1;
1134 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001135 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1136 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 wpa_supplicant_req_scan(wpa_s, 0, 0);
1138}
1139
1140
1141static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1142 const struct wpa_supplicant *src)
1143{
1144 struct wpa_config *d;
1145 const struct wpa_config *s;
1146
1147 d = dst->conf;
1148 s = src->conf;
1149
1150#define C(n) if (s->n) d->n = os_strdup(s->n)
1151 C(device_name);
1152 C(manufacturer);
1153 C(model_name);
1154 C(model_number);
1155 C(serial_number);
1156 C(config_methods);
1157#undef C
1158
1159 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1160 os_memcpy(d->sec_device_type, s->sec_device_type,
1161 sizeof(d->sec_device_type));
1162 d->num_sec_device_types = s->num_sec_device_types;
1163
1164 d->p2p_group_idle = s->p2p_group_idle;
1165 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001166 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001167 d->max_num_sta = s->max_num_sta;
1168 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001169 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001170 d->beacon_int = s->beacon_int;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001171 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001172 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173}
1174
1175
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001176static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1177 char *ifname, size_t len)
1178{
1179 char *ifname_ptr = wpa_s->ifname;
1180
1181 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1182 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1183 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1184 }
1185
1186 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1187 if (os_strlen(ifname) >= IFNAMSIZ &&
1188 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1189 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001190 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001191 }
1192}
1193
1194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1196 enum wpa_driver_if_type type)
1197{
1198 char ifname[120], force_ifname[120];
1199
1200 if (wpa_s->pending_interface_name[0]) {
1201 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1202 "- skip creation of a new one");
1203 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1204 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1205 "unknown?! ifname='%s'",
1206 wpa_s->pending_interface_name);
1207 return -1;
1208 }
1209 return 0;
1210 }
1211
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001212 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 force_ifname[0] = '\0';
1214
1215 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1216 ifname);
1217 wpa_s->p2p_group_idx++;
1218
1219 wpa_s->pending_interface_type = type;
1220 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1221 wpa_s->pending_interface_addr, NULL) < 0) {
1222 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1223 "interface");
1224 return -1;
1225 }
1226
1227 if (force_ifname[0]) {
1228 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1229 force_ifname);
1230 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1231 sizeof(wpa_s->pending_interface_name));
1232 } else
1233 os_strlcpy(wpa_s->pending_interface_name, ifname,
1234 sizeof(wpa_s->pending_interface_name));
1235 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1236 MACSTR, wpa_s->pending_interface_name,
1237 MAC2STR(wpa_s->pending_interface_addr));
1238
1239 return 0;
1240}
1241
1242
1243static void wpas_p2p_remove_pending_group_interface(
1244 struct wpa_supplicant *wpa_s)
1245{
1246 if (!wpa_s->pending_interface_name[0] ||
1247 is_zero_ether_addr(wpa_s->pending_interface_addr))
1248 return; /* No pending virtual interface */
1249
1250 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1251 wpa_s->pending_interface_name);
1252 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1253 wpa_s->pending_interface_name);
1254 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1255 wpa_s->pending_interface_name[0] = '\0';
1256}
1257
1258
1259static struct wpa_supplicant *
1260wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1261{
1262 struct wpa_interface iface;
1263 struct wpa_supplicant *group_wpa_s;
1264
1265 if (!wpa_s->pending_interface_name[0]) {
1266 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1267 if (!wpas_p2p_create_iface(wpa_s))
1268 return NULL;
1269 /*
1270 * Something has forced us to remove the pending interface; try
1271 * to create a new one and hope for the best that we will get
1272 * the same local address.
1273 */
1274 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1275 WPA_IF_P2P_CLIENT) < 0)
1276 return NULL;
1277 }
1278
1279 os_memset(&iface, 0, sizeof(iface));
1280 iface.ifname = wpa_s->pending_interface_name;
1281 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001282 if (wpa_s->conf->ctrl_interface == NULL &&
1283 wpa_s->parent != wpa_s &&
1284 wpa_s->p2p_mgmt &&
1285 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1286 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1287 else
1288 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 iface.driver_param = wpa_s->conf->driver_param;
1290 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1291 if (group_wpa_s == NULL) {
1292 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1293 "wpa_supplicant interface");
1294 return NULL;
1295 }
1296 wpa_s->pending_interface_name[0] = '\0';
1297 group_wpa_s->parent = wpa_s;
1298 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1299 P2P_GROUP_INTERFACE_CLIENT;
1300 wpa_s->global->p2p_group_formation = group_wpa_s;
1301
1302 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1303
1304 return group_wpa_s;
1305}
1306
1307
1308static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1309 void *timeout_ctx)
1310{
1311 struct wpa_supplicant *wpa_s = eloop_ctx;
1312 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001313 wpas_p2p_group_formation_failed(wpa_s);
1314}
1315
1316
1317void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1318{
1319 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1320 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 if (wpa_s->global->p2p)
1322 p2p_group_formation_failed(wpa_s->global->p2p);
1323 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1324 wpa_drv_p2p_group_formation_failed(wpa_s);
1325 wpas_group_formation_completed(wpa_s, 0);
1326}
1327
1328
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001329void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1330{
1331 if (wpa_s->global->p2p_group_formation != wpa_s)
1332 return;
1333 /* Speed up group formation timeout since this cannot succeed */
1334 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1335 wpa_s->parent, NULL);
1336 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1337 wpa_s->parent, NULL);
1338}
1339
1340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1342{
1343 struct wpa_supplicant *wpa_s = ctx;
1344
1345 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1346 wpa_drv_cancel_remain_on_channel(wpa_s);
1347 wpa_s->off_channel_freq = 0;
1348 wpa_s->roc_waiting_drv_freq = 0;
1349 }
1350
1351 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001352 wpa_msg_global(wpa_s, MSG_INFO,
1353 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1354 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001355 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 wpas_p2p_remove_pending_group_interface(wpa_s);
1357 return;
1358 }
1359
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001360 if (wpa_s->p2p_go_ht40)
1361 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001362 if (wpa_s->p2p_go_vht)
1363 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001364
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001365 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1366 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1367 " wps_method=%s",
1368 res->role_go ? "GO" : "client", res->freq, res->ht40,
1369 MAC2STR(res->peer_device_addr),
1370 MAC2STR(res->peer_interface_addr),
1371 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001372 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373
Dmitry Shmidt04949592012-07-19 12:16:46 -07001374 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1375 struct wpa_ssid *ssid;
1376 ssid = wpa_config_get_network(wpa_s->conf,
1377 wpa_s->p2p_persistent_id);
1378 if (ssid && ssid->disabled == 2 &&
1379 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1380 size_t len = os_strlen(ssid->passphrase);
1381 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1382 "on requested persistent group");
1383 os_memcpy(res->passphrase, ssid->passphrase, len);
1384 res->passphrase[len] = '\0';
1385 }
1386 }
1387
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 if (wpa_s->create_p2p_iface) {
1389 struct wpa_supplicant *group_wpa_s =
1390 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1391 if (group_wpa_s == NULL) {
1392 wpas_p2p_remove_pending_group_interface(wpa_s);
1393 return;
1394 }
1395 if (group_wpa_s != wpa_s) {
1396 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1397 sizeof(group_wpa_s->p2p_pin));
1398 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1399 }
1400 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1401 wpa_s->pending_interface_name[0] = '\0';
1402 group_wpa_s->p2p_in_provisioning = 1;
1403
1404 if (res->role_go)
1405 wpas_start_wps_go(group_wpa_s, res, 1);
1406 else
1407 wpas_start_wps_enrollee(group_wpa_s, res);
1408 } else {
1409 wpa_s->p2p_in_provisioning = 1;
1410 wpa_s->global->p2p_group_formation = wpa_s;
1411
1412 if (res->role_go)
1413 wpas_start_wps_go(wpa_s, res, 1);
1414 else
1415 wpas_start_wps_enrollee(ctx, res);
1416 }
1417
1418 wpa_s->p2p_long_listen = 0;
1419 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1420
1421 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1422 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1423 (res->peer_config_timeout % 100) * 10000,
1424 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1425}
1426
1427
1428void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1429{
1430 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001431 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1432 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433
1434 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1435}
1436
1437
1438void wpas_dev_found(void *ctx, const u8 *addr,
1439 const struct p2p_peer_info *info,
1440 int new_device)
1441{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001442#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 struct wpa_supplicant *wpa_s = ctx;
1444 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001445#define WFD_DEV_INFO_SIZE 9
1446 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001447 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001448#ifdef CONFIG_WIFI_DISPLAY
1449 if (info->wfd_subelems) {
1450 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1451 wpabuf_head(info->wfd_subelems),
1452 WFD_DEV_INFO_SIZE);
1453 }
1454#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001455 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1456 " p2p_dev_addr=" MACSTR
1457 " pri_dev_type=%s name='%s' config_methods=0x%x "
1458 "dev_capab=0x%x group_capab=0x%x%s%s",
1459 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1460 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1461 sizeof(devtype)),
1462 info->device_name, info->config_methods,
1463 info->dev_capab, info->group_capab,
1464 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001465#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466
1467 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1468}
1469
1470
1471static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1472{
1473 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001474
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001475 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1476 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1479}
1480
1481
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001482static void wpas_find_stopped(void *ctx)
1483{
1484 struct wpa_supplicant *wpa_s = ctx;
1485 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1486}
1487
1488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489static int wpas_start_listen(void *ctx, unsigned int freq,
1490 unsigned int duration,
1491 const struct wpabuf *probe_resp_ie)
1492{
1493 struct wpa_supplicant *wpa_s = ctx;
1494
1495 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1496
1497 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1498 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1499 "report received Probe Request frames");
1500 return -1;
1501 }
1502
1503 wpa_s->pending_listen_freq = freq;
1504 wpa_s->pending_listen_duration = duration;
1505
1506 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1507 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1508 "to remain on channel (%u MHz) for Listen "
1509 "state", freq);
1510 wpa_s->pending_listen_freq = 0;
1511 return -1;
1512 }
1513 wpa_s->off_channel_freq = 0;
1514 wpa_s->roc_waiting_drv_freq = freq;
1515
1516 return 0;
1517}
1518
1519
1520static void wpas_stop_listen(void *ctx)
1521{
1522 struct wpa_supplicant *wpa_s = ctx;
1523 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1524 wpa_drv_cancel_remain_on_channel(wpa_s);
1525 wpa_s->off_channel_freq = 0;
1526 wpa_s->roc_waiting_drv_freq = 0;
1527 }
1528 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1529 wpa_drv_probe_req_report(wpa_s, 0);
1530}
1531
1532
1533static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1534{
1535 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001536 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537}
1538
1539
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001540/*
1541 * DNS Header section is used only to calculate compression pointers, so the
1542 * contents of this data does not matter, but the length needs to be reserved
1543 * in the virtual packet.
1544 */
1545#define DNS_HEADER_LEN 12
1546
1547/*
1548 * 27-octet in-memory packet from P2P specification containing two implied
1549 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1550 */
1551#define P2P_SD_IN_MEMORY_LEN 27
1552
1553static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1554 u8 **spos, const u8 *end)
1555{
1556 while (*spos < end) {
1557 u8 val = ((*spos)[0] & 0xc0) >> 6;
1558 int len;
1559
1560 if (val == 1 || val == 2) {
1561 /* These are reserved values in RFC 1035 */
1562 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1563 "sequence starting with 0x%x", val);
1564 return -1;
1565 }
1566
1567 if (val == 3) {
1568 u16 offset;
1569 u8 *spos_tmp;
1570
1571 /* Offset */
1572 if (*spos + 2 > end) {
1573 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1574 "DNS offset field");
1575 return -1;
1576 }
1577
1578 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1579 if (offset >= *spos - start) {
1580 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1581 "pointer offset %u", offset);
1582 return -1;
1583 }
1584
1585 (*spos) += 2;
1586 spos_tmp = start + offset;
1587 return p2p_sd_dns_uncompress_label(upos, uend, start,
1588 &spos_tmp,
1589 *spos - 2);
1590 }
1591
1592 /* Label */
1593 len = (*spos)[0] & 0x3f;
1594 if (len == 0)
1595 return 0;
1596
1597 (*spos)++;
1598 if (*spos + len > end) {
1599 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1600 "sequence - no room for label with length "
1601 "%u", len);
1602 return -1;
1603 }
1604
1605 if (*upos + len + 2 > uend)
1606 return -2;
1607
1608 os_memcpy(*upos, *spos, len);
1609 *spos += len;
1610 *upos += len;
1611 (*upos)[0] = '.';
1612 (*upos)++;
1613 (*upos)[0] = '\0';
1614 }
1615
1616 return 0;
1617}
1618
1619
1620/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1621 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1622 * not large enough */
1623static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1624 size_t msg_len, size_t offset)
1625{
1626 /* 27-octet in-memory packet from P2P specification */
1627 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1628 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1629 u8 *tmp, *end, *spos;
1630 char *upos, *uend;
1631 int ret = 0;
1632
1633 if (buf_len < 2)
1634 return -1;
1635 if (offset > msg_len)
1636 return -1;
1637
1638 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1639 if (tmp == NULL)
1640 return -1;
1641 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1642 end = spos + msg_len;
1643 spos += offset;
1644
1645 os_memset(tmp, 0, DNS_HEADER_LEN);
1646 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1647 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1648
1649 upos = buf;
1650 uend = buf + buf_len;
1651
1652 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1653 if (ret) {
1654 os_free(tmp);
1655 return ret;
1656 }
1657
1658 if (upos == buf) {
1659 upos[0] = '.';
1660 upos[1] = '\0';
1661 } else if (upos[-1] == '.')
1662 upos[-1] = '\0';
1663
1664 os_free(tmp);
1665 return 0;
1666}
1667
1668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669static struct p2p_srv_bonjour *
1670wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1671 const struct wpabuf *query)
1672{
1673 struct p2p_srv_bonjour *bsrv;
1674 size_t len;
1675
1676 len = wpabuf_len(query);
1677 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1678 struct p2p_srv_bonjour, list) {
1679 if (len == wpabuf_len(bsrv->query) &&
1680 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1681 len) == 0)
1682 return bsrv;
1683 }
1684 return NULL;
1685}
1686
1687
1688static struct p2p_srv_upnp *
1689wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1690 const char *service)
1691{
1692 struct p2p_srv_upnp *usrv;
1693
1694 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1695 struct p2p_srv_upnp, list) {
1696 if (version == usrv->version &&
1697 os_strcmp(service, usrv->service) == 0)
1698 return usrv;
1699 }
1700 return NULL;
1701}
1702
1703
1704static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1705 u8 srv_trans_id)
1706{
1707 u8 *len_pos;
1708
1709 if (wpabuf_tailroom(resp) < 5)
1710 return;
1711
1712 /* Length (to be filled) */
1713 len_pos = wpabuf_put(resp, 2);
1714 wpabuf_put_u8(resp, srv_proto);
1715 wpabuf_put_u8(resp, srv_trans_id);
1716 /* Status Code */
1717 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1718 /* Response Data: empty */
1719 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1720}
1721
1722
1723static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1724 struct wpabuf *resp, u8 srv_trans_id)
1725{
1726 struct p2p_srv_bonjour *bsrv;
1727 u8 *len_pos;
1728
1729 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1730
1731 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1732 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1733 return;
1734 }
1735
1736 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1737 struct p2p_srv_bonjour, list) {
1738 if (wpabuf_tailroom(resp) <
1739 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1740 return;
1741 /* Length (to be filled) */
1742 len_pos = wpabuf_put(resp, 2);
1743 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1744 wpabuf_put_u8(resp, srv_trans_id);
1745 /* Status Code */
1746 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1747 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1748 wpabuf_head(bsrv->resp),
1749 wpabuf_len(bsrv->resp));
1750 /* Response Data */
1751 wpabuf_put_buf(resp, bsrv->query); /* Key */
1752 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1753 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1754 2);
1755 }
1756}
1757
1758
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001759static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1760 size_t query_len)
1761{
1762 char str_rx[256], str_srv[256];
1763
1764 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1765 return 0; /* Too short to include DNS Type and Version */
1766 if (os_memcmp(query + query_len - 3,
1767 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1768 3) != 0)
1769 return 0; /* Mismatch in DNS Type or Version */
1770 if (query_len == wpabuf_len(bsrv->query) &&
1771 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1772 return 1; /* Binary match */
1773
1774 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1775 0))
1776 return 0; /* Failed to uncompress query */
1777 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1778 wpabuf_head(bsrv->query),
1779 wpabuf_len(bsrv->query) - 3, 0))
1780 return 0; /* Failed to uncompress service */
1781
1782 return os_strcmp(str_rx, str_srv) == 0;
1783}
1784
1785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1787 struct wpabuf *resp, u8 srv_trans_id,
1788 const u8 *query, size_t query_len)
1789{
1790 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001792 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001793
1794 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1795 query, query_len);
1796 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1797 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1798 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1799 srv_trans_id);
1800 return;
1801 }
1802
1803 if (query_len == 0) {
1804 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1805 return;
1806 }
1807
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001808 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1809 struct p2p_srv_bonjour, list) {
1810 if (!match_bonjour_query(bsrv, query, query_len))
1811 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001812
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001813 if (wpabuf_tailroom(resp) <
1814 5 + query_len + wpabuf_len(bsrv->resp))
1815 return;
1816
1817 matches++;
1818
1819 /* Length (to be filled) */
1820 len_pos = wpabuf_put(resp, 2);
1821 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1822 wpabuf_put_u8(resp, srv_trans_id);
1823
1824 /* Status Code */
1825 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1826 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1827 wpabuf_head(bsrv->resp),
1828 wpabuf_len(bsrv->resp));
1829
1830 /* Response Data */
1831 wpabuf_put_data(resp, query, query_len); /* Key */
1832 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1833
1834 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1835 }
1836
1837 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1839 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001840 if (wpabuf_tailroom(resp) < 5)
1841 return;
1842
1843 /* Length (to be filled) */
1844 len_pos = wpabuf_put(resp, 2);
1845 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1846 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847
1848 /* Status Code */
1849 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1850 /* Response Data: empty */
1851 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1852 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854}
1855
1856
1857static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1858 struct wpabuf *resp, u8 srv_trans_id)
1859{
1860 struct p2p_srv_upnp *usrv;
1861 u8 *len_pos;
1862
1863 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1864
1865 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1866 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1867 return;
1868 }
1869
1870 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1871 struct p2p_srv_upnp, list) {
1872 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1873 return;
1874
1875 /* Length (to be filled) */
1876 len_pos = wpabuf_put(resp, 2);
1877 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1878 wpabuf_put_u8(resp, srv_trans_id);
1879
1880 /* Status Code */
1881 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1882 /* Response Data */
1883 wpabuf_put_u8(resp, usrv->version);
1884 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1885 usrv->service);
1886 wpabuf_put_str(resp, usrv->service);
1887 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1888 2);
1889 }
1890}
1891
1892
1893static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1894 struct wpabuf *resp, u8 srv_trans_id,
1895 const u8 *query, size_t query_len)
1896{
1897 struct p2p_srv_upnp *usrv;
1898 u8 *len_pos;
1899 u8 version;
1900 char *str;
1901 int count = 0;
1902
1903 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1904 query, query_len);
1905
1906 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1907 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1908 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1909 srv_trans_id);
1910 return;
1911 }
1912
1913 if (query_len == 0) {
1914 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1915 return;
1916 }
1917
1918 if (wpabuf_tailroom(resp) < 5)
1919 return;
1920
1921 /* Length (to be filled) */
1922 len_pos = wpabuf_put(resp, 2);
1923 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1924 wpabuf_put_u8(resp, srv_trans_id);
1925
1926 version = query[0];
1927 str = os_malloc(query_len);
1928 if (str == NULL)
1929 return;
1930 os_memcpy(str, query + 1, query_len - 1);
1931 str[query_len - 1] = '\0';
1932
1933 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1934 struct p2p_srv_upnp, list) {
1935 if (version != usrv->version)
1936 continue;
1937
1938 if (os_strcmp(str, "ssdp:all") != 0 &&
1939 os_strstr(usrv->service, str) == NULL)
1940 continue;
1941
1942 if (wpabuf_tailroom(resp) < 2)
1943 break;
1944 if (count == 0) {
1945 /* Status Code */
1946 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1947 /* Response Data */
1948 wpabuf_put_u8(resp, version);
1949 } else
1950 wpabuf_put_u8(resp, ',');
1951
1952 count++;
1953
1954 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1955 usrv->service);
1956 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1957 break;
1958 wpabuf_put_str(resp, usrv->service);
1959 }
1960 os_free(str);
1961
1962 if (count == 0) {
1963 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1964 "available");
1965 /* Status Code */
1966 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1967 /* Response Data: empty */
1968 }
1969
1970 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1971}
1972
1973
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001974#ifdef CONFIG_WIFI_DISPLAY
1975static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1976 struct wpabuf *resp, u8 srv_trans_id,
1977 const u8 *query, size_t query_len)
1978{
1979 const u8 *pos;
1980 u8 role;
1981 u8 *len_pos;
1982
1983 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1984
1985 if (!wpa_s->global->wifi_display) {
1986 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1987 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1988 srv_trans_id);
1989 return;
1990 }
1991
1992 if (query_len < 1) {
1993 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1994 "Role");
1995 return;
1996 }
1997
1998 if (wpabuf_tailroom(resp) < 5)
1999 return;
2000
2001 pos = query;
2002 role = *pos++;
2003 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2004
2005 /* TODO: role specific handling */
2006
2007 /* Length (to be filled) */
2008 len_pos = wpabuf_put(resp, 2);
2009 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2010 wpabuf_put_u8(resp, srv_trans_id);
2011 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2012
2013 while (pos < query + query_len) {
2014 if (*pos < MAX_WFD_SUBELEMS &&
2015 wpa_s->global->wfd_subelem[*pos] &&
2016 wpabuf_tailroom(resp) >=
2017 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2018 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2019 "subelement %u", *pos);
2020 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2021 }
2022 pos++;
2023 }
2024
2025 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2026}
2027#endif /* CONFIG_WIFI_DISPLAY */
2028
2029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2031 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2032{
2033 struct wpa_supplicant *wpa_s = ctx;
2034 const u8 *pos = tlvs;
2035 const u8 *end = tlvs + tlvs_len;
2036 const u8 *tlv_end;
2037 u16 slen;
2038 struct wpabuf *resp;
2039 u8 srv_proto, srv_trans_id;
2040 size_t buf_len;
2041 char *buf;
2042
2043 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2044 tlvs, tlvs_len);
2045 buf_len = 2 * tlvs_len + 1;
2046 buf = os_malloc(buf_len);
2047 if (buf) {
2048 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2049 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2050 MACSTR " %u %u %s",
2051 freq, MAC2STR(sa), dialog_token, update_indic,
2052 buf);
2053 os_free(buf);
2054 }
2055
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002056 if (wpa_s->p2p_sd_over_ctrl_iface) {
2057 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2058 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002060 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002061
2062 resp = wpabuf_alloc(10000);
2063 if (resp == NULL)
2064 return;
2065
2066 while (pos + 1 < end) {
2067 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2068 slen = WPA_GET_LE16(pos);
2069 pos += 2;
2070 if (pos + slen > end || slen < 2) {
2071 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2072 "length");
2073 wpabuf_free(resp);
2074 return;
2075 }
2076 tlv_end = pos + slen;
2077
2078 srv_proto = *pos++;
2079 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2080 srv_proto);
2081 srv_trans_id = *pos++;
2082 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2083 srv_trans_id);
2084
2085 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2086 pos, tlv_end - pos);
2087
2088
2089 if (wpa_s->force_long_sd) {
2090 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2091 "response");
2092 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2093 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2094 goto done;
2095 }
2096
2097 switch (srv_proto) {
2098 case P2P_SERV_ALL_SERVICES:
2099 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2100 "for all services");
2101 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2102 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2103 wpa_printf(MSG_DEBUG, "P2P: No service "
2104 "discovery protocols available");
2105 wpas_sd_add_proto_not_avail(
2106 resp, P2P_SERV_ALL_SERVICES,
2107 srv_trans_id);
2108 break;
2109 }
2110 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2111 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2112 break;
2113 case P2P_SERV_BONJOUR:
2114 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2115 pos, tlv_end - pos);
2116 break;
2117 case P2P_SERV_UPNP:
2118 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2119 pos, tlv_end - pos);
2120 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002121#ifdef CONFIG_WIFI_DISPLAY
2122 case P2P_SERV_WIFI_DISPLAY:
2123 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2124 pos, tlv_end - pos);
2125 break;
2126#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127 default:
2128 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2129 "protocol %u", srv_proto);
2130 wpas_sd_add_proto_not_avail(resp, srv_proto,
2131 srv_trans_id);
2132 break;
2133 }
2134
2135 pos = tlv_end;
2136 }
2137
2138done:
2139 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2140 update_indic, tlvs, tlvs_len);
2141
2142 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2143
2144 wpabuf_free(resp);
2145}
2146
2147
2148void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2149 const u8 *tlvs, size_t tlvs_len)
2150{
2151 struct wpa_supplicant *wpa_s = ctx;
2152 const u8 *pos = tlvs;
2153 const u8 *end = tlvs + tlvs_len;
2154 const u8 *tlv_end;
2155 u16 slen;
2156 size_t buf_len;
2157 char *buf;
2158
2159 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2160 tlvs, tlvs_len);
2161 if (tlvs_len > 1500) {
2162 /* TODO: better way for handling this */
2163 wpa_msg_ctrl(wpa_s, MSG_INFO,
2164 P2P_EVENT_SERV_DISC_RESP MACSTR
2165 " %u <long response: %u bytes>",
2166 MAC2STR(sa), update_indic,
2167 (unsigned int) tlvs_len);
2168 } else {
2169 buf_len = 2 * tlvs_len + 1;
2170 buf = os_malloc(buf_len);
2171 if (buf) {
2172 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2173 wpa_msg_ctrl(wpa_s, MSG_INFO,
2174 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2175 MAC2STR(sa), update_indic, buf);
2176 os_free(buf);
2177 }
2178 }
2179
2180 while (pos < end) {
2181 u8 srv_proto, srv_trans_id, status;
2182
2183 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2184 slen = WPA_GET_LE16(pos);
2185 pos += 2;
2186 if (pos + slen > end || slen < 3) {
2187 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2188 "length");
2189 return;
2190 }
2191 tlv_end = pos + slen;
2192
2193 srv_proto = *pos++;
2194 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2195 srv_proto);
2196 srv_trans_id = *pos++;
2197 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2198 srv_trans_id);
2199 status = *pos++;
2200 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2201 status);
2202
2203 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2204 pos, tlv_end - pos);
2205
2206 pos = tlv_end;
2207 }
2208
2209 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2210}
2211
2212
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002213u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2214 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215{
2216 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002217 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002218 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002219 return 0;
2220 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002221}
2222
2223
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002224u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2225 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002226{
2227 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002228 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229
2230 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2231 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002232 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2234 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2235 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2236 wpabuf_put_u8(tlvs, version);
2237 wpabuf_put_str(tlvs, query);
2238 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2239 wpabuf_free(tlvs);
2240 return ret;
2241}
2242
2243
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002244#ifdef CONFIG_WIFI_DISPLAY
2245
2246static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2247 const struct wpabuf *tlvs)
2248{
2249 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2250 return 0;
2251 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2252 return 0;
2253 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2254}
2255
2256
2257#define MAX_WFD_SD_SUBELEMS 20
2258
2259static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2260 const char *subelems)
2261{
2262 u8 *len;
2263 const char *pos;
2264 int val;
2265 int count = 0;
2266
2267 len = wpabuf_put(tlvs, 2);
2268 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2269 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2270
2271 wpabuf_put_u8(tlvs, role);
2272
2273 pos = subelems;
2274 while (*pos) {
2275 val = atoi(pos);
2276 if (val >= 0 && val < 256) {
2277 wpabuf_put_u8(tlvs, val);
2278 count++;
2279 if (count == MAX_WFD_SD_SUBELEMS)
2280 break;
2281 }
2282 pos = os_strchr(pos + 1, ',');
2283 if (pos == NULL)
2284 break;
2285 pos++;
2286 }
2287
2288 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2289}
2290
2291
2292u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2293 const u8 *dst, const char *role)
2294{
2295 struct wpabuf *tlvs;
2296 u64 ret;
2297 const char *subelems;
2298 u8 id = 1;
2299
2300 subelems = os_strchr(role, ' ');
2301 if (subelems == NULL)
2302 return 0;
2303 subelems++;
2304
2305 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2306 if (tlvs == NULL)
2307 return 0;
2308
2309 if (os_strstr(role, "[source]"))
2310 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2311 if (os_strstr(role, "[pri-sink]"))
2312 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2313 if (os_strstr(role, "[sec-sink]"))
2314 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2315 if (os_strstr(role, "[source+sink]"))
2316 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2317
2318 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2319 wpabuf_free(tlvs);
2320 return ret;
2321}
2322
2323#endif /* CONFIG_WIFI_DISPLAY */
2324
2325
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002326int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327{
2328 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002329 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002330 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2331 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002332 return p2p_sd_cancel_request(wpa_s->global->p2p,
2333 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334}
2335
2336
2337void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2338 const u8 *dst, u8 dialog_token,
2339 const struct wpabuf *resp_tlvs)
2340{
2341 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2342 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2343 resp_tlvs);
2344 return;
2345 }
2346 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2347 return;
2348 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2349 resp_tlvs);
2350}
2351
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2354{
2355 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2356 wpa_drv_p2p_service_update(wpa_s);
2357 return;
2358 }
2359 if (wpa_s->global->p2p)
2360 p2p_sd_service_update(wpa_s->global->p2p);
2361}
2362
2363
2364static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2365{
2366 dl_list_del(&bsrv->list);
2367 wpabuf_free(bsrv->query);
2368 wpabuf_free(bsrv->resp);
2369 os_free(bsrv);
2370}
2371
2372
2373static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2374{
2375 dl_list_del(&usrv->list);
2376 os_free(usrv->service);
2377 os_free(usrv);
2378}
2379
2380
2381void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2382{
2383 struct p2p_srv_bonjour *bsrv, *bn;
2384 struct p2p_srv_upnp *usrv, *un;
2385
2386 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2387 struct p2p_srv_bonjour, list)
2388 wpas_p2p_srv_bonjour_free(bsrv);
2389
2390 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2391 struct p2p_srv_upnp, list)
2392 wpas_p2p_srv_upnp_free(usrv);
2393
2394 wpas_p2p_sd_service_update(wpa_s);
2395}
2396
2397
2398int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2399 struct wpabuf *query, struct wpabuf *resp)
2400{
2401 struct p2p_srv_bonjour *bsrv;
2402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403 bsrv = os_zalloc(sizeof(*bsrv));
2404 if (bsrv == NULL)
2405 return -1;
2406 bsrv->query = query;
2407 bsrv->resp = resp;
2408 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2409
2410 wpas_p2p_sd_service_update(wpa_s);
2411 return 0;
2412}
2413
2414
2415int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2416 const struct wpabuf *query)
2417{
2418 struct p2p_srv_bonjour *bsrv;
2419
2420 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2421 if (bsrv == NULL)
2422 return -1;
2423 wpas_p2p_srv_bonjour_free(bsrv);
2424 wpas_p2p_sd_service_update(wpa_s);
2425 return 0;
2426}
2427
2428
2429int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2430 const char *service)
2431{
2432 struct p2p_srv_upnp *usrv;
2433
2434 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2435 return 0; /* Already listed */
2436 usrv = os_zalloc(sizeof(*usrv));
2437 if (usrv == NULL)
2438 return -1;
2439 usrv->version = version;
2440 usrv->service = os_strdup(service);
2441 if (usrv->service == NULL) {
2442 os_free(usrv);
2443 return -1;
2444 }
2445 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2446
2447 wpas_p2p_sd_service_update(wpa_s);
2448 return 0;
2449}
2450
2451
2452int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2453 const char *service)
2454{
2455 struct p2p_srv_upnp *usrv;
2456
2457 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2458 if (usrv == NULL)
2459 return -1;
2460 wpas_p2p_srv_upnp_free(usrv);
2461 wpas_p2p_sd_service_update(wpa_s);
2462 return 0;
2463}
2464
2465
2466static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2467 const u8 *peer, const char *params,
2468 unsigned int generated_pin)
2469{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002470 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2471 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472}
2473
2474
2475static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2476 const u8 *peer, const char *params)
2477{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002478 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2479 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480}
2481
2482
2483void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2484 const u8 *dev_addr, const u8 *pri_dev_type,
2485 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002486 u8 dev_capab, u8 group_capab, const u8 *group_id,
2487 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488{
2489 struct wpa_supplicant *wpa_s = ctx;
2490 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002491 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492 u8 empty_dev_type[8];
2493 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002494 struct wpa_supplicant *group = NULL;
2495
2496 if (group_id) {
2497 for (group = wpa_s->global->ifaces; group; group = group->next)
2498 {
2499 struct wpa_ssid *s = group->current_ssid;
2500 if (s != NULL &&
2501 s->mode == WPAS_MODE_P2P_GO &&
2502 group_id_len - ETH_ALEN == s->ssid_len &&
2503 os_memcmp(group_id + ETH_ALEN, s->ssid,
2504 s->ssid_len) == 0)
2505 break;
2506 }
2507 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508
2509 if (pri_dev_type == NULL) {
2510 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2511 pri_dev_type = empty_dev_type;
2512 }
2513 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2514 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002515 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516 MAC2STR(dev_addr),
2517 wps_dev_type_bin2str(pri_dev_type, devtype,
2518 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002519 dev_name, supp_config_methods, dev_capab, group_capab,
2520 group ? " group=" : "",
2521 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 if (config_methods & WPS_CONFIG_DISPLAY) {
2525 generated_pin = wps_generate_pin();
2526 wpas_prov_disc_local_display(wpa_s, peer, params,
2527 generated_pin);
2528 } else if (config_methods & WPS_CONFIG_KEYPAD)
2529 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2530 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002531 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2532 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002533
2534 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2535 P2P_PROV_DISC_SUCCESS,
2536 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537}
2538
2539
2540void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2541{
2542 struct wpa_supplicant *wpa_s = ctx;
2543 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002544 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002546 if (wpa_s->pending_pd_before_join &&
2547 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2548 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2549 wpa_s->pending_pd_before_join = 0;
2550 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2551 "join-existing-group operation");
2552 wpas_p2p_join_start(wpa_s);
2553 return;
2554 }
2555
Dmitry Shmidt04949592012-07-19 12:16:46 -07002556 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2557 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2558 os_snprintf(params, sizeof(params), " peer_go=%d",
2559 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2560 else
2561 params[0] = '\0';
2562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002564 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 else if (config_methods & WPS_CONFIG_KEYPAD) {
2566 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002567 wpas_prov_disc_local_display(wpa_s, peer, params,
2568 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002569 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002570 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2571 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572
Jouni Malinen75ecf522011-06-27 15:19:46 -07002573 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2574 P2P_PROV_DISC_SUCCESS,
2575 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576}
2577
2578
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002579static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2580 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002581{
2582 struct wpa_supplicant *wpa_s = ctx;
2583
Dmitry Shmidt04949592012-07-19 12:16:46 -07002584 if (wpa_s->p2p_fallback_to_go_neg) {
2585 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2586 "failed - fall back to GO Negotiation");
2587 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2588 return;
2589 }
2590
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002591 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002592 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002593 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2594 "join-existing-group operation (no ACK for PD "
2595 "Req attempts)");
2596 wpas_p2p_join_start(wpa_s);
2597 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002598 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002599
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002600 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2601 " p2p_dev_addr=" MACSTR " status=%d",
2602 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002603
Jouni Malinen75ecf522011-06-27 15:19:46 -07002604 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2605 status, 0, 0);
2606}
2607
2608
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002609static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2610{
2611 if (channels == NULL)
2612 return 1; /* Assume no restrictions */
2613 return p2p_channels_includes_freq(channels, freq);
2614
2615}
2616
2617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2619 const u8 *go_dev_addr, const u8 *ssid,
2620 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002621 int *force_freq, int persistent_group,
2622 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623{
2624 struct wpa_supplicant *wpa_s = ctx;
2625 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626 int res;
2627 struct wpa_supplicant *grp;
2628
2629 if (!persistent_group) {
2630 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2631 " to join an active group", MAC2STR(sa));
2632 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2633 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2634 == 0 ||
2635 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2636 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2637 "authorized invitation");
2638 goto accept_inv;
2639 }
2640 /*
2641 * Do not accept the invitation automatically; notify user and
2642 * request approval.
2643 */
2644 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2645 }
2646
2647 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2648 if (grp) {
2649 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2650 "running persistent group");
2651 if (*go)
2652 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2653 goto accept_inv;
2654 }
2655
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002656 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2657 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2658 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2659 "invitation to re-invoke a persistent group");
2660 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2662
2663 for (s = wpa_s->conf->ssid; s; s = s->next) {
2664 if (s->disabled == 2 &&
2665 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2666 s->ssid_len == ssid_len &&
2667 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2668 break;
2669 }
2670
2671 if (!s) {
2672 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2673 " requested reinvocation of an unknown group",
2674 MAC2STR(sa));
2675 return P2P_SC_FAIL_UNKNOWN_GROUP;
2676 }
2677
2678 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2679 *go = 1;
2680 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2681 wpa_printf(MSG_DEBUG, "P2P: The only available "
2682 "interface is already in use - reject "
2683 "invitation");
2684 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2685 }
2686 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2687 } else if (s->mode == WPAS_MODE_P2P_GO) {
2688 *go = 1;
2689 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2690 {
2691 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2692 "interface address for the group");
2693 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2694 }
2695 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2696 ETH_ALEN);
2697 }
2698
2699accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002700 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2701
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002702 /* Get one of the frequencies currently in use */
2703 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002704 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002705 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002706
2707 if (wpa_s->num_multichan_concurrent < 2 ||
2708 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2709 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2710 *force_freq = res;
2711 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712 }
2713
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002714 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2715 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002716 if (*go == 0) {
2717 /* We are the client */
2718 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2719 "running a GO but we are capable of MCC, "
2720 "figure out the best channel to use");
2721 *force_freq = 0;
2722 } else if (!freq_included(channels, *force_freq)) {
2723 /* We are the GO, and *force_freq is not in the
2724 * intersection */
2725 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2726 "in intersection but we are capable of MCC, "
2727 "figure out the best channel to use",
2728 *force_freq);
2729 *force_freq = 0;
2730 }
2731 }
2732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733 return P2P_SC_SUCCESS;
2734}
2735
2736
2737static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2738 const u8 *ssid, size_t ssid_len,
2739 const u8 *go_dev_addr, u8 status,
2740 int op_freq)
2741{
2742 struct wpa_supplicant *wpa_s = ctx;
2743 struct wpa_ssid *s;
2744
2745 for (s = wpa_s->conf->ssid; s; s = s->next) {
2746 if (s->disabled == 2 &&
2747 s->ssid_len == ssid_len &&
2748 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2749 break;
2750 }
2751
2752 if (status == P2P_SC_SUCCESS) {
2753 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2754 " was accepted; op_freq=%d MHz",
2755 MAC2STR(sa), op_freq);
2756 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002757 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758 wpas_p2p_group_add_persistent(
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002759 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002760 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002762 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002764 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765 }
2766 return;
2767 }
2768
2769 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2770 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2771 " was rejected (status %u)", MAC2STR(sa), status);
2772 return;
2773 }
2774
2775 if (!s) {
2776 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002777 wpa_msg_global(wpa_s, MSG_INFO,
2778 P2P_EVENT_INVITATION_RECEIVED
2779 "sa=" MACSTR " go_dev_addr=" MACSTR
2780 " bssid=" MACSTR " unknown-network",
2781 MAC2STR(sa), MAC2STR(go_dev_addr),
2782 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002784 wpa_msg_global(wpa_s, MSG_INFO,
2785 P2P_EVENT_INVITATION_RECEIVED
2786 "sa=" MACSTR " go_dev_addr=" MACSTR
2787 " unknown-network",
2788 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789 }
2790 return;
2791 }
2792
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002793 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002794 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2795 "sa=" MACSTR " persistent=%d freq=%d",
2796 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002797 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002798 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2799 "sa=" MACSTR " persistent=%d",
2800 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002801 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002802}
2803
2804
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002805static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2806 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002807 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002808{
2809 size_t i;
2810
2811 if (ssid == NULL)
2812 return;
2813
2814 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2815 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2816 ETH_ALEN) == 0)
2817 break;
2818 }
2819 if (i >= ssid->num_p2p_clients) {
2820 if (ssid->mode != WPAS_MODE_P2P_GO &&
2821 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2822 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2823 "due to invitation result", ssid->id);
2824 wpas_notify_network_removed(wpa_s, ssid);
2825 wpa_config_remove_network(wpa_s->conf, ssid->id);
2826 return;
2827 }
2828 return; /* Peer not found in client list */
2829 }
2830
2831 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002832 "group %d client list%s",
2833 MAC2STR(peer), ssid->id,
2834 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002835 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2836 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2837 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2838 ssid->num_p2p_clients--;
2839#ifndef CONFIG_NO_CONFIG_WRITE
2840 if (wpa_s->parent->conf->update_config &&
2841 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2842 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2843#endif /* CONFIG_NO_CONFIG_WRITE */
2844}
2845
2846
2847static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2848 const u8 *peer)
2849{
2850 struct wpa_ssid *ssid;
2851
2852 wpa_s = wpa_s->global->p2p_invite_group;
2853 if (wpa_s == NULL)
2854 return; /* No known invitation group */
2855 ssid = wpa_s->current_ssid;
2856 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2857 !ssid->p2p_persistent_group)
2858 return; /* Not operating as a GO in persistent group */
2859 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2860 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002861 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002862}
2863
2864
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002865static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002866 const struct p2p_channels *channels,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002867 const u8 *peer, int neg_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868{
2869 struct wpa_supplicant *wpa_s = ctx;
2870 struct wpa_ssid *ssid;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002871 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872
2873 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002874 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2875 "status=%d " MACSTR,
2876 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002878 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2879 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880 }
2881 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2882
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002883 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2884 status, MAC2STR(peer));
2885 if (wpa_s->pending_invite_ssid_id == -1) {
2886 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2887 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002889 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002890
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002891 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2892 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2893 "invitation exchange to indicate readiness for "
2894 "re-invocation");
2895 }
2896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002898 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2899 ssid = wpa_config_get_network(
2900 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002901 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002902 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 wpas_p2p_remove_pending_group_interface(wpa_s);
2904 return;
2905 }
2906
2907 ssid = wpa_config_get_network(wpa_s->conf,
2908 wpa_s->pending_invite_ssid_id);
2909 if (ssid == NULL) {
2910 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2911 "data matching with invitation");
2912 return;
2913 }
2914
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002915 /*
2916 * The peer could have missed our ctrl::ack frame for Invitation
2917 * Response and continue retransmitting the frame. To reduce the
2918 * likelihood of the peer not getting successful TX status for the
2919 * Invitation Response frame, wait a short time here before starting
2920 * the persistent group so that we will remain on the current channel to
2921 * acknowledge any possible retransmission from the peer.
2922 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002923#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002924 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2925 "starting persistent group");
2926 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002927#else
2928 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2929 "starting persistent group");
2930 os_sleep(0, 100000);
2931#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002932
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002933 freq = wpa_s->p2p_persistent_go_freq;
2934 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2935 freq_included(channels, neg_freq)) {
2936 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
2937 neg_freq);
2938 freq = neg_freq;
2939 }
2940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002942 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002943 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002944 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2945 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002946 ssid->mode == WPAS_MODE_P2P_GO ?
2947 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2948 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002949}
2950
2951
Dmitry Shmidt04949592012-07-19 12:16:46 -07002952static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2953 unsigned int freq)
2954{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002955 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002956}
2957
2958
2959static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2960{
2961 reg->channel[reg->channels] = chan;
2962 reg->channels++;
2963}
2964
2965
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002967 struct p2p_channels *chan,
2968 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969{
2970 int i, cla = 0;
2971
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002972 os_memset(cli_chan, 0, sizeof(*cli_chan));
2973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2975 "band");
2976
2977 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2978 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002979 chan->reg_class[cla].channels = 0;
2980 for (i = 0; i < 11; i++) {
2981 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2982 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2983 }
2984 if (chan->reg_class[cla].channels)
2985 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986
2987 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2988 "band");
2989
2990 /* Operating class 115 - 5 GHz, channels 36-48 */
2991 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002992 chan->reg_class[cla].channels = 0;
2993 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2994 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2995 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2996 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2997 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2998 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2999 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3000 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3001 if (chan->reg_class[cla].channels)
3002 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003
3004 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3005 "band");
3006
3007 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3008 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003009 chan->reg_class[cla].channels = 0;
3010 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3011 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3012 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3013 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3014 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3015 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3016 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3017 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3018 if (chan->reg_class[cla].channels)
3019 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020
3021 chan->reg_classes = cla;
3022 return 0;
3023}
3024
3025
3026static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3027 u16 num_modes,
3028 enum hostapd_hw_mode mode)
3029{
3030 u16 i;
3031
3032 for (i = 0; i < num_modes; i++) {
3033 if (modes[i].mode == mode)
3034 return &modes[i];
3035 }
3036
3037 return NULL;
3038}
3039
3040
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003041enum chan_allowed {
3042 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3043};
3044
Dmitry Shmidt04949592012-07-19 12:16:46 -07003045static int has_channel(struct wpa_global *global,
3046 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047{
3048 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003049 unsigned int freq;
3050
3051 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3052 chan * 5;
3053 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003054 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055
3056 for (i = 0; i < mode->num_channels; i++) {
3057 if (mode->channels[i].chan == chan) {
3058 if (flags)
3059 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003060 if (mode->channels[i].flag &
3061 (HOSTAPD_CHAN_DISABLED |
3062 HOSTAPD_CHAN_RADAR))
3063 return NOT_ALLOWED;
3064 if (mode->channels[i].flag &
3065 (HOSTAPD_CHAN_PASSIVE_SCAN |
3066 HOSTAPD_CHAN_NO_IBSS))
3067 return PASSIVE_ONLY;
3068 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003069 }
3070 }
3071
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003072 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073}
3074
3075
3076struct p2p_oper_class_map {
3077 enum hostapd_hw_mode mode;
3078 u8 op_class;
3079 u8 min_chan;
3080 u8 max_chan;
3081 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003082 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083};
3084
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003085static struct p2p_oper_class_map op_class[] = {
3086 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003087#if 0 /* Do not enable HT40 on 2 GHz for now */
3088 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3089 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3090#endif
3091 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3092 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3093 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3094 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3095 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3096 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003097
3098 /*
3099 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3100 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3101 * 80 MHz, but currently use the following definition for simplicity
3102 * (these center frequencies are not actual channels, which makes
3103 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3104 * removing invalid channels.
3105 */
3106 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003107 { -1, 0, 0, 0, 0, BW20 }
3108};
3109
3110
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003111static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3112 struct hostapd_hw_modes *mode,
3113 u8 channel)
3114{
3115 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3116 unsigned int i;
3117
3118 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3119 return 0;
3120
3121 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3122 /*
3123 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3124 * so the center channel is 6 channels away from the start/end.
3125 */
3126 if (channel >= center_channels[i] - 6 &&
3127 channel <= center_channels[i] + 6)
3128 return center_channels[i];
3129
3130 return 0;
3131}
3132
3133
3134static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3135 struct hostapd_hw_modes *mode,
3136 u8 channel, u8 bw)
3137{
3138 u8 center_chan;
3139 int i, flags;
3140 enum chan_allowed res, ret = ALLOWED;
3141
3142 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3143 if (!center_chan)
3144 return NOT_ALLOWED;
3145 if (center_chan >= 58 && center_chan <= 138)
3146 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3147
3148 /* check all the channels are available */
3149 for (i = 0; i < 4; i++) {
3150 int adj_chan = center_chan - 6 + i * 4;
3151
3152 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3153 if (res == NOT_ALLOWED)
3154 return NOT_ALLOWED;
3155 if (res == PASSIVE_ONLY)
3156 ret = PASSIVE_ONLY;
3157
3158 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3159 return NOT_ALLOWED;
3160 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3161 return NOT_ALLOWED;
3162 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3163 return NOT_ALLOWED;
3164 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3165 return NOT_ALLOWED;
3166 }
3167
3168 return ret;
3169}
3170
3171
3172static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3173 struct hostapd_hw_modes *mode,
3174 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003175{
3176 int flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003177 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003178
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003179 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3180 if (bw == BW40MINUS) {
3181 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3182 return NOT_ALLOWED;
3183 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3184 } else if (bw == BW40PLUS) {
3185 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3186 return NOT_ALLOWED;
3187 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3188 } else if (bw == BW80) {
3189 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3190 }
3191
3192 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3193 return NOT_ALLOWED;
3194 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3195 return PASSIVE_ONLY;
3196 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003197}
3198
3199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003201 struct p2p_channels *chan,
3202 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003203{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003204 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003205 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003207 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003208 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3209 "of all supported channels; assume dualband "
3210 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003211 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212 }
3213
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003214 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215
3216 for (op = 0; op_class[op].op_class; op++) {
3217 struct p2p_oper_class_map *o = &op_class[op];
3218 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003219 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003221 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222 if (mode == NULL)
3223 continue;
3224 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003225 enum chan_allowed res;
3226 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3227 if (res == ALLOWED) {
3228 if (reg == NULL) {
3229 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3230 o->op_class);
3231 reg = &chan->reg_class[cla];
3232 cla++;
3233 reg->reg_class = o->op_class;
3234 }
3235 reg->channel[reg->channels] = ch;
3236 reg->channels++;
3237 } else if (res == PASSIVE_ONLY &&
3238 wpa_s->conf->p2p_add_cli_chan) {
3239 if (cli_reg == NULL) {
3240 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3241 o->op_class);
3242 cli_reg = &cli_chan->reg_class[cli_cla];
3243 cli_cla++;
3244 cli_reg->reg_class = o->op_class;
3245 }
3246 cli_reg->channel[cli_reg->channels] = ch;
3247 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 }
3250 if (reg) {
3251 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3252 reg->channel, reg->channels);
3253 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003254 if (cli_reg) {
3255 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3256 cli_reg->channel, cli_reg->channels);
3257 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 }
3259
3260 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003261 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263 return 0;
3264}
3265
3266
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003267int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3268 struct hostapd_hw_modes *mode, u8 channel)
3269{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003270 int op;
3271 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003272
3273 for (op = 0; op_class[op].op_class; op++) {
3274 struct p2p_oper_class_map *o = &op_class[op];
3275 u8 ch;
3276
3277 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3278 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3279 o->bw == BW20 || ch != channel)
3280 continue;
3281 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003282 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003283 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003284 }
3285 }
3286 return 0;
3287}
3288
3289
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003290int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3291 struct hostapd_hw_modes *mode, u8 channel)
3292{
3293 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3294 return 0;
3295
3296 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3297}
3298
3299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3301 size_t buf_len)
3302{
3303 struct wpa_supplicant *wpa_s = ctx;
3304
3305 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3306 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3307 break;
3308 }
3309 if (wpa_s == NULL)
3310 return -1;
3311
3312 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3313}
3314
3315
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003316static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3317{
3318 struct wpa_supplicant *wpa_s = ctx;
3319
3320 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3321 struct wpa_ssid *ssid = wpa_s->current_ssid;
3322 if (ssid == NULL)
3323 continue;
3324 if (ssid->mode != WPAS_MODE_INFRA)
3325 continue;
3326 if (wpa_s->wpa_state != WPA_COMPLETED &&
3327 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3328 continue;
3329 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3330 return 1;
3331 }
3332
3333 return 0;
3334}
3335
3336
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003337static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3338{
3339 struct wpa_supplicant *wpa_s = ctx;
3340 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3341}
3342
3343
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003344int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3345{
3346 struct wpa_interface iface;
3347 struct wpa_supplicant *p2pdev_wpa_s;
3348 char ifname[100];
3349 char force_name[100];
3350 int ret;
3351
3352 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3353 wpa_s->ifname);
3354 force_name[0] = '\0';
3355 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3356 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3357 force_name, wpa_s->pending_interface_addr, NULL);
3358 if (ret < 0) {
3359 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3360 return ret;
3361 }
3362 os_strlcpy(wpa_s->pending_interface_name, ifname,
3363 sizeof(wpa_s->pending_interface_name));
3364
3365 os_memset(&iface, 0, sizeof(iface));
3366 iface.p2p_mgmt = 1;
3367 iface.ifname = wpa_s->pending_interface_name;
3368 iface.driver = wpa_s->driver->name;
3369 iface.driver_param = wpa_s->conf->driver_param;
3370 iface.confname = wpa_s->confname;
3371 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3372 if (!p2pdev_wpa_s) {
3373 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3374 return -1;
3375 }
3376 p2pdev_wpa_s->parent = wpa_s;
3377
3378 wpa_s->pending_interface_name[0] = '\0';
3379 return 0;
3380}
3381
3382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383/**
3384 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3385 * @global: Pointer to global data from wpa_supplicant_init()
3386 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3387 * Returns: 0 on success, -1 on failure
3388 */
3389int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3390{
3391 struct p2p_config p2p;
3392 unsigned int r;
3393 int i;
3394
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003395 if (wpa_s->conf->p2p_disabled)
3396 return 0;
3397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3399 return 0;
3400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003401 if (global->p2p)
3402 return 0;
3403
3404 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3405 struct p2p_params params;
3406
3407 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3408 os_memset(&params, 0, sizeof(params));
3409 params.dev_name = wpa_s->conf->device_name;
3410 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3411 WPS_DEV_TYPE_LEN);
3412 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3413 os_memcpy(params.sec_dev_type,
3414 wpa_s->conf->sec_device_type,
3415 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3416
3417 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3418 return -1;
3419
3420 return 0;
3421 }
3422
3423 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003424 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003425 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 p2p.p2p_scan = wpas_p2p_scan;
3427 p2p.send_action = wpas_send_action;
3428 p2p.send_action_done = wpas_send_action_done;
3429 p2p.go_neg_completed = wpas_go_neg_completed;
3430 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3431 p2p.dev_found = wpas_dev_found;
3432 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003433 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 p2p.start_listen = wpas_start_listen;
3435 p2p.stop_listen = wpas_stop_listen;
3436 p2p.send_probe_resp = wpas_send_probe_resp;
3437 p2p.sd_request = wpas_sd_request;
3438 p2p.sd_response = wpas_sd_response;
3439 p2p.prov_disc_req = wpas_prov_disc_req;
3440 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003441 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003442 p2p.invitation_process = wpas_invitation_process;
3443 p2p.invitation_received = wpas_invitation_received;
3444 p2p.invitation_result = wpas_invitation_result;
3445 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003446 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003447
3448 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003449 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450 p2p.dev_name = wpa_s->conf->device_name;
3451 p2p.manufacturer = wpa_s->conf->manufacturer;
3452 p2p.model_name = wpa_s->conf->model_name;
3453 p2p.model_number = wpa_s->conf->model_number;
3454 p2p.serial_number = wpa_s->conf->serial_number;
3455 if (wpa_s->wps) {
3456 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3457 p2p.config_methods = wpa_s->wps->config_methods;
3458 }
3459
3460 if (wpa_s->conf->p2p_listen_reg_class &&
3461 wpa_s->conf->p2p_listen_channel) {
3462 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3463 p2p.channel = wpa_s->conf->p2p_listen_channel;
3464 } else {
3465 p2p.reg_class = 81;
3466 /*
3467 * Pick one of the social channels randomly as the listen
3468 * channel.
3469 */
3470 os_get_random((u8 *) &r, sizeof(r));
3471 p2p.channel = 1 + (r % 3) * 5;
3472 }
3473 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3474
3475 if (wpa_s->conf->p2p_oper_reg_class &&
3476 wpa_s->conf->p2p_oper_channel) {
3477 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3478 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3479 p2p.cfg_op_channel = 1;
3480 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3481 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3482
3483 } else {
3484 p2p.op_reg_class = 81;
3485 /*
3486 * Use random operation channel from (1, 6, 11) if no other
3487 * preference is indicated.
3488 */
3489 os_get_random((u8 *) &r, sizeof(r));
3490 p2p.op_channel = 1 + (r % 3) * 5;
3491 p2p.cfg_op_channel = 0;
3492 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3493 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3494 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003495
3496 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3497 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3498 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3499 }
3500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3502 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3503 p2p.country[2] = 0x04;
3504 } else
3505 os_memcpy(p2p.country, "XX\x04", 3);
3506
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003507 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003508 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3509 "channel list");
3510 return -1;
3511 }
3512
3513 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3514 WPS_DEV_TYPE_LEN);
3515
3516 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3517 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3518 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3519
3520 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3521 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3522
3523 p2p.max_peers = 100;
3524
3525 if (wpa_s->conf->p2p_ssid_postfix) {
3526 p2p.ssid_postfix_len =
3527 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3528 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3529 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3530 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3531 p2p.ssid_postfix_len);
3532 }
3533
3534 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3535
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003536 p2p.max_listen = wpa_s->max_remain_on_chan;
3537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538 global->p2p = p2p_init(&p2p);
3539 if (global->p2p == NULL)
3540 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003541 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542
3543 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3544 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3545 continue;
3546 p2p_add_wps_vendor_extension(
3547 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3548 }
3549
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003550 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 return 0;
3553}
3554
3555
3556/**
3557 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3558 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3559 *
3560 * This function deinitialize per-interface P2P data.
3561 */
3562void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3563{
3564 if (wpa_s->driver && wpa_s->drv_priv)
3565 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003566
3567 if (wpa_s->go_params) {
3568 /* Clear any stored provisioning info */
3569 p2p_clear_provisioning_info(
3570 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003571 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003572 }
3573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003574 os_free(wpa_s->go_params);
3575 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3577 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3578 wpa_s->p2p_long_listen = 0;
3579 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3580 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3581 wpas_p2p_remove_pending_group_interface(wpa_s);
3582
3583 /* TODO: remove group interface from the driver if this wpa_s instance
3584 * is on top of a P2P group interface */
3585}
3586
3587
3588/**
3589 * wpas_p2p_deinit_global - Deinitialize global P2P module
3590 * @global: Pointer to global data from wpa_supplicant_init()
3591 *
3592 * This function deinitializes the global (per device) P2P module.
3593 */
3594void wpas_p2p_deinit_global(struct wpa_global *global)
3595{
3596 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003597
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003598 wpa_s = global->ifaces;
3599 if (wpa_s)
3600 wpas_p2p_service_flush(wpa_s);
3601
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602 if (global->p2p == NULL)
3603 return;
3604
3605 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3607 wpa_s = wpa_s->next;
3608 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 tmp = global->ifaces;
3610 while (tmp &&
3611 (tmp == wpa_s ||
3612 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3613 tmp = tmp->next;
3614 }
3615 if (tmp == NULL)
3616 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003617 /* Disconnect from the P2P group and deinit the interface */
3618 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 }
3620
3621 /*
3622 * Deinit GO data on any possibly remaining interface (if main
3623 * interface is used as GO).
3624 */
3625 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3626 if (wpa_s->ap_iface)
3627 wpas_p2p_group_deinit(wpa_s);
3628 }
3629
3630 p2p_deinit(global->p2p);
3631 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003632 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633}
3634
3635
3636static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3637{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003638 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3639 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003640 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003641 if (wpa_s->drv_flags &
3642 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3643 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3644 return 1; /* P2P group requires a new interface in every case
3645 */
3646 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3647 return 0; /* driver does not support concurrent operations */
3648 if (wpa_s->global->ifaces->next)
3649 return 1; /* more that one interface already in use */
3650 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3651 return 1; /* this interface is already in use */
3652 return 0;
3653}
3654
3655
3656static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3657 const u8 *peer_addr,
3658 enum p2p_wps_method wps_method,
3659 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003660 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003661 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003663 if (persistent_group && wpa_s->conf->persistent_reconnect)
3664 persistent_group = 2;
3665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3667 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3668 go_intent, own_interface_addr,
3669 force_freq, persistent_group);
3670 }
3671
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003672 /*
3673 * Increase GO config timeout if HT40 is used since it takes some time
3674 * to scan channels for coex purposes before the BSS can be started.
3675 */
3676 p2p_set_config_timeout(wpa_s->global->p2p,
3677 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3680 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003681 persistent_group, ssid ? ssid->ssid : NULL,
3682 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003683 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003684}
3685
3686
3687static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3688 const u8 *peer_addr,
3689 enum p2p_wps_method wps_method,
3690 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003691 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003692 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003694 if (persistent_group && wpa_s->conf->persistent_reconnect)
3695 persistent_group = 2;
3696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3698 return -1;
3699
3700 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3701 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003702 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003703 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003704}
3705
3706
3707static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3708{
3709 wpa_s->p2p_join_scan_count++;
3710 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3711 wpa_s->p2p_join_scan_count);
3712 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3713 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3714 " for join operationg - stop join attempt",
3715 MAC2STR(wpa_s->pending_join_iface_addr));
3716 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003717 if (wpa_s->p2p_auto_pd) {
3718 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003719 wpa_msg_global(wpa_s, MSG_INFO,
3720 P2P_EVENT_PROV_DISC_FAILURE
3721 " p2p_dev_addr=" MACSTR " status=N/A",
3722 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003723 return;
3724 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003725 wpa_msg_global(wpa_s->parent, MSG_INFO,
3726 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727 }
3728}
3729
3730
Dmitry Shmidt04949592012-07-19 12:16:46 -07003731static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3732{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003733 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003734
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003735 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3736 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003737 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003738 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003739
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003740 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3741 if (!freqs)
3742 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003743
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003744 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3745 wpa_s->num_multichan_concurrent);
3746 if (num < 0) {
3747 res = 1;
3748 goto exit_free;
3749 }
3750
3751 for (i = 0; i < num; i++) {
3752 if (freqs[i] == freq) {
3753 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3754 freq);
3755 res = 0;
3756 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003757 }
3758 }
3759
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003760 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003761
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003762exit_free:
3763 os_free(freqs);
3764 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003765}
3766
3767
3768static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3769 const u8 *peer_dev_addr)
3770{
3771 struct wpa_bss *bss;
3772 int updated;
3773
3774 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3775 if (bss == NULL)
3776 return -1;
3777 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3778 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3779 "last scan");
3780 return 0;
3781 }
3782
3783 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3784 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3785 "%ld.%06ld (%supdated in last scan)",
3786 bss->last_update.sec, bss->last_update.usec,
3787 updated ? "": "not ");
3788
3789 return updated;
3790}
3791
3792
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003793static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3794 struct wpa_scan_results *scan_res)
3795{
3796 struct wpa_bss *bss;
3797 int freq;
3798 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3801
3802 if (wpa_s->global->p2p_disabled)
3803 return;
3804
Dmitry Shmidt04949592012-07-19 12:16:46 -07003805 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3806 scan_res ? (int) scan_res->num : -1,
3807 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003808
3809 if (scan_res)
3810 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3811
Dmitry Shmidt04949592012-07-19 12:16:46 -07003812 if (wpa_s->p2p_auto_pd) {
3813 int join = wpas_p2p_peer_go(wpa_s,
3814 wpa_s->pending_join_dev_addr);
3815 if (join == 0 &&
3816 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3817 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003818 bss = wpa_bss_get_bssid_latest(
3819 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003820 if (bss) {
3821 freq = bss->freq;
3822 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3823 "the peer " MACSTR " at %d MHz",
3824 wpa_s->auto_pd_scan_retry,
3825 MAC2STR(wpa_s->
3826 pending_join_dev_addr),
3827 freq);
3828 wpas_p2p_join_scan_req(wpa_s, freq);
3829 return;
3830 }
3831 }
3832
3833 if (join < 0)
3834 join = 0;
3835
3836 wpa_s->p2p_auto_pd = 0;
3837 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3838 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3839 MAC2STR(wpa_s->pending_join_dev_addr), join);
3840 if (p2p_prov_disc_req(wpa_s->global->p2p,
3841 wpa_s->pending_join_dev_addr,
3842 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003843 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003844 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003845 wpa_msg_global(wpa_s, MSG_INFO,
3846 P2P_EVENT_PROV_DISC_FAILURE
3847 " p2p_dev_addr=" MACSTR " status=N/A",
3848 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003849 }
3850 return;
3851 }
3852
3853 if (wpa_s->p2p_auto_join) {
3854 int join = wpas_p2p_peer_go(wpa_s,
3855 wpa_s->pending_join_dev_addr);
3856 if (join < 0) {
3857 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3858 "running a GO -> use GO Negotiation");
3859 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3860 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3861 wpa_s->p2p_persistent_group, 0, 0, 0,
3862 wpa_s->p2p_go_intent,
3863 wpa_s->p2p_connect_freq,
3864 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003865 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003866 wpa_s->p2p_go_ht40,
3867 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003868 return;
3869 }
3870
3871 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3872 "try to join the group", join ? "" :
3873 " in older scan");
3874 if (!join)
3875 wpa_s->p2p_fallback_to_go_neg = 1;
3876 }
3877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3879 wpa_s->pending_join_iface_addr);
3880 if (freq < 0 &&
3881 p2p_get_interface_addr(wpa_s->global->p2p,
3882 wpa_s->pending_join_dev_addr,
3883 iface_addr) == 0 &&
3884 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3885 {
3886 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3887 "address for join from " MACSTR " to " MACSTR
3888 " based on newly discovered P2P peer entry",
3889 MAC2STR(wpa_s->pending_join_iface_addr),
3890 MAC2STR(iface_addr));
3891 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3892 ETH_ALEN);
3893
3894 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3895 wpa_s->pending_join_iface_addr);
3896 }
3897 if (freq >= 0) {
3898 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3899 "from P2P peer table: %d MHz", freq);
3900 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003901 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902 if (bss) {
3903 freq = bss->freq;
3904 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07003905 "from BSS table: %d MHz (SSID %s)", freq,
3906 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003907 }
3908 if (freq > 0) {
3909 u16 method;
3910
Dmitry Shmidt04949592012-07-19 12:16:46 -07003911 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003912 wpa_msg_global(wpa_s->parent, MSG_INFO,
3913 P2P_EVENT_GROUP_FORMATION_FAILURE
3914 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003915 return;
3916 }
3917
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3919 "prior to joining an existing group (GO " MACSTR
3920 " freq=%u MHz)",
3921 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3922 wpa_s->pending_pd_before_join = 1;
3923
3924 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925 case WPS_PIN_DISPLAY:
3926 method = WPS_CONFIG_KEYPAD;
3927 break;
3928 case WPS_PIN_KEYPAD:
3929 method = WPS_CONFIG_DISPLAY;
3930 break;
3931 case WPS_PBC:
3932 method = WPS_CONFIG_PUSHBUTTON;
3933 break;
3934 default:
3935 method = 0;
3936 break;
3937 }
3938
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003939 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3940 wpa_s->pending_join_dev_addr) ==
3941 method)) {
3942 /*
3943 * We have already performed provision discovery for
3944 * joining the group. Proceed directly to join
3945 * operation without duplicated provision discovery. */
3946 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3947 "with " MACSTR " already done - proceed to "
3948 "join",
3949 MAC2STR(wpa_s->pending_join_dev_addr));
3950 wpa_s->pending_pd_before_join = 0;
3951 goto start;
3952 }
3953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003955 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003956 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003957 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3958 "Discovery Request before joining an "
3959 "existing group");
3960 wpa_s->pending_pd_before_join = 0;
3961 goto start;
3962 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963 return;
3964 }
3965
3966 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3967 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3968 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3969 wpas_p2p_check_join_scan_limit(wpa_s);
3970 return;
3971
3972start:
3973 /* Start join operation immediately */
3974 wpas_p2p_join_start(wpa_s);
3975}
3976
3977
Dmitry Shmidt04949592012-07-19 12:16:46 -07003978static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003979{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003980 int ret;
3981 struct wpa_driver_scan_params params;
3982 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003983 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003984 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003985#ifdef ANDROID_P2P
3986 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003988 /* If freq is not provided, check the operating freq of the GO and do a
3989 * a directed scan to save time
3990 */
3991 if(!freq) {
3992 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3993 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3994 }
3995#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996 os_memset(&params, 0, sizeof(params));
3997
3998 /* P2P Wildcard SSID */
3999 params.num_ssids = 1;
4000 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4001 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4002
4003 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004004 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4005 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4006 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007 if (wps_ie == NULL) {
4008 wpas_p2p_scan_res_join(wpa_s, NULL);
4009 return;
4010 }
4011
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004012 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4013 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014 if (ies == NULL) {
4015 wpabuf_free(wps_ie);
4016 wpas_p2p_scan_res_join(wpa_s, NULL);
4017 return;
4018 }
4019 wpabuf_put_buf(ies, wps_ie);
4020 wpabuf_free(wps_ie);
4021
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004022 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004024 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004025 params.extra_ies = wpabuf_head(ies);
4026 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004027 if (freq > 0) {
4028 freqs[0] = freq;
4029 params.freqs = freqs;
4030 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031
4032 /*
4033 * Run a scan to update BSS table and start Provision Discovery once
4034 * the new scan results become available.
4035 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004036 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004037 if (!ret) {
4038 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004039 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004040 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041
4042 wpabuf_free(ies);
4043
4044 if (ret) {
4045 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4046 "try again later");
4047 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4048 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4049 wpas_p2p_check_join_scan_limit(wpa_s);
4050 }
4051}
4052
4053
Dmitry Shmidt04949592012-07-19 12:16:46 -07004054static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4055{
4056 struct wpa_supplicant *wpa_s = eloop_ctx;
4057 wpas_p2p_join_scan_req(wpa_s, 0);
4058}
4059
4060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004062 const u8 *dev_addr, enum p2p_wps_method wps_method,
4063 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064{
4065 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004066 MACSTR " dev " MACSTR ")%s",
4067 MAC2STR(iface_addr), MAC2STR(dev_addr),
4068 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069
Dmitry Shmidt04949592012-07-19 12:16:46 -07004070 wpa_s->p2p_auto_pd = 0;
4071 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004072 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4073 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4074 wpa_s->pending_join_wps_method = wps_method;
4075
4076 /* Make sure we are not running find during connection establishment */
4077 wpas_p2p_stop_find(wpa_s);
4078
4079 wpa_s->p2p_join_scan_count = 0;
4080 wpas_p2p_join_scan(wpa_s, NULL);
4081 return 0;
4082}
4083
4084
4085static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4086{
4087 struct wpa_supplicant *group;
4088 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004089 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090
4091 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4092 if (group == NULL)
4093 return -1;
4094 if (group != wpa_s) {
4095 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4096 sizeof(group->p2p_pin));
4097 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004098 } else {
4099 /*
4100 * Need to mark the current interface for p2p_group_formation
4101 * when a separate group interface is not used. This is needed
4102 * to allow p2p_cancel stop a pending p2p_connect-join.
4103 * wpas_p2p_init_group_interface() addresses this for the case
4104 * where a separate group interface is used.
4105 */
4106 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107 }
4108
4109 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004110 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004111
4112 os_memset(&res, 0, sizeof(res));
4113 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4114 ETH_ALEN);
4115 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004116 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004117 if (bss) {
4118 res.freq = bss->freq;
4119 res.ssid_len = bss->ssid_len;
4120 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07004121 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4122 "from BSS table: %d MHz (SSID %s)", bss->freq,
4123 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004124 }
4125
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004126 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4127 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4128 "starting client");
4129 wpa_drv_cancel_remain_on_channel(wpa_s);
4130 wpa_s->off_channel_freq = 0;
4131 wpa_s->roc_waiting_drv_freq = 0;
4132 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 wpas_start_wps_enrollee(group, &res);
4134
4135 /*
4136 * Allow a longer timeout for join-a-running-group than normal 15
4137 * second group formation timeout since the GO may not have authorized
4138 * our connection yet.
4139 */
4140 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4141 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4142 wpa_s, NULL);
4143
4144 return 0;
4145}
4146
4147
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004148static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004149 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004150{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004151 int *freqs, res;
4152 unsigned int freq_in_use = 0, num, i;
4153
4154 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4155 if (!freqs)
4156 return -1;
4157
4158 num = get_shared_radio_freqs(wpa_s, freqs,
4159 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004160 wpa_printf(MSG_DEBUG,
4161 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4162 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004163
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004164 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004165 int ret;
4166 if (go)
4167 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4168 else
4169 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4170 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004171 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4172 "(%u MHz) is not supported for P2P uses",
4173 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004174 res = -3;
4175 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004176 }
4177
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004178 for (i = 0; i < num; i++) {
4179 if (freqs[i] == freq)
4180 freq_in_use = 1;
4181 }
4182
4183 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4184 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4185 freq);
4186 res = -2;
4187 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004188 }
4189 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4190 "requested channel (%u MHz)", freq);
4191 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004192 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004193 }
4194
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004195 for (i = 0; i < num; i++) {
4196 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4197 continue;
4198
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004199 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004200 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4201 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004202 *pref_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004203 } else {
4204 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 -08004205 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004206 *force_freq = freqs[i];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004207 }
4208 break;
4209 }
4210
4211 if (i == num) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004212 if (num < wpa_s->num_multichan_concurrent && num > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004213 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4214 *force_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004215 } else if (num < wpa_s->num_multichan_concurrent) {
4216 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4217 *force_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004218 } else {
4219 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4220 res = -2;
4221 goto exit_free;
4222 }
4223 }
4224
4225exit_ok:
4226 res = 0;
4227exit_free:
4228 os_free(freqs);
4229 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004230}
4231
4232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004233/**
4234 * wpas_p2p_connect - Request P2P Group Formation to be started
4235 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4236 * @peer_addr: Address of the peer P2P Device
4237 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4238 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004239 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240 * @join: Whether to join an existing group (as a client) instead of starting
4241 * Group Owner negotiation; @peer_addr is BSSID in that case
4242 * @auth: Whether to only authorize the connection instead of doing that and
4243 * initiating Group Owner negotiation
4244 * @go_intent: GO Intent or -1 to use default
4245 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004246 * @persistent_id: Persistent group credentials to use for forcing GO
4247 * parameters or -1 to generate new values (SSID/passphrase)
4248 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4249 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004250 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004251 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4253 * failure, -2 on failure due to channel not currently available,
4254 * -3 if forced channel is not supported
4255 */
4256int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4257 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004258 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004259 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004260 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004262 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004263 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004264 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004265 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004266 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004267
4268 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4269 return -1;
4270
Dmitry Shmidt04949592012-07-19 12:16:46 -07004271 if (persistent_id >= 0) {
4272 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4273 if (ssid == NULL || ssid->disabled != 2 ||
4274 ssid->mode != WPAS_MODE_P2P_GO)
4275 return -1;
4276 }
4277
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004278 os_free(wpa_s->global->add_psk);
4279 wpa_s->global->add_psk = NULL;
4280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004281 if (go_intent < 0)
4282 go_intent = wpa_s->conf->p2p_go_intent;
4283
4284 if (!auth)
4285 wpa_s->p2p_long_listen = 0;
4286
4287 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004288 wpa_s->p2p_persistent_group = !!persistent_group;
4289 wpa_s->p2p_persistent_id = persistent_id;
4290 wpa_s->p2p_go_intent = go_intent;
4291 wpa_s->p2p_connect_freq = freq;
4292 wpa_s->p2p_fallback_to_go_neg = 0;
4293 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004294 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004295 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004296
4297 if (pin)
4298 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4299 else if (wps_method == WPS_PIN_DISPLAY) {
4300 ret = wps_generate_pin();
4301 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4302 ret);
4303 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4304 wpa_s->p2p_pin);
4305 } else
4306 wpa_s->p2p_pin[0] = '\0';
4307
Dmitry Shmidt04949592012-07-19 12:16:46 -07004308 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4310 if (auth) {
4311 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4312 "connect a running group from " MACSTR,
4313 MAC2STR(peer_addr));
4314 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4315 return ret;
4316 }
4317 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4318 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4319 iface_addr) < 0) {
4320 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4321 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4322 dev_addr);
4323 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004324 if (auto_join) {
4325 os_get_time(&wpa_s->p2p_auto_started);
4326 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4327 "%ld.%06ld",
4328 wpa_s->p2p_auto_started.sec,
4329 wpa_s->p2p_auto_started.usec);
4330 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004331 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004332 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4333 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334 return -1;
4335 return ret;
4336 }
4337
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004338 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4339 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004340 if (res)
4341 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004342 wpas_p2p_set_own_freq_preference(wpa_s,
4343 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344
4345 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4346
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004347 if (wpa_s->create_p2p_iface) {
4348 /* Prepare to add a new interface for the group */
4349 iftype = WPA_IF_P2P_GROUP;
4350 if (go_intent == 15)
4351 iftype = WPA_IF_P2P_GO;
4352 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4353 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4354 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004356 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004357
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004358 if_addr = wpa_s->pending_interface_addr;
4359 } else
4360 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004361
4362 if (auth) {
4363 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004364 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004365 force_freq, persistent_group, ssid,
4366 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004367 return -1;
4368 return ret;
4369 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004370
4371 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4372 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004373 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004374 if (wpa_s->create_p2p_iface)
4375 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004376 return -1;
4377 }
4378 return ret;
4379}
4380
4381
4382/**
4383 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4384 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4385 * @freq: Frequency of the channel in MHz
4386 * @duration: Duration of the stay on the channel in milliseconds
4387 *
4388 * This callback is called when the driver indicates that it has started the
4389 * requested remain-on-channel duration.
4390 */
4391void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4392 unsigned int freq, unsigned int duration)
4393{
4394 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4395 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004396 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4397 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4398 wpa_s->pending_listen_duration);
4399 wpa_s->pending_listen_freq = 0;
4400 }
4401}
4402
4403
4404static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4405 unsigned int timeout)
4406{
4407 /* Limit maximum Listen state time based on driver limitation. */
4408 if (timeout > wpa_s->max_remain_on_chan)
4409 timeout = wpa_s->max_remain_on_chan;
4410
4411 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4412 return wpa_drv_p2p_listen(wpa_s, timeout);
4413
4414 return p2p_listen(wpa_s->global->p2p, timeout);
4415}
4416
4417
4418/**
4419 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4420 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4421 * @freq: Frequency of the channel in MHz
4422 *
4423 * This callback is called when the driver indicates that a remain-on-channel
4424 * operation has been completed, i.e., the duration on the requested channel
4425 * has timed out.
4426 */
4427void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4428 unsigned int freq)
4429{
4430 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4431 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004432 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004433 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4434 return;
4435 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4436 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004437 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438 return;
4439 if (wpa_s->p2p_long_listen > 0)
4440 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4441 if (wpa_s->p2p_long_listen > 0) {
4442 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4443 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004444 } else {
4445 /*
4446 * When listen duration is over, stop listen & update p2p_state
4447 * to IDLE.
4448 */
4449 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450 }
4451}
4452
4453
4454/**
4455 * wpas_p2p_group_remove - Remove a P2P group
4456 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4457 * @ifname: Network interface name of the group interface or "*" to remove all
4458 * groups
4459 * Returns: 0 on success, -1 on failure
4460 *
4461 * This function is used to remove a P2P group. This can be used to disconnect
4462 * from a group in which the local end is a P2P Client or to end a P2P Group in
4463 * case the local end is the Group Owner. If a virtual network interface was
4464 * created for this group, that interface will be removed. Otherwise, only the
4465 * configured P2P group network will be removed from the interface.
4466 */
4467int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4468{
4469 struct wpa_global *global = wpa_s->global;
4470
4471 if (os_strcmp(ifname, "*") == 0) {
4472 struct wpa_supplicant *prev;
4473 wpa_s = global->ifaces;
4474 while (wpa_s) {
4475 prev = wpa_s;
4476 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004477 if (prev->p2p_group_interface !=
4478 NOT_P2P_GROUP_INTERFACE ||
4479 (prev->current_ssid &&
4480 prev->current_ssid->p2p_group))
4481 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004482 }
4483 return 0;
4484 }
4485
4486 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4487 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4488 break;
4489 }
4490
4491 return wpas_p2p_disconnect(wpa_s);
4492}
4493
4494
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004495static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4496{
4497 unsigned int r;
4498
4499 if (freq == 2) {
4500 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4501 "band");
4502 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004503 p2p_supported_freq_go(wpa_s->global->p2p,
4504 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004505 freq = wpa_s->best_24_freq;
4506 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4507 "channel: %d MHz", freq);
4508 } else {
4509 os_get_random((u8 *) &r, sizeof(r));
4510 freq = 2412 + (r % 3) * 25;
4511 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4512 "channel: %d MHz", freq);
4513 }
4514 }
4515
4516 if (freq == 5) {
4517 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4518 "band");
4519 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004520 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004521 wpa_s->best_5_freq)) {
4522 freq = wpa_s->best_5_freq;
4523 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4524 "channel: %d MHz", freq);
4525 } else {
4526 os_get_random((u8 *) &r, sizeof(r));
4527 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004528 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004529 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4530 "5 GHz channel for P2P group");
4531 return -1;
4532 }
4533 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4534 "channel: %d MHz", freq);
4535 }
4536 }
4537
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004538 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004539 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4540 "(%u MHz) is not supported for P2P uses",
4541 freq);
4542 return -1;
4543 }
4544
4545 return freq;
4546}
4547
4548
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004549static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4550 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004551 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004552 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004554 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004555 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004556 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004557
4558 os_memset(params, 0, sizeof(*params));
4559 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004560 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004561 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004563 if (!freq_included(channels, freq)) {
4564 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4565 "accepted", freq);
4566 return -1;
4567 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4569 "frequency %d MHz", freq);
4570 params->freq = freq;
4571 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4572 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004573 wpa_s->conf->p2p_oper_channel <= 11 &&
4574 freq_included(channels,
4575 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004576 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4577 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4578 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004579 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4580 wpa_s->conf->p2p_oper_reg_class == 116 ||
4581 wpa_s->conf->p2p_oper_reg_class == 117 ||
4582 wpa_s->conf->p2p_oper_reg_class == 124 ||
4583 wpa_s->conf->p2p_oper_reg_class == 126 ||
4584 wpa_s->conf->p2p_oper_reg_class == 127) &&
4585 freq_included(channels,
4586 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4588 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4589 "frequency %d MHz", params->freq);
4590 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4591 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004592 p2p_supported_freq_go(wpa_s->global->p2p,
4593 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004594 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595 params->freq = wpa_s->best_overall_freq;
4596 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4597 "channel %d MHz", params->freq);
4598 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4599 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004600 p2p_supported_freq_go(wpa_s->global->p2p,
4601 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004602 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004603 params->freq = wpa_s->best_24_freq;
4604 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4605 "channel %d MHz", params->freq);
4606 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4607 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004608 p2p_supported_freq_go(wpa_s->global->p2p,
4609 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004610 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 params->freq = wpa_s->best_5_freq;
4612 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4613 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004614 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4615 channels))) {
4616 params->freq = pref_freq;
4617 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4618 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004620 int chan;
4621 for (chan = 0; chan < 11; chan++) {
4622 params->freq = 2412 + chan * 5;
4623 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004624 params->freq) &&
4625 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004626 break;
4627 }
4628 if (chan == 11) {
4629 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4630 "allowed");
4631 return -1;
4632 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4634 "known)", params->freq);
4635 }
4636
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004637 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4638 if (!freqs)
4639 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004641 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4642 wpa_s->num_multichan_concurrent);
4643 if (res < 0) {
4644 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004645 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004647 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004648
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004649 for (i = 0; i < num; i++) {
4650 if (freq && freqs[i] == freq)
4651 break;
4652 if (!freq && freq_included(channels, freqs[i])) {
4653 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4654 freqs[i]);
4655 params->freq = freqs[i];
4656 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004657 }
4658 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004659
4660 if (i == num) {
4661 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
4662 if (freq)
4663 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4664 else
4665 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4666 os_free(freqs);
4667 return -1;
4668 } else if (num == 0) {
4669 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
4670 } else {
4671 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4672 }
4673 }
4674
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004675 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004676 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004677}
4678
4679
4680static struct wpa_supplicant *
4681wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4682 int go)
4683{
4684 struct wpa_supplicant *group_wpa_s;
4685
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004686 if (!wpas_p2p_create_iface(wpa_s)) {
4687 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4688 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004689 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004691 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692
4693 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004694 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004695 wpa_msg_global(wpa_s, MSG_ERROR,
4696 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004697 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004698 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004699 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4700 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004701 wpa_msg_global(wpa_s, MSG_ERROR,
4702 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703 wpas_p2p_remove_pending_group_interface(wpa_s);
4704 return NULL;
4705 }
4706
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004707 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4708 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004709 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004710 return group_wpa_s;
4711}
4712
4713
4714/**
4715 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4716 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4717 * @persistent_group: Whether to create a persistent group
4718 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004719 * @ht40: Start GO with 40 MHz channel width
4720 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004721 * Returns: 0 on success, -1 on failure
4722 *
4723 * This function creates a new P2P group with the local end as the Group Owner,
4724 * i.e., without using Group Owner Negotiation.
4725 */
4726int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004727 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004728{
4729 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004730
4731 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4732 return -1;
4733
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004734 os_free(wpa_s->global->add_psk);
4735 wpa_s->global->add_psk = NULL;
4736
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004737 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004738 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004739 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004740
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004741 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4742 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004744
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004745 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004746 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004747 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004748 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004749 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4750 "(%u MHz) is not supported for P2P uses",
4751 params.freq);
4752 return -1;
4753 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004754 p2p_go_params(wpa_s->global->p2p, &params);
4755 params.persistent_group = persistent_group;
4756
4757 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4758 if (wpa_s == NULL)
4759 return -1;
4760 wpas_start_wps_go(wpa_s, &params, 0);
4761
4762 return 0;
4763}
4764
4765
4766static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4767 struct wpa_ssid *params, int addr_allocated)
4768{
4769 struct wpa_ssid *ssid;
4770
4771 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4772 if (wpa_s == NULL)
4773 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004774 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004775
4776 wpa_supplicant_ap_deinit(wpa_s);
4777
4778 ssid = wpa_config_add_network(wpa_s->conf);
4779 if (ssid == NULL)
4780 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781 wpa_config_set_network_defaults(ssid);
4782 ssid->temporary = 1;
4783 ssid->proto = WPA_PROTO_RSN;
4784 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4785 ssid->group_cipher = WPA_CIPHER_CCMP;
4786 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4787 ssid->ssid = os_malloc(params->ssid_len);
4788 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004789 wpa_config_remove_network(wpa_s->conf, ssid->id);
4790 return -1;
4791 }
4792 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4793 ssid->ssid_len = params->ssid_len;
4794 ssid->p2p_group = 1;
4795 ssid->export_keys = 1;
4796 if (params->psk_set) {
4797 os_memcpy(ssid->psk, params->psk, 32);
4798 ssid->psk_set = 1;
4799 }
4800 if (params->passphrase)
4801 ssid->passphrase = os_strdup(params->passphrase);
4802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004803 wpa_s->show_group_started = 1;
4804
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08004805 wpa_supplicant_select_network(wpa_s, ssid);
4806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004807 return 0;
4808}
4809
4810
4811int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4812 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004813 int freq, int ht40, int vht,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004814 const struct p2p_channels *channels,
4815 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816{
4817 struct p2p_go_neg_results params;
4818 int go = 0;
4819
4820 if (ssid->disabled != 2 || ssid->ssid == NULL)
4821 return -1;
4822
4823 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4824 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4825 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4826 "already running");
4827 return 0;
4828 }
4829
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004830 os_free(wpa_s->global->add_psk);
4831 wpa_s->global->add_psk = NULL;
4832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004833 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004834 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835
Dmitry Shmidt04949592012-07-19 12:16:46 -07004836 wpa_s->p2p_fallback_to_go_neg = 0;
4837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004838 if (ssid->mode == WPAS_MODE_INFRA)
4839 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4840
4841 if (ssid->mode != WPAS_MODE_P2P_GO)
4842 return -1;
4843
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004844 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4845 if (freq < 0)
4846 return -1;
4847
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004848 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004849 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004850
4851 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004852 params.psk_set = ssid->psk_set;
4853 if (params.psk_set)
4854 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004855 if (ssid->passphrase) {
4856 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4857 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4858 "persistent group");
4859 return -1;
4860 }
4861 os_strlcpy(params.passphrase, ssid->passphrase,
4862 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4865 params.ssid_len = ssid->ssid_len;
4866 params.persistent_group = 1;
4867
4868 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4869 if (wpa_s == NULL)
4870 return -1;
4871
Dmitry Shmidt56052862013-10-04 10:23:25 -07004872 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004873 wpas_start_wps_go(wpa_s, &params, 0);
4874
4875 return 0;
4876}
4877
4878
4879static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4880 struct wpabuf *proberesp_ies)
4881{
4882 struct wpa_supplicant *wpa_s = ctx;
4883 if (wpa_s->ap_iface) {
4884 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004885 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4886 wpabuf_free(beacon_ies);
4887 wpabuf_free(proberesp_ies);
4888 return;
4889 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004890 if (beacon_ies) {
4891 wpabuf_free(hapd->p2p_beacon_ie);
4892 hapd->p2p_beacon_ie = beacon_ies;
4893 }
4894 wpabuf_free(hapd->p2p_probe_resp_ie);
4895 hapd->p2p_probe_resp_ie = proberesp_ies;
4896 } else {
4897 wpabuf_free(beacon_ies);
4898 wpabuf_free(proberesp_ies);
4899 }
4900 wpa_supplicant_ap_update_beacon(wpa_s);
4901}
4902
4903
4904static void wpas_p2p_idle_update(void *ctx, int idle)
4905{
4906 struct wpa_supplicant *wpa_s = ctx;
4907 if (!wpa_s->ap_iface)
4908 return;
4909 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004910 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004911 wpas_p2p_set_group_idle_timeout(wpa_s);
4912 else
4913 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4914}
4915
4916
4917struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004918 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004919{
4920 struct p2p_group *group;
4921 struct p2p_group_config *cfg;
4922
4923 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4924 return NULL;
4925 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4926 return NULL;
4927
4928 cfg = os_zalloc(sizeof(*cfg));
4929 if (cfg == NULL)
4930 return NULL;
4931
Dmitry Shmidt04949592012-07-19 12:16:46 -07004932 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004933 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004934 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004935 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004936 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4937 if (wpa_s->max_stations &&
4938 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4939 cfg->max_clients = wpa_s->max_stations;
4940 else
4941 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004942 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4943 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 cfg->cb_ctx = wpa_s;
4945 cfg->ie_update = wpas_p2p_ie_update;
4946 cfg->idle_update = wpas_p2p_idle_update;
4947
4948 group = p2p_group_init(wpa_s->global->p2p, cfg);
4949 if (group == NULL)
4950 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004951 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 p2p_group_notif_formation_done(group);
4953 wpa_s->p2p_group = group;
4954 return group;
4955}
4956
4957
4958void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4959 int registrar)
4960{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004961 struct wpa_ssid *ssid = wpa_s->current_ssid;
4962
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004963 if (!wpa_s->p2p_in_provisioning) {
4964 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4965 "provisioning not in progress");
4966 return;
4967 }
4968
Dmitry Shmidt04949592012-07-19 12:16:46 -07004969 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4970 u8 go_dev_addr[ETH_ALEN];
4971 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4972 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4973 ssid->ssid_len);
4974 /* Clear any stored provisioning info */
4975 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4976 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004978 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4979 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004980 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004981 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4982 /*
4983 * Use a separate timeout for initial data connection to
4984 * complete to allow the group to be removed automatically if
4985 * something goes wrong in this step before the P2P group idle
4986 * timeout mechanism is taken into use.
4987 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07004988 wpa_dbg(wpa_s, MSG_DEBUG,
4989 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4990 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004991 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4992 wpas_p2p_group_formation_timeout,
4993 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004994 } else if (ssid) {
4995 /*
4996 * Use a separate timeout for initial data connection to
4997 * complete to allow the group to be removed automatically if
4998 * the client does not complete data connection successfully.
4999 */
5000 wpa_dbg(wpa_s, MSG_DEBUG,
5001 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5002 P2P_MAX_INITIAL_CONN_WAIT_GO);
5003 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5004 wpas_p2p_group_formation_timeout,
5005 wpa_s->parent, NULL);
5006 /*
5007 * Complete group formation on first successful data connection
5008 */
5009 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005010 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005011 if (wpa_s->global->p2p)
5012 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5013 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5014 wpa_drv_wps_success_cb(wpa_s, peer_addr);
5015 wpas_group_formation_completed(wpa_s, 1);
5016}
5017
5018
Jouni Malinen75ecf522011-06-27 15:19:46 -07005019void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5020 struct wps_event_fail *fail)
5021{
5022 if (!wpa_s->p2p_in_provisioning) {
5023 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5024 "provisioning not in progress");
5025 return;
5026 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005027
5028 if (wpa_s->go_params) {
5029 p2p_clear_provisioning_info(
5030 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005031 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005032 }
5033
Jouni Malinen75ecf522011-06-27 15:19:46 -07005034 wpas_notify_p2p_wps_failed(wpa_s, fail);
5035}
5036
5037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005039 const char *config_method,
5040 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005041{
5042 u16 config_methods;
5043
Dmitry Shmidt04949592012-07-19 12:16:46 -07005044 wpa_s->p2p_fallback_to_go_neg = 0;
5045 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005046 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005047 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005048 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005049 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005050 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5051 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005052 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005053 else {
5054 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005055 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005056 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057
Dmitry Shmidt04949592012-07-19 12:16:46 -07005058 if (use == WPAS_P2P_PD_AUTO) {
5059 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5060 wpa_s->pending_pd_config_methods = config_methods;
5061 wpa_s->p2p_auto_pd = 1;
5062 wpa_s->p2p_auto_join = 0;
5063 wpa_s->pending_pd_before_join = 0;
5064 wpa_s->auto_pd_scan_retry = 0;
5065 wpas_p2p_stop_find(wpa_s);
5066 wpa_s->p2p_join_scan_count = 0;
5067 os_get_time(&wpa_s->p2p_auto_started);
5068 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5069 wpa_s->p2p_auto_started.sec,
5070 wpa_s->p2p_auto_started.usec);
5071 wpas_p2p_join_scan(wpa_s, NULL);
5072 return 0;
5073 }
5074
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005075 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5076 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005077 config_methods,
5078 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079 }
5080
5081 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5082 return -1;
5083
5084 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005085 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005086 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005087}
5088
5089
5090int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5091 char *end)
5092{
5093 return p2p_scan_result_text(ies, ies_len, buf, end);
5094}
5095
5096
5097static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5098{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005099 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005100 return;
5101
5102 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5103 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005104 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105}
5106
5107
5108int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5109 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005110 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005111 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112{
5113 wpas_p2p_clear_pending_action_tx(wpa_s);
5114 wpa_s->p2p_long_listen = 0;
5115
5116 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5117 return wpa_drv_p2p_find(wpa_s, timeout, type);
5118
Dmitry Shmidt04949592012-07-19 12:16:46 -07005119 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5120 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121 return -1;
5122
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005123 wpa_supplicant_cancel_sched_scan(wpa_s);
5124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005126 num_req_dev_types, req_dev_types, dev_id,
5127 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005128}
5129
5130
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005131static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005132{
5133 wpas_p2p_clear_pending_action_tx(wpa_s);
5134 wpa_s->p2p_long_listen = 0;
5135 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5136 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07005137 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005138
5139 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5140 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005141 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005142 }
5143
5144 if (wpa_s->global->p2p)
5145 p2p_stop_find(wpa_s->global->p2p);
5146
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005147 return 0;
5148}
5149
5150
5151void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5152{
5153 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5154 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005155 wpas_p2p_remove_pending_group_interface(wpa_s);
5156}
5157
5158
5159static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5160{
5161 struct wpa_supplicant *wpa_s = eloop_ctx;
5162 wpa_s->p2p_long_listen = 0;
5163}
5164
5165
5166int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5167{
5168 int res;
5169
5170 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5171 return -1;
5172
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005173 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174 wpas_p2p_clear_pending_action_tx(wpa_s);
5175
5176 if (timeout == 0) {
5177 /*
5178 * This is a request for unlimited Listen state. However, at
5179 * least for now, this is mapped to a Listen state for one
5180 * hour.
5181 */
5182 timeout = 3600;
5183 }
5184 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5185 wpa_s->p2p_long_listen = 0;
5186
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005187 /*
5188 * Stop previous find/listen operation to avoid trying to request a new
5189 * remain-on-channel operation while the driver is still running the
5190 * previous one.
5191 */
5192 if (wpa_s->global->p2p)
5193 p2p_stop_find(wpa_s->global->p2p);
5194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005195 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5196 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5197 wpa_s->p2p_long_listen = timeout * 1000;
5198 eloop_register_timeout(timeout, 0,
5199 wpas_p2p_long_listen_timeout,
5200 wpa_s, NULL);
5201 }
5202
5203 return res;
5204}
5205
5206
5207int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5208 u8 *buf, size_t len, int p2p_group)
5209{
5210 struct wpabuf *p2p_ie;
5211 int ret;
5212
5213 if (wpa_s->global->p2p_disabled)
5214 return -1;
5215 if (wpa_s->global->p2p == NULL)
5216 return -1;
5217 if (bss == NULL)
5218 return -1;
5219
5220 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5221 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5222 p2p_group, p2p_ie);
5223 wpabuf_free(p2p_ie);
5224
5225 return ret;
5226}
5227
5228
5229int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005230 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005231 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005232{
5233 if (wpa_s->global->p2p_disabled)
5234 return 0;
5235 if (wpa_s->global->p2p == NULL)
5236 return 0;
5237
Dmitry Shmidt04949592012-07-19 12:16:46 -07005238 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5239 ie, ie_len)) {
5240 case P2P_PREQ_NOT_P2P:
5241 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5242 ssi_signal);
5243 /* fall through */
5244 case P2P_PREQ_MALFORMED:
5245 case P2P_PREQ_NOT_LISTEN:
5246 case P2P_PREQ_NOT_PROCESSED:
5247 default: /* make gcc happy */
5248 return 0;
5249 case P2P_PREQ_PROCESSED:
5250 return 1;
5251 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005252}
5253
5254
5255void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5256 const u8 *sa, const u8 *bssid,
5257 u8 category, const u8 *data, size_t len, int freq)
5258{
5259 if (wpa_s->global->p2p_disabled)
5260 return;
5261 if (wpa_s->global->p2p == NULL)
5262 return;
5263
5264 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5265 freq);
5266}
5267
5268
5269void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5270{
5271 if (wpa_s->global->p2p_disabled)
5272 return;
5273 if (wpa_s->global->p2p == NULL)
5274 return;
5275
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005276 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005277}
5278
5279
5280void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5281{
5282 p2p_group_deinit(wpa_s->p2p_group);
5283 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005284
5285 wpa_s->ap_configured_cb = NULL;
5286 wpa_s->ap_configured_cb_ctx = NULL;
5287 wpa_s->ap_configured_cb_data = NULL;
5288 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005289}
5290
5291
5292int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5293{
5294 wpa_s->p2p_long_listen = 0;
5295
5296 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5297 return wpa_drv_p2p_reject(wpa_s, addr);
5298
5299 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5300 return -1;
5301
5302 return p2p_reject(wpa_s->global->p2p, addr);
5303}
5304
5305
5306/* Invite to reinvoke a persistent group */
5307int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005308 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005309 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005310{
5311 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005312 u8 *bssid = NULL;
5313 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005314 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005315 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005316
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005317 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005318 if (peer_addr)
5319 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5320 else
5321 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005322
Jouni Malinen31be0a42012-08-31 21:20:51 +03005323 wpa_s->p2p_persistent_go_freq = freq;
5324 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005325 if (ssid->mode == WPAS_MODE_P2P_GO) {
5326 role = P2P_INVITE_ROLE_GO;
5327 if (peer_addr == NULL) {
5328 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5329 "address in invitation command");
5330 return -1;
5331 }
5332 if (wpas_p2p_create_iface(wpa_s)) {
5333 if (wpas_p2p_add_group_interface(wpa_s,
5334 WPA_IF_P2P_GO) < 0) {
5335 wpa_printf(MSG_ERROR, "P2P: Failed to "
5336 "allocate a new interface for the "
5337 "group");
5338 return -1;
5339 }
5340 bssid = wpa_s->pending_interface_addr;
5341 } else
5342 bssid = wpa_s->own_addr;
5343 } else {
5344 role = P2P_INVITE_ROLE_CLIENT;
5345 peer_addr = ssid->bssid;
5346 }
5347 wpa_s->pending_invite_ssid_id = ssid->id;
5348
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005349 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5350 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005351 if (res)
5352 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005354 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5355 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5356 ssid->ssid, ssid->ssid_len,
5357 go_dev_addr, 1);
5358
5359 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5360 return -1;
5361
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005362 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5363 no_pref_freq_given && pref_freq > 0 &&
5364 wpa_s->num_multichan_concurrent > 1 &&
5365 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5366 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5367 pref_freq);
5368 pref_freq = 0;
5369 }
5370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005371 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005372 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5373 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005374}
5375
5376
5377/* Invite to join an active group */
5378int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5379 const u8 *peer_addr, const u8 *go_dev_addr)
5380{
5381 struct wpa_global *global = wpa_s->global;
5382 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005383 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005385 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005386 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005387 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005388
Jouni Malinen31be0a42012-08-31 21:20:51 +03005389 wpa_s->p2p_persistent_go_freq = 0;
5390 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005391 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005393 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5394 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5395 break;
5396 }
5397 if (wpa_s == NULL) {
5398 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5399 return -1;
5400 }
5401
5402 ssid = wpa_s->current_ssid;
5403 if (ssid == NULL) {
5404 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5405 "invitation");
5406 return -1;
5407 }
5408
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005409 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005410 persistent = ssid->p2p_persistent_group &&
5411 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5412 ssid->ssid, ssid->ssid_len);
5413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005414 if (ssid->mode == WPAS_MODE_P2P_GO) {
5415 role = P2P_INVITE_ROLE_ACTIVE_GO;
5416 bssid = wpa_s->own_addr;
5417 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005418 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005419 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005420 } else {
5421 role = P2P_INVITE_ROLE_CLIENT;
5422 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5423 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5424 "invite to current group");
5425 return -1;
5426 }
5427 bssid = wpa_s->bssid;
5428 if (go_dev_addr == NULL &&
5429 !is_zero_ether_addr(wpa_s->go_dev_addr))
5430 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005431 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5432 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005433 }
5434 wpa_s->parent->pending_invite_ssid_id = -1;
5435
5436 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5437 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5438 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005439 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440
5441 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5442 return -1;
5443
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005444 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5445 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005446 if (res)
5447 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005448 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005451 ssid->ssid, ssid->ssid_len, force_freq,
5452 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005453}
5454
5455
5456void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5457{
5458 struct wpa_ssid *ssid = wpa_s->current_ssid;
5459 const char *ssid_txt;
5460 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005461 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005463 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005464
Dmitry Shmidt04949592012-07-19 12:16:46 -07005465 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5466 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5467 wpa_s->parent, NULL);
5468 }
5469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005470 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005471 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472
5473 wpa_s->show_group_started = 0;
5474
5475 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5476 os_memset(go_dev_addr, 0, ETH_ALEN);
5477 if (ssid->bssid_set)
5478 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5479 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5480 ssid->ssid_len);
5481 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5482
5483 if (wpa_s->global->p2p_group_formation == wpa_s)
5484 wpa_s->global->p2p_group_formation = NULL;
5485
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005486 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5487 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005488 if (ssid->passphrase == NULL && ssid->psk_set) {
5489 char psk[65];
5490 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005491 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5492 "%s client ssid=\"%s\" freq=%d psk=%s "
5493 "go_dev_addr=" MACSTR "%s",
5494 wpa_s->ifname, ssid_txt, freq, psk,
5495 MAC2STR(go_dev_addr),
5496 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005497 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005498 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5499 "%s client ssid=\"%s\" freq=%d "
5500 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5501 wpa_s->ifname, ssid_txt, freq,
5502 ssid->passphrase ? ssid->passphrase : "",
5503 MAC2STR(go_dev_addr),
5504 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005505 }
5506
5507 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005508 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5509 ssid, go_dev_addr);
5510 if (network_id < 0)
5511 network_id = ssid->id;
5512 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005513
5514done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005515 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005516}
5517
5518
5519int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5520 u32 interval1, u32 duration2, u32 interval2)
5521{
5522 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5523 return -1;
5524 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5525 return -1;
5526
5527 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5528 wpa_s->current_ssid == NULL ||
5529 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5530 return -1;
5531
5532 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5533 wpa_s->own_addr, wpa_s->assoc_freq,
5534 duration1, interval1, duration2, interval2);
5535}
5536
5537
5538int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5539 unsigned int interval)
5540{
5541 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5542 return -1;
5543
5544 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5545 return -1;
5546
5547 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5548}
5549
5550
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005551static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5552{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005553 if (wpa_s->current_ssid == NULL) {
5554 /*
5555 * current_ssid can be cleared when P2P client interface gets
5556 * disconnected, so assume this interface was used as P2P
5557 * client.
5558 */
5559 return 1;
5560 }
5561 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005562 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5563}
5564
5565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005566static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5567{
5568 struct wpa_supplicant *wpa_s = eloop_ctx;
5569
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005570 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005571 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5572 "disabled");
5573 return;
5574 }
5575
Dmitry Shmidt04949592012-07-19 12:16:46 -07005576 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5577 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005578 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005579}
5580
5581
5582static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5583{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005584 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005585
Dmitry Shmidt04949592012-07-19 12:16:46 -07005586 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5587 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5590 return;
5591
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005592 timeout = wpa_s->conf->p2p_group_idle;
5593 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5594 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5595 timeout = P2P_MAX_CLIENT_IDLE;
5596
5597 if (timeout == 0)
5598 return;
5599
Dmitry Shmidt04949592012-07-19 12:16:46 -07005600 if (timeout < 0) {
5601 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5602 timeout = 0; /* special client mode no-timeout */
5603 else
5604 return;
5605 }
5606
5607 if (wpa_s->p2p_in_provisioning) {
5608 /*
5609 * Use the normal group formation timeout during the
5610 * provisioning phase to avoid terminating this process too
5611 * early due to group idle timeout.
5612 */
5613 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5614 "during provisioning");
5615 return;
5616 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305617
Dmitry Shmidt04949592012-07-19 12:16:46 -07005618 if (wpa_s->show_group_started) {
5619 /*
5620 * Use the normal group formation timeout between the end of
5621 * the provisioning phase and completion of 4-way handshake to
5622 * avoid terminating this process too early due to group idle
5623 * timeout.
5624 */
5625 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5626 "while waiting for initial 4-way handshake to "
5627 "complete");
5628 return;
5629 }
5630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005631 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005632 timeout);
5633 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5634 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635}
5636
5637
Jouni Malinen2b89da82012-08-31 22:04:41 +03005638/* Returns 1 if the interface was removed */
5639int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5640 u16 reason_code, const u8 *ie, size_t ie_len,
5641 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005642{
5643 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005644 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005645 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005646 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005647
Dmitry Shmidt04949592012-07-19 12:16:46 -07005648 if (!locally_generated)
5649 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5650 ie_len);
5651
5652 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5653 wpa_s->current_ssid &&
5654 wpa_s->current_ssid->p2p_group &&
5655 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5656 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5657 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005658 if (wpas_p2p_group_delete(wpa_s,
5659 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5660 > 0)
5661 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005662 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005663
5664 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005665}
5666
5667
5668void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005669 u16 reason_code, const u8 *ie, size_t ie_len,
5670 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005671{
5672 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5673 return;
5674 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5675 return;
5676
Dmitry Shmidt04949592012-07-19 12:16:46 -07005677 if (!locally_generated)
5678 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5679 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680}
5681
5682
5683void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5684{
5685 struct p2p_data *p2p = wpa_s->global->p2p;
5686
5687 if (p2p == NULL)
5688 return;
5689
5690 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5691 return;
5692
5693 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5694 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5695
5696 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5697 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5698
5699 if (wpa_s->wps &&
5700 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5701 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5702
5703 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5704 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5705
5706 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5707 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5708 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5709 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5710 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5711 }
5712
5713 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5714 p2p_set_sec_dev_types(p2p,
5715 (void *) wpa_s->conf->sec_device_type,
5716 wpa_s->conf->num_sec_device_types);
5717
5718 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5719 int i;
5720 p2p_remove_wps_vendor_extensions(p2p);
5721 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5722 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5723 continue;
5724 p2p_add_wps_vendor_extension(
5725 p2p, wpa_s->conf->wps_vendor_ext[i]);
5726 }
5727 }
5728
5729 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5730 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5731 char country[3];
5732 country[0] = wpa_s->conf->country[0];
5733 country[1] = wpa_s->conf->country[1];
5734 country[2] = 0x04;
5735 p2p_set_country(p2p, country);
5736 }
5737
5738 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5739 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5740 wpa_s->conf->p2p_ssid_postfix ?
5741 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5742 0);
5743 }
5744
5745 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5746 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005747
5748 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5749 u8 reg_class, channel;
5750 int ret;
5751 unsigned int r;
5752 if (wpa_s->conf->p2p_listen_reg_class &&
5753 wpa_s->conf->p2p_listen_channel) {
5754 reg_class = wpa_s->conf->p2p_listen_reg_class;
5755 channel = wpa_s->conf->p2p_listen_channel;
5756 } else {
5757 reg_class = 81;
5758 /*
5759 * Pick one of the social channels randomly as the
5760 * listen channel.
5761 */
5762 os_get_random((u8 *) &r, sizeof(r));
5763 channel = 1 + (r % 3) * 5;
5764 }
5765 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5766 if (ret)
5767 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5768 "failed: %d", ret);
5769 }
5770 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5771 u8 op_reg_class, op_channel, cfg_op_channel;
5772 int ret = 0;
5773 unsigned int r;
5774 if (wpa_s->conf->p2p_oper_reg_class &&
5775 wpa_s->conf->p2p_oper_channel) {
5776 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5777 op_channel = wpa_s->conf->p2p_oper_channel;
5778 cfg_op_channel = 1;
5779 } else {
5780 op_reg_class = 81;
5781 /*
5782 * Use random operation channel from (1, 6, 11)
5783 *if no other preference is indicated.
5784 */
5785 os_get_random((u8 *) &r, sizeof(r));
5786 op_channel = 1 + (r % 3) * 5;
5787 cfg_op_channel = 0;
5788 }
5789 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5790 cfg_op_channel);
5791 if (ret)
5792 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5793 "failed: %d", ret);
5794 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005795
5796 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5797 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5798 wpa_s->conf->p2p_pref_chan) < 0) {
5799 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5800 "update failed");
5801 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005802
5803 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
5804 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
5805 "update failed");
5806 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005807 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005808}
5809
5810
5811int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5812 int duration)
5813{
5814 if (!wpa_s->ap_iface)
5815 return -1;
5816 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5817 duration);
5818}
5819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005820
5821int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5822{
5823 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5824 return -1;
5825 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5826 return -1;
5827
5828 wpa_s->global->cross_connection = enabled;
5829 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5830
5831 if (!enabled) {
5832 struct wpa_supplicant *iface;
5833
5834 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5835 {
5836 if (iface->cross_connect_enabled == 0)
5837 continue;
5838
5839 iface->cross_connect_enabled = 0;
5840 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005841 wpa_msg_global(iface->parent, MSG_INFO,
5842 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5843 iface->ifname,
5844 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005845 }
5846 }
5847
5848 return 0;
5849}
5850
5851
5852static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5853{
5854 struct wpa_supplicant *iface;
5855
5856 if (!uplink->global->cross_connection)
5857 return;
5858
5859 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5860 if (!iface->cross_connect_enabled)
5861 continue;
5862 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5863 0)
5864 continue;
5865 if (iface->ap_iface == NULL)
5866 continue;
5867 if (iface->cross_connect_in_use)
5868 continue;
5869
5870 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005871 wpa_msg_global(iface->parent, MSG_INFO,
5872 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5873 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005874 }
5875}
5876
5877
5878static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5879{
5880 struct wpa_supplicant *iface;
5881
5882 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5883 if (!iface->cross_connect_enabled)
5884 continue;
5885 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5886 0)
5887 continue;
5888 if (!iface->cross_connect_in_use)
5889 continue;
5890
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005891 wpa_msg_global(iface->parent, MSG_INFO,
5892 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5893 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005894 iface->cross_connect_in_use = 0;
5895 }
5896}
5897
5898
5899void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5900{
5901 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5902 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5903 wpa_s->cross_connect_disallowed)
5904 wpas_p2p_disable_cross_connect(wpa_s);
5905 else
5906 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005907 if (!wpa_s->ap_iface &&
5908 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5909 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005910}
5911
5912
5913void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5914{
5915 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005916 if (!wpa_s->ap_iface &&
5917 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5918 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005919 wpas_p2p_set_group_idle_timeout(wpa_s);
5920}
5921
5922
5923static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5924{
5925 struct wpa_supplicant *iface;
5926
5927 if (!wpa_s->global->cross_connection)
5928 return;
5929
5930 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5931 if (iface == wpa_s)
5932 continue;
5933 if (iface->drv_flags &
5934 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5935 continue;
5936 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5937 continue;
5938
5939 wpa_s->cross_connect_enabled = 1;
5940 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5941 sizeof(wpa_s->cross_connect_uplink));
5942 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5943 "%s to %s whenever uplink is available",
5944 wpa_s->ifname, wpa_s->cross_connect_uplink);
5945
5946 if (iface->ap_iface || iface->current_ssid == NULL ||
5947 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5948 iface->cross_connect_disallowed ||
5949 iface->wpa_state != WPA_COMPLETED)
5950 break;
5951
5952 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005953 wpa_msg_global(wpa_s->parent, MSG_INFO,
5954 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5955 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005956 break;
5957 }
5958}
5959
5960
5961int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5962{
5963 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5964 !wpa_s->p2p_in_provisioning)
5965 return 0; /* not P2P client operation */
5966
5967 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5968 "session overlap");
5969 if (wpa_s != wpa_s->parent)
5970 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005971 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005972 return 1;
5973}
5974
5975
5976void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5977{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005978 struct p2p_channels chan, cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005979
5980 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5981 return;
5982
5983 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005984 os_memset(&cli_chan, 0, sizeof(cli_chan));
5985 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005986 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5987 "channel list");
5988 return;
5989 }
5990
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005991 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005992}
5993
5994
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005995static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5996 struct wpa_scan_results *scan_res)
5997{
5998 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5999}
6000
6001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006002int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6003{
6004 struct wpa_global *global = wpa_s->global;
6005 int found = 0;
6006 const u8 *peer;
6007
6008 if (global->p2p == NULL)
6009 return -1;
6010
6011 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6012
6013 if (wpa_s->pending_interface_name[0] &&
6014 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6015 found = 1;
6016
6017 peer = p2p_get_go_neg_peer(global->p2p);
6018 if (peer) {
6019 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6020 MACSTR, MAC2STR(peer));
6021 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006022 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006023 }
6024
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006025 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6026 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6027 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6028 found = 1;
6029 }
6030
6031 if (wpa_s->pending_pd_before_join) {
6032 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6033 wpa_s->pending_pd_before_join = 0;
6034 found = 1;
6035 }
6036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037 wpas_p2p_stop_find(wpa_s);
6038
6039 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6040 if (wpa_s == global->p2p_group_formation &&
6041 (wpa_s->p2p_in_provisioning ||
6042 wpa_s->parent->pending_interface_type ==
6043 WPA_IF_P2P_CLIENT)) {
6044 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6045 "formation found - cancelling",
6046 wpa_s->ifname);
6047 found = 1;
6048 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6049 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006050 if (wpa_s->p2p_in_provisioning) {
6051 wpas_group_formation_completed(wpa_s, 0);
6052 break;
6053 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006054 wpas_p2p_group_delete(wpa_s,
6055 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056 break;
6057 }
6058 }
6059
6060 if (!found) {
6061 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6062 return -1;
6063 }
6064
6065 return 0;
6066}
6067
6068
6069void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6070{
6071 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6072 return;
6073
6074 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6075 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006076 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006077}
6078
6079
6080void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6081 int freq_24, int freq_5, int freq_overall)
6082{
6083 struct p2p_data *p2p = wpa_s->global->p2p;
6084 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6085 return;
6086 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6087}
6088
6089
6090int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6091{
6092 u8 peer[ETH_ALEN];
6093 struct p2p_data *p2p = wpa_s->global->p2p;
6094
6095 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6096 return -1;
6097
6098 if (hwaddr_aton(addr, peer))
6099 return -1;
6100
6101 return p2p_unauthorize(p2p, peer);
6102}
6103
6104
6105/**
6106 * wpas_p2p_disconnect - Disconnect from a P2P Group
6107 * @wpa_s: Pointer to wpa_supplicant data
6108 * Returns: 0 on success, -1 on failure
6109 *
6110 * This can be used to disconnect from a group in which the local end is a P2P
6111 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6112 * virtual network interface was created for this group, that interface will be
6113 * removed. Otherwise, only the configured P2P group network will be removed
6114 * from the interface.
6115 */
6116int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6117{
6118
6119 if (wpa_s == NULL)
6120 return -1;
6121
Jouni Malinen2b89da82012-08-31 22:04:41 +03006122 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6123 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006124}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006125
6126
6127int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6128{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006129 int ret;
6130
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006131 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6132 return 0;
6133
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006134 ret = p2p_in_progress(wpa_s->global->p2p);
6135 if (ret == 0) {
6136 /*
6137 * Check whether there is an ongoing WPS provisioning step (or
6138 * other parts of group formation) on another interface since
6139 * p2p_in_progress() does not report this to avoid issues for
6140 * scans during such provisioning step.
6141 */
6142 if (wpa_s->global->p2p_group_formation &&
6143 wpa_s->global->p2p_group_formation != wpa_s) {
6144 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6145 "in group formation",
6146 wpa_s->global->p2p_group_formation->ifname);
6147 ret = 1;
6148 }
6149 }
6150
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006151 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6152 struct os_time now;
6153 os_get_time(&now);
6154 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
6155 P2P_MAX_INITIAL_CONN_WAIT_GO) {
6156 /* Wait for the first client has expired */
6157 wpa_s->global->p2p_go_wait_client.sec = 0;
6158 } else {
6159 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6160 ret = 1;
6161 }
6162 }
6163
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006164 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006165}
6166
6167
6168void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6169 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006170{
6171 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6172 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6173 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006174 /**
6175 * Remove the network by scheduling the group formation
6176 * timeout to happen immediately. The teardown code
6177 * needs to be scheduled to run asynch later so that we
6178 * don't delete data from under ourselves unexpectedly.
6179 * Calling wpas_p2p_group_formation_timeout directly
6180 * causes a series of crashes in WPS failure scenarios.
6181 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006182 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6183 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006184 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6185 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006186 }
6187}
6188
6189
6190struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006191 const u8 *addr, const u8 *ssid,
6192 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006193{
6194 struct wpa_ssid *s;
6195 size_t i;
6196
6197 for (s = wpa_s->conf->ssid; s; s = s->next) {
6198 if (s->disabled != 2)
6199 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006200 if (ssid &&
6201 (ssid_len != s->ssid_len ||
6202 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6203 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006204 if (addr == NULL) {
6205 if (s->mode == WPAS_MODE_P2P_GO)
6206 return s;
6207 continue;
6208 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006209 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6210 return s; /* peer is GO in the persistent group */
6211 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6212 continue;
6213 for (i = 0; i < s->num_p2p_clients; i++) {
6214 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6215 addr, ETH_ALEN) == 0)
6216 return s; /* peer is P2P client in persistent
6217 * group */
6218 }
6219 }
6220
6221 return NULL;
6222}
6223
6224
6225void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6226 const u8 *addr)
6227{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006228 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6229 wpa_s->parent, NULL) > 0) {
6230 /*
6231 * This can happen if WPS provisioning step is not terminated
6232 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6233 * peer was able to connect, there is no need to time out group
6234 * formation after this, though. In addition, this is used with
6235 * the initial connection wait on the GO as a separate formation
6236 * timeout and as such, expected to be hit after the initial WPS
6237 * provisioning step.
6238 */
6239 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6240 }
6241 if (!wpa_s->p2p_go_group_formation_completed) {
6242 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6243 wpa_s->p2p_go_group_formation_completed = 1;
6244 wpa_s->global->p2p_group_formation = NULL;
6245 wpa_s->p2p_in_provisioning = 0;
6246 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006247 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006248 if (addr == NULL)
6249 return;
6250 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6251}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006252
Dmitry Shmidt04949592012-07-19 12:16:46 -07006253
6254static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6255 int group_added)
6256{
6257 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006258 if (wpa_s->global->p2p_group_formation)
6259 group = wpa_s->global->p2p_group_formation;
6260 wpa_s = wpa_s->parent;
6261 offchannel_send_action_done(wpa_s);
6262 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006263 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006264 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6265 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6266 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6267 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6268 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006269 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006270 wpa_s->p2p_go_ht40,
6271 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006272}
6273
6274
6275int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6276{
6277 if (!wpa_s->p2p_fallback_to_go_neg ||
6278 wpa_s->p2p_in_provisioning <= 5)
6279 return 0;
6280
6281 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6282 return 0; /* peer operating as a GO */
6283
6284 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6285 "fallback to GO Negotiation");
6286 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6287
6288 return 1;
6289}
6290
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006291
6292unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6293{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006294 struct wpa_supplicant *ifs;
6295
6296 if (wpa_s->wpa_state > WPA_SCANNING) {
6297 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6298 "concurrent operation",
6299 P2P_CONCURRENT_SEARCH_DELAY);
6300 return P2P_CONCURRENT_SEARCH_DELAY;
6301 }
6302
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006303 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6304 radio_list) {
6305 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006306 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6307 "delay due to concurrent operation on "
6308 "interface %s",
6309 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6310 return P2P_CONCURRENT_SEARCH_DELAY;
6311 }
6312 }
6313
6314 return 0;
6315}
6316
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006317
6318void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6319{
6320 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6321 "pending anymore (sta_scan_pending=%d "
6322 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6323 wpa_s->global->p2p_cb_on_scan_complete);
6324 wpa_s->sta_scan_pending = 0;
6325
6326 if (!wpa_s->global->p2p_cb_on_scan_complete)
6327 return;
6328 wpa_s->global->p2p_cb_on_scan_complete = 0;
6329
6330 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6331 return;
6332
6333 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6334 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6335 "continued after successful connection");
6336 p2p_increase_search_delay(wpa_s->global->p2p,
6337 wpas_p2p_search_delay(wpa_s));
6338 }
6339}
6340
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006341
6342static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6343 struct wpa_ssid *s, const u8 *addr,
6344 int iface_addr)
6345{
6346 struct psk_list_entry *psk, *tmp;
6347 int changed = 0;
6348
6349 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6350 list) {
6351 if ((iface_addr && !psk->p2p &&
6352 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6353 (!iface_addr && psk->p2p &&
6354 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6355 wpa_dbg(wpa_s, MSG_DEBUG,
6356 "P2P: Remove persistent group PSK list entry for "
6357 MACSTR " p2p=%u",
6358 MAC2STR(psk->addr), psk->p2p);
6359 dl_list_del(&psk->list);
6360 os_free(psk);
6361 changed++;
6362 }
6363 }
6364
6365 return changed;
6366}
6367
6368
6369void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6370 const u8 *p2p_dev_addr,
6371 const u8 *psk, size_t psk_len)
6372{
6373 struct wpa_ssid *ssid = wpa_s->current_ssid;
6374 struct wpa_ssid *persistent;
6375 struct psk_list_entry *p;
6376
6377 if (psk_len != sizeof(p->psk))
6378 return;
6379
6380 if (p2p_dev_addr) {
6381 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6382 " p2p_dev_addr=" MACSTR,
6383 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6384 if (is_zero_ether_addr(p2p_dev_addr))
6385 p2p_dev_addr = NULL;
6386 } else {
6387 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6388 MAC2STR(mac_addr));
6389 }
6390
6391 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6392 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6393 /* To be added to persistent group once created */
6394 if (wpa_s->global->add_psk == NULL) {
6395 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6396 if (wpa_s->global->add_psk == NULL)
6397 return;
6398 }
6399 p = wpa_s->global->add_psk;
6400 if (p2p_dev_addr) {
6401 p->p2p = 1;
6402 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6403 } else {
6404 p->p2p = 0;
6405 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6406 }
6407 os_memcpy(p->psk, psk, psk_len);
6408 return;
6409 }
6410
6411 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6412 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6413 return;
6414 }
6415
6416 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6417 ssid->ssid_len);
6418 if (!persistent) {
6419 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6420 return;
6421 }
6422
6423 p = os_zalloc(sizeof(*p));
6424 if (p == NULL)
6425 return;
6426 if (p2p_dev_addr) {
6427 p->p2p = 1;
6428 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6429 } else {
6430 p->p2p = 0;
6431 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6432 }
6433 os_memcpy(p->psk, psk, psk_len);
6434
6435 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6436 struct psk_list_entry *last;
6437 last = dl_list_last(&persistent->psk_list,
6438 struct psk_list_entry, list);
6439 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6440 MACSTR " (p2p=%u) to make room for a new one",
6441 MAC2STR(last->addr), last->p2p);
6442 dl_list_del(&last->list);
6443 os_free(last);
6444 }
6445
6446 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6447 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6448 p2p_dev_addr == NULL);
6449 if (p2p_dev_addr) {
6450 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6451 MACSTR, MAC2STR(p2p_dev_addr));
6452 } else {
6453 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6454 MAC2STR(mac_addr));
6455 }
6456 dl_list_add(&persistent->psk_list, &p->list);
6457
6458#ifndef CONFIG_NO_CONFIG_WRITE
6459 if (wpa_s->parent->conf->update_config &&
6460 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6461 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6462#endif /* CONFIG_NO_CONFIG_WRITE */
6463}
6464
6465
6466static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6467 struct wpa_ssid *s, const u8 *addr,
6468 int iface_addr)
6469{
6470 int res;
6471
6472 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6473 if (res > 0) {
6474#ifndef CONFIG_NO_CONFIG_WRITE
6475 if (wpa_s->conf->update_config &&
6476 wpa_config_write(wpa_s->confname, wpa_s->conf))
6477 wpa_dbg(wpa_s, MSG_DEBUG,
6478 "P2P: Failed to update configuration");
6479#endif /* CONFIG_NO_CONFIG_WRITE */
6480 }
6481}
6482
6483
6484static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6485 const u8 *peer, int iface_addr)
6486{
6487 struct hostapd_data *hapd;
6488 struct hostapd_wpa_psk *psk, *prev, *rem;
6489 struct sta_info *sta;
6490
6491 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6492 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6493 return;
6494
6495 /* Remove per-station PSK entry */
6496 hapd = wpa_s->ap_iface->bss[0];
6497 prev = NULL;
6498 psk = hapd->conf->ssid.wpa_psk;
6499 while (psk) {
6500 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6501 (!iface_addr &&
6502 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6503 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6504 MACSTR " iface_addr=%d",
6505 MAC2STR(peer), iface_addr);
6506 if (prev)
6507 prev->next = psk->next;
6508 else
6509 hapd->conf->ssid.wpa_psk = psk->next;
6510 rem = psk;
6511 psk = psk->next;
6512 os_free(rem);
6513 } else {
6514 prev = psk;
6515 psk = psk->next;
6516 }
6517 }
6518
6519 /* Disconnect from group */
6520 if (iface_addr)
6521 sta = ap_get_sta(hapd, peer);
6522 else
6523 sta = ap_get_sta_p2p(hapd, peer);
6524 if (sta) {
6525 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6526 " (iface_addr=%d) from group",
6527 MAC2STR(peer), iface_addr);
6528 hostapd_drv_sta_deauth(hapd, sta->addr,
6529 WLAN_REASON_DEAUTH_LEAVING);
6530 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6531 }
6532}
6533
6534
6535void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6536 int iface_addr)
6537{
6538 struct wpa_ssid *s;
6539 struct wpa_supplicant *w;
6540
6541 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6542
6543 /* Remove from any persistent group */
6544 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6545 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6546 continue;
6547 if (!iface_addr)
6548 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6549 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6550 }
6551
6552 /* Remove from any operating group */
6553 for (w = wpa_s->global->ifaces; w; w = w->next)
6554 wpas_p2p_remove_client_go(w, peer, iface_addr);
6555}
6556
6557
6558static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6559{
6560 struct wpa_supplicant *wpa_s = eloop_ctx;
6561 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6562}
6563
6564
6565int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6566{
6567 struct wpa_ssid *ssid = wpa_s->current_ssid;
6568
6569 if (ssid == NULL || !ssid->p2p_group)
6570 return 0;
6571
6572 if (wpa_s->p2p_last_4way_hs_fail &&
6573 wpa_s->p2p_last_4way_hs_fail == ssid) {
6574 u8 go_dev_addr[ETH_ALEN];
6575 struct wpa_ssid *persistent;
6576
6577 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6578 ssid->ssid,
6579 ssid->ssid_len) <= 0) {
6580 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6581 goto disconnect;
6582 }
6583
6584 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6585 MACSTR, MAC2STR(go_dev_addr));
6586 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6587 ssid->ssid,
6588 ssid->ssid_len);
6589 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6590 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6591 goto disconnect;
6592 }
6593 wpa_msg_global(wpa_s->parent, MSG_INFO,
6594 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6595 persistent->id);
6596 disconnect:
6597 wpa_s->p2p_last_4way_hs_fail = NULL;
6598 /*
6599 * Remove the group from a timeout to avoid issues with caller
6600 * continuing to use the interface if this is on a P2P group
6601 * interface.
6602 */
6603 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6604 wpa_s, NULL);
6605 return 1;
6606 }
6607
6608 wpa_s->p2p_last_4way_hs_fail = ssid;
6609 return 0;
6610}