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