ieee802154_netdev.h 9.9 KB

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