xref: /freebsd/contrib/wpa/src/p2p/p2p_invitation.c (revision 40f65a4df509c6b29b9e72407c7afefe28838844)
1 /*
2  * Wi-Fi Direct - P2P Invitation procedure
3  * Copyright (c) 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 "p2p_i.h"
14 #include "p2p.h"
15 
16 
17 static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
18 						struct p2p_device *peer,
19 						const u8 *go_dev_addr)
20 {
21 	struct wpabuf *buf;
22 	u8 *len;
23 	const u8 *dev_addr;
24 	size_t extra = 0;
25 
26 #ifdef CONFIG_WIFI_DISPLAY
27 	struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
28 	if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
29 		size_t i;
30 		for (i = 0; i < p2p->num_groups; i++) {
31 			struct p2p_group *g = p2p->groups[i];
32 			struct wpabuf *ie;
33 			if (os_memcmp(p2p_group_get_interface_addr(g),
34 				      p2p->inv_bssid, ETH_ALEN) != 0)
35 				continue;
36 			ie = p2p_group_get_wfd_ie(g);
37 			if (ie) {
38 				wfd_ie = ie;
39 				break;
40 			}
41 		}
42 	}
43 	if (wfd_ie)
44 		extra = wpabuf_len(wfd_ie);
45 #endif /* CONFIG_WIFI_DISPLAY */
46 
47 	buf = wpabuf_alloc(1000 + extra);
48 	if (buf == NULL)
49 		return NULL;
50 
51 	peer->dialog_token++;
52 	if (peer->dialog_token == 0)
53 		peer->dialog_token = 1;
54 	p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
55 				      peer->dialog_token);
56 
57 	len = p2p_buf_add_ie_hdr(buf);
58 	if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
59 		p2p_buf_add_config_timeout(buf, 0, 0);
60 	else
61 		p2p_buf_add_config_timeout(buf, p2p->go_timeout,
62 					   p2p->client_timeout);
63 	p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
64 				     P2P_INVITATION_FLAGS_TYPE : 0);
65 	p2p_buf_add_operating_channel(buf, p2p->cfg->country,
66 				      p2p->op_reg_class, p2p->op_channel);
67 	if (p2p->inv_bssid_set)
68 		p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
69 	p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
70 	if (go_dev_addr)
71 		dev_addr = go_dev_addr;
72 	else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
73 		dev_addr = peer->info.p2p_device_addr;
74 	else
75 		dev_addr = p2p->cfg->dev_addr;
76 	p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
77 	p2p_buf_add_device_info(buf, p2p, peer);
78 	p2p_buf_update_ie_hdr(buf, len);
79 
80 #ifdef CONFIG_WIFI_DISPLAY
81 	if (wfd_ie)
82 		wpabuf_put_buf(buf, wfd_ie);
83 #endif /* CONFIG_WIFI_DISPLAY */
84 
85 	return buf;
86 }
87 
88 
89 static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
90 						 struct p2p_device *peer,
91 						 u8 dialog_token, u8 status,
92 						 const u8 *group_bssid,
93 						 u8 reg_class, u8 channel,
94 						 struct p2p_channels *channels)
95 {
96 	struct wpabuf *buf;
97 	u8 *len;
98 	size_t extra = 0;
99 
100 #ifdef CONFIG_WIFI_DISPLAY
101 	struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
102 	if (wfd_ie && group_bssid) {
103 		size_t i;
104 		for (i = 0; i < p2p->num_groups; i++) {
105 			struct p2p_group *g = p2p->groups[i];
106 			struct wpabuf *ie;
107 			if (os_memcmp(p2p_group_get_interface_addr(g),
108 				      group_bssid, ETH_ALEN) != 0)
109 				continue;
110 			ie = p2p_group_get_wfd_ie(g);
111 			if (ie) {
112 				wfd_ie = ie;
113 				break;
114 			}
115 		}
116 	}
117 	if (wfd_ie)
118 		extra = wpabuf_len(wfd_ie);
119 #endif /* CONFIG_WIFI_DISPLAY */
120 
121 	buf = wpabuf_alloc(1000 + extra);
122 	if (buf == NULL)
123 		return NULL;
124 
125 	p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
126 				      dialog_token);
127 
128 	len = p2p_buf_add_ie_hdr(buf);
129 	p2p_buf_add_status(buf, status);
130 	p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
131 	if (reg_class && channel)
132 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
133 					      reg_class, channel);
134 	if (group_bssid)
135 		p2p_buf_add_group_bssid(buf, group_bssid);
136 	if (channels)
137 		p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
138 	p2p_buf_update_ie_hdr(buf, len);
139 
140 #ifdef CONFIG_WIFI_DISPLAY
141 	if (wfd_ie)
142 		wpabuf_put_buf(buf, wfd_ie);
143 #endif /* CONFIG_WIFI_DISPLAY */
144 
145 	return buf;
146 }
147 
148 
149 void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
150 				const u8 *data, size_t len, int rx_freq)
151 {
152 	struct p2p_device *dev;
153 	struct p2p_message msg;
154 	struct wpabuf *resp = NULL;
155 	u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
156 	int freq;
157 	int go = 0;
158 	u8 group_bssid[ETH_ALEN], *bssid;
159 	int op_freq = 0;
160 	u8 reg_class = 0, channel = 0;
161 	struct p2p_channels intersection, *channels = NULL;
162 	int persistent;
163 
164 	os_memset(group_bssid, 0, sizeof(group_bssid));
165 
166 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
167 		"P2P: Received Invitation Request from " MACSTR " (freq=%d)",
168 		MAC2STR(sa), rx_freq);
169 
170 	if (p2p_parse(data, len, &msg))
171 		return;
172 
173 	dev = p2p_get_device(p2p, sa);
174 	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
175 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
176 			"P2P: Invitation Request from unknown peer "
177 			MACSTR, MAC2STR(sa));
178 
179 		if (p2p_add_device(p2p, sa, rx_freq, 0, 0, data + 1, len - 1,
180 				   0)) {
181 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
182 				"P2P: Invitation Request add device failed "
183 				MACSTR, MAC2STR(sa));
184 			status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
185 			goto fail;
186 		}
187 
188 		dev = p2p_get_device(p2p, sa);
189 		if (dev == NULL) {
190 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
191 				"P2P: Reject Invitation Request from unknown "
192 				"peer " MACSTR, MAC2STR(sa));
193 			status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
194 			goto fail;
195 		}
196 	}
197 
198 	if (!msg.group_id || !msg.channel_list) {
199 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
200 			"P2P: Mandatory attribute missing in Invitation "
201 			"Request from " MACSTR, MAC2STR(sa));
202 		status = P2P_SC_FAIL_INVALID_PARAMS;
203 		goto fail;
204 	}
205 
206 	if (msg.invitation_flags)
207 		persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
208 	else {
209 		/* Invitation Flags is a mandatory attribute starting from P2P
210 		 * spec 1.06. As a backwards compatibility mechanism, assume
211 		 * the request was for a persistent group if the attribute is
212 		 * missing.
213 		 */
214 		wpa_printf(MSG_DEBUG, "P2P: Mandatory Invitation Flags "
215 			   "attribute missing from Invitation Request");
216 		persistent = 1;
217 	}
218 
219 	if (p2p_peer_channels_check(p2p, &p2p->cfg->channels, dev,
220 				    msg.channel_list, msg.channel_list_len) <
221 	    0) {
222 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
223 			"P2P: No common channels found");
224 		status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
225 		goto fail;
226 	}
227 
228 	if (p2p->cfg->invitation_process) {
229 		status = p2p->cfg->invitation_process(
230 			p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
231 			msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
232 			&go, group_bssid, &op_freq, persistent);
233 	}
234 
235 	if (op_freq) {
236 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Invitation "
237 			"processing forced frequency %d MHz", op_freq);
238 		if (p2p_freq_to_channel(p2p->cfg->country, op_freq,
239 					&reg_class, &channel) < 0) {
240 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
241 				"P2P: Unknown forced freq %d MHz from "
242 				"invitation_process()", op_freq);
243 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
244 			goto fail;
245 		}
246 
247 		p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
248 				       &intersection);
249 		if (!p2p_channels_includes(&intersection, reg_class, channel))
250 		{
251 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
252 				"P2P: forced freq %d MHz not in the supported "
253 				"channels interaction", op_freq);
254 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
255 			goto fail;
256 		}
257 
258 		if (status == P2P_SC_SUCCESS)
259 			channels = &intersection;
260 	} else {
261 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
262 			"P2P: No forced channel from invitation processing - "
263 			"figure out best one to use");
264 
265 		p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
266 				       &intersection);
267 		/* Default to own configuration as a starting point */
268 		p2p->op_reg_class = p2p->cfg->op_reg_class;
269 		p2p->op_channel = p2p->cfg->op_channel;
270 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Own default "
271 			"op_class %d channel %d",
272 			p2p->op_reg_class, p2p->op_channel);
273 
274 		/* Use peer preference if specified and compatible */
275 		if (msg.operating_channel) {
276 			int req_freq;
277 			req_freq = p2p_channel_to_freq(
278 				(const char *) msg.operating_channel,
279 				msg.operating_channel[3],
280 				msg.operating_channel[4]);
281 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer "
282 				"operating channel preference: %d MHz",
283 				req_freq);
284 			if (req_freq > 0 &&
285 			    p2p_channels_includes(&intersection,
286 						  msg.operating_channel[3],
287 						  msg.operating_channel[4])) {
288 				p2p->op_reg_class = msg.operating_channel[3];
289 				p2p->op_channel = msg.operating_channel[4];
290 				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
291 					"P2P: Use peer preference op_class %d "
292 					"channel %d",
293 					p2p->op_reg_class, p2p->op_channel);
294 			} else {
295 				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
296 					"P2P: Cannot use peer channel "
297 					"preference");
298 			}
299 		}
300 
301 		if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
302 					   p2p->op_channel)) {
303 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
304 				"P2P: Initially selected channel (op_class %d "
305 				"channel %d) not in channel intersection - try "
306 				"to reselect",
307 				p2p->op_reg_class, p2p->op_channel);
308 			p2p_reselect_channel(p2p, &intersection);
309 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
310 				"P2P: Re-selection result: op_class %d "
311 				"channel %d",
312 				p2p->op_reg_class, p2p->op_channel);
313 			if (!p2p_channels_includes(&intersection,
314 						   p2p->op_reg_class,
315 						   p2p->op_channel)) {
316 				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
317 					"P2P: Peer does not support selected "
318 					"operating channel (reg_class=%u "
319 					"channel=%u)",
320 					p2p->op_reg_class, p2p->op_channel);
321 				status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
322 				goto fail;
323 			}
324 		}
325 
326 		op_freq = p2p_channel_to_freq(p2p->cfg->country,
327 					      p2p->op_reg_class,
328 					      p2p->op_channel);
329 		if (op_freq < 0) {
330 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
331 				"P2P: Unknown operational channel "
332 				"(country=%c%c reg_class=%u channel=%u)",
333 				p2p->cfg->country[0], p2p->cfg->country[1],
334 				p2p->op_reg_class, p2p->op_channel);
335 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
336 			goto fail;
337 		}
338 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
339 			"channel - %d MHz", op_freq);
340 
341 		if (status == P2P_SC_SUCCESS) {
342 			reg_class = p2p->op_reg_class;
343 			channel = p2p->op_channel;
344 			channels = &intersection;
345 		}
346 	}
347 
348 fail:
349 	if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
350 		bssid = group_bssid;
351 	else
352 		bssid = NULL;
353 	resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
354 					 bssid, reg_class, channel, channels);
355 
356 	if (resp == NULL)
357 		goto out;
358 
359 	if (rx_freq > 0)
360 		freq = rx_freq;
361 	else
362 		freq = p2p_channel_to_freq(p2p->cfg->country,
363 					   p2p->cfg->reg_class,
364 					   p2p->cfg->channel);
365 	if (freq < 0) {
366 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
367 			"P2P: Unknown regulatory class/channel");
368 		goto out;
369 	}
370 
371 	/*
372 	 * Store copy of invitation data to be used when processing TX status
373 	 * callback for the Acton frame.
374 	 */
375 	os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
376 	if (msg.group_bssid) {
377 		os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
378 		p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
379 	} else
380 		p2p->inv_group_bssid_ptr = NULL;
381 	if (msg.group_id_len - ETH_ALEN <= 32) {
382 		os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
383 			  msg.group_id_len - ETH_ALEN);
384 		p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
385 	}
386 	os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
387 	p2p->inv_status = status;
388 	p2p->inv_op_freq = op_freq;
389 
390 	p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
391 	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
392 			    p2p->cfg->dev_addr,
393 			    wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
394 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
395 			"P2P: Failed to send Action frame");
396 	}
397 
398 out:
399 	wpabuf_free(resp);
400 	p2p_parse_free(&msg);
401 }
402 
403 
404 void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
405 				 const u8 *data, size_t len)
406 {
407 	struct p2p_device *dev;
408 	struct p2p_message msg;
409 
410 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
411 		"P2P: Received Invitation Response from " MACSTR,
412 		MAC2STR(sa));
413 
414 	dev = p2p_get_device(p2p, sa);
415 	if (dev == NULL) {
416 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
417 			"P2P: Ignore Invitation Response from unknown peer "
418 			MACSTR, MAC2STR(sa));
419 		return;
420 	}
421 
422 	if (dev != p2p->invite_peer) {
423 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
424 			"P2P: Ignore unexpected Invitation Response from peer "
425 			MACSTR, MAC2STR(sa));
426 		return;
427 	}
428 
429 	if (p2p_parse(data, len, &msg))
430 		return;
431 
432 	if (!msg.status) {
433 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
434 			"P2P: Mandatory Status attribute missing in "
435 			"Invitation Response from " MACSTR, MAC2STR(sa));
436 		p2p_parse_free(&msg);
437 		return;
438 	}
439 
440 	if (p2p->cfg->invitation_result)
441 		p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
442 					    msg.group_bssid);
443 
444 	p2p_parse_free(&msg);
445 
446 	p2p_clear_timeout(p2p);
447 	p2p_set_state(p2p, P2P_IDLE);
448 	p2p->invite_peer = NULL;
449 }
450 
451 
452 int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
453 		    const u8 *go_dev_addr)
454 {
455 	struct wpabuf *req;
456 	int freq;
457 
458 	freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
459 	if (freq <= 0) {
460 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
461 			"P2P: No Listen/Operating frequency known for the "
462 			"peer " MACSTR " to send Invitation Request",
463 			MAC2STR(dev->info.p2p_device_addr));
464 		return -1;
465 	}
466 
467 	req = p2p_build_invitation_req(p2p, dev, go_dev_addr);
468 	if (req == NULL)
469 		return -1;
470 	if (p2p->state != P2P_IDLE)
471 		p2p_stop_listen_for_freq(p2p, freq);
472 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
473 		"P2P: Sending Invitation Request");
474 	p2p_set_state(p2p, P2P_INVITE);
475 	p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
476 	p2p->invite_peer = dev;
477 	dev->invitation_reqs++;
478 	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
479 			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
480 			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
481 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
482 			"P2P: Failed to send Action frame");
483 		/* Use P2P find to recover and retry */
484 		p2p_set_timeout(p2p, 0, 0);
485 	}
486 
487 	wpabuf_free(req);
488 
489 	return 0;
490 }
491 
492 
493 void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
494 {
495 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
496 		"P2P: Invitation Request TX callback: success=%d", success);
497 
498 	if (p2p->invite_peer == NULL) {
499 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
500 			"P2P: No pending Invite");
501 		return;
502 	}
503 
504 	/*
505 	 * Use P2P find, if needed, to find the other device from its listen
506 	 * channel.
507 	 */
508 	p2p_set_state(p2p, P2P_INVITE);
509 	p2p_set_timeout(p2p, 0, 100000);
510 }
511 
512 
513 void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
514 {
515 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
516 		"P2P: Invitation Response TX callback: success=%d", success);
517 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
518 
519 	if (success && p2p->cfg->invitation_received) {
520 		p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
521 					      p2p->inv_sa,
522 					      p2p->inv_group_bssid_ptr,
523 					      p2p->inv_ssid, p2p->inv_ssid_len,
524 					      p2p->inv_go_dev_addr,
525 					      p2p->inv_status,
526 					      p2p->inv_op_freq);
527 	}
528 }
529 
530 
531 int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
532 	       const u8 *bssid, const u8 *ssid, size_t ssid_len,
533 	       unsigned int force_freq, const u8 *go_dev_addr,
534 	       int persistent_group)
535 {
536 	struct p2p_device *dev;
537 
538 	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
539 		"P2P: Request to invite peer " MACSTR " role=%d persistent=%d "
540 		"force_freq=%u",
541 		MAC2STR(peer), role, persistent_group, force_freq);
542 	if (bssid)
543 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
544 			"P2P: Invitation for BSSID " MACSTR, MAC2STR(bssid));
545 	if (go_dev_addr) {
546 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
547 			"P2P: Invitation for GO Device Address " MACSTR,
548 			MAC2STR(go_dev_addr));
549 		os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
550 		p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
551 	} else
552 		p2p->invite_go_dev_addr = NULL;
553 	wpa_hexdump_ascii(MSG_DEBUG, "P2P: Invitation for SSID",
554 			  ssid, ssid_len);
555 
556 	dev = p2p_get_device(p2p, peer);
557 	if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0)) {
558 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
559 			"P2P: Cannot invite unknown P2P Device " MACSTR,
560 			MAC2STR(peer));
561 		return -1;
562 	}
563 
564 	if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
565 		if (!(dev->info.dev_capab &
566 		      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
567 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
568 				"P2P: Cannot invite a P2P Device " MACSTR
569 				" that is in a group and is not discoverable",
570 				MAC2STR(peer));
571 		}
572 		/* TODO: use device discoverability request through GO */
573 	}
574 
575 	dev->invitation_reqs = 0;
576 
577 	if (force_freq) {
578 		if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
579 					&p2p->op_reg_class, &p2p->op_channel) <
580 		    0) {
581 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
582 				"P2P: Unsupported frequency %u MHz",
583 				force_freq);
584 			return -1;
585 		}
586 		p2p->channels.reg_classes = 1;
587 		p2p->channels.reg_class[0].channels = 1;
588 		p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
589 		p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
590 	} else {
591 		p2p->op_reg_class = p2p->cfg->op_reg_class;
592 		p2p->op_channel = p2p->cfg->op_channel;
593 		os_memcpy(&p2p->channels, &p2p->cfg->channels,
594 			  sizeof(struct p2p_channels));
595 	}
596 
597 	if (p2p->state != P2P_IDLE)
598 		p2p_stop_find(p2p);
599 
600 	p2p->inv_role = role;
601 	p2p->inv_bssid_set = bssid != NULL;
602 	if (bssid)
603 		os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
604 	os_memcpy(p2p->inv_ssid, ssid, ssid_len);
605 	p2p->inv_ssid_len = ssid_len;
606 	p2p->inv_persistent = persistent_group;
607 	return p2p_invite_send(p2p, dev, go_dev_addr);
608 }
609