HS 2.0: Add Hotspot 2.0 ANQP routines
[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 "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 struct wpabuf * hs20_build_anqp_req(u32 stypes, const u8 *payload,
27                                     size_t payload_len)
28 {
29         struct wpabuf *buf;
30         u8 *len_pos;
31
32         buf = gas_anqp_build_initial_req(0, 100 + payload_len);
33         if (buf == NULL)
34                 return NULL;
35
36         len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
37         wpabuf_put_be24(buf, OUI_WFA);
38         wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
39         if (stypes == BIT(HS20_STYPE_NAI_HOME_REALM_QUERY)) {
40                 wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
41                 wpabuf_put_u8(buf, 0); /* Reserved */
42                 if (payload)
43                         wpabuf_put_data(buf, payload, payload_len);
44         } else {
45                 u8 i;
46                 wpabuf_put_u8(buf, HS20_STYPE_QUERY_LIST);
47                 wpabuf_put_u8(buf, 0); /* Reserved */
48                 for (i = 0; i < 32; i++) {
49                         if (stypes & BIT(i))
50                                 wpabuf_put_u8(buf, i);
51                 }
52         }
53         gas_anqp_set_element_len(buf, len_pos);
54
55         gas_anqp_set_len(buf);
56
57         return buf;
58 }
59
60
61 int hs20_anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, u32 stypes,
62                        const u8 *payload, size_t payload_len)
63 {
64         struct wpabuf *buf;
65         int ret = 0;
66         int freq;
67         struct wpa_bss *bss;
68         int res;
69
70         freq = wpa_s->assoc_freq;
71         bss = wpa_bss_get_bssid(wpa_s, dst);
72         if (bss)
73                 freq = bss->freq;
74         if (freq <= 0)
75                 return -1;
76
77         wpa_printf(MSG_DEBUG, "HS20: ANQP Query Request to " MACSTR " for "
78                    "subtypes 0x%x", MAC2STR(dst), stypes);
79
80         buf = hs20_build_anqp_req(stypes, payload, payload_len);
81         if (buf == NULL)
82                 return -1;
83
84         res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
85         if (res < 0) {
86                 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
87                 ret = -1;
88         } else
89                 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
90                            "%u", res);
91
92         wpabuf_free(buf);
93         return ret;
94 }
95
96
97 void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
98                                   const u8 *sa, const u8 *data, size_t slen)
99 {
100         const u8 *pos = data;
101         u8 subtype;
102         struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
103
104         if (slen < 2)
105                 return;
106
107         subtype = *pos++;
108         slen--;
109
110         pos++; /* Reserved */
111         slen--;
112
113         switch (subtype) {
114         case HS20_STYPE_CAPABILITY_LIST:
115                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
116                         " HS Capability List", MAC2STR(sa));
117                 wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
118                 break;
119         case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
120                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
121                         " Operator Friendly Name", MAC2STR(sa));
122                 wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
123                 if (bss) {
124                         wpabuf_free(bss->hs20_operator_friendly_name);
125                         bss->hs20_operator_friendly_name =
126                                 wpabuf_alloc_copy(pos, slen);
127                 }
128                 break;
129         case HS20_STYPE_WAN_METRICS:
130                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
131                         " WAN Metrics", MAC2STR(sa));
132                 wpa_hexdump_ascii(MSG_DEBUG, "WAN Metrics", pos, slen);
133                 if (bss) {
134                         wpabuf_free(bss->hs20_wan_metrics);
135                         bss->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
136                 }
137                 break;
138         case HS20_STYPE_CONNECTION_CAPABILITY:
139                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
140                         " Connection Capability", MAC2STR(sa));
141                 wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
142                 if (bss) {
143                         wpabuf_free(bss->hs20_connection_capability);
144                         bss->hs20_connection_capability =
145                                 wpabuf_alloc_copy(pos, slen);
146                 }
147                 break;
148         case HS20_STYPE_OPERATING_CLASS:
149                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
150                         " Operating Class", MAC2STR(sa));
151                 wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
152                 if (bss) {
153                         wpabuf_free(bss->hs20_operating_class);
154                         bss->hs20_operating_class =
155                                 wpabuf_alloc_copy(pos, slen);
156                 }
157                 break;
158         default:
159                 wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
160                 break;
161         }
162 }