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