xref: /linux/include/linux/ieee80211-eht.h (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * IEEE 802.11 EHT definitions
4  *
5  * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
6  * <jkmaline@cc.hut.fi>
7  * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
8  * Copyright (c) 2005, Devicescape Software, Inc.
9  * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
10  * Copyright (c) 2013 - 2014 Intel Mobile Communications GmbH
11  * Copyright (c) 2016 - 2017 Intel Deutschland GmbH
12  * Copyright (c) 2018 - 2026 Intel Corporation
13  */
14 
15 #ifndef LINUX_IEEE80211_EHT_H
16 #define LINUX_IEEE80211_EHT_H
17 
18 #include <linux/types.h>
19 #include <linux/if_ether.h>
20 /* need HE definitions for the inlines here */
21 #include <linux/ieee80211-he.h>
22 
23 #define IEEE80211_TTLM_MAX_CNT				2
24 #define IEEE80211_TTLM_CONTROL_DIRECTION		0x03
25 #define IEEE80211_TTLM_CONTROL_DEF_LINK_MAP		0x04
26 #define IEEE80211_TTLM_CONTROL_SWITCH_TIME_PRESENT	0x08
27 #define IEEE80211_TTLM_CONTROL_EXPECTED_DUR_PRESENT	0x10
28 #define IEEE80211_TTLM_CONTROL_LINK_MAP_SIZE		0x20
29 
30 #define IEEE80211_TTLM_DIRECTION_DOWN		0
31 #define IEEE80211_TTLM_DIRECTION_UP		1
32 #define IEEE80211_TTLM_DIRECTION_BOTH		2
33 
34 /**
35  * struct ieee80211_ttlm_elem - TID-To-Link Mapping element
36  *
37  * Defined in section 9.4.2.314 in P802.11be_D4
38  *
39  * @control: the first part of control field
40  * @optional: the second part of control field
41  */
42 struct ieee80211_ttlm_elem {
43 	u8 control;
44 	u8 optional[];
45 } __packed;
46 
47 #define IEEE80211_EHT_MCS_NSS_RX 0x0f
48 #define IEEE80211_EHT_MCS_NSS_TX 0xf0
49 
50 /**
51  * struct ieee80211_eht_mcs_nss_supp_20mhz_only - EHT 20MHz only station max
52  * supported NSS for per MCS.
53  *
54  * For each field below, bits 0 - 3 indicate the maximal number of spatial
55  * streams for Rx, and bits 4 - 7 indicate the maximal number of spatial streams
56  * for Tx.
57  *
58  * @rx_tx_mcs7_max_nss: indicates the maximum number of spatial streams
59  *     supported for reception and the maximum number of spatial streams
60  *     supported for transmission for MCS 0 - 7.
61  * @rx_tx_mcs9_max_nss: indicates the maximum number of spatial streams
62  *     supported for reception and the maximum number of spatial streams
63  *     supported for transmission for MCS 8 - 9.
64  * @rx_tx_mcs11_max_nss: indicates the maximum number of spatial streams
65  *     supported for reception and the maximum number of spatial streams
66  *     supported for transmission for MCS 10 - 11.
67  * @rx_tx_mcs13_max_nss: indicates the maximum number of spatial streams
68  *     supported for reception and the maximum number of spatial streams
69  *     supported for transmission for MCS 12 - 13.
70  * @rx_tx_max_nss: array of the previous fields for easier loop access
71  */
72 struct ieee80211_eht_mcs_nss_supp_20mhz_only {
73 	union {
74 		struct {
75 			u8 rx_tx_mcs7_max_nss;
76 			u8 rx_tx_mcs9_max_nss;
77 			u8 rx_tx_mcs11_max_nss;
78 			u8 rx_tx_mcs13_max_nss;
79 		};
80 		u8 rx_tx_max_nss[4];
81 	};
82 };
83 
84 /**
85  * struct ieee80211_eht_mcs_nss_supp_bw - EHT max supported NSS per MCS (except
86  * 20MHz only stations).
87  *
88  * For each field below, bits 0 - 3 indicate the maximal number of spatial
89  * streams for Rx, and bits 4 - 7 indicate the maximal number of spatial streams
90  * for Tx.
91  *
92  * @rx_tx_mcs9_max_nss: indicates the maximum number of spatial streams
93  *     supported for reception and the maximum number of spatial streams
94  *     supported for transmission for MCS 0 - 9.
95  * @rx_tx_mcs11_max_nss: indicates the maximum number of spatial streams
96  *     supported for reception and the maximum number of spatial streams
97  *     supported for transmission for MCS 10 - 11.
98  * @rx_tx_mcs13_max_nss: indicates the maximum number of spatial streams
99  *     supported for reception and the maximum number of spatial streams
100  *     supported for transmission for MCS 12 - 13.
101  * @rx_tx_max_nss: array of the previous fields for easier loop access
102  */
103 struct ieee80211_eht_mcs_nss_supp_bw {
104 	union {
105 		struct {
106 			u8 rx_tx_mcs9_max_nss;
107 			u8 rx_tx_mcs11_max_nss;
108 			u8 rx_tx_mcs13_max_nss;
109 		};
110 		u8 rx_tx_max_nss[3];
111 	};
112 };
113 
114 /**
115  * struct ieee80211_eht_cap_elem_fixed - EHT capabilities fixed data
116  *
117  * This structure is the "EHT Capabilities element" fixed fields as
118  * described in P802.11be_D2.0 section 9.4.2.313.
119  *
120  * @mac_cap_info: MAC capabilities, see IEEE80211_EHT_MAC_CAP*
121  * @phy_cap_info: PHY capabilities, see IEEE80211_EHT_PHY_CAP*
122  */
123 struct ieee80211_eht_cap_elem_fixed {
124 	u8 mac_cap_info[2];
125 	u8 phy_cap_info[9];
126 } __packed;
127 
128 /**
129  * struct ieee80211_eht_cap_elem - EHT capabilities element
130  * @fixed: fixed parts, see &ieee80211_eht_cap_elem_fixed
131  * @optional: optional parts
132  */
133 struct ieee80211_eht_cap_elem {
134 	struct ieee80211_eht_cap_elem_fixed fixed;
135 
136 	/*
137 	 * Followed by:
138 	 * Supported EHT-MCS And NSS Set field: 4, 3, 6 or 9 octets.
139 	 * EHT PPE Thresholds field: variable length.
140 	 */
141 	u8 optional[];
142 } __packed;
143 
144 #define IEEE80211_EHT_OPER_INFO_PRESENT	                        0x01
145 #define IEEE80211_EHT_OPER_DISABLED_SUBCHANNEL_BITMAP_PRESENT	0x02
146 #define IEEE80211_EHT_OPER_EHT_DEF_PE_DURATION	                0x04
147 #define IEEE80211_EHT_OPER_GROUP_ADDRESSED_BU_IND_LIMIT         0x08
148 #define IEEE80211_EHT_OPER_GROUP_ADDRESSED_BU_IND_EXP_MASK      0x30
149 #define IEEE80211_EHT_OPER_MCS15_DISABLE                        0x40
150 
151 /**
152  * struct ieee80211_eht_operation - eht operation element
153  *
154  * This structure is the "EHT Operation Element" fields as
155  * described in P802.11be_D2.0 section 9.4.2.311
156  *
157  * @params: EHT operation element parameters. See &IEEE80211_EHT_OPER_*
158  * @basic_mcs_nss: indicates the EHT-MCSs for each number of spatial streams in
159  *     EHT PPDUs that are supported by all EHT STAs in the BSS in transmit and
160  *     receive.
161  * @optional: optional parts
162  */
163 struct ieee80211_eht_operation {
164 	u8 params;
165 	struct ieee80211_eht_mcs_nss_supp_20mhz_only basic_mcs_nss;
166 	u8 optional[];
167 } __packed;
168 
169 /**
170  * struct ieee80211_eht_operation_info - eht operation information
171  *
172  * @control: EHT operation information control.
173  * @ccfs0: defines a channel center frequency for a 20, 40, 80, 160, or 320 MHz
174  *     EHT BSS.
175  * @ccfs1: defines a channel center frequency for a 160 or 320 MHz EHT BSS.
176  * @optional: optional parts
177  */
178 struct ieee80211_eht_operation_info {
179 	u8 control;
180 	u8 ccfs0;
181 	u8 ccfs1;
182 	u8 optional[];
183 } __packed;
184 
185 /* EHT MAC capabilities as defined in P802.11be_D2.0 section 9.4.2.313.2 */
186 #define IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS			0x01
187 #define IEEE80211_EHT_MAC_CAP0_OM_CONTROL			0x02
188 #define IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1		0x04
189 #define IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE2		0x08
190 #define IEEE80211_EHT_MAC_CAP0_RESTRICTED_TWT			0x10
191 #define IEEE80211_EHT_MAC_CAP0_SCS_TRAFFIC_DESC			0x20
192 #define IEEE80211_EHT_MAC_CAP0_MAX_MPDU_LEN_MASK		0xc0
193 #define	IEEE80211_EHT_MAC_CAP0_MAX_MPDU_LEN_3895	        0
194 #define	IEEE80211_EHT_MAC_CAP0_MAX_MPDU_LEN_7991	        1
195 #define	IEEE80211_EHT_MAC_CAP0_MAX_MPDU_LEN_11454	        2
196 
197 #define IEEE80211_EHT_MAC_CAP1_MAX_AMPDU_LEN_MASK		0x01
198 #define IEEE80211_EHT_MAC_CAP1_EHT_TRS				0x02
199 #define IEEE80211_EHT_MAC_CAP1_TXOP_RET				0x04
200 #define IEEE80211_EHT_MAC_CAP1_TWO_BQRS				0x08
201 #define IEEE80211_EHT_MAC_CAP1_EHT_LINK_ADAPT_MASK		0x30
202 #define IEEE80211_EHT_MAC_CAP1_UNSOL_EPCS_PRIO_ACCESS		0x40
203 
204 /* EHT PHY capabilities as defined in P802.11be_D2.0 section 9.4.2.313.3 */
205 #define IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ			0x02
206 #define IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ		0x04
207 #define IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI		0x08
208 #define IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO		0x10
209 #define IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER			0x20
210 #define IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE			0x40
211 
212 /* EHT beamformee number of spatial streams <= 80MHz is split */
213 #define IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK		0x80
214 #define IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK		0x03
215 
216 #define IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK	0x1c
217 #define IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK	0xe0
218 
219 #define IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK		0x07
220 #define IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK		0x38
221 
222 /* EHT number of sounding dimensions for 320MHz is split */
223 #define IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_320MHZ_MASK		0xc0
224 #define IEEE80211_EHT_PHY_CAP3_SOUNDING_DIM_320MHZ_MASK		0x01
225 #define IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK		0x02
226 #define IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK		0x04
227 #define IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK		0x08
228 #define IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK		0x10
229 #define IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK			0x20
230 #define IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK		0x40
231 #define IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK			0x80
232 
233 #define IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO		0x01
234 #define IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP			0x02
235 #define IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP		0x04
236 #define IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI	0x08
237 #define IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK			0xf0
238 
239 #define IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK		0x01
240 #define IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP		0x02
241 #define IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP		0x04
242 #define IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT		0x08
243 #define IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK	0x30
244 #define   IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US	0
245 #define   IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US	1
246 #define   IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US	2
247 #define   IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US	3
248 
249 /* Maximum number of supported EHT LTF is split */
250 #define IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK	0xc0
251 #define IEEE80211_EHT_PHY_CAP5_SUPP_EXTRA_EHT_LTF		0x40
252 #define IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK	0x07
253 
254 #define IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_80MHZ			0x10
255 #define IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_160MHZ		0x20
256 #define IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_320MHZ		0x40
257 #define IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK			0x78
258 #define IEEE80211_EHT_PHY_CAP6_EHT_DUP_6GHZ_SUPP		0x80
259 
260 #define IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW	0x01
261 #define IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ	0x02
262 #define IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ	0x04
263 #define IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ	0x08
264 #define IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ		0x10
265 #define IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ		0x20
266 #define IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ		0x40
267 #define IEEE80211_EHT_PHY_CAP7_TB_SOUNDING_FDBK_RATE_LIMIT	0x80
268 
269 #define IEEE80211_EHT_PHY_CAP8_RX_1024QAM_WIDER_BW_DL_OFDMA	0x01
270 #define IEEE80211_EHT_PHY_CAP8_RX_4096QAM_WIDER_BW_DL_OFDMA	0x02
271 
272 /*
273  * EHT operation channel width as defined in P802.11be_D2.0 section 9.4.2.311
274  */
275 #define IEEE80211_EHT_OPER_CHAN_WIDTH		0x7
276 #define IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ	0
277 #define IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ	1
278 #define IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ	2
279 #define IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ	3
280 #define IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ	4
281 
282 /* Calculate 802.11be EHT capabilities IE Tx/Rx EHT MCS NSS Support Field size */
283 static inline u8
ieee80211_eht_mcs_nss_size(const struct ieee80211_he_cap_elem * he_cap,const struct ieee80211_eht_cap_elem_fixed * eht_cap,bool from_ap)284 ieee80211_eht_mcs_nss_size(const struct ieee80211_he_cap_elem *he_cap,
285 			   const struct ieee80211_eht_cap_elem_fixed *eht_cap,
286 			   bool from_ap)
287 {
288 	u8 count = 0;
289 
290 	/* on 2.4 GHz, if it supports 40 MHz, the result is 3 */
291 	if (he_cap->phy_cap_info[0] &
292 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G)
293 		return 3;
294 
295 	/* on 2.4 GHz, these three bits are reserved, so should be 0 */
296 	if (he_cap->phy_cap_info[0] &
297 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)
298 		count += 3;
299 
300 	if (he_cap->phy_cap_info[0] &
301 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
302 		count += 3;
303 
304 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
305 		count += 3;
306 
307 	if (count)
308 		return count;
309 
310 	return from_ap ? 3 : 4;
311 }
312 
313 /* 802.11be EHT PPE Thresholds */
314 #define IEEE80211_EHT_PPE_THRES_NSS_POS			0
315 #define IEEE80211_EHT_PPE_THRES_NSS_MASK		0xf
316 #define IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK	0x1f0
317 #define IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE		3
318 #define IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE	9
319 
320 /*
321  * Calculate 802.11be EHT capabilities IE EHT field size
322  */
323 static inline u8
ieee80211_eht_ppe_size(u16 ppe_thres_hdr,const u8 * phy_cap_info)324 ieee80211_eht_ppe_size(u16 ppe_thres_hdr, const u8 *phy_cap_info)
325 {
326 	u32 n;
327 
328 	if (!(phy_cap_info[5] &
329 	      IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT))
330 		return 0;
331 
332 	n = hweight16(ppe_thres_hdr &
333 		      IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
334 	n *= 1 + u16_get_bits(ppe_thres_hdr, IEEE80211_EHT_PPE_THRES_NSS_MASK);
335 
336 	/*
337 	 * Each pair is 6 bits, and we need to add the 9 "header" bits to the
338 	 * total size.
339 	 */
340 	n = n * IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2 +
341 	    IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
342 	return DIV_ROUND_UP(n, 8);
343 }
344 
345 static inline bool
ieee80211_eht_capa_size_ok(const u8 * he_capa,const u8 * data,u8 len,bool from_ap)346 ieee80211_eht_capa_size_ok(const u8 *he_capa, const u8 *data, u8 len,
347 			   bool from_ap)
348 {
349 	const struct ieee80211_eht_cap_elem_fixed *elem = (const void *)data;
350 	u8 needed = sizeof(struct ieee80211_eht_cap_elem_fixed);
351 
352 	if (len < needed || !he_capa)
353 		return false;
354 
355 	needed += ieee80211_eht_mcs_nss_size((const void *)he_capa,
356 					     (const void *)data,
357 					     from_ap);
358 	if (len < needed)
359 		return false;
360 
361 	if (elem->phy_cap_info[5] &
362 			IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
363 		u16 ppe_thres_hdr;
364 
365 		if (len < needed + sizeof(ppe_thres_hdr))
366 			return false;
367 
368 		ppe_thres_hdr = get_unaligned_le16(data + needed);
369 		needed += ieee80211_eht_ppe_size(ppe_thres_hdr,
370 						 elem->phy_cap_info);
371 	}
372 
373 	return len >= needed;
374 }
375 
376 static inline bool
ieee80211_eht_oper_size_ok(const u8 * data,u8 len)377 ieee80211_eht_oper_size_ok(const u8 *data, u8 len)
378 {
379 	const struct ieee80211_eht_operation *elem = (const void *)data;
380 	u8 needed = sizeof(*elem);
381 
382 	if (len < needed)
383 		return false;
384 
385 	if (elem->params & IEEE80211_EHT_OPER_INFO_PRESENT) {
386 		needed += 3;
387 
388 		if (elem->params &
389 		    IEEE80211_EHT_OPER_DISABLED_SUBCHANNEL_BITMAP_PRESENT)
390 			needed += 2;
391 	}
392 
393 	return len >= needed;
394 }
395 
396 /* must validate ieee80211_eht_oper_size_ok() first */
397 static inline const struct ieee80211_eht_operation_info *
ieee80211_eht_oper_info(const struct ieee80211_eht_operation * eht_oper)398 ieee80211_eht_oper_info(const struct ieee80211_eht_operation *eht_oper)
399 {
400 	if (!(eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT))
401 		return NULL;
402 
403 	return (const void *)eht_oper->optional;
404 }
405 
406 /* must validate ieee80211_eht_oper_size_ok() first */
407 static inline u16
ieee80211_eht_oper_dis_subchan_bitmap(const struct ieee80211_eht_operation * eht_oper)408 ieee80211_eht_oper_dis_subchan_bitmap(const struct ieee80211_eht_operation *eht_oper)
409 {
410 	const struct ieee80211_eht_operation_info *info;
411 
412 	info = ieee80211_eht_oper_info(eht_oper);
413 	if (!info)
414 		return 0;
415 
416 	if (!(eht_oper->params & IEEE80211_EHT_OPER_DISABLED_SUBCHANNEL_BITMAP_PRESENT))
417 		return 0;
418 
419 	return get_unaligned_le16(info->optional);
420 }
421 
422 #define IEEE80211_BW_IND_DIS_SUBCH_PRESENT	BIT(1)
423 
424 struct ieee80211_bandwidth_indication {
425 	u8 params;
426 	struct ieee80211_eht_operation_info info;
427 } __packed;
428 
429 static inline bool
ieee80211_bandwidth_indication_size_ok(const u8 * data,u8 len)430 ieee80211_bandwidth_indication_size_ok(const u8 *data, u8 len)
431 {
432 	const struct ieee80211_bandwidth_indication *bwi = (const void *)data;
433 
434 	if (len < sizeof(*bwi))
435 		return false;
436 
437 	if (bwi->params & IEEE80211_BW_IND_DIS_SUBCH_PRESENT &&
438 	    len < sizeof(*bwi) + 2)
439 		return false;
440 
441 	return true;
442 }
443 
444 /* Protected EHT action codes */
445 enum ieee80211_protected_eht_actioncode {
446 	WLAN_PROTECTED_EHT_ACTION_TTLM_REQ = 0,
447 	WLAN_PROTECTED_EHT_ACTION_TTLM_RES = 1,
448 	WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN = 2,
449 	WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_REQ = 3,
450 	WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_RESP = 4,
451 	WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_TEARDOWN = 5,
452 	WLAN_PROTECTED_EHT_ACTION_EML_OP_MODE_NOTIF = 6,
453 	WLAN_PROTECTED_EHT_ACTION_LINK_RECOMMEND = 7,
454 	WLAN_PROTECTED_EHT_ACTION_ML_OP_UPDATE_REQ = 8,
455 	WLAN_PROTECTED_EHT_ACTION_ML_OP_UPDATE_RESP = 9,
456 	WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_NOTIF = 10,
457 	WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_REQ = 11,
458 	WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_RESP = 12,
459 };
460 
461 /* multi-link device */
462 #define IEEE80211_MLD_MAX_NUM_LINKS	15
463 
464 #define IEEE80211_ML_CONTROL_TYPE			0x0007
465 #define IEEE80211_ML_CONTROL_TYPE_BASIC			0
466 #define IEEE80211_ML_CONTROL_TYPE_PREQ			1
467 #define IEEE80211_ML_CONTROL_TYPE_RECONF		2
468 #define IEEE80211_ML_CONTROL_TYPE_TDLS			3
469 #define IEEE80211_ML_CONTROL_TYPE_PRIO_ACCESS		4
470 #define IEEE80211_ML_CONTROL_PRESENCE_MASK		0xfff0
471 
472 struct ieee80211_multi_link_elem {
473 	__le16 control;
474 	u8 variable[];
475 } __packed;
476 
477 #define IEEE80211_MLC_BASIC_PRES_LINK_ID		0x0010
478 #define IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT	0x0020
479 #define IEEE80211_MLC_BASIC_PRES_MED_SYNC_DELAY		0x0040
480 #define IEEE80211_MLC_BASIC_PRES_EML_CAPA		0x0080
481 #define IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP		0x0100
482 #define IEEE80211_MLC_BASIC_PRES_MLD_ID			0x0200
483 #define IEEE80211_MLC_BASIC_PRES_EXT_MLD_CAPA_OP	0x0400
484 #define IEEE80211_MLC_BASIC_PRES_ENH_CRIT_UPD		0x0800
485 
486 #define IEEE80211_MED_SYNC_DELAY_DURATION		0x00ff
487 #define IEEE80211_MED_SYNC_DELAY_SYNC_OFDM_ED_THRESH	0x0f00
488 #define IEEE80211_MED_SYNC_DELAY_SYNC_MAX_NUM_TXOPS	0xf000
489 
490 /*
491  * Described in P802.11be_D3.0
492  * dot11MSDTimerDuration should default to 5484 (i.e. 171.375)
493  * dot11MSDOFDMEDthreshold defaults to -72 (i.e. 0)
494  * dot11MSDTXOPMAX defaults to 1
495  */
496 #define IEEE80211_MED_SYNC_DELAY_DEFAULT		0x10ac
497 
498 #define IEEE80211_EML_CAP_EMLSR_SUPP			0x0001
499 #define IEEE80211_EML_CAP_EML_PADDING_DELAY		0x000e
500 /* Described Tables 9-417i & 9-417k in 802.11be-2024, which have the same values */
501 #define  IEEE80211_EML_CAP_EML_PADDING_DELAY_0US		0
502 #define  IEEE80211_EML_CAP_EML_PADDING_DELAY_32US		1
503 #define  IEEE80211_EML_CAP_EML_PADDING_DELAY_64US		2
504 #define  IEEE80211_EML_CAP_EML_PADDING_DELAY_128US		3
505 #define  IEEE80211_EML_CAP_EML_PADDING_DELAY_256US		4
506 #define IEEE80211_EML_CAP_EML_TRANSITION_DELAY	0x0070
507 /* Described in Table 9-417j in 802.11be-2024 */
508 #define  IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_0US		0
509 #define  IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_16US		1
510 #define  IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_32US		2
511 #define  IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_64US		3
512 #define  IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_128US		4
513 #define  IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_256US		5
514 /* Described in Table 9-417l in 802.11be-2024 */
515 #define  IEEE80211_EML_CAP_EMLMR_TRANSITION_DELAY_0US		0
516 #define  IEEE80211_EML_CAP_EMLMR_TRANSITION_DELAY_32US		1
517 #define  IEEE80211_EML_CAP_EMLMR_TRANSITION_DELAY_64US		2
518 #define  IEEE80211_EML_CAP_EMLMR_TRANSITION_DELAY_128US		3
519 #define  IEEE80211_EML_CAP_EMLMR_TRANSITION_DELAY_256US		4
520 #define IEEE80211_EML_CAP_EMLMR_SUPPORT			0x0080
521 #define IEEE80211_EML_CAP_TRANSITION_TIMEOUT		0x7800
522 #define  IEEE80211_EML_CAP_TRANSITION_TIMEOUT_0			0
523 #define  IEEE80211_EML_CAP_TRANSITION_TIMEOUT_128US		1
524 #define  IEEE80211_EML_CAP_TRANSITION_TIMEOUT_256US		2
525 #define  IEEE80211_EML_CAP_TRANSITION_TIMEOUT_512US		3
526 #define  IEEE80211_EML_CAP_TRANSITION_TIMEOUT_1TU		4
527 #define  IEEE80211_EML_CAP_TRANSITION_TIMEOUT_2TU		5
528 #define  IEEE80211_EML_CAP_TRANSITION_TIMEOUT_4TU		6
529 #define  IEEE80211_EML_CAP_TRANSITION_TIMEOUT_8TU		7
530 #define  IEEE80211_EML_CAP_TRANSITION_TIMEOUT_16TU		8
531 #define  IEEE80211_EML_CAP_TRANSITION_TIMEOUT_32TU		9
532 #define  IEEE80211_EML_CAP_TRANSITION_TIMEOUT_64TU		10
533 
534 #define IEEE80211_MLD_CAP_OP_MAX_SIMUL_LINKS		0x000f
535 #define IEEE80211_MLD_CAP_OP_SRS_SUPPORT		0x0010
536 #define IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP	0x0060
537 #define IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_NO_SUPP	0
538 #define IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP_SAME	1
539 #define IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_RESERVED	2
540 #define IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP_DIFF	3
541 #define IEEE80211_MLD_CAP_OP_FREQ_SEP_TYPE_IND		0x0f80
542 #define IEEE80211_MLD_CAP_OP_AAR_SUPPORT		0x1000
543 #define IEEE80211_MLD_CAP_OP_LINK_RECONF_SUPPORT	0x2000
544 #define IEEE80211_MLD_CAP_OP_ALIGNED_TWT_SUPPORT	0x4000
545 
546 struct ieee80211_mle_basic_common_info {
547 	u8 len;
548 	u8 mld_mac_addr[ETH_ALEN];
549 	u8 variable[];
550 } __packed;
551 
552 #define IEEE80211_MLC_PREQ_PRES_MLD_ID			0x0010
553 
554 struct ieee80211_mle_preq_common_info {
555 	u8 len;
556 	u8 variable[];
557 } __packed;
558 
559 #define IEEE80211_MLC_RECONF_PRES_MLD_MAC_ADDR		0x0010
560 #define IEEE80211_MLC_RECONF_PRES_EML_CAPA		0x0020
561 #define IEEE80211_MLC_RECONF_PRES_MLD_CAPA_OP		0x0040
562 #define IEEE80211_MLC_RECONF_PRES_EXT_MLD_CAPA_OP	0x0080
563 
564 /* no fixed fields in RECONF */
565 
566 struct ieee80211_mle_tdls_common_info {
567 	u8 len;
568 	u8 ap_mld_mac_addr[ETH_ALEN];
569 } __packed;
570 
571 #define IEEE80211_MLC_PRIO_ACCESS_PRES_AP_MLD_MAC_ADDR	0x0010
572 
573 #define IEEE80211_EML_CTRL_EMLSR_MODE		BIT(0)
574 #define IEEE80211_EML_CTRL_EMLMR_MODE		BIT(1)
575 #define IEEE80211_EML_CTRL_EMLSR_PARAM_UPDATE	BIT(2)
576 #define IEEE80211_EML_CTRL_INDEV_COEX_ACT	BIT(3)
577 
578 #define IEEE80211_EML_EMLSR_PAD_DELAY		0x07
579 #define IEEE80211_EML_EMLSR_TRANS_DELAY		0x38
580 
581 #define IEEE80211_EML_EMLMR_RX_MCS_MAP		0xf0
582 #define IEEE80211_EML_EMLMR_TX_MCS_MAP		0x0f
583 
584 /* no fixed fields in PRIO_ACCESS */
585 
586 /**
587  * ieee80211_mle_common_size - check multi-link element common size
588  * @data: multi-link element, must already be checked for size using
589  *	ieee80211_mle_size_ok()
590  * Return: the size of the multi-link element's "common" subfield
591  */
ieee80211_mle_common_size(const u8 * data)592 static inline u8 ieee80211_mle_common_size(const u8 *data)
593 {
594 	const struct ieee80211_multi_link_elem *mle = (const void *)data;
595 	u16 control = le16_to_cpu(mle->control);
596 
597 	switch (u16_get_bits(control, IEEE80211_ML_CONTROL_TYPE)) {
598 	case IEEE80211_ML_CONTROL_TYPE_BASIC:
599 	case IEEE80211_ML_CONTROL_TYPE_PREQ:
600 	case IEEE80211_ML_CONTROL_TYPE_TDLS:
601 	case IEEE80211_ML_CONTROL_TYPE_RECONF:
602 	case IEEE80211_ML_CONTROL_TYPE_PRIO_ACCESS:
603 		/*
604 		 * The length is the first octet pointed by mle->variable so no
605 		 * need to add anything
606 		 */
607 		break;
608 	default:
609 		WARN_ON(1);
610 		return 0;
611 	}
612 
613 	return sizeof(*mle) + mle->variable[0];
614 }
615 
616 /**
617  * ieee80211_mle_get_link_id - returns the link ID
618  * @data: the basic multi link element
619  * Return: the link ID, or -1 if not present
620  *
621  * The element is assumed to be of the correct type (BASIC) and big enough,
622  * this must be checked using ieee80211_mle_type_ok().
623  */
ieee80211_mle_get_link_id(const u8 * data)624 static inline int ieee80211_mle_get_link_id(const u8 *data)
625 {
626 	const struct ieee80211_multi_link_elem *mle = (const void *)data;
627 	u16 control = le16_to_cpu(mle->control);
628 	const u8 *common = mle->variable;
629 
630 	/* common points now at the beginning of ieee80211_mle_basic_common_info */
631 	common += sizeof(struct ieee80211_mle_basic_common_info);
632 
633 	if (!(control & IEEE80211_MLC_BASIC_PRES_LINK_ID))
634 		return -1;
635 
636 	return *common;
637 }
638 
639 /**
640  * ieee80211_mle_get_bss_param_ch_cnt - returns the BSS parameter change count
641  * @data: pointer to the basic multi link element
642  * Return: the BSS Parameter Change Count field value, or -1 if not present
643  *
644  * The element is assumed to be of the correct type (BASIC) and big enough,
645  * this must be checked using ieee80211_mle_type_ok().
646  */
647 static inline int
ieee80211_mle_get_bss_param_ch_cnt(const u8 * data)648 ieee80211_mle_get_bss_param_ch_cnt(const u8 *data)
649 {
650 	const struct ieee80211_multi_link_elem *mle = (const void *)data;
651 	u16 control = le16_to_cpu(mle->control);
652 	const u8 *common = mle->variable;
653 
654 	/* common points now at the beginning of ieee80211_mle_basic_common_info */
655 	common += sizeof(struct ieee80211_mle_basic_common_info);
656 
657 	if (!(control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT))
658 		return -1;
659 
660 	if (control & IEEE80211_MLC_BASIC_PRES_LINK_ID)
661 		common += 1;
662 
663 	return *common;
664 }
665 
666 /**
667  * ieee80211_mle_get_eml_med_sync_delay - returns the medium sync delay
668  * @data: pointer to the multi-link element
669  * Return: the medium synchronization delay field value from the multi-link
670  *	element, or the default value (%IEEE80211_MED_SYNC_DELAY_DEFAULT)
671  *	if not present
672  *
673  * The element is assumed to be of the correct type (BASIC) and big enough,
674  * this must be checked using ieee80211_mle_type_ok().
675  */
ieee80211_mle_get_eml_med_sync_delay(const u8 * data)676 static inline u16 ieee80211_mle_get_eml_med_sync_delay(const u8 *data)
677 {
678 	const struct ieee80211_multi_link_elem *mle = (const void *)data;
679 	u16 control = le16_to_cpu(mle->control);
680 	const u8 *common = mle->variable;
681 
682 	/* common points now at the beginning of ieee80211_mle_basic_common_info */
683 	common += sizeof(struct ieee80211_mle_basic_common_info);
684 
685 	if (!(control & IEEE80211_MLC_BASIC_PRES_MED_SYNC_DELAY))
686 		return IEEE80211_MED_SYNC_DELAY_DEFAULT;
687 
688 	if (control & IEEE80211_MLC_BASIC_PRES_LINK_ID)
689 		common += 1;
690 	if (control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT)
691 		common += 1;
692 
693 	return get_unaligned_le16(common);
694 }
695 
696 /**
697  * ieee80211_mle_get_eml_cap - returns the EML capability
698  * @data: pointer to the multi-link element
699  * Return: the EML capability field value from the multi-link element,
700  *	or 0 if not present
701  *
702  * The element is assumed to be of the correct type (BASIC) and big enough,
703  * this must be checked using ieee80211_mle_type_ok().
704  */
ieee80211_mle_get_eml_cap(const u8 * data)705 static inline u16 ieee80211_mle_get_eml_cap(const u8 *data)
706 {
707 	const struct ieee80211_multi_link_elem *mle = (const void *)data;
708 	u16 control = le16_to_cpu(mle->control);
709 	const u8 *common = mle->variable;
710 
711 	/* common points now at the beginning of ieee80211_mle_basic_common_info */
712 	common += sizeof(struct ieee80211_mle_basic_common_info);
713 
714 	if (!(control & IEEE80211_MLC_BASIC_PRES_EML_CAPA))
715 		return 0;
716 
717 	if (control & IEEE80211_MLC_BASIC_PRES_LINK_ID)
718 		common += 1;
719 	if (control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT)
720 		common += 1;
721 	if (control & IEEE80211_MLC_BASIC_PRES_MED_SYNC_DELAY)
722 		common += 2;
723 
724 	return get_unaligned_le16(common);
725 }
726 
727 /**
728  * ieee80211_mle_get_mld_capa_op - returns the MLD capabilities and operations.
729  * @data: pointer to the multi-link element
730  * Return: the MLD capabilities and operations field value from the multi-link
731  *	element, or 0 if not present
732  *
733  * The element is assumed to be of the correct type (BASIC) and big enough,
734  * this must be checked using ieee80211_mle_type_ok().
735  */
ieee80211_mle_get_mld_capa_op(const u8 * data)736 static inline u16 ieee80211_mle_get_mld_capa_op(const u8 *data)
737 {
738 	const struct ieee80211_multi_link_elem *mle = (const void *)data;
739 	u16 control = le16_to_cpu(mle->control);
740 	const u8 *common = mle->variable;
741 
742 	/*
743 	 * common points now at the beginning of
744 	 * ieee80211_mle_basic_common_info
745 	 */
746 	common += sizeof(struct ieee80211_mle_basic_common_info);
747 
748 	if (!(control & IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP))
749 		return 0;
750 
751 	if (control & IEEE80211_MLC_BASIC_PRES_LINK_ID)
752 		common += 1;
753 	if (control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT)
754 		common += 1;
755 	if (control & IEEE80211_MLC_BASIC_PRES_MED_SYNC_DELAY)
756 		common += 2;
757 	if (control & IEEE80211_MLC_BASIC_PRES_EML_CAPA)
758 		common += 2;
759 
760 	return get_unaligned_le16(common);
761 }
762 
763 /* Defined in Figure 9-1074t in P802.11be_D7.0 */
764 #define IEEE80211_EHT_ML_EXT_MLD_CAPA_OP_PARAM_UPDATE		0x0001
765 #define IEEE80211_EHT_ML_EXT_MLD_CAPA_OP_RECO_MAX_LINKS_MASK	0x001e
766 #define IEEE80211_EHT_ML_EXT_MLD_CAPA_NSTR_UPDATE		0x0020
767 #define IEEE80211_EHT_ML_EXT_MLD_CAPA_EMLSR_ENA_ON_ONE_LINK	0x0040
768 #define IEEE80211_EHT_ML_EXT_MLD_CAPA_BTM_MLD_RECO_MULTI_AP	0x0080
769 /* defined by UHR Draft P802.11bn_D1.3 Figure 9-1147 */
770 #define IEEE80211_UHR_ML_EXT_MLD_CAPA_ML_PM			0x0100
771 
772 /**
773  * ieee80211_mle_get_ext_mld_capa_op - returns the extended MLD capabilities
774  *	and operations.
775  * @data: pointer to the multi-link element
776  * Return: the extended MLD capabilities and operations field value from
777  *	the multi-link element, or 0 if not present
778  *
779  * The element is assumed to be of the correct type (BASIC) and big enough,
780  * this must be checked using ieee80211_mle_type_ok().
781  */
ieee80211_mle_get_ext_mld_capa_op(const u8 * data)782 static inline u16 ieee80211_mle_get_ext_mld_capa_op(const u8 *data)
783 {
784 	const struct ieee80211_multi_link_elem *mle = (const void *)data;
785 	u16 control = le16_to_cpu(mle->control);
786 	const u8 *common = mle->variable;
787 
788 	/*
789 	 * common points now at the beginning of
790 	 * ieee80211_mle_basic_common_info
791 	 */
792 	common += sizeof(struct ieee80211_mle_basic_common_info);
793 
794 	if (!(control & IEEE80211_MLC_BASIC_PRES_EXT_MLD_CAPA_OP))
795 		return 0;
796 
797 	if (control & IEEE80211_MLC_BASIC_PRES_LINK_ID)
798 		common += 1;
799 	if (control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT)
800 		common += 1;
801 	if (control & IEEE80211_MLC_BASIC_PRES_MED_SYNC_DELAY)
802 		common += 2;
803 	if (control & IEEE80211_MLC_BASIC_PRES_EML_CAPA)
804 		common += 2;
805 	if (control & IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP)
806 		common += 2;
807 	if (control & IEEE80211_MLC_BASIC_PRES_MLD_ID)
808 		common += 1;
809 
810 	return get_unaligned_le16(common);
811 }
812 
813 /**
814  * ieee80211_mle_get_enh_crit_upd_info - returns the enhanced critical
815  *	updates information
816  * @data: pointer to the multi-link element
817  * Return: the enhanced critical updates information field, or %NULL
818  *
819  * The element is assumed to be of the correct type (BASIC) and big enough,
820  * this must be checked using ieee80211_mle_type_ok().
821  */
822 static inline const struct ieee80211_enh_crit_upd *
ieee80211_mle_get_enh_crit_upd_info(const u8 * data)823 ieee80211_mle_get_enh_crit_upd_info(const u8 *data)
824 {
825 	const struct ieee80211_multi_link_elem *mle = (const void *)data;
826 	u16 control = le16_to_cpu(mle->control);
827 	const u8 *common = mle->variable;
828 
829 	/*
830 	 * common points now at the beginning of
831 	 * ieee80211_mle_basic_common_info
832 	 */
833 	common += sizeof(struct ieee80211_mle_basic_common_info);
834 
835 	if (!(control & IEEE80211_MLC_BASIC_PRES_ENH_CRIT_UPD))
836 		return NULL;
837 
838 	if (control & IEEE80211_MLC_BASIC_PRES_LINK_ID)
839 		common += 1;
840 	if (control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT)
841 		common += 1;
842 	if (control & IEEE80211_MLC_BASIC_PRES_MED_SYNC_DELAY)
843 		common += 2;
844 	if (control & IEEE80211_MLC_BASIC_PRES_EML_CAPA)
845 		common += 2;
846 	if (control & IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP)
847 		common += 2;
848 	if (control & IEEE80211_MLC_BASIC_PRES_MLD_ID)
849 		common += 1;
850 	if (control & IEEE80211_MLC_BASIC_PRES_EXT_MLD_CAPA_OP)
851 		common += 2;
852 
853 	return (const void *)common;
854 }
855 
856 /**
857  * ieee80211_mle_get_mld_id - returns the MLD ID
858  * @data: pointer to the multi-link element
859  * Return: The MLD ID in the given multi-link element, or 0 if not present
860  *
861  * The element is assumed to be of the correct type (BASIC) and big enough,
862  * this must be checked using ieee80211_mle_type_ok().
863  */
ieee80211_mle_get_mld_id(const u8 * data)864 static inline u8 ieee80211_mle_get_mld_id(const u8 *data)
865 {
866 	const struct ieee80211_multi_link_elem *mle = (const void *)data;
867 	u16 control = le16_to_cpu(mle->control);
868 	const u8 *common = mle->variable;
869 
870 	/*
871 	 * common points now at the beginning of
872 	 * ieee80211_mle_basic_common_info
873 	 */
874 	common += sizeof(struct ieee80211_mle_basic_common_info);
875 
876 	if (!(control & IEEE80211_MLC_BASIC_PRES_MLD_ID))
877 		return 0;
878 
879 	if (control & IEEE80211_MLC_BASIC_PRES_LINK_ID)
880 		common += 1;
881 	if (control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT)
882 		common += 1;
883 	if (control & IEEE80211_MLC_BASIC_PRES_MED_SYNC_DELAY)
884 		common += 2;
885 	if (control & IEEE80211_MLC_BASIC_PRES_EML_CAPA)
886 		common += 2;
887 	if (control & IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP)
888 		common += 2;
889 
890 	return *common;
891 }
892 
893 /**
894  * ieee80211_mle_size_ok - validate multi-link element size
895  * @data: pointer to the element data
896  * @len: length of the containing element
897  * Return: whether or not the multi-link element size is OK
898  */
ieee80211_mle_size_ok(const u8 * data,size_t len)899 static inline bool ieee80211_mle_size_ok(const u8 *data, size_t len)
900 {
901 	const struct ieee80211_multi_link_elem *mle = (const void *)data;
902 	u8 fixed = sizeof(*mle);
903 	u8 common = 0;
904 	u8 common_len;
905 	u16 control;
906 
907 	if (!data || len < fixed)
908 		return false;
909 
910 	control = le16_to_cpu(mle->control);
911 
912 	switch (u16_get_bits(control, IEEE80211_ML_CONTROL_TYPE)) {
913 	case IEEE80211_ML_CONTROL_TYPE_BASIC:
914 		common += sizeof(struct ieee80211_mle_basic_common_info);
915 		if (control & IEEE80211_MLC_BASIC_PRES_LINK_ID)
916 			common += 1;
917 		if (control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT)
918 			common += 1;
919 		if (control & IEEE80211_MLC_BASIC_PRES_MED_SYNC_DELAY)
920 			common += 2;
921 		if (control & IEEE80211_MLC_BASIC_PRES_EML_CAPA)
922 			common += 2;
923 		if (control & IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP)
924 			common += 2;
925 		if (control & IEEE80211_MLC_BASIC_PRES_MLD_ID)
926 			common += 1;
927 		if (control & IEEE80211_MLC_BASIC_PRES_EXT_MLD_CAPA_OP)
928 			common += 2;
929 		if (control & IEEE80211_MLC_BASIC_PRES_ENH_CRIT_UPD)
930 			common += 1;
931 		break;
932 	case IEEE80211_ML_CONTROL_TYPE_PREQ:
933 		common += sizeof(struct ieee80211_mle_preq_common_info);
934 		if (control & IEEE80211_MLC_PREQ_PRES_MLD_ID)
935 			common += 1;
936 		break;
937 	case IEEE80211_ML_CONTROL_TYPE_RECONF:
938 		common += 1;
939 		if (control & IEEE80211_MLC_RECONF_PRES_MLD_MAC_ADDR)
940 			common += ETH_ALEN;
941 		if (control & IEEE80211_MLC_RECONF_PRES_EML_CAPA)
942 			common += 2;
943 		if (control & IEEE80211_MLC_RECONF_PRES_MLD_CAPA_OP)
944 			common += 2;
945 		if (control & IEEE80211_MLC_RECONF_PRES_EXT_MLD_CAPA_OP)
946 			common += 2;
947 		break;
948 	case IEEE80211_ML_CONTROL_TYPE_TDLS:
949 		common += sizeof(struct ieee80211_mle_tdls_common_info);
950 		break;
951 	case IEEE80211_ML_CONTROL_TYPE_PRIO_ACCESS:
952 		common = ETH_ALEN + 1;
953 		break;
954 	default:
955 		/* we don't know this type */
956 		return true;
957 	}
958 
959 	if (len < fixed + common)
960 		return false;
961 
962 	common_len = mle->variable[0];
963 
964 	return common_len >= common && common_len <= len - fixed;
965 }
966 
967 /**
968  * ieee80211_mle_type_ok - validate multi-link element type and size
969  * @data: pointer to the element data
970  * @type: expected type of the element
971  * @len: length of the containing element
972  * Return: whether or not the multi-link element type matches and size is OK
973  */
ieee80211_mle_type_ok(const u8 * data,u8 type,size_t len)974 static inline bool ieee80211_mle_type_ok(const u8 *data, u8 type, size_t len)
975 {
976 	const struct ieee80211_multi_link_elem *mle = (const void *)data;
977 	u16 control;
978 
979 	if (!ieee80211_mle_size_ok(data, len))
980 		return false;
981 
982 	control = le16_to_cpu(mle->control);
983 
984 	if (u16_get_bits(control, IEEE80211_ML_CONTROL_TYPE) == type)
985 		return true;
986 
987 	return false;
988 }
989 
990 enum ieee80211_mle_subelems {
991 	IEEE80211_MLE_SUBELEM_PER_STA_PROFILE		= 0,
992 	IEEE80211_MLE_SUBELEM_FRAGMENT		        = 254,
993 };
994 
995 #define IEEE80211_MLE_STA_CONTROL_LINK_ID			0x000f
996 #define IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE		0x0010
997 #define IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT		0x0020
998 #define IEEE80211_MLE_STA_CONTROL_BEACON_INT_PRESENT		0x0040
999 #define IEEE80211_MLE_STA_CONTROL_TSF_OFFS_PRESENT		0x0080
1000 #define IEEE80211_MLE_STA_CONTROL_DTIM_INFO_PRESENT		0x0100
1001 #define IEEE80211_MLE_STA_CONTROL_NSTR_LINK_PAIR_PRESENT	0x0200
1002 #define IEEE80211_MLE_STA_CONTROL_NSTR_BITMAP_SIZE		0x0400
1003 #define IEEE80211_MLE_STA_CONTROL_BSS_PARAM_CHANGE_CNT_PRESENT	0x0800
1004 #define IEEE80211_MLE_STA_CONTROL_ENH_CRIT_UPD_PRESENT		0x1000
1005 #define IEEE80211_MLE_STA_CONTROL_AP_CONDUCTED_TX_PWR_PRESENT	0x2000
1006 
1007 struct ieee80211_mle_per_sta_profile {
1008 	__le16 control;
1009 	u8 sta_info_len;
1010 	u8 variable[];
1011 } __packed;
1012 
1013 /**
1014  * ieee80211_mle_basic_sta_prof_size_ok - validate basic multi-link element sta
1015  *	profile size
1016  * @data: pointer to the sub element data
1017  * @len: length of the containing sub element
1018  * Return: %true if the STA profile is large enough, %false otherwise
1019  */
ieee80211_mle_basic_sta_prof_size_ok(const u8 * data,size_t len)1020 static inline bool ieee80211_mle_basic_sta_prof_size_ok(const u8 *data,
1021 							size_t len)
1022 {
1023 	const struct ieee80211_mle_per_sta_profile *prof = (const void *)data;
1024 	u16 control;
1025 	u8 fixed = sizeof(*prof);
1026 	u8 info_len = 1;
1027 
1028 	if (len < fixed)
1029 		return false;
1030 
1031 	control = le16_to_cpu(prof->control);
1032 
1033 	if (control & IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT)
1034 		info_len += 6;
1035 	if (control & IEEE80211_MLE_STA_CONTROL_BEACON_INT_PRESENT)
1036 		info_len += 2;
1037 	if (control & IEEE80211_MLE_STA_CONTROL_TSF_OFFS_PRESENT)
1038 		info_len += 8;
1039 	if (control & IEEE80211_MLE_STA_CONTROL_DTIM_INFO_PRESENT)
1040 		info_len += 2;
1041 	if (control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE &&
1042 	    control & IEEE80211_MLE_STA_CONTROL_NSTR_LINK_PAIR_PRESENT) {
1043 		if (control & IEEE80211_MLE_STA_CONTROL_NSTR_BITMAP_SIZE)
1044 			info_len += 2;
1045 		else
1046 			info_len += 1;
1047 	}
1048 	if (control & IEEE80211_MLE_STA_CONTROL_BSS_PARAM_CHANGE_CNT_PRESENT)
1049 		info_len += 1;
1050 
1051 	if (control & IEEE80211_MLE_STA_CONTROL_ENH_CRIT_UPD_PRESENT)
1052 		info_len += 1;
1053 
1054 	if (control & IEEE80211_MLE_STA_CONTROL_AP_CONDUCTED_TX_PWR_PRESENT)
1055 		info_len += 1;
1056 
1057 	return prof->sta_info_len >= info_len &&
1058 	       fixed + prof->sta_info_len - 1 <= len;
1059 }
1060 
1061 /**
1062  * ieee80211_mle_basic_sta_prof_bss_param_ch_cnt - get per-STA profile BSS
1063  *	parameter change count
1064  * @prof: the per-STA profile, having been checked with
1065  *	ieee80211_mle_basic_sta_prof_size_ok() for the correct length
1066  *
1067  * Return: The BSS parameter change count value if present, 0 otherwise.
1068  */
1069 static inline u8
ieee80211_mle_basic_sta_prof_bss_param_ch_cnt(const struct ieee80211_mle_per_sta_profile * prof)1070 ieee80211_mle_basic_sta_prof_bss_param_ch_cnt(const struct ieee80211_mle_per_sta_profile *prof)
1071 {
1072 	u16 control = le16_to_cpu(prof->control);
1073 	const u8 *pos = prof->variable;
1074 
1075 	if (!(control & IEEE80211_MLE_STA_CONTROL_BSS_PARAM_CHANGE_CNT_PRESENT))
1076 		return 0;
1077 
1078 	if (control & IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT)
1079 		pos += 6;
1080 	if (control & IEEE80211_MLE_STA_CONTROL_BEACON_INT_PRESENT)
1081 		pos += 2;
1082 	if (control & IEEE80211_MLE_STA_CONTROL_TSF_OFFS_PRESENT)
1083 		pos += 8;
1084 	if (control & IEEE80211_MLE_STA_CONTROL_DTIM_INFO_PRESENT)
1085 		pos += 2;
1086 	if (control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE &&
1087 	    control & IEEE80211_MLE_STA_CONTROL_NSTR_LINK_PAIR_PRESENT) {
1088 		if (control & IEEE80211_MLE_STA_CONTROL_NSTR_BITMAP_SIZE)
1089 			pos += 2;
1090 		else
1091 			pos += 1;
1092 	}
1093 
1094 	return *pos;
1095 }
1096 
1097 /**
1098  * ieee80211_mle_basic_sta_prof_enh_crit_upd - get per-STA profile enhanced
1099  *	critical updates field
1100  * @prof: the per-STA profile, having been checked with
1101  *	ieee80211_mle_basic_sta_prof_size_ok() for the correct length
1102  *
1103  * Return: The enhanced critical updates field if present, %NULL otherwise.
1104  */
1105 static inline const struct ieee80211_enh_crit_upd *
ieee80211_mle_basic_sta_prof_enh_crit_upd(const struct ieee80211_mle_per_sta_profile * prof)1106 ieee80211_mle_basic_sta_prof_enh_crit_upd(const struct ieee80211_mle_per_sta_profile *prof)
1107 {
1108 	u16 control = le16_to_cpu(prof->control);
1109 	const u8 *pos = prof->variable;
1110 
1111 	if (!(control & IEEE80211_MLE_STA_CONTROL_ENH_CRIT_UPD_PRESENT))
1112 		return NULL;
1113 
1114 	if (control & IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT)
1115 		pos += 6;
1116 	if (control & IEEE80211_MLE_STA_CONTROL_BEACON_INT_PRESENT)
1117 		pos += 2;
1118 	if (control & IEEE80211_MLE_STA_CONTROL_TSF_OFFS_PRESENT)
1119 		pos += 8;
1120 	if (control & IEEE80211_MLE_STA_CONTROL_DTIM_INFO_PRESENT)
1121 		pos += 2;
1122 	if (control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE &&
1123 	    control & IEEE80211_MLE_STA_CONTROL_NSTR_LINK_PAIR_PRESENT) {
1124 		if (control & IEEE80211_MLE_STA_CONTROL_NSTR_BITMAP_SIZE)
1125 			pos += 2;
1126 		else
1127 			pos += 1;
1128 	}
1129 	if (control & IEEE80211_MLE_STA_CONTROL_BSS_PARAM_CHANGE_CNT_PRESENT)
1130 		pos += 1;
1131 
1132 	return (const void *)pos;
1133 }
1134 
1135 #define IEEE80211_MLE_STA_RECONF_CONTROL_LINK_ID			0x000f
1136 #define IEEE80211_MLE_STA_RECONF_CONTROL_COMPLETE_PROFILE		0x0010
1137 #define IEEE80211_MLE_STA_RECONF_CONTROL_STA_MAC_ADDR_PRESENT		0x0020
1138 #define IEEE80211_MLE_STA_RECONF_CONTROL_AP_REM_TIMER_PRESENT		0x0040
1139 #define IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE			0x0780
1140 #define IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_AP_REM		0
1141 #define IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_OP_PARAM_UPDATE	1
1142 #define IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_ADD_LINK	2
1143 #define IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_DEL_LINK	3
1144 #define IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_NSTR_STATUS	4
1145 #define IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_TYPE_UHR_OMP_UPD	5
1146 #define IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_PARAMS_PRESENT	0x0800
1147 #define IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_NSTR_BMAP_SIZE	0x1000
1148 #define IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_NSTR_IND_BMAP_PRES	0x2000
1149 #define IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_DSO_INFO_PRESENT	0x4000
1150 
1151 /**
1152  * ieee80211_mle_reconf_sta_prof_size_ok - validate reconfiguration multi-link
1153  *	element sta profile size.
1154  * @data: pointer to the sub element data
1155  * @len: length of the containing sub element
1156  * Return: %true if the STA profile is large enough, %false otherwise
1157  */
ieee80211_mle_reconf_sta_prof_size_ok(const u8 * data,size_t len)1158 static inline bool ieee80211_mle_reconf_sta_prof_size_ok(const u8 *data,
1159 							 size_t len)
1160 {
1161 	const struct ieee80211_mle_per_sta_profile *prof = (const void *)data;
1162 	u16 control;
1163 	u8 fixed = sizeof(*prof);
1164 	u8 info_len = 1;
1165 
1166 	if (len < fixed)
1167 		return false;
1168 
1169 	control = le16_to_cpu(prof->control);
1170 
1171 	if (control & IEEE80211_MLE_STA_RECONF_CONTROL_STA_MAC_ADDR_PRESENT)
1172 		info_len += ETH_ALEN;
1173 	if (control & IEEE80211_MLE_STA_RECONF_CONTROL_AP_REM_TIMER_PRESENT)
1174 		info_len += 2;
1175 	if (control & IEEE80211_MLE_STA_RECONF_CONTROL_OPERATION_PARAMS_PRESENT)
1176 		info_len += 2;
1177 
1178 	return prof->sta_info_len >= info_len &&
1179 	       fixed + prof->sta_info_len - 1 <= len;
1180 }
1181 
1182 #define IEEE80211_MLE_STA_EPCS_CONTROL_LINK_ID			0x000f
1183 #define IEEE80211_EPCS_ENA_RESP_BODY_LEN                        3
1184 
ieee80211_tid_to_link_map_size_ok(const u8 * data,size_t len)1185 static inline bool ieee80211_tid_to_link_map_size_ok(const u8 *data, size_t len)
1186 {
1187 	const struct ieee80211_ttlm_elem *t2l = (const void *)data;
1188 	u8 control, fixed = sizeof(*t2l), elem_len = 0;
1189 
1190 	if (len < fixed)
1191 		return false;
1192 
1193 	control = t2l->control;
1194 
1195 	if (control & IEEE80211_TTLM_CONTROL_SWITCH_TIME_PRESENT)
1196 		elem_len += 2;
1197 	if (control & IEEE80211_TTLM_CONTROL_EXPECTED_DUR_PRESENT)
1198 		elem_len += 3;
1199 
1200 	if (!(control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP)) {
1201 		u8 bm_size;
1202 
1203 		elem_len += 1;
1204 		if (len < fixed + elem_len)
1205 			return false;
1206 
1207 		if (control & IEEE80211_TTLM_CONTROL_LINK_MAP_SIZE)
1208 			bm_size = 1;
1209 		else
1210 			bm_size = 2;
1211 
1212 		elem_len += hweight8(t2l->optional[0]) * bm_size;
1213 	}
1214 
1215 	return len >= fixed + elem_len;
1216 }
1217 
1218 /**
1219  * ieee80211_emlsr_pad_delay_in_us - Fetch the EMLSR Padding delay
1220  *	in microseconds
1221  * @eml_cap: EML capabilities field value from common info field of
1222  *	the Multi-link element
1223  * Return: the EMLSR Padding delay (in microseconds) encoded in the
1224  *	EML Capabilities field
1225  */
1226 
ieee80211_emlsr_pad_delay_in_us(u16 eml_cap)1227 static inline u32 ieee80211_emlsr_pad_delay_in_us(u16 eml_cap)
1228 {
1229 	u32 emlsr_supp =
1230 		u16_get_bits(eml_cap, IEEE80211_EML_CAP_EMLSR_SUPP);
1231 
1232 	if (!emlsr_supp)
1233 		return 0;
1234 
1235 	/* IEEE Std 802.11be-2024 Table 9-417i—Encoding of the EMLSR
1236 	 * Padding Delay subfield.
1237 	 */
1238 	u32 pad_delay = u16_get_bits(eml_cap,
1239 				     IEEE80211_EML_CAP_EML_PADDING_DELAY);
1240 
1241 	if (!pad_delay ||
1242 	    pad_delay > IEEE80211_EML_CAP_EML_PADDING_DELAY_256US)
1243 		return 0;
1244 
1245 	return 32 * (1 << (pad_delay - 1));
1246 }
1247 
1248 /**
1249  * ieee80211_emlsr_trans_delay_in_us - Fetch the EMLSR Transition
1250  *	delay in microseconds
1251  * @eml_cap: EML capabilities field value from common info field of
1252  *	the Multi-link element
1253  * Return: the EMLSR Transition delay (in microseconds) encoded in the
1254  *	EML Capabilities field
1255  */
1256 
ieee80211_emlsr_trans_delay_in_us(u16 eml_cap)1257 static inline u32 ieee80211_emlsr_trans_delay_in_us(u16 eml_cap)
1258 {
1259 	u32 emlsr_supp =
1260 		u16_get_bits(eml_cap, IEEE80211_EML_CAP_EMLSR_SUPP);
1261 
1262 	if (!emlsr_supp)
1263 		return 0;
1264 
1265 	/* IEEE Std 802.11be-2024 Table 9-417j—Encoding of the EMLSR
1266 	 * Transition Delay subfield.
1267 	 */
1268 	u32 trans_delay =
1269 		u16_get_bits(eml_cap,
1270 			     IEEE80211_EML_CAP_EML_TRANSITION_DELAY);
1271 
1272 	/* invalid values also just use 0 */
1273 	if (!trans_delay ||
1274 	    trans_delay > IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_256US)
1275 		return 0;
1276 
1277 	return 16 * (1 << (trans_delay - 1));
1278 }
1279 
1280 /**
1281  * ieee80211_emlmr_pad_delay_in_us - Fetch the EMLMR Padding delay
1282  *	in microseconds
1283  * @eml_cap: EML capabilities field value from common info field of
1284  *	the Multi-link element
1285  * Return: the EMLMR Padding delay (in microseconds) encoded in the
1286  *	EML Capabilities field
1287  */
1288 
ieee80211_emlmr_pad_delay_in_us(u16 eml_cap)1289 static inline u32 ieee80211_emlmr_pad_delay_in_us(u16 eml_cap)
1290 {
1291 	u32 emlmr_supp =
1292 		u16_get_bits(eml_cap, IEEE80211_EML_CAP_EMLMR_SUPPORT);
1293 
1294 	if (!emlmr_supp)
1295 		return 0;
1296 
1297 	/* IEEE Std 802.11be-2024 Table 9-417k—Encoding of the EMLMR
1298 	 * Padding Delay subfield.
1299 	 */
1300 	u32 pad_delay = u16_get_bits(eml_cap,
1301 				     IEEE80211_EML_CAP_EML_PADDING_DELAY);
1302 
1303 	if (!pad_delay ||
1304 	    pad_delay > IEEE80211_EML_CAP_EML_PADDING_DELAY_256US)
1305 		return 0;
1306 
1307 	return 32 * (1 << (pad_delay - 1));
1308 }
1309 
1310 /**
1311  * ieee80211_emlmr_trans_delay_in_us - Fetch the EMLMR Transition
1312  *	delay in microseconds
1313  * @eml_cap: EML capabilities field value from common info field of
1314  *	the Multi-link element
1315  * Return: the EMLMR Transition delay (in microseconds) encoded in the
1316  *	EML Capabilities field
1317  */
1318 
ieee80211_emlmr_trans_delay_in_us(u16 eml_cap)1319 static inline u32 ieee80211_emlmr_trans_delay_in_us(u16 eml_cap)
1320 {
1321 	u32 emlmr_supp =
1322 		u16_get_bits(eml_cap, IEEE80211_EML_CAP_EMLMR_SUPPORT);
1323 
1324 	if (!emlmr_supp)
1325 		return 0;
1326 
1327 	/* IEEE Std 802.11be-2024 Table 9-417l—Encoding of the EMLMR
1328 	 * Transition Delay subfield.
1329 	 */
1330 	u32 trans_delay =
1331 		u16_get_bits(eml_cap,
1332 			     IEEE80211_EML_CAP_EML_TRANSITION_DELAY);
1333 
1334 	/* invalid values also just use 0 */
1335 	if (!trans_delay ||
1336 	    trans_delay > IEEE80211_EML_CAP_EMLMR_TRANSITION_DELAY_256US)
1337 		return 0;
1338 
1339 	return 32 * (1 << (trans_delay - 1));
1340 }
1341 
1342 /**
1343  * ieee80211_eml_trans_timeout_in_us - Fetch the EML Transition
1344  *	timeout value in microseconds
1345  * @eml_cap: EML capabilities field value from common info field of
1346  *	the Multi-link element
1347  * Return: the EML Transition timeout (in microseconds) encoded in
1348  *	the EML Capabilities field
1349  */
1350 
ieee80211_eml_trans_timeout_in_us(u16 eml_cap)1351 static inline u32 ieee80211_eml_trans_timeout_in_us(u16 eml_cap)
1352 {
1353 	/* IEEE Std 802.11be-2024 Table 9-417m—Encoding of the
1354 	 * Transition Timeout subfield.
1355 	 */
1356 	u8 timeout = u16_get_bits(eml_cap,
1357 				  IEEE80211_EML_CAP_TRANSITION_TIMEOUT);
1358 
1359 	/* invalid values also just use 0 */
1360 	if (!timeout || timeout > IEEE80211_EML_CAP_TRANSITION_TIMEOUT_64TU)
1361 		return 0;
1362 
1363 	return 128 * (1 << (timeout - 1));
1364 }
1365 
1366 #define for_each_mle_subelement(_elem, _data, _len)			\
1367 	if (ieee80211_mle_size_ok(_data, _len))				\
1368 		for_each_element(_elem,					\
1369 				 _data + ieee80211_mle_common_size(_data),\
1370 				 _len - ieee80211_mle_common_size(_data))
1371 
1372 #endif /* LINUX_IEEE80211_EHT_H */
1373