]>
Commit | Line | Data |
---|---|---|
b22128ef JM |
1 | /* |
2 | * wpa_supplicant - P2P | |
3 | * Copyright (c) 2009-2010, Atheros Communications | |
4 | * | |
e22d4d95 JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
b22128ef JM |
7 | */ |
8 | ||
9 | #include "includes.h" | |
10 | ||
11 | #include "common.h" | |
12 | #include "eloop.h" | |
13 | #include "common/ieee802_11_common.h" | |
14 | #include "common/ieee802_11_defs.h" | |
15 | #include "common/wpa_ctrl.h" | |
16 | #include "wps/wps_i.h" | |
17 | #include "p2p/p2p.h" | |
18 | #include "ap/hostapd.h" | |
adc33680 | 19 | #include "ap/ap_config.h" |
aefb53bd | 20 | #include "ap/p2p_hostapd.h" |
20a0b03d JM |
21 | #include "eapol_supp/eapol_supp_sm.h" |
22 | #include "rsn_supp/wpa.h" | |
b22128ef JM |
23 | #include "wpa_supplicant_i.h" |
24 | #include "driver_i.h" | |
25 | #include "ap.h" | |
26 | #include "config_ssid.h" | |
27 | #include "config.h" | |
b22128ef JM |
28 | #include "notify.h" |
29 | #include "scan.h" | |
30 | #include "bss.h" | |
24f6497c | 31 | #include "offchannel.h" |
b22128ef JM |
32 | #include "wps_supplicant.h" |
33 | #include "p2p_supplicant.h" | |
34 | ||
35 | ||
9b1ab931 JM |
36 | /* |
37 | * How many times to try to scan to find the GO before giving up on join | |
38 | * request. | |
39 | */ | |
40 | #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10 | |
41 | ||
c8106615 JM |
42 | #ifndef P2P_MAX_CLIENT_IDLE |
43 | /* | |
44 | * How many seconds to try to reconnect to the GO when connection in P2P client | |
45 | * role has been lost. | |
46 | */ | |
47 | #define P2P_MAX_CLIENT_IDLE 10 | |
48 | #endif /* P2P_MAX_CLIENT_IDLE */ | |
49 | ||
361cdf34 JM |
50 | #ifndef P2P_MAX_INITIAL_CONN_WAIT |
51 | /* | |
52 | * How many seconds to wait for initial 4-way handshake to get completed after | |
53 | * WPS provisioning step. | |
54 | */ | |
55 | #define P2P_MAX_INITIAL_CONN_WAIT 10 | |
56 | #endif /* P2P_MAX_INITIAL_CONN_WAIT */ | |
57 | ||
9b1ab931 | 58 | |
b22128ef JM |
59 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx); |
60 | static struct wpa_supplicant * | |
61 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, | |
62 | int go); | |
63 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s); | |
ef922c4a | 64 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx); |
108def93 | 65 | static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, |
b31be3a0 JM |
66 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
67 | int auto_join); | |
f63b8542 JM |
68 | static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, |
69 | void *timeout_ctx); | |
b22128ef | 70 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s); |
72044390 | 71 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s); |
3071e181 JM |
72 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx); |
73 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s); | |
b22128ef JM |
74 | |
75 | ||
76 | static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s, | |
77 | struct wpa_scan_results *scan_res) | |
78 | { | |
79 | size_t i; | |
80 | ||
9526fd29 | 81 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
b22128ef JM |
82 | return; |
83 | ||
84 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)", | |
85 | (int) scan_res->num); | |
86 | ||
87 | for (i = 0; i < scan_res->num; i++) { | |
88 | struct wpa_scan_res *bss = scan_res->res[i]; | |
89 | if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid, | |
90 | bss->freq, bss->level, | |
91 | (const u8 *) (bss + 1), | |
92 | bss->ie_len) > 0) | |
9bcf9541 | 93 | break; |
b22128ef JM |
94 | } |
95 | ||
96 | p2p_scan_res_handled(wpa_s->global->p2p); | |
97 | } | |
98 | ||
99 | ||
046ef4aa JMB |
100 | static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, |
101 | unsigned int num_req_dev_types, | |
360182ed | 102 | const u8 *req_dev_types, const u8 *dev_id, u16 pw_id) |
b22128ef JM |
103 | { |
104 | struct wpa_supplicant *wpa_s = ctx; | |
105 | struct wpa_driver_scan_params params; | |
106 | int ret; | |
107 | struct wpabuf *wps_ie, *ies; | |
108 | int social_channels[] = { 2412, 2437, 2462, 0, 0 }; | |
206e1f42 | 109 | size_t ielen; |
39185dfa | 110 | int was_in_p2p_scan; |
b22128ef | 111 | |
9526fd29 JM |
112 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
113 | return -1; | |
114 | ||
b22128ef JM |
115 | os_memset(¶ms, 0, sizeof(params)); |
116 | ||
117 | /* P2P Wildcard SSID */ | |
118 | params.num_ssids = 1; | |
119 | params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; | |
120 | params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; | |
121 | ||
122 | wpa_s->wps->dev.p2p = 1; | |
360182ed JM |
123 | wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev, |
124 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, | |
046ef4aa | 125 | num_req_dev_types, req_dev_types); |
b22128ef JM |
126 | if (wps_ie == NULL) |
127 | return -1; | |
128 | ||
206e1f42 JM |
129 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
130 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); | |
b22128ef JM |
131 | if (ies == NULL) { |
132 | wpabuf_free(wps_ie); | |
133 | return -1; | |
134 | } | |
135 | wpabuf_put_buf(ies, wps_ie); | |
136 | wpabuf_free(wps_ie); | |
137 | ||
6d92fa6e | 138 | p2p_scan_ie(wpa_s->global->p2p, ies, dev_id); |
b22128ef | 139 | |
47185fc7 | 140 | params.p2p_probe = 1; |
b22128ef JM |
141 | params.extra_ies = wpabuf_head(ies); |
142 | params.extra_ies_len = wpabuf_len(ies); | |
143 | ||
144 | switch (type) { | |
145 | case P2P_SCAN_SOCIAL: | |
146 | params.freqs = social_channels; | |
147 | break; | |
148 | case P2P_SCAN_FULL: | |
149 | break; | |
150 | case P2P_SCAN_SPECIFIC: | |
151 | social_channels[0] = freq; | |
152 | social_channels[1] = 0; | |
153 | params.freqs = social_channels; | |
154 | break; | |
155 | case P2P_SCAN_SOCIAL_PLUS_ONE: | |
156 | social_channels[3] = freq; | |
157 | params.freqs = social_channels; | |
158 | break; | |
159 | } | |
160 | ||
39185dfa | 161 | was_in_p2p_scan = wpa_s->scan_res_handler == wpas_p2p_scan_res_handler; |
b22128ef | 162 | wpa_s->scan_res_handler = wpas_p2p_scan_res_handler; |
17fbb751 | 163 | ret = wpa_drv_scan(wpa_s, ¶ms); |
b22128ef JM |
164 | |
165 | wpabuf_free(ies); | |
166 | ||
39185dfa | 167 | if (ret) { |
0c96fd6d | 168 | wpa_s->scan_res_handler = NULL; |
39185dfa JM |
169 | if (wpa_s->scanning || was_in_p2p_scan) { |
170 | wpa_s->p2p_cb_on_scan_complete = 1; | |
171 | ret = 1; | |
172 | } | |
173 | } | |
0c96fd6d | 174 | |
b22128ef JM |
175 | return ret; |
176 | } | |
177 | ||
178 | ||
b22128ef JM |
179 | static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface) |
180 | { | |
181 | switch (p2p_group_interface) { | |
182 | case P2P_GROUP_INTERFACE_PENDING: | |
183 | return WPA_IF_P2P_GROUP; | |
184 | case P2P_GROUP_INTERFACE_GO: | |
185 | return WPA_IF_P2P_GO; | |
186 | case P2P_GROUP_INTERFACE_CLIENT: | |
187 | return WPA_IF_P2P_CLIENT; | |
188 | } | |
189 | ||
190 | return WPA_IF_P2P_GROUP; | |
191 | } | |
192 | ||
193 | ||
6c0da49f JM |
194 | static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s, |
195 | const u8 *ssid, | |
196 | size_t ssid_len, int *go) | |
197 | { | |
198 | struct wpa_ssid *s; | |
199 | ||
200 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
201 | for (s = wpa_s->conf->ssid; s; s = s->next) { | |
202 | if (s->disabled != 0 || !s->p2p_group || | |
203 | s->ssid_len != ssid_len || | |
204 | os_memcmp(ssid, s->ssid, ssid_len) != 0) | |
205 | continue; | |
206 | if (s->mode == WPAS_MODE_P2P_GO && | |
207 | s != wpa_s->current_ssid) | |
208 | continue; | |
209 | if (go) | |
210 | *go = s->mode == WPAS_MODE_P2P_GO; | |
211 | return wpa_s; | |
212 | } | |
213 | } | |
214 | ||
215 | return NULL; | |
216 | } | |
217 | ||
218 | ||
b22128ef JM |
219 | static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s) |
220 | { | |
221 | struct wpa_ssid *ssid; | |
222 | char *gtype; | |
3071e181 JM |
223 | const char *reason; |
224 | ||
b22128ef JM |
225 | ssid = wpa_s->current_ssid; |
226 | if (ssid == NULL) { | |
227 | /* | |
228 | * The current SSID was not known, but there may still be a | |
229 | * pending P2P group interface waiting for provisioning. | |
230 | */ | |
231 | ssid = wpa_s->conf->ssid; | |
232 | while (ssid) { | |
233 | if (ssid->p2p_group && | |
234 | (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || | |
235 | (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) | |
236 | break; | |
237 | ssid = ssid->next; | |
238 | } | |
239 | } | |
240 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO) | |
241 | gtype = "GO"; | |
242 | else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT || | |
243 | (ssid && ssid->mode == WPAS_MODE_INFRA)) { | |
244 | wpa_s->reassociate = 0; | |
245 | wpa_s->disconnected = 1; | |
246 | wpa_supplicant_deauthenticate(wpa_s, | |
247 | WLAN_REASON_DEAUTH_LEAVING); | |
248 | gtype = "client"; | |
249 | } else | |
250 | gtype = "GO"; | |
72044390 JM |
251 | if (wpa_s->cross_connect_in_use) { |
252 | wpa_s->cross_connect_in_use = 0; | |
253 | wpa_msg(wpa_s->parent, MSG_INFO, | |
254 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", | |
255 | wpa_s->ifname, wpa_s->cross_connect_uplink); | |
256 | } | |
3071e181 JM |
257 | switch (wpa_s->removal_reason) { |
258 | case P2P_GROUP_REMOVAL_REQUESTED: | |
259 | reason = " reason=REQUESTED"; | |
260 | break; | |
261 | case P2P_GROUP_REMOVAL_IDLE_TIMEOUT: | |
262 | reason = " reason=IDLE"; | |
263 | break; | |
c973f386 JM |
264 | case P2P_GROUP_REMOVAL_UNAVAILABLE: |
265 | reason = " reason=UNAVAILABLE"; | |
266 | break; | |
3fc14102 JM |
267 | case P2P_GROUP_REMOVAL_GO_ENDING_SESSION: |
268 | reason = " reason=GO_ENDING_SESSION"; | |
269 | break; | |
3071e181 JM |
270 | default: |
271 | reason = ""; | |
272 | break; | |
273 | } | |
274 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s", | |
275 | wpa_s->ifname, gtype, reason); | |
408af93e | 276 | |
dddc7045 JM |
277 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
278 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); | |
a7a30b90 | 279 | |
408af93e JB |
280 | if (ssid) |
281 | wpas_notify_p2p_group_removed(wpa_s, ssid, gtype); | |
282 | ||
b22128ef JM |
283 | if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { |
284 | struct wpa_global *global; | |
285 | char *ifname; | |
286 | enum wpa_driver_if_type type; | |
287 | wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s", | |
288 | wpa_s->ifname); | |
289 | global = wpa_s->global; | |
290 | ifname = os_strdup(wpa_s->ifname); | |
291 | type = wpas_p2p_if_type(wpa_s->p2p_group_interface); | |
df509539 | 292 | wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0); |
b22128ef JM |
293 | wpa_s = global->ifaces; |
294 | if (wpa_s && ifname) | |
295 | wpa_drv_if_remove(wpa_s, type, ifname); | |
296 | os_free(ifname); | |
297 | return; | |
298 | } | |
299 | ||
300 | wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network"); | |
301 | if (ssid && (ssid->p2p_group || | |
302 | ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || | |
303 | (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) { | |
304 | int id = ssid->id; | |
20a0b03d JM |
305 | if (ssid == wpa_s->current_ssid) { |
306 | wpa_sm_set_config(wpa_s->wpa, NULL); | |
307 | eapol_sm_notify_config(wpa_s->eapol, NULL, NULL); | |
b22128ef | 308 | wpa_s->current_ssid = NULL; |
20a0b03d | 309 | } |
c2762e41 JS |
310 | /* |
311 | * Networks objects created during any P2P activities are not | |
312 | * exposed out as they might/will confuse certain non-P2P aware | |
313 | * applications since these network objects won't behave like | |
314 | * regular ones. | |
315 | * | |
316 | * Likewise, we don't send out network removed signals for such | |
317 | * network objects. | |
318 | */ | |
b22128ef JM |
319 | wpa_config_remove_network(wpa_s->conf, id); |
320 | wpa_supplicant_clear_status(wpa_s); | |
433cd2ce | 321 | wpa_supplicant_cancel_sched_scan(wpa_s); |
b22128ef JM |
322 | } else { |
323 | wpa_printf(MSG_DEBUG, "P2P: Temporary group network not " | |
324 | "found"); | |
325 | } | |
3c29244e EP |
326 | if (wpa_s->ap_iface) |
327 | wpa_supplicant_ap_deinit(wpa_s); | |
328 | else | |
329 | wpa_drv_deinit_p2p_cli(wpa_s); | |
b22128ef JM |
330 | } |
331 | ||
332 | ||
333 | static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s, | |
334 | u8 *go_dev_addr, | |
335 | const u8 *ssid, size_t ssid_len) | |
336 | { | |
337 | struct wpa_bss *bss; | |
338 | const u8 *bssid; | |
339 | struct wpabuf *p2p; | |
340 | u8 group_capab; | |
341 | const u8 *addr; | |
342 | ||
343 | if (wpa_s->go_params) | |
344 | bssid = wpa_s->go_params->peer_interface_addr; | |
345 | else | |
346 | bssid = wpa_s->bssid; | |
347 | ||
348 | bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len); | |
349 | if (bss == NULL) { | |
350 | u8 iface_addr[ETH_ALEN]; | |
351 | if (p2p_get_interface_addr(wpa_s->global->p2p, bssid, | |
352 | iface_addr) == 0) | |
353 | bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len); | |
354 | } | |
355 | if (bss == NULL) { | |
356 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " | |
357 | "group is persistent - BSS " MACSTR " not found", | |
358 | MAC2STR(bssid)); | |
359 | return 0; | |
360 | } | |
361 | ||
362 | p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); | |
363 | if (p2p == NULL) { | |
364 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " | |
365 | "group is persistent - BSS " MACSTR | |
366 | " did not include P2P IE", MAC2STR(bssid)); | |
367 | wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs", | |
368 | (u8 *) (bss + 1), bss->ie_len); | |
369 | wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs", | |
370 | ((u8 *) bss + 1) + bss->ie_len, | |
371 | bss->beacon_ie_len); | |
372 | return 0; | |
373 | } | |
374 | ||
375 | group_capab = p2p_get_group_capab(p2p); | |
376 | addr = p2p_get_go_dev_addr(p2p); | |
377 | wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: " | |
378 | "group_capab=0x%x", group_capab); | |
379 | if (addr) { | |
380 | os_memcpy(go_dev_addr, addr, ETH_ALEN); | |
381 | wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR, | |
382 | MAC2STR(addr)); | |
383 | } else | |
384 | os_memset(go_dev_addr, 0, ETH_ALEN); | |
385 | wpabuf_free(p2p); | |
386 | ||
387 | wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x " | |
388 | "go_dev_addr=" MACSTR, | |
389 | MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr)); | |
390 | ||
391 | return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP; | |
392 | } | |
393 | ||
394 | ||
4b6baa2f JMB |
395 | static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s, |
396 | struct wpa_ssid *ssid, | |
397 | const u8 *go_dev_addr) | |
b22128ef JM |
398 | { |
399 | struct wpa_ssid *s; | |
400 | int changed = 0; | |
401 | ||
402 | wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent " | |
403 | "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr)); | |
404 | for (s = wpa_s->conf->ssid; s; s = s->next) { | |
405 | if (s->disabled == 2 && | |
406 | os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 && | |
407 | s->ssid_len == ssid->ssid_len && | |
408 | os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0) | |
409 | break; | |
410 | } | |
411 | ||
412 | if (s) { | |
413 | wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group " | |
414 | "entry"); | |
415 | if (ssid->passphrase && !s->passphrase) | |
416 | changed = 1; | |
417 | else if (ssid->passphrase && s->passphrase && | |
418 | os_strcmp(ssid->passphrase, s->passphrase) != 0) | |
419 | changed = 1; | |
420 | } else { | |
421 | wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group " | |
422 | "entry"); | |
423 | changed = 1; | |
424 | s = wpa_config_add_network(wpa_s->conf); | |
425 | if (s == NULL) | |
4b6baa2f | 426 | return -1; |
c2762e41 JS |
427 | |
428 | /* | |
429 | * Instead of network_added we emit persistent_group_added | |
430 | * notification. Also to keep the defense checks in | |
431 | * persistent_group obj registration method, we set the | |
432 | * relevant flags in s to designate it as a persistent group. | |
433 | */ | |
434 | s->p2p_group = 1; | |
435 | s->p2p_persistent_group = 1; | |
436 | wpas_notify_persistent_group_added(wpa_s, s); | |
b22128ef JM |
437 | wpa_config_set_network_defaults(s); |
438 | } | |
439 | ||
440 | s->p2p_group = 1; | |
441 | s->p2p_persistent_group = 1; | |
442 | s->disabled = 2; | |
443 | s->bssid_set = 1; | |
444 | os_memcpy(s->bssid, go_dev_addr, ETH_ALEN); | |
445 | s->mode = ssid->mode; | |
446 | s->auth_alg = WPA_AUTH_ALG_OPEN; | |
447 | s->key_mgmt = WPA_KEY_MGMT_PSK; | |
448 | s->proto = WPA_PROTO_RSN; | |
449 | s->pairwise_cipher = WPA_CIPHER_CCMP; | |
d1c8ac88 | 450 | s->export_keys = 1; |
b22128ef JM |
451 | if (ssid->passphrase) { |
452 | os_free(s->passphrase); | |
453 | s->passphrase = os_strdup(ssid->passphrase); | |
454 | } | |
455 | if (ssid->psk_set) { | |
456 | s->psk_set = 1; | |
457 | os_memcpy(s->psk, ssid->psk, 32); | |
458 | } | |
459 | if (s->passphrase && !s->psk_set) | |
460 | wpa_config_update_psk(s); | |
461 | if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) { | |
462 | os_free(s->ssid); | |
463 | s->ssid = os_malloc(ssid->ssid_len); | |
464 | } | |
465 | if (s->ssid) { | |
466 | s->ssid_len = ssid->ssid_len; | |
467 | os_memcpy(s->ssid, ssid->ssid, s->ssid_len); | |
468 | } | |
469 | ||
470 | #ifndef CONFIG_NO_CONFIG_WRITE | |
471 | if (changed && wpa_s->conf->update_config && | |
472 | wpa_config_write(wpa_s->confname, wpa_s->conf)) { | |
473 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); | |
474 | } | |
475 | #endif /* CONFIG_NO_CONFIG_WRITE */ | |
4b6baa2f JMB |
476 | |
477 | return s->id; | |
b22128ef JM |
478 | } |
479 | ||
480 | ||
fbdcfd57 JM |
481 | static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s, |
482 | const u8 *addr) | |
483 | { | |
484 | struct wpa_ssid *ssid, *s; | |
485 | u8 *n; | |
486 | size_t i; | |
487 | ||
488 | ssid = wpa_s->current_ssid; | |
489 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || | |
490 | !ssid->p2p_persistent_group) | |
491 | return; | |
492 | ||
493 | for (s = wpa_s->parent->conf->ssid; s; s = s->next) { | |
494 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) | |
495 | continue; | |
496 | ||
497 | if (s->ssid_len == ssid->ssid_len && | |
498 | os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0) | |
499 | break; | |
500 | } | |
501 | ||
502 | if (s == NULL) | |
503 | return; | |
504 | ||
505 | for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) { | |
506 | if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr, | |
507 | ETH_ALEN) == 0) | |
508 | return; /* already in list */ | |
509 | } | |
510 | ||
511 | n = os_realloc(s->p2p_client_list, | |
512 | (s->num_p2p_clients + 1) * ETH_ALEN); | |
513 | if (n == NULL) | |
514 | return; | |
515 | os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN); | |
516 | s->p2p_client_list = n; | |
517 | s->num_p2p_clients++; | |
518 | ||
519 | #ifndef CONFIG_NO_CONFIG_WRITE | |
520 | if (wpa_s->parent->conf->update_config && | |
521 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) | |
522 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); | |
523 | #endif /* CONFIG_NO_CONFIG_WRITE */ | |
524 | } | |
525 | ||
526 | ||
b22128ef JM |
527 | static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s, |
528 | int success) | |
529 | { | |
530 | struct wpa_ssid *ssid; | |
531 | const char *ssid_txt; | |
532 | int client; | |
533 | int persistent; | |
534 | u8 go_dev_addr[ETH_ALEN]; | |
4b6baa2f | 535 | int network_id = -1; |
b22128ef JM |
536 | |
537 | /* | |
538 | * This callback is likely called for the main interface. Update wpa_s | |
539 | * to use the group interface if a new interface was created for the | |
540 | * group. | |
541 | */ | |
542 | if (wpa_s->global->p2p_group_formation) | |
543 | wpa_s = wpa_s->global->p2p_group_formation; | |
d1b024c9 | 544 | wpa_s->global->p2p_group_formation = NULL; |
b22128ef JM |
545 | wpa_s->p2p_in_provisioning = 0; |
546 | ||
547 | if (!success) { | |
548 | wpa_msg(wpa_s->parent, MSG_INFO, | |
549 | P2P_EVENT_GROUP_FORMATION_FAILURE); | |
550 | wpas_p2p_group_delete(wpa_s); | |
551 | return; | |
552 | } | |
553 | ||
554 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS); | |
555 | ||
556 | ssid = wpa_s->current_ssid; | |
557 | if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { | |
558 | ssid->mode = WPAS_MODE_P2P_GO; | |
559 | p2p_group_notif_formation_done(wpa_s->p2p_group); | |
560 | wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL); | |
561 | } | |
562 | ||
563 | persistent = 0; | |
564 | if (ssid) { | |
565 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); | |
566 | client = ssid->mode == WPAS_MODE_INFRA; | |
567 | if (ssid->mode == WPAS_MODE_P2P_GO) { | |
568 | persistent = ssid->p2p_persistent_group; | |
d7e70476 | 569 | os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr, |
b22128ef JM |
570 | ETH_ALEN); |
571 | } else | |
572 | persistent = wpas_p2p_persistent_group(wpa_s, | |
573 | go_dev_addr, | |
574 | ssid->ssid, | |
575 | ssid->ssid_len); | |
576 | } else { | |
577 | ssid_txt = ""; | |
578 | client = wpa_s->p2p_group_interface == | |
579 | P2P_GROUP_INTERFACE_CLIENT; | |
62c0d27e | 580 | os_memset(go_dev_addr, 0, ETH_ALEN); |
b22128ef JM |
581 | } |
582 | ||
583 | wpa_s->show_group_started = 0; | |
584 | if (client) { | |
585 | /* | |
586 | * Indicate event only after successfully completed 4-way | |
587 | * handshake, i.e., when the interface is ready for data | |
588 | * packets. | |
589 | */ | |
590 | wpa_s->show_group_started = 1; | |
591 | } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) { | |
592 | char psk[65]; | |
593 | wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); | |
594 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED | |
c481048f AC |
595 | "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR |
596 | "%s", | |
597 | wpa_s->ifname, ssid_txt, ssid->frequency, psk, | |
598 | MAC2STR(go_dev_addr), | |
b22128ef | 599 | persistent ? " [PERSISTENT]" : ""); |
72044390 | 600 | wpas_p2p_cross_connect_setup(wpa_s); |
3071e181 | 601 | wpas_p2p_set_group_idle_timeout(wpa_s); |
b22128ef JM |
602 | } else { |
603 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED | |
c481048f AC |
604 | "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" " |
605 | "go_dev_addr=" MACSTR "%s", | |
d394a22f | 606 | wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0, |
b22128ef JM |
607 | ssid && ssid->passphrase ? ssid->passphrase : "", |
608 | MAC2STR(go_dev_addr), | |
609 | persistent ? " [PERSISTENT]" : ""); | |
72044390 | 610 | wpas_p2p_cross_connect_setup(wpa_s); |
3071e181 | 611 | wpas_p2p_set_group_idle_timeout(wpa_s); |
b22128ef JM |
612 | } |
613 | ||
614 | if (persistent) | |
4b6baa2f JMB |
615 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
616 | ssid, go_dev_addr); | |
21fe9e75 | 617 | if (network_id < 0 && ssid) |
4b6baa2f JMB |
618 | network_id = ssid->id; |
619 | if (!client) | |
620 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); | |
b22128ef JM |
621 | } |
622 | ||
623 | ||
24f6497c JM |
624 | static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s, |
625 | unsigned int freq, | |
626 | const u8 *dst, const u8 *src, | |
627 | const u8 *bssid, | |
628 | const u8 *data, size_t data_len, | |
629 | enum offchannel_send_action_result | |
630 | result) | |
534525ff | 631 | { |
24f6497c | 632 | enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS; |
b22128ef | 633 | |
9526fd29 | 634 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) |
b22128ef | 635 | return; |
3ac17eba JM |
636 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
637 | return; | |
b22128ef | 638 | |
24f6497c JM |
639 | switch (result) { |
640 | case OFFCHANNEL_SEND_ACTION_SUCCESS: | |
641 | res = P2P_SEND_ACTION_SUCCESS; | |
642 | break; | |
643 | case OFFCHANNEL_SEND_ACTION_NO_ACK: | |
644 | res = P2P_SEND_ACTION_NO_ACK; | |
645 | break; | |
646 | case OFFCHANNEL_SEND_ACTION_FAILED: | |
647 | res = P2P_SEND_ACTION_FAILED; | |
648 | break; | |
b22128ef JM |
649 | } |
650 | ||
24f6497c | 651 | p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res); |
b22128ef | 652 | |
f63b8542 JM |
653 | if (result != OFFCHANNEL_SEND_ACTION_SUCCESS && |
654 | wpa_s->pending_pd_before_join && | |
24f6497c JM |
655 | (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || |
656 | os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { | |
b22128ef JM |
657 | wpa_s->pending_pd_before_join = 0; |
658 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " | |
f63b8542 JM |
659 | "join-existing-group operation (no ACK for PD " |
660 | "Req)"); | |
b22128ef JM |
661 | wpas_p2p_join_start(wpa_s); |
662 | } | |
663 | } | |
664 | ||
665 | ||
666 | static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst, | |
667 | const u8 *src, const u8 *bssid, const u8 *buf, | |
668 | size_t len, unsigned int wait_time) | |
669 | { | |
670 | struct wpa_supplicant *wpa_s = ctx; | |
24f6497c JM |
671 | return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len, |
672 | wait_time, | |
b106173a | 673 | wpas_p2p_send_action_tx_status, 1); |
b22128ef JM |
674 | } |
675 | ||
676 | ||
677 | static void wpas_send_action_done(void *ctx) | |
678 | { | |
679 | struct wpa_supplicant *wpa_s = ctx; | |
24f6497c | 680 | offchannel_send_action_done(wpa_s); |
b22128ef JM |
681 | } |
682 | ||
683 | ||
684 | static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s, | |
685 | struct p2p_go_neg_results *params) | |
686 | { | |
687 | if (wpa_s->go_params == NULL) { | |
688 | wpa_s->go_params = os_malloc(sizeof(*params)); | |
689 | if (wpa_s->go_params == NULL) | |
690 | return -1; | |
691 | } | |
692 | os_memcpy(wpa_s->go_params, params, sizeof(*params)); | |
693 | return 0; | |
694 | } | |
695 | ||
696 | ||
697 | static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s, | |
698 | struct p2p_go_neg_results *res) | |
699 | { | |
3b29972c JM |
700 | wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR, |
701 | MAC2STR(res->peer_interface_addr)); | |
e9a7ae41 JM |
702 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID", |
703 | res->ssid, res->ssid_len); | |
b22128ef JM |
704 | wpa_supplicant_ap_deinit(wpa_s); |
705 | wpas_copy_go_neg_results(wpa_s, res); | |
706 | if (res->wps_method == WPS_PBC) | |
3b29972c | 707 | wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1); |
3c5126a4 JM |
708 | else { |
709 | u16 dev_pw_id = DEV_PW_DEFAULT; | |
710 | if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD) | |
711 | dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED; | |
b22128ef | 712 | wpas_wps_start_pin(wpa_s, res->peer_interface_addr, |
3c5126a4 JM |
713 | wpa_s->p2p_pin, 1, dev_pw_id); |
714 | } | |
b22128ef JM |
715 | } |
716 | ||
717 | ||
718 | static void p2p_go_configured(void *ctx, void *data) | |
719 | { | |
720 | struct wpa_supplicant *wpa_s = ctx; | |
721 | struct p2p_go_neg_results *params = data; | |
722 | struct wpa_ssid *ssid; | |
4b6baa2f | 723 | int network_id = -1; |
b22128ef JM |
724 | |
725 | ssid = wpa_s->current_ssid; | |
726 | if (ssid && ssid->mode == WPAS_MODE_P2P_GO) { | |
727 | wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning"); | |
d1b024c9 JM |
728 | if (wpa_s->global->p2p_group_formation == wpa_s) |
729 | wpa_s->global->p2p_group_formation = NULL; | |
b22128ef | 730 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
c481048f AC |
731 | "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" " |
732 | "go_dev_addr=" MACSTR "%s", | |
b22128ef JM |
733 | wpa_s->ifname, |
734 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len), | |
c481048f | 735 | ssid->frequency, |
b22128ef | 736 | params->passphrase ? params->passphrase : "", |
d7e70476 | 737 | MAC2STR(wpa_s->global->p2p_dev_addr), |
b22128ef | 738 | params->persistent_group ? " [PERSISTENT]" : ""); |
4b6baa2f | 739 | |
b22128ef | 740 | if (params->persistent_group) |
4b6baa2f | 741 | network_id = wpas_p2p_store_persistent_group( |
b22128ef | 742 | wpa_s->parent, ssid, |
d7e70476 | 743 | wpa_s->global->p2p_dev_addr); |
4b6baa2f JMB |
744 | if (network_id < 0) |
745 | network_id = ssid->id; | |
746 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); | |
72044390 | 747 | wpas_p2p_cross_connect_setup(wpa_s); |
3071e181 | 748 | wpas_p2p_set_group_idle_timeout(wpa_s); |
b22128ef JM |
749 | return; |
750 | } | |
751 | ||
752 | wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning"); | |
753 | if (wpa_supplicant_ap_mac_addr_filter(wpa_s, | |
754 | params->peer_interface_addr)) { | |
755 | wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address " | |
756 | "filtering"); | |
757 | return; | |
758 | } | |
759 | if (params->wps_method == WPS_PBC) | |
d601247c | 760 | wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr, |
c3daaf33 | 761 | params->peer_device_addr); |
b22128ef JM |
762 | else if (wpa_s->p2p_pin[0]) |
763 | wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr, | |
764 | wpa_s->p2p_pin, NULL, 0); | |
765 | os_free(wpa_s->go_params); | |
766 | wpa_s->go_params = NULL; | |
767 | } | |
768 | ||
769 | ||
770 | static void wpas_start_wps_go(struct wpa_supplicant *wpa_s, | |
771 | struct p2p_go_neg_results *params, | |
772 | int group_formation) | |
773 | { | |
774 | struct wpa_ssid *ssid; | |
775 | ||
776 | if (wpas_copy_go_neg_results(wpa_s, params) < 0) | |
777 | return; | |
778 | ||
779 | ssid = wpa_config_add_network(wpa_s->conf); | |
780 | if (ssid == NULL) | |
781 | return; | |
782 | ||
a9e02d59 JM |
783 | wpa_s->show_group_started = 0; |
784 | ||
b22128ef JM |
785 | wpa_config_set_network_defaults(ssid); |
786 | ssid->temporary = 1; | |
787 | ssid->p2p_group = 1; | |
788 | ssid->p2p_persistent_group = params->persistent_group; | |
789 | ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION : | |
790 | WPAS_MODE_P2P_GO; | |
791 | ssid->frequency = params->freq; | |
792 | ssid->ssid = os_zalloc(params->ssid_len + 1); | |
793 | if (ssid->ssid) { | |
794 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); | |
795 | ssid->ssid_len = params->ssid_len; | |
796 | } | |
797 | ssid->auth_alg = WPA_AUTH_ALG_OPEN; | |
798 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; | |
799 | ssid->proto = WPA_PROTO_RSN; | |
800 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; | |
801 | ssid->passphrase = os_strdup(params->passphrase); | |
802 | ||
803 | wpa_s->ap_configured_cb = p2p_go_configured; | |
804 | wpa_s->ap_configured_cb_ctx = wpa_s; | |
805 | wpa_s->ap_configured_cb_data = wpa_s->go_params; | |
7dcdcfd6 | 806 | wpa_s->connect_without_scan = ssid; |
b22128ef JM |
807 | wpa_s->reassociate = 1; |
808 | wpa_s->disconnected = 0; | |
809 | wpa_supplicant_req_scan(wpa_s, 0, 0); | |
810 | } | |
811 | ||
812 | ||
813 | static void wpas_p2p_clone_config(struct wpa_supplicant *dst, | |
814 | const struct wpa_supplicant *src) | |
815 | { | |
816 | struct wpa_config *d; | |
817 | const struct wpa_config *s; | |
818 | ||
819 | d = dst->conf; | |
820 | s = src->conf; | |
821 | ||
822 | #define C(n) if (s->n) d->n = os_strdup(s->n) | |
823 | C(device_name); | |
824 | C(manufacturer); | |
825 | C(model_name); | |
826 | C(model_number); | |
827 | C(serial_number); | |
b22128ef JM |
828 | C(config_methods); |
829 | #undef C | |
3071e181 | 830 | |
2f646b6e JB |
831 | os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN); |
832 | os_memcpy(d->sec_device_type, s->sec_device_type, | |
833 | sizeof(d->sec_device_type)); | |
834 | d->num_sec_device_types = s->num_sec_device_types; | |
835 | ||
3071e181 | 836 | d->p2p_group_idle = s->p2p_group_idle; |
b029bd33 | 837 | d->p2p_intra_bss = s->p2p_intra_bss; |
acc247b2 | 838 | d->persistent_reconnect = s->persistent_reconnect; |
f571b593 | 839 | d->max_num_sta = s->max_num_sta; |
1298c145 | 840 | d->pbc_in_m1 = s->pbc_in_m1; |
b22128ef JM |
841 | } |
842 | ||
843 | ||
844 | static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, | |
845 | enum wpa_driver_if_type type) | |
846 | { | |
847 | char ifname[120], force_ifname[120]; | |
848 | ||
849 | if (wpa_s->pending_interface_name[0]) { | |
850 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists " | |
851 | "- skip creation of a new one"); | |
852 | if (is_zero_ether_addr(wpa_s->pending_interface_addr)) { | |
853 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual address " | |
854 | "unknown?! ifname='%s'", | |
855 | wpa_s->pending_interface_name); | |
856 | return -1; | |
857 | } | |
858 | return 0; | |
859 | } | |
860 | ||
a1eca5cf | 861 | os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname, |
b22128ef | 862 | wpa_s->p2p_group_idx); |
a1eca5cf JM |
863 | if (os_strlen(ifname) >= IFNAMSIZ && |
864 | os_strlen(wpa_s->ifname) < IFNAMSIZ) { | |
865 | /* Try to avoid going over the IFNAMSIZ length limit */ | |
866 | os_snprintf(ifname, sizeof(ifname), "p2p-%d", | |
867 | wpa_s->p2p_group_idx); | |
868 | } | |
b22128ef JM |
869 | force_ifname[0] = '\0'; |
870 | ||
871 | wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group", | |
872 | ifname); | |
873 | wpa_s->p2p_group_idx++; | |
874 | ||
875 | wpa_s->pending_interface_type = type; | |
876 | if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname, | |
e17a2477 | 877 | wpa_s->pending_interface_addr, NULL) < 0) { |
b22128ef JM |
878 | wpa_printf(MSG_ERROR, "P2P: Failed to create new group " |
879 | "interface"); | |
880 | return -1; | |
881 | } | |
882 | ||
883 | if (force_ifname[0]) { | |
884 | wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s", | |
885 | force_ifname); | |
886 | os_strlcpy(wpa_s->pending_interface_name, force_ifname, | |
887 | sizeof(wpa_s->pending_interface_name)); | |
888 | } else | |
889 | os_strlcpy(wpa_s->pending_interface_name, ifname, | |
890 | sizeof(wpa_s->pending_interface_name)); | |
891 | wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr " | |
892 | MACSTR, wpa_s->pending_interface_name, | |
893 | MAC2STR(wpa_s->pending_interface_addr)); | |
894 | ||
895 | return 0; | |
896 | } | |
897 | ||
898 | ||
899 | static void wpas_p2p_remove_pending_group_interface( | |
900 | struct wpa_supplicant *wpa_s) | |
901 | { | |
902 | if (!wpa_s->pending_interface_name[0] || | |
903 | is_zero_ether_addr(wpa_s->pending_interface_addr)) | |
904 | return; /* No pending virtual interface */ | |
905 | ||
906 | wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s", | |
907 | wpa_s->pending_interface_name); | |
908 | wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type, | |
909 | wpa_s->pending_interface_name); | |
910 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); | |
911 | wpa_s->pending_interface_name[0] = '\0'; | |
912 | } | |
913 | ||
914 | ||
915 | static struct wpa_supplicant * | |
916 | wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go) | |
917 | { | |
918 | struct wpa_interface iface; | |
919 | struct wpa_supplicant *group_wpa_s; | |
920 | ||
921 | if (!wpa_s->pending_interface_name[0]) { | |
922 | wpa_printf(MSG_ERROR, "P2P: No pending group interface"); | |
d75e8806 JM |
923 | if (!wpas_p2p_create_iface(wpa_s)) |
924 | return NULL; | |
925 | /* | |
926 | * Something has forced us to remove the pending interface; try | |
927 | * to create a new one and hope for the best that we will get | |
928 | * the same local address. | |
929 | */ | |
930 | if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : | |
931 | WPA_IF_P2P_CLIENT) < 0) | |
932 | return NULL; | |
b22128ef JM |
933 | } |
934 | ||
935 | os_memset(&iface, 0, sizeof(iface)); | |
936 | iface.ifname = wpa_s->pending_interface_name; | |
937 | iface.driver = wpa_s->driver->name; | |
938 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; | |
939 | iface.driver_param = wpa_s->conf->driver_param; | |
940 | group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface); | |
941 | if (group_wpa_s == NULL) { | |
942 | wpa_printf(MSG_ERROR, "P2P: Failed to create new " | |
943 | "wpa_supplicant interface"); | |
944 | return NULL; | |
945 | } | |
946 | wpa_s->pending_interface_name[0] = '\0'; | |
947 | group_wpa_s->parent = wpa_s; | |
948 | group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO : | |
949 | P2P_GROUP_INTERFACE_CLIENT; | |
950 | wpa_s->global->p2p_group_formation = group_wpa_s; | |
951 | ||
952 | wpas_p2p_clone_config(group_wpa_s, wpa_s); | |
953 | ||
954 | return group_wpa_s; | |
955 | } | |
956 | ||
957 | ||
958 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, | |
959 | void *timeout_ctx) | |
960 | { | |
961 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
962 | wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out"); | |
963 | if (wpa_s->global->p2p) | |
964 | p2p_group_formation_failed(wpa_s->global->p2p); | |
3ac17eba JM |
965 | else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
966 | wpa_drv_p2p_group_formation_failed(wpa_s); | |
b22128ef JM |
967 | wpas_group_formation_completed(wpa_s, 0); |
968 | } | |
969 | ||
970 | ||
971 | void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res) | |
972 | { | |
973 | struct wpa_supplicant *wpa_s = ctx; | |
974 | ||
8eada5ca | 975 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
b22128ef JM |
976 | wpa_drv_cancel_remain_on_channel(wpa_s); |
977 | wpa_s->off_channel_freq = 0; | |
07a30a66 | 978 | wpa_s->roc_waiting_drv_freq = 0; |
b22128ef JM |
979 | } |
980 | ||
981 | if (res->status) { | |
982 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d", | |
983 | res->status); | |
e5a359cf | 984 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
b22128ef JM |
985 | wpas_p2p_remove_pending_group_interface(wpa_s); |
986 | return; | |
987 | } | |
988 | ||
989 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS); | |
e5a359cf | 990 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
b22128ef | 991 | |
23c84252 JM |
992 | if (res->role_go && wpa_s->p2p_persistent_id >= 0) { |
993 | struct wpa_ssid *ssid; | |
994 | ssid = wpa_config_get_network(wpa_s->conf, | |
995 | wpa_s->p2p_persistent_id); | |
996 | if (ssid && ssid->disabled == 2 && | |
997 | ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) { | |
998 | size_t len = os_strlen(ssid->passphrase); | |
999 | wpa_printf(MSG_DEBUG, "P2P: Override passphrase based " | |
1000 | "on requested persistent group"); | |
1001 | os_memcpy(res->passphrase, ssid->passphrase, len); | |
1002 | res->passphrase[len] = '\0'; | |
1003 | } | |
1004 | } | |
1005 | ||
b22128ef JM |
1006 | if (wpa_s->create_p2p_iface) { |
1007 | struct wpa_supplicant *group_wpa_s = | |
1008 | wpas_p2p_init_group_interface(wpa_s, res->role_go); | |
1009 | if (group_wpa_s == NULL) { | |
1010 | wpas_p2p_remove_pending_group_interface(wpa_s); | |
1011 | return; | |
1012 | } | |
3c5126a4 | 1013 | if (group_wpa_s != wpa_s) { |
b22128ef JM |
1014 | os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin, |
1015 | sizeof(group_wpa_s->p2p_pin)); | |
3c5126a4 JM |
1016 | group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method; |
1017 | } | |
b22128ef JM |
1018 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
1019 | wpa_s->pending_interface_name[0] = '\0'; | |
1020 | group_wpa_s->p2p_in_provisioning = 1; | |
1021 | ||
1022 | if (res->role_go) | |
1023 | wpas_start_wps_go(group_wpa_s, res, 1); | |
1024 | else | |
1025 | wpas_start_wps_enrollee(group_wpa_s, res); | |
1026 | } else { | |
1027 | wpa_s->p2p_in_provisioning = 1; | |
1028 | wpa_s->global->p2p_group_formation = wpa_s; | |
1029 | ||
1030 | if (res->role_go) | |
1031 | wpas_start_wps_go(wpa_s, res, 1); | |
1032 | else | |
1033 | wpas_start_wps_enrollee(ctx, res); | |
1034 | } | |
1035 | ||
1036 | wpa_s->p2p_long_listen = 0; | |
1037 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); | |
1038 | ||
1039 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); | |
ae3e3421 JM |
1040 | eloop_register_timeout(15 + res->peer_config_timeout / 100, |
1041 | (res->peer_config_timeout % 100) * 10000, | |
1042 | wpas_p2p_group_formation_timeout, wpa_s, NULL); | |
b22128ef JM |
1043 | } |
1044 | ||
1045 | ||
3dfda83d | 1046 | void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id) |
b22128ef JM |
1047 | { |
1048 | struct wpa_supplicant *wpa_s = ctx; | |
3dfda83d JM |
1049 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR |
1050 | " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id); | |
32d1bce0 KRK |
1051 | |
1052 | wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id); | |
b22128ef JM |
1053 | } |
1054 | ||
1055 | ||
c5db8e51 | 1056 | void wpas_dev_found(void *ctx, const u8 *addr, |
8fd7dc1b JB |
1057 | const struct p2p_peer_info *info, |
1058 | int new_device) | |
b22128ef | 1059 | { |
5506d184 | 1060 | #ifndef CONFIG_NO_STDOUT_DEBUG |
b22128ef JM |
1061 | struct wpa_supplicant *wpa_s = ctx; |
1062 | char devtype[WPS_DEV_TYPE_BUFSIZE]; | |
c5db8e51 | 1063 | |
b22128ef JM |
1064 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR |
1065 | " p2p_dev_addr=" MACSTR | |
1066 | " pri_dev_type=%s name='%s' config_methods=0x%x " | |
1067 | "dev_capab=0x%x group_capab=0x%x", | |
c5db8e51 KRK |
1068 | MAC2STR(addr), MAC2STR(info->p2p_device_addr), |
1069 | wps_dev_type_bin2str(info->pri_dev_type, devtype, | |
1070 | sizeof(devtype)), | |
1071 | info->device_name, info->config_methods, | |
1072 | info->dev_capab, info->group_capab); | |
5506d184 | 1073 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
d642d2d2 JB |
1074 | |
1075 | wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device); | |
b22128ef JM |
1076 | } |
1077 | ||
1078 | ||
56eeb8f2 JB |
1079 | static void wpas_dev_lost(void *ctx, const u8 *dev_addr) |
1080 | { | |
1081 | struct wpa_supplicant *wpa_s = ctx; | |
1082 | ||
3074d8f1 JJ |
1083 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST |
1084 | "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr)); | |
1085 | ||
56eeb8f2 JB |
1086 | wpas_notify_p2p_device_lost(wpa_s, dev_addr); |
1087 | } | |
1088 | ||
1089 | ||
b22128ef JM |
1090 | static int wpas_start_listen(void *ctx, unsigned int freq, |
1091 | unsigned int duration, | |
1092 | const struct wpabuf *probe_resp_ie) | |
1093 | { | |
1094 | struct wpa_supplicant *wpa_s = ctx; | |
1095 | ||
0e2e565a | 1096 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL); |
b22128ef JM |
1097 | |
1098 | if (wpa_drv_probe_req_report(wpa_s, 1) < 0) { | |
1099 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to " | |
1100 | "report received Probe Request frames"); | |
1101 | return -1; | |
1102 | } | |
1103 | ||
1104 | wpa_s->pending_listen_freq = freq; | |
1105 | wpa_s->pending_listen_duration = duration; | |
1106 | ||
1107 | if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) { | |
1108 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver " | |
1109 | "to remain on channel (%u MHz) for Listen " | |
1110 | "state", freq); | |
1111 | wpa_s->pending_listen_freq = 0; | |
1112 | return -1; | |
1113 | } | |
09d660b9 | 1114 | wpa_s->off_channel_freq = 0; |
07a30a66 | 1115 | wpa_s->roc_waiting_drv_freq = freq; |
b22128ef JM |
1116 | |
1117 | return 0; | |
1118 | } | |
1119 | ||
1120 | ||
1121 | static void wpas_stop_listen(void *ctx) | |
1122 | { | |
1123 | struct wpa_supplicant *wpa_s = ctx; | |
6cb22d2f | 1124 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
b22128ef JM |
1125 | wpa_drv_cancel_remain_on_channel(wpa_s); |
1126 | wpa_s->off_channel_freq = 0; | |
07a30a66 | 1127 | wpa_s->roc_waiting_drv_freq = 0; |
b22128ef | 1128 | } |
839b33ad | 1129 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL); |
b22128ef JM |
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; | |
9dbf53fe | 1137 | return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1); |
b22128ef JM |
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 | } | |
714b8b53 | 1392 | os_free(str); |
b22128ef JM |
1393 | |
1394 | if (count == 0) { | |
1395 | wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not " | |
1396 | "available"); | |
1397 | /* Status Code */ | |
bf608cad | 1398 | wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); |
b22128ef JM |
1399 | /* Response Data: empty */ |
1400 | } | |
1401 | ||
1402 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); | |
1403 | } | |
1404 | ||
1405 | ||
1406 | void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token, | |
1407 | u16 update_indic, const u8 *tlvs, size_t tlvs_len) | |
1408 | { | |
1409 | struct wpa_supplicant *wpa_s = ctx; | |
1410 | const u8 *pos = tlvs; | |
1411 | const u8 *end = tlvs + tlvs_len; | |
1412 | const u8 *tlv_end; | |
1413 | u16 slen; | |
1414 | struct wpabuf *resp; | |
1415 | u8 srv_proto, srv_trans_id; | |
1416 | size_t buf_len; | |
1417 | char *buf; | |
1418 | ||
1419 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs", | |
1420 | tlvs, tlvs_len); | |
1421 | buf_len = 2 * tlvs_len + 1; | |
1422 | buf = os_malloc(buf_len); | |
1423 | if (buf) { | |
1424 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); | |
1425 | wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d " | |
1426 | MACSTR " %u %u %s", | |
1427 | freq, MAC2STR(sa), dialog_token, update_indic, | |
1428 | buf); | |
1429 | os_free(buf); | |
1430 | } | |
1431 | ||
3b655312 | 1432 | if (wpa_s->p2p_sd_over_ctrl_iface) { |
1433 | wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, | |
1434 | update_indic, tlvs, tlvs_len); | |
b22128ef | 1435 | return; /* to be processed by an external program */ |
3b655312 | 1436 | } |
b22128ef JM |
1437 | |
1438 | resp = wpabuf_alloc(10000); | |
1439 | if (resp == NULL) | |
1440 | return; | |
1441 | ||
1442 | while (pos + 1 < end) { | |
1443 | wpa_printf(MSG_DEBUG, "P2P: Service Request TLV"); | |
1444 | slen = WPA_GET_LE16(pos); | |
1445 | pos += 2; | |
1446 | if (pos + slen > end || slen < 2) { | |
1447 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data " | |
1448 | "length"); | |
1449 | wpabuf_free(resp); | |
1450 | return; | |
1451 | } | |
1452 | tlv_end = pos + slen; | |
1453 | ||
1454 | srv_proto = *pos++; | |
1455 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", | |
1456 | srv_proto); | |
1457 | srv_trans_id = *pos++; | |
1458 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", | |
1459 | srv_trans_id); | |
1460 | ||
1461 | wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data", | |
1462 | pos, tlv_end - pos); | |
1463 | ||
6e6963ea JM |
1464 | |
1465 | if (wpa_s->force_long_sd) { | |
1466 | wpa_printf(MSG_DEBUG, "P2P: SD test - force long " | |
1467 | "response"); | |
1468 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); | |
1469 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); | |
1470 | goto done; | |
1471 | } | |
1472 | ||
b22128ef JM |
1473 | switch (srv_proto) { |
1474 | case P2P_SERV_ALL_SERVICES: | |
1475 | wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request " | |
1476 | "for all services"); | |
1477 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) && | |
1478 | dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { | |
1479 | wpa_printf(MSG_DEBUG, "P2P: No service " | |
1480 | "discovery protocols available"); | |
1481 | wpas_sd_add_proto_not_avail( | |
1482 | resp, P2P_SERV_ALL_SERVICES, | |
1483 | srv_trans_id); | |
1484 | break; | |
1485 | } | |
1486 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); | |
1487 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); | |
1488 | break; | |
1489 | case P2P_SERV_BONJOUR: | |
1490 | wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id, | |
1491 | pos, tlv_end - pos); | |
1492 | break; | |
1493 | case P2P_SERV_UPNP: | |
1494 | wpas_sd_req_upnp(wpa_s, resp, srv_trans_id, | |
1495 | pos, tlv_end - pos); | |
1496 | break; | |
1497 | default: | |
1498 | wpa_printf(MSG_DEBUG, "P2P: Unavailable service " | |
1499 | "protocol %u", srv_proto); | |
1500 | wpas_sd_add_proto_not_avail(resp, srv_proto, | |
1501 | srv_trans_id); | |
1502 | break; | |
1503 | } | |
1504 | ||
1505 | pos = tlv_end; | |
1506 | } | |
1507 | ||
6e6963ea | 1508 | done: |
e1653cac KRK |
1509 | wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, |
1510 | update_indic, tlvs, tlvs_len); | |
1511 | ||
b22128ef JM |
1512 | wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp); |
1513 | ||
1514 | wpabuf_free(resp); | |
1515 | } | |
1516 | ||
1517 | ||
1518 | void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic, | |
1519 | const u8 *tlvs, size_t tlvs_len) | |
1520 | { | |
1521 | struct wpa_supplicant *wpa_s = ctx; | |
1522 | const u8 *pos = tlvs; | |
1523 | const u8 *end = tlvs + tlvs_len; | |
1524 | const u8 *tlv_end; | |
1525 | u16 slen; | |
1526 | size_t buf_len; | |
1527 | char *buf; | |
1528 | ||
1529 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs", | |
1530 | tlvs, tlvs_len); | |
18708aad JM |
1531 | if (tlvs_len > 1500) { |
1532 | /* TODO: better way for handling this */ | |
1533 | wpa_msg_ctrl(wpa_s, MSG_INFO, | |
1534 | P2P_EVENT_SERV_DISC_RESP MACSTR | |
1535 | " %u <long response: %u bytes>", | |
1536 | MAC2STR(sa), update_indic, | |
1537 | (unsigned int) tlvs_len); | |
1538 | } else { | |
1539 | buf_len = 2 * tlvs_len + 1; | |
1540 | buf = os_malloc(buf_len); | |
1541 | if (buf) { | |
1542 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); | |
1543 | wpa_msg_ctrl(wpa_s, MSG_INFO, | |
1544 | P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s", | |
1545 | MAC2STR(sa), update_indic, buf); | |
1546 | os_free(buf); | |
1547 | } | |
b22128ef JM |
1548 | } |
1549 | ||
1550 | while (pos < end) { | |
1551 | u8 srv_proto, srv_trans_id, status; | |
1552 | ||
1553 | wpa_printf(MSG_DEBUG, "P2P: Service Response TLV"); | |
1554 | slen = WPA_GET_LE16(pos); | |
1555 | pos += 2; | |
1556 | if (pos + slen > end || slen < 3) { | |
1557 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data " | |
1558 | "length"); | |
1559 | return; | |
1560 | } | |
1561 | tlv_end = pos + slen; | |
1562 | ||
1563 | srv_proto = *pos++; | |
1564 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", | |
1565 | srv_proto); | |
1566 | srv_trans_id = *pos++; | |
1567 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", | |
1568 | srv_trans_id); | |
1569 | status = *pos++; | |
1570 | wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u", | |
1571 | status); | |
1572 | ||
1573 | wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data", | |
1574 | pos, tlv_end - pos); | |
1575 | ||
1576 | pos = tlv_end; | |
1577 | } | |
43a26f60 KRK |
1578 | |
1579 | wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len); | |
b22128ef JM |
1580 | } |
1581 | ||
1582 | ||
7165c5dc JM |
1583 | u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst, |
1584 | const struct wpabuf *tlvs) | |
b22128ef | 1585 | { |
3ac17eba | 1586 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
7165c5dc | 1587 | return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs); |
9526fd29 | 1588 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
7165c5dc JM |
1589 | return 0; |
1590 | return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs); | |
b22128ef JM |
1591 | } |
1592 | ||
1593 | ||
7165c5dc JM |
1594 | u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst, |
1595 | u8 version, const char *query) | |
b22128ef JM |
1596 | { |
1597 | struct wpabuf *tlvs; | |
7165c5dc | 1598 | u64 ret; |
b22128ef JM |
1599 | |
1600 | tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query)); | |
1601 | if (tlvs == NULL) | |
7165c5dc | 1602 | return 0; |
b22128ef JM |
1603 | wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query)); |
1604 | wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */ | |
1605 | wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */ | |
1606 | wpabuf_put_u8(tlvs, version); | |
1607 | wpabuf_put_str(tlvs, query); | |
1608 | ret = wpas_p2p_sd_request(wpa_s, dst, tlvs); | |
1609 | wpabuf_free(tlvs); | |
1610 | return ret; | |
1611 | } | |
1612 | ||
1613 | ||
7165c5dc | 1614 | int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req) |
b22128ef | 1615 | { |
3ac17eba | 1616 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
7165c5dc | 1617 | return wpa_drv_p2p_sd_cancel_request(wpa_s, req); |
9526fd29 JM |
1618 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
1619 | return -1; | |
7165c5dc JM |
1620 | return p2p_sd_cancel_request(wpa_s->global->p2p, |
1621 | (void *) (uintptr_t) req); | |
b22128ef JM |
1622 | } |
1623 | ||
1624 | ||
1625 | void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq, | |
1626 | const u8 *dst, u8 dialog_token, | |
1627 | const struct wpabuf *resp_tlvs) | |
1628 | { | |
3ac17eba JM |
1629 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
1630 | wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token, | |
1631 | resp_tlvs); | |
1632 | return; | |
1633 | } | |
9526fd29 JM |
1634 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
1635 | return; | |
b22128ef JM |
1636 | p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token, |
1637 | resp_tlvs); | |
1638 | } | |
1639 | ||
1640 | ||
1641 | void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s) | |
1642 | { | |
3ac17eba JM |
1643 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
1644 | wpa_drv_p2p_service_update(wpa_s); | |
1645 | return; | |
1646 | } | |
9526fd29 JM |
1647 | if (wpa_s->global->p2p) |
1648 | p2p_sd_service_update(wpa_s->global->p2p); | |
b22128ef JM |
1649 | } |
1650 | ||
1651 | ||
1652 | static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv) | |
1653 | { | |
1654 | dl_list_del(&bsrv->list); | |
1655 | wpabuf_free(bsrv->query); | |
1656 | wpabuf_free(bsrv->resp); | |
1657 | os_free(bsrv); | |
1658 | } | |
1659 | ||
1660 | ||
1661 | static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv) | |
1662 | { | |
1663 | dl_list_del(&usrv->list); | |
1664 | os_free(usrv->service); | |
1665 | os_free(usrv); | |
1666 | } | |
1667 | ||
1668 | ||
1669 | void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s) | |
1670 | { | |
1671 | struct p2p_srv_bonjour *bsrv, *bn; | |
1672 | struct p2p_srv_upnp *usrv, *un; | |
1673 | ||
1674 | dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour, | |
1675 | struct p2p_srv_bonjour, list) | |
1676 | wpas_p2p_srv_bonjour_free(bsrv); | |
1677 | ||
1678 | dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp, | |
1679 | struct p2p_srv_upnp, list) | |
1680 | wpas_p2p_srv_upnp_free(usrv); | |
1681 | ||
1682 | wpas_p2p_sd_service_update(wpa_s); | |
1683 | } | |
1684 | ||
1685 | ||
1686 | int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s, | |
1687 | struct wpabuf *query, struct wpabuf *resp) | |
1688 | { | |
1689 | struct p2p_srv_bonjour *bsrv; | |
1690 | ||
1691 | bsrv = wpas_p2p_service_get_bonjour(wpa_s, query); | |
1692 | if (bsrv) { | |
1693 | wpabuf_free(query); | |
1694 | wpabuf_free(bsrv->resp); | |
1695 | bsrv->resp = resp; | |
1696 | return 0; | |
1697 | } | |
1698 | ||
1699 | bsrv = os_zalloc(sizeof(*bsrv)); | |
1700 | if (bsrv == NULL) | |
1701 | return -1; | |
1702 | bsrv->query = query; | |
1703 | bsrv->resp = resp; | |
1704 | dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list); | |
1705 | ||
1706 | wpas_p2p_sd_service_update(wpa_s); | |
1707 | return 0; | |
1708 | } | |
1709 | ||
1710 | ||
1711 | int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s, | |
1712 | const struct wpabuf *query) | |
1713 | { | |
1714 | struct p2p_srv_bonjour *bsrv; | |
1715 | ||
1716 | bsrv = wpas_p2p_service_get_bonjour(wpa_s, query); | |
1717 | if (bsrv == NULL) | |
1718 | return -1; | |
1719 | wpas_p2p_srv_bonjour_free(bsrv); | |
1720 | wpas_p2p_sd_service_update(wpa_s); | |
1721 | return 0; | |
1722 | } | |
1723 | ||
1724 | ||
1725 | int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version, | |
1726 | const char *service) | |
1727 | { | |
1728 | struct p2p_srv_upnp *usrv; | |
1729 | ||
1730 | if (wpas_p2p_service_get_upnp(wpa_s, version, service)) | |
1731 | return 0; /* Already listed */ | |
1732 | usrv = os_zalloc(sizeof(*usrv)); | |
1733 | if (usrv == NULL) | |
1734 | return -1; | |
1735 | usrv->version = version; | |
1736 | usrv->service = os_strdup(service); | |
1737 | if (usrv->service == NULL) { | |
1738 | os_free(usrv); | |
1739 | return -1; | |
1740 | } | |
1741 | dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list); | |
1742 | ||
1743 | wpas_p2p_sd_service_update(wpa_s); | |
1744 | return 0; | |
1745 | } | |
1746 | ||
1747 | ||
1748 | int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version, | |
1749 | const char *service) | |
1750 | { | |
1751 | struct p2p_srv_upnp *usrv; | |
1752 | ||
1753 | usrv = wpas_p2p_service_get_upnp(wpa_s, version, service); | |
1754 | if (usrv == NULL) | |
1755 | return -1; | |
1756 | wpas_p2p_srv_upnp_free(usrv); | |
1757 | wpas_p2p_sd_service_update(wpa_s); | |
1758 | return 0; | |
1759 | } | |
1760 | ||
1761 | ||
1762 | static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s, | |
bbeee9b0 JB |
1763 | const u8 *peer, const char *params, |
1764 | unsigned int generated_pin) | |
b22128ef JM |
1765 | { |
1766 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s", | |
bbeee9b0 | 1767 | MAC2STR(peer), generated_pin, params); |
b22128ef JM |
1768 | } |
1769 | ||
1770 | ||
1771 | static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s, | |
1772 | const u8 *peer, const char *params) | |
1773 | { | |
1774 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s", | |
1775 | MAC2STR(peer), params); | |
1776 | } | |
1777 | ||
1778 | ||
1779 | void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods, | |
1780 | const u8 *dev_addr, const u8 *pri_dev_type, | |
1781 | const char *dev_name, u16 supp_config_methods, | |
c3f42784 JM |
1782 | u8 dev_capab, u8 group_capab, const u8 *group_id, |
1783 | size_t group_id_len) | |
b22128ef JM |
1784 | { |
1785 | struct wpa_supplicant *wpa_s = ctx; | |
1786 | char devtype[WPS_DEV_TYPE_BUFSIZE]; | |
c3f42784 | 1787 | char params[300]; |
b22128ef | 1788 | u8 empty_dev_type[8]; |
bbeee9b0 | 1789 | unsigned int generated_pin = 0; |
c3f42784 JM |
1790 | struct wpa_supplicant *group = NULL; |
1791 | ||
1792 | if (group_id) { | |
1793 | for (group = wpa_s->global->ifaces; group; group = group->next) | |
1794 | { | |
1795 | struct wpa_ssid *s = group->current_ssid; | |
1796 | if (s != NULL && | |
1797 | s->mode == WPAS_MODE_P2P_GO && | |
1798 | group_id_len - ETH_ALEN == s->ssid_len && | |
1799 | os_memcmp(group_id + ETH_ALEN, s->ssid, | |
1800 | s->ssid_len) == 0) | |
1801 | break; | |
1802 | } | |
1803 | } | |
b22128ef JM |
1804 | |
1805 | if (pri_dev_type == NULL) { | |
1806 | os_memset(empty_dev_type, 0, sizeof(empty_dev_type)); | |
1807 | pri_dev_type = empty_dev_type; | |
1808 | } | |
1809 | os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR | |
1810 | " pri_dev_type=%s name='%s' config_methods=0x%x " | |
c3f42784 | 1811 | "dev_capab=0x%x group_capab=0x%x%s%s", |
b22128ef JM |
1812 | MAC2STR(dev_addr), |
1813 | wps_dev_type_bin2str(pri_dev_type, devtype, | |
1814 | sizeof(devtype)), | |
c3f42784 JM |
1815 | dev_name, supp_config_methods, dev_capab, group_capab, |
1816 | group ? " group=" : "", | |
1817 | group ? group->ifname : ""); | |
b22128ef JM |
1818 | params[sizeof(params) - 1] = '\0'; |
1819 | ||
bbeee9b0 JB |
1820 | if (config_methods & WPS_CONFIG_DISPLAY) { |
1821 | generated_pin = wps_generate_pin(); | |
1822 | wpas_prov_disc_local_display(wpa_s, peer, params, | |
1823 | generated_pin); | |
1824 | } else if (config_methods & WPS_CONFIG_KEYPAD) | |
b22128ef JM |
1825 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
1826 | else if (config_methods & WPS_CONFIG_PUSHBUTTON) | |
1827 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR | |
1828 | "%s", MAC2STR(peer), params); | |
dd8a7e05 JB |
1829 | |
1830 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */, | |
1831 | P2P_PROV_DISC_SUCCESS, | |
1832 | config_methods, generated_pin); | |
b22128ef JM |
1833 | } |
1834 | ||
1835 | ||
1836 | void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods) | |
1837 | { | |
1838 | struct wpa_supplicant *wpa_s = ctx; | |
bbeee9b0 | 1839 | unsigned int generated_pin = 0; |
0918c4bf | 1840 | char params[20]; |
bbeee9b0 | 1841 | |
c1931635 JM |
1842 | if (wpa_s->pending_pd_before_join && |
1843 | (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || | |
1844 | os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { | |
1845 | wpa_s->pending_pd_before_join = 0; | |
1846 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " | |
1847 | "join-existing-group operation"); | |
1848 | wpas_p2p_join_start(wpa_s); | |
1849 | return; | |
1850 | } | |
1851 | ||
0918c4bf JM |
1852 | if (wpa_s->pending_pd_use == AUTO_PD_JOIN || |
1853 | wpa_s->pending_pd_use == AUTO_PD_GO_NEG) | |
1854 | os_snprintf(params, sizeof(params), " peer_go=%d", | |
1855 | wpa_s->pending_pd_use == AUTO_PD_JOIN); | |
1856 | else | |
1857 | params[0] = '\0'; | |
1858 | ||
b22128ef | 1859 | if (config_methods & WPS_CONFIG_DISPLAY) |
0918c4bf | 1860 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
bbeee9b0 JB |
1861 | else if (config_methods & WPS_CONFIG_KEYPAD) { |
1862 | generated_pin = wps_generate_pin(); | |
0918c4bf JM |
1863 | wpas_prov_disc_local_display(wpa_s, peer, params, |
1864 | generated_pin); | |
bbeee9b0 | 1865 | } else if (config_methods & WPS_CONFIG_PUSHBUTTON) |
0918c4bf JM |
1866 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR |
1867 | "%s", MAC2STR(peer), params); | |
a482883f | 1868 | |
dd8a7e05 JB |
1869 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
1870 | P2P_PROV_DISC_SUCCESS, | |
1871 | config_methods, generated_pin); | |
b22128ef JM |
1872 | } |
1873 | ||
1874 | ||
19df9b07 JM |
1875 | static void wpas_prov_disc_fail(void *ctx, const u8 *peer, |
1876 | enum p2p_prov_disc_status status) | |
dd8a7e05 JB |
1877 | { |
1878 | struct wpa_supplicant *wpa_s = ctx; | |
1879 | ||
f65a239b DG |
1880 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE |
1881 | " p2p_dev_addr=" MACSTR " status=%d", | |
25a94f52 | 1882 | MAC2STR(peer), status); |
f65a239b | 1883 | |
dd8a7e05 JB |
1884 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
1885 | status, 0, 0); | |
1886 | } | |
1887 | ||
1888 | ||
b22128ef JM |
1889 | static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid, |
1890 | const u8 *go_dev_addr, const u8 *ssid, | |
1891 | size_t ssid_len, int *go, u8 *group_bssid, | |
1892 | int *force_freq, int persistent_group) | |
1893 | { | |
1894 | struct wpa_supplicant *wpa_s = ctx; | |
1895 | struct wpa_ssid *s; | |
1896 | u8 cur_bssid[ETH_ALEN]; | |
1897 | int res; | |
ab72eb52 | 1898 | struct wpa_supplicant *grp; |
b22128ef JM |
1899 | |
1900 | if (!persistent_group) { | |
1901 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR | |
1902 | " to join an active group", MAC2STR(sa)); | |
108def93 | 1903 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
131cb37c JM |
1904 | (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN) |
1905 | == 0 || | |
1906 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) { | |
108def93 JM |
1907 | wpa_printf(MSG_DEBUG, "P2P: Accept previously " |
1908 | "authorized invitation"); | |
1909 | goto accept_inv; | |
1910 | } | |
b22128ef JM |
1911 | /* |
1912 | * Do not accept the invitation automatically; notify user and | |
1913 | * request approval. | |
1914 | */ | |
1915 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; | |
1916 | } | |
1917 | ||
ab72eb52 JM |
1918 | grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go); |
1919 | if (grp) { | |
1920 | wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already " | |
1921 | "running persistent group"); | |
1922 | if (*go) | |
1923 | os_memcpy(group_bssid, grp->own_addr, ETH_ALEN); | |
1924 | goto accept_inv; | |
1925 | } | |
1926 | ||
b22128ef JM |
1927 | if (!wpa_s->conf->persistent_reconnect) |
1928 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; | |
1929 | ||
1930 | for (s = wpa_s->conf->ssid; s; s = s->next) { | |
1931 | if (s->disabled == 2 && | |
1932 | os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 && | |
1933 | s->ssid_len == ssid_len && | |
1934 | os_memcmp(ssid, s->ssid, ssid_len) == 0) | |
1935 | break; | |
1936 | } | |
1937 | ||
1938 | if (!s) { | |
1939 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR | |
1940 | " requested reinvocation of an unknown group", | |
1941 | MAC2STR(sa)); | |
1942 | return P2P_SC_FAIL_UNKNOWN_GROUP; | |
1943 | } | |
1944 | ||
1945 | if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) { | |
1946 | *go = 1; | |
1947 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { | |
1948 | wpa_printf(MSG_DEBUG, "P2P: The only available " | |
1949 | "interface is already in use - reject " | |
1950 | "invitation"); | |
1951 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; | |
1952 | } | |
1953 | os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN); | |
1954 | } else if (s->mode == WPAS_MODE_P2P_GO) { | |
1955 | *go = 1; | |
1956 | if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0) | |
1957 | { | |
1958 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " | |
1959 | "interface address for the group"); | |
1960 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; | |
1961 | } | |
1962 | os_memcpy(group_bssid, wpa_s->pending_interface_addr, | |
1963 | ETH_ALEN); | |
1964 | } | |
1965 | ||
108def93 | 1966 | accept_inv: |
b22128ef JM |
1967 | if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 && |
1968 | wpa_s->assoc_freq) { | |
1969 | wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match " | |
1970 | "the channel we are already using"); | |
1971 | *force_freq = wpa_s->assoc_freq; | |
1972 | } | |
1973 | ||
1974 | res = wpa_drv_shared_freq(wpa_s); | |
1975 | if (res > 0) { | |
1976 | wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match " | |
1977 | "with the channel we are already using on a " | |
1978 | "shared interface"); | |
1979 | *force_freq = res; | |
1980 | } | |
1981 | ||
1982 | return P2P_SC_SUCCESS; | |
1983 | } | |
1984 | ||
1985 | ||
1986 | static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid, | |
1987 | const u8 *ssid, size_t ssid_len, | |
1988 | const u8 *go_dev_addr, u8 status, | |
1989 | int op_freq) | |
1990 | { | |
1991 | struct wpa_supplicant *wpa_s = ctx; | |
1992 | struct wpa_ssid *s; | |
1993 | ||
1994 | for (s = wpa_s->conf->ssid; s; s = s->next) { | |
1995 | if (s->disabled == 2 && | |
1996 | s->ssid_len == ssid_len && | |
1997 | os_memcmp(ssid, s->ssid, ssid_len) == 0) | |
1998 | break; | |
1999 | } | |
2000 | ||
2001 | if (status == P2P_SC_SUCCESS) { | |
2002 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR | |
2003 | " was accepted; op_freq=%d MHz", | |
2004 | MAC2STR(sa), op_freq); | |
2005 | if (s) { | |
2006 | wpas_p2p_group_add_persistent( | |
2007 | wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0); | |
2049af2b | 2008 | } else if (bssid) { |
108def93 | 2009 | wpas_p2p_join(wpa_s, bssid, go_dev_addr, |
b31be3a0 | 2010 | wpa_s->p2p_wps_method, 0); |
b22128ef JM |
2011 | } |
2012 | return; | |
2013 | } | |
2014 | ||
2015 | if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { | |
2016 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR | |
2017 | " was rejected (status %u)", MAC2STR(sa), status); | |
2018 | return; | |
2019 | } | |
2020 | ||
2021 | if (!s) { | |
2022 | if (bssid) { | |
2023 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED | |
2024 | "sa=" MACSTR " go_dev_addr=" MACSTR | |
2025 | " bssid=" MACSTR " unknown-network", | |
2026 | MAC2STR(sa), MAC2STR(go_dev_addr), | |
2027 | MAC2STR(bssid)); | |
2028 | } else { | |
2029 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED | |
2030 | "sa=" MACSTR " go_dev_addr=" MACSTR | |
2031 | " unknown-network", | |
2032 | MAC2STR(sa), MAC2STR(go_dev_addr)); | |
2033 | } | |
2034 | return; | |
2035 | } | |
2036 | ||
2037 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR | |
2038 | " persistent=%d", MAC2STR(sa), s->id); | |
2039 | } | |
2040 | ||
2041 | ||
2042 | static void wpas_invitation_result(void *ctx, int status, const u8 *bssid) | |
2043 | { | |
2044 | struct wpa_supplicant *wpa_s = ctx; | |
2045 | struct wpa_ssid *ssid; | |
2046 | ||
2047 | if (bssid) { | |
2048 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT | |
2049 | "status=%d " MACSTR, | |
2050 | status, MAC2STR(bssid)); | |
2051 | } else { | |
2052 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT | |
2053 | "status=%d ", status); | |
2054 | } | |
5ccdf84f | 2055 | wpas_notify_p2p_invitation_result(wpa_s, status, bssid); |
b22128ef | 2056 | |
706887fc JM |
2057 | if (wpa_s->pending_invite_ssid_id == -1) |
2058 | return; /* Invitation to active group */ | |
2059 | ||
b22128ef JM |
2060 | if (status != P2P_SC_SUCCESS) { |
2061 | wpas_p2p_remove_pending_group_interface(wpa_s); | |
2062 | return; | |
2063 | } | |
2064 | ||
2065 | ssid = wpa_config_get_network(wpa_s->conf, | |
2066 | wpa_s->pending_invite_ssid_id); | |
2067 | if (ssid == NULL) { | |
2068 | wpa_printf(MSG_ERROR, "P2P: Could not find persistent group " | |
2069 | "data matching with invitation"); | |
2070 | return; | |
2071 | } | |
2072 | ||
2073 | wpas_p2p_group_add_persistent(wpa_s, ssid, | |
2074 | ssid->mode == WPAS_MODE_P2P_GO, 0); | |
2075 | } | |
2076 | ||
2077 | ||
6f3bc72b JM |
2078 | static int wpas_p2p_disallowed_freq(struct wpa_global *global, |
2079 | unsigned int freq) | |
2080 | { | |
2081 | unsigned int i; | |
2082 | ||
2083 | if (global->p2p_disallow_freq == NULL) | |
2084 | return 0; | |
2085 | ||
2086 | for (i = 0; i < global->num_p2p_disallow_freq; i++) { | |
2087 | if (freq >= global->p2p_disallow_freq[i].min && | |
2088 | freq <= global->p2p_disallow_freq[i].max) | |
2089 | return 1; | |
2090 | } | |
2091 | ||
2092 | return 0; | |
2093 | } | |
2094 | ||
2095 | ||
2096 | static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan) | |
2097 | { | |
2098 | reg->channel[reg->channels] = chan; | |
2099 | reg->channels++; | |
2100 | } | |
2101 | ||
2102 | ||
ac8d1011 JM |
2103 | static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s, |
2104 | struct p2p_channels *chan) | |
2105 | { | |
2106 | int i, cla = 0; | |
2107 | ||
2108 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz " | |
2109 | "band"); | |
2110 | ||
2111 | /* Operating class 81 - 2.4 GHz band channels 1..13 */ | |
2112 | chan->reg_class[cla].reg_class = 81; | |
6f3bc72b JM |
2113 | chan->reg_class[cla].channels = 0; |
2114 | for (i = 0; i < 11; i++) { | |
2115 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5)) | |
2116 | wpas_p2p_add_chan(&chan->reg_class[cla], i + 1); | |
2117 | } | |
2118 | if (chan->reg_class[cla].channels) | |
2119 | cla++; | |
ac8d1011 JM |
2120 | |
2121 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz " | |
2122 | "band"); | |
2123 | ||
2124 | /* Operating class 115 - 5 GHz, channels 36-48 */ | |
2125 | chan->reg_class[cla].reg_class = 115; | |
6f3bc72b JM |
2126 | chan->reg_class[cla].channels = 0; |
2127 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5)) | |
2128 | wpas_p2p_add_chan(&chan->reg_class[cla], 36); | |
2129 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5)) | |
2130 | wpas_p2p_add_chan(&chan->reg_class[cla], 40); | |
2131 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5)) | |
2132 | wpas_p2p_add_chan(&chan->reg_class[cla], 44); | |
2133 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5)) | |
2134 | wpas_p2p_add_chan(&chan->reg_class[cla], 48); | |
2135 | if (chan->reg_class[cla].channels) | |
2136 | cla++; | |
ac8d1011 JM |
2137 | |
2138 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz " | |
2139 | "band"); | |
2140 | ||
2141 | /* Operating class 124 - 5 GHz, channels 149,153,157,161 */ | |
2142 | chan->reg_class[cla].reg_class = 124; | |
6f3bc72b JM |
2143 | chan->reg_class[cla].channels = 0; |
2144 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5)) | |
2145 | wpas_p2p_add_chan(&chan->reg_class[cla], 149); | |
2146 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5)) | |
2147 | wpas_p2p_add_chan(&chan->reg_class[cla], 153); | |
2148 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5)) | |
2149 | wpas_p2p_add_chan(&chan->reg_class[cla], 157); | |
2150 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5)) | |
2151 | wpas_p2p_add_chan(&chan->reg_class[cla], 161); | |
2152 | if (chan->reg_class[cla].channels) | |
2153 | cla++; | |
ac8d1011 JM |
2154 | |
2155 | chan->reg_classes = cla; | |
2156 | return 0; | |
2157 | } | |
2158 | ||
2159 | ||
2160 | static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes, | |
2161 | u16 num_modes, | |
2162 | enum hostapd_hw_mode mode) | |
2163 | { | |
2164 | u16 i; | |
2165 | ||
2166 | for (i = 0; i < num_modes; i++) { | |
2167 | if (modes[i].mode == mode) | |
2168 | return &modes[i]; | |
2169 | } | |
2170 | ||
2171 | return NULL; | |
2172 | } | |
2173 | ||
2174 | ||
6f3bc72b JM |
2175 | static int has_channel(struct wpa_global *global, |
2176 | struct hostapd_hw_modes *mode, u8 chan, int *flags) | |
ac8d1011 JM |
2177 | { |
2178 | int i; | |
6f3bc72b JM |
2179 | unsigned int freq; |
2180 | ||
2181 | freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) + | |
2182 | chan * 5; | |
2183 | if (wpas_p2p_disallowed_freq(global, freq)) | |
2184 | return 0; | |
ac8d1011 JM |
2185 | |
2186 | for (i = 0; i < mode->num_channels; i++) { | |
2187 | if (mode->channels[i].chan == chan) { | |
51222429 JM |
2188 | if (flags) |
2189 | *flags = mode->channels[i].flag; | |
ac8d1011 JM |
2190 | return !(mode->channels[i].flag & |
2191 | (HOSTAPD_CHAN_DISABLED | | |
2192 | HOSTAPD_CHAN_PASSIVE_SCAN | | |
2193 | HOSTAPD_CHAN_NO_IBSS | | |
2194 | HOSTAPD_CHAN_RADAR)); | |
2195 | } | |
2196 | } | |
2197 | ||
2198 | return 0; | |
2199 | } | |
2200 | ||
2201 | ||
2202 | struct p2p_oper_class_map { | |
2203 | enum hostapd_hw_mode mode; | |
2204 | u8 op_class; | |
2205 | u8 min_chan; | |
2206 | u8 max_chan; | |
2207 | u8 inc; | |
51222429 | 2208 | enum { BW20, BW40PLUS, BW40MINUS } bw; |
ac8d1011 JM |
2209 | }; |
2210 | ||
b22128ef | 2211 | static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s, |
ac8d1011 | 2212 | struct p2p_channels *chan) |
b22128ef | 2213 | { |
6bf731e8 | 2214 | struct hostapd_hw_modes *mode; |
ac8d1011 JM |
2215 | int cla, op; |
2216 | struct p2p_oper_class_map op_class[] = { | |
51222429 JM |
2217 | { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 }, |
2218 | { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 }, | |
2219 | #if 0 /* Do not enable HT40 on 2 GHz for now */ | |
2220 | { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS }, | |
2221 | { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS }, | |
ac8d1011 | 2222 | #endif |
51222429 JM |
2223 | { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 }, |
2224 | { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 }, | |
2225 | { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS }, | |
2226 | { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS }, | |
2227 | { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS }, | |
2228 | { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS }, | |
2229 | { -1, 0, 0, 0, 0, BW20 } | |
ac8d1011 | 2230 | }; |
b22128ef | 2231 | |
6bf731e8 | 2232 | if (wpa_s->hw.modes == NULL) { |
b22128ef JM |
2233 | wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching " |
2234 | "of all supported channels; assume dualband " | |
2235 | "support"); | |
ac8d1011 | 2236 | return wpas_p2p_default_channels(wpa_s, chan); |
b22128ef JM |
2237 | } |
2238 | ||
2239 | cla = 0; | |
2240 | ||
ac8d1011 JM |
2241 | for (op = 0; op_class[op].op_class; op++) { |
2242 | struct p2p_oper_class_map *o = &op_class[op]; | |
2243 | u8 ch; | |
2244 | struct p2p_reg_class *reg = NULL; | |
b22128ef | 2245 | |
6bf731e8 | 2246 | mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode); |
ac8d1011 JM |
2247 | if (mode == NULL) |
2248 | continue; | |
2249 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { | |
51222429 | 2250 | int flag; |
6f3bc72b | 2251 | if (!has_channel(wpa_s->global, mode, ch, &flag)) |
51222429 JM |
2252 | continue; |
2253 | if (o->bw == BW40MINUS && | |
2254 | (!(flag & HOSTAPD_CHAN_HT40MINUS) || | |
6f3bc72b | 2255 | !has_channel(wpa_s->global, mode, ch - 4, NULL))) |
51222429 JM |
2256 | continue; |
2257 | if (o->bw == BW40PLUS && | |
2258 | (!(flag & HOSTAPD_CHAN_HT40PLUS) || | |
6f3bc72b | 2259 | !has_channel(wpa_s->global, mode, ch + 4, NULL))) |
ac8d1011 JM |
2260 | continue; |
2261 | if (reg == NULL) { | |
2262 | wpa_printf(MSG_DEBUG, "P2P: Add operating " | |
2263 | "class %u", o->op_class); | |
2264 | reg = &chan->reg_class[cla]; | |
2265 | cla++; | |
2266 | reg->reg_class = o->op_class; | |
2267 | } | |
2268 | reg->channel[reg->channels] = ch; | |
2269 | reg->channels++; | |
2270 | } | |
2271 | if (reg) { | |
2272 | wpa_hexdump(MSG_DEBUG, "P2P: Channels", | |
2273 | reg->channel, reg->channels); | |
2274 | } | |
b22128ef JM |
2275 | } |
2276 | ||
ac8d1011 | 2277 | chan->reg_classes = cla; |
b22128ef | 2278 | |
b22128ef JM |
2279 | return 0; |
2280 | } | |
2281 | ||
2282 | ||
2283 | static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf, | |
2284 | size_t buf_len) | |
2285 | { | |
2286 | struct wpa_supplicant *wpa_s = ctx; | |
2287 | ||
2288 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
2289 | if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0) | |
2290 | break; | |
2291 | } | |
2292 | if (wpa_s == NULL) | |
2293 | return -1; | |
2294 | ||
2295 | return wpa_drv_get_noa(wpa_s, buf, buf_len); | |
2296 | } | |
2297 | ||
2298 | ||
b1aebbc4 JM |
2299 | static int wpas_go_connected(void *ctx, const u8 *dev_addr) |
2300 | { | |
2301 | struct wpa_supplicant *wpa_s = ctx; | |
2302 | ||
2303 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
2304 | struct wpa_ssid *ssid = wpa_s->current_ssid; | |
2305 | if (ssid == NULL) | |
2306 | continue; | |
2307 | if (ssid->mode != WPAS_MODE_INFRA) | |
2308 | continue; | |
d9a0f666 PN |
2309 | if (wpa_s->wpa_state != WPA_COMPLETED && |
2310 | wpa_s->wpa_state != WPA_GROUP_HANDSHAKE) | |
b1aebbc4 JM |
2311 | continue; |
2312 | if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0) | |
2313 | return 1; | |
2314 | } | |
2315 | ||
2316 | return 0; | |
2317 | } | |
2318 | ||
2319 | ||
b22128ef JM |
2320 | /** |
2321 | * wpas_p2p_init - Initialize P2P module for %wpa_supplicant | |
2322 | * @global: Pointer to global data from wpa_supplicant_init() | |
2323 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
2324 | * Returns: 0 on success, -1 on failure | |
2325 | */ | |
2326 | int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s) | |
2327 | { | |
2328 | struct p2p_config p2p; | |
2329 | unsigned int r; | |
f95cac27 | 2330 | int i; |
b22128ef JM |
2331 | |
2332 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) | |
2333 | return 0; | |
2334 | ||
b22128ef JM |
2335 | if (global->p2p) |
2336 | return 0; | |
2337 | ||
3ac17eba JM |
2338 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
2339 | struct p2p_params params; | |
2340 | ||
2341 | wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management"); | |
2342 | os_memset(¶ms, 0, sizeof(params)); | |
2343 | params.dev_name = wpa_s->conf->device_name; | |
2f646b6e JB |
2344 | os_memcpy(params.pri_dev_type, wpa_s->conf->device_type, |
2345 | WPS_DEV_TYPE_LEN); | |
2346 | params.num_sec_dev_types = wpa_s->conf->num_sec_device_types; | |
2347 | os_memcpy(params.sec_dev_type, | |
2348 | wpa_s->conf->sec_device_type, | |
2349 | params.num_sec_dev_types * WPS_DEV_TYPE_LEN); | |
2350 | ||
3ac17eba JM |
2351 | if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0) |
2352 | return -1; | |
2353 | ||
2354 | return 0; | |
2355 | } | |
2356 | ||
b22128ef JM |
2357 | os_memset(&p2p, 0, sizeof(p2p)); |
2358 | p2p.msg_ctx = wpa_s; | |
2359 | p2p.cb_ctx = wpa_s; | |
2360 | p2p.p2p_scan = wpas_p2p_scan; | |
2361 | p2p.send_action = wpas_send_action; | |
2362 | p2p.send_action_done = wpas_send_action_done; | |
2363 | p2p.go_neg_completed = wpas_go_neg_completed; | |
2364 | p2p.go_neg_req_rx = wpas_go_neg_req_rx; | |
2365 | p2p.dev_found = wpas_dev_found; | |
56eeb8f2 | 2366 | p2p.dev_lost = wpas_dev_lost; |
b22128ef JM |
2367 | p2p.start_listen = wpas_start_listen; |
2368 | p2p.stop_listen = wpas_stop_listen; | |
2369 | p2p.send_probe_resp = wpas_send_probe_resp; | |
2370 | p2p.sd_request = wpas_sd_request; | |
2371 | p2p.sd_response = wpas_sd_response; | |
2372 | p2p.prov_disc_req = wpas_prov_disc_req; | |
2373 | p2p.prov_disc_resp = wpas_prov_disc_resp; | |
dd8a7e05 | 2374 | p2p.prov_disc_fail = wpas_prov_disc_fail; |
b22128ef JM |
2375 | p2p.invitation_process = wpas_invitation_process; |
2376 | p2p.invitation_received = wpas_invitation_received; | |
2377 | p2p.invitation_result = wpas_invitation_result; | |
2378 | p2p.get_noa = wpas_get_noa; | |
b1aebbc4 | 2379 | p2p.go_connected = wpas_go_connected; |
b22128ef JM |
2380 | |
2381 | os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN); | |
d7e70476 | 2382 | os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN); |
b22128ef | 2383 | p2p.dev_name = wpa_s->conf->device_name; |
b6e01800 JM |
2384 | p2p.manufacturer = wpa_s->conf->manufacturer; |
2385 | p2p.model_name = wpa_s->conf->model_name; | |
2386 | p2p.model_number = wpa_s->conf->model_number; | |
2387 | p2p.serial_number = wpa_s->conf->serial_number; | |
2388 | if (wpa_s->wps) { | |
2389 | os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16); | |
2390 | p2p.config_methods = wpa_s->wps->config_methods; | |
2391 | } | |
b22128ef JM |
2392 | |
2393 | if (wpa_s->conf->p2p_listen_reg_class && | |
2394 | wpa_s->conf->p2p_listen_channel) { | |
2395 | p2p.reg_class = wpa_s->conf->p2p_listen_reg_class; | |
2396 | p2p.channel = wpa_s->conf->p2p_listen_channel; | |
2397 | } else { | |
2398 | p2p.reg_class = 81; | |
2399 | /* | |
2400 | * Pick one of the social channels randomly as the listen | |
2401 | * channel. | |
2402 | */ | |
2403 | os_get_random((u8 *) &r, sizeof(r)); | |
2404 | p2p.channel = 1 + (r % 3) * 5; | |
2405 | } | |
7cfc4ac3 | 2406 | wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel); |
b22128ef JM |
2407 | |
2408 | if (wpa_s->conf->p2p_oper_reg_class && | |
2409 | wpa_s->conf->p2p_oper_channel) { | |
2410 | p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class; | |
2411 | p2p.op_channel = wpa_s->conf->p2p_oper_channel; | |
7cfc4ac3 AGS |
2412 | p2p.cfg_op_channel = 1; |
2413 | wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: " | |
2414 | "%d:%d", p2p.op_reg_class, p2p.op_channel); | |
2415 | ||
b22128ef JM |
2416 | } else { |
2417 | p2p.op_reg_class = 81; | |
2418 | /* | |
7cfc4ac3 AGS |
2419 | * Use random operation channel from (1, 6, 11) if no other |
2420 | * preference is indicated. | |
b22128ef | 2421 | */ |
b7412dab | 2422 | os_get_random((u8 *) &r, sizeof(r)); |
7cfc4ac3 AGS |
2423 | p2p.op_channel = 1 + (r % 3) * 5; |
2424 | p2p.cfg_op_channel = 0; | |
2425 | wpa_printf(MSG_DEBUG, "P2P: Random operating channel: " | |
2426 | "%d:%d", p2p.op_reg_class, p2p.op_channel); | |
b22128ef | 2427 | } |
b22128ef JM |
2428 | if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
2429 | os_memcpy(p2p.country, wpa_s->conf->country, 2); | |
2430 | p2p.country[2] = 0x04; | |
2431 | } else | |
aaca6505 | 2432 | os_memcpy(p2p.country, "XX\x04", 3); |
b22128ef | 2433 | |
ac8d1011 | 2434 | if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) { |
b22128ef JM |
2435 | wpa_printf(MSG_ERROR, "P2P: Failed to configure supported " |
2436 | "channel list"); | |
2437 | return -1; | |
2438 | } | |
2439 | ||
2f646b6e JB |
2440 | os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type, |
2441 | WPS_DEV_TYPE_LEN); | |
b22128ef | 2442 | |
2f646b6e JB |
2443 | p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types; |
2444 | os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type, | |
2445 | p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN); | |
b22128ef JM |
2446 | |
2447 | p2p.concurrent_operations = !!(wpa_s->drv_flags & | |
2448 | WPA_DRIVER_FLAGS_P2P_CONCURRENT); | |
2449 | ||
de979d8f | 2450 | p2p.max_peers = 100; |
b22128ef JM |
2451 | |
2452 | if (wpa_s->conf->p2p_ssid_postfix) { | |
2453 | p2p.ssid_postfix_len = | |
2454 | os_strlen(wpa_s->conf->p2p_ssid_postfix); | |
2455 | if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix)) | |
2456 | p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix); | |
2457 | os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix, | |
2458 | p2p.ssid_postfix_len); | |
2459 | } | |
2460 | ||
0f66abd2 SS |
2461 | p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss; |
2462 | ||
b22128ef JM |
2463 | global->p2p = p2p_init(&p2p); |
2464 | if (global->p2p == NULL) | |
2465 | return -1; | |
2466 | ||
f95cac27 JMB |
2467 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { |
2468 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) | |
2469 | continue; | |
2470 | p2p_add_wps_vendor_extension( | |
2471 | global->p2p, wpa_s->conf->wps_vendor_ext[i]); | |
2472 | } | |
2473 | ||
b22128ef JM |
2474 | return 0; |
2475 | } | |
2476 | ||
2477 | ||
2478 | /** | |
2479 | * wpas_p2p_deinit - Deinitialize per-interface P2P data | |
2480 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
2481 | * | |
2482 | * This function deinitialize per-interface P2P data. | |
2483 | */ | |
2484 | void wpas_p2p_deinit(struct wpa_supplicant *wpa_s) | |
2485 | { | |
2486 | if (wpa_s->driver && wpa_s->drv_priv) | |
2487 | wpa_drv_probe_req_report(wpa_s, 0); | |
ec437d9e JJ |
2488 | |
2489 | if (wpa_s->go_params) { | |
2490 | /* Clear any stored provisioning info */ | |
2491 | p2p_clear_provisioning_info( | |
2492 | wpa_s->global->p2p, | |
10531d21 | 2493 | wpa_s->go_params->peer_device_addr); |
ec437d9e JJ |
2494 | } |
2495 | ||
b22128ef JM |
2496 | os_free(wpa_s->go_params); |
2497 | wpa_s->go_params = NULL; | |
b22128ef | 2498 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
ef922c4a | 2499 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
f63b8542 | 2500 | eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL); |
b22128ef JM |
2501 | wpa_s->p2p_long_listen = 0; |
2502 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); | |
3071e181 | 2503 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
b22128ef JM |
2504 | wpas_p2p_remove_pending_group_interface(wpa_s); |
2505 | ||
2506 | /* TODO: remove group interface from the driver if this wpa_s instance | |
2507 | * is on top of a P2P group interface */ | |
2508 | } | |
2509 | ||
2510 | ||
2511 | /** | |
2512 | * wpas_p2p_deinit_global - Deinitialize global P2P module | |
2513 | * @global: Pointer to global data from wpa_supplicant_init() | |
2514 | * | |
2515 | * This function deinitializes the global (per device) P2P module. | |
2516 | */ | |
2517 | void wpas_p2p_deinit_global(struct wpa_global *global) | |
2518 | { | |
2519 | struct wpa_supplicant *wpa_s, *tmp; | |
b22128ef JM |
2520 | |
2521 | if (global->p2p == NULL) | |
2522 | return; | |
2523 | ||
2524 | /* Remove remaining P2P group interfaces */ | |
2525 | wpa_s = global->ifaces; | |
2a43101e JM |
2526 | if (wpa_s) |
2527 | wpas_p2p_service_flush(wpa_s); | |
b22128ef JM |
2528 | while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) |
2529 | wpa_s = wpa_s->next; | |
2530 | while (wpa_s) { | |
b22128ef JM |
2531 | tmp = global->ifaces; |
2532 | while (tmp && | |
2533 | (tmp == wpa_s || | |
2534 | tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) { | |
2535 | tmp = tmp->next; | |
2536 | } | |
2537 | if (tmp == NULL) | |
2538 | break; | |
103b8f4d NS |
2539 | /* Disconnect from the P2P group and deinit the interface */ |
2540 | wpas_p2p_disconnect(tmp); | |
b22128ef JM |
2541 | } |
2542 | ||
743ef799 JM |
2543 | /* |
2544 | * Deinit GO data on any possibly remaining interface (if main | |
2545 | * interface is used as GO). | |
2546 | */ | |
2547 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
2548 | if (wpa_s->ap_iface) | |
2549 | wpas_p2p_group_deinit(wpa_s); | |
2550 | } | |
2551 | ||
b22128ef JM |
2552 | p2p_deinit(global->p2p); |
2553 | global->p2p = NULL; | |
2554 | } | |
2555 | ||
2556 | ||
2557 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s) | |
2558 | { | |
971e357f JM |
2559 | if (wpa_s->drv_flags & |
2560 | (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE | | |
2561 | WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P)) | |
b22128ef JM |
2562 | return 1; /* P2P group requires a new interface in every case |
2563 | */ | |
2564 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)) | |
2565 | return 0; /* driver does not support concurrent operations */ | |
2566 | if (wpa_s->global->ifaces->next) | |
2567 | return 1; /* more that one interface already in use */ | |
2568 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) | |
2569 | return 1; /* this interface is already in use */ | |
2570 | return 0; | |
2571 | } | |
2572 | ||
2573 | ||
2574 | static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s, | |
2575 | const u8 *peer_addr, | |
2576 | enum p2p_wps_method wps_method, | |
2577 | int go_intent, const u8 *own_interface_addr, | |
23c84252 JM |
2578 | unsigned int force_freq, int persistent_group, |
2579 | struct wpa_ssid *ssid) | |
b22128ef | 2580 | { |
acc247b2 JM |
2581 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
2582 | persistent_group = 2; | |
2583 | ||
3ac17eba JM |
2584 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
2585 | return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method, | |
2586 | go_intent, own_interface_addr, | |
2587 | force_freq, persistent_group); | |
2588 | } | |
2589 | ||
b22128ef JM |
2590 | return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method, |
2591 | go_intent, own_interface_addr, force_freq, | |
23c84252 JM |
2592 | persistent_group, ssid ? ssid->ssid : NULL, |
2593 | ssid ? ssid->ssid_len : 0); | |
b22128ef JM |
2594 | } |
2595 | ||
2596 | ||
2597 | static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s, | |
2598 | const u8 *peer_addr, | |
2599 | enum p2p_wps_method wps_method, | |
2600 | int go_intent, const u8 *own_interface_addr, | |
23c84252 JM |
2601 | unsigned int force_freq, int persistent_group, |
2602 | struct wpa_ssid *ssid) | |
b22128ef | 2603 | { |
acc247b2 JM |
2604 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
2605 | persistent_group = 2; | |
2606 | ||
3ac17eba JM |
2607 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
2608 | return -1; | |
2609 | ||
b22128ef JM |
2610 | return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method, |
2611 | go_intent, own_interface_addr, force_freq, | |
23c84252 JM |
2612 | persistent_group, ssid ? ssid->ssid : NULL, |
2613 | ssid ? ssid->ssid_len : 0); | |
b22128ef JM |
2614 | } |
2615 | ||
2616 | ||
9b1ab931 JM |
2617 | static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s) |
2618 | { | |
2619 | wpa_s->p2p_join_scan_count++; | |
2620 | wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d", | |
2621 | wpa_s->p2p_join_scan_count); | |
2622 | if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) { | |
2623 | wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR | |
2624 | " for join operationg - stop join attempt", | |
2625 | MAC2STR(wpa_s->pending_join_iface_addr)); | |
2626 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | |
0918c4bf JM |
2627 | if (wpa_s->p2p_auto_pd) { |
2628 | wpa_s->p2p_auto_pd = 0; | |
2629 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE | |
2630 | " p2p_dev_addr=" MACSTR " status=N/A", | |
2631 | MAC2STR(wpa_s->pending_join_dev_addr)); | |
2632 | return; | |
2633 | } | |
9b1ab931 JM |
2634 | wpa_msg(wpa_s->parent, MSG_INFO, |
2635 | P2P_EVENT_GROUP_FORMATION_FAILURE); | |
2636 | } | |
2637 | } | |
2638 | ||
2639 | ||
f63b8542 JM |
2640 | static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx) |
2641 | { | |
2642 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
2643 | if (!wpa_s->pending_pd_before_join) | |
2644 | return; | |
2645 | /* | |
2646 | * Provision Discovery Response may have been lost - try to connect | |
2647 | * anyway since we do not need any information from this PD. | |
2648 | */ | |
2649 | wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - " | |
2650 | "try to connect anyway"); | |
2651 | wpas_p2p_join_start(wpa_s); | |
2652 | } | |
2653 | ||
2654 | ||
4b156206 JM |
2655 | static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq) |
2656 | { | |
2657 | struct wpa_supplicant *iface; | |
2658 | int shared_freq; | |
2659 | u8 bssid[ETH_ALEN]; | |
2660 | ||
2661 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) | |
2662 | return 0; | |
2663 | ||
2664 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { | |
2665 | if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s) | |
2666 | continue; | |
2667 | if (iface->current_ssid == NULL || iface->assoc_freq == 0) | |
2668 | continue; | |
86ae2e8a JM |
2669 | if (iface->current_ssid->mode == WPAS_MODE_AP || |
2670 | iface->current_ssid->mode == WPAS_MODE_P2P_GO) | |
2671 | shared_freq = iface->current_ssid->frequency; | |
2672 | else if (wpa_drv_get_bssid(iface, bssid) == 0) | |
2673 | shared_freq = iface->assoc_freq; | |
2674 | else | |
2675 | shared_freq = 0; | |
2676 | ||
2677 | if (shared_freq && freq != shared_freq) { | |
2678 | wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s " | |
2679 | "connected on %d MHz - new connection on " | |
2680 | "%d MHz", iface->ifname, shared_freq, freq); | |
2681 | return 1; | |
4b156206 JM |
2682 | } |
2683 | } | |
2684 | ||
2685 | shared_freq = wpa_drv_shared_freq(wpa_s); | |
2686 | if (shared_freq > 0 && shared_freq != freq) { | |
2687 | wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared " | |
2688 | "virtual interface connected on %d MHz - new " | |
2689 | "connection on %d MHz", shared_freq, freq); | |
2690 | return 1; | |
2691 | } | |
2692 | ||
2693 | return 0; | |
2694 | } | |
2695 | ||
2696 | ||
b31be3a0 JM |
2697 | static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s, |
2698 | const u8 *peer_dev_addr) | |
2699 | { | |
2700 | struct wpa_bss *bss; | |
2701 | int updated; | |
2702 | ||
2703 | bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr); | |
2704 | if (bss == NULL) | |
2705 | return 0; | |
2706 | if (bss->last_update_idx < wpa_s->bss_update_idx) { | |
2707 | wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the " | |
2708 | "last scan"); | |
2709 | return 0; | |
2710 | } | |
2711 | ||
2712 | updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update); | |
2713 | wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at " | |
2714 | "%ld.%06ld (%supdated in last scan)", | |
2715 | bss->last_update.sec, bss->last_update.usec, | |
2716 | updated ? "": "not "); | |
2717 | ||
2718 | return updated; | |
2719 | } | |
2720 | ||
2721 | ||
ef922c4a JM |
2722 | static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s, |
2723 | struct wpa_scan_results *scan_res) | |
b22128ef JM |
2724 | { |
2725 | struct wpa_bss *bss; | |
f8d0131a | 2726 | int freq; |
54960629 | 2727 | u8 iface_addr[ETH_ALEN]; |
b22128ef | 2728 | |
ef922c4a | 2729 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
b22128ef | 2730 | |
ef922c4a JM |
2731 | if (wpa_s->global->p2p_disabled) |
2732 | return; | |
b22128ef | 2733 | |
b31be3a0 JM |
2734 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin", |
2735 | scan_res ? (int) scan_res->num : -1, | |
2736 | wpa_s->p2p_auto_join ? "auto_" : ""); | |
ef922c4a JM |
2737 | |
2738 | if (scan_res) | |
2739 | wpas_p2p_scan_res_handler(wpa_s, scan_res); | |
b22128ef | 2740 | |
0918c4bf JM |
2741 | if (wpa_s->p2p_auto_pd) { |
2742 | int join = wpas_p2p_peer_go(wpa_s, | |
2743 | wpa_s->pending_join_dev_addr); | |
2744 | wpa_s->p2p_auto_pd = 0; | |
2745 | wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG; | |
2746 | wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d", | |
2747 | MAC2STR(wpa_s->pending_join_dev_addr), join); | |
2748 | if (p2p_prov_disc_req(wpa_s->global->p2p, | |
2749 | wpa_s->pending_join_dev_addr, | |
2750 | wpa_s->pending_pd_config_methods, join, | |
2751 | 0) < 0) { | |
2752 | wpa_s->p2p_auto_pd = 0; | |
2753 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE | |
2754 | " p2p_dev_addr=" MACSTR " status=N/A", | |
2755 | MAC2STR(wpa_s->pending_join_dev_addr)); | |
2756 | } | |
2757 | return; | |
2758 | } | |
2759 | ||
b31be3a0 JM |
2760 | if (wpa_s->p2p_auto_join) { |
2761 | if (!wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr)) { | |
2762 | wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be " | |
2763 | "running a GO -> use GO Negotiation"); | |
2764 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, | |
2765 | wpa_s->p2p_pin, wpa_s->p2p_wps_method, | |
2766 | wpa_s->p2p_persistent_group, 0, 0, 0, | |
2767 | wpa_s->p2p_go_intent, | |
23c84252 JM |
2768 | wpa_s->p2p_connect_freq, |
2769 | wpa_s->p2p_persistent_id); | |
b31be3a0 JM |
2770 | return; |
2771 | } | |
2772 | ||
2773 | wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO -> try " | |
2774 | "to join the group"); | |
2775 | } | |
2776 | ||
f8d0131a JM |
2777 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
2778 | wpa_s->pending_join_iface_addr); | |
54960629 AL |
2779 | if (freq < 0 && |
2780 | p2p_get_interface_addr(wpa_s->global->p2p, | |
2781 | wpa_s->pending_join_dev_addr, | |
2782 | iface_addr) == 0 && | |
2783 | os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0) | |
2784 | { | |
2785 | wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface " | |
2786 | "address for join from " MACSTR " to " MACSTR | |
2787 | " based on newly discovered P2P peer entry", | |
2788 | MAC2STR(wpa_s->pending_join_iface_addr), | |
2789 | MAC2STR(iface_addr)); | |
2790 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, | |
2791 | ETH_ALEN); | |
2792 | ||
2793 | freq = p2p_get_oper_freq(wpa_s->global->p2p, | |
2794 | wpa_s->pending_join_iface_addr); | |
2795 | } | |
f8d0131a JM |
2796 | if (freq >= 0) { |
2797 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " | |
2798 | "from P2P peer table: %d MHz", freq); | |
2799 | } | |
ef922c4a | 2800 | bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr); |
b22128ef | 2801 | if (bss) { |
f8d0131a JM |
2802 | freq = bss->freq; |
2803 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " | |
2804 | "from BSS table: %d MHz", freq); | |
2805 | } | |
2806 | if (freq > 0) { | |
b22128ef JM |
2807 | u16 method; |
2808 | ||
4b156206 JM |
2809 | if (wpas_check_freq_conflict(wpa_s, freq) > 0) { |
2810 | wpa_msg(wpa_s->parent, MSG_INFO, | |
2811 | P2P_EVENT_GROUP_FORMATION_FAILURE | |
2812 | "reason=FREQ_CONFLICT"); | |
2813 | return; | |
2814 | } | |
2815 | ||
b22128ef JM |
2816 | wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request " |
2817 | "prior to joining an existing group (GO " MACSTR | |
2818 | " freq=%u MHz)", | |
f8d0131a | 2819 | MAC2STR(wpa_s->pending_join_dev_addr), freq); |
b22128ef JM |
2820 | wpa_s->pending_pd_before_join = 1; |
2821 | ||
ef922c4a | 2822 | switch (wpa_s->pending_join_wps_method) { |
b22128ef JM |
2823 | case WPS_PIN_DISPLAY: |
2824 | method = WPS_CONFIG_KEYPAD; | |
2825 | break; | |
2826 | case WPS_PIN_KEYPAD: | |
2827 | method = WPS_CONFIG_DISPLAY; | |
2828 | break; | |
2829 | case WPS_PBC: | |
2830 | method = WPS_CONFIG_PUSHBUTTON; | |
2831 | break; | |
2832 | default: | |
2833 | method = 0; | |
2834 | break; | |
2835 | } | |
2836 | ||
ec437d9e JJ |
2837 | if ((p2p_get_provisioning_info(wpa_s->global->p2p, |
2838 | wpa_s->pending_join_dev_addr) == | |
2839 | method)) { | |
2840 | /* | |
2841 | * We have already performed provision discovery for | |
2842 | * joining the group. Proceed directly to join | |
2843 | * operation without duplicated provision discovery. */ | |
e3a0706b | 2844 | wpa_printf(MSG_DEBUG, "P2P: Provision discovery " |
ec437d9e JJ |
2845 | "with " MACSTR " already done - proceed to " |
2846 | "join", | |
2847 | MAC2STR(wpa_s->pending_join_dev_addr)); | |
2848 | wpa_s->pending_pd_before_join = 0; | |
2849 | goto start; | |
2850 | } | |
2851 | ||
ef922c4a | 2852 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
1ef2f7ff JM |
2853 | wpa_s->pending_join_dev_addr, method, 1, |
2854 | freq) < 0) { | |
b22128ef JM |
2855 | wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision " |
2856 | "Discovery Request before joining an " | |
2857 | "existing group"); | |
2858 | wpa_s->pending_pd_before_join = 0; | |
2859 | goto start; | |
2860 | } | |
2861 | ||
2862 | /* | |
2863 | * Actual join operation will be started from the Action frame | |
f63b8542 JM |
2864 | * TX status callback (if no ACK is received) or when the |
2865 | * Provision Discovery Response is received. Use a short | |
2866 | * timeout as a backup mechanism should the Provision Discovery | |
2867 | * Response be lost for any reason. | |
b22128ef | 2868 | */ |
f63b8542 JM |
2869 | eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, |
2870 | NULL); | |
2871 | eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout, | |
2872 | wpa_s, NULL); | |
ef922c4a | 2873 | return; |
b22128ef JM |
2874 | } |
2875 | ||
9b1ab931 JM |
2876 | wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later"); |
2877 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | |
2878 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); | |
2879 | wpas_p2p_check_join_scan_limit(wpa_s); | |
2880 | return; | |
b22128ef JM |
2881 | |
2882 | start: | |
2883 | /* Start join operation immediately */ | |
ef922c4a JM |
2884 | wpas_p2p_join_start(wpa_s); |
2885 | } | |
2886 | ||
2887 | ||
2888 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) | |
2889 | { | |
2890 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
2891 | int ret; | |
2892 | struct wpa_driver_scan_params params; | |
2893 | struct wpabuf *wps_ie, *ies; | |
206e1f42 | 2894 | size_t ielen; |
ef922c4a JM |
2895 | |
2896 | os_memset(¶ms, 0, sizeof(params)); | |
2897 | ||
2898 | /* P2P Wildcard SSID */ | |
2899 | params.num_ssids = 1; | |
2900 | params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; | |
2901 | params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; | |
2902 | ||
2903 | wpa_s->wps->dev.p2p = 1; | |
360182ed JM |
2904 | wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev, |
2905 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0, | |
2906 | NULL); | |
ef922c4a JM |
2907 | if (wps_ie == NULL) { |
2908 | wpas_p2p_scan_res_join(wpa_s, NULL); | |
2909 | return; | |
2910 | } | |
2911 | ||
206e1f42 JM |
2912 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
2913 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); | |
ef922c4a JM |
2914 | if (ies == NULL) { |
2915 | wpabuf_free(wps_ie); | |
2916 | wpas_p2p_scan_res_join(wpa_s, NULL); | |
2917 | return; | |
2918 | } | |
2919 | wpabuf_put_buf(ies, wps_ie); | |
2920 | wpabuf_free(wps_ie); | |
2921 | ||
6d92fa6e | 2922 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
ef922c4a | 2923 | |
47185fc7 | 2924 | params.p2p_probe = 1; |
ef922c4a JM |
2925 | params.extra_ies = wpabuf_head(ies); |
2926 | params.extra_ies_len = wpabuf_len(ies); | |
2927 | ||
2928 | /* | |
2929 | * Run a scan to update BSS table and start Provision Discovery once | |
2930 | * the new scan results become available. | |
2931 | */ | |
17fbb751 | 2932 | ret = wpa_drv_scan(wpa_s, ¶ms); |
205e6474 JM |
2933 | if (!ret) |
2934 | wpa_s->scan_res_handler = wpas_p2p_scan_res_join; | |
ef922c4a JM |
2935 | |
2936 | wpabuf_free(ies); | |
2937 | ||
2938 | if (ret) { | |
2939 | wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - " | |
2940 | "try again later"); | |
2941 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | |
2942 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); | |
9b1ab931 | 2943 | wpas_p2p_check_join_scan_limit(wpa_s); |
ef922c4a JM |
2944 | } |
2945 | } | |
2946 | ||
2947 | ||
2948 | static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, | |
b31be3a0 JM |
2949 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
2950 | int auto_join) | |
ef922c4a JM |
2951 | { |
2952 | wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface " | |
b31be3a0 JM |
2953 | MACSTR " dev " MACSTR ")%s", |
2954 | MAC2STR(iface_addr), MAC2STR(dev_addr), | |
2955 | auto_join ? " (auto_join)" : ""); | |
ef922c4a | 2956 | |
0918c4bf | 2957 | wpa_s->p2p_auto_pd = 0; |
b31be3a0 | 2958 | wpa_s->p2p_auto_join = !!auto_join; |
ef922c4a JM |
2959 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN); |
2960 | os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN); | |
2961 | wpa_s->pending_join_wps_method = wps_method; | |
2962 | ||
2963 | /* Make sure we are not running find during connection establishment */ | |
2964 | wpas_p2p_stop_find(wpa_s); | |
2965 | ||
9b1ab931 | 2966 | wpa_s->p2p_join_scan_count = 0; |
ef922c4a JM |
2967 | wpas_p2p_join_scan(wpa_s, NULL); |
2968 | return 0; | |
b22128ef JM |
2969 | } |
2970 | ||
2971 | ||
2972 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s) | |
2973 | { | |
2974 | struct wpa_supplicant *group; | |
2975 | struct p2p_go_neg_results res; | |
8e64f258 | 2976 | struct wpa_bss *bss; |
b22128ef | 2977 | |
f63b8542 | 2978 | eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL); |
b22128ef JM |
2979 | group = wpas_p2p_get_group_iface(wpa_s, 0, 0); |
2980 | if (group == NULL) | |
2981 | return -1; | |
3c5126a4 | 2982 | if (group != wpa_s) { |
b22128ef JM |
2983 | os_memcpy(group->p2p_pin, wpa_s->p2p_pin, |
2984 | sizeof(group->p2p_pin)); | |
3c5126a4 JM |
2985 | group->p2p_wps_method = wpa_s->p2p_wps_method; |
2986 | } | |
b22128ef JM |
2987 | |
2988 | group->p2p_in_provisioning = 1; | |
2989 | ||
2990 | os_memset(&res, 0, sizeof(res)); | |
2991 | os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr, | |
2992 | ETH_ALEN); | |
2993 | res.wps_method = wpa_s->pending_join_wps_method; | |
8e64f258 JM |
2994 | bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr); |
2995 | if (bss) { | |
2996 | res.freq = bss->freq; | |
2997 | res.ssid_len = bss->ssid_len; | |
2998 | os_memcpy(res.ssid, bss->ssid, bss->ssid_len); | |
2999 | } | |
3000 | ||
e91829f9 JM |
3001 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
3002 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to " | |
3003 | "starting client"); | |
3004 | wpa_drv_cancel_remain_on_channel(wpa_s); | |
3005 | wpa_s->off_channel_freq = 0; | |
3006 | wpa_s->roc_waiting_drv_freq = 0; | |
3007 | } | |
b22128ef JM |
3008 | wpas_start_wps_enrollee(group, &res); |
3009 | ||
3094d483 JM |
3010 | /* |
3011 | * Allow a longer timeout for join-a-running-group than normal 15 | |
3012 | * second group formation timeout since the GO may not have authorized | |
3013 | * our connection yet. | |
3014 | */ | |
3015 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); | |
3016 | eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout, | |
3017 | wpa_s, NULL); | |
3018 | ||
b22128ef JM |
3019 | return 0; |
3020 | } | |
3021 | ||
3022 | ||
3023 | /** | |
3024 | * wpas_p2p_connect - Request P2P Group Formation to be started | |
3025 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
3026 | * @peer_addr: Address of the peer P2P Device | |
3027 | * @pin: PIN to use during provisioning or %NULL to indicate PBC mode | |
3028 | * @persistent_group: Whether to create a persistent group | |
b31be3a0 | 3029 | * @auto_join: Whether to select join vs. GO Negotiation automatically |
b22128ef JM |
3030 | * @join: Whether to join an existing group (as a client) instead of starting |
3031 | * Group Owner negotiation; @peer_addr is BSSID in that case | |
3032 | * @auth: Whether to only authorize the connection instead of doing that and | |
3033 | * initiating Group Owner negotiation | |
3034 | * @go_intent: GO Intent or -1 to use default | |
3035 | * @freq: Frequency for the group or 0 for auto-selection | |
23c84252 JM |
3036 | * @persistent_id: Persistent group credentials to use for forcing GO |
3037 | * parameters or -1 to generate new values (SSID/passphrase) | |
d054a462 JM |
3038 | * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified |
3039 | * failure, -2 on failure due to channel not currently available, | |
3040 | * -3 if forced channel is not supported | |
b22128ef JM |
3041 | */ |
3042 | int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr, | |
3043 | const char *pin, enum p2p_wps_method wps_method, | |
b31be3a0 | 3044 | int persistent_group, int auto_join, int join, int auth, |
23c84252 | 3045 | int go_intent, int freq, int persistent_id) |
b22128ef | 3046 | { |
d054a462 | 3047 | int force_freq = 0, oper_freq = 0; |
b22128ef JM |
3048 | u8 bssid[ETH_ALEN]; |
3049 | int ret = 0; | |
3050 | enum wpa_driver_if_type iftype; | |
6cad95db | 3051 | const u8 *if_addr; |
23c84252 | 3052 | struct wpa_ssid *ssid = NULL; |
b22128ef | 3053 | |
9526fd29 JM |
3054 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
3055 | return -1; | |
3056 | ||
23c84252 JM |
3057 | if (persistent_id >= 0) { |
3058 | ssid = wpa_config_get_network(wpa_s->conf, persistent_id); | |
3059 | if (ssid == NULL || ssid->disabled != 2 || | |
3060 | ssid->mode != WPAS_MODE_P2P_GO) | |
3061 | return -1; | |
3062 | } | |
3063 | ||
b22128ef JM |
3064 | if (go_intent < 0) |
3065 | go_intent = wpa_s->conf->p2p_go_intent; | |
3066 | ||
3067 | if (!auth) | |
3068 | wpa_s->p2p_long_listen = 0; | |
3069 | ||
3c5126a4 | 3070 | wpa_s->p2p_wps_method = wps_method; |
b31be3a0 | 3071 | wpa_s->p2p_persistent_group = !!persistent_group; |
23c84252 | 3072 | wpa_s->p2p_persistent_id = persistent_id; |
b31be3a0 JM |
3073 | wpa_s->p2p_go_intent = go_intent; |
3074 | wpa_s->p2p_connect_freq = freq; | |
3c5126a4 | 3075 | |
b22128ef JM |
3076 | if (pin) |
3077 | os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin)); | |
3078 | else if (wps_method == WPS_PIN_DISPLAY) { | |
3079 | ret = wps_generate_pin(); | |
3080 | os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d", | |
3081 | ret); | |
3082 | wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s", | |
3083 | wpa_s->p2p_pin); | |
3084 | } else | |
3085 | wpa_s->p2p_pin[0] = '\0'; | |
3086 | ||
b31be3a0 | 3087 | if (join || auto_join) { |
4147a2cc | 3088 | u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN]; |
108def93 JM |
3089 | if (auth) { |
3090 | wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to " | |
3091 | "connect a running group from " MACSTR, | |
3092 | MAC2STR(peer_addr)); | |
3093 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); | |
3094 | return ret; | |
3095 | } | |
4147a2cc | 3096 | os_memcpy(dev_addr, peer_addr, ETH_ALEN); |
b22128ef | 3097 | if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr, |
4147a2cc | 3098 | iface_addr) < 0) { |
b22128ef | 3099 | os_memcpy(iface_addr, peer_addr, ETH_ALEN); |
4147a2cc JM |
3100 | p2p_get_dev_addr(wpa_s->global->p2p, peer_addr, |
3101 | dev_addr); | |
3102 | } | |
b31be3a0 JM |
3103 | if (auto_join) { |
3104 | os_get_time(&wpa_s->p2p_auto_started); | |
3105 | wpa_printf(MSG_DEBUG, "P2P: Auto join started at " | |
3106 | "%ld.%06ld", | |
3107 | wpa_s->p2p_auto_started.sec, | |
3108 | wpa_s->p2p_auto_started.usec); | |
3109 | } | |
3110 | if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method, | |
3111 | auto_join) < 0) | |
b22128ef JM |
3112 | return -1; |
3113 | return ret; | |
3114 | } | |
3115 | ||
d054a462 JM |
3116 | if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 && |
3117 | wpa_s->assoc_freq) | |
3118 | oper_freq = wpa_s->assoc_freq; | |
b22128ef | 3119 | else { |
d054a462 JM |
3120 | oper_freq = wpa_drv_shared_freq(wpa_s); |
3121 | if (oper_freq < 0) | |
3122 | oper_freq = 0; | |
b22128ef JM |
3123 | } |
3124 | ||
d054a462 JM |
3125 | if (freq > 0) { |
3126 | if (!p2p_supported_freq(wpa_s->global->p2p, freq)) { | |
3127 | wpa_printf(MSG_DEBUG, "P2P: The forced channel " | |
3128 | "(%u MHz) is not supported for P2P uses", | |
3129 | freq); | |
3130 | return -3; | |
3131 | } | |
3132 | ||
3133 | if (oper_freq > 0 && freq != oper_freq && | |
3134 | !(wpa_s->drv_flags & | |
3135 | WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) { | |
3136 | wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group " | |
3137 | "on %u MHz while connected on another " | |
3138 | "channel (%u MHz)", freq, oper_freq); | |
3139 | return -2; | |
3140 | } | |
3141 | wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " | |
3142 | "requested channel (%u MHz)", freq); | |
3143 | force_freq = freq; | |
3144 | } else if (oper_freq > 0 && | |
3145 | !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) { | |
3146 | if (!(wpa_s->drv_flags & | |
3147 | WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) { | |
3148 | wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group " | |
3149 | "while connected on non-P2P supported " | |
3150 | "channel (%u MHz)", oper_freq); | |
3151 | return -2; | |
3152 | } | |
3153 | wpa_printf(MSG_DEBUG, "P2P: Current operating channel " | |
3154 | "(%u MHz) not available for P2P - try to use " | |
3155 | "another channel", oper_freq); | |
3156 | force_freq = 0; | |
3157 | } else if (oper_freq > 0) { | |
b22128ef JM |
3158 | wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " |
3159 | "channel we are already using (%u MHz) on another " | |
d054a462 JM |
3160 | "interface", oper_freq); |
3161 | force_freq = oper_freq; | |
b22128ef JM |
3162 | } |
3163 | ||
3164 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); | |
3165 | ||
6cad95db JB |
3166 | if (wpa_s->create_p2p_iface) { |
3167 | /* Prepare to add a new interface for the group */ | |
3168 | iftype = WPA_IF_P2P_GROUP; | |
3169 | if (go_intent == 15) | |
3170 | iftype = WPA_IF_P2P_GO; | |
3171 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { | |
3172 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " | |
3173 | "interface for the group"); | |
b22128ef | 3174 | return -1; |
6cad95db | 3175 | } |
b22128ef | 3176 | |
6cad95db JB |
3177 | if_addr = wpa_s->pending_interface_addr; |
3178 | } else | |
3179 | if_addr = wpa_s->own_addr; | |
b22128ef JM |
3180 | |
3181 | if (auth) { | |
3182 | if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method, | |
6cad95db | 3183 | go_intent, if_addr, |
23c84252 JM |
3184 | force_freq, persistent_group, ssid) < |
3185 | 0) | |
b22128ef JM |
3186 | return -1; |
3187 | return ret; | |
3188 | } | |
6cad95db JB |
3189 | |
3190 | if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, | |
3191 | go_intent, if_addr, force_freq, | |
23c84252 | 3192 | persistent_group, ssid) < 0) { |
6cad95db JB |
3193 | if (wpa_s->create_p2p_iface) |
3194 | wpas_p2p_remove_pending_group_interface(wpa_s); | |
b22128ef JM |
3195 | return -1; |
3196 | } | |
3197 | return ret; | |
3198 | } | |
3199 | ||
3200 | ||
3201 | /** | |
3202 | * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start | |
3203 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
3204 | * @freq: Frequency of the channel in MHz | |
3205 | * @duration: Duration of the stay on the channel in milliseconds | |
3206 | * | |
3207 | * This callback is called when the driver indicates that it has started the | |
3208 | * requested remain-on-channel duration. | |
3209 | */ | |
3210 | void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s, | |
3211 | unsigned int freq, unsigned int duration) | |
3212 | { | |
9526fd29 JM |
3213 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
3214 | return; | |
b22128ef JM |
3215 | if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) { |
3216 | p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq, | |
3217 | wpa_s->pending_listen_duration); | |
3218 | wpa_s->pending_listen_freq = 0; | |
3219 | } | |
3220 | } | |
3221 | ||
3222 | ||
3223 | static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, | |
3224 | unsigned int timeout) | |
3225 | { | |
3226 | /* Limit maximum Listen state time based on driver limitation. */ | |
3227 | if (timeout > wpa_s->max_remain_on_chan) | |
3228 | timeout = wpa_s->max_remain_on_chan; | |
3229 | ||
3ac17eba JM |
3230 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
3231 | return wpa_drv_p2p_listen(wpa_s, timeout); | |
3232 | ||
b22128ef JM |
3233 | return p2p_listen(wpa_s->global->p2p, timeout); |
3234 | } | |
3235 | ||
3236 | ||
3237 | /** | |
3238 | * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout | |
3239 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
3240 | * @freq: Frequency of the channel in MHz | |
3241 | * | |
3242 | * This callback is called when the driver indicates that a remain-on-channel | |
3243 | * operation has been completed, i.e., the duration on the requested channel | |
3244 | * has timed out. | |
3245 | */ | |
3246 | void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s, | |
3247 | unsigned int freq) | |
3248 | { | |
1cc3a29d | 3249 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback " |
11fb02be | 3250 | "(p2p_long_listen=%d ms pending_action_tx=%p)", |
1cc3a29d | 3251 | wpa_s->p2p_long_listen, wpa_s->pending_action_tx); |
9526fd29 JM |
3252 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
3253 | return; | |
b22128ef JM |
3254 | if (p2p_listen_end(wpa_s->global->p2p, freq) > 0) |
3255 | return; /* P2P module started a new operation */ | |
3256 | if (wpa_s->pending_action_tx) | |
3257 | return; | |
3258 | if (wpa_s->p2p_long_listen > 0) | |
11fb02be | 3259 | wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan; |
b22128ef JM |
3260 | if (wpa_s->p2p_long_listen > 0) { |
3261 | wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state"); | |
11fb02be | 3262 | wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen); |
b22128ef JM |
3263 | } |
3264 | } | |
3265 | ||
3266 | ||
3267 | /** | |
3268 | * wpas_p2p_group_remove - Remove a P2P group | |
3269 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
3270 | * @ifname: Network interface name of the group interface or "*" to remove all | |
3271 | * groups | |
3272 | * Returns: 0 on success, -1 on failure | |
3273 | * | |
3274 | * This function is used to remove a P2P group. This can be used to disconnect | |
3275 | * from a group in which the local end is a P2P Client or to end a P2P Group in | |
3276 | * case the local end is the Group Owner. If a virtual network interface was | |
3277 | * created for this group, that interface will be removed. Otherwise, only the | |
3278 | * configured P2P group network will be removed from the interface. | |
3279 | */ | |
3280 | int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname) | |
3281 | { | |
3282 | struct wpa_global *global = wpa_s->global; | |
3283 | ||
3284 | if (os_strcmp(ifname, "*") == 0) { | |
3285 | struct wpa_supplicant *prev; | |
3286 | wpa_s = global->ifaces; | |
3287 | while (wpa_s) { | |
3288 | prev = wpa_s; | |
3289 | wpa_s = wpa_s->next; | |
3103f345 | 3290 | wpas_p2p_disconnect(prev); |
b22128ef JM |
3291 | } |
3292 | return 0; | |
3293 | } | |
3294 | ||
3295 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
3296 | if (os_strcmp(wpa_s->ifname, ifname) == 0) | |
3297 | break; | |
3298 | } | |
3299 | ||
3103f345 | 3300 | return wpas_p2p_disconnect(wpa_s); |
b22128ef JM |
3301 | } |
3302 | ||
3303 | ||
b8349523 NKG |
3304 | static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s, |
3305 | struct p2p_go_neg_results *params, | |
3306 | int freq) | |
b22128ef JM |
3307 | { |
3308 | u8 bssid[ETH_ALEN]; | |
3309 | int res; | |
3310 | ||
3311 | os_memset(params, 0, sizeof(*params)); | |
3312 | params->role_go = 1; | |
7cfc4ac3 AGS |
3313 | if (freq) { |
3314 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced " | |
3315 | "frequency %d MHz", freq); | |
b22128ef | 3316 | params->freq = freq; |
7cfc4ac3 AGS |
3317 | } else if (wpa_s->conf->p2p_oper_reg_class == 81 && |
3318 | wpa_s->conf->p2p_oper_channel >= 1 && | |
3319 | wpa_s->conf->p2p_oper_channel <= 11) { | |
b22128ef | 3320 | params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel; |
7cfc4ac3 AGS |
3321 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
3322 | "frequency %d MHz", params->freq); | |
3323 | } else if (wpa_s->conf->p2p_oper_reg_class == 115 || | |
64f99265 | 3324 | wpa_s->conf->p2p_oper_reg_class == 124) { |
b22128ef | 3325 | params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel; |
7cfc4ac3 AGS |
3326 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
3327 | "frequency %d MHz", params->freq); | |
3328 | } else if (wpa_s->conf->p2p_oper_channel == 0 && | |
3329 | wpa_s->best_overall_freq > 0 && | |
3330 | p2p_supported_freq(wpa_s->global->p2p, | |
3331 | wpa_s->best_overall_freq)) { | |
3332 | params->freq = wpa_s->best_overall_freq; | |
3333 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall " | |
3334 | "channel %d MHz", params->freq); | |
3335 | } else if (wpa_s->conf->p2p_oper_channel == 0 && | |
3336 | wpa_s->best_24_freq > 0 && | |
3337 | p2p_supported_freq(wpa_s->global->p2p, | |
3338 | wpa_s->best_24_freq)) { | |
3339 | params->freq = wpa_s->best_24_freq; | |
3340 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz " | |
3341 | "channel %d MHz", params->freq); | |
3342 | } else if (wpa_s->conf->p2p_oper_channel == 0 && | |
3343 | wpa_s->best_5_freq > 0 && | |
3344 | p2p_supported_freq(wpa_s->global->p2p, | |
3345 | wpa_s->best_5_freq)) { | |
3346 | params->freq = wpa_s->best_5_freq; | |
3347 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz " | |
3348 | "channel %d MHz", params->freq); | |
3349 | } else { | |
6f3bc72b JM |
3350 | int chan; |
3351 | for (chan = 0; chan < 11; chan++) { | |
3352 | params->freq = 2412 + chan * 5; | |
3353 | if (!wpas_p2p_disallowed_freq(wpa_s->global, | |
3354 | params->freq)) | |
3355 | break; | |
3356 | } | |
3357 | if (chan == 11) { | |
3358 | wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel " | |
3359 | "allowed"); | |
3360 | return -1; | |
3361 | } | |
7cfc4ac3 AGS |
3362 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference " |
3363 | "known)", params->freq); | |
3364 | } | |
3365 | ||
b22128ef JM |
3366 | if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 && |
3367 | wpa_s->assoc_freq && !freq) { | |
3368 | wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are " | |
3369 | "already using"); | |
3370 | params->freq = wpa_s->assoc_freq; | |
3371 | } | |
3372 | ||
3373 | res = wpa_drv_shared_freq(wpa_s); | |
3374 | if (res > 0 && !freq) { | |
3375 | wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are " | |
3376 | "already using on a shared interface"); | |
3377 | params->freq = res; | |
b8349523 NKG |
3378 | } else if (res > 0 && freq != res && |
3379 | !(wpa_s->drv_flags & | |
3380 | WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) { | |
3381 | wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz " | |
3382 | "while connected on another channel (%u MHz)", | |
3383 | freq, res); | |
3384 | return -1; | |
b22128ef | 3385 | } |
b8349523 NKG |
3386 | |
3387 | return 0; | |
b22128ef JM |
3388 | } |
3389 | ||
3390 | ||
3391 | static struct wpa_supplicant * | |
3392 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, | |
3393 | int go) | |
3394 | { | |
3395 | struct wpa_supplicant *group_wpa_s; | |
3396 | ||
3397 | if (!wpas_p2p_create_iface(wpa_s)) | |
3398 | return wpa_s; | |
3399 | ||
3400 | if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : | |
3401 | WPA_IF_P2P_CLIENT) < 0) | |
3402 | return NULL; | |
3403 | group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go); | |
3404 | if (group_wpa_s == NULL) { | |
3405 | wpas_p2p_remove_pending_group_interface(wpa_s); | |
3406 | return NULL; | |
3407 | } | |
3408 | ||
3409 | return group_wpa_s; | |
3410 | } | |
3411 | ||
3412 | ||
3413 | /** | |
3414 | * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner | |
3415 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | |
3416 | * @persistent_group: Whether to create a persistent group | |
3417 | * @freq: Frequency for the group or 0 to indicate no hardcoding | |
3418 | * Returns: 0 on success, -1 on failure | |
3419 | * | |
3420 | * This function creates a new P2P group with the local end as the Group Owner, | |
3421 | * i.e., without using Group Owner Negotiation. | |
3422 | */ | |
3423 | int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group, | |
3424 | int freq) | |
3425 | { | |
3426 | struct p2p_go_neg_results params; | |
7cfc4ac3 AGS |
3427 | unsigned int r; |
3428 | ||
9526fd29 JM |
3429 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
3430 | return -1; | |
3431 | ||
2d4f15d6 JJ |
3432 | /* Make sure we are not running find during connection establishment */ |
3433 | wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND"); | |
3434 | wpas_p2p_stop_find(wpa_s); | |
3435 | ||
7cfc4ac3 AGS |
3436 | if (freq == 2) { |
3437 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz " | |
3438 | "band"); | |
3439 | if (wpa_s->best_24_freq > 0 && | |
3440 | p2p_supported_freq(wpa_s->global->p2p, | |
3441 | wpa_s->best_24_freq)) { | |
3442 | freq = wpa_s->best_24_freq; | |
3443 | wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band " | |
3444 | "channel: %d MHz", freq); | |
3445 | } else { | |
3446 | os_get_random((u8 *) &r, sizeof(r)); | |
3447 | freq = 2412 + (r % 3) * 25; | |
3448 | wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band " | |
3449 | "channel: %d MHz", freq); | |
3450 | } | |
3451 | } | |
3452 | ||
3453 | if (freq == 5) { | |
3454 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz " | |
3455 | "band"); | |
3456 | if (wpa_s->best_5_freq > 0 && | |
3457 | p2p_supported_freq(wpa_s->global->p2p, | |
3458 | wpa_s->best_5_freq)) { | |
3459 | freq = wpa_s->best_5_freq; | |
3460 | wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band " | |
3461 | "channel: %d MHz", freq); | |
3462 | } else { | |
3463 | os_get_random((u8 *) &r, sizeof(r)); | |
3464 | freq = 5180 + (r % 4) * 20; | |
3465 | if (!p2p_supported_freq(wpa_s->global->p2p, freq)) { | |
3466 | wpa_printf(MSG_DEBUG, "P2P: Could not select " | |
3467 | "5 GHz channel for P2P group"); | |
3468 | return -1; | |
3469 | } | |
3470 | wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band " | |
3471 | "channel: %d MHz", freq); | |
3472 | } | |
3473 | } | |
b22128ef | 3474 | |
4ae4650b JM |
3475 | if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) { |
3476 | wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO " | |
3477 | "(%u MHz) is not supported for P2P uses", | |
3478 | freq); | |
3479 | return -1; | |
3480 | } | |
3481 | ||
b8349523 NKG |
3482 | if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq)) |
3483 | return -1; | |
f4329aa2 JM |
3484 | if (params.freq && |
3485 | !p2p_supported_freq(wpa_s->global->p2p, params.freq)) { | |
3486 | wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO " | |
3487 | "(%u MHz) is not supported for P2P uses", | |
3488 | params.freq); | |
3489 | return -1; | |
3490 | } | |
b22128ef JM |
3491 | p2p_go_params(wpa_s->global->p2p, ¶ms); |
3492 | params.persistent_group = persistent_group; | |
3493 | ||
3494 | wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1); | |
3495 | if (wpa_s == NULL) | |
3496 | return -1; | |
3497 | wpas_start_wps_go(wpa_s, ¶ms, 0); | |
3498 | ||
3499 | return 0; | |
3500 | } | |
3501 | ||
3502 | ||
3503 | static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s, | |
3504 | struct wpa_ssid *params, int addr_allocated) | |
3505 | { | |
3506 | struct wpa_ssid *ssid; | |
3507 | ||
3508 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0); | |
3509 | if (wpa_s == NULL) | |
3510 | return -1; | |
3511 | ||
3512 | wpa_supplicant_ap_deinit(wpa_s); | |
3513 | ||
3514 | ssid = wpa_config_add_network(wpa_s->conf); | |
3515 | if (ssid == NULL) | |
3516 | return -1; | |
b22128ef JM |
3517 | wpa_config_set_network_defaults(ssid); |
3518 | ssid->temporary = 1; | |
3519 | ssid->proto = WPA_PROTO_RSN; | |
3520 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; | |
3521 | ssid->group_cipher = WPA_CIPHER_CCMP; | |
3522 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; | |
3523 | ssid->ssid = os_malloc(params->ssid_len); | |
3524 | if (ssid->ssid == NULL) { | |
b22128ef JM |
3525 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
3526 | return -1; | |
3527 | } | |
3528 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); | |
3529 | ssid->ssid_len = params->ssid_len; | |
3530 | ssid->p2p_group = 1; | |
d1c8ac88 | 3531 | ssid->export_keys = 1; |
b22128ef JM |
3532 | if (params->psk_set) { |
3533 | os_memcpy(ssid->psk, params->psk, 32); | |
3534 | ssid->psk_set = 1; | |
3535 | } | |
3536 | if (params->passphrase) | |
3537 | ssid->passphrase = os_strdup(params->passphrase); | |
3538 | ||
3539 | wpa_supplicant_select_network(wpa_s, ssid); | |
3540 | ||
3541 | wpa_s->show_group_started = 1; | |
3542 | ||
3543 | return 0; | |
3544 | } | |
3545 | ||
3546 | ||
3547 | int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s, | |
3548 | struct wpa_ssid *ssid, int addr_allocated, | |
3549 | int freq) | |
3550 | { | |
3551 | struct p2p_go_neg_results params; | |
6c0da49f | 3552 | int go = 0; |
b22128ef JM |
3553 | |
3554 | if (ssid->disabled != 2 || ssid->ssid == NULL) | |
3555 | return -1; | |
3556 | ||
6c0da49f JM |
3557 | if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) && |
3558 | go == (ssid->mode == WPAS_MODE_P2P_GO)) { | |
3559 | wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is " | |
3560 | "already running"); | |
3561 | return 0; | |
3562 | } | |
3563 | ||
9d39057c JM |
3564 | /* Make sure we are not running find during connection establishment */ |
3565 | wpas_p2p_stop_find(wpa_s); | |
b22128ef JM |
3566 | |
3567 | if (ssid->mode == WPAS_MODE_INFRA) | |
3568 | return wpas_start_p2p_client(wpa_s, ssid, addr_allocated); | |
3569 | ||
3570 | if (ssid->mode != WPAS_MODE_P2P_GO) | |
3571 | return -1; | |
3572 | ||
b8349523 NKG |
3573 | if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq)) |
3574 | return -1; | |
b22128ef JM |
3575 | |
3576 | params.role_go = 1; | |
3577 | if (ssid->passphrase == NULL || | |
3578 | os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) { | |
3579 | wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent " | |
3580 | "group"); | |
3581 | return -1; | |
3582 | } | |
3583 | os_strlcpy(params.passphrase, ssid->passphrase, | |
3584 | sizeof(params.passphrase)); | |
3585 | os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len); | |
3586 | params.ssid_len = ssid->ssid_len; | |
3587 | params.persistent_group = 1; | |
3588 | ||
3589 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1); | |
3590 | if (wpa_s == NULL) | |
3591 | return -1; | |
3592 | ||
3593 | wpas_start_wps_go(wpa_s, ¶ms, 0); | |
3594 | ||
3595 | return 0; | |
3596 | } | |
3597 | ||
3598 | ||
3599 | static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies, | |
3600 | struct wpabuf *proberesp_ies) | |
3601 | { | |
3602 | struct wpa_supplicant *wpa_s = ctx; | |
3603 | if (wpa_s->ap_iface) { | |
3604 | struct hostapd_data *hapd = wpa_s->ap_iface->bss[0]; | |
adc33680 JM |
3605 | if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) { |
3606 | wpabuf_free(beacon_ies); | |
3607 | wpabuf_free(proberesp_ies); | |
3608 | return; | |
3609 | } | |
b22128ef JM |
3610 | if (beacon_ies) { |
3611 | wpabuf_free(hapd->p2p_beacon_ie); | |
3612 | hapd->p2p_beacon_ie = beacon_ies; | |
3613 | } | |
3614 | wpabuf_free(hapd->p2p_probe_resp_ie); | |
3615 | hapd->p2p_probe_resp_ie = proberesp_ies; | |
3616 | } else { | |
3617 | wpabuf_free(beacon_ies); | |
3618 | wpabuf_free(proberesp_ies); | |
3619 | } | |
3620 | wpa_supplicant_ap_update_beacon(wpa_s); | |
3621 | } | |
3622 | ||
3623 | ||
3071e181 JM |
3624 | static void wpas_p2p_idle_update(void *ctx, int idle) |
3625 | { | |
3626 | struct wpa_supplicant *wpa_s = ctx; | |
3627 | if (!wpa_s->ap_iface) | |
3628 | return; | |
3629 | wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not "); | |
3630 | if (idle) | |
3631 | wpas_p2p_set_group_idle_timeout(wpa_s); | |
3632 | else | |
3633 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); | |
3634 | } | |
3635 | ||
3636 | ||
b22128ef JM |
3637 | struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s, |
3638 | int persistent_group, | |
3639 | int group_formation) | |
3640 | { | |
3641 | struct p2p_group *group; | |
3642 | struct p2p_group_config *cfg; | |
3643 | ||
3ac17eba JM |
3644 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
3645 | return NULL; | |
9526fd29 JM |
3646 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
3647 | return NULL; | |
3ac17eba | 3648 | |
b22128ef JM |
3649 | cfg = os_zalloc(sizeof(*cfg)); |
3650 | if (cfg == NULL) | |
3651 | return NULL; | |
3652 | ||
acc247b2 JM |
3653 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
3654 | cfg->persistent_group = 2; | |
3655 | else if (persistent_group) | |
3656 | cfg->persistent_group = 1; | |
b22128ef | 3657 | os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN); |
3f4ce13f JM |
3658 | if (wpa_s->max_stations && |
3659 | wpa_s->max_stations < wpa_s->conf->max_num_sta) | |
3660 | cfg->max_clients = wpa_s->max_stations; | |
3661 | else | |
3662 | cfg->max_clients = wpa_s->conf->max_num_sta; | |
b22128ef JM |
3663 | cfg->cb_ctx = wpa_s; |
3664 | cfg->ie_update = wpas_p2p_ie_update; | |
3071e181 | 3665 | cfg->idle_update = wpas_p2p_idle_update; |
b22128ef JM |
3666 | |
3667 | group = p2p_group_init(wpa_s->global->p2p, cfg); | |
3668 | if (group == NULL) | |
3669 | os_free(cfg); | |
3670 | if (!group_formation) | |
3671 | p2p_group_notif_formation_done(group); | |
3672 | wpa_s->p2p_group = group; | |
3673 | return group; | |
3674 | } | |
3675 | ||
3676 | ||
3677 | void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr, | |
3678 | int registrar) | |
3679 | { | |
10531d21 JM |
3680 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
3681 | ||
b22128ef JM |
3682 | if (!wpa_s->p2p_in_provisioning) { |
3683 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P " | |
3684 | "provisioning not in progress"); | |
3685 | return; | |
3686 | } | |
3687 | ||
10531d21 JM |
3688 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
3689 | u8 go_dev_addr[ETH_ALEN]; | |
3690 | os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN); | |
3691 | wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, | |
3692 | ssid->ssid_len); | |
3693 | /* Clear any stored provisioning info */ | |
3694 | p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr); | |
3695 | } | |
ec437d9e | 3696 | |
b22128ef JM |
3697 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, |
3698 | NULL); | |
361cdf34 JM |
3699 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
3700 | /* | |
3701 | * Use a separate timeout for initial data connection to | |
3702 | * complete to allow the group to be removed automatically if | |
3703 | * something goes wrong in this step before the P2P group idle | |
3704 | * timeout mechanism is taken into use. | |
3705 | */ | |
3706 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, | |
3707 | wpas_p2p_group_formation_timeout, | |
3708 | wpa_s, NULL); | |
3709 | } | |
b22128ef JM |
3710 | if (wpa_s->global->p2p) |
3711 | p2p_wps_success_cb(wpa_s->global->p2p, peer_addr); | |
3ac17eba JM |
3712 | else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
3713 | wpa_drv_wps_success_cb(wpa_s, peer_addr); | |
b22128ef JM |
3714 | wpas_group_formation_completed(wpa_s, 1); |
3715 | } | |
3716 | ||
3717 | ||
3734552f JS |
3718 | void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s, |
3719 | struct wps_event_fail *fail) | |
3720 | { | |
3721 | if (!wpa_s->p2p_in_provisioning) { | |
3722 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P " | |
3723 | "provisioning not in progress"); | |
3724 | return; | |
3725 | } | |
ec437d9e JJ |
3726 | |
3727 | if (wpa_s->go_params) { | |
3728 | p2p_clear_provisioning_info( | |
3729 | wpa_s->global->p2p, | |
10531d21 | 3730 | wpa_s->go_params->peer_device_addr); |
ec437d9e JJ |
3731 | } |
3732 | ||
3734552f JS |
3733 | wpas_notify_p2p_wps_failed(wpa_s, fail); |
3734 | } | |
3735 | ||
3736 | ||
b22128ef | 3737 | int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
0918c4bf JM |
3738 | const char *config_method, |
3739 | enum wpas_p2p_prov_disc_use use) | |
b22128ef JM |
3740 | { |
3741 | u16 config_methods; | |
3742 | ||
0918c4bf | 3743 | wpa_s->pending_pd_use = NORMAL_PD; |
8c5f7309 | 3744 | if (os_strncmp(config_method, "display", 7) == 0) |
b22128ef | 3745 | config_methods = WPS_CONFIG_DISPLAY; |
8c5f7309 | 3746 | else if (os_strncmp(config_method, "keypad", 6) == 0) |
b22128ef | 3747 | config_methods = WPS_CONFIG_KEYPAD; |
8c5f7309 JJ |
3748 | else if (os_strncmp(config_method, "pbc", 3) == 0 || |
3749 | os_strncmp(config_method, "pushbutton", 10) == 0) | |
b22128ef | 3750 | config_methods = WPS_CONFIG_PUSHBUTTON; |
8c5f7309 JJ |
3751 | else { |
3752 | wpa_printf(MSG_DEBUG, "P2P: Unknown config method"); | |
b22128ef | 3753 | return -1; |
8c5f7309 | 3754 | } |
b22128ef | 3755 | |
0918c4bf JM |
3756 | if (use == WPAS_P2P_PD_AUTO) { |
3757 | os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN); | |
3758 | wpa_s->pending_pd_config_methods = config_methods; | |
3759 | wpa_s->p2p_auto_pd = 1; | |
3760 | wpa_s->p2p_auto_join = 0; | |
3761 | wpa_s->pending_pd_before_join = 0; | |
3762 | wpas_p2p_stop_find(wpa_s); | |
3763 | wpa_s->p2p_join_scan_count = 0; | |
3764 | wpas_p2p_join_scan(wpa_s, NULL); | |
3765 | return 0; | |
3766 | } | |
3767 | ||
3ac17eba JM |
3768 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
3769 | return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr, | |
0918c4bf JM |
3770 | config_methods, |
3771 | use == WPAS_P2P_PD_FOR_JOIN); | |
3ac17eba JM |
3772 | } |
3773 | ||
9526fd29 | 3774 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) |
b22128ef JM |
3775 | return -1; |
3776 | ||
3777 | return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, | |
0918c4bf JM |
3778 | config_methods, use == WPAS_P2P_PD_FOR_JOIN, |
3779 | 0); | |
b22128ef JM |
3780 | } |
3781 | ||
3782 | ||
3783 | int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, | |
3784 | char *end) | |
3785 | { | |
3786 | return p2p_scan_result_text(ies, ies_len, buf, end); | |
3787 | } | |
3788 | ||
3789 | ||
1cc3a29d JM |
3790 | static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s) |
3791 | { | |
3792 | if (!wpa_s->pending_action_tx) | |
3793 | return; | |
3794 | ||
3795 | wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new " | |
3796 | "operation request"); | |
3797 | wpabuf_free(wpa_s->pending_action_tx); | |
3798 | wpa_s->pending_action_tx = NULL; | |
3799 | } | |
3800 | ||
3801 | ||
b22128ef | 3802 | int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout, |
046ef4aa | 3803 | enum p2p_discovery_type type, |
6d92fa6e JM |
3804 | unsigned int num_req_dev_types, const u8 *req_dev_types, |
3805 | const u8 *dev_id) | |
b22128ef | 3806 | { |
1cc3a29d | 3807 | wpas_p2p_clear_pending_action_tx(wpa_s); |
b22128ef JM |
3808 | wpa_s->p2p_long_listen = 0; |
3809 | ||
3ac17eba JM |
3810 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
3811 | return wpa_drv_p2p_find(wpa_s, timeout, type); | |
3812 | ||
9526fd29 JM |
3813 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
3814 | return -1; | |
3815 | ||
433cd2ce JM |
3816 | wpa_supplicant_cancel_sched_scan(wpa_s); |
3817 | ||
046ef4aa | 3818 | return p2p_find(wpa_s->global->p2p, timeout, type, |
6d92fa6e | 3819 | num_req_dev_types, req_dev_types, dev_id); |
b22128ef JM |
3820 | } |
3821 | ||
3822 | ||
3823 | void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s) | |
3824 | { | |
1cc3a29d | 3825 | wpas_p2p_clear_pending_action_tx(wpa_s); |
b22128ef | 3826 | wpa_s->p2p_long_listen = 0; |
9d39057c | 3827 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
ef922c4a | 3828 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
39185dfa | 3829 | wpa_s->p2p_cb_on_scan_complete = 0; |
b22128ef | 3830 | |
3ac17eba JM |
3831 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
3832 | wpa_drv_p2p_stop_find(wpa_s); | |
3833 | return; | |
3834 | } | |
3835 | ||
9526fd29 JM |
3836 | if (wpa_s->global->p2p) |
3837 | p2p_stop_find(wpa_s->global->p2p); | |
b22128ef JM |
3838 | |
3839 | wpas_p2p_remove_pending_group_interface(wpa_s); | |
3840 | } | |
3841 | ||
3842 | ||
3843 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx) | |
3844 | { | |
3845 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
3846 | wpa_s->p2p_long_listen = 0; | |
3847 | } | |
3848 | ||
3849 | ||
3850 | int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout) | |
3851 | { | |
3852 | int res; | |
3853 | ||
9526fd29 JM |
3854 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
3855 | return -1; | |
3856 | ||
433cd2ce | 3857 | wpa_supplicant_cancel_sched_scan(wpa_s); |
1cc3a29d JM |
3858 | wpas_p2p_clear_pending_action_tx(wpa_s); |
3859 | ||
b22128ef JM |
3860 | if (timeout == 0) { |
3861 | /* | |
3862 | * This is a request for unlimited Listen state. However, at | |
3863 | * least for now, this is mapped to a Listen state for one | |
3864 | * hour. | |
3865 | */ | |
3866 | timeout = 3600; | |
3867 | } | |
3868 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); | |
3869 | wpa_s->p2p_long_listen = 0; | |
3870 | ||
2bb747e2 JM |
3871 | /* |
3872 | * Stop previous find/listen operation to avoid trying to request a new | |
3873 | * remain-on-channel operation while the driver is still running the | |
3874 | * previous one. | |
3875 | */ | |
3876 | if (wpa_s->global->p2p) | |
3877 | p2p_stop_find(wpa_s->global->p2p); | |
3878 | ||
b22128ef JM |
3879 | res = wpas_p2p_listen_start(wpa_s, timeout * 1000); |
3880 | if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) { | |
11fb02be | 3881 | wpa_s->p2p_long_listen = timeout * 1000; |
b22128ef JM |
3882 | eloop_register_timeout(timeout, 0, |
3883 | wpas_p2p_long_listen_timeout, | |
3884 | wpa_s, NULL); | |
3885 | } | |
3886 | ||
3887 | return res; | |
3888 | } | |
3889 | ||
3890 | ||
4c08c0bd | 3891 | int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, |
b22128ef JM |
3892 | u8 *buf, size_t len, int p2p_group) |
3893 | { | |
4c08c0bd JM |
3894 | struct wpabuf *p2p_ie; |
3895 | int ret; | |
3896 | ||
b22128ef JM |
3897 | if (wpa_s->global->p2p_disabled) |
3898 | return -1; | |
3899 | if (wpa_s->global->p2p == NULL) | |
3900 | return -1; | |
e1f1509b JM |
3901 | if (bss == NULL) |
3902 | return -1; | |
b22128ef | 3903 | |
4c08c0bd JM |
3904 | p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
3905 | ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len, | |
3906 | p2p_group, p2p_ie); | |
3907 | wpabuf_free(p2p_ie); | |
3908 | ||
3909 | return ret; | |
b22128ef JM |
3910 | } |
3911 | ||
3912 | ||
3913 | int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr, | |
04a85e44 | 3914 | const u8 *dst, const u8 *bssid, |
baf513d6 | 3915 | const u8 *ie, size_t ie_len, int ssi_signal) |
b22128ef JM |
3916 | { |
3917 | if (wpa_s->global->p2p_disabled) | |
3918 | return 0; | |
3919 | if (wpa_s->global->p2p == NULL) | |
3920 | return 0; | |
3921 | ||
2d43d37f JB |
3922 | switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid, |
3923 | ie, ie_len)) { | |
3924 | case P2P_PREQ_NOT_P2P: | |
3925 | wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len, | |
3926 | ssi_signal); | |
3927 | /* fall through */ | |
3928 | case P2P_PREQ_MALFORMED: | |
3929 | case P2P_PREQ_NOT_LISTEN: | |
3930 | case P2P_PREQ_NOT_PROCESSED: | |
3931 | default: /* make gcc happy */ | |
3932 | return 0; | |
3933 | case P2P_PREQ_PROCESSED: | |
3934 | return 1; | |
3935 | } | |
b22128ef JM |
3936 | } |
3937 | ||
3938 | ||
3939 | void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da, | |
3940 | const u8 *sa, const u8 *bssid, | |
3941 | u8 category, const u8 *data, size_t len, int freq) | |
3942 | { | |
3943 | if (wpa_s->global->p2p_disabled) | |
3944 | return; | |
3945 | if (wpa_s->global->p2p == NULL) | |
3946 | return; | |
3947 | ||
3948 | p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len, | |
3949 | freq); | |
3950 | } | |
3951 | ||
3952 | ||
3953 | void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies) | |
3954 | { | |
3955 | if (wpa_s->global->p2p_disabled) | |
3956 | return; | |
3957 | if (wpa_s->global->p2p == NULL) | |
3958 | return; | |
3959 | ||
6d92fa6e | 3960 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
b22128ef JM |
3961 | } |
3962 | ||
3963 | ||
3964 | void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s) | |
3965 | { | |
3966 | p2p_group_deinit(wpa_s->p2p_group); | |
3967 | wpa_s->p2p_group = NULL; | |
a7fd39bb JD |
3968 | |
3969 | wpa_s->ap_configured_cb = NULL; | |
3970 | wpa_s->ap_configured_cb_ctx = NULL; | |
3971 | wpa_s->ap_configured_cb_data = NULL; | |
3972 | wpa_s->connect_without_scan = NULL; | |
b22128ef JM |
3973 | } |
3974 | ||
3975 | ||
3976 | int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr) | |
3977 | { | |
3978 | wpa_s->p2p_long_listen = 0; | |
3979 | ||
3ac17eba JM |
3980 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
3981 | return wpa_drv_p2p_reject(wpa_s, addr); | |
3982 | ||
9526fd29 JM |
3983 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
3984 | return -1; | |
3985 | ||
b22128ef JM |
3986 | return p2p_reject(wpa_s->global->p2p, addr); |
3987 | } | |
3988 | ||
3989 | ||
3990 | /* Invite to reinvoke a persistent group */ | |
3991 | int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr, | |
3992 | struct wpa_ssid *ssid, const u8 *go_dev_addr) | |
3993 | { | |
3994 | enum p2p_invite_role role; | |
3995 | u8 *bssid = NULL; | |
3996 | ||
3997 | if (ssid->mode == WPAS_MODE_P2P_GO) { | |
3998 | role = P2P_INVITE_ROLE_GO; | |
3999 | if (peer_addr == NULL) { | |
4000 | wpa_printf(MSG_DEBUG, "P2P: Missing peer " | |
4001 | "address in invitation command"); | |
4002 | return -1; | |
4003 | } | |
4004 | if (wpas_p2p_create_iface(wpa_s)) { | |
4005 | if (wpas_p2p_add_group_interface(wpa_s, | |
4006 | WPA_IF_P2P_GO) < 0) { | |
4007 | wpa_printf(MSG_ERROR, "P2P: Failed to " | |
4008 | "allocate a new interface for the " | |
4009 | "group"); | |
4010 | return -1; | |
4011 | } | |
4012 | bssid = wpa_s->pending_interface_addr; | |
4013 | } else | |
4014 | bssid = wpa_s->own_addr; | |
4015 | } else { | |
4016 | role = P2P_INVITE_ROLE_CLIENT; | |
4017 | peer_addr = ssid->bssid; | |
4018 | } | |
4019 | wpa_s->pending_invite_ssid_id = ssid->id; | |
4020 | ||
3ac17eba JM |
4021 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
4022 | return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid, | |
4023 | ssid->ssid, ssid->ssid_len, | |
4024 | go_dev_addr, 1); | |
4025 | ||
9526fd29 JM |
4026 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
4027 | return -1; | |
4028 | ||
b22128ef JM |
4029 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
4030 | ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1); | |
4031 | } | |
4032 | ||
4033 | ||
4034 | /* Invite to join an active group */ | |
4035 | int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname, | |
4036 | const u8 *peer_addr, const u8 *go_dev_addr) | |
4037 | { | |
4038 | struct wpa_global *global = wpa_s->global; | |
4039 | enum p2p_invite_role role; | |
4040 | u8 *bssid = NULL; | |
4041 | struct wpa_ssid *ssid; | |
c427ac92 | 4042 | int persistent; |
b22128ef JM |
4043 | |
4044 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
4045 | if (os_strcmp(wpa_s->ifname, ifname) == 0) | |
4046 | break; | |
4047 | } | |
4048 | if (wpa_s == NULL) { | |
4049 | wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname); | |
4050 | return -1; | |
4051 | } | |
4052 | ||
4053 | ssid = wpa_s->current_ssid; | |
4054 | if (ssid == NULL) { | |
4055 | wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for " | |
4056 | "invitation"); | |
4057 | return -1; | |
4058 | } | |
4059 | ||
c427ac92 JM |
4060 | persistent = ssid->p2p_persistent_group && |
4061 | wpas_p2p_get_persistent(wpa_s->parent, peer_addr, | |
4062 | ssid->ssid, ssid->ssid_len); | |
4063 | ||
b22128ef JM |
4064 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
4065 | role = P2P_INVITE_ROLE_ACTIVE_GO; | |
4066 | bssid = wpa_s->own_addr; | |
4067 | if (go_dev_addr == NULL) | |
d7e70476 | 4068 | go_dev_addr = wpa_s->global->p2p_dev_addr; |
b22128ef JM |
4069 | } else { |
4070 | role = P2P_INVITE_ROLE_CLIENT; | |
4071 | if (wpa_s->wpa_state < WPA_ASSOCIATED) { | |
4072 | wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot " | |
4073 | "invite to current group"); | |
4074 | return -1; | |
4075 | } | |
4076 | bssid = wpa_s->bssid; | |
4077 | if (go_dev_addr == NULL && | |
4078 | !is_zero_ether_addr(wpa_s->go_dev_addr)) | |
4079 | go_dev_addr = wpa_s->go_dev_addr; | |
4080 | } | |
bb79dc72 | 4081 | wpa_s->parent->pending_invite_ssid_id = -1; |
b22128ef | 4082 | |
3ac17eba JM |
4083 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
4084 | return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid, | |
4085 | ssid->ssid, ssid->ssid_len, | |
c427ac92 | 4086 | go_dev_addr, persistent); |
3ac17eba | 4087 | |
9526fd29 JM |
4088 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
4089 | return -1; | |
4090 | ||
b22128ef | 4091 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
d9d6a58c | 4092 | ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq, |
c427ac92 | 4093 | go_dev_addr, persistent); |
b22128ef JM |
4094 | } |
4095 | ||
4096 | ||
4097 | void wpas_p2p_completed(struct wpa_supplicant *wpa_s) | |
4098 | { | |
4099 | struct wpa_ssid *ssid = wpa_s->current_ssid; | |
4100 | const char *ssid_txt; | |
4101 | u8 go_dev_addr[ETH_ALEN]; | |
4b6baa2f | 4102 | int network_id = -1; |
b22128ef | 4103 | int persistent; |
b49d6ccb | 4104 | int freq; |
b22128ef | 4105 | |
361cdf34 JM |
4106 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, |
4107 | NULL); | |
4108 | ||
b22128ef JM |
4109 | if (!wpa_s->show_group_started || !ssid) |
4110 | return; | |
4111 | ||
4112 | wpa_s->show_group_started = 0; | |
4113 | ||
4114 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); | |
4115 | os_memset(go_dev_addr, 0, ETH_ALEN); | |
4116 | if (ssid->bssid_set) | |
4117 | os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN); | |
4118 | persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, | |
4119 | ssid->ssid_len); | |
4120 | os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN); | |
4121 | ||
a0a9f3b0 JM |
4122 | if (wpa_s->global->p2p_group_formation == wpa_s) |
4123 | wpa_s->global->p2p_group_formation = NULL; | |
4124 | ||
b49d6ccb JJ |
4125 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : |
4126 | (int) wpa_s->assoc_freq; | |
b22128ef JM |
4127 | if (ssid->passphrase == NULL && ssid->psk_set) { |
4128 | char psk[65]; | |
4129 | wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); | |
4130 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED | |
c481048f AC |
4131 | "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr=" |
4132 | MACSTR "%s", | |
b49d6ccb | 4133 | wpa_s->ifname, ssid_txt, freq, psk, |
c481048f | 4134 | MAC2STR(go_dev_addr), |
b22128ef JM |
4135 | persistent ? " [PERSISTENT]" : ""); |
4136 | } else { | |
4137 | wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED | |
c481048f AC |
4138 | "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" " |
4139 | "go_dev_addr=" MACSTR "%s", | |
b49d6ccb | 4140 | wpa_s->ifname, ssid_txt, freq, |
b22128ef JM |
4141 | ssid->passphrase ? ssid->passphrase : "", |
4142 | MAC2STR(go_dev_addr), | |
4143 | persistent ? " [PERSISTENT]" : ""); | |
4144 | } | |
4145 | ||
4146 | if (persistent) | |
4b6baa2f JMB |
4147 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
4148 | ssid, go_dev_addr); | |
4149 | if (network_id < 0) | |
4150 | network_id = ssid->id; | |
4151 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1); | |
b22128ef JM |
4152 | } |
4153 | ||
4154 | ||
4155 | int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1, | |
4156 | u32 interval1, u32 duration2, u32 interval2) | |
4157 | { | |
3ac17eba JM |
4158 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
4159 | return -1; | |
9526fd29 JM |
4160 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
4161 | return -1; | |
3ac17eba | 4162 | |
b22128ef JM |
4163 | if (wpa_s->wpa_state < WPA_ASSOCIATED || |
4164 | wpa_s->current_ssid == NULL || | |
4165 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA) | |
4166 | return -1; | |
4167 | ||
4168 | return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid, | |
4169 | wpa_s->own_addr, wpa_s->assoc_freq, | |
4170 | duration1, interval1, duration2, interval2); | |
4171 | } | |
4172 | ||
4173 | ||
4174 | int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period, | |
4175 | unsigned int interval) | |
4176 | { | |
3ac17eba JM |
4177 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
4178 | return -1; | |
4179 | ||
9526fd29 JM |
4180 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
4181 | return -1; | |
4182 | ||
b22128ef JM |
4183 | return p2p_ext_listen(wpa_s->global->p2p, period, interval); |
4184 | } | |
4185 | ||
4186 | ||
c8106615 JM |
4187 | static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s) |
4188 | { | |
4189 | return wpa_s->current_ssid != NULL && | |
4190 | wpa_s->current_ssid->p2p_group && | |
4191 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA; | |
4192 | } | |
4193 | ||
4194 | ||
3071e181 JM |
4195 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx) |
4196 | { | |
4197 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
4198 | ||
c8106615 | 4199 | if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) { |
3071e181 JM |
4200 | wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - " |
4201 | "disabled"); | |
4202 | return; | |
4203 | } | |
4204 | ||
4205 | wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate " | |
4206 | "group"); | |
4207 | wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT; | |
4208 | wpas_p2p_group_delete(wpa_s); | |
4209 | } | |
4210 | ||
4211 | ||
4212 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s) | |
4213 | { | |
5f482d55 | 4214 | int timeout; |
c8106615 | 4215 | |
dddc7045 JM |
4216 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
4217 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); | |
4218 | ||
c8106615 | 4219 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) |
3071e181 JM |
4220 | return; |
4221 | ||
c8106615 JM |
4222 | timeout = wpa_s->conf->p2p_group_idle; |
4223 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA && | |
4224 | (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE)) | |
4225 | timeout = P2P_MAX_CLIENT_IDLE; | |
4226 | ||
4227 | if (timeout == 0) | |
3071e181 JM |
4228 | return; |
4229 | ||
5f482d55 JM |
4230 | if (timeout < 0) { |
4231 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA) | |
4232 | timeout = 0; /* special client mode no-timeout */ | |
4233 | else | |
4234 | return; | |
4235 | } | |
4236 | ||
8c472816 JM |
4237 | if (wpa_s->p2p_in_provisioning) { |
4238 | /* | |
4239 | * Use the normal group formation timeout during the | |
4240 | * provisioning phase to avoid terminating this process too | |
4241 | * early due to group idle timeout. | |
4242 | */ | |
4243 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " | |
4244 | "during provisioning"); | |
4245 | return; | |
4246 | } | |
4247 | ||
361cdf34 JM |
4248 | if (wpa_s->show_group_started) { |
4249 | /* | |
4250 | * Use the normal group formation timeout between the end of | |
4251 | * the provisioning phase and completion of 4-way handshake to | |
4252 | * avoid terminating this process too early due to group idle | |
4253 | * timeout. | |
4254 | */ | |
4255 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " | |
4256 | "while waiting for initial 4-way handshake to " | |
4257 | "complete"); | |
4258 | return; | |
4259 | } | |
4260 | ||
3071e181 | 4261 | wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds", |
c8106615 JM |
4262 | timeout); |
4263 | eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout, | |
4264 | wpa_s, NULL); | |
3071e181 JM |
4265 | } |
4266 | ||
4267 | ||
b22128ef | 4268 | void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, |
3fc14102 JM |
4269 | u16 reason_code, const u8 *ie, size_t ie_len, |
4270 | int locally_generated) | |
b22128ef | 4271 | { |
9526fd29 | 4272 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
b22128ef | 4273 | return; |
3ac17eba JM |
4274 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
4275 | return; | |
b22128ef | 4276 | |
3fc14102 JM |
4277 | if (!locally_generated) |
4278 | p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, | |
4279 | ie_len); | |
4280 | ||
4281 | if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated && | |
4282 | wpa_s->current_ssid && | |
4283 | wpa_s->current_ssid->p2p_group && | |
4284 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA) { | |
4285 | wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group " | |
4286 | "session is ending"); | |
4287 | wpa_s->removal_reason = P2P_GROUP_REMOVAL_GO_ENDING_SESSION; | |
4288 | wpas_p2p_group_delete(wpa_s); | |
4289 | } | |
b22128ef JM |
4290 | } |
4291 | ||
4292 | ||
4293 | void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, | |
3fc14102 JM |
4294 | u16 reason_code, const u8 *ie, size_t ie_len, |
4295 | int locally_generated) | |
b22128ef | 4296 | { |
9526fd29 | 4297 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
b22128ef | 4298 | return; |
3ac17eba JM |
4299 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
4300 | return; | |
b22128ef | 4301 | |
3fc14102 JM |
4302 | if (!locally_generated) |
4303 | p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, | |
4304 | ie_len); | |
b22128ef JM |
4305 | } |
4306 | ||
4307 | ||
4308 | void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) | |
4309 | { | |
4310 | struct p2p_data *p2p = wpa_s->global->p2p; | |
4311 | ||
4312 | if (p2p == NULL) | |
4313 | return; | |
4314 | ||
4c010834 KL |
4315 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
4316 | return; | |
4317 | ||
b22128ef JM |
4318 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME) |
4319 | p2p_set_dev_name(p2p, wpa_s->conf->device_name); | |
4320 | ||
2f646b6e JB |
4321 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) |
4322 | p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type); | |
b22128ef | 4323 | |
b6e01800 JM |
4324 | if (wpa_s->wps && |
4325 | (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS)) | |
4326 | p2p_set_config_methods(p2p, wpa_s->wps->config_methods); | |
4327 | ||
4328 | if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)) | |
4329 | p2p_set_uuid(p2p, wpa_s->wps->uuid); | |
4330 | ||
4331 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) { | |
4332 | p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer); | |
4333 | p2p_set_model_name(p2p, wpa_s->conf->model_name); | |
4334 | p2p_set_model_number(p2p, wpa_s->conf->model_number); | |
4335 | p2p_set_serial_number(p2p, wpa_s->conf->serial_number); | |
4336 | } | |
4337 | ||
2f646b6e JB |
4338 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) |
4339 | p2p_set_sec_dev_types(p2p, | |
4340 | (void *) wpa_s->conf->sec_device_type, | |
4341 | wpa_s->conf->num_sec_device_types); | |
b22128ef | 4342 | |
f95cac27 JMB |
4343 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) { |
4344 | int i; | |
4345 | p2p_remove_wps_vendor_extensions(p2p); | |
4346 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { | |
4347 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) | |
4348 | continue; | |
4349 | p2p_add_wps_vendor_extension( | |
4350 | p2p, wpa_s->conf->wps_vendor_ext[i]); | |
4351 | } | |
4352 | } | |
4353 | ||
b22128ef JM |
4354 | if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) && |
4355 | wpa_s->conf->country[0] && wpa_s->conf->country[1]) { | |
4356 | char country[3]; | |
4357 | country[0] = wpa_s->conf->country[0]; | |
4358 | country[1] = wpa_s->conf->country[1]; | |
4359 | country[2] = 0x04; | |
4360 | p2p_set_country(p2p, country); | |
4361 | } | |
4362 | ||
4363 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) { | |
4364 | p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix, | |
4365 | wpa_s->conf->p2p_ssid_postfix ? | |
4366 | os_strlen(wpa_s->conf->p2p_ssid_postfix) : | |
4367 | 0); | |
4368 | } | |
0f66abd2 SS |
4369 | |
4370 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS) | |
4371 | p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss); | |
2463ba70 JS |
4372 | |
4373 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) { | |
4374 | u8 reg_class, channel; | |
4375 | int ret; | |
4376 | unsigned int r; | |
4377 | if (wpa_s->conf->p2p_listen_reg_class && | |
4378 | wpa_s->conf->p2p_listen_channel) { | |
4379 | reg_class = wpa_s->conf->p2p_listen_reg_class; | |
4380 | channel = wpa_s->conf->p2p_listen_channel; | |
4381 | } else { | |
4382 | reg_class = 81; | |
4383 | /* | |
4384 | * Pick one of the social channels randomly as the | |
4385 | * listen channel. | |
4386 | */ | |
4387 | os_get_random((u8 *) &r, sizeof(r)); | |
4388 | channel = 1 + (r % 3) * 5; | |
4389 | } | |
4390 | ret = p2p_set_listen_channel(p2p, reg_class, channel); | |
4391 | if (ret) | |
4392 | wpa_printf(MSG_ERROR, "P2P: Own listen channel update " | |
4393 | "failed: %d", ret); | |
4394 | } | |
4395 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) { | |
4396 | u8 op_reg_class, op_channel, cfg_op_channel; | |
4397 | int ret = 0; | |
4398 | unsigned int r; | |
4399 | if (wpa_s->conf->p2p_oper_reg_class && | |
4400 | wpa_s->conf->p2p_oper_channel) { | |
4401 | op_reg_class = wpa_s->conf->p2p_oper_reg_class; | |
4402 | op_channel = wpa_s->conf->p2p_oper_channel; | |
4403 | cfg_op_channel = 1; | |
4404 | } else { | |
4405 | op_reg_class = 81; | |
4406 | /* | |
4407 | * Use random operation channel from (1, 6, 11) | |
4408 | *if no other preference is indicated. | |
4409 | */ | |
4410 | os_get_random((u8 *) &r, sizeof(r)); | |
4411 | op_channel = 1 + (r % 3) * 5; | |
4412 | cfg_op_channel = 0; | |
4413 | } | |
4414 | ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel, | |
4415 | cfg_op_channel); | |
4416 | if (ret) | |
4417 | wpa_printf(MSG_ERROR, "P2P: Own oper channel update " | |
4418 | "failed: %d", ret); | |
4419 | } | |
21d996f7 JM |
4420 | |
4421 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) { | |
4422 | if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan, | |
4423 | wpa_s->conf->p2p_pref_chan) < 0) { | |
4424 | wpa_printf(MSG_ERROR, "P2P: Preferred channel list " | |
4425 | "update failed"); | |
4426 | } | |
4427 | } | |
b22128ef | 4428 | } |
aefb53bd JM |
4429 | |
4430 | ||
4431 | int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start, | |
4432 | int duration) | |
4433 | { | |
4434 | if (!wpa_s->ap_iface) | |
4435 | return -1; | |
4436 | return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start, | |
4437 | duration); | |
4438 | } | |
72044390 JM |
4439 | |
4440 | ||
4441 | int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled) | |
4442 | { | |
9526fd29 | 4443 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
72044390 JM |
4444 | return -1; |
4445 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) | |
4446 | return -1; | |
4447 | ||
4448 | wpa_s->global->cross_connection = enabled; | |
4449 | p2p_set_cross_connect(wpa_s->global->p2p, enabled); | |
4450 | ||
4451 | if (!enabled) { | |
4452 | struct wpa_supplicant *iface; | |
4453 | ||
4454 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) | |
4455 | { | |
4456 | if (iface->cross_connect_enabled == 0) | |
4457 | continue; | |
4458 | ||
4459 | iface->cross_connect_enabled = 0; | |
4460 | iface->cross_connect_in_use = 0; | |
4461 | wpa_msg(iface->parent, MSG_INFO, | |
4462 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", | |
4463 | iface->ifname, iface->cross_connect_uplink); | |
4464 | } | |
4465 | } | |
4466 | ||
4467 | return 0; | |
4468 | } | |
4469 | ||
4470 | ||
4471 | static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink) | |
4472 | { | |
4473 | struct wpa_supplicant *iface; | |
4474 | ||
4475 | if (!uplink->global->cross_connection) | |
4476 | return; | |
4477 | ||
4478 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { | |
4479 | if (!iface->cross_connect_enabled) | |
4480 | continue; | |
4481 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != | |
4482 | 0) | |
4483 | continue; | |
4484 | if (iface->ap_iface == NULL) | |
4485 | continue; | |
4486 | if (iface->cross_connect_in_use) | |
4487 | continue; | |
4488 | ||
4489 | iface->cross_connect_in_use = 1; | |
4490 | wpa_msg(iface->parent, MSG_INFO, | |
4491 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", | |
4492 | iface->ifname, iface->cross_connect_uplink); | |
4493 | } | |
4494 | } | |
4495 | ||
4496 | ||
4497 | static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink) | |
4498 | { | |
4499 | struct wpa_supplicant *iface; | |
4500 | ||
4501 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { | |
4502 | if (!iface->cross_connect_enabled) | |
4503 | continue; | |
4504 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != | |
4505 | 0) | |
4506 | continue; | |
4507 | if (!iface->cross_connect_in_use) | |
4508 | continue; | |
4509 | ||
4510 | wpa_msg(iface->parent, MSG_INFO, | |
4511 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", | |
4512 | iface->ifname, iface->cross_connect_uplink); | |
4513 | iface->cross_connect_in_use = 0; | |
4514 | } | |
4515 | } | |
4516 | ||
4517 | ||
4518 | void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s) | |
4519 | { | |
4520 | if (wpa_s->ap_iface || wpa_s->current_ssid == NULL || | |
4521 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA || | |
4522 | wpa_s->cross_connect_disallowed) | |
4523 | wpas_p2p_disable_cross_connect(wpa_s); | |
4524 | else | |
4525 | wpas_p2p_enable_cross_connect(wpa_s); | |
dddc7045 JM |
4526 | if (!wpa_s->ap_iface && |
4527 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) | |
4528 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); | |
72044390 JM |
4529 | } |
4530 | ||
4531 | ||
4532 | void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s) | |
4533 | { | |
4534 | wpas_p2p_disable_cross_connect(wpa_s); | |
4c2c6751 JM |
4535 | if (!wpa_s->ap_iface && |
4536 | !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout, | |
4537 | wpa_s, NULL)) | |
3071e181 | 4538 | wpas_p2p_set_group_idle_timeout(wpa_s); |
72044390 JM |
4539 | } |
4540 | ||
4541 | ||
4542 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s) | |
4543 | { | |
4544 | struct wpa_supplicant *iface; | |
4545 | ||
4546 | if (!wpa_s->global->cross_connection) | |
4547 | return; | |
4548 | ||
4549 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { | |
4550 | if (iface == wpa_s) | |
4551 | continue; | |
4552 | if (iface->drv_flags & | |
4553 | WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) | |
4554 | continue; | |
4555 | if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) | |
4556 | continue; | |
4557 | ||
4558 | wpa_s->cross_connect_enabled = 1; | |
4559 | os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname, | |
4560 | sizeof(wpa_s->cross_connect_uplink)); | |
4561 | wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from " | |
4562 | "%s to %s whenever uplink is available", | |
4563 | wpa_s->ifname, wpa_s->cross_connect_uplink); | |
4564 | ||
4565 | if (iface->ap_iface || iface->current_ssid == NULL || | |
4566 | iface->current_ssid->mode != WPAS_MODE_INFRA || | |
4567 | iface->cross_connect_disallowed || | |
4568 | iface->wpa_state != WPA_COMPLETED) | |
4569 | break; | |
4570 | ||
4571 | wpa_s->cross_connect_in_use = 1; | |
4572 | wpa_msg(wpa_s->parent, MSG_INFO, | |
4573 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", | |
4574 | wpa_s->ifname, wpa_s->cross_connect_uplink); | |
4575 | break; | |
4576 | } | |
4577 | } | |
b73bf0a7 JM |
4578 | |
4579 | ||
4580 | int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s) | |
4581 | { | |
4582 | if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT && | |
4583 | !wpa_s->p2p_in_provisioning) | |
4584 | return 0; /* not P2P client operation */ | |
4585 | ||
4586 | wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC " | |
4587 | "session overlap"); | |
148bb37f JM |
4588 | if (wpa_s != wpa_s->parent) |
4589 | wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP); | |
0a14e3ec SS |
4590 | |
4591 | if (wpa_s->global->p2p) | |
4592 | p2p_group_formation_failed(wpa_s->global->p2p); | |
4593 | ||
4594 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | |
4595 | wpa_s->parent, NULL); | |
4596 | ||
b73bf0a7 JM |
4597 | wpas_group_formation_completed(wpa_s, 0); |
4598 | return 1; | |
4599 | } | |
b5c9da8d JM |
4600 | |
4601 | ||
4602 | void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s) | |
4603 | { | |
4604 | struct p2p_channels chan; | |
4605 | ||
4606 | if (wpa_s->global == NULL || wpa_s->global->p2p == NULL) | |
4607 | return; | |
4608 | ||
4609 | os_memset(&chan, 0, sizeof(chan)); | |
4610 | if (wpas_p2p_setup_channels(wpa_s, &chan)) { | |
4611 | wpa_printf(MSG_ERROR, "P2P: Failed to update supported " | |
4612 | "channel list"); | |
4613 | return; | |
4614 | } | |
4615 | ||
4616 | p2p_update_channel_list(wpa_s->global->p2p, &chan); | |
4617 | } | |
59eba7a2 JM |
4618 | |
4619 | ||
4620 | int wpas_p2p_cancel(struct wpa_supplicant *wpa_s) | |
4621 | { | |
4622 | struct wpa_global *global = wpa_s->global; | |
4623 | int found = 0; | |
231bbd03 | 4624 | const u8 *peer; |
59eba7a2 | 4625 | |
9526fd29 JM |
4626 | if (global->p2p == NULL) |
4627 | return -1; | |
4628 | ||
59eba7a2 JM |
4629 | wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation"); |
4630 | ||
4631 | if (wpa_s->pending_interface_name[0] && | |
4632 | !is_zero_ether_addr(wpa_s->pending_interface_addr)) | |
4633 | found = 1; | |
4634 | ||
231bbd03 SS |
4635 | peer = p2p_get_go_neg_peer(global->p2p); |
4636 | if (peer) { | |
4637 | wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer " | |
4638 | MACSTR, MAC2STR(peer)); | |
4639 | p2p_unauthorize(global->p2p, peer); | |
75c208b9 | 4640 | found = 1; |
231bbd03 SS |
4641 | } |
4642 | ||
59eba7a2 JM |
4643 | wpas_p2p_stop_find(wpa_s); |
4644 | ||
4645 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | |
4646 | if (wpa_s == global->p2p_group_formation && | |
a0a9f3b0 JM |
4647 | (wpa_s->p2p_in_provisioning || |
4648 | wpa_s->parent->pending_interface_type == | |
4649 | WPA_IF_P2P_CLIENT)) { | |
59eba7a2 JM |
4650 | wpa_printf(MSG_DEBUG, "P2P: Interface %s in group " |
4651 | "formation found - cancelling", | |
4652 | wpa_s->ifname); | |
4653 | found = 1; | |
45fee6f0 SS |
4654 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
4655 | wpa_s->parent, NULL); | |
59eba7a2 JM |
4656 | wpas_p2p_group_delete(wpa_s); |
4657 | break; | |
4658 | } | |
4659 | } | |
4660 | ||
4661 | if (!found) { | |
4662 | wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found"); | |
4663 | return -1; | |
4664 | } | |
4665 | ||
4666 | return 0; | |
4667 | } | |
c973f386 JM |
4668 | |
4669 | ||
4670 | void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s) | |
4671 | { | |
4672 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) | |
4673 | return; | |
4674 | ||
4675 | wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not " | |
4676 | "being available anymore"); | |
4677 | wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE; | |
4678 | wpas_p2p_group_delete(wpa_s); | |
4679 | } | |
7cfc4ac3 AGS |
4680 | |
4681 | ||
4682 | void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s, | |
4683 | int freq_24, int freq_5, int freq_overall) | |
4684 | { | |
4685 | struct p2p_data *p2p = wpa_s->global->p2p; | |
4686 | if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) | |
4687 | return; | |
4688 | p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall); | |
4689 | } | |
9d562b79 SS |
4690 | |
4691 | ||
4692 | int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr) | |
4693 | { | |
4694 | u8 peer[ETH_ALEN]; | |
4695 | struct p2p_data *p2p = wpa_s->global->p2p; | |
4696 | ||
4697 | if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) | |
4698 | return -1; | |
4699 | ||
4700 | if (hwaddr_aton(addr, peer)) | |
4701 | return -1; | |
4702 | ||
4703 | return p2p_unauthorize(p2p, peer); | |
4704 | } | |
3103f345 JB |
4705 | |
4706 | ||
4707 | /** | |
4708 | * wpas_p2p_disconnect - Disconnect from a P2P Group | |
4709 | * @wpa_s: Pointer to wpa_supplicant data | |
4710 | * Returns: 0 on success, -1 on failure | |
4711 | * | |
4712 | * This can be used to disconnect from a group in which the local end is a P2P | |
4713 | * Client or to end a P2P Group in case the local end is the Group Owner. If a | |
4714 | * virtual network interface was created for this group, that interface will be | |
4715 | * removed. Otherwise, only the configured P2P group network will be removed | |
4716 | * from the interface. | |
4717 | */ | |
4718 | int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s) | |
4719 | { | |
4720 | ||
4721 | if (wpa_s == NULL) | |
4722 | return -1; | |
4723 | ||
4724 | wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED; | |
4725 | wpas_p2p_group_delete(wpa_s); | |
4726 | ||
4727 | return 0; | |
4728 | } | |
303f60d3 JM |
4729 | |
4730 | ||
4731 | int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s) | |
4732 | { | |
4733 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | |
4734 | return 0; | |
4735 | ||
4736 | return p2p_in_progress(wpa_s->global->p2p); | |
4737 | } | |
502618f7 JM |
4738 | |
4739 | ||
4740 | void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s, | |
4741 | struct wpa_ssid *ssid) | |
502618f7 JM |
4742 | { |
4743 | if (wpa_s->p2p_in_provisioning && ssid->p2p_group && | |
4744 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | |
4745 | wpa_s->parent, NULL) > 0) { | |
eb6f8c2b AC |
4746 | /** |
4747 | * Remove the network by scheduling the group formation | |
4748 | * timeout to happen immediately. The teardown code | |
4749 | * needs to be scheduled to run asynch later so that we | |
4750 | * don't delete data from under ourselves unexpectedly. | |
4751 | * Calling wpas_p2p_group_formation_timeout directly | |
4752 | * causes a series of crashes in WPS failure scenarios. | |
4753 | */ | |
502618f7 JM |
4754 | wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to " |
4755 | "P2P group network getting removed"); | |
eb6f8c2b AC |
4756 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
4757 | wpa_s->parent, NULL); | |
502618f7 JM |
4758 | } |
4759 | } | |
87f841a1 JM |
4760 | |
4761 | ||
4762 | struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s, | |
c427ac92 JM |
4763 | const u8 *addr, const u8 *ssid, |
4764 | size_t ssid_len) | |
87f841a1 JM |
4765 | { |
4766 | struct wpa_ssid *s; | |
fbdcfd57 | 4767 | size_t i; |
87f841a1 JM |
4768 | |
4769 | for (s = wpa_s->conf->ssid; s; s = s->next) { | |
fbdcfd57 JM |
4770 | if (s->disabled != 2) |
4771 | continue; | |
c427ac92 JM |
4772 | if (ssid && |
4773 | (ssid_len != s->ssid_len || | |
4774 | os_memcmp(ssid, s->ssid, ssid_len) != 0)) | |
4775 | continue; | |
fbdcfd57 JM |
4776 | if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0) |
4777 | return s; /* peer is GO in the persistent group */ | |
4778 | if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL) | |
4779 | continue; | |
4780 | for (i = 0; i < s->num_p2p_clients; i++) { | |
4781 | if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, | |
4782 | addr, ETH_ALEN) == 0) | |
4783 | return s; /* peer is P2P client in persistent | |
4784 | * group */ | |
4785 | } | |
87f841a1 JM |
4786 | } |
4787 | ||
4788 | return NULL; | |
4789 | } | |
fbdcfd57 JM |
4790 | |
4791 | ||
4792 | void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s, | |
4793 | const u8 *addr) | |
4794 | { | |
4795 | if (addr == NULL) | |
4796 | return; | |
4797 | wpas_p2p_add_persistent_group_client(wpa_s, addr); | |
4798 | } |