P2P: Fix GAS fragmentation to match with IEEE 802.11u
[libeap.git] / src / p2p / p2p_pd.c
1 /*
2  * Wi-Fi Direct - P2P provision discovery
3  * Copyright (c) 2009-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 "wps/wps_defs.h"
20 #include "p2p_i.h"
21 #include "p2p.h"
22
23
24 static void p2p_build_wps_ie_config_methods(struct wpabuf *buf,
25                                             u16 config_methods)
26 {
27         u8 *len;
28         wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
29         len = wpabuf_put(buf, 1);
30         wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
31
32         /* Config Methods */
33         wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
34         wpabuf_put_be16(buf, 2);
35         wpabuf_put_be16(buf, config_methods);
36
37         p2p_buf_update_ie_hdr(buf, len);
38 }
39
40
41 static struct wpabuf * p2p_build_prov_disc_req(struct p2p_data *p2p,
42                                                u8 dialog_token,
43                                                u16 config_methods,
44                                                struct p2p_device *go)
45 {
46         struct wpabuf *buf;
47         u8 *len;
48
49         buf = wpabuf_alloc(1000);
50         if (buf == NULL)
51                 return NULL;
52
53         p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_REQ, dialog_token);
54
55         len = p2p_buf_add_ie_hdr(buf);
56         p2p_buf_add_capability(buf, p2p->dev_capab, 0);
57         p2p_buf_add_device_info(buf, p2p, NULL);
58         if (go) {
59                 p2p_buf_add_group_id(buf, go->p2p_device_addr, go->oper_ssid,
60                                      go->oper_ssid_len);
61         }
62         p2p_buf_update_ie_hdr(buf, len);
63
64         /* WPS IE with Config Methods attribute */
65         p2p_build_wps_ie_config_methods(buf, config_methods);
66
67         return buf;
68 }
69
70
71 static struct wpabuf * p2p_build_prov_disc_resp(struct p2p_data *p2p,
72                                                 u8 dialog_token,
73                                                 u16 config_methods)
74 {
75         struct wpabuf *buf;
76
77         buf = wpabuf_alloc(100);
78         if (buf == NULL)
79                 return NULL;
80
81         p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_RESP, dialog_token);
82
83         /* WPS IE with Config Methods attribute */
84         p2p_build_wps_ie_config_methods(buf, config_methods);
85
86         return buf;
87 }
88
89
90 void p2p_process_prov_disc_req(struct p2p_data *p2p, const u8 *sa,
91                                const u8 *data, size_t len, int rx_freq)
92 {
93         struct p2p_message msg;
94         struct p2p_device *dev;
95         int freq;
96         int reject = 1;
97         struct wpabuf *resp;
98
99         if (p2p_parse(data, len, &msg))
100                 return;
101
102         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
103                 "P2P: Received Provision Discovery Request from " MACSTR
104                 " with config methods 0x%x (freq=%d)",
105                 MAC2STR(sa), msg.wps_config_methods, rx_freq);
106
107         dev = p2p_get_device(p2p, sa);
108         if (dev == NULL) {
109                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
110                         "P2P: Provision Discovery Request from "
111                         "unknown peer " MACSTR, MAC2STR(sa));
112         }
113
114         if (!(msg.wps_config_methods &
115               (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD |
116                WPS_CONFIG_PUSHBUTTON))) {
117                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unsupported "
118                         "Config Methods in Provision Discovery Request");
119                 goto out;
120         }
121
122         if (dev)
123                 dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
124                                 P2P_DEV_PD_PEER_KEYPAD);
125         if (msg.wps_config_methods & WPS_CONFIG_DISPLAY) {
126                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
127                         " requested us to show a PIN on display", MAC2STR(sa));
128                 if (dev)
129                         dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
130         } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
131                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
132                         " requested us to write its PIN using keypad",
133                         MAC2STR(sa));
134                 if (dev)
135                         dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
136         }
137
138         reject = 0;
139
140 out:
141         resp = p2p_build_prov_disc_resp(p2p, msg.dialog_token,
142                                         reject ? 0 : msg.wps_config_methods);
143         if (resp == NULL) {
144                 p2p_parse_free(&msg);
145                 return;
146         }
147         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
148                 "P2P: Sending Provision Discovery Response");
149         if (rx_freq > 0)
150                 freq = rx_freq;
151         else
152                 freq = p2p_channel_to_freq(p2p->cfg->country,
153                                            p2p->cfg->reg_class,
154                                            p2p->cfg->channel);
155         if (freq < 0) {
156                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
157                         "P2P: Unknown regulatory class/channel");
158                 wpabuf_free(resp);
159                 p2p_parse_free(&msg);
160                 return;
161         }
162         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
163         if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, sa,
164                                   p2p->cfg->dev_addr, p2p->cfg->dev_addr,
165                                   wpabuf_head(resp), wpabuf_len(resp), 200) <
166             0) {
167                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
168                         "P2P: Failed to send Action frame");
169         }
170
171         wpabuf_free(resp);
172
173         if (!reject && p2p->cfg->prov_disc_req) {
174                 const u8 *dev_addr = sa;
175                 if (msg.p2p_device_addr)
176                         dev_addr = msg.p2p_device_addr;
177                 p2p->cfg->prov_disc_req(p2p->cfg->cb_ctx, sa,
178                                         msg.wps_config_methods,
179                                         dev_addr, msg.pri_dev_type,
180                                         msg.device_name, msg.config_methods,
181                                         msg.capability ? msg.capability[0] : 0,
182                                         msg.capability ? msg.capability[1] :
183                                         0);
184
185         }
186         p2p_parse_free(&msg);
187 }
188
189
190 void p2p_process_prov_disc_resp(struct p2p_data *p2p, const u8 *sa,
191                                 const u8 *data, size_t len)
192 {
193         struct p2p_message msg;
194         struct p2p_device *dev;
195         u16 report_config_methods = 0;
196
197         if (p2p_parse(data, len, &msg))
198                 return;
199
200         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
201                 "P2P: Received Provisioning Discovery Response from " MACSTR
202                 " with config methods 0x%x",
203                 MAC2STR(sa), msg.wps_config_methods);
204
205         dev = p2p_get_device(p2p, sa);
206         if (dev == NULL || !dev->req_config_methods) {
207                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
208                         "P2P: Ignore Provisioning Discovery Response from "
209                         MACSTR " with no pending request", MAC2STR(sa));
210                 p2p_parse_free(&msg);
211                 return;
212         }
213
214         if (dev->dialog_token != msg.dialog_token) {
215                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
216                         "P2P: Ignore Provisioning Discovery Response with "
217                         "unexpected Dialog Token %u (expected %u)",
218                         msg.dialog_token, dev->dialog_token);
219                 p2p_parse_free(&msg);
220                 return;
221         }
222
223         if (msg.wps_config_methods != dev->req_config_methods) {
224                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer rejected "
225                         "our Provisioning Discovery Request");
226                 p2p_parse_free(&msg);
227                 goto out;
228         }
229
230         report_config_methods = dev->req_config_methods;
231         dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
232                         P2P_DEV_PD_PEER_KEYPAD);
233         if (dev->req_config_methods & WPS_CONFIG_DISPLAY) {
234                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
235                         " accepted to show a PIN on display", MAC2STR(sa));
236                 dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
237         } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
238                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
239                         " accepted to write our PIN using keypad",
240                         MAC2STR(sa));
241                 dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
242         }
243         p2p_parse_free(&msg);
244
245 out:
246         dev->req_config_methods = 0;
247         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
248         if (p2p->cfg->prov_disc_resp)
249                 p2p->cfg->prov_disc_resp(p2p->cfg->cb_ctx, sa,
250                                          report_config_methods);
251 }
252
253
254 int p2p_send_prov_disc_req(struct p2p_data *p2p, struct p2p_device *dev,
255                            int join)
256 {
257         struct wpabuf *req;
258         int freq;
259
260         freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
261         if (freq <= 0) {
262                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
263                         "P2P: No Listen/Operating frequency known for the "
264                         "peer " MACSTR " to send Provision Discovery Request",
265                         MAC2STR(dev->p2p_device_addr));
266                 return -1;
267         }
268
269         if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
270                 if (!(dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
271                         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
272                                 "P2P: Cannot use PD with P2P Device " MACSTR
273                                 " that is in a group and is not discoverable",
274                                 MAC2STR(dev->p2p_device_addr));
275                         return -1;
276                 }
277                 /* TODO: use device discoverability request through GO */
278         }
279
280         dev->dialog_token++;
281         if (dev->dialog_token == 0)
282                 dev->dialog_token = 1;
283         req = p2p_build_prov_disc_req(p2p, dev->dialog_token,
284                                       dev->req_config_methods,
285                                       join ? dev : NULL);
286         if (req == NULL)
287                 return -1;
288
289         p2p->pending_action_state = P2P_PENDING_PD;
290         if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq,
291                                   dev->p2p_device_addr, p2p->cfg->dev_addr,
292                                   dev->p2p_device_addr,
293                                   wpabuf_head(req), wpabuf_len(req), 200) < 0)
294         {
295                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
296                         "P2P: Failed to send Action frame");
297                 wpabuf_free(req);
298                 return -1;
299         }
300
301         wpabuf_free(req);
302         return 0;
303 }
304
305
306 int p2p_prov_disc_req(struct p2p_data *p2p, const u8 *peer_addr,
307                       u16 config_methods, int join)
308 {
309         struct p2p_device *dev;
310
311         dev = p2p_get_device(p2p, peer_addr);
312         if (dev == NULL)
313                 dev = p2p_get_device_interface(p2p, peer_addr);
314         if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
315                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision "
316                         "Discovery Request destination " MACSTR
317                         " not yet known", MAC2STR(peer_addr));
318                 return -1;
319         }
320
321         wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision Discovery "
322                 "Request with " MACSTR " (config methods 0x%x)",
323                 MAC2STR(peer_addr), config_methods);
324         if (config_methods == 0)
325                 return -1;
326
327         dev->req_config_methods = config_methods;
328
329         if (p2p->go_neg_peer ||
330             (p2p->state != P2P_IDLE && p2p->state != P2P_SEARCH &&
331              p2p->state != P2P_LISTEN_ONLY)) {
332                 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Busy with other "
333                         "operations; postpone Provision Discovery Request "
334                         "with " MACSTR " (config methods 0x%x)",
335                         MAC2STR(peer_addr), config_methods);
336                 return 0;
337         }
338
339         return p2p_send_prov_disc_req(p2p, dev, join);
340 }