blob: 239e6080cccd158fef50915f9e86f074183b2d8d [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "common/ieee802_11_common.h"
14#include "common/ieee802_11_defs.h"
15#include "common/wpa_ctrl.h"
16#include "wps/wps_i.h"
17#include "p2p/p2p.h"
18#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080019#include "ap/ap_config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070021#include "eapol_supp/eapol_supp_sm.h"
22#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "wpa_supplicant_i.h"
24#include "driver_i.h"
25#include "ap.h"
26#include "config_ssid.h"
27#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "notify.h"
29#include "scan.h"
30#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "wps_supplicant.h"
33#include "p2p_supplicant.h"
34
35
36/*
37 * How many times to try to scan to find the GO before giving up on join
38 * request.
39 */
40#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
41
Dmitry Shmidt04949592012-07-19 12:16:46 -070042#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080044#ifndef P2P_MAX_CLIENT_IDLE
45/*
46 * How many seconds to try to reconnect to the GO when connection in P2P client
47 * role has been lost.
48 */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070049#ifdef ANDROID_P2P
50#define P2P_MAX_CLIENT_IDLE 20
51#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080052#define P2P_MAX_CLIENT_IDLE 10
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070053#endif /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054#endif /* P2P_MAX_CLIENT_IDLE */
55
Dmitry Shmidt04949592012-07-19 12:16:46 -070056#ifndef P2P_MAX_INITIAL_CONN_WAIT
57/*
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
60 */
61#define P2P_MAX_INITIAL_CONN_WAIT 10
62#endif /* P2P_MAX_INITIAL_CONN_WAIT */
63
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070064#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
65/*
66 * How many seconds to wait for initial 4-way handshake to get completed after
67 * WPS provisioning step on the GO. This controls the extra time the P2P
68 * operation is considered to be in progress (e.g., to delay other scans) after
69 * WPS provisioning has been completed on the GO during group formation.
70 */
71#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
72#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
73
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070074#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070075#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070076#endif /* P2P_CONCURRENT_SEARCH_DELAY */
77
Dmitry Shmidt34af3062013-07-11 10:46:32 -070078#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
79
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070080enum p2p_group_removal_reason {
81 P2P_GROUP_REMOVAL_UNKNOWN,
82 P2P_GROUP_REMOVAL_SILENT,
83 P2P_GROUP_REMOVAL_FORMATION_FAILED,
84 P2P_GROUP_REMOVAL_REQUESTED,
85 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
86 P2P_GROUP_REMOVAL_UNAVAILABLE,
87 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
88#ifdef ANDROID_P2P
89 P2P_GROUP_REMOVAL_FREQ_CONFLICT
90#endif
91};
92
Jouni Malinendc7b7132012-09-14 12:53:47 -070093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
95static struct wpa_supplicant *
96wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
97 int go);
98static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070099static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
101static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700102 const u8 *dev_addr, enum p2p_wps_method wps_method,
103 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
105static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
106static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
107static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800108static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
109 void *timeout_ctx);
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700110#ifdef ANDROID_P2P
111static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
112#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700113static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
114 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800115static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116
117
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700118/*
119 * Get the number of concurrent channels that the HW can operate, but that are
120 * currently not in use by any of the wpa_supplicant interfaces.
121 */
122static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
123{
124 int *freqs;
125 int num;
126
127 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
128 if (!freqs)
129 return -1;
130
131 num = get_shared_radio_freqs(wpa_s, freqs,
132 wpa_s->num_multichan_concurrent);
133 os_free(freqs);
134
135 return wpa_s->num_multichan_concurrent - num;
136}
137
138
139/*
140 * Get the frequencies that are currently in use by one or more of the virtual
141 * interfaces, and that are also valid for P2P operation.
142 */
143static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
144 int *p2p_freqs, unsigned int len)
145{
146 int *freqs;
147 unsigned int num, i, j;
148
149 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
150 if (!freqs)
151 return -1;
152
153 num = get_shared_radio_freqs(wpa_s, freqs,
154 wpa_s->num_multichan_concurrent);
155
156 os_memset(p2p_freqs, 0, sizeof(int) * len);
157
158 for (i = 0, j = 0; i < num && j < len; i++) {
159 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
160 p2p_freqs[j++] = freqs[i];
161 }
162
163 os_free(freqs);
164
165 return j;
166}
167
168
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700169static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
170 int freq)
171{
172 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
173 return;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700174 if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
175 wpas_p2p_num_unused_channels(wpa_s) > 0 &&
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700176 wpa_s->parent->conf->p2p_ignore_shared_freq)
177 freq = 0;
178 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
179}
180
181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
183 struct wpa_scan_results *scan_res)
184{
185 size_t i;
186
187 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
188 return;
189
190 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
191 (int) scan_res->num);
192
193 for (i = 0; i < scan_res->num; i++) {
194 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800195 struct os_time time_tmp_age, entry_ts;
196 time_tmp_age.sec = bss->age / 1000;
197 time_tmp_age.usec = (bss->age % 1000) * 1000;
198 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800200 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 (const u8 *) (bss + 1),
202 bss->ie_len) > 0)
203 break;
204 }
205
206 p2p_scan_res_handled(wpa_s->global->p2p);
207}
208
209
210static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
211 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700212 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213{
214 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700215 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216 struct wpa_driver_scan_params params;
217 int ret;
218 struct wpabuf *wps_ie, *ies;
219 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800220 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221
222 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
223 return -1;
224
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700225 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
226 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700227 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700228 wpas_p2p_in_progress(wpa_s) == 2) {
229 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
230 "pending station mode scan to be "
231 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700232 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700233 wpa_supplicant_req_scan(ifs, 0, 0);
234 return 1;
235 }
236 }
237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238 os_memset(&params, 0, sizeof(params));
239
240 /* P2P Wildcard SSID */
241 params.num_ssids = 1;
242 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
243 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
244
245 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700246 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
247 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 num_req_dev_types, req_dev_types);
249 if (wps_ie == NULL)
250 return -1;
251
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800252 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
253 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 if (ies == NULL) {
255 wpabuf_free(wps_ie);
256 return -1;
257 }
258 wpabuf_put_buf(ies, wps_ie);
259 wpabuf_free(wps_ie);
260
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800261 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800263 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264 params.extra_ies = wpabuf_head(ies);
265 params.extra_ies_len = wpabuf_len(ies);
266
267 switch (type) {
268 case P2P_SCAN_SOCIAL:
269 params.freqs = social_channels;
270 break;
271 case P2P_SCAN_FULL:
272 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273 case P2P_SCAN_SOCIAL_PLUS_ONE:
274 social_channels[3] = freq;
275 params.freqs = social_channels;
276 break;
277 }
278
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800279 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280
281 wpabuf_free(ies);
282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800283 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700284 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
285 if (ifs->scanning ||
286 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
287 wpa_s->global->p2p_cb_on_scan_complete = 1;
288 ret = 1;
289 break;
290 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700292 } else {
293 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700294 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700295 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 return ret;
298}
299
300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
302{
303 switch (p2p_group_interface) {
304 case P2P_GROUP_INTERFACE_PENDING:
305 return WPA_IF_P2P_GROUP;
306 case P2P_GROUP_INTERFACE_GO:
307 return WPA_IF_P2P_GO;
308 case P2P_GROUP_INTERFACE_CLIENT:
309 return WPA_IF_P2P_CLIENT;
310 }
311
312 return WPA_IF_P2P_GROUP;
313}
314
315
316static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
317 const u8 *ssid,
318 size_t ssid_len, int *go)
319{
320 struct wpa_ssid *s;
321
322 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
323 for (s = wpa_s->conf->ssid; s; s = s->next) {
324 if (s->disabled != 0 || !s->p2p_group ||
325 s->ssid_len != ssid_len ||
326 os_memcmp(ssid, s->ssid, ssid_len) != 0)
327 continue;
328 if (s->mode == WPAS_MODE_P2P_GO &&
329 s != wpa_s->current_ssid)
330 continue;
331 if (go)
332 *go = s->mode == WPAS_MODE_P2P_GO;
333 return wpa_s;
334 }
335 }
336
337 return NULL;
338}
339
340
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700341static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
342 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343{
344 struct wpa_ssid *ssid;
345 char *gtype;
346 const char *reason;
347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 ssid = wpa_s->current_ssid;
349 if (ssid == NULL) {
350 /*
351 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700352 * pending P2P group interface waiting for provisioning or a
353 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 */
355 ssid = wpa_s->conf->ssid;
356 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700357 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358 break;
359 ssid = ssid->next;
360 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300361 if (ssid == NULL &&
362 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
363 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700364 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
365 "not found");
366 return -1;
367 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 }
369 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
370 gtype = "GO";
371 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
372 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
373 wpa_s->reassociate = 0;
374 wpa_s->disconnected = 1;
375 wpa_supplicant_deauthenticate(wpa_s,
376 WLAN_REASON_DEAUTH_LEAVING);
377 gtype = "client";
378 } else
379 gtype = "GO";
380 if (wpa_s->cross_connect_in_use) {
381 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700382 wpa_msg_global(wpa_s->parent, MSG_INFO,
383 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
384 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700386 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 case P2P_GROUP_REMOVAL_REQUESTED:
388 reason = " reason=REQUESTED";
389 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700390 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
391 reason = " reason=FORMATION_FAILED";
392 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
394 reason = " reason=IDLE";
395 break;
396 case P2P_GROUP_REMOVAL_UNAVAILABLE:
397 reason = " reason=UNAVAILABLE";
398 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700399 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
400 reason = " reason=GO_ENDING_SESSION";
401 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700402#ifdef ANDROID_P2P
403 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
404 reason = " reason=FREQ_CONFLICT";
405 break;
406#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407 default:
408 reason = "";
409 break;
410 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700411 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700412 wpa_msg_global(wpa_s->parent, MSG_INFO,
413 P2P_EVENT_GROUP_REMOVED "%s %s%s",
414 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700415 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700417#ifdef ANDROID_P2P
418 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
419#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700420 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
421 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800422 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700423 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800424 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
425 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700426 wpa_s->p2p_in_provisioning = 0;
427 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700428
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700429 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
431
432 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
433 struct wpa_global *global;
434 char *ifname;
435 enum wpa_driver_if_type type;
436 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
437 wpa_s->ifname);
438 global = wpa_s->global;
439 ifname = os_strdup(wpa_s->ifname);
440 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700441 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 wpa_s = global->ifaces;
443 if (wpa_s && ifname)
444 wpa_drv_if_remove(wpa_s, type, ifname);
445 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300446 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 }
448
449 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
450 if (ssid && (ssid->p2p_group ||
451 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
452 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
453 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700454 if (ssid == wpa_s->current_ssid) {
455 wpa_sm_set_config(wpa_s->wpa, NULL);
456 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700458 }
459 /*
460 * Networks objects created during any P2P activities are not
461 * exposed out as they might/will confuse certain non-P2P aware
462 * applications since these network objects won't behave like
463 * regular ones.
464 *
465 * Likewise, we don't send out network removed signals for such
466 * network objects.
467 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468 wpa_config_remove_network(wpa_s->conf, id);
469 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800470 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700471 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472 } else {
473 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
474 "found");
475 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700476 if (wpa_s->ap_iface)
477 wpa_supplicant_ap_deinit(wpa_s);
478 else
479 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700480
481 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700482}
483
484
485static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
486 u8 *go_dev_addr,
487 const u8 *ssid, size_t ssid_len)
488{
489 struct wpa_bss *bss;
490 const u8 *bssid;
491 struct wpabuf *p2p;
492 u8 group_capab;
493 const u8 *addr;
494
495 if (wpa_s->go_params)
496 bssid = wpa_s->go_params->peer_interface_addr;
497 else
498 bssid = wpa_s->bssid;
499
500 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
501 if (bss == NULL) {
502 u8 iface_addr[ETH_ALEN];
503 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
504 iface_addr) == 0)
505 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
506 }
507 if (bss == NULL) {
508 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
509 "group is persistent - BSS " MACSTR " not found",
510 MAC2STR(bssid));
511 return 0;
512 }
513
514 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
515 if (p2p == NULL) {
516 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
517 "group is persistent - BSS " MACSTR
518 " did not include P2P IE", MAC2STR(bssid));
519 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
520 (u8 *) (bss + 1), bss->ie_len);
521 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
522 ((u8 *) bss + 1) + bss->ie_len,
523 bss->beacon_ie_len);
524 return 0;
525 }
526
527 group_capab = p2p_get_group_capab(p2p);
528 addr = p2p_get_go_dev_addr(p2p);
529 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
530 "group_capab=0x%x", group_capab);
531 if (addr) {
532 os_memcpy(go_dev_addr, addr, ETH_ALEN);
533 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
534 MAC2STR(addr));
535 } else
536 os_memset(go_dev_addr, 0, ETH_ALEN);
537 wpabuf_free(p2p);
538
539 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
540 "go_dev_addr=" MACSTR,
541 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
542
543 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
544}
545
546
Jouni Malinen75ecf522011-06-27 15:19:46 -0700547static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
548 struct wpa_ssid *ssid,
549 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550{
551 struct wpa_ssid *s;
552 int changed = 0;
553
554 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
555 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
556 for (s = wpa_s->conf->ssid; s; s = s->next) {
557 if (s->disabled == 2 &&
558 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
559 s->ssid_len == ssid->ssid_len &&
560 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
561 break;
562 }
563
564 if (s) {
565 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
566 "entry");
567 if (ssid->passphrase && !s->passphrase)
568 changed = 1;
569 else if (ssid->passphrase && s->passphrase &&
570 os_strcmp(ssid->passphrase, s->passphrase) != 0)
571 changed = 1;
572 } else {
573 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
574 "entry");
575 changed = 1;
576 s = wpa_config_add_network(wpa_s->conf);
577 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700578 return -1;
579
580 /*
581 * Instead of network_added we emit persistent_group_added
582 * notification. Also to keep the defense checks in
583 * persistent_group obj registration method, we set the
584 * relevant flags in s to designate it as a persistent group.
585 */
586 s->p2p_group = 1;
587 s->p2p_persistent_group = 1;
588 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589 wpa_config_set_network_defaults(s);
590 }
591
592 s->p2p_group = 1;
593 s->p2p_persistent_group = 1;
594 s->disabled = 2;
595 s->bssid_set = 1;
596 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
597 s->mode = ssid->mode;
598 s->auth_alg = WPA_AUTH_ALG_OPEN;
599 s->key_mgmt = WPA_KEY_MGMT_PSK;
600 s->proto = WPA_PROTO_RSN;
601 s->pairwise_cipher = WPA_CIPHER_CCMP;
602 s->export_keys = 1;
603 if (ssid->passphrase) {
604 os_free(s->passphrase);
605 s->passphrase = os_strdup(ssid->passphrase);
606 }
607 if (ssid->psk_set) {
608 s->psk_set = 1;
609 os_memcpy(s->psk, ssid->psk, 32);
610 }
611 if (s->passphrase && !s->psk_set)
612 wpa_config_update_psk(s);
613 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
614 os_free(s->ssid);
615 s->ssid = os_malloc(ssid->ssid_len);
616 }
617 if (s->ssid) {
618 s->ssid_len = ssid->ssid_len;
619 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
620 }
621
622#ifndef CONFIG_NO_CONFIG_WRITE
623 if (changed && wpa_s->conf->update_config &&
624 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
625 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
626 }
627#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700628
629 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630}
631
632
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800633static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
634 const u8 *addr)
635{
636 struct wpa_ssid *ssid, *s;
637 u8 *n;
638 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700639 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800640
641 ssid = wpa_s->current_ssid;
642 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
643 !ssid->p2p_persistent_group)
644 return;
645
646 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
647 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
648 continue;
649
650 if (s->ssid_len == ssid->ssid_len &&
651 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
652 break;
653 }
654
655 if (s == NULL)
656 return;
657
658 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
659 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700660 ETH_ALEN) != 0)
661 continue;
662
663 if (i == s->num_p2p_clients - 1)
664 return; /* already the most recent entry */
665
666 /* move the entry to mark it most recent */
667 os_memmove(s->p2p_client_list + i * ETH_ALEN,
668 s->p2p_client_list + (i + 1) * ETH_ALEN,
669 (s->num_p2p_clients - i - 1) * ETH_ALEN);
670 os_memcpy(s->p2p_client_list +
671 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
672 found = 1;
673 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800674 }
675
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700676 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
677 n = os_realloc_array(s->p2p_client_list,
678 s->num_p2p_clients + 1, ETH_ALEN);
679 if (n == NULL)
680 return;
681 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
682 s->p2p_client_list = n;
683 s->num_p2p_clients++;
684 } else if (!found) {
685 /* Not enough room for an additional entry - drop the oldest
686 * entry */
687 os_memmove(s->p2p_client_list,
688 s->p2p_client_list + ETH_ALEN,
689 (s->num_p2p_clients - 1) * ETH_ALEN);
690 os_memcpy(s->p2p_client_list +
691 (s->num_p2p_clients - 1) * ETH_ALEN,
692 addr, ETH_ALEN);
693 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800694
695#ifndef CONFIG_NO_CONFIG_WRITE
696 if (wpa_s->parent->conf->update_config &&
697 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
698 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
699#endif /* CONFIG_NO_CONFIG_WRITE */
700}
701
702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
704 int success)
705{
706 struct wpa_ssid *ssid;
707 const char *ssid_txt;
708 int client;
709 int persistent;
710 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700711 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700712
713 /*
714 * This callback is likely called for the main interface. Update wpa_s
715 * to use the group interface if a new interface was created for the
716 * group.
717 */
718 if (wpa_s->global->p2p_group_formation)
719 wpa_s = wpa_s->global->p2p_group_formation;
720 wpa_s->global->p2p_group_formation = NULL;
721 wpa_s->p2p_in_provisioning = 0;
722
723 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700724 wpa_msg_global(wpa_s->parent, MSG_INFO,
725 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700726 wpas_p2p_group_delete(wpa_s,
727 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 return;
729 }
730
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700731 wpa_msg_global(wpa_s->parent, MSG_INFO,
732 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733
734 ssid = wpa_s->current_ssid;
735 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
736 ssid->mode = WPAS_MODE_P2P_GO;
737 p2p_group_notif_formation_done(wpa_s->p2p_group);
738 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
739 }
740
741 persistent = 0;
742 if (ssid) {
743 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
744 client = ssid->mode == WPAS_MODE_INFRA;
745 if (ssid->mode == WPAS_MODE_P2P_GO) {
746 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700747 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
748 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 } else
750 persistent = wpas_p2p_persistent_group(wpa_s,
751 go_dev_addr,
752 ssid->ssid,
753 ssid->ssid_len);
754 } else {
755 ssid_txt = "";
756 client = wpa_s->p2p_group_interface ==
757 P2P_GROUP_INTERFACE_CLIENT;
758 os_memset(go_dev_addr, 0, ETH_ALEN);
759 }
760
761 wpa_s->show_group_started = 0;
762 if (client) {
763 /*
764 * Indicate event only after successfully completed 4-way
765 * handshake, i.e., when the interface is ready for data
766 * packets.
767 */
768 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700769#ifdef ANDROID_P2P
770 /* For client Second phase of Group formation (4-way handshake) can be still pending
771 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700772 wpa_printf(MSG_INFO, "Restoring back wpa_s->global->p2p_group_formation to wpa_s %p\n", wpa_s);
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700773 wpa_s->global->p2p_group_formation = wpa_s;
774#endif
775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
777 char psk[65];
778 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700779 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
780 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
781 MACSTR "%s",
782 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
783 MAC2STR(go_dev_addr),
784 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785 wpas_p2p_cross_connect_setup(wpa_s);
786 wpas_p2p_set_group_idle_timeout(wpa_s);
787 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700788 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
789 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
790 "go_dev_addr=" MACSTR "%s",
791 wpa_s->ifname, ssid_txt,
792 ssid ? ssid->frequency : 0,
793 ssid && ssid->passphrase ? ssid->passphrase : "",
794 MAC2STR(go_dev_addr),
795 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796 wpas_p2p_cross_connect_setup(wpa_s);
797 wpas_p2p_set_group_idle_timeout(wpa_s);
798 }
799
800 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700801 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
802 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800803 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700804 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700805 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700806 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700807 os_get_time(&wpa_s->global->p2p_go_wait_client);
808 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809}
810
811
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800812static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
813 unsigned int freq,
814 const u8 *dst, const u8 *src,
815 const u8 *bssid,
816 const u8 *data, size_t data_len,
817 enum offchannel_send_action_result
818 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800820 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
823 return;
824 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
825 return;
826
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800827 switch (result) {
828 case OFFCHANNEL_SEND_ACTION_SUCCESS:
829 res = P2P_SEND_ACTION_SUCCESS;
830 break;
831 case OFFCHANNEL_SEND_ACTION_NO_ACK:
832 res = P2P_SEND_ACTION_NO_ACK;
833 break;
834 case OFFCHANNEL_SEND_ACTION_FAILED:
835 res = P2P_SEND_ACTION_FAILED;
836 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837 }
838
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800839 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800841 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
842 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800843 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800844 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
845 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800847 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
848 "during p2p_connect-auto");
849 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
850 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 }
852}
853
854
855static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
856 const u8 *src, const u8 *bssid, const u8 *buf,
857 size_t len, unsigned int wait_time)
858{
859 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800860 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
861 wait_time,
862 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863}
864
865
866static void wpas_send_action_done(void *ctx)
867{
868 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800869 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870}
871
872
873static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
874 struct p2p_go_neg_results *params)
875{
876 if (wpa_s->go_params == NULL) {
877 wpa_s->go_params = os_malloc(sizeof(*params));
878 if (wpa_s->go_params == NULL)
879 return -1;
880 }
881 os_memcpy(wpa_s->go_params, params, sizeof(*params));
882 return 0;
883}
884
885
886static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
887 struct p2p_go_neg_results *res)
888{
889 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
890 MAC2STR(res->peer_interface_addr));
891 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
892 res->ssid, res->ssid_len);
893 wpa_supplicant_ap_deinit(wpa_s);
894 wpas_copy_go_neg_results(wpa_s, res);
895 if (res->wps_method == WPS_PBC)
896 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
897 else {
898 u16 dev_pw_id = DEV_PW_DEFAULT;
899 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
900 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
901 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
902 wpa_s->p2p_pin, 1, dev_pw_id);
903 }
904}
905
906
907static void p2p_go_configured(void *ctx, void *data)
908{
909 struct wpa_supplicant *wpa_s = ctx;
910 struct p2p_go_neg_results *params = data;
911 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700912 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700913
914 ssid = wpa_s->current_ssid;
915 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
916 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
917 if (wpa_s->global->p2p_group_formation == wpa_s)
918 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800919 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700920 wpa_msg_global(wpa_s->parent, MSG_INFO,
921 P2P_EVENT_GROUP_STARTED
922 "%s GO ssid=\"%s\" freq=%d "
923 "passphrase=\"%s\" go_dev_addr=" MACSTR
924 "%s", wpa_s->ifname,
925 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
926 ssid->frequency, params->passphrase,
927 MAC2STR(wpa_s->global->p2p_dev_addr),
928 params->persistent_group ?
929 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800930 } else {
931 char psk[65];
932 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
933 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700934 wpa_msg_global(wpa_s->parent, MSG_INFO,
935 P2P_EVENT_GROUP_STARTED
936 "%s GO ssid=\"%s\" freq=%d psk=%s "
937 "go_dev_addr=" MACSTR "%s",
938 wpa_s->ifname,
939 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
940 ssid->frequency, psk,
941 MAC2STR(wpa_s->global->p2p_dev_addr),
942 params->persistent_group ?
943 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800944 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700947 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700949 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700950 if (network_id < 0)
951 network_id = ssid->id;
952 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 wpas_p2p_cross_connect_setup(wpa_s);
954 wpas_p2p_set_group_idle_timeout(wpa_s);
955 return;
956 }
957
958 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
959 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
960 params->peer_interface_addr)) {
961 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
962 "filtering");
963 return;
964 }
965 if (params->wps_method == WPS_PBC)
966 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800967 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968 else if (wpa_s->p2p_pin[0])
969 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800970 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971 os_free(wpa_s->go_params);
972 wpa_s->go_params = NULL;
973}
974
975
976static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
977 struct p2p_go_neg_results *params,
978 int group_formation)
979{
980 struct wpa_ssid *ssid;
981
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700982 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
983 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
984 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
985 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700987 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988
989 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700990 if (ssid == NULL) {
991 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700993 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800995 wpa_s->show_group_started = 0;
996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 wpa_config_set_network_defaults(ssid);
998 ssid->temporary = 1;
999 ssid->p2p_group = 1;
1000 ssid->p2p_persistent_group = params->persistent_group;
1001 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1002 WPAS_MODE_P2P_GO;
1003 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001004 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 ssid->ssid = os_zalloc(params->ssid_len + 1);
1006 if (ssid->ssid) {
1007 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1008 ssid->ssid_len = params->ssid_len;
1009 }
1010 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1011 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1012 ssid->proto = WPA_PROTO_RSN;
1013 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001014 if (os_strlen(params->passphrase) > 0) {
1015 ssid->passphrase = os_strdup(params->passphrase);
1016 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001017 wpa_msg_global(wpa_s, MSG_ERROR,
1018 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001019 wpa_config_remove_network(wpa_s->conf, ssid->id);
1020 return;
1021 }
1022 } else
1023 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001024 ssid->psk_set = params->psk_set;
1025 if (ssid->psk_set)
1026 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001027 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001028 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001029 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030
1031 wpa_s->ap_configured_cb = p2p_go_configured;
1032 wpa_s->ap_configured_cb_ctx = wpa_s;
1033 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001034 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 wpa_s->reassociate = 1;
1036 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001037 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1038 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 wpa_supplicant_req_scan(wpa_s, 0, 0);
1040}
1041
1042
1043static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1044 const struct wpa_supplicant *src)
1045{
1046 struct wpa_config *d;
1047 const struct wpa_config *s;
1048
1049 d = dst->conf;
1050 s = src->conf;
1051
1052#define C(n) if (s->n) d->n = os_strdup(s->n)
1053 C(device_name);
1054 C(manufacturer);
1055 C(model_name);
1056 C(model_number);
1057 C(serial_number);
1058 C(config_methods);
1059#undef C
1060
1061 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1062 os_memcpy(d->sec_device_type, s->sec_device_type,
1063 sizeof(d->sec_device_type));
1064 d->num_sec_device_types = s->num_sec_device_types;
1065
1066 d->p2p_group_idle = s->p2p_group_idle;
1067 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001068 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001069 d->max_num_sta = s->max_num_sta;
1070 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001071 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001072 d->beacon_int = s->beacon_int;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001073 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074}
1075
1076
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001077static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1078 char *ifname, size_t len)
1079{
1080 char *ifname_ptr = wpa_s->ifname;
1081
1082 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1083 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1084 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1085 }
1086
1087 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1088 if (os_strlen(ifname) >= IFNAMSIZ &&
1089 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1090 /* Try to avoid going over the IFNAMSIZ length limit */
1091 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1092 wpa_s->p2p_group_idx);
1093 }
1094}
1095
1096
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1098 enum wpa_driver_if_type type)
1099{
1100 char ifname[120], force_ifname[120];
1101
1102 if (wpa_s->pending_interface_name[0]) {
1103 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1104 "- skip creation of a new one");
1105 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1106 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1107 "unknown?! ifname='%s'",
1108 wpa_s->pending_interface_name);
1109 return -1;
1110 }
1111 return 0;
1112 }
1113
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001114 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 force_ifname[0] = '\0';
1116
1117 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1118 ifname);
1119 wpa_s->p2p_group_idx++;
1120
1121 wpa_s->pending_interface_type = type;
1122 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1123 wpa_s->pending_interface_addr, NULL) < 0) {
1124 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1125 "interface");
1126 return -1;
1127 }
1128
1129 if (force_ifname[0]) {
1130 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1131 force_ifname);
1132 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1133 sizeof(wpa_s->pending_interface_name));
1134 } else
1135 os_strlcpy(wpa_s->pending_interface_name, ifname,
1136 sizeof(wpa_s->pending_interface_name));
1137 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1138 MACSTR, wpa_s->pending_interface_name,
1139 MAC2STR(wpa_s->pending_interface_addr));
1140
1141 return 0;
1142}
1143
1144
1145static void wpas_p2p_remove_pending_group_interface(
1146 struct wpa_supplicant *wpa_s)
1147{
1148 if (!wpa_s->pending_interface_name[0] ||
1149 is_zero_ether_addr(wpa_s->pending_interface_addr))
1150 return; /* No pending virtual interface */
1151
1152 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1153 wpa_s->pending_interface_name);
1154 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1155 wpa_s->pending_interface_name);
1156 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1157 wpa_s->pending_interface_name[0] = '\0';
1158}
1159
1160
1161static struct wpa_supplicant *
1162wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1163{
1164 struct wpa_interface iface;
1165 struct wpa_supplicant *group_wpa_s;
1166
1167 if (!wpa_s->pending_interface_name[0]) {
1168 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1169 if (!wpas_p2p_create_iface(wpa_s))
1170 return NULL;
1171 /*
1172 * Something has forced us to remove the pending interface; try
1173 * to create a new one and hope for the best that we will get
1174 * the same local address.
1175 */
1176 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1177 WPA_IF_P2P_CLIENT) < 0)
1178 return NULL;
1179 }
1180
1181 os_memset(&iface, 0, sizeof(iface));
1182 iface.ifname = wpa_s->pending_interface_name;
1183 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001184 if (wpa_s->conf->ctrl_interface == NULL &&
1185 wpa_s->parent != wpa_s &&
1186 wpa_s->p2p_mgmt &&
1187 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1188 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1189 else
1190 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191 iface.driver_param = wpa_s->conf->driver_param;
1192 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1193 if (group_wpa_s == NULL) {
1194 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1195 "wpa_supplicant interface");
1196 return NULL;
1197 }
1198 wpa_s->pending_interface_name[0] = '\0';
1199 group_wpa_s->parent = wpa_s;
1200 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1201 P2P_GROUP_INTERFACE_CLIENT;
1202 wpa_s->global->p2p_group_formation = group_wpa_s;
1203
1204 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1205
1206 return group_wpa_s;
1207}
1208
1209
1210static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1211 void *timeout_ctx)
1212{
1213 struct wpa_supplicant *wpa_s = eloop_ctx;
1214 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001215 wpas_p2p_group_formation_failed(wpa_s);
1216}
1217
1218
1219void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1220{
1221 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1222 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 if (wpa_s->global->p2p)
1224 p2p_group_formation_failed(wpa_s->global->p2p);
1225 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1226 wpa_drv_p2p_group_formation_failed(wpa_s);
1227 wpas_group_formation_completed(wpa_s, 0);
1228}
1229
1230
1231void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1232{
1233 struct wpa_supplicant *wpa_s = ctx;
1234
1235 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1236 wpa_drv_cancel_remain_on_channel(wpa_s);
1237 wpa_s->off_channel_freq = 0;
1238 wpa_s->roc_waiting_drv_freq = 0;
1239 }
1240
1241 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001242 wpa_msg_global(wpa_s, MSG_INFO,
1243 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1244 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 wpas_p2p_remove_pending_group_interface(wpa_s);
1247 return;
1248 }
1249
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001250 if (wpa_s->p2p_go_ht40)
1251 res->ht40 = 1;
1252
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001253 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1254 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1255 " wps_method=%s",
1256 res->role_go ? "GO" : "client", res->freq, res->ht40,
1257 MAC2STR(res->peer_device_addr),
1258 MAC2STR(res->peer_interface_addr),
1259 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001260 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261
Dmitry Shmidt04949592012-07-19 12:16:46 -07001262 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1263 struct wpa_ssid *ssid;
1264 ssid = wpa_config_get_network(wpa_s->conf,
1265 wpa_s->p2p_persistent_id);
1266 if (ssid && ssid->disabled == 2 &&
1267 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1268 size_t len = os_strlen(ssid->passphrase);
1269 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1270 "on requested persistent group");
1271 os_memcpy(res->passphrase, ssid->passphrase, len);
1272 res->passphrase[len] = '\0';
1273 }
1274 }
1275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 if (wpa_s->create_p2p_iface) {
1277 struct wpa_supplicant *group_wpa_s =
1278 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1279 if (group_wpa_s == NULL) {
1280 wpas_p2p_remove_pending_group_interface(wpa_s);
1281 return;
1282 }
1283 if (group_wpa_s != wpa_s) {
1284 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1285 sizeof(group_wpa_s->p2p_pin));
1286 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1287 }
1288 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1289 wpa_s->pending_interface_name[0] = '\0';
1290 group_wpa_s->p2p_in_provisioning = 1;
1291
1292 if (res->role_go)
1293 wpas_start_wps_go(group_wpa_s, res, 1);
1294 else
1295 wpas_start_wps_enrollee(group_wpa_s, res);
1296 } else {
1297 wpa_s->p2p_in_provisioning = 1;
1298 wpa_s->global->p2p_group_formation = wpa_s;
1299
1300 if (res->role_go)
1301 wpas_start_wps_go(wpa_s, res, 1);
1302 else
1303 wpas_start_wps_enrollee(ctx, res);
1304 }
1305
1306 wpa_s->p2p_long_listen = 0;
1307 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1308
1309 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1310 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1311 (res->peer_config_timeout % 100) * 10000,
1312 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1313}
1314
1315
1316void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1317{
1318 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001319 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1320 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321
1322 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1323}
1324
1325
1326void wpas_dev_found(void *ctx, const u8 *addr,
1327 const struct p2p_peer_info *info,
1328 int new_device)
1329{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001330#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 struct wpa_supplicant *wpa_s = ctx;
1332 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001333#define WFD_DEV_INFO_SIZE 9
1334 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001335 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001336#ifdef CONFIG_WIFI_DISPLAY
1337 if (info->wfd_subelems) {
1338 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1339 wpabuf_head(info->wfd_subelems),
1340 WFD_DEV_INFO_SIZE);
1341 }
1342#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001343 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1344 " p2p_dev_addr=" MACSTR
1345 " pri_dev_type=%s name='%s' config_methods=0x%x "
1346 "dev_capab=0x%x group_capab=0x%x%s%s",
1347 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1348 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1349 sizeof(devtype)),
1350 info->device_name, info->config_methods,
1351 info->dev_capab, info->group_capab,
1352 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001353#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354
1355 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1356}
1357
1358
1359static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1360{
1361 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001362
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001363 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1364 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1367}
1368
1369
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001370static void wpas_find_stopped(void *ctx)
1371{
1372 struct wpa_supplicant *wpa_s = ctx;
1373 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1374}
1375
1376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377static int wpas_start_listen(void *ctx, unsigned int freq,
1378 unsigned int duration,
1379 const struct wpabuf *probe_resp_ie)
1380{
1381 struct wpa_supplicant *wpa_s = ctx;
1382
1383 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1384
1385 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1386 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1387 "report received Probe Request frames");
1388 return -1;
1389 }
1390
1391 wpa_s->pending_listen_freq = freq;
1392 wpa_s->pending_listen_duration = duration;
1393
1394 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1395 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1396 "to remain on channel (%u MHz) for Listen "
1397 "state", freq);
1398 wpa_s->pending_listen_freq = 0;
1399 return -1;
1400 }
1401 wpa_s->off_channel_freq = 0;
1402 wpa_s->roc_waiting_drv_freq = freq;
1403
1404 return 0;
1405}
1406
1407
1408static void wpas_stop_listen(void *ctx)
1409{
1410 struct wpa_supplicant *wpa_s = ctx;
1411 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1412 wpa_drv_cancel_remain_on_channel(wpa_s);
1413 wpa_s->off_channel_freq = 0;
1414 wpa_s->roc_waiting_drv_freq = 0;
1415 }
1416 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1417 wpa_drv_probe_req_report(wpa_s, 0);
1418}
1419
1420
1421static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1422{
1423 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001424 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425}
1426
1427
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001428/*
1429 * DNS Header section is used only to calculate compression pointers, so the
1430 * contents of this data does not matter, but the length needs to be reserved
1431 * in the virtual packet.
1432 */
1433#define DNS_HEADER_LEN 12
1434
1435/*
1436 * 27-octet in-memory packet from P2P specification containing two implied
1437 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1438 */
1439#define P2P_SD_IN_MEMORY_LEN 27
1440
1441static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1442 u8 **spos, const u8 *end)
1443{
1444 while (*spos < end) {
1445 u8 val = ((*spos)[0] & 0xc0) >> 6;
1446 int len;
1447
1448 if (val == 1 || val == 2) {
1449 /* These are reserved values in RFC 1035 */
1450 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1451 "sequence starting with 0x%x", val);
1452 return -1;
1453 }
1454
1455 if (val == 3) {
1456 u16 offset;
1457 u8 *spos_tmp;
1458
1459 /* Offset */
1460 if (*spos + 2 > end) {
1461 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1462 "DNS offset field");
1463 return -1;
1464 }
1465
1466 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1467 if (offset >= *spos - start) {
1468 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1469 "pointer offset %u", offset);
1470 return -1;
1471 }
1472
1473 (*spos) += 2;
1474 spos_tmp = start + offset;
1475 return p2p_sd_dns_uncompress_label(upos, uend, start,
1476 &spos_tmp,
1477 *spos - 2);
1478 }
1479
1480 /* Label */
1481 len = (*spos)[0] & 0x3f;
1482 if (len == 0)
1483 return 0;
1484
1485 (*spos)++;
1486 if (*spos + len > end) {
1487 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1488 "sequence - no room for label with length "
1489 "%u", len);
1490 return -1;
1491 }
1492
1493 if (*upos + len + 2 > uend)
1494 return -2;
1495
1496 os_memcpy(*upos, *spos, len);
1497 *spos += len;
1498 *upos += len;
1499 (*upos)[0] = '.';
1500 (*upos)++;
1501 (*upos)[0] = '\0';
1502 }
1503
1504 return 0;
1505}
1506
1507
1508/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1509 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1510 * not large enough */
1511static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1512 size_t msg_len, size_t offset)
1513{
1514 /* 27-octet in-memory packet from P2P specification */
1515 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1516 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1517 u8 *tmp, *end, *spos;
1518 char *upos, *uend;
1519 int ret = 0;
1520
1521 if (buf_len < 2)
1522 return -1;
1523 if (offset > msg_len)
1524 return -1;
1525
1526 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1527 if (tmp == NULL)
1528 return -1;
1529 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1530 end = spos + msg_len;
1531 spos += offset;
1532
1533 os_memset(tmp, 0, DNS_HEADER_LEN);
1534 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1535 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1536
1537 upos = buf;
1538 uend = buf + buf_len;
1539
1540 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1541 if (ret) {
1542 os_free(tmp);
1543 return ret;
1544 }
1545
1546 if (upos == buf) {
1547 upos[0] = '.';
1548 upos[1] = '\0';
1549 } else if (upos[-1] == '.')
1550 upos[-1] = '\0';
1551
1552 os_free(tmp);
1553 return 0;
1554}
1555
1556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001557static struct p2p_srv_bonjour *
1558wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1559 const struct wpabuf *query)
1560{
1561 struct p2p_srv_bonjour *bsrv;
1562 size_t len;
1563
1564 len = wpabuf_len(query);
1565 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1566 struct p2p_srv_bonjour, list) {
1567 if (len == wpabuf_len(bsrv->query) &&
1568 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1569 len) == 0)
1570 return bsrv;
1571 }
1572 return NULL;
1573}
1574
1575
1576static struct p2p_srv_upnp *
1577wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1578 const char *service)
1579{
1580 struct p2p_srv_upnp *usrv;
1581
1582 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1583 struct p2p_srv_upnp, list) {
1584 if (version == usrv->version &&
1585 os_strcmp(service, usrv->service) == 0)
1586 return usrv;
1587 }
1588 return NULL;
1589}
1590
1591
1592static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1593 u8 srv_trans_id)
1594{
1595 u8 *len_pos;
1596
1597 if (wpabuf_tailroom(resp) < 5)
1598 return;
1599
1600 /* Length (to be filled) */
1601 len_pos = wpabuf_put(resp, 2);
1602 wpabuf_put_u8(resp, srv_proto);
1603 wpabuf_put_u8(resp, srv_trans_id);
1604 /* Status Code */
1605 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1606 /* Response Data: empty */
1607 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1608}
1609
1610
1611static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1612 struct wpabuf *resp, u8 srv_trans_id)
1613{
1614 struct p2p_srv_bonjour *bsrv;
1615 u8 *len_pos;
1616
1617 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1618
1619 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1620 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1621 return;
1622 }
1623
1624 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1625 struct p2p_srv_bonjour, list) {
1626 if (wpabuf_tailroom(resp) <
1627 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1628 return;
1629 /* Length (to be filled) */
1630 len_pos = wpabuf_put(resp, 2);
1631 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1632 wpabuf_put_u8(resp, srv_trans_id);
1633 /* Status Code */
1634 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1635 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1636 wpabuf_head(bsrv->resp),
1637 wpabuf_len(bsrv->resp));
1638 /* Response Data */
1639 wpabuf_put_buf(resp, bsrv->query); /* Key */
1640 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1641 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1642 2);
1643 }
1644}
1645
1646
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001647static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1648 size_t query_len)
1649{
1650 char str_rx[256], str_srv[256];
1651
1652 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1653 return 0; /* Too short to include DNS Type and Version */
1654 if (os_memcmp(query + query_len - 3,
1655 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1656 3) != 0)
1657 return 0; /* Mismatch in DNS Type or Version */
1658 if (query_len == wpabuf_len(bsrv->query) &&
1659 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1660 return 1; /* Binary match */
1661
1662 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1663 0))
1664 return 0; /* Failed to uncompress query */
1665 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1666 wpabuf_head(bsrv->query),
1667 wpabuf_len(bsrv->query) - 3, 0))
1668 return 0; /* Failed to uncompress service */
1669
1670 return os_strcmp(str_rx, str_srv) == 0;
1671}
1672
1673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1675 struct wpabuf *resp, u8 srv_trans_id,
1676 const u8 *query, size_t query_len)
1677{
1678 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001680 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681
1682 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1683 query, query_len);
1684 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1685 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1686 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1687 srv_trans_id);
1688 return;
1689 }
1690
1691 if (query_len == 0) {
1692 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1693 return;
1694 }
1695
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001696 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1697 struct p2p_srv_bonjour, list) {
1698 if (!match_bonjour_query(bsrv, query, query_len))
1699 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001700
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001701 if (wpabuf_tailroom(resp) <
1702 5 + query_len + wpabuf_len(bsrv->resp))
1703 return;
1704
1705 matches++;
1706
1707 /* Length (to be filled) */
1708 len_pos = wpabuf_put(resp, 2);
1709 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1710 wpabuf_put_u8(resp, srv_trans_id);
1711
1712 /* Status Code */
1713 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1714 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1715 wpabuf_head(bsrv->resp),
1716 wpabuf_len(bsrv->resp));
1717
1718 /* Response Data */
1719 wpabuf_put_data(resp, query, query_len); /* Key */
1720 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1721
1722 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1723 }
1724
1725 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1727 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001728 if (wpabuf_tailroom(resp) < 5)
1729 return;
1730
1731 /* Length (to be filled) */
1732 len_pos = wpabuf_put(resp, 2);
1733 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1734 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735
1736 /* Status Code */
1737 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1738 /* Response Data: empty */
1739 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1740 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001742}
1743
1744
1745static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1746 struct wpabuf *resp, u8 srv_trans_id)
1747{
1748 struct p2p_srv_upnp *usrv;
1749 u8 *len_pos;
1750
1751 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1752
1753 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1754 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1755 return;
1756 }
1757
1758 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1759 struct p2p_srv_upnp, list) {
1760 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1761 return;
1762
1763 /* Length (to be filled) */
1764 len_pos = wpabuf_put(resp, 2);
1765 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1766 wpabuf_put_u8(resp, srv_trans_id);
1767
1768 /* Status Code */
1769 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1770 /* Response Data */
1771 wpabuf_put_u8(resp, usrv->version);
1772 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1773 usrv->service);
1774 wpabuf_put_str(resp, usrv->service);
1775 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1776 2);
1777 }
1778}
1779
1780
1781static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1782 struct wpabuf *resp, u8 srv_trans_id,
1783 const u8 *query, size_t query_len)
1784{
1785 struct p2p_srv_upnp *usrv;
1786 u8 *len_pos;
1787 u8 version;
1788 char *str;
1789 int count = 0;
1790
1791 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1792 query, query_len);
1793
1794 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1795 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1796 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1797 srv_trans_id);
1798 return;
1799 }
1800
1801 if (query_len == 0) {
1802 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1803 return;
1804 }
1805
1806 if (wpabuf_tailroom(resp) < 5)
1807 return;
1808
1809 /* Length (to be filled) */
1810 len_pos = wpabuf_put(resp, 2);
1811 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1812 wpabuf_put_u8(resp, srv_trans_id);
1813
1814 version = query[0];
1815 str = os_malloc(query_len);
1816 if (str == NULL)
1817 return;
1818 os_memcpy(str, query + 1, query_len - 1);
1819 str[query_len - 1] = '\0';
1820
1821 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1822 struct p2p_srv_upnp, list) {
1823 if (version != usrv->version)
1824 continue;
1825
1826 if (os_strcmp(str, "ssdp:all") != 0 &&
1827 os_strstr(usrv->service, str) == NULL)
1828 continue;
1829
1830 if (wpabuf_tailroom(resp) < 2)
1831 break;
1832 if (count == 0) {
1833 /* Status Code */
1834 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1835 /* Response Data */
1836 wpabuf_put_u8(resp, version);
1837 } else
1838 wpabuf_put_u8(resp, ',');
1839
1840 count++;
1841
1842 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1843 usrv->service);
1844 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1845 break;
1846 wpabuf_put_str(resp, usrv->service);
1847 }
1848 os_free(str);
1849
1850 if (count == 0) {
1851 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1852 "available");
1853 /* Status Code */
1854 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1855 /* Response Data: empty */
1856 }
1857
1858 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1859}
1860
1861
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001862#ifdef CONFIG_WIFI_DISPLAY
1863static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1864 struct wpabuf *resp, u8 srv_trans_id,
1865 const u8 *query, size_t query_len)
1866{
1867 const u8 *pos;
1868 u8 role;
1869 u8 *len_pos;
1870
1871 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1872
1873 if (!wpa_s->global->wifi_display) {
1874 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1875 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1876 srv_trans_id);
1877 return;
1878 }
1879
1880 if (query_len < 1) {
1881 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1882 "Role");
1883 return;
1884 }
1885
1886 if (wpabuf_tailroom(resp) < 5)
1887 return;
1888
1889 pos = query;
1890 role = *pos++;
1891 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1892
1893 /* TODO: role specific handling */
1894
1895 /* Length (to be filled) */
1896 len_pos = wpabuf_put(resp, 2);
1897 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1898 wpabuf_put_u8(resp, srv_trans_id);
1899 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1900
1901 while (pos < query + query_len) {
1902 if (*pos < MAX_WFD_SUBELEMS &&
1903 wpa_s->global->wfd_subelem[*pos] &&
1904 wpabuf_tailroom(resp) >=
1905 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1906 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1907 "subelement %u", *pos);
1908 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1909 }
1910 pos++;
1911 }
1912
1913 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1914}
1915#endif /* CONFIG_WIFI_DISPLAY */
1916
1917
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1919 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1920{
1921 struct wpa_supplicant *wpa_s = ctx;
1922 const u8 *pos = tlvs;
1923 const u8 *end = tlvs + tlvs_len;
1924 const u8 *tlv_end;
1925 u16 slen;
1926 struct wpabuf *resp;
1927 u8 srv_proto, srv_trans_id;
1928 size_t buf_len;
1929 char *buf;
1930
1931 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1932 tlvs, tlvs_len);
1933 buf_len = 2 * tlvs_len + 1;
1934 buf = os_malloc(buf_len);
1935 if (buf) {
1936 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1937 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1938 MACSTR " %u %u %s",
1939 freq, MAC2STR(sa), dialog_token, update_indic,
1940 buf);
1941 os_free(buf);
1942 }
1943
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001944 if (wpa_s->p2p_sd_over_ctrl_iface) {
1945 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1946 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001948 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949
1950 resp = wpabuf_alloc(10000);
1951 if (resp == NULL)
1952 return;
1953
1954 while (pos + 1 < end) {
1955 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1956 slen = WPA_GET_LE16(pos);
1957 pos += 2;
1958 if (pos + slen > end || slen < 2) {
1959 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1960 "length");
1961 wpabuf_free(resp);
1962 return;
1963 }
1964 tlv_end = pos + slen;
1965
1966 srv_proto = *pos++;
1967 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1968 srv_proto);
1969 srv_trans_id = *pos++;
1970 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1971 srv_trans_id);
1972
1973 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1974 pos, tlv_end - pos);
1975
1976
1977 if (wpa_s->force_long_sd) {
1978 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1979 "response");
1980 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1981 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1982 goto done;
1983 }
1984
1985 switch (srv_proto) {
1986 case P2P_SERV_ALL_SERVICES:
1987 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1988 "for all services");
1989 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1990 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1991 wpa_printf(MSG_DEBUG, "P2P: No service "
1992 "discovery protocols available");
1993 wpas_sd_add_proto_not_avail(
1994 resp, P2P_SERV_ALL_SERVICES,
1995 srv_trans_id);
1996 break;
1997 }
1998 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1999 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2000 break;
2001 case P2P_SERV_BONJOUR:
2002 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2003 pos, tlv_end - pos);
2004 break;
2005 case P2P_SERV_UPNP:
2006 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2007 pos, tlv_end - pos);
2008 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002009#ifdef CONFIG_WIFI_DISPLAY
2010 case P2P_SERV_WIFI_DISPLAY:
2011 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2012 pos, tlv_end - pos);
2013 break;
2014#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 default:
2016 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2017 "protocol %u", srv_proto);
2018 wpas_sd_add_proto_not_avail(resp, srv_proto,
2019 srv_trans_id);
2020 break;
2021 }
2022
2023 pos = tlv_end;
2024 }
2025
2026done:
2027 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2028 update_indic, tlvs, tlvs_len);
2029
2030 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2031
2032 wpabuf_free(resp);
2033}
2034
2035
2036void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2037 const u8 *tlvs, size_t tlvs_len)
2038{
2039 struct wpa_supplicant *wpa_s = ctx;
2040 const u8 *pos = tlvs;
2041 const u8 *end = tlvs + tlvs_len;
2042 const u8 *tlv_end;
2043 u16 slen;
2044 size_t buf_len;
2045 char *buf;
2046
2047 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2048 tlvs, tlvs_len);
2049 if (tlvs_len > 1500) {
2050 /* TODO: better way for handling this */
2051 wpa_msg_ctrl(wpa_s, MSG_INFO,
2052 P2P_EVENT_SERV_DISC_RESP MACSTR
2053 " %u <long response: %u bytes>",
2054 MAC2STR(sa), update_indic,
2055 (unsigned int) tlvs_len);
2056 } else {
2057 buf_len = 2 * tlvs_len + 1;
2058 buf = os_malloc(buf_len);
2059 if (buf) {
2060 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2061 wpa_msg_ctrl(wpa_s, MSG_INFO,
2062 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2063 MAC2STR(sa), update_indic, buf);
2064 os_free(buf);
2065 }
2066 }
2067
2068 while (pos < end) {
2069 u8 srv_proto, srv_trans_id, status;
2070
2071 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2072 slen = WPA_GET_LE16(pos);
2073 pos += 2;
2074 if (pos + slen > end || slen < 3) {
2075 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2076 "length");
2077 return;
2078 }
2079 tlv_end = pos + slen;
2080
2081 srv_proto = *pos++;
2082 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2083 srv_proto);
2084 srv_trans_id = *pos++;
2085 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2086 srv_trans_id);
2087 status = *pos++;
2088 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2089 status);
2090
2091 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2092 pos, tlv_end - pos);
2093
2094 pos = tlv_end;
2095 }
2096
2097 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2098}
2099
2100
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002101u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2102 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103{
2104 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002105 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002107 return 0;
2108 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109}
2110
2111
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2113 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114{
2115 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002116 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117
2118 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2119 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002120 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2122 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2123 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2124 wpabuf_put_u8(tlvs, version);
2125 wpabuf_put_str(tlvs, query);
2126 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2127 wpabuf_free(tlvs);
2128 return ret;
2129}
2130
2131
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002132#ifdef CONFIG_WIFI_DISPLAY
2133
2134static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2135 const struct wpabuf *tlvs)
2136{
2137 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2138 return 0;
2139 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2140 return 0;
2141 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2142}
2143
2144
2145#define MAX_WFD_SD_SUBELEMS 20
2146
2147static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2148 const char *subelems)
2149{
2150 u8 *len;
2151 const char *pos;
2152 int val;
2153 int count = 0;
2154
2155 len = wpabuf_put(tlvs, 2);
2156 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2157 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2158
2159 wpabuf_put_u8(tlvs, role);
2160
2161 pos = subelems;
2162 while (*pos) {
2163 val = atoi(pos);
2164 if (val >= 0 && val < 256) {
2165 wpabuf_put_u8(tlvs, val);
2166 count++;
2167 if (count == MAX_WFD_SD_SUBELEMS)
2168 break;
2169 }
2170 pos = os_strchr(pos + 1, ',');
2171 if (pos == NULL)
2172 break;
2173 pos++;
2174 }
2175
2176 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2177}
2178
2179
2180u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2181 const u8 *dst, const char *role)
2182{
2183 struct wpabuf *tlvs;
2184 u64 ret;
2185 const char *subelems;
2186 u8 id = 1;
2187
2188 subelems = os_strchr(role, ' ');
2189 if (subelems == NULL)
2190 return 0;
2191 subelems++;
2192
2193 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2194 if (tlvs == NULL)
2195 return 0;
2196
2197 if (os_strstr(role, "[source]"))
2198 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2199 if (os_strstr(role, "[pri-sink]"))
2200 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2201 if (os_strstr(role, "[sec-sink]"))
2202 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2203 if (os_strstr(role, "[source+sink]"))
2204 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2205
2206 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2207 wpabuf_free(tlvs);
2208 return ret;
2209}
2210
2211#endif /* CONFIG_WIFI_DISPLAY */
2212
2213
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002214int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
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_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002218 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2219 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002220 return p2p_sd_cancel_request(wpa_s->global->p2p,
2221 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222}
2223
2224
2225void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2226 const u8 *dst, u8 dialog_token,
2227 const struct wpabuf *resp_tlvs)
2228{
2229 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2230 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2231 resp_tlvs);
2232 return;
2233 }
2234 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2235 return;
2236 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2237 resp_tlvs);
2238}
2239
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2242{
2243 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2244 wpa_drv_p2p_service_update(wpa_s);
2245 return;
2246 }
2247 if (wpa_s->global->p2p)
2248 p2p_sd_service_update(wpa_s->global->p2p);
2249}
2250
2251
2252static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2253{
2254 dl_list_del(&bsrv->list);
2255 wpabuf_free(bsrv->query);
2256 wpabuf_free(bsrv->resp);
2257 os_free(bsrv);
2258}
2259
2260
2261static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2262{
2263 dl_list_del(&usrv->list);
2264 os_free(usrv->service);
2265 os_free(usrv);
2266}
2267
2268
2269void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2270{
2271 struct p2p_srv_bonjour *bsrv, *bn;
2272 struct p2p_srv_upnp *usrv, *un;
2273
2274 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2275 struct p2p_srv_bonjour, list)
2276 wpas_p2p_srv_bonjour_free(bsrv);
2277
2278 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2279 struct p2p_srv_upnp, list)
2280 wpas_p2p_srv_upnp_free(usrv);
2281
2282 wpas_p2p_sd_service_update(wpa_s);
2283}
2284
2285
2286int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2287 struct wpabuf *query, struct wpabuf *resp)
2288{
2289 struct p2p_srv_bonjour *bsrv;
2290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291 bsrv = os_zalloc(sizeof(*bsrv));
2292 if (bsrv == NULL)
2293 return -1;
2294 bsrv->query = query;
2295 bsrv->resp = resp;
2296 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2297
2298 wpas_p2p_sd_service_update(wpa_s);
2299 return 0;
2300}
2301
2302
2303int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2304 const struct wpabuf *query)
2305{
2306 struct p2p_srv_bonjour *bsrv;
2307
2308 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2309 if (bsrv == NULL)
2310 return -1;
2311 wpas_p2p_srv_bonjour_free(bsrv);
2312 wpas_p2p_sd_service_update(wpa_s);
2313 return 0;
2314}
2315
2316
2317int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2318 const char *service)
2319{
2320 struct p2p_srv_upnp *usrv;
2321
2322 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2323 return 0; /* Already listed */
2324 usrv = os_zalloc(sizeof(*usrv));
2325 if (usrv == NULL)
2326 return -1;
2327 usrv->version = version;
2328 usrv->service = os_strdup(service);
2329 if (usrv->service == NULL) {
2330 os_free(usrv);
2331 return -1;
2332 }
2333 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2334
2335 wpas_p2p_sd_service_update(wpa_s);
2336 return 0;
2337}
2338
2339
2340int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2341 const char *service)
2342{
2343 struct p2p_srv_upnp *usrv;
2344
2345 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2346 if (usrv == NULL)
2347 return -1;
2348 wpas_p2p_srv_upnp_free(usrv);
2349 wpas_p2p_sd_service_update(wpa_s);
2350 return 0;
2351}
2352
2353
2354static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2355 const u8 *peer, const char *params,
2356 unsigned int generated_pin)
2357{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002358 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2359 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002360}
2361
2362
2363static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2364 const u8 *peer, const char *params)
2365{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002366 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2367 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368}
2369
2370
2371void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2372 const u8 *dev_addr, const u8 *pri_dev_type,
2373 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002374 u8 dev_capab, u8 group_capab, const u8 *group_id,
2375 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002376{
2377 struct wpa_supplicant *wpa_s = ctx;
2378 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002379 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380 u8 empty_dev_type[8];
2381 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002382 struct wpa_supplicant *group = NULL;
2383
2384 if (group_id) {
2385 for (group = wpa_s->global->ifaces; group; group = group->next)
2386 {
2387 struct wpa_ssid *s = group->current_ssid;
2388 if (s != NULL &&
2389 s->mode == WPAS_MODE_P2P_GO &&
2390 group_id_len - ETH_ALEN == s->ssid_len &&
2391 os_memcmp(group_id + ETH_ALEN, s->ssid,
2392 s->ssid_len) == 0)
2393 break;
2394 }
2395 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002396
2397 if (pri_dev_type == NULL) {
2398 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2399 pri_dev_type = empty_dev_type;
2400 }
2401 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2402 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002403 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404 MAC2STR(dev_addr),
2405 wps_dev_type_bin2str(pri_dev_type, devtype,
2406 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002407 dev_name, supp_config_methods, dev_capab, group_capab,
2408 group ? " group=" : "",
2409 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412 if (config_methods & WPS_CONFIG_DISPLAY) {
2413 generated_pin = wps_generate_pin();
2414 wpas_prov_disc_local_display(wpa_s, peer, params,
2415 generated_pin);
2416 } else if (config_methods & WPS_CONFIG_KEYPAD)
2417 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2418 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002419 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2420 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002421
2422 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2423 P2P_PROV_DISC_SUCCESS,
2424 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425}
2426
2427
2428void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2429{
2430 struct wpa_supplicant *wpa_s = ctx;
2431 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002432 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002434 if (wpa_s->pending_pd_before_join &&
2435 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2436 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2437 wpa_s->pending_pd_before_join = 0;
2438 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2439 "join-existing-group operation");
2440 wpas_p2p_join_start(wpa_s);
2441 return;
2442 }
2443
Dmitry Shmidt04949592012-07-19 12:16:46 -07002444 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2445 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2446 os_snprintf(params, sizeof(params), " peer_go=%d",
2447 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2448 else
2449 params[0] = '\0';
2450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002452 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453 else if (config_methods & WPS_CONFIG_KEYPAD) {
2454 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002455 wpas_prov_disc_local_display(wpa_s, peer, params,
2456 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002458 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2459 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460
Jouni Malinen75ecf522011-06-27 15:19:46 -07002461 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2462 P2P_PROV_DISC_SUCCESS,
2463 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464}
2465
2466
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002467static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2468 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002469{
2470 struct wpa_supplicant *wpa_s = ctx;
2471
Dmitry Shmidt04949592012-07-19 12:16:46 -07002472 if (wpa_s->p2p_fallback_to_go_neg) {
2473 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2474 "failed - fall back to GO Negotiation");
2475 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2476 return;
2477 }
2478
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002479 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002480 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002481 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2482 "join-existing-group operation (no ACK for PD "
2483 "Req attempts)");
2484 wpas_p2p_join_start(wpa_s);
2485 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002486 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002487
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002488 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2489 " p2p_dev_addr=" MACSTR " status=%d",
2490 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002491
Jouni Malinen75ecf522011-06-27 15:19:46 -07002492 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2493 status, 0, 0);
2494}
2495
2496
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002497static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2498{
2499 if (channels == NULL)
2500 return 1; /* Assume no restrictions */
2501 return p2p_channels_includes_freq(channels, freq);
2502
2503}
2504
2505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2507 const u8 *go_dev_addr, const u8 *ssid,
2508 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002509 int *force_freq, int persistent_group,
2510 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511{
2512 struct wpa_supplicant *wpa_s = ctx;
2513 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 int res;
2515 struct wpa_supplicant *grp;
2516
2517 if (!persistent_group) {
2518 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2519 " to join an active group", MAC2STR(sa));
2520 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2521 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2522 == 0 ||
2523 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2524 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2525 "authorized invitation");
2526 goto accept_inv;
2527 }
2528 /*
2529 * Do not accept the invitation automatically; notify user and
2530 * request approval.
2531 */
2532 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2533 }
2534
2535 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2536 if (grp) {
2537 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2538 "running persistent group");
2539 if (*go)
2540 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2541 goto accept_inv;
2542 }
2543
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002544 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2545 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2546 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2547 "invitation to re-invoke a persistent group");
2548 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2550
2551 for (s = wpa_s->conf->ssid; s; s = s->next) {
2552 if (s->disabled == 2 &&
2553 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2554 s->ssid_len == ssid_len &&
2555 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2556 break;
2557 }
2558
2559 if (!s) {
2560 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2561 " requested reinvocation of an unknown group",
2562 MAC2STR(sa));
2563 return P2P_SC_FAIL_UNKNOWN_GROUP;
2564 }
2565
2566 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2567 *go = 1;
2568 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2569 wpa_printf(MSG_DEBUG, "P2P: The only available "
2570 "interface is already in use - reject "
2571 "invitation");
2572 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2573 }
2574 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2575 } else if (s->mode == WPAS_MODE_P2P_GO) {
2576 *go = 1;
2577 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2578 {
2579 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2580 "interface address for the group");
2581 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2582 }
2583 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2584 ETH_ALEN);
2585 }
2586
2587accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002588 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2589
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002590 /* Get one of the frequencies currently in use */
2591 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2592 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002594 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002595 }
2596
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002597 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2598 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002599 if (*go == 0) {
2600 /* We are the client */
2601 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2602 "running a GO but we are capable of MCC, "
2603 "figure out the best channel to use");
2604 *force_freq = 0;
2605 } else if (!freq_included(channels, *force_freq)) {
2606 /* We are the GO, and *force_freq is not in the
2607 * intersection */
2608 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2609 "in intersection but we are capable of MCC, "
2610 "figure out the best channel to use",
2611 *force_freq);
2612 *force_freq = 0;
2613 }
2614 }
2615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616 return P2P_SC_SUCCESS;
2617}
2618
2619
2620static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2621 const u8 *ssid, size_t ssid_len,
2622 const u8 *go_dev_addr, u8 status,
2623 int op_freq)
2624{
2625 struct wpa_supplicant *wpa_s = ctx;
2626 struct wpa_ssid *s;
2627
2628 for (s = wpa_s->conf->ssid; s; s = s->next) {
2629 if (s->disabled == 2 &&
2630 s->ssid_len == ssid_len &&
2631 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2632 break;
2633 }
2634
2635 if (status == P2P_SC_SUCCESS) {
2636 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2637 " was accepted; op_freq=%d MHz",
2638 MAC2STR(sa), op_freq);
2639 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002640 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002642 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002644 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002646 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647 }
2648 return;
2649 }
2650
2651 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2652 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2653 " was rejected (status %u)", MAC2STR(sa), status);
2654 return;
2655 }
2656
2657 if (!s) {
2658 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002659 wpa_msg_global(wpa_s, MSG_INFO,
2660 P2P_EVENT_INVITATION_RECEIVED
2661 "sa=" MACSTR " go_dev_addr=" MACSTR
2662 " bssid=" MACSTR " unknown-network",
2663 MAC2STR(sa), MAC2STR(go_dev_addr),
2664 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002665 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002666 wpa_msg_global(wpa_s, MSG_INFO,
2667 P2P_EVENT_INVITATION_RECEIVED
2668 "sa=" MACSTR " go_dev_addr=" MACSTR
2669 " unknown-network",
2670 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 }
2672 return;
2673 }
2674
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002675 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002676 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2677 "sa=" MACSTR " persistent=%d freq=%d",
2678 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002679 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002680 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2681 "sa=" MACSTR " persistent=%d",
2682 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002683 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684}
2685
2686
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002687static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2688 struct wpa_ssid *ssid,
2689 const u8 *peer)
2690{
2691 size_t i;
2692
2693 if (ssid == NULL)
2694 return;
2695
2696 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2697 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2698 ETH_ALEN) == 0)
2699 break;
2700 }
2701 if (i >= ssid->num_p2p_clients) {
2702 if (ssid->mode != WPAS_MODE_P2P_GO &&
2703 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2704 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2705 "due to invitation result", ssid->id);
2706 wpas_notify_network_removed(wpa_s, ssid);
2707 wpa_config_remove_network(wpa_s->conf, ssid->id);
2708 return;
2709 }
2710 return; /* Peer not found in client list */
2711 }
2712
2713 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2714 "group %d client list due to invitation result",
2715 MAC2STR(peer), ssid->id);
2716 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2717 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2718 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2719 ssid->num_p2p_clients--;
2720#ifndef CONFIG_NO_CONFIG_WRITE
2721 if (wpa_s->parent->conf->update_config &&
2722 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2723 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2724#endif /* CONFIG_NO_CONFIG_WRITE */
2725}
2726
2727
2728static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2729 const u8 *peer)
2730{
2731 struct wpa_ssid *ssid;
2732
2733 wpa_s = wpa_s->global->p2p_invite_group;
2734 if (wpa_s == NULL)
2735 return; /* No known invitation group */
2736 ssid = wpa_s->current_ssid;
2737 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2738 !ssid->p2p_persistent_group)
2739 return; /* Not operating as a GO in persistent group */
2740 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2741 ssid->ssid, ssid->ssid_len);
2742 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2743}
2744
2745
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002746static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002747 const struct p2p_channels *channels,
2748 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749{
2750 struct wpa_supplicant *wpa_s = ctx;
2751 struct wpa_ssid *ssid;
2752
2753 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002754 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2755 "status=%d " MACSTR,
2756 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002757 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002758 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2759 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760 }
2761 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2762
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002763 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2764 status, MAC2STR(peer));
2765 if (wpa_s->pending_invite_ssid_id == -1) {
2766 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2767 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002768 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002771 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2772 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2773 "invitation exchange to indicate readiness for "
2774 "re-invocation");
2775 }
2776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002778 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2779 ssid = wpa_config_get_network(
2780 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2781 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2782 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783 wpas_p2p_remove_pending_group_interface(wpa_s);
2784 return;
2785 }
2786
2787 ssid = wpa_config_get_network(wpa_s->conf,
2788 wpa_s->pending_invite_ssid_id);
2789 if (ssid == NULL) {
2790 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2791 "data matching with invitation");
2792 return;
2793 }
2794
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002795 /*
2796 * The peer could have missed our ctrl::ack frame for Invitation
2797 * Response and continue retransmitting the frame. To reduce the
2798 * likelihood of the peer not getting successful TX status for the
2799 * Invitation Response frame, wait a short time here before starting
2800 * the persistent group so that we will remain on the current channel to
2801 * acknowledge any possible retransmission from the peer.
2802 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002803#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002804 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2805 "starting persistent group");
2806 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002807#else
2808 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2809 "starting persistent group");
2810 os_sleep(0, 100000);
2811#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002814 ssid->mode == WPAS_MODE_P2P_GO,
2815 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002816 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817}
2818
2819
Dmitry Shmidt04949592012-07-19 12:16:46 -07002820static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2821 unsigned int freq)
2822{
2823 unsigned int i;
2824
2825 if (global->p2p_disallow_freq == NULL)
2826 return 0;
2827
2828 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2829 if (freq >= global->p2p_disallow_freq[i].min &&
2830 freq <= global->p2p_disallow_freq[i].max)
2831 return 1;
2832 }
2833
2834 return 0;
2835}
2836
2837
2838static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2839{
2840 reg->channel[reg->channels] = chan;
2841 reg->channels++;
2842}
2843
2844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002845static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2846 struct p2p_channels *chan)
2847{
2848 int i, cla = 0;
2849
2850 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2851 "band");
2852
2853 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2854 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002855 chan->reg_class[cla].channels = 0;
2856 for (i = 0; i < 11; i++) {
2857 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2858 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2859 }
2860 if (chan->reg_class[cla].channels)
2861 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002862
2863 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2864 "band");
2865
2866 /* Operating class 115 - 5 GHz, channels 36-48 */
2867 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002868 chan->reg_class[cla].channels = 0;
2869 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2870 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2871 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2872 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2873 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2874 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2875 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2876 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2877 if (chan->reg_class[cla].channels)
2878 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879
2880 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2881 "band");
2882
2883 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2884 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002885 chan->reg_class[cla].channels = 0;
2886 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2887 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2888 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2889 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2890 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2891 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2892 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2893 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2894 if (chan->reg_class[cla].channels)
2895 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896
2897 chan->reg_classes = cla;
2898 return 0;
2899}
2900
2901
2902static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2903 u16 num_modes,
2904 enum hostapd_hw_mode mode)
2905{
2906 u16 i;
2907
2908 for (i = 0; i < num_modes; i++) {
2909 if (modes[i].mode == mode)
2910 return &modes[i];
2911 }
2912
2913 return NULL;
2914}
2915
2916
Dmitry Shmidt04949592012-07-19 12:16:46 -07002917static int has_channel(struct wpa_global *global,
2918 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002919{
2920 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002921 unsigned int freq;
2922
2923 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2924 chan * 5;
2925 if (wpas_p2p_disallowed_freq(global, freq))
2926 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927
2928 for (i = 0; i < mode->num_channels; i++) {
2929 if (mode->channels[i].chan == chan) {
2930 if (flags)
2931 *flags = mode->channels[i].flag;
2932 return !(mode->channels[i].flag &
2933 (HOSTAPD_CHAN_DISABLED |
2934 HOSTAPD_CHAN_PASSIVE_SCAN |
2935 HOSTAPD_CHAN_NO_IBSS |
2936 HOSTAPD_CHAN_RADAR));
2937 }
2938 }
2939
2940 return 0;
2941}
2942
2943
2944struct p2p_oper_class_map {
2945 enum hostapd_hw_mode mode;
2946 u8 op_class;
2947 u8 min_chan;
2948 u8 max_chan;
2949 u8 inc;
2950 enum { BW20, BW40PLUS, BW40MINUS } bw;
2951};
2952
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002953static struct p2p_oper_class_map op_class[] = {
2954 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002955#if 0 /* Do not enable HT40 on 2 GHz for now */
2956 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2957 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2958#endif
2959 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2960 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2961 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2962 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2963 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2964 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2965 { -1, 0, 0, 0, 0, BW20 }
2966};
2967
2968
2969static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2970 struct hostapd_hw_modes *mode,
2971 u8 channel, u8 bw)
2972{
2973 int flag;
2974
2975 if (!has_channel(wpa_s->global, mode, channel, &flag))
2976 return -1;
2977 if (bw == BW40MINUS &&
2978 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2979 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2980 return 0;
2981 if (bw == BW40PLUS &&
2982 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2983 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2984 return 0;
2985 return 1;
2986}
2987
2988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2990 struct p2p_channels *chan)
2991{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002992 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002994
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002995 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2997 "of all supported channels; assume dualband "
2998 "support");
2999 return wpas_p2p_default_channels(wpa_s, chan);
3000 }
3001
3002 cla = 0;
3003
3004 for (op = 0; op_class[op].op_class; op++) {
3005 struct p2p_oper_class_map *o = &op_class[op];
3006 u8 ch;
3007 struct p2p_reg_class *reg = NULL;
3008
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003009 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 if (mode == NULL)
3011 continue;
3012 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003013 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014 continue;
3015 if (reg == NULL) {
3016 wpa_printf(MSG_DEBUG, "P2P: Add operating "
3017 "class %u", o->op_class);
3018 reg = &chan->reg_class[cla];
3019 cla++;
3020 reg->reg_class = o->op_class;
3021 }
3022 reg->channel[reg->channels] = ch;
3023 reg->channels++;
3024 }
3025 if (reg) {
3026 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3027 reg->channel, reg->channels);
3028 }
3029 }
3030
3031 chan->reg_classes = cla;
3032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003033 return 0;
3034}
3035
3036
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003037int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3038 struct hostapd_hw_modes *mode, u8 channel)
3039{
3040 int op, ret;
3041
3042 for (op = 0; op_class[op].op_class; op++) {
3043 struct p2p_oper_class_map *o = &op_class[op];
3044 u8 ch;
3045
3046 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3047 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3048 o->bw == BW20 || ch != channel)
3049 continue;
3050 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3051 if (ret < 0)
3052 continue;
3053 else if (ret > 0)
3054 return (o->bw == BW40MINUS) ? -1 : 1;
3055 else
3056 return 0;
3057 }
3058 }
3059 return 0;
3060}
3061
3062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3064 size_t buf_len)
3065{
3066 struct wpa_supplicant *wpa_s = ctx;
3067
3068 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3069 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3070 break;
3071 }
3072 if (wpa_s == NULL)
3073 return -1;
3074
3075 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3076}
3077
3078
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003079static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3080{
3081 struct wpa_supplicant *wpa_s = ctx;
3082
3083 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3084 struct wpa_ssid *ssid = wpa_s->current_ssid;
3085 if (ssid == NULL)
3086 continue;
3087 if (ssid->mode != WPAS_MODE_INFRA)
3088 continue;
3089 if (wpa_s->wpa_state != WPA_COMPLETED &&
3090 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3091 continue;
3092 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3093 return 1;
3094 }
3095
3096 return 0;
3097}
3098
3099
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003100static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3101{
3102 struct wpa_supplicant *wpa_s = ctx;
3103 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3104}
3105
3106
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003107int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3108{
3109 struct wpa_interface iface;
3110 struct wpa_supplicant *p2pdev_wpa_s;
3111 char ifname[100];
3112 char force_name[100];
3113 int ret;
3114
3115 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3116 wpa_s->ifname);
3117 force_name[0] = '\0';
3118 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3119 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3120 force_name, wpa_s->pending_interface_addr, NULL);
3121 if (ret < 0) {
3122 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3123 return ret;
3124 }
3125 os_strlcpy(wpa_s->pending_interface_name, ifname,
3126 sizeof(wpa_s->pending_interface_name));
3127
3128 os_memset(&iface, 0, sizeof(iface));
3129 iface.p2p_mgmt = 1;
3130 iface.ifname = wpa_s->pending_interface_name;
3131 iface.driver = wpa_s->driver->name;
3132 iface.driver_param = wpa_s->conf->driver_param;
3133 iface.confname = wpa_s->confname;
3134 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3135 if (!p2pdev_wpa_s) {
3136 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3137 return -1;
3138 }
3139 p2pdev_wpa_s->parent = wpa_s;
3140
3141 wpa_s->pending_interface_name[0] = '\0';
3142 return 0;
3143}
3144
3145
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003146/**
3147 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3148 * @global: Pointer to global data from wpa_supplicant_init()
3149 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3150 * Returns: 0 on success, -1 on failure
3151 */
3152int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3153{
3154 struct p2p_config p2p;
3155 unsigned int r;
3156 int i;
3157
3158 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3159 return 0;
3160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161 if (global->p2p)
3162 return 0;
3163
3164 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3165 struct p2p_params params;
3166
3167 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3168 os_memset(&params, 0, sizeof(params));
3169 params.dev_name = wpa_s->conf->device_name;
3170 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3171 WPS_DEV_TYPE_LEN);
3172 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3173 os_memcpy(params.sec_dev_type,
3174 wpa_s->conf->sec_device_type,
3175 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3176
3177 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3178 return -1;
3179
3180 return 0;
3181 }
3182
3183 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003185 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186 p2p.p2p_scan = wpas_p2p_scan;
3187 p2p.send_action = wpas_send_action;
3188 p2p.send_action_done = wpas_send_action_done;
3189 p2p.go_neg_completed = wpas_go_neg_completed;
3190 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3191 p2p.dev_found = wpas_dev_found;
3192 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003193 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003194 p2p.start_listen = wpas_start_listen;
3195 p2p.stop_listen = wpas_stop_listen;
3196 p2p.send_probe_resp = wpas_send_probe_resp;
3197 p2p.sd_request = wpas_sd_request;
3198 p2p.sd_response = wpas_sd_response;
3199 p2p.prov_disc_req = wpas_prov_disc_req;
3200 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003201 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003202 p2p.invitation_process = wpas_invitation_process;
3203 p2p.invitation_received = wpas_invitation_received;
3204 p2p.invitation_result = wpas_invitation_result;
3205 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003206 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207
3208 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003209 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210 p2p.dev_name = wpa_s->conf->device_name;
3211 p2p.manufacturer = wpa_s->conf->manufacturer;
3212 p2p.model_name = wpa_s->conf->model_name;
3213 p2p.model_number = wpa_s->conf->model_number;
3214 p2p.serial_number = wpa_s->conf->serial_number;
3215 if (wpa_s->wps) {
3216 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3217 p2p.config_methods = wpa_s->wps->config_methods;
3218 }
3219
3220 if (wpa_s->conf->p2p_listen_reg_class &&
3221 wpa_s->conf->p2p_listen_channel) {
3222 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3223 p2p.channel = wpa_s->conf->p2p_listen_channel;
3224 } else {
3225 p2p.reg_class = 81;
3226 /*
3227 * Pick one of the social channels randomly as the listen
3228 * channel.
3229 */
3230 os_get_random((u8 *) &r, sizeof(r));
3231 p2p.channel = 1 + (r % 3) * 5;
3232 }
3233 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3234
3235 if (wpa_s->conf->p2p_oper_reg_class &&
3236 wpa_s->conf->p2p_oper_channel) {
3237 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3238 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3239 p2p.cfg_op_channel = 1;
3240 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3241 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3242
3243 } else {
3244 p2p.op_reg_class = 81;
3245 /*
3246 * Use random operation channel from (1, 6, 11) if no other
3247 * preference is indicated.
3248 */
3249 os_get_random((u8 *) &r, sizeof(r));
3250 p2p.op_channel = 1 + (r % 3) * 5;
3251 p2p.cfg_op_channel = 0;
3252 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3253 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3254 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003255
3256 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3257 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3258 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3259 }
3260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003261 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3262 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3263 p2p.country[2] = 0x04;
3264 } else
3265 os_memcpy(p2p.country, "XX\x04", 3);
3266
3267 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3268 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3269 "channel list");
3270 return -1;
3271 }
3272
3273 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3274 WPS_DEV_TYPE_LEN);
3275
3276 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3277 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3278 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3279
3280 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3281 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3282
3283 p2p.max_peers = 100;
3284
3285 if (wpa_s->conf->p2p_ssid_postfix) {
3286 p2p.ssid_postfix_len =
3287 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3288 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3289 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3290 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3291 p2p.ssid_postfix_len);
3292 }
3293
3294 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3295
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003296 p2p.max_listen = wpa_s->max_remain_on_chan;
3297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298 global->p2p = p2p_init(&p2p);
3299 if (global->p2p == NULL)
3300 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003301 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302
3303 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3304 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3305 continue;
3306 p2p_add_wps_vendor_extension(
3307 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3308 }
3309
3310 return 0;
3311}
3312
3313
3314/**
3315 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3316 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3317 *
3318 * This function deinitialize per-interface P2P data.
3319 */
3320void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3321{
3322 if (wpa_s->driver && wpa_s->drv_priv)
3323 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003324
3325 if (wpa_s->go_params) {
3326 /* Clear any stored provisioning info */
3327 p2p_clear_provisioning_info(
3328 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003329 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003330 }
3331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003332 os_free(wpa_s->go_params);
3333 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003334#ifdef ANDROID_P2P
3335 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3336#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3338 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3339 wpa_s->p2p_long_listen = 0;
3340 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3341 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3342 wpas_p2p_remove_pending_group_interface(wpa_s);
3343
3344 /* TODO: remove group interface from the driver if this wpa_s instance
3345 * is on top of a P2P group interface */
3346}
3347
3348
3349/**
3350 * wpas_p2p_deinit_global - Deinitialize global P2P module
3351 * @global: Pointer to global data from wpa_supplicant_init()
3352 *
3353 * This function deinitializes the global (per device) P2P module.
3354 */
3355void wpas_p2p_deinit_global(struct wpa_global *global)
3356{
3357 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003358
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003359 wpa_s = global->ifaces;
3360 if (wpa_s)
3361 wpas_p2p_service_flush(wpa_s);
3362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363 if (global->p2p == NULL)
3364 return;
3365
3366 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003367 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3368 wpa_s = wpa_s->next;
3369 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370 tmp = global->ifaces;
3371 while (tmp &&
3372 (tmp == wpa_s ||
3373 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3374 tmp = tmp->next;
3375 }
3376 if (tmp == NULL)
3377 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003378 /* Disconnect from the P2P group and deinit the interface */
3379 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003380 }
3381
3382 /*
3383 * Deinit GO data on any possibly remaining interface (if main
3384 * interface is used as GO).
3385 */
3386 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3387 if (wpa_s->ap_iface)
3388 wpas_p2p_group_deinit(wpa_s);
3389 }
3390
3391 p2p_deinit(global->p2p);
3392 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003393 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394}
3395
3396
3397static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3398{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003399 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3400 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003401 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003402 if (wpa_s->drv_flags &
3403 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3404 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3405 return 1; /* P2P group requires a new interface in every case
3406 */
3407 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3408 return 0; /* driver does not support concurrent operations */
3409 if (wpa_s->global->ifaces->next)
3410 return 1; /* more that one interface already in use */
3411 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3412 return 1; /* this interface is already in use */
3413 return 0;
3414}
3415
3416
3417static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3418 const u8 *peer_addr,
3419 enum p2p_wps_method wps_method,
3420 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003421 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003422 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003423{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003424 if (persistent_group && wpa_s->conf->persistent_reconnect)
3425 persistent_group = 2;
3426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3428 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3429 go_intent, own_interface_addr,
3430 force_freq, persistent_group);
3431 }
3432
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003433 /*
3434 * Increase GO config timeout if HT40 is used since it takes some time
3435 * to scan channels for coex purposes before the BSS can be started.
3436 */
3437 p2p_set_config_timeout(wpa_s->global->p2p,
3438 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3441 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003442 persistent_group, ssid ? ssid->ssid : NULL,
3443 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003444 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003445}
3446
3447
3448static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3449 const u8 *peer_addr,
3450 enum p2p_wps_method wps_method,
3451 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003452 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003453 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003454{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003455 if (persistent_group && wpa_s->conf->persistent_reconnect)
3456 persistent_group = 2;
3457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003458 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3459 return -1;
3460
3461 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3462 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003463 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003464 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465}
3466
3467
3468static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3469{
3470 wpa_s->p2p_join_scan_count++;
3471 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3472 wpa_s->p2p_join_scan_count);
3473 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3474 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3475 " for join operationg - stop join attempt",
3476 MAC2STR(wpa_s->pending_join_iface_addr));
3477 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003478 if (wpa_s->p2p_auto_pd) {
3479 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003480 wpa_msg_global(wpa_s, MSG_INFO,
3481 P2P_EVENT_PROV_DISC_FAILURE
3482 " p2p_dev_addr=" MACSTR " status=N/A",
3483 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003484 return;
3485 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003486 wpa_msg_global(wpa_s->parent, MSG_INFO,
3487 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488 }
3489}
3490
3491
Dmitry Shmidt04949592012-07-19 12:16:46 -07003492static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3493{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003494 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003495
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003496 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3497 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003498 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003499 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003500
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003501 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3502 if (!freqs)
3503 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003504
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003505 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3506 wpa_s->num_multichan_concurrent);
3507 if (num < 0) {
3508 res = 1;
3509 goto exit_free;
3510 }
3511
3512 for (i = 0; i < num; i++) {
3513 if (freqs[i] == freq) {
3514 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3515 freq);
3516 res = 0;
3517 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003518 }
3519 }
3520
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003521 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003522
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003523exit_free:
3524 os_free(freqs);
3525 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003526}
3527
3528
3529static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3530 const u8 *peer_dev_addr)
3531{
3532 struct wpa_bss *bss;
3533 int updated;
3534
3535 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3536 if (bss == NULL)
3537 return -1;
3538 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3539 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3540 "last scan");
3541 return 0;
3542 }
3543
3544 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3545 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3546 "%ld.%06ld (%supdated in last scan)",
3547 bss->last_update.sec, bss->last_update.usec,
3548 updated ? "": "not ");
3549
3550 return updated;
3551}
3552
3553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3555 struct wpa_scan_results *scan_res)
3556{
3557 struct wpa_bss *bss;
3558 int freq;
3559 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3562
3563 if (wpa_s->global->p2p_disabled)
3564 return;
3565
Dmitry Shmidt04949592012-07-19 12:16:46 -07003566 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3567 scan_res ? (int) scan_res->num : -1,
3568 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003569
3570 if (scan_res)
3571 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3572
Dmitry Shmidt04949592012-07-19 12:16:46 -07003573 if (wpa_s->p2p_auto_pd) {
3574 int join = wpas_p2p_peer_go(wpa_s,
3575 wpa_s->pending_join_dev_addr);
3576 if (join == 0 &&
3577 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3578 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003579 bss = wpa_bss_get_bssid_latest(
3580 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003581 if (bss) {
3582 freq = bss->freq;
3583 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3584 "the peer " MACSTR " at %d MHz",
3585 wpa_s->auto_pd_scan_retry,
3586 MAC2STR(wpa_s->
3587 pending_join_dev_addr),
3588 freq);
3589 wpas_p2p_join_scan_req(wpa_s, freq);
3590 return;
3591 }
3592 }
3593
3594 if (join < 0)
3595 join = 0;
3596
3597 wpa_s->p2p_auto_pd = 0;
3598 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3599 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3600 MAC2STR(wpa_s->pending_join_dev_addr), join);
3601 if (p2p_prov_disc_req(wpa_s->global->p2p,
3602 wpa_s->pending_join_dev_addr,
3603 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003604 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003605 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003606 wpa_msg_global(wpa_s, MSG_INFO,
3607 P2P_EVENT_PROV_DISC_FAILURE
3608 " p2p_dev_addr=" MACSTR " status=N/A",
3609 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003610 }
3611 return;
3612 }
3613
3614 if (wpa_s->p2p_auto_join) {
3615 int join = wpas_p2p_peer_go(wpa_s,
3616 wpa_s->pending_join_dev_addr);
3617 if (join < 0) {
3618 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3619 "running a GO -> use GO Negotiation");
3620 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3621 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3622 wpa_s->p2p_persistent_group, 0, 0, 0,
3623 wpa_s->p2p_go_intent,
3624 wpa_s->p2p_connect_freq,
3625 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003626 wpa_s->p2p_pd_before_go_neg,
3627 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003628 return;
3629 }
3630
3631 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3632 "try to join the group", join ? "" :
3633 " in older scan");
3634 if (!join)
3635 wpa_s->p2p_fallback_to_go_neg = 1;
3636 }
3637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003638 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3639 wpa_s->pending_join_iface_addr);
3640 if (freq < 0 &&
3641 p2p_get_interface_addr(wpa_s->global->p2p,
3642 wpa_s->pending_join_dev_addr,
3643 iface_addr) == 0 &&
3644 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3645 {
3646 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3647 "address for join from " MACSTR " to " MACSTR
3648 " based on newly discovered P2P peer entry",
3649 MAC2STR(wpa_s->pending_join_iface_addr),
3650 MAC2STR(iface_addr));
3651 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3652 ETH_ALEN);
3653
3654 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3655 wpa_s->pending_join_iface_addr);
3656 }
3657 if (freq >= 0) {
3658 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3659 "from P2P peer table: %d MHz", freq);
3660 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003661 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662 if (bss) {
3663 freq = bss->freq;
3664 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3665 "from BSS table: %d MHz", freq);
3666 }
3667 if (freq > 0) {
3668 u16 method;
3669
Dmitry Shmidt04949592012-07-19 12:16:46 -07003670 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003671 wpa_msg_global(wpa_s->parent, MSG_INFO,
3672 P2P_EVENT_GROUP_FORMATION_FAILURE
3673 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003674 return;
3675 }
3676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003677 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3678 "prior to joining an existing group (GO " MACSTR
3679 " freq=%u MHz)",
3680 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3681 wpa_s->pending_pd_before_join = 1;
3682
3683 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003684 case WPS_PIN_DISPLAY:
3685 method = WPS_CONFIG_KEYPAD;
3686 break;
3687 case WPS_PIN_KEYPAD:
3688 method = WPS_CONFIG_DISPLAY;
3689 break;
3690 case WPS_PBC:
3691 method = WPS_CONFIG_PUSHBUTTON;
3692 break;
3693 default:
3694 method = 0;
3695 break;
3696 }
3697
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003698 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3699 wpa_s->pending_join_dev_addr) ==
3700 method)) {
3701 /*
3702 * We have already performed provision discovery for
3703 * joining the group. Proceed directly to join
3704 * operation without duplicated provision discovery. */
3705 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3706 "with " MACSTR " already done - proceed to "
3707 "join",
3708 MAC2STR(wpa_s->pending_join_dev_addr));
3709 wpa_s->pending_pd_before_join = 0;
3710 goto start;
3711 }
3712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003713 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003714 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003715 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003716 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3717 "Discovery Request before joining an "
3718 "existing group");
3719 wpa_s->pending_pd_before_join = 0;
3720 goto start;
3721 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003722 return;
3723 }
3724
3725 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3726 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3727 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3728 wpas_p2p_check_join_scan_limit(wpa_s);
3729 return;
3730
3731start:
3732 /* Start join operation immediately */
3733 wpas_p2p_join_start(wpa_s);
3734}
3735
3736
Dmitry Shmidt04949592012-07-19 12:16:46 -07003737static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003738{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003739 int ret;
3740 struct wpa_driver_scan_params params;
3741 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003742 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003743 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003744#ifdef ANDROID_P2P
3745 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003747 /* If freq is not provided, check the operating freq of the GO and do a
3748 * a directed scan to save time
3749 */
3750 if(!freq) {
3751 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3752 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3753 }
3754#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003755 os_memset(&params, 0, sizeof(params));
3756
3757 /* P2P Wildcard SSID */
3758 params.num_ssids = 1;
3759 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3760 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3761
3762 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003763 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3764 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3765 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003766 if (wps_ie == NULL) {
3767 wpas_p2p_scan_res_join(wpa_s, NULL);
3768 return;
3769 }
3770
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003771 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3772 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773 if (ies == NULL) {
3774 wpabuf_free(wps_ie);
3775 wpas_p2p_scan_res_join(wpa_s, NULL);
3776 return;
3777 }
3778 wpabuf_put_buf(ies, wps_ie);
3779 wpabuf_free(wps_ie);
3780
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003781 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003783 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003784 params.extra_ies = wpabuf_head(ies);
3785 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003786 if (freq > 0) {
3787 freqs[0] = freq;
3788 params.freqs = freqs;
3789 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003790
3791 /*
3792 * Run a scan to update BSS table and start Provision Discovery once
3793 * the new scan results become available.
3794 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003795 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003796 if (!ret) {
3797 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003798 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003799 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800
3801 wpabuf_free(ies);
3802
3803 if (ret) {
3804 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3805 "try again later");
3806 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3807 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3808 wpas_p2p_check_join_scan_limit(wpa_s);
3809 }
3810}
3811
3812
Dmitry Shmidt04949592012-07-19 12:16:46 -07003813static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3814{
3815 struct wpa_supplicant *wpa_s = eloop_ctx;
3816 wpas_p2p_join_scan_req(wpa_s, 0);
3817}
3818
3819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003821 const u8 *dev_addr, enum p2p_wps_method wps_method,
3822 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003823{
3824 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003825 MACSTR " dev " MACSTR ")%s",
3826 MAC2STR(iface_addr), MAC2STR(dev_addr),
3827 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003828
Dmitry Shmidt04949592012-07-19 12:16:46 -07003829 wpa_s->p2p_auto_pd = 0;
3830 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003831 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3832 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3833 wpa_s->pending_join_wps_method = wps_method;
3834
3835 /* Make sure we are not running find during connection establishment */
3836 wpas_p2p_stop_find(wpa_s);
3837
3838 wpa_s->p2p_join_scan_count = 0;
3839 wpas_p2p_join_scan(wpa_s, NULL);
3840 return 0;
3841}
3842
3843
3844static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3845{
3846 struct wpa_supplicant *group;
3847 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003848 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849
3850 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3851 if (group == NULL)
3852 return -1;
3853 if (group != wpa_s) {
3854 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3855 sizeof(group->p2p_pin));
3856 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003857 } else {
3858 /*
3859 * Need to mark the current interface for p2p_group_formation
3860 * when a separate group interface is not used. This is needed
3861 * to allow p2p_cancel stop a pending p2p_connect-join.
3862 * wpas_p2p_init_group_interface() addresses this for the case
3863 * where a separate group interface is used.
3864 */
3865 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 }
3867
3868 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003869 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003870
3871 os_memset(&res, 0, sizeof(res));
3872 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3873 ETH_ALEN);
3874 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003875 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003876 if (bss) {
3877 res.freq = bss->freq;
3878 res.ssid_len = bss->ssid_len;
3879 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3880 }
3881
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003882 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3883 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3884 "starting client");
3885 wpa_drv_cancel_remain_on_channel(wpa_s);
3886 wpa_s->off_channel_freq = 0;
3887 wpa_s->roc_waiting_drv_freq = 0;
3888 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003889 wpas_start_wps_enrollee(group, &res);
3890
3891 /*
3892 * Allow a longer timeout for join-a-running-group than normal 15
3893 * second group formation timeout since the GO may not have authorized
3894 * our connection yet.
3895 */
3896 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3897 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3898 wpa_s, NULL);
3899
3900 return 0;
3901}
3902
3903
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003904static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003905 int *force_freq, int *pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003906{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003907 int *freqs, res;
3908 unsigned int freq_in_use = 0, num, i;
3909
3910 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3911 if (!freqs)
3912 return -1;
3913
3914 num = get_shared_radio_freqs(wpa_s, freqs,
3915 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003916 wpa_printf(MSG_DEBUG,
3917 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
3918 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003919
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003920 if (freq > 0) {
3921 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3922 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3923 "(%u MHz) is not supported for P2P uses",
3924 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003925 res = -3;
3926 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003927 }
3928
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003929 for (i = 0; i < num; i++) {
3930 if (freqs[i] == freq)
3931 freq_in_use = 1;
3932 }
3933
3934 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
3935 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
3936 freq);
3937 res = -2;
3938 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003939 }
3940 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3941 "requested channel (%u MHz)", freq);
3942 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003943 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003944 }
3945
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003946 for (i = 0; i < num; i++) {
3947 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
3948 continue;
3949
3950 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
3951 *force_freq);
3952 *force_freq = freqs[i];
3953
3954 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
3955 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
3956 *pref_freq = *force_freq;
3957 }
3958 break;
3959 }
3960
3961 if (i == num) {
3962 if (num < wpa_s->num_multichan_concurrent) {
3963 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
3964 *force_freq = 0;
3965 } else {
3966 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
3967 res = -2;
3968 goto exit_free;
3969 }
3970 }
3971
3972exit_ok:
3973 res = 0;
3974exit_free:
3975 os_free(freqs);
3976 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003977}
3978
3979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003980/**
3981 * wpas_p2p_connect - Request P2P Group Formation to be started
3982 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3983 * @peer_addr: Address of the peer P2P Device
3984 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3985 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003986 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987 * @join: Whether to join an existing group (as a client) instead of starting
3988 * Group Owner negotiation; @peer_addr is BSSID in that case
3989 * @auth: Whether to only authorize the connection instead of doing that and
3990 * initiating Group Owner negotiation
3991 * @go_intent: GO Intent or -1 to use default
3992 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003993 * @persistent_id: Persistent group credentials to use for forcing GO
3994 * parameters or -1 to generate new values (SSID/passphrase)
3995 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3996 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003997 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3999 * failure, -2 on failure due to channel not currently available,
4000 * -3 if forced channel is not supported
4001 */
4002int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4003 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004004 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004005 int go_intent, int freq, int persistent_id, int pd,
4006 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004008 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004009 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004010 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004011 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004012 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004013
4014 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4015 return -1;
4016
Dmitry Shmidt04949592012-07-19 12:16:46 -07004017 if (persistent_id >= 0) {
4018 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4019 if (ssid == NULL || ssid->disabled != 2 ||
4020 ssid->mode != WPAS_MODE_P2P_GO)
4021 return -1;
4022 }
4023
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004024 if (go_intent < 0)
4025 go_intent = wpa_s->conf->p2p_go_intent;
4026
4027 if (!auth)
4028 wpa_s->p2p_long_listen = 0;
4029
4030 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004031 wpa_s->p2p_persistent_group = !!persistent_group;
4032 wpa_s->p2p_persistent_id = persistent_id;
4033 wpa_s->p2p_go_intent = go_intent;
4034 wpa_s->p2p_connect_freq = freq;
4035 wpa_s->p2p_fallback_to_go_neg = 0;
4036 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004037 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004038
4039 if (pin)
4040 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4041 else if (wps_method == WPS_PIN_DISPLAY) {
4042 ret = wps_generate_pin();
4043 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4044 ret);
4045 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4046 wpa_s->p2p_pin);
4047 } else
4048 wpa_s->p2p_pin[0] = '\0';
4049
Dmitry Shmidt04949592012-07-19 12:16:46 -07004050 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004051 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4052 if (auth) {
4053 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4054 "connect a running group from " MACSTR,
4055 MAC2STR(peer_addr));
4056 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4057 return ret;
4058 }
4059 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4060 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4061 iface_addr) < 0) {
4062 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4063 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4064 dev_addr);
4065 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004066 if (auto_join) {
4067 os_get_time(&wpa_s->p2p_auto_started);
4068 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4069 "%ld.%06ld",
4070 wpa_s->p2p_auto_started.sec,
4071 wpa_s->p2p_auto_started.usec);
4072 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004073 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004074 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4075 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076 return -1;
4077 return ret;
4078 }
4079
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004080 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004081 if (res)
4082 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004083 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004084
4085 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4086
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004087 if (wpa_s->create_p2p_iface) {
4088 /* Prepare to add a new interface for the group */
4089 iftype = WPA_IF_P2P_GROUP;
4090 if (go_intent == 15)
4091 iftype = WPA_IF_P2P_GO;
4092 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4093 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4094 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004095 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004096 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004098 if_addr = wpa_s->pending_interface_addr;
4099 } else
4100 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004101
4102 if (auth) {
4103 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004104 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004105 force_freq, persistent_group, ssid,
4106 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107 return -1;
4108 return ret;
4109 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004110
4111 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4112 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004113 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004114 if (wpa_s->create_p2p_iface)
4115 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116 return -1;
4117 }
4118 return ret;
4119}
4120
4121
4122/**
4123 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4124 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4125 * @freq: Frequency of the channel in MHz
4126 * @duration: Duration of the stay on the channel in milliseconds
4127 *
4128 * This callback is called when the driver indicates that it has started the
4129 * requested remain-on-channel duration.
4130 */
4131void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4132 unsigned int freq, unsigned int duration)
4133{
4134 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4135 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4137 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4138 wpa_s->pending_listen_duration);
4139 wpa_s->pending_listen_freq = 0;
4140 }
4141}
4142
4143
4144static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4145 unsigned int timeout)
4146{
4147 /* Limit maximum Listen state time based on driver limitation. */
4148 if (timeout > wpa_s->max_remain_on_chan)
4149 timeout = wpa_s->max_remain_on_chan;
4150
4151 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4152 return wpa_drv_p2p_listen(wpa_s, timeout);
4153
4154 return p2p_listen(wpa_s->global->p2p, timeout);
4155}
4156
4157
4158/**
4159 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4160 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4161 * @freq: Frequency of the channel in MHz
4162 *
4163 * This callback is called when the driver indicates that a remain-on-channel
4164 * operation has been completed, i.e., the duration on the requested channel
4165 * has timed out.
4166 */
4167void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4168 unsigned int freq)
4169{
4170 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4171 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004172 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4174 return;
4175 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4176 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004177 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178 return;
4179 if (wpa_s->p2p_long_listen > 0)
4180 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4181 if (wpa_s->p2p_long_listen > 0) {
4182 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4183 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4184 }
4185}
4186
4187
4188/**
4189 * wpas_p2p_group_remove - Remove a P2P group
4190 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4191 * @ifname: Network interface name of the group interface or "*" to remove all
4192 * groups
4193 * Returns: 0 on success, -1 on failure
4194 *
4195 * This function is used to remove a P2P group. This can be used to disconnect
4196 * from a group in which the local end is a P2P Client or to end a P2P Group in
4197 * case the local end is the Group Owner. If a virtual network interface was
4198 * created for this group, that interface will be removed. Otherwise, only the
4199 * configured P2P group network will be removed from the interface.
4200 */
4201int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4202{
4203 struct wpa_global *global = wpa_s->global;
4204
4205 if (os_strcmp(ifname, "*") == 0) {
4206 struct wpa_supplicant *prev;
4207 wpa_s = global->ifaces;
4208 while (wpa_s) {
4209 prev = wpa_s;
4210 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004211 if (prev->p2p_group_interface !=
4212 NOT_P2P_GROUP_INTERFACE ||
4213 (prev->current_ssid &&
4214 prev->current_ssid->p2p_group))
4215 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004216 }
4217 return 0;
4218 }
4219
4220 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4221 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4222 break;
4223 }
4224
4225 return wpas_p2p_disconnect(wpa_s);
4226}
4227
4228
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004229static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4230{
4231 unsigned int r;
4232
4233 if (freq == 2) {
4234 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4235 "band");
4236 if (wpa_s->best_24_freq > 0 &&
4237 p2p_supported_freq(wpa_s->global->p2p,
4238 wpa_s->best_24_freq)) {
4239 freq = wpa_s->best_24_freq;
4240 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4241 "channel: %d MHz", freq);
4242 } else {
4243 os_get_random((u8 *) &r, sizeof(r));
4244 freq = 2412 + (r % 3) * 25;
4245 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4246 "channel: %d MHz", freq);
4247 }
4248 }
4249
4250 if (freq == 5) {
4251 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4252 "band");
4253 if (wpa_s->best_5_freq > 0 &&
4254 p2p_supported_freq(wpa_s->global->p2p,
4255 wpa_s->best_5_freq)) {
4256 freq = wpa_s->best_5_freq;
4257 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4258 "channel: %d MHz", freq);
4259 } else {
4260 os_get_random((u8 *) &r, sizeof(r));
4261 freq = 5180 + (r % 4) * 20;
4262 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4263 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4264 "5 GHz channel for P2P group");
4265 return -1;
4266 }
4267 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4268 "channel: %d MHz", freq);
4269 }
4270 }
4271
4272 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4273 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4274 "(%u MHz) is not supported for P2P uses",
4275 freq);
4276 return -1;
4277 }
4278
4279 return freq;
4280}
4281
4282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004283static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4284 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004285 int freq, int ht40,
4286 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004287{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004288 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004289 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004290 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004291
4292 os_memset(params, 0, sizeof(*params));
4293 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004294 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004295 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004296 if (!freq_included(channels, freq)) {
4297 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4298 "accepted", freq);
4299 return -1;
4300 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004301 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4302 "frequency %d MHz", freq);
4303 params->freq = freq;
4304 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4305 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004306 wpa_s->conf->p2p_oper_channel <= 11 &&
4307 freq_included(channels,
4308 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4310 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4311 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004312 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4313 wpa_s->conf->p2p_oper_reg_class == 116 ||
4314 wpa_s->conf->p2p_oper_reg_class == 117 ||
4315 wpa_s->conf->p2p_oper_reg_class == 124 ||
4316 wpa_s->conf->p2p_oper_reg_class == 126 ||
4317 wpa_s->conf->p2p_oper_reg_class == 127) &&
4318 freq_included(channels,
4319 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4321 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4322 "frequency %d MHz", params->freq);
4323 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4324 wpa_s->best_overall_freq > 0 &&
4325 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004326 wpa_s->best_overall_freq) &&
4327 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004328 params->freq = wpa_s->best_overall_freq;
4329 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4330 "channel %d MHz", params->freq);
4331 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4332 wpa_s->best_24_freq > 0 &&
4333 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004334 wpa_s->best_24_freq) &&
4335 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336 params->freq = wpa_s->best_24_freq;
4337 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4338 "channel %d MHz", params->freq);
4339 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4340 wpa_s->best_5_freq > 0 &&
4341 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004342 wpa_s->best_5_freq) &&
4343 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344 params->freq = wpa_s->best_5_freq;
4345 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4346 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004347 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4348 channels))) {
4349 params->freq = pref_freq;
4350 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4351 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004352 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004353 int chan;
4354 for (chan = 0; chan < 11; chan++) {
4355 params->freq = 2412 + chan * 5;
4356 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004357 params->freq) &&
4358 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004359 break;
4360 }
4361 if (chan == 11) {
4362 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4363 "allowed");
4364 return -1;
4365 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4367 "known)", params->freq);
4368 }
4369
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004370 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4371 if (!freqs)
4372 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004374 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4375 wpa_s->num_multichan_concurrent);
4376 if (res < 0) {
4377 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004378 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004380 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004381
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004382 if (!freq) {
4383 for (i = 0; i < num; i++) {
4384 if (freq_included(channels, freqs[i])) {
4385 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4386 freqs[i]);
4387 params->freq = freqs[i];
4388 break;
4389 }
4390 }
4391
4392 if (i == num) {
4393 if (num == wpa_s->num_multichan_concurrent) {
4394 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4395 os_free(freqs);
4396 return -1;
4397 } else {
4398 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4399 }
4400 }
4401 } else {
4402 for (i = 0; i < num; i++) {
4403 if (freqs[i] == freq)
4404 break;
4405 }
4406
4407 if (i == num) {
4408 if (num == wpa_s->num_multichan_concurrent) {
4409 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4410 os_free(freqs);
4411 return -1;
4412 } else {
4413 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4414 }
4415 }
4416 }
4417 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004418 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004419}
4420
4421
4422static struct wpa_supplicant *
4423wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4424 int go)
4425{
4426 struct wpa_supplicant *group_wpa_s;
4427
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004428 if (!wpas_p2p_create_iface(wpa_s)) {
4429 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4430 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004432 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004433
4434 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004435 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004436 wpa_msg_global(wpa_s, MSG_ERROR,
4437 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004439 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004440 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4441 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004442 wpa_msg_global(wpa_s, MSG_ERROR,
4443 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 wpas_p2p_remove_pending_group_interface(wpa_s);
4445 return NULL;
4446 }
4447
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004448 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4449 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450 return group_wpa_s;
4451}
4452
4453
4454/**
4455 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4456 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4457 * @persistent_group: Whether to create a persistent group
4458 * @freq: Frequency for the group or 0 to indicate no hardcoding
4459 * Returns: 0 on success, -1 on failure
4460 *
4461 * This function creates a new P2P group with the local end as the Group Owner,
4462 * i.e., without using Group Owner Negotiation.
4463 */
4464int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004465 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004466{
4467 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004468
4469 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4470 return -1;
4471
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004472 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004473 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004474 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004475
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004476 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4477 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004480 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004481 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004482 if (params.freq &&
4483 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4484 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4485 "(%u MHz) is not supported for P2P uses",
4486 params.freq);
4487 return -1;
4488 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489 p2p_go_params(wpa_s->global->p2p, &params);
4490 params.persistent_group = persistent_group;
4491
4492 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4493 if (wpa_s == NULL)
4494 return -1;
4495 wpas_start_wps_go(wpa_s, &params, 0);
4496
4497 return 0;
4498}
4499
4500
4501static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4502 struct wpa_ssid *params, int addr_allocated)
4503{
4504 struct wpa_ssid *ssid;
4505
4506 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4507 if (wpa_s == NULL)
4508 return -1;
4509
4510 wpa_supplicant_ap_deinit(wpa_s);
4511
4512 ssid = wpa_config_add_network(wpa_s->conf);
4513 if (ssid == NULL)
4514 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515 wpa_config_set_network_defaults(ssid);
4516 ssid->temporary = 1;
4517 ssid->proto = WPA_PROTO_RSN;
4518 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4519 ssid->group_cipher = WPA_CIPHER_CCMP;
4520 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4521 ssid->ssid = os_malloc(params->ssid_len);
4522 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523 wpa_config_remove_network(wpa_s->conf, ssid->id);
4524 return -1;
4525 }
4526 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4527 ssid->ssid_len = params->ssid_len;
4528 ssid->p2p_group = 1;
4529 ssid->export_keys = 1;
4530 if (params->psk_set) {
4531 os_memcpy(ssid->psk, params->psk, 32);
4532 ssid->psk_set = 1;
4533 }
4534 if (params->passphrase)
4535 ssid->passphrase = os_strdup(params->passphrase);
4536
4537 wpa_supplicant_select_network(wpa_s, ssid);
4538
4539 wpa_s->show_group_started = 1;
4540
4541 return 0;
4542}
4543
4544
4545int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4546 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004547 int freq, int ht40,
4548 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004549{
4550 struct p2p_go_neg_results params;
4551 int go = 0;
4552
4553 if (ssid->disabled != 2 || ssid->ssid == NULL)
4554 return -1;
4555
4556 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4557 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4558 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4559 "already running");
4560 return 0;
4561 }
4562
4563 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004564 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565
Dmitry Shmidt04949592012-07-19 12:16:46 -07004566 wpa_s->p2p_fallback_to_go_neg = 0;
4567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568 if (ssid->mode == WPAS_MODE_INFRA)
4569 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4570
4571 if (ssid->mode != WPAS_MODE_P2P_GO)
4572 return -1;
4573
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004574 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4575 if (freq < 0)
4576 return -1;
4577
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004578 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004579 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580
4581 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004582 params.psk_set = ssid->psk_set;
4583 if (params.psk_set)
4584 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004585 if (ssid->passphrase) {
4586 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4587 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4588 "persistent group");
4589 return -1;
4590 }
4591 os_strlcpy(params.passphrase, ssid->passphrase,
4592 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004594 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4595 params.ssid_len = ssid->ssid_len;
4596 params.persistent_group = 1;
4597
4598 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4599 if (wpa_s == NULL)
4600 return -1;
4601
4602 wpas_start_wps_go(wpa_s, &params, 0);
4603
4604 return 0;
4605}
4606
4607
4608static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4609 struct wpabuf *proberesp_ies)
4610{
4611 struct wpa_supplicant *wpa_s = ctx;
4612 if (wpa_s->ap_iface) {
4613 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004614 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4615 wpabuf_free(beacon_ies);
4616 wpabuf_free(proberesp_ies);
4617 return;
4618 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619 if (beacon_ies) {
4620 wpabuf_free(hapd->p2p_beacon_ie);
4621 hapd->p2p_beacon_ie = beacon_ies;
4622 }
4623 wpabuf_free(hapd->p2p_probe_resp_ie);
4624 hapd->p2p_probe_resp_ie = proberesp_ies;
4625 } else {
4626 wpabuf_free(beacon_ies);
4627 wpabuf_free(proberesp_ies);
4628 }
4629 wpa_supplicant_ap_update_beacon(wpa_s);
4630}
4631
4632
4633static void wpas_p2p_idle_update(void *ctx, int idle)
4634{
4635 struct wpa_supplicant *wpa_s = ctx;
4636 if (!wpa_s->ap_iface)
4637 return;
4638 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004639 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640 wpas_p2p_set_group_idle_timeout(wpa_s);
4641 else
4642 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4643}
4644
4645
4646struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004647 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648{
4649 struct p2p_group *group;
4650 struct p2p_group_config *cfg;
4651
4652 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4653 return NULL;
4654 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4655 return NULL;
4656
4657 cfg = os_zalloc(sizeof(*cfg));
4658 if (cfg == NULL)
4659 return NULL;
4660
Dmitry Shmidt04949592012-07-19 12:16:46 -07004661 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004662 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004663 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004664 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4666 if (wpa_s->max_stations &&
4667 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4668 cfg->max_clients = wpa_s->max_stations;
4669 else
4670 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004671 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4672 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 cfg->cb_ctx = wpa_s;
4674 cfg->ie_update = wpas_p2p_ie_update;
4675 cfg->idle_update = wpas_p2p_idle_update;
4676
4677 group = p2p_group_init(wpa_s->global->p2p, cfg);
4678 if (group == NULL)
4679 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004680 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004681 p2p_group_notif_formation_done(group);
4682 wpa_s->p2p_group = group;
4683 return group;
4684}
4685
4686
4687void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4688 int registrar)
4689{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004690 struct wpa_ssid *ssid = wpa_s->current_ssid;
4691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692 if (!wpa_s->p2p_in_provisioning) {
4693 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4694 "provisioning not in progress");
4695 return;
4696 }
4697
Dmitry Shmidt04949592012-07-19 12:16:46 -07004698 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4699 u8 go_dev_addr[ETH_ALEN];
4700 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4701 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4702 ssid->ssid_len);
4703 /* Clear any stored provisioning info */
4704 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4705 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004706
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4708 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004709 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4710 /*
4711 * Use a separate timeout for initial data connection to
4712 * complete to allow the group to be removed automatically if
4713 * something goes wrong in this step before the P2P group idle
4714 * timeout mechanism is taken into use.
4715 */
4716 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4717 wpas_p2p_group_formation_timeout,
4718 wpa_s->parent, NULL);
4719 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720 if (wpa_s->global->p2p)
4721 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4722 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4723 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4724 wpas_group_formation_completed(wpa_s, 1);
4725}
4726
4727
Jouni Malinen75ecf522011-06-27 15:19:46 -07004728void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4729 struct wps_event_fail *fail)
4730{
4731 if (!wpa_s->p2p_in_provisioning) {
4732 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4733 "provisioning not in progress");
4734 return;
4735 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004736
4737 if (wpa_s->go_params) {
4738 p2p_clear_provisioning_info(
4739 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004740 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004741 }
4742
Jouni Malinen75ecf522011-06-27 15:19:46 -07004743 wpas_notify_p2p_wps_failed(wpa_s, fail);
4744}
4745
4746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004747int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004748 const char *config_method,
4749 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750{
4751 u16 config_methods;
4752
Dmitry Shmidt04949592012-07-19 12:16:46 -07004753 wpa_s->p2p_fallback_to_go_neg = 0;
4754 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004755 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004757 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004759 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4760 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004761 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004762 else {
4763 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004764 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004765 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766
Dmitry Shmidt04949592012-07-19 12:16:46 -07004767 if (use == WPAS_P2P_PD_AUTO) {
4768 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4769 wpa_s->pending_pd_config_methods = config_methods;
4770 wpa_s->p2p_auto_pd = 1;
4771 wpa_s->p2p_auto_join = 0;
4772 wpa_s->pending_pd_before_join = 0;
4773 wpa_s->auto_pd_scan_retry = 0;
4774 wpas_p2p_stop_find(wpa_s);
4775 wpa_s->p2p_join_scan_count = 0;
4776 os_get_time(&wpa_s->p2p_auto_started);
4777 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4778 wpa_s->p2p_auto_started.sec,
4779 wpa_s->p2p_auto_started.usec);
4780 wpas_p2p_join_scan(wpa_s, NULL);
4781 return 0;
4782 }
4783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004784 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4785 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004786 config_methods,
4787 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004788 }
4789
4790 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4791 return -1;
4792
4793 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004794 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004795 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004796}
4797
4798
4799int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4800 char *end)
4801{
4802 return p2p_scan_result_text(ies, ies_len, buf, end);
4803}
4804
4805
4806static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4807{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004808 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809 return;
4810
4811 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4812 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004813 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814}
4815
4816
4817int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4818 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004819 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004820 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004821{
4822 wpas_p2p_clear_pending_action_tx(wpa_s);
4823 wpa_s->p2p_long_listen = 0;
4824
4825 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4826 return wpa_drv_p2p_find(wpa_s, timeout, type);
4827
Dmitry Shmidt04949592012-07-19 12:16:46 -07004828 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4829 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004830 return -1;
4831
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004832 wpa_supplicant_cancel_sched_scan(wpa_s);
4833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004834 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004835 num_req_dev_types, req_dev_types, dev_id,
4836 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837}
4838
4839
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004840static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004841{
4842 wpas_p2p_clear_pending_action_tx(wpa_s);
4843 wpa_s->p2p_long_listen = 0;
4844 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4845 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004846 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004847
4848 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4849 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004850 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004851 }
4852
4853 if (wpa_s->global->p2p)
4854 p2p_stop_find(wpa_s->global->p2p);
4855
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004856 return 0;
4857}
4858
4859
4860void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4861{
4862 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4863 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 wpas_p2p_remove_pending_group_interface(wpa_s);
4865}
4866
4867
4868static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4869{
4870 struct wpa_supplicant *wpa_s = eloop_ctx;
4871 wpa_s->p2p_long_listen = 0;
4872}
4873
4874
4875int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4876{
4877 int res;
4878
4879 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4880 return -1;
4881
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004882 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883 wpas_p2p_clear_pending_action_tx(wpa_s);
4884
4885 if (timeout == 0) {
4886 /*
4887 * This is a request for unlimited Listen state. However, at
4888 * least for now, this is mapped to a Listen state for one
4889 * hour.
4890 */
4891 timeout = 3600;
4892 }
4893 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4894 wpa_s->p2p_long_listen = 0;
4895
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004896 /*
4897 * Stop previous find/listen operation to avoid trying to request a new
4898 * remain-on-channel operation while the driver is still running the
4899 * previous one.
4900 */
4901 if (wpa_s->global->p2p)
4902 p2p_stop_find(wpa_s->global->p2p);
4903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4905 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4906 wpa_s->p2p_long_listen = timeout * 1000;
4907 eloop_register_timeout(timeout, 0,
4908 wpas_p2p_long_listen_timeout,
4909 wpa_s, NULL);
4910 }
4911
4912 return res;
4913}
4914
4915
4916int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4917 u8 *buf, size_t len, int p2p_group)
4918{
4919 struct wpabuf *p2p_ie;
4920 int ret;
4921
4922 if (wpa_s->global->p2p_disabled)
4923 return -1;
4924 if (wpa_s->global->p2p == NULL)
4925 return -1;
4926 if (bss == NULL)
4927 return -1;
4928
4929 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4930 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4931 p2p_group, p2p_ie);
4932 wpabuf_free(p2p_ie);
4933
4934 return ret;
4935}
4936
4937
4938int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004939 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004940 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941{
4942 if (wpa_s->global->p2p_disabled)
4943 return 0;
4944 if (wpa_s->global->p2p == NULL)
4945 return 0;
4946
Dmitry Shmidt04949592012-07-19 12:16:46 -07004947 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4948 ie, ie_len)) {
4949 case P2P_PREQ_NOT_P2P:
4950 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4951 ssi_signal);
4952 /* fall through */
4953 case P2P_PREQ_MALFORMED:
4954 case P2P_PREQ_NOT_LISTEN:
4955 case P2P_PREQ_NOT_PROCESSED:
4956 default: /* make gcc happy */
4957 return 0;
4958 case P2P_PREQ_PROCESSED:
4959 return 1;
4960 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961}
4962
4963
4964void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4965 const u8 *sa, const u8 *bssid,
4966 u8 category, const u8 *data, size_t len, int freq)
4967{
4968 if (wpa_s->global->p2p_disabled)
4969 return;
4970 if (wpa_s->global->p2p == NULL)
4971 return;
4972
4973 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4974 freq);
4975}
4976
4977
4978void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4979{
4980 if (wpa_s->global->p2p_disabled)
4981 return;
4982 if (wpa_s->global->p2p == NULL)
4983 return;
4984
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004985 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986}
4987
4988
4989void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4990{
4991 p2p_group_deinit(wpa_s->p2p_group);
4992 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004993
4994 wpa_s->ap_configured_cb = NULL;
4995 wpa_s->ap_configured_cb_ctx = NULL;
4996 wpa_s->ap_configured_cb_data = NULL;
4997 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998}
4999
5000
5001int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5002{
5003 wpa_s->p2p_long_listen = 0;
5004
5005 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5006 return wpa_drv_p2p_reject(wpa_s, addr);
5007
5008 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5009 return -1;
5010
5011 return p2p_reject(wpa_s->global->p2p, addr);
5012}
5013
5014
5015/* Invite to reinvoke a persistent group */
5016int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005017 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005018 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019{
5020 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005021 u8 *bssid = NULL;
5022 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005023 int res;
5024
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005025 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005026 if (peer_addr)
5027 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5028 else
5029 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030
Jouni Malinen31be0a42012-08-31 21:20:51 +03005031 wpa_s->p2p_persistent_go_freq = freq;
5032 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005033 if (ssid->mode == WPAS_MODE_P2P_GO) {
5034 role = P2P_INVITE_ROLE_GO;
5035 if (peer_addr == NULL) {
5036 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5037 "address in invitation command");
5038 return -1;
5039 }
5040 if (wpas_p2p_create_iface(wpa_s)) {
5041 if (wpas_p2p_add_group_interface(wpa_s,
5042 WPA_IF_P2P_GO) < 0) {
5043 wpa_printf(MSG_ERROR, "P2P: Failed to "
5044 "allocate a new interface for the "
5045 "group");
5046 return -1;
5047 }
5048 bssid = wpa_s->pending_interface_addr;
5049 } else
5050 bssid = wpa_s->own_addr;
5051 } else {
5052 role = P2P_INVITE_ROLE_CLIENT;
5053 peer_addr = ssid->bssid;
5054 }
5055 wpa_s->pending_invite_ssid_id = ssid->id;
5056
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005057 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005058 if (res)
5059 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5062 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5063 ssid->ssid, ssid->ssid_len,
5064 go_dev_addr, 1);
5065
5066 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5067 return -1;
5068
5069 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005070 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5071 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072}
5073
5074
5075/* Invite to join an active group */
5076int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5077 const u8 *peer_addr, const u8 *go_dev_addr)
5078{
5079 struct wpa_global *global = wpa_s->global;
5080 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005081 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005083 int persistent;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005084 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005085 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086
Jouni Malinen31be0a42012-08-31 21:20:51 +03005087 wpa_s->p2p_persistent_go_freq = 0;
5088 wpa_s->p2p_go_ht40 = 0;
5089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5091 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5092 break;
5093 }
5094 if (wpa_s == NULL) {
5095 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5096 return -1;
5097 }
5098
5099 ssid = wpa_s->current_ssid;
5100 if (ssid == NULL) {
5101 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5102 "invitation");
5103 return -1;
5104 }
5105
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005106 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005107 persistent = ssid->p2p_persistent_group &&
5108 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5109 ssid->ssid, ssid->ssid_len);
5110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005111 if (ssid->mode == WPAS_MODE_P2P_GO) {
5112 role = P2P_INVITE_ROLE_ACTIVE_GO;
5113 bssid = wpa_s->own_addr;
5114 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005115 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005116 } else {
5117 role = P2P_INVITE_ROLE_CLIENT;
5118 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5119 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5120 "invite to current group");
5121 return -1;
5122 }
5123 bssid = wpa_s->bssid;
5124 if (go_dev_addr == NULL &&
5125 !is_zero_ether_addr(wpa_s->go_dev_addr))
5126 go_dev_addr = wpa_s->go_dev_addr;
5127 }
5128 wpa_s->parent->pending_invite_ssid_id = -1;
5129
5130 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5131 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5132 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005133 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134
5135 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5136 return -1;
5137
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005138 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005139 if (res)
5140 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005141 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005143 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005144 ssid->ssid, ssid->ssid_len, force_freq,
5145 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005146}
5147
5148
5149void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5150{
5151 struct wpa_ssid *ssid = wpa_s->current_ssid;
5152 const char *ssid_txt;
5153 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005154 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005155 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005156 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005157
Dmitry Shmidt04949592012-07-19 12:16:46 -07005158 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5159 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5160 wpa_s->parent, NULL);
5161 }
5162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005163 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005164 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165
5166 wpa_s->show_group_started = 0;
5167
5168 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5169 os_memset(go_dev_addr, 0, ETH_ALEN);
5170 if (ssid->bssid_set)
5171 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5172 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5173 ssid->ssid_len);
5174 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5175
5176 if (wpa_s->global->p2p_group_formation == wpa_s)
5177 wpa_s->global->p2p_group_formation = NULL;
5178
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005179 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5180 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005181 if (ssid->passphrase == NULL && ssid->psk_set) {
5182 char psk[65];
5183 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005184 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5185 "%s client ssid=\"%s\" freq=%d psk=%s "
5186 "go_dev_addr=" MACSTR "%s",
5187 wpa_s->ifname, ssid_txt, freq, psk,
5188 MAC2STR(go_dev_addr),
5189 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005190 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005191 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5192 "%s client ssid=\"%s\" freq=%d "
5193 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5194 wpa_s->ifname, ssid_txt, freq,
5195 ssid->passphrase ? ssid->passphrase : "",
5196 MAC2STR(go_dev_addr),
5197 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005198 }
5199
5200 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005201 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5202 ssid, go_dev_addr);
5203 if (network_id < 0)
5204 network_id = ssid->id;
5205 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005206
5207done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005208 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005209}
5210
5211
5212int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5213 u32 interval1, u32 duration2, u32 interval2)
5214{
5215 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5216 return -1;
5217 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5218 return -1;
5219
5220 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5221 wpa_s->current_ssid == NULL ||
5222 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5223 return -1;
5224
5225 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5226 wpa_s->own_addr, wpa_s->assoc_freq,
5227 duration1, interval1, duration2, interval2);
5228}
5229
5230
5231int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5232 unsigned int interval)
5233{
5234 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5235 return -1;
5236
5237 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5238 return -1;
5239
5240 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5241}
5242
5243
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005244static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5245{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005246 if (wpa_s->current_ssid == NULL) {
5247 /*
5248 * current_ssid can be cleared when P2P client interface gets
5249 * disconnected, so assume this interface was used as P2P
5250 * client.
5251 */
5252 return 1;
5253 }
5254 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005255 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5256}
5257
5258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5260{
5261 struct wpa_supplicant *wpa_s = eloop_ctx;
5262
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005263 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005264 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5265 "disabled");
5266 return;
5267 }
5268
Dmitry Shmidt04949592012-07-19 12:16:46 -07005269 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5270 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005271 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005272}
5273
5274
5275static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5276{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005277 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005278
Dmitry Shmidt04949592012-07-19 12:16:46 -07005279 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5280 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005282 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5283 return;
5284
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005285 timeout = wpa_s->conf->p2p_group_idle;
5286 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5287 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5288 timeout = P2P_MAX_CLIENT_IDLE;
5289
5290 if (timeout == 0)
5291 return;
5292
Dmitry Shmidt04949592012-07-19 12:16:46 -07005293 if (timeout < 0) {
5294 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5295 timeout = 0; /* special client mode no-timeout */
5296 else
5297 return;
5298 }
5299
5300 if (wpa_s->p2p_in_provisioning) {
5301 /*
5302 * Use the normal group formation timeout during the
5303 * provisioning phase to avoid terminating this process too
5304 * early due to group idle timeout.
5305 */
5306 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5307 "during provisioning");
5308 return;
5309 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305310
Dmitry Shmidt04949592012-07-19 12:16:46 -07005311 if (wpa_s->show_group_started) {
5312 /*
5313 * Use the normal group formation timeout between the end of
5314 * the provisioning phase and completion of 4-way handshake to
5315 * avoid terminating this process too early due to group idle
5316 * timeout.
5317 */
5318 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5319 "while waiting for initial 4-way handshake to "
5320 "complete");
5321 return;
5322 }
5323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005324 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005325 timeout);
5326 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5327 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005328}
5329
5330
Jouni Malinen2b89da82012-08-31 22:04:41 +03005331/* Returns 1 if the interface was removed */
5332int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5333 u16 reason_code, const u8 *ie, size_t ie_len,
5334 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005335{
5336 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005337 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005339 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005340
Dmitry Shmidt04949592012-07-19 12:16:46 -07005341 if (!locally_generated)
5342 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5343 ie_len);
5344
5345 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5346 wpa_s->current_ssid &&
5347 wpa_s->current_ssid->p2p_group &&
5348 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5349 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5350 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005351 if (wpas_p2p_group_delete(wpa_s,
5352 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5353 > 0)
5354 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005355 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005356
5357 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005358}
5359
5360
5361void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005362 u16 reason_code, const u8 *ie, size_t ie_len,
5363 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364{
5365 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5366 return;
5367 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5368 return;
5369
Dmitry Shmidt04949592012-07-19 12:16:46 -07005370 if (!locally_generated)
5371 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5372 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005373}
5374
5375
5376void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5377{
5378 struct p2p_data *p2p = wpa_s->global->p2p;
5379
5380 if (p2p == NULL)
5381 return;
5382
5383 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5384 return;
5385
5386 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5387 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5388
5389 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5390 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5391
5392 if (wpa_s->wps &&
5393 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5394 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5395
5396 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5397 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5398
5399 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5400 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5401 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5402 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5403 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5404 }
5405
5406 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5407 p2p_set_sec_dev_types(p2p,
5408 (void *) wpa_s->conf->sec_device_type,
5409 wpa_s->conf->num_sec_device_types);
5410
5411 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5412 int i;
5413 p2p_remove_wps_vendor_extensions(p2p);
5414 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5415 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5416 continue;
5417 p2p_add_wps_vendor_extension(
5418 p2p, wpa_s->conf->wps_vendor_ext[i]);
5419 }
5420 }
5421
5422 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5423 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5424 char country[3];
5425 country[0] = wpa_s->conf->country[0];
5426 country[1] = wpa_s->conf->country[1];
5427 country[2] = 0x04;
5428 p2p_set_country(p2p, country);
5429 }
5430
5431 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5432 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5433 wpa_s->conf->p2p_ssid_postfix ?
5434 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5435 0);
5436 }
5437
5438 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5439 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005440
5441 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5442 u8 reg_class, channel;
5443 int ret;
5444 unsigned int r;
5445 if (wpa_s->conf->p2p_listen_reg_class &&
5446 wpa_s->conf->p2p_listen_channel) {
5447 reg_class = wpa_s->conf->p2p_listen_reg_class;
5448 channel = wpa_s->conf->p2p_listen_channel;
5449 } else {
5450 reg_class = 81;
5451 /*
5452 * Pick one of the social channels randomly as the
5453 * listen channel.
5454 */
5455 os_get_random((u8 *) &r, sizeof(r));
5456 channel = 1 + (r % 3) * 5;
5457 }
5458 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5459 if (ret)
5460 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5461 "failed: %d", ret);
5462 }
5463 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5464 u8 op_reg_class, op_channel, cfg_op_channel;
5465 int ret = 0;
5466 unsigned int r;
5467 if (wpa_s->conf->p2p_oper_reg_class &&
5468 wpa_s->conf->p2p_oper_channel) {
5469 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5470 op_channel = wpa_s->conf->p2p_oper_channel;
5471 cfg_op_channel = 1;
5472 } else {
5473 op_reg_class = 81;
5474 /*
5475 * Use random operation channel from (1, 6, 11)
5476 *if no other preference is indicated.
5477 */
5478 os_get_random((u8 *) &r, sizeof(r));
5479 op_channel = 1 + (r % 3) * 5;
5480 cfg_op_channel = 0;
5481 }
5482 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5483 cfg_op_channel);
5484 if (ret)
5485 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5486 "failed: %d", ret);
5487 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005488
5489 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5490 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5491 wpa_s->conf->p2p_pref_chan) < 0) {
5492 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5493 "update failed");
5494 }
5495 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496}
5497
5498
5499int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5500 int duration)
5501{
5502 if (!wpa_s->ap_iface)
5503 return -1;
5504 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5505 duration);
5506}
5507
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508
5509int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5510{
5511 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5512 return -1;
5513 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5514 return -1;
5515
5516 wpa_s->global->cross_connection = enabled;
5517 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5518
5519 if (!enabled) {
5520 struct wpa_supplicant *iface;
5521
5522 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5523 {
5524 if (iface->cross_connect_enabled == 0)
5525 continue;
5526
5527 iface->cross_connect_enabled = 0;
5528 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005529 wpa_msg_global(iface->parent, MSG_INFO,
5530 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5531 iface->ifname,
5532 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005533 }
5534 }
5535
5536 return 0;
5537}
5538
5539
5540static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5541{
5542 struct wpa_supplicant *iface;
5543
5544 if (!uplink->global->cross_connection)
5545 return;
5546
5547 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5548 if (!iface->cross_connect_enabled)
5549 continue;
5550 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5551 0)
5552 continue;
5553 if (iface->ap_iface == NULL)
5554 continue;
5555 if (iface->cross_connect_in_use)
5556 continue;
5557
5558 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005559 wpa_msg_global(iface->parent, MSG_INFO,
5560 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5561 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005562 }
5563}
5564
5565
5566static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5567{
5568 struct wpa_supplicant *iface;
5569
5570 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5571 if (!iface->cross_connect_enabled)
5572 continue;
5573 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5574 0)
5575 continue;
5576 if (!iface->cross_connect_in_use)
5577 continue;
5578
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005579 wpa_msg_global(iface->parent, MSG_INFO,
5580 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5581 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005582 iface->cross_connect_in_use = 0;
5583 }
5584}
5585
5586
5587void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5588{
5589 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5590 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5591 wpa_s->cross_connect_disallowed)
5592 wpas_p2p_disable_cross_connect(wpa_s);
5593 else
5594 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005595 if (!wpa_s->ap_iface &&
5596 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5597 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005598}
5599
5600
5601void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5602{
5603 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005604 if (!wpa_s->ap_iface &&
5605 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5606 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607 wpas_p2p_set_group_idle_timeout(wpa_s);
5608}
5609
5610
5611static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5612{
5613 struct wpa_supplicant *iface;
5614
5615 if (!wpa_s->global->cross_connection)
5616 return;
5617
5618 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5619 if (iface == wpa_s)
5620 continue;
5621 if (iface->drv_flags &
5622 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5623 continue;
5624 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5625 continue;
5626
5627 wpa_s->cross_connect_enabled = 1;
5628 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5629 sizeof(wpa_s->cross_connect_uplink));
5630 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5631 "%s to %s whenever uplink is available",
5632 wpa_s->ifname, wpa_s->cross_connect_uplink);
5633
5634 if (iface->ap_iface || iface->current_ssid == NULL ||
5635 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5636 iface->cross_connect_disallowed ||
5637 iface->wpa_state != WPA_COMPLETED)
5638 break;
5639
5640 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005641 wpa_msg_global(wpa_s->parent, MSG_INFO,
5642 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5643 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005644 break;
5645 }
5646}
5647
5648
5649int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5650{
5651 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5652 !wpa_s->p2p_in_provisioning)
5653 return 0; /* not P2P client operation */
5654
5655 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5656 "session overlap");
5657 if (wpa_s != wpa_s->parent)
5658 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005659 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005660 return 1;
5661}
5662
5663
5664void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5665{
5666 struct p2p_channels chan;
5667
5668 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5669 return;
5670
5671 os_memset(&chan, 0, sizeof(chan));
5672 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5673 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5674 "channel list");
5675 return;
5676 }
5677
5678 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5679}
5680
5681
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005682static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5683 struct wpa_scan_results *scan_res)
5684{
5685 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5686}
5687
5688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005689int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5690{
5691 struct wpa_global *global = wpa_s->global;
5692 int found = 0;
5693 const u8 *peer;
5694
5695 if (global->p2p == NULL)
5696 return -1;
5697
5698 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5699
5700 if (wpa_s->pending_interface_name[0] &&
5701 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5702 found = 1;
5703
5704 peer = p2p_get_go_neg_peer(global->p2p);
5705 if (peer) {
5706 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5707 MACSTR, MAC2STR(peer));
5708 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005709 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005710 }
5711
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005712 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5713 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5714 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5715 found = 1;
5716 }
5717
5718 if (wpa_s->pending_pd_before_join) {
5719 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5720 wpa_s->pending_pd_before_join = 0;
5721 found = 1;
5722 }
5723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005724 wpas_p2p_stop_find(wpa_s);
5725
5726 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5727 if (wpa_s == global->p2p_group_formation &&
5728 (wpa_s->p2p_in_provisioning ||
5729 wpa_s->parent->pending_interface_type ==
5730 WPA_IF_P2P_CLIENT)) {
5731 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5732 "formation found - cancelling",
5733 wpa_s->ifname);
5734 found = 1;
5735 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5736 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005737 if (wpa_s->p2p_in_provisioning) {
5738 wpas_group_formation_completed(wpa_s, 0);
5739 break;
5740 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005741 wpas_p2p_group_delete(wpa_s,
5742 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005743 break;
5744 }
5745 }
5746
5747 if (!found) {
5748 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5749 return -1;
5750 }
5751
5752 return 0;
5753}
5754
5755
5756void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5757{
5758 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5759 return;
5760
5761 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5762 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005763 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005764}
5765
5766
5767void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5768 int freq_24, int freq_5, int freq_overall)
5769{
5770 struct p2p_data *p2p = wpa_s->global->p2p;
5771 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5772 return;
5773 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5774}
5775
5776
5777int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5778{
5779 u8 peer[ETH_ALEN];
5780 struct p2p_data *p2p = wpa_s->global->p2p;
5781
5782 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5783 return -1;
5784
5785 if (hwaddr_aton(addr, peer))
5786 return -1;
5787
5788 return p2p_unauthorize(p2p, peer);
5789}
5790
5791
5792/**
5793 * wpas_p2p_disconnect - Disconnect from a P2P Group
5794 * @wpa_s: Pointer to wpa_supplicant data
5795 * Returns: 0 on success, -1 on failure
5796 *
5797 * This can be used to disconnect from a group in which the local end is a P2P
5798 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5799 * virtual network interface was created for this group, that interface will be
5800 * removed. Otherwise, only the configured P2P group network will be removed
5801 * from the interface.
5802 */
5803int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5804{
5805
5806 if (wpa_s == NULL)
5807 return -1;
5808
Jouni Malinen2b89da82012-08-31 22:04:41 +03005809 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5810 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005811}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005812
5813
5814int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5815{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005816 int ret;
5817
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005818 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5819 return 0;
5820
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005821 ret = p2p_in_progress(wpa_s->global->p2p);
5822 if (ret == 0) {
5823 /*
5824 * Check whether there is an ongoing WPS provisioning step (or
5825 * other parts of group formation) on another interface since
5826 * p2p_in_progress() does not report this to avoid issues for
5827 * scans during such provisioning step.
5828 */
5829 if (wpa_s->global->p2p_group_formation &&
5830 wpa_s->global->p2p_group_formation != wpa_s) {
5831 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5832 "in group formation",
5833 wpa_s->global->p2p_group_formation->ifname);
5834 ret = 1;
5835 }
5836 }
5837
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07005838 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
5839 struct os_time now;
5840 os_get_time(&now);
5841 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
5842 P2P_MAX_INITIAL_CONN_WAIT_GO) {
5843 /* Wait for the first client has expired */
5844 wpa_s->global->p2p_go_wait_client.sec = 0;
5845 } else {
5846 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
5847 ret = 1;
5848 }
5849 }
5850
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005851 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005852}
5853
5854
5855void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5856 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005857{
5858 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5859 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5860 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005861 /**
5862 * Remove the network by scheduling the group formation
5863 * timeout to happen immediately. The teardown code
5864 * needs to be scheduled to run asynch later so that we
5865 * don't delete data from under ourselves unexpectedly.
5866 * Calling wpas_p2p_group_formation_timeout directly
5867 * causes a series of crashes in WPS failure scenarios.
5868 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005869 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5870 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005871 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5872 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005873 }
5874}
5875
5876
5877struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005878 const u8 *addr, const u8 *ssid,
5879 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005880{
5881 struct wpa_ssid *s;
5882 size_t i;
5883
5884 for (s = wpa_s->conf->ssid; s; s = s->next) {
5885 if (s->disabled != 2)
5886 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005887 if (ssid &&
5888 (ssid_len != s->ssid_len ||
5889 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5890 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005891 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5892 return s; /* peer is GO in the persistent group */
5893 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5894 continue;
5895 for (i = 0; i < s->num_p2p_clients; i++) {
5896 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5897 addr, ETH_ALEN) == 0)
5898 return s; /* peer is P2P client in persistent
5899 * group */
5900 }
5901 }
5902
5903 return NULL;
5904}
5905
5906
5907void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5908 const u8 *addr)
5909{
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07005910 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005911 if (addr == NULL)
5912 return;
5913 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5914}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005915
Dmitry Shmidt04949592012-07-19 12:16:46 -07005916
5917static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5918 int group_added)
5919{
5920 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005921 if (wpa_s->global->p2p_group_formation)
5922 group = wpa_s->global->p2p_group_formation;
5923 wpa_s = wpa_s->parent;
5924 offchannel_send_action_done(wpa_s);
5925 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005926 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005927 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5928 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5929 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5930 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5931 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005932 wpa_s->p2p_pd_before_go_neg,
5933 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005934}
5935
5936
5937int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5938{
5939 if (!wpa_s->p2p_fallback_to_go_neg ||
5940 wpa_s->p2p_in_provisioning <= 5)
5941 return 0;
5942
5943 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5944 return 0; /* peer operating as a GO */
5945
5946 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5947 "fallback to GO Negotiation");
5948 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5949
5950 return 1;
5951}
5952
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005953
5954unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5955{
5956 const char *rn, *rn2;
5957 struct wpa_supplicant *ifs;
5958
5959 if (wpa_s->wpa_state > WPA_SCANNING) {
5960 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5961 "concurrent operation",
5962 P2P_CONCURRENT_SEARCH_DELAY);
5963 return P2P_CONCURRENT_SEARCH_DELAY;
5964 }
5965
5966 if (!wpa_s->driver->get_radio_name)
5967 return 0;
5968 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5969 if (rn == NULL || rn[0] == '\0')
5970 return 0;
5971
5972 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5973 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5974 continue;
5975
5976 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5977 if (!rn2 || os_strcmp(rn, rn2) != 0)
5978 continue;
5979 if (ifs->wpa_state > WPA_SCANNING) {
5980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5981 "delay due to concurrent operation on "
5982 "interface %s",
5983 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5984 return P2P_CONCURRENT_SEARCH_DELAY;
5985 }
5986 }
5987
5988 return 0;
5989}
5990
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005991
5992void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5993{
5994 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5995 "pending anymore (sta_scan_pending=%d "
5996 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5997 wpa_s->global->p2p_cb_on_scan_complete);
5998 wpa_s->sta_scan_pending = 0;
5999
6000 if (!wpa_s->global->p2p_cb_on_scan_complete)
6001 return;
6002 wpa_s->global->p2p_cb_on_scan_complete = 0;
6003
6004 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6005 return;
6006
6007 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6008 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6009 "continued after successful connection");
6010 p2p_increase_search_delay(wpa_s->global->p2p,
6011 wpas_p2p_search_delay(wpa_s));
6012 }
6013}
6014
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006015#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006016static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6017{
6018 struct wpa_supplicant *wpa_s = eloop_ctx;
6019
6020 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6021 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6022}
6023
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006024int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6025 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006026{
6027 struct wpa_supplicant *iface = NULL;
6028 struct p2p_data *p2p = wpa_s->global->p2p;
6029
6030 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006031 if ((iface->current_ssid) &&
6032 (iface->current_ssid->frequency != freq) &&
6033 ((iface->p2p_group_interface) ||
6034 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006035
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006036 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
6037 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
6038 /* Try to see whether we can move the GO. If it
6039 * is not possible, remove the GO interface
6040 */
6041 if (wpa_drv_switch_channel(iface, freq) == 0) {
6042 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
6043 iface->current_ssid->frequency = freq;
6044 continue;
6045 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006046 }
6047
6048 /* If GO cannot be moved or if the conflicting interface is a
6049 * P2P Client, remove the interface depending up on the connection
6050 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006051 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07006052 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006053 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006054 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006055 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006056 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
6057 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006058 /* If connection in progress is p2p connection, do not proceed for the connection */
6059 if (wpa_s == iface)
6060 return -1;
6061 else
6062 /* If connection in progress is STA connection, proceed for the connection */
6063 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006064 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006065 /* P2p connection has priority, disable the STA network*/
6066 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
6067 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6068 " id=%d", ssid->id);
6069 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6070 if (wpa_s == iface) {
6071 /* p2p connection is in progress, continue connecting...*/
6072 return 0;
6073 }
6074 else {
6075 /* STA connection is in progress, do not allow to continue */
6076 return -1;
6077 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006078 }
6079 }
6080 }
6081 return 0;
6082}
6083#endif