ieee802154_netdev.h 10 KB

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