18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/*
28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Wi-Fi Protected Setup
38d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
5c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * This software may be distributed under the terms of the BSD license.
6c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * See README for more details.
78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
98d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "includes.h"
108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common.h"
128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "crypto/dh_group5.h"
138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common/ieee802_11_defs.h"
148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "wps_i.h"
158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "wps_dev_attr.h"
168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_TESTING
198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_version_number = 0x20;
208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_testing_dummy_cred = 0;
218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_TESTING */
228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_init - Initialize WPS Registration protocol data
268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @cfg: WPS configuration
278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: Pointer to allocated data or %NULL on failure
288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function is used to initialize WPS data for a registration protocol
308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * instance (i.e., each run of registration protocol as a Registrar of
318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Enrollee. The caller is responsible for freeing this data after the
328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * registration run has been completed by calling wps_deinit().
338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wps_data * wps_init(const struct wps_config *cfg)
358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_data *data = os_zalloc(sizeof(*data));
378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (data == NULL)
388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	data->wps = cfg->wps;
408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	data->registrar = cfg->registrar;
418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cfg->registrar) {
428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(data->uuid_r, cfg->wps->uuid, WPS_UUID_LEN);
438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(data->mac_addr_e, cfg->wps->dev.mac_addr, ETH_ALEN);
458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(data->uuid_e, cfg->wps->uuid, WPS_UUID_LEN);
468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cfg->pin) {
48d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		data->dev_pw_id = cfg->dev_pw_id;
498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data->dev_password = os_malloc(cfg->pin_len);
508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (data->dev_password == NULL) {
518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(data);
528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return NULL;
538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(data->dev_password, cfg->pin, cfg->pin_len);
558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data->dev_password_len = cfg->pin_len;
56f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_hexdump_key(MSG_DEBUG, "WPS: AP PIN dev_password",
57f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt				data->dev_password, data->dev_password_len);
588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#ifdef CONFIG_WPS_NFC
6104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (cfg->wps->ap && !cfg->registrar && cfg->wps->ap_nfc_dev_pw_id) {
62f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		/* Keep AP PIN as alternative Device Password */
63f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		data->alt_dev_pw_id = data->dev_pw_id;
64f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		data->alt_dev_password = data->dev_password;
65f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		data->alt_dev_password_len = data->dev_password_len;
66f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt
6704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		data->dev_pw_id = cfg->wps->ap_nfc_dev_pw_id;
6804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		data->dev_password =
6904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			os_malloc(wpabuf_len(cfg->wps->ap_nfc_dev_pw));
7004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		if (data->dev_password == NULL) {
7104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			os_free(data);
7204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			return NULL;
7304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		}
7404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		os_memcpy(data->dev_password,
7504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			  wpabuf_head(cfg->wps->ap_nfc_dev_pw),
7604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			  wpabuf_len(cfg->wps->ap_nfc_dev_pw));
7704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		data->dev_password_len = wpabuf_len(cfg->wps->ap_nfc_dev_pw);
78f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		wpa_hexdump_key(MSG_DEBUG, "WPS: NFC dev_password",
79f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			    data->dev_password, data->dev_password_len);
8004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	}
8104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#endif /* CONFIG_WPS_NFC */
8204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	data->pbc = cfg->pbc;
848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cfg->pbc) {
858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Use special PIN '00000000' for PBC */
868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data->dev_pw_id = DEV_PW_PUSHBUTTON;
878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(data->dev_password);
881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		data->dev_password = (u8 *) os_strdup("00000000");
898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (data->dev_password == NULL) {
908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(data);
918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return NULL;
928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data->dev_password_len = 8;
948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	data->state = data->registrar ? RECV_M1 : SEND_M1;
978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cfg->assoc_wps_ie) {
998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct wps_parse_attr attr;
1008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump_buf(MSG_DEBUG, "WPS: WPS IE from (Re)AssocReq",
1018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				cfg->assoc_wps_ie);
1028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_parse_msg(cfg->assoc_wps_ie, &attr) < 0) {
1038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: Failed to parse WPS IE "
1048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "from (Re)AssocReq");
1058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		} else if (attr.request_type == NULL) {
1068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: No Request Type attribute "
1078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "in (Re)AssocReq WPS IE");
1088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		} else {
1098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: Request Type (from WPS IE "
1108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "in (Re)AssocReq WPS IE): %d",
1118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   *attr.request_type);
1128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			data->request_type = *attr.request_type;
1138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
1148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
1158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cfg->new_ap_settings) {
1178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		data->new_ap_settings =
1188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_malloc(sizeof(*data->new_ap_settings));
1198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (data->new_ap_settings == NULL) {
120d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			os_free(data->dev_password);
1218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(data);
1228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return NULL;
1238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
1248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(data->new_ap_settings, cfg->new_ap_settings,
1258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  sizeof(*data->new_ap_settings));
1268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
1278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cfg->peer_addr)
1298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(data->peer_dev.mac_addr, cfg->peer_addr, ETH_ALEN);
1308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cfg->p2p_dev_addr)
1318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(data->p2p_dev_addr, cfg->p2p_dev_addr, ETH_ALEN);
1328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	data->use_psk_key = cfg->use_psk_key;
13487fd279308af3f806848c8f2ab65ef18c6ac4c30Jouni Malinen	data->pbc_in_m1 = cfg->pbc_in_m1;
1358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return data;
1378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
1418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_deinit - Deinitialize WPS Registration protocol data
1428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @data: WPS Registration protocol data from wps_init()
1438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
1448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wps_deinit(struct wps_data *data)
1458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
14604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#ifdef CONFIG_WPS_NFC
14704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (data->registrar && data->nfc_pw_token)
14804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		wps_registrar_remove_nfc_pw_token(data->wps->registrar,
14904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt						  data->nfc_pw_token);
15004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#endif /* CONFIG_WPS_NFC */
15104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
1528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (data->wps_pin_revealed) {
1538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
1548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "negotiation failed");
1558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (data->registrar)
1568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_registrar_invalidate_pin(data->wps->registrar,
1578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						     data->uuid_e);
1588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else if (data->registrar)
1598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_unlock_pin(data->wps->registrar, data->uuid_e);
1608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(data->dh_privkey);
1628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(data->dh_pubkey_e);
1638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(data->dh_pubkey_r);
1648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(data->last_msg);
1658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(data->dev_password);
166f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt	os_free(data->alt_dev_password);
1678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(data->new_psk);
1688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_device_data_free(&data->peer_dev);
1698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(data->new_ap_settings);
1708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dh5_free(data->dh_ctx);
17104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	os_free(data->nfc_pw_token);
1728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(data);
1738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
1778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_process_msg - Process a WPS message
1788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @wps: WPS Registration protocol data from wps_init()
1798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @op_code: Message OP Code
1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @msg: Message data
1818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: Processing result
1828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
1838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function is used to process WPS messages with OP Codes WSC_ACK,
1848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * WSC_NACK, WSC_MSG, and WSC_Done. The caller (e.g., EAP server/peer) is
1858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * responsible for reassembling the messages before calling this function.
1868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Response to this message is built by calling wps_get_msg().
1878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
1888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtenum wps_process_res wps_process_msg(struct wps_data *wps,
1898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     enum wsc_op_code op_code,
1908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     const struct wpabuf *msg)
1918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->registrar)
1938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return wps_registrar_process_msg(wps, op_code, msg);
1948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
1958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return wps_enrollee_process_msg(wps, op_code, msg);
1968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
2008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_get_msg - Build a WPS message
2018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @wps: WPS Registration protocol data from wps_init()
2028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @op_code: Buffer for returning message OP Code
2038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: The generated WPS message or %NULL on failure
2048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
2058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function is used to build a response to a message processed by calling
2068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_process_msg(). The caller is responsible for freeing the buffer.
2078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
2088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code)
2098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->registrar)
2118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return wps_registrar_get_msg(wps, op_code);
2128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
2138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return wps_enrollee_get_msg(wps, op_code);
2148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
2188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PBC
2198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @msg: WPS IE contents from Beacon or Probe Response frame
2208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 1 if PBC Registrar is active, 0 if not
2218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
2228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_is_selected_pbc_registrar(const struct wpabuf *msg)
2238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr attr;
2258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
2278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * In theory, this could also verify that attr.sel_reg_config_methods
2288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * includes WPS_CONFIG_PUSHBUTTON, but some deployed AP implementations
2298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * do not set Selected Registrar Config Methods attribute properly, so
2308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * it is safer to just use Device Password ID here.
2318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
2328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(msg, &attr) < 0 ||
2348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    !attr.selected_registrar || *attr.selected_registrar == 0 ||
2358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    !attr.dev_password_id ||
2368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
2378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
2388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_STRICT
2408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!attr.sel_reg_config_methods ||
2418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    !(WPA_GET_BE16(attr.sel_reg_config_methods) &
2428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	      WPS_CONFIG_PUSHBUTTON))
2438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
2448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_STRICT */
2458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 1;
2478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int is_selected_pin_registrar(struct wps_parse_attr *attr)
2518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
2538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * In theory, this could also verify that attr.sel_reg_config_methods
2548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
2558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * but some deployed AP implementations do not set Selected Registrar
2568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Config Methods attribute properly, so it is safer to just use
2578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Device Password ID here.
2588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
2598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!attr->selected_registrar || *attr->selected_registrar == 0)
2618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
2628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr->dev_password_id != NULL &&
2648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    WPA_GET_BE16(attr->dev_password_id) == DEV_PW_PUSHBUTTON)
2658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
2668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_STRICT
2688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!attr->sel_reg_config_methods ||
2698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    !(WPA_GET_BE16(attr->sel_reg_config_methods) &
2708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	      (WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD)))
2718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
2728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_STRICT */
2738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 1;
2758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
2798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
2808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @msg: WPS IE contents from Beacon or Probe Response frame
2818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 1 if PIN Registrar is active, 0 if not
2828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
2838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_is_selected_pin_registrar(const struct wpabuf *msg)
2848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr attr;
2868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(msg, &attr) < 0)
2888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
2898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return is_selected_pin_registrar(&attr);
2918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
2958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_is_addr_authorized - Check whether WPS IE authorizes MAC address
2968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @msg: WPS IE contents from Beacon or Probe Response frame
2978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @addr: MAC address to search for
2988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @ver1_compat: Whether to use version 1 compatibility mode
29961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * Returns: 2 if the specified address is explicit authorized, 1 if address is
30061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * authorized (broadcast), 0 if not
3018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
3028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
3038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   int ver1_compat)
3048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
3058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr attr;
3068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	unsigned int i;
3078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *pos;
3088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(msg, &attr) < 0)
3118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
3128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!attr.version2 && ver1_compat) {
3148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
3158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Version 1.0 AP - AuthorizedMACs not used, so revert back to
3168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * old mechanism of using SelectedRegistrar.
3178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
3188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return is_selected_pin_registrar(&attr);
3198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!attr.authorized_macs)
3228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
3238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = attr.authorized_macs;
3258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < attr.authorized_macs_len / ETH_ALEN; i++) {
32661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (os_memcmp(pos, addr, ETH_ALEN) == 0)
32761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			return 2;
32861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (os_memcmp(pos, bcast, ETH_ALEN) == 0)
3298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return 1;
3308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ETH_ALEN;
3318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
3348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
3388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_ap_priority_compar - Prioritize WPS IE from two APs
3398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @wps_a: WPS IE contents from Beacon or Probe Response frame
3408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @wps_b: WPS IE contents from Beacon or Probe Response frame
3418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 1 if wps_b is considered more likely selection for WPS
3428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * provisioning, -1 if wps_a is considered more like, or 0 if no preference
3438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
3448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_ap_priority_compar(const struct wpabuf *wps_a,
3458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   const struct wpabuf *wps_b)
3468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
3478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr attr_a, attr_b;
3488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int sel_a, sel_b;
3498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_a == NULL || wps_parse_msg(wps_a, &attr_a) < 0)
3518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 1;
3528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_b == NULL || wps_parse_msg(wps_b, &attr_b) < 0)
3538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
3548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sel_a = attr_a.selected_registrar && *attr_a.selected_registrar != 0;
3568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sel_b = attr_b.selected_registrar && *attr_b.selected_registrar != 0;
3578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sel_a && !sel_b)
3598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
3608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!sel_a && sel_b)
3618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 1;
3628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
3648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
3688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_get_uuid_e - Get UUID-E from WPS IE
3698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @msg: WPS IE contents from Beacon or Probe Response frame
3708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: Pointer to UUID-E or %NULL if not included
3718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
3728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * The returned pointer is to the msg contents and it remains valid only as
3738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * long as the msg buffer is valid.
3748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
3758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtconst u8 * wps_get_uuid_e(const struct wpabuf *msg)
3768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
3778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr attr;
3788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(msg, &attr) < 0)
3808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
3818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return attr.uuid_e;
3828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
3861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * wps_is_20 - Check whether WPS attributes claim support for WPS 2.0
3871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt */
3881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtint wps_is_20(const struct wpabuf *msg)
3891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
3901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wps_parse_attr attr;
3911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
3921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (msg == NULL || wps_parse_msg(msg, &attr) < 0)
3931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return 0;
3941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return attr.version2 != NULL;
3951f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
3961f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
3971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
3981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt/**
3998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
4008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @req_type: Value for Request Type attribute
4018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: WPS IE or %NULL on failure
4028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
4038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * The caller is responsible for freeing the buffer.
4048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
4058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
4068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *ie;
4088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *len;
4098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
4118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "Request");
4128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ie = wpabuf_alloc(100);
4138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ie == NULL)
4148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
4158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
4178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len = wpabuf_put(ie, 1);
4188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
4198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_version(ie) ||
4218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_req_type(ie, req_type) ||
4228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wfa_ext(ie, 0, NULL, 0)) {
4238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(ie);
4248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
4258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
4268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*len = wpabuf_len(ie) - 2;
4288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ie;
4308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
4318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
4348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_build_assoc_resp_ie - Build WPS IE for (Re)Association Response
4358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: WPS IE or %NULL on failure
4368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
4378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * The caller is responsible for freeing the buffer.
4388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
4398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpabuf * wps_build_assoc_resp_ie(void)
4408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *ie;
4428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *len;
4438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
4458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "Response");
4468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ie = wpabuf_alloc(100);
4478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ie == NULL)
4488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
4498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
4518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len = wpabuf_put(ie, 1);
4528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
4538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_version(ie) ||
4558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_resp_type(ie, WPS_RESP_AP) ||
4568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wfa_ext(ie, 0, NULL, 0)) {
4578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(ie);
4588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
4598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
4608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*len = wpabuf_len(ie) - 2;
4628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ie;
4648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
4658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
4688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_build_probe_req_ie - Build WPS IE for Probe Request
46904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt * @pw_id: Password ID (DEV_PW_PUSHBUTTON for active PBC and DEV_PW_DEFAULT for
47004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt * most other use cases)
4718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @dev: Device attributes
4728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @uuid: Own UUID
4738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @req_type: Value for Request Type attribute
4748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @num_req_dev_types: Number of requested device types
4758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @req_dev_types: Requested device types (8 * num_req_dev_types octets) or
4768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *	%NULL if none
4778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: WPS IE or %NULL on failure
4788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
4798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * The caller is responsible for freeing the buffer.
4808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
48104949598a23f501be6eec21697465fd46a28840aDmitry Shmidtstruct wpabuf * wps_build_probe_req_ie(u16 pw_id, struct wps_device_data *dev,
4828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       const u8 *uuid,
4838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       enum wps_request_type req_type,
4848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       unsigned int num_req_dev_types,
4858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       const u8 *req_dev_types)
4868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *ie;
4888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
4908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ie = wpabuf_alloc(500);
4928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ie == NULL)
4938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
4948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_version(ie) ||
4968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_req_type(ie, req_type) ||
4971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	    wps_build_config_methods(ie, dev->config_methods) ||
4988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_uuid_e(ie, uuid) ||
4998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_primary_dev_type(dev, ie) ||
500b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	    wps_build_rf_bands(dev, ie, 0) ||
5018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_assoc_state(NULL, ie) ||
5028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
50304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	    wps_build_dev_password_id(ie, pw_id) ||
5048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS2
5058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_manufacturer(dev, ie) ||
5068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_model_name(dev, ie) ||
5078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_model_number(dev, ie) ||
5088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_dev_name(dev, ie) ||
5098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wfa_ext(ie, req_type == WPS_REQ_ENROLLEE, NULL, 0) ||
5108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS2 */
5118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_req_dev_type(dev, ie, num_req_dev_types, req_dev_types)
5128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    ||
5138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_secondary_dev_type(dev, ie)
5148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		) {
5158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(ie);
5168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
5178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifndef CONFIG_WPS2
5208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (dev->p2p && wps_build_dev_name(dev, ie)) {
5218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(ie);
5228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
5238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS2 */
5258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wps_ie_encapsulate(ie);
5278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wps_free_pending_msgs(struct upnp_pending_message *msgs)
5318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct upnp_pending_message *p, *prev;
5338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	p = msgs;
5348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (p) {
5358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		prev = p;
5368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		p = p->next;
5378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(prev->msg);
5388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(prev);
5398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_attr_text(struct wpabuf *data, char *buf, char *end)
5448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr attr;
5468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	char *pos = buf;
5478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int ret;
5488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(data, &attr) < 0)
5508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
5518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.wps_state) {
5538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (*attr.wps_state == WPS_STATE_NOT_CONFIGURED)
5548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			ret = os_snprintf(pos, end - pos,
5558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  "wps_state=unconfigured\n");
5568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		else if (*attr.wps_state == WPS_STATE_CONFIGURED)
5578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			ret = os_snprintf(pos, end - pos,
5588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  "wps_state=configured\n");
5598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		else
5608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			ret = 0;
5618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret < 0 || ret >= end - pos)
5628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos - buf;
5638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ret;
5648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.ap_setup_locked && *attr.ap_setup_locked) {
5678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = os_snprintf(pos, end - pos,
5688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  "wps_ap_setup_locked=1\n");
5698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret < 0 || ret >= end - pos)
5708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos - buf;
5718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ret;
5728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.selected_registrar && *attr.selected_registrar) {
5758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = os_snprintf(pos, end - pos,
5768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  "wps_selected_registrar=1\n");
5778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret < 0 || ret >= end - pos)
5788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos - buf;
5798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ret;
5808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.dev_password_id) {
5838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = os_snprintf(pos, end - pos,
5848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  "wps_device_password_id=%u\n",
5858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  WPA_GET_BE16(attr.dev_password_id));
5868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret < 0 || ret >= end - pos)
5878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos - buf;
5888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ret;
5898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.sel_reg_config_methods) {
5928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = os_snprintf(pos, end - pos,
5938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  "wps_selected_registrar_config_methods="
5948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  "0x%04x\n",
5958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  WPA_GET_BE16(attr.sel_reg_config_methods));
5968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret < 0 || ret >= end - pos)
5978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos - buf;
5988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ret;
5998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.primary_dev_type) {
6028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		char devtype[WPS_DEV_TYPE_BUFSIZE];
6038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = os_snprintf(pos, end - pos,
6048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  "wps_primary_device_type=%s\n",
6058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  wps_dev_type_bin2str(attr.primary_dev_type,
6068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						       devtype,
6078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						       sizeof(devtype)));
6088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret < 0 || ret >= end - pos)
6098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos - buf;
6108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ret;
6118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.dev_name) {
6148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		char *str = os_malloc(attr.dev_name_len + 1);
6158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		size_t i;
6168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (str == NULL)
6178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos - buf;
6188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		for (i = 0; i < attr.dev_name_len; i++) {
6198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (attr.dev_name[i] < 32)
6208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				str[i] = '_';
6218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			else
6228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				str[i] = attr.dev_name[i];
6238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
6248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		str[i] = '\0';
6258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = os_snprintf(pos, end - pos, "wps_device_name=%s\n", str);
6268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(str);
6278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret < 0 || ret >= end - pos)
6288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos - buf;
6298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ret;
6308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.config_methods) {
6338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = os_snprintf(pos, end - pos,
6348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  "wps_config_methods=0x%04x\n",
6358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  WPA_GET_BE16(attr.config_methods));
6368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret < 0 || ret >= end - pos)
6378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos - buf;
6388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ret;
6398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return pos - buf;
6428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
643b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
644b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt
645b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidtconst char * wps_ei_str(enum wps_error_indication ei)
646b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt{
647b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	switch (ei) {
648b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	case WPS_EI_NO_ERROR:
649b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return "No Error";
650b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	case WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED:
651b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return "TKIP Only Prohibited";
652b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	case WPS_EI_SECURITY_WEP_PROHIBITED:
653b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return "WEP Prohibited";
654b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	case WPS_EI_AUTH_FAILURE:
655b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return "Authentication Failure";
656b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	default:
657b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		return "Unknown";
658b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	}
659b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt}
660