blob: 3c819ff4d5f67925edf63dc7ef7c7e99425534c5 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * P2P - IE builder
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "common/ieee802_11_defs.h"
13#include "wps/wps_i.h"
14#include "p2p_i.h"
15
16
17void p2p_buf_add_action_hdr(struct wpabuf *buf, u8 subtype, u8 dialog_token)
18{
19 wpabuf_put_u8(buf, WLAN_ACTION_VENDOR_SPECIFIC);
20 wpabuf_put_be24(buf, OUI_WFA);
21 wpabuf_put_u8(buf, P2P_OUI_TYPE);
22
23 wpabuf_put_u8(buf, subtype); /* OUI Subtype */
24 wpabuf_put_u8(buf, dialog_token);
25 wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", dialog_token);
26}
27
28
29void p2p_buf_add_public_action_hdr(struct wpabuf *buf, u8 subtype,
30 u8 dialog_token)
31{
32 wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
33 wpabuf_put_u8(buf, WLAN_PA_VENDOR_SPECIFIC);
34 wpabuf_put_be24(buf, OUI_WFA);
35 wpabuf_put_u8(buf, P2P_OUI_TYPE);
36
37 wpabuf_put_u8(buf, subtype); /* OUI Subtype */
38 wpabuf_put_u8(buf, dialog_token);
39 wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", dialog_token);
40}
41
42
43u8 * p2p_buf_add_ie_hdr(struct wpabuf *buf)
44{
45 u8 *len;
46
47 /* P2P IE header */
48 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
49 len = wpabuf_put(buf, 1); /* IE length to be filled */
50 wpabuf_put_be24(buf, OUI_WFA);
51 wpabuf_put_u8(buf, P2P_OUI_TYPE);
52 wpa_printf(MSG_DEBUG, "P2P: * P2P IE header");
53 return len;
54}
55
56
57void p2p_buf_update_ie_hdr(struct wpabuf *buf, u8 *len)
58{
59 /* Update P2P IE Length */
60 *len = (u8 *) wpabuf_put(buf, 0) - len - 1;
61}
62
63
64void p2p_buf_add_capability(struct wpabuf *buf, u8 dev_capab, u8 group_capab)
65{
66 /* P2P Capability */
67 wpabuf_put_u8(buf, P2P_ATTR_CAPABILITY);
68 wpabuf_put_le16(buf, 2);
69 wpabuf_put_u8(buf, dev_capab); /* Device Capabilities */
70 wpabuf_put_u8(buf, group_capab); /* Group Capabilities */
71 wpa_printf(MSG_DEBUG, "P2P: * Capability dev=%02x group=%02x",
72 dev_capab, group_capab);
73}
74
75
76void p2p_buf_add_go_intent(struct wpabuf *buf, u8 go_intent)
77{
78 /* Group Owner Intent */
79 wpabuf_put_u8(buf, P2P_ATTR_GROUP_OWNER_INTENT);
80 wpabuf_put_le16(buf, 1);
81 wpabuf_put_u8(buf, go_intent);
82 wpa_printf(MSG_DEBUG, "P2P: * GO Intent: Intent %u Tie breaker %u",
83 go_intent >> 1, go_intent & 0x01);
84}
85
86
87void p2p_buf_add_listen_channel(struct wpabuf *buf, const char *country,
88 u8 reg_class, u8 channel)
89{
90 /* Listen Channel */
91 wpabuf_put_u8(buf, P2P_ATTR_LISTEN_CHANNEL);
92 wpabuf_put_le16(buf, 5);
93 wpabuf_put_data(buf, country, 3);
94 wpabuf_put_u8(buf, reg_class); /* Regulatory Class */
95 wpabuf_put_u8(buf, channel); /* Channel Number */
96 wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: Regulatory Class %u "
97 "Channel %u", reg_class, channel);
98}
99
100
101void p2p_buf_add_operating_channel(struct wpabuf *buf, const char *country,
102 u8 reg_class, u8 channel)
103{
104 /* Operating Channel */
105 wpabuf_put_u8(buf, P2P_ATTR_OPERATING_CHANNEL);
106 wpabuf_put_le16(buf, 5);
107 wpabuf_put_data(buf, country, 3);
108 wpabuf_put_u8(buf, reg_class); /* Regulatory Class */
109 wpabuf_put_u8(buf, channel); /* Channel Number */
110 wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: Regulatory Class %u "
111 "Channel %u", reg_class, channel);
112}
113
114
115void p2p_buf_add_channel_list(struct wpabuf *buf, const char *country,
116 struct p2p_channels *chan)
117{
118 u8 *len;
119 size_t i;
120
121 /* Channel List */
122 wpabuf_put_u8(buf, P2P_ATTR_CHANNEL_LIST);
123 len = wpabuf_put(buf, 2); /* IE length to be filled */
124 wpabuf_put_data(buf, country, 3); /* Country String */
125
126 for (i = 0; i < chan->reg_classes; i++) {
127 struct p2p_reg_class *c = &chan->reg_class[i];
128 wpabuf_put_u8(buf, c->reg_class);
129 wpabuf_put_u8(buf, c->channels);
130 wpabuf_put_data(buf, c->channel, c->channels);
131 }
132
133 /* Update attribute length */
134 WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700135 wpa_hexdump(MSG_DEBUG, "P2P: * Channel List",
136 len + 2, (u8 *) wpabuf_put(buf, 0) - len - 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137}
138
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140void p2p_buf_add_status(struct wpabuf *buf, u8 status)
141{
142 /* Status */
143 wpabuf_put_u8(buf, P2P_ATTR_STATUS);
144 wpabuf_put_le16(buf, 1);
145 wpabuf_put_u8(buf, status);
146 wpa_printf(MSG_DEBUG, "P2P: * Status: %d", status);
147}
148
149
150void p2p_buf_add_device_info(struct wpabuf *buf, struct p2p_data *p2p,
151 struct p2p_device *peer)
152{
153 u8 *len;
154 u16 methods;
155 size_t nlen, i;
156
157 /* P2P Device Info */
158 wpabuf_put_u8(buf, P2P_ATTR_DEVICE_INFO);
159 len = wpabuf_put(buf, 2); /* IE length to be filled */
160
161 /* P2P Device address */
162 wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163
164 /* Config Methods */
165 methods = 0;
166 if (peer && peer->wps_method != WPS_NOT_READY) {
167 if (peer->wps_method == WPS_PBC)
168 methods |= WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 else if (peer->wps_method == WPS_PIN_DISPLAY ||
170 peer->wps_method == WPS_PIN_KEYPAD)
171 methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800172 } else if (p2p->cfg->config_methods) {
173 methods |= p2p->cfg->config_methods &
174 (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_DISPLAY |
175 WPS_CONFIG_KEYPAD);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176 } else {
177 methods |= WPS_CONFIG_PUSHBUTTON;
178 methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
179 }
180 wpabuf_put_be16(buf, methods);
181
182 /* Primary Device Type */
183 wpabuf_put_data(buf, p2p->cfg->pri_dev_type,
184 sizeof(p2p->cfg->pri_dev_type));
185
186 /* Number of Secondary Device Types */
187 wpabuf_put_u8(buf, p2p->cfg->num_sec_dev_types);
188
189 /* Secondary Device Type List */
190 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
191 wpabuf_put_data(buf, p2p->cfg->sec_dev_type[i],
192 WPS_DEV_TYPE_LEN);
193
194 /* Device Name */
195 nlen = p2p->cfg->dev_name ? os_strlen(p2p->cfg->dev_name) : 0;
196 wpabuf_put_be16(buf, ATTR_DEV_NAME);
197 wpabuf_put_be16(buf, nlen);
198 wpabuf_put_data(buf, p2p->cfg->dev_name, nlen);
199
200 /* Update attribute length */
201 WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
202 wpa_printf(MSG_DEBUG, "P2P: * Device Info");
203}
204
205
206void p2p_buf_add_device_id(struct wpabuf *buf, const u8 *dev_addr)
207{
208 /* P2P Device ID */
209 wpabuf_put_u8(buf, P2P_ATTR_DEVICE_ID);
210 wpabuf_put_le16(buf, ETH_ALEN);
211 wpabuf_put_data(buf, dev_addr, ETH_ALEN);
212 wpa_printf(MSG_DEBUG, "P2P: * Device ID: " MACSTR, MAC2STR(dev_addr));
213}
214
215
216void p2p_buf_add_config_timeout(struct wpabuf *buf, u8 go_timeout,
217 u8 client_timeout)
218{
219 /* Configuration Timeout */
220 wpabuf_put_u8(buf, P2P_ATTR_CONFIGURATION_TIMEOUT);
221 wpabuf_put_le16(buf, 2);
222 wpabuf_put_u8(buf, go_timeout);
223 wpabuf_put_u8(buf, client_timeout);
224 wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout: GO %d (*10ms) "
225 "client %d (*10ms)", go_timeout, client_timeout);
226}
227
228
229void p2p_buf_add_intended_addr(struct wpabuf *buf, const u8 *interface_addr)
230{
231 /* Intended P2P Interface Address */
232 wpabuf_put_u8(buf, P2P_ATTR_INTENDED_INTERFACE_ADDR);
233 wpabuf_put_le16(buf, ETH_ALEN);
234 wpabuf_put_data(buf, interface_addr, ETH_ALEN);
235 wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address " MACSTR,
236 MAC2STR(interface_addr));
237}
238
239
240void p2p_buf_add_group_bssid(struct wpabuf *buf, const u8 *bssid)
241{
242 /* P2P Group BSSID */
243 wpabuf_put_u8(buf, P2P_ATTR_GROUP_BSSID);
244 wpabuf_put_le16(buf, ETH_ALEN);
245 wpabuf_put_data(buf, bssid, ETH_ALEN);
246 wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID " MACSTR,
247 MAC2STR(bssid));
248}
249
250
251void p2p_buf_add_group_id(struct wpabuf *buf, const u8 *dev_addr,
252 const u8 *ssid, size_t ssid_len)
253{
254 /* P2P Group ID */
255 wpabuf_put_u8(buf, P2P_ATTR_GROUP_ID);
256 wpabuf_put_le16(buf, ETH_ALEN + ssid_len);
257 wpabuf_put_data(buf, dev_addr, ETH_ALEN);
258 wpabuf_put_data(buf, ssid, ssid_len);
259 wpa_printf(MSG_DEBUG, "P2P: * P2P Group ID " MACSTR,
260 MAC2STR(dev_addr));
261}
262
263
264void p2p_buf_add_invitation_flags(struct wpabuf *buf, u8 flags)
265{
266 /* Invitation Flags */
267 wpabuf_put_u8(buf, P2P_ATTR_INVITATION_FLAGS);
268 wpabuf_put_le16(buf, 1);
269 wpabuf_put_u8(buf, flags);
270 wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x", flags);
271}
272
273
274static void p2p_buf_add_noa_desc(struct wpabuf *buf, struct p2p_noa_desc *desc)
275{
276 if (desc == NULL)
277 return;
278
279 wpabuf_put_u8(buf, desc->count_type);
280 wpabuf_put_le32(buf, desc->duration);
281 wpabuf_put_le32(buf, desc->interval);
282 wpabuf_put_le32(buf, desc->start_time);
283}
284
285
286void p2p_buf_add_noa(struct wpabuf *buf, u8 noa_index, u8 opp_ps, u8 ctwindow,
287 struct p2p_noa_desc *desc1, struct p2p_noa_desc *desc2)
288{
289 /* Notice of Absence */
290 wpabuf_put_u8(buf, P2P_ATTR_NOTICE_OF_ABSENCE);
291 wpabuf_put_le16(buf, 2 + (desc1 ? 13 : 0) + (desc2 ? 13 : 0));
292 wpabuf_put_u8(buf, noa_index);
293 wpabuf_put_u8(buf, (opp_ps ? 0x80 : 0) | (ctwindow & 0x7f));
294 p2p_buf_add_noa_desc(buf, desc1);
295 p2p_buf_add_noa_desc(buf, desc2);
296 wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
297}
298
299
300void p2p_buf_add_ext_listen_timing(struct wpabuf *buf, u16 period,
301 u16 interval)
302{
303 /* Extended Listen Timing */
304 wpabuf_put_u8(buf, P2P_ATTR_EXT_LISTEN_TIMING);
305 wpabuf_put_le16(buf, 4);
306 wpabuf_put_le16(buf, period);
307 wpabuf_put_le16(buf, interval);
308 wpa_printf(MSG_DEBUG, "P2P: * Extended Listen Timing (period %u msec "
309 "interval %u msec)", period, interval);
310}
311
312
313void p2p_buf_add_p2p_interface(struct wpabuf *buf, struct p2p_data *p2p)
314{
315 /* P2P Interface */
316 wpabuf_put_u8(buf, P2P_ATTR_INTERFACE);
317 wpabuf_put_le16(buf, ETH_ALEN + 1 + ETH_ALEN);
318 /* P2P Device address */
319 wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
320 /*
321 * FIX: Fetch interface address list from driver. Do not include
322 * the P2P Device address if it is never used as interface address.
323 */
324 /* P2P Interface Address Count */
325 wpabuf_put_u8(buf, 1);
326 wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
327}
328
329
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700330static int p2p_add_wps_string(struct wpabuf *buf, enum wps_attribute attr,
331 const char *val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332{
333 size_t len;
334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 len = val ? os_strlen(val) : 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700336 if (wpabuf_tailroom(buf) < 4 + len)
337 return -1;
338 wpabuf_put_be16(buf, attr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339#ifndef CONFIG_WPS_STRICT
340 if (len == 0) {
341 /*
342 * Some deployed WPS implementations fail to parse zeor-length
343 * attributes. As a workaround, send a space character if the
344 * device attribute string is empty.
345 */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700346 if (wpabuf_tailroom(buf) < 3)
347 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 wpabuf_put_be16(buf, 1);
349 wpabuf_put_u8(buf, ' ');
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700350 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 }
352#endif /* CONFIG_WPS_STRICT */
353 wpabuf_put_be16(buf, len);
354 if (val)
355 wpabuf_put_data(buf, val, len);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700356 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357}
358
359
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700360int p2p_build_wps_ie(struct p2p_data *p2p, struct wpabuf *buf, int pw_id,
361 int all_attr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362{
363 u8 *len;
364 int i;
365
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700366 if (wpabuf_tailroom(buf) < 6)
367 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
369 len = wpabuf_put(buf, 1);
370 wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
371
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700372 if (wps_build_version(buf) < 0)
373 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700374
375 if (all_attr) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700376 if (wpabuf_tailroom(buf) < 5)
377 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378 wpabuf_put_be16(buf, ATTR_WPS_STATE);
379 wpabuf_put_be16(buf, 1);
380 wpabuf_put_u8(buf, WPS_STATE_NOT_CONFIGURED);
381 }
382
Dmitry Shmidt04949592012-07-19 12:16:46 -0700383 if (pw_id >= 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700384 if (wpabuf_tailroom(buf) < 6)
385 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700386 /* Device Password ID */
387 wpabuf_put_be16(buf, ATTR_DEV_PASSWORD_ID);
388 wpabuf_put_be16(buf, 2);
389 wpa_printf(MSG_DEBUG, "P2P: WPS IE Device Password ID: %d",
390 pw_id);
391 wpabuf_put_be16(buf, pw_id);
392 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393
394 if (all_attr) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700395 if (wpabuf_tailroom(buf) < 5)
396 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397 wpabuf_put_be16(buf, ATTR_RESPONSE_TYPE);
398 wpabuf_put_be16(buf, 1);
399 wpabuf_put_u8(buf, WPS_RESP_ENROLLEE_INFO);
400
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700401 if (wps_build_uuid_e(buf, p2p->cfg->uuid) < 0 ||
402 p2p_add_wps_string(buf, ATTR_MANUFACTURER,
403 p2p->cfg->manufacturer) < 0 ||
404 p2p_add_wps_string(buf, ATTR_MODEL_NAME,
405 p2p->cfg->model_name) < 0 ||
406 p2p_add_wps_string(buf, ATTR_MODEL_NUMBER,
407 p2p->cfg->model_number) < 0 ||
408 p2p_add_wps_string(buf, ATTR_SERIAL_NUMBER,
409 p2p->cfg->serial_number) < 0)
410 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700411
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700412 if (wpabuf_tailroom(buf) < 4 + WPS_DEV_TYPE_LEN)
413 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 wpabuf_put_be16(buf, ATTR_PRIMARY_DEV_TYPE);
415 wpabuf_put_be16(buf, WPS_DEV_TYPE_LEN);
416 wpabuf_put_data(buf, p2p->cfg->pri_dev_type, WPS_DEV_TYPE_LEN);
417
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700418 if (p2p_add_wps_string(buf, ATTR_DEV_NAME, p2p->cfg->dev_name)
419 < 0)
420 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700422 if (wpabuf_tailroom(buf) < 6)
423 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
425 wpabuf_put_be16(buf, 2);
426 wpabuf_put_be16(buf, p2p->cfg->config_methods);
427 }
428
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700429 if (wps_build_wfa_ext(buf, 0, NULL, 0) < 0)
430 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431
432 if (all_attr && p2p->cfg->num_sec_dev_types) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700433 if (wpabuf_tailroom(buf) <
434 4 + WPS_DEV_TYPE_LEN * p2p->cfg->num_sec_dev_types)
435 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 wpabuf_put_be16(buf, ATTR_SECONDARY_DEV_TYPE_LIST);
437 wpabuf_put_be16(buf, WPS_DEV_TYPE_LEN *
438 p2p->cfg->num_sec_dev_types);
439 wpabuf_put_data(buf, p2p->cfg->sec_dev_type,
440 WPS_DEV_TYPE_LEN *
441 p2p->cfg->num_sec_dev_types);
442 }
443
444 /* Add the WPS vendor extensions */
445 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
446 if (p2p->wps_vendor_ext[i] == NULL)
447 break;
448 if (wpabuf_tailroom(buf) <
449 4 + wpabuf_len(p2p->wps_vendor_ext[i]))
450 continue;
451 wpabuf_put_be16(buf, ATTR_VENDOR_EXT);
452 wpabuf_put_be16(buf, wpabuf_len(p2p->wps_vendor_ext[i]));
453 wpabuf_put_buf(buf, p2p->wps_vendor_ext[i]);
454 }
455
456 p2p_buf_update_ie_hdr(buf, len);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700457
458 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459}