2 * Wi-Fi Protected Setup - device attributes
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "wps_dev_attr.h"
22 int wps_build_manufacturer(struct wps_device_data *dev, struct wpabuf *msg)
25 wpa_printf(MSG_DEBUG, "WPS: * Manufacturer");
26 wpabuf_put_be16(msg, ATTR_MANUFACTURER);
27 len = dev->manufacturer ? os_strlen(dev->manufacturer) : 0;
28 #ifndef CONFIG_WPS_STRICT
31 * Some deployed WPS implementations fail to parse zero-length
32 * attributes. As a workaround, send a space character if the
33 * device attribute string is empty.
35 wpabuf_put_be16(msg, 1);
36 wpabuf_put_u8(msg, ' ');
39 #endif /* CONFIG_WPS_STRICT */
40 wpabuf_put_be16(msg, len);
41 wpabuf_put_data(msg, dev->manufacturer, len);
46 int wps_build_model_name(struct wps_device_data *dev, struct wpabuf *msg)
49 wpa_printf(MSG_DEBUG, "WPS: * Model Name");
50 wpabuf_put_be16(msg, ATTR_MODEL_NAME);
51 len = dev->model_name ? os_strlen(dev->model_name) : 0;
52 #ifndef CONFIG_WPS_STRICT
55 * Some deployed WPS implementations fail to parse zero-length
56 * attributes. As a workaround, send a space character if the
57 * device attribute string is empty.
59 wpabuf_put_be16(msg, 1);
60 wpabuf_put_u8(msg, ' ');
63 #endif /* CONFIG_WPS_STRICT */
64 wpabuf_put_be16(msg, len);
65 wpabuf_put_data(msg, dev->model_name, len);
70 int wps_build_model_number(struct wps_device_data *dev, struct wpabuf *msg)
73 wpa_printf(MSG_DEBUG, "WPS: * Model Number");
74 wpabuf_put_be16(msg, ATTR_MODEL_NUMBER);
75 len = dev->model_number ? os_strlen(dev->model_number) : 0;
76 #ifndef CONFIG_WPS_STRICT
79 * Some deployed WPS implementations fail to parse zero-length
80 * attributes. As a workaround, send a space character if the
81 * device attribute string is empty.
83 wpabuf_put_be16(msg, 1);
84 wpabuf_put_u8(msg, ' ');
87 #endif /* CONFIG_WPS_STRICT */
88 wpabuf_put_be16(msg, len);
89 wpabuf_put_data(msg, dev->model_number, len);
94 static int wps_build_serial_number(struct wps_device_data *dev,
98 wpa_printf(MSG_DEBUG, "WPS: * Serial Number");
99 wpabuf_put_be16(msg, ATTR_SERIAL_NUMBER);
100 len = dev->serial_number ? os_strlen(dev->serial_number) : 0;
101 #ifndef CONFIG_WPS_STRICT
104 * Some deployed WPS implementations fail to parse zero-length
105 * attributes. As a workaround, send a space character if the
106 * device attribute string is empty.
108 wpabuf_put_be16(msg, 1);
109 wpabuf_put_u8(msg, ' ');
112 #endif /* CONFIG_WPS_STRICT */
113 wpabuf_put_be16(msg, len);
114 wpabuf_put_data(msg, dev->serial_number, len);
119 int wps_build_primary_dev_type(struct wps_device_data *dev, struct wpabuf *msg)
121 wpa_printf(MSG_DEBUG, "WPS: * Primary Device Type");
122 wpabuf_put_be16(msg, ATTR_PRIMARY_DEV_TYPE);
123 wpabuf_put_be16(msg, WPS_DEV_TYPE_LEN);
124 wpabuf_put_data(msg, dev->pri_dev_type, WPS_DEV_TYPE_LEN);
129 int wps_build_dev_name(struct wps_device_data *dev, struct wpabuf *msg)
132 wpa_printf(MSG_DEBUG, "WPS: * Device Name");
133 wpabuf_put_be16(msg, ATTR_DEV_NAME);
134 len = dev->device_name ? os_strlen(dev->device_name) : 0;
135 #ifndef CONFIG_WPS_STRICT
138 * Some deployed WPS implementations fail to parse zero-length
139 * attributes. As a workaround, send a space character if the
140 * device attribute string is empty.
142 wpabuf_put_be16(msg, 1);
143 wpabuf_put_u8(msg, ' ');
146 #endif /* CONFIG_WPS_STRICT */
147 wpabuf_put_be16(msg, len);
148 wpabuf_put_data(msg, dev->device_name, len);
153 int wps_build_device_attrs(struct wps_device_data *dev, struct wpabuf *msg)
155 if (wps_build_manufacturer(dev, msg) ||
156 wps_build_model_name(dev, msg) ||
157 wps_build_model_number(dev, msg) ||
158 wps_build_serial_number(dev, msg) ||
159 wps_build_primary_dev_type(dev, msg) ||
160 wps_build_dev_name(dev, msg))
166 int wps_build_os_version(struct wps_device_data *dev, struct wpabuf *msg)
168 wpa_printf(MSG_DEBUG, "WPS: * OS Version");
169 wpabuf_put_be16(msg, ATTR_OS_VERSION);
170 wpabuf_put_be16(msg, 4);
171 wpabuf_put_be32(msg, 0x80000000 | dev->os_version);
176 int wps_build_rf_bands(struct wps_device_data *dev, struct wpabuf *msg)
178 wpa_printf(MSG_DEBUG, "WPS: * RF Bands (%x)", dev->rf_bands);
179 wpabuf_put_be16(msg, ATTR_RF_BANDS);
180 wpabuf_put_be16(msg, 1);
181 wpabuf_put_u8(msg, dev->rf_bands);
186 static int wps_process_manufacturer(struct wps_device_data *dev, const u8 *str,
190 wpa_printf(MSG_DEBUG, "WPS: No Manufacturer received");
194 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer", str, str_len);
196 os_free(dev->manufacturer);
197 dev->manufacturer = os_malloc(str_len + 1);
198 if (dev->manufacturer == NULL)
200 os_memcpy(dev->manufacturer, str, str_len);
201 dev->manufacturer[str_len] = '\0';
207 static int wps_process_model_name(struct wps_device_data *dev, const u8 *str,
211 wpa_printf(MSG_DEBUG, "WPS: No Model Name received");
215 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name", str, str_len);
217 os_free(dev->model_name);
218 dev->model_name = os_malloc(str_len + 1);
219 if (dev->model_name == NULL)
221 os_memcpy(dev->model_name, str, str_len);
222 dev->model_name[str_len] = '\0';
228 static int wps_process_model_number(struct wps_device_data *dev, const u8 *str,
232 wpa_printf(MSG_DEBUG, "WPS: No Model Number received");
236 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number", str, str_len);
238 os_free(dev->model_number);
239 dev->model_number = os_malloc(str_len + 1);
240 if (dev->model_number == NULL)
242 os_memcpy(dev->model_number, str, str_len);
243 dev->model_number[str_len] = '\0';
249 static int wps_process_serial_number(struct wps_device_data *dev,
250 const u8 *str, size_t str_len)
253 wpa_printf(MSG_DEBUG, "WPS: No Serial Number received");
257 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number", str, str_len);
259 os_free(dev->serial_number);
260 dev->serial_number = os_malloc(str_len + 1);
261 if (dev->serial_number == NULL)
263 os_memcpy(dev->serial_number, str, str_len);
264 dev->serial_number[str_len] = '\0';
270 static int wps_process_dev_name(struct wps_device_data *dev, const u8 *str,
274 wpa_printf(MSG_DEBUG, "WPS: No Device Name received");
278 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name", str, str_len);
280 os_free(dev->device_name);
281 dev->device_name = os_malloc(str_len + 1);
282 if (dev->device_name == NULL)
284 os_memcpy(dev->device_name, str, str_len);
285 dev->device_name[str_len] = '\0';
291 static int wps_process_primary_dev_type(struct wps_device_data *dev,
294 #ifndef CONFIG_NO_STDOUT_DEBUG
295 char devtype[WPS_DEV_TYPE_BUFSIZE];
296 #endif /* CONFIG_NO_STDOUT_DEBUG */
298 if (dev_type == NULL) {
299 wpa_printf(MSG_DEBUG, "WPS: No Primary Device Type received");
303 os_memcpy(dev->pri_dev_type, dev_type, WPS_DEV_TYPE_LEN);
304 wpa_printf(MSG_DEBUG, "WPS: Primary Device Type: %s",
305 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
312 int wps_process_device_attrs(struct wps_device_data *dev,
313 struct wps_parse_attr *attr)
315 if (wps_process_manufacturer(dev, attr->manufacturer,
316 attr->manufacturer_len) ||
317 wps_process_model_name(dev, attr->model_name,
318 attr->model_name_len) ||
319 wps_process_model_number(dev, attr->model_number,
320 attr->model_number_len) ||
321 wps_process_serial_number(dev, attr->serial_number,
322 attr->serial_number_len) ||
323 wps_process_primary_dev_type(dev, attr->primary_dev_type) ||
324 wps_process_dev_name(dev, attr->dev_name, attr->dev_name_len))
330 int wps_process_os_version(struct wps_device_data *dev, const u8 *ver)
333 wpa_printf(MSG_DEBUG, "WPS: No OS Version received");
337 dev->os_version = WPA_GET_BE32(ver);
338 wpa_printf(MSG_DEBUG, "WPS: OS Version %08x", dev->os_version);
344 int wps_process_rf_bands(struct wps_device_data *dev, const u8 *bands)
347 wpa_printf(MSG_DEBUG, "WPS: No RF Bands received");
351 dev->rf_bands = *bands;
352 wpa_printf(MSG_DEBUG, "WPS: Enrollee RF Bands 0x%x", dev->rf_bands);
358 void wps_device_data_dup(struct wps_device_data *dst,
359 const struct wps_device_data *src)
361 if (src->device_name)
362 dst->device_name = os_strdup(src->device_name);
363 if (src->manufacturer)
364 dst->manufacturer = os_strdup(src->manufacturer);
366 dst->model_name = os_strdup(src->model_name);
367 if (src->model_number)
368 dst->model_number = os_strdup(src->model_number);
369 if (src->serial_number)
370 dst->serial_number = os_strdup(src->serial_number);
371 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
372 dst->os_version = src->os_version;
373 dst->rf_bands = src->rf_bands;
377 void wps_device_data_free(struct wps_device_data *dev)
379 os_free(dev->device_name);
380 dev->device_name = NULL;
381 os_free(dev->manufacturer);
382 dev->manufacturer = NULL;
383 os_free(dev->model_name);
384 dev->model_name = NULL;
385 os_free(dev->model_number);
386 dev->model_number = NULL;
387 os_free(dev->serial_number);
388 dev->serial_number = NULL;