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