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 struct wps_dev_type *d;
117 wpa_printf(MSG_DEBUG, "WPS: * Primary Device Type");
118 wpabuf_put_be16(msg, ATTR_PRIMARY_DEV_TYPE);
119 wpabuf_put_be16(msg, sizeof(*d));
120 d = wpabuf_put(msg, sizeof(*d));
121 WPA_PUT_BE16(d->categ_id, dev->categ);
122 WPA_PUT_BE32(d->oui, dev->oui);
123 WPA_PUT_BE16(d->sub_categ_id, dev->sub_categ);
128 static int wps_build_dev_name(struct wps_device_data *dev, struct wpabuf *msg)
131 wpa_printf(MSG_DEBUG, "WPS: * Device Name");
132 wpabuf_put_be16(msg, ATTR_DEV_NAME);
133 len = dev->device_name ? os_strlen(dev->device_name) : 0;
136 * Some deployed WPS implementations fail to parse zero-length
137 * attributes. As a workaround, send a null character if the
138 * device attribute string is empty.
140 wpabuf_put_be16(msg, 1);
141 wpabuf_put_u8(msg, '\0');
143 wpabuf_put_be16(msg, len);
144 wpabuf_put_data(msg, dev->device_name, len);
150 int wps_build_device_attrs(struct wps_device_data *dev, struct wpabuf *msg)
152 if (wps_build_manufacturer(dev, msg) ||
153 wps_build_model_name(dev, msg) ||
154 wps_build_model_number(dev, msg) ||
155 wps_build_serial_number(dev, msg) ||
156 wps_build_primary_dev_type(dev, msg) ||
157 wps_build_dev_name(dev, msg))
163 int wps_build_os_version(struct wps_device_data *dev, struct wpabuf *msg)
165 wpa_printf(MSG_DEBUG, "WPS: * OS Version");
166 wpabuf_put_be16(msg, ATTR_OS_VERSION);
167 wpabuf_put_be16(msg, 4);
168 wpabuf_put_be32(msg, 0x80000000 | dev->os_version);
173 int wps_build_rf_bands(struct wps_device_data *dev, struct wpabuf *msg)
175 wpa_printf(MSG_DEBUG, "WPS: * RF Bands (%x)", dev->rf_bands);
176 wpabuf_put_be16(msg, ATTR_RF_BANDS);
177 wpabuf_put_be16(msg, 1);
178 wpabuf_put_u8(msg, dev->rf_bands);
183 static int wps_process_manufacturer(struct wps_device_data *dev, const u8 *str,
187 wpa_printf(MSG_DEBUG, "WPS: No Manufacturer received");
191 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer", str, str_len);
193 os_free(dev->manufacturer);
194 dev->manufacturer = os_malloc(str_len + 1);
195 if (dev->manufacturer == NULL)
197 os_memcpy(dev->manufacturer, str, str_len);
198 dev->manufacturer[str_len] = '\0';
204 static int wps_process_model_name(struct wps_device_data *dev, const u8 *str,
208 wpa_printf(MSG_DEBUG, "WPS: No Model Name received");
212 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name", str, str_len);
214 os_free(dev->model_name);
215 dev->model_name = os_malloc(str_len + 1);
216 if (dev->model_name == NULL)
218 os_memcpy(dev->model_name, str, str_len);
219 dev->model_name[str_len] = '\0';
225 static int wps_process_model_number(struct wps_device_data *dev, const u8 *str,
229 wpa_printf(MSG_DEBUG, "WPS: No Model Number received");
233 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number", str, str_len);
235 os_free(dev->model_number);
236 dev->model_number = os_malloc(str_len + 1);
237 if (dev->model_number == NULL)
239 os_memcpy(dev->model_number, str, str_len);
240 dev->model_number[str_len] = '\0';
246 static int wps_process_serial_number(struct wps_device_data *dev,
247 const u8 *str, size_t str_len)
250 wpa_printf(MSG_DEBUG, "WPS: No Serial Number received");
254 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number", str, str_len);
256 os_free(dev->serial_number);
257 dev->serial_number = os_malloc(str_len + 1);
258 if (dev->serial_number == NULL)
260 os_memcpy(dev->serial_number, str, str_len);
261 dev->serial_number[str_len] = '\0';
267 static int wps_process_dev_name(struct wps_device_data *dev, const u8 *str,
271 wpa_printf(MSG_DEBUG, "WPS: No Device Name received");
275 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name", str, str_len);
277 os_free(dev->device_name);
278 dev->device_name = os_malloc(str_len + 1);
279 if (dev->device_name == NULL)
281 os_memcpy(dev->device_name, str, str_len);
282 dev->device_name[str_len] = '\0';
288 static int wps_process_primary_dev_type(struct wps_device_data *dev,
291 struct wps_dev_type *d;
293 if (dev_type == NULL) {
294 wpa_printf(MSG_DEBUG, "WPS: No Primary Device Type received");
298 d = (struct wps_dev_type *) dev_type;
299 dev->categ = WPA_GET_BE16(d->categ_id);
300 dev->oui = WPA_GET_BE32(d->oui);
301 dev->sub_categ = WPA_GET_BE16(d->sub_categ_id);
303 wpa_printf(MSG_DEBUG, "WPS: Primary Device Type: category %d "
304 "OUI %08x sub-category %d",
305 dev->categ, dev->oui, dev->sub_categ);
311 int wps_process_device_attrs(struct wps_device_data *dev,
312 struct wps_parse_attr *attr)
314 if (wps_process_manufacturer(dev, attr->manufacturer,
315 attr->manufacturer_len) ||
316 wps_process_model_name(dev, attr->model_name,
317 attr->model_name_len) ||
318 wps_process_model_number(dev, attr->model_number,
319 attr->model_number_len) ||
320 wps_process_serial_number(dev, attr->serial_number,
321 attr->serial_number_len) ||
322 wps_process_primary_dev_type(dev, attr->primary_dev_type) ||
323 wps_process_dev_name(dev, attr->dev_name, attr->dev_name_len))
329 int wps_process_os_version(struct wps_device_data *dev, const u8 *ver)
332 wpa_printf(MSG_DEBUG, "WPS: No OS Version received");
336 dev->os_version = WPA_GET_BE32(ver);
337 wpa_printf(MSG_DEBUG, "WPS: OS Version %08x", dev->os_version);
343 int wps_process_rf_bands(struct wps_device_data *dev, const u8 *bands)
346 wpa_printf(MSG_DEBUG, "WPS: No RF Bands received");
350 dev->rf_bands = *bands;
351 wpa_printf(MSG_DEBUG, "WPS: Enrollee RF Bands 0x%x", dev->rf_bands);
357 void wps_device_data_dup(struct wps_device_data *dst,
358 const struct wps_device_data *src)
360 if (src->device_name)
361 dst->device_name = os_strdup(src->device_name);
362 if (src->manufacturer)
363 dst->manufacturer = os_strdup(src->manufacturer);
365 dst->model_name = os_strdup(src->model_name);
366 if (src->model_number)
367 dst->model_number = os_strdup(src->model_number);
368 if (src->serial_number)
369 dst->serial_number = os_strdup(src->serial_number);
370 dst->categ = src->categ;
372 dst->sub_categ = src->sub_categ;
373 dst->os_version = src->os_version;
374 dst->rf_bands = src->rf_bands;
378 void wps_device_data_free(struct wps_device_data *dev)
380 os_free(dev->device_name);
381 dev->device_name = NULL;
382 os_free(dev->manufacturer);
383 dev->manufacturer = NULL;
384 os_free(dev->model_name);
385 dev->model_name = NULL;
386 os_free(dev->model_number);
387 dev->model_number = NULL;
388 os_free(dev->serial_number);
389 dev->serial_number = NULL;