ieee802154_netdev.h 11 KB

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