HS 2.0R2: Add Icon Request and Icon binary File ANQP elements
[mech_eap.git] / wpa_supplicant / hs20_supplicant.c
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 "rsn_supp/wpa.h"
18 #include "wpa_supplicant_i.h"
19 #include "driver_i.h"
20 #include "config.h"
21 #include "bss.h"
22 #include "blacklist.h"
23 #include "gas_query.h"
24 #include "interworking.h"
25 #include "hs20_supplicant.h"
26
27
28 void wpas_hs20_add_indication(struct wpabuf *buf, int pps_mo_id)
29 {
30         u8 conf;
31
32         wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
33         wpabuf_put_u8(buf, pps_mo_id >= 0 ? 7 : 5);
34         wpabuf_put_be24(buf, OUI_WFA);
35         wpabuf_put_u8(buf, HS20_INDICATION_OUI_TYPE);
36         conf = HS20_VERSION;
37         if (pps_mo_id >= 0)
38                 conf |= HS20_PPS_MO_ID_PRESENT;
39         wpabuf_put_u8(buf, conf);
40         if (pps_mo_id >= 0)
41                 wpabuf_put_le16(buf, pps_mo_id);
42 }
43
44
45 int is_hs20_network(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
46                     struct wpa_bss *bss)
47 {
48         if (!wpa_s->conf->hs20 || !ssid)
49                 return 0;
50
51         if (ssid->parent_cred)
52                 return 1;
53
54         if (bss && !wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE))
55                 return 0;
56
57         /*
58          * This may catch some non-Hotspot 2.0 cases, but it is safer to do that
59          * than cause Hotspot 2.0 connections without indication element getting
60          * added. Non-Hotspot 2.0 APs should ignore the unknown vendor element.
61          */
62
63         if (!(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X))
64                 return 0;
65         if (!(ssid->pairwise_cipher & WPA_CIPHER_CCMP))
66                 return 0;
67         if (ssid->proto != WPA_PROTO_RSN)
68                 return 0;
69
70         return 1;
71 }
72
73
74 int hs20_get_pps_mo_id(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
75 {
76         struct wpa_cred *cred;
77
78         if (ssid == NULL || ssid->parent_cred == NULL)
79                 return 0;
80
81         for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
82                 if (ssid->parent_cred == cred)
83                         return cred->update_identifier;
84         }
85
86         return 0;
87 }
88
89
90 struct wpabuf * hs20_build_anqp_req(u32 stypes, const u8 *payload,
91                                     size_t payload_len)
92 {
93         struct wpabuf *buf;
94         u8 *len_pos;
95
96         buf = gas_anqp_build_initial_req(0, 100 + payload_len);
97         if (buf == NULL)
98                 return NULL;
99
100         len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
101         wpabuf_put_be24(buf, OUI_WFA);
102         wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
103         if (stypes == BIT(HS20_STYPE_NAI_HOME_REALM_QUERY)) {
104                 wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
105                 wpabuf_put_u8(buf, 0); /* Reserved */
106                 if (payload)
107                         wpabuf_put_data(buf, payload, payload_len);
108         } else if (stypes == BIT(HS20_STYPE_ICON_REQUEST)) {
109                 wpabuf_put_u8(buf, HS20_STYPE_ICON_REQUEST);
110                 wpabuf_put_u8(buf, 0); /* Reserved */
111                 if (payload)
112                         wpabuf_put_data(buf, payload, payload_len);
113         } else {
114                 u8 i;
115                 wpabuf_put_u8(buf, HS20_STYPE_QUERY_LIST);
116                 wpabuf_put_u8(buf, 0); /* Reserved */
117                 for (i = 0; i < 32; i++) {
118                         if (stypes & BIT(i))
119                                 wpabuf_put_u8(buf, i);
120                 }
121         }
122         gas_anqp_set_element_len(buf, len_pos);
123
124         gas_anqp_set_len(buf);
125
126         return buf;
127 }
128
129
130 int hs20_anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, u32 stypes,
131                        const u8 *payload, size_t payload_len)
132 {
133         struct wpabuf *buf;
134         int ret = 0;
135         int freq;
136         struct wpa_bss *bss;
137         int res;
138
139         freq = wpa_s->assoc_freq;
140         bss = wpa_bss_get_bssid(wpa_s, dst);
141         if (bss) {
142                 wpa_bss_anqp_unshare_alloc(bss);
143                 freq = bss->freq;
144         }
145         if (freq <= 0)
146                 return -1;
147
148         wpa_printf(MSG_DEBUG, "HS20: ANQP Query Request to " MACSTR " for "
149                    "subtypes 0x%x", MAC2STR(dst), stypes);
150
151         buf = hs20_build_anqp_req(stypes, payload, payload_len);
152         if (buf == NULL)
153                 return -1;
154
155         res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
156         if (res < 0) {
157                 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
158                 wpabuf_free(buf);
159                 ret = -1;
160         } else
161                 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
162                            "%u", res);
163
164         return ret;
165 }
166
167
168 void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
169                                   const u8 *sa, const u8 *data, size_t slen)
170 {
171         const u8 *pos = data;
172         u8 subtype;
173         struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
174         struct wpa_bss_anqp *anqp = NULL;
175         u16 data_len;
176
177         if (slen < 2)
178                 return;
179
180         if (bss)
181                 anqp = bss->anqp;
182
183         subtype = *pos++;
184         slen--;
185
186         pos++; /* Reserved */
187         slen--;
188
189         switch (subtype) {
190         case HS20_STYPE_CAPABILITY_LIST:
191                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
192                         " HS Capability List", MAC2STR(sa));
193                 wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
194                 break;
195         case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
196                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
197                         " Operator Friendly Name", MAC2STR(sa));
198                 wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
199                 if (anqp) {
200                         wpabuf_free(anqp->hs20_operator_friendly_name);
201                         anqp->hs20_operator_friendly_name =
202                                 wpabuf_alloc_copy(pos, slen);
203                 }
204                 break;
205         case HS20_STYPE_WAN_METRICS:
206                 wpa_hexdump(MSG_DEBUG, "WAN Metrics", pos, slen);
207                 if (slen < 13) {
208                         wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short WAN "
209                                 "Metrics value from " MACSTR, MAC2STR(sa));
210                         break;
211                 }
212                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
213                         " WAN Metrics %02x:%u:%u:%u:%u:%u", MAC2STR(sa),
214                         pos[0], WPA_GET_LE32(pos + 1), WPA_GET_LE32(pos + 5),
215                         pos[9], pos[10], WPA_GET_LE16(pos + 11));
216                 if (anqp) {
217                         wpabuf_free(anqp->hs20_wan_metrics);
218                         anqp->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
219                 }
220                 break;
221         case HS20_STYPE_CONNECTION_CAPABILITY:
222                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
223                         " Connection Capability", MAC2STR(sa));
224                 wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
225                 if (anqp) {
226                         wpabuf_free(anqp->hs20_connection_capability);
227                         anqp->hs20_connection_capability =
228                                 wpabuf_alloc_copy(pos, slen);
229                 }
230                 break;
231         case HS20_STYPE_OPERATING_CLASS:
232                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
233                         " Operating Class", MAC2STR(sa));
234                 wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
235                 if (anqp) {
236                         wpabuf_free(anqp->hs20_operating_class);
237                         anqp->hs20_operating_class =
238                                 wpabuf_alloc_copy(pos, slen);
239                 }
240                 break;
241         case HS20_STYPE_ICON_BINARY_FILE:
242                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
243                         " Icon Binary File", MAC2STR(sa));
244
245                 if (slen < 4) {
246                         wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon "
247                                 "Binary File value from " MACSTR, MAC2STR(sa));
248                         break;
249                 }
250
251                 wpa_printf(MSG_DEBUG, "HS 2.0: Download Status Code %u", *pos);
252                 pos++;
253                 slen--;
254
255                 if ((size_t) 1 + pos[0] > slen) {
256                         wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon "
257                                 "Binary File value from " MACSTR, MAC2STR(sa));
258                         break;
259                 }
260                 wpa_hexdump_ascii(MSG_DEBUG, "Icon Type", pos + 1, pos[0]);
261                 slen -= 1 + pos[0];
262                 pos += 1 + pos[0];
263
264                 if (slen < 2) {
265                         wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon "
266                                 "Binary File value from " MACSTR, MAC2STR(sa));
267                         break;
268                 }
269                 data_len = WPA_GET_BE16(pos);
270                 pos += 2;
271                 slen -= 2;
272
273                 if (data_len > slen) {
274                         wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon "
275                                 "Binary File value from " MACSTR, MAC2STR(sa));
276                         break;
277                 }
278
279                 wpa_printf(MSG_DEBUG, "Icon Binary Data: %u bytes", data_len);
280                 break;
281         default:
282                 wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
283                 break;
284         }
285 }
286
287
288 void hs20_rx_subscription_remediation(struct wpa_supplicant *wpa_s,
289                                       const char *url, u8 osu_method)
290 {
291         if (url)
292                 wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION "%u %s",
293                         osu_method, url);
294         else
295                 wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION);
296 }
297
298
299 void hs20_rx_deauth_imminent_notice(struct wpa_supplicant *wpa_s, u8 code,
300                                     u16 reauth_delay, const char *url)
301 {
302         if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
303                 wpa_printf(MSG_DEBUG, "HS 2.0: Ignore deauthentication imminent notice since PMF was not enabled");
304                 return;
305         }
306
307         wpa_msg(wpa_s, MSG_INFO, HS20_DEAUTH_IMMINENT_NOTICE "%u %u %s",
308                 code, reauth_delay, url);
309
310         if (code == HS20_DEAUTH_REASON_CODE_BSS) {
311                 wpa_printf(MSG_DEBUG, "HS 2.0: Add BSS to blacklist");
312                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
313         }
314
315         if (code == HS20_DEAUTH_REASON_CODE_ESS && wpa_s->current_ssid) {
316                 struct os_time now;
317                 os_get_time(&now);
318                 if (now.sec + reauth_delay <=
319                     wpa_s->current_ssid->disabled_until.sec)
320                         return;
321                 wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds",
322                            reauth_delay);
323                 wpa_s->current_ssid->disabled_until.sec =
324                         now.sec + reauth_delay;
325         }
326 }