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