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