]>
Commit | Line | Data |
---|---|---|
b22128ef JM |
1 | /* |
2 | * wpa_supplicant - P2P | |
3 | * Copyright (c) 2009-2010, Atheros Communications | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Alternatively, this software may be distributed under the terms of BSD | |
10 | * license. | |
11 | * | |
12 | * See README and COPYING for more details. | |
13 | */ | |
14 | ||
15 | #include "includes.h" | |
16 | ||
17 | #include "common.h" | |
18 | #include "eloop.h" | |
19 | #include "common/ieee802_11_common.h" | |
20 | #include "common/ieee802_11_defs.h" | |
21 | #include "common/wpa_ctrl.h" | |
22 | #include "wps/wps_i.h" | |
23 | #include "p2p/p2p.h" | |
24 | #include "ap/hostapd.h" | |
aefb53bd | 25 | #include "ap/p2p_hostapd.h" |
b22128ef JM |
26 | #include "wpa_supplicant_i.h" |
27 | #include "driver_i.h" | |
28 | #include "ap.h" | |
29 | #include "config_ssid.h" | |
30 | #include "config.h" | |
31 | #include "mlme.h" | |
32 | #include "notify.h" | |
33 | #include "scan.h" | |
34 | #include "bss.h" | |
35 | #include "wps_supplicant.h" | |
36 | #include "p2p_supplicant.h" | |
37 | ||
38 | ||
9b1ab931 JM |
39 | /* |
40 | * How many times to try to scan to find the GO before giving up on join | |
41 | * request. | |
42 | */ | |
43 | #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10 | |
44 | ||
45 | ||
b22128ef JM |
46 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx); |
47 | static struct wpa_supplicant * | |
48 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, | |
49 | int go); | |
50 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s); | |
ef922c4a | 51 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx); |
108def93 JM |
52 | static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, |
53 | const u8 *dev_addr, enum p2p_wps_method wps_method); | |
b22128ef | 54 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s); |
72044390 | 55 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s); |
3071e181 JM |
56 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx); |
57 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s); | |
b22128ef JM |
58 | |
59 | ||
60 | static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s, | |
61 | struct wpa_scan_results *scan_res) | |
62 | { | |
63 | size_t i; | |
64 | ||
65 | if (wpa_s->global->p2p_disabled) | |
66 | return; | |
67 | ||
68 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)", | |
69 | (int) scan_res->num); | |
70 | ||
71 | for (i = 0; i < scan_res->num; i++) { | |
72 | struct wpa_scan_res *bss = scan_res->res[i]; | |
73 | if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid, | |
74 | bss->freq, bss->level, | |
75 | (const u8 *) (bss + 1), | |
76 | bss->ie_len) > 0) | |
9bcf9541 | 77 | break; |
b22128ef JM |
78 | } |
79 | ||
80 | p2p_scan_res_handled(wpa_s->global->p2p); | |
81 | } | |
82 | ||
83 | ||
84 | static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq) | |
85 | { | |
86 | struct wpa_supplicant *wpa_s = ctx; | |
87 | struct wpa_driver_scan_params params; | |
88 | int ret; | |
89 | struct wpabuf *wps_ie, *ies; | |
90 | int social_channels[] = { 2412, 2437, 2462, 0, 0 }; | |
91 | ||
92 | os_memset(¶ms, 0, sizeof(params)); | |
93 | ||
94 | /* P2P Wildcard SSID */ | |
95 | params.num_ssids = 1; | |
96 | params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; | |
97 | params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; | |
98 | ||
99 | wpa_s->wps->dev.p2p = 1; | |
100 | wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid, | |
101 | WPS_REQ_ENROLLEE); | |
102 | if (wps_ie == NULL) | |
103 | return -1; | |
104 | ||
105 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100); | |
106 | if (ies == NULL) { | |
107 | wpabuf_free(wps_ie); | |
108 | return -1; | |
109 | } | |
110 | wpabuf_put_buf(ies, wps_ie); | |
111 | wpabuf_free(wps_ie); | |
112 | ||
113 | p2p_scan_ie(wpa_s->global->p2p, ies); | |
114 | ||
115 | params.extra_ies = wpabuf_head(ies); | |
116 | params.extra_ies_len = wpabuf_len(ies); | |
117 | ||
118 | switch (type) { | |
119 | case P2P_SCAN_SOCIAL: | |
120 | params.freqs = social_channels; | |
121 | break; | |
122 | case P2P_SCAN_FULL: | |
123 | break; | |
124 | case P2P_SCAN_SPECIFIC: | |
125 | social_channels[0] = freq; | |
126 | social_channels[1] = 0; | |
127 | params.freqs = social_channels; | |
128 | break; | |
129 | case P2P_SCAN_SOCIAL_PLUS_ONE: | |
130 | social_channels[3] = freq; | |
131 | params.freqs = social_channels; | |
132 | break; | |
133 | } | |
134 | ||
135 | wpa_s->scan_res_handler = wpas_p2p_scan_res_handler; | |
136 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) | |
137 | ret = ieee80211_sta_req_scan(wpa_s, ¶ms); | |
138 | else | |
139 | ret = wpa_drv_scan(wpa_s, ¶ms); | |
140 | ||
141 | wpabuf_free(ies); | |
142 | ||
143 | return ret; | |
144 | } | |
145 | ||
146 | ||
147 | #ifdef CONFIG_CLIENT_MLME | |
148 | static void p2p_rx_action_mlme(void *ctx, const u8 *buf, size_t len, int freq) | |
149 | { | |
150 | struct wpa_supplicant *wpa_s = ctx; | |
151 | const struct ieee80211_mgmt *mgmt; | |
152 | size_t hdr_len; | |
153 | ||
154 | if (wpa_s->global->p2p_disabled) | |
155 | return; | |
156 | mgmt = (const struct ieee80211_mgmt *) buf; | |
157 | hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf; | |
158 | if (hdr_len > len) | |
159 | return; | |
160 | p2p_rx_action(wpa_s->global->p2p, mgmt->da, mgmt->sa, mgmt->bssid, | |
161 | mgmt->u.action.category, | |
162 | &mgmt->u.action.u.vs_public_action.action, | |
163 | len - hdr_len, freq); | |
164 | } | |
165 | #endif /* CONFIG_CLIENT_MLME */ | |
166 | ||
167 | ||
168 | static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface) | |
169 | { | |
170 | switch (p2p_group_interface) { | |
171 | case P2P_GROUP_INTERFACE_PENDING: | |
172 | return WPA_IF_P2P_GROUP; | |
173 | case P2P_GROUP_INTERFACE_GO: | |
174 | return WPA_IF_P2P_GO; | |
175 | case P2P_GROUP_INTERFACE_CLIENT: | |
176 | return WPA_IF_P2P_CLIENT; | |
177 | } | |
178 | ||
179 | return WPA_IF_P2P_GROUP; | |
180 | } | |
181 | ||
182 | ||
183 | static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s) | |
184 | { | |
185 | struct wpa_ssid *ssid; | |
186 | char *gtype; | |
3071e181 JM |
187 | const char *reason; |
188 | ||
189 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); | |
b22128ef JM |
190 | |
191 | ssid = wpa_s->current_ssid; | |
192 | if (ssid == NULL) { | |
193 | /* | |
194 | * The current SSID was not known, but there may still be a | |
195 | * pending P2P group interface waiting for provisioning. | |
196 | */ | |
197 | ssid = wpa_s->conf->ssid; | |
198 | while (ssid) { | |
199 | if (ssid->p2p_group && | |
200 | (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || | |
201 | (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) | |
202 | break; | |
203 | ssid = ssid->next; | |
204 | } | |
205 | } | |
206 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO) | |
207 | gtype = "GO"; | |
208 | else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT || | |
209 | (ssid && ssid->mode == WPAS_MODE_INFRA)) { | |
210 | wpa_s->reassociate = 0; | |
211 | wpa_s->disconnected = 1; | |
212 | wpa_supplicant_deauthenticate(wpa_s, | |
213 | WLAN_REASON_DEAUTH_LEAVING); | |
214 | gtype = "client"; | |
215 | } else | |
216 | gtype = "GO"; | |
72044390 JM |
217 | if (wpa_s->cross_connect_in_use) { |
218 | wpa_s->cross_connect_in_use = 0; | |
219 | wpa_msg(wpa_s->parent, MSG_INFO, | |
220 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", | |
221 | wpa_s->ifname, wpa_s->cross_connect_uplink); | |
222 | } | |
3071e181 JM |
223 | switch (wpa_s->removal_reason) { |
224 | case P2P_GROUP_REMOVAL_REQUESTED: | |
225 | reason = " reason=REQUESTED"; | |
226 | break; | |
227 | case P2P_GROUP_REMOVAL_IDLE_TIMEOUT: | |
228 | reason = " reason=IDLE"; | |
229 | break; | |
c973f386 JM |
230 | case P2P_GROUP_REMOVAL_UNAVAILABLE: |
231 | reason = " reason=UNAVAILABLE"; | |
232 | break; | |
3071e181 JM |
233 | default: |
234 | reason = ""; | |
235 | break; | |
236 | } | |
237 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s", | |
238 | wpa_s->ifname, gtype, reason); | |
b22128ef JM |
239 | if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { |
240 | struct wpa_global *global; | |
241 | char *ifname; | |
242 | enum wpa_driver_if_type type; | |
243 | wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s", | |
244 | wpa_s->ifname); | |
245 | global = wpa_s->global; | |
246 | ifname = os_strdup(wpa_s->ifname); | |
247 | type = wpas_p2p_if_type(wpa_s->p2p_group_interface); | |
248 | wpa_supplicant_remove_iface(wpa_s->global, wpa_s); | |
249 | wpa_s = global->ifaces; | |
250 | if (wpa_s && ifname) | |
251 | wpa_drv_if_remove(wpa_s, type, ifname); | |
252 | os_free(ifname); | |
253 | return; | |
254 | } | |
255 | ||
256 | wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network"); | |
257 | if (ssid && (ssid->p2p_group || | |
258 | ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || | |
259 | (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) { | |
260 | int id = ssid->id; | |
261 | if (ssid == wpa_s->current_ssid) | |
262 | wpa_s->current_ssid = NULL; | |
263 | wpas_notify_network_removed(wpa_s, ssid); | |
264 | wpa_config_remove_network(wpa_s->conf, id); | |
265 | wpa_supplicant_clear_status(wpa_s); | |
266 | } else { | |
267 | wpa_printf(MSG_DEBUG, "P2P: Temporary group network not " | |
268 | "found"); | |
269 | } | |
270 | wpa_supplicant_ap_deinit(wpa_s); | |
271 | } | |
272 | ||
273 | ||
274 | static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s, | |
275 | u8 *go_dev_addr, | |
276 | const u8 *ssid, size_t ssid_len) | |
277 | { | |
278 | struct wpa_bss *bss; | |
279 | const u8 *bssid; | |
280 | struct wpabuf *p2p; | |
281 | u8 group_capab; | |
282 | const u8 *addr; | |
283 | ||
284 | if (wpa_s->go_params) | |
285 | bssid = wpa_s->go_params->peer_interface_addr; | |
286 | else | |
287 | bssid = wpa_s->bssid; | |
288 | ||
289 | bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len); | |
290 | if (bss == NULL) { | |
291 | u8 iface_addr[ETH_ALEN]; | |
292 | if (p2p_get_interface_addr(wpa_s->global->p2p, bssid, | |
293 | iface_addr) == 0) | |
294 | bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len); | |
295 | } | |
296 | if (bss == NULL) { | |
297 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " | |
298 | "group is persistent - BSS " MACSTR " not found", | |
299 | MAC2STR(bssid)); | |
300 | return 0; | |
301 | } | |
302 | ||
303 | p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); | |
304 | if (p2p == NULL) { | |
305 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " | |
306 | "group is persistent - BSS " MACSTR | |
307 | " did not include P2P IE", MAC2STR(bssid)); | |
308 | wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs", | |
309 | (u8 *) (bss + 1), bss->ie_len); | |
310 | wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs", | |
311 | ((u8 *) bss + 1) + bss->ie_len, | |
312 | bss->beacon_ie_len); | |
313 | return 0; | |
314 | } | |
315 | ||
316 | group_capab = p2p_get_group_capab(p2p); | |
317 | addr = p2p_get_go_dev_addr(p2p); | |
318 | wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: " | |
319 | "group_capab=0x%x", group_capab); | |
320 | if (addr) { | |
321 | os_memcpy(go_dev_addr, addr, ETH_ALEN); | |
322 | wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR, | |
323 | MAC2STR(addr)); | |
324 | } else | |
325 | os_memset(go_dev_addr, 0, ETH_ALEN); | |
326 | wpabuf_free(p2p); | |
327 | ||
328 | wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x " | |
329 | "go_dev_addr=" MACSTR, | |
330 | MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr)); | |
331 | ||
332 | return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP; | |
333 | } | |
334 | ||
335 | ||
336 | static void wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s, | |
337 | struct wpa_ssid *ssid, | |
338 | const u8 *go_dev_addr) | |
339 | { | |
340 | struct wpa_ssid *s; | |
341 | int changed = 0; | |
342 | ||
343 | wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent " | |
344 | "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr)); | |
345 | for (s = wpa_s->conf->ssid; s; s = s->next) { | |
346 | if (s->disabled == 2 && | |
347 | os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 && | |
348 | s->ssid_len == ssid->ssid_len && | |
349 | os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0) | |
350 | break; | |
351 | } | |
352 | ||
353 | if (s) { | |
354 | wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group " | |
355 | "entry"); | |
356 | if (ssid->passphrase && !s->passphrase) | |
357 | changed = 1; | |
358 | else if (ssid->passphrase && s->passphrase && | |
359 | os_strcmp(ssid->passphrase, s->passphrase) != 0) | |
360 | changed = 1; | |
361 | } else { | |
362 | wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group " | |
363 | "entry"); | |
364 | changed = 1; | |
365 | s = wpa_config_add_network(wpa_s->conf); | |
366 | if (s == NULL) | |
367 | return; | |
368 | wpa_config_set_network_defaults(s); | |
369 | } | |
370 | ||
371 | s->p2p_group = 1; | |
372 | s->p2p_persistent_group = 1; | |
373 | s->disabled = 2; | |
374 | s->bssid_set = 1; | |
375 | os_memcpy(s->bssid, go_dev_addr, ETH_ALEN); | |
376 | s->mode = ssid->mode; | |
377 | s->auth_alg = WPA_AUTH_ALG_OPEN; | |
378 | s->key_mgmt = WPA_KEY_MGMT_PSK; | |
379 | s->proto = WPA_PROTO_RSN; | |
380 | s->pairwise_cipher = WPA_CIPHER_CCMP; | |
381 | if (ssid->passphrase) { | |
382 | os_free(s->passphrase); | |
383 | s->passphrase = os_strdup(ssid->passphrase); | |
384 | } | |
385 | if (ssid->psk_set) { | |
386 | s->psk_set = 1; | |
387 | os_memcpy(s->psk, ssid->psk, 32); | |
388 | } | |
389 | if (s->passphrase && !s->psk_set) | |
390 | wpa_config_update_psk(s); | |
391 | if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) { | |
392 | os_free(s->ssid); | |
393 | s->ssid = os_malloc(ssid->ssid_len); | |
394 | } | |
395 | if (s->ssid) { | |
396 | s->ssid_len = ssid->ssid_len; | |
397 | os_memcpy(s->ssid, ssid->ssid, s->ssid_len); | |
398 | } | |
399 | ||
400 | #ifndef CONFIG_NO_CONFIG_WRITE | |
401 | if (changed && wpa_s->conf->update_config && | |
402 | wpa_config_write(wpa_s->confname, wpa_s->conf)) { | |
403 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); | |
404 | } | |
405 | #endif /* CONFIG_NO_CONFIG_WRITE */ | |
406 | } | |
407 | ||
408 | ||
409 | static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s, | |
410 | int success) | |
411 | { | |
412 | struct wpa_ssid *ssid; | |
413 | const char *ssid_txt; | |
414 | int client; | |
415 | int persistent; | |
416 | u8 go_dev_addr[ETH_ALEN]; | |
417 | ||
418 | /* | |
419 | * This callback is likely called for the main interface. Update wpa_s | |
420 | * to use the group interface if a new interface was created for the | |
421 | * group. | |
422 | */ | |
423 | if (wpa_s->global->p2p_group_formation) | |
424 | wpa_s = wpa_s->global->p2p_group_formation; | |
d1b024c9 | 425 | wpa_s->global->p2p_group_formation = NULL; |
b22128ef JM |
426 | wpa_s->p2p_in_provisioning = 0; |
427 | ||
428 | if (!success) { | |
429 | wpa_msg(wpa_s->parent, MSG_INFO, | |
430 | P2P_EVENT_GROUP_FORMATION_FAILURE); | |
431 | wpas_p2p_group_delete(wpa_s); | |
432 | return; | |
433 | } | |
434 | ||
435 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS); | |
436 | ||
437 | ssid = wpa_s->current_ssid; | |
438 | if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { | |
439 | ssid->mode = WPAS_MODE_P2P_GO; | |
440 | p2p_group_notif_formation_done(wpa_s->p2p_group); | |
441 | wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL); | |
442 | } | |
443 | ||
444 | persistent = 0; | |
445 | if (ssid) { | |
446 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); | |
447 | client = ssid->mode == WPAS_MODE_INFRA; | |
448 | if (ssid->mode == WPAS_MODE_P2P_GO) { | |
449 | persistent = ssid->p2p_persistent_group; | |
450 | os_memcpy(go_dev_addr, wpa_s->parent->own_addr, | |
451 | ETH_ALEN); | |
452 | } else | |
453 | persistent = wpas_p2p_persistent_group(wpa_s, | |
454 | go_dev_addr, | |
455 | ssid->ssid, | |
456 | ssid->ssid_len); | |
457 | } else { | |
458 | ssid_txt = ""; | |
459 | client = wpa_s->p2p_group_interface == | |
460 | P2P_GROUP_INTERFACE_CLIENT; | |
461 | } | |
462 | ||
463 | wpa_s->show_group_started = 0; | |
464 | if (client) { | |
465 | /* | |
466 | * Indicate event only after successfully completed 4-way | |
467 | * handshake, i.e., when the interface is ready for data | |
468 | * packets. | |
469 | */ | |
470 | wpa_s->show_group_started = 1; | |
471 | } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) { | |
472 | char psk[65]; | |
473 | wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); | |
474 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED | |
c481048f AC |
475 | "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR |
476 | "%s", | |
477 | wpa_s->ifname, ssid_txt, ssid->frequency, psk, | |
478 | MAC2STR(go_dev_addr), | |
b22128ef | 479 | persistent ? " [PERSISTENT]" : ""); |
72044390 | 480 | wpas_p2p_cross_connect_setup(wpa_s); |
3071e181 | 481 | wpas_p2p_set_group_idle_timeout(wpa_s); |
b22128ef JM |
482 | } else { |
483 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED | |
c481048f AC |
484 | "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" " |
485 | "go_dev_addr=" MACSTR "%s", | |
d394a22f | 486 | wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0, |
b22128ef JM |
487 | ssid && ssid->passphrase ? ssid->passphrase : "", |
488 | MAC2STR(go_dev_addr), | |
489 | persistent ? " [PERSISTENT]" : ""); | |
72044390 | 490 | wpas_p2p_cross_connect_setup(wpa_s); |
3071e181 | 491 | wpas_p2p_set_group_idle_timeout(wpa_s); |
b22128ef JM |
492 | } |
493 | ||
494 | if (persistent) | |
495 | wpas_p2p_store_persistent_group(wpa_s->parent, ssid, | |
496 | go_dev_addr); | |
497 | } | |
498 | ||
499 | ||
534525ff JM |
500 | static struct wpa_supplicant * |
501 | wpas_get_tx_interface(struct wpa_supplicant *wpa_s, const u8 *src) | |
502 | { | |
503 | struct wpa_supplicant *iface; | |
504 | ||
505 | if (os_memcmp(src, wpa_s->own_addr, ETH_ALEN) == 0) | |
506 | return wpa_s; | |
507 | ||
508 | /* | |
509 | * Try to find a group interface that matches with the source address. | |
510 | */ | |
511 | iface = wpa_s->global->ifaces; | |
512 | while (iface) { | |
513 | if (os_memcmp(wpa_s->pending_action_src, | |
514 | iface->own_addr, ETH_ALEN) == 0) | |
515 | break; | |
516 | iface = iface->next; | |
517 | } | |
518 | if (iface) { | |
519 | wpa_printf(MSG_DEBUG, "P2P: Use group interface %s " | |
520 | "instead of interface %s for Action TX", | |
521 | iface->ifname, wpa_s->ifname); | |
522 | return iface; | |
523 | } | |
524 | ||
525 | return wpa_s; | |
526 | } | |
527 | ||
528 | ||
b22128ef JM |
529 | static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx) |
530 | { | |
531 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
532 | struct wpa_supplicant *iface; | |
533 | int res; | |
534 | int without_roc; | |
535 | ||
536 | without_roc = wpa_s->pending_action_without_roc; | |
537 | wpa_s->pending_action_without_roc = 0; | |
ab218b7c JM |
538 | wpa_printf(MSG_DEBUG, "P2P: Send Action callback (without_roc=%d " |
539 | "pending_action_tx=%p)", | |
540 | without_roc, wpa_s->pending_action_tx); | |
b22128ef JM |
541 | |
542 | if (wpa_s->pending_action_tx == NULL) | |
543 | return; | |
544 | ||
534525ff JM |
545 | /* |
546 | * This call is likely going to be on the P2P device instance if the | |
547 | * driver uses a separate interface for that purpose. However, some | |
548 | * Action frames are actually sent within a P2P Group and when that is | |
549 | * the case, we need to follow power saving (e.g., GO buffering the | |
550 | * frame for a client in PS mode or a client following the advertised | |
551 | * NoA from its GO). To make that easier for the driver, select the | |
552 | * correct group interface here. | |
553 | */ | |
554 | iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src); | |
555 | ||
b22128ef | 556 | if (wpa_s->off_channel_freq != wpa_s->pending_action_freq && |
534525ff JM |
557 | wpa_s->pending_action_freq != 0 && |
558 | wpa_s->pending_action_freq != iface->assoc_freq) { | |
b22128ef | 559 | wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX " |
534525ff JM |
560 | "waiting for another freq=%u (off_channel_freq=%u " |
561 | "assoc_freq=%u)", | |
b22128ef | 562 | wpa_s->pending_action_freq, |
534525ff JM |
563 | wpa_s->off_channel_freq, |
564 | iface->assoc_freq); | |
b22128ef JM |
565 | if (without_roc && wpa_s->off_channel_freq == 0) { |
566 | /* | |
567 | * We may get here if wpas_send_action() found us to be | |
568 | * on the correct channel, but remain-on-channel cancel | |
569 | * event was received before getting here. | |
570 | */ | |
571 | wpa_printf(MSG_DEBUG, "P2P: Schedule " | |
572 | "remain-on-channel to send Action frame"); | |
573 | if (wpa_drv_remain_on_channel( | |
574 | wpa_s, wpa_s->pending_action_freq, 200) < | |
575 | 0) { | |
576 | wpa_printf(MSG_DEBUG, "P2P: Failed to request " | |
577 | "driver to remain on channel (%u " | |
578 | "MHz) for Action Frame TX", | |
579 | wpa_s->pending_action_freq); | |
09d660b9 JM |
580 | } else { |
581 | wpa_s->off_channel_freq = 0; | |
07a30a66 JM |
582 | wpa_s->roc_waiting_drv_freq = |
583 | wpa_s->pending_action_freq; | |
09d660b9 | 584 | } |
b22128ef JM |
585 | } |
586 | return; | |
587 | } | |
588 | ||
b22128ef JM |
589 | wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to " |
590 | MACSTR " using interface %s", | |
591 | MAC2STR(wpa_s->pending_action_dst), iface->ifname); | |
592 | res = wpa_drv_send_action(iface, wpa_s->pending_action_freq, | |
593 | wpa_s->pending_action_dst, | |
594 | wpa_s->pending_action_src, | |
595 | wpa_s->pending_action_bssid, | |
596 | wpabuf_head(wpa_s->pending_action_tx), | |
597 | wpabuf_len(wpa_s->pending_action_tx)); | |
598 | if (res) { | |
599 | wpa_printf(MSG_DEBUG, "P2P: Failed to send the pending " | |
600 | "Action frame"); | |
601 | /* | |
602 | * Use fake TX status event to allow P2P state machine to | |
603 | * continue. | |
604 | */ | |
605 | wpas_send_action_tx_status( | |
606 | wpa_s, wpa_s->pending_action_dst, | |
607 | wpabuf_head(wpa_s->pending_action_tx), | |
93b7ddd0 JM |
608 | wpabuf_len(wpa_s->pending_action_tx), |
609 | P2P_SEND_ACTION_FAILED); | |
b22128ef JM |
610 | } |
611 | } | |
612 | ||
613 | ||
614 | void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst, | |
93b7ddd0 JM |
615 | const u8 *data, size_t data_len, |
616 | enum p2p_send_action_result result) | |
b22128ef JM |
617 | { |
618 | if (wpa_s->global->p2p_disabled) | |
619 | return; | |
620 | ||
621 | if (wpa_s->pending_action_tx == NULL) { | |
622 | wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - no " | |
623 | "pending operation"); | |
624 | return; | |
625 | } | |
626 | ||
627 | if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) { | |
628 | wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - unknown " | |
629 | "destination address"); | |
630 | return; | |
631 | } | |
632 | ||
633 | wpabuf_free(wpa_s->pending_action_tx); | |
634 | wpa_s->pending_action_tx = NULL; | |
635 | ||
636 | p2p_send_action_cb(wpa_s->global->p2p, wpa_s->pending_action_freq, | |
637 | wpa_s->pending_action_dst, | |
638 | wpa_s->pending_action_src, | |
639 | wpa_s->pending_action_bssid, | |
93b7ddd0 | 640 | result); |
b22128ef JM |
641 | |
642 | if (wpa_s->pending_pd_before_join && | |
643 | (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr, | |
644 | ETH_ALEN) == 0 || | |
645 | os_memcmp(wpa_s->pending_action_dst, | |
646 | wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { | |
647 | wpa_s->pending_pd_before_join = 0; | |
648 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " | |
649 | "join-existing-group operation"); | |
650 | wpas_p2p_join_start(wpa_s); | |
651 | } | |
652 | } | |
653 | ||
654 | ||
655 | static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst, | |
656 | const u8 *src, const u8 *bssid, const u8 *buf, | |
657 | size_t len, unsigned int wait_time) | |
658 | { | |
659 | struct wpa_supplicant *wpa_s = ctx; | |
660 | ||
661 | wpa_printf(MSG_DEBUG, "P2P: Send action frame: freq=%d dst=" MACSTR | |
ab218b7c JM |
662 | " src=" MACSTR " bssid=" MACSTR " len=%d", |
663 | freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid), | |
664 | (int) len); | |
b22128ef JM |
665 | |
666 | if (wpa_s->pending_action_tx) { | |
667 | wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX " | |
668 | "to " MACSTR, MAC2STR(wpa_s->pending_action_dst)); | |
669 | wpabuf_free(wpa_s->pending_action_tx); | |
670 | } | |
671 | wpa_s->pending_action_tx = wpabuf_alloc(len); | |
ab218b7c JM |
672 | if (wpa_s->pending_action_tx == NULL) { |
673 | wpa_printf(MSG_DEBUG, "P2P: Failed to allocate Action frame " | |
674 | "TX buffer (len=%llu)", (unsigned long long) len); | |
b22128ef | 675 | return -1; |
ab218b7c | 676 | } |
b22128ef JM |
677 | wpabuf_put_data(wpa_s->pending_action_tx, buf, len); |
678 | os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN); | |
679 | os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN); | |
680 | os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN); | |
681 | wpa_s->pending_action_freq = freq; | |
682 | ||
534525ff JM |
683 | if (freq) { |
684 | struct wpa_supplicant *tx_iface; | |
685 | tx_iface = wpas_get_tx_interface(wpa_s, src); | |
686 | if (tx_iface->assoc_freq == freq) { | |
687 | wpa_printf(MSG_DEBUG, "P2P: Already on requested " | |
688 | "channel (TX interface operating channel)"); | |
689 | freq = 0; | |
690 | } | |
691 | } | |
692 | ||
b22128ef | 693 | if (wpa_s->off_channel_freq == freq || freq == 0) { |
ab218b7c JM |
694 | wpa_printf(MSG_DEBUG, "P2P: Already on requested channel; " |
695 | "send Action frame immediately"); | |
b22128ef JM |
696 | /* TODO: Would there ever be need to extend the current |
697 | * duration on the channel? */ | |
698 | wpa_s->pending_action_without_roc = 1; | |
699 | eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL); | |
700 | eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL); | |
701 | return 0; | |
702 | } | |
703 | wpa_s->pending_action_without_roc = 0; | |
704 | ||
07a30a66 JM |
705 | if (wpa_s->roc_waiting_drv_freq == freq) { |
706 | wpa_printf(MSG_DEBUG, "P2P: Already waiting for driver to get " | |
707 | "to frequency %u MHz; continue waiting to send the " | |
708 | "Action frame", freq); | |
709 | return 0; | |
710 | } | |
711 | ||
b22128ef JM |
712 | wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted " |
713 | "once the driver gets to the requested channel"); | |
714 | if (wait_time > wpa_s->max_remain_on_chan) | |
715 | wait_time = wpa_s->max_remain_on_chan; | |
716 | if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) { | |
717 | wpa_printf(MSG_DEBUG, "P2P: Failed to request driver " | |
718 | "to remain on channel (%u MHz) for Action " | |
719 | "Frame TX", freq); | |
720 | return -1; | |
721 | } | |
09d660b9 | 722 | wpa_s->off_channel_freq = 0; |
07a30a66 | 723 | wpa_s->roc_waiting_drv_freq = freq; |
b22128ef JM |
724 | |
725 | return 0; | |
726 | } | |
727 | ||
728 | ||
729 | static void wpas_send_action_done(void *ctx) | |
730 | { | |
731 | struct wpa_supplicant *wpa_s = ctx; | |
732 | wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification"); | |
733 | wpabuf_free(wpa_s->pending_action_tx); | |
734 | wpa_s->pending_action_tx = NULL; | |
8eada5ca | 735 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
b22128ef JM |
736 | wpa_drv_cancel_remain_on_channel(wpa_s); |
737 | wpa_s->off_channel_freq = 0; | |
07a30a66 | 738 | wpa_s->roc_waiting_drv_freq = 0; |
b22128ef JM |
739 | } |
740 | } | |
741 | ||
742 | ||
743 | static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s, | |
744 | struct p2p_go_neg_results *params) | |
745 | { | |
746 | if (wpa_s->go_params == NULL) { | |
747 | wpa_s->go_params = os_malloc(sizeof(*params)); | |
748 | if (wpa_s->go_params == NULL) | |
749 | return -1; | |
750 | } | |
751 | os_memcpy(wpa_s->go_params, params, sizeof(*params)); | |
752 | return 0; | |
753 | } | |
754 | ||
755 | ||
756 | static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s, | |
757 | struct p2p_go_neg_results *res) | |
758 | { | |
3b29972c JM |
759 | wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR, |
760 | MAC2STR(res->peer_interface_addr)); | |
e9a7ae41 JM |
761 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID", |
762 | res->ssid, res->ssid_len); | |
b22128ef JM |
763 | wpa_supplicant_ap_deinit(wpa_s); |
764 | wpas_copy_go_neg_results(wpa_s, res); | |
765 | if (res->wps_method == WPS_PBC) | |
3b29972c | 766 | wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1); |
3c5126a4 JM |
767 | else { |
768 | u16 dev_pw_id = DEV_PW_DEFAULT; | |
769 | if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD) | |
770 | dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED; | |
b22128ef | 771 | wpas_wps_start_pin(wpa_s, res->peer_interface_addr, |
3c5126a4 JM |
772 | wpa_s->p2p_pin, 1, dev_pw_id); |
773 | } | |
b22128ef JM |
774 | } |
775 | ||
776 | ||
777 | static void p2p_go_configured(void *ctx, void *data) | |
778 | { | |
779 | struct wpa_supplicant *wpa_s = ctx; | |
780 | struct p2p_go_neg_results *params = data; | |
781 | struct wpa_ssid *ssid; | |
782 | ||
783 | ssid = wpa_s->current_ssid; | |
784 | if (ssid && ssid->mode == WPAS_MODE_P2P_GO) { | |
785 | wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning"); | |
d1b024c9 JM |
786 | if (wpa_s->global->p2p_group_formation == wpa_s) |
787 | wpa_s->global->p2p_group_formation = NULL; | |
b22128ef | 788 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
c481048f AC |
789 | "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" " |
790 | "go_dev_addr=" MACSTR "%s", | |
b22128ef JM |
791 | wpa_s->ifname, |
792 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len), | |
c481048f | 793 | ssid->frequency, |
b22128ef JM |
794 | params->passphrase ? params->passphrase : "", |
795 | MAC2STR(wpa_s->parent->own_addr), | |
796 | params->persistent_group ? " [PERSISTENT]" : ""); | |
797 | if (params->persistent_group) | |
798 | wpas_p2p_store_persistent_group( | |
799 | wpa_s->parent, ssid, | |
800 | wpa_s->parent->own_addr); | |
72044390 | 801 | wpas_p2p_cross_connect_setup(wpa_s); |
3071e181 | 802 | wpas_p2p_set_group_idle_timeout(wpa_s); |
b22128ef JM |
803 | return; |
804 | } | |
805 | ||
806 | wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning"); | |
807 | if (wpa_supplicant_ap_mac_addr_filter(wpa_s, | |
808 | params->peer_interface_addr)) { | |
809 | wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address " | |
810 | "filtering"); | |
811 | return; | |
812 | } | |
813 | if (params->wps_method == WPS_PBC) | |
814 | wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr); | |
815 | else if (wpa_s->p2p_pin[0]) | |
816 | wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr, | |
817 | wpa_s->p2p_pin, NULL, 0); | |
818 | os_free(wpa_s->go_params); | |
819 | wpa_s->go_params = NULL; | |
820 | } | |
821 | ||
822 | ||
823 | static void wpas_start_wps_go(struct wpa_supplicant *wpa_s, | |
824 | struct p2p_go_neg_results *params, | |
825 | int group_formation) | |
826 | { | |
827 | struct wpa_ssid *ssid; | |
828 | ||
829 | if (wpas_copy_go_neg_results(wpa_s, params) < 0) | |
830 | return; | |
831 | ||
832 | ssid = wpa_config_add_network(wpa_s->conf); | |
833 | if (ssid == NULL) | |
834 | return; | |
835 | ||
01d92811 | 836 | wpas_notify_network_added(wpa_s, ssid); |
b22128ef JM |
837 | wpa_config_set_network_defaults(ssid); |
838 | ssid->temporary = 1; | |
839 | ssid->p2p_group = 1; | |
840 | ssid->p2p_persistent_group = params->persistent_group; | |
841 | ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION : | |
842 | WPAS_MODE_P2P_GO; | |
843 | ssid->frequency = params->freq; | |
844 | ssid->ssid = os_zalloc(params->ssid_len + 1); | |
845 | if (ssid->ssid) { | |
846 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); | |
847 | ssid->ssid_len = params->ssid_len; | |
848 | } | |
849 | ssid->auth_alg = WPA_AUTH_ALG_OPEN; | |
850 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; | |
851 | ssid->proto = WPA_PROTO_RSN; | |
852 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; | |
853 | ssid->passphrase = os_strdup(params->passphrase); | |
854 | ||
855 | wpa_s->ap_configured_cb = p2p_go_configured; | |
856 | wpa_s->ap_configured_cb_ctx = wpa_s; | |
857 | wpa_s->ap_configured_cb_data = wpa_s->go_params; | |
858 | wpa_s->connect_without_scan = 1; | |
859 | wpa_s->reassociate = 1; | |
860 | wpa_s->disconnected = 0; | |
861 | wpa_supplicant_req_scan(wpa_s, 0, 0); | |
862 | } | |
863 | ||
864 | ||
865 | static void wpas_p2p_clone_config(struct wpa_supplicant *dst, | |
866 | const struct wpa_supplicant *src) | |
867 | { | |
868 | struct wpa_config *d; | |
869 | const struct wpa_config *s; | |
870 | ||
871 | d = dst->conf; | |
872 | s = src->conf; | |
873 | ||
874 | #define C(n) if (s->n) d->n = os_strdup(s->n) | |
875 | C(device_name); | |
876 | C(manufacturer); | |
877 | C(model_name); | |
878 | C(model_number); | |
879 | C(serial_number); | |
880 | C(device_type); | |
881 | C(config_methods); | |
882 | #undef C | |
3071e181 JM |
883 | |
884 | d->p2p_group_idle = s->p2p_group_idle; | |
b029bd33 | 885 | d->p2p_intra_bss = s->p2p_intra_bss; |
b22128ef JM |
886 | } |
887 | ||
888 | ||
889 | static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, | |
890 | enum wpa_driver_if_type type) | |
891 | { | |
892 | char ifname[120], force_ifname[120]; | |
893 | ||
894 | if (wpa_s->pending_interface_name[0]) { | |
895 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists " | |
896 | "- skip creation of a new one"); | |
897 | if (is_zero_ether_addr(wpa_s->pending_interface_addr)) { | |
898 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual address " | |
899 | "unknown?! ifname='%s'", | |
900 | wpa_s->pending_interface_name); | |
901 | return -1; | |
902 | } | |
903 | return 0; | |
904 | } | |
905 | ||
906 | os_snprintf(ifname, sizeof(ifname), "%s-p2p-%d", wpa_s->ifname, | |
907 | wpa_s->p2p_group_idx); | |
908 | force_ifname[0] = '\0'; | |
909 | ||
910 | wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group", | |
911 | ifname); | |
912 | wpa_s->p2p_group_idx++; | |
913 | ||
914 | wpa_s->pending_interface_type = type; | |
915 | if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname, | |
916 | wpa_s->pending_interface_addr) < 0) { | |
917 | wpa_printf(MSG_ERROR, "P2P: Failed to create new group " | |
918 | "interface"); | |
919 | return -1; | |
920 | } | |
921 | ||
922 | if (force_ifname[0]) { | |
923 | wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s", | |
924 | force_ifname); | |
925 | os_strlcpy(wpa_s->pending_interface_name, force_ifname, | |
926 | sizeof(wpa_s->pending_interface_name)); | |
927 | } else | |
928 | os_strlcpy(wpa_s->pending_interface_name, ifname, | |
929 | sizeof(wpa_s->pending_interface_name)); | |
930 | wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr " | |
931 | MACSTR, wpa_s->pending_interface_name, | |
932 | MAC2STR(wpa_s->pending_interface_addr)); | |
933 | ||
934 | return 0; | |
935 | } | |
936 | ||
937 | ||
938 | static void wpas_p2p_remove_pending_group_interface( | |
939 | struct wpa_supplicant *wpa_s) | |
940 | { | |
941 | if (!wpa_s->pending_interface_name[0] || | |
942 | is_zero_ether_addr(wpa_s->pending_interface_addr)) | |
943 | return; /* No pending virtual interface */ | |
944 | ||
945 | wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s", | |
946 | wpa_s->pending_interface_name); | |
947 | wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type, | |
948 | wpa_s->pending_interface_name); | |
949 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); | |
950 | wpa_s->pending_interface_name[0] = '\0'; | |
951 | } | |
952 | ||
953 | ||
954 | static struct wpa_supplicant * | |
955 | wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go) | |
956 | { | |
957 | struct wpa_interface iface; | |
958 | struct wpa_supplicant *group_wpa_s; | |
959 | ||
960 | if (!wpa_s->pending_interface_name[0]) { | |
961 | wpa_printf(MSG_ERROR, "P2P: No pending group interface"); | |
d75e8806 JM |
962 | if (!wpas_p2p_create_iface(wpa_s)) |
963 | return NULL; | |
964 | /* | |
965 | * Something has forced us to remove the pending interface; try | |
966 | * to create a new one and hope for the best that we will get | |
967 | * the same local address. | |
968 | */ | |
969 | if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : | |
970 | WPA_IF_P2P_CLIENT) < 0) | |
971 | return NULL; | |
b22128ef JM |
972 | } |
973 | ||
974 | os_memset(&iface, 0, sizeof(iface)); | |
975 | iface.ifname = wpa_s->pending_interface_name; | |
976 | iface.driver = wpa_s->driver->name; | |
977 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; | |
978 | iface.driver_param = wpa_s->conf->driver_param; | |
979 | group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface); | |
980 | if (group_wpa_s == NULL) { | |
981 | wpa_printf(MSG_ERROR, "P2P: Failed to create new " | |
982 | "wpa_supplicant interface"); | |
983 | return NULL; | |
984 | } | |
985 | wpa_s->pending_interface_name[0] = '\0'; | |
986 | group_wpa_s->parent = wpa_s; | |
987 | group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO : | |
988 | P2P_GROUP_INTERFACE_CLIENT; | |
989 | wpa_s->global->p2p_group_formation = group_wpa_s; | |
990 | ||
991 | wpas_p2p_clone_config(group_wpa_s, wpa_s); | |
992 | ||
993 | return group_wpa_s; | |
994 | } | |
995 | ||
996 | ||
997 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, | |
998 | void *timeout_ctx) | |
999 | { | |
1000 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
1001 | wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out"); | |
1002 | if (wpa_s->global->p2p) | |
1003 | p2p_group_formation_failed(wpa_s->global->p2p); | |
1004 | wpas_group_formation_completed(wpa_s, 0); | |
1005 | } | |
1006 | ||
1007 | ||
1008 | void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res) | |
1009 | { | |
1010 | struct wpa_supplicant *wpa_s = ctx; | |
1011 | ||
8eada5ca | 1012 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
b22128ef JM |
1013 | wpa_drv_cancel_remain_on_channel(wpa_s); |
1014 | wpa_s->off_channel_freq = 0; | |
07a30a66 | 1015 | wpa_s->roc_waiting_drv_freq = 0; |
b22128ef JM |
1016 | } |
1017 | ||
1018 | if (res->status) { | |
1019 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d", | |
1020 | res->status); | |
1021 | wpas_p2p_remove_pending_group_interface(wpa_s); | |
1022 | return; | |
1023 | } | |
1024 | ||
1025 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS); | |
1026 | ||
1027 | if (wpa_s->create_p2p_iface) { | |
1028 | struct wpa_supplicant *group_wpa_s = | |
1029 | wpas_p2p_init_group_interface(wpa_s, res->role_go); | |
1030 | if (group_wpa_s == NULL) { | |
1031 | wpas_p2p_remove_pending_group_interface(wpa_s); | |
1032 | return; | |
1033 | } | |
3c5126a4 | 1034 | if (group_wpa_s != wpa_s) { |
b22128ef JM |
1035 | os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin, |
1036 | sizeof(group_wpa_s->p2p_pin)); | |
3c5126a4 JM |
1037 | group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method; |
1038 | } | |
b22128ef JM |
1039 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
1040 | wpa_s->pending_interface_name[0] = '\0'; | |
1041 | group_wpa_s->p2p_in_provisioning = 1; | |
1042 | ||
1043 | if (res->role_go) | |
1044 | wpas_start_wps_go(group_wpa_s, res, 1); | |
1045 | else | |
1046 | wpas_start_wps_enrollee(group_wpa_s, res); | |
1047 | } else { | |
1048 | wpa_s->p2p_in_provisioning = 1; | |
1049 | wpa_s->global->p2p_group_formation = wpa_s; | |
1050 | ||
1051 | if (res->role_go) | |
1052 | wpas_start_wps_go(wpa_s, res, 1); | |
1053 | else | |
1054 | wpas_start_wps_enrollee(ctx, res); | |
1055 | } | |
1056 | ||
1057 | wpa_s->p2p_long_listen = 0; | |
1058 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); | |
1059 | ||
1060 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); | |
ae3e3421 JM |
1061 | eloop_register_timeout(15 + res->peer_config_timeout / 100, |
1062 | (res->peer_config_timeout % 100) * 10000, | |
1063 | wpas_p2p_group_formation_timeout, wpa_s, NULL); | |
b22128ef JM |
1064 | } |
1065 | ||
1066 | ||
3dfda83d | 1067 | void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id) |
b22128ef JM |
1068 | { |
1069 | struct wpa_supplicant *wpa_s = ctx; | |
3dfda83d JM |
1070 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR |
1071 | " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id); | |
b22128ef JM |
1072 | } |
1073 | ||
1074 | ||
1075 | void wpas_dev_found(void *ctx, const u8 *addr, const u8 *dev_addr, | |
1076 | const u8 *pri_dev_type, const char *dev_name, | |
1077 | u16 config_methods, u8 dev_capab, u8 group_capab) | |
1078 | { | |
1079 | struct wpa_supplicant *wpa_s = ctx; | |
1080 | char devtype[WPS_DEV_TYPE_BUFSIZE]; | |
1081 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR | |
1082 | " p2p_dev_addr=" MACSTR | |
1083 | " pri_dev_type=%s name='%s' config_methods=0x%x " | |
1084 | "dev_capab=0x%x group_capab=0x%x", | |
1085 | MAC2STR(addr), MAC2STR(dev_addr), | |
1086 | wps_dev_type_bin2str(pri_dev_type, devtype, sizeof(devtype)), | |
1087 | dev_name, config_methods, dev_capab, group_capab); | |
1088 | } | |
1089 | ||
1090 | ||
1091 | static int wpas_start_listen(void *ctx, unsigned int freq, | |
1092 | unsigned int duration, | |
1093 | const struct wpabuf *probe_resp_ie) | |
1094 | { | |
1095 | struct wpa_supplicant *wpa_s = ctx; | |
1096 | ||
0e2e565a | 1097 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL); |
b22128ef JM |
1098 | |
1099 | if (wpa_drv_probe_req_report(wpa_s, 1) < 0) { | |
1100 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to " | |
1101 | "report received Probe Request frames"); | |
1102 | return -1; | |
1103 | } | |
1104 | ||
1105 | wpa_s->pending_listen_freq = freq; | |
1106 | wpa_s->pending_listen_duration = duration; | |
1107 | ||
1108 | if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) { | |
1109 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver " | |
1110 | "to remain on channel (%u MHz) for Listen " | |
1111 | "state", freq); | |
1112 | wpa_s->pending_listen_freq = 0; | |
1113 | return -1; | |
1114 | } | |
09d660b9 | 1115 | wpa_s->off_channel_freq = 0; |
07a30a66 | 1116 | wpa_s->roc_waiting_drv_freq = freq; |
b22128ef JM |
1117 | |
1118 | return 0; | |
1119 | } | |
1120 | ||
1121 | ||
1122 | static void wpas_stop_listen(void *ctx) | |
1123 | { | |
1124 | struct wpa_supplicant *wpa_s = ctx; | |
6cb22d2f | 1125 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
b22128ef JM |
1126 | wpa_drv_cancel_remain_on_channel(wpa_s); |
1127 | wpa_s->off_channel_freq = 0; | |
07a30a66 | 1128 | wpa_s->roc_waiting_drv_freq = 0; |
b22128ef JM |
1129 | } |
1130 | wpa_drv_probe_req_report(wpa_s, 0); | |
1131 | } | |
1132 | ||
1133 | ||
1134 | static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf) | |
1135 | { | |
1136 | struct wpa_supplicant *wpa_s = ctx; | |
1137 | return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf)); | |
1138 | } | |
1139 | ||
1140 | ||
1141 | static struct p2p_srv_bonjour * | |
1142 | wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s, | |
1143 | const struct wpabuf *query) | |
1144 | { | |
1145 | struct p2p_srv_bonjour *bsrv; | |
1146 | size_t len; | |
1147 | ||
1148 | len = wpabuf_len(query); | |
1149 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, | |
1150 | struct p2p_srv_bonjour, list) { | |
1151 | if (len == wpabuf_len(bsrv->query) && | |
1152 | os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query), | |
1153 | len) == 0) | |
1154 | return bsrv; | |
1155 | } | |
1156 | return NULL; | |
1157 | } | |
1158 | ||
1159 | ||
1160 | static struct p2p_srv_upnp * | |
1161 | wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version, | |
1162 | const char *service) | |
1163 | { | |
1164 | struct p2p_srv_upnp *usrv; | |
1165 | ||
1166 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, | |
1167 | struct p2p_srv_upnp, list) { | |
1168 | if (version == usrv->version && | |
1169 | os_strcmp(service, usrv->service) == 0) | |
1170 | return usrv; | |
1171 | } | |
1172 | return NULL; | |
1173 | } | |
1174 | ||
1175 | ||
1176 | static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto, | |
1177 | u8 srv_trans_id) | |
1178 | { | |
1179 | u8 *len_pos; | |
1180 | ||
1181 | if (wpabuf_tailroom(resp) < 5) | |
1182 | return; | |
1183 | ||
1184 | /* Length (to be filled) */ | |
1185 | len_pos = wpabuf_put(resp, 2); | |
1186 | wpabuf_put_u8(resp, srv_proto); | |
1187 | wpabuf_put_u8(resp, srv_trans_id); | |
1188 | /* Status Code */ | |
1189 | wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE); | |
1190 | /* Response Data: empty */ | |
1191 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); | |
1192 | } | |
1193 | ||
1194 | ||
1195 | static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s, | |
1196 | struct wpabuf *resp, u8 srv_trans_id) | |
1197 | { | |
1198 | struct p2p_srv_bonjour *bsrv; | |
1199 | u8 *len_pos; | |
1200 | ||
1201 | wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services"); | |
1202 | ||
1203 | if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { | |
1204 | wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); | |
1205 | return; | |
1206 | } | |
1207 | ||
1208 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, | |
1209 | struct p2p_srv_bonjour, list) { | |
1210 | if (wpabuf_tailroom(resp) < | |
1211 | 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) | |
1212 | return; | |
1213 | /* Length (to be filled) */ | |
1214 | len_pos = wpabuf_put(resp, 2); | |
1215 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); | |
1216 | wpabuf_put_u8(resp, srv_trans_id); | |
1217 | /* Status Code */ | |
1218 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); | |
1219 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", | |
1220 | wpabuf_head(bsrv->resp), | |
1221 | wpabuf_len(bsrv->resp)); | |
1222 | /* Response Data */ | |
1223 | wpabuf_put_buf(resp, bsrv->query); /* Key */ | |
1224 | wpabuf_put_buf(resp, bsrv->resp); /* Value */ | |
1225 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - | |
1226 | 2); | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | ||
1231 | static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s, | |
1232 | struct wpabuf *resp, u8 srv_trans_id, | |
1233 | const u8 *query, size_t query_len) | |
1234 | { | |
1235 | struct p2p_srv_bonjour *bsrv; | |
1236 | struct wpabuf buf; | |
1237 | u8 *len_pos; | |
1238 | ||
1239 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour", | |
1240 | query, query_len); | |
1241 | if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { | |
1242 | wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); | |
1243 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR, | |
1244 | srv_trans_id); | |
1245 | return; | |
1246 | } | |
1247 | ||
1248 | if (query_len == 0) { | |
1249 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); | |
1250 | return; | |
1251 | } | |
1252 | ||
1253 | if (wpabuf_tailroom(resp) < 5) | |
1254 | return; | |
1255 | /* Length (to be filled) */ | |
1256 | len_pos = wpabuf_put(resp, 2); | |
1257 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); | |
1258 | wpabuf_put_u8(resp, srv_trans_id); | |
1259 | ||
1260 | wpabuf_set(&buf, query, query_len); | |
1261 | bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf); | |
1262 | if (bsrv == NULL) { | |
1263 | wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not " | |
1264 | "available"); | |
1265 | ||
1266 | /* Status Code */ | |
bf608cad | 1267 | wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); |
b22128ef JM |
1268 | /* Response Data: empty */ |
1269 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - | |
1270 | 2); | |
1271 | return; | |
1272 | } | |
1273 | ||
1274 | /* Status Code */ | |
1275 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); | |
1276 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", | |
1277 | wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp)); | |
1278 | ||
1279 | if (wpabuf_tailroom(resp) >= | |
1280 | wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) { | |
1281 | /* Response Data */ | |
1282 | wpabuf_put_buf(resp, bsrv->query); /* Key */ | |
1283 | wpabuf_put_buf(resp, bsrv->resp); /* Value */ | |
1284 | } | |
1285 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); | |
1286 | } | |
1287 | ||
1288 | ||
1289 | static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s, | |
1290 | struct wpabuf *resp, u8 srv_trans_id) | |
1291 | { | |
1292 | struct p2p_srv_upnp *usrv; | |
1293 | u8 *len_pos; | |
1294 | ||
1295 | wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services"); | |
1296 | ||
1297 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { | |
1298 | wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); | |
1299 | return; | |
1300 | } | |
1301 | ||
1302 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, | |
1303 | struct p2p_srv_upnp, list) { | |
1304 | if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service)) | |
1305 | return; | |
1306 | ||
1307 | /* Length (to be filled) */ | |
1308 | len_pos = wpabuf_put(resp, 2); | |
1309 | wpabuf_put_u8(resp, P2P_SERV_UPNP); | |
1310 | wpabuf_put_u8(resp, srv_trans_id); | |
1311 | ||
1312 | /* Status Code */ | |
1313 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); | |
1314 | /* Response Data */ | |
1315 | wpabuf_put_u8(resp, usrv->version); | |
1316 | wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", | |
1317 | usrv->service); | |
1318 | wpabuf_put_str(resp, usrv->service); | |
1319 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - | |
1320 | 2); | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | ||
1325 | static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s, | |
1326 | struct wpabuf *resp, u8 srv_trans_id, | |
1327 | const u8 *query, size_t query_len) | |
1328 | { | |
1329 | struct p2p_srv_upnp *usrv; | |
1330 | u8 *len_pos; | |
1331 | u8 version; | |
1332 | char *str; | |
1333 | int count = 0; | |
1334 | ||
1335 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP", | |
1336 | query, query_len); | |
1337 | ||
1338 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { | |
1339 | wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); | |
1340 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP, | |
1341 | srv_trans_id); | |
1342 | return; | |
1343 | } | |
1344 | ||
1345 | if (query_len == 0) { | |
1346 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); | |
1347 | return; | |
1348 | } | |
1349 | ||
b22128ef JM |
1350 | if (wpabuf_tailroom(resp) < 5) |
1351 | return; | |
1352 | ||
1353 | /* Length (to be filled) */ | |
1354 | len_pos = wpabuf_put(resp, 2); | |
1355 | wpabuf_put_u8(resp, P2P_SERV_UPNP); | |
1356 | wpabuf_put_u8(resp, srv_trans_id); | |
1357 | ||
79222cfa JM |
1358 | version = query[0]; |
1359 | str = os_malloc(query_len); | |
1360 | if (str == NULL) | |
1361 | return; | |
1362 | os_memcpy(str, query + 1, query_len - 1); | |
1363 | str[query_len - 1] = '\0'; | |
1364 | ||
b22128ef JM |
1365 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
1366 | struct p2p_srv_upnp, list) { | |
1367 | if (version != usrv->version) | |
1368 | continue; | |
1369 | ||
1370 | if (os_strcmp(str, "ssdp:all") != 0 && | |
1371 | os_strstr(usrv->service, str) == NULL) | |
1372 | continue; | |
1373 | ||
1374 | if (wpabuf_tailroom(resp) < 2) | |
1375 | break; | |
1376 | if (count == 0) { | |
1377 | /* Status Code */ | |
1378 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); | |
1379 | /* Response Data */ | |
1380 | wpabuf_put_u8(resp, version); | |
1381 | } else | |
1382 | wpabuf_put_u8(resp, ','); | |
1383 | ||
1384 | count++; | |
1385 | ||
1386 | wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", | |
1387 | usrv->service); | |
1388 | if (wpabuf_tailroom(resp) < os_strlen(usrv->service)) | |
1389 | break; | |
1390 | wpabuf_put_str(resp, usrv->service); | |
1391 | } | |
1392 | ||
1393 | if (count == 0) { | |
1394 | wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not " | |
1395 | "available"); | |
1396 | /* Status Code */ | |
bf608cad | 1397 | wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); |
b22128ef JM |
1398 | /* Response Data: empty */ |
1399 | } | |
1400 | ||
1401 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); | |
1402 | } | |
1403 | ||
1404 | ||
1405 | void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token, | |
1406 | u16 update_indic, const u8 *tlvs, size_t tlvs_len) | |
1407 | { | |
1408 | struct wpa_supplicant *wpa_s = ctx; | |
1409 | const u8 *pos = tlvs; | |
1410 | const u8 *end = tlvs + tlvs_len; | |
1411 | const u8 *tlv_end; | |
1412 | u16 slen; | |
1413 | struct wpabuf *resp; | |
1414 | u8 srv_proto, srv_trans_id; | |
1415 | size_t buf_len; | |
1416 | char *buf; | |
1417 | ||
1418 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs", | |
1419 | tlvs, tlvs_len); | |
1420 | buf_len = 2 * tlvs_len + 1; | |
1421 | buf = os_malloc(buf_len); | |
1422 | if (buf) { | |
1423 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); | |
1424 | wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d " | |
1425 | MACSTR " %u %u %s", | |
1426 | freq, MAC2STR(sa), dialog_token, update_indic, | |
1427 | buf); | |
1428 | os_free(buf); | |
1429 | } | |
1430 | ||
1431 | if (wpa_s->p2p_sd_over_ctrl_iface) | |
1432 | return; /* to be processed by an external program */ | |
1433 | ||
1434 | resp = wpabuf_alloc(10000); | |
1435 | if (resp == NULL) | |
1436 | return; | |
1437 | ||
1438 | while (pos + 1 < end) { | |
1439 | wpa_printf(MSG_DEBUG, "P2P: Service Request TLV"); | |
1440 | slen = WPA_GET_LE16(pos); | |
1441 | pos += 2; | |
1442 | if (pos + slen > end || slen < 2) { | |
1443 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data " | |
1444 | "length"); | |
1445 | wpabuf_free(resp); | |
1446 | return; | |
1447 | } | |
1448 | tlv_end = pos + slen; | |
1449 | ||
1450 | srv_proto = *pos++; | |
1451 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", | |
1452 | srv_proto); | |
1453 | srv_trans_id = *pos++; | |
1454 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", | |
1455 | srv_trans_id); | |
1456 | ||
1457 | wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data", | |
1458 | pos, tlv_end - pos); | |
1459 | ||
6e6963ea JM |
1460 | |
1461 | if (wpa_s->force_long_sd) { | |
1462 | wpa_printf(MSG_DEBUG, "P2P: SD test - force long " | |
1463 | "response"); | |
1464 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); | |
1465 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); | |
1466 | goto done; | |
1467 | } | |
1468 | ||
b22128ef JM |
1469 | switch (srv_proto) { |
1470 | case P2P_SERV_ALL_SERVICES: | |
1471 | wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request " | |
1472 | "for all services"); | |
1473 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) && | |
1474 | dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { | |
1475 | wpa_printf(MSG_DEBUG, "P2P: No service " | |
1476 | "discovery protocols available"); | |
1477 | wpas_sd_add_proto_not_avail( | |
1478 | resp, P2P_SERV_ALL_SERVICES, | |
1479 | srv_trans_id); | |
1480 | break; | |
1481 | } | |
1482 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); | |
1483 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); | |
1484 | break; | |
1485 | case P2P_SERV_BONJOUR: | |
1486 | wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id, | |
1487 | pos, tlv_end - pos); | |
1488 | break; | |
1489 | case P2P_SERV_UPNP: | |
1490 | wpas_sd_req_upnp(wpa_s, resp, srv_trans_id, | |
1491 | pos, tlv_end - pos); | |
1492 | break; | |
1493 | default: | |
1494 | wpa_printf(MSG_DEBUG, "P2P: Unavailable service " | |
1495 | "protocol %u", srv_proto); | |
1496 | wpas_sd_add_proto_not_avail(resp, srv_proto, | |
1497 | srv_trans_id); | |
1498 | break; | |
1499 | } | |
1500 | ||
1501 | pos = tlv_end; | |
1502 | } | |
1503 | ||
6e6963ea | 1504 | done: |
b22128ef JM |
1505 | wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp); |
1506 | ||
1507 | wpabuf_free(resp); | |
1508 | } | |
1509 | ||
1510 | ||
1511 | void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic, | |
1512 | const u8 *tlvs, size_t tlvs_len) | |
1513 | { | |
1514 | struct wpa_supplicant *wpa_s = ctx; | |
1515 | const u8 *pos = tlvs; | |
1516 | const u8 *end = tlvs + tlvs_len; | |
1517 | const u8 *tlv_end; | |
1518 | u16 slen; | |
1519 | size_t buf_len; | |
1520 | char *buf; | |
1521 | ||
1522 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs", | |
1523 | tlvs, tlvs_len); | |
18708aad JM |
1524 | if (tlvs_len > 1500) { |
1525 | /* TODO: better way for handling this */ | |
1526 | wpa_msg_ctrl(wpa_s, MSG_INFO, | |
1527 | P2P_EVENT_SERV_DISC_RESP MACSTR | |
1528 | " %u <long response: %u bytes>", | |
1529 | MAC2STR(sa), update_indic, | |
1530 | (unsigned int) tlvs_len); | |
1531 | } else { | |
1532 | buf_len = 2 * tlvs_len + 1; | |
1533 | buf = os_malloc(buf_len); | |
1534 | if (buf) { | |
1535 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); | |
1536 | wpa_msg_ctrl(wpa_s, MSG_INFO, | |
1537 | P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s", | |
1538 | MAC2STR(sa), update_indic, buf); | |
1539 | os_free(buf); | |
1540 | } | |
b22128ef JM |
1541 | } |
1542 | ||
1543 | while (pos < end) { | |
1544 | u8 srv_proto, srv_trans_id, status; | |
1545 | ||
1546 | wpa_printf(MSG_DEBUG, "P2P: Service Response TLV"); | |
1547 | slen = WPA_GET_LE16(pos); | |
1548 | pos += 2; | |
1549 | if (pos + slen > end || slen < 3) { | |
1550 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data " | |
1551 | "length"); | |
1552 | return; | |
1553 | } | |
1554 | tlv_end = pos + slen; | |
1555 | ||
1556 | srv_proto = *pos++; | |
1557 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", | |
1558 | srv_proto); | |
1559 | srv_trans_id = *pos++; | |
1560 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", | |
1561 | srv_trans_id); | |
1562 | status = *pos++; | |
1563 | wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u", | |
1564 | status); | |
1565 | ||
1566 | wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data", | |
1567 | pos, tlv_end - pos); | |
1568 | ||
1569 | pos = tlv_end; | |
1570 | } | |
1571 | } | |
1572 | ||
1573 | ||
1574 | void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst, | |
1575 | const struct wpabuf *tlvs) | |
1576 | { | |
1577 | return p2p_sd_request(wpa_s->global->p2p, dst, tlvs); | |
1578 | } | |
1579 | ||
1580 | ||
1581 | void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst, | |
1582 | u8 version, const char *query) | |
1583 | { | |
1584 | struct wpabuf *tlvs; | |
1585 | void *ret; | |
1586 | ||
1587 | tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query)); | |
1588 | if (tlvs == NULL) | |
1589 | return NULL; | |
1590 | wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query)); | |
1591 | wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */ | |
1592 | wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */ | |
1593 | wpabuf_put_u8(tlvs, version); | |
1594 | wpabuf_put_str(tlvs, query); | |
1595 | ret = wpas_p2p_sd_request(wpa_s, dst, tlvs); | |
1596 | wpabuf_free(tlvs); | |
1597 | return ret; | |
1598 | } | |
1599 | ||
1600 | ||
1601 | int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req) | |
1602 | { | |
1603 | return p2p_sd_cancel_request(wpa_s->global->p2p, req); | |
1604 | } | |
1605 | ||
1606 | ||
1607 | void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq, | |
1608 | const u8 *dst, u8 dialog_token, | |
1609 | const struct wpabuf *resp_tlvs) | |
1610 | { | |
1611 | p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token, | |
1612 | resp_tlvs); | |
1613 | } | |
1614 | ||
1615 | ||
1616 | void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s) | |
1617 | { | |
1618 | p2p_sd_service_update(wpa_s->global->p2p); | |
1619 | } | |
1620 | ||
1621 | ||
1622 | static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv) | |
1623 | { | |
1624 | dl_list_del(&bsrv->list); | |
1625 | wpabuf_free(bsrv->query); | |
1626 | wpabuf_free(bsrv->resp); | |
1627 | os_free(bsrv); | |
1628 | } | |
1629 | ||
1630 | ||
1631 | static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv) | |
1632 | { | |
1633 | dl_list_del(&usrv->list); | |
1634 | os_free(usrv->service); | |
1635 | os_free(usrv); | |
1636 | } | |
1637 | ||
1638 | ||
1639 | void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s) | |
1640 | { | |
1641 | struct p2p_srv_bonjour *bsrv, *bn; | |
1642 | struct p2p_srv_upnp *usrv, *un; | |
1643 | ||
1644 | dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour, | |
1645 | struct p2p_srv_bonjour, list) | |
1646 | wpas_p2p_srv_bonjour_free(bsrv); | |
1647 | ||
1648 | dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp, | |
1649 | struct p2p_srv_upnp, list) | |
1650 | wpas_p2p_srv_upnp_free(usrv); | |
1651 | ||
1652 | wpas_p2p_sd_service_update(wpa_s); | |
1653 | } | |
1654 | ||
1655 | ||
1656 | int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s, | |
1657 | struct wpabuf *query, struct wpabuf *resp) | |
1658 | { | |
1659 | struct p2p_srv_bonjour *bsrv; | |
1660 | ||
1661 | bsrv = wpas_p2p_service_get_bonjour(wpa_s, query); | |
1662 | if (bsrv) { | |
1663 | wpabuf_free(query); | |
1664 | wpabuf_free(bsrv->resp); | |
1665 | bsrv->resp = resp; | |
1666 | return 0; | |
1667 | } | |
1668 | ||
1669 | bsrv = os_zalloc(sizeof(*bsrv)); | |
1670 | if (bsrv == NULL) | |
1671 | return -1; | |
1672 | bsrv->query = query; | |
1673 | bsrv->resp = resp; | |
1674 | dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list); | |
1675 | ||
1676 | wpas_p2p_sd_service_update(wpa_s); | |
1677 | return 0; | |
1678 | } | |
1679 | ||
1680 | ||
1681 | int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s, | |
1682 | const struct wpabuf *query) | |
1683 | { | |
1684 | struct p2p_srv_bonjour *bsrv; | |
1685 | ||
1686 | bsrv = wpas_p2p_service_get_bonjour(wpa_s, query); | |
1687 | if (bsrv == NULL) | |
1688 | return -1; | |
1689 | wpas_p2p_srv_bonjour_free(bsrv); | |
1690 | wpas_p2p_sd_service_update(wpa_s); | |
1691 | return 0; | |
1692 | } | |
1693 | ||
1694 | ||
1695 | int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version, | |
1696 | const char *service) | |
1697 | { | |
1698 | struct p2p_srv_upnp *usrv; | |
1699 | ||
1700 | if (wpas_p2p_service_get_upnp(wpa_s, version, service)) | |
1701 | return 0; /* Already listed */ | |
1702 | usrv = os_zalloc(sizeof(*usrv)); | |
1703 | if (usrv == NULL) | |
1704 | return -1; | |
1705 | usrv->version = version; | |
1706 | usrv->service = os_strdup(service); | |
1707 | if (usrv->service == NULL) { | |
1708 | os_free(usrv); | |
1709 | return -1; | |
1710 | } | |
1711 | dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list); | |
1712 | ||
1713 | wpas_p2p_sd_service_update(wpa_s); | |
1714 | return 0; | |
1715 | } | |
1716 | ||
1717 | ||
1718 | int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version, | |
1719 | const char *service) | |
1720 | { | |
1721 | struct p2p_srv_upnp *usrv; | |
1722 | ||
1723 | usrv = wpas_p2p_service_get_upnp(wpa_s, version, service); | |
1724 | if (usrv == NULL) | |
1725 | return -1; | |
1726 | wpas_p2p_srv_upnp_free(usrv); | |
1727 | wpas_p2p_sd_service_update(wpa_s); | |
1728 | return 0; | |
1729 | } | |
1730 | ||
1731 | ||
1732 | static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s, | |
1733 | const u8 *peer, const char *params) | |
1734 | { | |
1735 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s", | |
1736 | MAC2STR(peer), wps_generate_pin(), params); | |
1737 | } | |
1738 | ||
1739 | ||
1740 | static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s, | |
1741 | const u8 *peer, const char *params) | |
1742 | { | |
1743 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s", | |
1744 | MAC2STR(peer), params); | |
1745 | } | |
1746 | ||
1747 | ||
1748 | void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods, | |
1749 | const u8 *dev_addr, const u8 *pri_dev_type, | |
1750 | const char *dev_name, u16 supp_config_methods, | |
1751 | u8 dev_capab, u8 group_capab) | |
1752 | { | |
1753 | struct wpa_supplicant *wpa_s = ctx; | |
1754 | char devtype[WPS_DEV_TYPE_BUFSIZE]; | |
1755 | char params[200]; | |
1756 | u8 empty_dev_type[8]; | |
1757 | ||
1758 | if (pri_dev_type == NULL) { | |
1759 | os_memset(empty_dev_type, 0, sizeof(empty_dev_type)); | |
1760 | pri_dev_type = empty_dev_type; | |
1761 | } | |
1762 | os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR | |
1763 | " pri_dev_type=%s name='%s' config_methods=0x%x " | |
1764 | "dev_capab=0x%x group_capab=0x%x", | |
1765 | MAC2STR(dev_addr), | |
1766 | wps_dev_type_bin2str(pri_dev_type, devtype, | |
1767 | sizeof(devtype)), | |
1768 | dev_name, supp_config_methods, dev_capab, group_capab); | |
1769 | params[sizeof(params) - 1] = '\0'; | |
1770 | ||
1771 | if (config_methods & WPS_CONFIG_DISPLAY) | |
1772 | wpas_prov_disc_local_display(wpa_s, peer, params); | |
1773 | else if (config_methods & WPS_CONFIG_KEYPAD) | |
1774 | wpas_prov_disc_local_keypad(wpa_s, peer, params); | |
1775 | else if (config_methods & WPS_CONFIG_PUSHBUTTON) | |
1776 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR | |
1777 | "%s", MAC2STR(peer), params); | |
1778 | } | |
1779 | ||
1780 | ||
1781 | void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods) | |
1782 | { | |
1783 | struct wpa_supplicant *wpa_s = ctx; | |
1784 | if (config_methods & WPS_CONFIG_DISPLAY) | |
1785 | wpas_prov_disc_local_keypad(wpa_s, peer, ""); | |
1786 | else if (config_methods & WPS_CONFIG_KEYPAD) | |
1787 | wpas_prov_disc_local_display(wpa_s, peer, ""); | |
1788 | else if (config_methods & WPS_CONFIG_PUSHBUTTON) | |
1789 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR, | |
1790 | MAC2STR(peer)); | |
a482883f JM |
1791 | |
1792 | if (wpa_s->pending_pd_before_join && | |
1793 | (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || | |
1794 | os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { | |
1795 | wpa_s->pending_pd_before_join = 0; | |
1796 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " | |
1797 | "join-existing-group operation"); | |
1798 | wpas_p2p_join_start(wpa_s); | |
1799 | } | |
b22128ef JM |
1800 | } |
1801 | ||
1802 | ||
1803 | static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid, | |
1804 | const u8 *go_dev_addr, const u8 *ssid, | |
1805 | size_t ssid_len, int *go, u8 *group_bssid, | |
1806 | int *force_freq, int persistent_group) | |
1807 | { | |
1808 | struct wpa_supplicant *wpa_s = ctx; | |
1809 | struct wpa_ssid *s; | |
1810 | u8 cur_bssid[ETH_ALEN]; | |
1811 | int res; | |
1812 | ||
1813 | if (!persistent_group) { | |
1814 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR | |
1815 | " to join an active group", MAC2STR(sa)); | |
108def93 | 1816 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
131cb37c JM |
1817 | (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN) |
1818 | == 0 || | |
1819 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) { | |
108def93 JM |
1820 | wpa_printf(MSG_DEBUG, "P2P: Accept previously " |
1821 | "authorized invitation"); | |
1822 | goto accept_inv; | |
1823 | } | |
b22128ef JM |
1824 | /* |
1825 | * Do not accept the invitation automatically; notify user and | |
1826 | * request approval. | |
1827 | */ | |
1828 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; | |
1829 | } | |
1830 | ||
1831 | if (!wpa_s->conf->persistent_reconnect) | |
1832 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; | |
1833 | ||
1834 | for (s = wpa_s->conf->ssid; s; s = s->next) { | |
1835 | if (s->disabled == 2 && | |
1836 | os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 && | |
1837 | s->ssid_len == ssid_len && | |
1838 | os_memcmp(ssid, s->ssid, ssid_len) == 0) | |
1839 | break; | |
1840 | } | |
1841 | ||
1842 | if (!s) { | |
1843 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR | |
1844 | " requested reinvocation of an unknown group", | |
1845 | MAC2STR(sa)); | |
1846 | return P2P_SC_FAIL_UNKNOWN_GROUP; | |
1847 | } | |
1848 | ||
1849 | if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) { | |
1850 | *go = 1; | |
1851 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { | |
1852 | wpa_printf(MSG_DEBUG, "P2P: The only available " | |
1853 | "interface is already in use - reject " | |
1854 | "invitation"); | |
1855 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; | |
1856 | } | |
1857 | os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN); | |
1858 | } else if (s->mode == WPAS_MODE_P2P_GO) { | |
1859 | *go = 1; | |
1860 | if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0) | |
1861 | { | |
1862 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " | |
1863 | "interface address for the group"); | |
1864 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; | |
1865 | } | |
1866 | os_memcpy(group_bssid, wpa_s->pending_interface_addr, | |
1867 | ETH_ALEN); | |
1868 | } | |
1869 | ||
108def93 | 1870 | accept_inv: |
b22128ef JM |
1871 | if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 && |
1872 | wpa_s->assoc_freq) { | |
1873 | wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match " | |
1874 | "the channel we are already using"); | |
1875 | *force_freq = wpa_s->assoc_freq; | |
1876 | } | |
1877 | ||
1878 | res = wpa_drv_shared_freq(wpa_s); | |
1879 | if (res > 0) { | |
1880 | wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match " | |
1881 | "with the channel we are already using on a " | |
1882 | "shared interface"); | |
1883 | *force_freq = res; | |
1884 | } | |
1885 | ||
1886 | return P2P_SC_SUCCESS; | |
1887 | } | |
1888 | ||
1889 | ||
1890 | static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid, | |
1891 | const u8 *ssid, size_t ssid_len, | |
1892 | const u8 *go_dev_addr, u8 status, | |
1893 | int op_freq) | |
1894 | { | |
1895 | struct wpa_supplicant *wpa_s = ctx; | |
1896 | struct wpa_ssid *s; | |
1897 | ||
1898 | for (s = wpa_s->conf->ssid; s; s = s->next) { | |
1899 | if (s->disabled == 2 && | |
1900 | s->ssid_len == ssid_len && | |
1901 | os_memcmp(ssid, s->ssid, ssid_len) == 0) | |
1902 | break; | |
1903 | } | |
1904 | ||
1905 | if (status == P2P_SC_SUCCESS) { | |
1906 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR | |
1907 | " was accepted; op_freq=%d MHz", | |
1908 | MAC2STR(sa), op_freq); | |
1909 | if (s) { | |
1910 | wpas_p2p_group_add_persistent( | |
1911 | wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0); | |
2049af2b | 1912 | } else if (bssid) { |
108def93 JM |
1913 | wpas_p2p_join(wpa_s, bssid, go_dev_addr, |
1914 | wpa_s->p2p_wps_method); | |
b22128ef JM |
1915 | } |
1916 | return; | |
1917 | } | |
1918 | ||
1919 | if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { | |
1920 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR | |
1921 | " was rejected (status %u)", MAC2STR(sa), status); | |
1922 | return; | |
1923 | } | |
1924 | ||
1925 | if (!s) { | |
1926 | if (bssid) { | |
1927 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED | |
1928 | "sa=" MACSTR " go_dev_addr=" MACSTR | |
1929 | " bssid=" MACSTR " unknown-network", | |
1930 | MAC2STR(sa), MAC2STR(go_dev_addr), | |
1931 | MAC2STR(bssid)); | |
1932 | } else { | |
1933 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED | |
1934 | "sa=" MACSTR " go_dev_addr=" MACSTR | |
1935 | " unknown-network", | |
1936 | MAC2STR(sa), MAC2STR(go_dev_addr)); | |
1937 | } | |
1938 | return; | |
1939 | } | |
1940 | ||
1941 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR | |
1942 | " persistent=%d", MAC2STR(sa), s->id); | |
1943 | } | |
1944 | ||
1945 | ||
1946 | static void wpas_invitation_result(void *ctx, int status, const u8 *bssid) | |
1947 | { | |
1948 | struct wpa_supplicant *wpa_s = ctx; | |
1949 | struct wpa_ssid *ssid; | |
1950 | ||
1951 | if (bssid) { | |
1952 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT | |
1953 | "status=%d " MACSTR, | |
1954 | status, MAC2STR(bssid)); | |
1955 | } else { | |
1956 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT | |
1957 | "status=%d ", status); | |
1958 | } | |
1959 | ||
706887fc JM |
1960 | if (wpa_s->pending_invite_ssid_id == -1) |
1961 | return; /* Invitation to active group */ | |
1962 | ||
b22128ef JM |
1963 | if (status != P2P_SC_SUCCESS) { |
1964 | wpas_p2p_remove_pending_group_interface(wpa_s); | |
1965 | return; | |
1966 | } | |
1967 | ||
1968 | ssid = wpa_config_get_network(wpa_s->conf, | |
1969 | wpa_s->pending_invite_ssid_id); | |
1970 | if (ssid == NULL) { | |
1971 | wpa_printf(MSG_ERROR, "P2P: Could not find persistent group " | |
1972 | "data matching with invitation"); | |
1973 | return; | |
1974 | } | |
1975 | ||
1976 | wpas_p2p_group_add_persistent(wpa_s, ssid, | |
1977 | ssid->mode == WPAS_MODE_P2P_GO, 0); | |
1978 | } | |
1979 | ||
1980 | ||
ac8d1011 JM |
1981 | static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s, |
1982 | struct p2p_channels *chan) | |
1983 | { | |
1984 | int i, cla = 0; | |
1985 | ||
1986 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz " | |
1987 | "band"); | |
1988 | ||
1989 | /* Operating class 81 - 2.4 GHz band channels 1..13 */ | |
1990 | chan->reg_class[cla].reg_class = 81; | |
1991 | chan->reg_class[cla].channels = 11; | |
1992 | for (i = 0; i < 11; i++) | |
1993 | chan->reg_class[cla].channel[i] = i + 1; | |
1994 | cla++; | |
1995 | ||
1996 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz " | |
1997 | "band"); | |
1998 | ||
1999 | /* Operating class 115 - 5 GHz, channels 36-48 */ | |
2000 | chan->reg_class[cla].reg_class = 115; | |
2001 | chan->reg_class[cla].channels = 4; | |
2002 | chan->reg_class[cla].channel[0] = 36; | |
2003 | chan->reg_class[cla].channel[1] = 40; | |
2004 | chan->reg_class[cla].channel[2] = 44; | |
2005 | chan->reg_class[cla].channel[3] = 48; | |
2006 | cla++; | |
2007 | ||
2008 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz " | |
2009 | "band"); | |
2010 | ||
2011 | /* Operating class 124 - 5 GHz, channels 149,153,157,161 */ | |
2012 | chan->reg_class[cla].reg_class = 124; | |
2013 | chan->reg_class[cla].channels = 4; | |
2014 | chan->reg_class[cla].channel[0] = 149; | |
2015 | chan->reg_class[cla].channel[1] = 153; | |
2016 | chan->reg_class[cla].channel[2] = 157; | |
2017 | chan->reg_class[cla].channel[3] = 161; | |
2018 | cla++; | |
2019 | ||
2020 | chan->reg_classes = cla; | |
2021 | return 0; | |
2022 | } | |
2023 | ||
2024 | ||
2025 | static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes, | |
2026 | u16 num_modes, | |
2027 | enum hostapd_hw_mode mode) | |
2028 | { | |
2029 | u16 i; | |
2030 | ||
2031 | for (i = 0; i < num_modes; i++) { | |
2032 | if (modes[i].mode == mode) | |
2033 | return &modes[i]; | |
2034 | } | |
2035 | ||
2036 | return NULL; | |
2037 | } | |
2038 | ||
2039 | ||
51222429 | 2040 | static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags) |
ac8d1011 JM |
2041 | { |
2042 | int i; | |
2043 | ||
2044 | for (i = 0; i < mode->num_channels; i++) { | |
2045 | if (mode->channels[i].chan == chan) { | |
51222429 JM |
2046 | if (flags) |
2047 | *flags = mode->channels[i].flag; | |
ac8d1011 JM |
2048 | return !(mode->channels[i].flag & |
2049 | (HOSTAPD_CHAN_DISABLED | | |
2050 | HOSTAPD_CHAN_PASSIVE_SCAN | | |
2051 | HOSTAPD_CHAN_NO_IBSS | | |
2052 | HOSTAPD_CHAN_RADAR)); | |
2053 | } | |
2054 | } | |
2055 | ||
2056 | return 0; | |
2057 | } | |
2058 | ||
2059 | ||
2060 | struct p2p_oper_class_map { | |
2061 | enum hostapd_hw_mode mode; | |
2062 | u8 op_class; | |
2063 | u8 min_chan; | |
2064 | u8 max_chan; | |
2065 | u8 inc; | |
51222429 | 2066 | enum { BW20, BW40PLUS, BW40MINUS } bw; |
ac8d1011 JM |
2067 | }; |
2068 | ||
b22128ef | 2069 | static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s, |
ac8d1011 | 2070 | struct p2p_channels *chan) |
b22128ef | 2071 | { |
ac8d1011 | 2072 | struct hostapd_hw_modes *modes, *mode; |
b22128ef | 2073 | u16 num_modes, flags; |
ac8d1011 JM |
2074 | int cla, op; |
2075 | struct p2p_oper_class_map op_class[] = { | |
51222429 JM |
2076 | { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 }, |
2077 | { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 }, | |
2078 | #if 0 /* Do not enable HT40 on 2 GHz for now */ | |
2079 | { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS }, | |
2080 | { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS }, | |
ac8d1011 | 2081 | #endif |
51222429 JM |
2082 | { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 }, |
2083 | { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 }, | |
2084 | { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS }, | |
2085 | { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS }, | |
2086 | { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS }, | |
2087 | { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS }, | |
2088 | { -1, 0, 0, 0, 0, BW20 } | |
ac8d1011 | 2089 | }; |
b22128ef JM |
2090 | |
2091 | modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags); | |
2092 | if (modes == NULL) { | |
2093 | wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching " | |
2094 | "of all supported channels; assume dualband " | |
2095 | "support"); | |
ac8d1011 | 2096 | return wpas_p2p_default_channels(wpa_s, chan); |
b22128ef JM |
2097 | } |
2098 | ||
2099 | cla = 0; | |
2100 | ||
ac8d1011 JM |
2101 | for (op = 0; op_class[op].op_class; op++) { |
2102 | struct p2p_oper_class_map *o = &op_class[op]; | |
2103 | u8 ch; | |
2104 | struct p2p_reg_class *reg = NULL; | |
b22128ef | 2105 | |
ac8d1011 JM |
2106 | mode = get_mode(modes, num_modes, o->mode); |
2107 | if (mode == NULL) | |
2108 | continue; | |
2109 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { | |
51222429 JM |
2110 | int flag; |
2111 | if (!has_channel(mode, ch, &flag)) | |
2112 | continue; | |
2113 | if (o->bw == BW40MINUS && | |
2114 | (!(flag & HOSTAPD_CHAN_HT40MINUS) || | |
5254eb7e | 2115 | !has_channel(mode, ch - 4, NULL))) |
51222429 JM |
2116 | continue; |
2117 | if (o->bw == BW40PLUS && | |
2118 | (!(flag & HOSTAPD_CHAN_HT40PLUS) || | |
5254eb7e | 2119 | !has_channel(mode, ch + 4, NULL))) |
ac8d1011 JM |
2120 | continue; |
2121 | if (reg == NULL) { | |
2122 | wpa_printf(MSG_DEBUG, "P2P: Add operating " | |
2123 | "class %u", o->op_class); | |
2124 | reg = &chan->reg_class[cla]; | |
2125 | cla++; | |
2126 | reg->reg_class = o->op_class; | |
2127 | } | |
2128 | reg->channel[reg->channels] = ch; | |
2129 | reg->channels++; | |
2130 | } | |
2131 | if (reg) { | |
2132 | wpa_hexdump(MSG_DEBUG, "P2P: Channels", | |
2133 | reg->channel, reg->channels); | |
2134 | } | |
b22128ef JM |
2135 | } |
2136 | ||
ac8d1011 | 2137 | chan->reg_classes = cla; |
b22128ef | 2138 | |
ac8d1011 | 2139 | ieee80211_sta_free_hw_features(modes, num_modes); |
b22128ef JM |
2140 | |
2141 | return 0; | |
2142 | } | |
2143 | ||
2144 | ||
2145 | static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf, | |
2146 | size_t buf_len) | |
2147 | { | |
2148 | struct wpa_supplicant *wpa_s = ctx; | |
2149 | ||
2150 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
2151 | if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0) | |
2152 | break; | |
2153 | } | |
2154 | if (wpa_s == NULL) | |
2155 | return -1; | |
2156 | ||
2157 | return wpa_drv_get_noa(wpa_s, buf, buf_len); | |
2158 | } | |
2159 | ||
2160 | ||
2161 | /** | |
2162 | * wpas_p2p_init - Initialize P2P module for %wpa_supplicant | |
2163 | * @global: Pointer to global data from wpa_supplicant_init() | |
2164 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
2165 | * Returns: 0 on success, -1 on failure | |
2166 | */ | |
2167 | int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s) | |
2168 | { | |
2169 | struct p2p_config p2p; | |
2170 | unsigned int r; | |
2171 | int i; | |
2172 | ||
2173 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) | |
2174 | return 0; | |
2175 | ||
2176 | #ifdef CONFIG_CLIENT_MLME | |
2177 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) { | |
2178 | wpa_s->mlme.public_action_cb = p2p_rx_action_mlme; | |
2179 | wpa_s->mlme.public_action_cb_ctx = wpa_s; | |
2180 | } | |
2181 | #endif /* CONFIG_CLIENT_MLME */ | |
2182 | ||
2183 | if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) { | |
2184 | wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates"); | |
2185 | /* Continue anyway; this is not really a fatal error */ | |
2186 | } | |
2187 | ||
2188 | if (global->p2p) | |
2189 | return 0; | |
2190 | ||
2191 | os_memset(&p2p, 0, sizeof(p2p)); | |
2192 | p2p.msg_ctx = wpa_s; | |
2193 | p2p.cb_ctx = wpa_s; | |
2194 | p2p.p2p_scan = wpas_p2p_scan; | |
2195 | p2p.send_action = wpas_send_action; | |
2196 | p2p.send_action_done = wpas_send_action_done; | |
2197 | p2p.go_neg_completed = wpas_go_neg_completed; | |
2198 | p2p.go_neg_req_rx = wpas_go_neg_req_rx; | |
2199 | p2p.dev_found = wpas_dev_found; | |
2200 | p2p.start_listen = wpas_start_listen; | |
2201 | p2p.stop_listen = wpas_stop_listen; | |
2202 | p2p.send_probe_resp = wpas_send_probe_resp; | |
2203 | p2p.sd_request = wpas_sd_request; | |
2204 | p2p.sd_response = wpas_sd_response; | |
2205 | p2p.prov_disc_req = wpas_prov_disc_req; | |
2206 | p2p.prov_disc_resp = wpas_prov_disc_resp; | |
2207 | p2p.invitation_process = wpas_invitation_process; | |
2208 | p2p.invitation_received = wpas_invitation_received; | |
2209 | p2p.invitation_result = wpas_invitation_result; | |
2210 | p2p.get_noa = wpas_get_noa; | |
2211 | ||
2212 | os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN); | |
2213 | os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN); | |
2214 | p2p.dev_name = wpa_s->conf->device_name; | |
2215 | ||
2216 | if (wpa_s->conf->p2p_listen_reg_class && | |
2217 | wpa_s->conf->p2p_listen_channel) { | |
2218 | p2p.reg_class = wpa_s->conf->p2p_listen_reg_class; | |
2219 | p2p.channel = wpa_s->conf->p2p_listen_channel; | |
2220 | } else { | |
2221 | p2p.reg_class = 81; | |
2222 | /* | |
2223 | * Pick one of the social channels randomly as the listen | |
2224 | * channel. | |
2225 | */ | |
2226 | os_get_random((u8 *) &r, sizeof(r)); | |
2227 | p2p.channel = 1 + (r % 3) * 5; | |
2228 | } | |
7cfc4ac3 | 2229 | wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel); |
b22128ef JM |
2230 | |
2231 | if (wpa_s->conf->p2p_oper_reg_class && | |
2232 | wpa_s->conf->p2p_oper_channel) { | |
2233 | p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class; | |
2234 | p2p.op_channel = wpa_s->conf->p2p_oper_channel; | |
7cfc4ac3 AGS |
2235 | p2p.cfg_op_channel = 1; |
2236 | wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: " | |
2237 | "%d:%d", p2p.op_reg_class, p2p.op_channel); | |
2238 | ||
b22128ef JM |
2239 | } else { |
2240 | p2p.op_reg_class = 81; | |
2241 | /* | |
7cfc4ac3 AGS |
2242 | * Use random operation channel from (1, 6, 11) if no other |
2243 | * preference is indicated. | |
b22128ef | 2244 | */ |
b7412dab | 2245 | os_get_random((u8 *) &r, sizeof(r)); |
7cfc4ac3 AGS |
2246 | p2p.op_channel = 1 + (r % 3) * 5; |
2247 | p2p.cfg_op_channel = 0; | |
2248 | wpa_printf(MSG_DEBUG, "P2P: Random operating channel: " | |
2249 | "%d:%d", p2p.op_reg_class, p2p.op_channel); | |
b22128ef | 2250 | } |
b22128ef JM |
2251 | if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
2252 | os_memcpy(p2p.country, wpa_s->conf->country, 2); | |
2253 | p2p.country[2] = 0x04; | |
2254 | } else | |
aaca6505 | 2255 | os_memcpy(p2p.country, "XX\x04", 3); |
b22128ef | 2256 | |
ac8d1011 | 2257 | if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) { |
b22128ef JM |
2258 | wpa_printf(MSG_ERROR, "P2P: Failed to configure supported " |
2259 | "channel list"); | |
2260 | return -1; | |
2261 | } | |
2262 | ||
2263 | if (wpa_s->conf->device_type && | |
2264 | wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) < | |
2265 | 0) { | |
2266 | wpa_printf(MSG_ERROR, "P2P: Invalid device_type"); | |
2267 | return -1; | |
2268 | } | |
2269 | ||
2270 | for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) { | |
2271 | if (wpa_s->conf->sec_device_type[i] == NULL) | |
2272 | continue; | |
2273 | if (wps_dev_type_str2bin( | |
2274 | wpa_s->conf->sec_device_type[i], | |
2275 | p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) { | |
2276 | wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type"); | |
2277 | return -1; | |
2278 | } | |
2279 | p2p.num_sec_dev_types++; | |
2280 | if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES) | |
2281 | break; | |
2282 | } | |
2283 | ||
2284 | p2p.concurrent_operations = !!(wpa_s->drv_flags & | |
2285 | WPA_DRIVER_FLAGS_P2P_CONCURRENT); | |
2286 | ||
de979d8f | 2287 | p2p.max_peers = 100; |
b22128ef JM |
2288 | |
2289 | if (wpa_s->conf->p2p_ssid_postfix) { | |
2290 | p2p.ssid_postfix_len = | |
2291 | os_strlen(wpa_s->conf->p2p_ssid_postfix); | |
2292 | if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix)) | |
2293 | p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix); | |
2294 | os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix, | |
2295 | p2p.ssid_postfix_len); | |
2296 | } | |
2297 | ||
0f66abd2 SS |
2298 | p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss; |
2299 | ||
b22128ef JM |
2300 | global->p2p = p2p_init(&p2p); |
2301 | if (global->p2p == NULL) | |
2302 | return -1; | |
2303 | ||
2304 | return 0; | |
2305 | } | |
2306 | ||
2307 | ||
2308 | /** | |
2309 | * wpas_p2p_deinit - Deinitialize per-interface P2P data | |
2310 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
2311 | * | |
2312 | * This function deinitialize per-interface P2P data. | |
2313 | */ | |
2314 | void wpas_p2p_deinit(struct wpa_supplicant *wpa_s) | |
2315 | { | |
2316 | if (wpa_s->driver && wpa_s->drv_priv) | |
2317 | wpa_drv_probe_req_report(wpa_s, 0); | |
2318 | os_free(wpa_s->go_params); | |
2319 | wpa_s->go_params = NULL; | |
2320 | wpabuf_free(wpa_s->pending_action_tx); | |
2321 | wpa_s->pending_action_tx = NULL; | |
2322 | eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL); | |
2323 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); | |
ef922c4a | 2324 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
b22128ef JM |
2325 | wpa_s->p2p_long_listen = 0; |
2326 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); | |
3071e181 | 2327 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
b22128ef JM |
2328 | wpas_p2p_remove_pending_group_interface(wpa_s); |
2329 | ||
2330 | /* TODO: remove group interface from the driver if this wpa_s instance | |
2331 | * is on top of a P2P group interface */ | |
2332 | } | |
2333 | ||
2334 | ||
2335 | /** | |
2336 | * wpas_p2p_deinit_global - Deinitialize global P2P module | |
2337 | * @global: Pointer to global data from wpa_supplicant_init() | |
2338 | * | |
2339 | * This function deinitializes the global (per device) P2P module. | |
2340 | */ | |
2341 | void wpas_p2p_deinit_global(struct wpa_global *global) | |
2342 | { | |
2343 | struct wpa_supplicant *wpa_s, *tmp; | |
2344 | char *ifname; | |
2345 | ||
2346 | if (global->p2p == NULL) | |
2347 | return; | |
2348 | ||
2349 | /* Remove remaining P2P group interfaces */ | |
2350 | wpa_s = global->ifaces; | |
2a43101e JM |
2351 | if (wpa_s) |
2352 | wpas_p2p_service_flush(wpa_s); | |
b22128ef JM |
2353 | while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) |
2354 | wpa_s = wpa_s->next; | |
2355 | while (wpa_s) { | |
2356 | enum wpa_driver_if_type type; | |
2357 | tmp = global->ifaces; | |
2358 | while (tmp && | |
2359 | (tmp == wpa_s || | |
2360 | tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) { | |
2361 | tmp = tmp->next; | |
2362 | } | |
2363 | if (tmp == NULL) | |
2364 | break; | |
2365 | ifname = os_strdup(tmp->ifname); | |
2366 | type = wpas_p2p_if_type(tmp->p2p_group_interface); | |
2367 | wpa_supplicant_remove_iface(global, tmp); | |
2368 | if (ifname) | |
2369 | wpa_drv_if_remove(wpa_s, type, ifname); | |
2370 | os_free(ifname); | |
2371 | } | |
2372 | ||
743ef799 JM |
2373 | /* |
2374 | * Deinit GO data on any possibly remaining interface (if main | |
2375 | * interface is used as GO). | |
2376 | */ | |
2377 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
2378 | if (wpa_s->ap_iface) | |
2379 | wpas_p2p_group_deinit(wpa_s); | |
2380 | } | |
2381 | ||
b22128ef JM |
2382 | p2p_deinit(global->p2p); |
2383 | global->p2p = NULL; | |
2384 | } | |
2385 | ||
2386 | ||
2387 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s) | |
2388 | { | |
2389 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) | |
2390 | return 1; /* P2P group requires a new interface in every case | |
2391 | */ | |
2392 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)) | |
2393 | return 0; /* driver does not support concurrent operations */ | |
2394 | if (wpa_s->global->ifaces->next) | |
2395 | return 1; /* more that one interface already in use */ | |
2396 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) | |
2397 | return 1; /* this interface is already in use */ | |
2398 | return 0; | |
2399 | } | |
2400 | ||
2401 | ||
2402 | static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s, | |
2403 | const u8 *peer_addr, | |
2404 | enum p2p_wps_method wps_method, | |
2405 | int go_intent, const u8 *own_interface_addr, | |
2406 | unsigned int force_freq, int persistent_group) | |
2407 | { | |
2408 | return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method, | |
2409 | go_intent, own_interface_addr, force_freq, | |
2410 | persistent_group); | |
2411 | } | |
2412 | ||
2413 | ||
2414 | static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s, | |
2415 | const u8 *peer_addr, | |
2416 | enum p2p_wps_method wps_method, | |
2417 | int go_intent, const u8 *own_interface_addr, | |
2418 | unsigned int force_freq, int persistent_group) | |
2419 | { | |
2420 | return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method, | |
2421 | go_intent, own_interface_addr, force_freq, | |
2422 | persistent_group); | |
2423 | } | |
2424 | ||
2425 | ||
9b1ab931 JM |
2426 | static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s) |
2427 | { | |
2428 | wpa_s->p2p_join_scan_count++; | |
2429 | wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d", | |
2430 | wpa_s->p2p_join_scan_count); | |
2431 | if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) { | |
2432 | wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR | |
2433 | " for join operationg - stop join attempt", | |
2434 | MAC2STR(wpa_s->pending_join_iface_addr)); | |
2435 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | |
2436 | wpa_msg(wpa_s->parent, MSG_INFO, | |
2437 | P2P_EVENT_GROUP_FORMATION_FAILURE); | |
2438 | } | |
2439 | } | |
2440 | ||
2441 | ||
ef922c4a JM |
2442 | static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s, |
2443 | struct wpa_scan_results *scan_res) | |
b22128ef JM |
2444 | { |
2445 | struct wpa_bss *bss; | |
f8d0131a | 2446 | int freq; |
54960629 | 2447 | u8 iface_addr[ETH_ALEN]; |
b22128ef | 2448 | |
ef922c4a | 2449 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
b22128ef | 2450 | |
ef922c4a JM |
2451 | if (wpa_s->global->p2p_disabled) |
2452 | return; | |
b22128ef | 2453 | |
ef922c4a JM |
2454 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join", |
2455 | scan_res ? (int) scan_res->num : -1); | |
2456 | ||
2457 | if (scan_res) | |
2458 | wpas_p2p_scan_res_handler(wpa_s, scan_res); | |
b22128ef | 2459 | |
f8d0131a JM |
2460 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
2461 | wpa_s->pending_join_iface_addr); | |
54960629 AL |
2462 | if (freq < 0 && |
2463 | p2p_get_interface_addr(wpa_s->global->p2p, | |
2464 | wpa_s->pending_join_dev_addr, | |
2465 | iface_addr) == 0 && | |
2466 | os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0) | |
2467 | { | |
2468 | wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface " | |
2469 | "address for join from " MACSTR " to " MACSTR | |
2470 | " based on newly discovered P2P peer entry", | |
2471 | MAC2STR(wpa_s->pending_join_iface_addr), | |
2472 | MAC2STR(iface_addr)); | |
2473 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, | |
2474 | ETH_ALEN); | |
2475 | ||
2476 | freq = p2p_get_oper_freq(wpa_s->global->p2p, | |
2477 | wpa_s->pending_join_iface_addr); | |
2478 | } | |
f8d0131a JM |
2479 | if (freq >= 0) { |
2480 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " | |
2481 | "from P2P peer table: %d MHz", freq); | |
2482 | } | |
ef922c4a | 2483 | bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr); |
b22128ef | 2484 | if (bss) { |
f8d0131a JM |
2485 | freq = bss->freq; |
2486 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " | |
2487 | "from BSS table: %d MHz", freq); | |
2488 | } | |
2489 | if (freq > 0) { | |
b22128ef JM |
2490 | u16 method; |
2491 | ||
2492 | wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request " | |
2493 | "prior to joining an existing group (GO " MACSTR | |
2494 | " freq=%u MHz)", | |
f8d0131a | 2495 | MAC2STR(wpa_s->pending_join_dev_addr), freq); |
b22128ef JM |
2496 | wpa_s->pending_pd_before_join = 1; |
2497 | ||
ef922c4a | 2498 | switch (wpa_s->pending_join_wps_method) { |
b22128ef JM |
2499 | case WPS_PIN_LABEL: |
2500 | case WPS_PIN_DISPLAY: | |
2501 | method = WPS_CONFIG_KEYPAD; | |
2502 | break; | |
2503 | case WPS_PIN_KEYPAD: | |
2504 | method = WPS_CONFIG_DISPLAY; | |
2505 | break; | |
2506 | case WPS_PBC: | |
2507 | method = WPS_CONFIG_PUSHBUTTON; | |
2508 | break; | |
2509 | default: | |
2510 | method = 0; | |
2511 | break; | |
2512 | } | |
2513 | ||
ef922c4a JM |
2514 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
2515 | wpa_s->pending_join_dev_addr, method, 1) | |
b22128ef JM |
2516 | < 0) { |
2517 | wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision " | |
2518 | "Discovery Request before joining an " | |
2519 | "existing group"); | |
2520 | wpa_s->pending_pd_before_join = 0; | |
2521 | goto start; | |
2522 | } | |
2523 | ||
2524 | /* | |
2525 | * Actual join operation will be started from the Action frame | |
2526 | * TX status callback. | |
2527 | */ | |
ef922c4a | 2528 | return; |
b22128ef JM |
2529 | } |
2530 | ||
9b1ab931 JM |
2531 | wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later"); |
2532 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | |
2533 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); | |
2534 | wpas_p2p_check_join_scan_limit(wpa_s); | |
2535 | return; | |
b22128ef JM |
2536 | |
2537 | start: | |
2538 | /* Start join operation immediately */ | |
ef922c4a JM |
2539 | wpas_p2p_join_start(wpa_s); |
2540 | } | |
2541 | ||
2542 | ||
2543 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) | |
2544 | { | |
2545 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
2546 | int ret; | |
2547 | struct wpa_driver_scan_params params; | |
2548 | struct wpabuf *wps_ie, *ies; | |
2549 | ||
2550 | os_memset(¶ms, 0, sizeof(params)); | |
2551 | ||
2552 | /* P2P Wildcard SSID */ | |
2553 | params.num_ssids = 1; | |
2554 | params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; | |
2555 | params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; | |
2556 | ||
2557 | wpa_s->wps->dev.p2p = 1; | |
2558 | wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid, | |
2559 | WPS_REQ_ENROLLEE); | |
2560 | if (wps_ie == NULL) { | |
2561 | wpas_p2p_scan_res_join(wpa_s, NULL); | |
2562 | return; | |
2563 | } | |
2564 | ||
2565 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100); | |
2566 | if (ies == NULL) { | |
2567 | wpabuf_free(wps_ie); | |
2568 | wpas_p2p_scan_res_join(wpa_s, NULL); | |
2569 | return; | |
2570 | } | |
2571 | wpabuf_put_buf(ies, wps_ie); | |
2572 | wpabuf_free(wps_ie); | |
2573 | ||
2574 | p2p_scan_ie(wpa_s->global->p2p, ies); | |
2575 | ||
2576 | params.extra_ies = wpabuf_head(ies); | |
2577 | params.extra_ies_len = wpabuf_len(ies); | |
2578 | ||
2579 | /* | |
2580 | * Run a scan to update BSS table and start Provision Discovery once | |
2581 | * the new scan results become available. | |
2582 | */ | |
2583 | wpa_s->scan_res_handler = wpas_p2p_scan_res_join; | |
2584 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) | |
2585 | ret = ieee80211_sta_req_scan(wpa_s, ¶ms); | |
2586 | else | |
2587 | ret = wpa_drv_scan(wpa_s, ¶ms); | |
2588 | ||
2589 | wpabuf_free(ies); | |
2590 | ||
2591 | if (ret) { | |
2592 | wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - " | |
2593 | "try again later"); | |
2594 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | |
2595 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); | |
9b1ab931 | 2596 | wpas_p2p_check_join_scan_limit(wpa_s); |
ef922c4a JM |
2597 | } |
2598 | } | |
2599 | ||
2600 | ||
2601 | static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, | |
2602 | const u8 *dev_addr, enum p2p_wps_method wps_method) | |
2603 | { | |
2604 | wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface " | |
2605 | MACSTR " dev " MACSTR ")", | |
2606 | MAC2STR(iface_addr), MAC2STR(dev_addr)); | |
2607 | ||
2608 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN); | |
2609 | os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN); | |
2610 | wpa_s->pending_join_wps_method = wps_method; | |
2611 | ||
2612 | /* Make sure we are not running find during connection establishment */ | |
2613 | wpas_p2p_stop_find(wpa_s); | |
2614 | ||
9b1ab931 | 2615 | wpa_s->p2p_join_scan_count = 0; |
ef922c4a JM |
2616 | wpas_p2p_join_scan(wpa_s, NULL); |
2617 | return 0; | |
b22128ef JM |
2618 | } |
2619 | ||
2620 | ||
2621 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s) | |
2622 | { | |
2623 | struct wpa_supplicant *group; | |
2624 | struct p2p_go_neg_results res; | |
2625 | ||
2626 | group = wpas_p2p_get_group_iface(wpa_s, 0, 0); | |
2627 | if (group == NULL) | |
2628 | return -1; | |
3c5126a4 | 2629 | if (group != wpa_s) { |
b22128ef JM |
2630 | os_memcpy(group->p2p_pin, wpa_s->p2p_pin, |
2631 | sizeof(group->p2p_pin)); | |
3c5126a4 JM |
2632 | group->p2p_wps_method = wpa_s->p2p_wps_method; |
2633 | } | |
b22128ef JM |
2634 | |
2635 | group->p2p_in_provisioning = 1; | |
2636 | ||
2637 | os_memset(&res, 0, sizeof(res)); | |
2638 | os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr, | |
2639 | ETH_ALEN); | |
2640 | res.wps_method = wpa_s->pending_join_wps_method; | |
2641 | wpas_start_wps_enrollee(group, &res); | |
2642 | ||
3094d483 JM |
2643 | /* |
2644 | * Allow a longer timeout for join-a-running-group than normal 15 | |
2645 | * second group formation timeout since the GO may not have authorized | |
2646 | * our connection yet. | |
2647 | */ | |
2648 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); | |
2649 | eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout, | |
2650 | wpa_s, NULL); | |
2651 | ||
b22128ef JM |
2652 | return 0; |
2653 | } | |
2654 | ||
2655 | ||
2656 | /** | |
2657 | * wpas_p2p_connect - Request P2P Group Formation to be started | |
2658 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
2659 | * @peer_addr: Address of the peer P2P Device | |
2660 | * @pin: PIN to use during provisioning or %NULL to indicate PBC mode | |
2661 | * @persistent_group: Whether to create a persistent group | |
2662 | * @join: Whether to join an existing group (as a client) instead of starting | |
2663 | * Group Owner negotiation; @peer_addr is BSSID in that case | |
2664 | * @auth: Whether to only authorize the connection instead of doing that and | |
2665 | * initiating Group Owner negotiation | |
2666 | * @go_intent: GO Intent or -1 to use default | |
2667 | * @freq: Frequency for the group or 0 for auto-selection | |
d054a462 JM |
2668 | * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified |
2669 | * failure, -2 on failure due to channel not currently available, | |
2670 | * -3 if forced channel is not supported | |
b22128ef JM |
2671 | */ |
2672 | int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr, | |
2673 | const char *pin, enum p2p_wps_method wps_method, | |
2674 | int persistent_group, int join, int auth, int go_intent, | |
2675 | int freq) | |
2676 | { | |
d054a462 | 2677 | int force_freq = 0, oper_freq = 0; |
b22128ef JM |
2678 | u8 bssid[ETH_ALEN]; |
2679 | int ret = 0; | |
2680 | enum wpa_driver_if_type iftype; | |
2681 | ||
2682 | if (go_intent < 0) | |
2683 | go_intent = wpa_s->conf->p2p_go_intent; | |
2684 | ||
2685 | if (!auth) | |
2686 | wpa_s->p2p_long_listen = 0; | |
2687 | ||
3c5126a4 JM |
2688 | wpa_s->p2p_wps_method = wps_method; |
2689 | ||
b22128ef JM |
2690 | if (pin) |
2691 | os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin)); | |
2692 | else if (wps_method == WPS_PIN_DISPLAY) { | |
2693 | ret = wps_generate_pin(); | |
2694 | os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d", | |
2695 | ret); | |
2696 | wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s", | |
2697 | wpa_s->p2p_pin); | |
2698 | } else | |
2699 | wpa_s->p2p_pin[0] = '\0'; | |
2700 | ||
2701 | if (join) { | |
4147a2cc | 2702 | u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN]; |
108def93 JM |
2703 | if (auth) { |
2704 | wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to " | |
2705 | "connect a running group from " MACSTR, | |
2706 | MAC2STR(peer_addr)); | |
2707 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); | |
2708 | return ret; | |
2709 | } | |
4147a2cc | 2710 | os_memcpy(dev_addr, peer_addr, ETH_ALEN); |
b22128ef | 2711 | if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr, |
4147a2cc | 2712 | iface_addr) < 0) { |
b22128ef | 2713 | os_memcpy(iface_addr, peer_addr, ETH_ALEN); |
4147a2cc JM |
2714 | p2p_get_dev_addr(wpa_s->global->p2p, peer_addr, |
2715 | dev_addr); | |
2716 | } | |
2717 | if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) < | |
b22128ef JM |
2718 | 0) |
2719 | return -1; | |
2720 | return ret; | |
2721 | } | |
2722 | ||
d054a462 JM |
2723 | if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 && |
2724 | wpa_s->assoc_freq) | |
2725 | oper_freq = wpa_s->assoc_freq; | |
b22128ef | 2726 | else { |
d054a462 JM |
2727 | oper_freq = wpa_drv_shared_freq(wpa_s); |
2728 | if (oper_freq < 0) | |
2729 | oper_freq = 0; | |
b22128ef JM |
2730 | } |
2731 | ||
d054a462 JM |
2732 | if (freq > 0) { |
2733 | if (!p2p_supported_freq(wpa_s->global->p2p, freq)) { | |
2734 | wpa_printf(MSG_DEBUG, "P2P: The forced channel " | |
2735 | "(%u MHz) is not supported for P2P uses", | |
2736 | freq); | |
2737 | return -3; | |
2738 | } | |
2739 | ||
2740 | if (oper_freq > 0 && freq != oper_freq && | |
2741 | !(wpa_s->drv_flags & | |
2742 | WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) { | |
2743 | wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group " | |
2744 | "on %u MHz while connected on another " | |
2745 | "channel (%u MHz)", freq, oper_freq); | |
2746 | return -2; | |
2747 | } | |
2748 | wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " | |
2749 | "requested channel (%u MHz)", freq); | |
2750 | force_freq = freq; | |
2751 | } else if (oper_freq > 0 && | |
2752 | !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) { | |
2753 | if (!(wpa_s->drv_flags & | |
2754 | WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) { | |
2755 | wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group " | |
2756 | "while connected on non-P2P supported " | |
2757 | "channel (%u MHz)", oper_freq); | |
2758 | return -2; | |
2759 | } | |
2760 | wpa_printf(MSG_DEBUG, "P2P: Current operating channel " | |
2761 | "(%u MHz) not available for P2P - try to use " | |
2762 | "another channel", oper_freq); | |
2763 | force_freq = 0; | |
2764 | } else if (oper_freq > 0) { | |
b22128ef JM |
2765 | wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " |
2766 | "channel we are already using (%u MHz) on another " | |
d054a462 JM |
2767 | "interface", oper_freq); |
2768 | force_freq = oper_freq; | |
b22128ef JM |
2769 | } |
2770 | ||
2771 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); | |
2772 | ||
2773 | if (!wpa_s->create_p2p_iface) { | |
2774 | if (auth) { | |
2775 | if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method, | |
2776 | go_intent, wpa_s->own_addr, | |
2777 | force_freq, persistent_group) | |
2778 | < 0) | |
2779 | return -1; | |
2780 | return ret; | |
2781 | } | |
2782 | if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, | |
2783 | go_intent, wpa_s->own_addr, | |
2784 | force_freq, persistent_group) < 0) | |
2785 | return -1; | |
2786 | return ret; | |
2787 | } | |
2788 | ||
2789 | /* Prepare to add a new interface for the group */ | |
2790 | iftype = WPA_IF_P2P_GROUP; | |
2791 | if (join) | |
2792 | iftype = WPA_IF_P2P_CLIENT; | |
2793 | else if (go_intent == 15) | |
2794 | iftype = WPA_IF_P2P_GO; | |
2795 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { | |
2796 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " | |
2797 | "interface for the group"); | |
2798 | return -1; | |
2799 | } | |
2800 | ||
2801 | if (auth) { | |
2802 | if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method, | |
2803 | go_intent, | |
2804 | wpa_s->pending_interface_addr, | |
2805 | force_freq, persistent_group) < 0) | |
2806 | return -1; | |
2807 | return ret; | |
2808 | } | |
2809 | if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent, | |
2810 | wpa_s->pending_interface_addr, | |
2811 | force_freq, persistent_group) < 0) { | |
2812 | wpas_p2p_remove_pending_group_interface(wpa_s); | |
2813 | return -1; | |
2814 | } | |
2815 | return ret; | |
2816 | } | |
2817 | ||
2818 | ||
2819 | /** | |
2820 | * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start | |
2821 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
2822 | * @freq: Frequency of the channel in MHz | |
2823 | * @duration: Duration of the stay on the channel in milliseconds | |
2824 | * | |
2825 | * This callback is called when the driver indicates that it has started the | |
2826 | * requested remain-on-channel duration. | |
2827 | */ | |
2828 | void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s, | |
2829 | unsigned int freq, unsigned int duration) | |
2830 | { | |
07a30a66 | 2831 | wpa_s->roc_waiting_drv_freq = 0; |
b22128ef JM |
2832 | wpa_s->off_channel_freq = freq; |
2833 | wpas_send_action_cb(wpa_s, NULL); | |
2834 | if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) { | |
2835 | p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq, | |
2836 | wpa_s->pending_listen_duration); | |
2837 | wpa_s->pending_listen_freq = 0; | |
2838 | } | |
2839 | } | |
2840 | ||
2841 | ||
2842 | static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, | |
2843 | unsigned int timeout) | |
2844 | { | |
2845 | /* Limit maximum Listen state time based on driver limitation. */ | |
2846 | if (timeout > wpa_s->max_remain_on_chan) | |
2847 | timeout = wpa_s->max_remain_on_chan; | |
2848 | ||
2849 | return p2p_listen(wpa_s->global->p2p, timeout); | |
2850 | } | |
2851 | ||
2852 | ||
2853 | /** | |
2854 | * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout | |
2855 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
2856 | * @freq: Frequency of the channel in MHz | |
2857 | * | |
2858 | * This callback is called when the driver indicates that a remain-on-channel | |
2859 | * operation has been completed, i.e., the duration on the requested channel | |
2860 | * has timed out. | |
2861 | */ | |
2862 | void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s, | |
2863 | unsigned int freq) | |
2864 | { | |
1cc3a29d JM |
2865 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback " |
2866 | "(p2p_long_listen=%d pending_action_tx=%p)", | |
2867 | wpa_s->p2p_long_listen, wpa_s->pending_action_tx); | |
b22128ef JM |
2868 | wpa_s->off_channel_freq = 0; |
2869 | if (p2p_listen_end(wpa_s->global->p2p, freq) > 0) | |
2870 | return; /* P2P module started a new operation */ | |
2871 | if (wpa_s->pending_action_tx) | |
2872 | return; | |
2873 | if (wpa_s->p2p_long_listen > 0) | |
2874 | wpa_s->p2p_long_listen -= 5; | |
2875 | if (wpa_s->p2p_long_listen > 0) { | |
2876 | wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state"); | |
2877 | wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen * 1000); | |
2878 | } | |
2879 | } | |
2880 | ||
2881 | ||
2882 | /** | |
2883 | * wpas_p2p_group_remove - Remove a P2P group | |
2884 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
2885 | * @ifname: Network interface name of the group interface or "*" to remove all | |
2886 | * groups | |
2887 | * Returns: 0 on success, -1 on failure | |
2888 | * | |
2889 | * This function is used to remove a P2P group. This can be used to disconnect | |
2890 | * from a group in which the local end is a P2P Client or to end a P2P Group in | |
2891 | * case the local end is the Group Owner. If a virtual network interface was | |
2892 | * created for this group, that interface will be removed. Otherwise, only the | |
2893 | * configured P2P group network will be removed from the interface. | |
2894 | */ | |
2895 | int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname) | |
2896 | { | |
2897 | struct wpa_global *global = wpa_s->global; | |
2898 | ||
2899 | if (os_strcmp(ifname, "*") == 0) { | |
2900 | struct wpa_supplicant *prev; | |
2901 | wpa_s = global->ifaces; | |
2902 | while (wpa_s) { | |
2903 | prev = wpa_s; | |
2904 | wpa_s = wpa_s->next; | |
58e54293 | 2905 | prev->removal_reason = P2P_GROUP_REMOVAL_REQUESTED; |
b22128ef JM |
2906 | wpas_p2p_group_delete(prev); |
2907 | } | |
2908 | return 0; | |
2909 | } | |
2910 | ||
2911 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
2912 | if (os_strcmp(wpa_s->ifname, ifname) == 0) | |
2913 | break; | |
2914 | } | |
2915 | ||
2916 | if (wpa_s == NULL) | |
2917 | return -1; | |
2918 | ||
3071e181 | 2919 | wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED; |
b22128ef JM |
2920 | wpas_p2p_group_delete(wpa_s); |
2921 | ||
2922 | return 0; | |
2923 | } | |
2924 | ||
2925 | ||
2926 | static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s, | |
2927 | struct p2p_go_neg_results *params, | |
2928 | int freq) | |
2929 | { | |
2930 | u8 bssid[ETH_ALEN]; | |
2931 | int res; | |
2932 | ||
2933 | os_memset(params, 0, sizeof(*params)); | |
2934 | params->role_go = 1; | |
7cfc4ac3 AGS |
2935 | if (freq) { |
2936 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced " | |
2937 | "frequency %d MHz", freq); | |
b22128ef | 2938 | params->freq = freq; |
7cfc4ac3 AGS |
2939 | } else if (wpa_s->conf->p2p_oper_reg_class == 81 && |
2940 | wpa_s->conf->p2p_oper_channel >= 1 && | |
2941 | wpa_s->conf->p2p_oper_channel <= 11) { | |
b22128ef | 2942 | params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel; |
7cfc4ac3 AGS |
2943 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
2944 | "frequency %d MHz", params->freq); | |
2945 | } else if (wpa_s->conf->p2p_oper_reg_class == 115 || | |
2946 | wpa_s->conf->p2p_oper_reg_class == 118) { | |
b22128ef | 2947 | params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel; |
7cfc4ac3 AGS |
2948 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
2949 | "frequency %d MHz", params->freq); | |
2950 | } else if (wpa_s->conf->p2p_oper_channel == 0 && | |
2951 | wpa_s->best_overall_freq > 0 && | |
2952 | p2p_supported_freq(wpa_s->global->p2p, | |
2953 | wpa_s->best_overall_freq)) { | |
2954 | params->freq = wpa_s->best_overall_freq; | |
2955 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall " | |
2956 | "channel %d MHz", params->freq); | |
2957 | } else if (wpa_s->conf->p2p_oper_channel == 0 && | |
2958 | wpa_s->best_24_freq > 0 && | |
2959 | p2p_supported_freq(wpa_s->global->p2p, | |
2960 | wpa_s->best_24_freq)) { | |
2961 | params->freq = wpa_s->best_24_freq; | |
2962 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz " | |
2963 | "channel %d MHz", params->freq); | |
2964 | } else if (wpa_s->conf->p2p_oper_channel == 0 && | |
2965 | wpa_s->best_5_freq > 0 && | |
2966 | p2p_supported_freq(wpa_s->global->p2p, | |
2967 | wpa_s->best_5_freq)) { | |
2968 | params->freq = wpa_s->best_5_freq; | |
2969 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz " | |
2970 | "channel %d MHz", params->freq); | |
2971 | } else { | |
2972 | params->freq = 2412; | |
2973 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference " | |
2974 | "known)", params->freq); | |
2975 | } | |
2976 | ||
b22128ef JM |
2977 | if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 && |
2978 | wpa_s->assoc_freq && !freq) { | |
2979 | wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are " | |
2980 | "already using"); | |
2981 | params->freq = wpa_s->assoc_freq; | |
2982 | } | |
2983 | ||
2984 | res = wpa_drv_shared_freq(wpa_s); | |
2985 | if (res > 0 && !freq) { | |
2986 | wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are " | |
2987 | "already using on a shared interface"); | |
2988 | params->freq = res; | |
2989 | } | |
2990 | } | |
2991 | ||
2992 | ||
2993 | static struct wpa_supplicant * | |
2994 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, | |
2995 | int go) | |
2996 | { | |
2997 | struct wpa_supplicant *group_wpa_s; | |
2998 | ||
2999 | if (!wpas_p2p_create_iface(wpa_s)) | |
3000 | return wpa_s; | |
3001 | ||
3002 | if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : | |
3003 | WPA_IF_P2P_CLIENT) < 0) | |
3004 | return NULL; | |
3005 | group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go); | |
3006 | if (group_wpa_s == NULL) { | |
3007 | wpas_p2p_remove_pending_group_interface(wpa_s); | |
3008 | return NULL; | |
3009 | } | |
3010 | ||
3011 | return group_wpa_s; | |
3012 | } | |
3013 | ||
3014 | ||
3015 | /** | |
3016 | * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner | |
3017 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
3018 | * @persistent_group: Whether to create a persistent group | |
3019 | * @freq: Frequency for the group or 0 to indicate no hardcoding | |
3020 | * Returns: 0 on success, -1 on failure | |
3021 | * | |
3022 | * This function creates a new P2P group with the local end as the Group Owner, | |
3023 | * i.e., without using Group Owner Negotiation. | |
3024 | */ | |
3025 | int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group, | |
3026 | int freq) | |
3027 | { | |
3028 | struct p2p_go_neg_results params; | |
7cfc4ac3 AGS |
3029 | unsigned int r; |
3030 | ||
3031 | if (freq == 2) { | |
3032 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz " | |
3033 | "band"); | |
3034 | if (wpa_s->best_24_freq > 0 && | |
3035 | p2p_supported_freq(wpa_s->global->p2p, | |
3036 | wpa_s->best_24_freq)) { | |
3037 | freq = wpa_s->best_24_freq; | |
3038 | wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band " | |
3039 | "channel: %d MHz", freq); | |
3040 | } else { | |
3041 | os_get_random((u8 *) &r, sizeof(r)); | |
3042 | freq = 2412 + (r % 3) * 25; | |
3043 | wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band " | |
3044 | "channel: %d MHz", freq); | |
3045 | } | |
3046 | } | |
3047 | ||
3048 | if (freq == 5) { | |
3049 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz " | |
3050 | "band"); | |
3051 | if (wpa_s->best_5_freq > 0 && | |
3052 | p2p_supported_freq(wpa_s->global->p2p, | |
3053 | wpa_s->best_5_freq)) { | |
3054 | freq = wpa_s->best_5_freq; | |
3055 | wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band " | |
3056 | "channel: %d MHz", freq); | |
3057 | } else { | |
3058 | os_get_random((u8 *) &r, sizeof(r)); | |
3059 | freq = 5180 + (r % 4) * 20; | |
3060 | if (!p2p_supported_freq(wpa_s->global->p2p, freq)) { | |
3061 | wpa_printf(MSG_DEBUG, "P2P: Could not select " | |
3062 | "5 GHz channel for P2P group"); | |
3063 | return -1; | |
3064 | } | |
3065 | wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band " | |
3066 | "channel: %d MHz", freq); | |
3067 | } | |
3068 | } | |
b22128ef | 3069 | |
4ae4650b JM |
3070 | if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) { |
3071 | wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO " | |
3072 | "(%u MHz) is not supported for P2P uses", | |
3073 | freq); | |
3074 | return -1; | |
3075 | } | |
3076 | ||
b22128ef JM |
3077 | wpas_p2p_init_go_params(wpa_s, ¶ms, freq); |
3078 | p2p_go_params(wpa_s->global->p2p, ¶ms); | |
3079 | params.persistent_group = persistent_group; | |
3080 | ||
3081 | wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1); | |
3082 | if (wpa_s == NULL) | |
3083 | return -1; | |
3084 | wpas_start_wps_go(wpa_s, ¶ms, 0); | |
3085 | ||
3086 | return 0; | |
3087 | } | |
3088 | ||
3089 | ||
3090 | static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s, | |
3091 | struct wpa_ssid *params, int addr_allocated) | |
3092 | { | |
3093 | struct wpa_ssid *ssid; | |
3094 | ||
3095 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0); | |
3096 | if (wpa_s == NULL) | |
3097 | return -1; | |
3098 | ||
3099 | wpa_supplicant_ap_deinit(wpa_s); | |
3100 | ||
3101 | ssid = wpa_config_add_network(wpa_s->conf); | |
3102 | if (ssid == NULL) | |
3103 | return -1; | |
3104 | wpas_notify_network_added(wpa_s, ssid); | |
3105 | wpa_config_set_network_defaults(ssid); | |
3106 | ssid->temporary = 1; | |
3107 | ssid->proto = WPA_PROTO_RSN; | |
3108 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; | |
3109 | ssid->group_cipher = WPA_CIPHER_CCMP; | |
3110 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; | |
3111 | ssid->ssid = os_malloc(params->ssid_len); | |
3112 | if (ssid->ssid == NULL) { | |
3113 | wpas_notify_network_removed(wpa_s, ssid); | |
3114 | wpa_config_remove_network(wpa_s->conf, ssid->id); | |
3115 | return -1; | |
3116 | } | |
3117 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); | |
3118 | ssid->ssid_len = params->ssid_len; | |
3119 | ssid->p2p_group = 1; | |
3120 | if (params->psk_set) { | |
3121 | os_memcpy(ssid->psk, params->psk, 32); | |
3122 | ssid->psk_set = 1; | |
3123 | } | |
3124 | if (params->passphrase) | |
3125 | ssid->passphrase = os_strdup(params->passphrase); | |
3126 | ||
3127 | wpa_supplicant_select_network(wpa_s, ssid); | |
3128 | ||
3129 | wpa_s->show_group_started = 1; | |
3130 | ||
3131 | return 0; | |
3132 | } | |
3133 | ||
3134 | ||
3135 | int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s, | |
3136 | struct wpa_ssid *ssid, int addr_allocated, | |
3137 | int freq) | |
3138 | { | |
3139 | struct p2p_go_neg_results params; | |
3140 | ||
3141 | if (ssid->disabled != 2 || ssid->ssid == NULL) | |
3142 | return -1; | |
3143 | ||
3144 | wpa_s->p2p_long_listen = 0; | |
3145 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); | |
3146 | ||
3147 | if (ssid->mode == WPAS_MODE_INFRA) | |
3148 | return wpas_start_p2p_client(wpa_s, ssid, addr_allocated); | |
3149 | ||
3150 | if (ssid->mode != WPAS_MODE_P2P_GO) | |
3151 | return -1; | |
3152 | ||
3153 | wpas_p2p_init_go_params(wpa_s, ¶ms, freq); | |
3154 | ||
3155 | params.role_go = 1; | |
3156 | if (ssid->passphrase == NULL || | |
3157 | os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) { | |
3158 | wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent " | |
3159 | "group"); | |
3160 | return -1; | |
3161 | } | |
3162 | os_strlcpy(params.passphrase, ssid->passphrase, | |
3163 | sizeof(params.passphrase)); | |
3164 | os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len); | |
3165 | params.ssid_len = ssid->ssid_len; | |
3166 | params.persistent_group = 1; | |
3167 | ||
3168 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1); | |
3169 | if (wpa_s == NULL) | |
3170 | return -1; | |
3171 | ||
3172 | wpas_start_wps_go(wpa_s, ¶ms, 0); | |
3173 | ||
3174 | return 0; | |
3175 | } | |
3176 | ||
3177 | ||
3178 | static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies, | |
3179 | struct wpabuf *proberesp_ies) | |
3180 | { | |
3181 | struct wpa_supplicant *wpa_s = ctx; | |
3182 | if (wpa_s->ap_iface) { | |
3183 | struct hostapd_data *hapd = wpa_s->ap_iface->bss[0]; | |
3184 | if (beacon_ies) { | |
3185 | wpabuf_free(hapd->p2p_beacon_ie); | |
3186 | hapd->p2p_beacon_ie = beacon_ies; | |
3187 | } | |
3188 | wpabuf_free(hapd->p2p_probe_resp_ie); | |
3189 | hapd->p2p_probe_resp_ie = proberesp_ies; | |
3190 | } else { | |
3191 | wpabuf_free(beacon_ies); | |
3192 | wpabuf_free(proberesp_ies); | |
3193 | } | |
3194 | wpa_supplicant_ap_update_beacon(wpa_s); | |
3195 | } | |
3196 | ||
3197 | ||
3071e181 JM |
3198 | static void wpas_p2p_idle_update(void *ctx, int idle) |
3199 | { | |
3200 | struct wpa_supplicant *wpa_s = ctx; | |
3201 | if (!wpa_s->ap_iface) | |
3202 | return; | |
3203 | wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not "); | |
3204 | if (idle) | |
3205 | wpas_p2p_set_group_idle_timeout(wpa_s); | |
3206 | else | |
3207 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); | |
3208 | } | |
3209 | ||
3210 | ||
b22128ef JM |
3211 | struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s, |
3212 | int persistent_group, | |
3213 | int group_formation) | |
3214 | { | |
3215 | struct p2p_group *group; | |
3216 | struct p2p_group_config *cfg; | |
3217 | ||
3218 | cfg = os_zalloc(sizeof(*cfg)); | |
3219 | if (cfg == NULL) | |
3220 | return NULL; | |
3221 | ||
3222 | cfg->persistent_group = persistent_group; | |
3223 | os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN); | |
3f4ce13f JM |
3224 | if (wpa_s->max_stations && |
3225 | wpa_s->max_stations < wpa_s->conf->max_num_sta) | |
3226 | cfg->max_clients = wpa_s->max_stations; | |
3227 | else | |
3228 | cfg->max_clients = wpa_s->conf->max_num_sta; | |
b22128ef JM |
3229 | cfg->cb_ctx = wpa_s; |
3230 | cfg->ie_update = wpas_p2p_ie_update; | |
3071e181 | 3231 | cfg->idle_update = wpas_p2p_idle_update; |
b22128ef JM |
3232 | |
3233 | group = p2p_group_init(wpa_s->global->p2p, cfg); | |
3234 | if (group == NULL) | |
3235 | os_free(cfg); | |
3236 | if (!group_formation) | |
3237 | p2p_group_notif_formation_done(group); | |
3238 | wpa_s->p2p_group = group; | |
3239 | return group; | |
3240 | } | |
3241 | ||
3242 | ||
3243 | void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr, | |
3244 | int registrar) | |
3245 | { | |
3246 | if (!wpa_s->p2p_in_provisioning) { | |
3247 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P " | |
3248 | "provisioning not in progress"); | |
3249 | return; | |
3250 | } | |
3251 | ||
3252 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, | |
3253 | NULL); | |
3254 | if (wpa_s->global->p2p) | |
3255 | p2p_wps_success_cb(wpa_s->global->p2p, peer_addr); | |
3256 | wpas_group_formation_completed(wpa_s, 1); | |
3257 | } | |
3258 | ||
3259 | ||
3260 | int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr, | |
3261 | const char *config_method) | |
3262 | { | |
3263 | u16 config_methods; | |
3264 | ||
3265 | if (os_strcmp(config_method, "display") == 0) | |
3266 | config_methods = WPS_CONFIG_DISPLAY; | |
3267 | else if (os_strcmp(config_method, "keypad") == 0) | |
3268 | config_methods = WPS_CONFIG_KEYPAD; | |
3269 | else if (os_strcmp(config_method, "pbc") == 0 || | |
3270 | os_strcmp(config_method, "pushbutton") == 0) | |
3271 | config_methods = WPS_CONFIG_PUSHBUTTON; | |
3272 | else | |
3273 | return -1; | |
3274 | ||
3275 | if (wpa_s->global->p2p == NULL) | |
3276 | return -1; | |
3277 | ||
3278 | return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, | |
3279 | config_methods, 0); | |
3280 | } | |
3281 | ||
3282 | ||
3283 | int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, | |
3284 | char *end) | |
3285 | { | |
3286 | return p2p_scan_result_text(ies, ies_len, buf, end); | |
3287 | } | |
3288 | ||
3289 | ||
1cc3a29d JM |
3290 | static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s) |
3291 | { | |
3292 | if (!wpa_s->pending_action_tx) | |
3293 | return; | |
3294 | ||
3295 | wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new " | |
3296 | "operation request"); | |
3297 | wpabuf_free(wpa_s->pending_action_tx); | |
3298 | wpa_s->pending_action_tx = NULL; | |
3299 | } | |
3300 | ||
3301 | ||
b22128ef JM |
3302 | int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout, |
3303 | enum p2p_discovery_type type) | |
3304 | { | |
1cc3a29d | 3305 | wpas_p2p_clear_pending_action_tx(wpa_s); |
b22128ef JM |
3306 | wpa_s->p2p_long_listen = 0; |
3307 | ||
3308 | return p2p_find(wpa_s->global->p2p, timeout, type); | |
3309 | } | |
3310 | ||
3311 | ||
3312 | void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s) | |
3313 | { | |
1cc3a29d | 3314 | wpas_p2p_clear_pending_action_tx(wpa_s); |
b22128ef | 3315 | wpa_s->p2p_long_listen = 0; |
ef922c4a | 3316 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
b22128ef JM |
3317 | |
3318 | p2p_stop_find(wpa_s->global->p2p); | |
3319 | ||
3320 | wpas_p2p_remove_pending_group_interface(wpa_s); | |
3321 | } | |
3322 | ||
3323 | ||
3324 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx) | |
3325 | { | |
3326 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
3327 | wpa_s->p2p_long_listen = 0; | |
3328 | } | |
3329 | ||
3330 | ||
3331 | int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout) | |
3332 | { | |
3333 | int res; | |
3334 | ||
1cc3a29d JM |
3335 | wpas_p2p_clear_pending_action_tx(wpa_s); |
3336 | ||
b22128ef JM |
3337 | if (timeout == 0) { |
3338 | /* | |
3339 | * This is a request for unlimited Listen state. However, at | |
3340 | * least for now, this is mapped to a Listen state for one | |
3341 | * hour. | |
3342 | */ | |
3343 | timeout = 3600; | |
3344 | } | |
3345 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); | |
3346 | wpa_s->p2p_long_listen = 0; | |
3347 | ||
3348 | res = wpas_p2p_listen_start(wpa_s, timeout * 1000); | |
3349 | if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) { | |
3350 | wpa_s->p2p_long_listen = timeout; | |
3351 | eloop_register_timeout(timeout, 0, | |
3352 | wpas_p2p_long_listen_timeout, | |
3353 | wpa_s, NULL); | |
3354 | } | |
3355 | ||
3356 | return res; | |
3357 | } | |
3358 | ||
3359 | ||
4c08c0bd | 3360 | int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, |
b22128ef JM |
3361 | u8 *buf, size_t len, int p2p_group) |
3362 | { | |
4c08c0bd JM |
3363 | struct wpabuf *p2p_ie; |
3364 | int ret; | |
3365 | ||
b22128ef JM |
3366 | if (wpa_s->global->p2p_disabled) |
3367 | return -1; | |
3368 | if (wpa_s->global->p2p == NULL) | |
3369 | return -1; | |
e1f1509b JM |
3370 | if (bss == NULL) |
3371 | return -1; | |
b22128ef | 3372 | |
4c08c0bd JM |
3373 | p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
3374 | ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len, | |
3375 | p2p_group, p2p_ie); | |
3376 | wpabuf_free(p2p_ie); | |
3377 | ||
3378 | return ret; | |
b22128ef JM |
3379 | } |
3380 | ||
3381 | ||
3382 | int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr, | |
3383 | const u8 *ie, size_t ie_len) | |
3384 | { | |
3385 | if (wpa_s->global->p2p_disabled) | |
3386 | return 0; | |
3387 | if (wpa_s->global->p2p == NULL) | |
3388 | return 0; | |
3389 | ||
3390 | return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len); | |
3391 | } | |
3392 | ||
3393 | ||
3394 | void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da, | |
3395 | const u8 *sa, const u8 *bssid, | |
3396 | u8 category, const u8 *data, size_t len, int freq) | |
3397 | { | |
3398 | if (wpa_s->global->p2p_disabled) | |
3399 | return; | |
3400 | if (wpa_s->global->p2p == NULL) | |
3401 | return; | |
3402 | ||
3403 | p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len, | |
3404 | freq); | |
3405 | } | |
3406 | ||
3407 | ||
3408 | void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies) | |
3409 | { | |
3410 | if (wpa_s->global->p2p_disabled) | |
3411 | return; | |
3412 | if (wpa_s->global->p2p == NULL) | |
3413 | return; | |
3414 | ||
3415 | p2p_scan_ie(wpa_s->global->p2p, ies); | |
3416 | } | |
3417 | ||
3418 | ||
3419 | void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s) | |
3420 | { | |
3421 | p2p_group_deinit(wpa_s->p2p_group); | |
3422 | wpa_s->p2p_group = NULL; | |
3423 | } | |
3424 | ||
3425 | ||
3426 | int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr) | |
3427 | { | |
3428 | wpa_s->p2p_long_listen = 0; | |
3429 | ||
3430 | return p2p_reject(wpa_s->global->p2p, addr); | |
3431 | } | |
3432 | ||
3433 | ||
3434 | /* Invite to reinvoke a persistent group */ | |
3435 | int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr, | |
3436 | struct wpa_ssid *ssid, const u8 *go_dev_addr) | |
3437 | { | |
3438 | enum p2p_invite_role role; | |
3439 | u8 *bssid = NULL; | |
3440 | ||
3441 | if (ssid->mode == WPAS_MODE_P2P_GO) { | |
3442 | role = P2P_INVITE_ROLE_GO; | |
3443 | if (peer_addr == NULL) { | |
3444 | wpa_printf(MSG_DEBUG, "P2P: Missing peer " | |
3445 | "address in invitation command"); | |
3446 | return -1; | |
3447 | } | |
3448 | if (wpas_p2p_create_iface(wpa_s)) { | |
3449 | if (wpas_p2p_add_group_interface(wpa_s, | |
3450 | WPA_IF_P2P_GO) < 0) { | |
3451 | wpa_printf(MSG_ERROR, "P2P: Failed to " | |
3452 | "allocate a new interface for the " | |
3453 | "group"); | |
3454 | return -1; | |
3455 | } | |
3456 | bssid = wpa_s->pending_interface_addr; | |
3457 | } else | |
3458 | bssid = wpa_s->own_addr; | |
3459 | } else { | |
3460 | role = P2P_INVITE_ROLE_CLIENT; | |
3461 | peer_addr = ssid->bssid; | |
3462 | } | |
3463 | wpa_s->pending_invite_ssid_id = ssid->id; | |
3464 | ||
3465 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, | |
3466 | ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1); | |
3467 | } | |
3468 | ||
3469 | ||
3470 | /* Invite to join an active group */ | |
3471 | int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname, | |
3472 | const u8 *peer_addr, const u8 *go_dev_addr) | |
3473 | { | |
3474 | struct wpa_global *global = wpa_s->global; | |
3475 | enum p2p_invite_role role; | |
3476 | u8 *bssid = NULL; | |
3477 | struct wpa_ssid *ssid; | |
3478 | ||
3479 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
3480 | if (os_strcmp(wpa_s->ifname, ifname) == 0) | |
3481 | break; | |
3482 | } | |
3483 | if (wpa_s == NULL) { | |
3484 | wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname); | |
3485 | return -1; | |
3486 | } | |
3487 | ||
3488 | ssid = wpa_s->current_ssid; | |
3489 | if (ssid == NULL) { | |
3490 | wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for " | |
3491 | "invitation"); | |
3492 | return -1; | |
3493 | } | |
3494 | ||
3495 | if (ssid->mode == WPAS_MODE_P2P_GO) { | |
3496 | role = P2P_INVITE_ROLE_ACTIVE_GO; | |
3497 | bssid = wpa_s->own_addr; | |
3498 | if (go_dev_addr == NULL) | |
2e062d5d | 3499 | go_dev_addr = wpa_s->parent->own_addr; |
b22128ef JM |
3500 | } else { |
3501 | role = P2P_INVITE_ROLE_CLIENT; | |
3502 | if (wpa_s->wpa_state < WPA_ASSOCIATED) { | |
3503 | wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot " | |
3504 | "invite to current group"); | |
3505 | return -1; | |
3506 | } | |
3507 | bssid = wpa_s->bssid; | |
3508 | if (go_dev_addr == NULL && | |
3509 | !is_zero_ether_addr(wpa_s->go_dev_addr)) | |
3510 | go_dev_addr = wpa_s->go_dev_addr; | |
3511 | } | |
bb79dc72 | 3512 | wpa_s->parent->pending_invite_ssid_id = -1; |
b22128ef JM |
3513 | |
3514 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, | |
d9d6a58c JM |
3515 | ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq, |
3516 | go_dev_addr, 0); | |
b22128ef JM |
3517 | } |
3518 | ||
3519 | ||
3520 | void wpas_p2p_completed(struct wpa_supplicant *wpa_s) | |
3521 | { | |
3522 | struct wpa_ssid *ssid = wpa_s->current_ssid; | |
3523 | const char *ssid_txt; | |
3524 | u8 go_dev_addr[ETH_ALEN]; | |
3525 | int persistent; | |
3526 | ||
3527 | if (!wpa_s->show_group_started || !ssid) | |
3528 | return; | |
3529 | ||
3530 | wpa_s->show_group_started = 0; | |
3531 | ||
3532 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); | |
3533 | os_memset(go_dev_addr, 0, ETH_ALEN); | |
3534 | if (ssid->bssid_set) | |
3535 | os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN); | |
3536 | persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, | |
3537 | ssid->ssid_len); | |
3538 | os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN); | |
3539 | ||
a0a9f3b0 JM |
3540 | if (wpa_s->global->p2p_group_formation == wpa_s) |
3541 | wpa_s->global->p2p_group_formation = NULL; | |
3542 | ||
b22128ef JM |
3543 | if (ssid->passphrase == NULL && ssid->psk_set) { |
3544 | char psk[65]; | |
3545 | wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); | |
3546 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED | |
c481048f AC |
3547 | "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr=" |
3548 | MACSTR "%s", | |
3549 | wpa_s->ifname, ssid_txt, ssid->frequency, psk, | |
3550 | MAC2STR(go_dev_addr), | |
b22128ef JM |
3551 | persistent ? " [PERSISTENT]" : ""); |
3552 | } else { | |
3553 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED | |
c481048f AC |
3554 | "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" " |
3555 | "go_dev_addr=" MACSTR "%s", | |
3556 | wpa_s->ifname, ssid_txt, ssid->frequency, | |
b22128ef JM |
3557 | ssid->passphrase ? ssid->passphrase : "", |
3558 | MAC2STR(go_dev_addr), | |
3559 | persistent ? " [PERSISTENT]" : ""); | |
3560 | } | |
3561 | ||
3562 | if (persistent) | |
3563 | wpas_p2p_store_persistent_group(wpa_s->parent, ssid, | |
3564 | go_dev_addr); | |
3565 | } | |
3566 | ||
3567 | ||
3568 | int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1, | |
3569 | u32 interval1, u32 duration2, u32 interval2) | |
3570 | { | |
3571 | if (wpa_s->wpa_state < WPA_ASSOCIATED || | |
3572 | wpa_s->current_ssid == NULL || | |
3573 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA) | |
3574 | return -1; | |
3575 | ||
3576 | return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid, | |
3577 | wpa_s->own_addr, wpa_s->assoc_freq, | |
3578 | duration1, interval1, duration2, interval2); | |
3579 | } | |
3580 | ||
3581 | ||
3582 | int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period, | |
3583 | unsigned int interval) | |
3584 | { | |
3585 | return p2p_ext_listen(wpa_s->global->p2p, period, interval); | |
3586 | } | |
3587 | ||
3588 | ||
3071e181 JM |
3589 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx) |
3590 | { | |
3591 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
3592 | ||
3593 | if (wpa_s->conf->p2p_group_idle == 0) { | |
3594 | wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - " | |
3595 | "disabled"); | |
3596 | return; | |
3597 | } | |
3598 | ||
3599 | wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate " | |
3600 | "group"); | |
3601 | wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT; | |
3602 | wpas_p2p_group_delete(wpa_s); | |
3603 | } | |
3604 | ||
3605 | ||
3606 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s) | |
3607 | { | |
3608 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); | |
3609 | if (wpa_s->conf->p2p_group_idle == 0) | |
3610 | return; | |
3611 | ||
3612 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) | |
3613 | return; | |
3614 | ||
3615 | wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds", | |
3616 | wpa_s->conf->p2p_group_idle); | |
3617 | eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0, | |
3618 | wpas_p2p_group_idle_timeout, wpa_s, NULL); | |
3619 | } | |
3620 | ||
3621 | ||
b22128ef JM |
3622 | void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, |
3623 | u16 reason_code, const u8 *ie, size_t ie_len) | |
3624 | { | |
3625 | if (wpa_s->global->p2p_disabled) | |
3626 | return; | |
3627 | ||
3628 | p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len); | |
3629 | } | |
3630 | ||
3631 | ||
3632 | void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, | |
3633 | u16 reason_code, const u8 *ie, size_t ie_len) | |
3634 | { | |
3635 | if (wpa_s->global->p2p_disabled) | |
3636 | return; | |
3637 | ||
3638 | p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len); | |
3639 | } | |
3640 | ||
3641 | ||
3642 | void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) | |
3643 | { | |
3644 | struct p2p_data *p2p = wpa_s->global->p2p; | |
3645 | ||
3646 | if (p2p == NULL) | |
3647 | return; | |
3648 | ||
4c010834 KL |
3649 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
3650 | return; | |
3651 | ||
b22128ef JM |
3652 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME) |
3653 | p2p_set_dev_name(p2p, wpa_s->conf->device_name); | |
3654 | ||
3655 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) { | |
3656 | u8 pri_dev_type[8]; | |
3657 | if (wpa_s->conf->device_type) { | |
3658 | if (wps_dev_type_str2bin(wpa_s->conf->device_type, | |
3659 | pri_dev_type) < 0) { | |
3660 | wpa_printf(MSG_ERROR, "P2P: Invalid " | |
3661 | "device_type"); | |
3662 | } else | |
3663 | p2p_set_pri_dev_type(p2p, pri_dev_type); | |
3664 | } | |
3665 | } | |
3666 | ||
3667 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) { | |
3668 | u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8]; | |
3669 | size_t num = 0; | |
3670 | int i; | |
3671 | for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) { | |
3672 | if (wpa_s->conf->sec_device_type[i] == NULL) | |
3673 | continue; | |
3674 | if (wps_dev_type_str2bin( | |
3675 | wpa_s->conf->sec_device_type[i], | |
3676 | sec_dev_type[num]) < 0) { | |
3677 | wpa_printf(MSG_ERROR, "P2P: Invalid " | |
3678 | "sec_device_type"); | |
3679 | continue; | |
3680 | } | |
3681 | num++; | |
3682 | if (num == P2P_SEC_DEVICE_TYPES) | |
3683 | break; | |
3684 | } | |
3685 | p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num); | |
3686 | } | |
3687 | ||
3688 | if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) && | |
3689 | wpa_s->conf->country[0] && wpa_s->conf->country[1]) { | |
3690 | char country[3]; | |
3691 | country[0] = wpa_s->conf->country[0]; | |
3692 | country[1] = wpa_s->conf->country[1]; | |
3693 | country[2] = 0x04; | |
3694 | p2p_set_country(p2p, country); | |
3695 | } | |
3696 | ||
3697 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) { | |
3698 | p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix, | |
3699 | wpa_s->conf->p2p_ssid_postfix ? | |
3700 | os_strlen(wpa_s->conf->p2p_ssid_postfix) : | |
3701 | 0); | |
3702 | } | |
0f66abd2 SS |
3703 | |
3704 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS) | |
3705 | p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss); | |
b22128ef | 3706 | } |
aefb53bd JM |
3707 | |
3708 | ||
3709 | int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start, | |
3710 | int duration) | |
3711 | { | |
3712 | if (!wpa_s->ap_iface) | |
3713 | return -1; | |
3714 | return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start, | |
3715 | duration); | |
3716 | } | |
72044390 JM |
3717 | |
3718 | ||
3719 | int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled) | |
3720 | { | |
3721 | if (wpa_s->global->p2p_disabled) | |
3722 | return -1; | |
3723 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) | |
3724 | return -1; | |
3725 | ||
3726 | wpa_s->global->cross_connection = enabled; | |
3727 | p2p_set_cross_connect(wpa_s->global->p2p, enabled); | |
3728 | ||
3729 | if (!enabled) { | |
3730 | struct wpa_supplicant *iface; | |
3731 | ||
3732 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) | |
3733 | { | |
3734 | if (iface->cross_connect_enabled == 0) | |
3735 | continue; | |
3736 | ||
3737 | iface->cross_connect_enabled = 0; | |
3738 | iface->cross_connect_in_use = 0; | |
3739 | wpa_msg(iface->parent, MSG_INFO, | |
3740 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", | |
3741 | iface->ifname, iface->cross_connect_uplink); | |
3742 | } | |
3743 | } | |
3744 | ||
3745 | return 0; | |
3746 | } | |
3747 | ||
3748 | ||
3749 | static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink) | |
3750 | { | |
3751 | struct wpa_supplicant *iface; | |
3752 | ||
3753 | if (!uplink->global->cross_connection) | |
3754 | return; | |
3755 | ||
3756 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { | |
3757 | if (!iface->cross_connect_enabled) | |
3758 | continue; | |
3759 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != | |
3760 | 0) | |
3761 | continue; | |
3762 | if (iface->ap_iface == NULL) | |
3763 | continue; | |
3764 | if (iface->cross_connect_in_use) | |
3765 | continue; | |
3766 | ||
3767 | iface->cross_connect_in_use = 1; | |
3768 | wpa_msg(iface->parent, MSG_INFO, | |
3769 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", | |
3770 | iface->ifname, iface->cross_connect_uplink); | |
3771 | } | |
3772 | } | |
3773 | ||
3774 | ||
3775 | static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink) | |
3776 | { | |
3777 | struct wpa_supplicant *iface; | |
3778 | ||
3779 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { | |
3780 | if (!iface->cross_connect_enabled) | |
3781 | continue; | |
3782 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != | |
3783 | 0) | |
3784 | continue; | |
3785 | if (!iface->cross_connect_in_use) | |
3786 | continue; | |
3787 | ||
3788 | wpa_msg(iface->parent, MSG_INFO, | |
3789 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", | |
3790 | iface->ifname, iface->cross_connect_uplink); | |
3791 | iface->cross_connect_in_use = 0; | |
3792 | } | |
3793 | } | |
3794 | ||
3795 | ||
3796 | void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s) | |
3797 | { | |
3798 | if (wpa_s->ap_iface || wpa_s->current_ssid == NULL || | |
3799 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA || | |
3800 | wpa_s->cross_connect_disallowed) | |
3801 | wpas_p2p_disable_cross_connect(wpa_s); | |
3802 | else | |
3803 | wpas_p2p_enable_cross_connect(wpa_s); | |
3071e181 JM |
3804 | if (!wpa_s->ap_iface) |
3805 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); | |
72044390 JM |
3806 | } |
3807 | ||
3808 | ||
3809 | void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s) | |
3810 | { | |
3811 | wpas_p2p_disable_cross_connect(wpa_s); | |
3071e181 JM |
3812 | if (!wpa_s->ap_iface) |
3813 | wpas_p2p_set_group_idle_timeout(wpa_s); | |
72044390 JM |
3814 | } |
3815 | ||
3816 | ||
3817 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s) | |
3818 | { | |
3819 | struct wpa_supplicant *iface; | |
3820 | ||
3821 | if (!wpa_s->global->cross_connection) | |
3822 | return; | |
3823 | ||
3824 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { | |
3825 | if (iface == wpa_s) | |
3826 | continue; | |
3827 | if (iface->drv_flags & | |
3828 | WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) | |
3829 | continue; | |
3830 | if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) | |
3831 | continue; | |
3832 | ||
3833 | wpa_s->cross_connect_enabled = 1; | |
3834 | os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname, | |
3835 | sizeof(wpa_s->cross_connect_uplink)); | |
3836 | wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from " | |
3837 | "%s to %s whenever uplink is available", | |
3838 | wpa_s->ifname, wpa_s->cross_connect_uplink); | |
3839 | ||
3840 | if (iface->ap_iface || iface->current_ssid == NULL || | |
3841 | iface->current_ssid->mode != WPAS_MODE_INFRA || | |
3842 | iface->cross_connect_disallowed || | |
3843 | iface->wpa_state != WPA_COMPLETED) | |
3844 | break; | |
3845 | ||
3846 | wpa_s->cross_connect_in_use = 1; | |
3847 | wpa_msg(wpa_s->parent, MSG_INFO, | |
3848 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", | |
3849 | wpa_s->ifname, wpa_s->cross_connect_uplink); | |
3850 | break; | |
3851 | } | |
3852 | } | |
b73bf0a7 JM |
3853 | |
3854 | ||
3855 | int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s) | |
3856 | { | |
3857 | if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT && | |
3858 | !wpa_s->p2p_in_provisioning) | |
3859 | return 0; /* not P2P client operation */ | |
3860 | ||
3861 | wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC " | |
3862 | "session overlap"); | |
148bb37f JM |
3863 | if (wpa_s != wpa_s->parent) |
3864 | wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP); | |
b73bf0a7 JM |
3865 | wpas_group_formation_completed(wpa_s, 0); |
3866 | return 1; | |
3867 | } | |
b5c9da8d JM |
3868 | |
3869 | ||
3870 | void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s) | |
3871 | { | |
3872 | struct p2p_channels chan; | |
3873 | ||
3874 | if (wpa_s->global == NULL || wpa_s->global->p2p == NULL) | |
3875 | return; | |
3876 | ||
3877 | os_memset(&chan, 0, sizeof(chan)); | |
3878 | if (wpas_p2p_setup_channels(wpa_s, &chan)) { | |
3879 | wpa_printf(MSG_ERROR, "P2P: Failed to update supported " | |
3880 | "channel list"); | |
3881 | return; | |
3882 | } | |
3883 | ||
3884 | p2p_update_channel_list(wpa_s->global->p2p, &chan); | |
3885 | } | |
59eba7a2 JM |
3886 | |
3887 | ||
3888 | int wpas_p2p_cancel(struct wpa_supplicant *wpa_s) | |
3889 | { | |
3890 | struct wpa_global *global = wpa_s->global; | |
3891 | int found = 0; | |
3892 | ||
3893 | wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation"); | |
3894 | ||
3895 | if (wpa_s->pending_interface_name[0] && | |
3896 | !is_zero_ether_addr(wpa_s->pending_interface_addr)) | |
3897 | found = 1; | |
3898 | ||
3899 | wpas_p2p_stop_find(wpa_s); | |
3900 | ||
3901 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
3902 | if (wpa_s == global->p2p_group_formation && | |
a0a9f3b0 JM |
3903 | (wpa_s->p2p_in_provisioning || |
3904 | wpa_s->parent->pending_interface_type == | |
3905 | WPA_IF_P2P_CLIENT)) { | |
59eba7a2 JM |
3906 | wpa_printf(MSG_DEBUG, "P2P: Interface %s in group " |
3907 | "formation found - cancelling", | |
3908 | wpa_s->ifname); | |
3909 | found = 1; | |
3910 | wpas_p2p_group_delete(wpa_s); | |
3911 | break; | |
3912 | } | |
3913 | } | |
3914 | ||
3915 | if (!found) { | |
3916 | wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found"); | |
3917 | return -1; | |
3918 | } | |
3919 | ||
3920 | return 0; | |
3921 | } | |
c973f386 JM |
3922 | |
3923 | ||
3924 | void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s) | |
3925 | { | |
3926 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) | |
3927 | return; | |
3928 | ||
3929 | wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not " | |
3930 | "being available anymore"); | |
3931 | wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE; | |
3932 | wpas_p2p_group_delete(wpa_s); | |
3933 | } | |
7cfc4ac3 AGS |
3934 | |
3935 | ||
3936 | void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s, | |
3937 | int freq_24, int freq_5, int freq_overall) | |
3938 | { | |
3939 | struct p2p_data *p2p = wpa_s->global->p2p; | |
3940 | if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) | |
3941 | return; | |
3942 | p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall); | |
3943 | } |