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