1 /*
2  * IEEE 802.11 Common routines
3  * Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef IEEE802_11_COMMON_H
10 #define IEEE802_11_COMMON_H
11 
12 #include "defs.h"
13 #include "ieee802_11_defs.h"
14 
15 struct element {
16 	u8 id;
17 	u8 datalen;
18 	u8 data[];
19 } STRUCT_PACKED;
20 
21 struct hostapd_hw_modes;
22 
23 #define MAX_NOF_MB_IES_SUPPORTED 5
24 
25 struct mb_ies_info {
26 	struct {
27 		const u8 *ie;
28 		u8 ie_len;
29 	} ies[MAX_NOF_MB_IES_SUPPORTED];
30 	u8 nof_ies;
31 };
32 
33 /* Parsed Information Elements */
34 struct ieee802_11_elems {
35 	const u8 *ssid;
36 	const u8 *supp_rates;
37 	const u8 *ds_params;
38 	const u8 *challenge;
39 	const u8 *erp_info;
40 	const u8 *ext_supp_rates;
41 	const u8 *wpa_ie;
42 	const u8 *rsn_ie;
43 	const u8 *wmm; /* WMM Information or Parameter Element */
44 	const u8 *wmm_tspec;
45 	const u8 *wps_ie;
46 	const u8 *supp_channels;
47 	const u8 *mdie;
48 	const u8 *ftie;
49 	const u8 *timeout_int;
50 	const u8 *ht_capabilities;
51 	const u8 *ht_operation;
52 	const u8 *mesh_config;
53 	const u8 *mesh_id;
54 	const u8 *peer_mgmt;
55 	const u8 *vht_capabilities;
56 	const u8 *vht_operation;
57 	const u8 *vht_opmode_notif;
58 	const u8 *vendor_ht_cap;
59 	const u8 *vendor_vht;
60 	const u8 *p2p;
61 	const u8 *wfd;
62 	const u8 *link_id;
63 	const u8 *interworking;
64 	const u8 *qos_map_set;
65 	const u8 *hs20;
66 	const u8 *ext_capab;
67 	const u8 *bss_max_idle_period;
68 	const u8 *ssid_list;
69 	const u8 *osen;
70 	const u8 *mbo;
71 	const u8 *ampe;
72 	const u8 *mic;
73 	const u8 *pref_freq_list;
74 	const u8 *supp_op_classes;
75 	const u8 *rrm_enabled;
76 	const u8 *cag_number;
77 	const u8 *ap_csn;
78 	const u8 *fils_indic;
79 	const u8 *dils;
80 	const u8 *assoc_delay_info;
81 	const u8 *fils_req_params;
82 	const u8 *fils_key_confirm;
83 	const u8 *fils_session;
84 	const u8 *fils_hlp;
85 	const u8 *fils_ip_addr_assign;
86 	const u8 *key_delivery;
87 	const u8 *fils_wrapped_data;
88 	const u8 *fils_pk;
89 	const u8 *fils_nonce;
90 	const u8 *owe_dh;
91 	const u8 *power_capab;
92 	const u8 *roaming_cons_sel;
93 	const u8 *password_id;
94 	const u8 *oci;
95 	const u8 *multi_ap;
96 	const u8 *he_capabilities;
97 	const u8 *he_operation;
98 
99 	u8 ssid_len;
100 	u8 supp_rates_len;
101 	u8 challenge_len;
102 	u8 ext_supp_rates_len;
103 	u8 wpa_ie_len;
104 	u8 rsn_ie_len;
105 	u8 wmm_len; /* 7 = WMM Information; 24 = WMM Parameter */
106 	u8 wmm_tspec_len;
107 	u8 wps_ie_len;
108 	u8 supp_channels_len;
109 	u8 mdie_len;
110 	u8 ftie_len;
111 	u8 mesh_config_len;
112 	u8 mesh_id_len;
113 	u8 peer_mgmt_len;
114 	u8 vendor_ht_cap_len;
115 	u8 vendor_vht_len;
116 	u8 p2p_len;
117 	u8 wfd_len;
118 	u8 interworking_len;
119 	u8 qos_map_set_len;
120 	u8 hs20_len;
121 	u8 ext_capab_len;
122 	u8 ssid_list_len;
123 	u8 osen_len;
124 	u8 mbo_len;
125 	u8 ampe_len;
126 	u8 mic_len;
127 	u8 pref_freq_list_len;
128 	u8 supp_op_classes_len;
129 	u8 rrm_enabled_len;
130 	u8 cag_number_len;
131 	u8 fils_indic_len;
132 	u8 dils_len;
133 	u8 fils_req_params_len;
134 	u8 fils_key_confirm_len;
135 	u8 fils_hlp_len;
136 	u8 fils_ip_addr_assign_len;
137 	u8 key_delivery_len;
138 	u8 fils_wrapped_data_len;
139 	u8 fils_pk_len;
140 	u8 owe_dh_len;
141 	u8 power_capab_len;
142 	u8 roaming_cons_sel_len;
143 	u8 password_id_len;
144 	u8 oci_len;
145 	u8 multi_ap_len;
146 	u8 he_capabilities_len;
147 	u8 he_operation_len;
148 
149 	struct mb_ies_info mb_ies;
150 };
151 
152 typedef enum { ParseOK = 0, ParseUnknown = 1, ParseFailed = -1 } ParseRes;
153 
154 ParseRes ieee802_11_parse_elems(const u8 *start, size_t len,
155 				struct ieee802_11_elems *elems,
156 				int show_errors);
157 int ieee802_11_ie_count(const u8 *ies, size_t ies_len);
158 struct wpabuf * ieee802_11_vendor_ie_concat(const u8 *ies, size_t ies_len,
159 					    u32 oui_type);
160 struct ieee80211_hdr;
161 const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len);
162 
163 struct hostapd_wmm_ac_params {
164 	int cwmin;
165 	int cwmax;
166 	int aifs;
167 	int txop_limit; /* in units of 32us */
168 	int admission_control_mandatory;
169 };
170 
171 int hostapd_config_wmm_ac(struct hostapd_wmm_ac_params wmm_ac_params[],
172 			  const char *name, const char *val);
173 enum hostapd_hw_mode ieee80211_freq_to_chan(int freq, u8 *channel);
174 int ieee80211_chan_to_freq(const char *country, u8 op_class, u8 chan);
175 enum hostapd_hw_mode ieee80211_freq_to_channel_ext(unsigned int freq,
176 						   int sec_channel, int vht,
177 						   u8 *op_class, u8 *channel);
178 int ieee80211_chaninfo_to_channel(unsigned int freq, enum chan_width chanwidth,
179 				  int sec_channel, u8 *op_class, u8 *channel);
180 int ieee80211_is_dfs(int freq, const struct hostapd_hw_modes *modes,
181 		     u16 num_modes);
182 enum phy_type ieee80211_get_phy_type(int freq, int ht, int vht);
183 
184 int supp_rates_11b_only(struct ieee802_11_elems *elems);
185 int mb_ies_info_by_ies(struct mb_ies_info *info, const u8 *ies_buf,
186 		       size_t ies_len);
187 struct wpabuf * mb_ies_by_info(struct mb_ies_info *info);
188 
189 const char * fc2str(u16 fc);
190 const char * reason2str(u16 reason);
191 const char * status2str(u16 status);
192 
193 struct oper_class_map {
194 	enum hostapd_hw_mode mode;
195 	u8 op_class;
196 	u8 min_chan;
197 	u8 max_chan;
198 	u8 inc;
199 	enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160, BW160, BW80P80 } bw;
200 	enum { P2P_SUPP, NO_P2P_SUPP } p2p;
201 };
202 
203 extern const struct oper_class_map global_op_class[];
204 extern size_t global_op_class_size;
205 
206 const u8 * get_ie(const u8 *ies, size_t len, u8 eid);
207 const u8 * get_ie_ext(const u8 *ies, size_t len, u8 ext);
208 const u8 * get_vendor_ie(const u8 *ies, size_t len, u32 vendor_type);
209 
210 size_t mbo_add_ie(u8 *buf, size_t len, const u8 *attr, size_t attr_len);
211 
212 size_t add_multi_ap_ie(u8 *buf, size_t len, u8 value);
213 
214 struct country_op_class {
215 	u8 country_op_class;
216 	u8 global_op_class;
217 };
218 
219 u8 country_to_global_op_class(const char *country, u8 op_class);
220 
221 const struct oper_class_map * get_oper_class(const char *country, u8 op_class);
222 int oper_class_bw_to_int(const struct oper_class_map *map);
223 
224 int ieee802_11_parse_candidate_list(const char *pos, u8 *nei_rep,
225 				    size_t nei_rep_len);
226 
227 int ieee802_11_ext_capab(const u8 *ie, unsigned int capab);
228 
229 /* element iteration helpers */
230 #define for_each_element(_elem, _data, _datalen)			\
231 	for (_elem = (const struct element *) (_data);			\
232 	     (const u8 *) (_data) + (_datalen) - (const u8 *) _elem >=	\
233 		(int) sizeof(*_elem) &&					\
234 	     (const u8 *) (_data) + (_datalen) - (const u8 *) _elem >=	\
235 		(int) sizeof(*_elem) + _elem->datalen;			\
236 	     _elem = (const struct element *) (_elem->data + _elem->datalen))
237 
238 #define for_each_element_id(element, _id, data, datalen)		\
239 	for_each_element(element, data, datalen)			\
240 		if (element->id == (_id))
241 
242 #define for_each_element_extid(element, extid, _data, _datalen)		\
243 	for_each_element(element, _data, _datalen)			\
244 		if (element->id == WLAN_EID_EXTENSION &&		\
245 		    element->datalen > 0 &&				\
246 		    element->data[0] == (extid))
247 
248 #define for_each_subelement(sub, element)				\
249 	for_each_element(sub, (element)->data, (element)->datalen)
250 
251 #define for_each_subelement_id(sub, id, element)			\
252 	for_each_element_id(sub, id, (element)->data, (element)->datalen)
253 
254 #define for_each_subelement_extid(sub, extid, element)			\
255 	for_each_element_extid(sub, extid, (element)->data, (element)->datalen)
256 
257 /**
258  * for_each_element_completed - Determine if element parsing consumed all data
259  * @element: Element pointer after for_each_element() or friends
260  * @data: Same data pointer as passed to for_each_element() or friends
261  * @datalen: Same data length as passed to for_each_element() or friends
262  *
263  * This function returns 1 if all the data was parsed or considered
264  * while walking the elements. Only use this if your for_each_element()
265  * loop cannot be broken out of, otherwise it always returns 0.
266  *
267  * If some data was malformed, this returns %false since the last parsed
268  * element will not fill the whole remaining data.
269  */
for_each_element_completed(const struct element * element,const void * data,size_t datalen)270 static inline int for_each_element_completed(const struct element *element,
271 					     const void *data, size_t datalen)
272 {
273 	return (const u8 *) element == (const u8 *) data + datalen;
274 }
275 
276 #endif /* IEEE802_11_COMMON_H */
277