blob: 08ae9e5407cee99ba947dacb8f6b7d0f9d8e48c7 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "common/ieee802_11_common.h"
14#include "common/ieee802_11_defs.h"
15#include "common/wpa_ctrl.h"
16#include "wps/wps_i.h"
17#include "p2p/p2p.h"
18#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080019#include "ap/ap_config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070021#include "eapol_supp/eapol_supp_sm.h"
22#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "wpa_supplicant_i.h"
24#include "driver_i.h"
25#include "ap.h"
26#include "config_ssid.h"
27#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "notify.h"
29#include "scan.h"
30#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "wps_supplicant.h"
33#include "p2p_supplicant.h"
34
35
36/*
37 * How many times to try to scan to find the GO before giving up on join
38 * request.
39 */
40#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
41
Dmitry Shmidt04949592012-07-19 12:16:46 -070042#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080044#ifndef P2P_MAX_CLIENT_IDLE
45/*
46 * How many seconds to try to reconnect to the GO when connection in P2P client
47 * role has been lost.
48 */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070049#ifdef ANDROID_P2P
50#define P2P_MAX_CLIENT_IDLE 20
51#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080052#define P2P_MAX_CLIENT_IDLE 10
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070053#endif /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054#endif /* P2P_MAX_CLIENT_IDLE */
55
Dmitry Shmidt04949592012-07-19 12:16:46 -070056#ifndef P2P_MAX_INITIAL_CONN_WAIT
57/*
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
60 */
61#define P2P_MAX_INITIAL_CONN_WAIT 10
62#endif /* P2P_MAX_INITIAL_CONN_WAIT */
63
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070064#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070065#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066#endif /* P2P_CONCURRENT_SEARCH_DELAY */
67
Dmitry Shmidt34af3062013-07-11 10:46:32 -070068#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
69
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070070enum p2p_group_removal_reason {
71 P2P_GROUP_REMOVAL_UNKNOWN,
72 P2P_GROUP_REMOVAL_SILENT,
73 P2P_GROUP_REMOVAL_FORMATION_FAILED,
74 P2P_GROUP_REMOVAL_REQUESTED,
75 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
76 P2P_GROUP_REMOVAL_UNAVAILABLE,
77 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
78#ifdef ANDROID_P2P
79 P2P_GROUP_REMOVAL_FREQ_CONFLICT
80#endif
81};
82
Jouni Malinendc7b7132012-09-14 12:53:47 -070083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
85static struct wpa_supplicant *
86wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
87 int go);
88static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070089static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
91static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070092 const u8 *dev_addr, enum p2p_wps_method wps_method,
93 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
95static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
96static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
97static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080098static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
99 void *timeout_ctx);
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700100#ifdef ANDROID_P2P
101static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
102#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700103static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
104 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800105static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106
107
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700108/*
109 * Get the number of concurrent channels that the HW can operate, but that are
110 * currently not in use by any of the wpa_supplicant interfaces.
111 */
112static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
113{
114 int *freqs;
115 int num;
116
117 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
118 if (!freqs)
119 return -1;
120
121 num = get_shared_radio_freqs(wpa_s, freqs,
122 wpa_s->num_multichan_concurrent);
123 os_free(freqs);
124
125 return wpa_s->num_multichan_concurrent - num;
126}
127
128
129/*
130 * Get the frequencies that are currently in use by one or more of the virtual
131 * interfaces, and that are also valid for P2P operation.
132 */
133static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
134 int *p2p_freqs, unsigned int len)
135{
136 int *freqs;
137 unsigned int num, i, j;
138
139 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
140 if (!freqs)
141 return -1;
142
143 num = get_shared_radio_freqs(wpa_s, freqs,
144 wpa_s->num_multichan_concurrent);
145
146 os_memset(p2p_freqs, 0, sizeof(int) * len);
147
148 for (i = 0, j = 0; i < num && j < len; i++) {
149 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
150 p2p_freqs[j++] = freqs[i];
151 }
152
153 os_free(freqs);
154
155 return j;
156}
157
158
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700159static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
160 int freq)
161{
162 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
163 return;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700164 if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
165 wpas_p2p_num_unused_channels(wpa_s) > 0 &&
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700166 wpa_s->parent->conf->p2p_ignore_shared_freq)
167 freq = 0;
168 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
169}
170
171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
173 struct wpa_scan_results *scan_res)
174{
175 size_t i;
176
177 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
178 return;
179
180 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
181 (int) scan_res->num);
182
183 for (i = 0; i < scan_res->num; i++) {
184 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800185 struct os_time time_tmp_age, entry_ts;
186 time_tmp_age.sec = bss->age / 1000;
187 time_tmp_age.usec = (bss->age % 1000) * 1000;
188 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800190 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191 (const u8 *) (bss + 1),
192 bss->ie_len) > 0)
193 break;
194 }
195
196 p2p_scan_res_handled(wpa_s->global->p2p);
197}
198
199
200static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
201 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700202 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203{
204 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700205 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 struct wpa_driver_scan_params params;
207 int ret;
208 struct wpabuf *wps_ie, *ies;
209 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800210 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211
212 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
213 return -1;
214
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700215 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
216 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700217 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700218 wpas_p2p_in_progress(wpa_s) == 2) {
219 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
220 "pending station mode scan to be "
221 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700222 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700223 wpa_supplicant_req_scan(ifs, 0, 0);
224 return 1;
225 }
226 }
227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 os_memset(&params, 0, sizeof(params));
229
230 /* P2P Wildcard SSID */
231 params.num_ssids = 1;
232 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
233 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
234
235 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700236 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
237 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238 num_req_dev_types, req_dev_types);
239 if (wps_ie == NULL)
240 return -1;
241
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800242 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
243 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 if (ies == NULL) {
245 wpabuf_free(wps_ie);
246 return -1;
247 }
248 wpabuf_put_buf(ies, wps_ie);
249 wpabuf_free(wps_ie);
250
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800251 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800253 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 params.extra_ies = wpabuf_head(ies);
255 params.extra_ies_len = wpabuf_len(ies);
256
257 switch (type) {
258 case P2P_SCAN_SOCIAL:
259 params.freqs = social_channels;
260 break;
261 case P2P_SCAN_FULL:
262 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700263 case P2P_SCAN_SPECIFIC:
264 social_channels[0] = freq;
265 social_channels[1] = 0;
266 params.freqs = social_channels;
267 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268 case P2P_SCAN_SOCIAL_PLUS_ONE:
269 social_channels[3] = freq;
270 params.freqs = social_channels;
271 break;
272 }
273
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800274 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275
276 wpabuf_free(ies);
277
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800278 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700279 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
280 if (ifs->scanning ||
281 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
282 wpa_s->global->p2p_cb_on_scan_complete = 1;
283 ret = 1;
284 break;
285 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800286 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700287 } else {
288 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700289 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700290 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 return ret;
293}
294
295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
297{
298 switch (p2p_group_interface) {
299 case P2P_GROUP_INTERFACE_PENDING:
300 return WPA_IF_P2P_GROUP;
301 case P2P_GROUP_INTERFACE_GO:
302 return WPA_IF_P2P_GO;
303 case P2P_GROUP_INTERFACE_CLIENT:
304 return WPA_IF_P2P_CLIENT;
305 }
306
307 return WPA_IF_P2P_GROUP;
308}
309
310
311static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
312 const u8 *ssid,
313 size_t ssid_len, int *go)
314{
315 struct wpa_ssid *s;
316
317 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
318 for (s = wpa_s->conf->ssid; s; s = s->next) {
319 if (s->disabled != 0 || !s->p2p_group ||
320 s->ssid_len != ssid_len ||
321 os_memcmp(ssid, s->ssid, ssid_len) != 0)
322 continue;
323 if (s->mode == WPAS_MODE_P2P_GO &&
324 s != wpa_s->current_ssid)
325 continue;
326 if (go)
327 *go = s->mode == WPAS_MODE_P2P_GO;
328 return wpa_s;
329 }
330 }
331
332 return NULL;
333}
334
335
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700336static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
337 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338{
339 struct wpa_ssid *ssid;
340 char *gtype;
341 const char *reason;
342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 ssid = wpa_s->current_ssid;
344 if (ssid == NULL) {
345 /*
346 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700347 * pending P2P group interface waiting for provisioning or a
348 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 */
350 ssid = wpa_s->conf->ssid;
351 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700352 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353 break;
354 ssid = ssid->next;
355 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300356 if (ssid == NULL &&
357 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
358 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700359 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
360 "not found");
361 return -1;
362 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 }
364 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
365 gtype = "GO";
366 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
367 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
368 wpa_s->reassociate = 0;
369 wpa_s->disconnected = 1;
370 wpa_supplicant_deauthenticate(wpa_s,
371 WLAN_REASON_DEAUTH_LEAVING);
372 gtype = "client";
373 } else
374 gtype = "GO";
375 if (wpa_s->cross_connect_in_use) {
376 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700377 wpa_msg_global(wpa_s->parent, MSG_INFO,
378 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
379 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700381 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 case P2P_GROUP_REMOVAL_REQUESTED:
383 reason = " reason=REQUESTED";
384 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700385 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
386 reason = " reason=FORMATION_FAILED";
387 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
389 reason = " reason=IDLE";
390 break;
391 case P2P_GROUP_REMOVAL_UNAVAILABLE:
392 reason = " reason=UNAVAILABLE";
393 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700394 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
395 reason = " reason=GO_ENDING_SESSION";
396 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700397#ifdef ANDROID_P2P
398 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
399 reason = " reason=FREQ_CONFLICT";
400 break;
401#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402 default:
403 reason = "";
404 break;
405 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700406 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700407 wpa_msg_global(wpa_s->parent, MSG_INFO,
408 P2P_EVENT_GROUP_REMOVED "%s %s%s",
409 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700410 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700411
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700412#ifdef ANDROID_P2P
413 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
414#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700415 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
416 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800417 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700418 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800419 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
420 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700421 wpa_s->p2p_in_provisioning = 0;
422 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700423
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700424 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
426
427 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
428 struct wpa_global *global;
429 char *ifname;
430 enum wpa_driver_if_type type;
431 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
432 wpa_s->ifname);
433 global = wpa_s->global;
434 ifname = os_strdup(wpa_s->ifname);
435 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700436 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 wpa_s = global->ifaces;
438 if (wpa_s && ifname)
439 wpa_drv_if_remove(wpa_s, type, ifname);
440 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300441 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 }
443
444 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
445 if (ssid && (ssid->p2p_group ||
446 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
447 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
448 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700449 if (ssid == wpa_s->current_ssid) {
450 wpa_sm_set_config(wpa_s->wpa, NULL);
451 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700453 }
454 /*
455 * Networks objects created during any P2P activities are not
456 * exposed out as they might/will confuse certain non-P2P aware
457 * applications since these network objects won't behave like
458 * regular ones.
459 *
460 * Likewise, we don't send out network removed signals for such
461 * network objects.
462 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463 wpa_config_remove_network(wpa_s->conf, id);
464 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800465 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700466 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 } else {
468 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
469 "found");
470 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700471 if (wpa_s->ap_iface)
472 wpa_supplicant_ap_deinit(wpa_s);
473 else
474 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700475
476 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700477}
478
479
480static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
481 u8 *go_dev_addr,
482 const u8 *ssid, size_t ssid_len)
483{
484 struct wpa_bss *bss;
485 const u8 *bssid;
486 struct wpabuf *p2p;
487 u8 group_capab;
488 const u8 *addr;
489
490 if (wpa_s->go_params)
491 bssid = wpa_s->go_params->peer_interface_addr;
492 else
493 bssid = wpa_s->bssid;
494
495 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
496 if (bss == NULL) {
497 u8 iface_addr[ETH_ALEN];
498 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
499 iface_addr) == 0)
500 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
501 }
502 if (bss == NULL) {
503 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
504 "group is persistent - BSS " MACSTR " not found",
505 MAC2STR(bssid));
506 return 0;
507 }
508
509 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
510 if (p2p == NULL) {
511 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
512 "group is persistent - BSS " MACSTR
513 " did not include P2P IE", MAC2STR(bssid));
514 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
515 (u8 *) (bss + 1), bss->ie_len);
516 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
517 ((u8 *) bss + 1) + bss->ie_len,
518 bss->beacon_ie_len);
519 return 0;
520 }
521
522 group_capab = p2p_get_group_capab(p2p);
523 addr = p2p_get_go_dev_addr(p2p);
524 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
525 "group_capab=0x%x", group_capab);
526 if (addr) {
527 os_memcpy(go_dev_addr, addr, ETH_ALEN);
528 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
529 MAC2STR(addr));
530 } else
531 os_memset(go_dev_addr, 0, ETH_ALEN);
532 wpabuf_free(p2p);
533
534 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
535 "go_dev_addr=" MACSTR,
536 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
537
538 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
539}
540
541
Jouni Malinen75ecf522011-06-27 15:19:46 -0700542static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
543 struct wpa_ssid *ssid,
544 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545{
546 struct wpa_ssid *s;
547 int changed = 0;
548
549 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
550 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
551 for (s = wpa_s->conf->ssid; s; s = s->next) {
552 if (s->disabled == 2 &&
553 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
554 s->ssid_len == ssid->ssid_len &&
555 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
556 break;
557 }
558
559 if (s) {
560 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
561 "entry");
562 if (ssid->passphrase && !s->passphrase)
563 changed = 1;
564 else if (ssid->passphrase && s->passphrase &&
565 os_strcmp(ssid->passphrase, s->passphrase) != 0)
566 changed = 1;
567 } else {
568 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
569 "entry");
570 changed = 1;
571 s = wpa_config_add_network(wpa_s->conf);
572 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700573 return -1;
574
575 /*
576 * Instead of network_added we emit persistent_group_added
577 * notification. Also to keep the defense checks in
578 * persistent_group obj registration method, we set the
579 * relevant flags in s to designate it as a persistent group.
580 */
581 s->p2p_group = 1;
582 s->p2p_persistent_group = 1;
583 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 wpa_config_set_network_defaults(s);
585 }
586
587 s->p2p_group = 1;
588 s->p2p_persistent_group = 1;
589 s->disabled = 2;
590 s->bssid_set = 1;
591 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
592 s->mode = ssid->mode;
593 s->auth_alg = WPA_AUTH_ALG_OPEN;
594 s->key_mgmt = WPA_KEY_MGMT_PSK;
595 s->proto = WPA_PROTO_RSN;
596 s->pairwise_cipher = WPA_CIPHER_CCMP;
597 s->export_keys = 1;
598 if (ssid->passphrase) {
599 os_free(s->passphrase);
600 s->passphrase = os_strdup(ssid->passphrase);
601 }
602 if (ssid->psk_set) {
603 s->psk_set = 1;
604 os_memcpy(s->psk, ssid->psk, 32);
605 }
606 if (s->passphrase && !s->psk_set)
607 wpa_config_update_psk(s);
608 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
609 os_free(s->ssid);
610 s->ssid = os_malloc(ssid->ssid_len);
611 }
612 if (s->ssid) {
613 s->ssid_len = ssid->ssid_len;
614 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
615 }
616
617#ifndef CONFIG_NO_CONFIG_WRITE
618 if (changed && wpa_s->conf->update_config &&
619 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
620 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
621 }
622#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700623
624 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625}
626
627
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800628static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
629 const u8 *addr)
630{
631 struct wpa_ssid *ssid, *s;
632 u8 *n;
633 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700634 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800635
636 ssid = wpa_s->current_ssid;
637 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
638 !ssid->p2p_persistent_group)
639 return;
640
641 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
642 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
643 continue;
644
645 if (s->ssid_len == ssid->ssid_len &&
646 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
647 break;
648 }
649
650 if (s == NULL)
651 return;
652
653 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
654 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700655 ETH_ALEN) != 0)
656 continue;
657
658 if (i == s->num_p2p_clients - 1)
659 return; /* already the most recent entry */
660
661 /* move the entry to mark it most recent */
662 os_memmove(s->p2p_client_list + i * ETH_ALEN,
663 s->p2p_client_list + (i + 1) * ETH_ALEN,
664 (s->num_p2p_clients - i - 1) * ETH_ALEN);
665 os_memcpy(s->p2p_client_list +
666 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
667 found = 1;
668 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800669 }
670
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700671 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
672 n = os_realloc_array(s->p2p_client_list,
673 s->num_p2p_clients + 1, ETH_ALEN);
674 if (n == NULL)
675 return;
676 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
677 s->p2p_client_list = n;
678 s->num_p2p_clients++;
679 } else if (!found) {
680 /* Not enough room for an additional entry - drop the oldest
681 * entry */
682 os_memmove(s->p2p_client_list,
683 s->p2p_client_list + ETH_ALEN,
684 (s->num_p2p_clients - 1) * ETH_ALEN);
685 os_memcpy(s->p2p_client_list +
686 (s->num_p2p_clients - 1) * ETH_ALEN,
687 addr, ETH_ALEN);
688 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800689
690#ifndef CONFIG_NO_CONFIG_WRITE
691 if (wpa_s->parent->conf->update_config &&
692 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
693 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
694#endif /* CONFIG_NO_CONFIG_WRITE */
695}
696
697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
699 int success)
700{
701 struct wpa_ssid *ssid;
702 const char *ssid_txt;
703 int client;
704 int persistent;
705 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700706 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700707
708 /*
709 * This callback is likely called for the main interface. Update wpa_s
710 * to use the group interface if a new interface was created for the
711 * group.
712 */
713 if (wpa_s->global->p2p_group_formation)
714 wpa_s = wpa_s->global->p2p_group_formation;
715 wpa_s->global->p2p_group_formation = NULL;
716 wpa_s->p2p_in_provisioning = 0;
717
718 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700719 wpa_msg_global(wpa_s->parent, MSG_INFO,
720 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700721 wpas_p2p_group_delete(wpa_s,
722 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 return;
724 }
725
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700726 wpa_msg_global(wpa_s->parent, MSG_INFO,
727 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728
729 ssid = wpa_s->current_ssid;
730 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
731 ssid->mode = WPAS_MODE_P2P_GO;
732 p2p_group_notif_formation_done(wpa_s->p2p_group);
733 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
734 }
735
736 persistent = 0;
737 if (ssid) {
738 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
739 client = ssid->mode == WPAS_MODE_INFRA;
740 if (ssid->mode == WPAS_MODE_P2P_GO) {
741 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700742 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
743 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 } else
745 persistent = wpas_p2p_persistent_group(wpa_s,
746 go_dev_addr,
747 ssid->ssid,
748 ssid->ssid_len);
749 } else {
750 ssid_txt = "";
751 client = wpa_s->p2p_group_interface ==
752 P2P_GROUP_INTERFACE_CLIENT;
753 os_memset(go_dev_addr, 0, ETH_ALEN);
754 }
755
756 wpa_s->show_group_started = 0;
757 if (client) {
758 /*
759 * Indicate event only after successfully completed 4-way
760 * handshake, i.e., when the interface is ready for data
761 * packets.
762 */
763 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700764#ifdef ANDROID_P2P
765 /* For client Second phase of Group formation (4-way handshake) can be still pending
766 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700767 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 -0700768 wpa_s->global->p2p_group_formation = wpa_s;
769#endif
770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
772 char psk[65];
773 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700774 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
775 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
776 MACSTR "%s",
777 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
778 MAC2STR(go_dev_addr),
779 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 wpas_p2p_cross_connect_setup(wpa_s);
781 wpas_p2p_set_group_idle_timeout(wpa_s);
782 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700783 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
784 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
785 "go_dev_addr=" MACSTR "%s",
786 wpa_s->ifname, ssid_txt,
787 ssid ? ssid->frequency : 0,
788 ssid && ssid->passphrase ? ssid->passphrase : "",
789 MAC2STR(go_dev_addr),
790 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700791 wpas_p2p_cross_connect_setup(wpa_s);
792 wpas_p2p_set_group_idle_timeout(wpa_s);
793 }
794
795 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700796 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
797 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700799 network_id = ssid->id;
800 if (!client)
801 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802}
803
804
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800805static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
806 unsigned int freq,
807 const u8 *dst, const u8 *src,
808 const u8 *bssid,
809 const u8 *data, size_t data_len,
810 enum offchannel_send_action_result
811 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800813 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
816 return;
817 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
818 return;
819
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800820 switch (result) {
821 case OFFCHANNEL_SEND_ACTION_SUCCESS:
822 res = P2P_SEND_ACTION_SUCCESS;
823 break;
824 case OFFCHANNEL_SEND_ACTION_NO_ACK:
825 res = P2P_SEND_ACTION_NO_ACK;
826 break;
827 case OFFCHANNEL_SEND_ACTION_FAILED:
828 res = P2P_SEND_ACTION_FAILED;
829 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830 }
831
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800832 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800834 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
835 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800836 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800837 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
838 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800840 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
841 "during p2p_connect-auto");
842 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
843 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 }
845}
846
847
848static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
849 const u8 *src, const u8 *bssid, const u8 *buf,
850 size_t len, unsigned int wait_time)
851{
852 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800853 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
854 wait_time,
855 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700856}
857
858
859static void wpas_send_action_done(void *ctx)
860{
861 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863}
864
865
866static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
867 struct p2p_go_neg_results *params)
868{
869 if (wpa_s->go_params == NULL) {
870 wpa_s->go_params = os_malloc(sizeof(*params));
871 if (wpa_s->go_params == NULL)
872 return -1;
873 }
874 os_memcpy(wpa_s->go_params, params, sizeof(*params));
875 return 0;
876}
877
878
879static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
880 struct p2p_go_neg_results *res)
881{
882 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
883 MAC2STR(res->peer_interface_addr));
884 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
885 res->ssid, res->ssid_len);
886 wpa_supplicant_ap_deinit(wpa_s);
887 wpas_copy_go_neg_results(wpa_s, res);
888 if (res->wps_method == WPS_PBC)
889 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
890 else {
891 u16 dev_pw_id = DEV_PW_DEFAULT;
892 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
893 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
894 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
895 wpa_s->p2p_pin, 1, dev_pw_id);
896 }
897}
898
899
900static void p2p_go_configured(void *ctx, void *data)
901{
902 struct wpa_supplicant *wpa_s = ctx;
903 struct p2p_go_neg_results *params = data;
904 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700905 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906
907 ssid = wpa_s->current_ssid;
908 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
909 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
910 if (wpa_s->global->p2p_group_formation == wpa_s)
911 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800912 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700913 wpa_msg_global(wpa_s->parent, MSG_INFO,
914 P2P_EVENT_GROUP_STARTED
915 "%s GO ssid=\"%s\" freq=%d "
916 "passphrase=\"%s\" go_dev_addr=" MACSTR
917 "%s", wpa_s->ifname,
918 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
919 ssid->frequency, params->passphrase,
920 MAC2STR(wpa_s->global->p2p_dev_addr),
921 params->persistent_group ?
922 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800923 } else {
924 char psk[65];
925 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
926 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700927 wpa_msg_global(wpa_s->parent, MSG_INFO,
928 P2P_EVENT_GROUP_STARTED
929 "%s GO ssid=\"%s\" freq=%d psk=%s "
930 "go_dev_addr=" MACSTR "%s",
931 wpa_s->ifname,
932 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
933 ssid->frequency, psk,
934 MAC2STR(wpa_s->global->p2p_dev_addr),
935 params->persistent_group ?
936 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800937 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700940 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700942 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700943 if (network_id < 0)
944 network_id = ssid->id;
945 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 wpas_p2p_cross_connect_setup(wpa_s);
947 wpas_p2p_set_group_idle_timeout(wpa_s);
948 return;
949 }
950
951 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
952 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
953 params->peer_interface_addr)) {
954 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
955 "filtering");
956 return;
957 }
958 if (params->wps_method == WPS_PBC)
959 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800960 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 else if (wpa_s->p2p_pin[0])
962 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800963 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964 os_free(wpa_s->go_params);
965 wpa_s->go_params = NULL;
966}
967
968
969static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
970 struct p2p_go_neg_results *params,
971 int group_formation)
972{
973 struct wpa_ssid *ssid;
974
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700975 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
976 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
977 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
978 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700980 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981
982 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700983 if (ssid == NULL) {
984 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700986 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800988 wpa_s->show_group_started = 0;
989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 wpa_config_set_network_defaults(ssid);
991 ssid->temporary = 1;
992 ssid->p2p_group = 1;
993 ssid->p2p_persistent_group = params->persistent_group;
994 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
995 WPAS_MODE_P2P_GO;
996 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700997 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 ssid->ssid = os_zalloc(params->ssid_len + 1);
999 if (ssid->ssid) {
1000 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1001 ssid->ssid_len = params->ssid_len;
1002 }
1003 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1004 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1005 ssid->proto = WPA_PROTO_RSN;
1006 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001007 if (os_strlen(params->passphrase) > 0) {
1008 ssid->passphrase = os_strdup(params->passphrase);
1009 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001010 wpa_msg_global(wpa_s, MSG_ERROR,
1011 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001012 wpa_config_remove_network(wpa_s->conf, ssid->id);
1013 return;
1014 }
1015 } else
1016 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001017 ssid->psk_set = params->psk_set;
1018 if (ssid->psk_set)
1019 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001020 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001021 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001022 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023
1024 wpa_s->ap_configured_cb = p2p_go_configured;
1025 wpa_s->ap_configured_cb_ctx = wpa_s;
1026 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001027 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 wpa_s->reassociate = 1;
1029 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001030 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1031 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 wpa_supplicant_req_scan(wpa_s, 0, 0);
1033}
1034
1035
1036static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1037 const struct wpa_supplicant *src)
1038{
1039 struct wpa_config *d;
1040 const struct wpa_config *s;
1041
1042 d = dst->conf;
1043 s = src->conf;
1044
1045#define C(n) if (s->n) d->n = os_strdup(s->n)
1046 C(device_name);
1047 C(manufacturer);
1048 C(model_name);
1049 C(model_number);
1050 C(serial_number);
1051 C(config_methods);
1052#undef C
1053
1054 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1055 os_memcpy(d->sec_device_type, s->sec_device_type,
1056 sizeof(d->sec_device_type));
1057 d->num_sec_device_types = s->num_sec_device_types;
1058
1059 d->p2p_group_idle = s->p2p_group_idle;
1060 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001061 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001062 d->max_num_sta = s->max_num_sta;
1063 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001064 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001065 d->beacon_int = s->beacon_int;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066}
1067
1068
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001069static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1070 char *ifname, size_t len)
1071{
1072 char *ifname_ptr = wpa_s->ifname;
1073
1074 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1075 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1076 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1077 }
1078
1079 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1080 if (os_strlen(ifname) >= IFNAMSIZ &&
1081 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1082 /* Try to avoid going over the IFNAMSIZ length limit */
1083 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1084 wpa_s->p2p_group_idx);
1085 }
1086}
1087
1088
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1090 enum wpa_driver_if_type type)
1091{
1092 char ifname[120], force_ifname[120];
1093
1094 if (wpa_s->pending_interface_name[0]) {
1095 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1096 "- skip creation of a new one");
1097 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1098 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1099 "unknown?! ifname='%s'",
1100 wpa_s->pending_interface_name);
1101 return -1;
1102 }
1103 return 0;
1104 }
1105
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001106 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 force_ifname[0] = '\0';
1108
1109 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1110 ifname);
1111 wpa_s->p2p_group_idx++;
1112
1113 wpa_s->pending_interface_type = type;
1114 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1115 wpa_s->pending_interface_addr, NULL) < 0) {
1116 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1117 "interface");
1118 return -1;
1119 }
1120
1121 if (force_ifname[0]) {
1122 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1123 force_ifname);
1124 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1125 sizeof(wpa_s->pending_interface_name));
1126 } else
1127 os_strlcpy(wpa_s->pending_interface_name, ifname,
1128 sizeof(wpa_s->pending_interface_name));
1129 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1130 MACSTR, wpa_s->pending_interface_name,
1131 MAC2STR(wpa_s->pending_interface_addr));
1132
1133 return 0;
1134}
1135
1136
1137static void wpas_p2p_remove_pending_group_interface(
1138 struct wpa_supplicant *wpa_s)
1139{
1140 if (!wpa_s->pending_interface_name[0] ||
1141 is_zero_ether_addr(wpa_s->pending_interface_addr))
1142 return; /* No pending virtual interface */
1143
1144 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1145 wpa_s->pending_interface_name);
1146 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1147 wpa_s->pending_interface_name);
1148 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1149 wpa_s->pending_interface_name[0] = '\0';
1150}
1151
1152
1153static struct wpa_supplicant *
1154wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1155{
1156 struct wpa_interface iface;
1157 struct wpa_supplicant *group_wpa_s;
1158
1159 if (!wpa_s->pending_interface_name[0]) {
1160 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1161 if (!wpas_p2p_create_iface(wpa_s))
1162 return NULL;
1163 /*
1164 * Something has forced us to remove the pending interface; try
1165 * to create a new one and hope for the best that we will get
1166 * the same local address.
1167 */
1168 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1169 WPA_IF_P2P_CLIENT) < 0)
1170 return NULL;
1171 }
1172
1173 os_memset(&iface, 0, sizeof(iface));
1174 iface.ifname = wpa_s->pending_interface_name;
1175 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001176 if (wpa_s->conf->ctrl_interface == NULL &&
1177 wpa_s->parent != wpa_s &&
1178 wpa_s->p2p_mgmt &&
1179 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1180 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1181 else
1182 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183 iface.driver_param = wpa_s->conf->driver_param;
1184 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1185 if (group_wpa_s == NULL) {
1186 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1187 "wpa_supplicant interface");
1188 return NULL;
1189 }
1190 wpa_s->pending_interface_name[0] = '\0';
1191 group_wpa_s->parent = wpa_s;
1192 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1193 P2P_GROUP_INTERFACE_CLIENT;
1194 wpa_s->global->p2p_group_formation = group_wpa_s;
1195
1196 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1197
1198 return group_wpa_s;
1199}
1200
1201
1202static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1203 void *timeout_ctx)
1204{
1205 struct wpa_supplicant *wpa_s = eloop_ctx;
1206 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001207 wpas_p2p_group_formation_failed(wpa_s);
1208}
1209
1210
1211void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1212{
1213 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1214 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215 if (wpa_s->global->p2p)
1216 p2p_group_formation_failed(wpa_s->global->p2p);
1217 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1218 wpa_drv_p2p_group_formation_failed(wpa_s);
1219 wpas_group_formation_completed(wpa_s, 0);
1220}
1221
1222
1223void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1224{
1225 struct wpa_supplicant *wpa_s = ctx;
1226
1227 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1228 wpa_drv_cancel_remain_on_channel(wpa_s);
1229 wpa_s->off_channel_freq = 0;
1230 wpa_s->roc_waiting_drv_freq = 0;
1231 }
1232
1233 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001234 wpa_msg_global(wpa_s, MSG_INFO,
1235 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1236 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001237 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 wpas_p2p_remove_pending_group_interface(wpa_s);
1239 return;
1240 }
1241
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001242 if (wpa_s->p2p_go_ht40)
1243 res->ht40 = 1;
1244
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001245 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001246 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247
Dmitry Shmidt04949592012-07-19 12:16:46 -07001248 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1249 struct wpa_ssid *ssid;
1250 ssid = wpa_config_get_network(wpa_s->conf,
1251 wpa_s->p2p_persistent_id);
1252 if (ssid && ssid->disabled == 2 &&
1253 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1254 size_t len = os_strlen(ssid->passphrase);
1255 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1256 "on requested persistent group");
1257 os_memcpy(res->passphrase, ssid->passphrase, len);
1258 res->passphrase[len] = '\0';
1259 }
1260 }
1261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262 if (wpa_s->create_p2p_iface) {
1263 struct wpa_supplicant *group_wpa_s =
1264 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1265 if (group_wpa_s == NULL) {
1266 wpas_p2p_remove_pending_group_interface(wpa_s);
1267 return;
1268 }
1269 if (group_wpa_s != wpa_s) {
1270 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1271 sizeof(group_wpa_s->p2p_pin));
1272 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1273 }
1274 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1275 wpa_s->pending_interface_name[0] = '\0';
1276 group_wpa_s->p2p_in_provisioning = 1;
1277
1278 if (res->role_go)
1279 wpas_start_wps_go(group_wpa_s, res, 1);
1280 else
1281 wpas_start_wps_enrollee(group_wpa_s, res);
1282 } else {
1283 wpa_s->p2p_in_provisioning = 1;
1284 wpa_s->global->p2p_group_formation = wpa_s;
1285
1286 if (res->role_go)
1287 wpas_start_wps_go(wpa_s, res, 1);
1288 else
1289 wpas_start_wps_enrollee(ctx, res);
1290 }
1291
1292 wpa_s->p2p_long_listen = 0;
1293 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1294
1295 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1296 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1297 (res->peer_config_timeout % 100) * 10000,
1298 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1299}
1300
1301
1302void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1303{
1304 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001305 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1306 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307
1308 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1309}
1310
1311
1312void wpas_dev_found(void *ctx, const u8 *addr,
1313 const struct p2p_peer_info *info,
1314 int new_device)
1315{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001316#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 struct wpa_supplicant *wpa_s = ctx;
1318 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001319#define WFD_DEV_INFO_SIZE 9
1320 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001321 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001322#ifdef CONFIG_WIFI_DISPLAY
1323 if (info->wfd_subelems) {
1324 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1325 wpabuf_head(info->wfd_subelems),
1326 WFD_DEV_INFO_SIZE);
1327 }
1328#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001329 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1330 " p2p_dev_addr=" MACSTR
1331 " pri_dev_type=%s name='%s' config_methods=0x%x "
1332 "dev_capab=0x%x group_capab=0x%x%s%s",
1333 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1334 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1335 sizeof(devtype)),
1336 info->device_name, info->config_methods,
1337 info->dev_capab, info->group_capab,
1338 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001339#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340
1341 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1342}
1343
1344
1345static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1346{
1347 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001348
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001349 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1350 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1353}
1354
1355
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001356static void wpas_find_stopped(void *ctx)
1357{
1358 struct wpa_supplicant *wpa_s = ctx;
1359 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1360}
1361
1362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363static int wpas_start_listen(void *ctx, unsigned int freq,
1364 unsigned int duration,
1365 const struct wpabuf *probe_resp_ie)
1366{
1367 struct wpa_supplicant *wpa_s = ctx;
1368
1369 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1370
1371 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1372 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1373 "report received Probe Request frames");
1374 return -1;
1375 }
1376
1377 wpa_s->pending_listen_freq = freq;
1378 wpa_s->pending_listen_duration = duration;
1379
1380 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1381 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1382 "to remain on channel (%u MHz) for Listen "
1383 "state", freq);
1384 wpa_s->pending_listen_freq = 0;
1385 return -1;
1386 }
1387 wpa_s->off_channel_freq = 0;
1388 wpa_s->roc_waiting_drv_freq = freq;
1389
1390 return 0;
1391}
1392
1393
1394static void wpas_stop_listen(void *ctx)
1395{
1396 struct wpa_supplicant *wpa_s = ctx;
1397 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1398 wpa_drv_cancel_remain_on_channel(wpa_s);
1399 wpa_s->off_channel_freq = 0;
1400 wpa_s->roc_waiting_drv_freq = 0;
1401 }
1402 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1403 wpa_drv_probe_req_report(wpa_s, 0);
1404}
1405
1406
1407static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1408{
1409 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001410 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411}
1412
1413
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001414/*
1415 * DNS Header section is used only to calculate compression pointers, so the
1416 * contents of this data does not matter, but the length needs to be reserved
1417 * in the virtual packet.
1418 */
1419#define DNS_HEADER_LEN 12
1420
1421/*
1422 * 27-octet in-memory packet from P2P specification containing two implied
1423 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1424 */
1425#define P2P_SD_IN_MEMORY_LEN 27
1426
1427static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1428 u8 **spos, const u8 *end)
1429{
1430 while (*spos < end) {
1431 u8 val = ((*spos)[0] & 0xc0) >> 6;
1432 int len;
1433
1434 if (val == 1 || val == 2) {
1435 /* These are reserved values in RFC 1035 */
1436 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1437 "sequence starting with 0x%x", val);
1438 return -1;
1439 }
1440
1441 if (val == 3) {
1442 u16 offset;
1443 u8 *spos_tmp;
1444
1445 /* Offset */
1446 if (*spos + 2 > end) {
1447 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1448 "DNS offset field");
1449 return -1;
1450 }
1451
1452 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1453 if (offset >= *spos - start) {
1454 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1455 "pointer offset %u", offset);
1456 return -1;
1457 }
1458
1459 (*spos) += 2;
1460 spos_tmp = start + offset;
1461 return p2p_sd_dns_uncompress_label(upos, uend, start,
1462 &spos_tmp,
1463 *spos - 2);
1464 }
1465
1466 /* Label */
1467 len = (*spos)[0] & 0x3f;
1468 if (len == 0)
1469 return 0;
1470
1471 (*spos)++;
1472 if (*spos + len > end) {
1473 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1474 "sequence - no room for label with length "
1475 "%u", len);
1476 return -1;
1477 }
1478
1479 if (*upos + len + 2 > uend)
1480 return -2;
1481
1482 os_memcpy(*upos, *spos, len);
1483 *spos += len;
1484 *upos += len;
1485 (*upos)[0] = '.';
1486 (*upos)++;
1487 (*upos)[0] = '\0';
1488 }
1489
1490 return 0;
1491}
1492
1493
1494/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1495 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1496 * not large enough */
1497static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1498 size_t msg_len, size_t offset)
1499{
1500 /* 27-octet in-memory packet from P2P specification */
1501 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1502 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1503 u8 *tmp, *end, *spos;
1504 char *upos, *uend;
1505 int ret = 0;
1506
1507 if (buf_len < 2)
1508 return -1;
1509 if (offset > msg_len)
1510 return -1;
1511
1512 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1513 if (tmp == NULL)
1514 return -1;
1515 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1516 end = spos + msg_len;
1517 spos += offset;
1518
1519 os_memset(tmp, 0, DNS_HEADER_LEN);
1520 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1521 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1522
1523 upos = buf;
1524 uend = buf + buf_len;
1525
1526 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1527 if (ret) {
1528 os_free(tmp);
1529 return ret;
1530 }
1531
1532 if (upos == buf) {
1533 upos[0] = '.';
1534 upos[1] = '\0';
1535 } else if (upos[-1] == '.')
1536 upos[-1] = '\0';
1537
1538 os_free(tmp);
1539 return 0;
1540}
1541
1542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001543static struct p2p_srv_bonjour *
1544wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1545 const struct wpabuf *query)
1546{
1547 struct p2p_srv_bonjour *bsrv;
1548 size_t len;
1549
1550 len = wpabuf_len(query);
1551 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1552 struct p2p_srv_bonjour, list) {
1553 if (len == wpabuf_len(bsrv->query) &&
1554 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1555 len) == 0)
1556 return bsrv;
1557 }
1558 return NULL;
1559}
1560
1561
1562static struct p2p_srv_upnp *
1563wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1564 const char *service)
1565{
1566 struct p2p_srv_upnp *usrv;
1567
1568 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1569 struct p2p_srv_upnp, list) {
1570 if (version == usrv->version &&
1571 os_strcmp(service, usrv->service) == 0)
1572 return usrv;
1573 }
1574 return NULL;
1575}
1576
1577
1578static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1579 u8 srv_trans_id)
1580{
1581 u8 *len_pos;
1582
1583 if (wpabuf_tailroom(resp) < 5)
1584 return;
1585
1586 /* Length (to be filled) */
1587 len_pos = wpabuf_put(resp, 2);
1588 wpabuf_put_u8(resp, srv_proto);
1589 wpabuf_put_u8(resp, srv_trans_id);
1590 /* Status Code */
1591 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1592 /* Response Data: empty */
1593 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1594}
1595
1596
1597static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1598 struct wpabuf *resp, u8 srv_trans_id)
1599{
1600 struct p2p_srv_bonjour *bsrv;
1601 u8 *len_pos;
1602
1603 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1604
1605 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1606 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1607 return;
1608 }
1609
1610 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1611 struct p2p_srv_bonjour, list) {
1612 if (wpabuf_tailroom(resp) <
1613 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1614 return;
1615 /* Length (to be filled) */
1616 len_pos = wpabuf_put(resp, 2);
1617 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1618 wpabuf_put_u8(resp, srv_trans_id);
1619 /* Status Code */
1620 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1621 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1622 wpabuf_head(bsrv->resp),
1623 wpabuf_len(bsrv->resp));
1624 /* Response Data */
1625 wpabuf_put_buf(resp, bsrv->query); /* Key */
1626 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1627 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1628 2);
1629 }
1630}
1631
1632
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001633static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1634 size_t query_len)
1635{
1636 char str_rx[256], str_srv[256];
1637
1638 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1639 return 0; /* Too short to include DNS Type and Version */
1640 if (os_memcmp(query + query_len - 3,
1641 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1642 3) != 0)
1643 return 0; /* Mismatch in DNS Type or Version */
1644 if (query_len == wpabuf_len(bsrv->query) &&
1645 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1646 return 1; /* Binary match */
1647
1648 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1649 0))
1650 return 0; /* Failed to uncompress query */
1651 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1652 wpabuf_head(bsrv->query),
1653 wpabuf_len(bsrv->query) - 3, 0))
1654 return 0; /* Failed to uncompress service */
1655
1656 return os_strcmp(str_rx, str_srv) == 0;
1657}
1658
1659
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1661 struct wpabuf *resp, u8 srv_trans_id,
1662 const u8 *query, size_t query_len)
1663{
1664 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001666 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667
1668 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1669 query, query_len);
1670 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1671 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1672 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1673 srv_trans_id);
1674 return;
1675 }
1676
1677 if (query_len == 0) {
1678 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1679 return;
1680 }
1681
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001682 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1683 struct p2p_srv_bonjour, list) {
1684 if (!match_bonjour_query(bsrv, query, query_len))
1685 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001687 if (wpabuf_tailroom(resp) <
1688 5 + query_len + wpabuf_len(bsrv->resp))
1689 return;
1690
1691 matches++;
1692
1693 /* Length (to be filled) */
1694 len_pos = wpabuf_put(resp, 2);
1695 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1696 wpabuf_put_u8(resp, srv_trans_id);
1697
1698 /* Status Code */
1699 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1700 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1701 wpabuf_head(bsrv->resp),
1702 wpabuf_len(bsrv->resp));
1703
1704 /* Response Data */
1705 wpabuf_put_data(resp, query, query_len); /* Key */
1706 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1707
1708 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1709 }
1710
1711 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1713 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001714 if (wpabuf_tailroom(resp) < 5)
1715 return;
1716
1717 /* Length (to be filled) */
1718 len_pos = wpabuf_put(resp, 2);
1719 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1720 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721
1722 /* Status Code */
1723 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1724 /* Response Data: empty */
1725 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1726 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728}
1729
1730
1731static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1732 struct wpabuf *resp, u8 srv_trans_id)
1733{
1734 struct p2p_srv_upnp *usrv;
1735 u8 *len_pos;
1736
1737 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1738
1739 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1740 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1741 return;
1742 }
1743
1744 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1745 struct p2p_srv_upnp, list) {
1746 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1747 return;
1748
1749 /* Length (to be filled) */
1750 len_pos = wpabuf_put(resp, 2);
1751 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1752 wpabuf_put_u8(resp, srv_trans_id);
1753
1754 /* Status Code */
1755 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1756 /* Response Data */
1757 wpabuf_put_u8(resp, usrv->version);
1758 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1759 usrv->service);
1760 wpabuf_put_str(resp, usrv->service);
1761 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1762 2);
1763 }
1764}
1765
1766
1767static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1768 struct wpabuf *resp, u8 srv_trans_id,
1769 const u8 *query, size_t query_len)
1770{
1771 struct p2p_srv_upnp *usrv;
1772 u8 *len_pos;
1773 u8 version;
1774 char *str;
1775 int count = 0;
1776
1777 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1778 query, query_len);
1779
1780 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1781 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1782 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1783 srv_trans_id);
1784 return;
1785 }
1786
1787 if (query_len == 0) {
1788 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1789 return;
1790 }
1791
1792 if (wpabuf_tailroom(resp) < 5)
1793 return;
1794
1795 /* Length (to be filled) */
1796 len_pos = wpabuf_put(resp, 2);
1797 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1798 wpabuf_put_u8(resp, srv_trans_id);
1799
1800 version = query[0];
1801 str = os_malloc(query_len);
1802 if (str == NULL)
1803 return;
1804 os_memcpy(str, query + 1, query_len - 1);
1805 str[query_len - 1] = '\0';
1806
1807 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1808 struct p2p_srv_upnp, list) {
1809 if (version != usrv->version)
1810 continue;
1811
1812 if (os_strcmp(str, "ssdp:all") != 0 &&
1813 os_strstr(usrv->service, str) == NULL)
1814 continue;
1815
1816 if (wpabuf_tailroom(resp) < 2)
1817 break;
1818 if (count == 0) {
1819 /* Status Code */
1820 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1821 /* Response Data */
1822 wpabuf_put_u8(resp, version);
1823 } else
1824 wpabuf_put_u8(resp, ',');
1825
1826 count++;
1827
1828 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1829 usrv->service);
1830 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1831 break;
1832 wpabuf_put_str(resp, usrv->service);
1833 }
1834 os_free(str);
1835
1836 if (count == 0) {
1837 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1838 "available");
1839 /* Status Code */
1840 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1841 /* Response Data: empty */
1842 }
1843
1844 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1845}
1846
1847
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001848#ifdef CONFIG_WIFI_DISPLAY
1849static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1850 struct wpabuf *resp, u8 srv_trans_id,
1851 const u8 *query, size_t query_len)
1852{
1853 const u8 *pos;
1854 u8 role;
1855 u8 *len_pos;
1856
1857 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1858
1859 if (!wpa_s->global->wifi_display) {
1860 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1861 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1862 srv_trans_id);
1863 return;
1864 }
1865
1866 if (query_len < 1) {
1867 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1868 "Role");
1869 return;
1870 }
1871
1872 if (wpabuf_tailroom(resp) < 5)
1873 return;
1874
1875 pos = query;
1876 role = *pos++;
1877 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1878
1879 /* TODO: role specific handling */
1880
1881 /* Length (to be filled) */
1882 len_pos = wpabuf_put(resp, 2);
1883 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1884 wpabuf_put_u8(resp, srv_trans_id);
1885 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1886
1887 while (pos < query + query_len) {
1888 if (*pos < MAX_WFD_SUBELEMS &&
1889 wpa_s->global->wfd_subelem[*pos] &&
1890 wpabuf_tailroom(resp) >=
1891 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1892 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1893 "subelement %u", *pos);
1894 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1895 }
1896 pos++;
1897 }
1898
1899 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1900}
1901#endif /* CONFIG_WIFI_DISPLAY */
1902
1903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1905 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1906{
1907 struct wpa_supplicant *wpa_s = ctx;
1908 const u8 *pos = tlvs;
1909 const u8 *end = tlvs + tlvs_len;
1910 const u8 *tlv_end;
1911 u16 slen;
1912 struct wpabuf *resp;
1913 u8 srv_proto, srv_trans_id;
1914 size_t buf_len;
1915 char *buf;
1916
1917 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1918 tlvs, tlvs_len);
1919 buf_len = 2 * tlvs_len + 1;
1920 buf = os_malloc(buf_len);
1921 if (buf) {
1922 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1923 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1924 MACSTR " %u %u %s",
1925 freq, MAC2STR(sa), dialog_token, update_indic,
1926 buf);
1927 os_free(buf);
1928 }
1929
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001930 if (wpa_s->p2p_sd_over_ctrl_iface) {
1931 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1932 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001933 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001934 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935
1936 resp = wpabuf_alloc(10000);
1937 if (resp == NULL)
1938 return;
1939
1940 while (pos + 1 < end) {
1941 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1942 slen = WPA_GET_LE16(pos);
1943 pos += 2;
1944 if (pos + slen > end || slen < 2) {
1945 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1946 "length");
1947 wpabuf_free(resp);
1948 return;
1949 }
1950 tlv_end = pos + slen;
1951
1952 srv_proto = *pos++;
1953 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1954 srv_proto);
1955 srv_trans_id = *pos++;
1956 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1957 srv_trans_id);
1958
1959 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1960 pos, tlv_end - pos);
1961
1962
1963 if (wpa_s->force_long_sd) {
1964 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1965 "response");
1966 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1967 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1968 goto done;
1969 }
1970
1971 switch (srv_proto) {
1972 case P2P_SERV_ALL_SERVICES:
1973 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1974 "for all services");
1975 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1976 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1977 wpa_printf(MSG_DEBUG, "P2P: No service "
1978 "discovery protocols available");
1979 wpas_sd_add_proto_not_avail(
1980 resp, P2P_SERV_ALL_SERVICES,
1981 srv_trans_id);
1982 break;
1983 }
1984 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1985 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1986 break;
1987 case P2P_SERV_BONJOUR:
1988 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1989 pos, tlv_end - pos);
1990 break;
1991 case P2P_SERV_UPNP:
1992 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1993 pos, tlv_end - pos);
1994 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001995#ifdef CONFIG_WIFI_DISPLAY
1996 case P2P_SERV_WIFI_DISPLAY:
1997 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1998 pos, tlv_end - pos);
1999 break;
2000#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001 default:
2002 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2003 "protocol %u", srv_proto);
2004 wpas_sd_add_proto_not_avail(resp, srv_proto,
2005 srv_trans_id);
2006 break;
2007 }
2008
2009 pos = tlv_end;
2010 }
2011
2012done:
2013 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2014 update_indic, tlvs, tlvs_len);
2015
2016 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2017
2018 wpabuf_free(resp);
2019}
2020
2021
2022void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2023 const u8 *tlvs, size_t tlvs_len)
2024{
2025 struct wpa_supplicant *wpa_s = ctx;
2026 const u8 *pos = tlvs;
2027 const u8 *end = tlvs + tlvs_len;
2028 const u8 *tlv_end;
2029 u16 slen;
2030 size_t buf_len;
2031 char *buf;
2032
2033 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2034 tlvs, tlvs_len);
2035 if (tlvs_len > 1500) {
2036 /* TODO: better way for handling this */
2037 wpa_msg_ctrl(wpa_s, MSG_INFO,
2038 P2P_EVENT_SERV_DISC_RESP MACSTR
2039 " %u <long response: %u bytes>",
2040 MAC2STR(sa), update_indic,
2041 (unsigned int) tlvs_len);
2042 } else {
2043 buf_len = 2 * tlvs_len + 1;
2044 buf = os_malloc(buf_len);
2045 if (buf) {
2046 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2047 wpa_msg_ctrl(wpa_s, MSG_INFO,
2048 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2049 MAC2STR(sa), update_indic, buf);
2050 os_free(buf);
2051 }
2052 }
2053
2054 while (pos < end) {
2055 u8 srv_proto, srv_trans_id, status;
2056
2057 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2058 slen = WPA_GET_LE16(pos);
2059 pos += 2;
2060 if (pos + slen > end || slen < 3) {
2061 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2062 "length");
2063 return;
2064 }
2065 tlv_end = pos + slen;
2066
2067 srv_proto = *pos++;
2068 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2069 srv_proto);
2070 srv_trans_id = *pos++;
2071 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2072 srv_trans_id);
2073 status = *pos++;
2074 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2075 status);
2076
2077 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2078 pos, tlv_end - pos);
2079
2080 pos = tlv_end;
2081 }
2082
2083 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2084}
2085
2086
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002087u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2088 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002089{
2090 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002091 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002092 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002093 return 0;
2094 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095}
2096
2097
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002098u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2099 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100{
2101 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103
2104 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2105 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002106 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2108 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2109 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2110 wpabuf_put_u8(tlvs, version);
2111 wpabuf_put_str(tlvs, query);
2112 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2113 wpabuf_free(tlvs);
2114 return ret;
2115}
2116
2117
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002118#ifdef CONFIG_WIFI_DISPLAY
2119
2120static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2121 const struct wpabuf *tlvs)
2122{
2123 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2124 return 0;
2125 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2126 return 0;
2127 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2128}
2129
2130
2131#define MAX_WFD_SD_SUBELEMS 20
2132
2133static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2134 const char *subelems)
2135{
2136 u8 *len;
2137 const char *pos;
2138 int val;
2139 int count = 0;
2140
2141 len = wpabuf_put(tlvs, 2);
2142 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2143 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2144
2145 wpabuf_put_u8(tlvs, role);
2146
2147 pos = subelems;
2148 while (*pos) {
2149 val = atoi(pos);
2150 if (val >= 0 && val < 256) {
2151 wpabuf_put_u8(tlvs, val);
2152 count++;
2153 if (count == MAX_WFD_SD_SUBELEMS)
2154 break;
2155 }
2156 pos = os_strchr(pos + 1, ',');
2157 if (pos == NULL)
2158 break;
2159 pos++;
2160 }
2161
2162 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2163}
2164
2165
2166u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2167 const u8 *dst, const char *role)
2168{
2169 struct wpabuf *tlvs;
2170 u64 ret;
2171 const char *subelems;
2172 u8 id = 1;
2173
2174 subelems = os_strchr(role, ' ');
2175 if (subelems == NULL)
2176 return 0;
2177 subelems++;
2178
2179 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2180 if (tlvs == NULL)
2181 return 0;
2182
2183 if (os_strstr(role, "[source]"))
2184 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2185 if (os_strstr(role, "[pri-sink]"))
2186 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2187 if (os_strstr(role, "[sec-sink]"))
2188 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2189 if (os_strstr(role, "[source+sink]"))
2190 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2191
2192 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2193 wpabuf_free(tlvs);
2194 return ret;
2195}
2196
2197#endif /* CONFIG_WIFI_DISPLAY */
2198
2199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002200int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201{
2202 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002203 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2205 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002206 return p2p_sd_cancel_request(wpa_s->global->p2p,
2207 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208}
2209
2210
2211void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2212 const u8 *dst, u8 dialog_token,
2213 const struct wpabuf *resp_tlvs)
2214{
2215 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2216 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2217 resp_tlvs);
2218 return;
2219 }
2220 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2221 return;
2222 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2223 resp_tlvs);
2224}
2225
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002226#ifdef ANDROID_P2P
2227void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2228#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002230#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231{
2232 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2233 wpa_drv_p2p_service_update(wpa_s);
2234 return;
2235 }
2236 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002237#ifdef ANDROID_P2P
2238 p2p_sd_service_update(wpa_s->global->p2p, action);
2239#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002240 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002241#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242}
2243
2244
2245static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2246{
2247 dl_list_del(&bsrv->list);
2248 wpabuf_free(bsrv->query);
2249 wpabuf_free(bsrv->resp);
2250 os_free(bsrv);
2251}
2252
2253
2254static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2255{
2256 dl_list_del(&usrv->list);
2257 os_free(usrv->service);
2258 os_free(usrv);
2259}
2260
2261
2262void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2263{
2264 struct p2p_srv_bonjour *bsrv, *bn;
2265 struct p2p_srv_upnp *usrv, *un;
2266
2267 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2268 struct p2p_srv_bonjour, list)
2269 wpas_p2p_srv_bonjour_free(bsrv);
2270
2271 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2272 struct p2p_srv_upnp, list)
2273 wpas_p2p_srv_upnp_free(usrv);
2274
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002275#ifdef ANDROID_P2P
2276 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2277#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002279#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002280}
2281
2282
2283int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2284 struct wpabuf *query, struct wpabuf *resp)
2285{
2286 struct p2p_srv_bonjour *bsrv;
2287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 bsrv = os_zalloc(sizeof(*bsrv));
2289 if (bsrv == NULL)
2290 return -1;
2291 bsrv->query = query;
2292 bsrv->resp = resp;
2293 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2294
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002295#ifdef ANDROID_P2P
2296 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2297#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002299#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300 return 0;
2301}
2302
2303
2304int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2305 const struct wpabuf *query)
2306{
2307 struct p2p_srv_bonjour *bsrv;
2308
2309 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2310 if (bsrv == NULL)
2311 return -1;
2312 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002313#ifdef ANDROID_P2P
2314 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2315#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002317#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 return 0;
2319}
2320
2321
2322int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2323 const char *service)
2324{
2325 struct p2p_srv_upnp *usrv;
2326
2327 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2328 return 0; /* Already listed */
2329 usrv = os_zalloc(sizeof(*usrv));
2330 if (usrv == NULL)
2331 return -1;
2332 usrv->version = version;
2333 usrv->service = os_strdup(service);
2334 if (usrv->service == NULL) {
2335 os_free(usrv);
2336 return -1;
2337 }
2338 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2339
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002340#ifdef ANDROID_P2P
2341 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2342#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002344#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345 return 0;
2346}
2347
2348
2349int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2350 const char *service)
2351{
2352 struct p2p_srv_upnp *usrv;
2353
2354 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2355 if (usrv == NULL)
2356 return -1;
2357 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002358#ifdef ANDROID_P2P
2359 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2360#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002362#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 return 0;
2364}
2365
2366
2367static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2368 const u8 *peer, const char *params,
2369 unsigned int generated_pin)
2370{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002371 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2372 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373}
2374
2375
2376static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2377 const u8 *peer, const char *params)
2378{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002379 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2380 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381}
2382
2383
2384void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2385 const u8 *dev_addr, const u8 *pri_dev_type,
2386 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002387 u8 dev_capab, u8 group_capab, const u8 *group_id,
2388 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389{
2390 struct wpa_supplicant *wpa_s = ctx;
2391 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002392 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 u8 empty_dev_type[8];
2394 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002395 struct wpa_supplicant *group = NULL;
2396
2397 if (group_id) {
2398 for (group = wpa_s->global->ifaces; group; group = group->next)
2399 {
2400 struct wpa_ssid *s = group->current_ssid;
2401 if (s != NULL &&
2402 s->mode == WPAS_MODE_P2P_GO &&
2403 group_id_len - ETH_ALEN == s->ssid_len &&
2404 os_memcmp(group_id + ETH_ALEN, s->ssid,
2405 s->ssid_len) == 0)
2406 break;
2407 }
2408 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409
2410 if (pri_dev_type == NULL) {
2411 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2412 pri_dev_type = empty_dev_type;
2413 }
2414 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2415 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002416 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 MAC2STR(dev_addr),
2418 wps_dev_type_bin2str(pri_dev_type, devtype,
2419 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002420 dev_name, supp_config_methods, dev_capab, group_capab,
2421 group ? " group=" : "",
2422 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425 if (config_methods & WPS_CONFIG_DISPLAY) {
2426 generated_pin = wps_generate_pin();
2427 wpas_prov_disc_local_display(wpa_s, peer, params,
2428 generated_pin);
2429 } else if (config_methods & WPS_CONFIG_KEYPAD)
2430 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2431 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002432 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2433 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002434
2435 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2436 P2P_PROV_DISC_SUCCESS,
2437 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438}
2439
2440
2441void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2442{
2443 struct wpa_supplicant *wpa_s = ctx;
2444 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002445 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002446
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002447 if (wpa_s->pending_pd_before_join &&
2448 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2449 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2450 wpa_s->pending_pd_before_join = 0;
2451 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2452 "join-existing-group operation");
2453 wpas_p2p_join_start(wpa_s);
2454 return;
2455 }
2456
Dmitry Shmidt04949592012-07-19 12:16:46 -07002457 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2458 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2459 os_snprintf(params, sizeof(params), " peer_go=%d",
2460 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2461 else
2462 params[0] = '\0';
2463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002465 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466 else if (config_methods & WPS_CONFIG_KEYPAD) {
2467 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002468 wpas_prov_disc_local_display(wpa_s, peer, params,
2469 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002471 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2472 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473
Jouni Malinen75ecf522011-06-27 15:19:46 -07002474 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2475 P2P_PROV_DISC_SUCCESS,
2476 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477}
2478
2479
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002480static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2481 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002482{
2483 struct wpa_supplicant *wpa_s = ctx;
2484
Dmitry Shmidt04949592012-07-19 12:16:46 -07002485 if (wpa_s->p2p_fallback_to_go_neg) {
2486 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2487 "failed - fall back to GO Negotiation");
2488 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2489 return;
2490 }
2491
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002492 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002493 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002494 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2495 "join-existing-group operation (no ACK for PD "
2496 "Req attempts)");
2497 wpas_p2p_join_start(wpa_s);
2498 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002499 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002500
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002501 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2502 " p2p_dev_addr=" MACSTR " status=%d",
2503 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002504
Jouni Malinen75ecf522011-06-27 15:19:46 -07002505 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2506 status, 0, 0);
2507}
2508
2509
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002510static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2511{
2512 if (channels == NULL)
2513 return 1; /* Assume no restrictions */
2514 return p2p_channels_includes_freq(channels, freq);
2515
2516}
2517
2518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002519static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2520 const u8 *go_dev_addr, const u8 *ssid,
2521 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002522 int *force_freq, int persistent_group,
2523 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524{
2525 struct wpa_supplicant *wpa_s = ctx;
2526 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527 int res;
2528 struct wpa_supplicant *grp;
2529
2530 if (!persistent_group) {
2531 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2532 " to join an active group", MAC2STR(sa));
2533 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2534 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2535 == 0 ||
2536 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2537 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2538 "authorized invitation");
2539 goto accept_inv;
2540 }
2541 /*
2542 * Do not accept the invitation automatically; notify user and
2543 * request approval.
2544 */
2545 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2546 }
2547
2548 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2549 if (grp) {
2550 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2551 "running persistent group");
2552 if (*go)
2553 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2554 goto accept_inv;
2555 }
2556
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002557 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2558 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2559 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2560 "invitation to re-invoke a persistent group");
2561 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2563
2564 for (s = wpa_s->conf->ssid; s; s = s->next) {
2565 if (s->disabled == 2 &&
2566 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2567 s->ssid_len == ssid_len &&
2568 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2569 break;
2570 }
2571
2572 if (!s) {
2573 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2574 " requested reinvocation of an unknown group",
2575 MAC2STR(sa));
2576 return P2P_SC_FAIL_UNKNOWN_GROUP;
2577 }
2578
2579 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2580 *go = 1;
2581 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2582 wpa_printf(MSG_DEBUG, "P2P: The only available "
2583 "interface is already in use - reject "
2584 "invitation");
2585 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2586 }
2587 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2588 } else if (s->mode == WPAS_MODE_P2P_GO) {
2589 *go = 1;
2590 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2591 {
2592 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2593 "interface address for the group");
2594 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2595 }
2596 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2597 ETH_ALEN);
2598 }
2599
2600accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002601 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2602
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002603 /* Get one of the frequencies currently in use */
2604 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2605 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 -07002606 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002607 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 }
2609
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002610 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2611 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002612 if (*go == 0) {
2613 /* We are the client */
2614 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2615 "running a GO but we are capable of MCC, "
2616 "figure out the best channel to use");
2617 *force_freq = 0;
2618 } else if (!freq_included(channels, *force_freq)) {
2619 /* We are the GO, and *force_freq is not in the
2620 * intersection */
2621 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2622 "in intersection but we are capable of MCC, "
2623 "figure out the best channel to use",
2624 *force_freq);
2625 *force_freq = 0;
2626 }
2627 }
2628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629 return P2P_SC_SUCCESS;
2630}
2631
2632
2633static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2634 const u8 *ssid, size_t ssid_len,
2635 const u8 *go_dev_addr, u8 status,
2636 int op_freq)
2637{
2638 struct wpa_supplicant *wpa_s = ctx;
2639 struct wpa_ssid *s;
2640
2641 for (s = wpa_s->conf->ssid; s; s = s->next) {
2642 if (s->disabled == 2 &&
2643 s->ssid_len == ssid_len &&
2644 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2645 break;
2646 }
2647
2648 if (status == P2P_SC_SUCCESS) {
2649 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2650 " was accepted; op_freq=%d MHz",
2651 MAC2STR(sa), op_freq);
2652 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002653 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002654 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002655 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002657 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002659 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002660 }
2661 return;
2662 }
2663
2664 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2665 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2666 " was rejected (status %u)", MAC2STR(sa), status);
2667 return;
2668 }
2669
2670 if (!s) {
2671 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002672 wpa_msg_global(wpa_s, MSG_INFO,
2673 P2P_EVENT_INVITATION_RECEIVED
2674 "sa=" MACSTR " go_dev_addr=" MACSTR
2675 " bssid=" MACSTR " unknown-network",
2676 MAC2STR(sa), MAC2STR(go_dev_addr),
2677 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002679 wpa_msg_global(wpa_s, MSG_INFO,
2680 P2P_EVENT_INVITATION_RECEIVED
2681 "sa=" MACSTR " go_dev_addr=" MACSTR
2682 " unknown-network",
2683 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 }
2685 return;
2686 }
2687
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002688 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002689 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2690 "sa=" MACSTR " persistent=%d freq=%d",
2691 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002692 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002693 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2694 "sa=" MACSTR " persistent=%d",
2695 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002696 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002697}
2698
2699
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002700static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2701 struct wpa_ssid *ssid,
2702 const u8 *peer)
2703{
2704 size_t i;
2705
2706 if (ssid == NULL)
2707 return;
2708
2709 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2710 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2711 ETH_ALEN) == 0)
2712 break;
2713 }
2714 if (i >= ssid->num_p2p_clients) {
2715 if (ssid->mode != WPAS_MODE_P2P_GO &&
2716 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2717 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2718 "due to invitation result", ssid->id);
2719 wpas_notify_network_removed(wpa_s, ssid);
2720 wpa_config_remove_network(wpa_s->conf, ssid->id);
2721 return;
2722 }
2723 return; /* Peer not found in client list */
2724 }
2725
2726 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2727 "group %d client list due to invitation result",
2728 MAC2STR(peer), ssid->id);
2729 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2730 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2731 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2732 ssid->num_p2p_clients--;
2733#ifndef CONFIG_NO_CONFIG_WRITE
2734 if (wpa_s->parent->conf->update_config &&
2735 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2736 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2737#endif /* CONFIG_NO_CONFIG_WRITE */
2738}
2739
2740
2741static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2742 const u8 *peer)
2743{
2744 struct wpa_ssid *ssid;
2745
2746 wpa_s = wpa_s->global->p2p_invite_group;
2747 if (wpa_s == NULL)
2748 return; /* No known invitation group */
2749 ssid = wpa_s->current_ssid;
2750 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2751 !ssid->p2p_persistent_group)
2752 return; /* Not operating as a GO in persistent group */
2753 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2754 ssid->ssid, ssid->ssid_len);
2755 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2756}
2757
2758
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002759static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002760 const struct p2p_channels *channels,
2761 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762{
2763 struct wpa_supplicant *wpa_s = ctx;
2764 struct wpa_ssid *ssid;
2765
2766 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002767 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2768 "status=%d " MACSTR,
2769 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002771 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2772 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 }
2774 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2775
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002776 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2777 status, MAC2STR(peer));
2778 if (wpa_s->pending_invite_ssid_id == -1) {
2779 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2780 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002782 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002784 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2785 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2786 "invitation exchange to indicate readiness for "
2787 "re-invocation");
2788 }
2789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002791 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2792 ssid = wpa_config_get_network(
2793 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2794 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2795 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796 wpas_p2p_remove_pending_group_interface(wpa_s);
2797 return;
2798 }
2799
2800 ssid = wpa_config_get_network(wpa_s->conf,
2801 wpa_s->pending_invite_ssid_id);
2802 if (ssid == NULL) {
2803 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2804 "data matching with invitation");
2805 return;
2806 }
2807
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002808 /*
2809 * The peer could have missed our ctrl::ack frame for Invitation
2810 * Response and continue retransmitting the frame. To reduce the
2811 * likelihood of the peer not getting successful TX status for the
2812 * Invitation Response frame, wait a short time here before starting
2813 * the persistent group so that we will remain on the current channel to
2814 * acknowledge any possible retransmission from the peer.
2815 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002816#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002817 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2818 "starting persistent group");
2819 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002820#else
2821 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2822 "starting persistent group");
2823 os_sleep(0, 100000);
2824#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002827 ssid->mode == WPAS_MODE_P2P_GO,
2828 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002829 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830}
2831
2832
Dmitry Shmidt04949592012-07-19 12:16:46 -07002833static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2834 unsigned int freq)
2835{
2836 unsigned int i;
2837
2838 if (global->p2p_disallow_freq == NULL)
2839 return 0;
2840
2841 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2842 if (freq >= global->p2p_disallow_freq[i].min &&
2843 freq <= global->p2p_disallow_freq[i].max)
2844 return 1;
2845 }
2846
2847 return 0;
2848}
2849
2850
2851static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2852{
2853 reg->channel[reg->channels] = chan;
2854 reg->channels++;
2855}
2856
2857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2859 struct p2p_channels *chan)
2860{
2861 int i, cla = 0;
2862
2863 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2864 "band");
2865
2866 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2867 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002868 chan->reg_class[cla].channels = 0;
2869 for (i = 0; i < 11; i++) {
2870 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2871 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2872 }
2873 if (chan->reg_class[cla].channels)
2874 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875
2876 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2877 "band");
2878
2879 /* Operating class 115 - 5 GHz, channels 36-48 */
2880 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002881 chan->reg_class[cla].channels = 0;
2882 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2883 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2884 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2885 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2886 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2887 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2888 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2889 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2890 if (chan->reg_class[cla].channels)
2891 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892
2893 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2894 "band");
2895
2896 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2897 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002898 chan->reg_class[cla].channels = 0;
2899 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2900 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2901 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2902 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2903 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2904 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2905 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2906 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2907 if (chan->reg_class[cla].channels)
2908 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909
2910 chan->reg_classes = cla;
2911 return 0;
2912}
2913
2914
2915static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2916 u16 num_modes,
2917 enum hostapd_hw_mode mode)
2918{
2919 u16 i;
2920
2921 for (i = 0; i < num_modes; i++) {
2922 if (modes[i].mode == mode)
2923 return &modes[i];
2924 }
2925
2926 return NULL;
2927}
2928
2929
Dmitry Shmidt04949592012-07-19 12:16:46 -07002930static int has_channel(struct wpa_global *global,
2931 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932{
2933 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002934 unsigned int freq;
2935
2936 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2937 chan * 5;
2938 if (wpas_p2p_disallowed_freq(global, freq))
2939 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940
2941 for (i = 0; i < mode->num_channels; i++) {
2942 if (mode->channels[i].chan == chan) {
2943 if (flags)
2944 *flags = mode->channels[i].flag;
2945 return !(mode->channels[i].flag &
2946 (HOSTAPD_CHAN_DISABLED |
2947 HOSTAPD_CHAN_PASSIVE_SCAN |
2948 HOSTAPD_CHAN_NO_IBSS |
2949 HOSTAPD_CHAN_RADAR));
2950 }
2951 }
2952
2953 return 0;
2954}
2955
2956
2957struct p2p_oper_class_map {
2958 enum hostapd_hw_mode mode;
2959 u8 op_class;
2960 u8 min_chan;
2961 u8 max_chan;
2962 u8 inc;
2963 enum { BW20, BW40PLUS, BW40MINUS } bw;
2964};
2965
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002966static struct p2p_oper_class_map op_class[] = {
2967 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002968#if 0 /* Do not enable HT40 on 2 GHz for now */
2969 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2970 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2971#endif
2972 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2973 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2974 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2975 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2976 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2977 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2978 { -1, 0, 0, 0, 0, BW20 }
2979};
2980
2981
2982static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2983 struct hostapd_hw_modes *mode,
2984 u8 channel, u8 bw)
2985{
2986 int flag;
2987
2988 if (!has_channel(wpa_s->global, mode, channel, &flag))
2989 return -1;
2990 if (bw == BW40MINUS &&
2991 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2992 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2993 return 0;
2994 if (bw == BW40PLUS &&
2995 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2996 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2997 return 0;
2998 return 1;
2999}
3000
3001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3003 struct p2p_channels *chan)
3004{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003005 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003008 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003009 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3010 "of all supported channels; assume dualband "
3011 "support");
3012 return wpas_p2p_default_channels(wpa_s, chan);
3013 }
3014
3015 cla = 0;
3016
3017 for (op = 0; op_class[op].op_class; op++) {
3018 struct p2p_oper_class_map *o = &op_class[op];
3019 u8 ch;
3020 struct p2p_reg_class *reg = NULL;
3021
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003022 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023 if (mode == NULL)
3024 continue;
3025 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003026 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 continue;
3028 if (reg == NULL) {
3029 wpa_printf(MSG_DEBUG, "P2P: Add operating "
3030 "class %u", o->op_class);
3031 reg = &chan->reg_class[cla];
3032 cla++;
3033 reg->reg_class = o->op_class;
3034 }
3035 reg->channel[reg->channels] = ch;
3036 reg->channels++;
3037 }
3038 if (reg) {
3039 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3040 reg->channel, reg->channels);
3041 }
3042 }
3043
3044 chan->reg_classes = cla;
3045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046 return 0;
3047}
3048
3049
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003050int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3051 struct hostapd_hw_modes *mode, u8 channel)
3052{
3053 int op, ret;
3054
3055 for (op = 0; op_class[op].op_class; op++) {
3056 struct p2p_oper_class_map *o = &op_class[op];
3057 u8 ch;
3058
3059 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3060 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3061 o->bw == BW20 || ch != channel)
3062 continue;
3063 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3064 if (ret < 0)
3065 continue;
3066 else if (ret > 0)
3067 return (o->bw == BW40MINUS) ? -1 : 1;
3068 else
3069 return 0;
3070 }
3071 }
3072 return 0;
3073}
3074
3075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3077 size_t buf_len)
3078{
3079 struct wpa_supplicant *wpa_s = ctx;
3080
3081 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3082 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3083 break;
3084 }
3085 if (wpa_s == NULL)
3086 return -1;
3087
3088 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3089}
3090
3091
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003092static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3093{
3094 struct wpa_supplicant *wpa_s = ctx;
3095
3096 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3097 struct wpa_ssid *ssid = wpa_s->current_ssid;
3098 if (ssid == NULL)
3099 continue;
3100 if (ssid->mode != WPAS_MODE_INFRA)
3101 continue;
3102 if (wpa_s->wpa_state != WPA_COMPLETED &&
3103 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3104 continue;
3105 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3106 return 1;
3107 }
3108
3109 return 0;
3110}
3111
3112
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003113static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3114{
3115 struct wpa_supplicant *wpa_s = ctx;
3116 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3117}
3118
3119
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003120int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3121{
3122 struct wpa_interface iface;
3123 struct wpa_supplicant *p2pdev_wpa_s;
3124 char ifname[100];
3125 char force_name[100];
3126 int ret;
3127
3128 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3129 wpa_s->ifname);
3130 force_name[0] = '\0';
3131 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3132 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3133 force_name, wpa_s->pending_interface_addr, NULL);
3134 if (ret < 0) {
3135 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3136 return ret;
3137 }
3138 os_strlcpy(wpa_s->pending_interface_name, ifname,
3139 sizeof(wpa_s->pending_interface_name));
3140
3141 os_memset(&iface, 0, sizeof(iface));
3142 iface.p2p_mgmt = 1;
3143 iface.ifname = wpa_s->pending_interface_name;
3144 iface.driver = wpa_s->driver->name;
3145 iface.driver_param = wpa_s->conf->driver_param;
3146 iface.confname = wpa_s->confname;
3147 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3148 if (!p2pdev_wpa_s) {
3149 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3150 return -1;
3151 }
3152 p2pdev_wpa_s->parent = wpa_s;
3153
3154 wpa_s->pending_interface_name[0] = '\0';
3155 return 0;
3156}
3157
3158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003159/**
3160 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3161 * @global: Pointer to global data from wpa_supplicant_init()
3162 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3163 * Returns: 0 on success, -1 on failure
3164 */
3165int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3166{
3167 struct p2p_config p2p;
3168 unsigned int r;
3169 int i;
3170
3171 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3172 return 0;
3173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003174 if (global->p2p)
3175 return 0;
3176
3177 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3178 struct p2p_params params;
3179
3180 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3181 os_memset(&params, 0, sizeof(params));
3182 params.dev_name = wpa_s->conf->device_name;
3183 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3184 WPS_DEV_TYPE_LEN);
3185 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3186 os_memcpy(params.sec_dev_type,
3187 wpa_s->conf->sec_device_type,
3188 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3189
3190 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3191 return -1;
3192
3193 return 0;
3194 }
3195
3196 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003198 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 p2p.p2p_scan = wpas_p2p_scan;
3200 p2p.send_action = wpas_send_action;
3201 p2p.send_action_done = wpas_send_action_done;
3202 p2p.go_neg_completed = wpas_go_neg_completed;
3203 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3204 p2p.dev_found = wpas_dev_found;
3205 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003206 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207 p2p.start_listen = wpas_start_listen;
3208 p2p.stop_listen = wpas_stop_listen;
3209 p2p.send_probe_resp = wpas_send_probe_resp;
3210 p2p.sd_request = wpas_sd_request;
3211 p2p.sd_response = wpas_sd_response;
3212 p2p.prov_disc_req = wpas_prov_disc_req;
3213 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003214 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215 p2p.invitation_process = wpas_invitation_process;
3216 p2p.invitation_received = wpas_invitation_received;
3217 p2p.invitation_result = wpas_invitation_result;
3218 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003219 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220
3221 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003222 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223 p2p.dev_name = wpa_s->conf->device_name;
3224 p2p.manufacturer = wpa_s->conf->manufacturer;
3225 p2p.model_name = wpa_s->conf->model_name;
3226 p2p.model_number = wpa_s->conf->model_number;
3227 p2p.serial_number = wpa_s->conf->serial_number;
3228 if (wpa_s->wps) {
3229 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3230 p2p.config_methods = wpa_s->wps->config_methods;
3231 }
3232
3233 if (wpa_s->conf->p2p_listen_reg_class &&
3234 wpa_s->conf->p2p_listen_channel) {
3235 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3236 p2p.channel = wpa_s->conf->p2p_listen_channel;
3237 } else {
3238 p2p.reg_class = 81;
3239 /*
3240 * Pick one of the social channels randomly as the listen
3241 * channel.
3242 */
3243 os_get_random((u8 *) &r, sizeof(r));
3244 p2p.channel = 1 + (r % 3) * 5;
3245 }
3246 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3247
3248 if (wpa_s->conf->p2p_oper_reg_class &&
3249 wpa_s->conf->p2p_oper_channel) {
3250 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3251 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3252 p2p.cfg_op_channel = 1;
3253 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3254 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3255
3256 } else {
3257 p2p.op_reg_class = 81;
3258 /*
3259 * Use random operation channel from (1, 6, 11) if no other
3260 * preference is indicated.
3261 */
3262 os_get_random((u8 *) &r, sizeof(r));
3263 p2p.op_channel = 1 + (r % 3) * 5;
3264 p2p.cfg_op_channel = 0;
3265 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3266 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3267 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003268
3269 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3270 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3271 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3272 }
3273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003274 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3275 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3276 p2p.country[2] = 0x04;
3277 } else
3278 os_memcpy(p2p.country, "XX\x04", 3);
3279
3280 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3281 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3282 "channel list");
3283 return -1;
3284 }
3285
3286 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3287 WPS_DEV_TYPE_LEN);
3288
3289 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3290 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3291 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3292
3293 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3294 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3295
3296 p2p.max_peers = 100;
3297
3298 if (wpa_s->conf->p2p_ssid_postfix) {
3299 p2p.ssid_postfix_len =
3300 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3301 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3302 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3303 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3304 p2p.ssid_postfix_len);
3305 }
3306
3307 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3308
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003309 p2p.max_listen = wpa_s->max_remain_on_chan;
3310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311 global->p2p = p2p_init(&p2p);
3312 if (global->p2p == NULL)
3313 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003314 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003315
3316 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3317 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3318 continue;
3319 p2p_add_wps_vendor_extension(
3320 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3321 }
3322
3323 return 0;
3324}
3325
3326
3327/**
3328 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3329 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3330 *
3331 * This function deinitialize per-interface P2P data.
3332 */
3333void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3334{
3335 if (wpa_s->driver && wpa_s->drv_priv)
3336 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003337
3338 if (wpa_s->go_params) {
3339 /* Clear any stored provisioning info */
3340 p2p_clear_provisioning_info(
3341 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003342 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003343 }
3344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345 os_free(wpa_s->go_params);
3346 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003347#ifdef ANDROID_P2P
3348 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3349#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003350 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3351 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3352 wpa_s->p2p_long_listen = 0;
3353 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3354 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3355 wpas_p2p_remove_pending_group_interface(wpa_s);
3356
3357 /* TODO: remove group interface from the driver if this wpa_s instance
3358 * is on top of a P2P group interface */
3359}
3360
3361
3362/**
3363 * wpas_p2p_deinit_global - Deinitialize global P2P module
3364 * @global: Pointer to global data from wpa_supplicant_init()
3365 *
3366 * This function deinitializes the global (per device) P2P module.
3367 */
3368void wpas_p2p_deinit_global(struct wpa_global *global)
3369{
3370 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003371
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003372 wpa_s = global->ifaces;
3373 if (wpa_s)
3374 wpas_p2p_service_flush(wpa_s);
3375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003376 if (global->p2p == NULL)
3377 return;
3378
3379 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003380 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3381 wpa_s = wpa_s->next;
3382 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383 tmp = global->ifaces;
3384 while (tmp &&
3385 (tmp == wpa_s ||
3386 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3387 tmp = tmp->next;
3388 }
3389 if (tmp == NULL)
3390 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003391 /* Disconnect from the P2P group and deinit the interface */
3392 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393 }
3394
3395 /*
3396 * Deinit GO data on any possibly remaining interface (if main
3397 * interface is used as GO).
3398 */
3399 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3400 if (wpa_s->ap_iface)
3401 wpas_p2p_group_deinit(wpa_s);
3402 }
3403
3404 p2p_deinit(global->p2p);
3405 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003406 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003407}
3408
3409
3410static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3411{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003412 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3413 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003414 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415 if (wpa_s->drv_flags &
3416 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3417 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3418 return 1; /* P2P group requires a new interface in every case
3419 */
3420 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3421 return 0; /* driver does not support concurrent operations */
3422 if (wpa_s->global->ifaces->next)
3423 return 1; /* more that one interface already in use */
3424 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3425 return 1; /* this interface is already in use */
3426 return 0;
3427}
3428
3429
3430static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3431 const u8 *peer_addr,
3432 enum p2p_wps_method wps_method,
3433 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003434 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003435 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003437 if (persistent_group && wpa_s->conf->persistent_reconnect)
3438 persistent_group = 2;
3439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3441 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3442 go_intent, own_interface_addr,
3443 force_freq, persistent_group);
3444 }
3445
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003446 /*
3447 * Increase GO config timeout if HT40 is used since it takes some time
3448 * to scan channels for coex purposes before the BSS can be started.
3449 */
3450 p2p_set_config_timeout(wpa_s->global->p2p,
3451 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3452
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003453 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3454 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003455 persistent_group, ssid ? ssid->ssid : NULL,
3456 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003457 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003458}
3459
3460
3461static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3462 const u8 *peer_addr,
3463 enum p2p_wps_method wps_method,
3464 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003465 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003466 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003467{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003468 if (persistent_group && wpa_s->conf->persistent_reconnect)
3469 persistent_group = 2;
3470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003471 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3472 return -1;
3473
3474 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3475 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003476 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003477 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003478}
3479
3480
3481static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3482{
3483 wpa_s->p2p_join_scan_count++;
3484 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3485 wpa_s->p2p_join_scan_count);
3486 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3487 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3488 " for join operationg - stop join attempt",
3489 MAC2STR(wpa_s->pending_join_iface_addr));
3490 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003491 if (wpa_s->p2p_auto_pd) {
3492 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003493 wpa_msg_global(wpa_s, MSG_INFO,
3494 P2P_EVENT_PROV_DISC_FAILURE
3495 " p2p_dev_addr=" MACSTR " status=N/A",
3496 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003497 return;
3498 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003499 wpa_msg_global(wpa_s->parent, MSG_INFO,
3500 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 }
3502}
3503
3504
Dmitry Shmidt04949592012-07-19 12:16:46 -07003505static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3506{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003507 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003508
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003509 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3510 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003511 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003512 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003513
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003514 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3515 if (!freqs)
3516 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003517
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003518 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3519 wpa_s->num_multichan_concurrent);
3520 if (num < 0) {
3521 res = 1;
3522 goto exit_free;
3523 }
3524
3525 for (i = 0; i < num; i++) {
3526 if (freqs[i] == freq) {
3527 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3528 freq);
3529 res = 0;
3530 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003531 }
3532 }
3533
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003534 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003535
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003536exit_free:
3537 os_free(freqs);
3538 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003539}
3540
3541
3542static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3543 const u8 *peer_dev_addr)
3544{
3545 struct wpa_bss *bss;
3546 int updated;
3547
3548 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3549 if (bss == NULL)
3550 return -1;
3551 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3552 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3553 "last scan");
3554 return 0;
3555 }
3556
3557 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3558 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3559 "%ld.%06ld (%supdated in last scan)",
3560 bss->last_update.sec, bss->last_update.usec,
3561 updated ? "": "not ");
3562
3563 return updated;
3564}
3565
3566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3568 struct wpa_scan_results *scan_res)
3569{
3570 struct wpa_bss *bss;
3571 int freq;
3572 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003574 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3575
3576 if (wpa_s->global->p2p_disabled)
3577 return;
3578
Dmitry Shmidt04949592012-07-19 12:16:46 -07003579 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3580 scan_res ? (int) scan_res->num : -1,
3581 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003582
3583 if (scan_res)
3584 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3585
Dmitry Shmidt04949592012-07-19 12:16:46 -07003586 if (wpa_s->p2p_auto_pd) {
3587 int join = wpas_p2p_peer_go(wpa_s,
3588 wpa_s->pending_join_dev_addr);
3589 if (join == 0 &&
3590 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3591 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003592 bss = wpa_bss_get_bssid_latest(
3593 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003594 if (bss) {
3595 freq = bss->freq;
3596 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3597 "the peer " MACSTR " at %d MHz",
3598 wpa_s->auto_pd_scan_retry,
3599 MAC2STR(wpa_s->
3600 pending_join_dev_addr),
3601 freq);
3602 wpas_p2p_join_scan_req(wpa_s, freq);
3603 return;
3604 }
3605 }
3606
3607 if (join < 0)
3608 join = 0;
3609
3610 wpa_s->p2p_auto_pd = 0;
3611 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3612 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3613 MAC2STR(wpa_s->pending_join_dev_addr), join);
3614 if (p2p_prov_disc_req(wpa_s->global->p2p,
3615 wpa_s->pending_join_dev_addr,
3616 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003617 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003618 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003619 wpa_msg_global(wpa_s, MSG_INFO,
3620 P2P_EVENT_PROV_DISC_FAILURE
3621 " p2p_dev_addr=" MACSTR " status=N/A",
3622 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003623 }
3624 return;
3625 }
3626
3627 if (wpa_s->p2p_auto_join) {
3628 int join = wpas_p2p_peer_go(wpa_s,
3629 wpa_s->pending_join_dev_addr);
3630 if (join < 0) {
3631 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3632 "running a GO -> use GO Negotiation");
3633 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3634 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3635 wpa_s->p2p_persistent_group, 0, 0, 0,
3636 wpa_s->p2p_go_intent,
3637 wpa_s->p2p_connect_freq,
3638 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003639 wpa_s->p2p_pd_before_go_neg,
3640 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003641 return;
3642 }
3643
3644 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3645 "try to join the group", join ? "" :
3646 " in older scan");
3647 if (!join)
3648 wpa_s->p2p_fallback_to_go_neg = 1;
3649 }
3650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3652 wpa_s->pending_join_iface_addr);
3653 if (freq < 0 &&
3654 p2p_get_interface_addr(wpa_s->global->p2p,
3655 wpa_s->pending_join_dev_addr,
3656 iface_addr) == 0 &&
3657 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3658 {
3659 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3660 "address for join from " MACSTR " to " MACSTR
3661 " based on newly discovered P2P peer entry",
3662 MAC2STR(wpa_s->pending_join_iface_addr),
3663 MAC2STR(iface_addr));
3664 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3665 ETH_ALEN);
3666
3667 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3668 wpa_s->pending_join_iface_addr);
3669 }
3670 if (freq >= 0) {
3671 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3672 "from P2P peer table: %d MHz", freq);
3673 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003674 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675 if (bss) {
3676 freq = bss->freq;
3677 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3678 "from BSS table: %d MHz", freq);
3679 }
3680 if (freq > 0) {
3681 u16 method;
3682
Dmitry Shmidt04949592012-07-19 12:16:46 -07003683 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003684 wpa_msg_global(wpa_s->parent, MSG_INFO,
3685 P2P_EVENT_GROUP_FORMATION_FAILURE
3686 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003687 return;
3688 }
3689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003690 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3691 "prior to joining an existing group (GO " MACSTR
3692 " freq=%u MHz)",
3693 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3694 wpa_s->pending_pd_before_join = 1;
3695
3696 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 case WPS_PIN_DISPLAY:
3698 method = WPS_CONFIG_KEYPAD;
3699 break;
3700 case WPS_PIN_KEYPAD:
3701 method = WPS_CONFIG_DISPLAY;
3702 break;
3703 case WPS_PBC:
3704 method = WPS_CONFIG_PUSHBUTTON;
3705 break;
3706 default:
3707 method = 0;
3708 break;
3709 }
3710
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003711 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3712 wpa_s->pending_join_dev_addr) ==
3713 method)) {
3714 /*
3715 * We have already performed provision discovery for
3716 * joining the group. Proceed directly to join
3717 * operation without duplicated provision discovery. */
3718 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3719 "with " MACSTR " already done - proceed to "
3720 "join",
3721 MAC2STR(wpa_s->pending_join_dev_addr));
3722 wpa_s->pending_pd_before_join = 0;
3723 goto start;
3724 }
3725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003727 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003728 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003729 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3730 "Discovery Request before joining an "
3731 "existing group");
3732 wpa_s->pending_pd_before_join = 0;
3733 goto start;
3734 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003735 return;
3736 }
3737
3738 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3739 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3740 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3741 wpas_p2p_check_join_scan_limit(wpa_s);
3742 return;
3743
3744start:
3745 /* Start join operation immediately */
3746 wpas_p2p_join_start(wpa_s);
3747}
3748
3749
Dmitry Shmidt04949592012-07-19 12:16:46 -07003750static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752 int ret;
3753 struct wpa_driver_scan_params params;
3754 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003755 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003756 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003757#ifdef ANDROID_P2P
3758 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003760 /* If freq is not provided, check the operating freq of the GO and do a
3761 * a directed scan to save time
3762 */
3763 if(!freq) {
3764 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3765 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3766 }
3767#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003768 os_memset(&params, 0, sizeof(params));
3769
3770 /* P2P Wildcard SSID */
3771 params.num_ssids = 1;
3772 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3773 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3774
3775 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003776 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3777 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3778 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779 if (wps_ie == NULL) {
3780 wpas_p2p_scan_res_join(wpa_s, NULL);
3781 return;
3782 }
3783
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003784 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3785 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786 if (ies == NULL) {
3787 wpabuf_free(wps_ie);
3788 wpas_p2p_scan_res_join(wpa_s, NULL);
3789 return;
3790 }
3791 wpabuf_put_buf(ies, wps_ie);
3792 wpabuf_free(wps_ie);
3793
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003794 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003796 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003797 params.extra_ies = wpabuf_head(ies);
3798 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003799 if (freq > 0) {
3800 freqs[0] = freq;
3801 params.freqs = freqs;
3802 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003803
3804 /*
3805 * Run a scan to update BSS table and start Provision Discovery once
3806 * the new scan results become available.
3807 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003808 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003809 if (!ret) {
3810 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003811 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003813
3814 wpabuf_free(ies);
3815
3816 if (ret) {
3817 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3818 "try again later");
3819 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3820 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3821 wpas_p2p_check_join_scan_limit(wpa_s);
3822 }
3823}
3824
3825
Dmitry Shmidt04949592012-07-19 12:16:46 -07003826static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3827{
3828 struct wpa_supplicant *wpa_s = eloop_ctx;
3829 wpas_p2p_join_scan_req(wpa_s, 0);
3830}
3831
3832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003833static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003834 const u8 *dev_addr, enum p2p_wps_method wps_method,
3835 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003836{
3837 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003838 MACSTR " dev " MACSTR ")%s",
3839 MAC2STR(iface_addr), MAC2STR(dev_addr),
3840 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003841
Dmitry Shmidt04949592012-07-19 12:16:46 -07003842 wpa_s->p2p_auto_pd = 0;
3843 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3845 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3846 wpa_s->pending_join_wps_method = wps_method;
3847
3848 /* Make sure we are not running find during connection establishment */
3849 wpas_p2p_stop_find(wpa_s);
3850
3851 wpa_s->p2p_join_scan_count = 0;
3852 wpas_p2p_join_scan(wpa_s, NULL);
3853 return 0;
3854}
3855
3856
3857static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3858{
3859 struct wpa_supplicant *group;
3860 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003861 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003862
3863 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3864 if (group == NULL)
3865 return -1;
3866 if (group != wpa_s) {
3867 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3868 sizeof(group->p2p_pin));
3869 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003870 } else {
3871 /*
3872 * Need to mark the current interface for p2p_group_formation
3873 * when a separate group interface is not used. This is needed
3874 * to allow p2p_cancel stop a pending p2p_connect-join.
3875 * wpas_p2p_init_group_interface() addresses this for the case
3876 * where a separate group interface is used.
3877 */
3878 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 }
3880
3881 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003882 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003883
3884 os_memset(&res, 0, sizeof(res));
3885 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3886 ETH_ALEN);
3887 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003888 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003889 if (bss) {
3890 res.freq = bss->freq;
3891 res.ssid_len = bss->ssid_len;
3892 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3893 }
3894
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003895 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3896 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3897 "starting client");
3898 wpa_drv_cancel_remain_on_channel(wpa_s);
3899 wpa_s->off_channel_freq = 0;
3900 wpa_s->roc_waiting_drv_freq = 0;
3901 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902 wpas_start_wps_enrollee(group, &res);
3903
3904 /*
3905 * Allow a longer timeout for join-a-running-group than normal 15
3906 * second group formation timeout since the GO may not have authorized
3907 * our connection yet.
3908 */
3909 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3910 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3911 wpa_s, NULL);
3912
3913 return 0;
3914}
3915
3916
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003917static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003918 int *force_freq, int *pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003919{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003920 int *freqs, res;
3921 unsigned int freq_in_use = 0, num, i;
3922
3923 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3924 if (!freqs)
3925 return -1;
3926
3927 num = get_shared_radio_freqs(wpa_s, freqs,
3928 wpa_s->num_multichan_concurrent);
3929
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003930 if (freq > 0) {
3931 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3932 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3933 "(%u MHz) is not supported for P2P uses",
3934 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003935 res = -3;
3936 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003937 }
3938
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003939 for (i = 0; i < num; i++) {
3940 if (freqs[i] == freq)
3941 freq_in_use = 1;
3942 }
3943
3944 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
3945 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
3946 freq);
3947 res = -2;
3948 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003949 }
3950 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3951 "requested channel (%u MHz)", freq);
3952 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003953 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003954 }
3955
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003956 for (i = 0; i < num; i++) {
3957 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
3958 continue;
3959
3960 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
3961 *force_freq);
3962 *force_freq = freqs[i];
3963
3964 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
3965 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
3966 *pref_freq = *force_freq;
3967 }
3968 break;
3969 }
3970
3971 if (i == num) {
3972 if (num < wpa_s->num_multichan_concurrent) {
3973 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
3974 *force_freq = 0;
3975 } else {
3976 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
3977 res = -2;
3978 goto exit_free;
3979 }
3980 }
3981
3982exit_ok:
3983 res = 0;
3984exit_free:
3985 os_free(freqs);
3986 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003987}
3988
3989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003990/**
3991 * wpas_p2p_connect - Request P2P Group Formation to be started
3992 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3993 * @peer_addr: Address of the peer P2P Device
3994 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3995 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003996 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997 * @join: Whether to join an existing group (as a client) instead of starting
3998 * Group Owner negotiation; @peer_addr is BSSID in that case
3999 * @auth: Whether to only authorize the connection instead of doing that and
4000 * initiating Group Owner negotiation
4001 * @go_intent: GO Intent or -1 to use default
4002 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004003 * @persistent_id: Persistent group credentials to use for forcing GO
4004 * parameters or -1 to generate new values (SSID/passphrase)
4005 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4006 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004007 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4009 * failure, -2 on failure due to channel not currently available,
4010 * -3 if forced channel is not supported
4011 */
4012int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4013 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004014 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004015 int go_intent, int freq, int persistent_id, int pd,
4016 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004017{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004018 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004019 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004021 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004022 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023
4024 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4025 return -1;
4026
Dmitry Shmidt04949592012-07-19 12:16:46 -07004027 if (persistent_id >= 0) {
4028 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4029 if (ssid == NULL || ssid->disabled != 2 ||
4030 ssid->mode != WPAS_MODE_P2P_GO)
4031 return -1;
4032 }
4033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034 if (go_intent < 0)
4035 go_intent = wpa_s->conf->p2p_go_intent;
4036
4037 if (!auth)
4038 wpa_s->p2p_long_listen = 0;
4039
4040 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004041 wpa_s->p2p_persistent_group = !!persistent_group;
4042 wpa_s->p2p_persistent_id = persistent_id;
4043 wpa_s->p2p_go_intent = go_intent;
4044 wpa_s->p2p_connect_freq = freq;
4045 wpa_s->p2p_fallback_to_go_neg = 0;
4046 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004047 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004048
4049 if (pin)
4050 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4051 else if (wps_method == WPS_PIN_DISPLAY) {
4052 ret = wps_generate_pin();
4053 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4054 ret);
4055 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4056 wpa_s->p2p_pin);
4057 } else
4058 wpa_s->p2p_pin[0] = '\0';
4059
Dmitry Shmidt04949592012-07-19 12:16:46 -07004060 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4062 if (auth) {
4063 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4064 "connect a running group from " MACSTR,
4065 MAC2STR(peer_addr));
4066 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4067 return ret;
4068 }
4069 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4070 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4071 iface_addr) < 0) {
4072 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4073 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4074 dev_addr);
4075 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004076 if (auto_join) {
4077 os_get_time(&wpa_s->p2p_auto_started);
4078 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4079 "%ld.%06ld",
4080 wpa_s->p2p_auto_started.sec,
4081 wpa_s->p2p_auto_started.usec);
4082 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004083 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004084 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4085 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004086 return -1;
4087 return ret;
4088 }
4089
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004090 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004091 if (res)
4092 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004093 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094
4095 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4096
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004097 if (wpa_s->create_p2p_iface) {
4098 /* Prepare to add a new interface for the group */
4099 iftype = WPA_IF_P2P_GROUP;
4100 if (go_intent == 15)
4101 iftype = WPA_IF_P2P_GO;
4102 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4103 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4104 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004106 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004108 if_addr = wpa_s->pending_interface_addr;
4109 } else
4110 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004111
4112 if (auth) {
4113 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004114 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004115 force_freq, persistent_group, ssid,
4116 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117 return -1;
4118 return ret;
4119 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004120
4121 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4122 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004123 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004124 if (wpa_s->create_p2p_iface)
4125 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126 return -1;
4127 }
4128 return ret;
4129}
4130
4131
4132/**
4133 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4134 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4135 * @freq: Frequency of the channel in MHz
4136 * @duration: Duration of the stay on the channel in milliseconds
4137 *
4138 * This callback is called when the driver indicates that it has started the
4139 * requested remain-on-channel duration.
4140 */
4141void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4142 unsigned int freq, unsigned int duration)
4143{
4144 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4145 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4147 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4148 wpa_s->pending_listen_duration);
4149 wpa_s->pending_listen_freq = 0;
4150 }
4151}
4152
4153
4154static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4155 unsigned int timeout)
4156{
4157 /* Limit maximum Listen state time based on driver limitation. */
4158 if (timeout > wpa_s->max_remain_on_chan)
4159 timeout = wpa_s->max_remain_on_chan;
4160
4161 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4162 return wpa_drv_p2p_listen(wpa_s, timeout);
4163
4164 return p2p_listen(wpa_s->global->p2p, timeout);
4165}
4166
4167
4168/**
4169 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4170 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4171 * @freq: Frequency of the channel in MHz
4172 *
4173 * This callback is called when the driver indicates that a remain-on-channel
4174 * operation has been completed, i.e., the duration on the requested channel
4175 * has timed out.
4176 */
4177void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4178 unsigned int freq)
4179{
4180 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4181 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004182 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004183 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4184 return;
4185 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4186 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004187 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004188 return;
4189 if (wpa_s->p2p_long_listen > 0)
4190 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4191 if (wpa_s->p2p_long_listen > 0) {
4192 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4193 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4194 }
4195}
4196
4197
4198/**
4199 * wpas_p2p_group_remove - Remove a P2P group
4200 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4201 * @ifname: Network interface name of the group interface or "*" to remove all
4202 * groups
4203 * Returns: 0 on success, -1 on failure
4204 *
4205 * This function is used to remove a P2P group. This can be used to disconnect
4206 * from a group in which the local end is a P2P Client or to end a P2P Group in
4207 * case the local end is the Group Owner. If a virtual network interface was
4208 * created for this group, that interface will be removed. Otherwise, only the
4209 * configured P2P group network will be removed from the interface.
4210 */
4211int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4212{
4213 struct wpa_global *global = wpa_s->global;
4214
4215 if (os_strcmp(ifname, "*") == 0) {
4216 struct wpa_supplicant *prev;
4217 wpa_s = global->ifaces;
4218 while (wpa_s) {
4219 prev = wpa_s;
4220 wpa_s = wpa_s->next;
4221 wpas_p2p_disconnect(prev);
4222 }
4223 return 0;
4224 }
4225
4226 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4227 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4228 break;
4229 }
4230
4231 return wpas_p2p_disconnect(wpa_s);
4232}
4233
4234
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004235static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4236{
4237 unsigned int r;
4238
4239 if (freq == 2) {
4240 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4241 "band");
4242 if (wpa_s->best_24_freq > 0 &&
4243 p2p_supported_freq(wpa_s->global->p2p,
4244 wpa_s->best_24_freq)) {
4245 freq = wpa_s->best_24_freq;
4246 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4247 "channel: %d MHz", freq);
4248 } else {
4249 os_get_random((u8 *) &r, sizeof(r));
4250 freq = 2412 + (r % 3) * 25;
4251 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4252 "channel: %d MHz", freq);
4253 }
4254 }
4255
4256 if (freq == 5) {
4257 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4258 "band");
4259 if (wpa_s->best_5_freq > 0 &&
4260 p2p_supported_freq(wpa_s->global->p2p,
4261 wpa_s->best_5_freq)) {
4262 freq = wpa_s->best_5_freq;
4263 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4264 "channel: %d MHz", freq);
4265 } else {
4266 os_get_random((u8 *) &r, sizeof(r));
4267 freq = 5180 + (r % 4) * 20;
4268 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4269 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4270 "5 GHz channel for P2P group");
4271 return -1;
4272 }
4273 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4274 "channel: %d MHz", freq);
4275 }
4276 }
4277
4278 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4279 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4280 "(%u MHz) is not supported for P2P uses",
4281 freq);
4282 return -1;
4283 }
4284
4285 return freq;
4286}
4287
4288
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004289static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4290 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004291 int freq, int ht40,
4292 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004293{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004294 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004295 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004296 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004297
4298 os_memset(params, 0, sizeof(*params));
4299 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004300 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004301 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004302 if (!freq_included(channels, freq)) {
4303 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4304 "accepted", freq);
4305 return -1;
4306 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004307 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4308 "frequency %d MHz", freq);
4309 params->freq = freq;
4310 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4311 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004312 wpa_s->conf->p2p_oper_channel <= 11 &&
4313 freq_included(channels,
4314 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004315 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4316 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4317 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004318 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4319 wpa_s->conf->p2p_oper_reg_class == 116 ||
4320 wpa_s->conf->p2p_oper_reg_class == 117 ||
4321 wpa_s->conf->p2p_oper_reg_class == 124 ||
4322 wpa_s->conf->p2p_oper_reg_class == 126 ||
4323 wpa_s->conf->p2p_oper_reg_class == 127) &&
4324 freq_included(channels,
4325 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4327 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4328 "frequency %d MHz", params->freq);
4329 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4330 wpa_s->best_overall_freq > 0 &&
4331 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004332 wpa_s->best_overall_freq) &&
4333 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334 params->freq = wpa_s->best_overall_freq;
4335 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4336 "channel %d MHz", params->freq);
4337 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4338 wpa_s->best_24_freq > 0 &&
4339 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004340 wpa_s->best_24_freq) &&
4341 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004342 params->freq = wpa_s->best_24_freq;
4343 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4344 "channel %d MHz", params->freq);
4345 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4346 wpa_s->best_5_freq > 0 &&
4347 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004348 wpa_s->best_5_freq) &&
4349 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350 params->freq = wpa_s->best_5_freq;
4351 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4352 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004353 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4354 channels))) {
4355 params->freq = pref_freq;
4356 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4357 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004358 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004359 int chan;
4360 for (chan = 0; chan < 11; chan++) {
4361 params->freq = 2412 + chan * 5;
4362 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004363 params->freq) &&
4364 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004365 break;
4366 }
4367 if (chan == 11) {
4368 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4369 "allowed");
4370 return -1;
4371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004372 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4373 "known)", params->freq);
4374 }
4375
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004376 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4377 if (!freqs)
4378 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004380 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4381 wpa_s->num_multichan_concurrent);
4382 if (res < 0) {
4383 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004384 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004385 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004386 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004387
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004388 if (!freq) {
4389 for (i = 0; i < num; i++) {
4390 if (freq_included(channels, freqs[i])) {
4391 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4392 freqs[i]);
4393 params->freq = freqs[i];
4394 break;
4395 }
4396 }
4397
4398 if (i == num) {
4399 if (num == wpa_s->num_multichan_concurrent) {
4400 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4401 os_free(freqs);
4402 return -1;
4403 } else {
4404 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4405 }
4406 }
4407 } else {
4408 for (i = 0; i < num; i++) {
4409 if (freqs[i] == freq)
4410 break;
4411 }
4412
4413 if (i == num) {
4414 if (num == wpa_s->num_multichan_concurrent) {
4415 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4416 os_free(freqs);
4417 return -1;
4418 } else {
4419 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4420 }
4421 }
4422 }
4423 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004424 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425}
4426
4427
4428static struct wpa_supplicant *
4429wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4430 int go)
4431{
4432 struct wpa_supplicant *group_wpa_s;
4433
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004434 if (!wpas_p2p_create_iface(wpa_s)) {
4435 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4436 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004437 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004438 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004439
4440 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004441 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004442 wpa_msg_global(wpa_s, MSG_ERROR,
4443 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004445 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004446 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4447 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004448 wpa_msg_global(wpa_s, MSG_ERROR,
4449 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450 wpas_p2p_remove_pending_group_interface(wpa_s);
4451 return NULL;
4452 }
4453
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004454 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4455 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004456 return group_wpa_s;
4457}
4458
4459
4460/**
4461 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4462 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4463 * @persistent_group: Whether to create a persistent group
4464 * @freq: Frequency for the group or 0 to indicate no hardcoding
4465 * Returns: 0 on success, -1 on failure
4466 *
4467 * This function creates a new P2P group with the local end as the Group Owner,
4468 * i.e., without using Group Owner Negotiation.
4469 */
4470int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004471 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472{
4473 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004474
4475 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4476 return -1;
4477
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004478 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004479 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004480 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004481
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004482 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4483 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004484 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004485
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004486 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004487 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004488 if (params.freq &&
4489 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4490 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4491 "(%u MHz) is not supported for P2P uses",
4492 params.freq);
4493 return -1;
4494 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004495 p2p_go_params(wpa_s->global->p2p, &params);
4496 params.persistent_group = persistent_group;
4497
4498 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4499 if (wpa_s == NULL)
4500 return -1;
4501 wpas_start_wps_go(wpa_s, &params, 0);
4502
4503 return 0;
4504}
4505
4506
4507static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4508 struct wpa_ssid *params, int addr_allocated)
4509{
4510 struct wpa_ssid *ssid;
4511
4512 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4513 if (wpa_s == NULL)
4514 return -1;
4515
4516 wpa_supplicant_ap_deinit(wpa_s);
4517
4518 ssid = wpa_config_add_network(wpa_s->conf);
4519 if (ssid == NULL)
4520 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004521 wpa_config_set_network_defaults(ssid);
4522 ssid->temporary = 1;
4523 ssid->proto = WPA_PROTO_RSN;
4524 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4525 ssid->group_cipher = WPA_CIPHER_CCMP;
4526 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4527 ssid->ssid = os_malloc(params->ssid_len);
4528 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004529 wpa_config_remove_network(wpa_s->conf, ssid->id);
4530 return -1;
4531 }
4532 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4533 ssid->ssid_len = params->ssid_len;
4534 ssid->p2p_group = 1;
4535 ssid->export_keys = 1;
4536 if (params->psk_set) {
4537 os_memcpy(ssid->psk, params->psk, 32);
4538 ssid->psk_set = 1;
4539 }
4540 if (params->passphrase)
4541 ssid->passphrase = os_strdup(params->passphrase);
4542
4543 wpa_supplicant_select_network(wpa_s, ssid);
4544
4545 wpa_s->show_group_started = 1;
4546
4547 return 0;
4548}
4549
4550
4551int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4552 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004553 int freq, int ht40,
4554 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004555{
4556 struct p2p_go_neg_results params;
4557 int go = 0;
4558
4559 if (ssid->disabled != 2 || ssid->ssid == NULL)
4560 return -1;
4561
4562 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4563 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4564 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4565 "already running");
4566 return 0;
4567 }
4568
4569 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004570 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571
Dmitry Shmidt04949592012-07-19 12:16:46 -07004572 wpa_s->p2p_fallback_to_go_neg = 0;
4573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004574 if (ssid->mode == WPAS_MODE_INFRA)
4575 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4576
4577 if (ssid->mode != WPAS_MODE_P2P_GO)
4578 return -1;
4579
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004580 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4581 if (freq < 0)
4582 return -1;
4583
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004584 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004585 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586
4587 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004588 params.psk_set = ssid->psk_set;
4589 if (params.psk_set)
4590 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004591 if (ssid->passphrase) {
4592 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4593 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4594 "persistent group");
4595 return -1;
4596 }
4597 os_strlcpy(params.passphrase, ssid->passphrase,
4598 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004599 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004600 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4601 params.ssid_len = ssid->ssid_len;
4602 params.persistent_group = 1;
4603
4604 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4605 if (wpa_s == NULL)
4606 return -1;
4607
4608 wpas_start_wps_go(wpa_s, &params, 0);
4609
4610 return 0;
4611}
4612
4613
4614static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4615 struct wpabuf *proberesp_ies)
4616{
4617 struct wpa_supplicant *wpa_s = ctx;
4618 if (wpa_s->ap_iface) {
4619 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004620 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4621 wpabuf_free(beacon_ies);
4622 wpabuf_free(proberesp_ies);
4623 return;
4624 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 if (beacon_ies) {
4626 wpabuf_free(hapd->p2p_beacon_ie);
4627 hapd->p2p_beacon_ie = beacon_ies;
4628 }
4629 wpabuf_free(hapd->p2p_probe_resp_ie);
4630 hapd->p2p_probe_resp_ie = proberesp_ies;
4631 } else {
4632 wpabuf_free(beacon_ies);
4633 wpabuf_free(proberesp_ies);
4634 }
4635 wpa_supplicant_ap_update_beacon(wpa_s);
4636}
4637
4638
4639static void wpas_p2p_idle_update(void *ctx, int idle)
4640{
4641 struct wpa_supplicant *wpa_s = ctx;
4642 if (!wpa_s->ap_iface)
4643 return;
4644 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004645 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646 wpas_p2p_set_group_idle_timeout(wpa_s);
4647 else
4648 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4649}
4650
4651
4652struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004653 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004654{
4655 struct p2p_group *group;
4656 struct p2p_group_config *cfg;
4657
4658 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4659 return NULL;
4660 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4661 return NULL;
4662
4663 cfg = os_zalloc(sizeof(*cfg));
4664 if (cfg == NULL)
4665 return NULL;
4666
Dmitry Shmidt04949592012-07-19 12:16:46 -07004667 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004668 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004669 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004670 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4672 if (wpa_s->max_stations &&
4673 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4674 cfg->max_clients = wpa_s->max_stations;
4675 else
4676 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004677 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4678 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004679 cfg->cb_ctx = wpa_s;
4680 cfg->ie_update = wpas_p2p_ie_update;
4681 cfg->idle_update = wpas_p2p_idle_update;
4682
4683 group = p2p_group_init(wpa_s->global->p2p, cfg);
4684 if (group == NULL)
4685 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004686 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004687 p2p_group_notif_formation_done(group);
4688 wpa_s->p2p_group = group;
4689 return group;
4690}
4691
4692
4693void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4694 int registrar)
4695{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004696 struct wpa_ssid *ssid = wpa_s->current_ssid;
4697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004698 if (!wpa_s->p2p_in_provisioning) {
4699 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4700 "provisioning not in progress");
4701 return;
4702 }
4703
Dmitry Shmidt04949592012-07-19 12:16:46 -07004704 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4705 u8 go_dev_addr[ETH_ALEN];
4706 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4707 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4708 ssid->ssid_len);
4709 /* Clear any stored provisioning info */
4710 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4711 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004713 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4714 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004715 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4716 /*
4717 * Use a separate timeout for initial data connection to
4718 * complete to allow the group to be removed automatically if
4719 * something goes wrong in this step before the P2P group idle
4720 * timeout mechanism is taken into use.
4721 */
4722 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4723 wpas_p2p_group_formation_timeout,
4724 wpa_s->parent, NULL);
4725 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726 if (wpa_s->global->p2p)
4727 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4728 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4729 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4730 wpas_group_formation_completed(wpa_s, 1);
4731}
4732
4733
Jouni Malinen75ecf522011-06-27 15:19:46 -07004734void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4735 struct wps_event_fail *fail)
4736{
4737 if (!wpa_s->p2p_in_provisioning) {
4738 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4739 "provisioning not in progress");
4740 return;
4741 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004742
4743 if (wpa_s->go_params) {
4744 p2p_clear_provisioning_info(
4745 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004746 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004747 }
4748
Jouni Malinen75ecf522011-06-27 15:19:46 -07004749 wpas_notify_p2p_wps_failed(wpa_s, fail);
4750}
4751
4752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004754 const char *config_method,
4755 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756{
4757 u16 config_methods;
4758
Dmitry Shmidt04949592012-07-19 12:16:46 -07004759 wpa_s->p2p_fallback_to_go_neg = 0;
4760 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004761 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004763 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004764 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004765 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4766 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004767 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004768 else {
4769 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004771 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004772
Dmitry Shmidt04949592012-07-19 12:16:46 -07004773 if (use == WPAS_P2P_PD_AUTO) {
4774 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4775 wpa_s->pending_pd_config_methods = config_methods;
4776 wpa_s->p2p_auto_pd = 1;
4777 wpa_s->p2p_auto_join = 0;
4778 wpa_s->pending_pd_before_join = 0;
4779 wpa_s->auto_pd_scan_retry = 0;
4780 wpas_p2p_stop_find(wpa_s);
4781 wpa_s->p2p_join_scan_count = 0;
4782 os_get_time(&wpa_s->p2p_auto_started);
4783 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4784 wpa_s->p2p_auto_started.sec,
4785 wpa_s->p2p_auto_started.usec);
4786 wpas_p2p_join_scan(wpa_s, NULL);
4787 return 0;
4788 }
4789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4791 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004792 config_methods,
4793 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004794 }
4795
4796 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4797 return -1;
4798
4799 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004800 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004801 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004802}
4803
4804
4805int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4806 char *end)
4807{
4808 return p2p_scan_result_text(ies, ies_len, buf, end);
4809}
4810
4811
4812static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4813{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004814 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004815 return;
4816
4817 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4818 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004819 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820}
4821
4822
4823int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4824 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004825 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004826 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827{
4828 wpas_p2p_clear_pending_action_tx(wpa_s);
4829 wpa_s->p2p_long_listen = 0;
4830
4831 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4832 return wpa_drv_p2p_find(wpa_s, timeout, type);
4833
Dmitry Shmidt04949592012-07-19 12:16:46 -07004834 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4835 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004836 return -1;
4837
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004838 wpa_supplicant_cancel_sched_scan(wpa_s);
4839
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004840 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004841 num_req_dev_types, req_dev_types, dev_id,
4842 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843}
4844
4845
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004846static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004847{
4848 wpas_p2p_clear_pending_action_tx(wpa_s);
4849 wpa_s->p2p_long_listen = 0;
4850 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4851 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004852 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004853
4854 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4855 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004856 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004857 }
4858
4859 if (wpa_s->global->p2p)
4860 p2p_stop_find(wpa_s->global->p2p);
4861
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004862 return 0;
4863}
4864
4865
4866void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4867{
4868 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4869 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870 wpas_p2p_remove_pending_group_interface(wpa_s);
4871}
4872
4873
4874static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4875{
4876 struct wpa_supplicant *wpa_s = eloop_ctx;
4877 wpa_s->p2p_long_listen = 0;
4878}
4879
4880
4881int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4882{
4883 int res;
4884
4885 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4886 return -1;
4887
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004888 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004889 wpas_p2p_clear_pending_action_tx(wpa_s);
4890
4891 if (timeout == 0) {
4892 /*
4893 * This is a request for unlimited Listen state. However, at
4894 * least for now, this is mapped to a Listen state for one
4895 * hour.
4896 */
4897 timeout = 3600;
4898 }
4899 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4900 wpa_s->p2p_long_listen = 0;
4901
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004902 /*
4903 * Stop previous find/listen operation to avoid trying to request a new
4904 * remain-on-channel operation while the driver is still running the
4905 * previous one.
4906 */
4907 if (wpa_s->global->p2p)
4908 p2p_stop_find(wpa_s->global->p2p);
4909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4911 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4912 wpa_s->p2p_long_listen = timeout * 1000;
4913 eloop_register_timeout(timeout, 0,
4914 wpas_p2p_long_listen_timeout,
4915 wpa_s, NULL);
4916 }
4917
4918 return res;
4919}
4920
4921
4922int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4923 u8 *buf, size_t len, int p2p_group)
4924{
4925 struct wpabuf *p2p_ie;
4926 int ret;
4927
4928 if (wpa_s->global->p2p_disabled)
4929 return -1;
4930 if (wpa_s->global->p2p == NULL)
4931 return -1;
4932 if (bss == NULL)
4933 return -1;
4934
4935 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4936 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4937 p2p_group, p2p_ie);
4938 wpabuf_free(p2p_ie);
4939
4940 return ret;
4941}
4942
4943
4944int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004945 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004946 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947{
4948 if (wpa_s->global->p2p_disabled)
4949 return 0;
4950 if (wpa_s->global->p2p == NULL)
4951 return 0;
4952
Dmitry Shmidt04949592012-07-19 12:16:46 -07004953 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4954 ie, ie_len)) {
4955 case P2P_PREQ_NOT_P2P:
4956 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4957 ssi_signal);
4958 /* fall through */
4959 case P2P_PREQ_MALFORMED:
4960 case P2P_PREQ_NOT_LISTEN:
4961 case P2P_PREQ_NOT_PROCESSED:
4962 default: /* make gcc happy */
4963 return 0;
4964 case P2P_PREQ_PROCESSED:
4965 return 1;
4966 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967}
4968
4969
4970void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4971 const u8 *sa, const u8 *bssid,
4972 u8 category, const u8 *data, size_t len, int freq)
4973{
4974 if (wpa_s->global->p2p_disabled)
4975 return;
4976 if (wpa_s->global->p2p == NULL)
4977 return;
4978
4979 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4980 freq);
4981}
4982
4983
4984void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4985{
4986 if (wpa_s->global->p2p_disabled)
4987 return;
4988 if (wpa_s->global->p2p == NULL)
4989 return;
4990
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004991 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992}
4993
4994
4995void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4996{
4997 p2p_group_deinit(wpa_s->p2p_group);
4998 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004999
5000 wpa_s->ap_configured_cb = NULL;
5001 wpa_s->ap_configured_cb_ctx = NULL;
5002 wpa_s->ap_configured_cb_data = NULL;
5003 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004}
5005
5006
5007int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5008{
5009 wpa_s->p2p_long_listen = 0;
5010
5011 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5012 return wpa_drv_p2p_reject(wpa_s, addr);
5013
5014 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5015 return -1;
5016
5017 return p2p_reject(wpa_s->global->p2p, addr);
5018}
5019
5020
5021/* Invite to reinvoke a persistent group */
5022int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005023 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005024 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025{
5026 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005027 u8 *bssid = NULL;
5028 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005029 int res;
5030
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005031 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005032 if (peer_addr)
5033 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5034 else
5035 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005036
Jouni Malinen31be0a42012-08-31 21:20:51 +03005037 wpa_s->p2p_persistent_go_freq = freq;
5038 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005039 if (ssid->mode == WPAS_MODE_P2P_GO) {
5040 role = P2P_INVITE_ROLE_GO;
5041 if (peer_addr == NULL) {
5042 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5043 "address in invitation command");
5044 return -1;
5045 }
5046 if (wpas_p2p_create_iface(wpa_s)) {
5047 if (wpas_p2p_add_group_interface(wpa_s,
5048 WPA_IF_P2P_GO) < 0) {
5049 wpa_printf(MSG_ERROR, "P2P: Failed to "
5050 "allocate a new interface for the "
5051 "group");
5052 return -1;
5053 }
5054 bssid = wpa_s->pending_interface_addr;
5055 } else
5056 bssid = wpa_s->own_addr;
5057 } else {
5058 role = P2P_INVITE_ROLE_CLIENT;
5059 peer_addr = ssid->bssid;
5060 }
5061 wpa_s->pending_invite_ssid_id = ssid->id;
5062
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005063 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005064 if (res)
5065 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005067 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5068 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5069 ssid->ssid, ssid->ssid_len,
5070 go_dev_addr, 1);
5071
5072 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5073 return -1;
5074
5075 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005076 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5077 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005078}
5079
5080
5081/* Invite to join an active group */
5082int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5083 const u8 *peer_addr, const u8 *go_dev_addr)
5084{
5085 struct wpa_global *global = wpa_s->global;
5086 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005087 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005089 int persistent;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005090 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005091 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092
Jouni Malinen31be0a42012-08-31 21:20:51 +03005093 wpa_s->p2p_persistent_go_freq = 0;
5094 wpa_s->p2p_go_ht40 = 0;
5095
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005096 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5097 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5098 break;
5099 }
5100 if (wpa_s == NULL) {
5101 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5102 return -1;
5103 }
5104
5105 ssid = wpa_s->current_ssid;
5106 if (ssid == NULL) {
5107 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5108 "invitation");
5109 return -1;
5110 }
5111
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005112 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005113 persistent = ssid->p2p_persistent_group &&
5114 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5115 ssid->ssid, ssid->ssid_len);
5116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005117 if (ssid->mode == WPAS_MODE_P2P_GO) {
5118 role = P2P_INVITE_ROLE_ACTIVE_GO;
5119 bssid = wpa_s->own_addr;
5120 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005121 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122 } else {
5123 role = P2P_INVITE_ROLE_CLIENT;
5124 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5125 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5126 "invite to current group");
5127 return -1;
5128 }
5129 bssid = wpa_s->bssid;
5130 if (go_dev_addr == NULL &&
5131 !is_zero_ether_addr(wpa_s->go_dev_addr))
5132 go_dev_addr = wpa_s->go_dev_addr;
5133 }
5134 wpa_s->parent->pending_invite_ssid_id = -1;
5135
5136 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5137 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5138 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005139 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140
5141 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5142 return -1;
5143
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005144 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005145 if (res)
5146 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005147 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005149 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005150 ssid->ssid, ssid->ssid_len, force_freq,
5151 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005152}
5153
5154
5155void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5156{
5157 struct wpa_ssid *ssid = wpa_s->current_ssid;
5158 const char *ssid_txt;
5159 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005160 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005161 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005162 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005163
Dmitry Shmidt04949592012-07-19 12:16:46 -07005164 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5165 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5166 wpa_s->parent, NULL);
5167 }
5168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005169 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005170 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005171
5172 wpa_s->show_group_started = 0;
5173
5174 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5175 os_memset(go_dev_addr, 0, ETH_ALEN);
5176 if (ssid->bssid_set)
5177 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5178 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5179 ssid->ssid_len);
5180 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5181
5182 if (wpa_s->global->p2p_group_formation == wpa_s)
5183 wpa_s->global->p2p_group_formation = NULL;
5184
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005185 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5186 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005187 if (ssid->passphrase == NULL && ssid->psk_set) {
5188 char psk[65];
5189 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005190 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5191 "%s client ssid=\"%s\" freq=%d psk=%s "
5192 "go_dev_addr=" MACSTR "%s",
5193 wpa_s->ifname, ssid_txt, freq, psk,
5194 MAC2STR(go_dev_addr),
5195 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005196 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005197 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5198 "%s client ssid=\"%s\" freq=%d "
5199 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5200 wpa_s->ifname, ssid_txt, freq,
5201 ssid->passphrase ? ssid->passphrase : "",
5202 MAC2STR(go_dev_addr),
5203 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005204 }
5205
5206 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005207 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5208 ssid, go_dev_addr);
5209 if (network_id < 0)
5210 network_id = ssid->id;
5211 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005212
5213done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005214 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005215}
5216
5217
5218int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5219 u32 interval1, u32 duration2, u32 interval2)
5220{
5221 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5222 return -1;
5223 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5224 return -1;
5225
5226 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5227 wpa_s->current_ssid == NULL ||
5228 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5229 return -1;
5230
5231 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5232 wpa_s->own_addr, wpa_s->assoc_freq,
5233 duration1, interval1, duration2, interval2);
5234}
5235
5236
5237int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5238 unsigned int interval)
5239{
5240 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5241 return -1;
5242
5243 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5244 return -1;
5245
5246 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5247}
5248
5249
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005250static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5251{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005252 if (wpa_s->current_ssid == NULL) {
5253 /*
5254 * current_ssid can be cleared when P2P client interface gets
5255 * disconnected, so assume this interface was used as P2P
5256 * client.
5257 */
5258 return 1;
5259 }
5260 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005261 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5262}
5263
5264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005265static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5266{
5267 struct wpa_supplicant *wpa_s = eloop_ctx;
5268
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005269 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005270 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5271 "disabled");
5272 return;
5273 }
5274
Dmitry Shmidt04949592012-07-19 12:16:46 -07005275 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5276 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005277 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005278}
5279
5280
5281static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5282{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005283 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005284
Dmitry Shmidt04949592012-07-19 12:16:46 -07005285 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5286 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005288 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5289 return;
5290
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005291 timeout = wpa_s->conf->p2p_group_idle;
5292 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5293 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5294 timeout = P2P_MAX_CLIENT_IDLE;
5295
5296 if (timeout == 0)
5297 return;
5298
Dmitry Shmidt04949592012-07-19 12:16:46 -07005299 if (timeout < 0) {
5300 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5301 timeout = 0; /* special client mode no-timeout */
5302 else
5303 return;
5304 }
5305
5306 if (wpa_s->p2p_in_provisioning) {
5307 /*
5308 * Use the normal group formation timeout during the
5309 * provisioning phase to avoid terminating this process too
5310 * early due to group idle timeout.
5311 */
5312 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5313 "during provisioning");
5314 return;
5315 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305316
Dmitry Shmidt04949592012-07-19 12:16:46 -07005317 if (wpa_s->show_group_started) {
5318 /*
5319 * Use the normal group formation timeout between the end of
5320 * the provisioning phase and completion of 4-way handshake to
5321 * avoid terminating this process too early due to group idle
5322 * timeout.
5323 */
5324 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5325 "while waiting for initial 4-way handshake to "
5326 "complete");
5327 return;
5328 }
5329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005330 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005331 timeout);
5332 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5333 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005334}
5335
5336
Jouni Malinen2b89da82012-08-31 22:04:41 +03005337/* Returns 1 if the interface was removed */
5338int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5339 u16 reason_code, const u8 *ie, size_t ie_len,
5340 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005341{
5342 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005343 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005344 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005345 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346
Dmitry Shmidt04949592012-07-19 12:16:46 -07005347 if (!locally_generated)
5348 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5349 ie_len);
5350
5351 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5352 wpa_s->current_ssid &&
5353 wpa_s->current_ssid->p2p_group &&
5354 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5355 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5356 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005357 if (wpas_p2p_group_delete(wpa_s,
5358 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5359 > 0)
5360 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005361 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005362
5363 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364}
5365
5366
5367void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005368 u16 reason_code, const u8 *ie, size_t ie_len,
5369 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005370{
5371 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5372 return;
5373 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5374 return;
5375
Dmitry Shmidt04949592012-07-19 12:16:46 -07005376 if (!locally_generated)
5377 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5378 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005379}
5380
5381
5382void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5383{
5384 struct p2p_data *p2p = wpa_s->global->p2p;
5385
5386 if (p2p == NULL)
5387 return;
5388
5389 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5390 return;
5391
5392 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5393 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5394
5395 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5396 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5397
5398 if (wpa_s->wps &&
5399 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5400 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5401
5402 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5403 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5404
5405 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5406 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5407 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5408 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5409 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5410 }
5411
5412 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5413 p2p_set_sec_dev_types(p2p,
5414 (void *) wpa_s->conf->sec_device_type,
5415 wpa_s->conf->num_sec_device_types);
5416
5417 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5418 int i;
5419 p2p_remove_wps_vendor_extensions(p2p);
5420 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5421 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5422 continue;
5423 p2p_add_wps_vendor_extension(
5424 p2p, wpa_s->conf->wps_vendor_ext[i]);
5425 }
5426 }
5427
5428 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5429 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5430 char country[3];
5431 country[0] = wpa_s->conf->country[0];
5432 country[1] = wpa_s->conf->country[1];
5433 country[2] = 0x04;
5434 p2p_set_country(p2p, country);
5435 }
5436
5437 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5438 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5439 wpa_s->conf->p2p_ssid_postfix ?
5440 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5441 0);
5442 }
5443
5444 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5445 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005446
5447 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5448 u8 reg_class, channel;
5449 int ret;
5450 unsigned int r;
5451 if (wpa_s->conf->p2p_listen_reg_class &&
5452 wpa_s->conf->p2p_listen_channel) {
5453 reg_class = wpa_s->conf->p2p_listen_reg_class;
5454 channel = wpa_s->conf->p2p_listen_channel;
5455 } else {
5456 reg_class = 81;
5457 /*
5458 * Pick one of the social channels randomly as the
5459 * listen channel.
5460 */
5461 os_get_random((u8 *) &r, sizeof(r));
5462 channel = 1 + (r % 3) * 5;
5463 }
5464 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5465 if (ret)
5466 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5467 "failed: %d", ret);
5468 }
5469 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5470 u8 op_reg_class, op_channel, cfg_op_channel;
5471 int ret = 0;
5472 unsigned int r;
5473 if (wpa_s->conf->p2p_oper_reg_class &&
5474 wpa_s->conf->p2p_oper_channel) {
5475 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5476 op_channel = wpa_s->conf->p2p_oper_channel;
5477 cfg_op_channel = 1;
5478 } else {
5479 op_reg_class = 81;
5480 /*
5481 * Use random operation channel from (1, 6, 11)
5482 *if no other preference is indicated.
5483 */
5484 os_get_random((u8 *) &r, sizeof(r));
5485 op_channel = 1 + (r % 3) * 5;
5486 cfg_op_channel = 0;
5487 }
5488 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5489 cfg_op_channel);
5490 if (ret)
5491 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5492 "failed: %d", ret);
5493 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005494
5495 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5496 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5497 wpa_s->conf->p2p_pref_chan) < 0) {
5498 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5499 "update failed");
5500 }
5501 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502}
5503
5504
5505int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5506 int duration)
5507{
5508 if (!wpa_s->ap_iface)
5509 return -1;
5510 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5511 duration);
5512}
5513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005514
5515int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5516{
5517 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5518 return -1;
5519 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5520 return -1;
5521
5522 wpa_s->global->cross_connection = enabled;
5523 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5524
5525 if (!enabled) {
5526 struct wpa_supplicant *iface;
5527
5528 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5529 {
5530 if (iface->cross_connect_enabled == 0)
5531 continue;
5532
5533 iface->cross_connect_enabled = 0;
5534 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005535 wpa_msg_global(iface->parent, MSG_INFO,
5536 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5537 iface->ifname,
5538 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005539 }
5540 }
5541
5542 return 0;
5543}
5544
5545
5546static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5547{
5548 struct wpa_supplicant *iface;
5549
5550 if (!uplink->global->cross_connection)
5551 return;
5552
5553 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5554 if (!iface->cross_connect_enabled)
5555 continue;
5556 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5557 0)
5558 continue;
5559 if (iface->ap_iface == NULL)
5560 continue;
5561 if (iface->cross_connect_in_use)
5562 continue;
5563
5564 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005565 wpa_msg_global(iface->parent, MSG_INFO,
5566 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5567 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005568 }
5569}
5570
5571
5572static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5573{
5574 struct wpa_supplicant *iface;
5575
5576 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5577 if (!iface->cross_connect_enabled)
5578 continue;
5579 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5580 0)
5581 continue;
5582 if (!iface->cross_connect_in_use)
5583 continue;
5584
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005585 wpa_msg_global(iface->parent, MSG_INFO,
5586 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5587 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005588 iface->cross_connect_in_use = 0;
5589 }
5590}
5591
5592
5593void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5594{
5595 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5596 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5597 wpa_s->cross_connect_disallowed)
5598 wpas_p2p_disable_cross_connect(wpa_s);
5599 else
5600 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005601 if (!wpa_s->ap_iface &&
5602 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5603 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005604}
5605
5606
5607void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5608{
5609 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005610 if (!wpa_s->ap_iface &&
5611 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5612 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005613 wpas_p2p_set_group_idle_timeout(wpa_s);
5614}
5615
5616
5617static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5618{
5619 struct wpa_supplicant *iface;
5620
5621 if (!wpa_s->global->cross_connection)
5622 return;
5623
5624 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5625 if (iface == wpa_s)
5626 continue;
5627 if (iface->drv_flags &
5628 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5629 continue;
5630 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5631 continue;
5632
5633 wpa_s->cross_connect_enabled = 1;
5634 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5635 sizeof(wpa_s->cross_connect_uplink));
5636 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5637 "%s to %s whenever uplink is available",
5638 wpa_s->ifname, wpa_s->cross_connect_uplink);
5639
5640 if (iface->ap_iface || iface->current_ssid == NULL ||
5641 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5642 iface->cross_connect_disallowed ||
5643 iface->wpa_state != WPA_COMPLETED)
5644 break;
5645
5646 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005647 wpa_msg_global(wpa_s->parent, MSG_INFO,
5648 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5649 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005650 break;
5651 }
5652}
5653
5654
5655int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5656{
5657 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5658 !wpa_s->p2p_in_provisioning)
5659 return 0; /* not P2P client operation */
5660
5661 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5662 "session overlap");
5663 if (wpa_s != wpa_s->parent)
5664 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005665 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005666 return 1;
5667}
5668
5669
5670void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5671{
5672 struct p2p_channels chan;
5673
5674 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5675 return;
5676
5677 os_memset(&chan, 0, sizeof(chan));
5678 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5679 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5680 "channel list");
5681 return;
5682 }
5683
5684 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5685}
5686
5687
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005688static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5689 struct wpa_scan_results *scan_res)
5690{
5691 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5692}
5693
5694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005695int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5696{
5697 struct wpa_global *global = wpa_s->global;
5698 int found = 0;
5699 const u8 *peer;
5700
5701 if (global->p2p == NULL)
5702 return -1;
5703
5704 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5705
5706 if (wpa_s->pending_interface_name[0] &&
5707 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5708 found = 1;
5709
5710 peer = p2p_get_go_neg_peer(global->p2p);
5711 if (peer) {
5712 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5713 MACSTR, MAC2STR(peer));
5714 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005715 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005716 }
5717
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005718 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5719 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5720 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5721 found = 1;
5722 }
5723
5724 if (wpa_s->pending_pd_before_join) {
5725 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5726 wpa_s->pending_pd_before_join = 0;
5727 found = 1;
5728 }
5729
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005730 wpas_p2p_stop_find(wpa_s);
5731
5732 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5733 if (wpa_s == global->p2p_group_formation &&
5734 (wpa_s->p2p_in_provisioning ||
5735 wpa_s->parent->pending_interface_type ==
5736 WPA_IF_P2P_CLIENT)) {
5737 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5738 "formation found - cancelling",
5739 wpa_s->ifname);
5740 found = 1;
5741 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5742 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005743 if (wpa_s->p2p_in_provisioning) {
5744 wpas_group_formation_completed(wpa_s, 0);
5745 break;
5746 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005747 wpas_p2p_group_delete(wpa_s,
5748 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005749 break;
5750 }
5751 }
5752
5753 if (!found) {
5754 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5755 return -1;
5756 }
5757
5758 return 0;
5759}
5760
5761
5762void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5763{
5764 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5765 return;
5766
5767 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5768 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005769 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770}
5771
5772
5773void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5774 int freq_24, int freq_5, int freq_overall)
5775{
5776 struct p2p_data *p2p = wpa_s->global->p2p;
5777 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5778 return;
5779 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5780}
5781
5782
5783int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5784{
5785 u8 peer[ETH_ALEN];
5786 struct p2p_data *p2p = wpa_s->global->p2p;
5787
5788 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5789 return -1;
5790
5791 if (hwaddr_aton(addr, peer))
5792 return -1;
5793
5794 return p2p_unauthorize(p2p, peer);
5795}
5796
5797
5798/**
5799 * wpas_p2p_disconnect - Disconnect from a P2P Group
5800 * @wpa_s: Pointer to wpa_supplicant data
5801 * Returns: 0 on success, -1 on failure
5802 *
5803 * This can be used to disconnect from a group in which the local end is a P2P
5804 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5805 * virtual network interface was created for this group, that interface will be
5806 * removed. Otherwise, only the configured P2P group network will be removed
5807 * from the interface.
5808 */
5809int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5810{
5811
5812 if (wpa_s == NULL)
5813 return -1;
5814
Jouni Malinen2b89da82012-08-31 22:04:41 +03005815 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5816 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005817}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005818
5819
5820int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5821{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005822 int ret;
5823
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005824 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5825 return 0;
5826
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005827 ret = p2p_in_progress(wpa_s->global->p2p);
5828 if (ret == 0) {
5829 /*
5830 * Check whether there is an ongoing WPS provisioning step (or
5831 * other parts of group formation) on another interface since
5832 * p2p_in_progress() does not report this to avoid issues for
5833 * scans during such provisioning step.
5834 */
5835 if (wpa_s->global->p2p_group_formation &&
5836 wpa_s->global->p2p_group_formation != wpa_s) {
5837 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5838 "in group formation",
5839 wpa_s->global->p2p_group_formation->ifname);
5840 ret = 1;
5841 }
5842 }
5843
5844 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005845}
5846
5847
5848void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5849 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005850{
5851 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5852 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5853 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005854 /**
5855 * Remove the network by scheduling the group formation
5856 * timeout to happen immediately. The teardown code
5857 * needs to be scheduled to run asynch later so that we
5858 * don't delete data from under ourselves unexpectedly.
5859 * Calling wpas_p2p_group_formation_timeout directly
5860 * causes a series of crashes in WPS failure scenarios.
5861 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005862 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5863 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005864 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5865 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005866 }
5867}
5868
5869
5870struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005871 const u8 *addr, const u8 *ssid,
5872 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005873{
5874 struct wpa_ssid *s;
5875 size_t i;
5876
5877 for (s = wpa_s->conf->ssid; s; s = s->next) {
5878 if (s->disabled != 2)
5879 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005880 if (ssid &&
5881 (ssid_len != s->ssid_len ||
5882 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5883 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005884 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5885 return s; /* peer is GO in the persistent group */
5886 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5887 continue;
5888 for (i = 0; i < s->num_p2p_clients; i++) {
5889 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5890 addr, ETH_ALEN) == 0)
5891 return s; /* peer is P2P client in persistent
5892 * group */
5893 }
5894 }
5895
5896 return NULL;
5897}
5898
5899
5900void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5901 const u8 *addr)
5902{
5903 if (addr == NULL)
5904 return;
5905 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5906}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005907
Dmitry Shmidt04949592012-07-19 12:16:46 -07005908
5909static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5910 int group_added)
5911{
5912 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005913 if (wpa_s->global->p2p_group_formation)
5914 group = wpa_s->global->p2p_group_formation;
5915 wpa_s = wpa_s->parent;
5916 offchannel_send_action_done(wpa_s);
5917 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005918 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005919 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5920 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5921 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5922 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5923 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005924 wpa_s->p2p_pd_before_go_neg,
5925 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005926}
5927
5928
5929int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5930{
5931 if (!wpa_s->p2p_fallback_to_go_neg ||
5932 wpa_s->p2p_in_provisioning <= 5)
5933 return 0;
5934
5935 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5936 return 0; /* peer operating as a GO */
5937
5938 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5939 "fallback to GO Negotiation");
5940 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5941
5942 return 1;
5943}
5944
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005945
5946unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5947{
5948 const char *rn, *rn2;
5949 struct wpa_supplicant *ifs;
5950
5951 if (wpa_s->wpa_state > WPA_SCANNING) {
5952 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5953 "concurrent operation",
5954 P2P_CONCURRENT_SEARCH_DELAY);
5955 return P2P_CONCURRENT_SEARCH_DELAY;
5956 }
5957
5958 if (!wpa_s->driver->get_radio_name)
5959 return 0;
5960 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5961 if (rn == NULL || rn[0] == '\0')
5962 return 0;
5963
5964 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5965 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5966 continue;
5967
5968 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5969 if (!rn2 || os_strcmp(rn, rn2) != 0)
5970 continue;
5971 if (ifs->wpa_state > WPA_SCANNING) {
5972 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5973 "delay due to concurrent operation on "
5974 "interface %s",
5975 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5976 return P2P_CONCURRENT_SEARCH_DELAY;
5977 }
5978 }
5979
5980 return 0;
5981}
5982
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005983
5984void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5985{
5986 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5987 "pending anymore (sta_scan_pending=%d "
5988 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5989 wpa_s->global->p2p_cb_on_scan_complete);
5990 wpa_s->sta_scan_pending = 0;
5991
5992 if (!wpa_s->global->p2p_cb_on_scan_complete)
5993 return;
5994 wpa_s->global->p2p_cb_on_scan_complete = 0;
5995
5996 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5997 return;
5998
5999 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6000 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6001 "continued after successful connection");
6002 p2p_increase_search_delay(wpa_s->global->p2p,
6003 wpas_p2p_search_delay(wpa_s));
6004 }
6005}
6006
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006007#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006008static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6009{
6010 struct wpa_supplicant *wpa_s = eloop_ctx;
6011
6012 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6013 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6014}
6015
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006016int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6017 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006018{
6019 struct wpa_supplicant *iface = NULL;
6020 struct p2p_data *p2p = wpa_s->global->p2p;
6021
6022 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006023 if ((iface->current_ssid) &&
6024 (iface->current_ssid->frequency != freq) &&
6025 ((iface->p2p_group_interface) ||
6026 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006027
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006028 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
6029 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
6030 /* Try to see whether we can move the GO. If it
6031 * is not possible, remove the GO interface
6032 */
6033 if (wpa_drv_switch_channel(iface, freq) == 0) {
6034 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
6035 iface->current_ssid->frequency = freq;
6036 continue;
6037 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006038 }
6039
6040 /* If GO cannot be moved or if the conflicting interface is a
6041 * P2P Client, remove the interface depending up on the connection
6042 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006043 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07006044 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006045 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006046 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006047 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006048 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
6049 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006050 /* If connection in progress is p2p connection, do not proceed for the connection */
6051 if (wpa_s == iface)
6052 return -1;
6053 else
6054 /* If connection in progress is STA connection, proceed for the connection */
6055 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006056 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006057 /* P2p connection has priority, disable the STA network*/
6058 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
6059 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6060 " id=%d", ssid->id);
6061 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6062 if (wpa_s == iface) {
6063 /* p2p connection is in progress, continue connecting...*/
6064 return 0;
6065 }
6066 else {
6067 /* STA connection is in progress, do not allow to continue */
6068 return -1;
6069 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006070 }
6071 }
6072 }
6073 return 0;
6074}
6075#endif