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