ieee802154.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * IEEE802.15.4-2003 specification
  4. *
  5. * Copyright (C) 2007, 2008 Siemens AG
  6. *
  7. * Written by:
  8. * Pavel Smolenskiy <[email protected]>
  9. * Maxim Gorbachyov <[email protected]>
  10. * Maxim Osipov <[email protected]>
  11. * Dmitry Eremin-Solenikov <[email protected]>
  12. * Alexander Smirnov <[email protected]>
  13. */
  14. #ifndef LINUX_IEEE802154_H
  15. #define LINUX_IEEE802154_H
  16. #include <linux/types.h>
  17. #include <linux/random.h>
  18. #define IEEE802154_MTU 127
  19. #define IEEE802154_ACK_PSDU_LEN 5
  20. #define IEEE802154_MIN_PSDU_LEN 9
  21. #define IEEE802154_FCS_LEN 2
  22. #define IEEE802154_MAX_AUTH_TAG_LEN 16
  23. #define IEEE802154_FC_LEN 2
  24. #define IEEE802154_SEQ_LEN 1
  25. /* General MAC frame format:
  26. * 2 bytes: Frame Control
  27. * 1 byte: Sequence Number
  28. * 20 bytes: Addressing fields
  29. * 14 bytes: Auxiliary Security Header
  30. */
  31. #define IEEE802154_MAX_HEADER_LEN (2 + 1 + 20 + 14)
  32. #define IEEE802154_MIN_HEADER_LEN (IEEE802154_ACK_PSDU_LEN - \
  33. IEEE802154_FCS_LEN)
  34. #define IEEE802154_PAN_ID_BROADCAST 0xffff
  35. #define IEEE802154_ADDR_SHORT_BROADCAST 0xffff
  36. #define IEEE802154_ADDR_SHORT_UNSPEC 0xfffe
  37. #define IEEE802154_EXTENDED_ADDR_LEN 8
  38. #define IEEE802154_SHORT_ADDR_LEN 2
  39. #define IEEE802154_PAN_ID_LEN 2
  40. #define IEEE802154_LIFS_PERIOD 40
  41. #define IEEE802154_SIFS_PERIOD 12
  42. #define IEEE802154_MAX_SIFS_FRAME_SIZE 18
  43. #define IEEE802154_MAX_CHANNEL 26
  44. #define IEEE802154_MAX_PAGE 31
  45. #define IEEE802154_FC_TYPE_BEACON 0x0 /* Frame is beacon */
  46. #define IEEE802154_FC_TYPE_DATA 0x1 /* Frame is data */
  47. #define IEEE802154_FC_TYPE_ACK 0x2 /* Frame is acknowledgment */
  48. #define IEEE802154_FC_TYPE_MAC_CMD 0x3 /* Frame is MAC command */
  49. #define IEEE802154_FC_TYPE_SHIFT 0
  50. #define IEEE802154_FC_TYPE_MASK ((1 << 3) - 1)
  51. #define IEEE802154_FC_TYPE(x) ((x & IEEE802154_FC_TYPE_MASK) >> IEEE802154_FC_TYPE_SHIFT)
  52. #define IEEE802154_FC_SET_TYPE(v, x) do { \
  53. v = (((v) & ~IEEE802154_FC_TYPE_MASK) | \
  54. (((x) << IEEE802154_FC_TYPE_SHIFT) & IEEE802154_FC_TYPE_MASK)); \
  55. } while (0)
  56. #define IEEE802154_FC_SECEN_SHIFT 3
  57. #define IEEE802154_FC_SECEN (1 << IEEE802154_FC_SECEN_SHIFT)
  58. #define IEEE802154_FC_FRPEND_SHIFT 4
  59. #define IEEE802154_FC_FRPEND (1 << IEEE802154_FC_FRPEND_SHIFT)
  60. #define IEEE802154_FC_ACK_REQ_SHIFT 5
  61. #define IEEE802154_FC_ACK_REQ (1 << IEEE802154_FC_ACK_REQ_SHIFT)
  62. #define IEEE802154_FC_INTRA_PAN_SHIFT 6
  63. #define IEEE802154_FC_INTRA_PAN (1 << IEEE802154_FC_INTRA_PAN_SHIFT)
  64. #define IEEE802154_FC_SAMODE_SHIFT 14
  65. #define IEEE802154_FC_SAMODE_MASK (3 << IEEE802154_FC_SAMODE_SHIFT)
  66. #define IEEE802154_FC_DAMODE_SHIFT 10
  67. #define IEEE802154_FC_DAMODE_MASK (3 << IEEE802154_FC_DAMODE_SHIFT)
  68. #define IEEE802154_FC_VERSION_SHIFT 12
  69. #define IEEE802154_FC_VERSION_MASK (3 << IEEE802154_FC_VERSION_SHIFT)
  70. #define IEEE802154_FC_VERSION(x) ((x & IEEE802154_FC_VERSION_MASK) >> IEEE802154_FC_VERSION_SHIFT)
  71. #define IEEE802154_FC_SAMODE(x) \
  72. (((x) & IEEE802154_FC_SAMODE_MASK) >> IEEE802154_FC_SAMODE_SHIFT)
  73. #define IEEE802154_FC_DAMODE(x) \
  74. (((x) & IEEE802154_FC_DAMODE_MASK) >> IEEE802154_FC_DAMODE_SHIFT)
  75. #define IEEE802154_SCF_SECLEVEL_MASK 7
  76. #define IEEE802154_SCF_SECLEVEL_SHIFT 0
  77. #define IEEE802154_SCF_SECLEVEL(x) (x & IEEE802154_SCF_SECLEVEL_MASK)
  78. #define IEEE802154_SCF_KEY_ID_MODE_SHIFT 3
  79. #define IEEE802154_SCF_KEY_ID_MODE_MASK (3 << IEEE802154_SCF_KEY_ID_MODE_SHIFT)
  80. #define IEEE802154_SCF_KEY_ID_MODE(x) \
  81. ((x & IEEE802154_SCF_KEY_ID_MODE_MASK) >> IEEE802154_SCF_KEY_ID_MODE_SHIFT)
  82. #define IEEE802154_SCF_KEY_IMPLICIT 0
  83. #define IEEE802154_SCF_KEY_INDEX 1
  84. #define IEEE802154_SCF_KEY_SHORT_INDEX 2
  85. #define IEEE802154_SCF_KEY_HW_INDEX 3
  86. #define IEEE802154_SCF_SECLEVEL_NONE 0
  87. #define IEEE802154_SCF_SECLEVEL_MIC32 1
  88. #define IEEE802154_SCF_SECLEVEL_MIC64 2
  89. #define IEEE802154_SCF_SECLEVEL_MIC128 3
  90. #define IEEE802154_SCF_SECLEVEL_ENC 4
  91. #define IEEE802154_SCF_SECLEVEL_ENC_MIC32 5
  92. #define IEEE802154_SCF_SECLEVEL_ENC_MIC64 6
  93. #define IEEE802154_SCF_SECLEVEL_ENC_MIC128 7
  94. /* MAC footer size */
  95. #define IEEE802154_MFR_SIZE 2 /* 2 octets */
  96. /* MAC's Command Frames Identifiers */
  97. #define IEEE802154_CMD_ASSOCIATION_REQ 0x01
  98. #define IEEE802154_CMD_ASSOCIATION_RESP 0x02
  99. #define IEEE802154_CMD_DISASSOCIATION_NOTIFY 0x03
  100. #define IEEE802154_CMD_DATA_REQ 0x04
  101. #define IEEE802154_CMD_PANID_CONFLICT_NOTIFY 0x05
  102. #define IEEE802154_CMD_ORPHAN_NOTIFY 0x06
  103. #define IEEE802154_CMD_BEACON_REQ 0x07
  104. #define IEEE802154_CMD_COORD_REALIGN_NOTIFY 0x08
  105. #define IEEE802154_CMD_GTS_REQ 0x09
  106. /*
  107. * The return values of MAC operations
  108. */
  109. enum {
  110. /*
  111. * The requested operation was completed successfully.
  112. * For a transmission request, this value indicates
  113. * a successful transmission.
  114. */
  115. IEEE802154_SUCCESS = 0x0,
  116. /* The requested operation failed. */
  117. IEEE802154_MAC_ERROR = 0x1,
  118. /* The requested operation has been cancelled. */
  119. IEEE802154_CANCELLED = 0x2,
  120. /*
  121. * Device is ready to poll the coordinator for data in a non beacon
  122. * enabled PAN.
  123. */
  124. IEEE802154_READY_FOR_POLL = 0x3,
  125. /* Wrong frame counter. */
  126. IEEE802154_COUNTER_ERROR = 0xdb,
  127. /*
  128. * The frame does not conforms to the incoming key usage policy checking
  129. * procedure.
  130. */
  131. IEEE802154_IMPROPER_KEY_TYPE = 0xdc,
  132. /*
  133. * The frame does not conforms to the incoming security level usage
  134. * policy checking procedure.
  135. */
  136. IEEE802154_IMPROPER_SECURITY_LEVEL = 0xdd,
  137. /* Secured frame received with an empty Frame Version field. */
  138. IEEE802154_UNSUPPORTED_LEGACY = 0xde,
  139. /*
  140. * A secured frame is received or must be sent but security is not
  141. * enabled in the device. Or, the Auxiliary Security Header has security
  142. * level of zero in it.
  143. */
  144. IEEE802154_UNSUPPORTED_SECURITY = 0xdf,
  145. /* The beacon was lost following a synchronization request. */
  146. IEEE802154_BEACON_LOST = 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_CHANNEL_ACCESS_FAILURE = 0xe1,
  152. /* The GTS request has been denied by the PAN coordinator. */
  153. IEEE802154_DENIED = 0xe2,
  154. /* The attempt to disable the transceiver has failed. */
  155. IEEE802154_DISABLE_TRX_FAILURE = 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_PAN_ID_CONFLICT = 0xee,
  196. /* A coordinator realignment command has been received. */
  197. IEEE802154_REALIGNMENT = 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_ATTRIBUTE = 0xf4,
  214. /* Missing source or destination address or address mode. */
  215. IEEE802154_INVALID_ADDRESS = 0xf5,
  216. /*
  217. * MLME asked to turn the receiver on, but the on time duration is too
  218. * big compared to the macBeaconOrder.
  219. */
  220. IEEE802154_ON_TIME_TOO_LONG = 0xf6,
  221. /*
  222. * MLME asaked to turn the receiver on, but the request was delayed for
  223. * too long before getting processed.
  224. */
  225. IEEE802154_PAST_TIME = 0xf7,
  226. /*
  227. * The StartTime parameter is nonzero, and the MLME is not currently
  228. * tracking the beacon of the coordinator through which it is
  229. * associated.
  230. */
  231. IEEE802154_TRACKING_OFF = 0xf8,
  232. /*
  233. * The index inside the hierarchical values in PIBAttribute is out of
  234. * range.
  235. */
  236. IEEE802154_INVALID_INDEX = 0xf9,
  237. /*
  238. * The number of PAN descriptors discovered during a scan has been
  239. * reached.
  240. */
  241. IEEE802154_LIMIT_REACHED = 0xfa,
  242. /*
  243. * The PIBAttribute parameter specifies an attribute that is a read-only
  244. * attribute.
  245. */
  246. IEEE802154_READ_ONLY = 0xfb,
  247. /*
  248. * A request to perform a scan operation failed because the MLME was
  249. * in the process of performing a previously initiated scan operation.
  250. */
  251. IEEE802154_SCAN_IN_PROGRESS = 0xfc,
  252. /* The outgoing superframe overlaps the incoming superframe. */
  253. IEEE802154_SUPERFRAME_OVERLAP = 0xfd,
  254. /* Any other error situation. */
  255. IEEE802154_SYSTEM_ERROR = 0xff,
  256. };
  257. /* frame control handling */
  258. #define IEEE802154_FCTL_FTYPE 0x0003
  259. #define IEEE802154_FCTL_ACKREQ 0x0020
  260. #define IEEE802154_FCTL_SECEN 0x0004
  261. #define IEEE802154_FCTL_INTRA_PAN 0x0040
  262. #define IEEE802154_FCTL_DADDR 0x0c00
  263. #define IEEE802154_FCTL_SADDR 0xc000
  264. #define IEEE802154_FTYPE_DATA 0x0001
  265. #define IEEE802154_FCTL_ADDR_NONE 0x0000
  266. #define IEEE802154_FCTL_DADDR_SHORT 0x0800
  267. #define IEEE802154_FCTL_DADDR_EXTENDED 0x0c00
  268. #define IEEE802154_FCTL_SADDR_SHORT 0x8000
  269. #define IEEE802154_FCTL_SADDR_EXTENDED 0xc000
  270. /*
  271. * ieee802154_is_data - check if type is IEEE802154_FTYPE_DATA
  272. * @fc: frame control bytes in little-endian byteorder
  273. */
  274. static inline int ieee802154_is_data(__le16 fc)
  275. {
  276. return (fc & cpu_to_le16(IEEE802154_FCTL_FTYPE)) ==
  277. cpu_to_le16(IEEE802154_FTYPE_DATA);
  278. }
  279. /**
  280. * ieee802154_is_secen - check if Security bit is set
  281. * @fc: frame control bytes in little-endian byteorder
  282. */
  283. static inline bool ieee802154_is_secen(__le16 fc)
  284. {
  285. return fc & cpu_to_le16(IEEE802154_FCTL_SECEN);
  286. }
  287. /**
  288. * ieee802154_is_ackreq - check if acknowledgment request bit is set
  289. * @fc: frame control bytes in little-endian byteorder
  290. */
  291. static inline bool ieee802154_is_ackreq(__le16 fc)
  292. {
  293. return fc & cpu_to_le16(IEEE802154_FCTL_ACKREQ);
  294. }
  295. /**
  296. * ieee802154_is_intra_pan - check if intra pan id communication
  297. * @fc: frame control bytes in little-endian byteorder
  298. */
  299. static inline bool ieee802154_is_intra_pan(__le16 fc)
  300. {
  301. return fc & cpu_to_le16(IEEE802154_FCTL_INTRA_PAN);
  302. }
  303. /*
  304. * ieee802154_daddr_mode - get daddr mode from fc
  305. * @fc: frame control bytes in little-endian byteorder
  306. */
  307. static inline __le16 ieee802154_daddr_mode(__le16 fc)
  308. {
  309. return fc & cpu_to_le16(IEEE802154_FCTL_DADDR);
  310. }
  311. /*
  312. * ieee802154_saddr_mode - get saddr mode from fc
  313. * @fc: frame control bytes in little-endian byteorder
  314. */
  315. static inline __le16 ieee802154_saddr_mode(__le16 fc)
  316. {
  317. return fc & cpu_to_le16(IEEE802154_FCTL_SADDR);
  318. }
  319. /**
  320. * ieee802154_is_valid_psdu_len - check if psdu len is valid
  321. * available lengths:
  322. * 0-4 Reserved
  323. * 5 MPDU (Acknowledgment)
  324. * 6-8 Reserved
  325. * 9-127 MPDU
  326. *
  327. * @len: psdu len with (MHR + payload + MFR)
  328. */
  329. static inline bool ieee802154_is_valid_psdu_len(u8 len)
  330. {
  331. return (len == IEEE802154_ACK_PSDU_LEN ||
  332. (len >= IEEE802154_MIN_PSDU_LEN && len <= IEEE802154_MTU));
  333. }
  334. /**
  335. * ieee802154_is_valid_extended_unicast_addr - check if extended addr is valid
  336. * @addr: extended addr to check
  337. */
  338. static inline bool ieee802154_is_valid_extended_unicast_addr(__le64 addr)
  339. {
  340. /* Bail out if the address is all zero, or if the group
  341. * address bit is set.
  342. */
  343. return ((addr != cpu_to_le64(0x0000000000000000ULL)) &&
  344. !(addr & cpu_to_le64(0x0100000000000000ULL)));
  345. }
  346. /**
  347. * ieee802154_is_broadcast_short_addr - check if short addr is broadcast
  348. * @addr: short addr to check
  349. */
  350. static inline bool ieee802154_is_broadcast_short_addr(__le16 addr)
  351. {
  352. return (addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST));
  353. }
  354. /**
  355. * ieee802154_is_unspec_short_addr - check if short addr is unspecified
  356. * @addr: short addr to check
  357. */
  358. static inline bool ieee802154_is_unspec_short_addr(__le16 addr)
  359. {
  360. return (addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC));
  361. }
  362. /**
  363. * ieee802154_is_valid_src_short_addr - check if source short address is valid
  364. * @addr: short addr to check
  365. */
  366. static inline bool ieee802154_is_valid_src_short_addr(__le16 addr)
  367. {
  368. return !(ieee802154_is_broadcast_short_addr(addr) ||
  369. ieee802154_is_unspec_short_addr(addr));
  370. }
  371. /**
  372. * ieee802154_random_extended_addr - generates a random extended address
  373. * @addr: extended addr pointer to place the random address
  374. */
  375. static inline void ieee802154_random_extended_addr(__le64 *addr)
  376. {
  377. get_random_bytes(addr, IEEE802154_EXTENDED_ADDR_LEN);
  378. /* clear the group bit, and set the locally administered bit */
  379. ((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] &= ~0x01;
  380. ((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] |= 0x02;
  381. }
  382. #endif /* LINUX_IEEE802154_H */