ieee802154_netdev.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. * 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 IEEE802154_NETDEVICE_H
  23. #define IEEE802154_NETDEVICE_H
  24. #include <net/af_ieee802154.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/ieee802154.h>
  28. #include <net/cfg802154.h>
  29. struct ieee802154_sechdr {
  30. #if defined(__LITTLE_ENDIAN_BITFIELD)
  31. u8 level:3,
  32. key_id_mode:2,
  33. reserved:3;
  34. #elif defined(__BIG_ENDIAN_BITFIELD)
  35. u8 reserved:3,
  36. key_id_mode:2,
  37. level:3;
  38. #else
  39. #error "Please fix <asm/byteorder.h>"
  40. #endif
  41. u8 key_id;
  42. __le32 frame_counter;
  43. union {
  44. __le32 short_src;
  45. __le64 extended_src;
  46. };
  47. };
  48. struct ieee802154_hdr_fc {
  49. #if defined(__LITTLE_ENDIAN_BITFIELD)
  50. u16 type:3,
  51. security_enabled:1,
  52. frame_pending:1,
  53. ack_request:1,
  54. intra_pan:1,
  55. reserved:3,
  56. dest_addr_mode:2,
  57. version:2,
  58. source_addr_mode:2;
  59. #elif defined(__BIG_ENDIAN_BITFIELD)
  60. u16 reserved:1,
  61. intra_pan:1,
  62. ack_request:1,
  63. frame_pending:1,
  64. security_enabled:1,
  65. type:3,
  66. source_addr_mode:2,
  67. version:2,
  68. dest_addr_mode:2,
  69. reserved2:2;
  70. #else
  71. #error "Please fix <asm/byteorder.h>"
  72. #endif
  73. };
  74. struct ieee802154_hdr {
  75. struct ieee802154_hdr_fc fc;
  76. u8 seq;
  77. struct ieee802154_addr source;
  78. struct ieee802154_addr dest;
  79. struct ieee802154_sechdr sec;
  80. };
  81. /* pushes hdr onto the skb. fields of hdr->fc that can be calculated from
  82. * the contents of hdr will be, and the actual value of those bits in
  83. * hdr->fc will be ignored. this includes the INTRA_PAN bit and the frame
  84. * version, if SECEN is set.
  85. */
  86. int ieee802154_hdr_push(struct sk_buff *skb, struct ieee802154_hdr *hdr);
  87. /* pulls the entire 802.15.4 header off of the skb, including the security
  88. * header, and performs pan id decompression
  89. */
  90. int ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr);
  91. /* parses the frame control, sequence number of address fields in a given skb
  92. * and stores them into hdr, performing pan id decompression and length checks
  93. * to be suitable for use in header_ops.parse
  94. */
  95. int ieee802154_hdr_peek_addrs(const struct sk_buff *skb,
  96. struct ieee802154_hdr *hdr);
  97. /* parses the full 802.15.4 header a given skb and stores them into hdr,
  98. * performing pan id decompression and length checks to be suitable for use in
  99. * header_ops.parse
  100. */
  101. int ieee802154_hdr_peek(const struct sk_buff *skb, struct ieee802154_hdr *hdr);
  102. int ieee802154_max_payload(const struct ieee802154_hdr *hdr);
  103. static inline int
  104. ieee802154_sechdr_authtag_len(const struct ieee802154_sechdr *sec)
  105. {
  106. switch (sec->level) {
  107. case IEEE802154_SCF_SECLEVEL_MIC32:
  108. case IEEE802154_SCF_SECLEVEL_ENC_MIC32:
  109. return 4;
  110. case IEEE802154_SCF_SECLEVEL_MIC64:
  111. case IEEE802154_SCF_SECLEVEL_ENC_MIC64:
  112. return 8;
  113. case IEEE802154_SCF_SECLEVEL_MIC128:
  114. case IEEE802154_SCF_SECLEVEL_ENC_MIC128:
  115. return 16;
  116. case IEEE802154_SCF_SECLEVEL_NONE:
  117. case IEEE802154_SCF_SECLEVEL_ENC:
  118. default:
  119. return 0;
  120. }
  121. }
  122. static inline int ieee802154_hdr_length(struct sk_buff *skb)
  123. {
  124. struct ieee802154_hdr hdr;
  125. int len = ieee802154_hdr_pull(skb, &hdr);
  126. if (len > 0)
  127. skb_push(skb, len);
  128. return len;
  129. }
  130. static inline bool ieee802154_addr_equal(const struct ieee802154_addr *a1,
  131. const struct ieee802154_addr *a2)
  132. {
  133. if (a1->pan_id != a2->pan_id || a1->mode != a2->mode)
  134. return false;
  135. if ((a1->mode == IEEE802154_ADDR_LONG &&
  136. a1->extended_addr != a2->extended_addr) ||
  137. (a1->mode == IEEE802154_ADDR_SHORT &&
  138. a1->short_addr != a2->short_addr))
  139. return false;
  140. return true;
  141. }
  142. static inline __le64 ieee802154_devaddr_from_raw(const void *raw)
  143. {
  144. u64 temp;
  145. memcpy(&temp, raw, IEEE802154_ADDR_LEN);
  146. return (__force __le64)swab64(temp);
  147. }
  148. static inline void ieee802154_devaddr_to_raw(void *raw, __le64 addr)
  149. {
  150. u64 temp = swab64((__force u64)addr);
  151. memcpy(raw, &temp, IEEE802154_ADDR_LEN);
  152. }
  153. static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a,
  154. const struct ieee802154_addr_sa *sa)
  155. {
  156. a->mode = sa->addr_type;
  157. a->pan_id = cpu_to_le16(sa->pan_id);
  158. switch (a->mode) {
  159. case IEEE802154_ADDR_SHORT:
  160. a->short_addr = cpu_to_le16(sa->short_addr);
  161. break;
  162. case IEEE802154_ADDR_LONG:
  163. a->extended_addr = ieee802154_devaddr_from_raw(sa->hwaddr);
  164. break;
  165. }
  166. }
  167. static inline void ieee802154_addr_to_sa(struct ieee802154_addr_sa *sa,
  168. const struct ieee802154_addr *a)
  169. {
  170. sa->addr_type = a->mode;
  171. sa->pan_id = le16_to_cpu(a->pan_id);
  172. switch (a->mode) {
  173. case IEEE802154_ADDR_SHORT:
  174. sa->short_addr = le16_to_cpu(a->short_addr);
  175. break;
  176. case IEEE802154_ADDR_LONG:
  177. ieee802154_devaddr_to_raw(sa->hwaddr, a->extended_addr);
  178. break;
  179. }
  180. }
  181. /*
  182. * A control block of skb passed between the ARPHRD_IEEE802154 device
  183. * and other stack parts.
  184. */
  185. struct ieee802154_mac_cb {
  186. u8 lqi;
  187. u8 type;
  188. bool ackreq;
  189. bool secen;
  190. bool secen_override;
  191. u8 seclevel;
  192. bool seclevel_override;
  193. struct ieee802154_addr source;
  194. struct ieee802154_addr dest;
  195. };
  196. static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb)
  197. {
  198. return (struct ieee802154_mac_cb *)skb->cb;
  199. }
  200. static inline struct ieee802154_mac_cb *mac_cb_init(struct sk_buff *skb)
  201. {
  202. BUILD_BUG_ON(sizeof(struct ieee802154_mac_cb) > sizeof(skb->cb));
  203. memset(skb->cb, 0, sizeof(struct ieee802154_mac_cb));
  204. return mac_cb(skb);
  205. }
  206. enum {
  207. IEEE802154_LLSEC_DEVKEY_IGNORE,
  208. IEEE802154_LLSEC_DEVKEY_RESTRICT,
  209. IEEE802154_LLSEC_DEVKEY_RECORD,
  210. __IEEE802154_LLSEC_DEVKEY_MAX,
  211. };
  212. #define IEEE802154_MAC_SCAN_ED 0
  213. #define IEEE802154_MAC_SCAN_ACTIVE 1
  214. #define IEEE802154_MAC_SCAN_PASSIVE 2
  215. #define IEEE802154_MAC_SCAN_ORPHAN 3
  216. struct ieee802154_mac_params {
  217. s8 transmit_power;
  218. u8 min_be;
  219. u8 max_be;
  220. u8 csma_retries;
  221. s8 frame_retries;
  222. bool lbt;
  223. struct wpan_phy_cca cca;
  224. s32 cca_ed_level;
  225. };
  226. struct wpan_phy;
  227. enum {
  228. IEEE802154_LLSEC_PARAM_ENABLED = BIT(0),
  229. IEEE802154_LLSEC_PARAM_FRAME_COUNTER = BIT(1),
  230. IEEE802154_LLSEC_PARAM_OUT_LEVEL = BIT(2),
  231. IEEE802154_LLSEC_PARAM_OUT_KEY = BIT(3),
  232. IEEE802154_LLSEC_PARAM_KEY_SOURCE = BIT(4),
  233. IEEE802154_LLSEC_PARAM_PAN_ID = BIT(5),
  234. IEEE802154_LLSEC_PARAM_HWADDR = BIT(6),
  235. IEEE802154_LLSEC_PARAM_COORD_HWADDR = BIT(7),
  236. IEEE802154_LLSEC_PARAM_COORD_SHORTADDR = BIT(8),
  237. };
  238. struct ieee802154_llsec_ops {
  239. int (*get_params)(struct net_device *dev,
  240. struct ieee802154_llsec_params *params);
  241. int (*set_params)(struct net_device *dev,
  242. const struct ieee802154_llsec_params *params,
  243. int changed);
  244. int (*add_key)(struct net_device *dev,
  245. const struct ieee802154_llsec_key_id *id,
  246. const struct ieee802154_llsec_key *key);
  247. int (*del_key)(struct net_device *dev,
  248. const struct ieee802154_llsec_key_id *id);
  249. int (*add_dev)(struct net_device *dev,
  250. const struct ieee802154_llsec_device *llsec_dev);
  251. int (*del_dev)(struct net_device *dev, __le64 dev_addr);
  252. int (*add_devkey)(struct net_device *dev,
  253. __le64 device_addr,
  254. const struct ieee802154_llsec_device_key *key);
  255. int (*del_devkey)(struct net_device *dev,
  256. __le64 device_addr,
  257. const struct ieee802154_llsec_device_key *key);
  258. int (*add_seclevel)(struct net_device *dev,
  259. const struct ieee802154_llsec_seclevel *sl);
  260. int (*del_seclevel)(struct net_device *dev,
  261. const struct ieee802154_llsec_seclevel *sl);
  262. void (*lock_table)(struct net_device *dev);
  263. void (*get_table)(struct net_device *dev,
  264. struct ieee802154_llsec_table **t);
  265. void (*unlock_table)(struct net_device *dev);
  266. };
  267. /*
  268. * This should be located at net_device->ml_priv
  269. *
  270. * get_phy should increment the reference counting on returned phy.
  271. * Use wpan_wpy_put to put that reference.
  272. */
  273. struct ieee802154_mlme_ops {
  274. /* The following fields are optional (can be NULL). */
  275. int (*assoc_req)(struct net_device *dev,
  276. struct ieee802154_addr *addr,
  277. u8 channel, u8 page, u8 cap);
  278. int (*assoc_resp)(struct net_device *dev,
  279. struct ieee802154_addr *addr,
  280. __le16 short_addr, u8 status);
  281. int (*disassoc_req)(struct net_device *dev,
  282. struct ieee802154_addr *addr,
  283. u8 reason);
  284. int (*start_req)(struct net_device *dev,
  285. struct ieee802154_addr *addr,
  286. u8 channel, u8 page, u8 bcn_ord, u8 sf_ord,
  287. u8 pan_coord, u8 blx, u8 coord_realign);
  288. int (*scan_req)(struct net_device *dev,
  289. u8 type, u32 channels, u8 page, u8 duration);
  290. int (*set_mac_params)(struct net_device *dev,
  291. const struct ieee802154_mac_params *params);
  292. void (*get_mac_params)(struct net_device *dev,
  293. struct ieee802154_mac_params *params);
  294. struct ieee802154_llsec_ops *llsec;
  295. };
  296. static inline struct ieee802154_mlme_ops *
  297. ieee802154_mlme_ops(const struct net_device *dev)
  298. {
  299. return dev->ml_priv;
  300. }
  301. #endif