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 struct multi_ap_params {
34 	u8 capability;
35 	u8 profile;
36 	u16 vlanid;
37 };
38 
39 /* Parsed Information Elements */
40 struct ieee802_11_elems {
41 	const u8 *ssid;
42 	const u8 *supp_rates;
43 	const u8 *ds_params;
44 	const u8 *challenge;
45 	const u8 *erp_info;
46 	const u8 *ext_supp_rates;
47 	const u8 *wpa_ie;
48 	const u8 *rsn_ie;
49 	const u8 *rsnxe;
50 	const u8 *wmm; /* WMM Information or Parameter Element */
51 	const u8 *wmm_tspec;
52 	const u8 *wps_ie;
53 	const u8 *supp_channels;
54 	const u8 *mdie;
55 	const u8 *ftie;
56 	const u8 *timeout_int;
57 	const u8 *ht_capabilities;
58 	const u8 *ht_operation;
59 	const u8 *mesh_config;
60 	const u8 *mesh_id;
61 	const u8 *peer_mgmt;
62 	const u8 *vht_capabilities;
63 	const u8 *vht_operation;
64 	const u8 *opmode_notif;
65 	const u8 *vendor_ht_cap;
66 	const u8 *vendor_vht;
67 	const u8 *p2p;
68 	const u8 *p2p2_ie;
69 	const u8 *pasn_encrypted_data;
70 	const u8 *wfd;
71 	const u8 *link_id;
72 	const u8 *interworking;
73 	const u8 *qos_map_set;
74 	const u8 *hs20;
75 	const u8 *ext_capab;
76 	const u8 *bss_max_idle_period;
77 	const u8 *ssid_list;
78 	const u8 *mbo;
79 	const u8 *ampe;
80 	const u8 *mic;
81 	const u8 *pref_freq_list;
82 	const u8 *supp_op_classes;
83 	const u8 *rrm_enabled;
84 	const u8 *cag_number;
85 	const u8 *ap_csn;
86 	const u8 *fils_indic;
87 	const u8 *dils;
88 	const u8 *assoc_delay_info;
89 	const u8 *fils_req_params;
90 	const u8 *fils_key_confirm;
91 	const u8 *fils_session;
92 	const u8 *fils_hlp;
93 	const u8 *fils_ip_addr_assign;
94 	const u8 *key_delivery;
95 	const u8 *wrapped_data;
96 	const u8 *fils_pk;
97 	const u8 *fils_nonce;
98 	const u8 *owe_dh;
99 	const u8 *power_capab;
100 	const u8 *roaming_cons_sel;
101 	const u8 *password_id;
102 	const u8 *oci;
103 	const u8 *multi_ap;
104 	const u8 *he_capabilities;
105 	const u8 *he_operation;
106 	const u8 *short_ssid_list;
107 	const u8 *he_6ghz_band_cap;
108 	const u8 *sae_pk;
109 	const u8 *s1g_capab;
110 	const u8 *pasn_params;
111 	const u8 *eht_capabilities;
112 	const u8 *eht_operation;
113 	const u8 *basic_mle;
114 	const u8 *probe_req_mle;
115 	const u8 *reconf_mle;
116 	const u8 *tdls_mle;
117 	const u8 *prior_access_mle;
118 	const u8 *mbssid_known_bss;
119 	const u8 *mbssid;
120 	const u8 *rsne_override;
121 	const u8 *rsne_override_2;
122 	const u8 *rsnxe_override;
123 	const u8 *rsn_selection;
124 	const u8 *wfa_capab;
125 
126 	u8 ssid_len;
127 	u8 supp_rates_len;
128 	u8 challenge_len;
129 	u8 ext_supp_rates_len;
130 	u8 wpa_ie_len;
131 	u8 rsn_ie_len;
132 	u8 rsnxe_len;
133 	u8 wmm_len; /* 7 = WMM Information; 24 = WMM Parameter */
134 	u8 wmm_tspec_len;
135 	u8 wps_ie_len;
136 	u8 supp_channels_len;
137 	u8 mdie_len;
138 	u8 ftie_len;
139 	u8 mesh_config_len;
140 	u8 mesh_id_len;
141 	u8 peer_mgmt_len;
142 	u8 vendor_ht_cap_len;
143 	u8 vendor_vht_len;
144 	u8 p2p_len;
145 	u8 p2p2_ie_len;
146 	u8 pasn_encrypted_data_len;
147 	u8 wfd_len;
148 	u8 interworking_len;
149 	u8 qos_map_set_len;
150 	u8 hs20_len;
151 	u8 ext_capab_len;
152 	u8 ssid_list_len;
153 	u8 mbo_len;
154 	u8 ampe_len;
155 	u8 mic_len;
156 	u8 pref_freq_list_len;
157 	u8 supp_op_classes_len;
158 	u8 rrm_enabled_len;
159 	u8 cag_number_len;
160 	u8 fils_indic_len;
161 	u8 dils_len;
162 	u8 fils_req_params_len;
163 	u8 fils_key_confirm_len;
164 	size_t fils_hlp_len;
165 	u8 fils_ip_addr_assign_len;
166 	u8 key_delivery_len;
167 	size_t wrapped_data_len;
168 	u8 fils_pk_len;
169 	u8 owe_dh_len;
170 	u8 power_capab_len;
171 	u8 roaming_cons_sel_len;
172 	u8 password_id_len;
173 	u8 oci_len;
174 	u8 multi_ap_len;
175 	u8 he_capabilities_len;
176 	u8 he_operation_len;
177 	u8 short_ssid_list_len;
178 	u8 sae_pk_len;
179 	u8 pasn_params_len;
180 	u8 eht_capabilities_len;
181 	u8 eht_operation_len;
182 	size_t basic_mle_len;
183 	size_t probe_req_mle_len;
184 	size_t reconf_mle_len;
185 	size_t tdls_mle_len;
186 	size_t prior_access_mle_len;
187 	u8 mbssid_known_bss_len;
188 	u8 mbssid_len;
189 	size_t rsne_override_len;
190 	size_t rsne_override_2_len;
191 	size_t rsnxe_override_len;
192 	size_t rsn_selection_len;
193 	u8 wfa_capab_len;
194 
195 	struct mb_ies_info mb_ies;
196 
197 	size_t fte_defrag_len;
198 
199 	/*
200 	 * The number of fragment elements to be skipped after a known
201 	 * fragmented element.
202 	 */
203 	unsigned int num_frag_elems;
204 };
205 
206 typedef enum { ParseOK = 0, ParseUnknown = 1, ParseFailed = -1 } ParseRes;
207 
208 ParseRes ieee802_11_parse_elems(const u8 *start, size_t len,
209 				struct ieee802_11_elems *elems,
210 				int show_errors);
211 void ieee802_11_elems_clear_ids(struct ieee802_11_elems *elems,
212 				const u8 *ids, size_t num);
213 void ieee802_11_elems_clear_ext_ids(struct ieee802_11_elems *elems,
214 				    const u8 *ids, size_t num);
215 ParseRes ieee802_11_parse_link_assoc_req(struct ieee802_11_elems *elems,
216 					 struct wpabuf *mlbuf,
217 					 u8 link_id, bool show_errors);
218 int ieee802_11_ie_count(const u8 *ies, size_t ies_len);
219 struct wpabuf * ieee802_11_vendor_ie_concat(const u8 *ies, size_t ies_len,
220 					    u32 oui_type);
221 struct ieee80211_hdr;
222 const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len);
223 
224 struct hostapd_wmm_ac_params {
225 	int cwmin;
226 	int cwmax;
227 	int aifs;
228 	int txop_limit; /* in units of 32us */
229 	int admission_control_mandatory;
230 };
231 
232 int hostapd_config_wmm_ac(struct hostapd_wmm_ac_params wmm_ac_params[],
233 			  const char *name, const char *val);
234 
235 struct hostapd_tx_queue_params {
236 	int aifs;
237 	int cwmin;
238 	int cwmax;
239 	int burst; /* maximum burst time in 0.1 ms, i.e., 10 = 1 ms */
240 };
241 
242 #define NUM_TX_QUEUES 4
243 
244 int hostapd_config_tx_queue(struct hostapd_tx_queue_params queue[],
245 			    const char *name, const char *val);
246 enum hostapd_hw_mode ieee80211_freq_to_chan(int freq, u8 *channel);
247 int ieee80211_chan_to_freq(const char *country, u8 op_class, u8 chan);
248 enum hostapd_hw_mode
249 ieee80211_freq_to_channel_ext(unsigned int freq, int sec_channel,
250 			      enum oper_chan_width chanwidth,
251 			      u8 *op_class, u8 *channel);
252 int ieee80211_chaninfo_to_channel(unsigned int freq, enum chan_width chanwidth,
253 				  int sec_channel, u8 *op_class, u8 *channel);
254 int ieee80211_is_dfs(int freq, const struct hostapd_hw_modes *modes,
255 		     u16 num_modes);
256 int is_dfs_global_op_class(u8 op_class);
257 bool is_80plus_op_class(u8 op_class);
258 enum phy_type ieee80211_get_phy_type(int freq, int ht, int vht);
259 
260 int supp_rates_11b_only(struct ieee802_11_elems *elems);
261 int mb_ies_info_by_ies(struct mb_ies_info *info, const u8 *ies_buf,
262 		       size_t ies_len);
263 struct wpabuf * mb_ies_by_info(struct mb_ies_info *info);
264 
265 const char * fc2str(u16 fc);
266 const char * reason2str(u16 reason);
267 const char * status2str(u16 status);
268 
269 struct oper_class_map {
270 	enum hostapd_hw_mode mode;
271 	u8 op_class;
272 	u8 min_chan;
273 	u8 max_chan;
274 	u8 inc;
275 	enum { BW20, BW40PLUS, BW40MINUS, BW40, BW80, BW2160, BW160, BW80P80,
276 	       BW320, BW4320, BW6480, BW8640} bw;
277 	enum { P2P_SUPP, NO_P2P_SUPP } p2p;
278 };
279 
280 extern const struct oper_class_map global_op_class[];
281 extern size_t global_op_class_size;
282 
283 const u8 * get_ie(const u8 *ies, size_t len, u8 eid);
284 const u8 * get_ie_ext(const u8 *ies, size_t len, u8 ext);
285 const u8 * get_vendor_ie(const u8 *ies, size_t len, u32 vendor_type);
286 
287 size_t mbo_add_ie(u8 *buf, size_t len, const u8 *attr, size_t attr_len);
288 
289 u16 check_multi_ap_ie(const u8 *multi_ap_ie, size_t multi_ap_len,
290 		      struct multi_ap_params *multi_ap);
291 size_t add_multi_ap_ie(u8 *buf, size_t len,
292 		       const struct multi_ap_params *multi_ap);
293 
294 struct country_op_class {
295 	u8 country_op_class;
296 	u8 global_op_class;
297 };
298 
299 u8 country_to_global_op_class(const char *country, u8 op_class);
300 
301 const struct oper_class_map * get_oper_class(const char *country, u8 op_class);
302 int oper_class_bw_to_int(const struct oper_class_map *map);
303 int center_idx_to_bw_6ghz(u8 idx);
304 bool is_6ghz_freq(int freq);
305 bool is_6ghz_op_class(u8 op_class);
306 bool is_6ghz_psc_frequency(int freq);
307 int get_6ghz_sec_channel(int channel);
308 
309 bool is_same_band(int freq1, int freq2);
310 #define IS_2P4GHZ(n) (n >= 2412 && n <= 2484)
311 #define IS_5GHZ(n) (n > 4000 && n < 5895)
312 
313 int ieee802_11_parse_candidate_list(const char *pos, u8 *nei_rep,
314 				    size_t nei_rep_len);
315 
316 int ieee802_11_ext_capab(const u8 *ie, unsigned int capab);
317 bool ieee802_11_rsnx_capab_len(const u8 *rsnxe, size_t rsnxe_len,
318 			       unsigned int capab);
319 bool ieee802_11_rsnx_capab(const u8 *rsnxe, unsigned int capab);
320 int op_class_to_bandwidth(u8 op_class);
321 enum oper_chan_width op_class_to_ch_width(u8 op_class);
322 int chwidth_freq2_to_ch_width(int chwidth, int freq2);
323 
324 /* element iteration helpers */
325 #define for_each_element(_elem, _data, _datalen)			\
326 	for (_elem = (const struct element *) (_data);			\
327 	     (const u8 *) (_data) + (_datalen) - (const u8 *) _elem >=	\
328 		(int) sizeof(*_elem) &&					\
329 	     (const u8 *) (_data) + (_datalen) - (const u8 *) _elem >=	\
330 		(int) sizeof(*_elem) + _elem->datalen;			\
331 	     _elem = (const struct element *) (_elem->data + _elem->datalen))
332 
333 #define for_each_element_id(element, _id, data, datalen)		\
334 	for_each_element(element, data, datalen)			\
335 		if (element->id == (_id))
336 
337 #define for_each_element_extid(element, extid, _data, _datalen)		\
338 	for_each_element(element, _data, _datalen)			\
339 		if (element->id == WLAN_EID_EXTENSION &&		\
340 		    element->datalen > 0 &&				\
341 		    element->data[0] == (extid))
342 
343 #define for_each_subelement(sub, element)				\
344 	for_each_element(sub, (element)->data, (element)->datalen)
345 
346 #define for_each_subelement_id(sub, id, element)			\
347 	for_each_element_id(sub, id, (element)->data, (element)->datalen)
348 
349 #define for_each_subelement_extid(sub, extid, element)			\
350 	for_each_element_extid(sub, extid, (element)->data, (element)->datalen)
351 
352 /**
353  * for_each_element_completed - Determine if element parsing consumed all data
354  * @element: Element pointer after for_each_element() or friends
355  * @data: Same data pointer as passed to for_each_element() or friends
356  * @datalen: Same data length as passed to for_each_element() or friends
357  *
358  * This function returns 1 if all the data was parsed or considered
359  * while walking the elements. Only use this if your for_each_element()
360  * loop cannot be broken out of, otherwise it always returns 0.
361  *
362  * If some data was malformed, this returns %false since the last parsed
363  * element will not fill the whole remaining data.
364  */
for_each_element_completed(const struct element * element,const void * data,size_t datalen)365 static inline int for_each_element_completed(const struct element *element,
366 					     const void *data, size_t datalen)
367 {
368 	return (const u8 *) element == (const u8 *) data + datalen;
369 }
370 
371 struct ieee80211_edmg_config;
372 
373 void hostapd_encode_edmg_chan(int edmg_enable, u8 edmg_channel,
374 			      int primary_channel,
375 			      struct ieee80211_edmg_config *edmg);
376 
377 int ieee802_edmg_is_allowed(struct ieee80211_edmg_config allowed,
378 			    struct ieee80211_edmg_config requested);
379 
380 struct wpabuf * ieee802_11_defrag(const u8 *data, size_t len, bool ext_elem);
381 const u8 * get_ml_ie(const u8 *ies, size_t len, u8 type);
382 const u8 * get_basic_mle_mld_addr(const u8 *buf, size_t len);
383 
384 #endif /* IEEE802_11_COMMON_H */
385