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