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