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