2 * Wi-Fi Protected Setup
3 * Copyright (c) 2007-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"
20 #include "ieee802_11_defs.h"
24 * wps_init - Initialize WPS Registration protocol data
25 * @cfg: WPS configuration
26 * Returns: Pointer to allocated data or %NULL on failure
28 * This function is used to initialize WPS data for a registration protocol
29 * instance (i.e., each run of registration protocol as a Registrar of
30 * Enrollee. The caller is responsible for freeing this data after the
31 * registration run has been completed by calling wps_deinit().
33 struct wps_data * wps_init(const struct wps_config *cfg)
35 struct wps_data *data = os_zalloc(sizeof(*data));
39 data->registrar = cfg->registrar;
41 os_memcpy(data->uuid_r, cfg->wps->uuid, WPS_UUID_LEN);
43 os_memcpy(data->mac_addr_e, cfg->wps->dev.mac_addr, ETH_ALEN);
44 os_memcpy(data->uuid_e, cfg->wps->uuid, WPS_UUID_LEN);
47 data->dev_pw_id = data->wps->oob_dev_pw_id == 0 ?
48 DEV_PW_DEFAULT : data->wps->oob_dev_pw_id;
49 data->dev_password = os_malloc(cfg->pin_len);
50 if (data->dev_password == NULL) {
54 os_memcpy(data->dev_password, cfg->pin, cfg->pin_len);
55 data->dev_password_len = cfg->pin_len;
60 /* Use special PIN '00000000' for PBC */
61 data->dev_pw_id = DEV_PW_PUSHBUTTON;
62 os_free(data->dev_password);
63 data->dev_password = os_malloc(8);
64 if (data->dev_password == NULL) {
68 os_memset(data->dev_password, '0', 8);
69 data->dev_password_len = 8;
72 data->state = data->registrar ? RECV_M1 : SEND_M1;
74 if (cfg->assoc_wps_ie) {
75 struct wps_parse_attr attr;
76 wpa_hexdump_buf(MSG_DEBUG, "WPS: WPS IE from (Re)AssocReq",
78 if (wps_parse_msg(cfg->assoc_wps_ie, &attr) < 0) {
79 wpa_printf(MSG_DEBUG, "WPS: Failed to parse WPS IE "
81 } else if (attr.request_type == NULL) {
82 wpa_printf(MSG_DEBUG, "WPS: No Request Type attribute "
83 "in (Re)AssocReq WPS IE");
85 wpa_printf(MSG_DEBUG, "WPS: Request Type (from WPS IE "
86 "in (Re)AssocReq WPS IE): %d",
88 data->request_type = *attr.request_type;
97 * wps_deinit - Deinitialize WPS Registration protocol data
98 * @data: WPS Registration protocol data from wps_init()
100 void wps_deinit(struct wps_data *data)
102 if (data->wps_pin_revealed) {
103 wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
104 "negotiation failed");
106 wps_registrar_invalidate_pin(data->wps->registrar,
108 } else if (data->registrar)
109 wps_registrar_unlock_pin(data->wps->registrar, data->uuid_e);
111 wpabuf_free(data->dh_privkey);
112 wpabuf_free(data->dh_pubkey_e);
113 wpabuf_free(data->dh_pubkey_r);
114 wpabuf_free(data->last_msg);
115 os_free(data->dev_password);
116 os_free(data->new_psk);
117 wps_device_data_free(&data->peer_dev);
123 * wps_process_msg - Process a WPS message
124 * @wps: WPS Registration protocol data from wps_init()
125 * @op_code: Message OP Code
127 * Returns: Processing result
129 * This function is used to process WPS messages with OP Codes WSC_ACK,
130 * WSC_NACK, WSC_MSG, and WSC_Done. The caller (e.g., EAP server/peer) is
131 * responsible for reassembling the messages before calling this function.
132 * Response to this message is built by calling wps_get_msg().
134 enum wps_process_res wps_process_msg(struct wps_data *wps,
135 enum wsc_op_code op_code,
136 const struct wpabuf *msg)
139 return wps_registrar_process_msg(wps, op_code, msg);
141 return wps_enrollee_process_msg(wps, op_code, msg);
146 * wps_get_msg - Build a WPS message
147 * @wps: WPS Registration protocol data from wps_init()
148 * @op_code: Buffer for returning message OP Code
149 * Returns: The generated WPS message or %NULL on failure
151 * This function is used to build a response to a message processed by calling
152 * wps_process_msg(). The caller is responsible for freeing the buffer.
154 struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code)
157 return wps_registrar_get_msg(wps, op_code);
159 return wps_enrollee_get_msg(wps, op_code);
164 * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PBC
165 * @msg: WPS IE contents from Beacon or Probe Response frame
166 * Returns: 1 if PBC Registrar is active, 0 if not
168 int wps_is_selected_pbc_registrar(const struct wpabuf *msg)
170 struct wps_parse_attr attr;
173 * In theory, this could also verify that attr.sel_reg_config_methods
174 * includes WPS_CONFIG_PUSHBUTTON, but some deployed AP implementations
175 * do not set Selected Registrar Config Methods attribute properly, so
176 * it is safer to just use Device Password ID here.
179 if (wps_parse_msg(msg, &attr) < 0 ||
180 !attr.selected_registrar || *attr.selected_registrar == 0 ||
181 !attr.dev_password_id ||
182 WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
190 * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
191 * @msg: WPS IE contents from Beacon or Probe Response frame
192 * Returns: 1 if PIN Registrar is active, 0 if not
194 int wps_is_selected_pin_registrar(const struct wpabuf *msg)
196 struct wps_parse_attr attr;
199 * In theory, this could also verify that attr.sel_reg_config_methods
200 * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
201 * but some deployed AP implementations do not set Selected Registrar
202 * Config Methods attribute properly, so it is safer to just use
203 * Device Password ID here.
206 if (wps_parse_msg(msg, &attr) < 0)
209 if (!attr.selected_registrar || *attr.selected_registrar == 0)
212 if (attr.dev_password_id != NULL &&
213 WPA_GET_BE16(attr.dev_password_id) == DEV_PW_PUSHBUTTON)
221 * wps_get_uuid_e - Get UUID-E from WPS IE
222 * @msg: WPS IE contents from Beacon or Probe Response frame
223 * Returns: Pointer to UUID-E or %NULL if not included
225 * The returned pointer is to the msg contents and it remains valid only as
226 * long as the msg buffer is valid.
228 const u8 * wps_get_uuid_e(const struct wpabuf *msg)
230 struct wps_parse_attr attr;
232 if (wps_parse_msg(msg, &attr) < 0)
239 * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
240 * @req_type: Value for Request Type attribute
241 * Returns: WPS IE or %NULL on failure
243 * The caller is responsible for freeing the buffer.
245 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
250 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
252 ie = wpabuf_alloc(100);
256 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
257 len = wpabuf_put(ie, 1);
258 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
260 if (wps_build_version(ie) ||
261 wps_build_req_type(ie, req_type)) {
266 *len = wpabuf_len(ie) - 2;
273 * wps_build_probe_req_ie - Build WPS IE for Probe Request
274 * @pbc: Whether searching for PBC mode APs
275 * @dev: Device attributes
277 * @req_type: Value for Request Type attribute
278 * Returns: WPS IE or %NULL on failure
280 * The caller is responsible for freeing the buffer.
282 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
284 enum wps_request_type req_type)
290 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
292 ie = wpabuf_alloc(200);
296 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
297 len = wpabuf_put(ie, 1);
298 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
301 methods = WPS_CONFIG_PUSHBUTTON;
303 methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY |
304 WPS_CONFIG_KEYPAD | WPS_CONFIG_USBA;
306 if (wps_build_version(ie) ||
307 wps_build_req_type(ie, req_type) ||
308 wps_build_config_methods(ie, methods) ||
309 wps_build_uuid_e(ie, uuid) ||
310 wps_build_primary_dev_type(dev, ie) ||
311 wps_build_rf_bands(dev, ie) ||
312 wps_build_assoc_state(NULL, ie) ||
313 wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
314 wps_build_dev_password_id(ie, pbc ? DEV_PW_PUSHBUTTON :
320 *len = wpabuf_len(ie) - 2;
326 void wps_free_pending_msgs(struct upnp_pending_message *msgs)
328 struct upnp_pending_message *p, *prev;
333 wpabuf_free(prev->msg);