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