1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * IEEE802.15.4-2003 specification
4  *
5  * Copyright (C) 2007, 2008 Siemens AG
6  *
7  * Written by:
8  * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
9  * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
10  * Maxim Osipov <maxim.osipov@siemens.com>
11  * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
12  * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
13  */
14 
15 #ifndef LINUX_IEEE802154_H
16 #define LINUX_IEEE802154_H
17 
18 #include <linux/types.h>
19 #include <linux/random.h>
20 
21 #define IEEE802154_MTU			127
22 #define IEEE802154_ACK_PSDU_LEN		5
23 #define IEEE802154_MIN_PSDU_LEN		9
24 #define IEEE802154_FCS_LEN		2
25 #define IEEE802154_MAX_AUTH_TAG_LEN	16
26 #define IEEE802154_FC_LEN		2
27 #define IEEE802154_SEQ_LEN		1
28 
29 /*  General MAC frame format:
30  *  2 bytes: Frame Control
31  *  1 byte:  Sequence Number
32  * 20 bytes: Addressing fields
33  * 14 bytes: Auxiliary Security Header
34  */
35 #define IEEE802154_MAX_HEADER_LEN	(2 + 1 + 20 + 14)
36 #define IEEE802154_MIN_HEADER_LEN	(IEEE802154_ACK_PSDU_LEN - \
37 					 IEEE802154_FCS_LEN)
38 
39 #define IEEE802154_PAN_ID_BROADCAST	0xffff
40 #define IEEE802154_ADDR_SHORT_BROADCAST	0xffff
41 #define IEEE802154_ADDR_SHORT_UNSPEC	0xfffe
42 
43 #define IEEE802154_EXTENDED_ADDR_LEN	8
44 #define IEEE802154_SHORT_ADDR_LEN	2
45 #define IEEE802154_PAN_ID_LEN		2
46 
47 #define IEEE802154_LIFS_PERIOD		40
48 #define IEEE802154_SIFS_PERIOD		12
49 #define IEEE802154_MAX_SIFS_FRAME_SIZE	18
50 
51 #define IEEE802154_MAX_CHANNEL		26
52 #define IEEE802154_MAX_PAGE		31
53 
54 #define IEEE802154_FC_TYPE_BEACON	0x0	/* Frame is beacon */
55 #define	IEEE802154_FC_TYPE_DATA		0x1	/* Frame is data */
56 #define IEEE802154_FC_TYPE_ACK		0x2	/* Frame is acknowledgment */
57 #define IEEE802154_FC_TYPE_MAC_CMD	0x3	/* Frame is MAC command */
58 
59 #define IEEE802154_FC_TYPE_SHIFT		0
60 #define IEEE802154_FC_TYPE_MASK		((1 << 3) - 1)
61 #define IEEE802154_FC_TYPE(x)		((x & IEEE802154_FC_TYPE_MASK) >> IEEE802154_FC_TYPE_SHIFT)
62 #define IEEE802154_FC_SET_TYPE(v, x)	do {	\
63 	v = (((v) & ~IEEE802154_FC_TYPE_MASK) | \
64 	    (((x) << IEEE802154_FC_TYPE_SHIFT) & IEEE802154_FC_TYPE_MASK)); \
65 	} while (0)
66 
67 #define IEEE802154_FC_SECEN_SHIFT	3
68 #define IEEE802154_FC_SECEN		(1 << IEEE802154_FC_SECEN_SHIFT)
69 #define IEEE802154_FC_FRPEND_SHIFT	4
70 #define IEEE802154_FC_FRPEND		(1 << IEEE802154_FC_FRPEND_SHIFT)
71 #define IEEE802154_FC_ACK_REQ_SHIFT	5
72 #define IEEE802154_FC_ACK_REQ		(1 << IEEE802154_FC_ACK_REQ_SHIFT)
73 #define IEEE802154_FC_INTRA_PAN_SHIFT	6
74 #define IEEE802154_FC_INTRA_PAN		(1 << IEEE802154_FC_INTRA_PAN_SHIFT)
75 
76 #define IEEE802154_FC_SAMODE_SHIFT	14
77 #define IEEE802154_FC_SAMODE_MASK	(3 << IEEE802154_FC_SAMODE_SHIFT)
78 #define IEEE802154_FC_DAMODE_SHIFT	10
79 #define IEEE802154_FC_DAMODE_MASK	(3 << IEEE802154_FC_DAMODE_SHIFT)
80 
81 #define IEEE802154_FC_VERSION_SHIFT	12
82 #define IEEE802154_FC_VERSION_MASK	(3 << IEEE802154_FC_VERSION_SHIFT)
83 #define IEEE802154_FC_VERSION(x)	((x & IEEE802154_FC_VERSION_MASK) >> IEEE802154_FC_VERSION_SHIFT)
84 
85 #define IEEE802154_FC_SAMODE(x)		\
86 	(((x) & IEEE802154_FC_SAMODE_MASK) >> IEEE802154_FC_SAMODE_SHIFT)
87 
88 #define IEEE802154_FC_DAMODE(x)		\
89 	(((x) & IEEE802154_FC_DAMODE_MASK) >> IEEE802154_FC_DAMODE_SHIFT)
90 
91 #define IEEE802154_SCF_SECLEVEL_MASK		7
92 #define IEEE802154_SCF_SECLEVEL_SHIFT		0
93 #define IEEE802154_SCF_SECLEVEL(x)		(x & IEEE802154_SCF_SECLEVEL_MASK)
94 #define IEEE802154_SCF_KEY_ID_MODE_SHIFT	3
95 #define IEEE802154_SCF_KEY_ID_MODE_MASK		(3 << IEEE802154_SCF_KEY_ID_MODE_SHIFT)
96 #define IEEE802154_SCF_KEY_ID_MODE(x)		\
97 	((x & IEEE802154_SCF_KEY_ID_MODE_MASK) >> IEEE802154_SCF_KEY_ID_MODE_SHIFT)
98 
99 #define IEEE802154_SCF_KEY_IMPLICIT		0
100 #define IEEE802154_SCF_KEY_INDEX		1
101 #define IEEE802154_SCF_KEY_SHORT_INDEX		2
102 #define IEEE802154_SCF_KEY_HW_INDEX		3
103 
104 #define IEEE802154_SCF_SECLEVEL_NONE		0
105 #define IEEE802154_SCF_SECLEVEL_MIC32		1
106 #define IEEE802154_SCF_SECLEVEL_MIC64		2
107 #define IEEE802154_SCF_SECLEVEL_MIC128		3
108 #define IEEE802154_SCF_SECLEVEL_ENC		4
109 #define IEEE802154_SCF_SECLEVEL_ENC_MIC32	5
110 #define IEEE802154_SCF_SECLEVEL_ENC_MIC64	6
111 #define IEEE802154_SCF_SECLEVEL_ENC_MIC128	7
112 
113 /* MAC footer size */
114 #define IEEE802154_MFR_SIZE	2 /* 2 octets */
115 
116 /* MAC's Command Frames Identifiers */
117 #define IEEE802154_CMD_ASSOCIATION_REQ		0x01
118 #define IEEE802154_CMD_ASSOCIATION_RESP		0x02
119 #define IEEE802154_CMD_DISASSOCIATION_NOTIFY	0x03
120 #define IEEE802154_CMD_DATA_REQ			0x04
121 #define IEEE802154_CMD_PANID_CONFLICT_NOTIFY	0x05
122 #define IEEE802154_CMD_ORPHAN_NOTIFY		0x06
123 #define IEEE802154_CMD_BEACON_REQ		0x07
124 #define IEEE802154_CMD_COORD_REALIGN_NOTIFY	0x08
125 #define IEEE802154_CMD_GTS_REQ			0x09
126 
127 /*
128  * The return values of MAC operations
129  */
130 enum {
131 	/*
132 	 * The requested operation was completed successfully.
133 	 * For a transmission request, this value indicates
134 	 * a successful transmission.
135 	 */
136 	IEEE802154_SUCCESS = 0x0,
137 	/* The requested operation failed. */
138 	IEEE802154_MAC_ERROR = 0x1,
139 	/* The requested operation has been cancelled. */
140 	IEEE802154_CANCELLED = 0x2,
141 	/*
142 	 * Device is ready to poll the coordinator for data in a non beacon
143 	 * enabled PAN.
144 	 */
145 	IEEE802154_READY_FOR_POLL = 0x3,
146 	/* Wrong frame counter. */
147 	IEEE802154_COUNTER_ERROR = 0xdb,
148 	/*
149 	 * The frame does not conforms to the incoming key usage policy checking
150 	 * procedure.
151 	 */
152 	IEEE802154_IMPROPER_KEY_TYPE = 0xdc,
153 	/*
154 	 * The frame does not conforms to the incoming security level usage
155 	 * policy checking procedure.
156 	 */
157 	IEEE802154_IMPROPER_SECURITY_LEVEL = 0xdd,
158 	/* Secured frame received with an empty Frame Version field. */
159 	IEEE802154_UNSUPPORTED_LEGACY = 0xde,
160 	/*
161 	 * A secured frame is received or must be sent but security is not
162 	 * enabled in the device. Or, the Auxiliary Security Header has security
163 	 * level of zero in it.
164 	 */
165 	IEEE802154_UNSUPPORTED_SECURITY = 0xdf,
166 	/* The beacon was lost following a synchronization request. */
167 	IEEE802154_BEACON_LOST = 0xe0,
168 	/*
169 	 * A transmission could not take place due to activity on the
170 	 * channel, i.e., the CSMA-CA mechanism has failed.
171 	 */
172 	IEEE802154_CHANNEL_ACCESS_FAILURE = 0xe1,
173 	/* The GTS request has been denied by the PAN coordinator. */
174 	IEEE802154_DENIED = 0xe2,
175 	/* The attempt to disable the transceiver has failed. */
176 	IEEE802154_DISABLE_TRX_FAILURE = 0xe3,
177 	/*
178 	 * The received frame induces a failed security check according to
179 	 * the security suite.
180 	 */
181 	IEEE802154_FAILED_SECURITY_CHECK = 0xe4,
182 	/*
183 	 * The frame resulting from secure processing has a length that is
184 	 * greater than aMACMaxFrameSize.
185 	 */
186 	IEEE802154_FRAME_TOO_LONG = 0xe5,
187 	/*
188 	 * The requested GTS transmission failed because the specified GTS
189 	 * either did not have a transmit GTS direction or was not defined.
190 	 */
191 	IEEE802154_INVALID_GTS = 0xe6,
192 	/*
193 	 * A request to purge an MSDU from the transaction queue was made using
194 	 * an MSDU handle that was not found in the transaction table.
195 	 */
196 	IEEE802154_INVALID_HANDLE = 0xe7,
197 	/* A parameter in the primitive is out of the valid range.*/
198 	IEEE802154_INVALID_PARAMETER = 0xe8,
199 	/* No acknowledgment was received after aMaxFrameRetries. */
200 	IEEE802154_NO_ACK = 0xe9,
201 	/* A scan operation failed to find any network beacons.*/
202 	IEEE802154_NO_BEACON = 0xea,
203 	/* No response data were available following a request. */
204 	IEEE802154_NO_DATA = 0xeb,
205 	/* The operation failed because a short address was not allocated. */
206 	IEEE802154_NO_SHORT_ADDRESS = 0xec,
207 	/*
208 	 * A receiver enable request was unsuccessful because it could not be
209 	 * completed within the CAP.
210 	 */
211 	IEEE802154_OUT_OF_CAP = 0xed,
212 	/*
213 	 * A PAN identifier conflict has been detected and communicated to the
214 	 * PAN coordinator.
215 	 */
216 	IEEE802154_PAN_ID_CONFLICT = 0xee,
217 	/* A coordinator realignment command has been received. */
218 	IEEE802154_REALIGNMENT = 0xef,
219 	/* The transaction has expired and its information discarded. */
220 	IEEE802154_TRANSACTION_EXPIRED = 0xf0,
221 	/* There is no capacity to store the transaction. */
222 	IEEE802154_TRANSACTION_OVERFLOW = 0xf1,
223 	/*
224 	 * The transceiver was in the transmitter enabled state when the
225 	 * receiver was requested to be enabled.
226 	 */
227 	IEEE802154_TX_ACTIVE = 0xf2,
228 	/* The appropriate key is not available in the ACL. */
229 	IEEE802154_UNAVAILABLE_KEY = 0xf3,
230 	/*
231 	 * A SET/GET request was issued with the identifier of a PIB attribute
232 	 * that is not supported.
233 	 */
234 	IEEE802154_UNSUPPORTED_ATTRIBUTE = 0xf4,
235 	/* Missing source or destination address or address mode. */
236 	IEEE802154_INVALID_ADDRESS = 0xf5,
237 	/*
238 	 * MLME asked to turn the receiver on, but the on time duration is too
239 	 * big compared to the macBeaconOrder.
240 	 */
241 	IEEE802154_ON_TIME_TOO_LONG = 0xf6,
242 	/*
243 	 * MLME asaked to turn the receiver on, but the request was delayed for
244 	 * too long before getting processed.
245 	 */
246 	IEEE802154_PAST_TIME = 0xf7,
247 	/*
248 	 * The StartTime parameter is nonzero, and the MLME is not currently
249 	 * tracking the beacon of the coordinator through which it is
250 	 * associated.
251 	 */
252 	IEEE802154_TRACKING_OFF = 0xf8,
253 	/*
254 	 * The index inside the hierarchical values in PIBAttribute is out of
255 	 * range.
256 	 */
257 	IEEE802154_INVALID_INDEX = 0xf9,
258 	/*
259 	 * The number of PAN descriptors discovered during a scan has been
260 	 * reached.
261 	 */
262 	IEEE802154_LIMIT_REACHED = 0xfa,
263 	/*
264 	 * The PIBAttribute parameter specifies an attribute that is a read-only
265 	 * attribute.
266 	 */
267 	IEEE802154_READ_ONLY = 0xfb,
268 	/*
269 	 * A request to perform a scan operation failed because the MLME was
270 	 * in the process of performing a previously initiated scan operation.
271 	 */
272 	IEEE802154_SCAN_IN_PROGRESS = 0xfc,
273 	/* The outgoing superframe overlaps the incoming superframe. */
274 	IEEE802154_SUPERFRAME_OVERLAP = 0xfd,
275 	/* Any other error situation. */
276 	IEEE802154_SYSTEM_ERROR = 0xff,
277 };
278 
279 /* frame control handling */
280 #define IEEE802154_FCTL_FTYPE		0x0003
281 #define IEEE802154_FCTL_ACKREQ		0x0020
282 #define IEEE802154_FCTL_SECEN		0x0004
283 #define IEEE802154_FCTL_INTRA_PAN	0x0040
284 #define IEEE802154_FCTL_DADDR		0x0c00
285 #define IEEE802154_FCTL_SADDR		0xc000
286 
287 #define IEEE802154_FTYPE_DATA		0x0001
288 
289 #define IEEE802154_FCTL_ADDR_NONE	0x0000
290 #define IEEE802154_FCTL_DADDR_SHORT	0x0800
291 #define IEEE802154_FCTL_DADDR_EXTENDED	0x0c00
292 #define IEEE802154_FCTL_SADDR_SHORT	0x8000
293 #define IEEE802154_FCTL_SADDR_EXTENDED	0xc000
294 
295 /*
296  * ieee802154_is_data - check if type is IEEE802154_FTYPE_DATA
297  * @fc: frame control bytes in little-endian byteorder
298  */
ieee802154_is_data(__le16 fc)299 static inline int ieee802154_is_data(__le16 fc)
300 {
301 	return (fc & cpu_to_le16(IEEE802154_FCTL_FTYPE)) ==
302 		cpu_to_le16(IEEE802154_FTYPE_DATA);
303 }
304 
305 /**
306  * ieee802154_is_secen - check if Security bit is set
307  * @fc: frame control bytes in little-endian byteorder
308  */
ieee802154_is_secen(__le16 fc)309 static inline bool ieee802154_is_secen(__le16 fc)
310 {
311 	return fc & cpu_to_le16(IEEE802154_FCTL_SECEN);
312 }
313 
314 /**
315  * ieee802154_is_ackreq - check if acknowledgment request bit is set
316  * @fc: frame control bytes in little-endian byteorder
317  */
ieee802154_is_ackreq(__le16 fc)318 static inline bool ieee802154_is_ackreq(__le16 fc)
319 {
320 	return fc & cpu_to_le16(IEEE802154_FCTL_ACKREQ);
321 }
322 
323 /**
324  * ieee802154_is_intra_pan - check if intra pan id communication
325  * @fc: frame control bytes in little-endian byteorder
326  */
ieee802154_is_intra_pan(__le16 fc)327 static inline bool ieee802154_is_intra_pan(__le16 fc)
328 {
329 	return fc & cpu_to_le16(IEEE802154_FCTL_INTRA_PAN);
330 }
331 
332 /*
333  * ieee802154_daddr_mode - get daddr mode from fc
334  * @fc: frame control bytes in little-endian byteorder
335  */
ieee802154_daddr_mode(__le16 fc)336 static inline __le16 ieee802154_daddr_mode(__le16 fc)
337 {
338 	return fc & cpu_to_le16(IEEE802154_FCTL_DADDR);
339 }
340 
341 /*
342  * ieee802154_saddr_mode - get saddr mode from fc
343  * @fc: frame control bytes in little-endian byteorder
344  */
ieee802154_saddr_mode(__le16 fc)345 static inline __le16 ieee802154_saddr_mode(__le16 fc)
346 {
347 	return fc & cpu_to_le16(IEEE802154_FCTL_SADDR);
348 }
349 
350 /**
351  * ieee802154_is_valid_psdu_len - check if psdu len is valid
352  * available lengths:
353  *	0-4	Reserved
354  *	5	MPDU (Acknowledgment)
355  *	6-8	Reserved
356  *	9-127	MPDU
357  *
358  * @len: psdu len with (MHR + payload + MFR)
359  */
ieee802154_is_valid_psdu_len(u8 len)360 static inline bool ieee802154_is_valid_psdu_len(u8 len)
361 {
362 	return (len == IEEE802154_ACK_PSDU_LEN ||
363 		(len >= IEEE802154_MIN_PSDU_LEN && len <= IEEE802154_MTU));
364 }
365 
366 /**
367  * ieee802154_is_valid_extended_unicast_addr - check if extended addr is valid
368  * @addr: extended addr to check
369  */
ieee802154_is_valid_extended_unicast_addr(__le64 addr)370 static inline bool ieee802154_is_valid_extended_unicast_addr(__le64 addr)
371 {
372 	/* Bail out if the address is all zero, or if the group
373 	 * address bit is set.
374 	 */
375 	return ((addr != cpu_to_le64(0x0000000000000000ULL)) &&
376 		!(addr & cpu_to_le64(0x0100000000000000ULL)));
377 }
378 
379 /**
380  * ieee802154_is_broadcast_short_addr - check if short addr is broadcast
381  * @addr: short addr to check
382  */
ieee802154_is_broadcast_short_addr(__le16 addr)383 static inline bool ieee802154_is_broadcast_short_addr(__le16 addr)
384 {
385 	return (addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST));
386 }
387 
388 /**
389  * ieee802154_is_unspec_short_addr - check if short addr is unspecified
390  * @addr: short addr to check
391  */
ieee802154_is_unspec_short_addr(__le16 addr)392 static inline bool ieee802154_is_unspec_short_addr(__le16 addr)
393 {
394 	return (addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC));
395 }
396 
397 /**
398  * ieee802154_is_valid_src_short_addr - check if source short address is valid
399  * @addr: short addr to check
400  */
ieee802154_is_valid_src_short_addr(__le16 addr)401 static inline bool ieee802154_is_valid_src_short_addr(__le16 addr)
402 {
403 	return !(ieee802154_is_broadcast_short_addr(addr) ||
404 		 ieee802154_is_unspec_short_addr(addr));
405 }
406 
407 /**
408  * ieee802154_random_extended_addr - generates a random extended address
409  * @addr: extended addr pointer to place the random address
410  */
ieee802154_random_extended_addr(__le64 * addr)411 static inline void ieee802154_random_extended_addr(__le64 *addr)
412 {
413 	get_random_bytes(addr, IEEE802154_EXTENDED_ADDR_LEN);
414 
415 	/* clear the group bit, and set the locally administered bit */
416 	((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] &= ~0x01;
417 	((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] |= 0x02;
418 }
419 
420 #endif /* LINUX_IEEE802154_H */
421