ieee802154_netdev.h 11 KB

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