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