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 static int wps_build_manufacturer(struct wps_device_data *dev,
26 wpa_printf(MSG_DEBUG, "WPS: * Manufacturer");
27 wpabuf_put_be16(msg, ATTR_MANUFACTURER);
28 len = dev->manufacturer ? os_strlen(dev->manufacturer) : 0;
31 * Some deployed WPS implementations fail to parse zero-length
32 * attributes. As a workaround, send a null character if the
33 * device attribute string is empty.
35 wpabuf_put_be16(msg, 1);
36 wpabuf_put_u8(msg, '\0');
38 wpabuf_put_be16(msg, len);
39 wpabuf_put_data(msg, dev->manufacturer, len);
45 static int wps_build_model_name(struct wps_device_data *dev,
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;
54 * Some deployed WPS implementations fail to parse zero-length
55 * attributes. As a workaround, send a null character if the
56 * device attribute string is empty.
58 wpabuf_put_be16(msg, 1);
59 wpabuf_put_u8(msg, '\0');
61 wpabuf_put_be16(msg, len);
62 wpabuf_put_data(msg, dev->model_name, len);
68 static int wps_build_model_number(struct wps_device_data *dev,
72 wpa_printf(MSG_DEBUG, "WPS: * Model Number");
73 wpabuf_put_be16(msg, ATTR_MODEL_NUMBER);
74 len = dev->model_number ? os_strlen(dev->model_number) : 0;
77 * Some deployed WPS implementations fail to parse zero-length
78 * attributes. As a workaround, send a null character if the
79 * device attribute string is empty.
81 wpabuf_put_be16(msg, 1);
82 wpabuf_put_u8(msg, '\0');
84 wpabuf_put_be16(msg, len);
85 wpabuf_put_data(msg, dev->model_number, len);
91 static int wps_build_serial_number(struct wps_device_data *dev,
95 wpa_printf(MSG_DEBUG, "WPS: * Serial Number");
96 wpabuf_put_be16(msg, ATTR_SERIAL_NUMBER);
97 len = dev->serial_number ? os_strlen(dev->serial_number) : 0;
100 * Some deployed WPS implementations fail to parse zero-length
101 * attributes. As a workaround, send a null character if the
102 * device attribute string is empty.
104 wpabuf_put_be16(msg, 1);
105 wpabuf_put_u8(msg, '\0');
107 wpabuf_put_be16(msg, len);
108 wpabuf_put_data(msg, dev->serial_number, len);
114 int wps_build_primary_dev_type(struct wps_device_data *dev, struct wpabuf *msg)
116 wpa_printf(MSG_DEBUG, "WPS: * Primary Device Type");
117 wpabuf_put_be16(msg, ATTR_PRIMARY_DEV_TYPE);
118 wpabuf_put_be16(msg, WPS_DEV_TYPE_LEN);
119 wpabuf_put_data(msg, dev->pri_dev_type, WPS_DEV_TYPE_LEN);
124 static int wps_build_dev_name(struct wps_device_data *dev, struct wpabuf *msg)
127 wpa_printf(MSG_DEBUG, "WPS: * Device Name");
128 wpabuf_put_be16(msg, ATTR_DEV_NAME);
129 len = dev->device_name ? os_strlen(dev->device_name) : 0;
132 * Some deployed WPS implementations fail to parse zero-length
133 * attributes. As a workaround, send a null character if the
134 * device attribute string is empty.
136 wpabuf_put_be16(msg, 1);
137 wpabuf_put_u8(msg, '\0');
139 wpabuf_put_be16(msg, len);
140 wpabuf_put_data(msg, dev->device_name, len);
146 int wps_build_device_attrs(struct wps_device_data *dev, struct wpabuf *msg)
148 if (wps_build_manufacturer(dev, msg) ||
149 wps_build_model_name(dev, msg) ||
150 wps_build_model_number(dev, msg) ||
151 wps_build_serial_number(dev, msg) ||
152 wps_build_primary_dev_type(dev, msg) ||
153 wps_build_dev_name(dev, msg))
159 int wps_build_os_version(struct wps_device_data *dev, struct wpabuf *msg)
161 wpa_printf(MSG_DEBUG, "WPS: * OS Version");
162 wpabuf_put_be16(msg, ATTR_OS_VERSION);
163 wpabuf_put_be16(msg, 4);
164 wpabuf_put_be32(msg, 0x80000000 | dev->os_version);
169 int wps_build_rf_bands(struct wps_device_data *dev, struct wpabuf *msg)
171 wpa_printf(MSG_DEBUG, "WPS: * RF Bands (%x)", dev->rf_bands);
172 wpabuf_put_be16(msg, ATTR_RF_BANDS);
173 wpabuf_put_be16(msg, 1);
174 wpabuf_put_u8(msg, dev->rf_bands);
179 static int wps_process_manufacturer(struct wps_device_data *dev, const u8 *str,
183 wpa_printf(MSG_DEBUG, "WPS: No Manufacturer received");
187 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer", str, str_len);
189 os_free(dev->manufacturer);
190 dev->manufacturer = os_malloc(str_len + 1);
191 if (dev->manufacturer == NULL)
193 os_memcpy(dev->manufacturer, str, str_len);
194 dev->manufacturer[str_len] = '\0';
200 static int wps_process_model_name(struct wps_device_data *dev, const u8 *str,
204 wpa_printf(MSG_DEBUG, "WPS: No Model Name received");
208 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name", str, str_len);
210 os_free(dev->model_name);
211 dev->model_name = os_malloc(str_len + 1);
212 if (dev->model_name == NULL)
214 os_memcpy(dev->model_name, str, str_len);
215 dev->model_name[str_len] = '\0';
221 static int wps_process_model_number(struct wps_device_data *dev, const u8 *str,
225 wpa_printf(MSG_DEBUG, "WPS: No Model Number received");
229 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number", str, str_len);
231 os_free(dev->model_number);
232 dev->model_number = os_malloc(str_len + 1);
233 if (dev->model_number == NULL)
235 os_memcpy(dev->model_number, str, str_len);
236 dev->model_number[str_len] = '\0';
242 static int wps_process_serial_number(struct wps_device_data *dev,
243 const u8 *str, size_t str_len)
246 wpa_printf(MSG_DEBUG, "WPS: No Serial Number received");
250 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number", str, str_len);
252 os_free(dev->serial_number);
253 dev->serial_number = os_malloc(str_len + 1);
254 if (dev->serial_number == NULL)
256 os_memcpy(dev->serial_number, str, str_len);
257 dev->serial_number[str_len] = '\0';
263 static int wps_process_dev_name(struct wps_device_data *dev, const u8 *str,
267 wpa_printf(MSG_DEBUG, "WPS: No Device Name received");
271 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name", str, str_len);
273 os_free(dev->device_name);
274 dev->device_name = os_malloc(str_len + 1);
275 if (dev->device_name == NULL)
277 os_memcpy(dev->device_name, str, str_len);
278 dev->device_name[str_len] = '\0';
284 static int wps_process_primary_dev_type(struct wps_device_data *dev,
287 #ifndef CONFIG_NO_STDOUT_DEBUG
288 char devtype[WPS_DEV_TYPE_BUFSIZE];
289 #endif /* CONFIG_NO_STDOUT_DEBUG */
291 if (dev_type == NULL) {
292 wpa_printf(MSG_DEBUG, "WPS: No Primary Device Type received");
296 os_memcpy(dev->pri_dev_type, dev_type, WPS_DEV_TYPE_LEN);
297 wpa_printf(MSG_DEBUG, "WPS: Primary Device Type: %s",
298 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
305 int wps_process_device_attrs(struct wps_device_data *dev,
306 struct wps_parse_attr *attr)
308 if (wps_process_manufacturer(dev, attr->manufacturer,
309 attr->manufacturer_len) ||
310 wps_process_model_name(dev, attr->model_name,
311 attr->model_name_len) ||
312 wps_process_model_number(dev, attr->model_number,
313 attr->model_number_len) ||
314 wps_process_serial_number(dev, attr->serial_number,
315 attr->serial_number_len) ||
316 wps_process_primary_dev_type(dev, attr->primary_dev_type) ||
317 wps_process_dev_name(dev, attr->dev_name, attr->dev_name_len))
323 int wps_process_os_version(struct wps_device_data *dev, const u8 *ver)
326 wpa_printf(MSG_DEBUG, "WPS: No OS Version received");
330 dev->os_version = WPA_GET_BE32(ver);
331 wpa_printf(MSG_DEBUG, "WPS: OS Version %08x", dev->os_version);
337 int wps_process_rf_bands(struct wps_device_data *dev, const u8 *bands)
340 wpa_printf(MSG_DEBUG, "WPS: No RF Bands received");
344 dev->rf_bands = *bands;
345 wpa_printf(MSG_DEBUG, "WPS: Enrollee RF Bands 0x%x", dev->rf_bands);
351 void wps_device_data_dup(struct wps_device_data *dst,
352 const struct wps_device_data *src)
354 if (src->device_name)
355 dst->device_name = os_strdup(src->device_name);
356 if (src->manufacturer)
357 dst->manufacturer = os_strdup(src->manufacturer);
359 dst->model_name = os_strdup(src->model_name);
360 if (src->model_number)
361 dst->model_number = os_strdup(src->model_number);
362 if (src->serial_number)
363 dst->serial_number = os_strdup(src->serial_number);
364 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
365 dst->os_version = src->os_version;
366 dst->rf_bands = src->rf_bands;
370 void wps_device_data_free(struct wps_device_data *dev)
372 os_free(dev->device_name);
373 dev->device_name = NULL;
374 os_free(dev->manufacturer);
375 dev->manufacturer = NULL;
376 os_free(dev->model_name);
377 dev->model_name = NULL;
378 os_free(dev->model_number);
379 dev->model_number = NULL;
380 os_free(dev->serial_number);
381 dev->serial_number = NULL;