blob: db7cd6e6e791187b45c1a2197d0d6c486bab1d9f [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
68enum p2p_group_removal_reason {
69 P2P_GROUP_REMOVAL_UNKNOWN,
70 P2P_GROUP_REMOVAL_SILENT,
71 P2P_GROUP_REMOVAL_FORMATION_FAILED,
72 P2P_GROUP_REMOVAL_REQUESTED,
73 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
74 P2P_GROUP_REMOVAL_UNAVAILABLE,
75 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
76#ifdef ANDROID_P2P
77 P2P_GROUP_REMOVAL_FREQ_CONFLICT
78#endif
79};
80
Jouni Malinendc7b7132012-09-14 12:53:47 -070081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
83static struct wpa_supplicant *
84wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
85 int go);
86static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070087static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
89static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070090 const u8 *dev_addr, enum p2p_wps_method wps_method,
91 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
93static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
94static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
95static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080096static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
97 void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -070098static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
99 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800100static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101
102
103static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
104 struct wpa_scan_results *scan_res)
105{
106 size_t i;
107
108 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
109 return;
110
111 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
112 (int) scan_res->num);
113
114 for (i = 0; i < scan_res->num; i++) {
115 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800116 struct os_time time_tmp_age, entry_ts;
117 time_tmp_age.sec = bss->age / 1000;
118 time_tmp_age.usec = (bss->age % 1000) * 1000;
119 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700120 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800121 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 (const u8 *) (bss + 1),
123 bss->ie_len) > 0)
124 break;
125 }
126
127 p2p_scan_res_handled(wpa_s->global->p2p);
128}
129
130
131static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
132 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700133 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134{
135 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700136 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137 struct wpa_driver_scan_params params;
138 int ret;
139 struct wpabuf *wps_ie, *ies;
140 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800141 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142
143 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
144 return -1;
145
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700146 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
147 if (ifs->sta_scan_pending &&
148 wpas_p2p_in_progress(wpa_s) == 2) {
149 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
150 "pending station mode scan to be "
151 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700152 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700153 wpa_supplicant_req_scan(ifs, 0, 0);
154 return 1;
155 }
156 }
157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158 os_memset(&params, 0, sizeof(params));
159
160 /* P2P Wildcard SSID */
161 params.num_ssids = 1;
162 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
163 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
164
165 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700166 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
167 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168 num_req_dev_types, req_dev_types);
169 if (wps_ie == NULL)
170 return -1;
171
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800172 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
173 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700174 if (ies == NULL) {
175 wpabuf_free(wps_ie);
176 return -1;
177 }
178 wpabuf_put_buf(ies, wps_ie);
179 wpabuf_free(wps_ie);
180
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800181 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800183 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 params.extra_ies = wpabuf_head(ies);
185 params.extra_ies_len = wpabuf_len(ies);
186
187 switch (type) {
188 case P2P_SCAN_SOCIAL:
189 params.freqs = social_channels;
190 break;
191 case P2P_SCAN_FULL:
192 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700193 case P2P_SCAN_SPECIFIC:
194 social_channels[0] = freq;
195 social_channels[1] = 0;
196 params.freqs = social_channels;
197 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 case P2P_SCAN_SOCIAL_PLUS_ONE:
199 social_channels[3] = freq;
200 params.freqs = social_channels;
201 break;
202 }
203
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800204 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205
206 wpabuf_free(ies);
207
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800208 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700209 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
210 if (ifs->scanning ||
211 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
212 wpa_s->global->p2p_cb_on_scan_complete = 1;
213 ret = 1;
214 break;
215 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800216 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700217 } else
218 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 return ret;
221}
222
223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
225{
226 switch (p2p_group_interface) {
227 case P2P_GROUP_INTERFACE_PENDING:
228 return WPA_IF_P2P_GROUP;
229 case P2P_GROUP_INTERFACE_GO:
230 return WPA_IF_P2P_GO;
231 case P2P_GROUP_INTERFACE_CLIENT:
232 return WPA_IF_P2P_CLIENT;
233 }
234
235 return WPA_IF_P2P_GROUP;
236}
237
238
239static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
240 const u8 *ssid,
241 size_t ssid_len, int *go)
242{
243 struct wpa_ssid *s;
244
245 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
246 for (s = wpa_s->conf->ssid; s; s = s->next) {
247 if (s->disabled != 0 || !s->p2p_group ||
248 s->ssid_len != ssid_len ||
249 os_memcmp(ssid, s->ssid, ssid_len) != 0)
250 continue;
251 if (s->mode == WPAS_MODE_P2P_GO &&
252 s != wpa_s->current_ssid)
253 continue;
254 if (go)
255 *go = s->mode == WPAS_MODE_P2P_GO;
256 return wpa_s;
257 }
258 }
259
260 return NULL;
261}
262
263
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700264static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
265 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266{
267 struct wpa_ssid *ssid;
268 char *gtype;
269 const char *reason;
270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271 ssid = wpa_s->current_ssid;
272 if (ssid == NULL) {
273 /*
274 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700275 * pending P2P group interface waiting for provisioning or a
276 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 */
278 ssid = wpa_s->conf->ssid;
279 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700280 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 break;
282 ssid = ssid->next;
283 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300284 if (ssid == NULL &&
285 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
286 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700287 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
288 "not found");
289 return -1;
290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 }
292 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
293 gtype = "GO";
294 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
295 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
296 wpa_s->reassociate = 0;
297 wpa_s->disconnected = 1;
298 wpa_supplicant_deauthenticate(wpa_s,
299 WLAN_REASON_DEAUTH_LEAVING);
300 gtype = "client";
301 } else
302 gtype = "GO";
303 if (wpa_s->cross_connect_in_use) {
304 wpa_s->cross_connect_in_use = 0;
305 wpa_msg(wpa_s->parent, MSG_INFO,
306 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
307 wpa_s->ifname, wpa_s->cross_connect_uplink);
308 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700309 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310 case P2P_GROUP_REMOVAL_REQUESTED:
311 reason = " reason=REQUESTED";
312 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700313 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
314 reason = " reason=FORMATION_FAILED";
315 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
317 reason = " reason=IDLE";
318 break;
319 case P2P_GROUP_REMOVAL_UNAVAILABLE:
320 reason = " reason=UNAVAILABLE";
321 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700322 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
323 reason = " reason=GO_ENDING_SESSION";
324 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700325#ifdef ANDROID_P2P
326 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
327 reason = " reason=FREQ_CONFLICT";
328 break;
329#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330 default:
331 reason = "";
332 break;
333 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700334 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700335 wpa_msg(wpa_s->parent, MSG_INFO,
336 P2P_EVENT_GROUP_REMOVED "%s %s%s",
337 wpa_s->ifname, gtype, reason);
338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339
Dmitry Shmidt04949592012-07-19 12:16:46 -0700340 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
341 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800342 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700343 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800344 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
345 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700346 wpa_s->p2p_in_provisioning = 0;
347 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700348
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700349 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
351
352 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
353 struct wpa_global *global;
354 char *ifname;
355 enum wpa_driver_if_type type;
356 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
357 wpa_s->ifname);
358 global = wpa_s->global;
359 ifname = os_strdup(wpa_s->ifname);
360 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700361 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 wpa_s = global->ifaces;
363 if (wpa_s && ifname)
364 wpa_drv_if_remove(wpa_s, type, ifname);
365 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300366 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367 }
368
369 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
370 if (ssid && (ssid->p2p_group ||
371 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
372 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
373 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700374 if (ssid == wpa_s->current_ssid) {
375 wpa_sm_set_config(wpa_s->wpa, NULL);
376 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700378 }
379 /*
380 * Networks objects created during any P2P activities are not
381 * exposed out as they might/will confuse certain non-P2P aware
382 * applications since these network objects won't behave like
383 * regular ones.
384 *
385 * Likewise, we don't send out network removed signals for such
386 * network objects.
387 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 wpa_config_remove_network(wpa_s->conf, id);
389 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800390 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700391 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 } else {
393 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
394 "found");
395 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700396 if (wpa_s->ap_iface)
397 wpa_supplicant_ap_deinit(wpa_s);
398 else
399 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700400
401 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402}
403
404
405static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
406 u8 *go_dev_addr,
407 const u8 *ssid, size_t ssid_len)
408{
409 struct wpa_bss *bss;
410 const u8 *bssid;
411 struct wpabuf *p2p;
412 u8 group_capab;
413 const u8 *addr;
414
415 if (wpa_s->go_params)
416 bssid = wpa_s->go_params->peer_interface_addr;
417 else
418 bssid = wpa_s->bssid;
419
420 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
421 if (bss == NULL) {
422 u8 iface_addr[ETH_ALEN];
423 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
424 iface_addr) == 0)
425 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
426 }
427 if (bss == NULL) {
428 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
429 "group is persistent - BSS " MACSTR " not found",
430 MAC2STR(bssid));
431 return 0;
432 }
433
434 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
435 if (p2p == NULL) {
436 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
437 "group is persistent - BSS " MACSTR
438 " did not include P2P IE", MAC2STR(bssid));
439 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
440 (u8 *) (bss + 1), bss->ie_len);
441 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
442 ((u8 *) bss + 1) + bss->ie_len,
443 bss->beacon_ie_len);
444 return 0;
445 }
446
447 group_capab = p2p_get_group_capab(p2p);
448 addr = p2p_get_go_dev_addr(p2p);
449 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
450 "group_capab=0x%x", group_capab);
451 if (addr) {
452 os_memcpy(go_dev_addr, addr, ETH_ALEN);
453 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
454 MAC2STR(addr));
455 } else
456 os_memset(go_dev_addr, 0, ETH_ALEN);
457 wpabuf_free(p2p);
458
459 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
460 "go_dev_addr=" MACSTR,
461 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
462
463 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
464}
465
466
Jouni Malinen75ecf522011-06-27 15:19:46 -0700467static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
468 struct wpa_ssid *ssid,
469 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470{
471 struct wpa_ssid *s;
472 int changed = 0;
473
474 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
475 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
476 for (s = wpa_s->conf->ssid; s; s = s->next) {
477 if (s->disabled == 2 &&
478 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
479 s->ssid_len == ssid->ssid_len &&
480 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
481 break;
482 }
483
484 if (s) {
485 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
486 "entry");
487 if (ssid->passphrase && !s->passphrase)
488 changed = 1;
489 else if (ssid->passphrase && s->passphrase &&
490 os_strcmp(ssid->passphrase, s->passphrase) != 0)
491 changed = 1;
492 } else {
493 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
494 "entry");
495 changed = 1;
496 s = wpa_config_add_network(wpa_s->conf);
497 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700498 return -1;
499
500 /*
501 * Instead of network_added we emit persistent_group_added
502 * notification. Also to keep the defense checks in
503 * persistent_group obj registration method, we set the
504 * relevant flags in s to designate it as a persistent group.
505 */
506 s->p2p_group = 1;
507 s->p2p_persistent_group = 1;
508 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509 wpa_config_set_network_defaults(s);
510 }
511
512 s->p2p_group = 1;
513 s->p2p_persistent_group = 1;
514 s->disabled = 2;
515 s->bssid_set = 1;
516 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
517 s->mode = ssid->mode;
518 s->auth_alg = WPA_AUTH_ALG_OPEN;
519 s->key_mgmt = WPA_KEY_MGMT_PSK;
520 s->proto = WPA_PROTO_RSN;
521 s->pairwise_cipher = WPA_CIPHER_CCMP;
522 s->export_keys = 1;
523 if (ssid->passphrase) {
524 os_free(s->passphrase);
525 s->passphrase = os_strdup(ssid->passphrase);
526 }
527 if (ssid->psk_set) {
528 s->psk_set = 1;
529 os_memcpy(s->psk, ssid->psk, 32);
530 }
531 if (s->passphrase && !s->psk_set)
532 wpa_config_update_psk(s);
533 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
534 os_free(s->ssid);
535 s->ssid = os_malloc(ssid->ssid_len);
536 }
537 if (s->ssid) {
538 s->ssid_len = ssid->ssid_len;
539 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
540 }
541
542#ifndef CONFIG_NO_CONFIG_WRITE
543 if (changed && wpa_s->conf->update_config &&
544 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
545 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
546 }
547#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700548
549 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550}
551
552
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800553static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
554 const u8 *addr)
555{
556 struct wpa_ssid *ssid, *s;
557 u8 *n;
558 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700559 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800560
561 ssid = wpa_s->current_ssid;
562 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
563 !ssid->p2p_persistent_group)
564 return;
565
566 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
567 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
568 continue;
569
570 if (s->ssid_len == ssid->ssid_len &&
571 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
572 break;
573 }
574
575 if (s == NULL)
576 return;
577
578 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
579 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700580 ETH_ALEN) != 0)
581 continue;
582
583 if (i == s->num_p2p_clients - 1)
584 return; /* already the most recent entry */
585
586 /* move the entry to mark it most recent */
587 os_memmove(s->p2p_client_list + i * ETH_ALEN,
588 s->p2p_client_list + (i + 1) * ETH_ALEN,
589 (s->num_p2p_clients - i - 1) * ETH_ALEN);
590 os_memcpy(s->p2p_client_list +
591 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
592 found = 1;
593 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800594 }
595
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700596 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
597 n = os_realloc_array(s->p2p_client_list,
598 s->num_p2p_clients + 1, ETH_ALEN);
599 if (n == NULL)
600 return;
601 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
602 s->p2p_client_list = n;
603 s->num_p2p_clients++;
604 } else if (!found) {
605 /* Not enough room for an additional entry - drop the oldest
606 * entry */
607 os_memmove(s->p2p_client_list,
608 s->p2p_client_list + ETH_ALEN,
609 (s->num_p2p_clients - 1) * ETH_ALEN);
610 os_memcpy(s->p2p_client_list +
611 (s->num_p2p_clients - 1) * ETH_ALEN,
612 addr, ETH_ALEN);
613 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614
615#ifndef CONFIG_NO_CONFIG_WRITE
616 if (wpa_s->parent->conf->update_config &&
617 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
618 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
619#endif /* CONFIG_NO_CONFIG_WRITE */
620}
621
622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
624 int success)
625{
626 struct wpa_ssid *ssid;
627 const char *ssid_txt;
628 int client;
629 int persistent;
630 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700631 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632
633 /*
634 * This callback is likely called for the main interface. Update wpa_s
635 * to use the group interface if a new interface was created for the
636 * group.
637 */
638 if (wpa_s->global->p2p_group_formation)
639 wpa_s = wpa_s->global->p2p_group_formation;
640 wpa_s->global->p2p_group_formation = NULL;
641 wpa_s->p2p_in_provisioning = 0;
642
643 if (!success) {
644 wpa_msg(wpa_s->parent, MSG_INFO,
645 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700646 wpas_p2p_group_delete(wpa_s,
647 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648 return;
649 }
650
651 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
652
653 ssid = wpa_s->current_ssid;
654 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
655 ssid->mode = WPAS_MODE_P2P_GO;
656 p2p_group_notif_formation_done(wpa_s->p2p_group);
657 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
658 }
659
660 persistent = 0;
661 if (ssid) {
662 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
663 client = ssid->mode == WPAS_MODE_INFRA;
664 if (ssid->mode == WPAS_MODE_P2P_GO) {
665 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700666 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
667 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700668 } else
669 persistent = wpas_p2p_persistent_group(wpa_s,
670 go_dev_addr,
671 ssid->ssid,
672 ssid->ssid_len);
673 } else {
674 ssid_txt = "";
675 client = wpa_s->p2p_group_interface ==
676 P2P_GROUP_INTERFACE_CLIENT;
677 os_memset(go_dev_addr, 0, ETH_ALEN);
678 }
679
680 wpa_s->show_group_started = 0;
681 if (client) {
682 /*
683 * Indicate event only after successfully completed 4-way
684 * handshake, i.e., when the interface is ready for data
685 * packets.
686 */
687 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700688#ifdef ANDROID_P2P
689 /* For client Second phase of Group formation (4-way handshake) can be still pending
690 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700691 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 -0700692 wpa_s->global->p2p_group_formation = wpa_s;
693#endif
694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
696 char psk[65];
697 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
698 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
699 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
700 "%s",
701 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
702 MAC2STR(go_dev_addr),
703 persistent ? " [PERSISTENT]" : "");
704 wpas_p2p_cross_connect_setup(wpa_s);
705 wpas_p2p_set_group_idle_timeout(wpa_s);
706 } else {
707 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
708 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
709 "go_dev_addr=" MACSTR "%s",
710 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
711 ssid && ssid->passphrase ? ssid->passphrase : "",
712 MAC2STR(go_dev_addr),
713 persistent ? " [PERSISTENT]" : "");
714 wpas_p2p_cross_connect_setup(wpa_s);
715 wpas_p2p_set_group_idle_timeout(wpa_s);
716 }
717
718 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700719 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
720 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800721 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700722 network_id = ssid->id;
723 if (!client)
724 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725}
726
727
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800728static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
729 unsigned int freq,
730 const u8 *dst, const u8 *src,
731 const u8 *bssid,
732 const u8 *data, size_t data_len,
733 enum offchannel_send_action_result
734 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800736 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
739 return;
740 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
741 return;
742
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800743 switch (result) {
744 case OFFCHANNEL_SEND_ACTION_SUCCESS:
745 res = P2P_SEND_ACTION_SUCCESS;
746 break;
747 case OFFCHANNEL_SEND_ACTION_NO_ACK:
748 res = P2P_SEND_ACTION_NO_ACK;
749 break;
750 case OFFCHANNEL_SEND_ACTION_FAILED:
751 res = P2P_SEND_ACTION_FAILED;
752 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 }
754
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800755 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800757 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
758 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800759 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800760 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
761 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800763 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
764 "during p2p_connect-auto");
765 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
766 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 }
768}
769
770
771static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
772 const u8 *src, const u8 *bssid, const u8 *buf,
773 size_t len, unsigned int wait_time)
774{
775 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800776 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
777 wait_time,
778 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779}
780
781
782static void wpas_send_action_done(void *ctx)
783{
784 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800785 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786}
787
788
789static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
790 struct p2p_go_neg_results *params)
791{
792 if (wpa_s->go_params == NULL) {
793 wpa_s->go_params = os_malloc(sizeof(*params));
794 if (wpa_s->go_params == NULL)
795 return -1;
796 }
797 os_memcpy(wpa_s->go_params, params, sizeof(*params));
798 return 0;
799}
800
801
802static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
803 struct p2p_go_neg_results *res)
804{
805 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
806 MAC2STR(res->peer_interface_addr));
807 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
808 res->ssid, res->ssid_len);
809 wpa_supplicant_ap_deinit(wpa_s);
810 wpas_copy_go_neg_results(wpa_s, res);
811 if (res->wps_method == WPS_PBC)
812 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
813 else {
814 u16 dev_pw_id = DEV_PW_DEFAULT;
815 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
816 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
817 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
818 wpa_s->p2p_pin, 1, dev_pw_id);
819 }
820}
821
822
823static void p2p_go_configured(void *ctx, void *data)
824{
825 struct wpa_supplicant *wpa_s = ctx;
826 struct p2p_go_neg_results *params = data;
827 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700828 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829
830 ssid = wpa_s->current_ssid;
831 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
832 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
833 if (wpa_s->global->p2p_group_formation == wpa_s)
834 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800835 if (os_strlen(params->passphrase) > 0) {
836 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
837 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
838 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
839 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
840 ssid->frequency, params->passphrase,
841 MAC2STR(wpa_s->global->p2p_dev_addr),
842 params->persistent_group ? " [PERSISTENT]" :
843 "");
844 } else {
845 char psk[65];
846 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
847 sizeof(params->psk));
848 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
849 "%s GO ssid=\"%s\" freq=%d psk=%s "
850 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
851 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
852 ssid->frequency, psk,
853 MAC2STR(wpa_s->global->p2p_dev_addr),
854 params->persistent_group ? " [PERSISTENT]" :
855 "");
856 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700859 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700861 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700862 if (network_id < 0)
863 network_id = ssid->id;
864 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865 wpas_p2p_cross_connect_setup(wpa_s);
866 wpas_p2p_set_group_idle_timeout(wpa_s);
867 return;
868 }
869
870 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
871 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
872 params->peer_interface_addr)) {
873 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
874 "filtering");
875 return;
876 }
877 if (params->wps_method == WPS_PBC)
878 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800879 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 else if (wpa_s->p2p_pin[0])
881 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800882 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 os_free(wpa_s->go_params);
884 wpa_s->go_params = NULL;
885}
886
887
888static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
889 struct p2p_go_neg_results *params,
890 int group_formation)
891{
892 struct wpa_ssid *ssid;
893
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700894 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
895 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
896 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
897 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700899 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900
901 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700902 if (ssid == NULL) {
903 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700905 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800907 wpa_s->show_group_started = 0;
908
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 wpa_config_set_network_defaults(ssid);
910 ssid->temporary = 1;
911 ssid->p2p_group = 1;
912 ssid->p2p_persistent_group = params->persistent_group;
913 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
914 WPAS_MODE_P2P_GO;
915 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700916 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 ssid->ssid = os_zalloc(params->ssid_len + 1);
918 if (ssid->ssid) {
919 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
920 ssid->ssid_len = params->ssid_len;
921 }
922 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
923 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
924 ssid->proto = WPA_PROTO_RSN;
925 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800926 if (os_strlen(params->passphrase) > 0) {
927 ssid->passphrase = os_strdup(params->passphrase);
928 if (ssid->passphrase == NULL) {
929 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
930 "passphrase for GO");
931 wpa_config_remove_network(wpa_s->conf, ssid->id);
932 return;
933 }
934 } else
935 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800936 ssid->psk_set = params->psk_set;
937 if (ssid->psk_set)
938 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800939 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800940 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700941 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942
943 wpa_s->ap_configured_cb = p2p_go_configured;
944 wpa_s->ap_configured_cb_ctx = wpa_s;
945 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700946 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 wpa_s->reassociate = 1;
948 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700949 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
950 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 wpa_supplicant_req_scan(wpa_s, 0, 0);
952}
953
954
955static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
956 const struct wpa_supplicant *src)
957{
958 struct wpa_config *d;
959 const struct wpa_config *s;
960
961 d = dst->conf;
962 s = src->conf;
963
964#define C(n) if (s->n) d->n = os_strdup(s->n)
965 C(device_name);
966 C(manufacturer);
967 C(model_name);
968 C(model_number);
969 C(serial_number);
970 C(config_methods);
971#undef C
972
973 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
974 os_memcpy(d->sec_device_type, s->sec_device_type,
975 sizeof(d->sec_device_type));
976 d->num_sec_device_types = s->num_sec_device_types;
977
978 d->p2p_group_idle = s->p2p_group_idle;
979 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800980 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700981 d->max_num_sta = s->max_num_sta;
982 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983}
984
985
986static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
987 enum wpa_driver_if_type type)
988{
989 char ifname[120], force_ifname[120];
990
991 if (wpa_s->pending_interface_name[0]) {
992 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
993 "- skip creation of a new one");
994 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
995 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
996 "unknown?! ifname='%s'",
997 wpa_s->pending_interface_name);
998 return -1;
999 }
1000 return 0;
1001 }
1002
1003 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
1004 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001005 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1007 /* Try to avoid going over the IFNAMSIZ length limit */
1008 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1009 wpa_s->p2p_group_idx);
1010 }
1011 force_ifname[0] = '\0';
1012
1013 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1014 ifname);
1015 wpa_s->p2p_group_idx++;
1016
1017 wpa_s->pending_interface_type = type;
1018 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1019 wpa_s->pending_interface_addr, NULL) < 0) {
1020 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1021 "interface");
1022 return -1;
1023 }
1024
1025 if (force_ifname[0]) {
1026 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1027 force_ifname);
1028 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1029 sizeof(wpa_s->pending_interface_name));
1030 } else
1031 os_strlcpy(wpa_s->pending_interface_name, ifname,
1032 sizeof(wpa_s->pending_interface_name));
1033 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1034 MACSTR, wpa_s->pending_interface_name,
1035 MAC2STR(wpa_s->pending_interface_addr));
1036
1037 return 0;
1038}
1039
1040
1041static void wpas_p2p_remove_pending_group_interface(
1042 struct wpa_supplicant *wpa_s)
1043{
1044 if (!wpa_s->pending_interface_name[0] ||
1045 is_zero_ether_addr(wpa_s->pending_interface_addr))
1046 return; /* No pending virtual interface */
1047
1048 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1049 wpa_s->pending_interface_name);
1050 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1051 wpa_s->pending_interface_name);
1052 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1053 wpa_s->pending_interface_name[0] = '\0';
1054}
1055
1056
1057static struct wpa_supplicant *
1058wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1059{
1060 struct wpa_interface iface;
1061 struct wpa_supplicant *group_wpa_s;
1062
1063 if (!wpa_s->pending_interface_name[0]) {
1064 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1065 if (!wpas_p2p_create_iface(wpa_s))
1066 return NULL;
1067 /*
1068 * Something has forced us to remove the pending interface; try
1069 * to create a new one and hope for the best that we will get
1070 * the same local address.
1071 */
1072 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1073 WPA_IF_P2P_CLIENT) < 0)
1074 return NULL;
1075 }
1076
1077 os_memset(&iface, 0, sizeof(iface));
1078 iface.ifname = wpa_s->pending_interface_name;
1079 iface.driver = wpa_s->driver->name;
1080 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1081 iface.driver_param = wpa_s->conf->driver_param;
1082 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1083 if (group_wpa_s == NULL) {
1084 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1085 "wpa_supplicant interface");
1086 return NULL;
1087 }
1088 wpa_s->pending_interface_name[0] = '\0';
1089 group_wpa_s->parent = wpa_s;
1090 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1091 P2P_GROUP_INTERFACE_CLIENT;
1092 wpa_s->global->p2p_group_formation = group_wpa_s;
1093
1094 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1095
1096 return group_wpa_s;
1097}
1098
1099
1100static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1101 void *timeout_ctx)
1102{
1103 struct wpa_supplicant *wpa_s = eloop_ctx;
1104 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1105 if (wpa_s->global->p2p)
1106 p2p_group_formation_failed(wpa_s->global->p2p);
1107 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1108 wpa_drv_p2p_group_formation_failed(wpa_s);
1109 wpas_group_formation_completed(wpa_s, 0);
1110}
1111
1112
1113void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1114{
1115 struct wpa_supplicant *wpa_s = ctx;
1116
1117 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1118 wpa_drv_cancel_remain_on_channel(wpa_s);
1119 wpa_s->off_channel_freq = 0;
1120 wpa_s->roc_waiting_drv_freq = 0;
1121 }
1122
1123 if (res->status) {
1124 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1125 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001126 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 wpas_p2p_remove_pending_group_interface(wpa_s);
1128 return;
1129 }
1130
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001131 if (wpa_s->p2p_go_ht40)
1132 res->ht40 = 1;
1133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001135 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136
Dmitry Shmidt04949592012-07-19 12:16:46 -07001137 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1138 struct wpa_ssid *ssid;
1139 ssid = wpa_config_get_network(wpa_s->conf,
1140 wpa_s->p2p_persistent_id);
1141 if (ssid && ssid->disabled == 2 &&
1142 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1143 size_t len = os_strlen(ssid->passphrase);
1144 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1145 "on requested persistent group");
1146 os_memcpy(res->passphrase, ssid->passphrase, len);
1147 res->passphrase[len] = '\0';
1148 }
1149 }
1150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 if (wpa_s->create_p2p_iface) {
1152 struct wpa_supplicant *group_wpa_s =
1153 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1154 if (group_wpa_s == NULL) {
1155 wpas_p2p_remove_pending_group_interface(wpa_s);
1156 return;
1157 }
1158 if (group_wpa_s != wpa_s) {
1159 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1160 sizeof(group_wpa_s->p2p_pin));
1161 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1162 }
1163 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1164 wpa_s->pending_interface_name[0] = '\0';
1165 group_wpa_s->p2p_in_provisioning = 1;
1166
1167 if (res->role_go)
1168 wpas_start_wps_go(group_wpa_s, res, 1);
1169 else
1170 wpas_start_wps_enrollee(group_wpa_s, res);
1171 } else {
1172 wpa_s->p2p_in_provisioning = 1;
1173 wpa_s->global->p2p_group_formation = wpa_s;
1174
1175 if (res->role_go)
1176 wpas_start_wps_go(wpa_s, res, 1);
1177 else
1178 wpas_start_wps_enrollee(ctx, res);
1179 }
1180
1181 wpa_s->p2p_long_listen = 0;
1182 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1183
1184 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1185 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1186 (res->peer_config_timeout % 100) * 10000,
1187 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1188}
1189
1190
1191void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1192{
1193 struct wpa_supplicant *wpa_s = ctx;
1194 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1195 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1196
1197 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1198}
1199
1200
1201void wpas_dev_found(void *ctx, const u8 *addr,
1202 const struct p2p_peer_info *info,
1203 int new_device)
1204{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001205#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206 struct wpa_supplicant *wpa_s = ctx;
1207 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001208#define WFD_DEV_INFO_SIZE 9
1209 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001210 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001211#ifdef CONFIG_WIFI_DISPLAY
1212 if (info->wfd_subelems) {
1213 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1214 wpabuf_head(info->wfd_subelems),
1215 WFD_DEV_INFO_SIZE);
1216 }
1217#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1219 " p2p_dev_addr=" MACSTR
1220 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001221 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1223 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1224 sizeof(devtype)),
1225 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001226 info->dev_capab, info->group_capab,
1227 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229
1230 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1231}
1232
1233
1234static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1235{
1236 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001237
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001238 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1239 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1242}
1243
1244
1245static int wpas_start_listen(void *ctx, unsigned int freq,
1246 unsigned int duration,
1247 const struct wpabuf *probe_resp_ie)
1248{
1249 struct wpa_supplicant *wpa_s = ctx;
1250
1251 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1252
1253 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1254 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1255 "report received Probe Request frames");
1256 return -1;
1257 }
1258
1259 wpa_s->pending_listen_freq = freq;
1260 wpa_s->pending_listen_duration = duration;
1261
1262 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1263 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1264 "to remain on channel (%u MHz) for Listen "
1265 "state", freq);
1266 wpa_s->pending_listen_freq = 0;
1267 return -1;
1268 }
1269 wpa_s->off_channel_freq = 0;
1270 wpa_s->roc_waiting_drv_freq = freq;
1271
1272 return 0;
1273}
1274
1275
1276static void wpas_stop_listen(void *ctx)
1277{
1278 struct wpa_supplicant *wpa_s = ctx;
1279 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1280 wpa_drv_cancel_remain_on_channel(wpa_s);
1281 wpa_s->off_channel_freq = 0;
1282 wpa_s->roc_waiting_drv_freq = 0;
1283 }
1284 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1285 wpa_drv_probe_req_report(wpa_s, 0);
1286}
1287
1288
1289static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1290{
1291 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001292 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293}
1294
1295
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001296/*
1297 * DNS Header section is used only to calculate compression pointers, so the
1298 * contents of this data does not matter, but the length needs to be reserved
1299 * in the virtual packet.
1300 */
1301#define DNS_HEADER_LEN 12
1302
1303/*
1304 * 27-octet in-memory packet from P2P specification containing two implied
1305 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1306 */
1307#define P2P_SD_IN_MEMORY_LEN 27
1308
1309static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1310 u8 **spos, const u8 *end)
1311{
1312 while (*spos < end) {
1313 u8 val = ((*spos)[0] & 0xc0) >> 6;
1314 int len;
1315
1316 if (val == 1 || val == 2) {
1317 /* These are reserved values in RFC 1035 */
1318 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1319 "sequence starting with 0x%x", val);
1320 return -1;
1321 }
1322
1323 if (val == 3) {
1324 u16 offset;
1325 u8 *spos_tmp;
1326
1327 /* Offset */
1328 if (*spos + 2 > end) {
1329 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1330 "DNS offset field");
1331 return -1;
1332 }
1333
1334 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1335 if (offset >= *spos - start) {
1336 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1337 "pointer offset %u", offset);
1338 return -1;
1339 }
1340
1341 (*spos) += 2;
1342 spos_tmp = start + offset;
1343 return p2p_sd_dns_uncompress_label(upos, uend, start,
1344 &spos_tmp,
1345 *spos - 2);
1346 }
1347
1348 /* Label */
1349 len = (*spos)[0] & 0x3f;
1350 if (len == 0)
1351 return 0;
1352
1353 (*spos)++;
1354 if (*spos + len > end) {
1355 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1356 "sequence - no room for label with length "
1357 "%u", len);
1358 return -1;
1359 }
1360
1361 if (*upos + len + 2 > uend)
1362 return -2;
1363
1364 os_memcpy(*upos, *spos, len);
1365 *spos += len;
1366 *upos += len;
1367 (*upos)[0] = '.';
1368 (*upos)++;
1369 (*upos)[0] = '\0';
1370 }
1371
1372 return 0;
1373}
1374
1375
1376/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1377 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1378 * not large enough */
1379static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1380 size_t msg_len, size_t offset)
1381{
1382 /* 27-octet in-memory packet from P2P specification */
1383 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1384 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1385 u8 *tmp, *end, *spos;
1386 char *upos, *uend;
1387 int ret = 0;
1388
1389 if (buf_len < 2)
1390 return -1;
1391 if (offset > msg_len)
1392 return -1;
1393
1394 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1395 if (tmp == NULL)
1396 return -1;
1397 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1398 end = spos + msg_len;
1399 spos += offset;
1400
1401 os_memset(tmp, 0, DNS_HEADER_LEN);
1402 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1403 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1404
1405 upos = buf;
1406 uend = buf + buf_len;
1407
1408 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1409 if (ret) {
1410 os_free(tmp);
1411 return ret;
1412 }
1413
1414 if (upos == buf) {
1415 upos[0] = '.';
1416 upos[1] = '\0';
1417 } else if (upos[-1] == '.')
1418 upos[-1] = '\0';
1419
1420 os_free(tmp);
1421 return 0;
1422}
1423
1424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425static struct p2p_srv_bonjour *
1426wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1427 const struct wpabuf *query)
1428{
1429 struct p2p_srv_bonjour *bsrv;
1430 size_t len;
1431
1432 len = wpabuf_len(query);
1433 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1434 struct p2p_srv_bonjour, list) {
1435 if (len == wpabuf_len(bsrv->query) &&
1436 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1437 len) == 0)
1438 return bsrv;
1439 }
1440 return NULL;
1441}
1442
1443
1444static struct p2p_srv_upnp *
1445wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1446 const char *service)
1447{
1448 struct p2p_srv_upnp *usrv;
1449
1450 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1451 struct p2p_srv_upnp, list) {
1452 if (version == usrv->version &&
1453 os_strcmp(service, usrv->service) == 0)
1454 return usrv;
1455 }
1456 return NULL;
1457}
1458
1459
1460static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1461 u8 srv_trans_id)
1462{
1463 u8 *len_pos;
1464
1465 if (wpabuf_tailroom(resp) < 5)
1466 return;
1467
1468 /* Length (to be filled) */
1469 len_pos = wpabuf_put(resp, 2);
1470 wpabuf_put_u8(resp, srv_proto);
1471 wpabuf_put_u8(resp, srv_trans_id);
1472 /* Status Code */
1473 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1474 /* Response Data: empty */
1475 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1476}
1477
1478
1479static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1480 struct wpabuf *resp, u8 srv_trans_id)
1481{
1482 struct p2p_srv_bonjour *bsrv;
1483 u8 *len_pos;
1484
1485 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1486
1487 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1488 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1489 return;
1490 }
1491
1492 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1493 struct p2p_srv_bonjour, list) {
1494 if (wpabuf_tailroom(resp) <
1495 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1496 return;
1497 /* Length (to be filled) */
1498 len_pos = wpabuf_put(resp, 2);
1499 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1500 wpabuf_put_u8(resp, srv_trans_id);
1501 /* Status Code */
1502 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1503 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1504 wpabuf_head(bsrv->resp),
1505 wpabuf_len(bsrv->resp));
1506 /* Response Data */
1507 wpabuf_put_buf(resp, bsrv->query); /* Key */
1508 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1509 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1510 2);
1511 }
1512}
1513
1514
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001515static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1516 size_t query_len)
1517{
1518 char str_rx[256], str_srv[256];
1519
1520 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1521 return 0; /* Too short to include DNS Type and Version */
1522 if (os_memcmp(query + query_len - 3,
1523 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1524 3) != 0)
1525 return 0; /* Mismatch in DNS Type or Version */
1526 if (query_len == wpabuf_len(bsrv->query) &&
1527 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1528 return 1; /* Binary match */
1529
1530 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1531 0))
1532 return 0; /* Failed to uncompress query */
1533 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1534 wpabuf_head(bsrv->query),
1535 wpabuf_len(bsrv->query) - 3, 0))
1536 return 0; /* Failed to uncompress service */
1537
1538 return os_strcmp(str_rx, str_srv) == 0;
1539}
1540
1541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1543 struct wpabuf *resp, u8 srv_trans_id,
1544 const u8 *query, size_t query_len)
1545{
1546 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001548 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001549
1550 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1551 query, query_len);
1552 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1553 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1554 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1555 srv_trans_id);
1556 return;
1557 }
1558
1559 if (query_len == 0) {
1560 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1561 return;
1562 }
1563
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001564 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1565 struct p2p_srv_bonjour, list) {
1566 if (!match_bonjour_query(bsrv, query, query_len))
1567 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001569 if (wpabuf_tailroom(resp) <
1570 5 + query_len + wpabuf_len(bsrv->resp))
1571 return;
1572
1573 matches++;
1574
1575 /* Length (to be filled) */
1576 len_pos = wpabuf_put(resp, 2);
1577 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1578 wpabuf_put_u8(resp, srv_trans_id);
1579
1580 /* Status Code */
1581 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1582 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1583 wpabuf_head(bsrv->resp),
1584 wpabuf_len(bsrv->resp));
1585
1586 /* Response Data */
1587 wpabuf_put_data(resp, query, query_len); /* Key */
1588 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1589
1590 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1591 }
1592
1593 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1595 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001596 if (wpabuf_tailroom(resp) < 5)
1597 return;
1598
1599 /* Length (to be filled) */
1600 len_pos = wpabuf_put(resp, 2);
1601 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1602 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603
1604 /* Status Code */
1605 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1606 /* Response Data: empty */
1607 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1608 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610}
1611
1612
1613static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1614 struct wpabuf *resp, u8 srv_trans_id)
1615{
1616 struct p2p_srv_upnp *usrv;
1617 u8 *len_pos;
1618
1619 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1620
1621 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1622 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1623 return;
1624 }
1625
1626 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1627 struct p2p_srv_upnp, list) {
1628 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1629 return;
1630
1631 /* Length (to be filled) */
1632 len_pos = wpabuf_put(resp, 2);
1633 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1634 wpabuf_put_u8(resp, srv_trans_id);
1635
1636 /* Status Code */
1637 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1638 /* Response Data */
1639 wpabuf_put_u8(resp, usrv->version);
1640 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1641 usrv->service);
1642 wpabuf_put_str(resp, usrv->service);
1643 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1644 2);
1645 }
1646}
1647
1648
1649static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1650 struct wpabuf *resp, u8 srv_trans_id,
1651 const u8 *query, size_t query_len)
1652{
1653 struct p2p_srv_upnp *usrv;
1654 u8 *len_pos;
1655 u8 version;
1656 char *str;
1657 int count = 0;
1658
1659 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1660 query, query_len);
1661
1662 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1663 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1664 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1665 srv_trans_id);
1666 return;
1667 }
1668
1669 if (query_len == 0) {
1670 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1671 return;
1672 }
1673
1674 if (wpabuf_tailroom(resp) < 5)
1675 return;
1676
1677 /* Length (to be filled) */
1678 len_pos = wpabuf_put(resp, 2);
1679 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1680 wpabuf_put_u8(resp, srv_trans_id);
1681
1682 version = query[0];
1683 str = os_malloc(query_len);
1684 if (str == NULL)
1685 return;
1686 os_memcpy(str, query + 1, query_len - 1);
1687 str[query_len - 1] = '\0';
1688
1689 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1690 struct p2p_srv_upnp, list) {
1691 if (version != usrv->version)
1692 continue;
1693
1694 if (os_strcmp(str, "ssdp:all") != 0 &&
1695 os_strstr(usrv->service, str) == NULL)
1696 continue;
1697
1698 if (wpabuf_tailroom(resp) < 2)
1699 break;
1700 if (count == 0) {
1701 /* Status Code */
1702 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1703 /* Response Data */
1704 wpabuf_put_u8(resp, version);
1705 } else
1706 wpabuf_put_u8(resp, ',');
1707
1708 count++;
1709
1710 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1711 usrv->service);
1712 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1713 break;
1714 wpabuf_put_str(resp, usrv->service);
1715 }
1716 os_free(str);
1717
1718 if (count == 0) {
1719 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1720 "available");
1721 /* Status Code */
1722 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1723 /* Response Data: empty */
1724 }
1725
1726 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1727}
1728
1729
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001730#ifdef CONFIG_WIFI_DISPLAY
1731static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1732 struct wpabuf *resp, u8 srv_trans_id,
1733 const u8 *query, size_t query_len)
1734{
1735 const u8 *pos;
1736 u8 role;
1737 u8 *len_pos;
1738
1739 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1740
1741 if (!wpa_s->global->wifi_display) {
1742 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1743 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1744 srv_trans_id);
1745 return;
1746 }
1747
1748 if (query_len < 1) {
1749 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1750 "Role");
1751 return;
1752 }
1753
1754 if (wpabuf_tailroom(resp) < 5)
1755 return;
1756
1757 pos = query;
1758 role = *pos++;
1759 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1760
1761 /* TODO: role specific handling */
1762
1763 /* Length (to be filled) */
1764 len_pos = wpabuf_put(resp, 2);
1765 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1766 wpabuf_put_u8(resp, srv_trans_id);
1767 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1768
1769 while (pos < query + query_len) {
1770 if (*pos < MAX_WFD_SUBELEMS &&
1771 wpa_s->global->wfd_subelem[*pos] &&
1772 wpabuf_tailroom(resp) >=
1773 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1774 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1775 "subelement %u", *pos);
1776 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1777 }
1778 pos++;
1779 }
1780
1781 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1782}
1783#endif /* CONFIG_WIFI_DISPLAY */
1784
1785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1787 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1788{
1789 struct wpa_supplicant *wpa_s = ctx;
1790 const u8 *pos = tlvs;
1791 const u8 *end = tlvs + tlvs_len;
1792 const u8 *tlv_end;
1793 u16 slen;
1794 struct wpabuf *resp;
1795 u8 srv_proto, srv_trans_id;
1796 size_t buf_len;
1797 char *buf;
1798
1799 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1800 tlvs, tlvs_len);
1801 buf_len = 2 * tlvs_len + 1;
1802 buf = os_malloc(buf_len);
1803 if (buf) {
1804 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1805 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1806 MACSTR " %u %u %s",
1807 freq, MAC2STR(sa), dialog_token, update_indic,
1808 buf);
1809 os_free(buf);
1810 }
1811
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001812 if (wpa_s->p2p_sd_over_ctrl_iface) {
1813 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1814 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001816 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001817
1818 resp = wpabuf_alloc(10000);
1819 if (resp == NULL)
1820 return;
1821
1822 while (pos + 1 < end) {
1823 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1824 slen = WPA_GET_LE16(pos);
1825 pos += 2;
1826 if (pos + slen > end || slen < 2) {
1827 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1828 "length");
1829 wpabuf_free(resp);
1830 return;
1831 }
1832 tlv_end = pos + slen;
1833
1834 srv_proto = *pos++;
1835 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1836 srv_proto);
1837 srv_trans_id = *pos++;
1838 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1839 srv_trans_id);
1840
1841 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1842 pos, tlv_end - pos);
1843
1844
1845 if (wpa_s->force_long_sd) {
1846 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1847 "response");
1848 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1849 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1850 goto done;
1851 }
1852
1853 switch (srv_proto) {
1854 case P2P_SERV_ALL_SERVICES:
1855 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1856 "for all services");
1857 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1858 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1859 wpa_printf(MSG_DEBUG, "P2P: No service "
1860 "discovery protocols available");
1861 wpas_sd_add_proto_not_avail(
1862 resp, P2P_SERV_ALL_SERVICES,
1863 srv_trans_id);
1864 break;
1865 }
1866 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1867 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1868 break;
1869 case P2P_SERV_BONJOUR:
1870 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1871 pos, tlv_end - pos);
1872 break;
1873 case P2P_SERV_UPNP:
1874 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1875 pos, tlv_end - pos);
1876 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001877#ifdef CONFIG_WIFI_DISPLAY
1878 case P2P_SERV_WIFI_DISPLAY:
1879 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1880 pos, tlv_end - pos);
1881 break;
1882#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883 default:
1884 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1885 "protocol %u", srv_proto);
1886 wpas_sd_add_proto_not_avail(resp, srv_proto,
1887 srv_trans_id);
1888 break;
1889 }
1890
1891 pos = tlv_end;
1892 }
1893
1894done:
1895 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1896 update_indic, tlvs, tlvs_len);
1897
1898 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1899
1900 wpabuf_free(resp);
1901}
1902
1903
1904void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1905 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 size_t buf_len;
1913 char *buf;
1914
1915 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1916 tlvs, tlvs_len);
1917 if (tlvs_len > 1500) {
1918 /* TODO: better way for handling this */
1919 wpa_msg_ctrl(wpa_s, MSG_INFO,
1920 P2P_EVENT_SERV_DISC_RESP MACSTR
1921 " %u <long response: %u bytes>",
1922 MAC2STR(sa), update_indic,
1923 (unsigned int) tlvs_len);
1924 } else {
1925 buf_len = 2 * tlvs_len + 1;
1926 buf = os_malloc(buf_len);
1927 if (buf) {
1928 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1929 wpa_msg_ctrl(wpa_s, MSG_INFO,
1930 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1931 MAC2STR(sa), update_indic, buf);
1932 os_free(buf);
1933 }
1934 }
1935
1936 while (pos < end) {
1937 u8 srv_proto, srv_trans_id, status;
1938
1939 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1940 slen = WPA_GET_LE16(pos);
1941 pos += 2;
1942 if (pos + slen > end || slen < 3) {
1943 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1944 "length");
1945 return;
1946 }
1947 tlv_end = pos + slen;
1948
1949 srv_proto = *pos++;
1950 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1951 srv_proto);
1952 srv_trans_id = *pos++;
1953 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1954 srv_trans_id);
1955 status = *pos++;
1956 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1957 status);
1958
1959 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1960 pos, tlv_end - pos);
1961
1962 pos = tlv_end;
1963 }
1964
1965 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1966}
1967
1968
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001969u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1970 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971{
1972 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001973 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001975 return 0;
1976 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001977}
1978
1979
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001980u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1981 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982{
1983 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001984 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985
1986 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1987 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001988 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1990 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1991 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1992 wpabuf_put_u8(tlvs, version);
1993 wpabuf_put_str(tlvs, query);
1994 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1995 wpabuf_free(tlvs);
1996 return ret;
1997}
1998
1999
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002000#ifdef CONFIG_WIFI_DISPLAY
2001
2002static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2003 const struct wpabuf *tlvs)
2004{
2005 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2006 return 0;
2007 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2008 return 0;
2009 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2010}
2011
2012
2013#define MAX_WFD_SD_SUBELEMS 20
2014
2015static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2016 const char *subelems)
2017{
2018 u8 *len;
2019 const char *pos;
2020 int val;
2021 int count = 0;
2022
2023 len = wpabuf_put(tlvs, 2);
2024 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2025 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2026
2027 wpabuf_put_u8(tlvs, role);
2028
2029 pos = subelems;
2030 while (*pos) {
2031 val = atoi(pos);
2032 if (val >= 0 && val < 256) {
2033 wpabuf_put_u8(tlvs, val);
2034 count++;
2035 if (count == MAX_WFD_SD_SUBELEMS)
2036 break;
2037 }
2038 pos = os_strchr(pos + 1, ',');
2039 if (pos == NULL)
2040 break;
2041 pos++;
2042 }
2043
2044 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2045}
2046
2047
2048u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2049 const u8 *dst, const char *role)
2050{
2051 struct wpabuf *tlvs;
2052 u64 ret;
2053 const char *subelems;
2054 u8 id = 1;
2055
2056 subelems = os_strchr(role, ' ');
2057 if (subelems == NULL)
2058 return 0;
2059 subelems++;
2060
2061 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2062 if (tlvs == NULL)
2063 return 0;
2064
2065 if (os_strstr(role, "[source]"))
2066 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2067 if (os_strstr(role, "[pri-sink]"))
2068 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2069 if (os_strstr(role, "[sec-sink]"))
2070 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2071 if (os_strstr(role, "[source+sink]"))
2072 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2073
2074 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2075 wpabuf_free(tlvs);
2076 return ret;
2077}
2078
2079#endif /* CONFIG_WIFI_DISPLAY */
2080
2081
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002082int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083{
2084 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002085 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2087 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002088 return p2p_sd_cancel_request(wpa_s->global->p2p,
2089 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090}
2091
2092
2093void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2094 const u8 *dst, u8 dialog_token,
2095 const struct wpabuf *resp_tlvs)
2096{
2097 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2098 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2099 resp_tlvs);
2100 return;
2101 }
2102 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2103 return;
2104 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2105 resp_tlvs);
2106}
2107
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002108#ifdef ANDROID_P2P
2109void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2110#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002112#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113{
2114 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2115 wpa_drv_p2p_service_update(wpa_s);
2116 return;
2117 }
2118 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002119#ifdef ANDROID_P2P
2120 p2p_sd_service_update(wpa_s->global->p2p, action);
2121#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002123#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124}
2125
2126
2127static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2128{
2129 dl_list_del(&bsrv->list);
2130 wpabuf_free(bsrv->query);
2131 wpabuf_free(bsrv->resp);
2132 os_free(bsrv);
2133}
2134
2135
2136static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2137{
2138 dl_list_del(&usrv->list);
2139 os_free(usrv->service);
2140 os_free(usrv);
2141}
2142
2143
2144void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2145{
2146 struct p2p_srv_bonjour *bsrv, *bn;
2147 struct p2p_srv_upnp *usrv, *un;
2148
2149 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2150 struct p2p_srv_bonjour, list)
2151 wpas_p2p_srv_bonjour_free(bsrv);
2152
2153 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2154 struct p2p_srv_upnp, list)
2155 wpas_p2p_srv_upnp_free(usrv);
2156
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002157#ifdef ANDROID_P2P
2158 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2159#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002161#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162}
2163
2164
2165int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2166 struct wpabuf *query, struct wpabuf *resp)
2167{
2168 struct p2p_srv_bonjour *bsrv;
2169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170 bsrv = os_zalloc(sizeof(*bsrv));
2171 if (bsrv == NULL)
2172 return -1;
2173 bsrv->query = query;
2174 bsrv->resp = resp;
2175 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2176
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002177#ifdef ANDROID_P2P
2178 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2179#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002181#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 return 0;
2183}
2184
2185
2186int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2187 const struct wpabuf *query)
2188{
2189 struct p2p_srv_bonjour *bsrv;
2190
2191 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2192 if (bsrv == NULL)
2193 return -1;
2194 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002195#ifdef ANDROID_P2P
2196 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2197#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002198 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002199#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200 return 0;
2201}
2202
2203
2204int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2205 const char *service)
2206{
2207 struct p2p_srv_upnp *usrv;
2208
2209 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2210 return 0; /* Already listed */
2211 usrv = os_zalloc(sizeof(*usrv));
2212 if (usrv == NULL)
2213 return -1;
2214 usrv->version = version;
2215 usrv->service = os_strdup(service);
2216 if (usrv->service == NULL) {
2217 os_free(usrv);
2218 return -1;
2219 }
2220 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2221
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002222#ifdef ANDROID_P2P
2223 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2224#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002226#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227 return 0;
2228}
2229
2230
2231int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2232 const char *service)
2233{
2234 struct p2p_srv_upnp *usrv;
2235
2236 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2237 if (usrv == NULL)
2238 return -1;
2239 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002240#ifdef ANDROID_P2P
2241 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2242#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002244#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 return 0;
2246}
2247
2248
2249static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2250 const u8 *peer, const char *params,
2251 unsigned int generated_pin)
2252{
2253 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2254 MAC2STR(peer), generated_pin, params);
2255}
2256
2257
2258static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2259 const u8 *peer, const char *params)
2260{
2261 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2262 MAC2STR(peer), params);
2263}
2264
2265
2266void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2267 const u8 *dev_addr, const u8 *pri_dev_type,
2268 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002269 u8 dev_capab, u8 group_capab, const u8 *group_id,
2270 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271{
2272 struct wpa_supplicant *wpa_s = ctx;
2273 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002274 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275 u8 empty_dev_type[8];
2276 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002277 struct wpa_supplicant *group = NULL;
2278
2279 if (group_id) {
2280 for (group = wpa_s->global->ifaces; group; group = group->next)
2281 {
2282 struct wpa_ssid *s = group->current_ssid;
2283 if (s != NULL &&
2284 s->mode == WPAS_MODE_P2P_GO &&
2285 group_id_len - ETH_ALEN == s->ssid_len &&
2286 os_memcmp(group_id + ETH_ALEN, s->ssid,
2287 s->ssid_len) == 0)
2288 break;
2289 }
2290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291
2292 if (pri_dev_type == NULL) {
2293 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2294 pri_dev_type = empty_dev_type;
2295 }
2296 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2297 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002298 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 MAC2STR(dev_addr),
2300 wps_dev_type_bin2str(pri_dev_type, devtype,
2301 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002302 dev_name, supp_config_methods, dev_capab, group_capab,
2303 group ? " group=" : "",
2304 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002305 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 if (config_methods & WPS_CONFIG_DISPLAY) {
2308 generated_pin = wps_generate_pin();
2309 wpas_prov_disc_local_display(wpa_s, peer, params,
2310 generated_pin);
2311 } else if (config_methods & WPS_CONFIG_KEYPAD)
2312 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2313 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2314 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2315 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002316
2317 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2318 P2P_PROV_DISC_SUCCESS,
2319 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320}
2321
2322
2323void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2324{
2325 struct wpa_supplicant *wpa_s = ctx;
2326 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002327 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002329 if (wpa_s->pending_pd_before_join &&
2330 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2331 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2332 wpa_s->pending_pd_before_join = 0;
2333 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2334 "join-existing-group operation");
2335 wpas_p2p_join_start(wpa_s);
2336 return;
2337 }
2338
Dmitry Shmidt04949592012-07-19 12:16:46 -07002339 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2340 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2341 os_snprintf(params, sizeof(params), " peer_go=%d",
2342 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2343 else
2344 params[0] = '\0';
2345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002347 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348 else if (config_methods & WPS_CONFIG_KEYPAD) {
2349 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002350 wpas_prov_disc_local_display(wpa_s, peer, params,
2351 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002353 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2354 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002355
Jouni Malinen75ecf522011-06-27 15:19:46 -07002356 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2357 P2P_PROV_DISC_SUCCESS,
2358 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002359}
2360
2361
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002362static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2363 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002364{
2365 struct wpa_supplicant *wpa_s = ctx;
2366
Dmitry Shmidt04949592012-07-19 12:16:46 -07002367 if (wpa_s->p2p_fallback_to_go_neg) {
2368 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2369 "failed - fall back to GO Negotiation");
2370 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2371 return;
2372 }
2373
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002374 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002375 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002376 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2377 "join-existing-group operation (no ACK for PD "
2378 "Req attempts)");
2379 wpas_p2p_join_start(wpa_s);
2380 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002381 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002382
Dmitry Shmidt04949592012-07-19 12:16:46 -07002383 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2384 " p2p_dev_addr=" MACSTR " status=%d",
2385 MAC2STR(peer), status);
2386
Jouni Malinen75ecf522011-06-27 15:19:46 -07002387 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2388 status, 0, 0);
2389}
2390
2391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2393 const u8 *go_dev_addr, const u8 *ssid,
2394 size_t ssid_len, int *go, u8 *group_bssid,
2395 int *force_freq, int persistent_group)
2396{
2397 struct wpa_supplicant *wpa_s = ctx;
2398 struct wpa_ssid *s;
2399 u8 cur_bssid[ETH_ALEN];
2400 int res;
2401 struct wpa_supplicant *grp;
2402
2403 if (!persistent_group) {
2404 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2405 " to join an active group", MAC2STR(sa));
2406 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2407 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2408 == 0 ||
2409 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2410 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2411 "authorized invitation");
2412 goto accept_inv;
2413 }
2414 /*
2415 * Do not accept the invitation automatically; notify user and
2416 * request approval.
2417 */
2418 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2419 }
2420
2421 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2422 if (grp) {
2423 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2424 "running persistent group");
2425 if (*go)
2426 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2427 goto accept_inv;
2428 }
2429
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002430 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2431 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2432 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2433 "invitation to re-invoke a persistent group");
2434 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002435 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2436
2437 for (s = wpa_s->conf->ssid; s; s = s->next) {
2438 if (s->disabled == 2 &&
2439 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2440 s->ssid_len == ssid_len &&
2441 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2442 break;
2443 }
2444
2445 if (!s) {
2446 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2447 " requested reinvocation of an unknown group",
2448 MAC2STR(sa));
2449 return P2P_SC_FAIL_UNKNOWN_GROUP;
2450 }
2451
2452 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2453 *go = 1;
2454 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2455 wpa_printf(MSG_DEBUG, "P2P: The only available "
2456 "interface is already in use - reject "
2457 "invitation");
2458 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2459 }
2460 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2461 } else if (s->mode == WPAS_MODE_P2P_GO) {
2462 *go = 1;
2463 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2464 {
2465 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2466 "interface address for the group");
2467 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2468 }
2469 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2470 ETH_ALEN);
2471 }
2472
2473accept_inv:
2474 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2475 wpa_s->assoc_freq) {
2476 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2477 "the channel we are already using");
2478 *force_freq = wpa_s->assoc_freq;
2479 }
2480
2481 res = wpa_drv_shared_freq(wpa_s);
2482 if (res > 0) {
2483 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2484 "with the channel we are already using on a "
2485 "shared interface");
2486 *force_freq = res;
2487 }
2488
2489 return P2P_SC_SUCCESS;
2490}
2491
2492
2493static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2494 const u8 *ssid, size_t ssid_len,
2495 const u8 *go_dev_addr, u8 status,
2496 int op_freq)
2497{
2498 struct wpa_supplicant *wpa_s = ctx;
2499 struct wpa_ssid *s;
2500
2501 for (s = wpa_s->conf->ssid; s; s = s->next) {
2502 if (s->disabled == 2 &&
2503 s->ssid_len == ssid_len &&
2504 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2505 break;
2506 }
2507
2508 if (status == P2P_SC_SUCCESS) {
2509 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2510 " was accepted; op_freq=%d MHz",
2511 MAC2STR(sa), op_freq);
2512 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002513 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002515 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002517 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002519 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 }
2521 return;
2522 }
2523
2524 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2525 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2526 " was rejected (status %u)", MAC2STR(sa), status);
2527 return;
2528 }
2529
2530 if (!s) {
2531 if (bssid) {
2532 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2533 "sa=" MACSTR " go_dev_addr=" MACSTR
2534 " bssid=" MACSTR " unknown-network",
2535 MAC2STR(sa), MAC2STR(go_dev_addr),
2536 MAC2STR(bssid));
2537 } else {
2538 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2539 "sa=" MACSTR " go_dev_addr=" MACSTR
2540 " unknown-network",
2541 MAC2STR(sa), MAC2STR(go_dev_addr));
2542 }
2543 return;
2544 }
2545
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002546 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2547 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2548 MACSTR " persistent=%d freq=%d",
2549 MAC2STR(sa), s->id, op_freq);
2550 } else {
2551 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2552 MACSTR " persistent=%d", MAC2STR(sa), s->id);
2553 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554}
2555
2556
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002557static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2558 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559{
2560 struct wpa_supplicant *wpa_s = ctx;
2561 struct wpa_ssid *ssid;
2562
2563 if (bssid) {
2564 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2565 "status=%d " MACSTR,
2566 status, MAC2STR(bssid));
2567 } else {
2568 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2569 "status=%d ", status);
2570 }
2571 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2572
2573 if (wpa_s->pending_invite_ssid_id == -1)
2574 return; /* Invitation to active group */
2575
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002576 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2577 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2578 "invitation exchange to indicate readiness for "
2579 "re-invocation");
2580 }
2581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582 if (status != P2P_SC_SUCCESS) {
2583 wpas_p2p_remove_pending_group_interface(wpa_s);
2584 return;
2585 }
2586
2587 ssid = wpa_config_get_network(wpa_s->conf,
2588 wpa_s->pending_invite_ssid_id);
2589 if (ssid == NULL) {
2590 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2591 "data matching with invitation");
2592 return;
2593 }
2594
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002595 /*
2596 * The peer could have missed our ctrl::ack frame for Invitation
2597 * Response and continue retransmitting the frame. To reduce the
2598 * likelihood of the peer not getting successful TX status for the
2599 * Invitation Response frame, wait a short time here before starting
2600 * the persistent group so that we will remain on the current channel to
2601 * acknowledge any possible retransmission from the peer.
2602 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002603#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002604 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2605 "starting persistent group");
2606 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002607#else
2608 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2609 "starting persistent group");
2610 os_sleep(0, 100000);
2611#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002614 ssid->mode == WPAS_MODE_P2P_GO,
2615 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002616 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002617}
2618
2619
Dmitry Shmidt04949592012-07-19 12:16:46 -07002620static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2621 unsigned int freq)
2622{
2623 unsigned int i;
2624
2625 if (global->p2p_disallow_freq == NULL)
2626 return 0;
2627
2628 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2629 if (freq >= global->p2p_disallow_freq[i].min &&
2630 freq <= global->p2p_disallow_freq[i].max)
2631 return 1;
2632 }
2633
2634 return 0;
2635}
2636
2637
2638static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2639{
2640 reg->channel[reg->channels] = chan;
2641 reg->channels++;
2642}
2643
2644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2646 struct p2p_channels *chan)
2647{
2648 int i, cla = 0;
2649
2650 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2651 "band");
2652
2653 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2654 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002655 chan->reg_class[cla].channels = 0;
2656 for (i = 0; i < 11; i++) {
2657 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2658 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2659 }
2660 if (chan->reg_class[cla].channels)
2661 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662
2663 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2664 "band");
2665
2666 /* Operating class 115 - 5 GHz, channels 36-48 */
2667 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002668 chan->reg_class[cla].channels = 0;
2669 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2670 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2671 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2672 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2673 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2674 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2675 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2676 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2677 if (chan->reg_class[cla].channels)
2678 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679
2680 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2681 "band");
2682
2683 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2684 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002685 chan->reg_class[cla].channels = 0;
2686 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2687 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2688 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2689 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2690 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2691 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2692 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2693 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2694 if (chan->reg_class[cla].channels)
2695 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002696
2697 chan->reg_classes = cla;
2698 return 0;
2699}
2700
2701
2702static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2703 u16 num_modes,
2704 enum hostapd_hw_mode mode)
2705{
2706 u16 i;
2707
2708 for (i = 0; i < num_modes; i++) {
2709 if (modes[i].mode == mode)
2710 return &modes[i];
2711 }
2712
2713 return NULL;
2714}
2715
2716
Dmitry Shmidt04949592012-07-19 12:16:46 -07002717static int has_channel(struct wpa_global *global,
2718 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719{
2720 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002721 unsigned int freq;
2722
2723 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2724 chan * 5;
2725 if (wpas_p2p_disallowed_freq(global, freq))
2726 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727
2728 for (i = 0; i < mode->num_channels; i++) {
2729 if (mode->channels[i].chan == chan) {
2730 if (flags)
2731 *flags = mode->channels[i].flag;
2732 return !(mode->channels[i].flag &
2733 (HOSTAPD_CHAN_DISABLED |
2734 HOSTAPD_CHAN_PASSIVE_SCAN |
2735 HOSTAPD_CHAN_NO_IBSS |
2736 HOSTAPD_CHAN_RADAR));
2737 }
2738 }
2739
2740 return 0;
2741}
2742
2743
2744struct p2p_oper_class_map {
2745 enum hostapd_hw_mode mode;
2746 u8 op_class;
2747 u8 min_chan;
2748 u8 max_chan;
2749 u8 inc;
2750 enum { BW20, BW40PLUS, BW40MINUS } bw;
2751};
2752
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002753static struct p2p_oper_class_map op_class[] = {
2754 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002755#if 0 /* Do not enable HT40 on 2 GHz for now */
2756 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2757 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2758#endif
2759 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2760 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2761 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2762 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2763 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2764 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2765 { -1, 0, 0, 0, 0, BW20 }
2766};
2767
2768
2769static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2770 struct hostapd_hw_modes *mode,
2771 u8 channel, u8 bw)
2772{
2773 int flag;
2774
2775 if (!has_channel(wpa_s->global, mode, channel, &flag))
2776 return -1;
2777 if (bw == BW40MINUS &&
2778 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2779 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2780 return 0;
2781 if (bw == BW40PLUS &&
2782 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2783 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2784 return 0;
2785 return 1;
2786}
2787
2788
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2790 struct p2p_channels *chan)
2791{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002792 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002795 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2797 "of all supported channels; assume dualband "
2798 "support");
2799 return wpas_p2p_default_channels(wpa_s, chan);
2800 }
2801
2802 cla = 0;
2803
2804 for (op = 0; op_class[op].op_class; op++) {
2805 struct p2p_oper_class_map *o = &op_class[op];
2806 u8 ch;
2807 struct p2p_reg_class *reg = NULL;
2808
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002809 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810 if (mode == NULL)
2811 continue;
2812 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002813 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 continue;
2815 if (reg == NULL) {
2816 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2817 "class %u", o->op_class);
2818 reg = &chan->reg_class[cla];
2819 cla++;
2820 reg->reg_class = o->op_class;
2821 }
2822 reg->channel[reg->channels] = ch;
2823 reg->channels++;
2824 }
2825 if (reg) {
2826 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2827 reg->channel, reg->channels);
2828 }
2829 }
2830
2831 chan->reg_classes = cla;
2832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833 return 0;
2834}
2835
2836
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002837int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2838 struct hostapd_hw_modes *mode, u8 channel)
2839{
2840 int op, ret;
2841
2842 for (op = 0; op_class[op].op_class; op++) {
2843 struct p2p_oper_class_map *o = &op_class[op];
2844 u8 ch;
2845
2846 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2847 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2848 o->bw == BW20 || ch != channel)
2849 continue;
2850 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2851 if (ret < 0)
2852 continue;
2853 else if (ret > 0)
2854 return (o->bw == BW40MINUS) ? -1 : 1;
2855 else
2856 return 0;
2857 }
2858 }
2859 return 0;
2860}
2861
2862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2864 size_t buf_len)
2865{
2866 struct wpa_supplicant *wpa_s = ctx;
2867
2868 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2869 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2870 break;
2871 }
2872 if (wpa_s == NULL)
2873 return -1;
2874
2875 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2876}
2877
2878
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002879static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2880{
2881 struct wpa_supplicant *wpa_s = ctx;
2882
2883 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2884 struct wpa_ssid *ssid = wpa_s->current_ssid;
2885 if (ssid == NULL)
2886 continue;
2887 if (ssid->mode != WPAS_MODE_INFRA)
2888 continue;
2889 if (wpa_s->wpa_state != WPA_COMPLETED &&
2890 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2891 continue;
2892 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2893 return 1;
2894 }
2895
2896 return 0;
2897}
2898
2899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900/**
2901 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2902 * @global: Pointer to global data from wpa_supplicant_init()
2903 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2904 * Returns: 0 on success, -1 on failure
2905 */
2906int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2907{
2908 struct p2p_config p2p;
2909 unsigned int r;
2910 int i;
2911
2912 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2913 return 0;
2914
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002915 if (global->p2p)
2916 return 0;
2917
2918 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2919 struct p2p_params params;
2920
2921 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2922 os_memset(&params, 0, sizeof(params));
2923 params.dev_name = wpa_s->conf->device_name;
2924 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2925 WPS_DEV_TYPE_LEN);
2926 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2927 os_memcpy(params.sec_dev_type,
2928 wpa_s->conf->sec_device_type,
2929 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2930
2931 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2932 return -1;
2933
2934 return 0;
2935 }
2936
2937 os_memset(&p2p, 0, sizeof(p2p));
2938 p2p.msg_ctx = wpa_s;
2939 p2p.cb_ctx = wpa_s;
2940 p2p.p2p_scan = wpas_p2p_scan;
2941 p2p.send_action = wpas_send_action;
2942 p2p.send_action_done = wpas_send_action_done;
2943 p2p.go_neg_completed = wpas_go_neg_completed;
2944 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2945 p2p.dev_found = wpas_dev_found;
2946 p2p.dev_lost = wpas_dev_lost;
2947 p2p.start_listen = wpas_start_listen;
2948 p2p.stop_listen = wpas_stop_listen;
2949 p2p.send_probe_resp = wpas_send_probe_resp;
2950 p2p.sd_request = wpas_sd_request;
2951 p2p.sd_response = wpas_sd_response;
2952 p2p.prov_disc_req = wpas_prov_disc_req;
2953 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002954 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002955 p2p.invitation_process = wpas_invitation_process;
2956 p2p.invitation_received = wpas_invitation_received;
2957 p2p.invitation_result = wpas_invitation_result;
2958 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002959 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960
2961 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002962 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002963 p2p.dev_name = wpa_s->conf->device_name;
2964 p2p.manufacturer = wpa_s->conf->manufacturer;
2965 p2p.model_name = wpa_s->conf->model_name;
2966 p2p.model_number = wpa_s->conf->model_number;
2967 p2p.serial_number = wpa_s->conf->serial_number;
2968 if (wpa_s->wps) {
2969 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2970 p2p.config_methods = wpa_s->wps->config_methods;
2971 }
2972
2973 if (wpa_s->conf->p2p_listen_reg_class &&
2974 wpa_s->conf->p2p_listen_channel) {
2975 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2976 p2p.channel = wpa_s->conf->p2p_listen_channel;
2977 } else {
2978 p2p.reg_class = 81;
2979 /*
2980 * Pick one of the social channels randomly as the listen
2981 * channel.
2982 */
2983 os_get_random((u8 *) &r, sizeof(r));
2984 p2p.channel = 1 + (r % 3) * 5;
2985 }
2986 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2987
2988 if (wpa_s->conf->p2p_oper_reg_class &&
2989 wpa_s->conf->p2p_oper_channel) {
2990 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2991 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2992 p2p.cfg_op_channel = 1;
2993 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2994 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2995
2996 } else {
2997 p2p.op_reg_class = 81;
2998 /*
2999 * Use random operation channel from (1, 6, 11) if no other
3000 * preference is indicated.
3001 */
3002 os_get_random((u8 *) &r, sizeof(r));
3003 p2p.op_channel = 1 + (r % 3) * 5;
3004 p2p.cfg_op_channel = 0;
3005 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3006 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3007 }
3008 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3009 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3010 p2p.country[2] = 0x04;
3011 } else
3012 os_memcpy(p2p.country, "XX\x04", 3);
3013
3014 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3015 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3016 "channel list");
3017 return -1;
3018 }
3019
3020 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3021 WPS_DEV_TYPE_LEN);
3022
3023 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3024 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3025 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3026
3027 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3028 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3029
3030 p2p.max_peers = 100;
3031
3032 if (wpa_s->conf->p2p_ssid_postfix) {
3033 p2p.ssid_postfix_len =
3034 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3035 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3036 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3037 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3038 p2p.ssid_postfix_len);
3039 }
3040
3041 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3042
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003043 p2p.max_listen = wpa_s->max_remain_on_chan;
3044
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003045#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003046 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003047 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003048 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3049 } else {
3050 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003051 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003052 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3053 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003054#endif
3055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 global->p2p = p2p_init(&p2p);
3057 if (global->p2p == NULL)
3058 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003059 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060
3061 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3062 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3063 continue;
3064 p2p_add_wps_vendor_extension(
3065 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3066 }
3067
3068 return 0;
3069}
3070
3071
3072/**
3073 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3074 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3075 *
3076 * This function deinitialize per-interface P2P data.
3077 */
3078void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3079{
3080 if (wpa_s->driver && wpa_s->drv_priv)
3081 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003082
3083 if (wpa_s->go_params) {
3084 /* Clear any stored provisioning info */
3085 p2p_clear_provisioning_info(
3086 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003087 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003088 }
3089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 os_free(wpa_s->go_params);
3091 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3093 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3094 wpa_s->p2p_long_listen = 0;
3095 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3096 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3097 wpas_p2p_remove_pending_group_interface(wpa_s);
3098
3099 /* TODO: remove group interface from the driver if this wpa_s instance
3100 * is on top of a P2P group interface */
3101}
3102
3103
3104/**
3105 * wpas_p2p_deinit_global - Deinitialize global P2P module
3106 * @global: Pointer to global data from wpa_supplicant_init()
3107 *
3108 * This function deinitializes the global (per device) P2P module.
3109 */
3110void wpas_p2p_deinit_global(struct wpa_global *global)
3111{
3112 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003114 wpa_s = global->ifaces;
3115 if (wpa_s)
3116 wpas_p2p_service_flush(wpa_s);
3117
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118 if (global->p2p == NULL)
3119 return;
3120
3121 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3123 wpa_s = wpa_s->next;
3124 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003125 tmp = global->ifaces;
3126 while (tmp &&
3127 (tmp == wpa_s ||
3128 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3129 tmp = tmp->next;
3130 }
3131 if (tmp == NULL)
3132 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003133 /* Disconnect from the P2P group and deinit the interface */
3134 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003135 }
3136
3137 /*
3138 * Deinit GO data on any possibly remaining interface (if main
3139 * interface is used as GO).
3140 */
3141 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3142 if (wpa_s->ap_iface)
3143 wpas_p2p_group_deinit(wpa_s);
3144 }
3145
3146 p2p_deinit(global->p2p);
3147 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003148 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003149}
3150
3151
3152static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3153{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003154 if (wpa_s->conf->p2p_no_group_iface)
3155 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 if (wpa_s->drv_flags &
3157 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3158 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3159 return 1; /* P2P group requires a new interface in every case
3160 */
3161 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3162 return 0; /* driver does not support concurrent operations */
3163 if (wpa_s->global->ifaces->next)
3164 return 1; /* more that one interface already in use */
3165 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3166 return 1; /* this interface is already in use */
3167 return 0;
3168}
3169
3170
3171static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3172 const u8 *peer_addr,
3173 enum p2p_wps_method wps_method,
3174 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003175 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003176 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003177{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003178 if (persistent_group && wpa_s->conf->persistent_reconnect)
3179 persistent_group = 2;
3180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3182 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3183 go_intent, own_interface_addr,
3184 force_freq, persistent_group);
3185 }
3186
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003187 /*
3188 * Increase GO config timeout if HT40 is used since it takes some time
3189 * to scan channels for coex purposes before the BSS can be started.
3190 */
3191 p2p_set_config_timeout(wpa_s->global->p2p,
3192 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003194 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3195 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003196 persistent_group, ssid ? ssid->ssid : NULL,
3197 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003198 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199}
3200
3201
3202static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3203 const u8 *peer_addr,
3204 enum p2p_wps_method wps_method,
3205 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003206 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003207 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003208{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003209 if (persistent_group && wpa_s->conf->persistent_reconnect)
3210 persistent_group = 2;
3211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3213 return -1;
3214
3215 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3216 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003217 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003218 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219}
3220
3221
3222static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3223{
3224 wpa_s->p2p_join_scan_count++;
3225 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3226 wpa_s->p2p_join_scan_count);
3227 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3228 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3229 " for join operationg - stop join attempt",
3230 MAC2STR(wpa_s->pending_join_iface_addr));
3231 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003232 if (wpa_s->p2p_auto_pd) {
3233 wpa_s->p2p_auto_pd = 0;
3234 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3235 " p2p_dev_addr=" MACSTR " status=N/A",
3236 MAC2STR(wpa_s->pending_join_dev_addr));
3237 return;
3238 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239 wpa_msg(wpa_s->parent, MSG_INFO,
3240 P2P_EVENT_GROUP_FORMATION_FAILURE);
3241 }
3242}
3243
3244
Dmitry Shmidt04949592012-07-19 12:16:46 -07003245static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3246{
3247 struct wpa_supplicant *iface;
3248 int shared_freq;
3249 u8 bssid[ETH_ALEN];
3250
3251 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3252 return 0;
3253
3254 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3255 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3256 continue;
3257 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3258 continue;
3259 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3260 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3261 shared_freq = iface->current_ssid->frequency;
3262 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3263 shared_freq = iface->assoc_freq;
3264 else
3265 shared_freq = 0;
3266
3267 if (shared_freq && freq != shared_freq) {
3268 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3269 "connected on %d MHz - new connection on "
3270 "%d MHz", iface->ifname, shared_freq, freq);
3271 return 1;
3272 }
3273 }
3274
3275 shared_freq = wpa_drv_shared_freq(wpa_s);
3276 if (shared_freq > 0 && shared_freq != freq) {
3277 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3278 "virtual interface connected on %d MHz - new "
3279 "connection on %d MHz", shared_freq, freq);
3280 return 1;
3281 }
3282
3283 return 0;
3284}
3285
3286
3287static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3288 const u8 *peer_dev_addr)
3289{
3290 struct wpa_bss *bss;
3291 int updated;
3292
3293 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3294 if (bss == NULL)
3295 return -1;
3296 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3297 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3298 "last scan");
3299 return 0;
3300 }
3301
3302 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3303 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3304 "%ld.%06ld (%supdated in last scan)",
3305 bss->last_update.sec, bss->last_update.usec,
3306 updated ? "": "not ");
3307
3308 return updated;
3309}
3310
3311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003312static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3313 struct wpa_scan_results *scan_res)
3314{
3315 struct wpa_bss *bss;
3316 int freq;
3317 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003319 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3320
3321 if (wpa_s->global->p2p_disabled)
3322 return;
3323
Dmitry Shmidt04949592012-07-19 12:16:46 -07003324 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3325 scan_res ? (int) scan_res->num : -1,
3326 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003327
3328 if (scan_res)
3329 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3330
Dmitry Shmidt04949592012-07-19 12:16:46 -07003331 if (wpa_s->p2p_auto_pd) {
3332 int join = wpas_p2p_peer_go(wpa_s,
3333 wpa_s->pending_join_dev_addr);
3334 if (join == 0 &&
3335 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3336 wpa_s->auto_pd_scan_retry++;
3337 bss = wpa_bss_get_bssid(wpa_s,
3338 wpa_s->pending_join_dev_addr);
3339 if (bss) {
3340 freq = bss->freq;
3341 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3342 "the peer " MACSTR " at %d MHz",
3343 wpa_s->auto_pd_scan_retry,
3344 MAC2STR(wpa_s->
3345 pending_join_dev_addr),
3346 freq);
3347 wpas_p2p_join_scan_req(wpa_s, freq);
3348 return;
3349 }
3350 }
3351
3352 if (join < 0)
3353 join = 0;
3354
3355 wpa_s->p2p_auto_pd = 0;
3356 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3357 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3358 MAC2STR(wpa_s->pending_join_dev_addr), join);
3359 if (p2p_prov_disc_req(wpa_s->global->p2p,
3360 wpa_s->pending_join_dev_addr,
3361 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003362 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003363 wpa_s->p2p_auto_pd = 0;
3364 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3365 " p2p_dev_addr=" MACSTR " status=N/A",
3366 MAC2STR(wpa_s->pending_join_dev_addr));
3367 }
3368 return;
3369 }
3370
3371 if (wpa_s->p2p_auto_join) {
3372 int join = wpas_p2p_peer_go(wpa_s,
3373 wpa_s->pending_join_dev_addr);
3374 if (join < 0) {
3375 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3376 "running a GO -> use GO Negotiation");
3377 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3378 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3379 wpa_s->p2p_persistent_group, 0, 0, 0,
3380 wpa_s->p2p_go_intent,
3381 wpa_s->p2p_connect_freq,
3382 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003383 wpa_s->p2p_pd_before_go_neg,
3384 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003385 return;
3386 }
3387
3388 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3389 "try to join the group", join ? "" :
3390 " in older scan");
3391 if (!join)
3392 wpa_s->p2p_fallback_to_go_neg = 1;
3393 }
3394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3396 wpa_s->pending_join_iface_addr);
3397 if (freq < 0 &&
3398 p2p_get_interface_addr(wpa_s->global->p2p,
3399 wpa_s->pending_join_dev_addr,
3400 iface_addr) == 0 &&
3401 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3402 {
3403 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3404 "address for join from " MACSTR " to " MACSTR
3405 " based on newly discovered P2P peer entry",
3406 MAC2STR(wpa_s->pending_join_iface_addr),
3407 MAC2STR(iface_addr));
3408 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3409 ETH_ALEN);
3410
3411 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3412 wpa_s->pending_join_iface_addr);
3413 }
3414 if (freq >= 0) {
3415 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3416 "from P2P peer table: %d MHz", freq);
3417 }
3418 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3419 if (bss) {
3420 freq = bss->freq;
3421 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3422 "from BSS table: %d MHz", freq);
3423 }
3424 if (freq > 0) {
3425 u16 method;
3426
Dmitry Shmidt04949592012-07-19 12:16:46 -07003427 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3428 wpa_msg(wpa_s->parent, MSG_INFO,
3429 P2P_EVENT_GROUP_FORMATION_FAILURE
3430 "reason=FREQ_CONFLICT");
3431 return;
3432 }
3433
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3435 "prior to joining an existing group (GO " MACSTR
3436 " freq=%u MHz)",
3437 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3438 wpa_s->pending_pd_before_join = 1;
3439
3440 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 case WPS_PIN_DISPLAY:
3442 method = WPS_CONFIG_KEYPAD;
3443 break;
3444 case WPS_PIN_KEYPAD:
3445 method = WPS_CONFIG_DISPLAY;
3446 break;
3447 case WPS_PBC:
3448 method = WPS_CONFIG_PUSHBUTTON;
3449 break;
3450 default:
3451 method = 0;
3452 break;
3453 }
3454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003455 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3456 wpa_s->pending_join_dev_addr) ==
3457 method)) {
3458 /*
3459 * We have already performed provision discovery for
3460 * joining the group. Proceed directly to join
3461 * operation without duplicated provision discovery. */
3462 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3463 "with " MACSTR " already done - proceed to "
3464 "join",
3465 MAC2STR(wpa_s->pending_join_dev_addr));
3466 wpa_s->pending_pd_before_join = 0;
3467 goto start;
3468 }
3469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003470 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003471 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003472 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003473 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3474 "Discovery Request before joining an "
3475 "existing group");
3476 wpa_s->pending_pd_before_join = 0;
3477 goto start;
3478 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003479 return;
3480 }
3481
3482 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3483 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3484 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3485 wpas_p2p_check_join_scan_limit(wpa_s);
3486 return;
3487
3488start:
3489 /* Start join operation immediately */
3490 wpas_p2p_join_start(wpa_s);
3491}
3492
3493
Dmitry Shmidt04949592012-07-19 12:16:46 -07003494static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003496 int ret;
3497 struct wpa_driver_scan_params params;
3498 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003499 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003500 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003501#ifdef ANDROID_P2P
3502 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003503
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003504 /* If freq is not provided, check the operating freq of the GO and do a
3505 * a directed scan to save time
3506 */
3507 if(!freq) {
3508 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3509 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3510 }
3511#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 os_memset(&params, 0, sizeof(params));
3513
3514 /* P2P Wildcard SSID */
3515 params.num_ssids = 1;
3516 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3517 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3518
3519 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003520 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3521 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3522 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523 if (wps_ie == NULL) {
3524 wpas_p2p_scan_res_join(wpa_s, NULL);
3525 return;
3526 }
3527
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003528 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3529 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003530 if (ies == NULL) {
3531 wpabuf_free(wps_ie);
3532 wpas_p2p_scan_res_join(wpa_s, NULL);
3533 return;
3534 }
3535 wpabuf_put_buf(ies, wps_ie);
3536 wpabuf_free(wps_ie);
3537
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003538 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003540 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003541 params.extra_ies = wpabuf_head(ies);
3542 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003543 if (freq > 0) {
3544 freqs[0] = freq;
3545 params.freqs = freqs;
3546 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547
3548 /*
3549 * Run a scan to update BSS table and start Provision Discovery once
3550 * the new scan results become available.
3551 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003552 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003553 if (!ret)
3554 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003555
3556 wpabuf_free(ies);
3557
3558 if (ret) {
3559 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3560 "try again later");
3561 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3562 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3563 wpas_p2p_check_join_scan_limit(wpa_s);
3564 }
3565}
3566
3567
Dmitry Shmidt04949592012-07-19 12:16:46 -07003568static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3569{
3570 struct wpa_supplicant *wpa_s = eloop_ctx;
3571 wpas_p2p_join_scan_req(wpa_s, 0);
3572}
3573
3574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003576 const u8 *dev_addr, enum p2p_wps_method wps_method,
3577 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003578{
3579 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003580 MACSTR " dev " MACSTR ")%s",
3581 MAC2STR(iface_addr), MAC2STR(dev_addr),
3582 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583
Dmitry Shmidt04949592012-07-19 12:16:46 -07003584 wpa_s->p2p_auto_pd = 0;
3585 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3587 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3588 wpa_s->pending_join_wps_method = wps_method;
3589
3590 /* Make sure we are not running find during connection establishment */
3591 wpas_p2p_stop_find(wpa_s);
3592
3593 wpa_s->p2p_join_scan_count = 0;
3594 wpas_p2p_join_scan(wpa_s, NULL);
3595 return 0;
3596}
3597
3598
3599static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3600{
3601 struct wpa_supplicant *group;
3602 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003603 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604
3605 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3606 if (group == NULL)
3607 return -1;
3608 if (group != wpa_s) {
3609 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3610 sizeof(group->p2p_pin));
3611 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003612 } else {
3613 /*
3614 * Need to mark the current interface for p2p_group_formation
3615 * when a separate group interface is not used. This is needed
3616 * to allow p2p_cancel stop a pending p2p_connect-join.
3617 * wpas_p2p_init_group_interface() addresses this for the case
3618 * where a separate group interface is used.
3619 */
3620 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003621 }
3622
3623 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003624 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625
3626 os_memset(&res, 0, sizeof(res));
3627 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3628 ETH_ALEN);
3629 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003630 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3631 if (bss) {
3632 res.freq = bss->freq;
3633 res.ssid_len = bss->ssid_len;
3634 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3635 }
3636
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003637 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3638 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3639 "starting client");
3640 wpa_drv_cancel_remain_on_channel(wpa_s);
3641 wpa_s->off_channel_freq = 0;
3642 wpa_s->roc_waiting_drv_freq = 0;
3643 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003644 wpas_start_wps_enrollee(group, &res);
3645
3646 /*
3647 * Allow a longer timeout for join-a-running-group than normal 15
3648 * second group formation timeout since the GO may not have authorized
3649 * our connection yet.
3650 */
3651 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3652 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3653 wpa_s, NULL);
3654
3655 return 0;
3656}
3657
3658
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003659static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3660 int *force_freq, int *pref_freq,
3661 int *oper_freq)
3662{
3663 if (freq > 0) {
3664 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3665 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3666 "(%u MHz) is not supported for P2P uses",
3667 freq);
3668 return -3;
3669 }
3670
3671 if (*oper_freq > 0 && freq != *oper_freq &&
3672 !(wpa_s->drv_flags &
3673 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3674 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3675 "on %u MHz while connected on another "
3676 "channel (%u MHz)", freq, *oper_freq);
3677 return -2;
3678 }
3679 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3680 "requested channel (%u MHz)", freq);
3681 *force_freq = freq;
3682 } else if (*oper_freq > 0 &&
3683 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3684 if (!(wpa_s->drv_flags &
3685 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3686 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3687 "while connected on non-P2P supported "
3688 "channel (%u MHz)", *oper_freq);
3689 return -2;
3690 }
3691 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3692 "(%u MHz) not available for P2P - try to use "
3693 "another channel", *oper_freq);
3694 *force_freq = 0;
3695 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3696 (wpa_s->drv_flags &
3697 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3698 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3699 "are already using (%u MHz) on another interface",
3700 *oper_freq);
3701 *pref_freq = *oper_freq;
3702 } else if (*oper_freq > 0) {
3703 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3704 "channel we are already using (%u MHz) on another "
3705 "interface", *oper_freq);
3706 *force_freq = *oper_freq;
3707 }
3708
3709 return 0;
3710}
3711
3712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003713/**
3714 * wpas_p2p_connect - Request P2P Group Formation to be started
3715 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3716 * @peer_addr: Address of the peer P2P Device
3717 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3718 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003719 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720 * @join: Whether to join an existing group (as a client) instead of starting
3721 * Group Owner negotiation; @peer_addr is BSSID in that case
3722 * @auth: Whether to only authorize the connection instead of doing that and
3723 * initiating Group Owner negotiation
3724 * @go_intent: GO Intent or -1 to use default
3725 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003726 * @persistent_id: Persistent group credentials to use for forcing GO
3727 * parameters or -1 to generate new values (SSID/passphrase)
3728 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3729 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003730 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003731 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3732 * failure, -2 on failure due to channel not currently available,
3733 * -3 if forced channel is not supported
3734 */
3735int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3736 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003737 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003738 int go_intent, int freq, int persistent_id, int pd,
3739 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003741 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003742 u8 bssid[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003743 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003745 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003746 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747
3748 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3749 return -1;
3750
Dmitry Shmidt04949592012-07-19 12:16:46 -07003751 if (persistent_id >= 0) {
3752 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3753 if (ssid == NULL || ssid->disabled != 2 ||
3754 ssid->mode != WPAS_MODE_P2P_GO)
3755 return -1;
3756 }
3757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003758 if (go_intent < 0)
3759 go_intent = wpa_s->conf->p2p_go_intent;
3760
3761 if (!auth)
3762 wpa_s->p2p_long_listen = 0;
3763
3764 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003765 wpa_s->p2p_persistent_group = !!persistent_group;
3766 wpa_s->p2p_persistent_id = persistent_id;
3767 wpa_s->p2p_go_intent = go_intent;
3768 wpa_s->p2p_connect_freq = freq;
3769 wpa_s->p2p_fallback_to_go_neg = 0;
3770 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003771 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772
3773 if (pin)
3774 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3775 else if (wps_method == WPS_PIN_DISPLAY) {
3776 ret = wps_generate_pin();
3777 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3778 ret);
3779 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3780 wpa_s->p2p_pin);
3781 } else
3782 wpa_s->p2p_pin[0] = '\0';
3783
Dmitry Shmidt04949592012-07-19 12:16:46 -07003784 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3786 if (auth) {
3787 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3788 "connect a running group from " MACSTR,
3789 MAC2STR(peer_addr));
3790 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3791 return ret;
3792 }
3793 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3794 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3795 iface_addr) < 0) {
3796 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3797 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3798 dev_addr);
3799 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003800 if (auto_join) {
3801 os_get_time(&wpa_s->p2p_auto_started);
3802 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3803 "%ld.%06ld",
3804 wpa_s->p2p_auto_started.sec,
3805 wpa_s->p2p_auto_started.usec);
3806 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003807 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003808 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3809 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003810 return -1;
3811 return ret;
3812 }
3813
3814 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003815 wpa_s->assoc_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003816 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003817 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003818 oper_freq = wpa_drv_shared_freq(wpa_s);
3819 if (oper_freq < 0)
3820 oper_freq = 0;
3821 }
3822
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003823 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
3824 &oper_freq);
3825 if (res)
3826 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827
3828 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3829
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003830 if (wpa_s->create_p2p_iface) {
3831 /* Prepare to add a new interface for the group */
3832 iftype = WPA_IF_P2P_GROUP;
3833 if (go_intent == 15)
3834 iftype = WPA_IF_P2P_GO;
3835 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3836 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3837 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003839 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003840
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003841 if_addr = wpa_s->pending_interface_addr;
3842 } else
3843 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844
3845 if (auth) {
3846 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003847 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003848 force_freq, persistent_group, ssid,
3849 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003850 return -1;
3851 return ret;
3852 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003853
3854 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3855 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003856 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003857 if (wpa_s->create_p2p_iface)
3858 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003859 return -1;
3860 }
3861 return ret;
3862}
3863
3864
3865/**
3866 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3867 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3868 * @freq: Frequency of the channel in MHz
3869 * @duration: Duration of the stay on the channel in milliseconds
3870 *
3871 * This callback is called when the driver indicates that it has started the
3872 * requested remain-on-channel duration.
3873 */
3874void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3875 unsigned int freq, unsigned int duration)
3876{
3877 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3878 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3880 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3881 wpa_s->pending_listen_duration);
3882 wpa_s->pending_listen_freq = 0;
3883 }
3884}
3885
3886
3887static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3888 unsigned int timeout)
3889{
3890 /* Limit maximum Listen state time based on driver limitation. */
3891 if (timeout > wpa_s->max_remain_on_chan)
3892 timeout = wpa_s->max_remain_on_chan;
3893
3894 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3895 return wpa_drv_p2p_listen(wpa_s, timeout);
3896
3897 return p2p_listen(wpa_s->global->p2p, timeout);
3898}
3899
3900
3901/**
3902 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3903 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3904 * @freq: Frequency of the channel in MHz
3905 *
3906 * This callback is called when the driver indicates that a remain-on-channel
3907 * operation has been completed, i.e., the duration on the requested channel
3908 * has timed out.
3909 */
3910void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3911 unsigned int freq)
3912{
3913 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3914 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003915 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003916 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3917 return;
3918 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3919 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003920 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921 return;
3922 if (wpa_s->p2p_long_listen > 0)
3923 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3924 if (wpa_s->p2p_long_listen > 0) {
3925 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3926 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3927 }
3928}
3929
3930
3931/**
3932 * wpas_p2p_group_remove - Remove a P2P group
3933 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3934 * @ifname: Network interface name of the group interface or "*" to remove all
3935 * groups
3936 * Returns: 0 on success, -1 on failure
3937 *
3938 * This function is used to remove a P2P group. This can be used to disconnect
3939 * from a group in which the local end is a P2P Client or to end a P2P Group in
3940 * case the local end is the Group Owner. If a virtual network interface was
3941 * created for this group, that interface will be removed. Otherwise, only the
3942 * configured P2P group network will be removed from the interface.
3943 */
3944int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3945{
3946 struct wpa_global *global = wpa_s->global;
3947
3948 if (os_strcmp(ifname, "*") == 0) {
3949 struct wpa_supplicant *prev;
3950 wpa_s = global->ifaces;
3951 while (wpa_s) {
3952 prev = wpa_s;
3953 wpa_s = wpa_s->next;
3954 wpas_p2p_disconnect(prev);
3955 }
3956 return 0;
3957 }
3958
3959 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3960 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3961 break;
3962 }
3963
3964 return wpas_p2p_disconnect(wpa_s);
3965}
3966
3967
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003968static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3969{
3970 if (channels == NULL)
3971 return 1; /* Assume no restrictions */
3972 return p2p_channels_includes_freq(channels, freq);
3973}
3974
3975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003976static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3977 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003978 int freq, int ht40,
3979 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003980{
3981 u8 bssid[ETH_ALEN];
3982 int res;
3983
3984 os_memset(params, 0, sizeof(*params));
3985 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003986 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003988 if (!freq_included(channels, freq)) {
3989 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3990 "accepted", freq);
3991 return -1;
3992 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3994 "frequency %d MHz", freq);
3995 params->freq = freq;
3996 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3997 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003998 wpa_s->conf->p2p_oper_channel <= 11 &&
3999 freq_included(channels,
4000 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4002 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4003 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004004 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4005 wpa_s->conf->p2p_oper_reg_class == 116 ||
4006 wpa_s->conf->p2p_oper_reg_class == 117 ||
4007 wpa_s->conf->p2p_oper_reg_class == 124 ||
4008 wpa_s->conf->p2p_oper_reg_class == 126 ||
4009 wpa_s->conf->p2p_oper_reg_class == 127) &&
4010 freq_included(channels,
4011 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4013 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4014 "frequency %d MHz", params->freq);
4015 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4016 wpa_s->best_overall_freq > 0 &&
4017 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004018 wpa_s->best_overall_freq) &&
4019 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020 params->freq = wpa_s->best_overall_freq;
4021 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4022 "channel %d MHz", params->freq);
4023 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4024 wpa_s->best_24_freq > 0 &&
4025 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004026 wpa_s->best_24_freq) &&
4027 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 params->freq = wpa_s->best_24_freq;
4029 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4030 "channel %d MHz", params->freq);
4031 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4032 wpa_s->best_5_freq > 0 &&
4033 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004034 wpa_s->best_5_freq) &&
4035 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004036 params->freq = wpa_s->best_5_freq;
4037 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4038 "channel %d MHz", params->freq);
4039 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004040 int chan;
4041 for (chan = 0; chan < 11; chan++) {
4042 params->freq = 2412 + chan * 5;
4043 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004044 params->freq) &&
4045 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004046 break;
4047 }
4048 if (chan == 11) {
4049 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4050 "allowed");
4051 return -1;
4052 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004053 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4054 "known)", params->freq);
4055 }
4056
4057 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4058 wpa_s->assoc_freq && !freq) {
4059 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4060 "already using");
4061 params->freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004062 if (!freq_included(channels, params->freq)) {
4063 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4064 "accepted", params->freq);
4065 return -1;
4066 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067 }
4068
4069 res = wpa_drv_shared_freq(wpa_s);
4070 if (res > 0 && !freq) {
4071 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4072 "already using on a shared interface");
4073 params->freq = res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004074 if (!freq_included(channels, params->freq)) {
4075 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4076 "accepted", params->freq);
4077 return -1;
4078 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004079 } else if (res > 0 && freq != res &&
4080 !(wpa_s->drv_flags &
4081 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4082 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4083 "while connected on another channel (%u MHz)",
4084 freq, res);
4085 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004086 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004087
4088 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089}
4090
4091
4092static struct wpa_supplicant *
4093wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4094 int go)
4095{
4096 struct wpa_supplicant *group_wpa_s;
4097
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004098 if (!wpas_p2p_create_iface(wpa_s)) {
4099 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4100 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004101 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004102 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103
4104 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004105 WPA_IF_P2P_CLIENT) < 0) {
4106 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004109 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4110 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004111 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4112 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004113 wpas_p2p_remove_pending_group_interface(wpa_s);
4114 return NULL;
4115 }
4116
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004117 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4118 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004119 return group_wpa_s;
4120}
4121
4122
4123/**
4124 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4125 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4126 * @persistent_group: Whether to create a persistent group
4127 * @freq: Frequency for the group or 0 to indicate no hardcoding
4128 * Returns: 0 on success, -1 on failure
4129 *
4130 * This function creates a new P2P group with the local end as the Group Owner,
4131 * i.e., without using Group Owner Negotiation.
4132 */
4133int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004134 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135{
4136 struct p2p_go_neg_results params;
4137 unsigned int r;
4138
4139 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4140 return -1;
4141
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004142 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004143 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004144 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004145
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146 if (freq == 2) {
4147 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4148 "band");
4149 if (wpa_s->best_24_freq > 0 &&
4150 p2p_supported_freq(wpa_s->global->p2p,
4151 wpa_s->best_24_freq)) {
4152 freq = wpa_s->best_24_freq;
4153 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4154 "channel: %d MHz", freq);
4155 } else {
4156 os_get_random((u8 *) &r, sizeof(r));
4157 freq = 2412 + (r % 3) * 25;
4158 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4159 "channel: %d MHz", freq);
4160 }
4161 }
4162
4163 if (freq == 5) {
4164 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4165 "band");
4166 if (wpa_s->best_5_freq > 0 &&
4167 p2p_supported_freq(wpa_s->global->p2p,
4168 wpa_s->best_5_freq)) {
4169 freq = wpa_s->best_5_freq;
4170 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4171 "channel: %d MHz", freq);
4172 } else {
4173 os_get_random((u8 *) &r, sizeof(r));
4174 freq = 5180 + (r % 4) * 20;
4175 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4176 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4177 "5 GHz channel for P2P group");
4178 return -1;
4179 }
4180 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4181 "channel: %d MHz", freq);
4182 }
4183 }
4184
4185 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4186 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4187 "(%u MHz) is not supported for P2P uses",
4188 freq);
4189 return -1;
4190 }
4191
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004192 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004193 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004194 if (params.freq &&
4195 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4196 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4197 "(%u MHz) is not supported for P2P uses",
4198 params.freq);
4199 return -1;
4200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 p2p_go_params(wpa_s->global->p2p, &params);
4202 params.persistent_group = persistent_group;
4203
4204 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4205 if (wpa_s == NULL)
4206 return -1;
4207 wpas_start_wps_go(wpa_s, &params, 0);
4208
4209 return 0;
4210}
4211
4212
4213static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4214 struct wpa_ssid *params, int addr_allocated)
4215{
4216 struct wpa_ssid *ssid;
4217
4218 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4219 if (wpa_s == NULL)
4220 return -1;
4221
4222 wpa_supplicant_ap_deinit(wpa_s);
4223
4224 ssid = wpa_config_add_network(wpa_s->conf);
4225 if (ssid == NULL)
4226 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227 wpa_config_set_network_defaults(ssid);
4228 ssid->temporary = 1;
4229 ssid->proto = WPA_PROTO_RSN;
4230 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4231 ssid->group_cipher = WPA_CIPHER_CCMP;
4232 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4233 ssid->ssid = os_malloc(params->ssid_len);
4234 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004235 wpa_config_remove_network(wpa_s->conf, ssid->id);
4236 return -1;
4237 }
4238 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4239 ssid->ssid_len = params->ssid_len;
4240 ssid->p2p_group = 1;
4241 ssid->export_keys = 1;
4242 if (params->psk_set) {
4243 os_memcpy(ssid->psk, params->psk, 32);
4244 ssid->psk_set = 1;
4245 }
4246 if (params->passphrase)
4247 ssid->passphrase = os_strdup(params->passphrase);
4248
4249 wpa_supplicant_select_network(wpa_s, ssid);
4250
4251 wpa_s->show_group_started = 1;
4252
4253 return 0;
4254}
4255
4256
4257int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4258 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004259 int freq, int ht40,
4260 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261{
4262 struct p2p_go_neg_results params;
4263 int go = 0;
4264
4265 if (ssid->disabled != 2 || ssid->ssid == NULL)
4266 return -1;
4267
4268 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4269 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4270 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4271 "already running");
4272 return 0;
4273 }
4274
4275 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004276 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004277
Dmitry Shmidt04949592012-07-19 12:16:46 -07004278 wpa_s->p2p_fallback_to_go_neg = 0;
4279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004280 if (ssid->mode == WPAS_MODE_INFRA)
4281 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4282
4283 if (ssid->mode != WPAS_MODE_P2P_GO)
4284 return -1;
4285
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004286 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004287 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288
4289 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004290 params.psk_set = ssid->psk_set;
4291 if (params.psk_set)
4292 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004293 if (ssid->passphrase) {
4294 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4295 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4296 "persistent group");
4297 return -1;
4298 }
4299 os_strlcpy(params.passphrase, ssid->passphrase,
4300 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4303 params.ssid_len = ssid->ssid_len;
4304 params.persistent_group = 1;
4305
4306 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4307 if (wpa_s == NULL)
4308 return -1;
4309
4310 wpas_start_wps_go(wpa_s, &params, 0);
4311
4312 return 0;
4313}
4314
4315
4316static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4317 struct wpabuf *proberesp_ies)
4318{
4319 struct wpa_supplicant *wpa_s = ctx;
4320 if (wpa_s->ap_iface) {
4321 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004322 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4323 wpabuf_free(beacon_ies);
4324 wpabuf_free(proberesp_ies);
4325 return;
4326 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004327 if (beacon_ies) {
4328 wpabuf_free(hapd->p2p_beacon_ie);
4329 hapd->p2p_beacon_ie = beacon_ies;
4330 }
4331 wpabuf_free(hapd->p2p_probe_resp_ie);
4332 hapd->p2p_probe_resp_ie = proberesp_ies;
4333 } else {
4334 wpabuf_free(beacon_ies);
4335 wpabuf_free(proberesp_ies);
4336 }
4337 wpa_supplicant_ap_update_beacon(wpa_s);
4338}
4339
4340
4341static void wpas_p2p_idle_update(void *ctx, int idle)
4342{
4343 struct wpa_supplicant *wpa_s = ctx;
4344 if (!wpa_s->ap_iface)
4345 return;
4346 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004347 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004348 wpas_p2p_set_group_idle_timeout(wpa_s);
4349 else
4350 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4351}
4352
4353
4354struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004355 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004356{
4357 struct p2p_group *group;
4358 struct p2p_group_config *cfg;
4359
4360 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4361 return NULL;
4362 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4363 return NULL;
4364
4365 cfg = os_zalloc(sizeof(*cfg));
4366 if (cfg == NULL)
4367 return NULL;
4368
Dmitry Shmidt04949592012-07-19 12:16:46 -07004369 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004370 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004371 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004372 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4374 if (wpa_s->max_stations &&
4375 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4376 cfg->max_clients = wpa_s->max_stations;
4377 else
4378 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004379 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4380 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004381 cfg->cb_ctx = wpa_s;
4382 cfg->ie_update = wpas_p2p_ie_update;
4383 cfg->idle_update = wpas_p2p_idle_update;
4384
4385 group = p2p_group_init(wpa_s->global->p2p, cfg);
4386 if (group == NULL)
4387 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004388 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389 p2p_group_notif_formation_done(group);
4390 wpa_s->p2p_group = group;
4391 return group;
4392}
4393
4394
4395void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4396 int registrar)
4397{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004398 struct wpa_ssid *ssid = wpa_s->current_ssid;
4399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004400 if (!wpa_s->p2p_in_provisioning) {
4401 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4402 "provisioning not in progress");
4403 return;
4404 }
4405
Dmitry Shmidt04949592012-07-19 12:16:46 -07004406 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4407 u8 go_dev_addr[ETH_ALEN];
4408 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4409 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4410 ssid->ssid_len);
4411 /* Clear any stored provisioning info */
4412 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4413 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004415 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4416 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004417 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4418 /*
4419 * Use a separate timeout for initial data connection to
4420 * complete to allow the group to be removed automatically if
4421 * something goes wrong in this step before the P2P group idle
4422 * timeout mechanism is taken into use.
4423 */
4424 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4425 wpas_p2p_group_formation_timeout,
4426 wpa_s->parent, NULL);
4427 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428 if (wpa_s->global->p2p)
4429 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4430 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4431 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4432 wpas_group_formation_completed(wpa_s, 1);
4433}
4434
4435
Jouni Malinen75ecf522011-06-27 15:19:46 -07004436void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4437 struct wps_event_fail *fail)
4438{
4439 if (!wpa_s->p2p_in_provisioning) {
4440 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4441 "provisioning not in progress");
4442 return;
4443 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004444
4445 if (wpa_s->go_params) {
4446 p2p_clear_provisioning_info(
4447 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004448 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004449 }
4450
Jouni Malinen75ecf522011-06-27 15:19:46 -07004451 wpas_notify_p2p_wps_failed(wpa_s, fail);
4452}
4453
4454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004455int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004456 const char *config_method,
4457 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458{
4459 u16 config_methods;
4460
Dmitry Shmidt04949592012-07-19 12:16:46 -07004461 wpa_s->p2p_fallback_to_go_neg = 0;
4462 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004463 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004464 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004465 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004466 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004467 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4468 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004469 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004470 else {
4471 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004473 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004474
Dmitry Shmidt04949592012-07-19 12:16:46 -07004475 if (use == WPAS_P2P_PD_AUTO) {
4476 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4477 wpa_s->pending_pd_config_methods = config_methods;
4478 wpa_s->p2p_auto_pd = 1;
4479 wpa_s->p2p_auto_join = 0;
4480 wpa_s->pending_pd_before_join = 0;
4481 wpa_s->auto_pd_scan_retry = 0;
4482 wpas_p2p_stop_find(wpa_s);
4483 wpa_s->p2p_join_scan_count = 0;
4484 os_get_time(&wpa_s->p2p_auto_started);
4485 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4486 wpa_s->p2p_auto_started.sec,
4487 wpa_s->p2p_auto_started.usec);
4488 wpas_p2p_join_scan(wpa_s, NULL);
4489 return 0;
4490 }
4491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004492 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4493 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004494 config_methods,
4495 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004496 }
4497
4498 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4499 return -1;
4500
4501 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004502 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004503 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504}
4505
4506
4507int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4508 char *end)
4509{
4510 return p2p_scan_result_text(ies, ies_len, buf, end);
4511}
4512
4513
4514static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4515{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004516 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004517 return;
4518
4519 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4520 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004521 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004522}
4523
4524
4525int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4526 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004527 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004528 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004529{
4530 wpas_p2p_clear_pending_action_tx(wpa_s);
4531 wpa_s->p2p_long_listen = 0;
4532
4533 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4534 return wpa_drv_p2p_find(wpa_s, timeout, type);
4535
Dmitry Shmidt04949592012-07-19 12:16:46 -07004536 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4537 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004538 return -1;
4539
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004540 wpa_supplicant_cancel_sched_scan(wpa_s);
4541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004543 num_req_dev_types, req_dev_types, dev_id,
4544 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004545}
4546
4547
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004548static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004549{
4550 wpas_p2p_clear_pending_action_tx(wpa_s);
4551 wpa_s->p2p_long_listen = 0;
4552 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4553 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004554 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004555
4556 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4557 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004558 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004559 }
4560
4561 if (wpa_s->global->p2p)
4562 p2p_stop_find(wpa_s->global->p2p);
4563
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004564 return 0;
4565}
4566
4567
4568void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4569{
4570 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4571 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 wpas_p2p_remove_pending_group_interface(wpa_s);
4573}
4574
4575
4576static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4577{
4578 struct wpa_supplicant *wpa_s = eloop_ctx;
4579 wpa_s->p2p_long_listen = 0;
4580}
4581
4582
4583int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4584{
4585 int res;
4586
4587 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4588 return -1;
4589
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004590 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004591 wpas_p2p_clear_pending_action_tx(wpa_s);
4592
4593 if (timeout == 0) {
4594 /*
4595 * This is a request for unlimited Listen state. However, at
4596 * least for now, this is mapped to a Listen state for one
4597 * hour.
4598 */
4599 timeout = 3600;
4600 }
4601 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4602 wpa_s->p2p_long_listen = 0;
4603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004604 /*
4605 * Stop previous find/listen operation to avoid trying to request a new
4606 * remain-on-channel operation while the driver is still running the
4607 * previous one.
4608 */
4609 if (wpa_s->global->p2p)
4610 p2p_stop_find(wpa_s->global->p2p);
4611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4613 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4614 wpa_s->p2p_long_listen = timeout * 1000;
4615 eloop_register_timeout(timeout, 0,
4616 wpas_p2p_long_listen_timeout,
4617 wpa_s, NULL);
4618 }
4619
4620 return res;
4621}
4622
4623
4624int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4625 u8 *buf, size_t len, int p2p_group)
4626{
4627 struct wpabuf *p2p_ie;
4628 int ret;
4629
4630 if (wpa_s->global->p2p_disabled)
4631 return -1;
4632 if (wpa_s->global->p2p == NULL)
4633 return -1;
4634 if (bss == NULL)
4635 return -1;
4636
4637 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4638 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4639 p2p_group, p2p_ie);
4640 wpabuf_free(p2p_ie);
4641
4642 return ret;
4643}
4644
4645
4646int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004647 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004648 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004649{
4650 if (wpa_s->global->p2p_disabled)
4651 return 0;
4652 if (wpa_s->global->p2p == NULL)
4653 return 0;
4654
Dmitry Shmidt04949592012-07-19 12:16:46 -07004655 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4656 ie, ie_len)) {
4657 case P2P_PREQ_NOT_P2P:
4658 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4659 ssi_signal);
4660 /* fall through */
4661 case P2P_PREQ_MALFORMED:
4662 case P2P_PREQ_NOT_LISTEN:
4663 case P2P_PREQ_NOT_PROCESSED:
4664 default: /* make gcc happy */
4665 return 0;
4666 case P2P_PREQ_PROCESSED:
4667 return 1;
4668 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669}
4670
4671
4672void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4673 const u8 *sa, const u8 *bssid,
4674 u8 category, const u8 *data, size_t len, int freq)
4675{
4676 if (wpa_s->global->p2p_disabled)
4677 return;
4678 if (wpa_s->global->p2p == NULL)
4679 return;
4680
4681 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4682 freq);
4683}
4684
4685
4686void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4687{
4688 if (wpa_s->global->p2p_disabled)
4689 return;
4690 if (wpa_s->global->p2p == NULL)
4691 return;
4692
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004693 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004694}
4695
4696
4697void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4698{
4699 p2p_group_deinit(wpa_s->p2p_group);
4700 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004701
4702 wpa_s->ap_configured_cb = NULL;
4703 wpa_s->ap_configured_cb_ctx = NULL;
4704 wpa_s->ap_configured_cb_data = NULL;
4705 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004706}
4707
4708
4709int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4710{
4711 wpa_s->p2p_long_listen = 0;
4712
4713 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4714 return wpa_drv_p2p_reject(wpa_s, addr);
4715
4716 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4717 return -1;
4718
4719 return p2p_reject(wpa_s->global->p2p, addr);
4720}
4721
4722
4723/* Invite to reinvoke a persistent group */
4724int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004725 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004726 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727{
4728 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004729 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004730 int force_freq = 0, oper_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004731 int res;
4732
4733 if (peer_addr)
4734 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4735 else
4736 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737
Jouni Malinen31be0a42012-08-31 21:20:51 +03004738 wpa_s->p2p_persistent_go_freq = freq;
4739 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740 if (ssid->mode == WPAS_MODE_P2P_GO) {
4741 role = P2P_INVITE_ROLE_GO;
4742 if (peer_addr == NULL) {
4743 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4744 "address in invitation command");
4745 return -1;
4746 }
4747 if (wpas_p2p_create_iface(wpa_s)) {
4748 if (wpas_p2p_add_group_interface(wpa_s,
4749 WPA_IF_P2P_GO) < 0) {
4750 wpa_printf(MSG_ERROR, "P2P: Failed to "
4751 "allocate a new interface for the "
4752 "group");
4753 return -1;
4754 }
4755 bssid = wpa_s->pending_interface_addr;
4756 } else
4757 bssid = wpa_s->own_addr;
4758 } else {
4759 role = P2P_INVITE_ROLE_CLIENT;
4760 peer_addr = ssid->bssid;
4761 }
4762 wpa_s->pending_invite_ssid_id = ssid->id;
4763
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004764 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4765 wpa_s->assoc_freq) {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004766 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004767 if (bssid == NULL)
4768 bssid = bssid_buf;
4769 } else {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004770 oper_freq = wpa_drv_shared_freq(wpa_s);
4771 if (oper_freq < 0)
4772 oper_freq = 0;
4773 }
4774
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004775 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4776 &oper_freq);
4777 if (res)
4778 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004780 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4781 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4782 ssid->ssid, ssid->ssid_len,
4783 go_dev_addr, 1);
4784
4785 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4786 return -1;
4787
4788 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004789 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4790 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004791}
4792
4793
4794/* Invite to join an active group */
4795int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4796 const u8 *peer_addr, const u8 *go_dev_addr)
4797{
4798 struct wpa_global *global = wpa_s->global;
4799 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004800 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004802 int persistent;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004803 int force_freq = 0, oper_freq = 0, pref_freq = 0;
4804 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805
Jouni Malinen31be0a42012-08-31 21:20:51 +03004806 wpa_s->p2p_persistent_go_freq = 0;
4807 wpa_s->p2p_go_ht40 = 0;
4808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4810 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4811 break;
4812 }
4813 if (wpa_s == NULL) {
4814 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4815 return -1;
4816 }
4817
4818 ssid = wpa_s->current_ssid;
4819 if (ssid == NULL) {
4820 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4821 "invitation");
4822 return -1;
4823 }
4824
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004825 persistent = ssid->p2p_persistent_group &&
4826 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4827 ssid->ssid, ssid->ssid_len);
4828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004829 if (ssid->mode == WPAS_MODE_P2P_GO) {
4830 role = P2P_INVITE_ROLE_ACTIVE_GO;
4831 bssid = wpa_s->own_addr;
4832 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004833 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004834 } else {
4835 role = P2P_INVITE_ROLE_CLIENT;
4836 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4837 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4838 "invite to current group");
4839 return -1;
4840 }
4841 bssid = wpa_s->bssid;
4842 if (go_dev_addr == NULL &&
4843 !is_zero_ether_addr(wpa_s->go_dev_addr))
4844 go_dev_addr = wpa_s->go_dev_addr;
4845 }
4846 wpa_s->parent->pending_invite_ssid_id = -1;
4847
4848 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4849 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4850 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004851 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004852
4853 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4854 return -1;
4855
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004856 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4857 wpa_s->assoc_freq) {
4858 oper_freq = wpa_s->assoc_freq;
4859 if (bssid == NULL)
4860 bssid = bssid_buf;
4861 } else {
4862 oper_freq = wpa_drv_shared_freq(wpa_s);
4863 if (oper_freq < 0)
4864 oper_freq = 0;
4865 }
4866
4867 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
4868 &oper_freq);
4869 if (res)
4870 return res;
4871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004873 ssid->ssid, ssid->ssid_len, force_freq,
4874 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004875}
4876
4877
4878void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4879{
4880 struct wpa_ssid *ssid = wpa_s->current_ssid;
4881 const char *ssid_txt;
4882 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004883 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004885 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004886
Dmitry Shmidt04949592012-07-19 12:16:46 -07004887 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4888 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4889 wpa_s->parent, NULL);
4890 }
4891
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004893 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004894
4895 wpa_s->show_group_started = 0;
4896
4897 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4898 os_memset(go_dev_addr, 0, ETH_ALEN);
4899 if (ssid->bssid_set)
4900 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4901 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4902 ssid->ssid_len);
4903 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4904
4905 if (wpa_s->global->p2p_group_formation == wpa_s)
4906 wpa_s->global->p2p_group_formation = NULL;
4907
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004908 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4909 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910 if (ssid->passphrase == NULL && ssid->psk_set) {
4911 char psk[65];
4912 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4913 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4914 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4915 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004916 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004917 MAC2STR(go_dev_addr),
4918 persistent ? " [PERSISTENT]" : "");
4919 } else {
4920 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4921 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4922 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004923 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924 ssid->passphrase ? ssid->passphrase : "",
4925 MAC2STR(go_dev_addr),
4926 persistent ? " [PERSISTENT]" : "");
4927 }
4928
4929 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004930 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4931 ssid, go_dev_addr);
4932 if (network_id < 0)
4933 network_id = ssid->id;
4934 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004935
4936done:
Jouni Malinendc7b7132012-09-14 12:53:47 -07004937 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004938 wpa_s->global->p2p != NULL) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07004939 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004940 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4941 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4942 "continued after successful connection");
4943 p2p_increase_search_delay(
4944 wpa_s->global->p2p,
4945 wpas_p2p_search_delay(wpa_s));
4946 }
4947 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004948}
4949
4950
4951int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4952 u32 interval1, u32 duration2, u32 interval2)
4953{
4954 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4955 return -1;
4956 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4957 return -1;
4958
4959 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4960 wpa_s->current_ssid == NULL ||
4961 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4962 return -1;
4963
4964 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4965 wpa_s->own_addr, wpa_s->assoc_freq,
4966 duration1, interval1, duration2, interval2);
4967}
4968
4969
4970int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4971 unsigned int interval)
4972{
4973 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4974 return -1;
4975
4976 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4977 return -1;
4978
4979 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4980}
4981
4982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004983static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4984{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004985 if (wpa_s->current_ssid == NULL) {
4986 /*
4987 * current_ssid can be cleared when P2P client interface gets
4988 * disconnected, so assume this interface was used as P2P
4989 * client.
4990 */
4991 return 1;
4992 }
4993 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004994 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4995}
4996
4997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4999{
5000 struct wpa_supplicant *wpa_s = eloop_ctx;
5001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005002 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5004 "disabled");
5005 return;
5006 }
5007
Dmitry Shmidt04949592012-07-19 12:16:46 -07005008 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5009 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005010 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005011}
5012
5013
5014static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5015{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005016 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017
Dmitry Shmidt04949592012-07-19 12:16:46 -07005018 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5019 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5022 return;
5023
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005024 timeout = wpa_s->conf->p2p_group_idle;
5025 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5026 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5027 timeout = P2P_MAX_CLIENT_IDLE;
5028
5029 if (timeout == 0)
5030 return;
5031
Dmitry Shmidt04949592012-07-19 12:16:46 -07005032 if (timeout < 0) {
5033 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5034 timeout = 0; /* special client mode no-timeout */
5035 else
5036 return;
5037 }
5038
5039 if (wpa_s->p2p_in_provisioning) {
5040 /*
5041 * Use the normal group formation timeout during the
5042 * provisioning phase to avoid terminating this process too
5043 * early due to group idle timeout.
5044 */
5045 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5046 "during provisioning");
5047 return;
5048 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07005049#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07005050 if (wpa_s->show_group_started) {
5051 /*
5052 * Use the normal group formation timeout between the end of
5053 * the provisioning phase and completion of 4-way handshake to
5054 * avoid terminating this process too early due to group idle
5055 * timeout.
5056 */
5057 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5058 "while waiting for initial 4-way handshake to "
5059 "complete");
5060 return;
5061 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07005062#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07005063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005065 timeout);
5066 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5067 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005068}
5069
5070
Jouni Malinen2b89da82012-08-31 22:04:41 +03005071/* Returns 1 if the interface was removed */
5072int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5073 u16 reason_code, const u8 *ie, size_t ie_len,
5074 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005075{
5076 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005077 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005078 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005079 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080
Dmitry Shmidt04949592012-07-19 12:16:46 -07005081 if (!locally_generated)
5082 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5083 ie_len);
5084
5085 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5086 wpa_s->current_ssid &&
5087 wpa_s->current_ssid->p2p_group &&
5088 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5089 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5090 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005091 if (wpas_p2p_group_delete(wpa_s,
5092 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5093 > 0)
5094 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005095 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005096
5097 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098}
5099
5100
5101void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005102 u16 reason_code, const u8 *ie, size_t ie_len,
5103 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104{
5105 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5106 return;
5107 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5108 return;
5109
Dmitry Shmidt04949592012-07-19 12:16:46 -07005110 if (!locally_generated)
5111 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5112 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005113}
5114
5115
5116void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5117{
5118 struct p2p_data *p2p = wpa_s->global->p2p;
5119
5120 if (p2p == NULL)
5121 return;
5122
5123 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5124 return;
5125
5126 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5127 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5128
5129 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5130 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5131
5132 if (wpa_s->wps &&
5133 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5134 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5135
5136 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5137 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5138
5139 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5140 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5141 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5142 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5143 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5144 }
5145
5146 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5147 p2p_set_sec_dev_types(p2p,
5148 (void *) wpa_s->conf->sec_device_type,
5149 wpa_s->conf->num_sec_device_types);
5150
5151 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5152 int i;
5153 p2p_remove_wps_vendor_extensions(p2p);
5154 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5155 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5156 continue;
5157 p2p_add_wps_vendor_extension(
5158 p2p, wpa_s->conf->wps_vendor_ext[i]);
5159 }
5160 }
5161
5162 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5163 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5164 char country[3];
5165 country[0] = wpa_s->conf->country[0];
5166 country[1] = wpa_s->conf->country[1];
5167 country[2] = 0x04;
5168 p2p_set_country(p2p, country);
5169 }
5170
5171 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5172 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5173 wpa_s->conf->p2p_ssid_postfix ?
5174 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5175 0);
5176 }
5177
5178 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5179 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005180
5181 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5182 u8 reg_class, channel;
5183 int ret;
5184 unsigned int r;
5185 if (wpa_s->conf->p2p_listen_reg_class &&
5186 wpa_s->conf->p2p_listen_channel) {
5187 reg_class = wpa_s->conf->p2p_listen_reg_class;
5188 channel = wpa_s->conf->p2p_listen_channel;
5189 } else {
5190 reg_class = 81;
5191 /*
5192 * Pick one of the social channels randomly as the
5193 * listen channel.
5194 */
5195 os_get_random((u8 *) &r, sizeof(r));
5196 channel = 1 + (r % 3) * 5;
5197 }
5198 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5199 if (ret)
5200 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5201 "failed: %d", ret);
5202 }
5203 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5204 u8 op_reg_class, op_channel, cfg_op_channel;
5205 int ret = 0;
5206 unsigned int r;
5207 if (wpa_s->conf->p2p_oper_reg_class &&
5208 wpa_s->conf->p2p_oper_channel) {
5209 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5210 op_channel = wpa_s->conf->p2p_oper_channel;
5211 cfg_op_channel = 1;
5212 } else {
5213 op_reg_class = 81;
5214 /*
5215 * Use random operation channel from (1, 6, 11)
5216 *if no other preference is indicated.
5217 */
5218 os_get_random((u8 *) &r, sizeof(r));
5219 op_channel = 1 + (r % 3) * 5;
5220 cfg_op_channel = 0;
5221 }
5222 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5223 cfg_op_channel);
5224 if (ret)
5225 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5226 "failed: %d", ret);
5227 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005228
5229 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5230 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5231 wpa_s->conf->p2p_pref_chan) < 0) {
5232 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5233 "update failed");
5234 }
5235 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005236}
5237
5238
5239int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5240 int duration)
5241{
5242 if (!wpa_s->ap_iface)
5243 return -1;
5244 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5245 duration);
5246}
5247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005248
5249int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5250{
5251 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5252 return -1;
5253 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5254 return -1;
5255
5256 wpa_s->global->cross_connection = enabled;
5257 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5258
5259 if (!enabled) {
5260 struct wpa_supplicant *iface;
5261
5262 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5263 {
5264 if (iface->cross_connect_enabled == 0)
5265 continue;
5266
5267 iface->cross_connect_enabled = 0;
5268 iface->cross_connect_in_use = 0;
5269 wpa_msg(iface->parent, MSG_INFO,
5270 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5271 iface->ifname, iface->cross_connect_uplink);
5272 }
5273 }
5274
5275 return 0;
5276}
5277
5278
5279static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5280{
5281 struct wpa_supplicant *iface;
5282
5283 if (!uplink->global->cross_connection)
5284 return;
5285
5286 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5287 if (!iface->cross_connect_enabled)
5288 continue;
5289 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5290 0)
5291 continue;
5292 if (iface->ap_iface == NULL)
5293 continue;
5294 if (iface->cross_connect_in_use)
5295 continue;
5296
5297 iface->cross_connect_in_use = 1;
5298 wpa_msg(iface->parent, MSG_INFO,
5299 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5300 iface->ifname, iface->cross_connect_uplink);
5301 }
5302}
5303
5304
5305static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5306{
5307 struct wpa_supplicant *iface;
5308
5309 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5310 if (!iface->cross_connect_enabled)
5311 continue;
5312 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5313 0)
5314 continue;
5315 if (!iface->cross_connect_in_use)
5316 continue;
5317
5318 wpa_msg(iface->parent, MSG_INFO,
5319 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5320 iface->ifname, iface->cross_connect_uplink);
5321 iface->cross_connect_in_use = 0;
5322 }
5323}
5324
5325
5326void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5327{
5328 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5329 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5330 wpa_s->cross_connect_disallowed)
5331 wpas_p2p_disable_cross_connect(wpa_s);
5332 else
5333 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005334 if (!wpa_s->ap_iface &&
5335 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5336 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005337}
5338
5339
5340void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5341{
5342 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005343 if (!wpa_s->ap_iface &&
5344 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5345 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 wpas_p2p_set_group_idle_timeout(wpa_s);
5347}
5348
5349
5350static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5351{
5352 struct wpa_supplicant *iface;
5353
5354 if (!wpa_s->global->cross_connection)
5355 return;
5356
5357 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5358 if (iface == wpa_s)
5359 continue;
5360 if (iface->drv_flags &
5361 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5362 continue;
5363 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5364 continue;
5365
5366 wpa_s->cross_connect_enabled = 1;
5367 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5368 sizeof(wpa_s->cross_connect_uplink));
5369 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5370 "%s to %s whenever uplink is available",
5371 wpa_s->ifname, wpa_s->cross_connect_uplink);
5372
5373 if (iface->ap_iface || iface->current_ssid == NULL ||
5374 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5375 iface->cross_connect_disallowed ||
5376 iface->wpa_state != WPA_COMPLETED)
5377 break;
5378
5379 wpa_s->cross_connect_in_use = 1;
5380 wpa_msg(wpa_s->parent, MSG_INFO,
5381 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5382 wpa_s->ifname, wpa_s->cross_connect_uplink);
5383 break;
5384 }
5385}
5386
5387
5388int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5389{
5390 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5391 !wpa_s->p2p_in_provisioning)
5392 return 0; /* not P2P client operation */
5393
5394 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5395 "session overlap");
5396 if (wpa_s != wpa_s->parent)
5397 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5398
5399 if (wpa_s->global->p2p)
5400 p2p_group_formation_failed(wpa_s->global->p2p);
5401
5402 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5403 wpa_s->parent, NULL);
5404
5405 wpas_group_formation_completed(wpa_s, 0);
5406 return 1;
5407}
5408
5409
5410void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5411{
5412 struct p2p_channels chan;
5413
5414 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5415 return;
5416
5417 os_memset(&chan, 0, sizeof(chan));
5418 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5419 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5420 "channel list");
5421 return;
5422 }
5423
5424 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5425}
5426
5427
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005428static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5429 struct wpa_scan_results *scan_res)
5430{
5431 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5432}
5433
5434
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5436{
5437 struct wpa_global *global = wpa_s->global;
5438 int found = 0;
5439 const u8 *peer;
5440
5441 if (global->p2p == NULL)
5442 return -1;
5443
5444 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5445
5446 if (wpa_s->pending_interface_name[0] &&
5447 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5448 found = 1;
5449
5450 peer = p2p_get_go_neg_peer(global->p2p);
5451 if (peer) {
5452 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5453 MACSTR, MAC2STR(peer));
5454 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005455 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456 }
5457
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005458 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5459 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5460 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5461 found = 1;
5462 }
5463
5464 if (wpa_s->pending_pd_before_join) {
5465 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5466 wpa_s->pending_pd_before_join = 0;
5467 found = 1;
5468 }
5469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005470 wpas_p2p_stop_find(wpa_s);
5471
5472 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5473 if (wpa_s == global->p2p_group_formation &&
5474 (wpa_s->p2p_in_provisioning ||
5475 wpa_s->parent->pending_interface_type ==
5476 WPA_IF_P2P_CLIENT)) {
5477 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5478 "formation found - cancelling",
5479 wpa_s->ifname);
5480 found = 1;
5481 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5482 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005483 if (wpa_s->p2p_in_provisioning) {
5484 wpas_group_formation_completed(wpa_s, 0);
5485 break;
5486 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005487 wpas_p2p_group_delete(wpa_s,
5488 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005489 break;
5490 }
5491 }
5492
5493 if (!found) {
5494 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5495 return -1;
5496 }
5497
5498 return 0;
5499}
5500
5501
5502void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5503{
5504 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5505 return;
5506
5507 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5508 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005509 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510}
5511
5512
5513void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5514 int freq_24, int freq_5, int freq_overall)
5515{
5516 struct p2p_data *p2p = wpa_s->global->p2p;
5517 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5518 return;
5519 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5520}
5521
5522
5523int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5524{
5525 u8 peer[ETH_ALEN];
5526 struct p2p_data *p2p = wpa_s->global->p2p;
5527
5528 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5529 return -1;
5530
5531 if (hwaddr_aton(addr, peer))
5532 return -1;
5533
5534 return p2p_unauthorize(p2p, peer);
5535}
5536
5537
5538/**
5539 * wpas_p2p_disconnect - Disconnect from a P2P Group
5540 * @wpa_s: Pointer to wpa_supplicant data
5541 * Returns: 0 on success, -1 on failure
5542 *
5543 * This can be used to disconnect from a group in which the local end is a P2P
5544 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5545 * virtual network interface was created for this group, that interface will be
5546 * removed. Otherwise, only the configured P2P group network will be removed
5547 * from the interface.
5548 */
5549int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5550{
5551
5552 if (wpa_s == NULL)
5553 return -1;
5554
Jouni Malinen2b89da82012-08-31 22:04:41 +03005555 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5556 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005557}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005558
5559
5560int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5561{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005562 int ret;
5563
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005564 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5565 return 0;
5566
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005567 ret = p2p_in_progress(wpa_s->global->p2p);
5568 if (ret == 0) {
5569 /*
5570 * Check whether there is an ongoing WPS provisioning step (or
5571 * other parts of group formation) on another interface since
5572 * p2p_in_progress() does not report this to avoid issues for
5573 * scans during such provisioning step.
5574 */
5575 if (wpa_s->global->p2p_group_formation &&
5576 wpa_s->global->p2p_group_formation != wpa_s) {
5577 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5578 "in group formation",
5579 wpa_s->global->p2p_group_formation->ifname);
5580 ret = 1;
5581 }
5582 }
5583
5584 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005585}
5586
5587
5588void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5589 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005590{
5591 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5592 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5593 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005594 /**
5595 * Remove the network by scheduling the group formation
5596 * timeout to happen immediately. The teardown code
5597 * needs to be scheduled to run asynch later so that we
5598 * don't delete data from under ourselves unexpectedly.
5599 * Calling wpas_p2p_group_formation_timeout directly
5600 * causes a series of crashes in WPS failure scenarios.
5601 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005602 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5603 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005604 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5605 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005606 }
5607}
5608
5609
5610struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005611 const u8 *addr, const u8 *ssid,
5612 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005613{
5614 struct wpa_ssid *s;
5615 size_t i;
5616
5617 for (s = wpa_s->conf->ssid; s; s = s->next) {
5618 if (s->disabled != 2)
5619 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005620 if (ssid &&
5621 (ssid_len != s->ssid_len ||
5622 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5623 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005624 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5625 return s; /* peer is GO in the persistent group */
5626 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5627 continue;
5628 for (i = 0; i < s->num_p2p_clients; i++) {
5629 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5630 addr, ETH_ALEN) == 0)
5631 return s; /* peer is P2P client in persistent
5632 * group */
5633 }
5634 }
5635
5636 return NULL;
5637}
5638
5639
5640void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5641 const u8 *addr)
5642{
5643 if (addr == NULL)
5644 return;
5645 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5646}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005647
Dmitry Shmidt04949592012-07-19 12:16:46 -07005648
5649static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5650 int group_added)
5651{
5652 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005653 if (wpa_s->global->p2p_group_formation)
5654 group = wpa_s->global->p2p_group_formation;
5655 wpa_s = wpa_s->parent;
5656 offchannel_send_action_done(wpa_s);
5657 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005658 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005659 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5660 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5661 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5662 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5663 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005664 wpa_s->p2p_pd_before_go_neg,
5665 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005666}
5667
5668
5669int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5670{
5671 if (!wpa_s->p2p_fallback_to_go_neg ||
5672 wpa_s->p2p_in_provisioning <= 5)
5673 return 0;
5674
5675 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5676 return 0; /* peer operating as a GO */
5677
5678 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5679 "fallback to GO Negotiation");
5680 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5681
5682 return 1;
5683}
5684
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005685
5686unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5687{
5688 const char *rn, *rn2;
5689 struct wpa_supplicant *ifs;
5690
5691 if (wpa_s->wpa_state > WPA_SCANNING) {
5692 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5693 "concurrent operation",
5694 P2P_CONCURRENT_SEARCH_DELAY);
5695 return P2P_CONCURRENT_SEARCH_DELAY;
5696 }
5697
5698 if (!wpa_s->driver->get_radio_name)
5699 return 0;
5700 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5701 if (rn == NULL || rn[0] == '\0')
5702 return 0;
5703
5704 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5705 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5706 continue;
5707
5708 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5709 if (!rn2 || os_strcmp(rn, rn2) != 0)
5710 continue;
5711 if (ifs->wpa_state > WPA_SCANNING) {
5712 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5713 "delay due to concurrent operation on "
5714 "interface %s",
5715 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5716 return P2P_CONCURRENT_SEARCH_DELAY;
5717 }
5718 }
5719
5720 return 0;
5721}
5722
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005723#ifdef ANDROID_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005724int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5725 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005726{
5727 struct wpa_supplicant *iface = NULL;
5728 struct p2p_data *p2p = wpa_s->global->p2p;
5729
5730 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005731 if ((iface->current_ssid) &&
5732 (iface->current_ssid->frequency != freq) &&
5733 ((iface->p2p_group_interface) ||
5734 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005735
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005736 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5737 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5738 /* Try to see whether we can move the GO. If it
5739 * is not possible, remove the GO interface
5740 */
5741 if (wpa_drv_switch_channel(iface, freq) == 0) {
5742 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5743 iface->current_ssid->frequency = freq;
5744 continue;
5745 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005746 }
5747
5748 /* If GO cannot be moved or if the conflicting interface is a
5749 * P2P Client, remove the interface depending up on the connection
5750 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005751 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005752 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005753 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005754 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005755 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005756 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005757 /* If connection in progress is p2p connection, do not proceed for the connection */
5758 if (wpa_s == iface)
5759 return -1;
5760 else
5761 /* If connection in progress is STA connection, proceed for the connection */
5762 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005763 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005764 /* P2p connection has priority, disable the STA network*/
5765 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5766 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5767 " id=%d", ssid->id);
5768 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5769 if (wpa_s == iface) {
5770 /* p2p connection is in progress, continue connecting...*/
5771 return 0;
5772 }
5773 else {
5774 /* STA connection is in progress, do not allow to continue */
5775 return -1;
5776 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005777 }
5778 }
5779 }
5780 return 0;
5781}
5782#endif