automake build system
[mech_eap.orig] / src / p2p / p2p_build.c
1 /*
2  * P2P - IE builder
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_i.h"
20 #include "p2p_i.h"
21
22
23 void p2p_buf_add_action_hdr(struct wpabuf *buf, u8 subtype, u8 dialog_token)
24 {
25         wpabuf_put_u8(buf, WLAN_ACTION_VENDOR_SPECIFIC);
26         wpabuf_put_be24(buf, OUI_WFA);
27         wpabuf_put_u8(buf, P2P_OUI_TYPE);
28
29         wpabuf_put_u8(buf, subtype); /* OUI Subtype */
30         wpabuf_put_u8(buf, dialog_token);
31         wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", dialog_token);
32 }
33
34
35 void p2p_buf_add_public_action_hdr(struct wpabuf *buf, u8 subtype,
36                                    u8 dialog_token)
37 {
38         wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
39         wpabuf_put_u8(buf, WLAN_PA_VENDOR_SPECIFIC);
40         wpabuf_put_be24(buf, OUI_WFA);
41         wpabuf_put_u8(buf, P2P_OUI_TYPE);
42
43         wpabuf_put_u8(buf, subtype); /* OUI Subtype */
44         wpabuf_put_u8(buf, dialog_token);
45         wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", dialog_token);
46 }
47
48
49 u8 * p2p_buf_add_ie_hdr(struct wpabuf *buf)
50 {
51         u8 *len;
52
53         /* P2P IE header */
54         wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
55         len = wpabuf_put(buf, 1); /* IE length to be filled */
56         wpabuf_put_be24(buf, OUI_WFA);
57         wpabuf_put_u8(buf, P2P_OUI_TYPE);
58         wpa_printf(MSG_DEBUG, "P2P: * P2P IE header");
59         return len;
60 }
61
62
63 void p2p_buf_update_ie_hdr(struct wpabuf *buf, u8 *len)
64 {
65         /* Update P2P IE Length */
66         *len = (u8 *) wpabuf_put(buf, 0) - len - 1;
67 }
68
69
70 void p2p_buf_add_capability(struct wpabuf *buf, u8 dev_capab, u8 group_capab)
71 {
72         /* P2P Capability */
73         wpabuf_put_u8(buf, P2P_ATTR_CAPABILITY);
74         wpabuf_put_le16(buf, 2);
75         wpabuf_put_u8(buf, dev_capab); /* Device Capabilities */
76         wpabuf_put_u8(buf, group_capab); /* Group Capabilities */
77         wpa_printf(MSG_DEBUG, "P2P: * Capability dev=%02x group=%02x",
78                    dev_capab, group_capab);
79 }
80
81
82 void p2p_buf_add_go_intent(struct wpabuf *buf, u8 go_intent)
83 {
84         /* Group Owner Intent */
85         wpabuf_put_u8(buf, P2P_ATTR_GROUP_OWNER_INTENT);
86         wpabuf_put_le16(buf, 1);
87         wpabuf_put_u8(buf, go_intent);
88         wpa_printf(MSG_DEBUG, "P2P: * GO Intent: Intent %u Tie breaker %u",
89                    go_intent >> 1, go_intent & 0x01);
90 }
91
92
93 void p2p_buf_add_listen_channel(struct wpabuf *buf, const char *country,
94                                 u8 reg_class, u8 channel)
95 {
96         /* Listen Channel */
97         wpabuf_put_u8(buf, P2P_ATTR_LISTEN_CHANNEL);
98         wpabuf_put_le16(buf, 5);
99         wpabuf_put_data(buf, country, 3);
100         wpabuf_put_u8(buf, reg_class); /* Regulatory Class */
101         wpabuf_put_u8(buf, channel); /* Channel Number */
102         wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: Regulatory Class %u "
103                    "Channel %u", reg_class, channel);
104 }
105
106
107 void p2p_buf_add_operating_channel(struct wpabuf *buf, const char *country,
108                                    u8 reg_class, u8 channel)
109 {
110         /* Operating Channel */
111         wpabuf_put_u8(buf, P2P_ATTR_OPERATING_CHANNEL);
112         wpabuf_put_le16(buf, 5);
113         wpabuf_put_data(buf, country, 3);
114         wpabuf_put_u8(buf, reg_class); /* Regulatory Class */
115         wpabuf_put_u8(buf, channel); /* Channel Number */
116         wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: Regulatory Class %u "
117                    "Channel %u", reg_class, channel);
118 }
119
120
121 void p2p_buf_add_channel_list(struct wpabuf *buf, const char *country,
122                               struct p2p_channels *chan)
123 {
124         u8 *len;
125         size_t i;
126
127         /* Channel List */
128         wpabuf_put_u8(buf, P2P_ATTR_CHANNEL_LIST);
129         len = wpabuf_put(buf, 2); /* IE length to be filled */
130         wpabuf_put_data(buf, country, 3); /* Country String */
131
132         for (i = 0; i < chan->reg_classes; i++) {
133                 struct p2p_reg_class *c = &chan->reg_class[i];
134                 wpabuf_put_u8(buf, c->reg_class);
135                 wpabuf_put_u8(buf, c->channels);
136                 wpabuf_put_data(buf, c->channel, c->channels);
137         }
138
139         /* Update attribute length */
140         WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
141         wpa_printf(MSG_DEBUG, "P2P: * Channel List");
142 }
143
144
145 void p2p_buf_add_status(struct wpabuf *buf, u8 status)
146 {
147         /* Status */
148         wpabuf_put_u8(buf, P2P_ATTR_STATUS);
149         wpabuf_put_le16(buf, 1);
150         wpabuf_put_u8(buf, status);
151         wpa_printf(MSG_DEBUG, "P2P: * Status: %d", status);
152 }
153
154
155 void p2p_buf_add_device_info(struct wpabuf *buf, struct p2p_data *p2p,
156                              struct p2p_device *peer)
157 {
158         u8 *len;
159         u16 methods;
160         size_t nlen, i;
161
162         /* P2P Device Info */
163         wpabuf_put_u8(buf, P2P_ATTR_DEVICE_INFO);
164         len = wpabuf_put(buf, 2); /* IE length to be filled */
165
166         /* P2P Device address */
167         wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
168
169         /* Config Methods */
170         methods = 0;
171         if (peer && peer->wps_method != WPS_NOT_READY) {
172                 if (peer->wps_method == WPS_PBC)
173                         methods |= WPS_CONFIG_PUSHBUTTON;
174                 else if (peer->wps_method == WPS_PIN_LABEL)
175                         methods |= WPS_CONFIG_LABEL;
176                 else if (peer->wps_method == WPS_PIN_DISPLAY ||
177                          peer->wps_method == WPS_PIN_KEYPAD)
178                         methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
179         } else {
180                 methods |= WPS_CONFIG_PUSHBUTTON;
181                 methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
182         }
183         wpabuf_put_be16(buf, methods);
184
185         /* Primary Device Type */
186         wpabuf_put_data(buf, p2p->cfg->pri_dev_type,
187                         sizeof(p2p->cfg->pri_dev_type));
188
189         /* Number of Secondary Device Types */
190         wpabuf_put_u8(buf, p2p->cfg->num_sec_dev_types);
191
192         /* Secondary Device Type List */
193         for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
194                 wpabuf_put_data(buf, p2p->cfg->sec_dev_type[i],
195                                 WPS_DEV_TYPE_LEN);
196
197         /* Device Name */
198         nlen = p2p->cfg->dev_name ? os_strlen(p2p->cfg->dev_name) : 0;
199         wpabuf_put_be16(buf, ATTR_DEV_NAME);
200         wpabuf_put_be16(buf, nlen);
201         wpabuf_put_data(buf, p2p->cfg->dev_name, nlen);
202
203         /* Update attribute length */
204         WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
205         wpa_printf(MSG_DEBUG, "P2P: * Device Info");
206 }
207
208
209 void p2p_buf_add_device_id(struct wpabuf *buf, const u8 *dev_addr)
210 {
211         /* P2P Device ID */
212         wpabuf_put_u8(buf, P2P_ATTR_DEVICE_ID);
213         wpabuf_put_le16(buf, ETH_ALEN);
214         wpabuf_put_data(buf, dev_addr, ETH_ALEN);
215         wpa_printf(MSG_DEBUG, "P2P: * Device ID: " MACSTR, MAC2STR(dev_addr));
216 }
217
218
219 void p2p_buf_add_config_timeout(struct wpabuf *buf, u8 go_timeout,
220                                 u8 client_timeout)
221 {
222         /* Configuration Timeout */
223         wpabuf_put_u8(buf, P2P_ATTR_CONFIGURATION_TIMEOUT);
224         wpabuf_put_le16(buf, 2);
225         wpabuf_put_u8(buf, go_timeout);
226         wpabuf_put_u8(buf, client_timeout);
227         wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout: GO %d (*10ms)  "
228                    "client %d (*10ms)", go_timeout, client_timeout);
229 }
230
231
232 void p2p_buf_add_intended_addr(struct wpabuf *buf, const u8 *interface_addr)
233 {
234         /* Intended P2P Interface Address */
235         wpabuf_put_u8(buf, P2P_ATTR_INTENDED_INTERFACE_ADDR);
236         wpabuf_put_le16(buf, ETH_ALEN);
237         wpabuf_put_data(buf, interface_addr, ETH_ALEN);
238         wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address " MACSTR,
239                    MAC2STR(interface_addr));
240 }
241
242
243 void p2p_buf_add_group_bssid(struct wpabuf *buf, const u8 *bssid)
244 {
245         /* P2P Group BSSID */
246         wpabuf_put_u8(buf, P2P_ATTR_GROUP_BSSID);
247         wpabuf_put_le16(buf, ETH_ALEN);
248         wpabuf_put_data(buf, bssid, ETH_ALEN);
249         wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID " MACSTR,
250                    MAC2STR(bssid));
251 }
252
253
254 void p2p_buf_add_group_id(struct wpabuf *buf, const u8 *dev_addr,
255                           const u8 *ssid, size_t ssid_len)
256 {
257         /* P2P Group ID */
258         wpabuf_put_u8(buf, P2P_ATTR_GROUP_ID);
259         wpabuf_put_le16(buf, ETH_ALEN + ssid_len);
260         wpabuf_put_data(buf, dev_addr, ETH_ALEN);
261         wpabuf_put_data(buf, ssid, ssid_len);
262         wpa_printf(MSG_DEBUG, "P2P: * P2P Group ID " MACSTR,
263                    MAC2STR(dev_addr));
264 }
265
266
267 void p2p_buf_add_invitation_flags(struct wpabuf *buf, u8 flags)
268 {
269         /* Invitation Flags */
270         wpabuf_put_u8(buf, P2P_ATTR_INVITATION_FLAGS);
271         wpabuf_put_le16(buf, 1);
272         wpabuf_put_u8(buf, flags);
273         wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x", flags);
274 }
275
276
277 static void p2p_buf_add_noa_desc(struct wpabuf *buf, struct p2p_noa_desc *desc)
278 {
279         if (desc == NULL)
280                 return;
281
282         wpabuf_put_u8(buf, desc->count_type);
283         wpabuf_put_le32(buf, desc->duration);
284         wpabuf_put_le32(buf, desc->interval);
285         wpabuf_put_le32(buf, desc->start_time);
286 }
287
288
289 void p2p_buf_add_noa(struct wpabuf *buf, u8 noa_index, u8 opp_ps, u8 ctwindow,
290                      struct p2p_noa_desc *desc1, struct p2p_noa_desc *desc2)
291 {
292         /* Notice of Absence */
293         wpabuf_put_u8(buf, P2P_ATTR_NOTICE_OF_ABSENCE);
294         wpabuf_put_le16(buf, 2 + (desc1 ? 13 : 0) + (desc2 ? 13 : 0));
295         wpabuf_put_u8(buf, noa_index);
296         wpabuf_put_u8(buf, (opp_ps ? 0x80 : 0) | (ctwindow & 0x7f));
297         p2p_buf_add_noa_desc(buf, desc1);
298         p2p_buf_add_noa_desc(buf, desc2);
299         wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
300 }
301
302
303 void p2p_buf_add_ext_listen_timing(struct wpabuf *buf, u16 period,
304                                    u16 interval)
305 {
306         /* Extended Listen Timing */
307         wpabuf_put_u8(buf, P2P_ATTR_EXT_LISTEN_TIMING);
308         wpabuf_put_le16(buf, 4);
309         wpabuf_put_le16(buf, period);
310         wpabuf_put_le16(buf, interval);
311         wpa_printf(MSG_DEBUG, "P2P: * Extended Listen Timing (period %u msec  "
312                    "interval %u msec)", period, interval);
313 }
314
315
316 void p2p_buf_add_p2p_interface(struct wpabuf *buf, struct p2p_data *p2p)
317 {
318         /* P2P Interface */
319         wpabuf_put_u8(buf, P2P_ATTR_INTERFACE);
320         wpabuf_put_le16(buf, ETH_ALEN + 1 + ETH_ALEN);
321         /* P2P Device address */
322         wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
323         /*
324          * FIX: Fetch interface address list from driver. Do not include
325          * the P2P Device address if it is never used as interface address.
326          */
327         /* P2P Interface Address Count */
328         wpabuf_put_u8(buf, 1);
329         wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
330 }
331
332
333 void p2p_build_wps_ie(struct p2p_data *p2p, struct wpabuf *buf, u16 pw_id,
334                       int all_attr)
335 {
336         u8 *len;
337         wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
338         len = wpabuf_put(buf, 1);
339         wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
340
341         wps_build_version(buf);
342
343         if (all_attr) {
344                 wpabuf_put_be16(buf, ATTR_WPS_STATE);
345                 wpabuf_put_be16(buf, 1);
346                 wpabuf_put_u8(buf, WPS_STATE_NOT_CONFIGURED);
347         }
348
349         /* Device Password ID */
350         wpabuf_put_be16(buf, ATTR_DEV_PASSWORD_ID);
351         wpabuf_put_be16(buf, 2);
352         wpa_printf(MSG_DEBUG, "P2P: WPS IE Device Password ID: %d", pw_id);
353         wpabuf_put_be16(buf, pw_id);
354
355         if (all_attr) {
356                 size_t nlen;
357
358                 wpabuf_put_be16(buf, ATTR_RESPONSE_TYPE);
359                 wpabuf_put_be16(buf, 1);
360                 wpabuf_put_u8(buf, WPS_RESP_ENROLLEE_INFO);
361
362 #if 0
363                 /* FIX */
364                 wps_build_uuid_e(buf, reg->wps->uuid);
365                 wps_build_manufacturer(dev, buf);
366                 wps_build_model_name(dev, buf);
367                 wps_build_model_number(dev, buf);
368                 wps_build_serial_number(dev, buf);
369 #endif
370
371                 wpabuf_put_be16(buf, ATTR_PRIMARY_DEV_TYPE);
372                 wpabuf_put_be16(buf, WPS_DEV_TYPE_LEN);
373                 wpabuf_put_data(buf, p2p->cfg->pri_dev_type, WPS_DEV_TYPE_LEN);
374
375                 wpabuf_put_be16(buf, ATTR_DEV_NAME);
376                 nlen = p2p->cfg->dev_name ? os_strlen(p2p->cfg->dev_name) : 0;
377                 wpabuf_put_be16(buf, nlen);
378                 if (p2p->cfg->dev_name)
379                         wpabuf_put_data(buf, p2p->cfg->dev_name, nlen);
380
381                 wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
382                 wpabuf_put_be16(buf, 2);
383                 wpabuf_put_be16(buf, 0); /* FIX: ? */
384         }
385
386         wps_build_wfa_ext(buf, 0, NULL, 0);
387
388         p2p_buf_update_ie_hdr(buf, len);
389 }