• 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 "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "wps/wps_defs.h"
16 #include "p2p_i.h"
17 #include "p2p.h"
18 
19 #ifdef CONFIG_OPEN_HARMONY_PATCH
20 #include "wpa_supplicant_i.h"
21 #define LTECOEX_SAFE_CHANNEL 6
22 #ifdef OPEN_HARMONY_MIRACAST_SINK_OPT
23 #include "securec.h"
24 #include "p2p_supplicant.h"
25 #include "hm_miracast_sink.h"
26 #endif
27 #endif
28 
29 #ifdef CONFIG_OPEN_HARMONY_P2P_DFH_CONNECT
30 #include "p2p_harmony.h"
31 #endif
32 
33 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
34 #define HM_SHARE_FREQ_5G_MIN 4900
35 #define HM_SHARE_FREQ_5G_MAX 6000
36 #define HM_SHARE_FREQ_2G_MIN 2400
37 #define HM_SHARE_FREQ_2G_MAX 2500
38 #endif
39 
p2p_go_det(u8 own_intent,u8 peer_value)40 static int p2p_go_det(u8 own_intent, u8 peer_value)
41 {
42 	u8 peer_intent = peer_value >> 1;
43 	if (own_intent == peer_intent) {
44 		if (own_intent == P2P_MAX_GO_INTENT)
45 			return -1; /* both devices want to become GO */
46 
47 		/* Use tie breaker bit to determine GO */
48 		return (peer_value & 0x01) ? 0 : 1;
49 	}
50 
51 	return own_intent > peer_intent;
52 }
53 
54 
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)55 int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own,
56 			    struct p2p_device *dev,
57 			    const u8 *channel_list, size_t channel_list_len)
58 {
59 	const u8 *pos, *end;
60 	struct p2p_channels *ch;
61 	u8 channels;
62 	struct p2p_channels intersection;
63 
64 	ch = &dev->channels;
65 	os_memset(ch, 0, sizeof(*ch));
66 	pos = channel_list;
67 	end = channel_list + channel_list_len;
68 
69 	if (end - pos < 3)
70 		return -1;
71 	os_memcpy(dev->country, pos, 3);
72 	if (pos[2] != 0x04 && os_memcmp(pos, p2p->cfg->country, 2) != 0) {
73 		p2p_info(p2p, "Mismatching country (ours=** peer's=**)");
74 		return -1;
75 	}
76 	pos += 3;
77 
78 	while (end - pos > 2) {
79 		struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes];
80 		cl->reg_class = *pos++;
81 		channels = *pos++;
82 		if (channels > end - pos) {
83 			p2p_info(p2p, "Invalid peer Channel List");
84 			return -1;
85 		}
86 		cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
87 			P2P_MAX_REG_CLASS_CHANNELS : channels;
88 		os_memcpy(cl->channel, pos, cl->channels);
89 		pos += channels;
90 		ch->reg_classes++;
91 		if (ch->reg_classes == P2P_MAX_REG_CLASSES)
92 			break;
93 	}
94 
95 	p2p_channels_intersect(own, &dev->channels, &intersection);
96 	p2p_dbg(p2p, "Own reg_classes %d peer reg_classes %d intersection reg_classes %d",
97 		(int) own->reg_classes,
98 		(int) dev->channels.reg_classes,
99 		(int) intersection.reg_classes);
100 	if (intersection.reg_classes == 0) {
101 		p2p_info(p2p, "No common channels found");
102 		return -1;
103 	}
104 	return 0;
105 }
106 
107 
p2p_peer_channels(struct p2p_data * p2p,struct p2p_device * dev,const u8 * channel_list,size_t channel_list_len)108 static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
109 			     const u8 *channel_list, size_t channel_list_len)
110 {
111 	return p2p_peer_channels_check(p2p, &p2p->channels, dev,
112 				       channel_list, channel_list_len);
113 }
114 
115 
p2p_wps_method_pw_id(enum p2p_wps_method wps_method)116 u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
117 {
118 	switch (wps_method) {
119 	case WPS_PIN_DISPLAY:
120 		return DEV_PW_REGISTRAR_SPECIFIED;
121 	case WPS_PIN_KEYPAD:
122 		return DEV_PW_USER_SPECIFIED;
123 	case WPS_PBC:
124 		return DEV_PW_PUSHBUTTON;
125 	case WPS_NFC:
126 		return DEV_PW_NFC_CONNECTION_HANDOVER;
127 	case WPS_P2PS:
128 		return DEV_PW_P2PS_DEFAULT;
129 	default:
130 		return DEV_PW_DEFAULT;
131 	}
132 }
133 
134 
p2p_wps_method_str(enum p2p_wps_method wps_method)135 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
136 {
137 	switch (wps_method) {
138 	case WPS_PIN_DISPLAY:
139 		return "Display";
140 	case WPS_PIN_KEYPAD:
141 		return "Keypad";
142 	case WPS_PBC:
143 		return "PBC";
144 	case WPS_NFC:
145 		return "NFC";
146 	case WPS_P2PS:
147 		return "P2PS";
148 	default:
149 		return "??";
150 	}
151 }
152 
153 
p2p_build_go_neg_req(struct p2p_data * p2p,struct p2p_device * peer)154 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
155 					    struct p2p_device *peer)
156 {
157 	struct wpabuf *buf;
158 	u8 *len;
159 	u8 group_capab;
160 	size_t extra = 0;
161 	u16 pw_id;
162 
163 #ifdef CONFIG_WIFI_DISPLAY
164 	if (p2p->wfd_ie_go_neg)
165 		extra = wpabuf_len(p2p->wfd_ie_go_neg);
166 #endif /* CONFIG_WIFI_DISPLAY */
167 
168 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
169 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
170 
171 	buf = wpabuf_alloc(1000 + extra);
172 	if (buf == NULL)
173 		return NULL;
174 
175 	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
176 
177 	len = p2p_buf_add_ie_hdr(buf);
178 	group_capab = 0;
179 	if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
180 		group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
181 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
182 			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
183 	}
184 	if (p2p->cross_connect)
185 		group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
186 	if (p2p->cfg->p2p_intra_bss)
187 		group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
188 	p2p_buf_add_capability(buf, p2p->dev_capab &
189 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
190 			       group_capab);
191 	p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | peer->tie_breaker);
192 	p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
193 	p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
194 				   p2p->cfg->channel);
195 	if (p2p->ext_listen_interval)
196 		p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
197 					      p2p->ext_listen_interval);
198 	p2p_buf_add_intended_addr(buf, p2p->intended_addr);
199 	p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
200 	p2p_buf_add_device_info(buf, p2p, peer);
201 	p2p_buf_add_operating_channel(buf, p2p->cfg->country,
202 				      p2p->op_reg_class, p2p->op_channel);
203 	p2p_buf_update_ie_hdr(buf, len);
204 
205 	p2p_buf_add_pref_channel_list(buf, p2p->pref_freq_list,
206 				      p2p->num_pref_freq);
207 
208 	/* WPS IE with Device Password ID attribute */
209 	pw_id = p2p_wps_method_pw_id(peer->wps_method);
210 	if (peer->oob_pw_id)
211 		pw_id = peer->oob_pw_id;
212 	if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
213 		p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Request");
214 		wpabuf_free(buf);
215 		return NULL;
216 	}
217 
218 #ifdef CONFIG_WIFI_DISPLAY
219 	if (p2p->wfd_ie_go_neg)
220 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
221 #endif /* CONFIG_WIFI_DISPLAY */
222 
223 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
224 		wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
225 
226 	return buf;
227 }
228 
229 
p2p_connect_send(struct p2p_data * p2p,struct p2p_device * dev)230 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
231 {
232 	struct wpabuf *req;
233 	int freq;
234 
235 	if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
236 		u16 config_method;
237 		p2p_dbg(p2p, "Use PD-before-GO-Neg workaround for " MACSTR_SEC,
238 			MAC2STR_SEC(dev->info.p2p_device_addr));
239 		if (dev->wps_method == WPS_PIN_DISPLAY)
240 			config_method = WPS_CONFIG_KEYPAD;
241 		else if (dev->wps_method == WPS_PIN_KEYPAD)
242 			config_method = WPS_CONFIG_DISPLAY;
243 		else if (dev->wps_method == WPS_PBC)
244 			config_method = WPS_CONFIG_PUSHBUTTON;
245 		else if (dev->wps_method == WPS_P2PS)
246 			config_method = WPS_CONFIG_P2PS;
247 		else
248 			return -1;
249 		return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
250 					 NULL, config_method, 0, 0, 1);
251 	}
252 
253 	freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
254 	if (dev->oob_go_neg_freq > 0)
255 		freq = dev->oob_go_neg_freq;
256 	if (freq <= 0) {
257 		p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
258 			MACSTR_SEC " to send GO Negotiation Request",
259 			MAC2STR_SEC(dev->info.p2p_device_addr));
260 		return -1;
261 	}
262 
263 	req = p2p_build_go_neg_req(p2p, dev);
264 	if (req == NULL)
265 		return -1;
266 	p2p_warning(p2p, "Sending GO Negotiation Request");
267 	p2p_set_state(p2p, P2P_CONNECT);
268 	p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
269 	p2p->go_neg_peer = dev;
270 	eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
271 	dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
272 	dev->connect_reqs++;
273 	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
274 			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
275 			    wpabuf_head(req), wpabuf_len(req), 500) < 0) {
276 		p2p_dbg(p2p, "Failed to send Action frame");
277 		/* Use P2P find to recover and retry */
278 		p2p_set_timeout(p2p, 0, 0);
279 	} else
280 		dev->go_neg_req_sent++;
281 
282 	wpabuf_free(req);
283 
284 	return 0;
285 }
286 
287 
p2p_build_go_neg_resp(struct p2p_data * p2p,struct p2p_device * peer,u8 dialog_token,u8 status,u8 tie_breaker)288 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
289 					     struct p2p_device *peer,
290 					     u8 dialog_token, u8 status,
291 					     u8 tie_breaker)
292 {
293 	struct wpabuf *buf;
294 	u8 *len;
295 	u8 group_capab;
296 	size_t extra = 0;
297 	u16 pw_id;
298 
299 	p2p_dbg(p2p, "Building GO Negotiation Response");
300 
301 #ifdef CONFIG_WIFI_DISPLAY
302 	if (p2p->wfd_ie_go_neg)
303 		extra = wpabuf_len(p2p->wfd_ie_go_neg);
304 #endif /* CONFIG_WIFI_DISPLAY */
305 
306 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
307 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
308 
309 	buf = wpabuf_alloc(1000 + extra);
310 	if (buf == NULL)
311 		return NULL;
312 
313 	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
314 
315 	len = p2p_buf_add_ie_hdr(buf);
316 	p2p_buf_add_status(buf, status);
317 	group_capab = 0;
318 	if (peer && peer->go_state == LOCAL_GO) {
319 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
320 			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
321 			if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
322 				group_capab |=
323 					P2P_GROUP_CAPAB_PERSISTENT_RECONN;
324 		}
325 		if (p2p->cross_connect)
326 			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
327 		if (p2p->cfg->p2p_intra_bss)
328 			group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
329 	}
330 	p2p_buf_add_capability(buf, p2p->dev_capab &
331 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
332 			       group_capab);
333 	p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
334 	p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
335 	if (p2p->override_pref_op_class) {
336 		p2p_dbg(p2p, "Override operating channel preference");
337 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
338 					      p2p->override_pref_op_class,
339 					      p2p->override_pref_channel);
340 	} else if (peer && peer->go_state == REMOTE_GO && !p2p->num_pref_freq) {
341 		p2p_dbg(p2p, "Omit Operating Channel attribute");
342 	} else {
343 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
344 					      p2p->op_reg_class,
345 					      p2p->op_channel);
346 	}
347 	p2p_buf_add_intended_addr(buf, p2p->intended_addr);
348 	if (status || peer == NULL) {
349 		p2p_buf_add_channel_list(buf, p2p->cfg->country,
350 					 &p2p->channels);
351 	} else if (peer->go_state == REMOTE_GO) {
352 		p2p_buf_add_channel_list(buf, p2p->cfg->country,
353 					 &p2p->channels);
354 	} else {
355 		struct p2p_channels res;
356 		p2p_channels_intersect(&p2p->channels, &peer->channels,
357 				       &res);
358 		p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
359 	}
360 	p2p_buf_add_device_info(buf, p2p, peer);
361 	if (peer && peer->go_state == LOCAL_GO) {
362 		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
363 				     p2p->ssid_len);
364 	}
365 	p2p_buf_update_ie_hdr(buf, len);
366 
367 	/* WPS IE with Device Password ID attribute */
368 	pw_id = p2p_wps_method_pw_id(peer ? peer->wps_method : WPS_NOT_READY);
369 	if (peer && peer->oob_pw_id)
370 		pw_id = peer->oob_pw_id;
371 	if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
372 		p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Response");
373 		wpabuf_free(buf);
374 		return NULL;
375 	}
376 
377 #ifdef CONFIG_WIFI_DISPLAY
378 	if (p2p->wfd_ie_go_neg)
379 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
380 #endif /* CONFIG_WIFI_DISPLAY */
381 
382 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
383 		wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
384 
385 	return buf;
386 }
387 
388 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
hm_get_share_freq(struct p2p_data * p2p,int * share_freq)389 int hm_get_share_freq(struct p2p_data *p2p, int *share_freq)
390 {
391 	struct wpa_supplicant *wpa_s = p2p->cfg->cb_ctx;
392 	struct wpa_used_freq_data *freqs;
393 	int num;
394 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(struct wpa_used_freq_data));
395 	if (!freqs) {
396 		return 0;
397 	}
398 	num = get_shared_radio_freqs_data(wpa_s, freqs, wpa_s->num_multichan_concurrent);
399 	if (num > 0) {
400 		*share_freq = freqs[0].freq;
401 	}
402 	for (u8 i = 0; i < num; i++) {
403 		if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
404 			*share_freq = freqs[i].freq;
405 			break;
406 		}
407 		if (freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT) {
408 			*share_freq = freqs[i].freq;
409 		}
410 	}
411 	os_free(freqs);
412 	return num;
413 }
414 #endif
415 
416 /**
417  * p2p_reselect_channel - Re-select operating channel based on peer information
418  * @p2p: P2P module context from p2p_init()
419  * @intersection: Support channel list intersection from local and peer
420  *
421  * This function is used to re-select the best channel after having received
422  * information from the peer to allow supported channel lists to be intersected.
423  * This can be used to improve initial channel selection done in
424  * p2p_prepare_channel() prior to the start of GO Negotiation. In addition, this
425  * can be used for Invitation case.
426  */
p2p_reselect_channel(struct p2p_data * p2p,struct p2p_channels * intersection)427 void p2p_reselect_channel(struct p2p_data *p2p,
428 			  struct p2p_channels *intersection)
429 {
430 	struct p2p_reg_class *cl;
431 	int freq;
432 	u8 op_reg_class, op_channel;
433 	unsigned int i;
434 	const int op_classes_5ghz[] = { 124, 125, 115, 0 };
435 	const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
436 	const int op_classes_vht[] = { 128, 129, 130, 0 };
437 	const int op_classes_edmg[] = { 181, 182, 183, 0 };
438 
439 	if (p2p->own_freq_preference > 0 &&
440 	    p2p_freq_to_channel(p2p->own_freq_preference,
441 				&op_reg_class, &op_channel) == 0 &&
442 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
443 		p2p_dbg(p2p, "Pick own channel preference (reg_class %u channel %u) from intersection",
444 			op_reg_class, op_channel);
445 		p2p->op_reg_class = op_reg_class;
446 		p2p->op_channel = op_channel;
447 		return;
448 	}
449 
450 	if (p2p->best_freq_overall > 0 &&
451 	    p2p_freq_to_channel(p2p->best_freq_overall,
452 				&op_reg_class, &op_channel) == 0 &&
453 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
454 		p2p_dbg(p2p, "Pick best overall channel (reg_class %u channel %u) from intersection",
455 			op_reg_class, op_channel);
456 		p2p->op_reg_class = op_reg_class;
457 		p2p->op_channel = op_channel;
458 		return;
459 	}
460 
461 	/* First, try to pick the best channel from another band */
462 	freq = p2p_channel_to_freq(p2p->op_reg_class, p2p->op_channel);
463 	if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
464 	    !p2p_channels_includes(intersection, p2p->op_reg_class,
465 				   p2p->op_channel) &&
466 	    p2p_freq_to_channel(p2p->best_freq_5,
467 				&op_reg_class, &op_channel) == 0 &&
468 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
469 		p2p_dbg(p2p, "Pick best 5 GHz channel (reg_class %u channel %u) from intersection",
470 			op_reg_class, op_channel);
471 		p2p->op_reg_class = op_reg_class;
472 		p2p->op_channel = op_channel;
473 		return;
474 	}
475 
476 	if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
477 	    !p2p_channels_includes(intersection, p2p->op_reg_class,
478 				   p2p->op_channel) &&
479 	    p2p_freq_to_channel(p2p->best_freq_24,
480 				&op_reg_class, &op_channel) == 0 &&
481 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
482 		p2p_dbg(p2p, "Pick best 2.4 GHz channel (reg_class %u channel %u) from intersection",
483 			op_reg_class, op_channel);
484 		p2p->op_reg_class = op_reg_class;
485 		p2p->op_channel = op_channel;
486 		return;
487 	}
488 
489 	/* Select channel with highest preference if the peer supports it */
490 	for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
491 		if (p2p_channels_includes(intersection,
492 					  p2p->cfg->pref_chan[i].op_class,
493 					  p2p->cfg->pref_chan[i].chan)) {
494 			p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
495 			p2p->op_channel = p2p->cfg->pref_chan[i].chan;
496 			p2p_dbg(p2p, "Pick highest preferred channel (op_class %u channel %u) from intersection",
497 				p2p->op_reg_class, p2p->op_channel);
498 			return;
499 		}
500 	}
501 
502 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
503 	struct wpa_supplicant *wpa_s = p2p->cfg->cb_ctx;
504 	int num = 0;
505 	int share_freq = 0;
506 #ifdef OPEN_HARMONY_MIRACAST_SINK_OPT
507 	int best_freq = 0;
508 	int ret;
509 	/* If the wifi is associated wifi 5G, the channel of wlan0 is preferred as the working channel of the GO. */
510 	ret = hm_p2p_pick_op_channel_in_5g_assoc(p2p, intersection, wpa_s, &best_freq, &num);
511 	if (ret == HM_SUCC) {
512 		p2p_dbg(p2p, "Pick our sta channel (reg_class %u channel %u) as p2p op channel",
513 			p2p->op_reg_class, p2p->op_channel);
514 		return;
515 	}
516 #else
517 	num = hm_get_share_freq(p2p, &share_freq);
518 	p2p_dbg(p2p, "p2p_reselect_channel:num_MCC %d, shared_freq_num %u", wpa_s->num_multichan_concurrent, num);
519 	/* follow share freq 5G */
520 	if (num > 0 && share_freq >= HM_SHARE_FREQ_5G_MIN && share_freq < HM_SHARE_FREQ_5G_MAX) {
521 		if (p2p_freq_to_channel(share_freq, &op_reg_class, &op_channel) == 0 &&
522 			p2p_channels_includes(intersection, op_reg_class, op_channel)) {
523 			p2p_dbg(p2p, "share freq on 5G (reg_class %u channel %u)", op_reg_class, op_channel);
524 			p2p->op_reg_class = op_reg_class;
525 			p2p->op_channel = op_channel;
526 			return;
527 		}
528 	}
529 #endif
530 #endif
531 
532 	/* Try a channel where we might be able to use EDMG */
533 	if (p2p_channel_select(intersection, op_classes_edmg,
534 			       &p2p->op_reg_class, &p2p->op_channel) == 0) {
535 		p2p_dbg(p2p, "Pick possible EDMG channel (op_class %u channel %u) from intersection",
536 			p2p->op_reg_class, p2p->op_channel);
537 		return;
538 	}
539 
540 	/* Try a channel where we might be able to use VHT */
541 	if (p2p_channel_select(intersection, op_classes_vht,
542 			       &p2p->op_reg_class, &p2p->op_channel) == 0) {
543 		p2p_dbg(p2p, "Pick possible VHT channel (op_class %u channel %u) from intersection",
544 			p2p->op_reg_class, p2p->op_channel);
545 		return;
546 	}
547 
548 	/* Try a channel where we might be able to use HT40 */
549 	if (p2p_channel_select(intersection, op_classes_ht40,
550 			       &p2p->op_reg_class, &p2p->op_channel) == 0) {
551 		p2p_dbg(p2p, "Pick possible HT40 channel (op_class %u channel %u) from intersection",
552 			p2p->op_reg_class, p2p->op_channel);
553 		return;
554 	}
555 
556 	/* Prefer a 5 GHz channel */
557 	if (p2p_channel_select(intersection, op_classes_5ghz,
558 			       &p2p->op_reg_class, &p2p->op_channel) == 0) {
559 		p2p_dbg(p2p, "Pick possible 5 GHz channel (op_class %u channel %u) from intersection",
560 			p2p->op_reg_class, p2p->op_channel);
561 		return;
562 	}
563 
564 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
565 #ifdef OPEN_HARMONY_MIRACAST_SINK_OPT
566 	/* intersection not find 5G channel, so only support 2.4G channel */
567 	if (hm_pick_2g_op_channel(p2p, intersection, num, best_freq) == HM_SUCC) {
568 		p2p_dbg(p2p, "p2p_reselect_channel Pick 2.4g channel (op_class %u channel %u) ok",
569 			p2p->op_reg_class, p2p->op_channel);
570 		return;
571 	}
572 #else
573 	/* follow share freq 2G */
574 	if (num > 0 && share_freq >= HM_SHARE_FREQ_2G_MIN && share_freq < HM_SHARE_FREQ_2G_MAX) {
575 		if (p2p_freq_to_channel(share_freq, &op_reg_class, &op_channel) == 0 &&
576 			p2p_channels_includes(intersection, op_reg_class, op_channel)) {
577 			p2p_dbg(p2p, "share freq on 2G (reg_class %u channel %u)", op_reg_class, op_channel);
578 			p2p->op_reg_class = op_reg_class;
579 			p2p->op_channel = op_channel;
580 			return;
581 		}
582 	}
583 #endif
584 #endif
585 
586 	/*
587 	 * Try to see if the original channel is in the intersection. If
588 	 * so, no need to change anything, as it already contains some
589 	 * randomness.
590 	 */
591 	if (p2p_channels_includes(intersection, p2p->op_reg_class,
592 				  p2p->op_channel)) {
593 		p2p_dbg(p2p, "Using original operating class and channel (op_class %u channel %u) from intersection",
594 			p2p->op_reg_class, p2p->op_channel);
595 		return;
596 	}
597 
598 	/*
599 	 * Fall back to whatever is included in the channel intersection since
600 	 * no better options seems to be available.
601 	 */
602 	cl = &intersection->reg_class[0];
603 	p2p_dbg(p2p, "Pick another channel (reg_class %u channel %u) from intersection",
604 		cl->reg_class, cl->channel[0]);
605 	p2p->op_reg_class = cl->reg_class;
606 	p2p->op_channel = cl->channel[0];
607 }
608 
609 
p2p_go_select_channel(struct p2p_data * p2p,struct p2p_device * dev,u8 * status)610 int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
611 			  u8 *status)
612 {
613 	struct p2p_channels tmp, intersection;
614 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
615 	int pvt_peer = FALSE;
616 	struct hm_p2p_pvt_peer pvt_peer_info;
617 #endif
618 
619 	p2p_channels_dump(p2p, "own channels", &p2p->channels);
620 	p2p_channels_dump(p2p, "peer channels", &dev->channels);
621 	p2p_channels_intersect(&p2p->channels, &dev->channels, &tmp);
622 	p2p_channels_dump(p2p, "intersection", &tmp);
623 	p2p_channels_remove_freqs(&tmp, &p2p->no_go_freq);
624 	p2p_channels_dump(p2p, "intersection after no-GO removal", &tmp);
625 	p2p_channels_intersect(&tmp, &p2p->cfg->channels, &intersection);
626 	p2p_channels_dump(p2p, "intersection with local channel list",
627 			  &intersection);
628 	if (intersection.reg_classes == 0 ||
629 	    intersection.reg_class[0].channels == 0) {
630 		*status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
631 		p2p_dbg(p2p, "No common channels found");
632 		return -1;
633 	}
634 
635 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
636 	pvt_peer = hm_p2p_get_peer_info(dev, &pvt_peer_info);
637 
638 	p2p_dbg(p2p, "origin pepare operating channel (op_class %u channel %u)",
639 		p2p->op_reg_class, p2p->op_channel);
640 
641 	if (pvt_peer == TRUE) {
642 		hm_p2p_pvt_peer_select_channel(p2p, &intersection, &pvt_peer_info);
643 	} else {
644 #endif
645 	if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
646 				   p2p->op_channel)) {
647 		if (dev->flags & P2P_DEV_FORCE_FREQ) {
648 			*status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
649 			p2p_dbg(p2p, "Peer does not support the forced channel");
650 			return -1;
651 		}
652 
653 		p2p_dbg(p2p, "Selected operating channel (op_class %u channel %u) not acceptable to the peer",
654 			p2p->op_reg_class, p2p->op_channel);
655 		p2p_reselect_channel(p2p, &intersection);
656 	} else if (!(dev->flags & P2P_DEV_FORCE_FREQ) &&
657 		   !p2p->cfg->cfg_op_channel) {
658 		p2p_dbg(p2p, "Try to optimize channel selection with peer information received; previously selected op_class %u channel %u",
659 			p2p->op_reg_class, p2p->op_channel);
660 		p2p_reselect_channel(p2p, &intersection);
661 	}
662 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
663 	}
664 #endif
665 
666 	if (!p2p->ssid_set) {
667 		p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
668 		p2p->ssid_set = 1;
669 	}
670 
671 	return 0;
672 }
673 
674 
p2p_check_pref_chan_no_recv(struct p2p_data * p2p,int go,struct p2p_device * dev,struct p2p_message * msg,unsigned freq_list[],unsigned int size)675 static void p2p_check_pref_chan_no_recv(struct p2p_data *p2p, int go,
676 					struct p2p_device *dev,
677 					struct p2p_message *msg,
678 					unsigned freq_list[], unsigned int size)
679 {
680 	u8 op_class, op_channel;
681 	unsigned int oper_freq = 0, i, j;
682 	int found = 0;
683 
684 	p2p_dbg(p2p,
685 		"Peer didn't provide a preferred frequency list, see if any of our preferred channels are supported by peer device");
686 
687 	/*
688 	 * Search for a common channel in our preferred frequency list which is
689 	 * also supported by the peer device.
690 	 */
691 	for (i = 0; i < size && !found; i++) {
692 		/* Make sure that the common frequency is supported by peer. */
693 		oper_freq = freq_list[i];
694 		if (p2p_freq_to_channel(oper_freq, &op_class,
695 					&op_channel) < 0)
696 			continue; /* cannot happen due to earlier check */
697 		for (j = 0; j < msg->channel_list_len; j++) {
698 			if (!msg->channel_list ||
699 			    op_channel != msg->channel_list[j])
700 				continue;
701 
702 			p2p->op_reg_class = op_class;
703 			p2p->op_channel = op_channel;
704 			os_memcpy(&p2p->channels, &p2p->cfg->channels,
705 				  sizeof(struct p2p_channels));
706 			found = 1;
707 			break;
708 		}
709 	}
710 
711 	if (found) {
712 		p2p_dbg(p2p,
713 			"Freq %d MHz is a preferred channel and is also supported by peer, use it as the operating channel",
714 			oper_freq);
715 	} else {
716 		p2p_dbg(p2p,
717 			"None of our preferred channels are supported by peer!");
718 	}
719 }
720 
721 
p2p_check_pref_chan_recv(struct p2p_data * p2p,int go,struct p2p_device * dev,struct p2p_message * msg,unsigned freq_list[],unsigned int size)722 static void p2p_check_pref_chan_recv(struct p2p_data *p2p, int go,
723 				     struct p2p_device *dev,
724 				     struct p2p_message *msg,
725 				     unsigned freq_list[], unsigned int size)
726 {
727 	u8 op_class, op_channel;
728 	unsigned int oper_freq = 0, i, j;
729 	int found = 0;
730 
731 	/*
732 	 * Peer device supports a Preferred Frequency List.
733 	 * Search for a common channel in the preferred frequency lists
734 	 * of both peer and local devices.
735 	 */
736 	for (i = 0; i < size && !found; i++) {
737 		for (j = 2; j < (msg->pref_freq_list_len / 2); j++) {
738 			oper_freq = p2p_channel_to_freq(
739 				msg->pref_freq_list[2 * j],
740 				msg->pref_freq_list[2 * j + 1]);
741 			if (freq_list[i] != oper_freq)
742 				continue;
743 			if (p2p_freq_to_channel(oper_freq, &op_class,
744 						&op_channel) < 0)
745 				continue; /* cannot happen */
746 			p2p->op_reg_class = op_class;
747 			p2p->op_channel = op_channel;
748 			os_memcpy(&p2p->channels, &p2p->cfg->channels,
749 				  sizeof(struct p2p_channels));
750 			found = 1;
751 			break;
752 		}
753 	}
754 
755 	if (found) {
756 		p2p_dbg(p2p,
757 			"Freq %d MHz is a common preferred channel for both peer and local, use it as operating channel",
758 			oper_freq);
759 	} else {
760 		p2p_dbg(p2p, "No common preferred channels found!");
761 	}
762 }
763 
764 
p2p_check_pref_chan(struct p2p_data * p2p,int go,struct p2p_device * dev,struct p2p_message * msg)765 void p2p_check_pref_chan(struct p2p_data *p2p, int go,
766 			 struct p2p_device *dev, struct p2p_message *msg)
767 {
768 	unsigned int freq_list[P2P_MAX_PREF_CHANNELS], size;
769 	unsigned int i;
770 	u8 op_class, op_channel;
771 	char txt[100], *pos, *end;
772 	int res;
773 
774 	/*
775 	 * Use the preferred channel list from the driver only if there is no
776 	 * forced_freq, e.g., P2P_CONNECT freq=..., and no preferred operating
777 	 * channel hardcoded in the configuration file.
778 	 */
779 	if (!p2p->cfg->get_pref_freq_list || p2p->cfg->num_pref_chan ||
780 	    (dev->flags & P2P_DEV_FORCE_FREQ) || p2p->cfg->cfg_op_channel)
781 		return;
782 
783 	/* Obtain our preferred frequency list from driver based on P2P role. */
784 	size = P2P_MAX_PREF_CHANNELS;
785 	if (p2p->cfg->get_pref_freq_list(p2p->cfg->cb_ctx, go, &size,
786 					 freq_list))
787 		return;
788 	/* Filter out frequencies that are not acceptable for P2P use */
789 	i = 0;
790 	while (i < size) {
791 		if (p2p_freq_to_channel(freq_list[i], &op_class,
792 					&op_channel) < 0 ||
793 		    (!p2p_channels_includes(&p2p->cfg->channels,
794 					    op_class, op_channel) &&
795 		     (go || !p2p_channels_includes(&p2p->cfg->cli_channels,
796 						   op_class, op_channel)))) {
797 			p2p_dbg(p2p,
798 				"Ignore local driver frequency preference %u MHz since it is not acceptable for P2P use (go=%d)",
799 				freq_list[i], go);
800 			if (size - i - 1 > 0)
801 				os_memmove(&freq_list[i], &freq_list[i + 1],
802 					   (size - i - 1) *
803 					   sizeof(unsigned int));
804 			size--;
805 			continue;
806 		}
807 
808 		/* Preferred frequency is acceptable for P2P use */
809 		i++;
810 	}
811 
812 	pos = txt;
813 	end = pos + sizeof(txt);
814 	for (i = 0; i < size; i++) {
815 		res = os_snprintf(pos, end - pos, " %u", freq_list[i]);
816 		if (os_snprintf_error(end - pos, res))
817 			break;
818 		pos += res;
819 	}
820 	*pos = '\0';
821 	p2p_dbg(p2p, "Local driver frequency preference (size=%u):%s",
822 		size, txt);
823 
824 	/*
825 	 * Check if peer's preference of operating channel is in
826 	 * our preferred channel list.
827 	 */
828 	for (i = 0; i < size; i++) {
829 		if (freq_list[i] == (unsigned int) dev->oper_freq)
830 			break;
831 	}
832 	if (i != size &&
833 	    p2p_freq_to_channel(freq_list[i], &op_class, &op_channel) == 0) {
834 		/* Peer operating channel preference matches our preference */
835 		p2p->op_reg_class = op_class;
836 		p2p->op_channel = op_channel;
837 		os_memcpy(&p2p->channels, &p2p->cfg->channels,
838 			  sizeof(struct p2p_channels));
839 		return;
840 	}
841 
842 	p2p_dbg(p2p,
843 		"Peer operating channel preference: %d MHz is not in our preferred channel list",
844 		dev->oper_freq);
845 
846 	/*
847 	  Check if peer's preferred channel list is
848 	  * _not_ included in the GO Negotiation Request or Invitation Request.
849 	  */
850 	if (msg->pref_freq_list_len == 0)
851 		p2p_check_pref_chan_no_recv(p2p, go, dev, msg, freq_list, size);
852 	else
853 		p2p_check_pref_chan_recv(p2p, go, dev, msg, freq_list, size);
854 }
855 
856 
p2p_process_go_neg_req(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len,int rx_freq)857 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
858 			    const u8 *data, size_t len, int rx_freq)
859 {
860 	struct p2p_device *dev = NULL;
861 	struct wpabuf *resp;
862 	struct p2p_message msg;
863 	u8 status = P2P_SC_FAIL_INVALID_PARAMS;
864 	int tie_breaker = 0;
865 	int freq;
866 
867 	p2p_warning(p2p, "Received GO Negotiation Request from " MACSTR_SEC "(freq=%d)",
868 		MAC2STR_SEC(sa), rx_freq);
869 
870 	if (p2p_parse(data, len, &msg))
871 		return;
872 
873 	if (!msg.capability) {
874 		p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Request");
875 #ifdef CONFIG_P2P_STRICT
876 		goto fail;
877 #endif /* CONFIG_P2P_STRICT */
878 	}
879 
880 	if (msg.go_intent)
881 		tie_breaker = *msg.go_intent & 0x01;
882 	else {
883 		p2p_dbg(p2p, "Mandatory GO Intent attribute missing from GO Negotiation Request");
884 #ifdef CONFIG_P2P_STRICT
885 		goto fail;
886 #endif /* CONFIG_P2P_STRICT */
887 	}
888 
889 	if (!msg.config_timeout) {
890 		p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Request");
891 #ifdef CONFIG_P2P_STRICT
892 		goto fail;
893 #endif /* CONFIG_P2P_STRICT */
894 	}
895 
896 	if (!msg.listen_channel) {
897 		p2p_dbg(p2p, "No Listen Channel attribute received");
898 		goto fail;
899 	}
900 	if (!msg.operating_channel) {
901 		p2p_dbg(p2p, "No Operating Channel attribute received");
902 		goto fail;
903 	}
904 	if (!msg.channel_list) {
905 		p2p_dbg(p2p, "No Channel List attribute received");
906 		goto fail;
907 	}
908 	if (!msg.intended_addr) {
909 		p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
910 		goto fail;
911 	}
912 	if (!msg.p2p_device_info) {
913 		p2p_dbg(p2p, "No P2P Device Info attribute received");
914 		goto fail;
915 	}
916 
917 	if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
918 		p2p_dbg(p2p, "Unexpected GO Negotiation Request SA=" MACSTR_SEC
919 			" != dev_addr=" MACSTR_SEC,
920 			MAC2STR_SEC(sa), MAC2STR_SEC(msg.p2p_device_addr));
921 		goto fail;
922 	}
923 
924 	dev = p2p_get_device(p2p, sa);
925 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
926 	if (dev && p2p->cfg && p2p->cfg->dev_found)
927 		p2p->cfg->dev_found(p2p->cfg->cb_ctx, sa, &dev->info,
928 					!(dev->flags & P2P_DEV_REPORTED_ONCE));
929 #endif
930 	if (msg.status && *msg.status) {
931 		p2p_dbg(p2p, "Unexpected Status attribute (%d) in GO Negotiation Request",
932 			*msg.status);
933 		if (dev && p2p->go_neg_peer == dev &&
934 		    *msg.status == P2P_SC_FAIL_REJECTED_BY_USER) {
935 			/*
936 			 * This mechanism for using Status attribute in GO
937 			 * Negotiation Request is not compliant with the P2P
938 			 * specification, but some deployed devices use it to
939 			 * indicate rejection of GO Negotiation in a case where
940 			 * they have sent out GO Negotiation Response with
941 			 * status 1. The P2P specification explicitly disallows
942 			 * this. To avoid unnecessary interoperability issues
943 			 * and extra frames, mark the pending negotiation as
944 			 * failed and do not reply to this GO Negotiation
945 			 * Request frame.
946 			 */
947 			p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
948 			p2p_go_neg_failed(p2p, *msg.status);
949 			p2p_parse_free(&msg);
950 			return;
951 		}
952 		goto fail;
953 	}
954 
955 	if (dev == NULL)
956 		dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
957 	else if ((dev->flags & P2P_DEV_PROBE_REQ_ONLY) ||
958 		  !(dev->flags & P2P_DEV_REPORTED))
959 		p2p_add_dev_info(p2p, sa, dev, &msg);
960 	else if (!dev->listen_freq && !dev->oper_freq) {
961 		/*
962 		 * This may happen if the peer entry was added based on PD
963 		 * Request and no Probe Request/Response frame has been received
964 		 * from this peer (or that information has timed out).
965 		 */
966 		p2p_dbg(p2p, "Update peer " MACSTR_SEC
967 			" based on GO Neg Req since listen/oper freq not known",
968 			MAC2STR_SEC(dev->info.p2p_device_addr));
969 		p2p_add_dev_info(p2p, sa, dev, &msg);
970 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
971 	} else if (dev->info.group_capab != msg.capability[1]) {
972 		p2p_add_dev_info(p2p, sa, dev, &msg);
973 #endif /* HARMONY_P2P_CONNECTIVITY_PATCH */
974 	}
975 
976 	if (p2p->go_neg_peer && p2p->go_neg_peer == dev)
977 		eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
978 
979 	if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
980 		p2p_dbg(p2p, "User has rejected this peer");
981 		status = P2P_SC_FAIL_REJECTED_BY_USER;
982 	} else if (dev == NULL ||
983 		   (dev->wps_method == WPS_NOT_READY &&
984 		    (p2p->authorized_oob_dev_pw_id == 0 ||
985 		     p2p->authorized_oob_dev_pw_id !=
986 		     msg.dev_password_id))) {
987 		p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR_SEC,
988 			MAC2STR_SEC(sa));
989 
990 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
991 	if (dev != NULL) {
992 		/*
993 		 * If the intent value of the peer end off the screen is not the maximum value.
994 		 * Set the local intent value to the maximum value,
995 		 * and the purpose is to make the big scree try to do GO.
996 		*/
997 		u8 operating_channel = ((msg.operating_channel == NULL) ? 0 :
998 			msg.operating_channel[HM_OPERATING_CHANNEL_POS]);
999 		hm_p2p_update_peer_info(dev->info.p2p_device_addr,
1000 			HM_GO_NEG_REQ, operating_channel ,NULL);
1001 		hm_p2p_save_go_req_count(dev->info.p2p_device_addr, HM_GO_NEG_REQ, &msg);
1002 		hm_p2p_calculate_go_intent(p2p, &msg, dev->info.p2p_device_addr);
1003 	}
1004 #endif
1005 		status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1006 		p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
1007 					msg.dev_password_id,
1008 					msg.go_intent ? (*msg.go_intent >> 1) :
1009 					0);
1010 	} else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
1011 		p2p_dbg(p2p, "Already in Group Formation with another peer");
1012 		status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1013 	} else {
1014 		int go;
1015 
1016 		if (!p2p->go_neg_peer) {
1017 			p2p_dbg(p2p, "Starting GO Negotiation with previously authorized peer");
1018 			if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
1019 				p2p_dbg(p2p, "Use default channel settings");
1020 				p2p->op_reg_class = p2p->cfg->op_reg_class;
1021 				p2p->op_channel = p2p->cfg->op_channel;
1022 				os_memcpy(&p2p->channels, &p2p->cfg->channels,
1023 					  sizeof(struct p2p_channels));
1024 			} else {
1025 				p2p_dbg(p2p, "Use previously configured forced channel settings");
1026 			}
1027 		}
1028 
1029 		dev->flags &= ~P2P_DEV_NOT_YET_READY;
1030 
1031 		if (!msg.go_intent) {
1032 			p2p_dbg(p2p, "No GO Intent attribute received");
1033 			goto fail;
1034 		}
1035 		if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
1036 			p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
1037 				*msg.go_intent >> 1);
1038 			goto fail;
1039 		}
1040 
1041 		if (dev->go_neg_req_sent &&
1042 		    os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
1043 			p2p_dbg(p2p, "Do not reply since peer has higher address and GO Neg Request already sent");
1044 			p2p_parse_free(&msg);
1045 			return;
1046 		}
1047 
1048 		if (dev->go_neg_req_sent &&
1049 		    (dev->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
1050 			p2p_dbg(p2p,
1051 				"Do not reply since peer is waiting for us to start a new GO Negotiation and GO Neg Request already sent");
1052 			p2p_parse_free(&msg);
1053 			return;
1054 		}
1055 
1056 		go = p2p_go_det(p2p->go_intent, *msg.go_intent);
1057 		if (go < 0) {
1058 			p2p_dbg(p2p, "Incompatible GO Intent");
1059 			status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
1060 			goto fail;
1061 		}
1062 
1063 		if (p2p_peer_channels(p2p, dev, msg.channel_list,
1064 				      msg.channel_list_len) < 0) {
1065 			p2p_dbg(p2p, "No common channels found");
1066 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
1067 			goto fail;
1068 		}
1069 
1070 		switch (msg.dev_password_id) {
1071 		case DEV_PW_REGISTRAR_SPECIFIED:
1072 			p2p_dbg(p2p, "PIN from peer Display");
1073 			if (dev->wps_method != WPS_PIN_KEYPAD) {
1074 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1075 					p2p_wps_method_str(dev->wps_method));
1076 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1077 				goto fail;
1078 			}
1079 			break;
1080 		case DEV_PW_USER_SPECIFIED:
1081 			p2p_dbg(p2p, "Peer entered PIN on Keypad");
1082 			if (dev->wps_method != WPS_PIN_DISPLAY) {
1083 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1084 					p2p_wps_method_str(dev->wps_method));
1085 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1086 				goto fail;
1087 			}
1088 			break;
1089 		case DEV_PW_PUSHBUTTON:
1090 			p2p_dbg(p2p, "Peer using pushbutton");
1091 			if (dev->wps_method != WPS_PBC) {
1092 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1093 					p2p_wps_method_str(dev->wps_method));
1094 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1095 				goto fail;
1096 			}
1097 			break;
1098 		case DEV_PW_P2PS_DEFAULT:
1099 			p2p_dbg(p2p, "Peer using P2PS pin");
1100 			if (dev->wps_method != WPS_P2PS) {
1101 				p2p_dbg(p2p,
1102 					"We have wps_method=%s -> incompatible",
1103 					p2p_wps_method_str(dev->wps_method));
1104 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1105 				goto fail;
1106 			}
1107 			break;
1108 		default:
1109 			if (msg.dev_password_id &&
1110 			    msg.dev_password_id == dev->oob_pw_id) {
1111 				p2p_dbg(p2p, "Peer using NFC");
1112 				if (dev->wps_method != WPS_NFC) {
1113 					p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1114 						p2p_wps_method_str(
1115 							dev->wps_method));
1116 					status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1117 					goto fail;
1118 				}
1119 				break;
1120 			}
1121 #ifdef CONFIG_WPS_NFC
1122 			if (p2p->authorized_oob_dev_pw_id &&
1123 			    msg.dev_password_id ==
1124 			    p2p->authorized_oob_dev_pw_id) {
1125 				p2p_dbg(p2p, "Using static handover with our device password from NFC Tag");
1126 				dev->wps_method = WPS_NFC;
1127 				dev->oob_pw_id = p2p->authorized_oob_dev_pw_id;
1128 				break;
1129 			}
1130 #endif /* CONFIG_WPS_NFC */
1131 			p2p_dbg(p2p, "Unsupported Device Password ID %d",
1132 				msg.dev_password_id);
1133 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1134 			goto fail;
1135 		}
1136 
1137 		if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1138 			goto fail;
1139 
1140 		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1141 		dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1142 						     msg.operating_channel[4]);
1143 		p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
1144 			dev->oper_freq);
1145 
1146 		/*
1147 		 * Use the driver preferred frequency list extension if
1148 		 * supported.
1149 		 */
1150 		p2p_check_pref_chan(p2p, go, dev, &msg);
1151 
1152 #ifdef CONFIG_OPEN_HARMONY_P2P_DFH_CONNECT
1153 	if (go) {
1154 		pvt_p2p_adjust_channel(p2p, dev);
1155 	}
1156 #endif
1157 
1158 		if (msg.config_timeout) {
1159 			dev->go_timeout = msg.config_timeout[0];
1160 			dev->client_timeout = msg.config_timeout[1];
1161 		}
1162 
1163 		p2p_dbg(p2p, "GO Negotiation with " MACSTR_SEC, MAC2STR_SEC(sa));
1164 		if (p2p->state != P2P_IDLE)
1165 			p2p_stop_find_for_freq(p2p, rx_freq);
1166 		p2p_set_state(p2p, P2P_GO_NEG);
1167 		p2p_clear_timeout(p2p);
1168 		dev->dialog_token = msg.dialog_token;
1169 		os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1170 		p2p->go_neg_peer = dev;
1171 		eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
1172 		status = P2P_SC_SUCCESS;
1173 	}
1174 
1175 fail:
1176 	if (dev)
1177 		dev->status = status;
1178 	resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
1179 				     !tie_breaker);
1180 	p2p_parse_free(&msg);
1181 	if (resp == NULL)
1182 		return;
1183 	p2p_warning(p2p, "Sending GO Negotiation Response");
1184 
1185 #ifdef CONFIG_MIRACAST_SOURCE_OPT
1186 	p2p_dbg(p2p, "Use cfg channel for sending action frame Tx");
1187 	freq = p2p_channel_to_freq(p2p->cfg->reg_class,
1188 					p2p->cfg->channel);
1189 #else
1190 	if (rx_freq > 0)
1191 		freq = rx_freq;
1192 	else
1193 		freq = p2p_channel_to_freq(p2p->cfg->reg_class,
1194 					   p2p->cfg->channel);
1195 #endif
1196 	if (freq < 0) {
1197 		p2p_dbg(p2p, "Unknown regulatory class/channel");
1198 		wpabuf_free(resp);
1199 		return;
1200 	}
1201 	if (status == P2P_SC_SUCCESS) {
1202 		p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
1203 		dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
1204 		if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
1205 			/*
1206 			 * Peer has smaller address, so the GO Negotiation
1207 			 * Response from us is expected to complete
1208 			 * negotiation. Ignore a GO Negotiation Response from
1209 			 * the peer if it happens to be received after this
1210 			 * point due to a race condition in GO Negotiation
1211 			 * Request transmission and processing.
1212 			 */
1213 			dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1214 		}
1215 	} else
1216 		p2p->pending_action_state =
1217 			P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
1218 	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
1219 			    p2p->cfg->dev_addr,
1220 			    wpabuf_head(resp), wpabuf_len(resp), 100) < 0) {
1221 		p2p_dbg(p2p, "Failed to send Action frame");
1222 	}
1223 
1224 	wpabuf_free(resp);
1225 }
1226 
1227 
p2p_build_go_neg_conf(struct p2p_data * p2p,struct p2p_device * peer,u8 dialog_token,u8 status,const u8 * resp_chan,int go)1228 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
1229 					     struct p2p_device *peer,
1230 					     u8 dialog_token, u8 status,
1231 					     const u8 *resp_chan, int go)
1232 {
1233 	struct wpabuf *buf;
1234 	u8 *len;
1235 	struct p2p_channels res;
1236 	u8 group_capab;
1237 	size_t extra = 0;
1238 
1239 	p2p_dbg(p2p, "Building GO Negotiation Confirm");
1240 
1241 #ifdef CONFIG_WIFI_DISPLAY
1242 	if (p2p->wfd_ie_go_neg)
1243 		extra = wpabuf_len(p2p->wfd_ie_go_neg);
1244 #endif /* CONFIG_WIFI_DISPLAY */
1245 
1246 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
1247 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
1248 
1249 	buf = wpabuf_alloc(1000 + extra);
1250 	if (buf == NULL)
1251 		return NULL;
1252 
1253 	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
1254 
1255 	len = p2p_buf_add_ie_hdr(buf);
1256 	p2p_buf_add_status(buf, status);
1257 	group_capab = 0;
1258 	if (peer->go_state == LOCAL_GO) {
1259 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1260 			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1261 			if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1262 				group_capab |=
1263 					P2P_GROUP_CAPAB_PERSISTENT_RECONN;
1264 		}
1265 		if (p2p->cross_connect)
1266 			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
1267 		if (p2p->cfg->p2p_intra_bss)
1268 			group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
1269 	}
1270 	p2p_buf_add_capability(buf, p2p->dev_capab &
1271 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
1272 			       group_capab);
1273 	if (go || resp_chan == NULL)
1274 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
1275 					      p2p->op_reg_class,
1276 					      p2p->op_channel);
1277 	else
1278 		p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
1279 					      resp_chan[3], resp_chan[4]);
1280 	p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
1281 	p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
1282 	if (go) {
1283 		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
1284 				     p2p->ssid_len);
1285 	}
1286 	p2p_buf_update_ie_hdr(buf, len);
1287 
1288 #ifdef CONFIG_WIFI_DISPLAY
1289 	if (p2p->wfd_ie_go_neg)
1290 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
1291 #endif /* CONFIG_WIFI_DISPLAY */
1292 
1293 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
1294 		wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
1295 
1296 	return buf;
1297 }
1298 
1299 
p2p_process_go_neg_resp(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len,int rx_freq)1300 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
1301 			     const u8 *data, size_t len, int rx_freq)
1302 {
1303 	struct p2p_device *dev;
1304 	int go = -1;
1305 	struct p2p_message msg;
1306 	u8 status = P2P_SC_SUCCESS;
1307 	int freq;
1308 
1309 	p2p_dbg(p2p, "Received GO Negotiation Response from " MACSTR_SEC
1310 		" (freq=%d)", MAC2STR_SEC(sa), rx_freq);
1311 	dev = p2p_get_device(p2p, sa);
1312 	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1313 	    dev != p2p->go_neg_peer) {
1314 		p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR_SEC,
1315 			MAC2STR_SEC(sa));
1316 		return;
1317 	}
1318 
1319 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
1320 	if (dev->flags & P2P_DEV_USER_REJECTED) {
1321 		p2p_dbg(p2p, "user has rejected, do not go on go neg");
1322 		p2p_go_neg_failed(p2p, -1);
1323 		return;
1324 	}
1325 #endif
1326 
1327 	if (p2p_parse(data, len, &msg))
1328 		return;
1329 
1330 	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
1331 		p2p_dbg(p2p, "Was not expecting GO Negotiation Response - ignore");
1332 		p2p_parse_free(&msg);
1333 		return;
1334 	}
1335 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1336 
1337 	if (msg.dialog_token != dev->dialog_token) {
1338 		p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
1339 			msg.dialog_token, dev->dialog_token);
1340 		p2p_parse_free(&msg);
1341 		return;
1342 	}
1343 
1344 	if (!msg.status) {
1345 		p2p_dbg(p2p, "No Status attribute received");
1346 		status = P2P_SC_FAIL_INVALID_PARAMS;
1347 		goto fail;
1348 	}
1349 	if (*msg.status) {
1350 		p2p_warning(p2p, "GO Negotiation rejected: status %d", *msg.status);
1351 		dev->go_neg_req_sent = 0;
1352 		if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1353 			p2p_dbg(p2p, "Wait for the peer to become ready for GO Negotiation");
1354 			dev->flags |= P2P_DEV_NOT_YET_READY;
1355 			eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p,
1356 					     NULL);
1357 			eloop_register_timeout(120, 0, p2p_go_neg_wait_timeout,
1358 					       p2p, NULL);
1359 			if (p2p->state == P2P_CONNECT_LISTEN)
1360 				p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
1361 			else
1362 				p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
1363 			p2p_set_timeout(p2p, 0, 0);
1364 		} else {
1365 			p2p_dbg(p2p, "Stop GO Negotiation attempt");
1366 			p2p_go_neg_failed(p2p, *msg.status);
1367 		}
1368 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1369 		p2p_parse_free(&msg);
1370 		return;
1371 	}
1372 
1373 	if (!msg.capability) {
1374 		p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Response");
1375 #ifdef CONFIG_P2P_STRICT
1376 		status = P2P_SC_FAIL_INVALID_PARAMS;
1377 		goto fail;
1378 #endif /* CONFIG_P2P_STRICT */
1379 	}
1380 
1381 	if (!msg.p2p_device_info) {
1382 		p2p_dbg(p2p, "Mandatory P2P Device Info attribute missing from GO Negotiation Response");
1383 #ifdef CONFIG_P2P_STRICT
1384 		status = P2P_SC_FAIL_INVALID_PARAMS;
1385 		goto fail;
1386 #endif /* CONFIG_P2P_STRICT */
1387 	}
1388 
1389 	if (!msg.intended_addr) {
1390 		p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
1391 		status = P2P_SC_FAIL_INVALID_PARAMS;
1392 		goto fail;
1393 	}
1394 
1395 	if (!msg.go_intent) {
1396 		p2p_dbg(p2p, "No GO Intent attribute received");
1397 		status = P2P_SC_FAIL_INVALID_PARAMS;
1398 		goto fail;
1399 	}
1400 	if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
1401 		p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
1402 			*msg.go_intent >> 1);
1403 		status = P2P_SC_FAIL_INVALID_PARAMS;
1404 		goto fail;
1405 	}
1406 
1407 	go = p2p_go_det(p2p->go_intent, *msg.go_intent);
1408 	if (go < 0) {
1409 		p2p_dbg(p2p, "Incompatible GO Intent");
1410 		status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
1411 		goto fail;
1412 	}
1413 
1414 	if (!go && msg.group_id) {
1415 		/* Store SSID for Provisioning step */
1416 		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1417 		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1418 	} else if (!go) {
1419 		p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Response");
1420 		p2p->ssid_len = 0;
1421 		status = P2P_SC_FAIL_INVALID_PARAMS;
1422 		goto fail;
1423 	}
1424 
1425 	if (!msg.config_timeout) {
1426 		p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Response");
1427 #ifdef CONFIG_P2P_STRICT
1428 		status = P2P_SC_FAIL_INVALID_PARAMS;
1429 		goto fail;
1430 #endif /* CONFIG_P2P_STRICT */
1431 	} else {
1432 		dev->go_timeout = msg.config_timeout[0];
1433 		dev->client_timeout = msg.config_timeout[1];
1434 	}
1435 
1436 	if (msg.wfd_subelems) {
1437 		wpabuf_free(dev->info.wfd_subelems);
1438 		dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1439 	}
1440 
1441 	if (!msg.operating_channel && !go) {
1442 		/*
1443 		 * Note: P2P Client may omit Operating Channel attribute to
1444 		 * indicate it does not have a preference.
1445 		 */
1446 		p2p_dbg(p2p, "No Operating Channel attribute received");
1447 		status = P2P_SC_FAIL_INVALID_PARAMS;
1448 		goto fail;
1449 	}
1450 	if (!msg.channel_list) {
1451 		p2p_dbg(p2p, "No Channel List attribute received");
1452 		status = P2P_SC_FAIL_INVALID_PARAMS;
1453 		goto fail;
1454 	}
1455 
1456 	if (p2p_peer_channels(p2p, dev, msg.channel_list,
1457 			      msg.channel_list_len) < 0) {
1458 		p2p_dbg(p2p, "No common channels found");
1459 		status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
1460 		goto fail;
1461 	}
1462 
1463 	if (msg.operating_channel) {
1464 		dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1465 						     msg.operating_channel[4]);
1466 		p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
1467 			dev->oper_freq);
1468 	} else
1469 		dev->oper_freq = 0;
1470 
1471 	switch (msg.dev_password_id) {
1472 	case DEV_PW_REGISTRAR_SPECIFIED:
1473 		p2p_dbg(p2p, "PIN from peer Display");
1474 		if (dev->wps_method != WPS_PIN_KEYPAD) {
1475 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1476 				p2p_wps_method_str(dev->wps_method));
1477 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1478 			goto fail;
1479 		}
1480 		break;
1481 	case DEV_PW_USER_SPECIFIED:
1482 		p2p_dbg(p2p, "Peer entered PIN on Keypad");
1483 		if (dev->wps_method != WPS_PIN_DISPLAY) {
1484 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1485 				p2p_wps_method_str(dev->wps_method));
1486 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1487 			goto fail;
1488 		}
1489 		break;
1490 	case DEV_PW_PUSHBUTTON:
1491 		p2p_dbg(p2p, "Peer using pushbutton");
1492 		if (dev->wps_method != WPS_PBC) {
1493 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1494 				p2p_wps_method_str(dev->wps_method));
1495 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1496 			goto fail;
1497 		}
1498 		break;
1499 	case DEV_PW_P2PS_DEFAULT:
1500 		p2p_dbg(p2p, "P2P: Peer using P2PS default pin");
1501 		if (dev->wps_method != WPS_P2PS) {
1502 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1503 				p2p_wps_method_str(dev->wps_method));
1504 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1505 			goto fail;
1506 		}
1507 		break;
1508 	default:
1509 		if (msg.dev_password_id &&
1510 		    msg.dev_password_id == dev->oob_pw_id) {
1511 			p2p_dbg(p2p, "Peer using NFC");
1512 			if (dev->wps_method != WPS_NFC) {
1513 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1514 					p2p_wps_method_str(dev->wps_method));
1515 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1516 				goto fail;
1517 			}
1518 			break;
1519 		}
1520 		p2p_dbg(p2p, "Unsupported Device Password ID %d",
1521 			msg.dev_password_id);
1522 		status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1523 		goto fail;
1524 	}
1525 
1526 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
1527 	u8 operating_channel = ((msg.operating_channel == NULL) ? 0 : msg.operating_channel[HM_OPERATING_CHANNEL_POS]);
1528 	hm_p2p_update_peer_info(dev->info.p2p_device_addr,
1529 		HM_GO_NEG_RESP, operating_channel, dev);
1530 #endif
1531 
1532 	if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1533 		goto fail;
1534 
1535 	/*
1536 	 * Use the driver preferred frequency list extension if local device is
1537 	 * GO.
1538 	 */
1539 	if (go)
1540 		p2p_check_pref_chan(p2p, go, dev, &msg);
1541 
1542 #ifdef CONFIG_OPEN_HARMONY_P2P_DFH_CONNECT
1543 	if (go) {
1544 		pvt_p2p_adjust_channel(p2p, dev);
1545 	}
1546 #endif
1547 
1548 	p2p_set_state(p2p, P2P_GO_NEG);
1549 	p2p_clear_timeout(p2p);
1550 
1551 	p2p_dbg(p2p, "GO Negotiation with " MACSTR_SEC, MAC2STR_SEC(sa));
1552 	os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1553 
1554 fail:
1555 	/* Store GO Negotiation Confirmation to allow retransmission */
1556 	wpabuf_free(dev->go_neg_conf);
1557 	dev->go_neg_conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token,
1558 						 status, msg.operating_channel,
1559 						 go);
1560 	p2p_parse_free(&msg);
1561 	if (dev->go_neg_conf == NULL)
1562 		return;
1563 	p2p_dbg(p2p, "Sending GO Negotiation Confirm");
1564 	if (status == P2P_SC_SUCCESS) {
1565 		p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1566 		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1567 	} else
1568 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1569 	if (rx_freq > 0)
1570 		freq = rx_freq;
1571 	else
1572 		freq = dev->listen_freq;
1573 
1574 	dev->go_neg_conf_freq = freq;
1575 	dev->go_neg_conf_sent = 0;
1576 
1577 	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1578 			    wpabuf_head(dev->go_neg_conf),
1579 			    wpabuf_len(dev->go_neg_conf), 50) < 0) {
1580 		p2p_dbg(p2p, "Failed to send Action frame");
1581 		p2p_go_neg_failed(p2p, -1);
1582 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1583 	} else
1584 		dev->go_neg_conf_sent++;
1585 	if (status != P2P_SC_SUCCESS) {
1586 		p2p_dbg(p2p, "GO Negotiation failed");
1587 		p2p_go_neg_failed(p2p, status);
1588 	}
1589 }
1590 
1591 
p2p_process_go_neg_conf(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len)1592 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1593 			     const u8 *data, size_t len)
1594 {
1595 	struct p2p_device *dev;
1596 	struct p2p_message msg;
1597 
1598 	p2p_warning(p2p, "Received GO Negotiation Confirm from " MACSTR_SEC,
1599 		MAC2STR_SEC(sa));
1600 	dev = p2p_get_device(p2p, sa);
1601 	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1602 	    dev != p2p->go_neg_peer) {
1603 		p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR_SEC,
1604 			MAC2STR_SEC(sa));
1605 		return;
1606 	}
1607 
1608 	if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1609 		p2p_dbg(p2p, "Stopped waiting for TX status on GO Negotiation Response since we already received Confirmation");
1610 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1611 	}
1612 
1613 	if (p2p_parse(data, len, &msg))
1614 		return;
1615 
1616 	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1617 		p2p_dbg(p2p, "Was not expecting GO Negotiation Confirm - ignore");
1618 		p2p_parse_free(&msg);
1619 		return;
1620 	}
1621 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1622 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1623 
1624 	if (msg.dialog_token != dev->dialog_token) {
1625 		p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
1626 			msg.dialog_token, dev->dialog_token);
1627 		p2p_parse_free(&msg);
1628 		return;
1629 	}
1630 
1631 	if (!msg.status) {
1632 		p2p_dbg(p2p, "No Status attribute received");
1633 		p2p_parse_free(&msg);
1634 		return;
1635 	}
1636 	if (*msg.status) {
1637 		p2p_warning(p2p, "GO Negotiation rejected: status %d", *msg.status);
1638 		p2p_go_neg_failed(p2p, *msg.status);
1639 		p2p_parse_free(&msg);
1640 		return;
1641 	}
1642 
1643 	if (dev->go_state == REMOTE_GO && msg.group_id) {
1644 		/* Store SSID for Provisioning step */
1645 		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1646 		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1647 	} else if (dev->go_state == REMOTE_GO) {
1648 		p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Confirmation");
1649 		p2p->ssid_len = 0;
1650 		p2p_go_neg_failed(p2p, P2P_SC_FAIL_INVALID_PARAMS);
1651 		p2p_parse_free(&msg);
1652 		return;
1653 	}
1654 
1655 	if (!msg.operating_channel) {
1656 		p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1657 #ifdef CONFIG_P2P_STRICT
1658 		p2p_parse_free(&msg);
1659 		return;
1660 #endif /* CONFIG_P2P_STRICT */
1661 	} else if (dev->go_state == REMOTE_GO) {
1662 		int oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1663 						    msg.operating_channel[4]);
1664 		if (oper_freq != dev->oper_freq) {
1665 			p2p_dbg(p2p, "Updated peer (GO) operating channel preference from %d MHz to %d MHz",
1666 				dev->oper_freq, oper_freq);
1667 			dev->oper_freq = oper_freq;
1668 		}
1669 	}
1670 
1671 	if (!msg.channel_list) {
1672 		p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1673 #ifdef CONFIG_P2P_STRICT
1674 		p2p_parse_free(&msg);
1675 		return;
1676 #endif /* CONFIG_P2P_STRICT */
1677 	}
1678 
1679 	p2p_parse_free(&msg);
1680 
1681 	if (dev->go_state == UNKNOWN_GO) {
1682 		/*
1683 		 * This should not happen since GO negotiation has already
1684 		 * been completed.
1685 		 */
1686 		p2p_dbg(p2p, "Unexpected GO Neg state - do not know which end becomes GO");
1687 		return;
1688 	}
1689 
1690 	/*
1691 	 * The peer could have missed our ctrl::ack frame for GO Negotiation
1692 	 * Confirm and continue retransmitting the frame. To reduce the
1693 	 * likelihood of the peer not getting successful TX status for the
1694 	 * GO Negotiation Confirm frame, wait a short time here before starting
1695 	 * the group so that we will remain on the current channel to
1696 	 * acknowledge any possible retransmission from the peer.
1697 	 */
1698 	p2p_dbg(p2p, "20 ms wait on current channel before starting group");
1699 	os_sleep(0, 20000);
1700 
1701 	p2p_go_complete(p2p, dev);
1702 }
1703