ieee802154.h 12 KB

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