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