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
p2p_build_invitation_req(struct p2p_data * p2p,struct p2p_device * peer,const u8 * go_dev_addr)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
25 buf = wpabuf_alloc(1000);
26 if (buf == NULL)
27 return NULL;
28
29 peer->dialog_token++;
30 if (peer->dialog_token == 0)
31 peer->dialog_token = 1;
32 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
33 peer->dialog_token);
34
35 len = p2p_buf_add_ie_hdr(buf);
36 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
37 p2p_buf_add_config_timeout(buf, 0, 0);
38 else
39 p2p_buf_add_config_timeout(buf, 100, 20);
40 p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
41 P2P_INVITATION_FLAGS_TYPE : 0);
42 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
43 p2p->op_reg_class, p2p->op_channel);
44 if (p2p->inv_bssid_set)
45 p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
46 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
47 if (go_dev_addr)
48 dev_addr = go_dev_addr;
49 else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
50 dev_addr = peer->info.p2p_device_addr;
51 else
52 dev_addr = p2p->cfg->dev_addr;
53 p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
54 p2p_buf_add_device_info(buf, p2p, peer);
55 p2p_buf_update_ie_hdr(buf, len);
56
57 return buf;
58 }
59
60
p2p_build_invitation_resp(struct p2p_data * p2p,struct p2p_device * peer,u8 dialog_token,u8 status,const u8 * group_bssid,u8 reg_class,u8 channel,struct p2p_channels * channels)61 static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
62 struct p2p_device *peer,
63 u8 dialog_token, u8 status,
64 const u8 *group_bssid,
65 u8 reg_class, u8 channel,
66 struct p2p_channels *channels)
67 {
68 struct wpabuf *buf;
69 u8 *len;
70
71 buf = wpabuf_alloc(1000);
72 if (buf == NULL)
73 return NULL;
74
75 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
76 dialog_token);
77
78 len = p2p_buf_add_ie_hdr(buf);
79 p2p_buf_add_status(buf, status);
80 p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
81 if (reg_class && channel)
82 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
83 reg_class, channel);
84 if (group_bssid)
85 p2p_buf_add_group_bssid(buf, group_bssid);
86 if (channels)
87 p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
88 p2p_buf_update_ie_hdr(buf, len);
89
90 return buf;
91 }
92
93
p2p_process_invitation_req(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len,int rx_freq)94 void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
95 const u8 *data, size_t len, int rx_freq)
96 {
97 struct p2p_device *dev;
98 struct p2p_message msg;
99 struct wpabuf *resp = NULL;
100 u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
101 int freq;
102 int go = 0;
103 u8 group_bssid[ETH_ALEN], *bssid;
104 int op_freq = 0;
105 u8 reg_class = 0, channel = 0;
106 struct p2p_channels intersection, *channels = NULL;
107 int persistent;
108
109 os_memset(group_bssid, 0, sizeof(group_bssid));
110
111 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
112 "P2P: Received Invitation Request from " MACSTR " (freq=%d)",
113 MAC2STR(sa), rx_freq);
114
115 if (p2p_parse(data, len, &msg))
116 return;
117
118 dev = p2p_get_device(p2p, sa);
119 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
120 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
121 "P2P: Invitation Request from unknown peer "
122 MACSTR, MAC2STR(sa));
123
124 if (p2p_add_device(p2p, sa, rx_freq, 0, data + 1, len - 1)) {
125 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
126 "P2P: Invitation Request add device failed "
127 MACSTR, MAC2STR(sa));
128 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
129 goto fail;
130 }
131
132 dev = p2p_get_device(p2p, sa);
133 if (dev == NULL) {
134 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
135 "P2P: Reject Invitation Request from unknown "
136 "peer " MACSTR, MAC2STR(sa));
137 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
138 goto fail;
139 }
140 }
141
142 if (!msg.group_id || !msg.channel_list) {
143 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
144 "P2P: Mandatory attribute missing in Invitation "
145 "Request from " MACSTR, MAC2STR(sa));
146 status = P2P_SC_FAIL_INVALID_PARAMS;
147 goto fail;
148 }
149
150 if (msg.invitation_flags)
151 persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
152 else {
153 /* Invitation Flags is a mandatory attribute starting from P2P
154 * spec 1.06. As a backwards compatibility mechanism, assume
155 * the request was for a persistent group if the attribute is
156 * missing.
157 */
158 wpa_printf(MSG_DEBUG, "P2P: Mandatory Invitation Flags "
159 "attribute missing from Invitation Request");
160 persistent = 1;
161 }
162
163 if (p2p_peer_channels_check(p2p, &p2p->cfg->channels, dev,
164 msg.channel_list, msg.channel_list_len) <
165 0) {
166 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
167 "P2P: No common channels found");
168 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
169 goto fail;
170 }
171
172 if (p2p->cfg->invitation_process) {
173 status = p2p->cfg->invitation_process(
174 p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
175 msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
176 &go, group_bssid, &op_freq, persistent);
177 }
178
179 if (op_freq) {
180 if (p2p_freq_to_channel(p2p->cfg->country, op_freq,
181 ®_class, &channel) < 0) {
182 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
183 "P2P: Unknown forced freq %d MHz from "
184 "invitation_process()", op_freq);
185 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
186 goto fail;
187 }
188
189 p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
190 &intersection);
191 if (!p2p_channels_includes(&intersection, reg_class, channel))
192 {
193 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
194 "P2P: forced freq %d MHz not in the supported "
195 "channels interaction", op_freq);
196 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
197 goto fail;
198 }
199
200 if (status == P2P_SC_SUCCESS)
201 channels = &intersection;
202 } else {
203 op_freq = p2p_channel_to_freq(p2p->cfg->country,
204 p2p->cfg->op_reg_class,
205 p2p->cfg->op_channel);
206 if (op_freq < 0) {
207 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
208 "P2P: Unknown operational channel "
209 "(country=%c%c reg_class=%u channel=%u)",
210 p2p->cfg->country[0], p2p->cfg->country[1],
211 p2p->cfg->op_reg_class, p2p->cfg->op_channel);
212 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
213 goto fail;
214 }
215
216 p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
217 &intersection);
218 if (status == P2P_SC_SUCCESS) {
219 reg_class = p2p->cfg->op_reg_class;
220 channel = p2p->cfg->op_channel;
221 channels = &intersection;
222 }
223 }
224
225 fail:
226 if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
227 bssid = group_bssid;
228 else
229 bssid = NULL;
230 resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
231 bssid, reg_class, channel, channels);
232
233 if (resp == NULL)
234 goto out;
235
236 if (rx_freq > 0)
237 freq = rx_freq;
238 else
239 freq = p2p_channel_to_freq(p2p->cfg->country,
240 p2p->cfg->reg_class,
241 p2p->cfg->channel);
242 if (freq < 0) {
243 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
244 "P2P: Unknown regulatory class/channel");
245 goto out;
246 }
247
248 /*
249 * Store copy of invitation data to be used when processing TX status
250 * callback for the Acton frame.
251 */
252 os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
253 if (msg.group_bssid) {
254 os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
255 p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
256 } else
257 p2p->inv_group_bssid_ptr = NULL;
258 if (msg.group_id_len - ETH_ALEN <= 32) {
259 os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
260 msg.group_id_len - ETH_ALEN);
261 p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
262 }
263 os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
264 p2p->inv_status = status;
265 p2p->inv_op_freq = op_freq;
266
267 p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
268 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
269 p2p->cfg->dev_addr,
270 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
271 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
272 "P2P: Failed to send Action frame");
273 }
274
275 out:
276 wpabuf_free(resp);
277 p2p_parse_free(&msg);
278 }
279
280
p2p_process_invitation_resp(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len)281 void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
282 const u8 *data, size_t len)
283 {
284 struct p2p_device *dev;
285 struct p2p_message msg;
286
287 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
288 "P2P: Received Invitation Response from " MACSTR,
289 MAC2STR(sa));
290
291 dev = p2p_get_device(p2p, sa);
292 if (dev == NULL) {
293 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
294 "P2P: Ignore Invitation Response from unknown peer "
295 MACSTR, MAC2STR(sa));
296 return;
297 }
298
299 if (dev != p2p->invite_peer) {
300 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
301 "P2P: Ignore unexpected Invitation Response from peer "
302 MACSTR, MAC2STR(sa));
303 return;
304 }
305
306 if (p2p_parse(data, len, &msg))
307 return;
308
309 if (!msg.status) {
310 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
311 "P2P: Mandatory Status attribute missing in "
312 "Invitation Response from " MACSTR, MAC2STR(sa));
313 p2p_parse_free(&msg);
314 return;
315 }
316
317 if (p2p->cfg->invitation_result)
318 p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
319 msg.group_bssid);
320
321 p2p_parse_free(&msg);
322
323 p2p_clear_timeout(p2p);
324 p2p_set_state(p2p, P2P_IDLE);
325 p2p->invite_peer = NULL;
326 }
327
328
p2p_invite_send(struct p2p_data * p2p,struct p2p_device * dev,const u8 * go_dev_addr)329 int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
330 const u8 *go_dev_addr)
331 {
332 struct wpabuf *req;
333 int freq;
334
335 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
336 if (freq <= 0) {
337 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
338 "P2P: No Listen/Operating frequency known for the "
339 "peer " MACSTR " to send Invitation Request",
340 MAC2STR(dev->info.p2p_device_addr));
341 return -1;
342 }
343
344 req = p2p_build_invitation_req(p2p, dev, go_dev_addr);
345 if (req == NULL)
346 return -1;
347 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
348 "P2P: Sending Invitation Request");
349 p2p_set_state(p2p, P2P_INVITE);
350 p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
351 p2p->invite_peer = dev;
352 dev->invitation_reqs++;
353 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
354 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
355 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
356 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
357 "P2P: Failed to send Action frame");
358 /* Use P2P find to recover and retry */
359 p2p_set_timeout(p2p, 0, 0);
360 }
361
362 wpabuf_free(req);
363
364 return 0;
365 }
366
367
p2p_invitation_req_cb(struct p2p_data * p2p,int success)368 void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
369 {
370 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
371 "P2P: Invitation Request TX callback: success=%d", success);
372
373 if (p2p->invite_peer == NULL) {
374 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
375 "P2P: No pending Invite");
376 return;
377 }
378
379 /*
380 * Use P2P find, if needed, to find the other device from its listen
381 * channel.
382 */
383 p2p_set_state(p2p, P2P_INVITE);
384 p2p_set_timeout(p2p, 0, 100000);
385 }
386
387
p2p_invitation_resp_cb(struct p2p_data * p2p,int success)388 void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
389 {
390 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
391 "P2P: Invitation Response TX callback: success=%d", success);
392 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
393
394 if (success && p2p->cfg->invitation_received) {
395 p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
396 p2p->inv_sa,
397 p2p->inv_group_bssid_ptr,
398 p2p->inv_ssid, p2p->inv_ssid_len,
399 p2p->inv_go_dev_addr,
400 p2p->inv_status,
401 p2p->inv_op_freq);
402 }
403 }
404
405
p2p_invite(struct p2p_data * p2p,const u8 * peer,enum p2p_invite_role role,const u8 * bssid,const u8 * ssid,size_t ssid_len,unsigned int force_freq,const u8 * go_dev_addr,int persistent_group)406 int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
407 const u8 *bssid, const u8 *ssid, size_t ssid_len,
408 unsigned int force_freq, const u8 *go_dev_addr,
409 int persistent_group)
410 {
411 struct p2p_device *dev;
412
413 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
414 "P2P: Request to invite peer " MACSTR " role=%d persistent=%d "
415 "force_freq=%u",
416 MAC2STR(peer), role, persistent_group, force_freq);
417 if (bssid)
418 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
419 "P2P: Invitation for BSSID " MACSTR, MAC2STR(bssid));
420 if (go_dev_addr) {
421 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
422 "P2P: Invitation for GO Device Address " MACSTR,
423 MAC2STR(go_dev_addr));
424 os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
425 p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
426 } else
427 p2p->invite_go_dev_addr = NULL;
428 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Invitation for SSID",
429 ssid, ssid_len);
430
431 dev = p2p_get_device(p2p, peer);
432 if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0)) {
433 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
434 "P2P: Cannot invite unknown P2P Device " MACSTR,
435 MAC2STR(peer));
436 return -1;
437 }
438
439 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
440 if (!(dev->info.dev_capab &
441 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
442 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
443 "P2P: Cannot invite a P2P Device " MACSTR
444 " that is in a group and is not discoverable",
445 MAC2STR(peer));
446 }
447 /* TODO: use device discoverability request through GO */
448 }
449
450 dev->invitation_reqs = 0;
451
452 if (force_freq) {
453 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
454 &p2p->op_reg_class, &p2p->op_channel) <
455 0) {
456 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
457 "P2P: Unsupported frequency %u MHz",
458 force_freq);
459 return -1;
460 }
461 p2p->channels.reg_classes = 1;
462 p2p->channels.reg_class[0].channels = 1;
463 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
464 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
465 } else {
466 p2p->op_reg_class = p2p->cfg->op_reg_class;
467 p2p->op_channel = p2p->cfg->op_channel;
468 os_memcpy(&p2p->channels, &p2p->cfg->channels,
469 sizeof(struct p2p_channels));
470 }
471
472 if (p2p->state != P2P_IDLE)
473 p2p_stop_find(p2p);
474
475 p2p->inv_role = role;
476 p2p->inv_bssid_set = bssid != NULL;
477 if (bssid)
478 os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
479 os_memcpy(p2p->inv_ssid, ssid, ssid_len);
480 p2p->inv_ssid_len = ssid_len;
481 p2p->inv_persistent = persistent_group;
482 return p2p_invite_send(p2p, dev, go_dev_addr);
483 }
484