blob: 95c831162212ec609b4b2172469723e5c4d4d299 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Sergey Lapin9ec76712009-06-08 12:18:48 +00002/*
3 * IEEE802.15.4-2003 specification
4 *
5 * Copyright (C) 2007, 2008 Siemens AG
6 *
Sergey Lapin9ec76712009-06-08 12:18:48 +00007 * 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>
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +000012 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
Sergey Lapin9ec76712009-06-08 12:18:48 +000013 */
14
Alexander Aring4ca24ac2014-10-25 09:41:04 +020015#ifndef LINUX_IEEE802154_H
16#define LINUX_IEEE802154_H
Sergey Lapin9ec76712009-06-08 12:18:48 +000017
Alexander Aringfa491002014-10-27 17:13:40 +010018#include <linux/types.h>
Alexander Aring35d5a372014-11-05 20:51:22 +010019#include <linux/random.h>
Alexander Aringfa491002014-10-27 17:13:40 +010020
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +000021#define IEEE802154_MTU 127
Alexander Aring306f7aa2015-02-11 14:39:15 +010022#define IEEE802154_ACK_PSDU_LEN 5
23#define IEEE802154_MIN_PSDU_LEN 9
Alexander Aring3f3c4bb2015-03-04 21:19:59 +010024#define IEEE802154_FCS_LEN 2
Alexander Aring87a93e42015-09-18 11:30:43 +020025#define IEEE802154_MAX_AUTH_TAG_LEN 16
Alexander Aring9cc577d2016-07-06 23:32:24 +020026#define IEEE802154_FC_LEN 2
27#define IEEE802154_SEQ_LEN 1
Alexander Aring87a93e42015-09-18 11:30:43 +020028
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)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +000038
Alexander Aring702bf372014-11-12 03:36:57 +010039#define IEEE802154_PAN_ID_BROADCAST 0xffff
Alexander Aring9830c622014-11-12 03:36:58 +010040#define IEEE802154_ADDR_SHORT_BROADCAST 0xffff
41#define IEEE802154_ADDR_SHORT_UNSPEC 0xfffe
Alexander Aring702bf372014-11-12 03:36:57 +010042
Alexander Aring1906bbb2014-11-05 20:51:21 +010043#define IEEE802154_EXTENDED_ADDR_LEN 8
Alexander Aring118a5cf2016-04-11 11:04:15 +020044#define IEEE802154_SHORT_ADDR_LEN 2
Alexander Aring19580cc2016-07-06 23:32:25 +020045#define IEEE802154_PAN_ID_LEN 2
Alexander Aring1906bbb2014-11-05 20:51:21 +010046
Alexander Aring61f2dcb2014-11-12 19:51:56 +010047#define IEEE802154_LIFS_PERIOD 40
48#define IEEE802154_SIFS_PERIOD 12
Alexander Aring3f3c4bb2015-03-04 21:19:59 +010049#define IEEE802154_MAX_SIFS_FRAME_SIZE 18
Alexander Aring61f2dcb2014-11-12 19:51:56 +010050
Alexander Aringcb41c8d2014-11-17 08:20:54 +010051#define IEEE802154_MAX_CHANNEL 26
52#define IEEE802154_MAX_PAGE 31
53
Sergey Lapin9ec76712009-06-08 12:18:48 +000054#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
Phoebe Buckheister94b4f6c2014-03-14 21:24:00 +010067#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)
Sergey Lapin9ec76712009-06-08 12:18:48 +000075
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
Phoebe Buckheister94b4f6c2014-03-14 21:24:00 +010081#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
Sergey Lapin9ec76712009-06-08 12:18:48 +000085#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
Phoebe Buckheister94b4f6c2014-03-14 21:24:00 +010091#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
Sergey Lapin9ec76712009-06-08 12:18:48 +0000103
Phoebe Buckheisterc3a61142014-05-14 17:43:06 +0200104#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
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000113/* MAC footer size */
114#define IEEE802154_MFR_SIZE 2 /* 2 octets */
115
Sergey Lapin9ec76712009-06-08 12:18:48 +0000116/* 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 */
130enum {
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
138 /* The beacon was lost following a synchronization request. */
139 IEEE802154_BEACON_LOSS = 0xe0,
140 /*
141 * A transmission could not take place due to activity on the
142 * channel, i.e., the CSMA-CA mechanism has failed.
143 */
144 IEEE802154_CHNL_ACCESS_FAIL = 0xe1,
145 /* The GTS request has been denied by the PAN coordinator. */
146 IEEE802154_DENINED = 0xe2,
147 /* The attempt to disable the transceiver has failed. */
148 IEEE802154_DISABLE_TRX_FAIL = 0xe3,
149 /*
150 * The received frame induces a failed security check according to
151 * the security suite.
152 */
153 IEEE802154_FAILED_SECURITY_CHECK = 0xe4,
154 /*
155 * The frame resulting from secure processing has a length that is
156 * greater than aMACMaxFrameSize.
157 */
158 IEEE802154_FRAME_TOO_LONG = 0xe5,
159 /*
160 * The requested GTS transmission failed because the specified GTS
161 * either did not have a transmit GTS direction or was not defined.
162 */
163 IEEE802154_INVALID_GTS = 0xe6,
164 /*
165 * A request to purge an MSDU from the transaction queue was made using
166 * an MSDU handle that was not found in the transaction table.
167 */
168 IEEE802154_INVALID_HANDLE = 0xe7,
169 /* A parameter in the primitive is out of the valid range.*/
170 IEEE802154_INVALID_PARAMETER = 0xe8,
171 /* No acknowledgment was received after aMaxFrameRetries. */
172 IEEE802154_NO_ACK = 0xe9,
173 /* A scan operation failed to find any network beacons.*/
174 IEEE802154_NO_BEACON = 0xea,
175 /* No response data were available following a request. */
176 IEEE802154_NO_DATA = 0xeb,
177 /* The operation failed because a short address was not allocated. */
178 IEEE802154_NO_SHORT_ADDRESS = 0xec,
179 /*
180 * A receiver enable request was unsuccessful because it could not be
181 * completed within the CAP.
182 */
183 IEEE802154_OUT_OF_CAP = 0xed,
184 /*
185 * A PAN identifier conflict has been detected and communicated to the
186 * PAN coordinator.
187 */
188 IEEE802154_PANID_CONFLICT = 0xee,
189 /* A coordinator realignment command has been received. */
190 IEEE802154_REALIGMENT = 0xef,
191 /* The transaction has expired and its information discarded. */
192 IEEE802154_TRANSACTION_EXPIRED = 0xf0,
193 /* There is no capacity to store the transaction. */
194 IEEE802154_TRANSACTION_OVERFLOW = 0xf1,
195 /*
196 * The transceiver was in the transmitter enabled state when the
197 * receiver was requested to be enabled.
198 */
199 IEEE802154_TX_ACTIVE = 0xf2,
200 /* The appropriate key is not available in the ACL. */
201 IEEE802154_UNAVAILABLE_KEY = 0xf3,
202 /*
203 * A SET/GET request was issued with the identifier of a PIB attribute
204 * that is not supported.
205 */
206 IEEE802154_UNSUPPORTED_ATTR = 0xf4,
207 /*
208 * A request to perform a scan operation failed because the MLME was
209 * in the process of performing a previously initiated scan operation.
210 */
211 IEEE802154_SCAN_IN_PROGRESS = 0xfc,
212};
213
Alexander Aring54552d02015-09-02 14:21:29 +0200214/* frame control handling */
215#define IEEE802154_FCTL_FTYPE 0x0003
Alexander Aring79750ac2015-09-21 11:24:33 +0200216#define IEEE802154_FCTL_ACKREQ 0x0020
Alexandre Macabiesbc405cd2016-04-12 18:53:00 +0200217#define IEEE802154_FCTL_SECEN 0x0004
Alexander Aring54552d02015-09-02 14:21:29 +0200218#define IEEE802154_FCTL_INTRA_PAN 0x0040
Alexander Aring9cc577d2016-07-06 23:32:24 +0200219#define IEEE802154_FCTL_DADDR 0x0c00
Alexander Aring19580cc2016-07-06 23:32:25 +0200220#define IEEE802154_FCTL_SADDR 0xc000
Alexander Aring54552d02015-09-02 14:21:29 +0200221
222#define IEEE802154_FTYPE_DATA 0x0001
223
Alexander Aring9cc577d2016-07-06 23:32:24 +0200224#define IEEE802154_FCTL_ADDR_NONE 0x0000
225#define IEEE802154_FCTL_DADDR_SHORT 0x0800
226#define IEEE802154_FCTL_DADDR_EXTENDED 0x0c00
Alexander Aring19580cc2016-07-06 23:32:25 +0200227#define IEEE802154_FCTL_SADDR_SHORT 0x8000
228#define IEEE802154_FCTL_SADDR_EXTENDED 0xc000
Alexander Aring9cc577d2016-07-06 23:32:24 +0200229
Alexander Aring54552d02015-09-02 14:21:29 +0200230/*
231 * ieee802154_is_data - check if type is IEEE802154_FTYPE_DATA
232 * @fc: frame control bytes in little-endian byteorder
233 */
234static inline int ieee802154_is_data(__le16 fc)
235{
236 return (fc & cpu_to_le16(IEEE802154_FCTL_FTYPE)) ==
237 cpu_to_le16(IEEE802154_FTYPE_DATA);
238}
239
240/**
Alexandre Macabiesbc405cd2016-04-12 18:53:00 +0200241 * ieee802154_is_secen - check if Security bit is set
242 * @fc: frame control bytes in little-endian byteorder
243 */
244static inline bool ieee802154_is_secen(__le16 fc)
245{
246 return fc & cpu_to_le16(IEEE802154_FCTL_SECEN);
247}
248
249/**
Alexander Aring79750ac2015-09-21 11:24:33 +0200250 * ieee802154_is_ackreq - check if acknowledgment request bit is set
251 * @fc: frame control bytes in little-endian byteorder
252 */
253static inline bool ieee802154_is_ackreq(__le16 fc)
254{
255 return fc & cpu_to_le16(IEEE802154_FCTL_ACKREQ);
256}
257
258/**
Alexander Aring54552d02015-09-02 14:21:29 +0200259 * ieee802154_is_intra_pan - check if intra pan id communication
260 * @fc: frame control bytes in little-endian byteorder
261 */
262static inline bool ieee802154_is_intra_pan(__le16 fc)
263{
264 return fc & cpu_to_le16(IEEE802154_FCTL_INTRA_PAN);
265}
266
Alexander Aring9cc577d2016-07-06 23:32:24 +0200267/*
268 * ieee802154_daddr_mode - get daddr mode from fc
269 * @fc: frame control bytes in little-endian byteorder
270 */
271static inline __le16 ieee802154_daddr_mode(__le16 fc)
272{
273 return fc & cpu_to_le16(IEEE802154_FCTL_DADDR);
274}
275
Alexander Aring19580cc2016-07-06 23:32:25 +0200276/*
277 * ieee802154_saddr_mode - get saddr mode from fc
278 * @fc: frame control bytes in little-endian byteorder
279 */
280static inline __le16 ieee802154_saddr_mode(__le16 fc)
281{
282 return fc & cpu_to_le16(IEEE802154_FCTL_SADDR);
283}
284
Alexander Aringfa491002014-10-27 17:13:40 +0100285/**
286 * ieee802154_is_valid_psdu_len - check if psdu len is valid
Alexander Aring306f7aa2015-02-11 14:39:15 +0100287 * available lengths:
288 * 0-4 Reserved
289 * 5 MPDU (Acknowledgment)
290 * 6-8 Reserved
291 * 9-127 MPDU
292 *
Alexander Aringfa491002014-10-27 17:13:40 +0100293 * @len: psdu len with (MHR + payload + MFR)
294 */
Alexander Aringb7594142016-04-11 11:04:14 +0200295static inline bool ieee802154_is_valid_psdu_len(u8 len)
Alexander Aringfa491002014-10-27 17:13:40 +0100296{
Alexander Aring306f7aa2015-02-11 14:39:15 +0100297 return (len == IEEE802154_ACK_PSDU_LEN ||
298 (len >= IEEE802154_MIN_PSDU_LEN && len <= IEEE802154_MTU));
Alexander Aringfa491002014-10-27 17:13:40 +0100299}
Sergey Lapin9ec76712009-06-08 12:18:48 +0000300
Alexander Aringcb904b02014-11-02 04:18:45 +0100301/**
Alexander Aringb7594142016-04-11 11:04:14 +0200302 * ieee802154_is_valid_extended_unicast_addr - check if extended addr is valid
Alexander Aringcb904b02014-11-02 04:18:45 +0100303 * @addr: extended addr to check
304 */
Alexander Aringb7594142016-04-11 11:04:14 +0200305static inline bool ieee802154_is_valid_extended_unicast_addr(__le64 addr)
Alexander Aringcb904b02014-11-02 04:18:45 +0100306{
Lennert Buytenhekdaf4e2c2015-05-28 15:38:43 +0300307 /* Bail out if the address is all zero, or if the group
308 * address bit is set.
Alexander Aringcb904b02014-11-02 04:18:45 +0100309 */
Dan Carpenter0d8a52f2014-11-04 11:55:09 +0300310 return ((addr != cpu_to_le64(0x0000000000000000ULL)) &&
Lennert Buytenhekdaf4e2c2015-05-28 15:38:43 +0300311 !(addr & cpu_to_le64(0x0100000000000000ULL)));
Alexander Aringcb904b02014-11-02 04:18:45 +0100312}
313
Alexander Aring35d5a372014-11-05 20:51:22 +0100314/**
Alexander Aring118a5cf2016-04-11 11:04:15 +0200315 * ieee802154_is_broadcast_short_addr - check if short addr is broadcast
316 * @addr: short addr to check
317 */
318static inline bool ieee802154_is_broadcast_short_addr(__le16 addr)
319{
320 return (addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST));
321}
322
323/**
324 * ieee802154_is_unspec_short_addr - check if short addr is unspecified
325 * @addr: short addr to check
326 */
327static inline bool ieee802154_is_unspec_short_addr(__le16 addr)
328{
329 return (addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC));
330}
331
332/**
333 * ieee802154_is_valid_src_short_addr - check if source short address is valid
334 * @addr: short addr to check
335 */
336static inline bool ieee802154_is_valid_src_short_addr(__le16 addr)
337{
338 return !(ieee802154_is_broadcast_short_addr(addr) ||
339 ieee802154_is_unspec_short_addr(addr));
340}
341
342/**
Alexander Aring35d5a372014-11-05 20:51:22 +0100343 * ieee802154_random_extended_addr - generates a random extended address
344 * @addr: extended addr pointer to place the random address
345 */
346static inline void ieee802154_random_extended_addr(__le64 *addr)
347{
348 get_random_bytes(addr, IEEE802154_EXTENDED_ADDR_LEN);
349
Lennert Buytenhek3b369bd2015-05-28 15:38:32 +0300350 /* clear the group bit, and set the locally administered bit */
351 ((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] &= ~0x01;
352 ((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] |= 0x02;
Alexander Aring35d5a372014-11-05 20:51:22 +0100353}
354
Alexander Aring4ca24ac2014-10-25 09:41:04 +0200355#endif /* LINUX_IEEE802154_H */