blob: f1e15ffc9971bbf06d9098cbc24ca6e969a39d44 [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 Shmidt1cf45732013-04-29 17:36:45 -070098#ifdef ANDROID_P2P
99static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
100#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700101static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
102 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800103static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104
105
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700106static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
107 int freq)
108{
109 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
110 return;
111 if (freq > 0 &&
112 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
113 wpa_s->parent->conf->p2p_ignore_shared_freq)
114 freq = 0;
115 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
116}
117
118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
120 struct wpa_scan_results *scan_res)
121{
122 size_t i;
123
124 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
125 return;
126
127 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
128 (int) scan_res->num);
129
130 for (i = 0; i < scan_res->num; i++) {
131 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800132 struct os_time time_tmp_age, entry_ts;
133 time_tmp_age.sec = bss->age / 1000;
134 time_tmp_age.usec = (bss->age % 1000) * 1000;
135 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800137 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138 (const u8 *) (bss + 1),
139 bss->ie_len) > 0)
140 break;
141 }
142
143 p2p_scan_res_handled(wpa_s->global->p2p);
144}
145
146
147static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
148 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700149 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150{
151 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700152 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 struct wpa_driver_scan_params params;
154 int ret;
155 struct wpabuf *wps_ie, *ies;
156 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800157 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158
159 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
160 return -1;
161
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700162 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
163 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700164 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700165 wpas_p2p_in_progress(wpa_s) == 2) {
166 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
167 "pending station mode scan to be "
168 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700169 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700170 wpa_supplicant_req_scan(ifs, 0, 0);
171 return 1;
172 }
173 }
174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700175 os_memset(&params, 0, sizeof(params));
176
177 /* P2P Wildcard SSID */
178 params.num_ssids = 1;
179 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
180 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
181
182 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700183 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
184 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185 num_req_dev_types, req_dev_types);
186 if (wps_ie == NULL)
187 return -1;
188
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800189 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
190 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191 if (ies == NULL) {
192 wpabuf_free(wps_ie);
193 return -1;
194 }
195 wpabuf_put_buf(ies, wps_ie);
196 wpabuf_free(wps_ie);
197
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800198 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800200 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 params.extra_ies = wpabuf_head(ies);
202 params.extra_ies_len = wpabuf_len(ies);
203
204 switch (type) {
205 case P2P_SCAN_SOCIAL:
206 params.freqs = social_channels;
207 break;
208 case P2P_SCAN_FULL:
209 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700210 case P2P_SCAN_SPECIFIC:
211 social_channels[0] = freq;
212 social_channels[1] = 0;
213 params.freqs = social_channels;
214 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 case P2P_SCAN_SOCIAL_PLUS_ONE:
216 social_channels[3] = freq;
217 params.freqs = social_channels;
218 break;
219 }
220
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222
223 wpabuf_free(ies);
224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800225 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700226 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
227 if (ifs->scanning ||
228 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
229 wpa_s->global->p2p_cb_on_scan_complete = 1;
230 ret = 1;
231 break;
232 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700234 } else {
235 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700236 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700237 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239 return ret;
240}
241
242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
244{
245 switch (p2p_group_interface) {
246 case P2P_GROUP_INTERFACE_PENDING:
247 return WPA_IF_P2P_GROUP;
248 case P2P_GROUP_INTERFACE_GO:
249 return WPA_IF_P2P_GO;
250 case P2P_GROUP_INTERFACE_CLIENT:
251 return WPA_IF_P2P_CLIENT;
252 }
253
254 return WPA_IF_P2P_GROUP;
255}
256
257
258static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
259 const u8 *ssid,
260 size_t ssid_len, int *go)
261{
262 struct wpa_ssid *s;
263
264 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
265 for (s = wpa_s->conf->ssid; s; s = s->next) {
266 if (s->disabled != 0 || !s->p2p_group ||
267 s->ssid_len != ssid_len ||
268 os_memcmp(ssid, s->ssid, ssid_len) != 0)
269 continue;
270 if (s->mode == WPAS_MODE_P2P_GO &&
271 s != wpa_s->current_ssid)
272 continue;
273 if (go)
274 *go = s->mode == WPAS_MODE_P2P_GO;
275 return wpa_s;
276 }
277 }
278
279 return NULL;
280}
281
282
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700283static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
284 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285{
286 struct wpa_ssid *ssid;
287 char *gtype;
288 const char *reason;
289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 ssid = wpa_s->current_ssid;
291 if (ssid == NULL) {
292 /*
293 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700294 * pending P2P group interface waiting for provisioning or a
295 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296 */
297 ssid = wpa_s->conf->ssid;
298 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700299 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 break;
301 ssid = ssid->next;
302 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300303 if (ssid == NULL &&
304 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
305 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700306 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
307 "not found");
308 return -1;
309 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310 }
311 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
312 gtype = "GO";
313 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
314 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
315 wpa_s->reassociate = 0;
316 wpa_s->disconnected = 1;
317 wpa_supplicant_deauthenticate(wpa_s,
318 WLAN_REASON_DEAUTH_LEAVING);
319 gtype = "client";
320 } else
321 gtype = "GO";
322 if (wpa_s->cross_connect_in_use) {
323 wpa_s->cross_connect_in_use = 0;
324 wpa_msg(wpa_s->parent, MSG_INFO,
325 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
326 wpa_s->ifname, wpa_s->cross_connect_uplink);
327 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700328 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 case P2P_GROUP_REMOVAL_REQUESTED:
330 reason = " reason=REQUESTED";
331 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700332 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
333 reason = " reason=FORMATION_FAILED";
334 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
336 reason = " reason=IDLE";
337 break;
338 case P2P_GROUP_REMOVAL_UNAVAILABLE:
339 reason = " reason=UNAVAILABLE";
340 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700341 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
342 reason = " reason=GO_ENDING_SESSION";
343 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700344#ifdef ANDROID_P2P
345 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
346 reason = " reason=FREQ_CONFLICT";
347 break;
348#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 default:
350 reason = "";
351 break;
352 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700353 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700354 wpa_msg(wpa_s->parent, MSG_INFO,
355 P2P_EVENT_GROUP_REMOVED "%s %s%s",
356 wpa_s->ifname, gtype, reason);
357 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700359#ifdef ANDROID_P2P
360 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
361#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700362 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
363 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800364 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700365 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800366 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
367 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700368 wpa_s->p2p_in_provisioning = 0;
369 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700370
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700371 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
373
374 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
375 struct wpa_global *global;
376 char *ifname;
377 enum wpa_driver_if_type type;
378 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
379 wpa_s->ifname);
380 global = wpa_s->global;
381 ifname = os_strdup(wpa_s->ifname);
382 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700383 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 wpa_s = global->ifaces;
385 if (wpa_s && ifname)
386 wpa_drv_if_remove(wpa_s, type, ifname);
387 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300388 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 }
390
391 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
392 if (ssid && (ssid->p2p_group ||
393 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
394 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
395 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700396 if (ssid == wpa_s->current_ssid) {
397 wpa_sm_set_config(wpa_s->wpa, NULL);
398 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700400 }
401 /*
402 * Networks objects created during any P2P activities are not
403 * exposed out as they might/will confuse certain non-P2P aware
404 * applications since these network objects won't behave like
405 * regular ones.
406 *
407 * Likewise, we don't send out network removed signals for such
408 * network objects.
409 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410 wpa_config_remove_network(wpa_s->conf, id);
411 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800412 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700413 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 } else {
415 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
416 "found");
417 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700418 if (wpa_s->ap_iface)
419 wpa_supplicant_ap_deinit(wpa_s);
420 else
421 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700422
423 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424}
425
426
427static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
428 u8 *go_dev_addr,
429 const u8 *ssid, size_t ssid_len)
430{
431 struct wpa_bss *bss;
432 const u8 *bssid;
433 struct wpabuf *p2p;
434 u8 group_capab;
435 const u8 *addr;
436
437 if (wpa_s->go_params)
438 bssid = wpa_s->go_params->peer_interface_addr;
439 else
440 bssid = wpa_s->bssid;
441
442 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
443 if (bss == NULL) {
444 u8 iface_addr[ETH_ALEN];
445 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
446 iface_addr) == 0)
447 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
448 }
449 if (bss == NULL) {
450 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
451 "group is persistent - BSS " MACSTR " not found",
452 MAC2STR(bssid));
453 return 0;
454 }
455
456 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
457 if (p2p == NULL) {
458 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
459 "group is persistent - BSS " MACSTR
460 " did not include P2P IE", MAC2STR(bssid));
461 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
462 (u8 *) (bss + 1), bss->ie_len);
463 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
464 ((u8 *) bss + 1) + bss->ie_len,
465 bss->beacon_ie_len);
466 return 0;
467 }
468
469 group_capab = p2p_get_group_capab(p2p);
470 addr = p2p_get_go_dev_addr(p2p);
471 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
472 "group_capab=0x%x", group_capab);
473 if (addr) {
474 os_memcpy(go_dev_addr, addr, ETH_ALEN);
475 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
476 MAC2STR(addr));
477 } else
478 os_memset(go_dev_addr, 0, ETH_ALEN);
479 wpabuf_free(p2p);
480
481 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
482 "go_dev_addr=" MACSTR,
483 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
484
485 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
486}
487
488
Jouni Malinen75ecf522011-06-27 15:19:46 -0700489static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
490 struct wpa_ssid *ssid,
491 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700492{
493 struct wpa_ssid *s;
494 int changed = 0;
495
496 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
497 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
498 for (s = wpa_s->conf->ssid; s; s = s->next) {
499 if (s->disabled == 2 &&
500 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
501 s->ssid_len == ssid->ssid_len &&
502 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
503 break;
504 }
505
506 if (s) {
507 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
508 "entry");
509 if (ssid->passphrase && !s->passphrase)
510 changed = 1;
511 else if (ssid->passphrase && s->passphrase &&
512 os_strcmp(ssid->passphrase, s->passphrase) != 0)
513 changed = 1;
514 } else {
515 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
516 "entry");
517 changed = 1;
518 s = wpa_config_add_network(wpa_s->conf);
519 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700520 return -1;
521
522 /*
523 * Instead of network_added we emit persistent_group_added
524 * notification. Also to keep the defense checks in
525 * persistent_group obj registration method, we set the
526 * relevant flags in s to designate it as a persistent group.
527 */
528 s->p2p_group = 1;
529 s->p2p_persistent_group = 1;
530 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 wpa_config_set_network_defaults(s);
532 }
533
534 s->p2p_group = 1;
535 s->p2p_persistent_group = 1;
536 s->disabled = 2;
537 s->bssid_set = 1;
538 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
539 s->mode = ssid->mode;
540 s->auth_alg = WPA_AUTH_ALG_OPEN;
541 s->key_mgmt = WPA_KEY_MGMT_PSK;
542 s->proto = WPA_PROTO_RSN;
543 s->pairwise_cipher = WPA_CIPHER_CCMP;
544 s->export_keys = 1;
545 if (ssid->passphrase) {
546 os_free(s->passphrase);
547 s->passphrase = os_strdup(ssid->passphrase);
548 }
549 if (ssid->psk_set) {
550 s->psk_set = 1;
551 os_memcpy(s->psk, ssid->psk, 32);
552 }
553 if (s->passphrase && !s->psk_set)
554 wpa_config_update_psk(s);
555 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
556 os_free(s->ssid);
557 s->ssid = os_malloc(ssid->ssid_len);
558 }
559 if (s->ssid) {
560 s->ssid_len = ssid->ssid_len;
561 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
562 }
563
564#ifndef CONFIG_NO_CONFIG_WRITE
565 if (changed && wpa_s->conf->update_config &&
566 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
567 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
568 }
569#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700570
571 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572}
573
574
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800575static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
576 const u8 *addr)
577{
578 struct wpa_ssid *ssid, *s;
579 u8 *n;
580 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700581 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800582
583 ssid = wpa_s->current_ssid;
584 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
585 !ssid->p2p_persistent_group)
586 return;
587
588 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
589 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
590 continue;
591
592 if (s->ssid_len == ssid->ssid_len &&
593 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
594 break;
595 }
596
597 if (s == NULL)
598 return;
599
600 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
601 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700602 ETH_ALEN) != 0)
603 continue;
604
605 if (i == s->num_p2p_clients - 1)
606 return; /* already the most recent entry */
607
608 /* move the entry to mark it most recent */
609 os_memmove(s->p2p_client_list + i * ETH_ALEN,
610 s->p2p_client_list + (i + 1) * ETH_ALEN,
611 (s->num_p2p_clients - i - 1) * ETH_ALEN);
612 os_memcpy(s->p2p_client_list +
613 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
614 found = 1;
615 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800616 }
617
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700618 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
619 n = os_realloc_array(s->p2p_client_list,
620 s->num_p2p_clients + 1, ETH_ALEN);
621 if (n == NULL)
622 return;
623 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
624 s->p2p_client_list = n;
625 s->num_p2p_clients++;
626 } else if (!found) {
627 /* Not enough room for an additional entry - drop the oldest
628 * entry */
629 os_memmove(s->p2p_client_list,
630 s->p2p_client_list + ETH_ALEN,
631 (s->num_p2p_clients - 1) * ETH_ALEN);
632 os_memcpy(s->p2p_client_list +
633 (s->num_p2p_clients - 1) * ETH_ALEN,
634 addr, ETH_ALEN);
635 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800636
637#ifndef CONFIG_NO_CONFIG_WRITE
638 if (wpa_s->parent->conf->update_config &&
639 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
640 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
641#endif /* CONFIG_NO_CONFIG_WRITE */
642}
643
644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
646 int success)
647{
648 struct wpa_ssid *ssid;
649 const char *ssid_txt;
650 int client;
651 int persistent;
652 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700653 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654
655 /*
656 * This callback is likely called for the main interface. Update wpa_s
657 * to use the group interface if a new interface was created for the
658 * group.
659 */
660 if (wpa_s->global->p2p_group_formation)
661 wpa_s = wpa_s->global->p2p_group_formation;
662 wpa_s->global->p2p_group_formation = NULL;
663 wpa_s->p2p_in_provisioning = 0;
664
665 if (!success) {
666 wpa_msg(wpa_s->parent, MSG_INFO,
667 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700668 wpas_p2p_group_delete(wpa_s,
669 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670 return;
671 }
672
673 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
674
675 ssid = wpa_s->current_ssid;
676 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
677 ssid->mode = WPAS_MODE_P2P_GO;
678 p2p_group_notif_formation_done(wpa_s->p2p_group);
679 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
680 }
681
682 persistent = 0;
683 if (ssid) {
684 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
685 client = ssid->mode == WPAS_MODE_INFRA;
686 if (ssid->mode == WPAS_MODE_P2P_GO) {
687 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700688 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
689 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 } else
691 persistent = wpas_p2p_persistent_group(wpa_s,
692 go_dev_addr,
693 ssid->ssid,
694 ssid->ssid_len);
695 } else {
696 ssid_txt = "";
697 client = wpa_s->p2p_group_interface ==
698 P2P_GROUP_INTERFACE_CLIENT;
699 os_memset(go_dev_addr, 0, ETH_ALEN);
700 }
701
702 wpa_s->show_group_started = 0;
703 if (client) {
704 /*
705 * Indicate event only after successfully completed 4-way
706 * handshake, i.e., when the interface is ready for data
707 * packets.
708 */
709 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700710#ifdef ANDROID_P2P
711 /* For client Second phase of Group formation (4-way handshake) can be still pending
712 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700713 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 -0700714 wpa_s->global->p2p_group_formation = wpa_s;
715#endif
716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
718 char psk[65];
719 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
720 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
721 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
722 "%s",
723 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
724 MAC2STR(go_dev_addr),
725 persistent ? " [PERSISTENT]" : "");
726 wpas_p2p_cross_connect_setup(wpa_s);
727 wpas_p2p_set_group_idle_timeout(wpa_s);
728 } else {
729 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
730 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
731 "go_dev_addr=" MACSTR "%s",
732 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
733 ssid && ssid->passphrase ? ssid->passphrase : "",
734 MAC2STR(go_dev_addr),
735 persistent ? " [PERSISTENT]" : "");
736 wpas_p2p_cross_connect_setup(wpa_s);
737 wpas_p2p_set_group_idle_timeout(wpa_s);
738 }
739
740 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700741 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
742 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800743 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700744 network_id = ssid->id;
745 if (!client)
746 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747}
748
749
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800750static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
751 unsigned int freq,
752 const u8 *dst, const u8 *src,
753 const u8 *bssid,
754 const u8 *data, size_t data_len,
755 enum offchannel_send_action_result
756 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700757{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800758 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
761 return;
762 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
763 return;
764
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800765 switch (result) {
766 case OFFCHANNEL_SEND_ACTION_SUCCESS:
767 res = P2P_SEND_ACTION_SUCCESS;
768 break;
769 case OFFCHANNEL_SEND_ACTION_NO_ACK:
770 res = P2P_SEND_ACTION_NO_ACK;
771 break;
772 case OFFCHANNEL_SEND_ACTION_FAILED:
773 res = P2P_SEND_ACTION_FAILED;
774 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 }
776
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800779 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
780 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800781 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800782 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
783 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800785 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
786 "during p2p_connect-auto");
787 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
788 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789 }
790}
791
792
793static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
794 const u8 *src, const u8 *bssid, const u8 *buf,
795 size_t len, unsigned int wait_time)
796{
797 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
799 wait_time,
800 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801}
802
803
804static void wpas_send_action_done(void *ctx)
805{
806 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800807 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808}
809
810
811static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
812 struct p2p_go_neg_results *params)
813{
814 if (wpa_s->go_params == NULL) {
815 wpa_s->go_params = os_malloc(sizeof(*params));
816 if (wpa_s->go_params == NULL)
817 return -1;
818 }
819 os_memcpy(wpa_s->go_params, params, sizeof(*params));
820 return 0;
821}
822
823
824static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
825 struct p2p_go_neg_results *res)
826{
827 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
828 MAC2STR(res->peer_interface_addr));
829 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
830 res->ssid, res->ssid_len);
831 wpa_supplicant_ap_deinit(wpa_s);
832 wpas_copy_go_neg_results(wpa_s, res);
833 if (res->wps_method == WPS_PBC)
834 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
835 else {
836 u16 dev_pw_id = DEV_PW_DEFAULT;
837 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
838 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
839 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
840 wpa_s->p2p_pin, 1, dev_pw_id);
841 }
842}
843
844
845static void p2p_go_configured(void *ctx, void *data)
846{
847 struct wpa_supplicant *wpa_s = ctx;
848 struct p2p_go_neg_results *params = data;
849 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700850 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851
852 ssid = wpa_s->current_ssid;
853 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
854 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
855 if (wpa_s->global->p2p_group_formation == wpa_s)
856 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800857 if (os_strlen(params->passphrase) > 0) {
858 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
859 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
860 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
861 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
862 ssid->frequency, params->passphrase,
863 MAC2STR(wpa_s->global->p2p_dev_addr),
864 params->persistent_group ? " [PERSISTENT]" :
865 "");
866 } else {
867 char psk[65];
868 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
869 sizeof(params->psk));
870 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
871 "%s GO ssid=\"%s\" freq=%d psk=%s "
872 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
873 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
874 ssid->frequency, psk,
875 MAC2STR(wpa_s->global->p2p_dev_addr),
876 params->persistent_group ? " [PERSISTENT]" :
877 "");
878 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700881 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700883 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700884 if (network_id < 0)
885 network_id = ssid->id;
886 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 wpas_p2p_cross_connect_setup(wpa_s);
888 wpas_p2p_set_group_idle_timeout(wpa_s);
889 return;
890 }
891
892 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
893 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
894 params->peer_interface_addr)) {
895 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
896 "filtering");
897 return;
898 }
899 if (params->wps_method == WPS_PBC)
900 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800901 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 else if (wpa_s->p2p_pin[0])
903 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800904 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 os_free(wpa_s->go_params);
906 wpa_s->go_params = NULL;
907}
908
909
910static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
911 struct p2p_go_neg_results *params,
912 int group_formation)
913{
914 struct wpa_ssid *ssid;
915
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700916 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
917 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
918 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
919 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922
923 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700924 if (ssid == NULL) {
925 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700927 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800929 wpa_s->show_group_started = 0;
930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 wpa_config_set_network_defaults(ssid);
932 ssid->temporary = 1;
933 ssid->p2p_group = 1;
934 ssid->p2p_persistent_group = params->persistent_group;
935 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
936 WPAS_MODE_P2P_GO;
937 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700938 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 ssid->ssid = os_zalloc(params->ssid_len + 1);
940 if (ssid->ssid) {
941 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
942 ssid->ssid_len = params->ssid_len;
943 }
944 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
945 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
946 ssid->proto = WPA_PROTO_RSN;
947 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800948 if (os_strlen(params->passphrase) > 0) {
949 ssid->passphrase = os_strdup(params->passphrase);
950 if (ssid->passphrase == NULL) {
951 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
952 "passphrase for GO");
953 wpa_config_remove_network(wpa_s->conf, ssid->id);
954 return;
955 }
956 } else
957 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800958 ssid->psk_set = params->psk_set;
959 if (ssid->psk_set)
960 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800961 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800962 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700963 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964
965 wpa_s->ap_configured_cb = p2p_go_configured;
966 wpa_s->ap_configured_cb_ctx = wpa_s;
967 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700968 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969 wpa_s->reassociate = 1;
970 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700971 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
972 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 wpa_supplicant_req_scan(wpa_s, 0, 0);
974}
975
976
977static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
978 const struct wpa_supplicant *src)
979{
980 struct wpa_config *d;
981 const struct wpa_config *s;
982
983 d = dst->conf;
984 s = src->conf;
985
986#define C(n) if (s->n) d->n = os_strdup(s->n)
987 C(device_name);
988 C(manufacturer);
989 C(model_name);
990 C(model_number);
991 C(serial_number);
992 C(config_methods);
993#undef C
994
995 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
996 os_memcpy(d->sec_device_type, s->sec_device_type,
997 sizeof(d->sec_device_type));
998 d->num_sec_device_types = s->num_sec_device_types;
999
1000 d->p2p_group_idle = s->p2p_group_idle;
1001 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001002 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001003 d->max_num_sta = s->max_num_sta;
1004 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001005 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001006 d->beacon_int = s->beacon_int;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007}
1008
1009
1010static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1011 enum wpa_driver_if_type type)
1012{
1013 char ifname[120], force_ifname[120];
1014
1015 if (wpa_s->pending_interface_name[0]) {
1016 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1017 "- skip creation of a new one");
1018 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1019 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1020 "unknown?! ifname='%s'",
1021 wpa_s->pending_interface_name);
1022 return -1;
1023 }
1024 return 0;
1025 }
1026
1027 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
1028 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001029 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1031 /* Try to avoid going over the IFNAMSIZ length limit */
1032 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1033 wpa_s->p2p_group_idx);
1034 }
1035 force_ifname[0] = '\0';
1036
1037 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1038 ifname);
1039 wpa_s->p2p_group_idx++;
1040
1041 wpa_s->pending_interface_type = type;
1042 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1043 wpa_s->pending_interface_addr, NULL) < 0) {
1044 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1045 "interface");
1046 return -1;
1047 }
1048
1049 if (force_ifname[0]) {
1050 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1051 force_ifname);
1052 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1053 sizeof(wpa_s->pending_interface_name));
1054 } else
1055 os_strlcpy(wpa_s->pending_interface_name, ifname,
1056 sizeof(wpa_s->pending_interface_name));
1057 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1058 MACSTR, wpa_s->pending_interface_name,
1059 MAC2STR(wpa_s->pending_interface_addr));
1060
1061 return 0;
1062}
1063
1064
1065static void wpas_p2p_remove_pending_group_interface(
1066 struct wpa_supplicant *wpa_s)
1067{
1068 if (!wpa_s->pending_interface_name[0] ||
1069 is_zero_ether_addr(wpa_s->pending_interface_addr))
1070 return; /* No pending virtual interface */
1071
1072 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1073 wpa_s->pending_interface_name);
1074 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1075 wpa_s->pending_interface_name);
1076 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1077 wpa_s->pending_interface_name[0] = '\0';
1078}
1079
1080
1081static struct wpa_supplicant *
1082wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1083{
1084 struct wpa_interface iface;
1085 struct wpa_supplicant *group_wpa_s;
1086
1087 if (!wpa_s->pending_interface_name[0]) {
1088 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1089 if (!wpas_p2p_create_iface(wpa_s))
1090 return NULL;
1091 /*
1092 * Something has forced us to remove the pending interface; try
1093 * to create a new one and hope for the best that we will get
1094 * the same local address.
1095 */
1096 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1097 WPA_IF_P2P_CLIENT) < 0)
1098 return NULL;
1099 }
1100
1101 os_memset(&iface, 0, sizeof(iface));
1102 iface.ifname = wpa_s->pending_interface_name;
1103 iface.driver = wpa_s->driver->name;
1104 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1105 iface.driver_param = wpa_s->conf->driver_param;
1106 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1107 if (group_wpa_s == NULL) {
1108 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1109 "wpa_supplicant interface");
1110 return NULL;
1111 }
1112 wpa_s->pending_interface_name[0] = '\0';
1113 group_wpa_s->parent = wpa_s;
1114 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1115 P2P_GROUP_INTERFACE_CLIENT;
1116 wpa_s->global->p2p_group_formation = group_wpa_s;
1117
1118 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1119
1120 return group_wpa_s;
1121}
1122
1123
1124static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1125 void *timeout_ctx)
1126{
1127 struct wpa_supplicant *wpa_s = eloop_ctx;
1128 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1129 if (wpa_s->global->p2p)
1130 p2p_group_formation_failed(wpa_s->global->p2p);
1131 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1132 wpa_drv_p2p_group_formation_failed(wpa_s);
1133 wpas_group_formation_completed(wpa_s, 0);
1134}
1135
1136
1137void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1138{
1139 struct wpa_supplicant *wpa_s = ctx;
1140
1141 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1142 wpa_drv_cancel_remain_on_channel(wpa_s);
1143 wpa_s->off_channel_freq = 0;
1144 wpa_s->roc_waiting_drv_freq = 0;
1145 }
1146
1147 if (res->status) {
1148 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1149 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001150 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 wpas_p2p_remove_pending_group_interface(wpa_s);
1152 return;
1153 }
1154
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001155 if (wpa_s->p2p_go_ht40)
1156 res->ht40 = 1;
1157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001159 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160
Dmitry Shmidt04949592012-07-19 12:16:46 -07001161 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1162 struct wpa_ssid *ssid;
1163 ssid = wpa_config_get_network(wpa_s->conf,
1164 wpa_s->p2p_persistent_id);
1165 if (ssid && ssid->disabled == 2 &&
1166 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1167 size_t len = os_strlen(ssid->passphrase);
1168 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1169 "on requested persistent group");
1170 os_memcpy(res->passphrase, ssid->passphrase, len);
1171 res->passphrase[len] = '\0';
1172 }
1173 }
1174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 if (wpa_s->create_p2p_iface) {
1176 struct wpa_supplicant *group_wpa_s =
1177 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1178 if (group_wpa_s == NULL) {
1179 wpas_p2p_remove_pending_group_interface(wpa_s);
1180 return;
1181 }
1182 if (group_wpa_s != wpa_s) {
1183 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1184 sizeof(group_wpa_s->p2p_pin));
1185 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1186 }
1187 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1188 wpa_s->pending_interface_name[0] = '\0';
1189 group_wpa_s->p2p_in_provisioning = 1;
1190
1191 if (res->role_go)
1192 wpas_start_wps_go(group_wpa_s, res, 1);
1193 else
1194 wpas_start_wps_enrollee(group_wpa_s, res);
1195 } else {
1196 wpa_s->p2p_in_provisioning = 1;
1197 wpa_s->global->p2p_group_formation = wpa_s;
1198
1199 if (res->role_go)
1200 wpas_start_wps_go(wpa_s, res, 1);
1201 else
1202 wpas_start_wps_enrollee(ctx, res);
1203 }
1204
1205 wpa_s->p2p_long_listen = 0;
1206 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1207
1208 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1209 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1210 (res->peer_config_timeout % 100) * 10000,
1211 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1212}
1213
1214
1215void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1216{
1217 struct wpa_supplicant *wpa_s = ctx;
1218 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1219 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1220
1221 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1222}
1223
1224
1225void wpas_dev_found(void *ctx, const u8 *addr,
1226 const struct p2p_peer_info *info,
1227 int new_device)
1228{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001229#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230 struct wpa_supplicant *wpa_s = ctx;
1231 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001232#define WFD_DEV_INFO_SIZE 9
1233 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001234 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001235#ifdef CONFIG_WIFI_DISPLAY
1236 if (info->wfd_subelems) {
1237 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1238 wpabuf_head(info->wfd_subelems),
1239 WFD_DEV_INFO_SIZE);
1240 }
1241#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1243 " p2p_dev_addr=" MACSTR
1244 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001245 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1247 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1248 sizeof(devtype)),
1249 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001250 info->dev_capab, info->group_capab,
1251 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001252#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253
1254 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1255}
1256
1257
1258static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1259{
1260 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001261
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001262 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1263 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1266}
1267
1268
1269static int wpas_start_listen(void *ctx, unsigned int freq,
1270 unsigned int duration,
1271 const struct wpabuf *probe_resp_ie)
1272{
1273 struct wpa_supplicant *wpa_s = ctx;
1274
1275 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1276
1277 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1278 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1279 "report received Probe Request frames");
1280 return -1;
1281 }
1282
1283 wpa_s->pending_listen_freq = freq;
1284 wpa_s->pending_listen_duration = duration;
1285
1286 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1287 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1288 "to remain on channel (%u MHz) for Listen "
1289 "state", freq);
1290 wpa_s->pending_listen_freq = 0;
1291 return -1;
1292 }
1293 wpa_s->off_channel_freq = 0;
1294 wpa_s->roc_waiting_drv_freq = freq;
1295
1296 return 0;
1297}
1298
1299
1300static void wpas_stop_listen(void *ctx)
1301{
1302 struct wpa_supplicant *wpa_s = ctx;
1303 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1304 wpa_drv_cancel_remain_on_channel(wpa_s);
1305 wpa_s->off_channel_freq = 0;
1306 wpa_s->roc_waiting_drv_freq = 0;
1307 }
1308 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1309 wpa_drv_probe_req_report(wpa_s, 0);
1310}
1311
1312
1313static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1314{
1315 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001316 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317}
1318
1319
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001320/*
1321 * DNS Header section is used only to calculate compression pointers, so the
1322 * contents of this data does not matter, but the length needs to be reserved
1323 * in the virtual packet.
1324 */
1325#define DNS_HEADER_LEN 12
1326
1327/*
1328 * 27-octet in-memory packet from P2P specification containing two implied
1329 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1330 */
1331#define P2P_SD_IN_MEMORY_LEN 27
1332
1333static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1334 u8 **spos, const u8 *end)
1335{
1336 while (*spos < end) {
1337 u8 val = ((*spos)[0] & 0xc0) >> 6;
1338 int len;
1339
1340 if (val == 1 || val == 2) {
1341 /* These are reserved values in RFC 1035 */
1342 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1343 "sequence starting with 0x%x", val);
1344 return -1;
1345 }
1346
1347 if (val == 3) {
1348 u16 offset;
1349 u8 *spos_tmp;
1350
1351 /* Offset */
1352 if (*spos + 2 > end) {
1353 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1354 "DNS offset field");
1355 return -1;
1356 }
1357
1358 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1359 if (offset >= *spos - start) {
1360 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1361 "pointer offset %u", offset);
1362 return -1;
1363 }
1364
1365 (*spos) += 2;
1366 spos_tmp = start + offset;
1367 return p2p_sd_dns_uncompress_label(upos, uend, start,
1368 &spos_tmp,
1369 *spos - 2);
1370 }
1371
1372 /* Label */
1373 len = (*spos)[0] & 0x3f;
1374 if (len == 0)
1375 return 0;
1376
1377 (*spos)++;
1378 if (*spos + len > end) {
1379 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1380 "sequence - no room for label with length "
1381 "%u", len);
1382 return -1;
1383 }
1384
1385 if (*upos + len + 2 > uend)
1386 return -2;
1387
1388 os_memcpy(*upos, *spos, len);
1389 *spos += len;
1390 *upos += len;
1391 (*upos)[0] = '.';
1392 (*upos)++;
1393 (*upos)[0] = '\0';
1394 }
1395
1396 return 0;
1397}
1398
1399
1400/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1401 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1402 * not large enough */
1403static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1404 size_t msg_len, size_t offset)
1405{
1406 /* 27-octet in-memory packet from P2P specification */
1407 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1408 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1409 u8 *tmp, *end, *spos;
1410 char *upos, *uend;
1411 int ret = 0;
1412
1413 if (buf_len < 2)
1414 return -1;
1415 if (offset > msg_len)
1416 return -1;
1417
1418 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1419 if (tmp == NULL)
1420 return -1;
1421 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1422 end = spos + msg_len;
1423 spos += offset;
1424
1425 os_memset(tmp, 0, DNS_HEADER_LEN);
1426 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1427 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1428
1429 upos = buf;
1430 uend = buf + buf_len;
1431
1432 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1433 if (ret) {
1434 os_free(tmp);
1435 return ret;
1436 }
1437
1438 if (upos == buf) {
1439 upos[0] = '.';
1440 upos[1] = '\0';
1441 } else if (upos[-1] == '.')
1442 upos[-1] = '\0';
1443
1444 os_free(tmp);
1445 return 0;
1446}
1447
1448
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449static struct p2p_srv_bonjour *
1450wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1451 const struct wpabuf *query)
1452{
1453 struct p2p_srv_bonjour *bsrv;
1454 size_t len;
1455
1456 len = wpabuf_len(query);
1457 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1458 struct p2p_srv_bonjour, list) {
1459 if (len == wpabuf_len(bsrv->query) &&
1460 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1461 len) == 0)
1462 return bsrv;
1463 }
1464 return NULL;
1465}
1466
1467
1468static struct p2p_srv_upnp *
1469wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1470 const char *service)
1471{
1472 struct p2p_srv_upnp *usrv;
1473
1474 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1475 struct p2p_srv_upnp, list) {
1476 if (version == usrv->version &&
1477 os_strcmp(service, usrv->service) == 0)
1478 return usrv;
1479 }
1480 return NULL;
1481}
1482
1483
1484static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1485 u8 srv_trans_id)
1486{
1487 u8 *len_pos;
1488
1489 if (wpabuf_tailroom(resp) < 5)
1490 return;
1491
1492 /* Length (to be filled) */
1493 len_pos = wpabuf_put(resp, 2);
1494 wpabuf_put_u8(resp, srv_proto);
1495 wpabuf_put_u8(resp, srv_trans_id);
1496 /* Status Code */
1497 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1498 /* Response Data: empty */
1499 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1500}
1501
1502
1503static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1504 struct wpabuf *resp, u8 srv_trans_id)
1505{
1506 struct p2p_srv_bonjour *bsrv;
1507 u8 *len_pos;
1508
1509 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1510
1511 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1512 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1513 return;
1514 }
1515
1516 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1517 struct p2p_srv_bonjour, list) {
1518 if (wpabuf_tailroom(resp) <
1519 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1520 return;
1521 /* Length (to be filled) */
1522 len_pos = wpabuf_put(resp, 2);
1523 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1524 wpabuf_put_u8(resp, srv_trans_id);
1525 /* Status Code */
1526 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1527 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1528 wpabuf_head(bsrv->resp),
1529 wpabuf_len(bsrv->resp));
1530 /* Response Data */
1531 wpabuf_put_buf(resp, bsrv->query); /* Key */
1532 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1533 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1534 2);
1535 }
1536}
1537
1538
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001539static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1540 size_t query_len)
1541{
1542 char str_rx[256], str_srv[256];
1543
1544 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1545 return 0; /* Too short to include DNS Type and Version */
1546 if (os_memcmp(query + query_len - 3,
1547 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1548 3) != 0)
1549 return 0; /* Mismatch in DNS Type or Version */
1550 if (query_len == wpabuf_len(bsrv->query) &&
1551 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1552 return 1; /* Binary match */
1553
1554 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1555 0))
1556 return 0; /* Failed to uncompress query */
1557 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1558 wpabuf_head(bsrv->query),
1559 wpabuf_len(bsrv->query) - 3, 0))
1560 return 0; /* Failed to uncompress service */
1561
1562 return os_strcmp(str_rx, str_srv) == 0;
1563}
1564
1565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1567 struct wpabuf *resp, u8 srv_trans_id,
1568 const u8 *query, size_t query_len)
1569{
1570 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001572 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573
1574 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1575 query, query_len);
1576 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1577 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1578 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1579 srv_trans_id);
1580 return;
1581 }
1582
1583 if (query_len == 0) {
1584 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1585 return;
1586 }
1587
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001588 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1589 struct p2p_srv_bonjour, list) {
1590 if (!match_bonjour_query(bsrv, query, query_len))
1591 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001593 if (wpabuf_tailroom(resp) <
1594 5 + query_len + wpabuf_len(bsrv->resp))
1595 return;
1596
1597 matches++;
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);
1603
1604 /* Status Code */
1605 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1606 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1607 wpabuf_head(bsrv->resp),
1608 wpabuf_len(bsrv->resp));
1609
1610 /* Response Data */
1611 wpabuf_put_data(resp, query, query_len); /* Key */
1612 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1613
1614 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1615 }
1616
1617 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001618 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1619 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001620 if (wpabuf_tailroom(resp) < 5)
1621 return;
1622
1623 /* Length (to be filled) */
1624 len_pos = wpabuf_put(resp, 2);
1625 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1626 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627
1628 /* Status Code */
1629 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1630 /* Response Data: empty */
1631 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1632 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634}
1635
1636
1637static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1638 struct wpabuf *resp, u8 srv_trans_id)
1639{
1640 struct p2p_srv_upnp *usrv;
1641 u8 *len_pos;
1642
1643 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1644
1645 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1646 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1647 return;
1648 }
1649
1650 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1651 struct p2p_srv_upnp, list) {
1652 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1653 return;
1654
1655 /* Length (to be filled) */
1656 len_pos = wpabuf_put(resp, 2);
1657 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1658 wpabuf_put_u8(resp, srv_trans_id);
1659
1660 /* Status Code */
1661 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1662 /* Response Data */
1663 wpabuf_put_u8(resp, usrv->version);
1664 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1665 usrv->service);
1666 wpabuf_put_str(resp, usrv->service);
1667 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1668 2);
1669 }
1670}
1671
1672
1673static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1674 struct wpabuf *resp, u8 srv_trans_id,
1675 const u8 *query, size_t query_len)
1676{
1677 struct p2p_srv_upnp *usrv;
1678 u8 *len_pos;
1679 u8 version;
1680 char *str;
1681 int count = 0;
1682
1683 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1684 query, query_len);
1685
1686 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1687 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1688 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1689 srv_trans_id);
1690 return;
1691 }
1692
1693 if (query_len == 0) {
1694 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1695 return;
1696 }
1697
1698 if (wpabuf_tailroom(resp) < 5)
1699 return;
1700
1701 /* Length (to be filled) */
1702 len_pos = wpabuf_put(resp, 2);
1703 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1704 wpabuf_put_u8(resp, srv_trans_id);
1705
1706 version = query[0];
1707 str = os_malloc(query_len);
1708 if (str == NULL)
1709 return;
1710 os_memcpy(str, query + 1, query_len - 1);
1711 str[query_len - 1] = '\0';
1712
1713 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1714 struct p2p_srv_upnp, list) {
1715 if (version != usrv->version)
1716 continue;
1717
1718 if (os_strcmp(str, "ssdp:all") != 0 &&
1719 os_strstr(usrv->service, str) == NULL)
1720 continue;
1721
1722 if (wpabuf_tailroom(resp) < 2)
1723 break;
1724 if (count == 0) {
1725 /* Status Code */
1726 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1727 /* Response Data */
1728 wpabuf_put_u8(resp, version);
1729 } else
1730 wpabuf_put_u8(resp, ',');
1731
1732 count++;
1733
1734 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1735 usrv->service);
1736 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1737 break;
1738 wpabuf_put_str(resp, usrv->service);
1739 }
1740 os_free(str);
1741
1742 if (count == 0) {
1743 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1744 "available");
1745 /* Status Code */
1746 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1747 /* Response Data: empty */
1748 }
1749
1750 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1751}
1752
1753
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001754#ifdef CONFIG_WIFI_DISPLAY
1755static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1756 struct wpabuf *resp, u8 srv_trans_id,
1757 const u8 *query, size_t query_len)
1758{
1759 const u8 *pos;
1760 u8 role;
1761 u8 *len_pos;
1762
1763 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1764
1765 if (!wpa_s->global->wifi_display) {
1766 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1767 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1768 srv_trans_id);
1769 return;
1770 }
1771
1772 if (query_len < 1) {
1773 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1774 "Role");
1775 return;
1776 }
1777
1778 if (wpabuf_tailroom(resp) < 5)
1779 return;
1780
1781 pos = query;
1782 role = *pos++;
1783 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1784
1785 /* TODO: role specific handling */
1786
1787 /* Length (to be filled) */
1788 len_pos = wpabuf_put(resp, 2);
1789 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1790 wpabuf_put_u8(resp, srv_trans_id);
1791 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1792
1793 while (pos < query + query_len) {
1794 if (*pos < MAX_WFD_SUBELEMS &&
1795 wpa_s->global->wfd_subelem[*pos] &&
1796 wpabuf_tailroom(resp) >=
1797 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1798 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1799 "subelement %u", *pos);
1800 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1801 }
1802 pos++;
1803 }
1804
1805 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1806}
1807#endif /* CONFIG_WIFI_DISPLAY */
1808
1809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1811 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1812{
1813 struct wpa_supplicant *wpa_s = ctx;
1814 const u8 *pos = tlvs;
1815 const u8 *end = tlvs + tlvs_len;
1816 const u8 *tlv_end;
1817 u16 slen;
1818 struct wpabuf *resp;
1819 u8 srv_proto, srv_trans_id;
1820 size_t buf_len;
1821 char *buf;
1822
1823 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1824 tlvs, tlvs_len);
1825 buf_len = 2 * tlvs_len + 1;
1826 buf = os_malloc(buf_len);
1827 if (buf) {
1828 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1829 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1830 MACSTR " %u %u %s",
1831 freq, MAC2STR(sa), dialog_token, update_indic,
1832 buf);
1833 os_free(buf);
1834 }
1835
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001836 if (wpa_s->p2p_sd_over_ctrl_iface) {
1837 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1838 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001840 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001841
1842 resp = wpabuf_alloc(10000);
1843 if (resp == NULL)
1844 return;
1845
1846 while (pos + 1 < end) {
1847 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1848 slen = WPA_GET_LE16(pos);
1849 pos += 2;
1850 if (pos + slen > end || slen < 2) {
1851 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1852 "length");
1853 wpabuf_free(resp);
1854 return;
1855 }
1856 tlv_end = pos + slen;
1857
1858 srv_proto = *pos++;
1859 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1860 srv_proto);
1861 srv_trans_id = *pos++;
1862 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1863 srv_trans_id);
1864
1865 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1866 pos, tlv_end - pos);
1867
1868
1869 if (wpa_s->force_long_sd) {
1870 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1871 "response");
1872 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1873 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1874 goto done;
1875 }
1876
1877 switch (srv_proto) {
1878 case P2P_SERV_ALL_SERVICES:
1879 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1880 "for all services");
1881 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1882 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1883 wpa_printf(MSG_DEBUG, "P2P: No service "
1884 "discovery protocols available");
1885 wpas_sd_add_proto_not_avail(
1886 resp, P2P_SERV_ALL_SERVICES,
1887 srv_trans_id);
1888 break;
1889 }
1890 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1891 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1892 break;
1893 case P2P_SERV_BONJOUR:
1894 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1895 pos, tlv_end - pos);
1896 break;
1897 case P2P_SERV_UPNP:
1898 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1899 pos, tlv_end - pos);
1900 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001901#ifdef CONFIG_WIFI_DISPLAY
1902 case P2P_SERV_WIFI_DISPLAY:
1903 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1904 pos, tlv_end - pos);
1905 break;
1906#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907 default:
1908 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1909 "protocol %u", srv_proto);
1910 wpas_sd_add_proto_not_avail(resp, srv_proto,
1911 srv_trans_id);
1912 break;
1913 }
1914
1915 pos = tlv_end;
1916 }
1917
1918done:
1919 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1920 update_indic, tlvs, tlvs_len);
1921
1922 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1923
1924 wpabuf_free(resp);
1925}
1926
1927
1928void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1929 const u8 *tlvs, size_t tlvs_len)
1930{
1931 struct wpa_supplicant *wpa_s = ctx;
1932 const u8 *pos = tlvs;
1933 const u8 *end = tlvs + tlvs_len;
1934 const u8 *tlv_end;
1935 u16 slen;
1936 size_t buf_len;
1937 char *buf;
1938
1939 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1940 tlvs, tlvs_len);
1941 if (tlvs_len > 1500) {
1942 /* TODO: better way for handling this */
1943 wpa_msg_ctrl(wpa_s, MSG_INFO,
1944 P2P_EVENT_SERV_DISC_RESP MACSTR
1945 " %u <long response: %u bytes>",
1946 MAC2STR(sa), update_indic,
1947 (unsigned int) tlvs_len);
1948 } else {
1949 buf_len = 2 * tlvs_len + 1;
1950 buf = os_malloc(buf_len);
1951 if (buf) {
1952 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1953 wpa_msg_ctrl(wpa_s, MSG_INFO,
1954 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1955 MAC2STR(sa), update_indic, buf);
1956 os_free(buf);
1957 }
1958 }
1959
1960 while (pos < end) {
1961 u8 srv_proto, srv_trans_id, status;
1962
1963 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1964 slen = WPA_GET_LE16(pos);
1965 pos += 2;
1966 if (pos + slen > end || slen < 3) {
1967 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1968 "length");
1969 return;
1970 }
1971 tlv_end = pos + slen;
1972
1973 srv_proto = *pos++;
1974 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1975 srv_proto);
1976 srv_trans_id = *pos++;
1977 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1978 srv_trans_id);
1979 status = *pos++;
1980 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1981 status);
1982
1983 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1984 pos, tlv_end - pos);
1985
1986 pos = tlv_end;
1987 }
1988
1989 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1990}
1991
1992
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001993u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1994 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995{
1996 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001997 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001999 return 0;
2000 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001}
2002
2003
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002004u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2005 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002006{
2007 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002008 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009
2010 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2011 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002012 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2014 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2015 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2016 wpabuf_put_u8(tlvs, version);
2017 wpabuf_put_str(tlvs, query);
2018 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2019 wpabuf_free(tlvs);
2020 return ret;
2021}
2022
2023
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002024#ifdef CONFIG_WIFI_DISPLAY
2025
2026static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2027 const struct wpabuf *tlvs)
2028{
2029 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2030 return 0;
2031 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2032 return 0;
2033 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2034}
2035
2036
2037#define MAX_WFD_SD_SUBELEMS 20
2038
2039static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2040 const char *subelems)
2041{
2042 u8 *len;
2043 const char *pos;
2044 int val;
2045 int count = 0;
2046
2047 len = wpabuf_put(tlvs, 2);
2048 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2049 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2050
2051 wpabuf_put_u8(tlvs, role);
2052
2053 pos = subelems;
2054 while (*pos) {
2055 val = atoi(pos);
2056 if (val >= 0 && val < 256) {
2057 wpabuf_put_u8(tlvs, val);
2058 count++;
2059 if (count == MAX_WFD_SD_SUBELEMS)
2060 break;
2061 }
2062 pos = os_strchr(pos + 1, ',');
2063 if (pos == NULL)
2064 break;
2065 pos++;
2066 }
2067
2068 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2069}
2070
2071
2072u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2073 const u8 *dst, const char *role)
2074{
2075 struct wpabuf *tlvs;
2076 u64 ret;
2077 const char *subelems;
2078 u8 id = 1;
2079
2080 subelems = os_strchr(role, ' ');
2081 if (subelems == NULL)
2082 return 0;
2083 subelems++;
2084
2085 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2086 if (tlvs == NULL)
2087 return 0;
2088
2089 if (os_strstr(role, "[source]"))
2090 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2091 if (os_strstr(role, "[pri-sink]"))
2092 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2093 if (os_strstr(role, "[sec-sink]"))
2094 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2095 if (os_strstr(role, "[source+sink]"))
2096 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2097
2098 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2099 wpabuf_free(tlvs);
2100 return ret;
2101}
2102
2103#endif /* CONFIG_WIFI_DISPLAY */
2104
2105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002106int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107{
2108 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002109 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2111 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112 return p2p_sd_cancel_request(wpa_s->global->p2p,
2113 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114}
2115
2116
2117void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2118 const u8 *dst, u8 dialog_token,
2119 const struct wpabuf *resp_tlvs)
2120{
2121 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2122 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2123 resp_tlvs);
2124 return;
2125 }
2126 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2127 return;
2128 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2129 resp_tlvs);
2130}
2131
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002132#ifdef ANDROID_P2P
2133void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2134#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002136#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137{
2138 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2139 wpa_drv_p2p_service_update(wpa_s);
2140 return;
2141 }
2142 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002143#ifdef ANDROID_P2P
2144 p2p_sd_service_update(wpa_s->global->p2p, action);
2145#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002147#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148}
2149
2150
2151static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2152{
2153 dl_list_del(&bsrv->list);
2154 wpabuf_free(bsrv->query);
2155 wpabuf_free(bsrv->resp);
2156 os_free(bsrv);
2157}
2158
2159
2160static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2161{
2162 dl_list_del(&usrv->list);
2163 os_free(usrv->service);
2164 os_free(usrv);
2165}
2166
2167
2168void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2169{
2170 struct p2p_srv_bonjour *bsrv, *bn;
2171 struct p2p_srv_upnp *usrv, *un;
2172
2173 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2174 struct p2p_srv_bonjour, list)
2175 wpas_p2p_srv_bonjour_free(bsrv);
2176
2177 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2178 struct p2p_srv_upnp, list)
2179 wpas_p2p_srv_upnp_free(usrv);
2180
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002181#ifdef ANDROID_P2P
2182 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2183#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002185#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002186}
2187
2188
2189int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2190 struct wpabuf *query, struct wpabuf *resp)
2191{
2192 struct p2p_srv_bonjour *bsrv;
2193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 bsrv = os_zalloc(sizeof(*bsrv));
2195 if (bsrv == NULL)
2196 return -1;
2197 bsrv->query = query;
2198 bsrv->resp = resp;
2199 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2200
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002201#ifdef ANDROID_P2P
2202 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2203#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002205#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002206 return 0;
2207}
2208
2209
2210int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2211 const struct wpabuf *query)
2212{
2213 struct p2p_srv_bonjour *bsrv;
2214
2215 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2216 if (bsrv == NULL)
2217 return -1;
2218 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002219#ifdef ANDROID_P2P
2220 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2221#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002223#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002224 return 0;
2225}
2226
2227
2228int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2229 const char *service)
2230{
2231 struct p2p_srv_upnp *usrv;
2232
2233 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2234 return 0; /* Already listed */
2235 usrv = os_zalloc(sizeof(*usrv));
2236 if (usrv == NULL)
2237 return -1;
2238 usrv->version = version;
2239 usrv->service = os_strdup(service);
2240 if (usrv->service == NULL) {
2241 os_free(usrv);
2242 return -1;
2243 }
2244 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2245
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002246#ifdef ANDROID_P2P
2247 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2248#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002250#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251 return 0;
2252}
2253
2254
2255int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2256 const char *service)
2257{
2258 struct p2p_srv_upnp *usrv;
2259
2260 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2261 if (usrv == NULL)
2262 return -1;
2263 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002264#ifdef ANDROID_P2P
2265 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2266#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002268#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 return 0;
2270}
2271
2272
2273static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2274 const u8 *peer, const char *params,
2275 unsigned int generated_pin)
2276{
2277 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2278 MAC2STR(peer), generated_pin, params);
2279}
2280
2281
2282static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2283 const u8 *peer, const char *params)
2284{
2285 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2286 MAC2STR(peer), params);
2287}
2288
2289
2290void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2291 const u8 *dev_addr, const u8 *pri_dev_type,
2292 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002293 u8 dev_capab, u8 group_capab, const u8 *group_id,
2294 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295{
2296 struct wpa_supplicant *wpa_s = ctx;
2297 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002298 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 u8 empty_dev_type[8];
2300 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002301 struct wpa_supplicant *group = NULL;
2302
2303 if (group_id) {
2304 for (group = wpa_s->global->ifaces; group; group = group->next)
2305 {
2306 struct wpa_ssid *s = group->current_ssid;
2307 if (s != NULL &&
2308 s->mode == WPAS_MODE_P2P_GO &&
2309 group_id_len - ETH_ALEN == s->ssid_len &&
2310 os_memcmp(group_id + ETH_ALEN, s->ssid,
2311 s->ssid_len) == 0)
2312 break;
2313 }
2314 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315
2316 if (pri_dev_type == NULL) {
2317 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2318 pri_dev_type = empty_dev_type;
2319 }
2320 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2321 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002322 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323 MAC2STR(dev_addr),
2324 wps_dev_type_bin2str(pri_dev_type, devtype,
2325 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002326 dev_name, supp_config_methods, dev_capab, group_capab,
2327 group ? " group=" : "",
2328 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331 if (config_methods & WPS_CONFIG_DISPLAY) {
2332 generated_pin = wps_generate_pin();
2333 wpas_prov_disc_local_display(wpa_s, peer, params,
2334 generated_pin);
2335 } else if (config_methods & WPS_CONFIG_KEYPAD)
2336 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2337 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2338 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2339 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002340
2341 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2342 P2P_PROV_DISC_SUCCESS,
2343 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344}
2345
2346
2347void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2348{
2349 struct wpa_supplicant *wpa_s = ctx;
2350 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002351 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002353 if (wpa_s->pending_pd_before_join &&
2354 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2355 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2356 wpa_s->pending_pd_before_join = 0;
2357 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2358 "join-existing-group operation");
2359 wpas_p2p_join_start(wpa_s);
2360 return;
2361 }
2362
Dmitry Shmidt04949592012-07-19 12:16:46 -07002363 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2364 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2365 os_snprintf(params, sizeof(params), " peer_go=%d",
2366 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2367 else
2368 params[0] = '\0';
2369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002370 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002371 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 else if (config_methods & WPS_CONFIG_KEYPAD) {
2373 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002374 wpas_prov_disc_local_display(wpa_s, peer, params,
2375 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002376 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002377 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2378 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379
Jouni Malinen75ecf522011-06-27 15:19:46 -07002380 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2381 P2P_PROV_DISC_SUCCESS,
2382 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383}
2384
2385
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2387 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002388{
2389 struct wpa_supplicant *wpa_s = ctx;
2390
Dmitry Shmidt04949592012-07-19 12:16:46 -07002391 if (wpa_s->p2p_fallback_to_go_neg) {
2392 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2393 "failed - fall back to GO Negotiation");
2394 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2395 return;
2396 }
2397
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002398 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002399 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002400 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2401 "join-existing-group operation (no ACK for PD "
2402 "Req attempts)");
2403 wpas_p2p_join_start(wpa_s);
2404 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002405 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002406
Dmitry Shmidt04949592012-07-19 12:16:46 -07002407 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2408 " p2p_dev_addr=" MACSTR " status=%d",
2409 MAC2STR(peer), status);
2410
Jouni Malinen75ecf522011-06-27 15:19:46 -07002411 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2412 status, 0, 0);
2413}
2414
2415
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2417 const u8 *go_dev_addr, const u8 *ssid,
2418 size_t ssid_len, int *go, u8 *group_bssid,
2419 int *force_freq, int persistent_group)
2420{
2421 struct wpa_supplicant *wpa_s = ctx;
2422 struct wpa_ssid *s;
2423 u8 cur_bssid[ETH_ALEN];
2424 int res;
2425 struct wpa_supplicant *grp;
2426
2427 if (!persistent_group) {
2428 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2429 " to join an active group", MAC2STR(sa));
2430 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2431 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2432 == 0 ||
2433 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2434 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2435 "authorized invitation");
2436 goto accept_inv;
2437 }
2438 /*
2439 * Do not accept the invitation automatically; notify user and
2440 * request approval.
2441 */
2442 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2443 }
2444
2445 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2446 if (grp) {
2447 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2448 "running persistent group");
2449 if (*go)
2450 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2451 goto accept_inv;
2452 }
2453
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002454 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2455 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2456 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2457 "invitation to re-invoke a persistent group");
2458 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002459 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2460
2461 for (s = wpa_s->conf->ssid; s; s = s->next) {
2462 if (s->disabled == 2 &&
2463 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2464 s->ssid_len == ssid_len &&
2465 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2466 break;
2467 }
2468
2469 if (!s) {
2470 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2471 " requested reinvocation of an unknown group",
2472 MAC2STR(sa));
2473 return P2P_SC_FAIL_UNKNOWN_GROUP;
2474 }
2475
2476 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2477 *go = 1;
2478 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2479 wpa_printf(MSG_DEBUG, "P2P: The only available "
2480 "interface is already in use - reject "
2481 "invitation");
2482 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2483 }
2484 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2485 } else if (s->mode == WPAS_MODE_P2P_GO) {
2486 *go = 1;
2487 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2488 {
2489 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2490 "interface address for the group");
2491 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2492 }
2493 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2494 ETH_ALEN);
2495 }
2496
2497accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002498 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2501 wpa_s->assoc_freq) {
2502 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2503 "the channel we are already using");
2504 *force_freq = wpa_s->assoc_freq;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002505 wpas_p2p_set_own_freq_preference(wpa_s, wpa_s->assoc_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506 }
2507
2508 res = wpa_drv_shared_freq(wpa_s);
2509 if (res > 0) {
2510 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2511 "with the channel we are already using on a "
2512 "shared interface");
2513 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002514 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515 }
2516
2517 return P2P_SC_SUCCESS;
2518}
2519
2520
2521static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2522 const u8 *ssid, size_t ssid_len,
2523 const u8 *go_dev_addr, u8 status,
2524 int op_freq)
2525{
2526 struct wpa_supplicant *wpa_s = ctx;
2527 struct wpa_ssid *s;
2528
2529 for (s = wpa_s->conf->ssid; s; s = s->next) {
2530 if (s->disabled == 2 &&
2531 s->ssid_len == ssid_len &&
2532 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2533 break;
2534 }
2535
2536 if (status == P2P_SC_SUCCESS) {
2537 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2538 " was accepted; op_freq=%d MHz",
2539 MAC2STR(sa), op_freq);
2540 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002541 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542 wpas_p2p_group_add_persistent(
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002543 wpa_s, s, go, go ? op_freq : 0, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002545 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002547 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548 }
2549 return;
2550 }
2551
2552 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2553 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2554 " was rejected (status %u)", MAC2STR(sa), status);
2555 return;
2556 }
2557
2558 if (!s) {
2559 if (bssid) {
2560 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2561 "sa=" MACSTR " go_dev_addr=" MACSTR
2562 " bssid=" MACSTR " unknown-network",
2563 MAC2STR(sa), MAC2STR(go_dev_addr),
2564 MAC2STR(bssid));
2565 } else {
2566 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2567 "sa=" MACSTR " go_dev_addr=" MACSTR
2568 " unknown-network",
2569 MAC2STR(sa), MAC2STR(go_dev_addr));
2570 }
2571 return;
2572 }
2573
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002574 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2575 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2576 MACSTR " persistent=%d freq=%d",
2577 MAC2STR(sa), s->id, op_freq);
2578 } else {
2579 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa="
2580 MACSTR " persistent=%d", MAC2STR(sa), s->id);
2581 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582}
2583
2584
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002585static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2586 struct wpa_ssid *ssid,
2587 const u8 *peer)
2588{
2589 size_t i;
2590
2591 if (ssid == NULL)
2592 return;
2593
2594 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2595 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2596 ETH_ALEN) == 0)
2597 break;
2598 }
2599 if (i >= ssid->num_p2p_clients) {
2600 if (ssid->mode != WPAS_MODE_P2P_GO &&
2601 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2602 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2603 "due to invitation result", ssid->id);
2604 wpas_notify_network_removed(wpa_s, ssid);
2605 wpa_config_remove_network(wpa_s->conf, ssid->id);
2606 return;
2607 }
2608 return; /* Peer not found in client list */
2609 }
2610
2611 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2612 "group %d client list due to invitation result",
2613 MAC2STR(peer), ssid->id);
2614 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2615 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2616 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2617 ssid->num_p2p_clients--;
2618#ifndef CONFIG_NO_CONFIG_WRITE
2619 if (wpa_s->parent->conf->update_config &&
2620 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2621 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2622#endif /* CONFIG_NO_CONFIG_WRITE */
2623}
2624
2625
2626static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2627 const u8 *peer)
2628{
2629 struct wpa_ssid *ssid;
2630
2631 wpa_s = wpa_s->global->p2p_invite_group;
2632 if (wpa_s == NULL)
2633 return; /* No known invitation group */
2634 ssid = wpa_s->current_ssid;
2635 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2636 !ssid->p2p_persistent_group)
2637 return; /* Not operating as a GO in persistent group */
2638 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2639 ssid->ssid, ssid->ssid_len);
2640 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2641}
2642
2643
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002644static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002645 const struct p2p_channels *channels,
2646 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647{
2648 struct wpa_supplicant *wpa_s = ctx;
2649 struct wpa_ssid *ssid;
2650
2651 if (bssid) {
2652 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2653 "status=%d " MACSTR,
2654 status, MAC2STR(bssid));
2655 } else {
2656 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2657 "status=%d ", status);
2658 }
2659 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2660
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002661 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2662 status, MAC2STR(peer));
2663 if (wpa_s->pending_invite_ssid_id == -1) {
2664 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2665 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002666 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002667 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002668
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002669 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2670 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2671 "invitation exchange to indicate readiness for "
2672 "re-invocation");
2673 }
2674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002676 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2677 ssid = wpa_config_get_network(
2678 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2679 wpas_remove_persistent_peer(wpa_s, ssid, peer);
2680 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681 wpas_p2p_remove_pending_group_interface(wpa_s);
2682 return;
2683 }
2684
2685 ssid = wpa_config_get_network(wpa_s->conf,
2686 wpa_s->pending_invite_ssid_id);
2687 if (ssid == NULL) {
2688 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2689 "data matching with invitation");
2690 return;
2691 }
2692
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002693 /*
2694 * The peer could have missed our ctrl::ack frame for Invitation
2695 * Response and continue retransmitting the frame. To reduce the
2696 * likelihood of the peer not getting successful TX status for the
2697 * Invitation Response frame, wait a short time here before starting
2698 * the persistent group so that we will remain on the current channel to
2699 * acknowledge any possible retransmission from the peer.
2700 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002701#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002702 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2703 "starting persistent group");
2704 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002705#else
2706 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2707 "starting persistent group");
2708 os_sleep(0, 100000);
2709#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002712 ssid->mode == WPAS_MODE_P2P_GO,
2713 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002714 wpa_s->p2p_go_ht40, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715}
2716
2717
Dmitry Shmidt04949592012-07-19 12:16:46 -07002718static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2719 unsigned int freq)
2720{
2721 unsigned int i;
2722
2723 if (global->p2p_disallow_freq == NULL)
2724 return 0;
2725
2726 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2727 if (freq >= global->p2p_disallow_freq[i].min &&
2728 freq <= global->p2p_disallow_freq[i].max)
2729 return 1;
2730 }
2731
2732 return 0;
2733}
2734
2735
2736static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2737{
2738 reg->channel[reg->channels] = chan;
2739 reg->channels++;
2740}
2741
2742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2744 struct p2p_channels *chan)
2745{
2746 int i, cla = 0;
2747
2748 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2749 "band");
2750
2751 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2752 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002753 chan->reg_class[cla].channels = 0;
2754 for (i = 0; i < 11; i++) {
2755 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2756 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2757 }
2758 if (chan->reg_class[cla].channels)
2759 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760
2761 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2762 "band");
2763
2764 /* Operating class 115 - 5 GHz, channels 36-48 */
2765 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002766 chan->reg_class[cla].channels = 0;
2767 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2768 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2769 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2770 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2771 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2772 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2773 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2774 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2775 if (chan->reg_class[cla].channels)
2776 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777
2778 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2779 "band");
2780
2781 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2782 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002783 chan->reg_class[cla].channels = 0;
2784 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2785 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2786 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2787 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2788 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2789 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2790 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2791 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2792 if (chan->reg_class[cla].channels)
2793 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794
2795 chan->reg_classes = cla;
2796 return 0;
2797}
2798
2799
2800static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2801 u16 num_modes,
2802 enum hostapd_hw_mode mode)
2803{
2804 u16 i;
2805
2806 for (i = 0; i < num_modes; i++) {
2807 if (modes[i].mode == mode)
2808 return &modes[i];
2809 }
2810
2811 return NULL;
2812}
2813
2814
Dmitry Shmidt04949592012-07-19 12:16:46 -07002815static int has_channel(struct wpa_global *global,
2816 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817{
2818 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002819 unsigned int freq;
2820
2821 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2822 chan * 5;
2823 if (wpas_p2p_disallowed_freq(global, freq))
2824 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825
2826 for (i = 0; i < mode->num_channels; i++) {
2827 if (mode->channels[i].chan == chan) {
2828 if (flags)
2829 *flags = mode->channels[i].flag;
2830 return !(mode->channels[i].flag &
2831 (HOSTAPD_CHAN_DISABLED |
2832 HOSTAPD_CHAN_PASSIVE_SCAN |
2833 HOSTAPD_CHAN_NO_IBSS |
2834 HOSTAPD_CHAN_RADAR));
2835 }
2836 }
2837
2838 return 0;
2839}
2840
2841
2842struct p2p_oper_class_map {
2843 enum hostapd_hw_mode mode;
2844 u8 op_class;
2845 u8 min_chan;
2846 u8 max_chan;
2847 u8 inc;
2848 enum { BW20, BW40PLUS, BW40MINUS } bw;
2849};
2850
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002851static struct p2p_oper_class_map op_class[] = {
2852 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002853#if 0 /* Do not enable HT40 on 2 GHz for now */
2854 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2855 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2856#endif
2857 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2858 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2859 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2860 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2861 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2862 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2863 { -1, 0, 0, 0, 0, BW20 }
2864};
2865
2866
2867static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2868 struct hostapd_hw_modes *mode,
2869 u8 channel, u8 bw)
2870{
2871 int flag;
2872
2873 if (!has_channel(wpa_s->global, mode, channel, &flag))
2874 return -1;
2875 if (bw == BW40MINUS &&
2876 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2877 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2878 return 0;
2879 if (bw == BW40PLUS &&
2880 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2881 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2882 return 0;
2883 return 1;
2884}
2885
2886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2888 struct p2p_channels *chan)
2889{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002890 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002893 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2895 "of all supported channels; assume dualband "
2896 "support");
2897 return wpas_p2p_default_channels(wpa_s, chan);
2898 }
2899
2900 cla = 0;
2901
2902 for (op = 0; op_class[op].op_class; op++) {
2903 struct p2p_oper_class_map *o = &op_class[op];
2904 u8 ch;
2905 struct p2p_reg_class *reg = NULL;
2906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002907 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 if (mode == NULL)
2909 continue;
2910 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002911 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002912 continue;
2913 if (reg == NULL) {
2914 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2915 "class %u", o->op_class);
2916 reg = &chan->reg_class[cla];
2917 cla++;
2918 reg->reg_class = o->op_class;
2919 }
2920 reg->channel[reg->channels] = ch;
2921 reg->channels++;
2922 }
2923 if (reg) {
2924 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2925 reg->channel, reg->channels);
2926 }
2927 }
2928
2929 chan->reg_classes = cla;
2930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931 return 0;
2932}
2933
2934
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002935int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2936 struct hostapd_hw_modes *mode, u8 channel)
2937{
2938 int op, ret;
2939
2940 for (op = 0; op_class[op].op_class; op++) {
2941 struct p2p_oper_class_map *o = &op_class[op];
2942 u8 ch;
2943
2944 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2945 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2946 o->bw == BW20 || ch != channel)
2947 continue;
2948 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2949 if (ret < 0)
2950 continue;
2951 else if (ret > 0)
2952 return (o->bw == BW40MINUS) ? -1 : 1;
2953 else
2954 return 0;
2955 }
2956 }
2957 return 0;
2958}
2959
2960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2962 size_t buf_len)
2963{
2964 struct wpa_supplicant *wpa_s = ctx;
2965
2966 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2967 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2968 break;
2969 }
2970 if (wpa_s == NULL)
2971 return -1;
2972
2973 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2974}
2975
2976
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002977static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2978{
2979 struct wpa_supplicant *wpa_s = ctx;
2980
2981 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2982 struct wpa_ssid *ssid = wpa_s->current_ssid;
2983 if (ssid == NULL)
2984 continue;
2985 if (ssid->mode != WPAS_MODE_INFRA)
2986 continue;
2987 if (wpa_s->wpa_state != WPA_COMPLETED &&
2988 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2989 continue;
2990 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2991 return 1;
2992 }
2993
2994 return 0;
2995}
2996
2997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998/**
2999 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3000 * @global: Pointer to global data from wpa_supplicant_init()
3001 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3002 * Returns: 0 on success, -1 on failure
3003 */
3004int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3005{
3006 struct p2p_config p2p;
3007 unsigned int r;
3008 int i;
3009
3010 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3011 return 0;
3012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003013 if (global->p2p)
3014 return 0;
3015
3016 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3017 struct p2p_params params;
3018
3019 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3020 os_memset(&params, 0, sizeof(params));
3021 params.dev_name = wpa_s->conf->device_name;
3022 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3023 WPS_DEV_TYPE_LEN);
3024 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3025 os_memcpy(params.sec_dev_type,
3026 wpa_s->conf->sec_device_type,
3027 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3028
3029 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3030 return -1;
3031
3032 return 0;
3033 }
3034
3035 os_memset(&p2p, 0, sizeof(p2p));
3036 p2p.msg_ctx = wpa_s;
3037 p2p.cb_ctx = wpa_s;
3038 p2p.p2p_scan = wpas_p2p_scan;
3039 p2p.send_action = wpas_send_action;
3040 p2p.send_action_done = wpas_send_action_done;
3041 p2p.go_neg_completed = wpas_go_neg_completed;
3042 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3043 p2p.dev_found = wpas_dev_found;
3044 p2p.dev_lost = wpas_dev_lost;
3045 p2p.start_listen = wpas_start_listen;
3046 p2p.stop_listen = wpas_stop_listen;
3047 p2p.send_probe_resp = wpas_send_probe_resp;
3048 p2p.sd_request = wpas_sd_request;
3049 p2p.sd_response = wpas_sd_response;
3050 p2p.prov_disc_req = wpas_prov_disc_req;
3051 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003052 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 p2p.invitation_process = wpas_invitation_process;
3054 p2p.invitation_received = wpas_invitation_received;
3055 p2p.invitation_result = wpas_invitation_result;
3056 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003057 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058
3059 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003060 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 p2p.dev_name = wpa_s->conf->device_name;
3062 p2p.manufacturer = wpa_s->conf->manufacturer;
3063 p2p.model_name = wpa_s->conf->model_name;
3064 p2p.model_number = wpa_s->conf->model_number;
3065 p2p.serial_number = wpa_s->conf->serial_number;
3066 if (wpa_s->wps) {
3067 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3068 p2p.config_methods = wpa_s->wps->config_methods;
3069 }
3070
3071 if (wpa_s->conf->p2p_listen_reg_class &&
3072 wpa_s->conf->p2p_listen_channel) {
3073 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3074 p2p.channel = wpa_s->conf->p2p_listen_channel;
3075 } else {
3076 p2p.reg_class = 81;
3077 /*
3078 * Pick one of the social channels randomly as the listen
3079 * channel.
3080 */
3081 os_get_random((u8 *) &r, sizeof(r));
3082 p2p.channel = 1 + (r % 3) * 5;
3083 }
3084 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3085
3086 if (wpa_s->conf->p2p_oper_reg_class &&
3087 wpa_s->conf->p2p_oper_channel) {
3088 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3089 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3090 p2p.cfg_op_channel = 1;
3091 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3092 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3093
3094 } else {
3095 p2p.op_reg_class = 81;
3096 /*
3097 * Use random operation channel from (1, 6, 11) if no other
3098 * preference is indicated.
3099 */
3100 os_get_random((u8 *) &r, sizeof(r));
3101 p2p.op_channel = 1 + (r % 3) * 5;
3102 p2p.cfg_op_channel = 0;
3103 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3104 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3105 }
3106 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3107 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3108 p2p.country[2] = 0x04;
3109 } else
3110 os_memcpy(p2p.country, "XX\x04", 3);
3111
3112 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3113 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3114 "channel list");
3115 return -1;
3116 }
3117
3118 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3119 WPS_DEV_TYPE_LEN);
3120
3121 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3122 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3123 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3124
3125 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3126 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3127
3128 p2p.max_peers = 100;
3129
3130 if (wpa_s->conf->p2p_ssid_postfix) {
3131 p2p.ssid_postfix_len =
3132 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3133 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3134 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3135 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3136 p2p.ssid_postfix_len);
3137 }
3138
3139 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3140
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003141 p2p.max_listen = wpa_s->max_remain_on_chan;
3142
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003143#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003144 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003145 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003146 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3147 } else {
3148 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003149 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003150 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3151 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003152#endif
3153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003154 global->p2p = p2p_init(&p2p);
3155 if (global->p2p == NULL)
3156 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003157 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003158
3159 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3160 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3161 continue;
3162 p2p_add_wps_vendor_extension(
3163 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3164 }
3165
3166 return 0;
3167}
3168
3169
3170/**
3171 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3172 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3173 *
3174 * This function deinitialize per-interface P2P data.
3175 */
3176void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3177{
3178 if (wpa_s->driver && wpa_s->drv_priv)
3179 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003180
3181 if (wpa_s->go_params) {
3182 /* Clear any stored provisioning info */
3183 p2p_clear_provisioning_info(
3184 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003185 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003186 }
3187
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003188 os_free(wpa_s->go_params);
3189 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003190#ifdef ANDROID_P2P
3191 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3192#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003193 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3194 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3195 wpa_s->p2p_long_listen = 0;
3196 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3197 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3198 wpas_p2p_remove_pending_group_interface(wpa_s);
3199
3200 /* TODO: remove group interface from the driver if this wpa_s instance
3201 * is on top of a P2P group interface */
3202}
3203
3204
3205/**
3206 * wpas_p2p_deinit_global - Deinitialize global P2P module
3207 * @global: Pointer to global data from wpa_supplicant_init()
3208 *
3209 * This function deinitializes the global (per device) P2P module.
3210 */
3211void wpas_p2p_deinit_global(struct wpa_global *global)
3212{
3213 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003215 wpa_s = global->ifaces;
3216 if (wpa_s)
3217 wpas_p2p_service_flush(wpa_s);
3218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219 if (global->p2p == NULL)
3220 return;
3221
3222 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3224 wpa_s = wpa_s->next;
3225 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 tmp = global->ifaces;
3227 while (tmp &&
3228 (tmp == wpa_s ||
3229 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3230 tmp = tmp->next;
3231 }
3232 if (tmp == NULL)
3233 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003234 /* Disconnect from the P2P group and deinit the interface */
3235 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003236 }
3237
3238 /*
3239 * Deinit GO data on any possibly remaining interface (if main
3240 * interface is used as GO).
3241 */
3242 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3243 if (wpa_s->ap_iface)
3244 wpas_p2p_group_deinit(wpa_s);
3245 }
3246
3247 p2p_deinit(global->p2p);
3248 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003249 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003250}
3251
3252
3253static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3254{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003255 if (wpa_s->conf->p2p_no_group_iface)
3256 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 if (wpa_s->drv_flags &
3258 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3259 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3260 return 1; /* P2P group requires a new interface in every case
3261 */
3262 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3263 return 0; /* driver does not support concurrent operations */
3264 if (wpa_s->global->ifaces->next)
3265 return 1; /* more that one interface already in use */
3266 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3267 return 1; /* this interface is already in use */
3268 return 0;
3269}
3270
3271
3272static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3273 const u8 *peer_addr,
3274 enum p2p_wps_method wps_method,
3275 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003276 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003277 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003279 if (persistent_group && wpa_s->conf->persistent_reconnect)
3280 persistent_group = 2;
3281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3283 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3284 go_intent, own_interface_addr,
3285 force_freq, persistent_group);
3286 }
3287
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003288 /*
3289 * Increase GO config timeout if HT40 is used since it takes some time
3290 * to scan channels for coex purposes before the BSS can be started.
3291 */
3292 p2p_set_config_timeout(wpa_s->global->p2p,
3293 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003295 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3296 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003297 persistent_group, ssid ? ssid->ssid : NULL,
3298 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003299 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300}
3301
3302
3303static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3304 const u8 *peer_addr,
3305 enum p2p_wps_method wps_method,
3306 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003307 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003308 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003310 if (persistent_group && wpa_s->conf->persistent_reconnect)
3311 persistent_group = 2;
3312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3314 return -1;
3315
3316 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3317 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003318 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003319 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320}
3321
3322
3323static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3324{
3325 wpa_s->p2p_join_scan_count++;
3326 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3327 wpa_s->p2p_join_scan_count);
3328 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3329 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3330 " for join operationg - stop join attempt",
3331 MAC2STR(wpa_s->pending_join_iface_addr));
3332 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003333 if (wpa_s->p2p_auto_pd) {
3334 wpa_s->p2p_auto_pd = 0;
3335 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3336 " p2p_dev_addr=" MACSTR " status=N/A",
3337 MAC2STR(wpa_s->pending_join_dev_addr));
3338 return;
3339 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340 wpa_msg(wpa_s->parent, MSG_INFO,
3341 P2P_EVENT_GROUP_FORMATION_FAILURE);
3342 }
3343}
3344
3345
Dmitry Shmidt04949592012-07-19 12:16:46 -07003346static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3347{
3348 struct wpa_supplicant *iface;
3349 int shared_freq;
3350 u8 bssid[ETH_ALEN];
3351
3352 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3353 return 0;
3354
3355 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3356 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3357 continue;
3358 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3359 continue;
3360 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3361 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3362 shared_freq = iface->current_ssid->frequency;
3363 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3364 shared_freq = iface->assoc_freq;
3365 else
3366 shared_freq = 0;
3367
3368 if (shared_freq && freq != shared_freq) {
3369 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3370 "connected on %d MHz - new connection on "
3371 "%d MHz", iface->ifname, shared_freq, freq);
3372 return 1;
3373 }
3374 }
3375
3376 shared_freq = wpa_drv_shared_freq(wpa_s);
3377 if (shared_freq > 0 && shared_freq != freq) {
3378 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3379 "virtual interface connected on %d MHz - new "
3380 "connection on %d MHz", shared_freq, freq);
3381 return 1;
3382 }
3383
3384 return 0;
3385}
3386
3387
3388static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3389 const u8 *peer_dev_addr)
3390{
3391 struct wpa_bss *bss;
3392 int updated;
3393
3394 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3395 if (bss == NULL)
3396 return -1;
3397 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3398 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3399 "last scan");
3400 return 0;
3401 }
3402
3403 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3404 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3405 "%ld.%06ld (%supdated in last scan)",
3406 bss->last_update.sec, bss->last_update.usec,
3407 updated ? "": "not ");
3408
3409 return updated;
3410}
3411
3412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3414 struct wpa_scan_results *scan_res)
3415{
3416 struct wpa_bss *bss;
3417 int freq;
3418 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3421
3422 if (wpa_s->global->p2p_disabled)
3423 return;
3424
Dmitry Shmidt04949592012-07-19 12:16:46 -07003425 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3426 scan_res ? (int) scan_res->num : -1,
3427 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428
3429 if (scan_res)
3430 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3431
Dmitry Shmidt04949592012-07-19 12:16:46 -07003432 if (wpa_s->p2p_auto_pd) {
3433 int join = wpas_p2p_peer_go(wpa_s,
3434 wpa_s->pending_join_dev_addr);
3435 if (join == 0 &&
3436 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3437 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003438 bss = wpa_bss_get_bssid_latest(
3439 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003440 if (bss) {
3441 freq = bss->freq;
3442 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3443 "the peer " MACSTR " at %d MHz",
3444 wpa_s->auto_pd_scan_retry,
3445 MAC2STR(wpa_s->
3446 pending_join_dev_addr),
3447 freq);
3448 wpas_p2p_join_scan_req(wpa_s, freq);
3449 return;
3450 }
3451 }
3452
3453 if (join < 0)
3454 join = 0;
3455
3456 wpa_s->p2p_auto_pd = 0;
3457 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3458 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3459 MAC2STR(wpa_s->pending_join_dev_addr), join);
3460 if (p2p_prov_disc_req(wpa_s->global->p2p,
3461 wpa_s->pending_join_dev_addr,
3462 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003463 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003464 wpa_s->p2p_auto_pd = 0;
3465 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3466 " p2p_dev_addr=" MACSTR " status=N/A",
3467 MAC2STR(wpa_s->pending_join_dev_addr));
3468 }
3469 return;
3470 }
3471
3472 if (wpa_s->p2p_auto_join) {
3473 int join = wpas_p2p_peer_go(wpa_s,
3474 wpa_s->pending_join_dev_addr);
3475 if (join < 0) {
3476 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3477 "running a GO -> use GO Negotiation");
3478 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3479 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3480 wpa_s->p2p_persistent_group, 0, 0, 0,
3481 wpa_s->p2p_go_intent,
3482 wpa_s->p2p_connect_freq,
3483 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003484 wpa_s->p2p_pd_before_go_neg,
3485 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003486 return;
3487 }
3488
3489 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3490 "try to join the group", join ? "" :
3491 " in older scan");
3492 if (!join)
3493 wpa_s->p2p_fallback_to_go_neg = 1;
3494 }
3495
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003496 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3497 wpa_s->pending_join_iface_addr);
3498 if (freq < 0 &&
3499 p2p_get_interface_addr(wpa_s->global->p2p,
3500 wpa_s->pending_join_dev_addr,
3501 iface_addr) == 0 &&
3502 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3503 {
3504 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3505 "address for join from " MACSTR " to " MACSTR
3506 " based on newly discovered P2P peer entry",
3507 MAC2STR(wpa_s->pending_join_iface_addr),
3508 MAC2STR(iface_addr));
3509 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3510 ETH_ALEN);
3511
3512 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3513 wpa_s->pending_join_iface_addr);
3514 }
3515 if (freq >= 0) {
3516 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3517 "from P2P peer table: %d MHz", freq);
3518 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003519 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003520 if (bss) {
3521 freq = bss->freq;
3522 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3523 "from BSS table: %d MHz", freq);
3524 }
3525 if (freq > 0) {
3526 u16 method;
3527
Dmitry Shmidt04949592012-07-19 12:16:46 -07003528 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3529 wpa_msg(wpa_s->parent, MSG_INFO,
3530 P2P_EVENT_GROUP_FORMATION_FAILURE
3531 "reason=FREQ_CONFLICT");
3532 return;
3533 }
3534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003535 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3536 "prior to joining an existing group (GO " MACSTR
3537 " freq=%u MHz)",
3538 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3539 wpa_s->pending_pd_before_join = 1;
3540
3541 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542 case WPS_PIN_DISPLAY:
3543 method = WPS_CONFIG_KEYPAD;
3544 break;
3545 case WPS_PIN_KEYPAD:
3546 method = WPS_CONFIG_DISPLAY;
3547 break;
3548 case WPS_PBC:
3549 method = WPS_CONFIG_PUSHBUTTON;
3550 break;
3551 default:
3552 method = 0;
3553 break;
3554 }
3555
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003556 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3557 wpa_s->pending_join_dev_addr) ==
3558 method)) {
3559 /*
3560 * We have already performed provision discovery for
3561 * joining the group. Proceed directly to join
3562 * operation without duplicated provision discovery. */
3563 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3564 "with " MACSTR " already done - proceed to "
3565 "join",
3566 MAC2STR(wpa_s->pending_join_dev_addr));
3567 wpa_s->pending_pd_before_join = 0;
3568 goto start;
3569 }
3570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003572 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003573 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003574 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3575 "Discovery Request before joining an "
3576 "existing group");
3577 wpa_s->pending_pd_before_join = 0;
3578 goto start;
3579 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 return;
3581 }
3582
3583 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3584 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3585 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3586 wpas_p2p_check_join_scan_limit(wpa_s);
3587 return;
3588
3589start:
3590 /* Start join operation immediately */
3591 wpas_p2p_join_start(wpa_s);
3592}
3593
3594
Dmitry Shmidt04949592012-07-19 12:16:46 -07003595static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003596{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003597 int ret;
3598 struct wpa_driver_scan_params params;
3599 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003600 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003601 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003602#ifdef ANDROID_P2P
3603 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003605 /* If freq is not provided, check the operating freq of the GO and do a
3606 * a directed scan to save time
3607 */
3608 if(!freq) {
3609 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3610 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3611 }
3612#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613 os_memset(&params, 0, sizeof(params));
3614
3615 /* P2P Wildcard SSID */
3616 params.num_ssids = 1;
3617 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3618 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3619
3620 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003621 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3622 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3623 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003624 if (wps_ie == NULL) {
3625 wpas_p2p_scan_res_join(wpa_s, NULL);
3626 return;
3627 }
3628
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003629 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3630 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003631 if (ies == NULL) {
3632 wpabuf_free(wps_ie);
3633 wpas_p2p_scan_res_join(wpa_s, NULL);
3634 return;
3635 }
3636 wpabuf_put_buf(ies, wps_ie);
3637 wpabuf_free(wps_ie);
3638
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003639 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003641 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003642 params.extra_ies = wpabuf_head(ies);
3643 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003644 if (freq > 0) {
3645 freqs[0] = freq;
3646 params.freqs = freqs;
3647 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648
3649 /*
3650 * Run a scan to update BSS table and start Provision Discovery once
3651 * the new scan results become available.
3652 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003653 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003654 if (!ret) {
3655 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003656 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003657 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658
3659 wpabuf_free(ies);
3660
3661 if (ret) {
3662 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3663 "try again later");
3664 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3665 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3666 wpas_p2p_check_join_scan_limit(wpa_s);
3667 }
3668}
3669
3670
Dmitry Shmidt04949592012-07-19 12:16:46 -07003671static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3672{
3673 struct wpa_supplicant *wpa_s = eloop_ctx;
3674 wpas_p2p_join_scan_req(wpa_s, 0);
3675}
3676
3677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003678static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003679 const u8 *dev_addr, enum p2p_wps_method wps_method,
3680 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003681{
3682 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003683 MACSTR " dev " MACSTR ")%s",
3684 MAC2STR(iface_addr), MAC2STR(dev_addr),
3685 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686
Dmitry Shmidt04949592012-07-19 12:16:46 -07003687 wpa_s->p2p_auto_pd = 0;
3688 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003689 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3690 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3691 wpa_s->pending_join_wps_method = wps_method;
3692
3693 /* Make sure we are not running find during connection establishment */
3694 wpas_p2p_stop_find(wpa_s);
3695
3696 wpa_s->p2p_join_scan_count = 0;
3697 wpas_p2p_join_scan(wpa_s, NULL);
3698 return 0;
3699}
3700
3701
3702static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3703{
3704 struct wpa_supplicant *group;
3705 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003706 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003707
3708 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3709 if (group == NULL)
3710 return -1;
3711 if (group != wpa_s) {
3712 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3713 sizeof(group->p2p_pin));
3714 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003715 } else {
3716 /*
3717 * Need to mark the current interface for p2p_group_formation
3718 * when a separate group interface is not used. This is needed
3719 * to allow p2p_cancel stop a pending p2p_connect-join.
3720 * wpas_p2p_init_group_interface() addresses this for the case
3721 * where a separate group interface is used.
3722 */
3723 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724 }
3725
3726 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003727 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003728
3729 os_memset(&res, 0, sizeof(res));
3730 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3731 ETH_ALEN);
3732 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003733 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003734 if (bss) {
3735 res.freq = bss->freq;
3736 res.ssid_len = bss->ssid_len;
3737 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3738 }
3739
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003740 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3741 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3742 "starting client");
3743 wpa_drv_cancel_remain_on_channel(wpa_s);
3744 wpa_s->off_channel_freq = 0;
3745 wpa_s->roc_waiting_drv_freq = 0;
3746 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747 wpas_start_wps_enrollee(group, &res);
3748
3749 /*
3750 * Allow a longer timeout for join-a-running-group than normal 15
3751 * second group formation timeout since the GO may not have authorized
3752 * our connection yet.
3753 */
3754 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3755 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3756 wpa_s, NULL);
3757
3758 return 0;
3759}
3760
3761
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003762static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3763 int *force_freq, int *pref_freq,
3764 int *oper_freq)
3765{
3766 if (freq > 0) {
3767 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3768 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3769 "(%u MHz) is not supported for P2P uses",
3770 freq);
3771 return -3;
3772 }
3773
3774 if (*oper_freq > 0 && freq != *oper_freq &&
3775 !(wpa_s->drv_flags &
3776 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3777 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3778 "on %u MHz while connected on another "
3779 "channel (%u MHz)", freq, *oper_freq);
3780 return -2;
3781 }
3782 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3783 "requested channel (%u MHz)", freq);
3784 *force_freq = freq;
3785 } else if (*oper_freq > 0 &&
3786 !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
3787 if (!(wpa_s->drv_flags &
3788 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3789 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3790 "while connected on non-P2P supported "
3791 "channel (%u MHz)", *oper_freq);
3792 return -2;
3793 }
3794 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3795 "(%u MHz) not available for P2P - try to use "
3796 "another channel", *oper_freq);
3797 *force_freq = 0;
3798 } else if (*oper_freq > 0 && *pref_freq == 0 &&
3799 (wpa_s->drv_flags &
3800 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3801 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3802 "are already using (%u MHz) on another interface",
3803 *oper_freq);
3804 *pref_freq = *oper_freq;
3805 } else if (*oper_freq > 0) {
3806 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3807 "channel we are already using (%u MHz) on another "
3808 "interface", *oper_freq);
3809 *force_freq = *oper_freq;
3810 }
3811
3812 return 0;
3813}
3814
3815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003816/**
3817 * wpas_p2p_connect - Request P2P Group Formation to be started
3818 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3819 * @peer_addr: Address of the peer P2P Device
3820 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3821 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003822 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003823 * @join: Whether to join an existing group (as a client) instead of starting
3824 * Group Owner negotiation; @peer_addr is BSSID in that case
3825 * @auth: Whether to only authorize the connection instead of doing that and
3826 * initiating Group Owner negotiation
3827 * @go_intent: GO Intent or -1 to use default
3828 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003829 * @persistent_id: Persistent group credentials to use for forcing GO
3830 * parameters or -1 to generate new values (SSID/passphrase)
3831 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3832 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003833 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003834 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3835 * failure, -2 on failure due to channel not currently available,
3836 * -3 if forced channel is not supported
3837 */
3838int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3839 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003840 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003841 int go_intent, int freq, int persistent_id, int pd,
3842 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003843{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003844 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845 u8 bssid[ETH_ALEN];
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003846 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003847 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003848 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003849 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003850
3851 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3852 return -1;
3853
Dmitry Shmidt04949592012-07-19 12:16:46 -07003854 if (persistent_id >= 0) {
3855 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3856 if (ssid == NULL || ssid->disabled != 2 ||
3857 ssid->mode != WPAS_MODE_P2P_GO)
3858 return -1;
3859 }
3860
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003861 if (go_intent < 0)
3862 go_intent = wpa_s->conf->p2p_go_intent;
3863
3864 if (!auth)
3865 wpa_s->p2p_long_listen = 0;
3866
3867 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003868 wpa_s->p2p_persistent_group = !!persistent_group;
3869 wpa_s->p2p_persistent_id = persistent_id;
3870 wpa_s->p2p_go_intent = go_intent;
3871 wpa_s->p2p_connect_freq = freq;
3872 wpa_s->p2p_fallback_to_go_neg = 0;
3873 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003874 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003875
3876 if (pin)
3877 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3878 else if (wps_method == WPS_PIN_DISPLAY) {
3879 ret = wps_generate_pin();
3880 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3881 ret);
3882 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3883 wpa_s->p2p_pin);
3884 } else
3885 wpa_s->p2p_pin[0] = '\0';
3886
Dmitry Shmidt04949592012-07-19 12:16:46 -07003887 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3889 if (auth) {
3890 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3891 "connect a running group from " MACSTR,
3892 MAC2STR(peer_addr));
3893 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3894 return ret;
3895 }
3896 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3897 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3898 iface_addr) < 0) {
3899 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3900 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3901 dev_addr);
3902 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003903 if (auto_join) {
3904 os_get_time(&wpa_s->p2p_auto_started);
3905 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3906 "%ld.%06ld",
3907 wpa_s->p2p_auto_started.sec,
3908 wpa_s->p2p_auto_started.usec);
3909 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003910 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003911 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3912 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003913 return -1;
3914 return ret;
3915 }
3916
3917 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003918 wpa_s->assoc_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003919 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003920 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921 oper_freq = wpa_drv_shared_freq(wpa_s);
3922 if (oper_freq < 0)
3923 oper_freq = 0;
3924 }
3925
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003926 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
3927 &oper_freq);
3928 if (res)
3929 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003930 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003931
3932 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3933
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003934 if (wpa_s->create_p2p_iface) {
3935 /* Prepare to add a new interface for the group */
3936 iftype = WPA_IF_P2P_GROUP;
3937 if (go_intent == 15)
3938 iftype = WPA_IF_P2P_GO;
3939 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3940 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3941 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003942 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003945 if_addr = wpa_s->pending_interface_addr;
3946 } else
3947 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948
3949 if (auth) {
3950 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003951 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003952 force_freq, persistent_group, ssid,
3953 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 return -1;
3955 return ret;
3956 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003957
3958 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3959 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003960 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003961 if (wpa_s->create_p2p_iface)
3962 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963 return -1;
3964 }
3965 return ret;
3966}
3967
3968
3969/**
3970 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3971 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3972 * @freq: Frequency of the channel in MHz
3973 * @duration: Duration of the stay on the channel in milliseconds
3974 *
3975 * This callback is called when the driver indicates that it has started the
3976 * requested remain-on-channel duration.
3977 */
3978void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3979 unsigned int freq, unsigned int duration)
3980{
3981 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3982 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003983 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3984 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3985 wpa_s->pending_listen_duration);
3986 wpa_s->pending_listen_freq = 0;
3987 }
3988}
3989
3990
3991static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3992 unsigned int timeout)
3993{
3994 /* Limit maximum Listen state time based on driver limitation. */
3995 if (timeout > wpa_s->max_remain_on_chan)
3996 timeout = wpa_s->max_remain_on_chan;
3997
3998 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3999 return wpa_drv_p2p_listen(wpa_s, timeout);
4000
4001 return p2p_listen(wpa_s->global->p2p, timeout);
4002}
4003
4004
4005/**
4006 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4007 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4008 * @freq: Frequency of the channel in MHz
4009 *
4010 * This callback is called when the driver indicates that a remain-on-channel
4011 * operation has been completed, i.e., the duration on the requested channel
4012 * has timed out.
4013 */
4014void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4015 unsigned int freq)
4016{
4017 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4018 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004019 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4021 return;
4022 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4023 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004024 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004025 return;
4026 if (wpa_s->p2p_long_listen > 0)
4027 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4028 if (wpa_s->p2p_long_listen > 0) {
4029 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4030 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4031 }
4032}
4033
4034
4035/**
4036 * wpas_p2p_group_remove - Remove a P2P group
4037 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4038 * @ifname: Network interface name of the group interface or "*" to remove all
4039 * groups
4040 * Returns: 0 on success, -1 on failure
4041 *
4042 * This function is used to remove a P2P group. This can be used to disconnect
4043 * from a group in which the local end is a P2P Client or to end a P2P Group in
4044 * case the local end is the Group Owner. If a virtual network interface was
4045 * created for this group, that interface will be removed. Otherwise, only the
4046 * configured P2P group network will be removed from the interface.
4047 */
4048int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4049{
4050 struct wpa_global *global = wpa_s->global;
4051
4052 if (os_strcmp(ifname, "*") == 0) {
4053 struct wpa_supplicant *prev;
4054 wpa_s = global->ifaces;
4055 while (wpa_s) {
4056 prev = wpa_s;
4057 wpa_s = wpa_s->next;
4058 wpas_p2p_disconnect(prev);
4059 }
4060 return 0;
4061 }
4062
4063 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4064 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4065 break;
4066 }
4067
4068 return wpas_p2p_disconnect(wpa_s);
4069}
4070
4071
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004072static int freq_included(const struct p2p_channels *channels, unsigned int freq)
4073{
4074 if (channels == NULL)
4075 return 1; /* Assume no restrictions */
4076 return p2p_channels_includes_freq(channels, freq);
4077}
4078
4079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004080static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4081 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004082 int freq, int ht40,
4083 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004084{
4085 u8 bssid[ETH_ALEN];
4086 int res;
4087
4088 os_memset(params, 0, sizeof(*params));
4089 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004090 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004092 if (!freq_included(channels, freq)) {
4093 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4094 "accepted", freq);
4095 return -1;
4096 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4098 "frequency %d MHz", freq);
4099 params->freq = freq;
4100 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4101 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004102 wpa_s->conf->p2p_oper_channel <= 11 &&
4103 freq_included(channels,
4104 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4106 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4107 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004108 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4109 wpa_s->conf->p2p_oper_reg_class == 116 ||
4110 wpa_s->conf->p2p_oper_reg_class == 117 ||
4111 wpa_s->conf->p2p_oper_reg_class == 124 ||
4112 wpa_s->conf->p2p_oper_reg_class == 126 ||
4113 wpa_s->conf->p2p_oper_reg_class == 127) &&
4114 freq_included(channels,
4115 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4117 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4118 "frequency %d MHz", params->freq);
4119 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4120 wpa_s->best_overall_freq > 0 &&
4121 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004122 wpa_s->best_overall_freq) &&
4123 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124 params->freq = wpa_s->best_overall_freq;
4125 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4126 "channel %d MHz", params->freq);
4127 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4128 wpa_s->best_24_freq > 0 &&
4129 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004130 wpa_s->best_24_freq) &&
4131 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004132 params->freq = wpa_s->best_24_freq;
4133 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4134 "channel %d MHz", params->freq);
4135 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4136 wpa_s->best_5_freq > 0 &&
4137 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004138 wpa_s->best_5_freq) &&
4139 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004140 params->freq = wpa_s->best_5_freq;
4141 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4142 "channel %d MHz", params->freq);
4143 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004144 int chan;
4145 for (chan = 0; chan < 11; chan++) {
4146 params->freq = 2412 + chan * 5;
4147 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004148 params->freq) &&
4149 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004150 break;
4151 }
4152 if (chan == 11) {
4153 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4154 "allowed");
4155 return -1;
4156 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4158 "known)", params->freq);
4159 }
4160
4161 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4162 wpa_s->assoc_freq && !freq) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004163 if (!p2p_supported_freq(wpa_s->global->p2p, wpa_s->assoc_freq)
4164 || !freq_included(channels, wpa_s->assoc_freq)) {
4165 if (wpa_s->drv_flags &
4166 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4167 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4168 "the channel we are already using "
4169 "(%u MHz) - allow multi-channel "
4170 "concurrency", wpa_s->assoc_freq);
4171 } else {
4172 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
4173 "the channel we are already using "
4174 "(%u MHz)", wpa_s->assoc_freq);
4175 return -1;
4176 }
4177 } else {
4178 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we "
4179 "are already using (%u MHz)",
4180 wpa_s->assoc_freq);
4181 params->freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004182 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004183 }
4184
4185 res = wpa_drv_shared_freq(wpa_s);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004186 if (res > 0 && !freq &&
4187 (!p2p_supported_freq(wpa_s->global->p2p, res) ||
4188 !freq_included(channels, res))) {
4189 if (wpa_s->drv_flags &
4190 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
4191 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4192 "channel we are already using on a shared "
4193 "interface (%u MHz) - allow multi-channel "
4194 "concurrency", res);
4195 } else {
4196 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
4197 "channel we are already using on a shared "
4198 "interface (%u MHz)", res);
4199 return -1;
4200 }
4201 } else if (res > 0 && !freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004202 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4203 "already using on a shared interface");
4204 params->freq = res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004205 if (!freq_included(channels, params->freq)) {
4206 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4207 "accepted", params->freq);
4208 return -1;
4209 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004210 } else if (res > 0 && freq != res &&
4211 !(wpa_s->drv_flags &
4212 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4213 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4214 "while connected on another channel (%u MHz)",
4215 freq, res);
4216 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004217 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004218
4219 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220}
4221
4222
4223static struct wpa_supplicant *
4224wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4225 int go)
4226{
4227 struct wpa_supplicant *group_wpa_s;
4228
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004229 if (!wpas_p2p_create_iface(wpa_s)) {
4230 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4231 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234
4235 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004236 WPA_IF_P2P_CLIENT) < 0) {
4237 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004238 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004239 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4241 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004242 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4243 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004244 wpas_p2p_remove_pending_group_interface(wpa_s);
4245 return NULL;
4246 }
4247
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004248 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4249 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250 return group_wpa_s;
4251}
4252
4253
4254/**
4255 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4256 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4257 * @persistent_group: Whether to create a persistent group
4258 * @freq: Frequency for the group or 0 to indicate no hardcoding
4259 * Returns: 0 on success, -1 on failure
4260 *
4261 * This function creates a new P2P group with the local end as the Group Owner,
4262 * i.e., without using Group Owner Negotiation.
4263 */
4264int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004265 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004266{
4267 struct p2p_go_neg_results params;
4268 unsigned int r;
4269
4270 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4271 return -1;
4272
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004273 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004274 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004275 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004277 if (freq == 2) {
4278 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4279 "band");
4280 if (wpa_s->best_24_freq > 0 &&
4281 p2p_supported_freq(wpa_s->global->p2p,
4282 wpa_s->best_24_freq)) {
4283 freq = wpa_s->best_24_freq;
4284 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4285 "channel: %d MHz", freq);
4286 } else {
4287 os_get_random((u8 *) &r, sizeof(r));
4288 freq = 2412 + (r % 3) * 25;
4289 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4290 "channel: %d MHz", freq);
4291 }
4292 }
4293
4294 if (freq == 5) {
4295 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4296 "band");
4297 if (wpa_s->best_5_freq > 0 &&
4298 p2p_supported_freq(wpa_s->global->p2p,
4299 wpa_s->best_5_freq)) {
4300 freq = wpa_s->best_5_freq;
4301 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4302 "channel: %d MHz", freq);
4303 } else {
4304 os_get_random((u8 *) &r, sizeof(r));
4305 freq = 5180 + (r % 4) * 20;
4306 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4307 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4308 "5 GHz channel for P2P group");
4309 return -1;
4310 }
4311 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4312 "channel: %d MHz", freq);
4313 }
4314 }
4315
4316 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4317 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4318 "(%u MHz) is not supported for P2P uses",
4319 freq);
4320 return -1;
4321 }
4322
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004323 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004324 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004325 if (params.freq &&
4326 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4327 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4328 "(%u MHz) is not supported for P2P uses",
4329 params.freq);
4330 return -1;
4331 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332 p2p_go_params(wpa_s->global->p2p, &params);
4333 params.persistent_group = persistent_group;
4334
4335 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4336 if (wpa_s == NULL)
4337 return -1;
4338 wpas_start_wps_go(wpa_s, &params, 0);
4339
4340 return 0;
4341}
4342
4343
4344static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4345 struct wpa_ssid *params, int addr_allocated)
4346{
4347 struct wpa_ssid *ssid;
4348
4349 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4350 if (wpa_s == NULL)
4351 return -1;
4352
4353 wpa_supplicant_ap_deinit(wpa_s);
4354
4355 ssid = wpa_config_add_network(wpa_s->conf);
4356 if (ssid == NULL)
4357 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004358 wpa_config_set_network_defaults(ssid);
4359 ssid->temporary = 1;
4360 ssid->proto = WPA_PROTO_RSN;
4361 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4362 ssid->group_cipher = WPA_CIPHER_CCMP;
4363 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4364 ssid->ssid = os_malloc(params->ssid_len);
4365 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 wpa_config_remove_network(wpa_s->conf, ssid->id);
4367 return -1;
4368 }
4369 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4370 ssid->ssid_len = params->ssid_len;
4371 ssid->p2p_group = 1;
4372 ssid->export_keys = 1;
4373 if (params->psk_set) {
4374 os_memcpy(ssid->psk, params->psk, 32);
4375 ssid->psk_set = 1;
4376 }
4377 if (params->passphrase)
4378 ssid->passphrase = os_strdup(params->passphrase);
4379
4380 wpa_supplicant_select_network(wpa_s, ssid);
4381
4382 wpa_s->show_group_started = 1;
4383
4384 return 0;
4385}
4386
4387
4388int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4389 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004390 int freq, int ht40,
4391 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004392{
4393 struct p2p_go_neg_results params;
4394 int go = 0;
4395
4396 if (ssid->disabled != 2 || ssid->ssid == NULL)
4397 return -1;
4398
4399 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4400 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4401 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4402 "already running");
4403 return 0;
4404 }
4405
4406 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004407 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004408
Dmitry Shmidt04949592012-07-19 12:16:46 -07004409 wpa_s->p2p_fallback_to_go_neg = 0;
4410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004411 if (ssid->mode == WPAS_MODE_INFRA)
4412 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4413
4414 if (ssid->mode != WPAS_MODE_P2P_GO)
4415 return -1;
4416
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004417 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004418 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004419
4420 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004421 params.psk_set = ssid->psk_set;
4422 if (params.psk_set)
4423 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004424 if (ssid->passphrase) {
4425 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4426 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4427 "persistent group");
4428 return -1;
4429 }
4430 os_strlcpy(params.passphrase, ssid->passphrase,
4431 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004433 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4434 params.ssid_len = ssid->ssid_len;
4435 params.persistent_group = 1;
4436
4437 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4438 if (wpa_s == NULL)
4439 return -1;
4440
4441 wpas_start_wps_go(wpa_s, &params, 0);
4442
4443 return 0;
4444}
4445
4446
4447static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4448 struct wpabuf *proberesp_ies)
4449{
4450 struct wpa_supplicant *wpa_s = ctx;
4451 if (wpa_s->ap_iface) {
4452 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004453 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4454 wpabuf_free(beacon_ies);
4455 wpabuf_free(proberesp_ies);
4456 return;
4457 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458 if (beacon_ies) {
4459 wpabuf_free(hapd->p2p_beacon_ie);
4460 hapd->p2p_beacon_ie = beacon_ies;
4461 }
4462 wpabuf_free(hapd->p2p_probe_resp_ie);
4463 hapd->p2p_probe_resp_ie = proberesp_ies;
4464 } else {
4465 wpabuf_free(beacon_ies);
4466 wpabuf_free(proberesp_ies);
4467 }
4468 wpa_supplicant_ap_update_beacon(wpa_s);
4469}
4470
4471
4472static void wpas_p2p_idle_update(void *ctx, int idle)
4473{
4474 struct wpa_supplicant *wpa_s = ctx;
4475 if (!wpa_s->ap_iface)
4476 return;
4477 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004478 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479 wpas_p2p_set_group_idle_timeout(wpa_s);
4480 else
4481 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4482}
4483
4484
4485struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004486 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004487{
4488 struct p2p_group *group;
4489 struct p2p_group_config *cfg;
4490
4491 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4492 return NULL;
4493 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4494 return NULL;
4495
4496 cfg = os_zalloc(sizeof(*cfg));
4497 if (cfg == NULL)
4498 return NULL;
4499
Dmitry Shmidt04949592012-07-19 12:16:46 -07004500 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004501 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004502 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004503 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4505 if (wpa_s->max_stations &&
4506 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4507 cfg->max_clients = wpa_s->max_stations;
4508 else
4509 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004510 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4511 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 cfg->cb_ctx = wpa_s;
4513 cfg->ie_update = wpas_p2p_ie_update;
4514 cfg->idle_update = wpas_p2p_idle_update;
4515
4516 group = p2p_group_init(wpa_s->global->p2p, cfg);
4517 if (group == NULL)
4518 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004519 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004520 p2p_group_notif_formation_done(group);
4521 wpa_s->p2p_group = group;
4522 return group;
4523}
4524
4525
4526void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4527 int registrar)
4528{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004529 struct wpa_ssid *ssid = wpa_s->current_ssid;
4530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531 if (!wpa_s->p2p_in_provisioning) {
4532 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4533 "provisioning not in progress");
4534 return;
4535 }
4536
Dmitry Shmidt04949592012-07-19 12:16:46 -07004537 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4538 u8 go_dev_addr[ETH_ALEN];
4539 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4540 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4541 ssid->ssid_len);
4542 /* Clear any stored provisioning info */
4543 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4544 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004546 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4547 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004548 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4549 /*
4550 * Use a separate timeout for initial data connection to
4551 * complete to allow the group to be removed automatically if
4552 * something goes wrong in this step before the P2P group idle
4553 * timeout mechanism is taken into use.
4554 */
4555 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4556 wpas_p2p_group_formation_timeout,
4557 wpa_s->parent, NULL);
4558 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004559 if (wpa_s->global->p2p)
4560 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4561 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4562 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4563 wpas_group_formation_completed(wpa_s, 1);
4564}
4565
4566
Jouni Malinen75ecf522011-06-27 15:19:46 -07004567void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4568 struct wps_event_fail *fail)
4569{
4570 if (!wpa_s->p2p_in_provisioning) {
4571 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4572 "provisioning not in progress");
4573 return;
4574 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004575
4576 if (wpa_s->go_params) {
4577 p2p_clear_provisioning_info(
4578 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004579 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004580 }
4581
Jouni Malinen75ecf522011-06-27 15:19:46 -07004582 wpas_notify_p2p_wps_failed(wpa_s, fail);
4583}
4584
4585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004587 const char *config_method,
4588 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589{
4590 u16 config_methods;
4591
Dmitry Shmidt04949592012-07-19 12:16:46 -07004592 wpa_s->p2p_fallback_to_go_neg = 0;
4593 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004594 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004596 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004597 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004598 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4599 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004600 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004601 else {
4602 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004603 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004604 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605
Dmitry Shmidt04949592012-07-19 12:16:46 -07004606 if (use == WPAS_P2P_PD_AUTO) {
4607 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4608 wpa_s->pending_pd_config_methods = config_methods;
4609 wpa_s->p2p_auto_pd = 1;
4610 wpa_s->p2p_auto_join = 0;
4611 wpa_s->pending_pd_before_join = 0;
4612 wpa_s->auto_pd_scan_retry = 0;
4613 wpas_p2p_stop_find(wpa_s);
4614 wpa_s->p2p_join_scan_count = 0;
4615 os_get_time(&wpa_s->p2p_auto_started);
4616 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4617 wpa_s->p2p_auto_started.sec,
4618 wpa_s->p2p_auto_started.usec);
4619 wpas_p2p_join_scan(wpa_s, NULL);
4620 return 0;
4621 }
4622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4624 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004625 config_methods,
4626 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004627 }
4628
4629 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4630 return -1;
4631
4632 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004633 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004634 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635}
4636
4637
4638int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4639 char *end)
4640{
4641 return p2p_scan_result_text(ies, ies_len, buf, end);
4642}
4643
4644
4645static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4646{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004647 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648 return;
4649
4650 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4651 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004652 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004653}
4654
4655
4656int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4657 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004658 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004659 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004660{
4661 wpas_p2p_clear_pending_action_tx(wpa_s);
4662 wpa_s->p2p_long_listen = 0;
4663
4664 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4665 return wpa_drv_p2p_find(wpa_s, timeout, type);
4666
Dmitry Shmidt04949592012-07-19 12:16:46 -07004667 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4668 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669 return -1;
4670
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004671 wpa_supplicant_cancel_sched_scan(wpa_s);
4672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004674 num_req_dev_types, req_dev_types, dev_id,
4675 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676}
4677
4678
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004679static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004680{
4681 wpas_p2p_clear_pending_action_tx(wpa_s);
4682 wpa_s->p2p_long_listen = 0;
4683 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4684 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004685 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004686
4687 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4688 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004689 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690 }
4691
4692 if (wpa_s->global->p2p)
4693 p2p_stop_find(wpa_s->global->p2p);
4694
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004695 return 0;
4696}
4697
4698
4699void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4700{
4701 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4702 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703 wpas_p2p_remove_pending_group_interface(wpa_s);
4704}
4705
4706
4707static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4708{
4709 struct wpa_supplicant *wpa_s = eloop_ctx;
4710 wpa_s->p2p_long_listen = 0;
4711}
4712
4713
4714int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4715{
4716 int res;
4717
4718 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4719 return -1;
4720
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004721 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 wpas_p2p_clear_pending_action_tx(wpa_s);
4723
4724 if (timeout == 0) {
4725 /*
4726 * This is a request for unlimited Listen state. However, at
4727 * least for now, this is mapped to a Listen state for one
4728 * hour.
4729 */
4730 timeout = 3600;
4731 }
4732 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4733 wpa_s->p2p_long_listen = 0;
4734
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004735 /*
4736 * Stop previous find/listen operation to avoid trying to request a new
4737 * remain-on-channel operation while the driver is still running the
4738 * previous one.
4739 */
4740 if (wpa_s->global->p2p)
4741 p2p_stop_find(wpa_s->global->p2p);
4742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4744 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4745 wpa_s->p2p_long_listen = timeout * 1000;
4746 eloop_register_timeout(timeout, 0,
4747 wpas_p2p_long_listen_timeout,
4748 wpa_s, NULL);
4749 }
4750
4751 return res;
4752}
4753
4754
4755int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4756 u8 *buf, size_t len, int p2p_group)
4757{
4758 struct wpabuf *p2p_ie;
4759 int ret;
4760
4761 if (wpa_s->global->p2p_disabled)
4762 return -1;
4763 if (wpa_s->global->p2p == NULL)
4764 return -1;
4765 if (bss == NULL)
4766 return -1;
4767
4768 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4769 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4770 p2p_group, p2p_ie);
4771 wpabuf_free(p2p_ie);
4772
4773 return ret;
4774}
4775
4776
4777int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004778 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004779 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004780{
4781 if (wpa_s->global->p2p_disabled)
4782 return 0;
4783 if (wpa_s->global->p2p == NULL)
4784 return 0;
4785
Dmitry Shmidt04949592012-07-19 12:16:46 -07004786 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4787 ie, ie_len)) {
4788 case P2P_PREQ_NOT_P2P:
4789 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4790 ssi_signal);
4791 /* fall through */
4792 case P2P_PREQ_MALFORMED:
4793 case P2P_PREQ_NOT_LISTEN:
4794 case P2P_PREQ_NOT_PROCESSED:
4795 default: /* make gcc happy */
4796 return 0;
4797 case P2P_PREQ_PROCESSED:
4798 return 1;
4799 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800}
4801
4802
4803void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4804 const u8 *sa, const u8 *bssid,
4805 u8 category, const u8 *data, size_t len, int freq)
4806{
4807 if (wpa_s->global->p2p_disabled)
4808 return;
4809 if (wpa_s->global->p2p == NULL)
4810 return;
4811
4812 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4813 freq);
4814}
4815
4816
4817void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4818{
4819 if (wpa_s->global->p2p_disabled)
4820 return;
4821 if (wpa_s->global->p2p == NULL)
4822 return;
4823
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004824 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825}
4826
4827
4828void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4829{
4830 p2p_group_deinit(wpa_s->p2p_group);
4831 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004832
4833 wpa_s->ap_configured_cb = NULL;
4834 wpa_s->ap_configured_cb_ctx = NULL;
4835 wpa_s->ap_configured_cb_data = NULL;
4836 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837}
4838
4839
4840int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4841{
4842 wpa_s->p2p_long_listen = 0;
4843
4844 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4845 return wpa_drv_p2p_reject(wpa_s, addr);
4846
4847 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4848 return -1;
4849
4850 return p2p_reject(wpa_s->global->p2p, addr);
4851}
4852
4853
4854/* Invite to reinvoke a persistent group */
4855int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004856 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004857 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004858{
4859 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004860 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004861 int force_freq = 0, oper_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004862 int res;
4863
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004864 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004865 if (peer_addr)
4866 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4867 else
4868 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869
Jouni Malinen31be0a42012-08-31 21:20:51 +03004870 wpa_s->p2p_persistent_go_freq = freq;
4871 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 if (ssid->mode == WPAS_MODE_P2P_GO) {
4873 role = P2P_INVITE_ROLE_GO;
4874 if (peer_addr == NULL) {
4875 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4876 "address in invitation command");
4877 return -1;
4878 }
4879 if (wpas_p2p_create_iface(wpa_s)) {
4880 if (wpas_p2p_add_group_interface(wpa_s,
4881 WPA_IF_P2P_GO) < 0) {
4882 wpa_printf(MSG_ERROR, "P2P: Failed to "
4883 "allocate a new interface for the "
4884 "group");
4885 return -1;
4886 }
4887 bssid = wpa_s->pending_interface_addr;
4888 } else
4889 bssid = wpa_s->own_addr;
4890 } else {
4891 role = P2P_INVITE_ROLE_CLIENT;
4892 peer_addr = ssid->bssid;
4893 }
4894 wpa_s->pending_invite_ssid_id = ssid->id;
4895
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004896 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4897 wpa_s->assoc_freq) {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004898 oper_freq = wpa_s->assoc_freq;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004899 if (bssid == NULL)
4900 bssid = bssid_buf;
4901 } else {
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004902 oper_freq = wpa_drv_shared_freq(wpa_s);
4903 if (oper_freq < 0)
4904 oper_freq = 0;
4905 }
4906
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004907 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4908 &oper_freq);
4909 if (res)
4910 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004912 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4913 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4914 ssid->ssid, ssid->ssid_len,
4915 go_dev_addr, 1);
4916
4917 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4918 return -1;
4919
4920 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004921 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4922 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923}
4924
4925
4926/* Invite to join an active group */
4927int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4928 const u8 *peer_addr, const u8 *go_dev_addr)
4929{
4930 struct wpa_global *global = wpa_s->global;
4931 enum p2p_invite_role role;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004932 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004933 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004934 int persistent;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004935 int force_freq = 0, oper_freq = 0, pref_freq = 0;
4936 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937
Jouni Malinen31be0a42012-08-31 21:20:51 +03004938 wpa_s->p2p_persistent_go_freq = 0;
4939 wpa_s->p2p_go_ht40 = 0;
4940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4942 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4943 break;
4944 }
4945 if (wpa_s == NULL) {
4946 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4947 return -1;
4948 }
4949
4950 ssid = wpa_s->current_ssid;
4951 if (ssid == NULL) {
4952 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4953 "invitation");
4954 return -1;
4955 }
4956
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004957 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004958 persistent = ssid->p2p_persistent_group &&
4959 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4960 ssid->ssid, ssid->ssid_len);
4961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962 if (ssid->mode == WPAS_MODE_P2P_GO) {
4963 role = P2P_INVITE_ROLE_ACTIVE_GO;
4964 bssid = wpa_s->own_addr;
4965 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004966 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 } else {
4968 role = P2P_INVITE_ROLE_CLIENT;
4969 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4970 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4971 "invite to current group");
4972 return -1;
4973 }
4974 bssid = wpa_s->bssid;
4975 if (go_dev_addr == NULL &&
4976 !is_zero_ether_addr(wpa_s->go_dev_addr))
4977 go_dev_addr = wpa_s->go_dev_addr;
4978 }
4979 wpa_s->parent->pending_invite_ssid_id = -1;
4980
4981 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4982 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4983 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004984 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004985
4986 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4987 return -1;
4988
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004989 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4990 wpa_s->assoc_freq) {
4991 oper_freq = wpa_s->assoc_freq;
4992 if (bssid == NULL)
4993 bssid = bssid_buf;
4994 } else {
4995 oper_freq = wpa_drv_shared_freq(wpa_s);
4996 if (oper_freq < 0)
4997 oper_freq = 0;
4998 }
4999
5000 res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
5001 &oper_freq);
5002 if (res)
5003 return res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005004 wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005006 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005007 ssid->ssid, ssid->ssid_len, force_freq,
5008 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009}
5010
5011
5012void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5013{
5014 struct wpa_ssid *ssid = wpa_s->current_ssid;
5015 const char *ssid_txt;
5016 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005017 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005019 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005020
Dmitry Shmidt04949592012-07-19 12:16:46 -07005021 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5022 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5023 wpa_s->parent, NULL);
5024 }
5025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005027 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028
5029 wpa_s->show_group_started = 0;
5030
5031 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5032 os_memset(go_dev_addr, 0, ETH_ALEN);
5033 if (ssid->bssid_set)
5034 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5035 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5036 ssid->ssid_len);
5037 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5038
5039 if (wpa_s->global->p2p_group_formation == wpa_s)
5040 wpa_s->global->p2p_group_formation = NULL;
5041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005042 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5043 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044 if (ssid->passphrase == NULL && ssid->psk_set) {
5045 char psk[65];
5046 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5047 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5048 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
5049 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005050 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005051 MAC2STR(go_dev_addr),
5052 persistent ? " [PERSISTENT]" : "");
5053 } else {
5054 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5055 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
5056 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005057 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005058 ssid->passphrase ? ssid->passphrase : "",
5059 MAC2STR(go_dev_addr),
5060 persistent ? " [PERSISTENT]" : "");
5061 }
5062
5063 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005064 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5065 ssid, go_dev_addr);
5066 if (network_id < 0)
5067 network_id = ssid->id;
5068 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005069
5070done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005071 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072}
5073
5074
5075int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5076 u32 interval1, u32 duration2, u32 interval2)
5077{
5078 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5079 return -1;
5080 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5081 return -1;
5082
5083 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5084 wpa_s->current_ssid == NULL ||
5085 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5086 return -1;
5087
5088 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5089 wpa_s->own_addr, wpa_s->assoc_freq,
5090 duration1, interval1, duration2, interval2);
5091}
5092
5093
5094int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5095 unsigned int interval)
5096{
5097 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5098 return -1;
5099
5100 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5101 return -1;
5102
5103 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5104}
5105
5106
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005107static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5108{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005109 if (wpa_s->current_ssid == NULL) {
5110 /*
5111 * current_ssid can be cleared when P2P client interface gets
5112 * disconnected, so assume this interface was used as P2P
5113 * client.
5114 */
5115 return 1;
5116 }
5117 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005118 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5119}
5120
5121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5123{
5124 struct wpa_supplicant *wpa_s = eloop_ctx;
5125
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005126 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005127 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5128 "disabled");
5129 return;
5130 }
5131
Dmitry Shmidt04949592012-07-19 12:16:46 -07005132 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5133 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005134 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135}
5136
5137
5138static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5139{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005140 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005141
Dmitry Shmidt04949592012-07-19 12:16:46 -07005142 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5143 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5146 return;
5147
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005148 timeout = wpa_s->conf->p2p_group_idle;
5149 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5150 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5151 timeout = P2P_MAX_CLIENT_IDLE;
5152
5153 if (timeout == 0)
5154 return;
5155
Dmitry Shmidt04949592012-07-19 12:16:46 -07005156 if (timeout < 0) {
5157 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5158 timeout = 0; /* special client mode no-timeout */
5159 else
5160 return;
5161 }
5162
5163 if (wpa_s->p2p_in_provisioning) {
5164 /*
5165 * Use the normal group formation timeout during the
5166 * provisioning phase to avoid terminating this process too
5167 * early due to group idle timeout.
5168 */
5169 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5170 "during provisioning");
5171 return;
5172 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305173
Dmitry Shmidt04949592012-07-19 12:16:46 -07005174 if (wpa_s->show_group_started) {
5175 /*
5176 * Use the normal group formation timeout between the end of
5177 * the provisioning phase and completion of 4-way handshake to
5178 * avoid terminating this process too early due to group idle
5179 * timeout.
5180 */
5181 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5182 "while waiting for initial 4-way handshake to "
5183 "complete");
5184 return;
5185 }
5186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005187 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005188 timeout);
5189 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5190 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005191}
5192
5193
Jouni Malinen2b89da82012-08-31 22:04:41 +03005194/* Returns 1 if the interface was removed */
5195int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5196 u16 reason_code, const u8 *ie, size_t ie_len,
5197 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005198{
5199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005200 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005201 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005202 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005203
Dmitry Shmidt04949592012-07-19 12:16:46 -07005204 if (!locally_generated)
5205 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5206 ie_len);
5207
5208 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5209 wpa_s->current_ssid &&
5210 wpa_s->current_ssid->p2p_group &&
5211 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5212 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5213 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005214 if (wpas_p2p_group_delete(wpa_s,
5215 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5216 > 0)
5217 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005218 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005219
5220 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005221}
5222
5223
5224void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005225 u16 reason_code, const u8 *ie, size_t ie_len,
5226 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005227{
5228 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5229 return;
5230 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5231 return;
5232
Dmitry Shmidt04949592012-07-19 12:16:46 -07005233 if (!locally_generated)
5234 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5235 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005236}
5237
5238
5239void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5240{
5241 struct p2p_data *p2p = wpa_s->global->p2p;
5242
5243 if (p2p == NULL)
5244 return;
5245
5246 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5247 return;
5248
5249 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5250 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5251
5252 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5253 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5254
5255 if (wpa_s->wps &&
5256 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5257 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5258
5259 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5260 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5261
5262 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5263 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5264 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5265 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5266 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5267 }
5268
5269 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5270 p2p_set_sec_dev_types(p2p,
5271 (void *) wpa_s->conf->sec_device_type,
5272 wpa_s->conf->num_sec_device_types);
5273
5274 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5275 int i;
5276 p2p_remove_wps_vendor_extensions(p2p);
5277 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5278 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5279 continue;
5280 p2p_add_wps_vendor_extension(
5281 p2p, wpa_s->conf->wps_vendor_ext[i]);
5282 }
5283 }
5284
5285 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5286 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5287 char country[3];
5288 country[0] = wpa_s->conf->country[0];
5289 country[1] = wpa_s->conf->country[1];
5290 country[2] = 0x04;
5291 p2p_set_country(p2p, country);
5292 }
5293
5294 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5295 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5296 wpa_s->conf->p2p_ssid_postfix ?
5297 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5298 0);
5299 }
5300
5301 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5302 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005303
5304 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5305 u8 reg_class, channel;
5306 int ret;
5307 unsigned int r;
5308 if (wpa_s->conf->p2p_listen_reg_class &&
5309 wpa_s->conf->p2p_listen_channel) {
5310 reg_class = wpa_s->conf->p2p_listen_reg_class;
5311 channel = wpa_s->conf->p2p_listen_channel;
5312 } else {
5313 reg_class = 81;
5314 /*
5315 * Pick one of the social channels randomly as the
5316 * listen channel.
5317 */
5318 os_get_random((u8 *) &r, sizeof(r));
5319 channel = 1 + (r % 3) * 5;
5320 }
5321 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5322 if (ret)
5323 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5324 "failed: %d", ret);
5325 }
5326 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5327 u8 op_reg_class, op_channel, cfg_op_channel;
5328 int ret = 0;
5329 unsigned int r;
5330 if (wpa_s->conf->p2p_oper_reg_class &&
5331 wpa_s->conf->p2p_oper_channel) {
5332 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5333 op_channel = wpa_s->conf->p2p_oper_channel;
5334 cfg_op_channel = 1;
5335 } else {
5336 op_reg_class = 81;
5337 /*
5338 * Use random operation channel from (1, 6, 11)
5339 *if no other preference is indicated.
5340 */
5341 os_get_random((u8 *) &r, sizeof(r));
5342 op_channel = 1 + (r % 3) * 5;
5343 cfg_op_channel = 0;
5344 }
5345 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5346 cfg_op_channel);
5347 if (ret)
5348 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5349 "failed: %d", ret);
5350 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005351
5352 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5353 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5354 wpa_s->conf->p2p_pref_chan) < 0) {
5355 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5356 "update failed");
5357 }
5358 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005359}
5360
5361
5362int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5363 int duration)
5364{
5365 if (!wpa_s->ap_iface)
5366 return -1;
5367 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5368 duration);
5369}
5370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005371
5372int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5373{
5374 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5375 return -1;
5376 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5377 return -1;
5378
5379 wpa_s->global->cross_connection = enabled;
5380 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5381
5382 if (!enabled) {
5383 struct wpa_supplicant *iface;
5384
5385 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5386 {
5387 if (iface->cross_connect_enabled == 0)
5388 continue;
5389
5390 iface->cross_connect_enabled = 0;
5391 iface->cross_connect_in_use = 0;
5392 wpa_msg(iface->parent, MSG_INFO,
5393 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5394 iface->ifname, iface->cross_connect_uplink);
5395 }
5396 }
5397
5398 return 0;
5399}
5400
5401
5402static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5403{
5404 struct wpa_supplicant *iface;
5405
5406 if (!uplink->global->cross_connection)
5407 return;
5408
5409 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5410 if (!iface->cross_connect_enabled)
5411 continue;
5412 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5413 0)
5414 continue;
5415 if (iface->ap_iface == NULL)
5416 continue;
5417 if (iface->cross_connect_in_use)
5418 continue;
5419
5420 iface->cross_connect_in_use = 1;
5421 wpa_msg(iface->parent, MSG_INFO,
5422 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5423 iface->ifname, iface->cross_connect_uplink);
5424 }
5425}
5426
5427
5428static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5429{
5430 struct wpa_supplicant *iface;
5431
5432 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5433 if (!iface->cross_connect_enabled)
5434 continue;
5435 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5436 0)
5437 continue;
5438 if (!iface->cross_connect_in_use)
5439 continue;
5440
5441 wpa_msg(iface->parent, MSG_INFO,
5442 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5443 iface->ifname, iface->cross_connect_uplink);
5444 iface->cross_connect_in_use = 0;
5445 }
5446}
5447
5448
5449void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5450{
5451 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5452 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5453 wpa_s->cross_connect_disallowed)
5454 wpas_p2p_disable_cross_connect(wpa_s);
5455 else
5456 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005457 if (!wpa_s->ap_iface &&
5458 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5459 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460}
5461
5462
5463void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5464{
5465 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005466 if (!wpa_s->ap_iface &&
5467 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5468 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005469 wpas_p2p_set_group_idle_timeout(wpa_s);
5470}
5471
5472
5473static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5474{
5475 struct wpa_supplicant *iface;
5476
5477 if (!wpa_s->global->cross_connection)
5478 return;
5479
5480 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5481 if (iface == wpa_s)
5482 continue;
5483 if (iface->drv_flags &
5484 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5485 continue;
5486 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5487 continue;
5488
5489 wpa_s->cross_connect_enabled = 1;
5490 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5491 sizeof(wpa_s->cross_connect_uplink));
5492 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5493 "%s to %s whenever uplink is available",
5494 wpa_s->ifname, wpa_s->cross_connect_uplink);
5495
5496 if (iface->ap_iface || iface->current_ssid == NULL ||
5497 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5498 iface->cross_connect_disallowed ||
5499 iface->wpa_state != WPA_COMPLETED)
5500 break;
5501
5502 wpa_s->cross_connect_in_use = 1;
5503 wpa_msg(wpa_s->parent, MSG_INFO,
5504 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5505 wpa_s->ifname, wpa_s->cross_connect_uplink);
5506 break;
5507 }
5508}
5509
5510
5511int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5512{
5513 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5514 !wpa_s->p2p_in_provisioning)
5515 return 0; /* not P2P client operation */
5516
5517 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5518 "session overlap");
5519 if (wpa_s != wpa_s->parent)
5520 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5521
5522 if (wpa_s->global->p2p)
5523 p2p_group_formation_failed(wpa_s->global->p2p);
5524
5525 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5526 wpa_s->parent, NULL);
5527
5528 wpas_group_formation_completed(wpa_s, 0);
5529 return 1;
5530}
5531
5532
5533void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5534{
5535 struct p2p_channels chan;
5536
5537 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5538 return;
5539
5540 os_memset(&chan, 0, sizeof(chan));
5541 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5542 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5543 "channel list");
5544 return;
5545 }
5546
5547 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5548}
5549
5550
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005551static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5552 struct wpa_scan_results *scan_res)
5553{
5554 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5555}
5556
5557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005558int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5559{
5560 struct wpa_global *global = wpa_s->global;
5561 int found = 0;
5562 const u8 *peer;
5563
5564 if (global->p2p == NULL)
5565 return -1;
5566
5567 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5568
5569 if (wpa_s->pending_interface_name[0] &&
5570 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5571 found = 1;
5572
5573 peer = p2p_get_go_neg_peer(global->p2p);
5574 if (peer) {
5575 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5576 MACSTR, MAC2STR(peer));
5577 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005578 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005579 }
5580
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005581 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5582 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5583 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5584 found = 1;
5585 }
5586
5587 if (wpa_s->pending_pd_before_join) {
5588 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5589 wpa_s->pending_pd_before_join = 0;
5590 found = 1;
5591 }
5592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005593 wpas_p2p_stop_find(wpa_s);
5594
5595 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5596 if (wpa_s == global->p2p_group_formation &&
5597 (wpa_s->p2p_in_provisioning ||
5598 wpa_s->parent->pending_interface_type ==
5599 WPA_IF_P2P_CLIENT)) {
5600 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5601 "formation found - cancelling",
5602 wpa_s->ifname);
5603 found = 1;
5604 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5605 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005606 if (wpa_s->p2p_in_provisioning) {
5607 wpas_group_formation_completed(wpa_s, 0);
5608 break;
5609 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005610 wpas_p2p_group_delete(wpa_s,
5611 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005612 break;
5613 }
5614 }
5615
5616 if (!found) {
5617 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5618 return -1;
5619 }
5620
5621 return 0;
5622}
5623
5624
5625void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5626{
5627 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5628 return;
5629
5630 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5631 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005632 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005633}
5634
5635
5636void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5637 int freq_24, int freq_5, int freq_overall)
5638{
5639 struct p2p_data *p2p = wpa_s->global->p2p;
5640 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5641 return;
5642 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5643}
5644
5645
5646int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5647{
5648 u8 peer[ETH_ALEN];
5649 struct p2p_data *p2p = wpa_s->global->p2p;
5650
5651 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5652 return -1;
5653
5654 if (hwaddr_aton(addr, peer))
5655 return -1;
5656
5657 return p2p_unauthorize(p2p, peer);
5658}
5659
5660
5661/**
5662 * wpas_p2p_disconnect - Disconnect from a P2P Group
5663 * @wpa_s: Pointer to wpa_supplicant data
5664 * Returns: 0 on success, -1 on failure
5665 *
5666 * This can be used to disconnect from a group in which the local end is a P2P
5667 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5668 * virtual network interface was created for this group, that interface will be
5669 * removed. Otherwise, only the configured P2P group network will be removed
5670 * from the interface.
5671 */
5672int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5673{
5674
5675 if (wpa_s == NULL)
5676 return -1;
5677
Jouni Malinen2b89da82012-08-31 22:04:41 +03005678 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5679 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005681
5682
5683int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5684{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005685 int ret;
5686
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005687 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5688 return 0;
5689
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005690 ret = p2p_in_progress(wpa_s->global->p2p);
5691 if (ret == 0) {
5692 /*
5693 * Check whether there is an ongoing WPS provisioning step (or
5694 * other parts of group formation) on another interface since
5695 * p2p_in_progress() does not report this to avoid issues for
5696 * scans during such provisioning step.
5697 */
5698 if (wpa_s->global->p2p_group_formation &&
5699 wpa_s->global->p2p_group_formation != wpa_s) {
5700 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5701 "in group formation",
5702 wpa_s->global->p2p_group_formation->ifname);
5703 ret = 1;
5704 }
5705 }
5706
5707 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005708}
5709
5710
5711void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5712 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005713{
5714 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5715 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5716 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005717 /**
5718 * Remove the network by scheduling the group formation
5719 * timeout to happen immediately. The teardown code
5720 * needs to be scheduled to run asynch later so that we
5721 * don't delete data from under ourselves unexpectedly.
5722 * Calling wpas_p2p_group_formation_timeout directly
5723 * causes a series of crashes in WPS failure scenarios.
5724 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005725 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5726 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005727 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5728 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005729 }
5730}
5731
5732
5733struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005734 const u8 *addr, const u8 *ssid,
5735 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005736{
5737 struct wpa_ssid *s;
5738 size_t i;
5739
5740 for (s = wpa_s->conf->ssid; s; s = s->next) {
5741 if (s->disabled != 2)
5742 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005743 if (ssid &&
5744 (ssid_len != s->ssid_len ||
5745 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5746 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005747 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5748 return s; /* peer is GO in the persistent group */
5749 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5750 continue;
5751 for (i = 0; i < s->num_p2p_clients; i++) {
5752 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5753 addr, ETH_ALEN) == 0)
5754 return s; /* peer is P2P client in persistent
5755 * group */
5756 }
5757 }
5758
5759 return NULL;
5760}
5761
5762
5763void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5764 const u8 *addr)
5765{
5766 if (addr == NULL)
5767 return;
5768 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5769}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005770
Dmitry Shmidt04949592012-07-19 12:16:46 -07005771
5772static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5773 int group_added)
5774{
5775 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005776 if (wpa_s->global->p2p_group_formation)
5777 group = wpa_s->global->p2p_group_formation;
5778 wpa_s = wpa_s->parent;
5779 offchannel_send_action_done(wpa_s);
5780 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005781 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005782 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5783 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5784 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5785 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5786 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005787 wpa_s->p2p_pd_before_go_neg,
5788 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005789}
5790
5791
5792int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5793{
5794 if (!wpa_s->p2p_fallback_to_go_neg ||
5795 wpa_s->p2p_in_provisioning <= 5)
5796 return 0;
5797
5798 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5799 return 0; /* peer operating as a GO */
5800
5801 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5802 "fallback to GO Negotiation");
5803 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5804
5805 return 1;
5806}
5807
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005808
5809unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5810{
5811 const char *rn, *rn2;
5812 struct wpa_supplicant *ifs;
5813
5814 if (wpa_s->wpa_state > WPA_SCANNING) {
5815 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5816 "concurrent operation",
5817 P2P_CONCURRENT_SEARCH_DELAY);
5818 return P2P_CONCURRENT_SEARCH_DELAY;
5819 }
5820
5821 if (!wpa_s->driver->get_radio_name)
5822 return 0;
5823 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5824 if (rn == NULL || rn[0] == '\0')
5825 return 0;
5826
5827 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5828 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5829 continue;
5830
5831 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5832 if (!rn2 || os_strcmp(rn, rn2) != 0)
5833 continue;
5834 if (ifs->wpa_state > WPA_SCANNING) {
5835 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5836 "delay due to concurrent operation on "
5837 "interface %s",
5838 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5839 return P2P_CONCURRENT_SEARCH_DELAY;
5840 }
5841 }
5842
5843 return 0;
5844}
5845
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005846
5847void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
5848{
5849 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
5850 "pending anymore (sta_scan_pending=%d "
5851 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
5852 wpa_s->global->p2p_cb_on_scan_complete);
5853 wpa_s->sta_scan_pending = 0;
5854
5855 if (!wpa_s->global->p2p_cb_on_scan_complete)
5856 return;
5857 wpa_s->global->p2p_cb_on_scan_complete = 0;
5858
5859 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5860 return;
5861
5862 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
5863 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
5864 "continued after successful connection");
5865 p2p_increase_search_delay(wpa_s->global->p2p,
5866 wpas_p2p_search_delay(wpa_s));
5867 }
5868}
5869
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005870#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07005871static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
5872{
5873 struct wpa_supplicant *wpa_s = eloop_ctx;
5874
5875 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
5876 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
5877}
5878
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005879int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5880 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005881{
5882 struct wpa_supplicant *iface = NULL;
5883 struct p2p_data *p2p = wpa_s->global->p2p;
5884
5885 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005886 if ((iface->current_ssid) &&
5887 (iface->current_ssid->frequency != freq) &&
5888 ((iface->p2p_group_interface) ||
5889 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005890
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005891 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5892 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5893 /* Try to see whether we can move the GO. If it
5894 * is not possible, remove the GO interface
5895 */
5896 if (wpa_drv_switch_channel(iface, freq) == 0) {
5897 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5898 iface->current_ssid->frequency = freq;
5899 continue;
5900 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005901 }
5902
5903 /* If GO cannot be moved or if the conflicting interface is a
5904 * P2P Client, remove the interface depending up on the connection
5905 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005906 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005907 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005908 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005909 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005910 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07005911 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
5912 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005913 /* If connection in progress is p2p connection, do not proceed for the connection */
5914 if (wpa_s == iface)
5915 return -1;
5916 else
5917 /* If connection in progress is STA connection, proceed for the connection */
5918 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005919 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005920 /* P2p connection has priority, disable the STA network*/
5921 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5922 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5923 " id=%d", ssid->id);
5924 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5925 if (wpa_s == iface) {
5926 /* p2p connection is in progress, continue connecting...*/
5927 return 0;
5928 }
5929 else {
5930 /* STA connection is in progress, do not allow to continue */
5931 return -1;
5932 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005933 }
5934 }
5935 }
5936 return 0;
5937}
5938#endif