blob: 6cff577bfa9213713f48e464b12382b0cc7e6a87 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "common/ieee802_11_common.h"
14#include "common/ieee802_11_defs.h"
15#include "common/wpa_ctrl.h"
16#include "wps/wps_i.h"
17#include "p2p/p2p.h"
18#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080019#include "ap/ap_config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070021#include "eapol_supp/eapol_supp_sm.h"
22#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "wpa_supplicant_i.h"
24#include "driver_i.h"
25#include "ap.h"
26#include "config_ssid.h"
27#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "notify.h"
29#include "scan.h"
30#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "wps_supplicant.h"
33#include "p2p_supplicant.h"
34
35
36/*
37 * How many times to try to scan to find the GO before giving up on join
38 * request.
39 */
40#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
41
Dmitry Shmidt04949592012-07-19 12:16:46 -070042#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080044#ifndef P2P_MAX_CLIENT_IDLE
45/*
46 * How many seconds to try to reconnect to the GO when connection in P2P client
47 * role has been lost.
48 */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070049#ifdef ANDROID_P2P
50#define P2P_MAX_CLIENT_IDLE 20
51#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080052#define P2P_MAX_CLIENT_IDLE 10
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070053#endif /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054#endif /* P2P_MAX_CLIENT_IDLE */
55
Dmitry Shmidt04949592012-07-19 12:16:46 -070056#ifndef P2P_MAX_INITIAL_CONN_WAIT
57/*
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
60 */
61#define P2P_MAX_INITIAL_CONN_WAIT 10
62#endif /* P2P_MAX_INITIAL_CONN_WAIT */
63
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070064#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070065#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066#endif /* P2P_CONCURRENT_SEARCH_DELAY */
67
68enum p2p_group_removal_reason {
69 P2P_GROUP_REMOVAL_UNKNOWN,
70 P2P_GROUP_REMOVAL_SILENT,
71 P2P_GROUP_REMOVAL_FORMATION_FAILED,
72 P2P_GROUP_REMOVAL_REQUESTED,
73 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
74 P2P_GROUP_REMOVAL_UNAVAILABLE,
75 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
76#ifdef ANDROID_P2P
77 P2P_GROUP_REMOVAL_FREQ_CONFLICT
78#endif
79};
80
Jouni Malinendc7b7132012-09-14 12:53:47 -070081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
83static struct wpa_supplicant *
84wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
85 int go);
86static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070087static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
89static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070090 const u8 *dev_addr, enum p2p_wps_method wps_method,
91 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
93static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
94static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
95static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -080096static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
97 void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -070098static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
99 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800100static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101
102
103static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
104 struct wpa_scan_results *scan_res)
105{
106 size_t i;
107
108 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
109 return;
110
111 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
112 (int) scan_res->num);
113
114 for (i = 0; i < scan_res->num; i++) {
115 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800116 struct os_time time_tmp_age, entry_ts;
117 time_tmp_age.sec = bss->age / 1000;
118 time_tmp_age.usec = (bss->age % 1000) * 1000;
119 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700120 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800121 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 (const u8 *) (bss + 1),
123 bss->ie_len) > 0)
124 break;
125 }
126
127 p2p_scan_res_handled(wpa_s->global->p2p);
128}
129
130
131static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
132 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700133 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134{
135 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700136 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137 struct wpa_driver_scan_params params;
138 int ret;
139 struct wpabuf *wps_ie, *ies;
140 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800141 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142
143 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
144 return -1;
145
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700146 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
147 if (ifs->sta_scan_pending &&
148 wpas_p2p_in_progress(wpa_s) == 2) {
149 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
150 "pending station mode scan to be "
151 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700152 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700153 wpa_supplicant_req_scan(ifs, 0, 0);
154 return 1;
155 }
156 }
157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158 os_memset(&params, 0, sizeof(params));
159
160 /* P2P Wildcard SSID */
161 params.num_ssids = 1;
162 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
163 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
164
165 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700166 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
167 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168 num_req_dev_types, req_dev_types);
169 if (wps_ie == NULL)
170 return -1;
171
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800172 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
173 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700174 if (ies == NULL) {
175 wpabuf_free(wps_ie);
176 return -1;
177 }
178 wpabuf_put_buf(ies, wps_ie);
179 wpabuf_free(wps_ie);
180
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800181 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800183 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 params.extra_ies = wpabuf_head(ies);
185 params.extra_ies_len = wpabuf_len(ies);
186
187 switch (type) {
188 case P2P_SCAN_SOCIAL:
189 params.freqs = social_channels;
190 break;
191 case P2P_SCAN_FULL:
192 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700193 case P2P_SCAN_SPECIFIC:
194 social_channels[0] = freq;
195 social_channels[1] = 0;
196 params.freqs = social_channels;
197 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 case P2P_SCAN_SOCIAL_PLUS_ONE:
199 social_channels[3] = freq;
200 params.freqs = social_channels;
201 break;
202 }
203
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800204 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205
206 wpabuf_free(ies);
207
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800208 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700209 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
210 if (ifs->scanning ||
211 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
212 wpa_s->global->p2p_cb_on_scan_complete = 1;
213 ret = 1;
214 break;
215 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800216 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700217 } else
218 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 return ret;
221}
222
223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
225{
226 switch (p2p_group_interface) {
227 case P2P_GROUP_INTERFACE_PENDING:
228 return WPA_IF_P2P_GROUP;
229 case P2P_GROUP_INTERFACE_GO:
230 return WPA_IF_P2P_GO;
231 case P2P_GROUP_INTERFACE_CLIENT:
232 return WPA_IF_P2P_CLIENT;
233 }
234
235 return WPA_IF_P2P_GROUP;
236}
237
238
239static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
240 const u8 *ssid,
241 size_t ssid_len, int *go)
242{
243 struct wpa_ssid *s;
244
245 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
246 for (s = wpa_s->conf->ssid; s; s = s->next) {
247 if (s->disabled != 0 || !s->p2p_group ||
248 s->ssid_len != ssid_len ||
249 os_memcmp(ssid, s->ssid, ssid_len) != 0)
250 continue;
251 if (s->mode == WPAS_MODE_P2P_GO &&
252 s != wpa_s->current_ssid)
253 continue;
254 if (go)
255 *go = s->mode == WPAS_MODE_P2P_GO;
256 return wpa_s;
257 }
258 }
259
260 return NULL;
261}
262
263
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700264static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
265 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266{
267 struct wpa_ssid *ssid;
268 char *gtype;
269 const char *reason;
270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271 ssid = wpa_s->current_ssid;
272 if (ssid == NULL) {
273 /*
274 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700275 * pending P2P group interface waiting for provisioning or a
276 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 */
278 ssid = wpa_s->conf->ssid;
279 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700280 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 break;
282 ssid = ssid->next;
283 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300284 if (ssid == NULL &&
285 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
286 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700287 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
288 "not found");
289 return -1;
290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 }
292 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
293 gtype = "GO";
294 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
295 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
296 wpa_s->reassociate = 0;
297 wpa_s->disconnected = 1;
298 wpa_supplicant_deauthenticate(wpa_s,
299 WLAN_REASON_DEAUTH_LEAVING);
300 gtype = "client";
301 } else
302 gtype = "GO";
303 if (wpa_s->cross_connect_in_use) {
304 wpa_s->cross_connect_in_use = 0;
305 wpa_msg(wpa_s->parent, MSG_INFO,
306 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
307 wpa_s->ifname, wpa_s->cross_connect_uplink);
308 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700309 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310 case P2P_GROUP_REMOVAL_REQUESTED:
311 reason = " reason=REQUESTED";
312 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700313 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
314 reason = " reason=FORMATION_FAILED";
315 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
317 reason = " reason=IDLE";
318 break;
319 case P2P_GROUP_REMOVAL_UNAVAILABLE:
320 reason = " reason=UNAVAILABLE";
321 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700322 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
323 reason = " reason=GO_ENDING_SESSION";
324 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700325#ifdef ANDROID_P2P
326 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
327 reason = " reason=FREQ_CONFLICT";
328 break;
329#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330 default:
331 reason = "";
332 break;
333 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700334 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700335 wpa_msg(wpa_s->parent, MSG_INFO,
336 P2P_EVENT_GROUP_REMOVED "%s %s%s",
337 wpa_s->ifname, gtype, reason);
338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339
Dmitry Shmidt04949592012-07-19 12:16:46 -0700340 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
341 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800342 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
343 wpa_s->parent, NULL) > 0)
344 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
345 "timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700346
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700347 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
349
350 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
351 struct wpa_global *global;
352 char *ifname;
353 enum wpa_driver_if_type type;
354 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
355 wpa_s->ifname);
356 global = wpa_s->global;
357 ifname = os_strdup(wpa_s->ifname);
358 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700359 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360 wpa_s = global->ifaces;
361 if (wpa_s && ifname)
362 wpa_drv_if_remove(wpa_s, type, ifname);
363 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300364 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365 }
366
367 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
368 if (ssid && (ssid->p2p_group ||
369 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
370 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
371 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700372 if (ssid == wpa_s->current_ssid) {
373 wpa_sm_set_config(wpa_s->wpa, NULL);
374 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700376 }
377 /*
378 * Networks objects created during any P2P activities are not
379 * exposed out as they might/will confuse certain non-P2P aware
380 * applications since these network objects won't behave like
381 * regular ones.
382 *
383 * Likewise, we don't send out network removed signals for such
384 * network objects.
385 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 wpa_config_remove_network(wpa_s->conf, id);
387 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800388 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700389 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390 } else {
391 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
392 "found");
393 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700394 if (wpa_s->ap_iface)
395 wpa_supplicant_ap_deinit(wpa_s);
396 else
397 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700398
399 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400}
401
402
403static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
404 u8 *go_dev_addr,
405 const u8 *ssid, size_t ssid_len)
406{
407 struct wpa_bss *bss;
408 const u8 *bssid;
409 struct wpabuf *p2p;
410 u8 group_capab;
411 const u8 *addr;
412
413 if (wpa_s->go_params)
414 bssid = wpa_s->go_params->peer_interface_addr;
415 else
416 bssid = wpa_s->bssid;
417
418 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
419 if (bss == NULL) {
420 u8 iface_addr[ETH_ALEN];
421 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
422 iface_addr) == 0)
423 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
424 }
425 if (bss == NULL) {
426 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
427 "group is persistent - BSS " MACSTR " not found",
428 MAC2STR(bssid));
429 return 0;
430 }
431
432 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
433 if (p2p == NULL) {
434 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
435 "group is persistent - BSS " MACSTR
436 " did not include P2P IE", MAC2STR(bssid));
437 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
438 (u8 *) (bss + 1), bss->ie_len);
439 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
440 ((u8 *) bss + 1) + bss->ie_len,
441 bss->beacon_ie_len);
442 return 0;
443 }
444
445 group_capab = p2p_get_group_capab(p2p);
446 addr = p2p_get_go_dev_addr(p2p);
447 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
448 "group_capab=0x%x", group_capab);
449 if (addr) {
450 os_memcpy(go_dev_addr, addr, ETH_ALEN);
451 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
452 MAC2STR(addr));
453 } else
454 os_memset(go_dev_addr, 0, ETH_ALEN);
455 wpabuf_free(p2p);
456
457 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
458 "go_dev_addr=" MACSTR,
459 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
460
461 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
462}
463
464
Jouni Malinen75ecf522011-06-27 15:19:46 -0700465static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
466 struct wpa_ssid *ssid,
467 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468{
469 struct wpa_ssid *s;
470 int changed = 0;
471
472 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
473 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
474 for (s = wpa_s->conf->ssid; s; s = s->next) {
475 if (s->disabled == 2 &&
476 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
477 s->ssid_len == ssid->ssid_len &&
478 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
479 break;
480 }
481
482 if (s) {
483 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
484 "entry");
485 if (ssid->passphrase && !s->passphrase)
486 changed = 1;
487 else if (ssid->passphrase && s->passphrase &&
488 os_strcmp(ssid->passphrase, s->passphrase) != 0)
489 changed = 1;
490 } else {
491 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
492 "entry");
493 changed = 1;
494 s = wpa_config_add_network(wpa_s->conf);
495 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700496 return -1;
497
498 /*
499 * Instead of network_added we emit persistent_group_added
500 * notification. Also to keep the defense checks in
501 * persistent_group obj registration method, we set the
502 * relevant flags in s to designate it as a persistent group.
503 */
504 s->p2p_group = 1;
505 s->p2p_persistent_group = 1;
506 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507 wpa_config_set_network_defaults(s);
508 }
509
510 s->p2p_group = 1;
511 s->p2p_persistent_group = 1;
512 s->disabled = 2;
513 s->bssid_set = 1;
514 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
515 s->mode = ssid->mode;
516 s->auth_alg = WPA_AUTH_ALG_OPEN;
517 s->key_mgmt = WPA_KEY_MGMT_PSK;
518 s->proto = WPA_PROTO_RSN;
519 s->pairwise_cipher = WPA_CIPHER_CCMP;
520 s->export_keys = 1;
521 if (ssid->passphrase) {
522 os_free(s->passphrase);
523 s->passphrase = os_strdup(ssid->passphrase);
524 }
525 if (ssid->psk_set) {
526 s->psk_set = 1;
527 os_memcpy(s->psk, ssid->psk, 32);
528 }
529 if (s->passphrase && !s->psk_set)
530 wpa_config_update_psk(s);
531 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
532 os_free(s->ssid);
533 s->ssid = os_malloc(ssid->ssid_len);
534 }
535 if (s->ssid) {
536 s->ssid_len = ssid->ssid_len;
537 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
538 }
539
540#ifndef CONFIG_NO_CONFIG_WRITE
541 if (changed && wpa_s->conf->update_config &&
542 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
543 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
544 }
545#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700546
547 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548}
549
550
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800551static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
552 const u8 *addr)
553{
554 struct wpa_ssid *ssid, *s;
555 u8 *n;
556 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700557 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800558
559 ssid = wpa_s->current_ssid;
560 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
561 !ssid->p2p_persistent_group)
562 return;
563
564 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
565 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
566 continue;
567
568 if (s->ssid_len == ssid->ssid_len &&
569 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
570 break;
571 }
572
573 if (s == NULL)
574 return;
575
576 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
577 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700578 ETH_ALEN) != 0)
579 continue;
580
581 if (i == s->num_p2p_clients - 1)
582 return; /* already the most recent entry */
583
584 /* move the entry to mark it most recent */
585 os_memmove(s->p2p_client_list + i * ETH_ALEN,
586 s->p2p_client_list + (i + 1) * ETH_ALEN,
587 (s->num_p2p_clients - i - 1) * ETH_ALEN);
588 os_memcpy(s->p2p_client_list +
589 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
590 found = 1;
591 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800592 }
593
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700594 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
595 n = os_realloc_array(s->p2p_client_list,
596 s->num_p2p_clients + 1, ETH_ALEN);
597 if (n == NULL)
598 return;
599 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
600 s->p2p_client_list = n;
601 s->num_p2p_clients++;
602 } else if (!found) {
603 /* Not enough room for an additional entry - drop the oldest
604 * entry */
605 os_memmove(s->p2p_client_list,
606 s->p2p_client_list + ETH_ALEN,
607 (s->num_p2p_clients - 1) * ETH_ALEN);
608 os_memcpy(s->p2p_client_list +
609 (s->num_p2p_clients - 1) * ETH_ALEN,
610 addr, ETH_ALEN);
611 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800612
613#ifndef CONFIG_NO_CONFIG_WRITE
614 if (wpa_s->parent->conf->update_config &&
615 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
616 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
617#endif /* CONFIG_NO_CONFIG_WRITE */
618}
619
620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
622 int success)
623{
624 struct wpa_ssid *ssid;
625 const char *ssid_txt;
626 int client;
627 int persistent;
628 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700629 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630
631 /*
632 * This callback is likely called for the main interface. Update wpa_s
633 * to use the group interface if a new interface was created for the
634 * group.
635 */
636 if (wpa_s->global->p2p_group_formation)
637 wpa_s = wpa_s->global->p2p_group_formation;
638 wpa_s->global->p2p_group_formation = NULL;
639 wpa_s->p2p_in_provisioning = 0;
640
641 if (!success) {
642 wpa_msg(wpa_s->parent, MSG_INFO,
643 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700644 wpas_p2p_group_delete(wpa_s,
645 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646 return;
647 }
648
649 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
650
651 ssid = wpa_s->current_ssid;
652 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
653 ssid->mode = WPAS_MODE_P2P_GO;
654 p2p_group_notif_formation_done(wpa_s->p2p_group);
655 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
656 }
657
658 persistent = 0;
659 if (ssid) {
660 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
661 client = ssid->mode == WPAS_MODE_INFRA;
662 if (ssid->mode == WPAS_MODE_P2P_GO) {
663 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700664 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
665 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666 } else
667 persistent = wpas_p2p_persistent_group(wpa_s,
668 go_dev_addr,
669 ssid->ssid,
670 ssid->ssid_len);
671 } else {
672 ssid_txt = "";
673 client = wpa_s->p2p_group_interface ==
674 P2P_GROUP_INTERFACE_CLIENT;
675 os_memset(go_dev_addr, 0, ETH_ALEN);
676 }
677
678 wpa_s->show_group_started = 0;
679 if (client) {
680 /*
681 * Indicate event only after successfully completed 4-way
682 * handshake, i.e., when the interface is ready for data
683 * packets.
684 */
685 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700686#ifdef ANDROID_P2P
687 /* For client Second phase of Group formation (4-way handshake) can be still pending
688 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700689 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 -0700690 wpa_s->global->p2p_group_formation = wpa_s;
691#endif
692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
694 char psk[65];
695 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
696 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
697 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
698 "%s",
699 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
700 MAC2STR(go_dev_addr),
701 persistent ? " [PERSISTENT]" : "");
702 wpas_p2p_cross_connect_setup(wpa_s);
703 wpas_p2p_set_group_idle_timeout(wpa_s);
704 } else {
705 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
706 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
707 "go_dev_addr=" MACSTR "%s",
708 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
709 ssid && ssid->passphrase ? ssid->passphrase : "",
710 MAC2STR(go_dev_addr),
711 persistent ? " [PERSISTENT]" : "");
712 wpas_p2p_cross_connect_setup(wpa_s);
713 wpas_p2p_set_group_idle_timeout(wpa_s);
714 }
715
716 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700717 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
718 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800719 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700720 network_id = ssid->id;
721 if (!client)
722 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723}
724
725
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800726static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
727 unsigned int freq,
728 const u8 *dst, const u8 *src,
729 const u8 *bssid,
730 const u8 *data, size_t data_len,
731 enum offchannel_send_action_result
732 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800734 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
737 return;
738 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
739 return;
740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800741 switch (result) {
742 case OFFCHANNEL_SEND_ACTION_SUCCESS:
743 res = P2P_SEND_ACTION_SUCCESS;
744 break;
745 case OFFCHANNEL_SEND_ACTION_NO_ACK:
746 res = P2P_SEND_ACTION_NO_ACK;
747 break;
748 case OFFCHANNEL_SEND_ACTION_FAILED:
749 res = P2P_SEND_ACTION_FAILED;
750 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751 }
752
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800753 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800755 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
756 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800757 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800758 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
759 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800761 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
762 "during p2p_connect-auto");
763 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
764 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765 }
766}
767
768
769static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
770 const u8 *src, const u8 *bssid, const u8 *buf,
771 size_t len, unsigned int wait_time)
772{
773 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800774 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
775 wait_time,
776 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777}
778
779
780static void wpas_send_action_done(void *ctx)
781{
782 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800783 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784}
785
786
787static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
788 struct p2p_go_neg_results *params)
789{
790 if (wpa_s->go_params == NULL) {
791 wpa_s->go_params = os_malloc(sizeof(*params));
792 if (wpa_s->go_params == NULL)
793 return -1;
794 }
795 os_memcpy(wpa_s->go_params, params, sizeof(*params));
796 return 0;
797}
798
799
800static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
801 struct p2p_go_neg_results *res)
802{
803 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
804 MAC2STR(res->peer_interface_addr));
805 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
806 res->ssid, res->ssid_len);
807 wpa_supplicant_ap_deinit(wpa_s);
808 wpas_copy_go_neg_results(wpa_s, res);
809 if (res->wps_method == WPS_PBC)
810 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
811 else {
812 u16 dev_pw_id = DEV_PW_DEFAULT;
813 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
814 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
815 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
816 wpa_s->p2p_pin, 1, dev_pw_id);
817 }
818}
819
820
821static void p2p_go_configured(void *ctx, void *data)
822{
823 struct wpa_supplicant *wpa_s = ctx;
824 struct p2p_go_neg_results *params = data;
825 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700826 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827
828 ssid = wpa_s->current_ssid;
829 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
830 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
831 if (wpa_s->global->p2p_group_formation == wpa_s)
832 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800833 if (os_strlen(params->passphrase) > 0) {
834 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
835 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
836 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
837 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
838 ssid->frequency, params->passphrase,
839 MAC2STR(wpa_s->global->p2p_dev_addr),
840 params->persistent_group ? " [PERSISTENT]" :
841 "");
842 } else {
843 char psk[65];
844 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
845 sizeof(params->psk));
846 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
847 "%s GO ssid=\"%s\" freq=%d psk=%s "
848 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
849 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
850 ssid->frequency, psk,
851 MAC2STR(wpa_s->global->p2p_dev_addr),
852 params->persistent_group ? " [PERSISTENT]" :
853 "");
854 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700856 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700857 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700859 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700860 if (network_id < 0)
861 network_id = ssid->id;
862 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863 wpas_p2p_cross_connect_setup(wpa_s);
864 wpas_p2p_set_group_idle_timeout(wpa_s);
865 return;
866 }
867
868 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
869 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
870 params->peer_interface_addr)) {
871 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
872 "filtering");
873 return;
874 }
875 if (params->wps_method == WPS_PBC)
876 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800877 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 else if (wpa_s->p2p_pin[0])
879 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800880 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 os_free(wpa_s->go_params);
882 wpa_s->go_params = NULL;
883}
884
885
886static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
887 struct p2p_go_neg_results *params,
888 int group_formation)
889{
890 struct wpa_ssid *ssid;
891
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700892 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
893 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
894 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
895 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898
899 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700900 if (ssid == NULL) {
901 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700903 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800905 wpa_s->show_group_started = 0;
906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 wpa_config_set_network_defaults(ssid);
908 ssid->temporary = 1;
909 ssid->p2p_group = 1;
910 ssid->p2p_persistent_group = params->persistent_group;
911 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
912 WPAS_MODE_P2P_GO;
913 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700914 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 ssid->ssid = os_zalloc(params->ssid_len + 1);
916 if (ssid->ssid) {
917 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
918 ssid->ssid_len = params->ssid_len;
919 }
920 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
921 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
922 ssid->proto = WPA_PROTO_RSN;
923 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800924 if (os_strlen(params->passphrase) > 0) {
925 ssid->passphrase = os_strdup(params->passphrase);
926 if (ssid->passphrase == NULL) {
927 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
928 "passphrase for GO");
929 wpa_config_remove_network(wpa_s->conf, ssid->id);
930 return;
931 }
932 } else
933 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800934 ssid->psk_set = params->psk_set;
935 if (ssid->psk_set)
936 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800937 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800938 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700939 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940
941 wpa_s->ap_configured_cb = p2p_go_configured;
942 wpa_s->ap_configured_cb_ctx = wpa_s;
943 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700944 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 wpa_s->reassociate = 1;
946 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700947 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
948 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949 wpa_supplicant_req_scan(wpa_s, 0, 0);
950}
951
952
953static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
954 const struct wpa_supplicant *src)
955{
956 struct wpa_config *d;
957 const struct wpa_config *s;
958
959 d = dst->conf;
960 s = src->conf;
961
962#define C(n) if (s->n) d->n = os_strdup(s->n)
963 C(device_name);
964 C(manufacturer);
965 C(model_name);
966 C(model_number);
967 C(serial_number);
968 C(config_methods);
969#undef C
970
971 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
972 os_memcpy(d->sec_device_type, s->sec_device_type,
973 sizeof(d->sec_device_type));
974 d->num_sec_device_types = s->num_sec_device_types;
975
976 d->p2p_group_idle = s->p2p_group_idle;
977 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800978 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700979 d->max_num_sta = s->max_num_sta;
980 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981}
982
983
984static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
985 enum wpa_driver_if_type type)
986{
987 char ifname[120], force_ifname[120];
988
989 if (wpa_s->pending_interface_name[0]) {
990 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
991 "- skip creation of a new one");
992 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
993 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
994 "unknown?! ifname='%s'",
995 wpa_s->pending_interface_name);
996 return -1;
997 }
998 return 0;
999 }
1000
1001 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
1002 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001003 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1005 /* Try to avoid going over the IFNAMSIZ length limit */
1006 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1007 wpa_s->p2p_group_idx);
1008 }
1009 force_ifname[0] = '\0';
1010
1011 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1012 ifname);
1013 wpa_s->p2p_group_idx++;
1014
1015 wpa_s->pending_interface_type = type;
1016 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1017 wpa_s->pending_interface_addr, NULL) < 0) {
1018 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1019 "interface");
1020 return -1;
1021 }
1022
1023 if (force_ifname[0]) {
1024 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1025 force_ifname);
1026 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1027 sizeof(wpa_s->pending_interface_name));
1028 } else
1029 os_strlcpy(wpa_s->pending_interface_name, ifname,
1030 sizeof(wpa_s->pending_interface_name));
1031 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1032 MACSTR, wpa_s->pending_interface_name,
1033 MAC2STR(wpa_s->pending_interface_addr));
1034
1035 return 0;
1036}
1037
1038
1039static void wpas_p2p_remove_pending_group_interface(
1040 struct wpa_supplicant *wpa_s)
1041{
1042 if (!wpa_s->pending_interface_name[0] ||
1043 is_zero_ether_addr(wpa_s->pending_interface_addr))
1044 return; /* No pending virtual interface */
1045
1046 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1047 wpa_s->pending_interface_name);
1048 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1049 wpa_s->pending_interface_name);
1050 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1051 wpa_s->pending_interface_name[0] = '\0';
1052}
1053
1054
1055static struct wpa_supplicant *
1056wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1057{
1058 struct wpa_interface iface;
1059 struct wpa_supplicant *group_wpa_s;
1060
1061 if (!wpa_s->pending_interface_name[0]) {
1062 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1063 if (!wpas_p2p_create_iface(wpa_s))
1064 return NULL;
1065 /*
1066 * Something has forced us to remove the pending interface; try
1067 * to create a new one and hope for the best that we will get
1068 * the same local address.
1069 */
1070 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1071 WPA_IF_P2P_CLIENT) < 0)
1072 return NULL;
1073 }
1074
1075 os_memset(&iface, 0, sizeof(iface));
1076 iface.ifname = wpa_s->pending_interface_name;
1077 iface.driver = wpa_s->driver->name;
1078 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1079 iface.driver_param = wpa_s->conf->driver_param;
1080 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1081 if (group_wpa_s == NULL) {
1082 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1083 "wpa_supplicant interface");
1084 return NULL;
1085 }
1086 wpa_s->pending_interface_name[0] = '\0';
1087 group_wpa_s->parent = wpa_s;
1088 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1089 P2P_GROUP_INTERFACE_CLIENT;
1090 wpa_s->global->p2p_group_formation = group_wpa_s;
1091
1092 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1093
1094 return group_wpa_s;
1095}
1096
1097
1098static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1099 void *timeout_ctx)
1100{
1101 struct wpa_supplicant *wpa_s = eloop_ctx;
1102 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1103 if (wpa_s->global->p2p)
1104 p2p_group_formation_failed(wpa_s->global->p2p);
1105 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1106 wpa_drv_p2p_group_formation_failed(wpa_s);
1107 wpas_group_formation_completed(wpa_s, 0);
1108}
1109
1110
1111void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1112{
1113 struct wpa_supplicant *wpa_s = ctx;
1114
1115 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1116 wpa_drv_cancel_remain_on_channel(wpa_s);
1117 wpa_s->off_channel_freq = 0;
1118 wpa_s->roc_waiting_drv_freq = 0;
1119 }
1120
1121 if (res->status) {
1122 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1123 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001124 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 wpas_p2p_remove_pending_group_interface(wpa_s);
1126 return;
1127 }
1128
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001129 if (wpa_s->p2p_go_ht40)
1130 res->ht40 = 1;
1131
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001133 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134
Dmitry Shmidt04949592012-07-19 12:16:46 -07001135 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1136 struct wpa_ssid *ssid;
1137 ssid = wpa_config_get_network(wpa_s->conf,
1138 wpa_s->p2p_persistent_id);
1139 if (ssid && ssid->disabled == 2 &&
1140 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1141 size_t len = os_strlen(ssid->passphrase);
1142 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1143 "on requested persistent group");
1144 os_memcpy(res->passphrase, ssid->passphrase, len);
1145 res->passphrase[len] = '\0';
1146 }
1147 }
1148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 if (wpa_s->create_p2p_iface) {
1150 struct wpa_supplicant *group_wpa_s =
1151 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1152 if (group_wpa_s == NULL) {
1153 wpas_p2p_remove_pending_group_interface(wpa_s);
1154 return;
1155 }
1156 if (group_wpa_s != wpa_s) {
1157 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1158 sizeof(group_wpa_s->p2p_pin));
1159 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1160 }
1161 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1162 wpa_s->pending_interface_name[0] = '\0';
1163 group_wpa_s->p2p_in_provisioning = 1;
1164
1165 if (res->role_go)
1166 wpas_start_wps_go(group_wpa_s, res, 1);
1167 else
1168 wpas_start_wps_enrollee(group_wpa_s, res);
1169 } else {
1170 wpa_s->p2p_in_provisioning = 1;
1171 wpa_s->global->p2p_group_formation = wpa_s;
1172
1173 if (res->role_go)
1174 wpas_start_wps_go(wpa_s, res, 1);
1175 else
1176 wpas_start_wps_enrollee(ctx, res);
1177 }
1178
1179 wpa_s->p2p_long_listen = 0;
1180 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1181
1182 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1183 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1184 (res->peer_config_timeout % 100) * 10000,
1185 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1186}
1187
1188
1189void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1190{
1191 struct wpa_supplicant *wpa_s = ctx;
1192 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1193 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1194
1195 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1196}
1197
1198
1199void wpas_dev_found(void *ctx, const u8 *addr,
1200 const struct p2p_peer_info *info,
1201 int new_device)
1202{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001203#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 struct wpa_supplicant *wpa_s = ctx;
1205 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001206#define WFD_DEV_INFO_SIZE 9
1207 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001208 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001209#ifdef CONFIG_WIFI_DISPLAY
1210 if (info->wfd_subelems) {
1211 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1212 wpabuf_head(info->wfd_subelems),
1213 WFD_DEV_INFO_SIZE);
1214 }
1215#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1217 " p2p_dev_addr=" MACSTR
1218 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001219 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1221 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1222 sizeof(devtype)),
1223 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001224 info->dev_capab, info->group_capab,
1225 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001226#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227
1228 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1229}
1230
1231
1232static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1233{
1234 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001235
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001236 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1237 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1240}
1241
1242
1243static int wpas_start_listen(void *ctx, unsigned int freq,
1244 unsigned int duration,
1245 const struct wpabuf *probe_resp_ie)
1246{
1247 struct wpa_supplicant *wpa_s = ctx;
1248
1249 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1250
1251 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1252 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1253 "report received Probe Request frames");
1254 return -1;
1255 }
1256
1257 wpa_s->pending_listen_freq = freq;
1258 wpa_s->pending_listen_duration = duration;
1259
1260 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1261 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1262 "to remain on channel (%u MHz) for Listen "
1263 "state", freq);
1264 wpa_s->pending_listen_freq = 0;
1265 return -1;
1266 }
1267 wpa_s->off_channel_freq = 0;
1268 wpa_s->roc_waiting_drv_freq = freq;
1269
1270 return 0;
1271}
1272
1273
1274static void wpas_stop_listen(void *ctx)
1275{
1276 struct wpa_supplicant *wpa_s = ctx;
1277 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1278 wpa_drv_cancel_remain_on_channel(wpa_s);
1279 wpa_s->off_channel_freq = 0;
1280 wpa_s->roc_waiting_drv_freq = 0;
1281 }
1282 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1283 wpa_drv_probe_req_report(wpa_s, 0);
1284}
1285
1286
1287static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1288{
1289 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001290 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291}
1292
1293
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001294/*
1295 * DNS Header section is used only to calculate compression pointers, so the
1296 * contents of this data does not matter, but the length needs to be reserved
1297 * in the virtual packet.
1298 */
1299#define DNS_HEADER_LEN 12
1300
1301/*
1302 * 27-octet in-memory packet from P2P specification containing two implied
1303 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1304 */
1305#define P2P_SD_IN_MEMORY_LEN 27
1306
1307static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1308 u8 **spos, const u8 *end)
1309{
1310 while (*spos < end) {
1311 u8 val = ((*spos)[0] & 0xc0) >> 6;
1312 int len;
1313
1314 if (val == 1 || val == 2) {
1315 /* These are reserved values in RFC 1035 */
1316 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1317 "sequence starting with 0x%x", val);
1318 return -1;
1319 }
1320
1321 if (val == 3) {
1322 u16 offset;
1323 u8 *spos_tmp;
1324
1325 /* Offset */
1326 if (*spos + 2 > end) {
1327 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1328 "DNS offset field");
1329 return -1;
1330 }
1331
1332 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1333 if (offset >= *spos - start) {
1334 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1335 "pointer offset %u", offset);
1336 return -1;
1337 }
1338
1339 (*spos) += 2;
1340 spos_tmp = start + offset;
1341 return p2p_sd_dns_uncompress_label(upos, uend, start,
1342 &spos_tmp,
1343 *spos - 2);
1344 }
1345
1346 /* Label */
1347 len = (*spos)[0] & 0x3f;
1348 if (len == 0)
1349 return 0;
1350
1351 (*spos)++;
1352 if (*spos + len > end) {
1353 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1354 "sequence - no room for label with length "
1355 "%u", len);
1356 return -1;
1357 }
1358
1359 if (*upos + len + 2 > uend)
1360 return -2;
1361
1362 os_memcpy(*upos, *spos, len);
1363 *spos += len;
1364 *upos += len;
1365 (*upos)[0] = '.';
1366 (*upos)++;
1367 (*upos)[0] = '\0';
1368 }
1369
1370 return 0;
1371}
1372
1373
1374/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1375 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1376 * not large enough */
1377static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1378 size_t msg_len, size_t offset)
1379{
1380 /* 27-octet in-memory packet from P2P specification */
1381 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1382 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1383 u8 *tmp, *end, *spos;
1384 char *upos, *uend;
1385 int ret = 0;
1386
1387 if (buf_len < 2)
1388 return -1;
1389 if (offset > msg_len)
1390 return -1;
1391
1392 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1393 if (tmp == NULL)
1394 return -1;
1395 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1396 end = spos + msg_len;
1397 spos += offset;
1398
1399 os_memset(tmp, 0, DNS_HEADER_LEN);
1400 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1401 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1402
1403 upos = buf;
1404 uend = buf + buf_len;
1405
1406 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1407 if (ret) {
1408 os_free(tmp);
1409 return ret;
1410 }
1411
1412 if (upos == buf) {
1413 upos[0] = '.';
1414 upos[1] = '\0';
1415 } else if (upos[-1] == '.')
1416 upos[-1] = '\0';
1417
1418 os_free(tmp);
1419 return 0;
1420}
1421
1422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423static struct p2p_srv_bonjour *
1424wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1425 const struct wpabuf *query)
1426{
1427 struct p2p_srv_bonjour *bsrv;
1428 size_t len;
1429
1430 len = wpabuf_len(query);
1431 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1432 struct p2p_srv_bonjour, list) {
1433 if (len == wpabuf_len(bsrv->query) &&
1434 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1435 len) == 0)
1436 return bsrv;
1437 }
1438 return NULL;
1439}
1440
1441
1442static struct p2p_srv_upnp *
1443wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1444 const char *service)
1445{
1446 struct p2p_srv_upnp *usrv;
1447
1448 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1449 struct p2p_srv_upnp, list) {
1450 if (version == usrv->version &&
1451 os_strcmp(service, usrv->service) == 0)
1452 return usrv;
1453 }
1454 return NULL;
1455}
1456
1457
1458static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1459 u8 srv_trans_id)
1460{
1461 u8 *len_pos;
1462
1463 if (wpabuf_tailroom(resp) < 5)
1464 return;
1465
1466 /* Length (to be filled) */
1467 len_pos = wpabuf_put(resp, 2);
1468 wpabuf_put_u8(resp, srv_proto);
1469 wpabuf_put_u8(resp, srv_trans_id);
1470 /* Status Code */
1471 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1472 /* Response Data: empty */
1473 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1474}
1475
1476
1477static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1478 struct wpabuf *resp, u8 srv_trans_id)
1479{
1480 struct p2p_srv_bonjour *bsrv;
1481 u8 *len_pos;
1482
1483 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1484
1485 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1486 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1487 return;
1488 }
1489
1490 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1491 struct p2p_srv_bonjour, list) {
1492 if (wpabuf_tailroom(resp) <
1493 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1494 return;
1495 /* Length (to be filled) */
1496 len_pos = wpabuf_put(resp, 2);
1497 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1498 wpabuf_put_u8(resp, srv_trans_id);
1499 /* Status Code */
1500 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1501 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1502 wpabuf_head(bsrv->resp),
1503 wpabuf_len(bsrv->resp));
1504 /* Response Data */
1505 wpabuf_put_buf(resp, bsrv->query); /* Key */
1506 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1507 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1508 2);
1509 }
1510}
1511
1512
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001513static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1514 size_t query_len)
1515{
1516 char str_rx[256], str_srv[256];
1517
1518 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1519 return 0; /* Too short to include DNS Type and Version */
1520 if (os_memcmp(query + query_len - 3,
1521 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1522 3) != 0)
1523 return 0; /* Mismatch in DNS Type or Version */
1524 if (query_len == wpabuf_len(bsrv->query) &&
1525 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1526 return 1; /* Binary match */
1527
1528 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1529 0))
1530 return 0; /* Failed to uncompress query */
1531 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1532 wpabuf_head(bsrv->query),
1533 wpabuf_len(bsrv->query) - 3, 0))
1534 return 0; /* Failed to uncompress service */
1535
1536 return os_strcmp(str_rx, str_srv) == 0;
1537}
1538
1539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001540static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1541 struct wpabuf *resp, u8 srv_trans_id,
1542 const u8 *query, size_t query_len)
1543{
1544 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001545 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001546 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547
1548 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1549 query, query_len);
1550 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1551 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1552 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1553 srv_trans_id);
1554 return;
1555 }
1556
1557 if (query_len == 0) {
1558 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1559 return;
1560 }
1561
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001562 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1563 struct p2p_srv_bonjour, list) {
1564 if (!match_bonjour_query(bsrv, query, query_len))
1565 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001567 if (wpabuf_tailroom(resp) <
1568 5 + query_len + wpabuf_len(bsrv->resp))
1569 return;
1570
1571 matches++;
1572
1573 /* Length (to be filled) */
1574 len_pos = wpabuf_put(resp, 2);
1575 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1576 wpabuf_put_u8(resp, srv_trans_id);
1577
1578 /* Status Code */
1579 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1580 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1581 wpabuf_head(bsrv->resp),
1582 wpabuf_len(bsrv->resp));
1583
1584 /* Response Data */
1585 wpabuf_put_data(resp, query, query_len); /* Key */
1586 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1587
1588 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1589 }
1590
1591 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1593 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001594 if (wpabuf_tailroom(resp) < 5)
1595 return;
1596
1597 /* Length (to be filled) */
1598 len_pos = wpabuf_put(resp, 2);
1599 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1600 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601
1602 /* Status Code */
1603 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1604 /* Response Data: empty */
1605 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1606 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608}
1609
1610
1611static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1612 struct wpabuf *resp, u8 srv_trans_id)
1613{
1614 struct p2p_srv_upnp *usrv;
1615 u8 *len_pos;
1616
1617 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1618
1619 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1620 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1621 return;
1622 }
1623
1624 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1625 struct p2p_srv_upnp, list) {
1626 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1627 return;
1628
1629 /* Length (to be filled) */
1630 len_pos = wpabuf_put(resp, 2);
1631 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1632 wpabuf_put_u8(resp, srv_trans_id);
1633
1634 /* Status Code */
1635 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1636 /* Response Data */
1637 wpabuf_put_u8(resp, usrv->version);
1638 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1639 usrv->service);
1640 wpabuf_put_str(resp, usrv->service);
1641 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1642 2);
1643 }
1644}
1645
1646
1647static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1648 struct wpabuf *resp, u8 srv_trans_id,
1649 const u8 *query, size_t query_len)
1650{
1651 struct p2p_srv_upnp *usrv;
1652 u8 *len_pos;
1653 u8 version;
1654 char *str;
1655 int count = 0;
1656
1657 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1658 query, query_len);
1659
1660 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1661 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1662 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1663 srv_trans_id);
1664 return;
1665 }
1666
1667 if (query_len == 0) {
1668 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1669 return;
1670 }
1671
1672 if (wpabuf_tailroom(resp) < 5)
1673 return;
1674
1675 /* Length (to be filled) */
1676 len_pos = wpabuf_put(resp, 2);
1677 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1678 wpabuf_put_u8(resp, srv_trans_id);
1679
1680 version = query[0];
1681 str = os_malloc(query_len);
1682 if (str == NULL)
1683 return;
1684 os_memcpy(str, query + 1, query_len - 1);
1685 str[query_len - 1] = '\0';
1686
1687 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1688 struct p2p_srv_upnp, list) {
1689 if (version != usrv->version)
1690 continue;
1691
1692 if (os_strcmp(str, "ssdp:all") != 0 &&
1693 os_strstr(usrv->service, str) == NULL)
1694 continue;
1695
1696 if (wpabuf_tailroom(resp) < 2)
1697 break;
1698 if (count == 0) {
1699 /* Status Code */
1700 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1701 /* Response Data */
1702 wpabuf_put_u8(resp, version);
1703 } else
1704 wpabuf_put_u8(resp, ',');
1705
1706 count++;
1707
1708 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1709 usrv->service);
1710 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1711 break;
1712 wpabuf_put_str(resp, usrv->service);
1713 }
1714 os_free(str);
1715
1716 if (count == 0) {
1717 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1718 "available");
1719 /* Status Code */
1720 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1721 /* Response Data: empty */
1722 }
1723
1724 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1725}
1726
1727
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001728#ifdef CONFIG_WIFI_DISPLAY
1729static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1730 struct wpabuf *resp, u8 srv_trans_id,
1731 const u8 *query, size_t query_len)
1732{
1733 const u8 *pos;
1734 u8 role;
1735 u8 *len_pos;
1736
1737 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1738
1739 if (!wpa_s->global->wifi_display) {
1740 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1741 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1742 srv_trans_id);
1743 return;
1744 }
1745
1746 if (query_len < 1) {
1747 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1748 "Role");
1749 return;
1750 }
1751
1752 if (wpabuf_tailroom(resp) < 5)
1753 return;
1754
1755 pos = query;
1756 role = *pos++;
1757 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1758
1759 /* TODO: role specific handling */
1760
1761 /* Length (to be filled) */
1762 len_pos = wpabuf_put(resp, 2);
1763 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1764 wpabuf_put_u8(resp, srv_trans_id);
1765 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1766
1767 while (pos < query + query_len) {
1768 if (*pos < MAX_WFD_SUBELEMS &&
1769 wpa_s->global->wfd_subelem[*pos] &&
1770 wpabuf_tailroom(resp) >=
1771 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1772 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1773 "subelement %u", *pos);
1774 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1775 }
1776 pos++;
1777 }
1778
1779 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1780}
1781#endif /* CONFIG_WIFI_DISPLAY */
1782
1783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1785 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1786{
1787 struct wpa_supplicant *wpa_s = ctx;
1788 const u8 *pos = tlvs;
1789 const u8 *end = tlvs + tlvs_len;
1790 const u8 *tlv_end;
1791 u16 slen;
1792 struct wpabuf *resp;
1793 u8 srv_proto, srv_trans_id;
1794 size_t buf_len;
1795 char *buf;
1796
1797 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1798 tlvs, tlvs_len);
1799 buf_len = 2 * tlvs_len + 1;
1800 buf = os_malloc(buf_len);
1801 if (buf) {
1802 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1803 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1804 MACSTR " %u %u %s",
1805 freq, MAC2STR(sa), dialog_token, update_indic,
1806 buf);
1807 os_free(buf);
1808 }
1809
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001810 if (wpa_s->p2p_sd_over_ctrl_iface) {
1811 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1812 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001814 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815
1816 resp = wpabuf_alloc(10000);
1817 if (resp == NULL)
1818 return;
1819
1820 while (pos + 1 < end) {
1821 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1822 slen = WPA_GET_LE16(pos);
1823 pos += 2;
1824 if (pos + slen > end || slen < 2) {
1825 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1826 "length");
1827 wpabuf_free(resp);
1828 return;
1829 }
1830 tlv_end = pos + slen;
1831
1832 srv_proto = *pos++;
1833 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1834 srv_proto);
1835 srv_trans_id = *pos++;
1836 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1837 srv_trans_id);
1838
1839 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1840 pos, tlv_end - pos);
1841
1842
1843 if (wpa_s->force_long_sd) {
1844 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1845 "response");
1846 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1847 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1848 goto done;
1849 }
1850
1851 switch (srv_proto) {
1852 case P2P_SERV_ALL_SERVICES:
1853 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1854 "for all services");
1855 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1856 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1857 wpa_printf(MSG_DEBUG, "P2P: No service "
1858 "discovery protocols available");
1859 wpas_sd_add_proto_not_avail(
1860 resp, P2P_SERV_ALL_SERVICES,
1861 srv_trans_id);
1862 break;
1863 }
1864 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1865 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1866 break;
1867 case P2P_SERV_BONJOUR:
1868 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1869 pos, tlv_end - pos);
1870 break;
1871 case P2P_SERV_UPNP:
1872 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1873 pos, tlv_end - pos);
1874 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001875#ifdef CONFIG_WIFI_DISPLAY
1876 case P2P_SERV_WIFI_DISPLAY:
1877 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1878 pos, tlv_end - pos);
1879 break;
1880#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 default:
1882 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1883 "protocol %u", srv_proto);
1884 wpas_sd_add_proto_not_avail(resp, srv_proto,
1885 srv_trans_id);
1886 break;
1887 }
1888
1889 pos = tlv_end;
1890 }
1891
1892done:
1893 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1894 update_indic, tlvs, tlvs_len);
1895
1896 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1897
1898 wpabuf_free(resp);
1899}
1900
1901
1902void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1903 const u8 *tlvs, size_t tlvs_len)
1904{
1905 struct wpa_supplicant *wpa_s = ctx;
1906 const u8 *pos = tlvs;
1907 const u8 *end = tlvs + tlvs_len;
1908 const u8 *tlv_end;
1909 u16 slen;
1910 size_t buf_len;
1911 char *buf;
1912
1913 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1914 tlvs, tlvs_len);
1915 if (tlvs_len > 1500) {
1916 /* TODO: better way for handling this */
1917 wpa_msg_ctrl(wpa_s, MSG_INFO,
1918 P2P_EVENT_SERV_DISC_RESP MACSTR
1919 " %u <long response: %u bytes>",
1920 MAC2STR(sa), update_indic,
1921 (unsigned int) tlvs_len);
1922 } else {
1923 buf_len = 2 * tlvs_len + 1;
1924 buf = os_malloc(buf_len);
1925 if (buf) {
1926 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1927 wpa_msg_ctrl(wpa_s, MSG_INFO,
1928 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1929 MAC2STR(sa), update_indic, buf);
1930 os_free(buf);
1931 }
1932 }
1933
1934 while (pos < end) {
1935 u8 srv_proto, srv_trans_id, status;
1936
1937 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1938 slen = WPA_GET_LE16(pos);
1939 pos += 2;
1940 if (pos + slen > end || slen < 3) {
1941 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1942 "length");
1943 return;
1944 }
1945 tlv_end = pos + slen;
1946
1947 srv_proto = *pos++;
1948 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1949 srv_proto);
1950 srv_trans_id = *pos++;
1951 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1952 srv_trans_id);
1953 status = *pos++;
1954 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1955 status);
1956
1957 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1958 pos, tlv_end - pos);
1959
1960 pos = tlv_end;
1961 }
1962
1963 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1964}
1965
1966
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001967u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1968 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969{
1970 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001971 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001973 return 0;
1974 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975}
1976
1977
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001978u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1979 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980{
1981 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001982 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983
1984 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1985 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001986 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1988 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1989 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1990 wpabuf_put_u8(tlvs, version);
1991 wpabuf_put_str(tlvs, query);
1992 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1993 wpabuf_free(tlvs);
1994 return ret;
1995}
1996
1997
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001998#ifdef CONFIG_WIFI_DISPLAY
1999
2000static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2001 const struct wpabuf *tlvs)
2002{
2003 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2004 return 0;
2005 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2006 return 0;
2007 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2008}
2009
2010
2011#define MAX_WFD_SD_SUBELEMS 20
2012
2013static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2014 const char *subelems)
2015{
2016 u8 *len;
2017 const char *pos;
2018 int val;
2019 int count = 0;
2020
2021 len = wpabuf_put(tlvs, 2);
2022 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2023 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2024
2025 wpabuf_put_u8(tlvs, role);
2026
2027 pos = subelems;
2028 while (*pos) {
2029 val = atoi(pos);
2030 if (val >= 0 && val < 256) {
2031 wpabuf_put_u8(tlvs, val);
2032 count++;
2033 if (count == MAX_WFD_SD_SUBELEMS)
2034 break;
2035 }
2036 pos = os_strchr(pos + 1, ',');
2037 if (pos == NULL)
2038 break;
2039 pos++;
2040 }
2041
2042 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2043}
2044
2045
2046u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2047 const u8 *dst, const char *role)
2048{
2049 struct wpabuf *tlvs;
2050 u64 ret;
2051 const char *subelems;
2052 u8 id = 1;
2053
2054 subelems = os_strchr(role, ' ');
2055 if (subelems == NULL)
2056 return 0;
2057 subelems++;
2058
2059 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2060 if (tlvs == NULL)
2061 return 0;
2062
2063 if (os_strstr(role, "[source]"))
2064 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2065 if (os_strstr(role, "[pri-sink]"))
2066 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2067 if (os_strstr(role, "[sec-sink]"))
2068 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2069 if (os_strstr(role, "[source+sink]"))
2070 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2071
2072 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2073 wpabuf_free(tlvs);
2074 return ret;
2075}
2076
2077#endif /* CONFIG_WIFI_DISPLAY */
2078
2079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002080int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081{
2082 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002083 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2085 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002086 return p2p_sd_cancel_request(wpa_s->global->p2p,
2087 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088}
2089
2090
2091void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2092 const u8 *dst, u8 dialog_token,
2093 const struct wpabuf *resp_tlvs)
2094{
2095 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2096 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2097 resp_tlvs);
2098 return;
2099 }
2100 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2101 return;
2102 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2103 resp_tlvs);
2104}
2105
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002106#ifdef ANDROID_P2P
2107void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2108#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002110#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111{
2112 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2113 wpa_drv_p2p_service_update(wpa_s);
2114 return;
2115 }
2116 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002117#ifdef ANDROID_P2P
2118 p2p_sd_service_update(wpa_s->global->p2p, action);
2119#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002121#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122}
2123
2124
2125static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2126{
2127 dl_list_del(&bsrv->list);
2128 wpabuf_free(bsrv->query);
2129 wpabuf_free(bsrv->resp);
2130 os_free(bsrv);
2131}
2132
2133
2134static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2135{
2136 dl_list_del(&usrv->list);
2137 os_free(usrv->service);
2138 os_free(usrv);
2139}
2140
2141
2142void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2143{
2144 struct p2p_srv_bonjour *bsrv, *bn;
2145 struct p2p_srv_upnp *usrv, *un;
2146
2147 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2148 struct p2p_srv_bonjour, list)
2149 wpas_p2p_srv_bonjour_free(bsrv);
2150
2151 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2152 struct p2p_srv_upnp, list)
2153 wpas_p2p_srv_upnp_free(usrv);
2154
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002155#ifdef ANDROID_P2P
2156 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2157#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002159#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160}
2161
2162
2163int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2164 struct wpabuf *query, struct wpabuf *resp)
2165{
2166 struct p2p_srv_bonjour *bsrv;
2167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168 bsrv = os_zalloc(sizeof(*bsrv));
2169 if (bsrv == NULL)
2170 return -1;
2171 bsrv->query = query;
2172 bsrv->resp = resp;
2173 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2174
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002175#ifdef ANDROID_P2P
2176 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2177#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002179#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180 return 0;
2181}
2182
2183
2184int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2185 const struct wpabuf *query)
2186{
2187 struct p2p_srv_bonjour *bsrv;
2188
2189 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2190 if (bsrv == NULL)
2191 return -1;
2192 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002193#ifdef ANDROID_P2P
2194 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2195#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002197#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002198 return 0;
2199}
2200
2201
2202int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2203 const char *service)
2204{
2205 struct p2p_srv_upnp *usrv;
2206
2207 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2208 return 0; /* Already listed */
2209 usrv = os_zalloc(sizeof(*usrv));
2210 if (usrv == NULL)
2211 return -1;
2212 usrv->version = version;
2213 usrv->service = os_strdup(service);
2214 if (usrv->service == NULL) {
2215 os_free(usrv);
2216 return -1;
2217 }
2218 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2219
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002220#ifdef ANDROID_P2P
2221 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2222#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002224#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225 return 0;
2226}
2227
2228
2229int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2230 const char *service)
2231{
2232 struct p2p_srv_upnp *usrv;
2233
2234 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2235 if (usrv == NULL)
2236 return -1;
2237 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002238#ifdef ANDROID_P2P
2239 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2240#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002242#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 return 0;
2244}
2245
2246
2247static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2248 const u8 *peer, const char *params,
2249 unsigned int generated_pin)
2250{
2251 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2252 MAC2STR(peer), generated_pin, params);
2253}
2254
2255
2256static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2257 const u8 *peer, const char *params)
2258{
2259 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2260 MAC2STR(peer), params);
2261}
2262
2263
2264void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2265 const u8 *dev_addr, const u8 *pri_dev_type,
2266 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002267 u8 dev_capab, u8 group_capab, const u8 *group_id,
2268 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269{
2270 struct wpa_supplicant *wpa_s = ctx;
2271 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002272 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002273 u8 empty_dev_type[8];
2274 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002275 struct wpa_supplicant *group = NULL;
2276
2277 if (group_id) {
2278 for (group = wpa_s->global->ifaces; group; group = group->next)
2279 {
2280 struct wpa_ssid *s = group->current_ssid;
2281 if (s != NULL &&
2282 s->mode == WPAS_MODE_P2P_GO &&
2283 group_id_len - ETH_ALEN == s->ssid_len &&
2284 os_memcmp(group_id + ETH_ALEN, s->ssid,
2285 s->ssid_len) == 0)
2286 break;
2287 }
2288 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289
2290 if (pri_dev_type == NULL) {
2291 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2292 pri_dev_type = empty_dev_type;
2293 }
2294 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2295 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002296 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297 MAC2STR(dev_addr),
2298 wps_dev_type_bin2str(pri_dev_type, devtype,
2299 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002300 dev_name, supp_config_methods, dev_capab, group_capab,
2301 group ? " group=" : "",
2302 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002305 if (config_methods & WPS_CONFIG_DISPLAY) {
2306 generated_pin = wps_generate_pin();
2307 wpas_prov_disc_local_display(wpa_s, peer, params,
2308 generated_pin);
2309 } else if (config_methods & WPS_CONFIG_KEYPAD)
2310 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2311 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2312 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2313 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002314
2315 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2316 P2P_PROV_DISC_SUCCESS,
2317 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318}
2319
2320
2321void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2322{
2323 struct wpa_supplicant *wpa_s = ctx;
2324 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002325 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002326
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002327 if (wpa_s->pending_pd_before_join &&
2328 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2329 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2330 wpa_s->pending_pd_before_join = 0;
2331 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2332 "join-existing-group operation");
2333 wpas_p2p_join_start(wpa_s);
2334 return;
2335 }
2336
Dmitry Shmidt04949592012-07-19 12:16:46 -07002337 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2338 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2339 os_snprintf(params, sizeof(params), " peer_go=%d",
2340 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2341 else
2342 params[0] = '\0';
2343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002345 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 else if (config_methods & WPS_CONFIG_KEYPAD) {
2347 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002348 wpas_prov_disc_local_display(wpa_s, peer, params,
2349 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002351 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2352 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353
Jouni Malinen75ecf522011-06-27 15:19:46 -07002354 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2355 P2P_PROV_DISC_SUCCESS,
2356 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357}
2358
2359
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002360static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2361 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002362{
2363 struct wpa_supplicant *wpa_s = ctx;
2364
Dmitry Shmidt04949592012-07-19 12:16:46 -07002365 if (wpa_s->p2p_fallback_to_go_neg) {
2366 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2367 "failed - fall back to GO Negotiation");
2368 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2369 return;
2370 }
2371
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002372 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002373 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002374 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2375 "join-existing-group operation (no ACK for PD "
2376 "Req attempts)");
2377 wpas_p2p_join_start(wpa_s);
2378 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002379 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002380
Dmitry Shmidt04949592012-07-19 12:16:46 -07002381 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2382 " p2p_dev_addr=" MACSTR " status=%d",
2383 MAC2STR(peer), status);
2384
Jouni Malinen75ecf522011-06-27 15:19:46 -07002385 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2386 status, 0, 0);
2387}
2388
2389
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002390static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2391 const u8 *go_dev_addr, const u8 *ssid,
2392 size_t ssid_len, int *go, u8 *group_bssid,
2393 int *force_freq, int persistent_group)
2394{
2395 struct wpa_supplicant *wpa_s = ctx;
2396 struct wpa_ssid *s;
2397 u8 cur_bssid[ETH_ALEN];
2398 int res;
2399 struct wpa_supplicant *grp;
2400
2401 if (!persistent_group) {
2402 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2403 " to join an active group", MAC2STR(sa));
2404 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2405 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2406 == 0 ||
2407 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2408 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2409 "authorized invitation");
2410 goto accept_inv;
2411 }
2412 /*
2413 * Do not accept the invitation automatically; notify user and
2414 * request approval.
2415 */
2416 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2417 }
2418
2419 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2420 if (grp) {
2421 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2422 "running persistent group");
2423 if (*go)
2424 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2425 goto accept_inv;
2426 }
2427
2428 if (!wpa_s->conf->persistent_reconnect)
2429 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2430
2431 for (s = wpa_s->conf->ssid; s; s = s->next) {
2432 if (s->disabled == 2 &&
2433 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2434 s->ssid_len == ssid_len &&
2435 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2436 break;
2437 }
2438
2439 if (!s) {
2440 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2441 " requested reinvocation of an unknown group",
2442 MAC2STR(sa));
2443 return P2P_SC_FAIL_UNKNOWN_GROUP;
2444 }
2445
2446 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2447 *go = 1;
2448 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2449 wpa_printf(MSG_DEBUG, "P2P: The only available "
2450 "interface is already in use - reject "
2451 "invitation");
2452 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2453 }
2454 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2455 } else if (s->mode == WPAS_MODE_P2P_GO) {
2456 *go = 1;
2457 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2458 {
2459 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2460 "interface address for the group");
2461 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2462 }
2463 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2464 ETH_ALEN);
2465 }
2466
2467accept_inv:
2468 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2469 wpa_s->assoc_freq) {
2470 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2471 "the channel we are already using");
2472 *force_freq = wpa_s->assoc_freq;
2473 }
2474
2475 res = wpa_drv_shared_freq(wpa_s);
2476 if (res > 0) {
2477 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2478 "with the channel we are already using on a "
2479 "shared interface");
2480 *force_freq = res;
2481 }
2482
2483 return P2P_SC_SUCCESS;
2484}
2485
2486
2487static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2488 const u8 *ssid, size_t ssid_len,
2489 const u8 *go_dev_addr, u8 status,
2490 int op_freq)
2491{
2492 struct wpa_supplicant *wpa_s = ctx;
2493 struct wpa_ssid *s;
2494
2495 for (s = wpa_s->conf->ssid; s; s = s->next) {
2496 if (s->disabled == 2 &&
2497 s->ssid_len == ssid_len &&
2498 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2499 break;
2500 }
2501
2502 if (status == P2P_SC_SUCCESS) {
2503 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2504 " was accepted; op_freq=%d MHz",
2505 MAC2STR(sa), op_freq);
2506 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002507 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508 wpas_p2p_group_add_persistent(
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002509 wpa_s, s, go, go ? op_freq : 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002511 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002513 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 }
2515 return;
2516 }
2517
2518 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2519 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2520 " was rejected (status %u)", MAC2STR(sa), status);
2521 return;
2522 }
2523
2524 if (!s) {
2525 if (bssid) {
2526 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2527 "sa=" MACSTR " go_dev_addr=" MACSTR
2528 " bssid=" MACSTR " unknown-network",
2529 MAC2STR(sa), MAC2STR(go_dev_addr),
2530 MAC2STR(bssid));
2531 } else {
2532 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2533 "sa=" MACSTR " go_dev_addr=" MACSTR
2534 " unknown-network",
2535 MAC2STR(sa), MAC2STR(go_dev_addr));
2536 }
2537 return;
2538 }
2539
2540 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2541 " persistent=%d", MAC2STR(sa), s->id);
2542}
2543
2544
2545static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2546{
2547 struct wpa_supplicant *wpa_s = ctx;
2548 struct wpa_ssid *ssid;
2549
2550 if (bssid) {
2551 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2552 "status=%d " MACSTR,
2553 status, MAC2STR(bssid));
2554 } else {
2555 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2556 "status=%d ", status);
2557 }
2558 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2559
2560 if (wpa_s->pending_invite_ssid_id == -1)
2561 return; /* Invitation to active group */
2562
2563 if (status != P2P_SC_SUCCESS) {
2564 wpas_p2p_remove_pending_group_interface(wpa_s);
2565 return;
2566 }
2567
2568 ssid = wpa_config_get_network(wpa_s->conf,
2569 wpa_s->pending_invite_ssid_id);
2570 if (ssid == NULL) {
2571 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2572 "data matching with invitation");
2573 return;
2574 }
2575
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002576 /*
2577 * The peer could have missed our ctrl::ack frame for Invitation
2578 * Response and continue retransmitting the frame. To reduce the
2579 * likelihood of the peer not getting successful TX status for the
2580 * Invitation Response frame, wait a short time here before starting
2581 * the persistent group so that we will remain on the current channel to
2582 * acknowledge any possible retransmission from the peer.
2583 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002584#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002585 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2586 "starting persistent group");
2587 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002588#else
2589 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2590 "starting persistent group");
2591 os_sleep(0, 100000);
2592#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002594 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002595 ssid->mode == WPAS_MODE_P2P_GO,
2596 wpa_s->p2p_persistent_go_freq,
2597 wpa_s->p2p_go_ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598}
2599
2600
Dmitry Shmidt04949592012-07-19 12:16:46 -07002601static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2602 unsigned int freq)
2603{
2604 unsigned int i;
2605
2606 if (global->p2p_disallow_freq == NULL)
2607 return 0;
2608
2609 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2610 if (freq >= global->p2p_disallow_freq[i].min &&
2611 freq <= global->p2p_disallow_freq[i].max)
2612 return 1;
2613 }
2614
2615 return 0;
2616}
2617
2618
2619static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2620{
2621 reg->channel[reg->channels] = chan;
2622 reg->channels++;
2623}
2624
2625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2627 struct p2p_channels *chan)
2628{
2629 int i, cla = 0;
2630
2631 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2632 "band");
2633
2634 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2635 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002636 chan->reg_class[cla].channels = 0;
2637 for (i = 0; i < 11; i++) {
2638 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2639 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2640 }
2641 if (chan->reg_class[cla].channels)
2642 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643
2644 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2645 "band");
2646
2647 /* Operating class 115 - 5 GHz, channels 36-48 */
2648 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002649 chan->reg_class[cla].channels = 0;
2650 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2651 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2652 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2653 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2654 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2655 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2656 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2657 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2658 if (chan->reg_class[cla].channels)
2659 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002660
2661 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2662 "band");
2663
2664 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2665 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002666 chan->reg_class[cla].channels = 0;
2667 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2668 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2669 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2670 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2671 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2672 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2673 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2674 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2675 if (chan->reg_class[cla].channels)
2676 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002677
2678 chan->reg_classes = cla;
2679 return 0;
2680}
2681
2682
2683static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2684 u16 num_modes,
2685 enum hostapd_hw_mode mode)
2686{
2687 u16 i;
2688
2689 for (i = 0; i < num_modes; i++) {
2690 if (modes[i].mode == mode)
2691 return &modes[i];
2692 }
2693
2694 return NULL;
2695}
2696
2697
Dmitry Shmidt04949592012-07-19 12:16:46 -07002698static int has_channel(struct wpa_global *global,
2699 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002700{
2701 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002702 unsigned int freq;
2703
2704 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2705 chan * 5;
2706 if (wpas_p2p_disallowed_freq(global, freq))
2707 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002708
2709 for (i = 0; i < mode->num_channels; i++) {
2710 if (mode->channels[i].chan == chan) {
2711 if (flags)
2712 *flags = mode->channels[i].flag;
2713 return !(mode->channels[i].flag &
2714 (HOSTAPD_CHAN_DISABLED |
2715 HOSTAPD_CHAN_PASSIVE_SCAN |
2716 HOSTAPD_CHAN_NO_IBSS |
2717 HOSTAPD_CHAN_RADAR));
2718 }
2719 }
2720
2721 return 0;
2722}
2723
2724
2725struct p2p_oper_class_map {
2726 enum hostapd_hw_mode mode;
2727 u8 op_class;
2728 u8 min_chan;
2729 u8 max_chan;
2730 u8 inc;
2731 enum { BW20, BW40PLUS, BW40MINUS } bw;
2732};
2733
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002734static struct p2p_oper_class_map op_class[] = {
2735 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002736#if 0 /* Do not enable HT40 on 2 GHz for now */
2737 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2738 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2739#endif
2740 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2741 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2742 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2743 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2744 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2745 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2746 { -1, 0, 0, 0, 0, BW20 }
2747};
2748
2749
2750static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2751 struct hostapd_hw_modes *mode,
2752 u8 channel, u8 bw)
2753{
2754 int flag;
2755
2756 if (!has_channel(wpa_s->global, mode, channel, &flag))
2757 return -1;
2758 if (bw == BW40MINUS &&
2759 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2760 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2761 return 0;
2762 if (bw == BW40PLUS &&
2763 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2764 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2765 return 0;
2766 return 1;
2767}
2768
2769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2771 struct p2p_channels *chan)
2772{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002773 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002776 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2778 "of all supported channels; assume dualband "
2779 "support");
2780 return wpas_p2p_default_channels(wpa_s, chan);
2781 }
2782
2783 cla = 0;
2784
2785 for (op = 0; op_class[op].op_class; op++) {
2786 struct p2p_oper_class_map *o = &op_class[op];
2787 u8 ch;
2788 struct p2p_reg_class *reg = NULL;
2789
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002790 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791 if (mode == NULL)
2792 continue;
2793 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002794 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795 continue;
2796 if (reg == NULL) {
2797 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2798 "class %u", o->op_class);
2799 reg = &chan->reg_class[cla];
2800 cla++;
2801 reg->reg_class = o->op_class;
2802 }
2803 reg->channel[reg->channels] = ch;
2804 reg->channels++;
2805 }
2806 if (reg) {
2807 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2808 reg->channel, reg->channels);
2809 }
2810 }
2811
2812 chan->reg_classes = cla;
2813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 return 0;
2815}
2816
2817
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002818int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2819 struct hostapd_hw_modes *mode, u8 channel)
2820{
2821 int op, ret;
2822
2823 for (op = 0; op_class[op].op_class; op++) {
2824 struct p2p_oper_class_map *o = &op_class[op];
2825 u8 ch;
2826
2827 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2828 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2829 o->bw == BW20 || ch != channel)
2830 continue;
2831 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2832 if (ret < 0)
2833 continue;
2834 else if (ret > 0)
2835 return (o->bw == BW40MINUS) ? -1 : 1;
2836 else
2837 return 0;
2838 }
2839 }
2840 return 0;
2841}
2842
2843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2845 size_t buf_len)
2846{
2847 struct wpa_supplicant *wpa_s = ctx;
2848
2849 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2850 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2851 break;
2852 }
2853 if (wpa_s == NULL)
2854 return -1;
2855
2856 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2857}
2858
2859
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002860static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2861{
2862 struct wpa_supplicant *wpa_s = ctx;
2863
2864 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2865 struct wpa_ssid *ssid = wpa_s->current_ssid;
2866 if (ssid == NULL)
2867 continue;
2868 if (ssid->mode != WPAS_MODE_INFRA)
2869 continue;
2870 if (wpa_s->wpa_state != WPA_COMPLETED &&
2871 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2872 continue;
2873 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2874 return 1;
2875 }
2876
2877 return 0;
2878}
2879
2880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881/**
2882 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2883 * @global: Pointer to global data from wpa_supplicant_init()
2884 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2885 * Returns: 0 on success, -1 on failure
2886 */
2887int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2888{
2889 struct p2p_config p2p;
2890 unsigned int r;
2891 int i;
2892
2893 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2894 return 0;
2895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896 if (global->p2p)
2897 return 0;
2898
2899 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2900 struct p2p_params params;
2901
2902 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2903 os_memset(&params, 0, sizeof(params));
2904 params.dev_name = wpa_s->conf->device_name;
2905 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2906 WPS_DEV_TYPE_LEN);
2907 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2908 os_memcpy(params.sec_dev_type,
2909 wpa_s->conf->sec_device_type,
2910 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2911
2912 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2913 return -1;
2914
2915 return 0;
2916 }
2917
2918 os_memset(&p2p, 0, sizeof(p2p));
2919 p2p.msg_ctx = wpa_s;
2920 p2p.cb_ctx = wpa_s;
2921 p2p.p2p_scan = wpas_p2p_scan;
2922 p2p.send_action = wpas_send_action;
2923 p2p.send_action_done = wpas_send_action_done;
2924 p2p.go_neg_completed = wpas_go_neg_completed;
2925 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2926 p2p.dev_found = wpas_dev_found;
2927 p2p.dev_lost = wpas_dev_lost;
2928 p2p.start_listen = wpas_start_listen;
2929 p2p.stop_listen = wpas_stop_listen;
2930 p2p.send_probe_resp = wpas_send_probe_resp;
2931 p2p.sd_request = wpas_sd_request;
2932 p2p.sd_response = wpas_sd_response;
2933 p2p.prov_disc_req = wpas_prov_disc_req;
2934 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002935 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 p2p.invitation_process = wpas_invitation_process;
2937 p2p.invitation_received = wpas_invitation_received;
2938 p2p.invitation_result = wpas_invitation_result;
2939 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002940 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941
2942 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002943 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002944 p2p.dev_name = wpa_s->conf->device_name;
2945 p2p.manufacturer = wpa_s->conf->manufacturer;
2946 p2p.model_name = wpa_s->conf->model_name;
2947 p2p.model_number = wpa_s->conf->model_number;
2948 p2p.serial_number = wpa_s->conf->serial_number;
2949 if (wpa_s->wps) {
2950 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2951 p2p.config_methods = wpa_s->wps->config_methods;
2952 }
2953
2954 if (wpa_s->conf->p2p_listen_reg_class &&
2955 wpa_s->conf->p2p_listen_channel) {
2956 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2957 p2p.channel = wpa_s->conf->p2p_listen_channel;
2958 } else {
2959 p2p.reg_class = 81;
2960 /*
2961 * Pick one of the social channels randomly as the listen
2962 * channel.
2963 */
2964 os_get_random((u8 *) &r, sizeof(r));
2965 p2p.channel = 1 + (r % 3) * 5;
2966 }
2967 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2968
2969 if (wpa_s->conf->p2p_oper_reg_class &&
2970 wpa_s->conf->p2p_oper_channel) {
2971 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2972 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2973 p2p.cfg_op_channel = 1;
2974 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2975 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2976
2977 } else {
2978 p2p.op_reg_class = 81;
2979 /*
2980 * Use random operation channel from (1, 6, 11) if no other
2981 * preference is indicated.
2982 */
2983 os_get_random((u8 *) &r, sizeof(r));
2984 p2p.op_channel = 1 + (r % 3) * 5;
2985 p2p.cfg_op_channel = 0;
2986 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2987 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2988 }
2989 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2990 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2991 p2p.country[2] = 0x04;
2992 } else
2993 os_memcpy(p2p.country, "XX\x04", 3);
2994
2995 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2996 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2997 "channel list");
2998 return -1;
2999 }
3000
3001 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3002 WPS_DEV_TYPE_LEN);
3003
3004 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3005 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3006 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3007
3008 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3009 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3010
3011 p2p.max_peers = 100;
3012
3013 if (wpa_s->conf->p2p_ssid_postfix) {
3014 p2p.ssid_postfix_len =
3015 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3016 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3017 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3018 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3019 p2p.ssid_postfix_len);
3020 }
3021
3022 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3023
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003024 p2p.max_listen = wpa_s->max_remain_on_chan;
3025
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003026#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003027 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003028 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003029 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3030 } else {
3031 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003032 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003033 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3034 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003035#endif
3036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 global->p2p = p2p_init(&p2p);
3038 if (global->p2p == NULL)
3039 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003040 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003041
3042 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3043 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3044 continue;
3045 p2p_add_wps_vendor_extension(
3046 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3047 }
3048
3049 return 0;
3050}
3051
3052
3053/**
3054 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3055 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3056 *
3057 * This function deinitialize per-interface P2P data.
3058 */
3059void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3060{
3061 if (wpa_s->driver && wpa_s->drv_priv)
3062 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003063
3064 if (wpa_s->go_params) {
3065 /* Clear any stored provisioning info */
3066 p2p_clear_provisioning_info(
3067 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003068 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003069 }
3070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071 os_free(wpa_s->go_params);
3072 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3074 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3075 wpa_s->p2p_long_listen = 0;
3076 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3077 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3078 wpas_p2p_remove_pending_group_interface(wpa_s);
3079
3080 /* TODO: remove group interface from the driver if this wpa_s instance
3081 * is on top of a P2P group interface */
3082}
3083
3084
3085/**
3086 * wpas_p2p_deinit_global - Deinitialize global P2P module
3087 * @global: Pointer to global data from wpa_supplicant_init()
3088 *
3089 * This function deinitializes the global (per device) P2P module.
3090 */
3091void wpas_p2p_deinit_global(struct wpa_global *global)
3092{
3093 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003094
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003095 wpa_s = global->ifaces;
3096 if (wpa_s)
3097 wpas_p2p_service_flush(wpa_s);
3098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 if (global->p2p == NULL)
3100 return;
3101
3102 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003103 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3104 wpa_s = wpa_s->next;
3105 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 tmp = global->ifaces;
3107 while (tmp &&
3108 (tmp == wpa_s ||
3109 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3110 tmp = tmp->next;
3111 }
3112 if (tmp == NULL)
3113 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003114 /* Disconnect from the P2P group and deinit the interface */
3115 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116 }
3117
3118 /*
3119 * Deinit GO data on any possibly remaining interface (if main
3120 * interface is used as GO).
3121 */
3122 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3123 if (wpa_s->ap_iface)
3124 wpas_p2p_group_deinit(wpa_s);
3125 }
3126
3127 p2p_deinit(global->p2p);
3128 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003129 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130}
3131
3132
3133static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3134{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003135 if (wpa_s->conf->p2p_no_group_iface)
3136 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003137 if (wpa_s->drv_flags &
3138 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3139 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3140 return 1; /* P2P group requires a new interface in every case
3141 */
3142 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3143 return 0; /* driver does not support concurrent operations */
3144 if (wpa_s->global->ifaces->next)
3145 return 1; /* more that one interface already in use */
3146 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3147 return 1; /* this interface is already in use */
3148 return 0;
3149}
3150
3151
3152static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3153 const u8 *peer_addr,
3154 enum p2p_wps_method wps_method,
3155 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003156 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003157 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003158{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003159 if (persistent_group && wpa_s->conf->persistent_reconnect)
3160 persistent_group = 2;
3161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003162 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3163 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3164 go_intent, own_interface_addr,
3165 force_freq, persistent_group);
3166 }
3167
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003168 /*
3169 * Increase GO config timeout if HT40 is used since it takes some time
3170 * to scan channels for coex purposes before the BSS can be started.
3171 */
3172 p2p_set_config_timeout(wpa_s->global->p2p,
3173 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003175 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3176 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003177 persistent_group, ssid ? ssid->ssid : NULL,
3178 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003179 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003180}
3181
3182
3183static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3184 const u8 *peer_addr,
3185 enum p2p_wps_method wps_method,
3186 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003187 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003188 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003189{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003190 if (persistent_group && wpa_s->conf->persistent_reconnect)
3191 persistent_group = 2;
3192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003193 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3194 return -1;
3195
3196 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3197 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003198 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003199 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200}
3201
3202
3203static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3204{
3205 wpa_s->p2p_join_scan_count++;
3206 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3207 wpa_s->p2p_join_scan_count);
3208 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3209 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3210 " for join operationg - stop join attempt",
3211 MAC2STR(wpa_s->pending_join_iface_addr));
3212 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003213 if (wpa_s->p2p_auto_pd) {
3214 wpa_s->p2p_auto_pd = 0;
3215 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3216 " p2p_dev_addr=" MACSTR " status=N/A",
3217 MAC2STR(wpa_s->pending_join_dev_addr));
3218 return;
3219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 wpa_msg(wpa_s->parent, MSG_INFO,
3221 P2P_EVENT_GROUP_FORMATION_FAILURE);
3222 }
3223}
3224
3225
Dmitry Shmidt04949592012-07-19 12:16:46 -07003226static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3227{
3228 struct wpa_supplicant *iface;
3229 int shared_freq;
3230 u8 bssid[ETH_ALEN];
3231
3232 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3233 return 0;
3234
3235 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3236 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3237 continue;
3238 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3239 continue;
3240 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3241 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3242 shared_freq = iface->current_ssid->frequency;
3243 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3244 shared_freq = iface->assoc_freq;
3245 else
3246 shared_freq = 0;
3247
3248 if (shared_freq && freq != shared_freq) {
3249 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3250 "connected on %d MHz - new connection on "
3251 "%d MHz", iface->ifname, shared_freq, freq);
3252 return 1;
3253 }
3254 }
3255
3256 shared_freq = wpa_drv_shared_freq(wpa_s);
3257 if (shared_freq > 0 && shared_freq != freq) {
3258 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3259 "virtual interface connected on %d MHz - new "
3260 "connection on %d MHz", shared_freq, freq);
3261 return 1;
3262 }
3263
3264 return 0;
3265}
3266
3267
3268static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3269 const u8 *peer_dev_addr)
3270{
3271 struct wpa_bss *bss;
3272 int updated;
3273
3274 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3275 if (bss == NULL)
3276 return -1;
3277 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3278 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3279 "last scan");
3280 return 0;
3281 }
3282
3283 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3284 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3285 "%ld.%06ld (%supdated in last scan)",
3286 bss->last_update.sec, bss->last_update.usec,
3287 updated ? "": "not ");
3288
3289 return updated;
3290}
3291
3292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3294 struct wpa_scan_results *scan_res)
3295{
3296 struct wpa_bss *bss;
3297 int freq;
3298 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3301
3302 if (wpa_s->global->p2p_disabled)
3303 return;
3304
Dmitry Shmidt04949592012-07-19 12:16:46 -07003305 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3306 scan_res ? (int) scan_res->num : -1,
3307 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003308
3309 if (scan_res)
3310 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3311
Dmitry Shmidt04949592012-07-19 12:16:46 -07003312 if (wpa_s->p2p_auto_pd) {
3313 int join = wpas_p2p_peer_go(wpa_s,
3314 wpa_s->pending_join_dev_addr);
3315 if (join == 0 &&
3316 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3317 wpa_s->auto_pd_scan_retry++;
3318 bss = wpa_bss_get_bssid(wpa_s,
3319 wpa_s->pending_join_dev_addr);
3320 if (bss) {
3321 freq = bss->freq;
3322 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3323 "the peer " MACSTR " at %d MHz",
3324 wpa_s->auto_pd_scan_retry,
3325 MAC2STR(wpa_s->
3326 pending_join_dev_addr),
3327 freq);
3328 wpas_p2p_join_scan_req(wpa_s, freq);
3329 return;
3330 }
3331 }
3332
3333 if (join < 0)
3334 join = 0;
3335
3336 wpa_s->p2p_auto_pd = 0;
3337 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3338 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3339 MAC2STR(wpa_s->pending_join_dev_addr), join);
3340 if (p2p_prov_disc_req(wpa_s->global->p2p,
3341 wpa_s->pending_join_dev_addr,
3342 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003343 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003344 wpa_s->p2p_auto_pd = 0;
3345 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3346 " p2p_dev_addr=" MACSTR " status=N/A",
3347 MAC2STR(wpa_s->pending_join_dev_addr));
3348 }
3349 return;
3350 }
3351
3352 if (wpa_s->p2p_auto_join) {
3353 int join = wpas_p2p_peer_go(wpa_s,
3354 wpa_s->pending_join_dev_addr);
3355 if (join < 0) {
3356 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3357 "running a GO -> use GO Negotiation");
3358 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3359 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3360 wpa_s->p2p_persistent_group, 0, 0, 0,
3361 wpa_s->p2p_go_intent,
3362 wpa_s->p2p_connect_freq,
3363 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003364 wpa_s->p2p_pd_before_go_neg,
3365 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003366 return;
3367 }
3368
3369 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3370 "try to join the group", join ? "" :
3371 " in older scan");
3372 if (!join)
3373 wpa_s->p2p_fallback_to_go_neg = 1;
3374 }
3375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003376 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3377 wpa_s->pending_join_iface_addr);
3378 if (freq < 0 &&
3379 p2p_get_interface_addr(wpa_s->global->p2p,
3380 wpa_s->pending_join_dev_addr,
3381 iface_addr) == 0 &&
3382 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3383 {
3384 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3385 "address for join from " MACSTR " to " MACSTR
3386 " based on newly discovered P2P peer entry",
3387 MAC2STR(wpa_s->pending_join_iface_addr),
3388 MAC2STR(iface_addr));
3389 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3390 ETH_ALEN);
3391
3392 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3393 wpa_s->pending_join_iface_addr);
3394 }
3395 if (freq >= 0) {
3396 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3397 "from P2P peer table: %d MHz", freq);
3398 }
3399 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3400 if (bss) {
3401 freq = bss->freq;
3402 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3403 "from BSS table: %d MHz", freq);
3404 }
3405 if (freq > 0) {
3406 u16 method;
3407
Dmitry Shmidt04949592012-07-19 12:16:46 -07003408 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3409 wpa_msg(wpa_s->parent, MSG_INFO,
3410 P2P_EVENT_GROUP_FORMATION_FAILURE
3411 "reason=FREQ_CONFLICT");
3412 return;
3413 }
3414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3416 "prior to joining an existing group (GO " MACSTR
3417 " freq=%u MHz)",
3418 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3419 wpa_s->pending_pd_before_join = 1;
3420
3421 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003422 case WPS_PIN_DISPLAY:
3423 method = WPS_CONFIG_KEYPAD;
3424 break;
3425 case WPS_PIN_KEYPAD:
3426 method = WPS_CONFIG_DISPLAY;
3427 break;
3428 case WPS_PBC:
3429 method = WPS_CONFIG_PUSHBUTTON;
3430 break;
3431 default:
3432 method = 0;
3433 break;
3434 }
3435
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003436 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3437 wpa_s->pending_join_dev_addr) ==
3438 method)) {
3439 /*
3440 * We have already performed provision discovery for
3441 * joining the group. Proceed directly to join
3442 * operation without duplicated provision discovery. */
3443 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3444 "with " MACSTR " already done - proceed to "
3445 "join",
3446 MAC2STR(wpa_s->pending_join_dev_addr));
3447 wpa_s->pending_pd_before_join = 0;
3448 goto start;
3449 }
3450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003451 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003452 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003453 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003454 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3455 "Discovery Request before joining an "
3456 "existing group");
3457 wpa_s->pending_pd_before_join = 0;
3458 goto start;
3459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460 return;
3461 }
3462
3463 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3464 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3465 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3466 wpas_p2p_check_join_scan_limit(wpa_s);
3467 return;
3468
3469start:
3470 /* Start join operation immediately */
3471 wpas_p2p_join_start(wpa_s);
3472}
3473
3474
Dmitry Shmidt04949592012-07-19 12:16:46 -07003475static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003476{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003477 int ret;
3478 struct wpa_driver_scan_params params;
3479 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003480 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003481 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003482#ifdef ANDROID_P2P
3483 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003484
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003485 /* If freq is not provided, check the operating freq of the GO and do a
3486 * a directed scan to save time
3487 */
3488 if(!freq) {
3489 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3490 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3491 }
3492#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003493 os_memset(&params, 0, sizeof(params));
3494
3495 /* P2P Wildcard SSID */
3496 params.num_ssids = 1;
3497 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3498 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3499
3500 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003501 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3502 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3503 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504 if (wps_ie == NULL) {
3505 wpas_p2p_scan_res_join(wpa_s, NULL);
3506 return;
3507 }
3508
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003509 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3510 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003511 if (ies == NULL) {
3512 wpabuf_free(wps_ie);
3513 wpas_p2p_scan_res_join(wpa_s, NULL);
3514 return;
3515 }
3516 wpabuf_put_buf(ies, wps_ie);
3517 wpabuf_free(wps_ie);
3518
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003519 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003520
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003521 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003522 params.extra_ies = wpabuf_head(ies);
3523 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003524 if (freq > 0) {
3525 freqs[0] = freq;
3526 params.freqs = freqs;
3527 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003528
3529 /*
3530 * Run a scan to update BSS table and start Provision Discovery once
3531 * the new scan results become available.
3532 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003533 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003534 if (!ret)
3535 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003536
3537 wpabuf_free(ies);
3538
3539 if (ret) {
3540 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3541 "try again later");
3542 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3543 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3544 wpas_p2p_check_join_scan_limit(wpa_s);
3545 }
3546}
3547
3548
Dmitry Shmidt04949592012-07-19 12:16:46 -07003549static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3550{
3551 struct wpa_supplicant *wpa_s = eloop_ctx;
3552 wpas_p2p_join_scan_req(wpa_s, 0);
3553}
3554
3555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003556static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003557 const u8 *dev_addr, enum p2p_wps_method wps_method,
3558 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003559{
3560 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003561 MACSTR " dev " MACSTR ")%s",
3562 MAC2STR(iface_addr), MAC2STR(dev_addr),
3563 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003564
Dmitry Shmidt04949592012-07-19 12:16:46 -07003565 wpa_s->p2p_auto_pd = 0;
3566 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3568 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3569 wpa_s->pending_join_wps_method = wps_method;
3570
3571 /* Make sure we are not running find during connection establishment */
3572 wpas_p2p_stop_find(wpa_s);
3573
3574 wpa_s->p2p_join_scan_count = 0;
3575 wpas_p2p_join_scan(wpa_s, NULL);
3576 return 0;
3577}
3578
3579
3580static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3581{
3582 struct wpa_supplicant *group;
3583 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003584 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003585
3586 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3587 if (group == NULL)
3588 return -1;
3589 if (group != wpa_s) {
3590 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3591 sizeof(group->p2p_pin));
3592 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003593 } else {
3594 /*
3595 * Need to mark the current interface for p2p_group_formation
3596 * when a separate group interface is not used. This is needed
3597 * to allow p2p_cancel stop a pending p2p_connect-join.
3598 * wpas_p2p_init_group_interface() addresses this for the case
3599 * where a separate group interface is used.
3600 */
3601 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602 }
3603
3604 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003605 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606
3607 os_memset(&res, 0, sizeof(res));
3608 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3609 ETH_ALEN);
3610 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003611 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3612 if (bss) {
3613 res.freq = bss->freq;
3614 res.ssid_len = bss->ssid_len;
3615 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3616 }
3617
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003618 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3619 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3620 "starting client");
3621 wpa_drv_cancel_remain_on_channel(wpa_s);
3622 wpa_s->off_channel_freq = 0;
3623 wpa_s->roc_waiting_drv_freq = 0;
3624 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625 wpas_start_wps_enrollee(group, &res);
3626
3627 /*
3628 * Allow a longer timeout for join-a-running-group than normal 15
3629 * second group formation timeout since the GO may not have authorized
3630 * our connection yet.
3631 */
3632 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3633 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3634 wpa_s, NULL);
3635
3636 return 0;
3637}
3638
3639
3640/**
3641 * wpas_p2p_connect - Request P2P Group Formation to be started
3642 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3643 * @peer_addr: Address of the peer P2P Device
3644 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3645 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003646 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003647 * @join: Whether to join an existing group (as a client) instead of starting
3648 * Group Owner negotiation; @peer_addr is BSSID in that case
3649 * @auth: Whether to only authorize the connection instead of doing that and
3650 * initiating Group Owner negotiation
3651 * @go_intent: GO Intent or -1 to use default
3652 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003653 * @persistent_id: Persistent group credentials to use for forcing GO
3654 * parameters or -1 to generate new values (SSID/passphrase)
3655 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3656 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003657 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3659 * failure, -2 on failure due to channel not currently available,
3660 * -3 if forced channel is not supported
3661 */
3662int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3663 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003664 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003665 int go_intent, int freq, int persistent_id, int pd,
3666 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003667{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003668 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669 u8 bssid[ETH_ALEN];
3670 int ret = 0;
3671 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003672 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003673 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003674
3675 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3676 return -1;
3677
Dmitry Shmidt04949592012-07-19 12:16:46 -07003678 if (persistent_id >= 0) {
3679 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3680 if (ssid == NULL || ssid->disabled != 2 ||
3681 ssid->mode != WPAS_MODE_P2P_GO)
3682 return -1;
3683 }
3684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003685 if (go_intent < 0)
3686 go_intent = wpa_s->conf->p2p_go_intent;
3687
3688 if (!auth)
3689 wpa_s->p2p_long_listen = 0;
3690
3691 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003692 wpa_s->p2p_persistent_group = !!persistent_group;
3693 wpa_s->p2p_persistent_id = persistent_id;
3694 wpa_s->p2p_go_intent = go_intent;
3695 wpa_s->p2p_connect_freq = freq;
3696 wpa_s->p2p_fallback_to_go_neg = 0;
3697 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003698 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699
3700 if (pin)
3701 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3702 else if (wps_method == WPS_PIN_DISPLAY) {
3703 ret = wps_generate_pin();
3704 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3705 ret);
3706 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3707 wpa_s->p2p_pin);
3708 } else
3709 wpa_s->p2p_pin[0] = '\0';
3710
Dmitry Shmidt04949592012-07-19 12:16:46 -07003711 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003712 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3713 if (auth) {
3714 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3715 "connect a running group from " MACSTR,
3716 MAC2STR(peer_addr));
3717 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3718 return ret;
3719 }
3720 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3721 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3722 iface_addr) < 0) {
3723 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3724 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3725 dev_addr);
3726 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003727 if (auto_join) {
3728 os_get_time(&wpa_s->p2p_auto_started);
3729 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3730 "%ld.%06ld",
3731 wpa_s->p2p_auto_started.sec,
3732 wpa_s->p2p_auto_started.usec);
3733 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003734 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003735 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3736 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003737 return -1;
3738 return ret;
3739 }
3740
3741 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3742 wpa_s->assoc_freq)
3743 oper_freq = wpa_s->assoc_freq;
3744 else {
3745 oper_freq = wpa_drv_shared_freq(wpa_s);
3746 if (oper_freq < 0)
3747 oper_freq = 0;
3748 }
3749
3750 if (freq > 0) {
3751 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3752 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3753 "(%u MHz) is not supported for P2P uses",
3754 freq);
3755 return -3;
3756 }
3757
3758 if (oper_freq > 0 && freq != oper_freq &&
3759 !(wpa_s->drv_flags &
3760 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3761 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3762 "on %u MHz while connected on another "
3763 "channel (%u MHz)", freq, oper_freq);
3764 return -2;
3765 }
3766 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3767 "requested channel (%u MHz)", freq);
3768 force_freq = freq;
3769 } else if (oper_freq > 0 &&
3770 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3771 if (!(wpa_s->drv_flags &
3772 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3773 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3774 "while connected on non-P2P supported "
3775 "channel (%u MHz)", oper_freq);
3776 return -2;
3777 }
3778 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3779 "(%u MHz) not available for P2P - try to use "
3780 "another channel", oper_freq);
3781 force_freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003782 } else if (oper_freq > 0 &&
3783 (wpa_s->drv_flags &
3784 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3785 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3786 "are already using (%u MHz) on another interface",
3787 oper_freq);
3788 pref_freq = oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003789 } else if (oper_freq > 0) {
3790 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3791 "channel we are already using (%u MHz) on another "
3792 "interface", oper_freq);
3793 force_freq = oper_freq;
3794 }
3795
3796 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3797
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003798 if (wpa_s->create_p2p_iface) {
3799 /* Prepare to add a new interface for the group */
3800 iftype = WPA_IF_P2P_GROUP;
3801 if (go_intent == 15)
3802 iftype = WPA_IF_P2P_GO;
3803 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3804 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3805 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003807 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003808
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003809 if_addr = wpa_s->pending_interface_addr;
3810 } else
3811 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003812
3813 if (auth) {
3814 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003815 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003816 force_freq, persistent_group, ssid,
3817 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003818 return -1;
3819 return ret;
3820 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003821
3822 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3823 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003824 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003825 if (wpa_s->create_p2p_iface)
3826 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827 return -1;
3828 }
3829 return ret;
3830}
3831
3832
3833/**
3834 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3835 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3836 * @freq: Frequency of the channel in MHz
3837 * @duration: Duration of the stay on the channel in milliseconds
3838 *
3839 * This callback is called when the driver indicates that it has started the
3840 * requested remain-on-channel duration.
3841 */
3842void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3843 unsigned int freq, unsigned int duration)
3844{
3845 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3846 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003847 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3848 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3849 wpa_s->pending_listen_duration);
3850 wpa_s->pending_listen_freq = 0;
3851 }
3852}
3853
3854
3855static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3856 unsigned int timeout)
3857{
3858 /* Limit maximum Listen state time based on driver limitation. */
3859 if (timeout > wpa_s->max_remain_on_chan)
3860 timeout = wpa_s->max_remain_on_chan;
3861
3862 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3863 return wpa_drv_p2p_listen(wpa_s, timeout);
3864
3865 return p2p_listen(wpa_s->global->p2p, timeout);
3866}
3867
3868
3869/**
3870 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3871 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3872 * @freq: Frequency of the channel in MHz
3873 *
3874 * This callback is called when the driver indicates that a remain-on-channel
3875 * operation has been completed, i.e., the duration on the requested channel
3876 * has timed out.
3877 */
3878void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3879 unsigned int freq)
3880{
3881 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3882 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003883 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3885 return;
3886 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3887 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003888 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003889 return;
3890 if (wpa_s->p2p_long_listen > 0)
3891 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3892 if (wpa_s->p2p_long_listen > 0) {
3893 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3894 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3895 }
3896}
3897
3898
3899/**
3900 * wpas_p2p_group_remove - Remove a P2P group
3901 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3902 * @ifname: Network interface name of the group interface or "*" to remove all
3903 * groups
3904 * Returns: 0 on success, -1 on failure
3905 *
3906 * This function is used to remove a P2P group. This can be used to disconnect
3907 * from a group in which the local end is a P2P Client or to end a P2P Group in
3908 * case the local end is the Group Owner. If a virtual network interface was
3909 * created for this group, that interface will be removed. Otherwise, only the
3910 * configured P2P group network will be removed from the interface.
3911 */
3912int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3913{
3914 struct wpa_global *global = wpa_s->global;
3915
3916 if (os_strcmp(ifname, "*") == 0) {
3917 struct wpa_supplicant *prev;
3918 wpa_s = global->ifaces;
3919 while (wpa_s) {
3920 prev = wpa_s;
3921 wpa_s = wpa_s->next;
3922 wpas_p2p_disconnect(prev);
3923 }
3924 return 0;
3925 }
3926
3927 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3928 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3929 break;
3930 }
3931
3932 return wpas_p2p_disconnect(wpa_s);
3933}
3934
3935
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003936static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3937 struct p2p_go_neg_results *params,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003938 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939{
3940 u8 bssid[ETH_ALEN];
3941 int res;
3942
3943 os_memset(params, 0, sizeof(*params));
3944 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003945 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003946 if (freq) {
3947 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3948 "frequency %d MHz", freq);
3949 params->freq = freq;
3950 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3951 wpa_s->conf->p2p_oper_channel >= 1 &&
3952 wpa_s->conf->p2p_oper_channel <= 11) {
3953 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3954 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3955 "frequency %d MHz", params->freq);
3956 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003957 wpa_s->conf->p2p_oper_reg_class == 116 ||
3958 wpa_s->conf->p2p_oper_reg_class == 117 ||
3959 wpa_s->conf->p2p_oper_reg_class == 124 ||
3960 wpa_s->conf->p2p_oper_reg_class == 126 ||
3961 wpa_s->conf->p2p_oper_reg_class == 127) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003962 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3963 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3964 "frequency %d MHz", params->freq);
3965 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3966 wpa_s->best_overall_freq > 0 &&
3967 p2p_supported_freq(wpa_s->global->p2p,
3968 wpa_s->best_overall_freq)) {
3969 params->freq = wpa_s->best_overall_freq;
3970 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3971 "channel %d MHz", params->freq);
3972 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3973 wpa_s->best_24_freq > 0 &&
3974 p2p_supported_freq(wpa_s->global->p2p,
3975 wpa_s->best_24_freq)) {
3976 params->freq = wpa_s->best_24_freq;
3977 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3978 "channel %d MHz", params->freq);
3979 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3980 wpa_s->best_5_freq > 0 &&
3981 p2p_supported_freq(wpa_s->global->p2p,
3982 wpa_s->best_5_freq)) {
3983 params->freq = wpa_s->best_5_freq;
3984 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3985 "channel %d MHz", params->freq);
3986 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003987 int chan;
3988 for (chan = 0; chan < 11; chan++) {
3989 params->freq = 2412 + chan * 5;
3990 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3991 params->freq))
3992 break;
3993 }
3994 if (chan == 11) {
3995 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3996 "allowed");
3997 return -1;
3998 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003999 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4000 "known)", params->freq);
4001 }
4002
4003 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4004 wpa_s->assoc_freq && !freq) {
4005 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4006 "already using");
4007 params->freq = wpa_s->assoc_freq;
4008 }
4009
4010 res = wpa_drv_shared_freq(wpa_s);
4011 if (res > 0 && !freq) {
4012 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
4013 "already using on a shared interface");
4014 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004015 } else if (res > 0 && freq != res &&
4016 !(wpa_s->drv_flags &
4017 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4018 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4019 "while connected on another channel (%u MHz)",
4020 freq, res);
4021 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004023
4024 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004025}
4026
4027
4028static struct wpa_supplicant *
4029wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4030 int go)
4031{
4032 struct wpa_supplicant *group_wpa_s;
4033
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004034 if (!wpas_p2p_create_iface(wpa_s)) {
4035 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4036 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004037 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004038 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004039
4040 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004041 WPA_IF_P2P_CLIENT) < 0) {
4042 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004043 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004044 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004045 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4046 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004047 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4048 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004049 wpas_p2p_remove_pending_group_interface(wpa_s);
4050 return NULL;
4051 }
4052
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004053 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4054 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 return group_wpa_s;
4056}
4057
4058
4059/**
4060 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4061 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4062 * @persistent_group: Whether to create a persistent group
4063 * @freq: Frequency for the group or 0 to indicate no hardcoding
4064 * Returns: 0 on success, -1 on failure
4065 *
4066 * This function creates a new P2P group with the local end as the Group Owner,
4067 * i.e., without using Group Owner Negotiation.
4068 */
4069int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004070 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071{
4072 struct p2p_go_neg_results params;
4073 unsigned int r;
4074
4075 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4076 return -1;
4077
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004078 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004079 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004080 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 if (freq == 2) {
4083 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4084 "band");
4085 if (wpa_s->best_24_freq > 0 &&
4086 p2p_supported_freq(wpa_s->global->p2p,
4087 wpa_s->best_24_freq)) {
4088 freq = wpa_s->best_24_freq;
4089 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4090 "channel: %d MHz", freq);
4091 } else {
4092 os_get_random((u8 *) &r, sizeof(r));
4093 freq = 2412 + (r % 3) * 25;
4094 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4095 "channel: %d MHz", freq);
4096 }
4097 }
4098
4099 if (freq == 5) {
4100 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4101 "band");
4102 if (wpa_s->best_5_freq > 0 &&
4103 p2p_supported_freq(wpa_s->global->p2p,
4104 wpa_s->best_5_freq)) {
4105 freq = wpa_s->best_5_freq;
4106 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4107 "channel: %d MHz", freq);
4108 } else {
4109 os_get_random((u8 *) &r, sizeof(r));
4110 freq = 5180 + (r % 4) * 20;
4111 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4112 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4113 "5 GHz channel for P2P group");
4114 return -1;
4115 }
4116 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4117 "channel: %d MHz", freq);
4118 }
4119 }
4120
4121 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4122 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4123 "(%u MHz) is not supported for P2P uses",
4124 freq);
4125 return -1;
4126 }
4127
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004128 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004129 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004130 if (params.freq &&
4131 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4132 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4133 "(%u MHz) is not supported for P2P uses",
4134 params.freq);
4135 return -1;
4136 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004137 p2p_go_params(wpa_s->global->p2p, &params);
4138 params.persistent_group = persistent_group;
4139
4140 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4141 if (wpa_s == NULL)
4142 return -1;
4143 wpas_start_wps_go(wpa_s, &params, 0);
4144
4145 return 0;
4146}
4147
4148
4149static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4150 struct wpa_ssid *params, int addr_allocated)
4151{
4152 struct wpa_ssid *ssid;
4153
4154 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4155 if (wpa_s == NULL)
4156 return -1;
4157
4158 wpa_supplicant_ap_deinit(wpa_s);
4159
4160 ssid = wpa_config_add_network(wpa_s->conf);
4161 if (ssid == NULL)
4162 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004163 wpa_config_set_network_defaults(ssid);
4164 ssid->temporary = 1;
4165 ssid->proto = WPA_PROTO_RSN;
4166 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4167 ssid->group_cipher = WPA_CIPHER_CCMP;
4168 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4169 ssid->ssid = os_malloc(params->ssid_len);
4170 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004171 wpa_config_remove_network(wpa_s->conf, ssid->id);
4172 return -1;
4173 }
4174 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4175 ssid->ssid_len = params->ssid_len;
4176 ssid->p2p_group = 1;
4177 ssid->export_keys = 1;
4178 if (params->psk_set) {
4179 os_memcpy(ssid->psk, params->psk, 32);
4180 ssid->psk_set = 1;
4181 }
4182 if (params->passphrase)
4183 ssid->passphrase = os_strdup(params->passphrase);
4184
4185 wpa_supplicant_select_network(wpa_s, ssid);
4186
4187 wpa_s->show_group_started = 1;
4188
4189 return 0;
4190}
4191
4192
4193int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4194 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004195 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004196{
4197 struct p2p_go_neg_results params;
4198 int go = 0;
4199
4200 if (ssid->disabled != 2 || ssid->ssid == NULL)
4201 return -1;
4202
4203 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4204 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4205 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4206 "already running");
4207 return 0;
4208 }
4209
4210 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004211 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004212
Dmitry Shmidt04949592012-07-19 12:16:46 -07004213 wpa_s->p2p_fallback_to_go_neg = 0;
4214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004215 if (ssid->mode == WPAS_MODE_INFRA)
4216 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4217
4218 if (ssid->mode != WPAS_MODE_P2P_GO)
4219 return -1;
4220
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004221 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004222 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004223
4224 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004225 params.psk_set = ssid->psk_set;
4226 if (params.psk_set)
4227 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004228 if (ssid->passphrase) {
4229 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4230 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4231 "persistent group");
4232 return -1;
4233 }
4234 os_strlcpy(params.passphrase, ssid->passphrase,
4235 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004237 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4238 params.ssid_len = ssid->ssid_len;
4239 params.persistent_group = 1;
4240
4241 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4242 if (wpa_s == NULL)
4243 return -1;
4244
4245 wpas_start_wps_go(wpa_s, &params, 0);
4246
4247 return 0;
4248}
4249
4250
4251static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4252 struct wpabuf *proberesp_ies)
4253{
4254 struct wpa_supplicant *wpa_s = ctx;
4255 if (wpa_s->ap_iface) {
4256 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004257 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4258 wpabuf_free(beacon_ies);
4259 wpabuf_free(proberesp_ies);
4260 return;
4261 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004262 if (beacon_ies) {
4263 wpabuf_free(hapd->p2p_beacon_ie);
4264 hapd->p2p_beacon_ie = beacon_ies;
4265 }
4266 wpabuf_free(hapd->p2p_probe_resp_ie);
4267 hapd->p2p_probe_resp_ie = proberesp_ies;
4268 } else {
4269 wpabuf_free(beacon_ies);
4270 wpabuf_free(proberesp_ies);
4271 }
4272 wpa_supplicant_ap_update_beacon(wpa_s);
4273}
4274
4275
4276static void wpas_p2p_idle_update(void *ctx, int idle)
4277{
4278 struct wpa_supplicant *wpa_s = ctx;
4279 if (!wpa_s->ap_iface)
4280 return;
4281 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004282 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283 wpas_p2p_set_group_idle_timeout(wpa_s);
4284 else
4285 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4286}
4287
4288
4289struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004290 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004291{
4292 struct p2p_group *group;
4293 struct p2p_group_config *cfg;
4294
4295 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4296 return NULL;
4297 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4298 return NULL;
4299
4300 cfg = os_zalloc(sizeof(*cfg));
4301 if (cfg == NULL)
4302 return NULL;
4303
Dmitry Shmidt04949592012-07-19 12:16:46 -07004304 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004305 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004306 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004307 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004308 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4309 if (wpa_s->max_stations &&
4310 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4311 cfg->max_clients = wpa_s->max_stations;
4312 else
4313 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004314 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4315 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004316 cfg->cb_ctx = wpa_s;
4317 cfg->ie_update = wpas_p2p_ie_update;
4318 cfg->idle_update = wpas_p2p_idle_update;
4319
4320 group = p2p_group_init(wpa_s->global->p2p, cfg);
4321 if (group == NULL)
4322 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004323 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 p2p_group_notif_formation_done(group);
4325 wpa_s->p2p_group = group;
4326 return group;
4327}
4328
4329
4330void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4331 int registrar)
4332{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004333 struct wpa_ssid *ssid = wpa_s->current_ssid;
4334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004335 if (!wpa_s->p2p_in_provisioning) {
4336 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4337 "provisioning not in progress");
4338 return;
4339 }
4340
Dmitry Shmidt04949592012-07-19 12:16:46 -07004341 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4342 u8 go_dev_addr[ETH_ALEN];
4343 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4344 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4345 ssid->ssid_len);
4346 /* Clear any stored provisioning info */
4347 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4348 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4351 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004352 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4353 /*
4354 * Use a separate timeout for initial data connection to
4355 * complete to allow the group to be removed automatically if
4356 * something goes wrong in this step before the P2P group idle
4357 * timeout mechanism is taken into use.
4358 */
4359 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4360 wpas_p2p_group_formation_timeout,
4361 wpa_s->parent, NULL);
4362 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004363 if (wpa_s->global->p2p)
4364 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4365 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4366 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4367 wpas_group_formation_completed(wpa_s, 1);
4368}
4369
4370
Jouni Malinen75ecf522011-06-27 15:19:46 -07004371void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4372 struct wps_event_fail *fail)
4373{
4374 if (!wpa_s->p2p_in_provisioning) {
4375 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4376 "provisioning not in progress");
4377 return;
4378 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004379
4380 if (wpa_s->go_params) {
4381 p2p_clear_provisioning_info(
4382 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004383 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004384 }
4385
Jouni Malinen75ecf522011-06-27 15:19:46 -07004386 wpas_notify_p2p_wps_failed(wpa_s, fail);
4387}
4388
4389
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004390int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004391 const char *config_method,
4392 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004393{
4394 u16 config_methods;
4395
Dmitry Shmidt04949592012-07-19 12:16:46 -07004396 wpa_s->p2p_fallback_to_go_neg = 0;
4397 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004398 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004400 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004401 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004402 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4403 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004405 else {
4406 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004408 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004409
Dmitry Shmidt04949592012-07-19 12:16:46 -07004410 if (use == WPAS_P2P_PD_AUTO) {
4411 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4412 wpa_s->pending_pd_config_methods = config_methods;
4413 wpa_s->p2p_auto_pd = 1;
4414 wpa_s->p2p_auto_join = 0;
4415 wpa_s->pending_pd_before_join = 0;
4416 wpa_s->auto_pd_scan_retry = 0;
4417 wpas_p2p_stop_find(wpa_s);
4418 wpa_s->p2p_join_scan_count = 0;
4419 os_get_time(&wpa_s->p2p_auto_started);
4420 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4421 wpa_s->p2p_auto_started.sec,
4422 wpa_s->p2p_auto_started.usec);
4423 wpas_p2p_join_scan(wpa_s, NULL);
4424 return 0;
4425 }
4426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004427 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4428 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004429 config_methods,
4430 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431 }
4432
4433 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4434 return -1;
4435
4436 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004437 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004438 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004439}
4440
4441
4442int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4443 char *end)
4444{
4445 return p2p_scan_result_text(ies, ies_len, buf, end);
4446}
4447
4448
4449static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4450{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004451 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004452 return;
4453
4454 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4455 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004456 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004457}
4458
4459
4460int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4461 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004462 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004463 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004464{
4465 wpas_p2p_clear_pending_action_tx(wpa_s);
4466 wpa_s->p2p_long_listen = 0;
4467
4468 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4469 return wpa_drv_p2p_find(wpa_s, timeout, type);
4470
Dmitry Shmidt04949592012-07-19 12:16:46 -07004471 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4472 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004473 return -1;
4474
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004475 wpa_supplicant_cancel_sched_scan(wpa_s);
4476
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004477 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004478 num_req_dev_types, req_dev_types, dev_id,
4479 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004480}
4481
4482
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004483static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004484{
4485 wpas_p2p_clear_pending_action_tx(wpa_s);
4486 wpa_s->p2p_long_listen = 0;
4487 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4488 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004489 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004490
4491 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4492 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004493 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004494 }
4495
4496 if (wpa_s->global->p2p)
4497 p2p_stop_find(wpa_s->global->p2p);
4498
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004499 return 0;
4500}
4501
4502
4503void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4504{
4505 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4506 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004507 wpas_p2p_remove_pending_group_interface(wpa_s);
4508}
4509
4510
4511static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4512{
4513 struct wpa_supplicant *wpa_s = eloop_ctx;
4514 wpa_s->p2p_long_listen = 0;
4515}
4516
4517
4518int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4519{
4520 int res;
4521
4522 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4523 return -1;
4524
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004525 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004526 wpas_p2p_clear_pending_action_tx(wpa_s);
4527
4528 if (timeout == 0) {
4529 /*
4530 * This is a request for unlimited Listen state. However, at
4531 * least for now, this is mapped to a Listen state for one
4532 * hour.
4533 */
4534 timeout = 3600;
4535 }
4536 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4537 wpa_s->p2p_long_listen = 0;
4538
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004539 /*
4540 * Stop previous find/listen operation to avoid trying to request a new
4541 * remain-on-channel operation while the driver is still running the
4542 * previous one.
4543 */
4544 if (wpa_s->global->p2p)
4545 p2p_stop_find(wpa_s->global->p2p);
4546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4548 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4549 wpa_s->p2p_long_listen = timeout * 1000;
4550 eloop_register_timeout(timeout, 0,
4551 wpas_p2p_long_listen_timeout,
4552 wpa_s, NULL);
4553 }
4554
4555 return res;
4556}
4557
4558
4559int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4560 u8 *buf, size_t len, int p2p_group)
4561{
4562 struct wpabuf *p2p_ie;
4563 int ret;
4564
4565 if (wpa_s->global->p2p_disabled)
4566 return -1;
4567 if (wpa_s->global->p2p == NULL)
4568 return -1;
4569 if (bss == NULL)
4570 return -1;
4571
4572 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4573 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4574 p2p_group, p2p_ie);
4575 wpabuf_free(p2p_ie);
4576
4577 return ret;
4578}
4579
4580
4581int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004582 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004583 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584{
4585 if (wpa_s->global->p2p_disabled)
4586 return 0;
4587 if (wpa_s->global->p2p == NULL)
4588 return 0;
4589
Dmitry Shmidt04949592012-07-19 12:16:46 -07004590 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4591 ie, ie_len)) {
4592 case P2P_PREQ_NOT_P2P:
4593 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4594 ssi_signal);
4595 /* fall through */
4596 case P2P_PREQ_MALFORMED:
4597 case P2P_PREQ_NOT_LISTEN:
4598 case P2P_PREQ_NOT_PROCESSED:
4599 default: /* make gcc happy */
4600 return 0;
4601 case P2P_PREQ_PROCESSED:
4602 return 1;
4603 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604}
4605
4606
4607void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4608 const u8 *sa, const u8 *bssid,
4609 u8 category, const u8 *data, size_t len, int freq)
4610{
4611 if (wpa_s->global->p2p_disabled)
4612 return;
4613 if (wpa_s->global->p2p == NULL)
4614 return;
4615
4616 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4617 freq);
4618}
4619
4620
4621void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4622{
4623 if (wpa_s->global->p2p_disabled)
4624 return;
4625 if (wpa_s->global->p2p == NULL)
4626 return;
4627
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004628 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629}
4630
4631
4632void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4633{
4634 p2p_group_deinit(wpa_s->p2p_group);
4635 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004636
4637 wpa_s->ap_configured_cb = NULL;
4638 wpa_s->ap_configured_cb_ctx = NULL;
4639 wpa_s->ap_configured_cb_data = NULL;
4640 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004641}
4642
4643
4644int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4645{
4646 wpa_s->p2p_long_listen = 0;
4647
4648 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4649 return wpa_drv_p2p_reject(wpa_s, addr);
4650
4651 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4652 return -1;
4653
4654 return p2p_reject(wpa_s->global->p2p, addr);
4655}
4656
4657
4658/* Invite to reinvoke a persistent group */
4659int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004660 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4661 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662{
4663 enum p2p_invite_role role;
4664 u8 *bssid = NULL;
Irfan Sherifff0619662012-09-23 19:11:37 -07004665#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004666 int force_freq = 0, oper_freq = 0;
Irfan Sherifff0619662012-09-23 19:11:37 -07004667#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668
Jouni Malinen31be0a42012-08-31 21:20:51 +03004669 wpa_s->p2p_persistent_go_freq = freq;
4670 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671 if (ssid->mode == WPAS_MODE_P2P_GO) {
4672 role = P2P_INVITE_ROLE_GO;
4673 if (peer_addr == NULL) {
4674 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4675 "address in invitation command");
4676 return -1;
4677 }
4678 if (wpas_p2p_create_iface(wpa_s)) {
4679 if (wpas_p2p_add_group_interface(wpa_s,
4680 WPA_IF_P2P_GO) < 0) {
4681 wpa_printf(MSG_ERROR, "P2P: Failed to "
4682 "allocate a new interface for the "
4683 "group");
4684 return -1;
4685 }
4686 bssid = wpa_s->pending_interface_addr;
4687 } else
4688 bssid = wpa_s->own_addr;
4689 } else {
4690 role = P2P_INVITE_ROLE_CLIENT;
4691 peer_addr = ssid->bssid;
4692 }
4693 wpa_s->pending_invite_ssid_id = ssid->id;
4694
Irfan Sherifff0619662012-09-23 19:11:37 -07004695#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004696 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4697 wpa_s->assoc_freq)
4698 oper_freq = wpa_s->assoc_freq;
4699 else {
4700 oper_freq = wpa_drv_shared_freq(wpa_s);
4701 if (oper_freq < 0)
4702 oper_freq = 0;
4703 }
4704
4705 if (freq > 0) {
4706 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4707 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4708 "(%u MHz) is not supported for P2P uses",
4709 freq);
4710 return -3;
4711 }
4712
4713 if (oper_freq > 0 && freq != oper_freq &&
4714 !(wpa_s->drv_flags &
4715 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4716 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4717 "on %u MHz while connected on another "
4718 "channel (%u MHz)", freq, oper_freq);
4719 return -2;
4720 }
4721 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4722 "requested channel (%u MHz)", freq);
4723 force_freq = freq;
4724 } else if (oper_freq > 0 &&
4725 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
4726 if (!(wpa_s->drv_flags &
4727 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4728 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4729 "while connected on non-P2P supported "
4730 "channel (%u MHz)", oper_freq);
4731 return -2;
4732 }
4733 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
4734 "(%u MHz) not available for P2P - try to use "
4735 "another channel", oper_freq);
4736 force_freq = 0;
4737 } else if (oper_freq > 0) {
4738 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4739 "channel we are already using (%u MHz) on another "
4740 "interface", oper_freq);
4741 force_freq = oper_freq;
4742 }
Irfan Sherifff0619662012-09-23 19:11:37 -07004743#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004744 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4745 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4746 ssid->ssid, ssid->ssid_len,
4747 go_dev_addr, 1);
4748
4749 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4750 return -1;
4751
Irfan Sherifff0619662012-09-23 19:11:37 -07004752#ifdef ANDROID_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004754 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, 1);
Irfan Sherifff0619662012-09-23 19:11:37 -07004755#else
4756 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4757 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4758#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759}
4760
4761
4762/* Invite to join an active group */
4763int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4764 const u8 *peer_addr, const u8 *go_dev_addr)
4765{
4766 struct wpa_global *global = wpa_s->global;
4767 enum p2p_invite_role role;
4768 u8 *bssid = NULL;
4769 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004770 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004771
Jouni Malinen31be0a42012-08-31 21:20:51 +03004772 wpa_s->p2p_persistent_go_freq = 0;
4773 wpa_s->p2p_go_ht40 = 0;
4774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004775 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4776 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4777 break;
4778 }
4779 if (wpa_s == NULL) {
4780 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4781 return -1;
4782 }
4783
4784 ssid = wpa_s->current_ssid;
4785 if (ssid == NULL) {
4786 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4787 "invitation");
4788 return -1;
4789 }
4790
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004791 persistent = ssid->p2p_persistent_group &&
4792 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4793 ssid->ssid, ssid->ssid_len);
4794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795 if (ssid->mode == WPAS_MODE_P2P_GO) {
4796 role = P2P_INVITE_ROLE_ACTIVE_GO;
4797 bssid = wpa_s->own_addr;
4798 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004799 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800 } else {
4801 role = P2P_INVITE_ROLE_CLIENT;
4802 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4803 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4804 "invite to current group");
4805 return -1;
4806 }
4807 bssid = wpa_s->bssid;
4808 if (go_dev_addr == NULL &&
4809 !is_zero_ether_addr(wpa_s->go_dev_addr))
4810 go_dev_addr = wpa_s->go_dev_addr;
4811 }
4812 wpa_s->parent->pending_invite_ssid_id = -1;
4813
4814 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4815 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4816 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004817 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818
4819 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4820 return -1;
4821
4822 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4823 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004824 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825}
4826
4827
4828void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4829{
4830 struct wpa_ssid *ssid = wpa_s->current_ssid;
4831 const char *ssid_txt;
4832 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004833 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004834 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004835 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004836
Dmitry Shmidt04949592012-07-19 12:16:46 -07004837 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4838 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4839 wpa_s->parent, NULL);
4840 }
4841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004842 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004843 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004844
4845 wpa_s->show_group_started = 0;
4846
4847 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4848 os_memset(go_dev_addr, 0, ETH_ALEN);
4849 if (ssid->bssid_set)
4850 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4851 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4852 ssid->ssid_len);
4853 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4854
4855 if (wpa_s->global->p2p_group_formation == wpa_s)
4856 wpa_s->global->p2p_group_formation = NULL;
4857
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004858 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4859 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004860 if (ssid->passphrase == NULL && ssid->psk_set) {
4861 char psk[65];
4862 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4863 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4864 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4865 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004866 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 MAC2STR(go_dev_addr),
4868 persistent ? " [PERSISTENT]" : "");
4869 } else {
4870 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4871 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4872 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004873 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874 ssid->passphrase ? ssid->passphrase : "",
4875 MAC2STR(go_dev_addr),
4876 persistent ? " [PERSISTENT]" : "");
4877 }
4878
4879 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004880 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4881 ssid, go_dev_addr);
4882 if (network_id < 0)
4883 network_id = ssid->id;
4884 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004885
4886done:
Jouni Malinendc7b7132012-09-14 12:53:47 -07004887 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004888 wpa_s->global->p2p != NULL) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07004889 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004890 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4891 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4892 "continued after successful connection");
4893 p2p_increase_search_delay(
4894 wpa_s->global->p2p,
4895 wpas_p2p_search_delay(wpa_s));
4896 }
4897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004898}
4899
4900
4901int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4902 u32 interval1, u32 duration2, u32 interval2)
4903{
4904 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4905 return -1;
4906 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4907 return -1;
4908
4909 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4910 wpa_s->current_ssid == NULL ||
4911 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4912 return -1;
4913
4914 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4915 wpa_s->own_addr, wpa_s->assoc_freq,
4916 duration1, interval1, duration2, interval2);
4917}
4918
4919
4920int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4921 unsigned int interval)
4922{
4923 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4924 return -1;
4925
4926 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4927 return -1;
4928
4929 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4930}
4931
4932
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004933static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4934{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004935 if (wpa_s->current_ssid == NULL) {
4936 /*
4937 * current_ssid can be cleared when P2P client interface gets
4938 * disconnected, so assume this interface was used as P2P
4939 * client.
4940 */
4941 return 1;
4942 }
4943 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004944 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4945}
4946
4947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004948static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4949{
4950 struct wpa_supplicant *wpa_s = eloop_ctx;
4951
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004952 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4954 "disabled");
4955 return;
4956 }
4957
Dmitry Shmidt04949592012-07-19 12:16:46 -07004958 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4959 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004960 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961}
4962
4963
4964static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4965{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004966 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967
Dmitry Shmidt04949592012-07-19 12:16:46 -07004968 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4969 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004971 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4972 return;
4973
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004974 timeout = wpa_s->conf->p2p_group_idle;
4975 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4976 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4977 timeout = P2P_MAX_CLIENT_IDLE;
4978
4979 if (timeout == 0)
4980 return;
4981
Dmitry Shmidt04949592012-07-19 12:16:46 -07004982 if (timeout < 0) {
4983 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4984 timeout = 0; /* special client mode no-timeout */
4985 else
4986 return;
4987 }
4988
4989 if (wpa_s->p2p_in_provisioning) {
4990 /*
4991 * Use the normal group formation timeout during the
4992 * provisioning phase to avoid terminating this process too
4993 * early due to group idle timeout.
4994 */
4995 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4996 "during provisioning");
4997 return;
4998 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004999#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07005000 if (wpa_s->show_group_started) {
5001 /*
5002 * Use the normal group formation timeout between the end of
5003 * the provisioning phase and completion of 4-way handshake to
5004 * avoid terminating this process too early due to group idle
5005 * timeout.
5006 */
5007 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5008 "while waiting for initial 4-way handshake to "
5009 "complete");
5010 return;
5011 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07005012#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07005013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005015 timeout);
5016 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5017 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018}
5019
5020
Jouni Malinen2b89da82012-08-31 22:04:41 +03005021/* Returns 1 if the interface was removed */
5022int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5023 u16 reason_code, const u8 *ie, size_t ie_len,
5024 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025{
5026 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005027 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005029 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030
Dmitry Shmidt04949592012-07-19 12:16:46 -07005031 if (!locally_generated)
5032 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5033 ie_len);
5034
5035 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5036 wpa_s->current_ssid &&
5037 wpa_s->current_ssid->p2p_group &&
5038 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5039 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5040 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005041 if (wpas_p2p_group_delete(wpa_s,
5042 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5043 > 0)
5044 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005045 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005046
5047 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005048}
5049
5050
5051void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005052 u16 reason_code, const u8 *ie, size_t ie_len,
5053 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005054{
5055 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5056 return;
5057 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5058 return;
5059
Dmitry Shmidt04949592012-07-19 12:16:46 -07005060 if (!locally_generated)
5061 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5062 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063}
5064
5065
5066void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5067{
5068 struct p2p_data *p2p = wpa_s->global->p2p;
5069
5070 if (p2p == NULL)
5071 return;
5072
5073 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5074 return;
5075
5076 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5077 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5078
5079 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5080 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5081
5082 if (wpa_s->wps &&
5083 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5084 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5085
5086 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5087 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5088
5089 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5090 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5091 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5092 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5093 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5094 }
5095
5096 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5097 p2p_set_sec_dev_types(p2p,
5098 (void *) wpa_s->conf->sec_device_type,
5099 wpa_s->conf->num_sec_device_types);
5100
5101 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5102 int i;
5103 p2p_remove_wps_vendor_extensions(p2p);
5104 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5105 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5106 continue;
5107 p2p_add_wps_vendor_extension(
5108 p2p, wpa_s->conf->wps_vendor_ext[i]);
5109 }
5110 }
5111
5112 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5113 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5114 char country[3];
5115 country[0] = wpa_s->conf->country[0];
5116 country[1] = wpa_s->conf->country[1];
5117 country[2] = 0x04;
5118 p2p_set_country(p2p, country);
5119 }
5120
5121 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5122 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5123 wpa_s->conf->p2p_ssid_postfix ?
5124 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5125 0);
5126 }
5127
5128 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5129 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005130
5131 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5132 u8 reg_class, channel;
5133 int ret;
5134 unsigned int r;
5135 if (wpa_s->conf->p2p_listen_reg_class &&
5136 wpa_s->conf->p2p_listen_channel) {
5137 reg_class = wpa_s->conf->p2p_listen_reg_class;
5138 channel = wpa_s->conf->p2p_listen_channel;
5139 } else {
5140 reg_class = 81;
5141 /*
5142 * Pick one of the social channels randomly as the
5143 * listen channel.
5144 */
5145 os_get_random((u8 *) &r, sizeof(r));
5146 channel = 1 + (r % 3) * 5;
5147 }
5148 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5149 if (ret)
5150 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5151 "failed: %d", ret);
5152 }
5153 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5154 u8 op_reg_class, op_channel, cfg_op_channel;
5155 int ret = 0;
5156 unsigned int r;
5157 if (wpa_s->conf->p2p_oper_reg_class &&
5158 wpa_s->conf->p2p_oper_channel) {
5159 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5160 op_channel = wpa_s->conf->p2p_oper_channel;
5161 cfg_op_channel = 1;
5162 } else {
5163 op_reg_class = 81;
5164 /*
5165 * Use random operation channel from (1, 6, 11)
5166 *if no other preference is indicated.
5167 */
5168 os_get_random((u8 *) &r, sizeof(r));
5169 op_channel = 1 + (r % 3) * 5;
5170 cfg_op_channel = 0;
5171 }
5172 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5173 cfg_op_channel);
5174 if (ret)
5175 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5176 "failed: %d", ret);
5177 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005178
5179 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5180 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5181 wpa_s->conf->p2p_pref_chan) < 0) {
5182 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5183 "update failed");
5184 }
5185 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005186}
5187
5188
5189int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5190 int duration)
5191{
5192 if (!wpa_s->ap_iface)
5193 return -1;
5194 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5195 duration);
5196}
5197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005198
5199int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5200{
5201 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5202 return -1;
5203 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5204 return -1;
5205
5206 wpa_s->global->cross_connection = enabled;
5207 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5208
5209 if (!enabled) {
5210 struct wpa_supplicant *iface;
5211
5212 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5213 {
5214 if (iface->cross_connect_enabled == 0)
5215 continue;
5216
5217 iface->cross_connect_enabled = 0;
5218 iface->cross_connect_in_use = 0;
5219 wpa_msg(iface->parent, MSG_INFO,
5220 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5221 iface->ifname, iface->cross_connect_uplink);
5222 }
5223 }
5224
5225 return 0;
5226}
5227
5228
5229static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5230{
5231 struct wpa_supplicant *iface;
5232
5233 if (!uplink->global->cross_connection)
5234 return;
5235
5236 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5237 if (!iface->cross_connect_enabled)
5238 continue;
5239 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5240 0)
5241 continue;
5242 if (iface->ap_iface == NULL)
5243 continue;
5244 if (iface->cross_connect_in_use)
5245 continue;
5246
5247 iface->cross_connect_in_use = 1;
5248 wpa_msg(iface->parent, MSG_INFO,
5249 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5250 iface->ifname, iface->cross_connect_uplink);
5251 }
5252}
5253
5254
5255static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5256{
5257 struct wpa_supplicant *iface;
5258
5259 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5260 if (!iface->cross_connect_enabled)
5261 continue;
5262 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5263 0)
5264 continue;
5265 if (!iface->cross_connect_in_use)
5266 continue;
5267
5268 wpa_msg(iface->parent, MSG_INFO,
5269 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5270 iface->ifname, iface->cross_connect_uplink);
5271 iface->cross_connect_in_use = 0;
5272 }
5273}
5274
5275
5276void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5277{
5278 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5279 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5280 wpa_s->cross_connect_disallowed)
5281 wpas_p2p_disable_cross_connect(wpa_s);
5282 else
5283 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005284 if (!wpa_s->ap_iface &&
5285 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5286 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005287}
5288
5289
5290void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5291{
5292 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005293 if (!wpa_s->ap_iface &&
5294 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5295 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005296 wpas_p2p_set_group_idle_timeout(wpa_s);
5297}
5298
5299
5300static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5301{
5302 struct wpa_supplicant *iface;
5303
5304 if (!wpa_s->global->cross_connection)
5305 return;
5306
5307 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5308 if (iface == wpa_s)
5309 continue;
5310 if (iface->drv_flags &
5311 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5312 continue;
5313 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5314 continue;
5315
5316 wpa_s->cross_connect_enabled = 1;
5317 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5318 sizeof(wpa_s->cross_connect_uplink));
5319 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5320 "%s to %s whenever uplink is available",
5321 wpa_s->ifname, wpa_s->cross_connect_uplink);
5322
5323 if (iface->ap_iface || iface->current_ssid == NULL ||
5324 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5325 iface->cross_connect_disallowed ||
5326 iface->wpa_state != WPA_COMPLETED)
5327 break;
5328
5329 wpa_s->cross_connect_in_use = 1;
5330 wpa_msg(wpa_s->parent, MSG_INFO,
5331 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5332 wpa_s->ifname, wpa_s->cross_connect_uplink);
5333 break;
5334 }
5335}
5336
5337
5338int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5339{
5340 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5341 !wpa_s->p2p_in_provisioning)
5342 return 0; /* not P2P client operation */
5343
5344 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5345 "session overlap");
5346 if (wpa_s != wpa_s->parent)
5347 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5348
5349 if (wpa_s->global->p2p)
5350 p2p_group_formation_failed(wpa_s->global->p2p);
5351
5352 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5353 wpa_s->parent, NULL);
5354
5355 wpas_group_formation_completed(wpa_s, 0);
5356 return 1;
5357}
5358
5359
5360void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5361{
5362 struct p2p_channels chan;
5363
5364 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5365 return;
5366
5367 os_memset(&chan, 0, sizeof(chan));
5368 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5369 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5370 "channel list");
5371 return;
5372 }
5373
5374 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5375}
5376
5377
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005378static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5379 struct wpa_scan_results *scan_res)
5380{
5381 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5382}
5383
5384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005385int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5386{
5387 struct wpa_global *global = wpa_s->global;
5388 int found = 0;
5389 const u8 *peer;
5390
5391 if (global->p2p == NULL)
5392 return -1;
5393
5394 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5395
5396 if (wpa_s->pending_interface_name[0] &&
5397 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5398 found = 1;
5399
5400 peer = p2p_get_go_neg_peer(global->p2p);
5401 if (peer) {
5402 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5403 MACSTR, MAC2STR(peer));
5404 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005405 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005406 }
5407
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005408 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5409 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5410 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5411 found = 1;
5412 }
5413
5414 if (wpa_s->pending_pd_before_join) {
5415 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5416 wpa_s->pending_pd_before_join = 0;
5417 found = 1;
5418 }
5419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005420 wpas_p2p_stop_find(wpa_s);
5421
5422 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5423 if (wpa_s == global->p2p_group_formation &&
5424 (wpa_s->p2p_in_provisioning ||
5425 wpa_s->parent->pending_interface_type ==
5426 WPA_IF_P2P_CLIENT)) {
5427 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5428 "formation found - cancelling",
5429 wpa_s->ifname);
5430 found = 1;
5431 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5432 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005433 if (wpa_s->p2p_in_provisioning) {
5434 wpas_group_formation_completed(wpa_s, 0);
5435 break;
5436 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005437 wpas_p2p_group_delete(wpa_s,
5438 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005439 break;
5440 }
5441 }
5442
5443 if (!found) {
5444 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5445 return -1;
5446 }
5447
5448 return 0;
5449}
5450
5451
5452void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5453{
5454 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5455 return;
5456
5457 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5458 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005459 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460}
5461
5462
5463void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5464 int freq_24, int freq_5, int freq_overall)
5465{
5466 struct p2p_data *p2p = wpa_s->global->p2p;
5467 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5468 return;
5469 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5470}
5471
5472
5473int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5474{
5475 u8 peer[ETH_ALEN];
5476 struct p2p_data *p2p = wpa_s->global->p2p;
5477
5478 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5479 return -1;
5480
5481 if (hwaddr_aton(addr, peer))
5482 return -1;
5483
5484 return p2p_unauthorize(p2p, peer);
5485}
5486
5487
5488/**
5489 * wpas_p2p_disconnect - Disconnect from a P2P Group
5490 * @wpa_s: Pointer to wpa_supplicant data
5491 * Returns: 0 on success, -1 on failure
5492 *
5493 * This can be used to disconnect from a group in which the local end is a P2P
5494 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5495 * virtual network interface was created for this group, that interface will be
5496 * removed. Otherwise, only the configured P2P group network will be removed
5497 * from the interface.
5498 */
5499int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5500{
5501
5502 if (wpa_s == NULL)
5503 return -1;
5504
Jouni Malinen2b89da82012-08-31 22:04:41 +03005505 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5506 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005508
5509
5510int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5511{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005512 int ret;
5513
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005514 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5515 return 0;
5516
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005517 ret = p2p_in_progress(wpa_s->global->p2p);
5518 if (ret == 0) {
5519 /*
5520 * Check whether there is an ongoing WPS provisioning step (or
5521 * other parts of group formation) on another interface since
5522 * p2p_in_progress() does not report this to avoid issues for
5523 * scans during such provisioning step.
5524 */
5525 if (wpa_s->global->p2p_group_formation &&
5526 wpa_s->global->p2p_group_formation != wpa_s) {
5527 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5528 "in group formation",
5529 wpa_s->global->p2p_group_formation->ifname);
5530 ret = 1;
5531 }
5532 }
5533
5534 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005535}
5536
5537
5538void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5539 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005540{
5541 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5542 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5543 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005544 /**
5545 * Remove the network by scheduling the group formation
5546 * timeout to happen immediately. The teardown code
5547 * needs to be scheduled to run asynch later so that we
5548 * don't delete data from under ourselves unexpectedly.
5549 * Calling wpas_p2p_group_formation_timeout directly
5550 * causes a series of crashes in WPS failure scenarios.
5551 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005552 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5553 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005554 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5555 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005556 }
5557}
5558
5559
5560struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005561 const u8 *addr, const u8 *ssid,
5562 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005563{
5564 struct wpa_ssid *s;
5565 size_t i;
5566
5567 for (s = wpa_s->conf->ssid; s; s = s->next) {
5568 if (s->disabled != 2)
5569 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005570 if (ssid &&
5571 (ssid_len != s->ssid_len ||
5572 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5573 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005574 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5575 return s; /* peer is GO in the persistent group */
5576 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5577 continue;
5578 for (i = 0; i < s->num_p2p_clients; i++) {
5579 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5580 addr, ETH_ALEN) == 0)
5581 return s; /* peer is P2P client in persistent
5582 * group */
5583 }
5584 }
5585
5586 return NULL;
5587}
5588
5589
5590void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5591 const u8 *addr)
5592{
5593 if (addr == NULL)
5594 return;
5595 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5596}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005597
Dmitry Shmidt04949592012-07-19 12:16:46 -07005598
5599static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5600 int group_added)
5601{
5602 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005603 if (wpa_s->global->p2p_group_formation)
5604 group = wpa_s->global->p2p_group_formation;
5605 wpa_s = wpa_s->parent;
5606 offchannel_send_action_done(wpa_s);
5607 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005608 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005609 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5610 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5611 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5612 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5613 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005614 wpa_s->p2p_pd_before_go_neg,
5615 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005616}
5617
5618
5619int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5620{
5621 if (!wpa_s->p2p_fallback_to_go_neg ||
5622 wpa_s->p2p_in_provisioning <= 5)
5623 return 0;
5624
5625 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5626 return 0; /* peer operating as a GO */
5627
5628 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5629 "fallback to GO Negotiation");
5630 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5631
5632 return 1;
5633}
5634
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005635
5636unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5637{
5638 const char *rn, *rn2;
5639 struct wpa_supplicant *ifs;
5640
5641 if (wpa_s->wpa_state > WPA_SCANNING) {
5642 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5643 "concurrent operation",
5644 P2P_CONCURRENT_SEARCH_DELAY);
5645 return P2P_CONCURRENT_SEARCH_DELAY;
5646 }
5647
5648 if (!wpa_s->driver->get_radio_name)
5649 return 0;
5650 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5651 if (rn == NULL || rn[0] == '\0')
5652 return 0;
5653
5654 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5655 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5656 continue;
5657
5658 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5659 if (!rn2 || os_strcmp(rn, rn2) != 0)
5660 continue;
5661 if (ifs->wpa_state > WPA_SCANNING) {
5662 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5663 "delay due to concurrent operation on "
5664 "interface %s",
5665 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5666 return P2P_CONCURRENT_SEARCH_DELAY;
5667 }
5668 }
5669
5670 return 0;
5671}
5672
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005673#ifdef ANDROID_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005674int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5675 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005676{
5677 struct wpa_supplicant *iface = NULL;
5678 struct p2p_data *p2p = wpa_s->global->p2p;
5679
5680 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005681 if ((iface->current_ssid) &&
5682 (iface->current_ssid->frequency != freq) &&
5683 ((iface->p2p_group_interface) ||
5684 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005685
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005686 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5687 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5688 /* Try to see whether we can move the GO. If it
5689 * is not possible, remove the GO interface
5690 */
5691 if (wpa_drv_switch_channel(iface, freq) == 0) {
5692 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5693 iface->current_ssid->frequency = freq;
5694 continue;
5695 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005696 }
5697
5698 /* If GO cannot be moved or if the conflicting interface is a
5699 * P2P Client, remove the interface depending up on the connection
5700 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005701 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005702 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005703 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005704 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005705 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005706 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005707 /* If connection in progress is p2p connection, do not proceed for the connection */
5708 if (wpa_s == iface)
5709 return -1;
5710 else
5711 /* If connection in progress is STA connection, proceed for the connection */
5712 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005713 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005714 /* P2p connection has priority, disable the STA network*/
5715 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5716 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5717 " id=%d", ssid->id);
5718 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5719 if (wpa_s == iface) {
5720 /* p2p connection is in progress, continue connecting...*/
5721 return 0;
5722 }
5723 else {
5724 /* STA connection is in progress, do not allow to continue */
5725 return -1;
5726 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005727 }
5728 }
5729 }
5730 return 0;
5731}
5732#endif