xref: /freebsd/contrib/wpa/wpa_supplicant/hs20_supplicant.c (revision 4fd0d10e0fe684211328bc148edf89a792425b39)
1 /*
2  * Copyright (c) 2009, Atheros Communications, Inc.
3  * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "eloop.h"
13 #include "common/ieee802_11_common.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/gas.h"
16 #include "common/wpa_ctrl.h"
17 #include "wpa_supplicant_i.h"
18 #include "driver_i.h"
19 #include "config.h"
20 #include "bss.h"
21 #include "gas_query.h"
22 #include "interworking.h"
23 #include "hs20_supplicant.h"
24 
25 
26 void wpas_hs20_add_indication(struct wpabuf *buf)
27 {
28 	wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
29 	wpabuf_put_u8(buf, 5);
30 	wpabuf_put_be24(buf, OUI_WFA);
31 	wpabuf_put_u8(buf, HS20_INDICATION_OUI_TYPE);
32 	wpabuf_put_u8(buf, 0x00); /* Hotspot Configuration */
33 }
34 
35 
36 struct wpabuf * hs20_build_anqp_req(u32 stypes, const u8 *payload,
37 				    size_t payload_len)
38 {
39 	struct wpabuf *buf;
40 	u8 *len_pos;
41 
42 	buf = gas_anqp_build_initial_req(0, 100 + payload_len);
43 	if (buf == NULL)
44 		return NULL;
45 
46 	len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
47 	wpabuf_put_be24(buf, OUI_WFA);
48 	wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
49 	if (stypes == BIT(HS20_STYPE_NAI_HOME_REALM_QUERY)) {
50 		wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
51 		wpabuf_put_u8(buf, 0); /* Reserved */
52 		if (payload)
53 			wpabuf_put_data(buf, payload, payload_len);
54 	} else {
55 		u8 i;
56 		wpabuf_put_u8(buf, HS20_STYPE_QUERY_LIST);
57 		wpabuf_put_u8(buf, 0); /* Reserved */
58 		for (i = 0; i < 32; i++) {
59 			if (stypes & BIT(i))
60 				wpabuf_put_u8(buf, i);
61 		}
62 	}
63 	gas_anqp_set_element_len(buf, len_pos);
64 
65 	gas_anqp_set_len(buf);
66 
67 	return buf;
68 }
69 
70 
71 int hs20_anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, u32 stypes,
72 		       const u8 *payload, size_t payload_len)
73 {
74 	struct wpabuf *buf;
75 	int ret = 0;
76 	int freq;
77 	struct wpa_bss *bss;
78 	int res;
79 
80 	freq = wpa_s->assoc_freq;
81 	bss = wpa_bss_get_bssid(wpa_s, dst);
82 	if (bss) {
83 		wpa_bss_anqp_unshare_alloc(bss);
84 		freq = bss->freq;
85 	}
86 	if (freq <= 0)
87 		return -1;
88 
89 	wpa_printf(MSG_DEBUG, "HS20: ANQP Query Request to " MACSTR " for "
90 		   "subtypes 0x%x", MAC2STR(dst), stypes);
91 
92 	buf = hs20_build_anqp_req(stypes, payload, payload_len);
93 	if (buf == NULL)
94 		return -1;
95 
96 	res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
97 	if (res < 0) {
98 		wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
99 		ret = -1;
100 	} else
101 		wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
102 			   "%u", res);
103 
104 	wpabuf_free(buf);
105 	return ret;
106 }
107 
108 
109 void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
110 				  const u8 *sa, const u8 *data, size_t slen)
111 {
112 	const u8 *pos = data;
113 	u8 subtype;
114 	struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
115 	struct wpa_bss_anqp *anqp = NULL;
116 
117 	if (slen < 2)
118 		return;
119 
120 	if (bss)
121 		anqp = bss->anqp;
122 
123 	subtype = *pos++;
124 	slen--;
125 
126 	pos++; /* Reserved */
127 	slen--;
128 
129 	switch (subtype) {
130 	case HS20_STYPE_CAPABILITY_LIST:
131 		wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
132 			" HS Capability List", MAC2STR(sa));
133 		wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
134 		break;
135 	case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
136 		wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
137 			" Operator Friendly Name", MAC2STR(sa));
138 		wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
139 		if (anqp) {
140 			wpabuf_free(anqp->hs20_operator_friendly_name);
141 			anqp->hs20_operator_friendly_name =
142 				wpabuf_alloc_copy(pos, slen);
143 		}
144 		break;
145 	case HS20_STYPE_WAN_METRICS:
146 		wpa_hexdump(MSG_DEBUG, "WAN Metrics", pos, slen);
147 		if (slen < 13) {
148 			wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short WAN "
149 				"Metrics value from " MACSTR, MAC2STR(sa));
150 			break;
151 		}
152 		wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
153 			" WAN Metrics %02x:%u:%u:%u:%u:%u", MAC2STR(sa),
154 			pos[0], WPA_GET_LE32(pos + 1), WPA_GET_LE32(pos + 5),
155 			pos[9], pos[10], WPA_GET_LE16(pos + 11));
156 		if (anqp) {
157 			wpabuf_free(anqp->hs20_wan_metrics);
158 			anqp->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
159 		}
160 		break;
161 	case HS20_STYPE_CONNECTION_CAPABILITY:
162 		wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
163 			" Connection Capability", MAC2STR(sa));
164 		wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
165 		if (anqp) {
166 			wpabuf_free(anqp->hs20_connection_capability);
167 			anqp->hs20_connection_capability =
168 				wpabuf_alloc_copy(pos, slen);
169 		}
170 		break;
171 	case HS20_STYPE_OPERATING_CLASS:
172 		wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
173 			" Operating Class", MAC2STR(sa));
174 		wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
175 		if (anqp) {
176 			wpabuf_free(anqp->hs20_operating_class);
177 			anqp->hs20_operating_class =
178 				wpabuf_alloc_copy(pos, slen);
179 		}
180 		break;
181 	default:
182 		wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
183 		break;
184 	}
185 }
186