• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
p2p_go_det(u8 own_intent,u8 peer_value)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 
p2p_peer_channels_check(struct p2p_data * p2p,struct p2p_channels * own,struct p2p_device * dev,const u8 * channel_list,size_t channel_list_len)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 
p2p_peer_channels(struct p2p_data * p2p,struct p2p_device * dev,const u8 * channel_list,size_t channel_list_len)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 
p2p_wps_method_pw_id(enum p2p_wps_method wps_method)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 
p2p_wps_method_str(enum p2p_wps_method wps_method)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 
p2p_build_go_neg_req(struct p2p_data * p2p,struct p2p_device * peer)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 
p2p_connect_send(struct p2p_data * p2p,struct p2p_device * dev)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 
p2p_build_go_neg_resp(struct p2p_data * p2p,struct p2p_device * peer,u8 dialog_token,u8 status,u8 tie_breaker)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  */
p2p_reselect_channel(struct p2p_data * p2p,struct p2p_channels * intersection)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 
p2p_go_select_channel(struct p2p_data * p2p,struct p2p_device * dev,u8 * status)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 
p2p_process_go_neg_req(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len,int rx_freq)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 
p2p_build_go_neg_conf(struct p2p_data * p2p,struct p2p_device * peer,u8 dialog_token,u8 status,const u8 * resp_chan,int go)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 
p2p_process_go_neg_resp(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len,int rx_freq)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 			p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
962 			p2p_set_timeout(p2p, 0, 0);
963 		} else {
964 			p2p_dbg(p2p, "Stop GO Negotiation attempt");
965 			p2p_go_neg_failed(p2p, dev, *msg.status);
966 		}
967 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
968 		p2p_parse_free(&msg);
969 		return;
970 	}
971 
972 	if (!msg.capability) {
973 		p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Response");
974 #ifdef CONFIG_P2P_STRICT
975 		status = P2P_SC_FAIL_INVALID_PARAMS;
976 		goto fail;
977 #endif /* CONFIG_P2P_STRICT */
978 	}
979 
980 	if (!msg.p2p_device_info) {
981 		p2p_dbg(p2p, "Mandatory P2P Device Info attribute missing from GO Negotiation Response");
982 #ifdef CONFIG_P2P_STRICT
983 		status = P2P_SC_FAIL_INVALID_PARAMS;
984 		goto fail;
985 #endif /* CONFIG_P2P_STRICT */
986 	}
987 
988 	if (!msg.intended_addr) {
989 		p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
990 		status = P2P_SC_FAIL_INVALID_PARAMS;
991 		goto fail;
992 	}
993 
994 	if (!msg.go_intent) {
995 		p2p_dbg(p2p, "No GO Intent attribute received");
996 		status = P2P_SC_FAIL_INVALID_PARAMS;
997 		goto fail;
998 	}
999 	if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
1000 		p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
1001 			*msg.go_intent >> 1);
1002 		status = P2P_SC_FAIL_INVALID_PARAMS;
1003 		goto fail;
1004 	}
1005 
1006 	go = p2p_go_det(p2p->go_intent, *msg.go_intent);
1007 	if (go < 0) {
1008 		p2p_dbg(p2p, "Incompatible GO Intent");
1009 		status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
1010 		goto fail;
1011 	}
1012 
1013 	if (!go && msg.group_id) {
1014 		/* Store SSID for Provisioning step */
1015 		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1016 		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1017 	} else if (!go) {
1018 		p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Response");
1019 		p2p->ssid_len = 0;
1020 		status = P2P_SC_FAIL_INVALID_PARAMS;
1021 		goto fail;
1022 	}
1023 
1024 	if (!msg.config_timeout) {
1025 		p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Response");
1026 #ifdef CONFIG_P2P_STRICT
1027 		status = P2P_SC_FAIL_INVALID_PARAMS;
1028 		goto fail;
1029 #endif /* CONFIG_P2P_STRICT */
1030 	} else {
1031 		dev->go_timeout = msg.config_timeout[0];
1032 		dev->client_timeout = msg.config_timeout[1];
1033 	}
1034 
1035 	if (!msg.operating_channel && !go) {
1036 		/*
1037 		 * Note: P2P Client may omit Operating Channel attribute to
1038 		 * indicate it does not have a preference.
1039 		 */
1040 		p2p_dbg(p2p, "No Operating Channel attribute received");
1041 		status = P2P_SC_FAIL_INVALID_PARAMS;
1042 		goto fail;
1043 	}
1044 	if (!msg.channel_list) {
1045 		p2p_dbg(p2p, "No Channel List attribute received");
1046 		status = P2P_SC_FAIL_INVALID_PARAMS;
1047 		goto fail;
1048 	}
1049 
1050 	if (p2p_peer_channels(p2p, dev, msg.channel_list,
1051 			      msg.channel_list_len) < 0) {
1052 		p2p_dbg(p2p, "No common channels found");
1053 		status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
1054 		goto fail;
1055 	}
1056 
1057 	if (msg.operating_channel) {
1058 		dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1059 						     msg.operating_channel[4]);
1060 		p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
1061 			dev->oper_freq);
1062 	} else
1063 		dev->oper_freq = 0;
1064 
1065 	switch (msg.dev_password_id) {
1066 	case DEV_PW_REGISTRAR_SPECIFIED:
1067 		p2p_dbg(p2p, "PIN from peer Display");
1068 		if (dev->wps_method != WPS_PIN_KEYPAD) {
1069 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1070 				p2p_wps_method_str(dev->wps_method));
1071 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1072 			goto fail;
1073 		}
1074 		break;
1075 	case DEV_PW_USER_SPECIFIED:
1076 		p2p_dbg(p2p, "Peer entered PIN on Keypad");
1077 		if (dev->wps_method != WPS_PIN_DISPLAY) {
1078 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1079 				p2p_wps_method_str(dev->wps_method));
1080 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1081 			goto fail;
1082 		}
1083 		break;
1084 	case DEV_PW_PUSHBUTTON:
1085 		p2p_dbg(p2p, "Peer using pushbutton");
1086 		if (dev->wps_method != WPS_PBC) {
1087 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1088 				p2p_wps_method_str(dev->wps_method));
1089 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1090 			goto fail;
1091 		}
1092 		break;
1093 	default:
1094 		if (msg.dev_password_id &&
1095 		    msg.dev_password_id == dev->oob_pw_id) {
1096 			p2p_dbg(p2p, "Peer using NFC");
1097 			if (dev->wps_method != WPS_NFC) {
1098 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1099 					p2p_wps_method_str(dev->wps_method));
1100 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1101 				goto fail;
1102 			}
1103 			break;
1104 		}
1105 		p2p_dbg(p2p, "Unsupported Device Password ID %d",
1106 			msg.dev_password_id);
1107 		status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1108 		goto fail;
1109 	}
1110 
1111 	if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1112 		goto fail;
1113 
1114 	p2p_set_state(p2p, P2P_GO_NEG);
1115 	p2p_clear_timeout(p2p);
1116 
1117 	p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
1118 	os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1119 
1120 fail:
1121 	/* Store GO Negotiation Confirmation to allow retransmission */
1122 	wpabuf_free(dev->go_neg_conf);
1123 	dev->go_neg_conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token,
1124 						 status, msg.operating_channel,
1125 						 go);
1126 	p2p_parse_free(&msg);
1127 	if (dev->go_neg_conf == NULL)
1128 		return;
1129 	p2p_dbg(p2p, "Sending GO Negotiation Confirm");
1130 	if (status == P2P_SC_SUCCESS) {
1131 		p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1132 		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1133 	} else
1134 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1135 	if (rx_freq > 0)
1136 		freq = rx_freq;
1137 	else
1138 		freq = dev->listen_freq;
1139 
1140 	dev->go_neg_conf_freq = freq;
1141 	dev->go_neg_conf_sent = 0;
1142 
1143 	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1144 			    wpabuf_head(dev->go_neg_conf),
1145 			    wpabuf_len(dev->go_neg_conf), 200) < 0) {
1146 		p2p_dbg(p2p, "Failed to send Action frame");
1147 		p2p_go_neg_failed(p2p, dev, -1);
1148 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1149 	} else
1150 		dev->go_neg_conf_sent++;
1151 	if (status != P2P_SC_SUCCESS) {
1152 		p2p_dbg(p2p, "GO Negotiation failed");
1153 		p2p_go_neg_failed(p2p, dev, status);
1154 	}
1155 }
1156 
1157 
p2p_process_go_neg_conf(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len)1158 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1159 			     const u8 *data, size_t len)
1160 {
1161 	struct p2p_device *dev;
1162 	struct p2p_message msg;
1163 
1164 	p2p_dbg(p2p, "Received GO Negotiation Confirm from " MACSTR,
1165 		MAC2STR(sa));
1166 	dev = p2p_get_device(p2p, sa);
1167 	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1168 	    dev != p2p->go_neg_peer) {
1169 		p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
1170 			MAC2STR(sa));
1171 		return;
1172 	}
1173 
1174 	if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1175 		p2p_dbg(p2p, "Stopped waiting for TX status on GO Negotiation Response since we already received Confirmation");
1176 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1177 	}
1178 
1179 	if (p2p_parse(data, len, &msg))
1180 		return;
1181 
1182 	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1183 		p2p_dbg(p2p, "Was not expecting GO Negotiation Confirm - ignore");
1184 		p2p_parse_free(&msg);
1185 		return;
1186 	}
1187 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1188 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1189 
1190 	if (msg.dialog_token != dev->dialog_token) {
1191 		p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
1192 			msg.dialog_token, dev->dialog_token);
1193 		p2p_parse_free(&msg);
1194 		return;
1195 	}
1196 
1197 	if (!msg.status) {
1198 		p2p_dbg(p2p, "No Status attribute received");
1199 		p2p_parse_free(&msg);
1200 		return;
1201 	}
1202 	if (*msg.status) {
1203 		p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
1204 		p2p_go_neg_failed(p2p, dev, *msg.status);
1205 		p2p_parse_free(&msg);
1206 		return;
1207 	}
1208 
1209 	if (dev->go_state == REMOTE_GO && msg.group_id) {
1210 		/* Store SSID for Provisioning step */
1211 		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1212 		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1213 	} else if (dev->go_state == REMOTE_GO) {
1214 		p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Confirmation");
1215 		p2p->ssid_len = 0;
1216 		p2p_go_neg_failed(p2p, dev, P2P_SC_FAIL_INVALID_PARAMS);
1217 		p2p_parse_free(&msg);
1218 		return;
1219 	}
1220 
1221 	if (!msg.operating_channel) {
1222 		p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1223 #ifdef CONFIG_P2P_STRICT
1224 		p2p_parse_free(&msg);
1225 		return;
1226 #endif /* CONFIG_P2P_STRICT */
1227 	} else if (dev->go_state == REMOTE_GO) {
1228 		int oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1229 						    msg.operating_channel[4]);
1230 		if (oper_freq != dev->oper_freq) {
1231 			p2p_dbg(p2p, "Updated peer (GO) operating channel preference from %d MHz to %d MHz",
1232 				dev->oper_freq, oper_freq);
1233 			dev->oper_freq = oper_freq;
1234 		}
1235 	}
1236 
1237 	if (!msg.channel_list) {
1238 		p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1239 #ifdef CONFIG_P2P_STRICT
1240 		p2p_parse_free(&msg);
1241 		return;
1242 #endif /* CONFIG_P2P_STRICT */
1243 	}
1244 
1245 	p2p_parse_free(&msg);
1246 
1247 	if (dev->go_state == UNKNOWN_GO) {
1248 		/*
1249 		 * This should not happen since GO negotiation has already
1250 		 * been completed.
1251 		 */
1252 		p2p_dbg(p2p, "Unexpected GO Neg state - do not know which end becomes GO");
1253 		return;
1254 	}
1255 
1256 	/*
1257 	 * The peer could have missed our ctrl::ack frame for GO Negotiation
1258 	 * Confirm and continue retransmitting the frame. To reduce the
1259 	 * likelihood of the peer not getting successful TX status for the
1260 	 * GO Negotiation Confirm frame, wait a short time here before starting
1261 	 * the group so that we will remain on the current channel to
1262 	 * acknowledge any possible retransmission from the peer.
1263 	 */
1264 	p2p_dbg(p2p, "20 ms wait on current channel before starting group");
1265 	os_sleep(0, 20000);
1266 
1267 	p2p_go_complete(p2p, dev);
1268 }
1269