xref: /linux-6.15/include/linux/ieee802154.h (revision 60f9e37a)
1 /*
2  * IEEE802.15.4-2003 specification
3  *
4  * Copyright (C) 2007, 2008 Siemens AG
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * Written by:
16  * Pavel Smolenskiy <[email protected]>
17  * Maxim Gorbachyov <[email protected]>
18  * Maxim Osipov <[email protected]>
19  * Dmitry Eremin-Solenikov <[email protected]>
20  * Alexander Smirnov <[email protected]>
21  */
22 
23 #ifndef LINUX_IEEE802154_H
24 #define LINUX_IEEE802154_H
25 
26 #include <linux/types.h>
27 #include <linux/random.h>
28 #include <asm/byteorder.h>
29 
30 #define IEEE802154_MTU			127
31 #define IEEE802154_MIN_PSDU_LEN		5
32 
33 #define IEEE802154_PAN_ID_BROADCAST	0xffff
34 #define IEEE802154_ADDR_SHORT_BROADCAST	0xffff
35 #define IEEE802154_ADDR_SHORT_UNSPEC	0xfffe
36 
37 #define IEEE802154_EXTENDED_ADDR_LEN	8
38 
39 #define IEEE802154_LIFS_PERIOD		40
40 #define IEEE802154_SIFS_PERIOD		12
41 
42 #define IEEE802154_MAX_CHANNEL		26
43 #define IEEE802154_MAX_PAGE		31
44 
45 #define IEEE802154_FC_TYPE_BEACON	0x0	/* Frame is beacon */
46 #define	IEEE802154_FC_TYPE_DATA		0x1	/* Frame is data */
47 #define IEEE802154_FC_TYPE_ACK		0x2	/* Frame is acknowledgment */
48 #define IEEE802154_FC_TYPE_MAC_CMD	0x3	/* Frame is MAC command */
49 
50 #define IEEE802154_FC_TYPE_SHIFT		0
51 #define IEEE802154_FC_TYPE_MASK		((1 << 3) - 1)
52 #define IEEE802154_FC_TYPE(x)		((x & IEEE802154_FC_TYPE_MASK) >> IEEE802154_FC_TYPE_SHIFT)
53 #define IEEE802154_FC_SET_TYPE(v, x)	do {	\
54 	v = (((v) & ~IEEE802154_FC_TYPE_MASK) | \
55 	    (((x) << IEEE802154_FC_TYPE_SHIFT) & IEEE802154_FC_TYPE_MASK)); \
56 	} while (0)
57 
58 #define IEEE802154_FC_SECEN_SHIFT	3
59 #define IEEE802154_FC_SECEN		(1 << IEEE802154_FC_SECEN_SHIFT)
60 #define IEEE802154_FC_FRPEND_SHIFT	4
61 #define IEEE802154_FC_FRPEND		(1 << IEEE802154_FC_FRPEND_SHIFT)
62 #define IEEE802154_FC_ACK_REQ_SHIFT	5
63 #define IEEE802154_FC_ACK_REQ		(1 << IEEE802154_FC_ACK_REQ_SHIFT)
64 #define IEEE802154_FC_INTRA_PAN_SHIFT	6
65 #define IEEE802154_FC_INTRA_PAN		(1 << IEEE802154_FC_INTRA_PAN_SHIFT)
66 
67 #define IEEE802154_FC_SAMODE_SHIFT	14
68 #define IEEE802154_FC_SAMODE_MASK	(3 << IEEE802154_FC_SAMODE_SHIFT)
69 #define IEEE802154_FC_DAMODE_SHIFT	10
70 #define IEEE802154_FC_DAMODE_MASK	(3 << IEEE802154_FC_DAMODE_SHIFT)
71 
72 #define IEEE802154_FC_VERSION_SHIFT	12
73 #define IEEE802154_FC_VERSION_MASK	(3 << IEEE802154_FC_VERSION_SHIFT)
74 #define IEEE802154_FC_VERSION(x)	((x & IEEE802154_FC_VERSION_MASK) >> IEEE802154_FC_VERSION_SHIFT)
75 
76 #define IEEE802154_FC_SAMODE(x)		\
77 	(((x) & IEEE802154_FC_SAMODE_MASK) >> IEEE802154_FC_SAMODE_SHIFT)
78 
79 #define IEEE802154_FC_DAMODE(x)		\
80 	(((x) & IEEE802154_FC_DAMODE_MASK) >> IEEE802154_FC_DAMODE_SHIFT)
81 
82 #define IEEE802154_SCF_SECLEVEL_MASK		7
83 #define IEEE802154_SCF_SECLEVEL_SHIFT		0
84 #define IEEE802154_SCF_SECLEVEL(x)		(x & IEEE802154_SCF_SECLEVEL_MASK)
85 #define IEEE802154_SCF_KEY_ID_MODE_SHIFT	3
86 #define IEEE802154_SCF_KEY_ID_MODE_MASK		(3 << IEEE802154_SCF_KEY_ID_MODE_SHIFT)
87 #define IEEE802154_SCF_KEY_ID_MODE(x)		\
88 	((x & IEEE802154_SCF_KEY_ID_MODE_MASK) >> IEEE802154_SCF_KEY_ID_MODE_SHIFT)
89 
90 #define IEEE802154_SCF_KEY_IMPLICIT		0
91 #define IEEE802154_SCF_KEY_INDEX		1
92 #define IEEE802154_SCF_KEY_SHORT_INDEX		2
93 #define IEEE802154_SCF_KEY_HW_INDEX		3
94 
95 #define IEEE802154_SCF_SECLEVEL_NONE		0
96 #define IEEE802154_SCF_SECLEVEL_MIC32		1
97 #define IEEE802154_SCF_SECLEVEL_MIC64		2
98 #define IEEE802154_SCF_SECLEVEL_MIC128		3
99 #define IEEE802154_SCF_SECLEVEL_ENC		4
100 #define IEEE802154_SCF_SECLEVEL_ENC_MIC32	5
101 #define IEEE802154_SCF_SECLEVEL_ENC_MIC64	6
102 #define IEEE802154_SCF_SECLEVEL_ENC_MIC128	7
103 
104 /* MAC footer size */
105 #define IEEE802154_MFR_SIZE	2 /* 2 octets */
106 
107 /* MAC's Command Frames Identifiers */
108 #define IEEE802154_CMD_ASSOCIATION_REQ		0x01
109 #define IEEE802154_CMD_ASSOCIATION_RESP		0x02
110 #define IEEE802154_CMD_DISASSOCIATION_NOTIFY	0x03
111 #define IEEE802154_CMD_DATA_REQ			0x04
112 #define IEEE802154_CMD_PANID_CONFLICT_NOTIFY	0x05
113 #define IEEE802154_CMD_ORPHAN_NOTIFY		0x06
114 #define IEEE802154_CMD_BEACON_REQ		0x07
115 #define IEEE802154_CMD_COORD_REALIGN_NOTIFY	0x08
116 #define IEEE802154_CMD_GTS_REQ			0x09
117 
118 /*
119  * The return values of MAC operations
120  */
121 enum {
122 	/*
123 	 * The requested operation was completed successfully.
124 	 * For a transmission request, this value indicates
125 	 * a successful transmission.
126 	 */
127 	IEEE802154_SUCCESS = 0x0,
128 
129 	/* The beacon was lost following a synchronization request. */
130 	IEEE802154_BEACON_LOSS = 0xe0,
131 	/*
132 	 * A transmission could not take place due to activity on the
133 	 * channel, i.e., the CSMA-CA mechanism has failed.
134 	 */
135 	IEEE802154_CHNL_ACCESS_FAIL = 0xe1,
136 	/* The GTS request has been denied by the PAN coordinator. */
137 	IEEE802154_DENINED = 0xe2,
138 	/* The attempt to disable the transceiver has failed. */
139 	IEEE802154_DISABLE_TRX_FAIL = 0xe3,
140 	/*
141 	 * The received frame induces a failed security check according to
142 	 * the security suite.
143 	 */
144 	IEEE802154_FAILED_SECURITY_CHECK = 0xe4,
145 	/*
146 	 * The frame resulting from secure processing has a length that is
147 	 * greater than aMACMaxFrameSize.
148 	 */
149 	IEEE802154_FRAME_TOO_LONG = 0xe5,
150 	/*
151 	 * The requested GTS transmission failed because the specified GTS
152 	 * either did not have a transmit GTS direction or was not defined.
153 	 */
154 	IEEE802154_INVALID_GTS = 0xe6,
155 	/*
156 	 * A request to purge an MSDU from the transaction queue was made using
157 	 * an MSDU handle that was not found in the transaction table.
158 	 */
159 	IEEE802154_INVALID_HANDLE = 0xe7,
160 	/* A parameter in the primitive is out of the valid range.*/
161 	IEEE802154_INVALID_PARAMETER = 0xe8,
162 	/* No acknowledgment was received after aMaxFrameRetries. */
163 	IEEE802154_NO_ACK = 0xe9,
164 	/* A scan operation failed to find any network beacons.*/
165 	IEEE802154_NO_BEACON = 0xea,
166 	/* No response data were available following a request. */
167 	IEEE802154_NO_DATA = 0xeb,
168 	/* The operation failed because a short address was not allocated. */
169 	IEEE802154_NO_SHORT_ADDRESS = 0xec,
170 	/*
171 	 * A receiver enable request was unsuccessful because it could not be
172 	 * completed within the CAP.
173 	 */
174 	IEEE802154_OUT_OF_CAP = 0xed,
175 	/*
176 	 * A PAN identifier conflict has been detected and communicated to the
177 	 * PAN coordinator.
178 	 */
179 	IEEE802154_PANID_CONFLICT = 0xee,
180 	/* A coordinator realignment command has been received. */
181 	IEEE802154_REALIGMENT = 0xef,
182 	/* The transaction has expired and its information discarded. */
183 	IEEE802154_TRANSACTION_EXPIRED = 0xf0,
184 	/* There is no capacity to store the transaction. */
185 	IEEE802154_TRANSACTION_OVERFLOW = 0xf1,
186 	/*
187 	 * The transceiver was in the transmitter enabled state when the
188 	 * receiver was requested to be enabled.
189 	 */
190 	IEEE802154_TX_ACTIVE = 0xf2,
191 	/* The appropriate key is not available in the ACL. */
192 	IEEE802154_UNAVAILABLE_KEY = 0xf3,
193 	/*
194 	 * A SET/GET request was issued with the identifier of a PIB attribute
195 	 * that is not supported.
196 	 */
197 	IEEE802154_UNSUPPORTED_ATTR = 0xf4,
198 	/*
199 	 * A request to perform a scan operation failed because the MLME was
200 	 * in the process of performing a previously initiated scan operation.
201 	 */
202 	IEEE802154_SCAN_IN_PROGRESS = 0xfc,
203 };
204 
205 /**
206  * ieee802154_is_valid_psdu_len - check if psdu len is valid
207  * @len: psdu len with (MHR + payload + MFR)
208  */
209 static inline bool ieee802154_is_valid_psdu_len(const u8 len)
210 {
211 	return (len >= IEEE802154_MIN_PSDU_LEN && len <= IEEE802154_MTU);
212 }
213 
214 /**
215  * ieee802154_is_valid_psdu_len - check if extended addr is valid
216  * @addr: extended addr to check
217  */
218 static inline bool ieee802154_is_valid_extended_addr(const __le64 addr)
219 {
220 	/* These EUI-64 addresses are reserved by IEEE. 0xffffffffffffffff
221 	 * is used internally as extended to short address broadcast mapping.
222 	 * This is currently a workaround because neighbor discovery can't
223 	 * deal with short addresses types right now.
224 	 */
225 	return ((addr != cpu_to_le64(0x0000000000000000ULL)) &&
226 		(addr != cpu_to_le64(0xffffffffffffffffULL)));
227 }
228 
229 /**
230  * ieee802154_random_extended_addr - generates a random extended address
231  * @addr: extended addr pointer to place the random address
232  */
233 static inline void ieee802154_random_extended_addr(__le64 *addr)
234 {
235 	get_random_bytes(addr, IEEE802154_EXTENDED_ADDR_LEN);
236 
237 	/* toggle some bit if we hit an invalid extended addr */
238 	if (!ieee802154_is_valid_extended_addr(*addr))
239 		((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] ^= 0x01;
240 }
241 
242 #endif /* LINUX_IEEE802154_H */
243