blob: d3e415674dac4dfa34145c3f1d3a49685c70413c [file] [log] [blame]
Sergey Lapin9ec76712009-06-08 12:18:48 +00001/*
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 *
Sergey Lapin9ec76712009-06-08 12:18:48 +000015 * 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>
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +000020 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
Sergey Lapin9ec76712009-06-08 12:18:48 +000021 */
22
Alexander Aring4ca24ac2014-10-25 09:41:04 +020023#ifndef LINUX_IEEE802154_H
24#define LINUX_IEEE802154_H
Sergey Lapin9ec76712009-06-08 12:18:48 +000025
Alexander Aringfa491002014-10-27 17:13:40 +010026#include <linux/types.h>
Alexander Aring35d5a372014-11-05 20:51:22 +010027#include <linux/random.h>
Alexander Aringfa491002014-10-27 17:13:40 +010028
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +000029#define IEEE802154_MTU 127
Alexander Aring306f7aa2015-02-11 14:39:15 +010030#define IEEE802154_ACK_PSDU_LEN 5
31#define IEEE802154_MIN_PSDU_LEN 9
Alexander Aring3f3c4bb2015-03-04 21:19:59 +010032#define IEEE802154_FCS_LEN 2
Alexander Aring87a93e42015-09-18 11:30:43 +020033#define IEEE802154_MAX_AUTH_TAG_LEN 16
34
35/* General MAC frame format:
36 * 2 bytes: Frame Control
37 * 1 byte: Sequence Number
38 * 20 bytes: Addressing fields
39 * 14 bytes: Auxiliary Security Header
40 */
41#define IEEE802154_MAX_HEADER_LEN (2 + 1 + 20 + 14)
42#define IEEE802154_MIN_HEADER_LEN (IEEE802154_ACK_PSDU_LEN - \
43 IEEE802154_FCS_LEN)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +000044
Alexander Aring702bf372014-11-12 03:36:57 +010045#define IEEE802154_PAN_ID_BROADCAST 0xffff
Alexander Aring9830c622014-11-12 03:36:58 +010046#define IEEE802154_ADDR_SHORT_BROADCAST 0xffff
47#define IEEE802154_ADDR_SHORT_UNSPEC 0xfffe
Alexander Aring702bf372014-11-12 03:36:57 +010048
Alexander Aring1906bbb2014-11-05 20:51:21 +010049#define IEEE802154_EXTENDED_ADDR_LEN 8
50
Alexander Aring61f2dcb2014-11-12 19:51:56 +010051#define IEEE802154_LIFS_PERIOD 40
52#define IEEE802154_SIFS_PERIOD 12
Alexander Aring3f3c4bb2015-03-04 21:19:59 +010053#define IEEE802154_MAX_SIFS_FRAME_SIZE 18
Alexander Aring61f2dcb2014-11-12 19:51:56 +010054
Alexander Aringcb41c8d2014-11-17 08:20:54 +010055#define IEEE802154_MAX_CHANNEL 26
56#define IEEE802154_MAX_PAGE 31
57
Sergey Lapin9ec76712009-06-08 12:18:48 +000058#define IEEE802154_FC_TYPE_BEACON 0x0 /* Frame is beacon */
59#define IEEE802154_FC_TYPE_DATA 0x1 /* Frame is data */
60#define IEEE802154_FC_TYPE_ACK 0x2 /* Frame is acknowledgment */
61#define IEEE802154_FC_TYPE_MAC_CMD 0x3 /* Frame is MAC command */
62
63#define IEEE802154_FC_TYPE_SHIFT 0
64#define IEEE802154_FC_TYPE_MASK ((1 << 3) - 1)
65#define IEEE802154_FC_TYPE(x) ((x & IEEE802154_FC_TYPE_MASK) >> IEEE802154_FC_TYPE_SHIFT)
66#define IEEE802154_FC_SET_TYPE(v, x) do { \
67 v = (((v) & ~IEEE802154_FC_TYPE_MASK) | \
68 (((x) << IEEE802154_FC_TYPE_SHIFT) & IEEE802154_FC_TYPE_MASK)); \
69 } while (0)
70
Phoebe Buckheister94b4f6c2014-03-14 21:24:00 +010071#define IEEE802154_FC_SECEN_SHIFT 3
72#define IEEE802154_FC_SECEN (1 << IEEE802154_FC_SECEN_SHIFT)
73#define IEEE802154_FC_FRPEND_SHIFT 4
74#define IEEE802154_FC_FRPEND (1 << IEEE802154_FC_FRPEND_SHIFT)
75#define IEEE802154_FC_ACK_REQ_SHIFT 5
76#define IEEE802154_FC_ACK_REQ (1 << IEEE802154_FC_ACK_REQ_SHIFT)
77#define IEEE802154_FC_INTRA_PAN_SHIFT 6
78#define IEEE802154_FC_INTRA_PAN (1 << IEEE802154_FC_INTRA_PAN_SHIFT)
Sergey Lapin9ec76712009-06-08 12:18:48 +000079
80#define IEEE802154_FC_SAMODE_SHIFT 14
81#define IEEE802154_FC_SAMODE_MASK (3 << IEEE802154_FC_SAMODE_SHIFT)
82#define IEEE802154_FC_DAMODE_SHIFT 10
83#define IEEE802154_FC_DAMODE_MASK (3 << IEEE802154_FC_DAMODE_SHIFT)
84
Phoebe Buckheister94b4f6c2014-03-14 21:24:00 +010085#define IEEE802154_FC_VERSION_SHIFT 12
86#define IEEE802154_FC_VERSION_MASK (3 << IEEE802154_FC_VERSION_SHIFT)
87#define IEEE802154_FC_VERSION(x) ((x & IEEE802154_FC_VERSION_MASK) >> IEEE802154_FC_VERSION_SHIFT)
88
Sergey Lapin9ec76712009-06-08 12:18:48 +000089#define IEEE802154_FC_SAMODE(x) \
90 (((x) & IEEE802154_FC_SAMODE_MASK) >> IEEE802154_FC_SAMODE_SHIFT)
91
92#define IEEE802154_FC_DAMODE(x) \
93 (((x) & IEEE802154_FC_DAMODE_MASK) >> IEEE802154_FC_DAMODE_SHIFT)
94
Phoebe Buckheister94b4f6c2014-03-14 21:24:00 +010095#define IEEE802154_SCF_SECLEVEL_MASK 7
96#define IEEE802154_SCF_SECLEVEL_SHIFT 0
97#define IEEE802154_SCF_SECLEVEL(x) (x & IEEE802154_SCF_SECLEVEL_MASK)
98#define IEEE802154_SCF_KEY_ID_MODE_SHIFT 3
99#define IEEE802154_SCF_KEY_ID_MODE_MASK (3 << IEEE802154_SCF_KEY_ID_MODE_SHIFT)
100#define IEEE802154_SCF_KEY_ID_MODE(x) \
101 ((x & IEEE802154_SCF_KEY_ID_MODE_MASK) >> IEEE802154_SCF_KEY_ID_MODE_SHIFT)
102
103#define IEEE802154_SCF_KEY_IMPLICIT 0
104#define IEEE802154_SCF_KEY_INDEX 1
105#define IEEE802154_SCF_KEY_SHORT_INDEX 2
106#define IEEE802154_SCF_KEY_HW_INDEX 3
Sergey Lapin9ec76712009-06-08 12:18:48 +0000107
Phoebe Buckheisterc3a61142014-05-14 17:43:06 +0200108#define IEEE802154_SCF_SECLEVEL_NONE 0
109#define IEEE802154_SCF_SECLEVEL_MIC32 1
110#define IEEE802154_SCF_SECLEVEL_MIC64 2
111#define IEEE802154_SCF_SECLEVEL_MIC128 3
112#define IEEE802154_SCF_SECLEVEL_ENC 4
113#define IEEE802154_SCF_SECLEVEL_ENC_MIC32 5
114#define IEEE802154_SCF_SECLEVEL_ENC_MIC64 6
115#define IEEE802154_SCF_SECLEVEL_ENC_MIC128 7
116
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000117/* MAC footer size */
118#define IEEE802154_MFR_SIZE 2 /* 2 octets */
119
Sergey Lapin9ec76712009-06-08 12:18:48 +0000120/* MAC's Command Frames Identifiers */
121#define IEEE802154_CMD_ASSOCIATION_REQ 0x01
122#define IEEE802154_CMD_ASSOCIATION_RESP 0x02
123#define IEEE802154_CMD_DISASSOCIATION_NOTIFY 0x03
124#define IEEE802154_CMD_DATA_REQ 0x04
125#define IEEE802154_CMD_PANID_CONFLICT_NOTIFY 0x05
126#define IEEE802154_CMD_ORPHAN_NOTIFY 0x06
127#define IEEE802154_CMD_BEACON_REQ 0x07
128#define IEEE802154_CMD_COORD_REALIGN_NOTIFY 0x08
129#define IEEE802154_CMD_GTS_REQ 0x09
130
131/*
132 * The return values of MAC operations
133 */
134enum {
135 /*
136 * The requested operation was completed successfully.
137 * For a transmission request, this value indicates
138 * a successful transmission.
139 */
140 IEEE802154_SUCCESS = 0x0,
141
142 /* The beacon was lost following a synchronization request. */
143 IEEE802154_BEACON_LOSS = 0xe0,
144 /*
145 * A transmission could not take place due to activity on the
146 * channel, i.e., the CSMA-CA mechanism has failed.
147 */
148 IEEE802154_CHNL_ACCESS_FAIL = 0xe1,
149 /* The GTS request has been denied by the PAN coordinator. */
150 IEEE802154_DENINED = 0xe2,
151 /* The attempt to disable the transceiver has failed. */
152 IEEE802154_DISABLE_TRX_FAIL = 0xe3,
153 /*
154 * The received frame induces a failed security check according to
155 * the security suite.
156 */
157 IEEE802154_FAILED_SECURITY_CHECK = 0xe4,
158 /*
159 * The frame resulting from secure processing has a length that is
160 * greater than aMACMaxFrameSize.
161 */
162 IEEE802154_FRAME_TOO_LONG = 0xe5,
163 /*
164 * The requested GTS transmission failed because the specified GTS
165 * either did not have a transmit GTS direction or was not defined.
166 */
167 IEEE802154_INVALID_GTS = 0xe6,
168 /*
169 * A request to purge an MSDU from the transaction queue was made using
170 * an MSDU handle that was not found in the transaction table.
171 */
172 IEEE802154_INVALID_HANDLE = 0xe7,
173 /* A parameter in the primitive is out of the valid range.*/
174 IEEE802154_INVALID_PARAMETER = 0xe8,
175 /* No acknowledgment was received after aMaxFrameRetries. */
176 IEEE802154_NO_ACK = 0xe9,
177 /* A scan operation failed to find any network beacons.*/
178 IEEE802154_NO_BEACON = 0xea,
179 /* No response data were available following a request. */
180 IEEE802154_NO_DATA = 0xeb,
181 /* The operation failed because a short address was not allocated. */
182 IEEE802154_NO_SHORT_ADDRESS = 0xec,
183 /*
184 * A receiver enable request was unsuccessful because it could not be
185 * completed within the CAP.
186 */
187 IEEE802154_OUT_OF_CAP = 0xed,
188 /*
189 * A PAN identifier conflict has been detected and communicated to the
190 * PAN coordinator.
191 */
192 IEEE802154_PANID_CONFLICT = 0xee,
193 /* A coordinator realignment command has been received. */
194 IEEE802154_REALIGMENT = 0xef,
195 /* The transaction has expired and its information discarded. */
196 IEEE802154_TRANSACTION_EXPIRED = 0xf0,
197 /* There is no capacity to store the transaction. */
198 IEEE802154_TRANSACTION_OVERFLOW = 0xf1,
199 /*
200 * The transceiver was in the transmitter enabled state when the
201 * receiver was requested to be enabled.
202 */
203 IEEE802154_TX_ACTIVE = 0xf2,
204 /* The appropriate key is not available in the ACL. */
205 IEEE802154_UNAVAILABLE_KEY = 0xf3,
206 /*
207 * A SET/GET request was issued with the identifier of a PIB attribute
208 * that is not supported.
209 */
210 IEEE802154_UNSUPPORTED_ATTR = 0xf4,
211 /*
212 * A request to perform a scan operation failed because the MLME was
213 * in the process of performing a previously initiated scan operation.
214 */
215 IEEE802154_SCAN_IN_PROGRESS = 0xfc,
216};
217
Alexander Aring54552d02015-09-02 14:21:29 +0200218/* frame control handling */
219#define IEEE802154_FCTL_FTYPE 0x0003
Alexander Aring79750ac2015-09-21 11:24:33 +0200220#define IEEE802154_FCTL_ACKREQ 0x0020
Alexander Aring54552d02015-09-02 14:21:29 +0200221#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 */
229static 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/**
Alexander Aring79750ac2015-09-21 11:24:33 +0200236 * ieee802154_is_ackreq - check if acknowledgment request bit is set
237 * @fc: frame control bytes in little-endian byteorder
238 */
239static inline bool ieee802154_is_ackreq(__le16 fc)
240{
241 return fc & cpu_to_le16(IEEE802154_FCTL_ACKREQ);
242}
243
244/**
Alexander Aring54552d02015-09-02 14:21:29 +0200245 * ieee802154_is_intra_pan - check if intra pan id communication
246 * @fc: frame control bytes in little-endian byteorder
247 */
248static inline bool ieee802154_is_intra_pan(__le16 fc)
249{
250 return fc & cpu_to_le16(IEEE802154_FCTL_INTRA_PAN);
251}
252
Alexander Aringfa491002014-10-27 17:13:40 +0100253/**
254 * ieee802154_is_valid_psdu_len - check if psdu len is valid
Alexander Aring306f7aa2015-02-11 14:39:15 +0100255 * available lengths:
256 * 0-4 Reserved
257 * 5 MPDU (Acknowledgment)
258 * 6-8 Reserved
259 * 9-127 MPDU
260 *
Alexander Aringfa491002014-10-27 17:13:40 +0100261 * @len: psdu len with (MHR + payload + MFR)
262 */
263static inline bool ieee802154_is_valid_psdu_len(const u8 len)
264{
Alexander Aring306f7aa2015-02-11 14:39:15 +0100265 return (len == IEEE802154_ACK_PSDU_LEN ||
266 (len >= IEEE802154_MIN_PSDU_LEN && len <= IEEE802154_MTU));
Alexander Aringfa491002014-10-27 17:13:40 +0100267}
Sergey Lapin9ec76712009-06-08 12:18:48 +0000268
Alexander Aringcb904b02014-11-02 04:18:45 +0100269/**
270 * ieee802154_is_valid_psdu_len - check if extended addr is valid
271 * @addr: extended addr to check
272 */
Lennert Buytenhekdaf4e2c2015-05-28 15:38:43 +0300273static inline bool ieee802154_is_valid_extended_unicast_addr(const __le64 addr)
Alexander Aringcb904b02014-11-02 04:18:45 +0100274{
Lennert Buytenhekdaf4e2c2015-05-28 15:38:43 +0300275 /* Bail out if the address is all zero, or if the group
276 * address bit is set.
Alexander Aringcb904b02014-11-02 04:18:45 +0100277 */
Dan Carpenter0d8a52f2014-11-04 11:55:09 +0300278 return ((addr != cpu_to_le64(0x0000000000000000ULL)) &&
Lennert Buytenhekdaf4e2c2015-05-28 15:38:43 +0300279 !(addr & cpu_to_le64(0x0100000000000000ULL)));
Alexander Aringcb904b02014-11-02 04:18:45 +0100280}
281
Alexander Aring35d5a372014-11-05 20:51:22 +0100282/**
283 * ieee802154_random_extended_addr - generates a random extended address
284 * @addr: extended addr pointer to place the random address
285 */
286static inline void ieee802154_random_extended_addr(__le64 *addr)
287{
288 get_random_bytes(addr, IEEE802154_EXTENDED_ADDR_LEN);
289
Lennert Buytenhek3b369bd2015-05-28 15:38:32 +0300290 /* clear the group bit, and set the locally administered bit */
291 ((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] &= ~0x01;
292 ((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] |= 0x02;
Alexander Aring35d5a372014-11-05 20:51:22 +0100293}
294
Alexander Aring4ca24ac2014-10-25 09:41:04 +0200295#endif /* LINUX_IEEE802154_H */