]>
Commit | Line | Data |
---|---|---|
b22128ef JM |
1 | /* |
2 | * Wi-Fi Direct - P2P module | |
3 | * Copyright (c) 2009-2010, Atheros Communications | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Alternatively, this software may be distributed under the terms of BSD | |
10 | * license. | |
11 | * | |
12 | * See README and COPYING for more details. | |
13 | */ | |
14 | ||
15 | #include "includes.h" | |
16 | ||
17 | #include "common.h" | |
18 | #include "eloop.h" | |
19 | #include "common/ieee802_11_defs.h" | |
20 | #include "common/ieee802_11_common.h" | |
21 | #include "wps/wps_i.h" | |
22 | #include "p2p_i.h" | |
23 | #include "p2p.h" | |
24 | ||
25 | ||
26 | static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx); | |
27 | static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev); | |
28 | static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, | |
29 | const u8 *sa, const u8 *data, size_t len, | |
30 | int rx_freq); | |
31 | static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, | |
32 | const u8 *sa, const u8 *data, | |
33 | size_t len); | |
34 | static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx); | |
40c03fd4 | 35 | static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx); |
b22128ef JM |
36 | |
37 | ||
40c03fd4 JM |
38 | /* |
39 | * p2p_scan recovery timeout | |
40 | * | |
41 | * Many drivers are using 30 second timeout on scan results. Allow a bit larger | |
42 | * timeout for this to avoid hitting P2P timeout unnecessarily. | |
43 | */ | |
44 | #define P2P_SCAN_TIMEOUT 35 | |
45 | ||
b22128ef JM |
46 | /** |
47 | * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer | |
48 | * entries will be removed | |
49 | */ | |
50 | #define P2P_PEER_EXPIRATION_AGE 300 | |
51 | ||
52 | #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2) | |
53 | ||
54 | static void p2p_expire_peers(struct p2p_data *p2p) | |
55 | { | |
56 | struct p2p_device *dev, *n; | |
57 | struct os_time now; | |
58 | ||
59 | os_get_time(&now); | |
60 | dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) { | |
61 | if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec) | |
62 | continue; | |
63 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer " | |
64 | "entry " MACSTR, MAC2STR(dev->p2p_device_addr)); | |
65 | dl_list_del(&dev->list); | |
66 | p2p_device_free(p2p, dev); | |
67 | } | |
68 | } | |
69 | ||
70 | ||
71 | static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx) | |
72 | { | |
73 | struct p2p_data *p2p = eloop_ctx; | |
74 | p2p_expire_peers(p2p); | |
75 | eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0, | |
76 | p2p_expiration_timeout, p2p, NULL); | |
77 | } | |
78 | ||
79 | ||
80 | static const char * p2p_state_txt(int state) | |
81 | { | |
82 | switch (state) { | |
83 | case P2P_IDLE: | |
84 | return "IDLE"; | |
85 | case P2P_SEARCH: | |
86 | return "SEARCH"; | |
87 | case P2P_CONNECT: | |
88 | return "CONNECT"; | |
89 | case P2P_CONNECT_LISTEN: | |
90 | return "CONNECT_LISTEN"; | |
91 | case P2P_GO_NEG: | |
92 | return "GO_NEG"; | |
93 | case P2P_LISTEN_ONLY: | |
94 | return "LISTEN_ONLY"; | |
95 | case P2P_WAIT_PEER_CONNECT: | |
96 | return "WAIT_PEER_CONNECT"; | |
97 | case P2P_WAIT_PEER_IDLE: | |
98 | return "WAIT_PEER_IDLE"; | |
99 | case P2P_SD_DURING_FIND: | |
100 | return "SD_DURING_FIND"; | |
101 | case P2P_PROVISIONING: | |
102 | return "PROVISIONING"; | |
103 | case P2P_PD_DURING_FIND: | |
104 | return "PD_DURING_FIND"; | |
105 | case P2P_INVITE: | |
106 | return "INVITE"; | |
107 | case P2P_INVITE_LISTEN: | |
108 | return "INVITE_LISTEN"; | |
109 | default: | |
110 | return "?"; | |
111 | } | |
112 | } | |
113 | ||
114 | ||
115 | void p2p_set_state(struct p2p_data *p2p, int new_state) | |
116 | { | |
117 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s", | |
118 | p2p_state_txt(p2p->state), p2p_state_txt(new_state)); | |
119 | p2p->state = new_state; | |
120 | } | |
121 | ||
122 | ||
123 | void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec) | |
124 | { | |
125 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
126 | "P2P: Set timeout (state=%s): %u.%06u sec", | |
127 | p2p_state_txt(p2p->state), sec, usec); | |
128 | eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); | |
129 | eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL); | |
130 | } | |
131 | ||
132 | ||
133 | void p2p_clear_timeout(struct p2p_data *p2p) | |
134 | { | |
135 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)", | |
136 | p2p_state_txt(p2p->state)); | |
137 | eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); | |
138 | } | |
139 | ||
140 | ||
141 | void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer, | |
142 | int status) | |
143 | { | |
144 | struct p2p_go_neg_results res; | |
145 | p2p_clear_timeout(p2p); | |
146 | p2p_set_state(p2p, P2P_IDLE); | |
147 | p2p->go_neg_peer = NULL; | |
148 | ||
149 | os_memset(&res, 0, sizeof(res)); | |
150 | res.status = status; | |
151 | if (peer) { | |
152 | os_memcpy(res.peer_device_addr, peer->p2p_device_addr, | |
153 | ETH_ALEN); | |
154 | os_memcpy(res.peer_interface_addr, peer->intended_addr, | |
155 | ETH_ALEN); | |
156 | } | |
157 | p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); | |
158 | } | |
159 | ||
160 | ||
161 | static void p2p_listen_in_find(struct p2p_data *p2p) | |
162 | { | |
163 | unsigned int r, tu; | |
164 | int freq; | |
165 | struct wpabuf *ies; | |
166 | ||
167 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
168 | "P2P: Starting short listen state (state=%s)", | |
169 | p2p_state_txt(p2p->state)); | |
170 | ||
171 | freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class, | |
172 | p2p->cfg->channel); | |
173 | if (freq < 0) { | |
174 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
175 | "P2P: Unknown regulatory class/channel"); | |
176 | return; | |
177 | } | |
178 | ||
179 | os_get_random((u8 *) &r, sizeof(r)); | |
180 | tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) + | |
181 | p2p->min_disc_int) * 100; | |
182 | ||
183 | p2p->pending_listen_freq = freq; | |
184 | p2p->pending_listen_sec = 0; | |
185 | p2p->pending_listen_usec = 1024 * tu; | |
186 | ||
187 | ies = p2p_build_probe_resp_ies(p2p); | |
188 | if (ies == NULL) | |
189 | return; | |
190 | ||
191 | if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000, | |
192 | ies) < 0) { | |
193 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
194 | "P2P: Failed to start listen mode"); | |
195 | p2p->pending_listen_freq = 0; | |
196 | } | |
197 | wpabuf_free(ies); | |
198 | } | |
199 | ||
200 | ||
201 | int p2p_listen(struct p2p_data *p2p, unsigned int timeout) | |
202 | { | |
203 | int freq; | |
204 | struct wpabuf *ies; | |
205 | ||
206 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
207 | "P2P: Going to listen(only) state"); | |
208 | ||
209 | freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class, | |
210 | p2p->cfg->channel); | |
211 | if (freq < 0) { | |
212 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
213 | "P2P: Unknown regulatory class/channel"); | |
214 | return -1; | |
215 | } | |
216 | ||
217 | p2p->pending_listen_freq = freq; | |
218 | p2p->pending_listen_sec = timeout / 1000; | |
219 | p2p->pending_listen_usec = (timeout % 1000) * 1000; | |
220 | ||
221 | if (p2p->p2p_scan_running) { | |
222 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
223 | "P2P: p2p_scan running - delay start of listen state"); | |
224 | p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN; | |
225 | return 0; | |
226 | } | |
227 | ||
228 | ies = p2p_build_probe_resp_ies(p2p); | |
229 | if (ies == NULL) | |
230 | return -1; | |
231 | ||
232 | if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) { | |
233 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
234 | "P2P: Failed to start listen mode"); | |
235 | p2p->pending_listen_freq = 0; | |
236 | wpabuf_free(ies); | |
237 | return -1; | |
238 | } | |
239 | wpabuf_free(ies); | |
240 | ||
241 | p2p_set_state(p2p, P2P_LISTEN_ONLY); | |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
246 | ||
247 | static void p2p_device_clear_reported(struct p2p_data *p2p) | |
248 | { | |
249 | struct p2p_device *dev; | |
250 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) | |
251 | dev->flags &= ~P2P_DEV_REPORTED; | |
252 | } | |
253 | ||
254 | ||
255 | /** | |
256 | * p2p_get_device - Fetch a peer entry | |
257 | * @p2p: P2P module context from p2p_init() | |
258 | * @addr: P2P Device Address of the peer | |
259 | * Returns: Pointer to the device entry or %NULL if not found | |
260 | */ | |
261 | struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr) | |
262 | { | |
263 | struct p2p_device *dev; | |
264 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
265 | if (os_memcmp(dev->p2p_device_addr, addr, ETH_ALEN) == 0) | |
266 | return dev; | |
267 | } | |
268 | return NULL; | |
269 | } | |
270 | ||
271 | ||
272 | /** | |
273 | * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address | |
274 | * @p2p: P2P module context from p2p_init() | |
275 | * @addr: P2P Interface Address of the peer | |
276 | * Returns: Pointer to the device entry or %NULL if not found | |
277 | */ | |
278 | struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p, | |
279 | const u8 *addr) | |
280 | { | |
281 | struct p2p_device *dev; | |
282 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
283 | if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0) | |
284 | return dev; | |
285 | } | |
286 | return NULL; | |
287 | } | |
288 | ||
289 | ||
290 | /** | |
291 | * p2p_create_device - Create a peer entry | |
292 | * @p2p: P2P module context from p2p_init() | |
293 | * @addr: P2P Device Address of the peer | |
294 | * Returns: Pointer to the device entry or %NULL on failure | |
295 | * | |
296 | * If there is already an entry for the peer, it will be returned instead of | |
297 | * creating a new one. | |
298 | */ | |
299 | static struct p2p_device * p2p_create_device(struct p2p_data *p2p, | |
300 | const u8 *addr) | |
301 | { | |
302 | struct p2p_device *dev, *oldest = NULL; | |
303 | size_t count = 0; | |
304 | ||
305 | dev = p2p_get_device(p2p, addr); | |
306 | if (dev) | |
307 | return dev; | |
308 | ||
309 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
310 | count++; | |
311 | if (oldest == NULL || | |
312 | os_time_before(&dev->last_seen, &oldest->last_seen)) | |
313 | oldest = dev; | |
314 | } | |
315 | if (count + 1 > p2p->cfg->max_peers && oldest) { | |
316 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
317 | "P2P: Remove oldest peer entry to make room for a new " | |
318 | "peer"); | |
319 | dl_list_del(&oldest->list); | |
320 | p2p_device_free(p2p, oldest); | |
321 | } | |
322 | ||
323 | dev = os_zalloc(sizeof(*dev)); | |
324 | if (dev == NULL) | |
325 | return NULL; | |
326 | dl_list_add(&p2p->devices, &dev->list); | |
327 | os_memcpy(dev->p2p_device_addr, addr, ETH_ALEN); | |
328 | ||
329 | return dev; | |
330 | } | |
331 | ||
332 | ||
333 | static void p2p_copy_client_info(struct p2p_device *dev, | |
334 | struct p2p_client_info *cli) | |
335 | { | |
336 | os_memcpy(dev->device_name, cli->dev_name, cli->dev_name_len); | |
337 | dev->device_name[cli->dev_name_len] = '\0'; | |
338 | dev->dev_capab = cli->dev_capab; | |
339 | dev->config_methods = cli->config_methods; | |
340 | os_memcpy(dev->pri_dev_type, cli->pri_dev_type, 8); | |
341 | } | |
342 | ||
343 | ||
344 | static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr, | |
345 | const u8 *go_interface_addr, int freq, | |
346 | const u8 *gi, size_t gi_len) | |
347 | { | |
348 | struct p2p_group_info info; | |
349 | size_t c; | |
350 | struct p2p_device *dev; | |
351 | ||
352 | if (gi == NULL) | |
353 | return 0; | |
354 | ||
355 | if (p2p_group_info_parse(gi, gi_len, &info) < 0) | |
356 | return -1; | |
357 | ||
358 | /* | |
359 | * Clear old data for this group; if the devices are still in the | |
360 | * group, the information will be restored in the loop following this. | |
361 | */ | |
362 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
363 | if (os_memcpy(dev->member_in_go_iface, go_interface_addr, | |
364 | ETH_ALEN) == 0) { | |
365 | os_memset(dev->member_in_go_iface, 0, ETH_ALEN); | |
366 | os_memset(dev->member_in_go_dev, 0, ETH_ALEN); | |
367 | } | |
368 | } | |
369 | ||
370 | for (c = 0; c < info.num_clients; c++) { | |
371 | struct p2p_client_info *cli = &info.client[c]; | |
372 | dev = p2p_get_device(p2p, cli->p2p_device_addr); | |
373 | if (dev) { | |
374 | /* | |
375 | * Update information only if we have not received this | |
376 | * directly from the client. | |
377 | */ | |
378 | if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY | | |
379 | P2P_DEV_PROBE_REQ_ONLY)) | |
380 | p2p_copy_client_info(dev, cli); | |
381 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { | |
382 | dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; | |
383 | } | |
384 | } else { | |
385 | dev = p2p_create_device(p2p, cli->p2p_device_addr); | |
386 | if (dev == NULL) | |
387 | continue; | |
388 | dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY; | |
389 | p2p_copy_client_info(dev, cli); | |
390 | dev->oper_freq = freq; | |
391 | p2p->cfg->dev_found( | |
392 | p2p->cfg->cb_ctx, dev->p2p_device_addr, | |
393 | dev->p2p_device_addr, dev->pri_dev_type, | |
394 | dev->device_name, dev->config_methods, | |
395 | dev->dev_capab, 0); | |
396 | } | |
397 | ||
398 | os_memcpy(dev->interface_addr, cli->p2p_interface_addr, | |
399 | ETH_ALEN); | |
400 | os_get_time(&dev->last_seen); | |
401 | os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN); | |
402 | os_memcpy(dev->member_in_go_iface, go_interface_addr, | |
403 | ETH_ALEN); | |
404 | } | |
405 | ||
406 | return 0; | |
407 | } | |
408 | ||
409 | ||
410 | /** | |
411 | * p2p_add_device - Add peer entries based on scan results | |
412 | * @p2p: P2P module context from p2p_init() | |
413 | * @addr: Source address of Beacon or Probe Response frame (may be either | |
414 | * P2P Device Address or P2P Interface Address) | |
415 | * @level: Signal level (signal strength of the received frame from the peer) | |
416 | * @freq: Frequency on which the Beacon or Probe Response frame was received | |
417 | * @ies: IEs from the Beacon or Probe Response frame | |
418 | * @ies_len: Length of ies buffer in octets | |
419 | * Returns: 0 on success, -1 on failure | |
420 | * | |
421 | * If the scan result is for a GO, the clients in the group will also be added | |
17bef1e9 AC |
422 | * to the peer table. This function can also be used with some other frames |
423 | * like Provision Discovery Request that contains P2P Capability and P2P Device | |
424 | * Info attributes. | |
b22128ef | 425 | */ |
17bef1e9 AC |
426 | int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level, |
427 | const u8 *ies, size_t ies_len) | |
b22128ef JM |
428 | { |
429 | struct p2p_device *dev; | |
430 | struct p2p_message msg; | |
431 | const u8 *p2p_dev_addr; | |
432 | ||
433 | os_memset(&msg, 0, sizeof(msg)); | |
434 | if (p2p_parse_ies(ies, ies_len, &msg)) { | |
435 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
436 | "P2P: Failed to parse P2P IE for a device entry"); | |
437 | p2p_parse_free(&msg); | |
438 | return -1; | |
439 | } | |
440 | ||
441 | if (msg.p2p_device_addr) | |
442 | p2p_dev_addr = msg.p2p_device_addr; | |
443 | else if (msg.device_id) | |
444 | p2p_dev_addr = msg.device_id; | |
445 | else { | |
446 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
447 | "P2P: Ignore scan data without P2P Device Info or " | |
448 | "P2P Device Id"); | |
449 | p2p_parse_free(&msg); | |
450 | return -1; | |
451 | } | |
452 | ||
80c9582a JM |
453 | if (!is_zero_ether_addr(p2p->peer_filter) && |
454 | os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) { | |
455 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer " | |
456 | "filter for " MACSTR " due to peer filter", | |
457 | MAC2STR(p2p_dev_addr)); | |
458 | return 0; | |
459 | } | |
460 | ||
b22128ef JM |
461 | dev = p2p_create_device(p2p, p2p_dev_addr); |
462 | if (dev == NULL) { | |
463 | p2p_parse_free(&msg); | |
464 | return -1; | |
465 | } | |
466 | os_get_time(&dev->last_seen); | |
467 | dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY); | |
468 | ||
469 | if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0) | |
470 | os_memcpy(dev->interface_addr, addr, ETH_ALEN); | |
471 | if (msg.ssid && | |
472 | (msg.ssid[1] != P2P_WILDCARD_SSID_LEN || | |
473 | os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) | |
474 | != 0)) { | |
475 | os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]); | |
476 | dev->oper_ssid_len = msg.ssid[1]; | |
477 | } | |
478 | ||
479 | if (freq >= 2412 && freq <= 2484 && msg.ds_params && | |
480 | *msg.ds_params >= 1 && *msg.ds_params <= 14) { | |
481 | int ds_freq; | |
482 | if (*msg.ds_params == 14) | |
483 | ds_freq = 2484; | |
484 | else | |
485 | ds_freq = 2407 + *msg.ds_params * 5; | |
486 | if (freq != ds_freq) { | |
487 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
488 | "P2P: Update Listen frequency based on DS " | |
489 | "Parameter Set IE: %d -> %d MHz", | |
490 | freq, ds_freq); | |
491 | freq = ds_freq; | |
492 | } | |
493 | } | |
494 | ||
495 | if (dev->listen_freq && dev->listen_freq != freq) { | |
496 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
497 | "P2P: Update Listen frequency based on scan " | |
498 | "results (" MACSTR " %d -> %d MHz (DS param %d)", | |
499 | MAC2STR(dev->p2p_device_addr), dev->listen_freq, freq, | |
500 | msg.ds_params ? *msg.ds_params : -1); | |
501 | } | |
502 | dev->listen_freq = freq; | |
f8d0131a JM |
503 | if (msg.group_info) |
504 | dev->oper_freq = freq; | |
b22128ef JM |
505 | dev->level = level; |
506 | ||
507 | if (msg.pri_dev_type) | |
508 | os_memcpy(dev->pri_dev_type, msg.pri_dev_type, | |
509 | sizeof(dev->pri_dev_type)); | |
510 | os_memcpy(dev->device_name, msg.device_name, sizeof(dev->device_name)); | |
511 | dev->config_methods = msg.config_methods ? msg.config_methods : | |
512 | msg.wps_config_methods; | |
513 | if (msg.capability) { | |
514 | dev->dev_capab = msg.capability[0]; | |
515 | dev->group_capab = msg.capability[1]; | |
516 | } | |
517 | ||
518 | if (msg.ext_listen_timing) { | |
519 | dev->ext_listen_period = WPA_GET_LE16(msg.ext_listen_timing); | |
520 | dev->ext_listen_interval = | |
521 | WPA_GET_LE16(msg.ext_listen_timing + 2); | |
522 | } | |
523 | ||
524 | p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, msg.group_info, | |
525 | msg.group_info_len); | |
526 | ||
527 | p2p_parse_free(&msg); | |
528 | ||
529 | if (p2p_pending_sd_req(p2p, dev)) | |
530 | dev->flags |= P2P_DEV_SD_SCHEDULE; | |
531 | ||
532 | if (dev->flags & P2P_DEV_REPORTED) | |
533 | return 0; | |
534 | ||
535 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
536 | "P2P: Peer found with Listen frequency %d MHz", freq); | |
537 | if (dev->flags & P2P_DEV_USER_REJECTED) { | |
538 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
539 | "P2P: Do not report rejected device"); | |
540 | return 0; | |
541 | } | |
542 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, dev->p2p_device_addr, | |
543 | dev->pri_dev_type, dev->device_name, | |
544 | dev->config_methods, dev->dev_capab, | |
545 | dev->group_capab); | |
546 | dev->flags |= P2P_DEV_REPORTED; | |
547 | ||
548 | return 0; | |
549 | } | |
550 | ||
551 | ||
552 | static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev) | |
553 | { | |
554 | if (p2p->go_neg_peer == dev) | |
555 | p2p->go_neg_peer = NULL; | |
556 | if (p2p->invite_peer == dev) | |
557 | p2p->invite_peer = NULL; | |
558 | if (p2p->sd_peer == dev) | |
559 | p2p->sd_peer = NULL; | |
560 | if (p2p->pending_client_disc_go == dev) | |
561 | p2p->pending_client_disc_go = NULL; | |
562 | ||
563 | os_free(dev); | |
564 | } | |
565 | ||
566 | ||
567 | static int p2p_get_next_prog_freq(struct p2p_data *p2p) | |
568 | { | |
569 | struct p2p_channels *c; | |
570 | struct p2p_reg_class *cla; | |
571 | size_t cl, ch; | |
572 | int found = 0; | |
573 | u8 reg_class; | |
574 | u8 channel; | |
575 | int freq; | |
576 | ||
577 | c = &p2p->cfg->channels; | |
578 | for (cl = 0; cl < c->reg_classes; cl++) { | |
579 | cla = &c->reg_class[cl]; | |
580 | if (cla->reg_class != p2p->last_prog_scan_class) | |
581 | continue; | |
582 | for (ch = 0; ch < cla->channels; ch++) { | |
583 | if (cla->channel[ch] == p2p->last_prog_scan_chan) { | |
584 | found = 1; | |
585 | break; | |
586 | } | |
587 | } | |
588 | if (found) | |
589 | break; | |
590 | } | |
591 | ||
592 | if (!found) { | |
593 | /* Start from beginning */ | |
594 | reg_class = c->reg_class[0].reg_class; | |
595 | channel = c->reg_class[0].channel[0]; | |
596 | } else { | |
597 | /* Pick the next channel */ | |
598 | ch++; | |
599 | if (ch == cla->channels) { | |
600 | cl++; | |
601 | if (cl == c->reg_classes) | |
602 | cl = 0; | |
603 | ch = 0; | |
604 | } | |
605 | reg_class = c->reg_class[cl].reg_class; | |
606 | channel = c->reg_class[cl].channel[ch]; | |
607 | } | |
608 | ||
609 | freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel); | |
610 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search " | |
611 | "channel: reg_class %u channel %u -> %d MHz", | |
612 | reg_class, channel, freq); | |
613 | p2p->last_prog_scan_class = reg_class; | |
614 | p2p->last_prog_scan_chan = channel; | |
615 | ||
616 | if (freq == 2412 || freq == 2437 || freq == 2462) | |
617 | return 0; /* No need to add social channels */ | |
618 | return freq; | |
619 | } | |
620 | ||
621 | ||
622 | static void p2p_search(struct p2p_data *p2p) | |
623 | { | |
624 | int freq = 0; | |
625 | enum p2p_scan_type type; | |
626 | ||
627 | if (p2p->drv_in_listen) { | |
628 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still " | |
629 | "in Listen state - wait for it to end before " | |
630 | "continuing"); | |
631 | return; | |
632 | } | |
633 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); | |
634 | ||
635 | if (p2p->go_neg_peer) { | |
636 | /* | |
637 | * Only scan the known listen frequency of the peer | |
638 | * during GO Negotiation start. | |
639 | */ | |
640 | freq = p2p->go_neg_peer->listen_freq; | |
641 | if (freq <= 0) | |
642 | freq = p2p->go_neg_peer->oper_freq; | |
643 | type = P2P_SCAN_SPECIFIC; | |
644 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search " | |
645 | "for freq %u (GO Neg)", freq); | |
646 | } else if (p2p->invite_peer) { | |
647 | /* | |
648 | * Only scan the known listen frequency of the peer | |
649 | * during Invite start. | |
650 | */ | |
651 | freq = p2p->invite_peer->listen_freq; | |
652 | if (freq <= 0) | |
653 | freq = p2p->invite_peer->oper_freq; | |
654 | type = P2P_SCAN_SPECIFIC; | |
655 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search " | |
656 | "for freq %u (Invite)", freq); | |
657 | } else if (p2p->find_type == P2P_FIND_PROGRESSIVE && | |
658 | (freq = p2p_get_next_prog_freq(p2p)) > 0) { | |
659 | type = P2P_SCAN_SOCIAL_PLUS_ONE; | |
660 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search " | |
661 | "(+ freq %u)", freq); | |
662 | } else { | |
663 | type = P2P_SCAN_SOCIAL; | |
664 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search"); | |
665 | } | |
666 | ||
667 | if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq) < 0) { | |
668 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
669 | "P2P: Scan request failed"); | |
670 | p2p_continue_find(p2p); | |
40c03fd4 JM |
671 | } else { |
672 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan"); | |
673 | p2p->p2p_scan_running = 1; | |
674 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
675 | eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout, | |
676 | p2p, NULL); | |
b22128ef JM |
677 | } |
678 | } | |
679 | ||
680 | ||
681 | static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx) | |
682 | { | |
683 | struct p2p_data *p2p = eloop_ctx; | |
684 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop"); | |
685 | p2p_stop_find(p2p); | |
686 | } | |
687 | ||
688 | ||
689 | static int p2p_run_after_scan(struct p2p_data *p2p) | |
690 | { | |
691 | struct p2p_device *dev; | |
692 | enum p2p_after_scan op; | |
693 | ||
3f9285ff JM |
694 | if (p2p->after_scan_tx) { |
695 | int ret; | |
696 | /* TODO: schedule p2p_run_after_scan to be called from TX | |
697 | * status callback(?) */ | |
698 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending " | |
699 | "Action frame at p2p_scan completion"); | |
700 | ret = p2p->cfg->send_action(p2p->cfg->cb_ctx, | |
701 | p2p->after_scan_tx->freq, | |
702 | p2p->after_scan_tx->dst, | |
703 | p2p->after_scan_tx->src, | |
704 | p2p->after_scan_tx->bssid, | |
705 | (u8 *) (p2p->after_scan_tx + 1), | |
706 | p2p->after_scan_tx->len, | |
707 | p2p->after_scan_tx->wait_time); | |
708 | os_free(p2p->after_scan_tx); | |
709 | p2p->after_scan_tx = NULL; | |
710 | return 1; | |
711 | } | |
712 | ||
b22128ef JM |
713 | op = p2p->start_after_scan; |
714 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; | |
715 | switch (op) { | |
716 | case P2P_AFTER_SCAN_NOTHING: | |
717 | break; | |
718 | case P2P_AFTER_SCAN_LISTEN: | |
719 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously " | |
720 | "requested Listen state"); | |
721 | p2p_listen(p2p, p2p->pending_listen_sec * 1000 + | |
722 | p2p->pending_listen_usec / 1000); | |
723 | return 1; | |
724 | case P2P_AFTER_SCAN_CONNECT: | |
725 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously " | |
726 | "requested connect with " MACSTR, | |
727 | MAC2STR(p2p->after_scan_peer)); | |
728 | dev = p2p_get_device(p2p, p2p->after_scan_peer); | |
729 | if (dev == NULL) { | |
730 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not " | |
731 | "known anymore"); | |
732 | break; | |
733 | } | |
734 | p2p_connect_send(p2p, dev); | |
735 | return 1; | |
736 | } | |
737 | ||
738 | return 0; | |
739 | } | |
740 | ||
741 | ||
b22128ef JM |
742 | static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx) |
743 | { | |
744 | struct p2p_data *p2p = eloop_ctx; | |
745 | int running; | |
746 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout " | |
747 | "(running=%d)", p2p->p2p_scan_running); | |
748 | running = p2p->p2p_scan_running; | |
749 | /* Make sure we recover from missed scan results callback */ | |
750 | p2p->p2p_scan_running = 0; | |
751 | ||
752 | if (running) | |
753 | p2p_run_after_scan(p2p); | |
754 | } | |
755 | ||
756 | ||
757 | int p2p_find(struct p2p_data *p2p, unsigned int timeout, | |
758 | enum p2p_discovery_type type) | |
759 | { | |
760 | int res; | |
761 | ||
762 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)", | |
763 | type); | |
764 | if (p2p->p2p_scan_running) { | |
765 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is " | |
766 | "already running"); | |
767 | } | |
768 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; | |
769 | p2p_clear_timeout(p2p); | |
770 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); | |
771 | p2p->find_type = type; | |
772 | p2p_device_clear_reported(p2p); | |
773 | p2p_set_state(p2p, P2P_SEARCH); | |
774 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); | |
775 | if (timeout) | |
776 | eloop_register_timeout(timeout, 0, p2p_find_timeout, | |
777 | p2p, NULL); | |
778 | switch (type) { | |
779 | case P2P_FIND_START_WITH_FULL: | |
780 | case P2P_FIND_PROGRESSIVE: | |
781 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0); | |
782 | break; | |
783 | case P2P_FIND_ONLY_SOCIAL: | |
784 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0); | |
785 | break; | |
786 | default: | |
787 | return -1; | |
788 | } | |
789 | ||
790 | if (res == 0) { | |
791 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan"); | |
792 | p2p->p2p_scan_running = 1; | |
793 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
794 | eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout, | |
795 | p2p, NULL); | |
796 | } else { | |
797 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start " | |
798 | "p2p_scan"); | |
799 | } | |
800 | ||
801 | return res; | |
802 | } | |
803 | ||
804 | ||
0b8889d8 | 805 | void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq) |
b22128ef JM |
806 | { |
807 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find"); | |
808 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); | |
809 | p2p_clear_timeout(p2p); | |
810 | p2p_set_state(p2p, P2P_IDLE); | |
811 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; | |
812 | p2p->go_neg_peer = NULL; | |
813 | p2p->sd_peer = NULL; | |
814 | p2p->invite_peer = NULL; | |
0b8889d8 JM |
815 | if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) { |
816 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen " | |
817 | "since we are on correct channel for response"); | |
818 | return; | |
819 | } | |
b22128ef JM |
820 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
821 | } | |
822 | ||
823 | ||
0b8889d8 JM |
824 | void p2p_stop_find(struct p2p_data *p2p) |
825 | { | |
826 | p2p_stop_find_for_freq(p2p, 0); | |
827 | } | |
828 | ||
829 | ||
7861cb08 | 830 | static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq) |
b22128ef | 831 | { |
b22128ef | 832 | if (force_freq) { |
1e19f734 | 833 | u8 op_reg_class, op_channel; |
b22128ef | 834 | if (p2p_freq_to_channel(p2p->cfg->country, force_freq, |
1e19f734 | 835 | &op_reg_class, &op_channel) < 0) { |
b22128ef JM |
836 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
837 | "P2P: Unsupported frequency %u MHz", | |
838 | force_freq); | |
839 | return -1; | |
840 | } | |
1e19f734 JM |
841 | if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class, |
842 | op_channel)) { | |
843 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
844 | "P2P: Frequency %u MHz (oper_class %u " | |
845 | "channel %u) not allowed for P2P", | |
846 | force_freq, op_reg_class, op_channel); | |
847 | return -1; | |
848 | } | |
849 | p2p->op_reg_class = op_reg_class; | |
850 | p2p->op_channel = op_channel; | |
b22128ef JM |
851 | p2p->channels.reg_classes = 1; |
852 | p2p->channels.reg_class[0].channels = 1; | |
853 | p2p->channels.reg_class[0].reg_class = p2p->op_reg_class; | |
854 | p2p->channels.reg_class[0].channel[0] = p2p->op_channel; | |
855 | } else { | |
7cfc4ac3 AGS |
856 | u8 op_reg_class, op_channel; |
857 | ||
858 | if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 && | |
859 | p2p_supported_freq(p2p, p2p->best_freq_overall) && | |
860 | p2p_freq_to_channel(p2p->cfg->country, | |
861 | p2p->best_freq_overall, | |
862 | &op_reg_class, &op_channel) == 0) { | |
863 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
864 | "P2P: Select best overall channel as " | |
865 | "operating channel preference"); | |
866 | p2p->op_reg_class = op_reg_class; | |
867 | p2p->op_channel = op_channel; | |
868 | } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 && | |
869 | p2p_supported_freq(p2p, p2p->best_freq_5) && | |
870 | p2p_freq_to_channel(p2p->cfg->country, | |
871 | p2p->best_freq_5, | |
872 | &op_reg_class, &op_channel) == | |
873 | 0) { | |
874 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
875 | "P2P: Select best 5 GHz channel as " | |
876 | "operating channel preference"); | |
877 | p2p->op_reg_class = op_reg_class; | |
878 | p2p->op_channel = op_channel; | |
879 | } else if (!p2p->cfg->cfg_op_channel && | |
880 | p2p->best_freq_24 > 0 && | |
881 | p2p_supported_freq(p2p, p2p->best_freq_24) && | |
882 | p2p_freq_to_channel(p2p->cfg->country, | |
883 | p2p->best_freq_24, | |
884 | &op_reg_class, &op_channel) == | |
885 | 0) { | |
886 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
887 | "P2P: Select best 2.4 GHz channel as " | |
888 | "operating channel preference"); | |
889 | p2p->op_reg_class = op_reg_class; | |
890 | p2p->op_channel = op_channel; | |
891 | } else { | |
892 | p2p->op_reg_class = p2p->cfg->op_reg_class; | |
893 | p2p->op_channel = p2p->cfg->op_channel; | |
894 | } | |
895 | ||
b22128ef JM |
896 | os_memcpy(&p2p->channels, &p2p->cfg->channels, |
897 | sizeof(struct p2p_channels)); | |
898 | } | |
899 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
900 | "P2P: Own preference for operation channel: " | |
7cfc4ac3 | 901 | "Operating Class %u Channel %u%s", |
b22128ef JM |
902 | p2p->op_reg_class, p2p->op_channel, |
903 | force_freq ? " (forced)" : ""); | |
904 | ||
7861cb08 JM |
905 | return 0; |
906 | } | |
907 | ||
908 | ||
909 | int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr, | |
910 | enum p2p_wps_method wps_method, | |
911 | int go_intent, const u8 *own_interface_addr, | |
912 | unsigned int force_freq, int persistent_group) | |
913 | { | |
914 | struct p2p_device *dev; | |
915 | ||
916 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
917 | "P2P: Request to start group negotiation - peer=" MACSTR | |
918 | " GO Intent=%d Intended Interface Address=" MACSTR | |
919 | " wps_method=%d persistent_group=%d", | |
920 | MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), | |
921 | wps_method, persistent_group); | |
922 | ||
923 | if (p2p_prepare_channel(p2p, force_freq) < 0) | |
924 | return -1; | |
925 | ||
b22128ef JM |
926 | dev = p2p_get_device(p2p, peer_addr); |
927 | if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) { | |
928 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
929 | "P2P: Cannot connect to unknown P2P Device " MACSTR, | |
930 | MAC2STR(peer_addr)); | |
931 | return -1; | |
932 | } | |
933 | ||
934 | if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) { | |
935 | if (!(dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) { | |
936 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
937 | "P2P: Cannot connect to P2P Device " MACSTR | |
938 | " that is in a group and is not discoverable", | |
939 | MAC2STR(peer_addr)); | |
940 | return -1; | |
941 | } | |
942 | if (dev->oper_freq <= 0) { | |
943 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
944 | "P2P: Cannot connect to P2P Device " MACSTR | |
945 | " with incomplete information", | |
946 | MAC2STR(peer_addr)); | |
947 | return -1; | |
948 | } | |
949 | ||
950 | /* | |
951 | * First, try to connect directly. If the peer does not | |
952 | * acknowledge frames, assume it is sleeping and use device | |
953 | * discoverability via the GO at that point. | |
954 | */ | |
955 | } | |
956 | ||
957 | dev->flags &= ~P2P_DEV_NOT_YET_READY; | |
958 | dev->flags &= ~P2P_DEV_USER_REJECTED; | |
959 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; | |
960 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; | |
961 | dev->go_neg_req_sent = 0; | |
962 | dev->go_state = UNKNOWN_GO; | |
963 | if (persistent_group) | |
964 | dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP; | |
965 | else | |
966 | dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP; | |
967 | p2p->go_intent = go_intent; | |
968 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); | |
969 | ||
970 | if (p2p->state != P2P_IDLE) | |
971 | p2p_stop_find(p2p); | |
972 | ||
f44ae207 JM |
973 | if (p2p->after_scan_tx) { |
974 | /* | |
975 | * We need to drop the pending frame to avoid issues with the | |
976 | * new GO Negotiation, e.g., when the pending frame was from a | |
977 | * previous attempt at starting a GO Negotiation. | |
978 | */ | |
979 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped " | |
980 | "previous pending Action frame TX that was waiting " | |
981 | "for p2p_scan completion"); | |
982 | os_free(p2p->after_scan_tx); | |
983 | p2p->after_scan_tx = NULL; | |
984 | } | |
985 | ||
b22128ef JM |
986 | dev->wps_method = wps_method; |
987 | dev->status = P2P_SC_SUCCESS; | |
d5b20a73 JM |
988 | |
989 | if (force_freq) | |
990 | dev->flags |= P2P_DEV_FORCE_FREQ; | |
991 | else | |
992 | dev->flags &= ~P2P_DEV_FORCE_FREQ; | |
993 | ||
b22128ef JM |
994 | if (p2p->p2p_scan_running) { |
995 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
996 | "P2P: p2p_scan running - delay connect send"); | |
997 | p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT; | |
998 | os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN); | |
999 | return 0; | |
1000 | } | |
1001 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; | |
1002 | ||
1003 | return p2p_connect_send(p2p, dev); | |
1004 | } | |
1005 | ||
1006 | ||
1007 | int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr, | |
1008 | enum p2p_wps_method wps_method, | |
1009 | int go_intent, const u8 *own_interface_addr, | |
1010 | unsigned int force_freq, int persistent_group) | |
1011 | { | |
1012 | struct p2p_device *dev; | |
1013 | ||
1014 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1015 | "P2P: Request to authorize group negotiation - peer=" MACSTR | |
1016 | " GO Intent=%d Intended Interface Address=" MACSTR | |
1017 | " wps_method=%d persistent_group=%d", | |
1018 | MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), | |
1019 | wps_method, persistent_group); | |
1020 | ||
7861cb08 JM |
1021 | if (p2p_prepare_channel(p2p, force_freq) < 0) |
1022 | return -1; | |
b22128ef JM |
1023 | |
1024 | dev = p2p_get_device(p2p, peer_addr); | |
1025 | if (dev == NULL) { | |
1026 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1027 | "P2P: Cannot authorize unknown P2P Device " MACSTR, | |
1028 | MAC2STR(peer_addr)); | |
1029 | return -1; | |
1030 | } | |
1031 | ||
1032 | dev->flags &= ~P2P_DEV_NOT_YET_READY; | |
1033 | dev->flags &= ~P2P_DEV_USER_REJECTED; | |
1034 | dev->go_neg_req_sent = 0; | |
1035 | dev->go_state = UNKNOWN_GO; | |
1036 | if (persistent_group) | |
1037 | dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP; | |
1038 | else | |
1039 | dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP; | |
1040 | p2p->go_intent = go_intent; | |
1041 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); | |
1042 | ||
1043 | dev->wps_method = wps_method; | |
1044 | dev->status = P2P_SC_SUCCESS; | |
1045 | ||
d5b20a73 JM |
1046 | if (force_freq) |
1047 | dev->flags |= P2P_DEV_FORCE_FREQ; | |
1048 | else | |
1049 | dev->flags &= ~P2P_DEV_FORCE_FREQ; | |
1050 | ||
b22128ef JM |
1051 | return 0; |
1052 | } | |
1053 | ||
1054 | ||
1055 | void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr, | |
1056 | struct p2p_device *dev, struct p2p_message *msg) | |
1057 | { | |
1058 | os_get_time(&dev->last_seen); | |
1059 | ||
1060 | if (msg->pri_dev_type) | |
1061 | os_memcpy(dev->pri_dev_type, msg->pri_dev_type, | |
1062 | sizeof(dev->pri_dev_type)); | |
1063 | os_memcpy(dev->device_name, msg->device_name, | |
1064 | sizeof(dev->device_name)); | |
1065 | dev->config_methods = msg->config_methods ? msg->config_methods : | |
1066 | msg->wps_config_methods; | |
1067 | if (msg->capability) { | |
1068 | dev->dev_capab = msg->capability[0]; | |
1069 | dev->group_capab = msg->capability[1]; | |
1070 | } | |
1071 | if (msg->listen_channel) { | |
1072 | int freq; | |
1073 | freq = p2p_channel_to_freq((char *) msg->listen_channel, | |
1074 | msg->listen_channel[3], | |
1075 | msg->listen_channel[4]); | |
1076 | if (freq < 0) { | |
1077 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1078 | "P2P: Unknown peer Listen channel: " | |
1079 | "country=%c%c(0x%02x) reg_class=%u channel=%u", | |
1080 | msg->listen_channel[0], | |
1081 | msg->listen_channel[1], | |
1082 | msg->listen_channel[2], | |
1083 | msg->listen_channel[3], | |
1084 | msg->listen_channel[4]); | |
1085 | } else { | |
1086 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update " | |
1087 | "peer " MACSTR " Listen channel: %u -> %u MHz", | |
1088 | MAC2STR(dev->p2p_device_addr), | |
1089 | dev->listen_freq, freq); | |
1090 | dev->listen_freq = freq; | |
1091 | } | |
1092 | } | |
1093 | if (msg->ext_listen_timing) { | |
1094 | dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing); | |
1095 | dev->ext_listen_interval = | |
1096 | WPA_GET_LE16(msg->ext_listen_timing + 2); | |
1097 | } | |
1098 | ||
1099 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { | |
1100 | dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; | |
1101 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1102 | "P2P: Completed device entry based on data from " | |
1103 | "GO Negotiation Request"); | |
1104 | } else { | |
1105 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1106 | "P2P: Created device entry based on GO Neg Req: " | |
1107 | MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' " | |
1108 | "listen_freq=%d", | |
1109 | MAC2STR(dev->p2p_device_addr), dev->dev_capab, | |
1110 | dev->group_capab, dev->device_name, dev->listen_freq); | |
1111 | } | |
1112 | ||
1113 | dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY; | |
1114 | ||
1115 | if (dev->flags & P2P_DEV_USER_REJECTED) { | |
1116 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1117 | "P2P: Do not report rejected device"); | |
1118 | return; | |
1119 | } | |
1120 | ||
1121 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, dev->p2p_device_addr, | |
1122 | dev->pri_dev_type, dev->device_name, | |
1123 | dev->config_methods, dev->dev_capab, | |
1124 | dev->group_capab); | |
1125 | } | |
1126 | ||
1127 | ||
1128 | void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len) | |
1129 | { | |
1130 | os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); | |
1131 | p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2); | |
1132 | os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2], | |
1133 | p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len); | |
1134 | *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len; | |
1135 | } | |
1136 | ||
1137 | ||
1138 | int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params) | |
1139 | { | |
1140 | p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len); | |
1141 | p2p_random(params->passphrase, 8); | |
1142 | return 0; | |
1143 | } | |
1144 | ||
1145 | ||
1146 | void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer) | |
1147 | { | |
1148 | struct p2p_go_neg_results res; | |
1149 | int go = peer->go_state == LOCAL_GO; | |
1150 | struct p2p_channels intersection; | |
1151 | int freqs; | |
1152 | size_t i, j; | |
1153 | ||
1154 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1155 | "P2P: GO Negotiation with " MACSTR " completed (%s will be " | |
1156 | "GO)", MAC2STR(peer->p2p_device_addr), | |
1157 | go ? "local end" : "peer"); | |
1158 | ||
1159 | os_memset(&res, 0, sizeof(res)); | |
1160 | res.role_go = go; | |
1161 | os_memcpy(res.peer_device_addr, peer->p2p_device_addr, ETH_ALEN); | |
1162 | os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN); | |
1163 | res.wps_method = peer->wps_method; | |
1164 | if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) | |
1165 | res.persistent_group = 1; | |
1166 | ||
1167 | if (go) { | |
1168 | /* Setup AP mode for WPS provisioning */ | |
1169 | res.freq = p2p_channel_to_freq(p2p->cfg->country, | |
1170 | p2p->op_reg_class, | |
1171 | p2p->op_channel); | |
1172 | os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); | |
1173 | res.ssid_len = p2p->ssid_len; | |
1174 | p2p_random(res.passphrase, 8); | |
e9a7ae41 | 1175 | } else { |
b22128ef | 1176 | res.freq = peer->oper_freq; |
e9a7ae41 JM |
1177 | if (p2p->ssid_len) { |
1178 | os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); | |
1179 | res.ssid_len = p2p->ssid_len; | |
1180 | } | |
1181 | } | |
b22128ef JM |
1182 | |
1183 | p2p_channels_intersect(&p2p->channels, &peer->channels, | |
1184 | &intersection); | |
1185 | freqs = 0; | |
1186 | for (i = 0; i < intersection.reg_classes; i++) { | |
1187 | struct p2p_reg_class *c = &intersection.reg_class[i]; | |
1188 | if (freqs + 1 == P2P_MAX_CHANNELS) | |
1189 | break; | |
1190 | for (j = 0; j < c->channels; j++) { | |
1191 | int freq; | |
1192 | if (freqs + 1 == P2P_MAX_CHANNELS) | |
1193 | break; | |
1194 | freq = p2p_channel_to_freq(peer->country, c->reg_class, | |
1195 | c->channel[j]); | |
1196 | if (freq < 0) | |
1197 | continue; | |
1198 | res.freq_list[freqs++] = freq; | |
1199 | } | |
1200 | } | |
1201 | ||
ae3e3421 JM |
1202 | res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout; |
1203 | ||
b22128ef JM |
1204 | p2p_clear_timeout(p2p); |
1205 | peer->go_neg_req_sent = 0; | |
1206 | peer->wps_method = WPS_NOT_READY; | |
1207 | ||
1208 | p2p_set_state(p2p, P2P_PROVISIONING); | |
1209 | p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); | |
1210 | } | |
1211 | ||
1212 | ||
1213 | static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa, | |
1214 | const u8 *data, size_t len, int rx_freq) | |
1215 | { | |
1216 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1217 | "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa)); | |
1218 | wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len); | |
1219 | ||
1220 | if (len < 1) | |
1221 | return; | |
1222 | ||
1223 | switch (data[0]) { | |
1224 | case P2P_GO_NEG_REQ: | |
1225 | p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq); | |
1226 | break; | |
1227 | case P2P_GO_NEG_RESP: | |
1228 | p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq); | |
1229 | break; | |
1230 | case P2P_GO_NEG_CONF: | |
1231 | p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1); | |
1232 | break; | |
1233 | case P2P_INVITATION_REQ: | |
1234 | p2p_process_invitation_req(p2p, sa, data + 1, len - 1, | |
1235 | rx_freq); | |
1236 | break; | |
1237 | case P2P_INVITATION_RESP: | |
1238 | p2p_process_invitation_resp(p2p, sa, data + 1, len - 1); | |
1239 | break; | |
1240 | case P2P_PROV_DISC_REQ: | |
1241 | p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq); | |
1242 | break; | |
1243 | case P2P_PROV_DISC_RESP: | |
1244 | p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1); | |
1245 | break; | |
1246 | case P2P_DEV_DISC_REQ: | |
1247 | p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq); | |
1248 | break; | |
1249 | case P2P_DEV_DISC_RESP: | |
1250 | p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1); | |
1251 | break; | |
1252 | default: | |
1253 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1254 | "P2P: Unsupported P2P Public Action frame type %d", | |
1255 | data[0]); | |
1256 | break; | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | ||
1261 | void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, const u8 *sa, | |
1262 | const u8 *bssid, const u8 *data, size_t len, | |
1263 | int freq) | |
1264 | { | |
1265 | if (len < 1) | |
1266 | return; | |
1267 | ||
1268 | switch (data[0]) { | |
1269 | case WLAN_PA_VENDOR_SPECIFIC: | |
1270 | data++; | |
1271 | len--; | |
1272 | if (len < 3) | |
1273 | return; | |
1274 | if (WPA_GET_BE24(data) != OUI_WFA) | |
1275 | return; | |
1276 | ||
1277 | data += 3; | |
1278 | len -= 3; | |
1279 | if (len < 1) | |
1280 | return; | |
1281 | ||
1282 | if (*data != P2P_OUI_TYPE) | |
1283 | return; | |
1284 | ||
1285 | p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq); | |
1286 | break; | |
1287 | case WLAN_PA_GAS_INITIAL_REQ: | |
1288 | p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq); | |
1289 | break; | |
1290 | case WLAN_PA_GAS_INITIAL_RESP: | |
18708aad JM |
1291 | p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq); |
1292 | break; | |
1293 | case WLAN_PA_GAS_COMEBACK_REQ: | |
1294 | p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq); | |
1295 | break; | |
1296 | case WLAN_PA_GAS_COMEBACK_RESP: | |
1297 | p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq); | |
b22128ef JM |
1298 | break; |
1299 | } | |
1300 | } | |
1301 | ||
1302 | ||
1303 | void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa, | |
1304 | const u8 *bssid, u8 category, | |
1305 | const u8 *data, size_t len, int freq) | |
1306 | { | |
1307 | if (category == WLAN_ACTION_PUBLIC) { | |
1308 | p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq); | |
1309 | return; | |
1310 | } | |
1311 | ||
1312 | if (category != WLAN_ACTION_VENDOR_SPECIFIC) | |
1313 | return; | |
1314 | ||
1315 | if (len < 4) | |
1316 | return; | |
1317 | ||
1318 | if (WPA_GET_BE24(data) != OUI_WFA) | |
1319 | return; | |
1320 | data += 3; | |
1321 | len -= 3; | |
1322 | ||
1323 | if (*data != P2P_OUI_TYPE) | |
1324 | return; | |
1325 | data++; | |
1326 | len--; | |
1327 | ||
1328 | /* P2P action frame */ | |
1329 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1330 | "P2P: RX P2P Action from " MACSTR, MAC2STR(sa)); | |
1331 | wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len); | |
1332 | ||
1333 | if (len < 1) | |
1334 | return; | |
1335 | switch (data[0]) { | |
1336 | case P2P_NOA: | |
1337 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1338 | "P2P: Received P2P Action - Notice of Absence"); | |
1339 | /* TODO */ | |
1340 | break; | |
1341 | case P2P_PRESENCE_REQ: | |
1342 | p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq); | |
1343 | break; | |
1344 | case P2P_PRESENCE_RESP: | |
1345 | p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1); | |
1346 | break; | |
1347 | case P2P_GO_DISC_REQ: | |
1348 | p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq); | |
1349 | break; | |
1350 | default: | |
1351 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1352 | "P2P: Received P2P Action - unknown type %u", data[0]); | |
1353 | break; | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | ||
1358 | static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx) | |
1359 | { | |
1360 | struct p2p_data *p2p = eloop_ctx; | |
1361 | if (p2p->go_neg_peer == NULL) | |
1362 | return; | |
1363 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); | |
1364 | p2p->go_neg_peer->status = P2P_SC_SUCCESS; | |
1365 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
1366 | } | |
1367 | ||
1368 | ||
1369 | static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx) | |
1370 | { | |
1371 | struct p2p_data *p2p = eloop_ctx; | |
1372 | if (p2p->invite_peer == NULL) | |
1373 | return; | |
1374 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); | |
1375 | p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr); | |
1376 | } | |
1377 | ||
1378 | ||
1379 | static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr, | |
1380 | const u8 *ie, size_t ie_len) | |
1381 | { | |
1382 | struct p2p_message msg; | |
1383 | struct p2p_device *dev; | |
1384 | ||
1385 | os_memset(&msg, 0, sizeof(msg)); | |
1386 | if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL) | |
1387 | { | |
1388 | p2p_parse_free(&msg); | |
1389 | return; /* not a P2P probe */ | |
1390 | } | |
1391 | ||
1392 | if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN || | |
1393 | os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) | |
1394 | != 0) { | |
1395 | /* The Probe Request is not part of P2P Device Discovery. It is | |
1396 | * not known whether the source address of the frame is the P2P | |
1397 | * Device Address or P2P Interface Address. Do not add a new | |
1398 | * peer entry based on this frames. | |
1399 | */ | |
1400 | p2p_parse_free(&msg); | |
1401 | return; | |
1402 | } | |
1403 | ||
1404 | dev = p2p_get_device(p2p, addr); | |
1405 | if (dev) { | |
1406 | if (dev->country[0] == 0 && msg.listen_channel) | |
1407 | os_memcpy(dev->country, msg.listen_channel, 3); | |
1408 | p2p_parse_free(&msg); | |
1409 | return; /* already known */ | |
1410 | } | |
1411 | ||
1412 | dev = p2p_create_device(p2p, addr); | |
1413 | if (dev == NULL) { | |
1414 | p2p_parse_free(&msg); | |
1415 | return; | |
1416 | } | |
1417 | ||
1418 | os_get_time(&dev->last_seen); | |
1419 | dev->flags |= P2P_DEV_PROBE_REQ_ONLY; | |
1420 | ||
1421 | if (msg.capability) { | |
1422 | dev->dev_capab = msg.capability[0]; | |
1423 | dev->group_capab = msg.capability[1]; | |
1424 | } | |
1425 | ||
1426 | if (msg.listen_channel) { | |
1427 | os_memcpy(dev->country, msg.listen_channel, 3); | |
1428 | dev->listen_freq = p2p_channel_to_freq(dev->country, | |
1429 | msg.listen_channel[3], | |
1430 | msg.listen_channel[4]); | |
1431 | } | |
1432 | ||
1433 | os_memcpy(dev->device_name, msg.device_name, sizeof(dev->device_name)); | |
1434 | ||
1435 | if (msg.wps_pri_dev_type) | |
1436 | os_memcpy(dev->pri_dev_type, msg.wps_pri_dev_type, | |
1437 | sizeof(dev->pri_dev_type)); | |
1438 | ||
1439 | p2p_parse_free(&msg); | |
1440 | ||
1441 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1442 | "P2P: Created device entry based on Probe Req: " MACSTR | |
1443 | " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d", | |
1444 | MAC2STR(dev->p2p_device_addr), dev->dev_capab, | |
1445 | dev->group_capab, dev->device_name, dev->listen_freq); | |
1446 | } | |
1447 | ||
1448 | ||
1449 | struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p, | |
1450 | const u8 *addr, | |
1451 | struct p2p_message *msg) | |
1452 | { | |
1453 | struct p2p_device *dev; | |
1454 | ||
1455 | dev = p2p_get_device(p2p, addr); | |
1456 | if (dev) { | |
1457 | os_get_time(&dev->last_seen); | |
1458 | return dev; /* already known */ | |
1459 | } | |
1460 | ||
1461 | dev = p2p_create_device(p2p, addr); | |
1462 | if (dev == NULL) | |
1463 | return NULL; | |
1464 | ||
1465 | p2p_add_dev_info(p2p, addr, dev, msg); | |
1466 | ||
1467 | return dev; | |
1468 | } | |
1469 | ||
1470 | ||
1471 | static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type) | |
1472 | { | |
1473 | if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0) | |
1474 | return 1; | |
1475 | if (os_memcmp(dev_type, req_dev_type, 2) == 0 && | |
1476 | WPA_GET_BE32(&req_dev_type[2]) == 0 && | |
1477 | WPA_GET_BE16(&req_dev_type[6]) == 0) | |
1478 | return 1; /* Category match with wildcard OUI/sub-category */ | |
1479 | return 0; | |
1480 | } | |
1481 | ||
1482 | ||
1483 | int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[], | |
1484 | size_t num_req_dev_type) | |
1485 | { | |
1486 | size_t i; | |
1487 | for (i = 0; i < num_req_dev_type; i++) { | |
1488 | if (dev_type_match(dev_type, req_dev_type[i])) | |
1489 | return 1; | |
1490 | } | |
1491 | return 0; | |
1492 | } | |
1493 | ||
1494 | ||
1495 | /** | |
1496 | * p2p_match_dev_type - Match local device type with requested type | |
1497 | * @p2p: P2P module context from p2p_init() | |
1498 | * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs) | |
1499 | * Returns: 1 on match, 0 on mismatch | |
1500 | * | |
1501 | * This function can be used to match the Requested Device Type attribute in | |
1502 | * WPS IE with the local device types for deciding whether to reply to a Probe | |
1503 | * Request frame. | |
1504 | */ | |
1505 | int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps) | |
1506 | { | |
1507 | struct wps_parse_attr attr; | |
1508 | size_t i; | |
1509 | ||
1510 | if (wps_parse_msg(wps, &attr)) | |
1511 | return 1; /* assume no Requested Device Type attributes */ | |
1512 | ||
1513 | if (attr.num_req_dev_type == 0) | |
1514 | return 1; /* no Requested Device Type attributes -> match */ | |
1515 | ||
1516 | if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type, | |
1517 | attr.num_req_dev_type)) | |
1518 | return 1; /* Own Primary Device Type matches */ | |
1519 | ||
1520 | for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) | |
1521 | if (dev_type_list_match(p2p->cfg->sec_dev_type[i], | |
1522 | attr.req_dev_type, | |
1523 | attr.num_req_dev_type)) | |
1524 | return 1; /* Own Secondary Device Type matches */ | |
1525 | ||
1526 | /* No matching device type found */ | |
1527 | return 0; | |
1528 | } | |
1529 | ||
1530 | ||
1531 | struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p) | |
1532 | { | |
1533 | struct wpabuf *buf; | |
1534 | u8 *len; | |
1535 | ||
1536 | buf = wpabuf_alloc(1000); | |
1537 | if (buf == NULL) | |
1538 | return NULL; | |
1539 | ||
1540 | /* TODO: add more info into WPS IE; maybe get from WPS module? */ | |
1541 | p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1); | |
1542 | ||
1543 | /* P2P IE */ | |
1544 | len = p2p_buf_add_ie_hdr(buf); | |
1545 | p2p_buf_add_capability(buf, p2p->dev_capab, 0); | |
1546 | if (p2p->ext_listen_interval) | |
1547 | p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period, | |
1548 | p2p->ext_listen_interval); | |
1549 | p2p_buf_add_device_info(buf, p2p, NULL); | |
1550 | p2p_buf_update_ie_hdr(buf, len); | |
1551 | ||
1552 | return buf; | |
1553 | } | |
1554 | ||
1555 | ||
1556 | static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *ie, | |
1557 | size_t ie_len) | |
1558 | { | |
1559 | struct ieee802_11_elems elems; | |
1560 | struct wpabuf *buf; | |
1561 | struct ieee80211_mgmt *resp; | |
1562 | struct wpabuf *wps; | |
1563 | struct wpabuf *ies; | |
1564 | ||
1565 | if (!p2p->in_listen || !p2p->drv_in_listen) { | |
1566 | /* not in Listen state - ignore Probe Request */ | |
1567 | return; | |
1568 | } | |
1569 | ||
1570 | if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) == | |
1571 | ParseFailed) { | |
1572 | /* Ignore invalid Probe Request frames */ | |
1573 | return; | |
1574 | } | |
1575 | ||
1576 | if (elems.p2p == NULL) { | |
1577 | /* not a P2P probe - ignore it */ | |
1578 | return; | |
1579 | } | |
1580 | ||
1581 | if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN || | |
1582 | os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) != | |
1583 | 0) { | |
1584 | /* not using P2P Wildcard SSID - ignore */ | |
1585 | return; | |
1586 | } | |
1587 | ||
1588 | /* Check Requested Device Type match */ | |
1589 | wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA); | |
1590 | if (wps && !p2p_match_dev_type(p2p, wps)) { | |
1591 | wpabuf_free(wps); | |
1592 | /* No match with Requested Device Type */ | |
1593 | return; | |
1594 | } | |
1595 | wpabuf_free(wps); | |
1596 | ||
1597 | if (!p2p->cfg->send_probe_resp) | |
1598 | return; /* Response generated elsewhere */ | |
1599 | ||
1600 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1601 | "P2P: Reply to P2P Probe Request in Listen state"); | |
1602 | ||
1603 | /* | |
1604 | * We do not really have a specific BSS that this frame is advertising, | |
1605 | * so build a frame that has some information in valid format. This is | |
1606 | * really only used for discovery purposes, not to learn exact BSS | |
1607 | * parameters. | |
1608 | */ | |
1609 | ies = p2p_build_probe_resp_ies(p2p); | |
1610 | if (ies == NULL) | |
1611 | return; | |
1612 | ||
1613 | buf = wpabuf_alloc(200 + wpabuf_len(ies)); | |
1614 | if (buf == NULL) { | |
1615 | wpabuf_free(ies); | |
1616 | return; | |
1617 | } | |
1618 | ||
1619 | resp = NULL; | |
1620 | resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp); | |
1621 | ||
1622 | resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) | | |
1623 | (WLAN_FC_STYPE_PROBE_RESP << 4)); | |
1624 | os_memcpy(resp->da, addr, ETH_ALEN); | |
1625 | os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN); | |
1626 | os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN); | |
1627 | resp->u.probe_resp.beacon_int = host_to_le16(100); | |
1628 | /* hardware or low-level driver will setup seq_ctrl and timestamp */ | |
1629 | resp->u.probe_resp.capab_info = | |
1630 | host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE | | |
1631 | WLAN_CAPABILITY_PRIVACY | | |
1632 | WLAN_CAPABILITY_SHORT_SLOT_TIME); | |
1633 | ||
1634 | wpabuf_put_u8(buf, WLAN_EID_SSID); | |
1635 | wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN); | |
1636 | wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); | |
1637 | ||
1638 | wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES); | |
1639 | wpabuf_put_u8(buf, 8); | |
1640 | wpabuf_put_u8(buf, (60 / 5) | 0x80); | |
1641 | wpabuf_put_u8(buf, 90 / 5); | |
1642 | wpabuf_put_u8(buf, (120 / 5) | 0x80); | |
1643 | wpabuf_put_u8(buf, 180 / 5); | |
1644 | wpabuf_put_u8(buf, (240 / 5) | 0x80); | |
1645 | wpabuf_put_u8(buf, 360 / 5); | |
1646 | wpabuf_put_u8(buf, 480 / 5); | |
1647 | wpabuf_put_u8(buf, 540 / 5); | |
1648 | ||
1649 | wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS); | |
1650 | wpabuf_put_u8(buf, 1); | |
1651 | wpabuf_put_u8(buf, p2p->cfg->channel); | |
1652 | ||
1653 | wpabuf_put_buf(buf, ies); | |
1654 | wpabuf_free(ies); | |
1655 | ||
1656 | p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf); | |
1657 | ||
1658 | wpabuf_free(buf); | |
1659 | } | |
1660 | ||
1661 | ||
1662 | int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *ie, | |
1663 | size_t ie_len) | |
1664 | { | |
1665 | p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len); | |
1666 | ||
1667 | p2p_reply_probe(p2p, addr, ie, ie_len); | |
1668 | ||
1669 | if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) && | |
1670 | p2p->go_neg_peer && | |
1671 | os_memcmp(addr, p2p->go_neg_peer->p2p_device_addr, ETH_ALEN) == 0) | |
1672 | { | |
1673 | /* Received a Probe Request from GO Negotiation peer */ | |
1674 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1675 | "P2P: Found GO Negotiation peer - try to start GO " | |
1676 | "negotiation from timeout"); | |
1677 | eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL); | |
1678 | return 1; | |
1679 | } | |
1680 | ||
1681 | if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) && | |
1682 | p2p->invite_peer && | |
1683 | os_memcmp(addr, p2p->invite_peer->p2p_device_addr, ETH_ALEN) == 0) | |
1684 | { | |
1685 | /* Received a Probe Request from Invite peer */ | |
1686 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1687 | "P2P: Found Invite peer - try to start Invite from " | |
1688 | "timeout"); | |
1689 | eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL); | |
1690 | return 1; | |
1691 | } | |
1692 | ||
1693 | return 0; | |
1694 | } | |
1695 | ||
1696 | ||
1697 | static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid, | |
4c08c0bd | 1698 | u8 *buf, size_t len, struct wpabuf *p2p_ie) |
b22128ef JM |
1699 | { |
1700 | struct wpabuf *tmp; | |
1701 | u8 *lpos; | |
1702 | size_t tmplen; | |
1703 | int res; | |
72044390 | 1704 | u8 group_capab; |
b22128ef | 1705 | |
4c08c0bd JM |
1706 | if (p2p_ie == NULL) |
1707 | return 0; /* WLAN AP is not a P2P manager */ | |
b22128ef JM |
1708 | |
1709 | /* | |
1710 | * (Re)Association Request - P2P IE | |
1711 | * P2P Capability attribute (shall be present) | |
4c08c0bd JM |
1712 | * P2P Interface attribute (present if concurrent device and |
1713 | * P2P Management is enabled) | |
b22128ef JM |
1714 | */ |
1715 | tmp = wpabuf_alloc(200); | |
1716 | if (tmp == NULL) | |
1717 | return -1; | |
1718 | ||
1719 | lpos = p2p_buf_add_ie_hdr(tmp); | |
72044390 JM |
1720 | group_capab = 0; |
1721 | if (p2p->num_groups > 0) { | |
1722 | group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER; | |
1723 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && | |
1724 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) && | |
1725 | p2p->cross_connect) | |
1726 | group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; | |
1727 | } | |
1728 | p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab); | |
4c08c0bd JM |
1729 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && |
1730 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED)) | |
b22128ef JM |
1731 | p2p_buf_add_p2p_interface(tmp, p2p); |
1732 | p2p_buf_update_ie_hdr(tmp, lpos); | |
1733 | ||
1734 | tmplen = wpabuf_len(tmp); | |
1735 | if (tmplen > len) | |
1736 | res = -1; | |
1737 | else { | |
1738 | os_memcpy(buf, wpabuf_head(tmp), tmplen); | |
1739 | res = tmplen; | |
1740 | } | |
1741 | wpabuf_free(tmp); | |
1742 | ||
1743 | return res; | |
1744 | } | |
1745 | ||
1746 | ||
1747 | int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf, | |
4c08c0bd | 1748 | size_t len, int p2p_group, struct wpabuf *p2p_ie) |
b22128ef JM |
1749 | { |
1750 | struct wpabuf *tmp; | |
1751 | u8 *lpos; | |
1752 | struct p2p_device *peer; | |
1753 | size_t tmplen; | |
1754 | int res; | |
1755 | ||
1756 | if (!p2p_group) | |
4c08c0bd | 1757 | return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie); |
b22128ef JM |
1758 | |
1759 | /* | |
1760 | * (Re)Association Request - P2P IE | |
1761 | * P2P Capability attribute (shall be present) | |
1762 | * Extended Listen Timing (may be present) | |
1763 | * P2P Device Info attribute (shall be present) | |
1764 | */ | |
1765 | tmp = wpabuf_alloc(200); | |
1766 | if (tmp == NULL) | |
1767 | return -1; | |
1768 | ||
1769 | peer = bssid ? p2p_get_device(p2p, bssid) : NULL; | |
1770 | ||
1771 | lpos = p2p_buf_add_ie_hdr(tmp); | |
1772 | p2p_buf_add_capability(tmp, p2p->dev_capab, 0); | |
5be5305b JM |
1773 | if (p2p->ext_listen_interval) |
1774 | p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period, | |
1775 | p2p->ext_listen_interval); | |
b22128ef JM |
1776 | p2p_buf_add_device_info(tmp, p2p, peer); |
1777 | p2p_buf_update_ie_hdr(tmp, lpos); | |
1778 | ||
1779 | tmplen = wpabuf_len(tmp); | |
1780 | if (tmplen > len) | |
1781 | res = -1; | |
1782 | else { | |
1783 | os_memcpy(buf, wpabuf_head(tmp), tmplen); | |
1784 | res = tmplen; | |
1785 | } | |
1786 | wpabuf_free(tmp); | |
1787 | ||
1788 | return res; | |
1789 | } | |
1790 | ||
1791 | ||
1792 | int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end) | |
1793 | { | |
1794 | struct wpabuf *p2p_ie; | |
1795 | int ret; | |
1796 | ||
1797 | p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE); | |
1798 | if (p2p_ie == NULL) | |
1799 | return 0; | |
1800 | ||
1801 | ret = p2p_attr_text(p2p_ie, buf, end); | |
1802 | wpabuf_free(p2p_ie); | |
1803 | return ret; | |
1804 | } | |
1805 | ||
1806 | ||
1807 | static void p2p_clear_go_neg(struct p2p_data *p2p) | |
1808 | { | |
1809 | p2p->go_neg_peer = NULL; | |
1810 | p2p_clear_timeout(p2p); | |
1811 | p2p_set_state(p2p, P2P_IDLE); | |
1812 | } | |
1813 | ||
1814 | ||
1815 | void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr) | |
1816 | { | |
1817 | if (p2p->go_neg_peer == NULL) { | |
1818 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1819 | "P2P: No pending Group Formation - " | |
1820 | "ignore WPS registration success notification"); | |
1821 | return; /* No pending Group Formation */ | |
1822 | } | |
1823 | ||
1824 | if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) != | |
1825 | 0) { | |
1826 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1827 | "P2P: Ignore WPS registration success notification " | |
1828 | "for " MACSTR " (GO Negotiation peer " MACSTR ")", | |
1829 | MAC2STR(mac_addr), | |
1830 | MAC2STR(p2p->go_neg_peer->intended_addr)); | |
1831 | return; /* Ignore unexpected peer address */ | |
1832 | } | |
1833 | ||
1834 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1835 | "P2P: Group Formation completed successfully with " MACSTR, | |
1836 | MAC2STR(mac_addr)); | |
1837 | ||
1838 | p2p_clear_go_neg(p2p); | |
1839 | } | |
1840 | ||
1841 | ||
1842 | void p2p_group_formation_failed(struct p2p_data *p2p) | |
1843 | { | |
1844 | if (p2p->go_neg_peer == NULL) { | |
1845 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1846 | "P2P: No pending Group Formation - " | |
1847 | "ignore group formation failure notification"); | |
1848 | return; /* No pending Group Formation */ | |
1849 | } | |
1850 | ||
1851 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1852 | "P2P: Group Formation failed with " MACSTR, | |
1853 | MAC2STR(p2p->go_neg_peer->intended_addr)); | |
1854 | ||
1855 | p2p_clear_go_neg(p2p); | |
1856 | } | |
1857 | ||
1858 | ||
1859 | struct p2p_data * p2p_init(const struct p2p_config *cfg) | |
1860 | { | |
1861 | struct p2p_data *p2p; | |
1862 | ||
1863 | if (cfg->max_peers < 1) | |
1864 | return NULL; | |
1865 | ||
1866 | p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg)); | |
1867 | if (p2p == NULL) | |
1868 | return NULL; | |
1869 | p2p->cfg = (struct p2p_config *) (p2p + 1); | |
1870 | os_memcpy(p2p->cfg, cfg, sizeof(*cfg)); | |
1871 | if (cfg->dev_name) | |
1872 | p2p->cfg->dev_name = os_strdup(cfg->dev_name); | |
1873 | ||
1874 | p2p->min_disc_int = 1; | |
1875 | p2p->max_disc_int = 3; | |
1876 | ||
1877 | os_get_random(&p2p->next_tie_breaker, 1); | |
1878 | p2p->next_tie_breaker &= 0x01; | |
1879 | if (cfg->sd_request) | |
1880 | p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY; | |
1881 | p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE; | |
1882 | if (cfg->concurrent_operations) | |
1883 | p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER; | |
1884 | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
1885 | ||
1886 | dl_list_init(&p2p->devices); | |
1887 | ||
1888 | eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0, | |
1889 | p2p_expiration_timeout, p2p, NULL); | |
1890 | ||
1891 | return p2p; | |
1892 | } | |
1893 | ||
1894 | ||
1895 | void p2p_deinit(struct p2p_data *p2p) | |
1896 | { | |
1897 | eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL); | |
1898 | eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); | |
1899 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
1900 | p2p_flush(p2p); | |
1901 | os_free(p2p->cfg->dev_name); | |
1902 | os_free(p2p->groups); | |
18708aad | 1903 | wpabuf_free(p2p->sd_resp); |
3f9285ff | 1904 | os_free(p2p->after_scan_tx); |
b22128ef JM |
1905 | os_free(p2p); |
1906 | } | |
1907 | ||
1908 | ||
1909 | void p2p_flush(struct p2p_data *p2p) | |
1910 | { | |
1911 | struct p2p_device *dev, *prev; | |
1912 | p2p_clear_timeout(p2p); | |
1913 | p2p_set_state(p2p, P2P_IDLE); | |
1914 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; | |
1915 | p2p->go_neg_peer = NULL; | |
1916 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); | |
1917 | dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device, | |
1918 | list) { | |
1919 | dl_list_del(&dev->list); | |
1920 | p2p_device_free(p2p, dev); | |
1921 | } | |
1922 | p2p_free_sd_queries(p2p); | |
f44ae207 JM |
1923 | os_free(p2p->after_scan_tx); |
1924 | p2p->after_scan_tx = NULL; | |
b22128ef JM |
1925 | } |
1926 | ||
1927 | ||
9d562b79 SS |
1928 | int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr) |
1929 | { | |
1930 | struct p2p_device *dev; | |
1931 | ||
1932 | dev = p2p_get_device(p2p, addr); | |
1933 | if (dev == NULL) | |
1934 | return -1; | |
1935 | ||
1936 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR, | |
1937 | MAC2STR(addr)); | |
1938 | ||
1939 | if (p2p->go_neg_peer == dev) | |
1940 | p2p->go_neg_peer = NULL; | |
1941 | ||
1942 | dev->wps_method = WPS_NOT_READY; | |
1943 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; | |
1944 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; | |
1945 | ||
1946 | /* Check if after_scan_tx is for this peer. If so free it */ | |
1947 | if (p2p->after_scan_tx && | |
1948 | os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) { | |
1949 | os_free(p2p->after_scan_tx); | |
1950 | p2p->after_scan_tx = NULL; | |
1951 | } | |
1952 | ||
1953 | return 0; | |
1954 | } | |
1955 | ||
1956 | ||
b22128ef JM |
1957 | int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name) |
1958 | { | |
1959 | os_free(p2p->cfg->dev_name); | |
1960 | if (dev_name) { | |
1961 | p2p->cfg->dev_name = os_strdup(dev_name); | |
1962 | if (p2p->cfg->dev_name == NULL) | |
1963 | return -1; | |
1964 | } else | |
1965 | p2p->cfg->dev_name = NULL; | |
1966 | return 0; | |
1967 | } | |
1968 | ||
1969 | ||
1970 | int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type) | |
1971 | { | |
1972 | os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8); | |
1973 | return 0; | |
1974 | } | |
1975 | ||
1976 | ||
1977 | int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8], | |
1978 | size_t num_dev_types) | |
1979 | { | |
1980 | if (num_dev_types > P2P_SEC_DEVICE_TYPES) | |
1981 | num_dev_types = P2P_SEC_DEVICE_TYPES; | |
1982 | p2p->cfg->num_sec_dev_types = num_dev_types; | |
1983 | os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8); | |
1984 | return 0; | |
1985 | } | |
1986 | ||
1987 | ||
1988 | int p2p_set_country(struct p2p_data *p2p, const char *country) | |
1989 | { | |
1990 | os_memcpy(p2p->cfg->country, country, 3); | |
1991 | return 0; | |
1992 | } | |
1993 | ||
1994 | ||
1995 | void p2p_continue_find(struct p2p_data *p2p) | |
1996 | { | |
1997 | struct p2p_device *dev; | |
1998 | p2p_set_state(p2p, P2P_SEARCH); | |
1999 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
2000 | if (dev->flags & P2P_DEV_SD_SCHEDULE) { | |
2001 | if (p2p_start_sd(p2p, dev) == 0) | |
2002 | return; | |
2003 | else | |
2004 | break; | |
10c4edde JM |
2005 | } else if (dev->req_config_methods && |
2006 | !(dev->flags & P2P_DEV_PD_FOR_JOIN)) { | |
2007 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send " | |
2008 | "pending Provisioning Discovery Request to " | |
2009 | MACSTR " (config methods 0x%x)", | |
2010 | MAC2STR(dev->p2p_device_addr), | |
2011 | dev->req_config_methods); | |
b22128ef JM |
2012 | if (p2p_send_prov_disc_req(p2p, dev, 0) == 0) |
2013 | return; | |
2014 | } | |
2015 | } | |
2016 | ||
2017 | p2p_listen_in_find(p2p); | |
2018 | } | |
2019 | ||
2020 | ||
2021 | static void p2p_sd_cb(struct p2p_data *p2p, int success) | |
2022 | { | |
2023 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2024 | "P2P: Service Discovery Query TX callback: success=%d", | |
2025 | success); | |
2026 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
2027 | ||
2028 | if (!success) { | |
2029 | if (p2p->sd_peer) { | |
2030 | p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE; | |
2031 | p2p->sd_peer = NULL; | |
2032 | } | |
2033 | p2p_continue_find(p2p); | |
2034 | return; | |
2035 | } | |
2036 | ||
2037 | if (p2p->sd_peer == NULL) { | |
2038 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2039 | "P2P: No SD peer entry known"); | |
2040 | p2p_continue_find(p2p); | |
2041 | return; | |
2042 | } | |
2043 | ||
2044 | /* Wait for response from the peer */ | |
2045 | p2p_set_state(p2p, P2P_SD_DURING_FIND); | |
2046 | p2p_set_timeout(p2p, 0, 200000); | |
2047 | } | |
2048 | ||
2049 | ||
2050 | static void p2p_prov_disc_cb(struct p2p_data *p2p, int success) | |
2051 | { | |
2052 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2053 | "P2P: Provision Discovery Request TX callback: success=%d", | |
2054 | success); | |
2055 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
2056 | ||
2057 | if (!success) { | |
2058 | if (p2p->state != P2P_IDLE) | |
2059 | p2p_continue_find(p2p); | |
2060 | return; | |
2061 | } | |
2062 | ||
2063 | /* Wait for response from the peer */ | |
2064 | if (p2p->state == P2P_SEARCH) | |
2065 | p2p_set_state(p2p, P2P_PD_DURING_FIND); | |
2066 | p2p_set_timeout(p2p, 0, 200000); | |
2067 | } | |
2068 | ||
2069 | ||
2070 | int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq, | |
2071 | int level, const u8 *ies, size_t ies_len) | |
2072 | { | |
2073 | p2p_add_device(p2p, bssid, freq, level, ies, ies_len); | |
2074 | ||
2075 | if (p2p->go_neg_peer && p2p->state == P2P_SEARCH && | |
2076 | os_memcmp(p2p->go_neg_peer->p2p_device_addr, bssid, ETH_ALEN) == 0) | |
2077 | { | |
2078 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2079 | "P2P: Found GO Negotiation peer - try to start GO " | |
2080 | "negotiation"); | |
2081 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
2082 | return 1; | |
2083 | } | |
2084 | ||
2085 | return 0; | |
2086 | } | |
2087 | ||
2088 | ||
2089 | void p2p_scan_res_handled(struct p2p_data *p2p) | |
2090 | { | |
2091 | if (!p2p->p2p_scan_running) { | |
2092 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not " | |
2093 | "running, but scan results received"); | |
2094 | } | |
2095 | p2p->p2p_scan_running = 0; | |
2096 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
2097 | ||
2098 | if (p2p_run_after_scan(p2p)) | |
2099 | return; | |
2100 | if (p2p->state == P2P_SEARCH) | |
2101 | p2p_continue_find(p2p); | |
2102 | } | |
2103 | ||
2104 | ||
2105 | void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies) | |
2106 | { | |
2107 | u8 *len = p2p_buf_add_ie_hdr(ies); | |
2108 | p2p_buf_add_capability(ies, p2p->dev_capab, 0); | |
2109 | if (p2p->cfg->reg_class && p2p->cfg->channel) | |
2110 | p2p_buf_add_listen_channel(ies, p2p->cfg->country, | |
2111 | p2p->cfg->reg_class, | |
2112 | p2p->cfg->channel); | |
2113 | if (p2p->ext_listen_interval) | |
2114 | p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period, | |
2115 | p2p->ext_listen_interval); | |
2116 | /* TODO: p2p_buf_add_operating_channel() if GO */ | |
2117 | p2p_buf_update_ie_hdr(ies, len); | |
2118 | } | |
2119 | ||
2120 | ||
2121 | int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end) | |
2122 | { | |
2123 | return p2p_attr_text(p2p_ie, buf, end); | |
2124 | } | |
2125 | ||
2126 | ||
2127 | static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success) | |
2128 | { | |
2129 | struct p2p_device *dev = p2p->go_neg_peer; | |
2130 | ||
2131 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2132 | "P2P: GO Negotiation Request TX callback: success=%d", | |
2133 | success); | |
2134 | ||
2135 | if (dev == NULL) { | |
2136 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2137 | "P2P: No pending GO Negotiation"); | |
2138 | return; | |
2139 | } | |
2140 | ||
2141 | if (success) { | |
2142 | dev->go_neg_req_sent++; | |
2143 | if (dev->flags & P2P_DEV_USER_REJECTED) { | |
2144 | p2p_set_state(p2p, P2P_IDLE); | |
2145 | return; | |
2146 | } | |
2147 | } | |
2148 | ||
2149 | if (!success && | |
2150 | (dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) && | |
2151 | !is_zero_ether_addr(dev->member_in_go_dev)) { | |
2152 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2153 | "P2P: Peer " MACSTR " did not acknowledge request - " | |
2154 | "try to use device discoverability through its GO", | |
2155 | MAC2STR(dev->p2p_device_addr)); | |
2156 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
2157 | p2p_send_dev_disc_req(p2p, dev); | |
2158 | return; | |
2159 | } | |
2160 | ||
2161 | /* | |
2162 | * Use P2P find, if needed, to find the other device from its listen | |
2163 | * channel. | |
2164 | */ | |
2165 | p2p_set_state(p2p, P2P_CONNECT); | |
2166 | p2p_set_timeout(p2p, 0, 100000); | |
2167 | } | |
2168 | ||
2169 | ||
2170 | static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success) | |
2171 | { | |
2172 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2173 | "P2P: GO Negotiation Response TX callback: success=%d", | |
2174 | success); | |
2175 | if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) { | |
2176 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2177 | "P2P: Ignore TX callback event - GO Negotiation is " | |
2178 | "not running anymore"); | |
2179 | return; | |
2180 | } | |
2181 | p2p_set_state(p2p, P2P_CONNECT); | |
2182 | p2p_set_timeout(p2p, 0, 100000); | |
2183 | } | |
2184 | ||
2185 | ||
2186 | static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success) | |
2187 | { | |
2188 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2189 | "P2P: GO Negotiation Response (failure) TX callback: " | |
2190 | "success=%d", success); | |
fbe70272 JM |
2191 | if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) { |
2192 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, | |
2193 | p2p->go_neg_peer->status); | |
2194 | } | |
b22128ef JM |
2195 | } |
2196 | ||
2197 | ||
93b7ddd0 JM |
2198 | static void p2p_go_neg_conf_cb(struct p2p_data *p2p, |
2199 | enum p2p_send_action_result result) | |
b22128ef JM |
2200 | { |
2201 | struct p2p_device *dev; | |
2202 | ||
2203 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
93b7ddd0 JM |
2204 | "P2P: GO Negotiation Confirm TX callback: result=%d", |
2205 | result); | |
b22128ef | 2206 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
93b7ddd0 JM |
2207 | if (result == P2P_SEND_ACTION_FAILED) { |
2208 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); | |
2209 | return; | |
2210 | } | |
2211 | if (result == P2P_SEND_ACTION_NO_ACK) { | |
b22128ef JM |
2212 | /* |
2213 | * It looks like the TX status for GO Negotiation Confirm is | |
2214 | * often showing failure even when the peer has actually | |
2215 | * received the frame. Since the peer may change channels | |
2216 | * immediately after having received the frame, we may not see | |
2217 | * an Ack for retries, so just dropping a single frame may | |
2218 | * trigger this. To allow the group formation to succeed if the | |
2219 | * peer did indeed receive the frame, continue regardless of | |
2220 | * the TX status. | |
2221 | */ | |
2222 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2223 | "P2P: Assume GO Negotiation Confirm TX was actually " | |
2224 | "received by the peer even though Ack was not " | |
2225 | "reported"); | |
2226 | } | |
2227 | ||
2228 | dev = p2p->go_neg_peer; | |
2229 | if (dev == NULL) | |
2230 | return; | |
2231 | ||
2232 | p2p_go_complete(p2p, dev); | |
2233 | } | |
2234 | ||
2235 | ||
2236 | void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst, | |
93b7ddd0 JM |
2237 | const u8 *src, const u8 *bssid, |
2238 | enum p2p_send_action_result result) | |
b22128ef JM |
2239 | { |
2240 | enum p2p_pending_action_state state; | |
93b7ddd0 | 2241 | int success; |
b22128ef JM |
2242 | |
2243 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2244 | "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR | |
93b7ddd0 | 2245 | " src=" MACSTR " bssid=" MACSTR " result=%d", |
b22128ef | 2246 | p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src), |
93b7ddd0 JM |
2247 | MAC2STR(bssid), result); |
2248 | success = result == P2P_SEND_ACTION_SUCCESS; | |
b22128ef JM |
2249 | state = p2p->pending_action_state; |
2250 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
2251 | switch (state) { | |
2252 | case P2P_NO_PENDING_ACTION: | |
2253 | break; | |
2254 | case P2P_PENDING_GO_NEG_REQUEST: | |
2255 | p2p_go_neg_req_cb(p2p, success); | |
2256 | break; | |
2257 | case P2P_PENDING_GO_NEG_RESPONSE: | |
2258 | p2p_go_neg_resp_cb(p2p, success); | |
2259 | break; | |
2260 | case P2P_PENDING_GO_NEG_RESPONSE_FAILURE: | |
2261 | p2p_go_neg_resp_failure_cb(p2p, success); | |
2262 | break; | |
2263 | case P2P_PENDING_GO_NEG_CONFIRM: | |
93b7ddd0 | 2264 | p2p_go_neg_conf_cb(p2p, result); |
b22128ef JM |
2265 | break; |
2266 | case P2P_PENDING_SD: | |
2267 | p2p_sd_cb(p2p, success); | |
2268 | break; | |
2269 | case P2P_PENDING_PD: | |
2270 | p2p_prov_disc_cb(p2p, success); | |
2271 | break; | |
2272 | case P2P_PENDING_INVITATION_REQUEST: | |
2273 | p2p_invitation_req_cb(p2p, success); | |
2274 | break; | |
2275 | case P2P_PENDING_INVITATION_RESPONSE: | |
2276 | p2p_invitation_resp_cb(p2p, success); | |
2277 | break; | |
2278 | case P2P_PENDING_DEV_DISC_REQUEST: | |
2279 | p2p_dev_disc_req_cb(p2p, success); | |
2280 | break; | |
2281 | case P2P_PENDING_DEV_DISC_RESPONSE: | |
2282 | p2p_dev_disc_resp_cb(p2p, success); | |
2283 | break; | |
2284 | case P2P_PENDING_GO_DISC_REQ: | |
2285 | p2p_go_disc_req_cb(p2p, success); | |
2286 | break; | |
2287 | } | |
2288 | } | |
2289 | ||
2290 | ||
2291 | void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq, | |
2292 | unsigned int duration) | |
2293 | { | |
2294 | if (freq == p2p->pending_client_disc_freq) { | |
2295 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2296 | "P2P: Client discoverability remain-awake completed"); | |
2297 | p2p->pending_client_disc_freq = 0; | |
2298 | return; | |
2299 | } | |
2300 | ||
2301 | if (freq != p2p->pending_listen_freq) { | |
2302 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2303 | "P2P: Unexpected listen callback for freq=%u " | |
2304 | "duration=%u (pending_listen_freq=%u)", | |
2305 | freq, duration, p2p->pending_listen_freq); | |
2306 | return; | |
2307 | } | |
2308 | ||
2309 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2310 | "P2P: Starting Listen timeout(%u,%u) on freq=%u based on " | |
2311 | "callback", | |
2312 | p2p->pending_listen_sec, p2p->pending_listen_usec, | |
2313 | p2p->pending_listen_freq); | |
2314 | p2p->in_listen = 1; | |
0b8889d8 | 2315 | p2p->drv_in_listen = freq; |
b22128ef JM |
2316 | if (p2p->pending_listen_sec || p2p->pending_listen_usec) { |
2317 | /* | |
2318 | * Add 20 msec extra wait to avoid race condition with driver | |
2319 | * remain-on-channel end event, i.e., give driver more time to | |
2320 | * complete the operation before our timeout expires. | |
2321 | */ | |
2322 | p2p_set_timeout(p2p, p2p->pending_listen_sec, | |
2323 | p2p->pending_listen_usec + 20000); | |
2324 | } | |
2325 | ||
2326 | p2p->pending_listen_freq = 0; | |
2327 | } | |
2328 | ||
2329 | ||
2330 | int p2p_listen_end(struct p2p_data *p2p, unsigned int freq) | |
2331 | { | |
2332 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen " | |
2333 | "state (freq=%u)", freq); | |
2334 | p2p->drv_in_listen = 0; | |
2335 | if (p2p->in_listen) | |
2336 | return 0; /* Internal timeout will trigger the next step */ | |
2337 | ||
2338 | if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) { | |
2339 | p2p_set_state(p2p, P2P_CONNECT); | |
2340 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
2341 | return 1; | |
2342 | } else if (p2p->state == P2P_SEARCH) { | |
2343 | p2p_search(p2p); | |
2344 | return 1; | |
2345 | } | |
2346 | ||
2347 | return 0; | |
2348 | } | |
2349 | ||
2350 | ||
2351 | static void p2p_timeout_connect(struct p2p_data *p2p) | |
2352 | { | |
2353 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
2354 | p2p_set_state(p2p, P2P_CONNECT_LISTEN); | |
2355 | p2p_listen_in_find(p2p); | |
2356 | } | |
2357 | ||
2358 | ||
2359 | static void p2p_timeout_connect_listen(struct p2p_data *p2p) | |
2360 | { | |
2361 | if (p2p->go_neg_peer) { | |
2362 | if (p2p->drv_in_listen) { | |
2363 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is " | |
2364 | "still in Listen state; wait for it to " | |
2365 | "complete"); | |
2366 | return; | |
2367 | } | |
2368 | p2p_set_state(p2p, P2P_CONNECT); | |
2369 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
2370 | } else | |
2371 | p2p_set_state(p2p, P2P_IDLE); | |
2372 | } | |
2373 | ||
2374 | ||
2375 | static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p) | |
2376 | { | |
2377 | /* | |
2378 | * TODO: could remain constantly in Listen state for some time if there | |
2379 | * are no other concurrent uses for the radio. For now, go to listen | |
2380 | * state once per second to give other uses a chance to use the radio. | |
2381 | */ | |
2382 | p2p_set_state(p2p, P2P_WAIT_PEER_IDLE); | |
2383 | p2p_set_timeout(p2p, 1, 0); | |
2384 | } | |
2385 | ||
2386 | ||
2387 | static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p) | |
2388 | { | |
2389 | struct p2p_device *dev = p2p->go_neg_peer; | |
2390 | ||
2391 | if (dev == NULL) { | |
2392 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2393 | "P2P: Unknown GO Neg peer - stop GO Neg wait"); | |
2394 | return; | |
2395 | } | |
2396 | ||
2397 | dev->wait_count++; | |
2398 | if (dev->wait_count >= 120) { | |
2399 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2400 | "P2P: Timeout on waiting peer to become ready for GO " | |
2401 | "Negotiation"); | |
2402 | p2p_go_neg_failed(p2p, dev, -1); | |
2403 | return; | |
2404 | } | |
2405 | ||
2406 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2407 | "P2P: Go to Listen state while waiting for the peer to become " | |
2408 | "ready for GO Negotiation"); | |
2409 | p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT); | |
2410 | p2p_listen_in_find(p2p); | |
2411 | } | |
2412 | ||
2413 | ||
2414 | static void p2p_timeout_sd_during_find(struct p2p_data *p2p) | |
2415 | { | |
2416 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2417 | "P2P: Service Discovery Query timeout"); | |
2418 | if (p2p->sd_peer) { | |
2419 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
2420 | p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE; | |
2421 | p2p->sd_peer = NULL; | |
2422 | } | |
2423 | p2p_continue_find(p2p); | |
2424 | } | |
2425 | ||
2426 | ||
2427 | static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p) | |
2428 | { | |
2429 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2430 | "P2P: Provision Discovery Request timeout"); | |
2431 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
2432 | p2p_continue_find(p2p); | |
2433 | } | |
2434 | ||
2435 | ||
2436 | static void p2p_timeout_invite(struct p2p_data *p2p) | |
2437 | { | |
2438 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
2439 | p2p_set_state(p2p, P2P_INVITE_LISTEN); | |
04d8dad5 JM |
2440 | if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) { |
2441 | /* | |
2442 | * Better remain on operating channel instead of listen channel | |
2443 | * when running a group. | |
2444 | */ | |
2445 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in " | |
2446 | "active GO role - wait on operating channel"); | |
2447 | p2p_set_timeout(p2p, 0, 100000); | |
2448 | return; | |
2449 | } | |
b22128ef JM |
2450 | p2p_listen_in_find(p2p); |
2451 | } | |
2452 | ||
2453 | ||
2454 | static void p2p_timeout_invite_listen(struct p2p_data *p2p) | |
2455 | { | |
2456 | if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) { | |
2457 | p2p_set_state(p2p, P2P_INVITE); | |
2458 | p2p_invite_send(p2p, p2p->invite_peer, | |
2459 | p2p->invite_go_dev_addr); | |
2460 | } else { | |
2461 | if (p2p->invite_peer) { | |
2462 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2463 | "P2P: Invitation Request retry limit reached"); | |
2464 | if (p2p->cfg->invitation_result) | |
2465 | p2p->cfg->invitation_result( | |
2466 | p2p->cfg->cb_ctx, -1, NULL); | |
2467 | } | |
2468 | p2p_set_state(p2p, P2P_IDLE); | |
2469 | } | |
2470 | } | |
2471 | ||
2472 | ||
2473 | static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx) | |
2474 | { | |
2475 | struct p2p_data *p2p = eloop_ctx; | |
2476 | ||
2477 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)", | |
2478 | p2p_state_txt(p2p->state)); | |
2479 | ||
2480 | p2p->in_listen = 0; | |
2481 | ||
2482 | switch (p2p->state) { | |
2483 | case P2P_IDLE: | |
2484 | break; | |
2485 | case P2P_SEARCH: | |
2486 | p2p_search(p2p); | |
2487 | break; | |
2488 | case P2P_CONNECT: | |
2489 | p2p_timeout_connect(p2p); | |
2490 | break; | |
2491 | case P2P_CONNECT_LISTEN: | |
2492 | p2p_timeout_connect_listen(p2p); | |
2493 | break; | |
2494 | case P2P_GO_NEG: | |
2495 | break; | |
2496 | case P2P_LISTEN_ONLY: | |
2497 | if (p2p->ext_listen_only) { | |
2498 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2499 | "P2P: Extended Listen Timing - Listen State " | |
2500 | "completed"); | |
2501 | p2p->ext_listen_only = 0; | |
2502 | p2p_set_state(p2p, P2P_IDLE); | |
2503 | } | |
2504 | break; | |
2505 | case P2P_WAIT_PEER_CONNECT: | |
2506 | p2p_timeout_wait_peer_connect(p2p); | |
2507 | break; | |
2508 | case P2P_WAIT_PEER_IDLE: | |
2509 | p2p_timeout_wait_peer_idle(p2p); | |
2510 | break; | |
2511 | case P2P_SD_DURING_FIND: | |
2512 | p2p_timeout_sd_during_find(p2p); | |
2513 | break; | |
2514 | case P2P_PROVISIONING: | |
2515 | break; | |
2516 | case P2P_PD_DURING_FIND: | |
2517 | p2p_timeout_prov_disc_during_find(p2p); | |
2518 | break; | |
2519 | case P2P_INVITE: | |
2520 | p2p_timeout_invite(p2p); | |
2521 | break; | |
2522 | case P2P_INVITE_LISTEN: | |
2523 | p2p_timeout_invite_listen(p2p); | |
2524 | break; | |
2525 | } | |
2526 | } | |
2527 | ||
2528 | ||
2529 | int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr) | |
2530 | { | |
2531 | struct p2p_device *dev; | |
2532 | ||
2533 | dev = p2p_get_device(p2p, peer_addr); | |
2534 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject " | |
2535 | "connection attempts by peer " MACSTR, MAC2STR(peer_addr)); | |
2536 | if (dev == NULL) { | |
2537 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR | |
2538 | " unknown", MAC2STR(peer_addr)); | |
2539 | return -1; | |
2540 | } | |
2541 | dev->status = P2P_SC_FAIL_REJECTED_BY_USER; | |
2542 | dev->flags |= P2P_DEV_USER_REJECTED; | |
2543 | return 0; | |
2544 | } | |
2545 | ||
2546 | ||
2547 | static const char * p2p_wps_method_text(enum p2p_wps_method method) | |
2548 | { | |
2549 | switch (method) { | |
2550 | case WPS_NOT_READY: | |
2551 | return "not-ready"; | |
2552 | case WPS_PIN_LABEL: | |
2553 | return "Label"; | |
2554 | case WPS_PIN_DISPLAY: | |
2555 | return "Display"; | |
2556 | case WPS_PIN_KEYPAD: | |
2557 | return "Keypad"; | |
2558 | case WPS_PBC: | |
2559 | return "PBC"; | |
2560 | } | |
2561 | ||
2562 | return "??"; | |
2563 | } | |
2564 | ||
2565 | ||
2566 | static const char * p2p_go_state_text(enum p2p_go_state go_state) | |
2567 | { | |
2568 | switch (go_state) { | |
2569 | case UNKNOWN_GO: | |
2570 | return "unknown"; | |
2571 | case LOCAL_GO: | |
2572 | return "local"; | |
2573 | case REMOTE_GO: | |
2574 | return "remote"; | |
2575 | } | |
2576 | ||
2577 | return "??"; | |
2578 | } | |
2579 | ||
2580 | ||
2581 | int p2p_get_peer_info(struct p2p_data *p2p, const u8 *addr, int next, | |
2582 | char *buf, size_t buflen) | |
2583 | { | |
2584 | struct p2p_device *dev; | |
2585 | int res; | |
2586 | char *pos, *end; | |
2587 | struct os_time now; | |
2588 | char devtype[WPS_DEV_TYPE_BUFSIZE]; | |
2589 | ||
2590 | if (addr) | |
2591 | dev = p2p_get_device(p2p, addr); | |
2592 | else | |
2593 | dev = dl_list_first(&p2p->devices, struct p2p_device, list); | |
2594 | ||
2595 | if (dev && next) { | |
2596 | dev = dl_list_first(&dev->list, struct p2p_device, list); | |
2597 | if (&dev->list == &p2p->devices) | |
2598 | dev = NULL; | |
2599 | } | |
2600 | ||
2601 | if (dev == NULL) | |
2602 | return -1; | |
2603 | ||
2604 | pos = buf; | |
2605 | end = buf + buflen; | |
2606 | ||
2607 | res = os_snprintf(pos, end - pos, MACSTR "\n", | |
2608 | MAC2STR(dev->p2p_device_addr)); | |
2609 | if (res < 0 || res >= end - pos) | |
2610 | return pos - buf; | |
2611 | pos += res; | |
2612 | ||
2613 | os_get_time(&now); | |
2614 | res = os_snprintf(pos, end - pos, | |
2615 | "age=%d\n" | |
2616 | "listen_freq=%d\n" | |
2617 | "level=%d\n" | |
2618 | "wps_method=%s\n" | |
2619 | "interface_addr=" MACSTR "\n" | |
2620 | "member_in_go_dev=" MACSTR "\n" | |
2621 | "member_in_go_iface=" MACSTR "\n" | |
2622 | "pri_dev_type=%s\n" | |
2623 | "device_name=%s\n" | |
2624 | "config_methods=0x%x\n" | |
2625 | "dev_capab=0x%x\n" | |
2626 | "group_capab=0x%x\n" | |
2627 | "go_neg_req_sent=%d\n" | |
2628 | "go_state=%s\n" | |
2629 | "dialog_token=%u\n" | |
2630 | "intended_addr=" MACSTR "\n" | |
2631 | "country=%c%c\n" | |
2632 | "oper_freq=%d\n" | |
2633 | "req_config_methods=0x%x\n" | |
10c4edde | 2634 | "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n" |
b22128ef JM |
2635 | "status=%d\n" |
2636 | "wait_count=%u\n" | |
2637 | "invitation_reqs=%u\n", | |
2638 | (int) (now.sec - dev->last_seen.sec), | |
2639 | dev->listen_freq, | |
2640 | dev->level, | |
2641 | p2p_wps_method_text(dev->wps_method), | |
2642 | MAC2STR(dev->interface_addr), | |
2643 | MAC2STR(dev->member_in_go_dev), | |
2644 | MAC2STR(dev->member_in_go_iface), | |
2645 | wps_dev_type_bin2str(dev->pri_dev_type, | |
2646 | devtype, sizeof(devtype)), | |
2647 | dev->device_name, | |
2648 | dev->config_methods, | |
2649 | dev->dev_capab, | |
2650 | dev->group_capab, | |
2651 | dev->go_neg_req_sent, | |
2652 | p2p_go_state_text(dev->go_state), | |
2653 | dev->dialog_token, | |
2654 | MAC2STR(dev->intended_addr), | |
2655 | dev->country[0] ? dev->country[0] : '_', | |
2656 | dev->country[1] ? dev->country[1] : '_', | |
2657 | dev->oper_freq, | |
2658 | dev->req_config_methods, | |
2659 | dev->flags & P2P_DEV_PROBE_REQ_ONLY ? | |
2660 | "[PROBE_REQ_ONLY]" : "", | |
2661 | dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "", | |
2662 | dev->flags & P2P_DEV_NOT_YET_READY ? | |
2663 | "[NOT_YET_READY]" : "", | |
2664 | dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "", | |
2665 | dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" : | |
2666 | "", | |
2667 | dev->flags & P2P_DEV_PD_PEER_DISPLAY ? | |
2668 | "[PD_PEER_DISPLAY]" : "", | |
2669 | dev->flags & P2P_DEV_PD_PEER_KEYPAD ? | |
2670 | "[PD_PEER_KEYPAD]" : "", | |
2671 | dev->flags & P2P_DEV_USER_REJECTED ? | |
2672 | "[USER_REJECTED]" : "", | |
2673 | dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ? | |
2674 | "[PEER_WAITING_RESPONSE]" : "", | |
2675 | dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ? | |
2676 | "[PREFER_PERSISTENT_GROUP]" : "", | |
2677 | dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ? | |
2678 | "[WAIT_GO_NEG_RESPONSE]" : "", | |
2679 | dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ? | |
2680 | "[WAIT_GO_NEG_CONFIRM]" : "", | |
2681 | dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ? | |
2682 | "[GROUP_CLIENT_ONLY]" : "", | |
d5b20a73 | 2683 | dev->flags & P2P_DEV_FORCE_FREQ ? |
10c4edde JM |
2684 | "[FORCE_FREQ]" : "", |
2685 | dev->flags & P2P_DEV_PD_FOR_JOIN ? | |
2686 | "[PD_FOR_JOIN]" : "", | |
b22128ef JM |
2687 | dev->status, |
2688 | dev->wait_count, | |
2689 | dev->invitation_reqs); | |
2690 | if (res < 0 || res >= end - pos) | |
2691 | return pos - buf; | |
2692 | pos += res; | |
2693 | ||
2694 | if (dev->ext_listen_period) { | |
2695 | res = os_snprintf(pos, end - pos, | |
2696 | "ext_listen_period=%u\n" | |
2697 | "ext_listen_interval=%u\n", | |
2698 | dev->ext_listen_period, | |
2699 | dev->ext_listen_interval); | |
2700 | if (res < 0 || res >= end - pos) | |
2701 | return pos - buf; | |
2702 | pos += res; | |
2703 | } | |
2704 | ||
2705 | if (dev->oper_ssid_len) { | |
2706 | res = os_snprintf(pos, end - pos, | |
2707 | "oper_ssid=%s\n", | |
2708 | wpa_ssid_txt(dev->oper_ssid, | |
2709 | dev->oper_ssid_len)); | |
2710 | if (res < 0 || res >= end - pos) | |
2711 | return pos - buf; | |
2712 | pos += res; | |
2713 | } | |
2714 | ||
2715 | return pos - buf; | |
2716 | } | |
2717 | ||
2718 | ||
2719 | void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled) | |
2720 | { | |
2721 | if (enabled) { | |
2722 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client " | |
2723 | "discoverability enabled"); | |
2724 | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
2725 | } else { | |
2726 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client " | |
2727 | "discoverability disabled"); | |
2728 | p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
2729 | } | |
2730 | } | |
2731 | ||
2732 | ||
2733 | static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1, | |
2734 | u32 duration2, u32 interval2) | |
2735 | { | |
2736 | struct wpabuf *req; | |
2737 | struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL; | |
2738 | u8 *len; | |
2739 | ||
2740 | req = wpabuf_alloc(100); | |
2741 | if (req == NULL) | |
2742 | return NULL; | |
2743 | ||
2744 | if (duration1 || interval1) { | |
2745 | os_memset(&desc1, 0, sizeof(desc1)); | |
2746 | desc1.count_type = 1; | |
2747 | desc1.duration = duration1; | |
2748 | desc1.interval = interval1; | |
2749 | ptr1 = &desc1; | |
2750 | ||
2751 | if (duration2 || interval2) { | |
2752 | os_memset(&desc2, 0, sizeof(desc2)); | |
2753 | desc2.count_type = 2; | |
2754 | desc2.duration = duration2; | |
2755 | desc2.interval = interval2; | |
2756 | ptr2 = &desc2; | |
2757 | } | |
2758 | } | |
2759 | ||
2760 | p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1); | |
2761 | len = p2p_buf_add_ie_hdr(req); | |
2762 | p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2); | |
2763 | p2p_buf_update_ie_hdr(req, len); | |
2764 | ||
2765 | return req; | |
2766 | } | |
2767 | ||
2768 | ||
2769 | int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr, | |
2770 | const u8 *own_interface_addr, unsigned int freq, | |
2771 | u32 duration1, u32 interval1, u32 duration2, | |
2772 | u32 interval2) | |
2773 | { | |
2774 | struct wpabuf *req; | |
2775 | ||
2776 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to " | |
2777 | "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u " | |
2778 | "int1=%u dur2=%u int2=%u", | |
2779 | MAC2STR(go_interface_addr), MAC2STR(own_interface_addr), | |
2780 | freq, duration1, interval1, duration2, interval2); | |
2781 | ||
2782 | req = p2p_build_presence_req(duration1, interval1, duration2, | |
2783 | interval2); | |
2784 | if (req == NULL) | |
2785 | return -1; | |
2786 | ||
2787 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3f9285ff JM |
2788 | if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr, |
2789 | go_interface_addr, | |
2790 | wpabuf_head(req), wpabuf_len(req), 200) < 0) { | |
b22128ef JM |
2791 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
2792 | "P2P: Failed to send Action frame"); | |
2793 | } | |
2794 | wpabuf_free(req); | |
2795 | ||
2796 | return 0; | |
2797 | } | |
2798 | ||
2799 | ||
2800 | static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa, | |
2801 | size_t noa_len, u8 dialog_token) | |
2802 | { | |
2803 | struct wpabuf *resp; | |
2804 | u8 *len; | |
2805 | ||
2806 | resp = wpabuf_alloc(100 + noa_len); | |
2807 | if (resp == NULL) | |
2808 | return NULL; | |
2809 | ||
2810 | p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token); | |
2811 | len = p2p_buf_add_ie_hdr(resp); | |
2812 | p2p_buf_add_status(resp, status); | |
2813 | if (noa) { | |
2814 | wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE); | |
2815 | wpabuf_put_le16(resp, noa_len); | |
2816 | wpabuf_put_data(resp, noa, noa_len); | |
2817 | } else | |
2818 | p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL); | |
2819 | p2p_buf_update_ie_hdr(resp, len); | |
2820 | ||
2821 | return resp; | |
2822 | } | |
2823 | ||
2824 | ||
2825 | static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, | |
2826 | const u8 *sa, const u8 *data, size_t len, | |
2827 | int rx_freq) | |
2828 | { | |
2829 | struct p2p_message msg; | |
2830 | u8 status; | |
2831 | struct wpabuf *resp; | |
2832 | size_t g; | |
2833 | struct p2p_group *group = NULL; | |
2834 | int parsed = 0; | |
2835 | u8 noa[50]; | |
2836 | int noa_len; | |
2837 | ||
2838 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2839 | "P2P: Received P2P Action - P2P Presence Request"); | |
2840 | ||
2841 | for (g = 0; g < p2p->num_groups; g++) { | |
2842 | if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]), | |
2843 | ETH_ALEN) == 0) { | |
2844 | group = p2p->groups[g]; | |
2845 | break; | |
2846 | } | |
2847 | } | |
2848 | if (group == NULL) { | |
2849 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2850 | "P2P: Ignore P2P Presence Request for unknown group " | |
2851 | MACSTR, MAC2STR(da)); | |
2852 | return; | |
2853 | } | |
2854 | ||
2855 | if (p2p_parse(data, len, &msg) < 0) { | |
2856 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2857 | "P2P: Failed to parse P2P Presence Request"); | |
2858 | status = P2P_SC_FAIL_INVALID_PARAMS; | |
2859 | goto fail; | |
2860 | } | |
2861 | parsed = 1; | |
2862 | ||
2863 | if (msg.noa == NULL) { | |
2864 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2865 | "P2P: No NoA attribute in P2P Presence Request"); | |
2866 | status = P2P_SC_FAIL_INVALID_PARAMS; | |
2867 | goto fail; | |
2868 | } | |
2869 | ||
2870 | status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len); | |
2871 | ||
2872 | fail: | |
2873 | if (p2p->cfg->get_noa) | |
2874 | noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa, | |
2875 | sizeof(noa)); | |
2876 | else | |
2877 | noa_len = -1; | |
2878 | resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL, | |
2879 | noa_len > 0 ? noa_len : 0, | |
2880 | msg.dialog_token); | |
2881 | if (parsed) | |
2882 | p2p_parse_free(&msg); | |
2883 | if (resp == NULL) | |
2884 | return; | |
2885 | ||
2886 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3f9285ff JM |
2887 | if (p2p_send_action(p2p, rx_freq, sa, da, da, |
2888 | wpabuf_head(resp), wpabuf_len(resp), 200) < 0) { | |
b22128ef JM |
2889 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
2890 | "P2P: Failed to send Action frame"); | |
2891 | } | |
2892 | wpabuf_free(resp); | |
2893 | } | |
2894 | ||
2895 | ||
2896 | static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, | |
2897 | const u8 *sa, const u8 *data, size_t len) | |
2898 | { | |
2899 | struct p2p_message msg; | |
2900 | ||
2901 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2902 | "P2P: Received P2P Action - P2P Presence Response"); | |
2903 | ||
2904 | if (p2p_parse(data, len, &msg) < 0) { | |
2905 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2906 | "P2P: Failed to parse P2P Presence Response"); | |
2907 | return; | |
2908 | } | |
2909 | ||
2910 | if (msg.status == NULL || msg.noa == NULL) { | |
2911 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2912 | "P2P: No Status or NoA attribute in P2P Presence " | |
2913 | "Response"); | |
2914 | p2p_parse_free(&msg); | |
2915 | return; | |
2916 | } | |
2917 | ||
2918 | if (*msg.status) { | |
2919 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2920 | "P2P: P2P Presence Request was rejected: status %u", | |
2921 | *msg.status); | |
2922 | p2p_parse_free(&msg); | |
2923 | return; | |
2924 | } | |
2925 | ||
2926 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2927 | "P2P: P2P Presence Request was accepted"); | |
2928 | wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA", | |
2929 | msg.noa, msg.noa_len); | |
2930 | /* TODO: process NoA */ | |
2931 | p2p_parse_free(&msg); | |
2932 | } | |
2933 | ||
2934 | ||
2935 | static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx) | |
2936 | { | |
2937 | struct p2p_data *p2p = eloop_ctx; | |
2938 | ||
2939 | if (p2p->ext_listen_interval) { | |
2940 | /* Schedule next extended listen timeout */ | |
2941 | eloop_register_timeout(p2p->ext_listen_interval_sec, | |
2942 | p2p->ext_listen_interval_usec, | |
2943 | p2p_ext_listen_timeout, p2p, NULL); | |
2944 | } | |
2945 | ||
f7a69057 JM |
2946 | if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) { |
2947 | /* | |
2948 | * This should not really happen, but it looks like the Listen | |
2949 | * command may fail is something else (e.g., a scan) was | |
2950 | * running at an inconvenient time. As a workaround, allow new | |
2951 | * Extended Listen operation to be started. | |
2952 | */ | |
2953 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous " | |
2954 | "Extended Listen operation had not been completed - " | |
2955 | "try again"); | |
2956 | p2p->ext_listen_only = 0; | |
2957 | p2p_set_state(p2p, P2P_IDLE); | |
2958 | } | |
2959 | ||
b22128ef JM |
2960 | if (p2p->state != P2P_IDLE) { |
2961 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended " | |
2962 | "Listen timeout in active state (%s)", | |
2963 | p2p_state_txt(p2p->state)); | |
2964 | return; | |
2965 | } | |
2966 | ||
2967 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout"); | |
2968 | p2p->ext_listen_only = 1; | |
2969 | if (p2p_listen(p2p, p2p->ext_listen_period) < 0) { | |
2970 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start " | |
2971 | "Listen state for Extended Listen Timing"); | |
2972 | p2p->ext_listen_only = 0; | |
2973 | } | |
2974 | } | |
2975 | ||
2976 | ||
2977 | int p2p_ext_listen(struct p2p_data *p2p, unsigned int period, | |
2978 | unsigned int interval) | |
2979 | { | |
2980 | if (period > 65535 || interval > 65535 || period > interval || | |
2981 | (period == 0 && interval > 0) || (period > 0 && interval == 0)) { | |
2982 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2983 | "P2P: Invalid Extended Listen Timing request: " | |
2984 | "period=%u interval=%u", period, interval); | |
2985 | return -1; | |
2986 | } | |
2987 | ||
2988 | eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); | |
2989 | ||
2990 | if (interval == 0) { | |
2991 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2992 | "P2P: Disabling Extended Listen Timing"); | |
2993 | p2p->ext_listen_period = 0; | |
2994 | p2p->ext_listen_interval = 0; | |
2995 | return 0; | |
2996 | } | |
2997 | ||
2998 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2999 | "P2P: Enabling Extended Listen Timing: period %u msec, " | |
3000 | "interval %u msec", period, interval); | |
3001 | p2p->ext_listen_period = period; | |
3002 | p2p->ext_listen_interval = interval; | |
3003 | p2p->ext_listen_interval_sec = interval / 1000; | |
3004 | p2p->ext_listen_interval_usec = (interval % 1000) * 1000; | |
3005 | ||
3006 | eloop_register_timeout(p2p->ext_listen_interval_sec, | |
3007 | p2p->ext_listen_interval_usec, | |
3008 | p2p_ext_listen_timeout, p2p, NULL); | |
3009 | ||
3010 | return 0; | |
3011 | } | |
3012 | ||
3013 | ||
3014 | void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, | |
3015 | const u8 *ie, size_t ie_len) | |
3016 | { | |
3017 | struct p2p_message msg; | |
3018 | ||
3019 | if (bssid == NULL || ie == NULL) | |
3020 | return; | |
3021 | ||
3022 | os_memset(&msg, 0, sizeof(msg)); | |
3023 | if (p2p_parse_ies(ie, ie_len, &msg)) | |
3024 | return; | |
3025 | if (msg.minor_reason_code == NULL) | |
3026 | return; | |
3027 | ||
3028 | wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, | |
3029 | "P2P: Deauthentication notification BSSID " MACSTR | |
3030 | " reason_code=%u minor_reason_code=%u", | |
3031 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); | |
3032 | ||
3033 | p2p_parse_free(&msg); | |
3034 | } | |
3035 | ||
3036 | ||
3037 | void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, | |
3038 | const u8 *ie, size_t ie_len) | |
3039 | { | |
3040 | struct p2p_message msg; | |
3041 | ||
3042 | if (bssid == NULL || ie == NULL) | |
3043 | return; | |
3044 | ||
3045 | os_memset(&msg, 0, sizeof(msg)); | |
3046 | if (p2p_parse_ies(ie, ie_len, &msg)) | |
3047 | return; | |
3048 | if (msg.minor_reason_code == NULL) | |
3049 | return; | |
3050 | ||
3051 | wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, | |
3052 | "P2P: Disassociation notification BSSID " MACSTR | |
3053 | " reason_code=%u minor_reason_code=%u", | |
3054 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); | |
3055 | ||
3056 | p2p_parse_free(&msg); | |
3057 | } | |
3058 | ||
3059 | ||
3060 | void p2p_set_managed_oper(struct p2p_data *p2p, int enabled) | |
3061 | { | |
3062 | if (enabled) { | |
3063 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P " | |
3064 | "Device operations enabled"); | |
3065 | p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED; | |
3066 | } else { | |
3067 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P " | |
3068 | "Device operations disabled"); | |
3069 | p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED; | |
3070 | } | |
3071 | } | |
3072 | ||
3073 | ||
3074 | int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel) | |
3075 | { | |
3076 | if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0) | |
3077 | return -1; | |
3078 | ||
3079 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: " | |
3080 | "reg_class %u channel %u", reg_class, channel); | |
3081 | p2p->cfg->reg_class = reg_class; | |
3082 | p2p->cfg->channel = channel; | |
3083 | ||
3084 | return 0; | |
3085 | } | |
3086 | ||
3087 | ||
3088 | int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len) | |
3089 | { | |
3090 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len); | |
3091 | if (postfix == NULL) { | |
3092 | p2p->cfg->ssid_postfix_len = 0; | |
3093 | return 0; | |
3094 | } | |
3095 | if (len > sizeof(p2p->cfg->ssid_postfix)) | |
3096 | return -1; | |
3097 | os_memcpy(p2p->cfg->ssid_postfix, postfix, len); | |
3098 | p2p->cfg->ssid_postfix_len = len; | |
3099 | return 0; | |
3100 | } | |
3101 | ||
3102 | ||
3103 | int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr, | |
3104 | u8 *iface_addr) | |
3105 | { | |
3106 | struct p2p_device *dev = p2p_get_device(p2p, dev_addr); | |
3107 | if (dev == NULL || is_zero_ether_addr(dev->interface_addr)) | |
3108 | return -1; | |
3109 | os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN); | |
3110 | return 0; | |
3111 | } | |
80c9582a JM |
3112 | |
3113 | ||
4147a2cc JM |
3114 | int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr, |
3115 | u8 *dev_addr) | |
3116 | { | |
3117 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); | |
3118 | if (dev == NULL) | |
3119 | return -1; | |
3120 | os_memcpy(dev_addr, dev->p2p_device_addr, ETH_ALEN); | |
3121 | return 0; | |
3122 | } | |
3123 | ||
3124 | ||
80c9582a JM |
3125 | void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr) |
3126 | { | |
3127 | os_memcpy(p2p->peer_filter, addr, ETH_ALEN); | |
3128 | if (is_zero_ether_addr(p2p->peer_filter)) | |
3129 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer " | |
3130 | "filter"); | |
3131 | else | |
3132 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer " | |
3133 | "filter for " MACSTR, MAC2STR(p2p->peer_filter)); | |
3134 | } | |
72044390 JM |
3135 | |
3136 | ||
3137 | void p2p_set_cross_connect(struct p2p_data *p2p, int enabled) | |
3138 | { | |
3139 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s", | |
3140 | enabled ? "enabled" : "disabled"); | |
3141 | if (p2p->cross_connect == enabled) | |
3142 | return; | |
3143 | p2p->cross_connect = enabled; | |
3144 | /* TODO: may need to tear down any action group where we are GO(?) */ | |
3145 | } | |
f8d0131a JM |
3146 | |
3147 | ||
3148 | int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr) | |
3149 | { | |
3150 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); | |
3151 | if (dev == NULL) | |
3152 | return -1; | |
3153 | if (dev->oper_freq <= 0) | |
3154 | return -1; | |
3155 | return dev->oper_freq; | |
3156 | } | |
0f66abd2 SS |
3157 | |
3158 | ||
3159 | void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled) | |
3160 | { | |
3161 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s", | |
3162 | enabled ? "enabled" : "disabled"); | |
3163 | p2p->cfg->p2p_intra_bss = enabled; | |
3164 | } | |
b5c9da8d JM |
3165 | |
3166 | ||
3167 | void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan) | |
3168 | { | |
3169 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list"); | |
3170 | os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels)); | |
3171 | } | |
3f9285ff JM |
3172 | |
3173 | ||
3174 | int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst, | |
3175 | const u8 *src, const u8 *bssid, const u8 *buf, | |
3176 | size_t len, unsigned int wait_time) | |
3177 | { | |
3178 | if (p2p->p2p_scan_running) { | |
3179 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action " | |
3180 | "frame TX until p2p_scan completes"); | |
3181 | if (p2p->after_scan_tx) { | |
3182 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped " | |
3183 | "previous pending Action frame TX"); | |
3184 | os_free(p2p->after_scan_tx); | |
3185 | } | |
3186 | p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) + | |
3187 | len); | |
3188 | if (p2p->after_scan_tx == NULL) | |
3189 | return -1; | |
3190 | p2p->after_scan_tx->freq = freq; | |
3191 | os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN); | |
3192 | os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN); | |
3193 | os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN); | |
3194 | p2p->after_scan_tx->len = len; | |
3195 | p2p->after_scan_tx->wait_time = wait_time; | |
3196 | os_memcpy(p2p->after_scan_tx + 1, buf, len); | |
3197 | return 0; | |
3198 | } | |
3199 | ||
3200 | return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid, | |
3201 | buf, len, wait_time); | |
3202 | } | |
7cfc4ac3 AGS |
3203 | |
3204 | ||
3205 | void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5, | |
3206 | int freq_overall) | |
3207 | { | |
3208 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d," | |
3209 | " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall); | |
3210 | p2p->best_freq_24 = freq_24; | |
3211 | p2p->best_freq_5 = freq_5; | |
3212 | p2p->best_freq_overall = freq_overall; | |
3213 | } | |
231bbd03 SS |
3214 | |
3215 | ||
3216 | const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p) | |
3217 | { | |
3218 | if (p2p == NULL || p2p->go_neg_peer == NULL) | |
3219 | return NULL; | |
3220 | return p2p->go_neg_peer->p2p_device_addr; | |
3221 | } |