ieee802154.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #include <asm/byteorder.h>
  27. #define IEEE802154_MTU 127
  28. #define IEEE802154_ACK_PSDU_LEN 5
  29. #define IEEE802154_MIN_PSDU_LEN 9
  30. #define IEEE802154_FCS_LEN 2
  31. #define IEEE802154_PAN_ID_BROADCAST 0xffff
  32. #define IEEE802154_ADDR_SHORT_BROADCAST 0xffff
  33. #define IEEE802154_ADDR_SHORT_UNSPEC 0xfffe
  34. #define IEEE802154_EXTENDED_ADDR_LEN 8
  35. #define IEEE802154_LIFS_PERIOD 40
  36. #define IEEE802154_SIFS_PERIOD 12
  37. #define IEEE802154_MAX_SIFS_FRAME_SIZE 18
  38. #define IEEE802154_MAX_CHANNEL 26
  39. #define IEEE802154_MAX_PAGE 31
  40. #define IEEE802154_FC_TYPE_BEACON 0x0 /* Frame is beacon */
  41. #define IEEE802154_FC_TYPE_DATA 0x1 /* Frame is data */
  42. #define IEEE802154_FC_TYPE_ACK 0x2 /* Frame is acknowledgment */
  43. #define IEEE802154_FC_TYPE_MAC_CMD 0x3 /* Frame is MAC command */
  44. #define IEEE802154_FC_TYPE_SHIFT 0
  45. #define IEEE802154_FC_TYPE_MASK ((1 << 3) - 1)
  46. #define IEEE802154_FC_TYPE(x) ((x & IEEE802154_FC_TYPE_MASK) >> IEEE802154_FC_TYPE_SHIFT)
  47. #define IEEE802154_FC_SET_TYPE(v, x) do { \
  48. v = (((v) & ~IEEE802154_FC_TYPE_MASK) | \
  49. (((x) << IEEE802154_FC_TYPE_SHIFT) & IEEE802154_FC_TYPE_MASK)); \
  50. } while (0)
  51. #define IEEE802154_FC_SECEN_SHIFT 3
  52. #define IEEE802154_FC_SECEN (1 << IEEE802154_FC_SECEN_SHIFT)
  53. #define IEEE802154_FC_FRPEND_SHIFT 4
  54. #define IEEE802154_FC_FRPEND (1 << IEEE802154_FC_FRPEND_SHIFT)
  55. #define IEEE802154_FC_ACK_REQ_SHIFT 5
  56. #define IEEE802154_FC_ACK_REQ (1 << IEEE802154_FC_ACK_REQ_SHIFT)
  57. #define IEEE802154_FC_INTRA_PAN_SHIFT 6
  58. #define IEEE802154_FC_INTRA_PAN (1 << IEEE802154_FC_INTRA_PAN_SHIFT)
  59. #define IEEE802154_FC_SAMODE_SHIFT 14
  60. #define IEEE802154_FC_SAMODE_MASK (3 << IEEE802154_FC_SAMODE_SHIFT)
  61. #define IEEE802154_FC_DAMODE_SHIFT 10
  62. #define IEEE802154_FC_DAMODE_MASK (3 << IEEE802154_FC_DAMODE_SHIFT)
  63. #define IEEE802154_FC_VERSION_SHIFT 12
  64. #define IEEE802154_FC_VERSION_MASK (3 << IEEE802154_FC_VERSION_SHIFT)
  65. #define IEEE802154_FC_VERSION(x) ((x & IEEE802154_FC_VERSION_MASK) >> IEEE802154_FC_VERSION_SHIFT)
  66. #define IEEE802154_FC_SAMODE(x) \
  67. (((x) & IEEE802154_FC_SAMODE_MASK) >> IEEE802154_FC_SAMODE_SHIFT)
  68. #define IEEE802154_FC_DAMODE(x) \
  69. (((x) & IEEE802154_FC_DAMODE_MASK) >> IEEE802154_FC_DAMODE_SHIFT)
  70. #define IEEE802154_SCF_SECLEVEL_MASK 7
  71. #define IEEE802154_SCF_SECLEVEL_SHIFT 0
  72. #define IEEE802154_SCF_SECLEVEL(x) (x & IEEE802154_SCF_SECLEVEL_MASK)
  73. #define IEEE802154_SCF_KEY_ID_MODE_SHIFT 3
  74. #define IEEE802154_SCF_KEY_ID_MODE_MASK (3 << IEEE802154_SCF_KEY_ID_MODE_SHIFT)
  75. #define IEEE802154_SCF_KEY_ID_MODE(x) \
  76. ((x & IEEE802154_SCF_KEY_ID_MODE_MASK) >> IEEE802154_SCF_KEY_ID_MODE_SHIFT)
  77. #define IEEE802154_SCF_KEY_IMPLICIT 0
  78. #define IEEE802154_SCF_KEY_INDEX 1
  79. #define IEEE802154_SCF_KEY_SHORT_INDEX 2
  80. #define IEEE802154_SCF_KEY_HW_INDEX 3
  81. #define IEEE802154_SCF_SECLEVEL_NONE 0
  82. #define IEEE802154_SCF_SECLEVEL_MIC32 1
  83. #define IEEE802154_SCF_SECLEVEL_MIC64 2
  84. #define IEEE802154_SCF_SECLEVEL_MIC128 3
  85. #define IEEE802154_SCF_SECLEVEL_ENC 4
  86. #define IEEE802154_SCF_SECLEVEL_ENC_MIC32 5
  87. #define IEEE802154_SCF_SECLEVEL_ENC_MIC64 6
  88. #define IEEE802154_SCF_SECLEVEL_ENC_MIC128 7
  89. /* MAC footer size */
  90. #define IEEE802154_MFR_SIZE 2 /* 2 octets */
  91. /* MAC's Command Frames Identifiers */
  92. #define IEEE802154_CMD_ASSOCIATION_REQ 0x01
  93. #define IEEE802154_CMD_ASSOCIATION_RESP 0x02
  94. #define IEEE802154_CMD_DISASSOCIATION_NOTIFY 0x03
  95. #define IEEE802154_CMD_DATA_REQ 0x04
  96. #define IEEE802154_CMD_PANID_CONFLICT_NOTIFY 0x05
  97. #define IEEE802154_CMD_ORPHAN_NOTIFY 0x06
  98. #define IEEE802154_CMD_BEACON_REQ 0x07
  99. #define IEEE802154_CMD_COORD_REALIGN_NOTIFY 0x08
  100. #define IEEE802154_CMD_GTS_REQ 0x09
  101. /*
  102. * The return values of MAC operations
  103. */
  104. enum {
  105. /*
  106. * The requested operation was completed successfully.
  107. * For a transmission request, this value indicates
  108. * a successful transmission.
  109. */
  110. IEEE802154_SUCCESS = 0x0,
  111. /* The beacon was lost following a synchronization request. */
  112. IEEE802154_BEACON_LOSS = 0xe0,
  113. /*
  114. * A transmission could not take place due to activity on the
  115. * channel, i.e., the CSMA-CA mechanism has failed.
  116. */
  117. IEEE802154_CHNL_ACCESS_FAIL = 0xe1,
  118. /* The GTS request has been denied by the PAN coordinator. */
  119. IEEE802154_DENINED = 0xe2,
  120. /* The attempt to disable the transceiver has failed. */
  121. IEEE802154_DISABLE_TRX_FAIL = 0xe3,
  122. /*
  123. * The received frame induces a failed security check according to
  124. * the security suite.
  125. */
  126. IEEE802154_FAILED_SECURITY_CHECK = 0xe4,
  127. /*
  128. * The frame resulting from secure processing has a length that is
  129. * greater than aMACMaxFrameSize.
  130. */
  131. IEEE802154_FRAME_TOO_LONG = 0xe5,
  132. /*
  133. * The requested GTS transmission failed because the specified GTS
  134. * either did not have a transmit GTS direction or was not defined.
  135. */
  136. IEEE802154_INVALID_GTS = 0xe6,
  137. /*
  138. * A request to purge an MSDU from the transaction queue was made using
  139. * an MSDU handle that was not found in the transaction table.
  140. */
  141. IEEE802154_INVALID_HANDLE = 0xe7,
  142. /* A parameter in the primitive is out of the valid range.*/
  143. IEEE802154_INVALID_PARAMETER = 0xe8,
  144. /* No acknowledgment was received after aMaxFrameRetries. */
  145. IEEE802154_NO_ACK = 0xe9,
  146. /* A scan operation failed to find any network beacons.*/
  147. IEEE802154_NO_BEACON = 0xea,
  148. /* No response data were available following a request. */
  149. IEEE802154_NO_DATA = 0xeb,
  150. /* The operation failed because a short address was not allocated. */
  151. IEEE802154_NO_SHORT_ADDRESS = 0xec,
  152. /*
  153. * A receiver enable request was unsuccessful because it could not be
  154. * completed within the CAP.
  155. */
  156. IEEE802154_OUT_OF_CAP = 0xed,
  157. /*
  158. * A PAN identifier conflict has been detected and communicated to the
  159. * PAN coordinator.
  160. */
  161. IEEE802154_PANID_CONFLICT = 0xee,
  162. /* A coordinator realignment command has been received. */
  163. IEEE802154_REALIGMENT = 0xef,
  164. /* The transaction has expired and its information discarded. */
  165. IEEE802154_TRANSACTION_EXPIRED = 0xf0,
  166. /* There is no capacity to store the transaction. */
  167. IEEE802154_TRANSACTION_OVERFLOW = 0xf1,
  168. /*
  169. * The transceiver was in the transmitter enabled state when the
  170. * receiver was requested to be enabled.
  171. */
  172. IEEE802154_TX_ACTIVE = 0xf2,
  173. /* The appropriate key is not available in the ACL. */
  174. IEEE802154_UNAVAILABLE_KEY = 0xf3,
  175. /*
  176. * A SET/GET request was issued with the identifier of a PIB attribute
  177. * that is not supported.
  178. */
  179. IEEE802154_UNSUPPORTED_ATTR = 0xf4,
  180. /*
  181. * A request to perform a scan operation failed because the MLME was
  182. * in the process of performing a previously initiated scan operation.
  183. */
  184. IEEE802154_SCAN_IN_PROGRESS = 0xfc,
  185. };
  186. /**
  187. * ieee802154_is_valid_psdu_len - check if psdu len is valid
  188. * available lengths:
  189. * 0-4 Reserved
  190. * 5 MPDU (Acknowledgment)
  191. * 6-8 Reserved
  192. * 9-127 MPDU
  193. *
  194. * @len: psdu len with (MHR + payload + MFR)
  195. */
  196. static inline bool ieee802154_is_valid_psdu_len(const u8 len)
  197. {
  198. return (len == IEEE802154_ACK_PSDU_LEN ||
  199. (len >= IEEE802154_MIN_PSDU_LEN && len <= IEEE802154_MTU));
  200. }
  201. /**
  202. * ieee802154_is_valid_psdu_len - check if extended addr is valid
  203. * @addr: extended addr to check
  204. */
  205. static inline bool ieee802154_is_valid_extended_addr(const __le64 addr)
  206. {
  207. /* These EUI-64 addresses are reserved by IEEE. 0xffffffffffffffff
  208. * is used internally as extended to short address broadcast mapping.
  209. * This is currently a workaround because neighbor discovery can't
  210. * deal with short addresses types right now.
  211. */
  212. return ((addr != cpu_to_le64(0x0000000000000000ULL)) &&
  213. (addr != cpu_to_le64(0xffffffffffffffffULL)));
  214. }
  215. /**
  216. * ieee802154_random_extended_addr - generates a random extended address
  217. * @addr: extended addr pointer to place the random address
  218. */
  219. static inline void ieee802154_random_extended_addr(__le64 *addr)
  220. {
  221. get_random_bytes(addr, IEEE802154_EXTENDED_ADDR_LEN);
  222. /* toggle some bit if we hit an invalid extended addr */
  223. if (!ieee802154_is_valid_extended_addr(*addr))
  224. ((u8 *)addr)[IEEE802154_EXTENDED_ADDR_LEN - 1] ^= 0x01;
  225. }
  226. #endif /* LINUX_IEEE802154_H */