cfg802154.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright (C) 2007, 2008, 2009 Siemens AG
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * Written by:
  14. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  15. */
  16. #ifndef __NET_CFG802154_H
  17. #define __NET_CFG802154_H
  18. #include <linux/ieee802154.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/mutex.h>
  21. #include <linux/bug.h>
  22. #include <net/nl802154.h>
  23. struct wpan_phy;
  24. struct wpan_phy_cca;
  25. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  26. struct ieee802154_llsec_device_key;
  27. struct ieee802154_llsec_seclevel;
  28. struct ieee802154_llsec_params;
  29. struct ieee802154_llsec_device;
  30. struct ieee802154_llsec_table;
  31. struct ieee802154_llsec_key_id;
  32. struct ieee802154_llsec_key;
  33. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  34. struct cfg802154_ops {
  35. struct net_device * (*add_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
  36. const char *name,
  37. unsigned char name_assign_type,
  38. int type);
  39. void (*del_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
  40. struct net_device *dev);
  41. int (*suspend)(struct wpan_phy *wpan_phy);
  42. int (*resume)(struct wpan_phy *wpan_phy);
  43. int (*add_virtual_intf)(struct wpan_phy *wpan_phy,
  44. const char *name,
  45. unsigned char name_assign_type,
  46. enum nl802154_iftype type,
  47. __le64 extended_addr);
  48. int (*del_virtual_intf)(struct wpan_phy *wpan_phy,
  49. struct wpan_dev *wpan_dev);
  50. int (*set_channel)(struct wpan_phy *wpan_phy, u8 page, u8 channel);
  51. int (*set_cca_mode)(struct wpan_phy *wpan_phy,
  52. const struct wpan_phy_cca *cca);
  53. int (*set_cca_ed_level)(struct wpan_phy *wpan_phy, s32 ed_level);
  54. int (*set_tx_power)(struct wpan_phy *wpan_phy, s32 power);
  55. int (*set_pan_id)(struct wpan_phy *wpan_phy,
  56. struct wpan_dev *wpan_dev, __le16 pan_id);
  57. int (*set_short_addr)(struct wpan_phy *wpan_phy,
  58. struct wpan_dev *wpan_dev, __le16 short_addr);
  59. int (*set_backoff_exponent)(struct wpan_phy *wpan_phy,
  60. struct wpan_dev *wpan_dev, u8 min_be,
  61. u8 max_be);
  62. int (*set_max_csma_backoffs)(struct wpan_phy *wpan_phy,
  63. struct wpan_dev *wpan_dev,
  64. u8 max_csma_backoffs);
  65. int (*set_max_frame_retries)(struct wpan_phy *wpan_phy,
  66. struct wpan_dev *wpan_dev,
  67. s8 max_frame_retries);
  68. int (*set_lbt_mode)(struct wpan_phy *wpan_phy,
  69. struct wpan_dev *wpan_dev, bool mode);
  70. int (*set_ackreq_default)(struct wpan_phy *wpan_phy,
  71. struct wpan_dev *wpan_dev, bool ackreq);
  72. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  73. void (*get_llsec_table)(struct wpan_phy *wpan_phy,
  74. struct wpan_dev *wpan_dev,
  75. struct ieee802154_llsec_table **table);
  76. void (*lock_llsec_table)(struct wpan_phy *wpan_phy,
  77. struct wpan_dev *wpan_dev);
  78. void (*unlock_llsec_table)(struct wpan_phy *wpan_phy,
  79. struct wpan_dev *wpan_dev);
  80. /* TODO remove locking/get table callbacks, this is part of the
  81. * nl802154 interface and should be accessible from ieee802154 layer.
  82. */
  83. int (*get_llsec_params)(struct wpan_phy *wpan_phy,
  84. struct wpan_dev *wpan_dev,
  85. struct ieee802154_llsec_params *params);
  86. int (*set_llsec_params)(struct wpan_phy *wpan_phy,
  87. struct wpan_dev *wpan_dev,
  88. const struct ieee802154_llsec_params *params,
  89. int changed);
  90. int (*add_llsec_key)(struct wpan_phy *wpan_phy,
  91. struct wpan_dev *wpan_dev,
  92. const struct ieee802154_llsec_key_id *id,
  93. const struct ieee802154_llsec_key *key);
  94. int (*del_llsec_key)(struct wpan_phy *wpan_phy,
  95. struct wpan_dev *wpan_dev,
  96. const struct ieee802154_llsec_key_id *id);
  97. int (*add_seclevel)(struct wpan_phy *wpan_phy,
  98. struct wpan_dev *wpan_dev,
  99. const struct ieee802154_llsec_seclevel *sl);
  100. int (*del_seclevel)(struct wpan_phy *wpan_phy,
  101. struct wpan_dev *wpan_dev,
  102. const struct ieee802154_llsec_seclevel *sl);
  103. int (*add_device)(struct wpan_phy *wpan_phy,
  104. struct wpan_dev *wpan_dev,
  105. const struct ieee802154_llsec_device *dev);
  106. int (*del_device)(struct wpan_phy *wpan_phy,
  107. struct wpan_dev *wpan_dev, __le64 extended_addr);
  108. int (*add_devkey)(struct wpan_phy *wpan_phy,
  109. struct wpan_dev *wpan_dev,
  110. __le64 extended_addr,
  111. const struct ieee802154_llsec_device_key *key);
  112. int (*del_devkey)(struct wpan_phy *wpan_phy,
  113. struct wpan_dev *wpan_dev,
  114. __le64 extended_addr,
  115. const struct ieee802154_llsec_device_key *key);
  116. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  117. };
  118. static inline bool
  119. wpan_phy_supported_bool(bool b, enum nl802154_supported_bool_states st)
  120. {
  121. switch (st) {
  122. case NL802154_SUPPORTED_BOOL_TRUE:
  123. return b;
  124. case NL802154_SUPPORTED_BOOL_FALSE:
  125. return !b;
  126. case NL802154_SUPPORTED_BOOL_BOTH:
  127. return true;
  128. default:
  129. WARN_ON(1);
  130. }
  131. return false;
  132. }
  133. struct wpan_phy_supported {
  134. u32 channels[IEEE802154_MAX_PAGE + 1],
  135. cca_modes, cca_opts, iftypes;
  136. enum nl802154_supported_bool_states lbt;
  137. u8 min_minbe, max_minbe, min_maxbe, max_maxbe,
  138. min_csma_backoffs, max_csma_backoffs;
  139. s8 min_frame_retries, max_frame_retries;
  140. size_t tx_powers_size, cca_ed_levels_size;
  141. const s32 *tx_powers, *cca_ed_levels;
  142. };
  143. struct wpan_phy_cca {
  144. enum nl802154_cca_modes mode;
  145. enum nl802154_cca_opts opt;
  146. };
  147. static inline bool
  148. wpan_phy_cca_cmp(const struct wpan_phy_cca *a, const struct wpan_phy_cca *b)
  149. {
  150. if (a->mode != b->mode)
  151. return false;
  152. if (a->mode == NL802154_CCA_ENERGY_CARRIER)
  153. return a->opt == b->opt;
  154. return true;
  155. }
  156. /**
  157. * @WPAN_PHY_FLAG_TRANSMIT_POWER: Indicates that transceiver will support
  158. * transmit power setting.
  159. * @WPAN_PHY_FLAG_CCA_ED_LEVEL: Indicates that transceiver will support cca ed
  160. * level setting.
  161. * @WPAN_PHY_FLAG_CCA_MODE: Indicates that transceiver will support cca mode
  162. * setting.
  163. */
  164. enum wpan_phy_flags {
  165. WPAN_PHY_FLAG_TXPOWER = BIT(1),
  166. WPAN_PHY_FLAG_CCA_ED_LEVEL = BIT(2),
  167. WPAN_PHY_FLAG_CCA_MODE = BIT(3),
  168. };
  169. struct wpan_phy {
  170. /* If multiple wpan_phys are registered and you're handed e.g.
  171. * a regular netdev with assigned ieee802154_ptr, you won't
  172. * know whether it points to a wpan_phy your driver has registered
  173. * or not. Assign this to something global to your driver to
  174. * help determine whether you own this wpan_phy or not.
  175. */
  176. const void *privid;
  177. u32 flags;
  178. /*
  179. * This is a PIB according to 802.15.4-2011.
  180. * We do not provide timing-related variables, as they
  181. * aren't used outside of driver
  182. */
  183. u8 current_channel;
  184. u8 current_page;
  185. struct wpan_phy_supported supported;
  186. /* current transmit_power in mBm */
  187. s32 transmit_power;
  188. struct wpan_phy_cca cca;
  189. __le64 perm_extended_addr;
  190. /* current cca ed threshold in mBm */
  191. s32 cca_ed_level;
  192. /* PHY depended MAC PIB values */
  193. /* 802.15.4 acronym: Tdsym in usec */
  194. u8 symbol_duration;
  195. /* lifs and sifs periods timing */
  196. u16 lifs_period;
  197. u16 sifs_period;
  198. struct device dev;
  199. char priv[0] __aligned(NETDEV_ALIGN);
  200. };
  201. struct ieee802154_addr {
  202. u8 mode;
  203. __le16 pan_id;
  204. union {
  205. __le16 short_addr;
  206. __le64 extended_addr;
  207. };
  208. };
  209. struct ieee802154_llsec_key_id {
  210. u8 mode;
  211. u8 id;
  212. union {
  213. struct ieee802154_addr device_addr;
  214. __le32 short_source;
  215. __le64 extended_source;
  216. };
  217. };
  218. #define IEEE802154_LLSEC_KEY_SIZE 16
  219. struct ieee802154_llsec_key {
  220. u8 frame_types;
  221. u32 cmd_frame_ids;
  222. /* TODO replace with NL802154_KEY_SIZE */
  223. u8 key[IEEE802154_LLSEC_KEY_SIZE];
  224. };
  225. struct ieee802154_llsec_key_entry {
  226. struct list_head list;
  227. struct ieee802154_llsec_key_id id;
  228. struct ieee802154_llsec_key *key;
  229. };
  230. struct ieee802154_llsec_params {
  231. bool enabled;
  232. __be32 frame_counter;
  233. u8 out_level;
  234. struct ieee802154_llsec_key_id out_key;
  235. __le64 default_key_source;
  236. __le16 pan_id;
  237. __le64 hwaddr;
  238. __le64 coord_hwaddr;
  239. __le16 coord_shortaddr;
  240. };
  241. struct ieee802154_llsec_table {
  242. struct list_head keys;
  243. struct list_head devices;
  244. struct list_head security_levels;
  245. };
  246. struct ieee802154_llsec_seclevel {
  247. struct list_head list;
  248. u8 frame_type;
  249. u8 cmd_frame_id;
  250. bool device_override;
  251. u32 sec_levels;
  252. };
  253. struct ieee802154_llsec_device {
  254. struct list_head list;
  255. __le16 pan_id;
  256. __le16 short_addr;
  257. __le64 hwaddr;
  258. u32 frame_counter;
  259. bool seclevel_exempt;
  260. u8 key_mode;
  261. struct list_head keys;
  262. };
  263. struct ieee802154_llsec_device_key {
  264. struct list_head list;
  265. struct ieee802154_llsec_key_id key_id;
  266. u32 frame_counter;
  267. };
  268. struct wpan_dev_header_ops {
  269. /* TODO create callback currently assumes ieee802154_mac_cb inside
  270. * skb->cb. This should be changed to give these information as
  271. * parameter.
  272. */
  273. int (*create)(struct sk_buff *skb, struct net_device *dev,
  274. const struct ieee802154_addr *daddr,
  275. const struct ieee802154_addr *saddr,
  276. unsigned int len);
  277. };
  278. struct wpan_dev {
  279. struct wpan_phy *wpan_phy;
  280. int iftype;
  281. /* the remainder of this struct should be private to cfg802154 */
  282. struct list_head list;
  283. struct net_device *netdev;
  284. const struct wpan_dev_header_ops *header_ops;
  285. /* lowpan interface, set when the wpan_dev belongs to one lowpan_dev */
  286. struct net_device *lowpan_dev;
  287. u32 identifier;
  288. /* MAC PIB */
  289. __le16 pan_id;
  290. __le16 short_addr;
  291. __le64 extended_addr;
  292. /* MAC BSN field */
  293. atomic_t bsn;
  294. /* MAC DSN field */
  295. atomic_t dsn;
  296. u8 min_be;
  297. u8 max_be;
  298. u8 csma_retries;
  299. s8 frame_retries;
  300. bool lbt;
  301. bool promiscuous_mode;
  302. /* fallback for acknowledgment bit setting */
  303. bool ackreq;
  304. };
  305. #define to_phy(_dev) container_of(_dev, struct wpan_phy, dev)
  306. static inline int
  307. wpan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  308. const struct ieee802154_addr *daddr,
  309. const struct ieee802154_addr *saddr,
  310. unsigned int len)
  311. {
  312. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  313. return wpan_dev->header_ops->create(skb, dev, daddr, saddr, len);
  314. }
  315. struct wpan_phy *
  316. wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size);
  317. static inline void wpan_phy_set_dev(struct wpan_phy *phy, struct device *dev)
  318. {
  319. phy->dev.parent = dev;
  320. }
  321. int wpan_phy_register(struct wpan_phy *phy);
  322. void wpan_phy_unregister(struct wpan_phy *phy);
  323. void wpan_phy_free(struct wpan_phy *phy);
  324. /* Same semantics as for class_for_each_device */
  325. int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), void *data);
  326. static inline void *wpan_phy_priv(struct wpan_phy *phy)
  327. {
  328. BUG_ON(!phy);
  329. return &phy->priv;
  330. }
  331. struct wpan_phy *wpan_phy_find(const char *str);
  332. static inline void wpan_phy_put(struct wpan_phy *phy)
  333. {
  334. put_device(&phy->dev);
  335. }
  336. static inline const char *wpan_phy_name(struct wpan_phy *phy)
  337. {
  338. return dev_name(&phy->dev);
  339. }
  340. #endif /* __NET_CFG802154_H */