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