Commit | Line | Data |
---|---|---|
9ec76716 SL |
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 | * | |
9ec76716 SL |
15 | * Written by: |
16 | * Pavel Smolenskiy <pavel.smolenskiy@gmail.com> | |
17 | * Maxim Gorbachyov <maxim.gorbachev@siemens.com> | |
18 | * Maxim Osipov <maxim.osipov@siemens.com> | |
19 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> | |
719269af | 20 | * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> |
9ec76716 SL |
21 | */ |
22 | ||
4ca24aca AA |
23 | #ifndef LINUX_IEEE802154_H |
24 | #define LINUX_IEEE802154_H | |
9ec76716 | 25 | |
fa491001 | 26 | #include <linux/types.h> |
35d5a374 | 27 | #include <linux/random.h> |
c28bee84 | 28 | #include <asm/byteorder.h> |
fa491001 | 29 | |
719269af | 30 | #define IEEE802154_MTU 127 |
306f7aa1 AA |
31 | #define IEEE802154_ACK_PSDU_LEN 5 |
32 | #define IEEE802154_MIN_PSDU_LEN 9 | |
3f3c4bb5 | 33 | #define IEEE802154_FCS_LEN 2 |
87a93e4e AA |
34 | #define IEEE802154_MAX_AUTH_TAG_LEN 16 |
35 | ||
36 | /* General MAC frame format: | |
37 | * 2 bytes: Frame Control | |
38 | * 1 byte: Sequence Number | |
39 | * 20 bytes: Addressing fields | |
40 | * 14 bytes: Auxiliary Security Header | |
41 | */ | |
42 | #define IEEE802154_MAX_HEADER_LEN (2 + 1 + 20 + 14) | |
43 | #define IEEE802154_MIN_HEADER_LEN (IEEE802154_ACK_PSDU_LEN - \ | |
44 | IEEE802154_FCS_LEN) | |
719269af | 45 | |
702bf371 | 46 | #define IEEE802154_PAN_ID_BROADCAST 0xffff |
9830c62a AA |
47 | #define IEEE802154_ADDR_SHORT_BROADCAST 0xffff |
48 | #define IEEE802154_ADDR_SHORT_UNSPEC 0xfffe | |
702bf371 | 49 | |
1906bbbd AA |
50 | #define IEEE802154_EXTENDED_ADDR_LEN 8 |
51 | ||
61f2dcba AA |
52 | #define IEEE802154_LIFS_PERIOD 40 |
53 | #define IEEE802154_SIFS_PERIOD 12 | |
3f3c4bb5 | 54 | #define IEEE802154_MAX_SIFS_FRAME_SIZE 18 |
61f2dcba | 55 | |
cb41c8dd AA |
56 | #define IEEE802154_MAX_CHANNEL 26 |
57 | #define IEEE802154_MAX_PAGE 31 | |
58 | ||
9ec76716 SL |
59 | #define IEEE802154_FC_TYPE_BEACON 0x0 /* Frame is beacon */ |
60 | #define IEEE802154_FC_TYPE_DATA 0x1 /* Frame is data */ | |
61 | #define IEEE802154_FC_TYPE_ACK 0x2 /* Frame is acknowledgment */ | |
62 | #define IEEE802154_FC_TYPE_MAC_CMD 0x3 /* Frame is MAC command */ | |
63 | ||
64 | #define IEEE802154_FC_TYPE_SHIFT 0 | |
65 | #define IEEE802154_FC_TYPE_MASK ((1 << 3) - 1) | |
66 | #define IEEE802154_FC_TYPE(x) ((x & IEEE802154_FC_TYPE_MASK) >> IEEE802154_FC_TYPE_SHIFT) | |
67 | #define IEEE802154_FC_SET_TYPE(v, x) do { \ | |
68 | v = (((v) & ~IEEE802154_FC_TYPE_MASK) | \ | |
69 | (((x) << IEEE802154_FC_TYPE_SHIFT) & IEEE802154_FC_TYPE_MASK)); \ | |
70 | } while (0) | |
71 | ||
94b4f6c2 PB |
72 | #define IEEE802154_FC_SECEN_SHIFT 3 |
73 | #define IEEE802154_FC_SECEN (1 << IEEE802154_FC_SECEN_SHIFT) | |
74 | #define IEEE802154_FC_FRPEND_SHIFT 4 | |
75 | #define IEEE802154_FC_FRPEND (1 << IEEE802154_FC_FRPEND_SHIFT) | |
76 | #define IEEE802154_FC_ACK_REQ_SHIFT 5 | |
77 | #define IEEE802154_FC_ACK_REQ (1 << IEEE802154_FC_ACK_REQ_SHIFT) | |
78 | #define IEEE802154_FC_INTRA_PAN_SHIFT 6 | |
79 | #define IEEE802154_FC_INTRA_PAN (1 << IEEE802154_FC_INTRA_PAN_SHIFT) | |
9ec76716 SL |
80 | |
81 | #define IEEE802154_FC_SAMODE_SHIFT 14 | |
82 | #define IEEE802154_FC_SAMODE_MASK (3 << IEEE802154_FC_SAMODE_SHIFT) | |
83 | #define IEEE802154_FC_DAMODE_SHIFT 10 | |
84 | #define IEEE802154_FC_DAMODE_MASK (3 << IEEE802154_FC_DAMODE_SHIFT) | |
85 | ||
94b4f6c2 PB |
86 | #define IEEE802154_FC_VERSION_SHIFT 12 |
87 | #define IEEE802154_FC_VERSION_MASK (3 << IEEE802154_FC_VERSION_SHIFT) | |
88 | #define IEEE802154_FC_VERSION(x) ((x & IEEE802154_FC_VERSION_MASK) >> IEEE802154_FC_VERSION_SHIFT) | |
89 | ||
9ec76716 SL |
90 | #define IEEE802154_FC_SAMODE(x) \ |
91 | (((x) & IEEE802154_FC_SAMODE_MASK) >> IEEE802154_FC_SAMODE_SHIFT) | |
92 | ||
93 | #define IEEE802154_FC_DAMODE(x) \ | |
94 | (((x) & IEEE802154_FC_DAMODE_MASK) >> IEEE802154_FC_DAMODE_SHIFT) | |
95 | ||
94b4f6c2 PB |
96 | #define IEEE802154_SCF_SECLEVEL_MASK 7 |
97 | #define IEEE802154_SCF_SECLEVEL_SHIFT 0 | |
98 | #define IEEE802154_SCF_SECLEVEL(x) (x & IEEE802154_SCF_SECLEVEL_MASK) | |
99 | #define IEEE802154_SCF_KEY_ID_MODE_SHIFT 3 | |
100 | #define IEEE802154_SCF_KEY_ID_MODE_MASK (3 << IEEE802154_SCF_KEY_ID_MODE_SHIFT) | |
101 | #define IEEE802154_SCF_KEY_ID_MODE(x) \ | |
102 | ((x & IEEE802154_SCF_KEY_ID_MODE_MASK) >> IEEE802154_SCF_KEY_ID_MODE_SHIFT) | |
103 | ||
104 | #define IEEE802154_SCF_KEY_IMPLICIT 0 | |
105 | #define IEEE802154_SCF_KEY_INDEX 1 | |
106 | #define IEEE802154_SCF_KEY_SHORT_INDEX 2 | |
107 | #define IEEE802154_SCF_KEY_HW_INDEX 3 | |
9ec76716 | 108 | |
c3a6114f PB |
109 | #define IEEE802154_SCF_SECLEVEL_NONE 0 |
110 | #define IEEE802154_SCF_SECLEVEL_MIC32 1 | |
111 | #define IEEE802154_SCF_SECLEVEL_MIC64 2 | |
112 | #define IEEE802154_SCF_SECLEVEL_MIC128 3 | |
113 | #define IEEE802154_SCF_SECLEVEL_ENC 4 | |
114 | #define IEEE802154_SCF_SECLEVEL_ENC_MIC32 5 | |
115 | #define IEEE802154_SCF_SECLEVEL_ENC_MIC64 6 | |
116 | #define IEEE802154_SCF_SECLEVEL_ENC_MIC128 7 | |
117 | ||
719269af | 118 | /* MAC footer size */ |
119 | #define IEEE802154_MFR_SIZE 2 /* 2 octets */ | |
120 | ||
9ec76716 SL |
121 | /* MAC's Command Frames Identifiers */ |
122 | #define IEEE802154_CMD_ASSOCIATION_REQ 0x01 | |
123 | #define IEEE802154_CMD_ASSOCIATION_RESP 0x02 | |
124 | #define IEEE802154_CMD_DISASSOCIATION_NOTIFY 0x03 | |
125 | #define IEEE802154_CMD_DATA_REQ 0x04 | |
126 | #define IEEE802154_CMD_PANID_CONFLICT_NOTIFY 0x05 | |
127 | #define IEEE802154_CMD_ORPHAN_NOTIFY 0x06 | |
128 | #define IEEE802154_CMD_BEACON_REQ 0x07 | |
129 | #define IEEE802154_CMD_COORD_REALIGN_NOTIFY 0x08 | |
130 | #define IEEE802154_CMD_GTS_REQ 0x09 | |
131 | ||
132 | /* | |
133 | * The return values of MAC operations | |
134 | */ | |
135 | enum { | |
136 | /* | |
137 | * The requested operation was completed successfully. | |
138 | * For a transmission request, this value indicates | |
139 | * a successful transmission. | |
140 | */ | |
141 | IEEE802154_SUCCESS = 0x0, | |
142 | ||
143 | /* The beacon was lost following a synchronization request. */ | |
144 | IEEE802154_BEACON_LOSS = 0xe0, | |
145 | /* | |
146 | * A transmission could not take place due to activity on the | |
147 | * channel, i.e., the CSMA-CA mechanism has failed. | |
148 | */ | |
149 | IEEE802154_CHNL_ACCESS_FAIL = 0xe1, | |
150 | /* The GTS request has been denied by the PAN coordinator. */ | |
151 | IEEE802154_DENINED = 0xe2, | |
152 | /* The attempt to disable the transceiver has failed. */ | |
153 | IEEE802154_DISABLE_TRX_FAIL = 0xe3, | |
154 | /* | |
155 | * The received frame induces a failed security check according to | |
156 | * the security suite. | |
157 | */ | |
158 | IEEE802154_FAILED_SECURITY_CHECK = 0xe4, | |
159 | /* | |
160 | * The frame resulting from secure processing has a length that is | |
161 | * greater than aMACMaxFrameSize. | |
162 | */ | |
163 | IEEE802154_FRAME_TOO_LONG = 0xe5, | |
164 | /* | |
165 | * The requested GTS transmission failed because the specified GTS | |
166 | * either did not have a transmit GTS direction or was not defined. | |
167 | */ | |
168 | IEEE802154_INVALID_GTS = 0xe6, | |
169 | /* | |
170 | * A request to purge an MSDU from the transaction queue was made using | |
171 | * an MSDU handle that was not found in the transaction table. | |
172 | */ | |
173 | IEEE802154_INVALID_HANDLE = 0xe7, | |
174 | /* A parameter in the primitive is out of the valid range.*/ | |
175 | IEEE802154_INVALID_PARAMETER = 0xe8, | |
176 | /* No acknowledgment was received after aMaxFrameRetries. */ | |
177 | IEEE802154_NO_ACK = 0xe9, | |
178 | /* A scan operation failed to find any network beacons.*/ | |
179 | IEEE802154_NO_BEACON = 0xea, | |
180 | /* No response data were available following a request. */ | |
181 | IEEE802154_NO_DATA = 0xeb, | |
182 | /* The operation failed because a short address was not allocated. */ | |
183 | IEEE802154_NO_SHORT_ADDRESS = 0xec, | |
184 | /* | |
185 | * A receiver enable request was unsuccessful because it could not be | |
186 | * completed within the CAP. | |
187 | */ | |
188 | IEEE802154_OUT_OF_CAP = 0xed, | |
189 | /* | |
190 | * A PAN identifier conflict has been detected and communicated to the | |
191 | * PAN coordinator. | |
192 | */ | |
193 | IEEE802154_PANID_CONFLICT = 0xee, | |
194 | /* A coordinator realignment command has been received. */ | |
195 | IEEE802154_REALIGMENT = 0xef, | |
196 | /* The transaction has expired and its information discarded. */ | |
197 | IEEE802154_TRANSACTION_EXPIRED = 0xf0, | |
198 | /* There is no capacity to store the transaction. */ | |
199 | IEEE802154_TRANSACTION_OVERFLOW = 0xf1, | |
200 | /* | |
201 | * The transceiver was in the transmitter enabled state when the | |
202 | * receiver was requested to be enabled. | |
203 | */ | |
204 | IEEE802154_TX_ACTIVE = 0xf2, | |
205 | /* The appropriate key is not available in the ACL. */ | |
206 | IEEE802154_UNAVAILABLE_KEY = 0xf3, | |
207 | /* | |
208 | * A SET/GET request was issued with the identifier of a PIB attribute | |
209 | * that is not supported. | |
210 | */ | |
211 | IEEE802154_UNSUPPORTED_ATTR = 0xf4, | |
212 | /* | |
213 | * A request to perform a scan operation failed because the MLME was | |
214 | * in the process of performing a previously initiated scan operation. | |
215 | */ | |
216 | IEEE802154_SCAN_IN_PROGRESS = 0xfc, | |
217 | }; | |
218 | ||
54552d03 AA |
219 | /* frame control handling */ |
220 | #define IEEE802154_FCTL_FTYPE 0x0003 | |
221 | #define IEEE802154_FCTL_INTRA_PAN 0x0040 | |
222 | ||
223 | #define IEEE802154_FTYPE_DATA 0x0001 | |
224 | ||
225 | /* | |
226 | * ieee802154_is_data - check if type is IEEE802154_FTYPE_DATA | |
227 | * @fc: frame control bytes in little-endian byteorder | |
228 | */ | |
229 | static inline int ieee802154_is_data(__le16 fc) | |
230 | { | |
231 | return (fc & cpu_to_le16(IEEE802154_FCTL_FTYPE)) == | |
232 | cpu_to_le16(IEEE802154_FTYPE_DATA); | |
233 | } | |
234 | ||
235 | /** | |
236 | * ieee802154_is_intra_pan - check if intra pan id communication | |
237 | * @fc: frame control bytes in little-endian byteorder | |
238 | */ | |
239 | static inline bool ieee802154_is_intra_pan(__le16 fc) | |
240 | { | |
241 | return fc & cpu_to_le16(IEEE802154_FCTL_INTRA_PAN); | |
242 | } | |
243 | ||
fa491001 AA |
244 | /** |
245 | * ieee802154_is_valid_psdu_len - check if psdu len is valid | |
306f7aa1 AA |
246 | * available lengths: |
247 | * 0-4 Reserved | |
248 | * 5 MPDU (Acknowledgment) | |
249 | * 6-8 Reserved | |
250 | * 9-127 MPDU | |
251 | * | |
fa491001 AA |
252 | * @len: psdu len with (MHR + payload + MFR) |
253 | */ | |
254 | static inline bool ieee802154_is_valid_psdu_len(const u8 len) | |
255 | { | |
306f7aa1 AA |
256 | return (len == IEEE802154_ACK_PSDU_LEN || |
257 | (len >= IEEE802154_MIN_PSDU_LEN && len <= IEEE802154_MTU)); | |
fa491001 | 258 | } |
9ec76716 | 259 | |
cb904b0a AA |
260 | /** |
261 | * ieee802154_is_valid_psdu_len - check if extended addr is valid | |
262 | * @addr: extended addr to check | |
263 | */ | |
daf4e2c8 | 264 | static inline bool ieee802154_is_valid_extended_unicast_addr(const __le64 addr) |
cb904b0a | 265 | { |
daf4e2c8 LB |
266 | /* Bail out if the address is all zero, or if the group |
267 | * address bit is set. | |
cb904b0a | 268 | */ |
0d8a52f9 | 269 | return ((addr != cpu_to_le64(0x0000000000000000ULL)) && |
daf4e2c8 | 270 | !(addr & cpu_to_le64(0x0100000000000000ULL))); |
cb904b0a AA |
271 | } |
272 | ||
35d5a374 AA |
273 | /** |
274 | * ieee802154_random_extended_addr - generates a random extended address | |
275 | * @addr: extended addr pointer to place the random address | |
276 | */ | |
277 | static inline void ieee802154_random_extended_addr(__le64 *addr) | |
278 | { | |
279 | get_random_bytes(addr, IEEE802154_EXTENDED_ADDR_LEN); | |
280 | ||
3b369bd2 LB |
281 | /* clear the group bit, and set the locally administered bit */ |
282 | ((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] &= ~0x01; | |
283 | ((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] |= 0x02; | |
35d5a374 AA |
284 | } |
285 | ||
4ca24aca | 286 | #endif /* LINUX_IEEE802154_H */ |