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