ieee802154_netdev.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * An interface between IEEE802.15.4 device and rest of the kernel.
  3. *
  4. * Copyright (C) 2007-2012 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. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Written by:
  20. * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
  21. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  22. * Maxim Osipov <maxim.osipov@siemens.com>
  23. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  24. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  25. */
  26. #ifndef IEEE802154_NETDEVICE_H
  27. #define IEEE802154_NETDEVICE_H
  28. #include <net/af_ieee802154.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/skbuff.h>
  31. struct ieee802154_sechdr {
  32. #if defined(__LITTLE_ENDIAN_BITFIELD)
  33. u8 level:3,
  34. key_id_mode:2,
  35. reserved:3;
  36. #elif defined(__BIG_ENDIAN_BITFIELD)
  37. u8 reserved:3,
  38. key_id_mode:2,
  39. level:3;
  40. #else
  41. #error "Please fix <asm/byteorder.h>"
  42. #endif
  43. u8 key_id;
  44. __le32 frame_counter;
  45. union {
  46. __le32 short_src;
  47. __le64 extended_src;
  48. };
  49. };
  50. struct ieee802154_addr {
  51. u8 mode;
  52. __le16 pan_id;
  53. union {
  54. __le16 short_addr;
  55. __le64 extended_addr;
  56. };
  57. };
  58. struct ieee802154_hdr_fc {
  59. #if defined(__LITTLE_ENDIAN_BITFIELD)
  60. u16 type:3,
  61. security_enabled:1,
  62. frame_pending:1,
  63. ack_request:1,
  64. intra_pan:1,
  65. reserved:3,
  66. dest_addr_mode:2,
  67. version:2,
  68. source_addr_mode:2;
  69. #elif defined(__BIG_ENDIAN_BITFIELD)
  70. u16 reserved:1,
  71. intra_pan:1,
  72. ack_request:1,
  73. frame_pending:1,
  74. security_enabled:1,
  75. type:3,
  76. source_addr_mode:2,
  77. version:2,
  78. dest_addr_mode:2,
  79. reserved2:2;
  80. #else
  81. #error "Please fix <asm/byteorder.h>"
  82. #endif
  83. };
  84. struct ieee802154_hdr {
  85. struct ieee802154_hdr_fc fc;
  86. u8 seq;
  87. struct ieee802154_addr source;
  88. struct ieee802154_addr dest;
  89. struct ieee802154_sechdr sec;
  90. };
  91. /* pushes hdr onto the skb. fields of hdr->fc that can be calculated from
  92. * the contents of hdr will be, and the actual value of those bits in
  93. * hdr->fc will be ignored. this includes the INTRA_PAN bit and the frame
  94. * version, if SECEN is set.
  95. */
  96. int ieee802154_hdr_push(struct sk_buff *skb, const struct ieee802154_hdr *hdr);
  97. /* pulls the entire 802.15.4 header off of the skb, including the security
  98. * header, and performs pan id decompression
  99. */
  100. int ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr);
  101. /* parses the frame control, sequence number of address fields in a given skb
  102. * and stores them into hdr, performing pan id decompression and length checks
  103. * to be suitable for use in header_ops.parse
  104. */
  105. int ieee802154_hdr_peek_addrs(const struct sk_buff *skb,
  106. struct ieee802154_hdr *hdr);
  107. static inline int ieee802154_hdr_length(struct sk_buff *skb)
  108. {
  109. struct ieee802154_hdr hdr;
  110. int len = ieee802154_hdr_pull(skb, &hdr);
  111. if (len > 0)
  112. skb_push(skb, len);
  113. return len;
  114. }
  115. static inline bool ieee802154_addr_equal(const struct ieee802154_addr *a1,
  116. const struct ieee802154_addr *a2)
  117. {
  118. if (a1->pan_id != a2->pan_id || a1->mode != a2->mode)
  119. return false;
  120. if ((a1->mode == IEEE802154_ADDR_LONG &&
  121. a1->extended_addr != a2->extended_addr) ||
  122. (a1->mode == IEEE802154_ADDR_SHORT &&
  123. a1->short_addr != a2->short_addr))
  124. return false;
  125. return true;
  126. }
  127. static inline __le64 ieee802154_devaddr_from_raw(const void *raw)
  128. {
  129. u64 temp;
  130. memcpy(&temp, raw, IEEE802154_ADDR_LEN);
  131. return (__force __le64)swab64(temp);
  132. }
  133. static inline void ieee802154_devaddr_to_raw(void *raw, __le64 addr)
  134. {
  135. u64 temp = swab64((__force u64)addr);
  136. memcpy(raw, &temp, IEEE802154_ADDR_LEN);
  137. }
  138. static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a,
  139. const struct ieee802154_addr_sa *sa)
  140. {
  141. a->mode = sa->addr_type;
  142. a->pan_id = cpu_to_le16(sa->pan_id);
  143. switch (a->mode) {
  144. case IEEE802154_ADDR_SHORT:
  145. a->short_addr = cpu_to_le16(sa->short_addr);
  146. break;
  147. case IEEE802154_ADDR_LONG:
  148. a->extended_addr = ieee802154_devaddr_from_raw(sa->hwaddr);
  149. break;
  150. }
  151. }
  152. static inline void ieee802154_addr_to_sa(struct ieee802154_addr_sa *sa,
  153. const struct ieee802154_addr *a)
  154. {
  155. sa->addr_type = a->mode;
  156. sa->pan_id = le16_to_cpu(a->pan_id);
  157. switch (a->mode) {
  158. case IEEE802154_ADDR_SHORT:
  159. sa->short_addr = le16_to_cpu(a->short_addr);
  160. break;
  161. case IEEE802154_ADDR_LONG:
  162. ieee802154_devaddr_to_raw(sa->hwaddr, a->extended_addr);
  163. break;
  164. }
  165. }
  166. /*
  167. * A control block of skb passed between the ARPHRD_IEEE802154 device
  168. * and other stack parts.
  169. */
  170. struct ieee802154_mac_cb {
  171. u8 lqi;
  172. u8 flags;
  173. u8 seq;
  174. struct ieee802154_addr source;
  175. struct ieee802154_addr dest;
  176. };
  177. static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb)
  178. {
  179. return (struct ieee802154_mac_cb *)skb->cb;
  180. }
  181. #define MAC_CB_FLAG_TYPEMASK ((1 << 3) - 1)
  182. #define MAC_CB_FLAG_ACKREQ (1 << 3)
  183. #define MAC_CB_FLAG_SECEN (1 << 4)
  184. static inline bool mac_cb_is_ackreq(struct sk_buff *skb)
  185. {
  186. return mac_cb(skb)->flags & MAC_CB_FLAG_ACKREQ;
  187. }
  188. static inline bool mac_cb_is_secen(struct sk_buff *skb)
  189. {
  190. return mac_cb(skb)->flags & MAC_CB_FLAG_SECEN;
  191. }
  192. static inline int mac_cb_type(struct sk_buff *skb)
  193. {
  194. return mac_cb(skb)->flags & MAC_CB_FLAG_TYPEMASK;
  195. }
  196. #define IEEE802154_MAC_SCAN_ED 0
  197. #define IEEE802154_MAC_SCAN_ACTIVE 1
  198. #define IEEE802154_MAC_SCAN_PASSIVE 2
  199. #define IEEE802154_MAC_SCAN_ORPHAN 3
  200. struct ieee802154_mac_params {
  201. s8 transmit_power;
  202. u8 min_be;
  203. u8 max_be;
  204. u8 csma_retries;
  205. s8 frame_retries;
  206. bool lbt;
  207. u8 cca_mode;
  208. s32 cca_ed_level;
  209. };
  210. struct wpan_phy;
  211. /*
  212. * This should be located at net_device->ml_priv
  213. *
  214. * get_phy should increment the reference counting on returned phy.
  215. * Use wpan_wpy_put to put that reference.
  216. */
  217. struct ieee802154_mlme_ops {
  218. /* The following fields are optional (can be NULL). */
  219. int (*assoc_req)(struct net_device *dev,
  220. struct ieee802154_addr *addr,
  221. u8 channel, u8 page, u8 cap);
  222. int (*assoc_resp)(struct net_device *dev,
  223. struct ieee802154_addr *addr,
  224. __le16 short_addr, u8 status);
  225. int (*disassoc_req)(struct net_device *dev,
  226. struct ieee802154_addr *addr,
  227. u8 reason);
  228. int (*start_req)(struct net_device *dev,
  229. struct ieee802154_addr *addr,
  230. u8 channel, u8 page, u8 bcn_ord, u8 sf_ord,
  231. u8 pan_coord, u8 blx, u8 coord_realign);
  232. int (*scan_req)(struct net_device *dev,
  233. u8 type, u32 channels, u8 page, u8 duration);
  234. int (*set_mac_params)(struct net_device *dev,
  235. const struct ieee802154_mac_params *params);
  236. void (*get_mac_params)(struct net_device *dev,
  237. struct ieee802154_mac_params *params);
  238. /* The fields below are required. */
  239. struct wpan_phy *(*get_phy)(const struct net_device *dev);
  240. /*
  241. * FIXME: these should become the part of PIB/MIB interface.
  242. * However we still don't have IB interface of any kind
  243. */
  244. __le16 (*get_pan_id)(const struct net_device *dev);
  245. __le16 (*get_short_addr)(const struct net_device *dev);
  246. u8 (*get_dsn)(const struct net_device *dev);
  247. };
  248. /* The IEEE 802.15.4 standard defines 2 type of the devices:
  249. * - FFD - full functionality device
  250. * - RFD - reduce functionality device
  251. *
  252. * So 2 sets of mlme operations are needed
  253. */
  254. struct ieee802154_reduced_mlme_ops {
  255. struct wpan_phy *(*get_phy)(const struct net_device *dev);
  256. };
  257. static inline struct ieee802154_mlme_ops *
  258. ieee802154_mlme_ops(const struct net_device *dev)
  259. {
  260. return dev->ml_priv;
  261. }
  262. static inline struct ieee802154_reduced_mlme_ops *
  263. ieee802154_reduced_mlme_ops(const struct net_device *dev)
  264. {
  265. return dev->ml_priv;
  266. }
  267. #endif