1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#ifndef LINUX_IEEE802154_H
24#define LINUX_IEEE802154_H
25
26#include <linux/types.h>
27#include <linux/random.h>
28
29#define IEEE802154_MTU 127
30#define IEEE802154_ACK_PSDU_LEN 5
31#define IEEE802154_MIN_PSDU_LEN 9
32#define IEEE802154_FCS_LEN 2
33#define IEEE802154_MAX_AUTH_TAG_LEN 16
34#define IEEE802154_FC_LEN 2
35#define IEEE802154_SEQ_LEN 1
36
37
38
39
40
41
42
43#define IEEE802154_MAX_HEADER_LEN (2 + 1 + 20 + 14)
44#define IEEE802154_MIN_HEADER_LEN (IEEE802154_ACK_PSDU_LEN - \
45 IEEE802154_FCS_LEN)
46
47#define IEEE802154_PAN_ID_BROADCAST 0xffff
48#define IEEE802154_ADDR_SHORT_BROADCAST 0xffff
49#define IEEE802154_ADDR_SHORT_UNSPEC 0xfffe
50
51#define IEEE802154_EXTENDED_ADDR_LEN 8
52#define IEEE802154_SHORT_ADDR_LEN 2
53#define IEEE802154_PAN_ID_LEN 2
54
55#define IEEE802154_LIFS_PERIOD 40
56#define IEEE802154_SIFS_PERIOD 12
57#define IEEE802154_MAX_SIFS_FRAME_SIZE 18
58
59#define IEEE802154_MAX_CHANNEL 26
60#define IEEE802154_MAX_PAGE 31
61
62#define IEEE802154_FC_TYPE_BEACON 0x0
63#define IEEE802154_FC_TYPE_DATA 0x1
64#define IEEE802154_FC_TYPE_ACK 0x2
65#define IEEE802154_FC_TYPE_MAC_CMD 0x3
66
67#define IEEE802154_FC_TYPE_SHIFT 0
68#define IEEE802154_FC_TYPE_MASK ((1 << 3) - 1)
69#define IEEE802154_FC_TYPE(x) ((x & IEEE802154_FC_TYPE_MASK) >> IEEE802154_FC_TYPE_SHIFT)
70#define IEEE802154_FC_SET_TYPE(v, x) do { \
71 v = (((v) & ~IEEE802154_FC_TYPE_MASK) | \
72 (((x) << IEEE802154_FC_TYPE_SHIFT) & IEEE802154_FC_TYPE_MASK)); \
73 } while (0)
74
75#define IEEE802154_FC_SECEN_SHIFT 3
76#define IEEE802154_FC_SECEN (1 << IEEE802154_FC_SECEN_SHIFT)
77#define IEEE802154_FC_FRPEND_SHIFT 4
78#define IEEE802154_FC_FRPEND (1 << IEEE802154_FC_FRPEND_SHIFT)
79#define IEEE802154_FC_ACK_REQ_SHIFT 5
80#define IEEE802154_FC_ACK_REQ (1 << IEEE802154_FC_ACK_REQ_SHIFT)
81#define IEEE802154_FC_INTRA_PAN_SHIFT 6
82#define IEEE802154_FC_INTRA_PAN (1 << IEEE802154_FC_INTRA_PAN_SHIFT)
83
84#define IEEE802154_FC_SAMODE_SHIFT 14
85#define IEEE802154_FC_SAMODE_MASK (3 << IEEE802154_FC_SAMODE_SHIFT)
86#define IEEE802154_FC_DAMODE_SHIFT 10
87#define IEEE802154_FC_DAMODE_MASK (3 << IEEE802154_FC_DAMODE_SHIFT)
88
89#define IEEE802154_FC_VERSION_SHIFT 12
90#define IEEE802154_FC_VERSION_MASK (3 << IEEE802154_FC_VERSION_SHIFT)
91#define IEEE802154_FC_VERSION(x) ((x & IEEE802154_FC_VERSION_MASK) >> IEEE802154_FC_VERSION_SHIFT)
92
93#define IEEE802154_FC_SAMODE(x) \
94 (((x) & IEEE802154_FC_SAMODE_MASK) >> IEEE802154_FC_SAMODE_SHIFT)
95
96#define IEEE802154_FC_DAMODE(x) \
97 (((x) & IEEE802154_FC_DAMODE_MASK) >> IEEE802154_FC_DAMODE_SHIFT)
98
99#define IEEE802154_SCF_SECLEVEL_MASK 7
100#define IEEE802154_SCF_SECLEVEL_SHIFT 0
101#define IEEE802154_SCF_SECLEVEL(x) (x & IEEE802154_SCF_SECLEVEL_MASK)
102#define IEEE802154_SCF_KEY_ID_MODE_SHIFT 3
103#define IEEE802154_SCF_KEY_ID_MODE_MASK (3 << IEEE802154_SCF_KEY_ID_MODE_SHIFT)
104#define IEEE802154_SCF_KEY_ID_MODE(x) \
105 ((x & IEEE802154_SCF_KEY_ID_MODE_MASK) >> IEEE802154_SCF_KEY_ID_MODE_SHIFT)
106
107#define IEEE802154_SCF_KEY_IMPLICIT 0
108#define IEEE802154_SCF_KEY_INDEX 1
109#define IEEE802154_SCF_KEY_SHORT_INDEX 2
110#define IEEE802154_SCF_KEY_HW_INDEX 3
111
112#define IEEE802154_SCF_SECLEVEL_NONE 0
113#define IEEE802154_SCF_SECLEVEL_MIC32 1
114#define IEEE802154_SCF_SECLEVEL_MIC64 2
115#define IEEE802154_SCF_SECLEVEL_MIC128 3
116#define IEEE802154_SCF_SECLEVEL_ENC 4
117#define IEEE802154_SCF_SECLEVEL_ENC_MIC32 5
118#define IEEE802154_SCF_SECLEVEL_ENC_MIC64 6
119#define IEEE802154_SCF_SECLEVEL_ENC_MIC128 7
120
121
122#define IEEE802154_MFR_SIZE 2
123
124
125#define IEEE802154_CMD_ASSOCIATION_REQ 0x01
126#define IEEE802154_CMD_ASSOCIATION_RESP 0x02
127#define IEEE802154_CMD_DISASSOCIATION_NOTIFY 0x03
128#define IEEE802154_CMD_DATA_REQ 0x04
129#define IEEE802154_CMD_PANID_CONFLICT_NOTIFY 0x05
130#define IEEE802154_CMD_ORPHAN_NOTIFY 0x06
131#define IEEE802154_CMD_BEACON_REQ 0x07
132#define IEEE802154_CMD_COORD_REALIGN_NOTIFY 0x08
133#define IEEE802154_CMD_GTS_REQ 0x09
134
135
136
137
138enum {
139
140
141
142
143
144 IEEE802154_SUCCESS = 0x0,
145
146
147 IEEE802154_BEACON_LOSS = 0xe0,
148
149
150
151
152 IEEE802154_CHNL_ACCESS_FAIL = 0xe1,
153
154 IEEE802154_DENINED = 0xe2,
155
156 IEEE802154_DISABLE_TRX_FAIL = 0xe3,
157
158
159
160
161 IEEE802154_FAILED_SECURITY_CHECK = 0xe4,
162
163
164
165
166 IEEE802154_FRAME_TOO_LONG = 0xe5,
167
168
169
170
171 IEEE802154_INVALID_GTS = 0xe6,
172
173
174
175
176 IEEE802154_INVALID_HANDLE = 0xe7,
177
178 IEEE802154_INVALID_PARAMETER = 0xe8,
179
180 IEEE802154_NO_ACK = 0xe9,
181
182 IEEE802154_NO_BEACON = 0xea,
183
184 IEEE802154_NO_DATA = 0xeb,
185
186 IEEE802154_NO_SHORT_ADDRESS = 0xec,
187
188
189
190
191 IEEE802154_OUT_OF_CAP = 0xed,
192
193
194
195
196 IEEE802154_PANID_CONFLICT = 0xee,
197
198 IEEE802154_REALIGMENT = 0xef,
199
200 IEEE802154_TRANSACTION_EXPIRED = 0xf0,
201
202 IEEE802154_TRANSACTION_OVERFLOW = 0xf1,
203
204
205
206
207 IEEE802154_TX_ACTIVE = 0xf2,
208
209 IEEE802154_UNAVAILABLE_KEY = 0xf3,
210
211
212
213
214 IEEE802154_UNSUPPORTED_ATTR = 0xf4,
215
216
217
218
219 IEEE802154_SCAN_IN_PROGRESS = 0xfc,
220};
221
222
223#define IEEE802154_FCTL_FTYPE 0x0003
224#define IEEE802154_FCTL_ACKREQ 0x0020
225#define IEEE802154_FCTL_SECEN 0x0004
226#define IEEE802154_FCTL_INTRA_PAN 0x0040
227#define IEEE802154_FCTL_DADDR 0x0c00
228#define IEEE802154_FCTL_SADDR 0xc000
229
230#define IEEE802154_FTYPE_DATA 0x0001
231
232#define IEEE802154_FCTL_ADDR_NONE 0x0000
233#define IEEE802154_FCTL_DADDR_SHORT 0x0800
234#define IEEE802154_FCTL_DADDR_EXTENDED 0x0c00
235#define IEEE802154_FCTL_SADDR_SHORT 0x8000
236#define IEEE802154_FCTL_SADDR_EXTENDED 0xc000
237
238
239
240
241
242static inline int ieee802154_is_data(__le16 fc)
243{
244 return (fc & cpu_to_le16(IEEE802154_FCTL_FTYPE)) ==
245 cpu_to_le16(IEEE802154_FTYPE_DATA);
246}
247
248
249
250
251
252static inline bool ieee802154_is_secen(__le16 fc)
253{
254 return fc & cpu_to_le16(IEEE802154_FCTL_SECEN);
255}
256
257
258
259
260
261static inline bool ieee802154_is_ackreq(__le16 fc)
262{
263 return fc & cpu_to_le16(IEEE802154_FCTL_ACKREQ);
264}
265
266
267
268
269
270static inline bool ieee802154_is_intra_pan(__le16 fc)
271{
272 return fc & cpu_to_le16(IEEE802154_FCTL_INTRA_PAN);
273}
274
275
276
277
278
279static inline __le16 ieee802154_daddr_mode(__le16 fc)
280{
281 return fc & cpu_to_le16(IEEE802154_FCTL_DADDR);
282}
283
284
285
286
287
288static inline __le16 ieee802154_saddr_mode(__le16 fc)
289{
290 return fc & cpu_to_le16(IEEE802154_FCTL_SADDR);
291}
292
293
294
295
296
297
298
299
300
301
302
303static inline bool ieee802154_is_valid_psdu_len(u8 len)
304{
305 return (len == IEEE802154_ACK_PSDU_LEN ||
306 (len >= IEEE802154_MIN_PSDU_LEN && len <= IEEE802154_MTU));
307}
308
309
310
311
312
313static inline bool ieee802154_is_valid_extended_unicast_addr(__le64 addr)
314{
315
316
317
318 return ((addr != cpu_to_le64(0x0000000000000000ULL)) &&
319 !(addr & cpu_to_le64(0x0100000000000000ULL)));
320}
321
322
323
324
325
326static inline bool ieee802154_is_broadcast_short_addr(__le16 addr)
327{
328 return (addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST));
329}
330
331
332
333
334
335static inline bool ieee802154_is_unspec_short_addr(__le16 addr)
336{
337 return (addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC));
338}
339
340
341
342
343
344static inline bool ieee802154_is_valid_src_short_addr(__le16 addr)
345{
346 return !(ieee802154_is_broadcast_short_addr(addr) ||
347 ieee802154_is_unspec_short_addr(addr));
348}
349
350
351
352
353
354static inline void ieee802154_random_extended_addr(__le64 *addr)
355{
356 get_random_bytes(addr, IEEE802154_EXTENDED_ADDR_LEN);
357
358
359 ((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] &= ~0x01;
360 ((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] |= 0x02;
361}
362
363#endif
364