Home | History | Annotate | Download | only in p2p
      1 /*
      2  * Wi-Fi Direct - P2P Group Owner Negotiation
      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 "common/ieee802_11_defs.h"
     13 #include "wps/wps_defs.h"
     14 #include "p2p_i.h"
     15 #include "p2p.h"
     16 
     17 
     18 static int p2p_go_det(u8 own_intent, u8 peer_value)
     19 {
     20 	u8 peer_intent = peer_value >> 1;
     21 	if (own_intent == peer_intent) {
     22 		if (own_intent == P2P_MAX_GO_INTENT)
     23 			return -1; /* both devices want to become GO */
     24 
     25 		/* Use tie breaker bit to determine GO */
     26 		return (peer_value & 0x01) ? 0 : 1;
     27 	}
     28 
     29 	return own_intent > peer_intent;
     30 }
     31 
     32 
     33 int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own,
     34 			    struct p2p_device *dev,
     35 			    const u8 *channel_list, size_t channel_list_len)
     36 {
     37 	const u8 *pos, *end;
     38 	struct p2p_channels *ch;
     39 	size_t channels;
     40 	struct p2p_channels intersection;
     41 
     42 	ch = &dev->channels;
     43 	os_memset(ch, 0, sizeof(*ch));
     44 	pos = channel_list;
     45 	end = channel_list + channel_list_len;
     46 
     47 	if (end - pos < 3)
     48 		return -1;
     49 	os_memcpy(dev->country, pos, 3);
     50 	wpa_hexdump_ascii(MSG_DEBUG, "P2P: Peer country", pos, 3);
     51 	if (pos[2] != 0x04 && os_memcmp(pos, p2p->cfg->country, 2) != 0) {
     52 		wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
     53 			"P2P: Mismatching country (ours=%c%c peer's=%c%c)",
     54 			p2p->cfg->country[0], p2p->cfg->country[1],
     55 			pos[0], pos[1]);
     56 		return -1;
     57 	}
     58 	pos += 3;
     59 
     60 	while (pos + 2 < end) {
     61 		struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes];
     62 		cl->reg_class = *pos++;
     63 		if (pos + 1 + pos[0] > end) {
     64 			wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
     65 				"P2P: Invalid peer Channel List");
     66 			return -1;
     67 		}
     68 		channels = *pos++;
     69 		cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
     70 			P2P_MAX_REG_CLASS_CHANNELS : channels;
     71 		os_memcpy(cl->channel, pos, cl->channels);
     72 		pos += channels;
     73 		ch->reg_classes++;
     74 		if (ch->reg_classes == P2P_MAX_REG_CLASSES)
     75 			break;
     76 	}
     77 
     78 	p2p_channels_intersect(own, &dev->channels, &intersection);
     79 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Own reg_classes %d "
     80 		"peer reg_classes %d intersection reg_classes %d",
     81 		(int) own->reg_classes,
     82 		(int) dev->channels.reg_classes,
     83 		(int) intersection.reg_classes);
     84 	if (intersection.reg_classes == 0) {
     85 		wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
     86 			"P2P: No common channels found");
     87 		return -1;
     88 	}
     89 	return 0;
     90 }
     91 
     92 
     93 static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
     94 			     const u8 *channel_list, size_t channel_list_len)
     95 {
     96 	return p2p_peer_channels_check(p2p, &p2p->channels, dev,
     97 				       channel_list, channel_list_len);
     98 }
     99 
    100 
    101 u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
    102 {
    103 	switch (wps_method) {
    104 	case WPS_PIN_DISPLAY:
    105 		return DEV_PW_REGISTRAR_SPECIFIED;
    106 	case WPS_PIN_KEYPAD:
    107 		return DEV_PW_USER_SPECIFIED;
    108 	case WPS_PBC:
    109 		return DEV_PW_PUSHBUTTON;
    110 	default:
    111 		return DEV_PW_DEFAULT;
    112 	}
    113 }
    114 
    115 
    116 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
    117 {
    118 	switch (wps_method) {
    119 	case WPS_PIN_DISPLAY:
    120 		return "Display";
    121 	case WPS_PIN_KEYPAD:
    122 		return "Keypad";
    123 	case WPS_PBC:
    124 		return "PBC";
    125 	default:
    126 		return "??";
    127 	}
    128 }
    129 
    130 
    131 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
    132 					    struct p2p_device *peer)
    133 {
    134 	struct wpabuf *buf;
    135 	u8 *len;
    136 	u8 group_capab;
    137 	size_t extra = 0;
    138 
    139 #ifdef CONFIG_WIFI_DISPLAY
    140 	if (p2p->wfd_ie_go_neg)
    141 		extra = wpabuf_len(p2p->wfd_ie_go_neg);
    142 #endif /* CONFIG_WIFI_DISPLAY */
    143 
    144 	buf = wpabuf_alloc(1000 + extra);
    145 	if (buf == NULL)
    146 		return NULL;
    147 
    148 	peer->dialog_token++;
    149 	if (peer->dialog_token == 0)
    150 		peer->dialog_token = 1;
    151 	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
    152 
    153 	len = p2p_buf_add_ie_hdr(buf);
    154 	group_capab = 0;
    155 	if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
    156 		group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
    157 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
    158 			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
    159 	}
    160 	if (p2p->cross_connect)
    161 		group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
    162 	if (p2p->cfg->p2p_intra_bss)
    163 		group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
    164 	p2p_buf_add_capability(buf, p2p->dev_capab &
    165 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
    166 			       group_capab);
    167 	p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) |
    168 			      p2p->next_tie_breaker);
    169 	p2p->next_tie_breaker = !p2p->next_tie_breaker;
    170 	p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
    171 	p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
    172 				   p2p->cfg->channel);
    173 	if (p2p->ext_listen_interval)
    174 		p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
    175 					      p2p->ext_listen_interval);
    176 	p2p_buf_add_intended_addr(buf, p2p->intended_addr);
    177 	p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
    178 	p2p_buf_add_device_info(buf, p2p, peer);
    179 	p2p_buf_add_operating_channel(buf, p2p->cfg->country,
    180 				      p2p->op_reg_class, p2p->op_channel);
    181 	p2p_buf_update_ie_hdr(buf, len);
    182 
    183 	/* WPS IE with Device Password ID attribute */
    184 	p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
    185 
    186 #ifdef CONFIG_WIFI_DISPLAY
    187 	if (p2p->wfd_ie_go_neg)
    188 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
    189 #endif /* CONFIG_WIFI_DISPLAY */
    190 
    191 	return buf;
    192 }
    193 
    194 
    195 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
    196 {
    197 	struct wpabuf *req;
    198 	int freq;
    199 
    200 	if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
    201 		u16 config_method;
    202 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    203 			"P2P: Use PD-before-GO-Neg workaround for " MACSTR,
    204 			MAC2STR(dev->info.p2p_device_addr));
    205 		if (dev->wps_method == WPS_PIN_DISPLAY)
    206 			config_method = WPS_CONFIG_KEYPAD;
    207 		else if (dev->wps_method == WPS_PIN_KEYPAD)
    208 			config_method = WPS_CONFIG_DISPLAY;
    209 		else if (dev->wps_method == WPS_PBC)
    210 			config_method = WPS_CONFIG_PUSHBUTTON;
    211 		else
    212 			return -1;
    213 		return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
    214 					 config_method, 0, 0);
    215 	}
    216 
    217 	freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
    218 	if (freq <= 0) {
    219 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    220 			"P2P: No Listen/Operating frequency known for the "
    221 			"peer " MACSTR " to send GO Negotiation Request",
    222 			MAC2STR(dev->info.p2p_device_addr));
    223 		return -1;
    224 	}
    225 
    226 	req = p2p_build_go_neg_req(p2p, dev);
    227 	if (req == NULL)
    228 		return -1;
    229 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    230 		"P2P: Sending GO Negotiation Request");
    231 	p2p_set_state(p2p, P2P_CONNECT);
    232 	p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
    233 	p2p->go_neg_peer = dev;
    234 	dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
    235 	dev->connect_reqs++;
    236 	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
    237 			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
    238 			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
    239 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    240 			"P2P: Failed to send Action frame");
    241 		/* Use P2P find to recover and retry */
    242 		p2p_set_timeout(p2p, 0, 0);
    243 	} else
    244 		dev->go_neg_req_sent++;
    245 
    246 	wpabuf_free(req);
    247 
    248 	return 0;
    249 }
    250 
    251 
    252 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
    253 					     struct p2p_device *peer,
    254 					     u8 dialog_token, u8 status,
    255 					     u8 tie_breaker)
    256 {
    257 	struct wpabuf *buf;
    258 	u8 *len;
    259 	u8 group_capab;
    260 	size_t extra = 0;
    261 
    262 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    263 		"P2P: Building GO Negotiation Response");
    264 
    265 #ifdef CONFIG_WIFI_DISPLAY
    266 	if (p2p->wfd_ie_go_neg)
    267 		extra = wpabuf_len(p2p->wfd_ie_go_neg);
    268 #endif /* CONFIG_WIFI_DISPLAY */
    269 
    270 	buf = wpabuf_alloc(1000 + extra);
    271 	if (buf == NULL)
    272 		return NULL;
    273 
    274 	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
    275 
    276 	len = p2p_buf_add_ie_hdr(buf);
    277 	p2p_buf_add_status(buf, status);
    278 	group_capab = 0;
    279 	if (peer && peer->go_state == LOCAL_GO) {
    280 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
    281 			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
    282 			if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
    283 				group_capab |=
    284 					P2P_GROUP_CAPAB_PERSISTENT_RECONN;
    285 		}
    286 		if (p2p->cross_connect)
    287 			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
    288 		if (p2p->cfg->p2p_intra_bss)
    289 			group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
    290 	}
    291 	p2p_buf_add_capability(buf, p2p->dev_capab &
    292 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
    293 			       group_capab);
    294 	p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
    295 	p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
    296 	if (peer && peer->go_state == REMOTE_GO) {
    297 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating "
    298 			"Channel attribute");
    299 	} else {
    300 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
    301 					      p2p->op_reg_class,
    302 					      p2p->op_channel);
    303 	}
    304 	p2p_buf_add_intended_addr(buf, p2p->intended_addr);
    305 
    306 	if (status || peer == NULL) {
    307 		p2p_buf_add_channel_list(buf, p2p->cfg->country,
    308 					 &p2p->channels);
    309 	} else if (peer->go_state == REMOTE_GO) {
    310 		p2p_buf_add_channel_list(buf, p2p->cfg->country,
    311 					 &p2p->channels);
    312 	} else {
    313 		struct p2p_channels res;
    314 		p2p_channels_intersect(&p2p->channels, &peer->channels,
    315 				       &res);
    316 		p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
    317 	}
    318 
    319 	p2p_buf_add_device_info(buf, p2p, peer);
    320 	if (peer && peer->go_state == LOCAL_GO) {
    321 		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
    322 				     p2p->ssid_len);
    323 	}
    324 	p2p_buf_update_ie_hdr(buf, len);
    325 
    326 	/* WPS IE with Device Password ID attribute */
    327 	p2p_build_wps_ie(p2p, buf,
    328 			 p2p_wps_method_pw_id(peer ? peer->wps_method :
    329 					      WPS_NOT_READY), 0);
    330 
    331 #ifdef CONFIG_WIFI_DISPLAY
    332 	if (p2p->wfd_ie_go_neg)
    333 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
    334 #endif /* CONFIG_WIFI_DISPLAY */
    335 
    336 
    337 	return buf;
    338 }
    339 
    340 
    341 void p2p_reselect_channel(struct p2p_data *p2p,
    342 			  struct p2p_channels *intersection)
    343 {
    344 	struct p2p_reg_class *cl;
    345 	int freq;
    346 	u8 op_reg_class, op_channel;
    347 	unsigned int i;
    348 
    349 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
    350 		"channel (reg_class %u channel %u) not acceptable to the "
    351 		"peer", p2p->op_reg_class, p2p->op_channel);
    352 
    353 	/* First, try to pick the best channel from another band */
    354 	freq = p2p_channel_to_freq(p2p->cfg->country, p2p->op_reg_class,
    355 				   p2p->op_channel);
    356 	if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
    357 	    p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5,
    358 				&op_reg_class, &op_channel) == 0 &&
    359 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
    360 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 5 GHz "
    361 			"channel (reg_class %u channel %u) from intersection",
    362 			op_reg_class, op_channel);
    363 		p2p->op_reg_class = op_reg_class;
    364 		p2p->op_channel = op_channel;
    365 		return;
    366 	}
    367 
    368 	if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
    369 	    p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24,
    370 				&op_reg_class, &op_channel) == 0 &&
    371 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
    372 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 2.4 GHz "
    373 			"channel (reg_class %u channel %u) from intersection",
    374 			op_reg_class, op_channel);
    375 		p2p->op_reg_class = op_reg_class;
    376 		p2p->op_channel = op_channel;
    377 		return;
    378 	}
    379 
    380 	/* Select channel with highest preference if the peer supports it */
    381 	for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
    382 		if (p2p_channels_includes(intersection,
    383 					  p2p->cfg->pref_chan[i].op_class,
    384 					  p2p->cfg->pref_chan[i].chan)) {
    385 			p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
    386 			p2p->op_channel = p2p->cfg->pref_chan[i].chan;
    387 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick "
    388 				"highest preferred chnnel (op_class %u "
    389 				"channel %u) from intersection",
    390 				p2p->op_reg_class, p2p->op_channel);
    391 			return;
    392 		}
    393 	}
    394 
    395 	/*
    396 	 * Fall back to whatever is included in the channel intersection since
    397 	 * no better options seems to be available.
    398 	 */
    399 	cl = &intersection->reg_class[0];
    400 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick another channel "
    401 		"(reg_class %u channel %u) from intersection",
    402 		cl->reg_class, cl->channel[0]);
    403 	p2p->op_reg_class = cl->reg_class;
    404 	p2p->op_channel = cl->channel[0];
    405 }
    406 
    407 
    408 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
    409 			    const u8 *data, size_t len, int rx_freq)
    410 {
    411 	struct p2p_device *dev = NULL;
    412 	struct wpabuf *resp;
    413 	struct p2p_message msg;
    414 	u8 status = P2P_SC_FAIL_INVALID_PARAMS;
    415 	int tie_breaker = 0;
    416 	int freq;
    417 
    418 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    419 		"P2P: Received GO Negotiation Request from " MACSTR
    420 		"(freq=%d)", MAC2STR(sa), rx_freq);
    421 
    422 	if (p2p_parse(data, len, &msg))
    423 		return;
    424 
    425 	if (!msg.capability) {
    426 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    427 			"P2P: Mandatory Capability attribute missing from GO "
    428 			"Negotiation Request");
    429 #ifdef CONFIG_P2P_STRICT
    430 		goto fail;
    431 #endif /* CONFIG_P2P_STRICT */
    432 	}
    433 
    434 	if (msg.go_intent)
    435 		tie_breaker = *msg.go_intent & 0x01;
    436 	else {
    437 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    438 			"P2P: Mandatory GO Intent attribute missing from GO "
    439 			"Negotiation Request");
    440 #ifdef CONFIG_P2P_STRICT
    441 		goto fail;
    442 #endif /* CONFIG_P2P_STRICT */
    443 	}
    444 
    445 	if (!msg.config_timeout) {
    446 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    447 			"P2P: Mandatory Configuration Timeout attribute "
    448 			"missing from GO Negotiation Request");
    449 #ifdef CONFIG_P2P_STRICT
    450 		goto fail;
    451 #endif /* CONFIG_P2P_STRICT */
    452 	}
    453 
    454 	if (!msg.listen_channel) {
    455 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    456 			"P2P: No Listen Channel attribute received");
    457 		goto fail;
    458 	}
    459 	if (!msg.operating_channel) {
    460 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    461 			"P2P: No Operating Channel attribute received");
    462 		goto fail;
    463 	}
    464 	if (!msg.channel_list) {
    465 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    466 			"P2P: No Channel List attribute received");
    467 		goto fail;
    468 	}
    469 	if (!msg.intended_addr) {
    470 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    471 			"P2P: No Intended P2P Interface Address attribute "
    472 			"received");
    473 		goto fail;
    474 	}
    475 	if (!msg.p2p_device_info) {
    476 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    477 			"P2P: No P2P Device Info attribute received");
    478 		goto fail;
    479 	}
    480 
    481 	if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
    482 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    483 			"P2P: Unexpected GO Negotiation Request SA=" MACSTR
    484 			" != dev_addr=" MACSTR,
    485 			MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
    486 		goto fail;
    487 	}
    488 
    489 	dev = p2p_get_device(p2p, sa);
    490 
    491 	if (msg.status && *msg.status) {
    492 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    493 			"P2P: Unexpected Status attribute (%d) in GO "
    494 			"Negotiation Request", *msg.status);
    495 		goto fail;
    496 	}
    497 
    498 	if (dev == NULL)
    499 		dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
    500 	else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
    501 		p2p_add_dev_info(p2p, sa, dev, &msg);
    502 	if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
    503 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    504 			"P2P: User has rejected this peer");
    505 		status = P2P_SC_FAIL_REJECTED_BY_USER;
    506 	} else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
    507 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    508 			"P2P: Not ready for GO negotiation with " MACSTR,
    509 			MAC2STR(sa));
    510 		status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
    511 		if (dev)
    512 			dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
    513 		p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
    514 					msg.dev_password_id);
    515 	} else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
    516 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    517 			"P2P: Already in Group Formation with another peer");
    518 		status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
    519 	} else {
    520 		int go;
    521 
    522 		if (!p2p->go_neg_peer) {
    523 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
    524 				"GO Negotiation with previously authorized "
    525 				"peer");
    526 			if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
    527 				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    528 					"P2P: Use default channel settings");
    529 				p2p->op_reg_class = p2p->cfg->op_reg_class;
    530 				p2p->op_channel = p2p->cfg->op_channel;
    531 				os_memcpy(&p2p->channels, &p2p->cfg->channels,
    532 					  sizeof(struct p2p_channels));
    533 			} else {
    534 				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    535 					"P2P: Use previously configured "
    536 					"forced channel settings");
    537 			}
    538 		}
    539 
    540 		dev->flags &= ~P2P_DEV_NOT_YET_READY;
    541 
    542 		if (!msg.go_intent) {
    543 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    544 				"P2P: No GO Intent attribute received");
    545 			goto fail;
    546 		}
    547 		if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
    548 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    549 				"P2P: Invalid GO Intent value (%u) received",
    550 				*msg.go_intent >> 1);
    551 			goto fail;
    552 		}
    553 
    554 		if (dev->go_neg_req_sent &&
    555 		    os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
    556 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    557 				"P2P: Do not reply since peer has higher "
    558 				"address and GO Neg Request already sent");
    559 			p2p_parse_free(&msg);
    560 			return;
    561 		}
    562 
    563 		go = p2p_go_det(p2p->go_intent, *msg.go_intent);
    564 		if (go < 0) {
    565 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    566 				"P2P: Incompatible GO Intent");
    567 			status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
    568 			goto fail;
    569 		}
    570 
    571 		if (p2p_peer_channels(p2p, dev, msg.channel_list,
    572 				      msg.channel_list_len) < 0) {
    573 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    574 				"P2P: No common channels found");
    575 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
    576 			goto fail;
    577 		}
    578 
    579 		switch (msg.dev_password_id) {
    580 		case DEV_PW_REGISTRAR_SPECIFIED:
    581 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    582 				"P2P: PIN from peer Display");
    583 			if (dev->wps_method != WPS_PIN_KEYPAD) {
    584 				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    585 					"P2P: We have wps_method=%s -> "
    586 					"incompatible",
    587 					p2p_wps_method_str(dev->wps_method));
    588 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
    589 				goto fail;
    590 			}
    591 			break;
    592 		case DEV_PW_USER_SPECIFIED:
    593 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    594 				"P2P: Peer entered PIN on Keypad");
    595 			if (dev->wps_method != WPS_PIN_DISPLAY) {
    596 				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    597 					"P2P: We have wps_method=%s -> "
    598 					"incompatible",
    599 					p2p_wps_method_str(dev->wps_method));
    600 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
    601 				goto fail;
    602 			}
    603 			break;
    604 		case DEV_PW_PUSHBUTTON:
    605 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    606 				"P2P: Peer using pushbutton");
    607 			if (dev->wps_method != WPS_PBC) {
    608 				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    609 					"P2P: We have wps_method=%s -> "
    610 					"incompatible",
    611 					p2p_wps_method_str(dev->wps_method));
    612 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
    613 				goto fail;
    614 			}
    615 			break;
    616 		default:
    617 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    618 				"P2P: Unsupported Device Password ID %d",
    619 				msg.dev_password_id);
    620 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
    621 			goto fail;
    622 		}
    623 
    624 		if (go) {
    625 			struct p2p_channels intersection;
    626 			size_t i;
    627 			p2p_channels_intersect(&p2p->channels, &dev->channels,
    628 					       &intersection);
    629 			if (intersection.reg_classes == 0 ||
    630 			    intersection.reg_class[0].channels == 0) {
    631 				status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
    632 				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    633 					"P2P: No common channels found");
    634 				goto fail;
    635 			}
    636 			for (i = 0; i < intersection.reg_classes; i++) {
    637 				struct p2p_reg_class *c;
    638 				c = &intersection.reg_class[i];
    639 				wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
    640 					   c->reg_class);
    641 				wpa_hexdump(MSG_DEBUG, "P2P: channels",
    642 					    c->channel, c->channels);
    643 			}
    644 			if (!p2p_channels_includes(&intersection,
    645 						   p2p->op_reg_class,
    646 						   p2p->op_channel))
    647 				p2p_reselect_channel(p2p, &intersection);
    648 
    649 			if (!p2p->ssid_set) {
    650 				p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
    651 				p2p->ssid_set = 1;
    652 			}
    653 		}
    654 
    655 		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
    656 		dev->oper_freq = p2p_channel_to_freq((const char *)
    657 						     msg.operating_channel,
    658 						     msg.operating_channel[3],
    659 						     msg.operating_channel[4]);
    660 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
    661 			"channel preference: %d MHz", dev->oper_freq);
    662 
    663 		if (msg.config_timeout) {
    664 			dev->go_timeout = msg.config_timeout[0];
    665 			dev->client_timeout = msg.config_timeout[1];
    666 		}
    667 
    668 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    669 			"P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
    670 		if (p2p->state != P2P_IDLE)
    671 			p2p_stop_find_for_freq(p2p, rx_freq);
    672 		p2p_set_state(p2p, P2P_GO_NEG);
    673 		p2p_clear_timeout(p2p);
    674 		dev->dialog_token = msg.dialog_token;
    675 		os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
    676 		p2p->go_neg_peer = dev;
    677 		status = P2P_SC_SUCCESS;
    678 	}
    679 
    680 fail:
    681 	if (dev)
    682 		dev->status = status;
    683 	resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
    684 				     !tie_breaker);
    685 	p2p_parse_free(&msg);
    686 	if (resp == NULL)
    687 		return;
    688 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    689 		"P2P: Sending GO Negotiation Response");
    690 	if (rx_freq > 0)
    691 		freq = rx_freq;
    692 	else
    693 		freq = p2p_channel_to_freq(p2p->cfg->country,
    694 					   p2p->cfg->reg_class,
    695 					   p2p->cfg->channel);
    696 	if (freq < 0) {
    697 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    698 			"P2P: Unknown regulatory class/channel");
    699 		wpabuf_free(resp);
    700 		return;
    701 	}
    702 	if (status == P2P_SC_SUCCESS) {
    703 		p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
    704 		dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
    705 		if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
    706 			/*
    707 			 * Peer has smaller address, so the GO Negotiation
    708 			 * Response from us is expected to complete
    709 			 * negotiation. Ignore a GO Negotiation Response from
    710 			 * the peer if it happens to be received after this
    711 			 * point due to a race condition in GO Negotiation
    712 			 * Request transmission and processing.
    713 			 */
    714 			dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
    715 		}
    716 	} else
    717 		p2p->pending_action_state =
    718 			P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
    719 	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
    720 			    p2p->cfg->dev_addr,
    721 			    wpabuf_head(resp), wpabuf_len(resp), 250) < 0) {
    722 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    723 			"P2P: Failed to send Action frame");
    724 	}
    725 
    726 	wpabuf_free(resp);
    727 }
    728 
    729 
    730 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
    731 					     struct p2p_device *peer,
    732 					     u8 dialog_token, u8 status,
    733 					     const u8 *resp_chan, int go)
    734 {
    735 	struct wpabuf *buf;
    736 	u8 *len;
    737 	struct p2p_channels res;
    738 	u8 group_capab;
    739 	size_t extra = 0;
    740 
    741 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    742 		"P2P: Building GO Negotiation Confirm");
    743 
    744 #ifdef CONFIG_WIFI_DISPLAY
    745 	if (p2p->wfd_ie_go_neg)
    746 		extra = wpabuf_len(p2p->wfd_ie_go_neg);
    747 #endif /* CONFIG_WIFI_DISPLAY */
    748 
    749 	buf = wpabuf_alloc(1000 + extra);
    750 	if (buf == NULL)
    751 		return NULL;
    752 
    753 	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
    754 
    755 	len = p2p_buf_add_ie_hdr(buf);
    756 	p2p_buf_add_status(buf, status);
    757 	group_capab = 0;
    758 	if (peer->go_state == LOCAL_GO) {
    759 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
    760 			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
    761 			if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
    762 				group_capab |=
    763 					P2P_GROUP_CAPAB_PERSISTENT_RECONN;
    764 		}
    765 		if (p2p->cross_connect)
    766 			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
    767 		if (p2p->cfg->p2p_intra_bss)
    768 			group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
    769 	}
    770 	p2p_buf_add_capability(buf, p2p->dev_capab &
    771 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
    772 			       group_capab);
    773 	if (go || resp_chan == NULL)
    774 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
    775 					      p2p->op_reg_class,
    776 					      p2p->op_channel);
    777 	else
    778 		p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
    779 					      resp_chan[3], resp_chan[4]);
    780 	p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
    781 	p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
    782 	if (go) {
    783 		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
    784 				     p2p->ssid_len);
    785 	}
    786 	p2p_buf_update_ie_hdr(buf, len);
    787 
    788 #ifdef CONFIG_WIFI_DISPLAY
    789 	if (p2p->wfd_ie_go_neg)
    790 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
    791 #endif /* CONFIG_WIFI_DISPLAY */
    792 
    793 	return buf;
    794 }
    795 
    796 
    797 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
    798 			     const u8 *data, size_t len, int rx_freq)
    799 {
    800 	struct p2p_device *dev;
    801 	struct wpabuf *conf;
    802 	int go = -1;
    803 	struct p2p_message msg;
    804 	u8 status = P2P_SC_SUCCESS;
    805 	int freq;
    806 
    807 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    808 		"P2P: Received GO Negotiation Response from " MACSTR
    809 		" (freq=%d)", MAC2STR(sa), rx_freq);
    810 	dev = p2p_get_device(p2p, sa);
    811 	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
    812 	    dev != p2p->go_neg_peer) {
    813 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    814 			"P2P: Not ready for GO negotiation with " MACSTR,
    815 			MAC2STR(sa));
    816 		return;
    817 	}
    818 
    819 	if (p2p_parse(data, len, &msg))
    820 		return;
    821 
    822 	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
    823 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    824 			"P2P: Was not expecting GO Negotiation Response - "
    825 			"ignore");
    826 		p2p_parse_free(&msg);
    827 		return;
    828 	}
    829 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
    830 
    831 	if (msg.dialog_token != dev->dialog_token) {
    832 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    833 			"P2P: Unexpected Dialog Token %u (expected %u)",
    834 			msg.dialog_token, dev->dialog_token);
    835 		p2p_parse_free(&msg);
    836 		return;
    837 	}
    838 
    839 	if (!msg.status) {
    840 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    841 			"P2P: No Status attribute received");
    842 		status = P2P_SC_FAIL_INVALID_PARAMS;
    843 		goto fail;
    844 	}
    845 	if (*msg.status) {
    846 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    847 			"P2P: GO Negotiation rejected: status %d",
    848 			*msg.status);
    849 		dev->go_neg_req_sent = 0;
    850 		if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
    851 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    852 				"P2P: Wait for the peer to become ready for "
    853 				"GO Negotiation");
    854 			dev->flags |= P2P_DEV_NOT_YET_READY;
    855 			dev->wait_count = 0;
    856 			p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
    857 			p2p_set_timeout(p2p, 0, 0);
    858 		} else {
    859 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    860 				"P2P: Stop GO Negotiation attempt");
    861 			p2p_go_neg_failed(p2p, dev, *msg.status);
    862 		}
    863 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    864 		p2p_parse_free(&msg);
    865 		return;
    866 	}
    867 
    868 	if (!msg.capability) {
    869 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    870 			"P2P: Mandatory Capability attribute missing from GO "
    871 			"Negotiation Response");
    872 #ifdef CONFIG_P2P_STRICT
    873 		status = P2P_SC_FAIL_INVALID_PARAMS;
    874 		goto fail;
    875 #endif /* CONFIG_P2P_STRICT */
    876 	}
    877 
    878 	if (!msg.p2p_device_info) {
    879 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    880 			"P2P: Mandatory P2P Device Info attribute missing "
    881 			"from GO Negotiation Response");
    882 #ifdef CONFIG_P2P_STRICT
    883 		status = P2P_SC_FAIL_INVALID_PARAMS;
    884 		goto fail;
    885 #endif /* CONFIG_P2P_STRICT */
    886 	}
    887 
    888 	if (!msg.intended_addr) {
    889 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    890 			"P2P: No Intended P2P Interface Address attribute "
    891 			"received");
    892 		status = P2P_SC_FAIL_INVALID_PARAMS;
    893 		goto fail;
    894 	}
    895 
    896 	if (!msg.go_intent) {
    897 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    898 			"P2P: No GO Intent attribute received");
    899 		status = P2P_SC_FAIL_INVALID_PARAMS;
    900 		goto fail;
    901 	}
    902 	if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
    903 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    904 			"P2P: Invalid GO Intent value (%u) received",
    905 			*msg.go_intent >> 1);
    906 		status = P2P_SC_FAIL_INVALID_PARAMS;
    907 		goto fail;
    908 	}
    909 
    910 	go = p2p_go_det(p2p->go_intent, *msg.go_intent);
    911 	if (go < 0) {
    912 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    913 			"P2P: Incompatible GO Intent");
    914 		status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
    915 		goto fail;
    916 	}
    917 
    918 	if (!go && msg.group_id) {
    919 		/* Store SSID for Provisioning step */
    920 		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
    921 		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
    922 	} else if (!go) {
    923 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    924 			"P2P: Mandatory P2P Group ID attribute missing from "
    925 			"GO Negotiation Response");
    926 		p2p->ssid_len = 0;
    927 #ifdef CONFIG_P2P_STRICT
    928 		status = P2P_SC_FAIL_INVALID_PARAMS;
    929 		goto fail;
    930 #endif /* CONFIG_P2P_STRICT */
    931 	}
    932 
    933 	if (!msg.config_timeout) {
    934 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    935 			"P2P: Mandatory Configuration Timeout attribute "
    936 			"missing from GO Negotiation Response");
    937 #ifdef CONFIG_P2P_STRICT
    938 		status = P2P_SC_FAIL_INVALID_PARAMS;
    939 		goto fail;
    940 #endif /* CONFIG_P2P_STRICT */
    941 	} else {
    942 		dev->go_timeout = msg.config_timeout[0];
    943 		dev->client_timeout = msg.config_timeout[1];
    944 	}
    945 
    946 	if (!msg.operating_channel && !go) {
    947 		/*
    948 		 * Note: P2P Client may omit Operating Channel attribute to
    949 		 * indicate it does not have a preference.
    950 		 */
    951 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    952 			"P2P: No Operating Channel attribute received");
    953 		status = P2P_SC_FAIL_INVALID_PARAMS;
    954 		goto fail;
    955 	}
    956 	if (!msg.channel_list) {
    957 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    958 			"P2P: No Channel List attribute received");
    959 		status = P2P_SC_FAIL_INVALID_PARAMS;
    960 		goto fail;
    961 	}
    962 
    963 	if (p2p_peer_channels(p2p, dev, msg.channel_list,
    964 			      msg.channel_list_len) < 0) {
    965 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    966 			"P2P: No common channels found");
    967 		status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
    968 		goto fail;
    969 	}
    970 
    971 	if (msg.operating_channel) {
    972 		dev->oper_freq = p2p_channel_to_freq((const char *)
    973 						     msg.operating_channel,
    974 						     msg.operating_channel[3],
    975 						     msg.operating_channel[4]);
    976 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
    977 			"channel preference: %d MHz", dev->oper_freq);
    978 	} else
    979 		dev->oper_freq = 0;
    980 
    981 	switch (msg.dev_password_id) {
    982 	case DEV_PW_REGISTRAR_SPECIFIED:
    983 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    984 			"P2P: PIN from peer Display");
    985 		if (dev->wps_method != WPS_PIN_KEYPAD) {
    986 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    987 				"P2P: We have wps_method=%s -> "
    988 				"incompatible",
    989 				p2p_wps_method_str(dev->wps_method));
    990 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
    991 			goto fail;
    992 		}
    993 		break;
    994 	case DEV_PW_USER_SPECIFIED:
    995 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    996 			"P2P: Peer entered PIN on Keypad");
    997 		if (dev->wps_method != WPS_PIN_DISPLAY) {
    998 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
    999 				"P2P: We have wps_method=%s -> "
   1000 				"incompatible",
   1001 				p2p_wps_method_str(dev->wps_method));
   1002 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
   1003 			goto fail;
   1004 		}
   1005 		break;
   1006 	case DEV_PW_PUSHBUTTON:
   1007 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1008 			"P2P: Peer using pushbutton");
   1009 		if (dev->wps_method != WPS_PBC) {
   1010 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1011 				"P2P: We have wps_method=%s -> "
   1012 				"incompatible",
   1013 				p2p_wps_method_str(dev->wps_method));
   1014 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
   1015 			goto fail;
   1016 		}
   1017 		break;
   1018 	default:
   1019 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1020 			"P2P: Unsupported Device Password ID %d",
   1021 			msg.dev_password_id);
   1022 		status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
   1023 		goto fail;
   1024 	}
   1025 
   1026 	if (go) {
   1027 		struct p2p_channels intersection;
   1028 		size_t i;
   1029 		p2p_channels_intersect(&p2p->channels, &dev->channels,
   1030 				       &intersection);
   1031 		if (intersection.reg_classes == 0 ||
   1032 		    intersection.reg_class[0].channels == 0) {
   1033 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
   1034 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1035 				"P2P: No common channels found");
   1036 			goto fail;
   1037 		}
   1038 		for (i = 0; i < intersection.reg_classes; i++) {
   1039 			struct p2p_reg_class *c;
   1040 			c = &intersection.reg_class[i];
   1041 			wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
   1042 				   c->reg_class);
   1043 			wpa_hexdump(MSG_DEBUG, "P2P: channels",
   1044 				    c->channel, c->channels);
   1045 		}
   1046 		if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
   1047 					   p2p->op_channel))
   1048 			p2p_reselect_channel(p2p, &intersection);
   1049 
   1050 		if (!p2p->ssid_set) {
   1051 			p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
   1052 			p2p->ssid_set = 1;
   1053 		}
   1054 	}
   1055 
   1056 	p2p_set_state(p2p, P2P_GO_NEG);
   1057 	p2p_clear_timeout(p2p);
   1058 
   1059 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1060 		"P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
   1061 	os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
   1062 
   1063 fail:
   1064 	conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
   1065 				     msg.operating_channel, go);
   1066 	p2p_parse_free(&msg);
   1067 	if (conf == NULL)
   1068 		return;
   1069 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1070 		"P2P: Sending GO Negotiation Confirm");
   1071 	if (status == P2P_SC_SUCCESS) {
   1072 		p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
   1073 		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
   1074 	} else
   1075 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
   1076 	if (rx_freq > 0)
   1077 		freq = rx_freq;
   1078 	else
   1079 		freq = dev->listen_freq;
   1080 	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
   1081 			    wpabuf_head(conf), wpabuf_len(conf), 0) < 0) {
   1082 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1083 			"P2P: Failed to send Action frame");
   1084 		p2p_go_neg_failed(p2p, dev, -1);
   1085 	}
   1086 	wpabuf_free(conf);
   1087 }
   1088 
   1089 
   1090 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
   1091 			     const u8 *data, size_t len)
   1092 {
   1093 	struct p2p_device *dev;
   1094 	struct p2p_message msg;
   1095 
   1096 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1097 		"P2P: Received GO Negotiation Confirm from " MACSTR,
   1098 		MAC2STR(sa));
   1099 	dev = p2p_get_device(p2p, sa);
   1100 	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
   1101 	    dev != p2p->go_neg_peer) {
   1102 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1103 			"P2P: Not ready for GO negotiation with " MACSTR,
   1104 			MAC2STR(sa));
   1105 		return;
   1106 	}
   1107 
   1108 	if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
   1109 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
   1110 			"for TX status on GO Negotiation Response since we "
   1111 			"already received Confirmation");
   1112 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
   1113 	}
   1114 
   1115 	if (p2p_parse(data, len, &msg))
   1116 		return;
   1117 
   1118 	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
   1119 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1120 			"P2P: Was not expecting GO Negotiation Confirm - "
   1121 			"ignore");
   1122 		return;
   1123 	}
   1124 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
   1125 
   1126 	if (msg.dialog_token != dev->dialog_token) {
   1127 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1128 			"P2P: Unexpected Dialog Token %u (expected %u)",
   1129 			msg.dialog_token, dev->dialog_token);
   1130 		p2p_parse_free(&msg);
   1131 		return;
   1132 	}
   1133 
   1134 	if (!msg.status) {
   1135 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1136 			"P2P: No Status attribute received");
   1137 		p2p_parse_free(&msg);
   1138 		return;
   1139 	}
   1140 	if (*msg.status) {
   1141 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1142 			"P2P: GO Negotiation rejected: status %d",
   1143 			*msg.status);
   1144 		p2p_parse_free(&msg);
   1145 		return;
   1146 	}
   1147 
   1148 	if (dev->go_state == REMOTE_GO && msg.group_id) {
   1149 		/* Store SSID for Provisioning step */
   1150 		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
   1151 		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
   1152 	} else if (dev->go_state == REMOTE_GO) {
   1153 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1154 			"P2P: Mandatory P2P Group ID attribute missing from "
   1155 			"GO Negotiation Confirmation");
   1156 		p2p->ssid_len = 0;
   1157 #ifdef CONFIG_P2P_STRICT
   1158 		p2p_parse_free(&msg);
   1159 		return;
   1160 #endif /* CONFIG_P2P_STRICT */
   1161 	}
   1162 
   1163 	if (!msg.operating_channel) {
   1164 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1165 			"P2P: Mandatory Operating Channel attribute missing "
   1166 			"from GO Negotiation Confirmation");
   1167 #ifdef CONFIG_P2P_STRICT
   1168 		p2p_parse_free(&msg);
   1169 		return;
   1170 #endif /* CONFIG_P2P_STRICT */
   1171 	}
   1172 
   1173 #ifdef ANDROID_P2P
   1174 	if (msg.operating_channel) {
   1175 		dev->oper_freq = p2p_channel_to_freq((const char *)
   1176 						     msg.operating_channel,
   1177 						     msg.operating_channel[3],
   1178 						     msg.operating_channel[4]);
   1179 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
   1180 			"channel preference: %d MHz", dev->oper_freq);
   1181 	} else
   1182 		dev->oper_freq = 0;
   1183 #endif
   1184 
   1185 	if (!msg.channel_list) {
   1186 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1187 			"P2P: Mandatory Operating Channel attribute missing "
   1188 			"from GO Negotiation Confirmation");
   1189 #ifdef CONFIG_P2P_STRICT
   1190 		p2p_parse_free(&msg);
   1191 		return;
   1192 #endif /* CONFIG_P2P_STRICT */
   1193 	}
   1194 
   1195 	p2p_parse_free(&msg);
   1196 
   1197 	if (dev->go_state == UNKNOWN_GO) {
   1198 		/*
   1199 		 * This should not happen since GO negotiation has already
   1200 		 * been completed.
   1201 		 */
   1202 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
   1203 			"P2P: Unexpected GO Neg state - do not know which end "
   1204 			"becomes GO");
   1205 		return;
   1206 	}
   1207 
   1208 	/*
   1209 	 * The peer could have missed our ctrl::ack frame for GO Negotiation
   1210 	 * Confirm and continue retransmitting the frame. To reduce the
   1211 	 * likelihood of the peer not getting successful TX status for the
   1212 	 * GO Negotiation Confirm frame, wait a short time here before starting
   1213 	 * the group so that we will remain on the current channel to
   1214 	 * acknowledge any possible retransmission from the peer.
   1215 	 */
   1216 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: 20 ms wait on current "
   1217 		"channel before starting group");
   1218 	os_sleep(0, 20000);
   1219 
   1220 	p2p_go_complete(p2p, dev);
   1221 }
   1222