2 * Wi-Fi Protected Setup - device attributes
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "wps_dev_attr.h"
16 int wps_build_manufacturer(struct wps_device_data *dev, struct wpabuf *msg)
19 wpa_printf(MSG_DEBUG, "WPS: * Manufacturer");
20 wpabuf_put_be16(msg, ATTR_MANUFACTURER);
21 len = dev->manufacturer ? os_strlen(dev->manufacturer) : 0;
22 #ifndef CONFIG_WPS_STRICT
25 * Some deployed WPS implementations fail to parse zero-length
26 * attributes. As a workaround, send a space character if the
27 * device attribute string is empty.
29 wpabuf_put_be16(msg, 1);
30 wpabuf_put_u8(msg, ' ');
33 #endif /* CONFIG_WPS_STRICT */
34 wpabuf_put_be16(msg, len);
35 wpabuf_put_data(msg, dev->manufacturer, len);
40 int wps_build_model_name(struct wps_device_data *dev, struct wpabuf *msg)
43 wpa_printf(MSG_DEBUG, "WPS: * Model Name");
44 wpabuf_put_be16(msg, ATTR_MODEL_NAME);
45 len = dev->model_name ? os_strlen(dev->model_name) : 0;
46 #ifndef CONFIG_WPS_STRICT
49 * Some deployed WPS implementations fail to parse zero-length
50 * attributes. As a workaround, send a space character if the
51 * device attribute string is empty.
53 wpabuf_put_be16(msg, 1);
54 wpabuf_put_u8(msg, ' ');
57 #endif /* CONFIG_WPS_STRICT */
58 wpabuf_put_be16(msg, len);
59 wpabuf_put_data(msg, dev->model_name, len);
64 int wps_build_model_number(struct wps_device_data *dev, struct wpabuf *msg)
67 wpa_printf(MSG_DEBUG, "WPS: * Model Number");
68 wpabuf_put_be16(msg, ATTR_MODEL_NUMBER);
69 len = dev->model_number ? os_strlen(dev->model_number) : 0;
70 #ifndef CONFIG_WPS_STRICT
73 * Some deployed WPS implementations fail to parse zero-length
74 * attributes. As a workaround, send a space character if the
75 * device attribute string is empty.
77 wpabuf_put_be16(msg, 1);
78 wpabuf_put_u8(msg, ' ');
81 #endif /* CONFIG_WPS_STRICT */
82 wpabuf_put_be16(msg, len);
83 wpabuf_put_data(msg, dev->model_number, len);
88 static int wps_build_serial_number(struct wps_device_data *dev,
92 wpa_printf(MSG_DEBUG, "WPS: * Serial Number");
93 wpabuf_put_be16(msg, ATTR_SERIAL_NUMBER);
94 len = dev->serial_number ? os_strlen(dev->serial_number) : 0;
95 #ifndef CONFIG_WPS_STRICT
98 * Some deployed WPS implementations fail to parse zero-length
99 * attributes. As a workaround, send a space character if the
100 * device attribute string is empty.
102 wpabuf_put_be16(msg, 1);
103 wpabuf_put_u8(msg, ' ');
106 #endif /* CONFIG_WPS_STRICT */
107 wpabuf_put_be16(msg, len);
108 wpabuf_put_data(msg, dev->serial_number, len);
113 int wps_build_primary_dev_type(struct wps_device_data *dev, struct wpabuf *msg)
115 wpa_printf(MSG_DEBUG, "WPS: * Primary Device Type");
116 wpabuf_put_be16(msg, ATTR_PRIMARY_DEV_TYPE);
117 wpabuf_put_be16(msg, WPS_DEV_TYPE_LEN);
118 wpabuf_put_data(msg, dev->pri_dev_type, WPS_DEV_TYPE_LEN);
123 int wps_build_secondary_dev_type(struct wps_device_data *dev,
126 if (!dev->num_sec_dev_types)
129 wpa_printf(MSG_DEBUG, "WPS: * Secondary Device Type");
130 wpabuf_put_be16(msg, ATTR_SECONDARY_DEV_TYPE_LIST);
131 wpabuf_put_be16(msg, WPS_DEV_TYPE_LEN * dev->num_sec_dev_types);
132 wpabuf_put_data(msg, dev->sec_dev_type,
133 WPS_DEV_TYPE_LEN * dev->num_sec_dev_types);
139 int wps_build_req_dev_type(struct wps_device_data *dev, struct wpabuf *msg,
140 unsigned int num_req_dev_types,
141 const u8 *req_dev_types)
145 for (i = 0; i < num_req_dev_types; i++) {
146 wpa_hexdump(MSG_DEBUG, "WPS: * Requested Device Type",
147 req_dev_types + i * WPS_DEV_TYPE_LEN,
149 wpabuf_put_be16(msg, ATTR_REQUESTED_DEV_TYPE);
150 wpabuf_put_be16(msg, WPS_DEV_TYPE_LEN);
151 wpabuf_put_data(msg, req_dev_types + i * WPS_DEV_TYPE_LEN,
159 int wps_build_dev_name(struct wps_device_data *dev, struct wpabuf *msg)
162 wpa_printf(MSG_DEBUG, "WPS: * Device Name");
163 wpabuf_put_be16(msg, ATTR_DEV_NAME);
164 len = dev->device_name ? os_strlen(dev->device_name) : 0;
165 #ifndef CONFIG_WPS_STRICT
168 * Some deployed WPS implementations fail to parse zero-length
169 * attributes. As a workaround, send a space character if the
170 * device attribute string is empty.
172 wpabuf_put_be16(msg, 1);
173 wpabuf_put_u8(msg, ' ');
176 #endif /* CONFIG_WPS_STRICT */
177 wpabuf_put_be16(msg, len);
178 wpabuf_put_data(msg, dev->device_name, len);
183 int wps_build_device_attrs(struct wps_device_data *dev, struct wpabuf *msg)
185 if (wps_build_manufacturer(dev, msg) ||
186 wps_build_model_name(dev, msg) ||
187 wps_build_model_number(dev, msg) ||
188 wps_build_serial_number(dev, msg) ||
189 wps_build_primary_dev_type(dev, msg) ||
190 wps_build_dev_name(dev, msg))
196 int wps_build_os_version(struct wps_device_data *dev, struct wpabuf *msg)
198 wpa_printf(MSG_DEBUG, "WPS: * OS Version");
199 wpabuf_put_be16(msg, ATTR_OS_VERSION);
200 wpabuf_put_be16(msg, 4);
201 wpabuf_put_be32(msg, 0x80000000 | dev->os_version);
206 int wps_build_vendor_ext_m1(struct wps_device_data *dev, struct wpabuf *msg)
208 if (dev->vendor_ext_m1 != NULL) {
209 wpa_hexdump(MSG_DEBUG, "WPS: * Vendor Extension M1",
210 wpabuf_head_u8(dev->vendor_ext_m1),
211 wpabuf_len(dev->vendor_ext_m1));
212 wpabuf_put_be16(msg, ATTR_VENDOR_EXT);
213 wpabuf_put_be16(msg, wpabuf_len(dev->vendor_ext_m1));
214 wpabuf_put_buf(msg, dev->vendor_ext_m1);
220 int wps_build_rf_bands(struct wps_device_data *dev, struct wpabuf *msg)
222 wpa_printf(MSG_DEBUG, "WPS: * RF Bands (%x)", dev->rf_bands);
223 wpabuf_put_be16(msg, ATTR_RF_BANDS);
224 wpabuf_put_be16(msg, 1);
225 wpabuf_put_u8(msg, dev->rf_bands);
230 int wps_build_vendor_ext(struct wps_device_data *dev, struct wpabuf *msg)
234 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
235 if (dev->vendor_ext[i] == NULL)
237 wpa_hexdump(MSG_DEBUG, "WPS: * Vendor Extension",
238 wpabuf_head_u8(dev->vendor_ext[i]),
239 wpabuf_len(dev->vendor_ext[i]));
240 wpabuf_put_be16(msg, ATTR_VENDOR_EXT);
241 wpabuf_put_be16(msg, wpabuf_len(dev->vendor_ext[i]));
242 wpabuf_put_buf(msg, dev->vendor_ext[i]);
249 static int wps_process_manufacturer(struct wps_device_data *dev, const u8 *str,
253 wpa_printf(MSG_DEBUG, "WPS: No Manufacturer received");
257 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer", str, str_len);
259 os_free(dev->manufacturer);
260 dev->manufacturer = os_malloc(str_len + 1);
261 if (dev->manufacturer == NULL)
263 os_memcpy(dev->manufacturer, str, str_len);
264 dev->manufacturer[str_len] = '\0';
270 static int wps_process_model_name(struct wps_device_data *dev, const u8 *str,
274 wpa_printf(MSG_DEBUG, "WPS: No Model Name received");
278 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name", str, str_len);
280 os_free(dev->model_name);
281 dev->model_name = os_malloc(str_len + 1);
282 if (dev->model_name == NULL)
284 os_memcpy(dev->model_name, str, str_len);
285 dev->model_name[str_len] = '\0';
291 static int wps_process_model_number(struct wps_device_data *dev, const u8 *str,
295 wpa_printf(MSG_DEBUG, "WPS: No Model Number received");
299 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number", str, str_len);
301 os_free(dev->model_number);
302 dev->model_number = os_malloc(str_len + 1);
303 if (dev->model_number == NULL)
305 os_memcpy(dev->model_number, str, str_len);
306 dev->model_number[str_len] = '\0';
312 static int wps_process_serial_number(struct wps_device_data *dev,
313 const u8 *str, size_t str_len)
316 wpa_printf(MSG_DEBUG, "WPS: No Serial Number received");
320 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number", str, str_len);
322 os_free(dev->serial_number);
323 dev->serial_number = os_malloc(str_len + 1);
324 if (dev->serial_number == NULL)
326 os_memcpy(dev->serial_number, str, str_len);
327 dev->serial_number[str_len] = '\0';
333 static int wps_process_dev_name(struct wps_device_data *dev, const u8 *str,
337 wpa_printf(MSG_DEBUG, "WPS: No Device Name received");
341 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name", str, str_len);
343 os_free(dev->device_name);
344 dev->device_name = os_malloc(str_len + 1);
345 if (dev->device_name == NULL)
347 os_memcpy(dev->device_name, str, str_len);
348 dev->device_name[str_len] = '\0';
354 static int wps_process_primary_dev_type(struct wps_device_data *dev,
357 #ifndef CONFIG_NO_STDOUT_DEBUG
358 char devtype[WPS_DEV_TYPE_BUFSIZE];
359 #endif /* CONFIG_NO_STDOUT_DEBUG */
361 if (dev_type == NULL) {
362 wpa_printf(MSG_DEBUG, "WPS: No Primary Device Type received");
366 os_memcpy(dev->pri_dev_type, dev_type, WPS_DEV_TYPE_LEN);
367 wpa_printf(MSG_DEBUG, "WPS: Primary Device Type: %s",
368 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
375 int wps_process_device_attrs(struct wps_device_data *dev,
376 struct wps_parse_attr *attr)
378 if (wps_process_manufacturer(dev, attr->manufacturer,
379 attr->manufacturer_len) ||
380 wps_process_model_name(dev, attr->model_name,
381 attr->model_name_len) ||
382 wps_process_model_number(dev, attr->model_number,
383 attr->model_number_len) ||
384 wps_process_serial_number(dev, attr->serial_number,
385 attr->serial_number_len) ||
386 wps_process_primary_dev_type(dev, attr->primary_dev_type) ||
387 wps_process_dev_name(dev, attr->dev_name, attr->dev_name_len))
393 int wps_process_os_version(struct wps_device_data *dev, const u8 *ver)
396 wpa_printf(MSG_DEBUG, "WPS: No OS Version received");
400 dev->os_version = WPA_GET_BE32(ver);
401 wpa_printf(MSG_DEBUG, "WPS: OS Version %08x", dev->os_version);
407 int wps_process_rf_bands(struct wps_device_data *dev, const u8 *bands)
410 wpa_printf(MSG_DEBUG, "WPS: No RF Bands received");
414 dev->rf_bands = *bands;
415 wpa_printf(MSG_DEBUG, "WPS: Enrollee RF Bands 0x%x", dev->rf_bands);
421 void wps_device_data_dup(struct wps_device_data *dst,
422 const struct wps_device_data *src)
424 if (src->device_name)
425 dst->device_name = os_strdup(src->device_name);
426 if (src->manufacturer)
427 dst->manufacturer = os_strdup(src->manufacturer);
429 dst->model_name = os_strdup(src->model_name);
430 if (src->model_number)
431 dst->model_number = os_strdup(src->model_number);
432 if (src->serial_number)
433 dst->serial_number = os_strdup(src->serial_number);
434 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
435 dst->os_version = src->os_version;
436 dst->rf_bands = src->rf_bands;
440 void wps_device_data_free(struct wps_device_data *dev)
442 os_free(dev->device_name);
443 dev->device_name = NULL;
444 os_free(dev->manufacturer);
445 dev->manufacturer = NULL;
446 os_free(dev->model_name);
447 dev->model_name = NULL;
448 os_free(dev->model_number);
449 dev->model_number = NULL;
450 os_free(dev->serial_number);
451 dev->serial_number = NULL;