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