cfg802154.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. /* the network namespace this phy lives in currently */
  200. possible_net_t _net;
  201. char priv[0] __aligned(NETDEV_ALIGN);
  202. };
  203. static inline struct net *wpan_phy_net(struct wpan_phy *wpan_phy)
  204. {
  205. return read_pnet(&wpan_phy->_net);
  206. }
  207. static inline void wpan_phy_net_set(struct wpan_phy *wpan_phy, struct net *net)
  208. {
  209. write_pnet(&wpan_phy->_net, net);
  210. }
  211. struct ieee802154_addr {
  212. u8 mode;
  213. __le16 pan_id;
  214. union {
  215. __le16 short_addr;
  216. __le64 extended_addr;
  217. };
  218. };
  219. struct ieee802154_llsec_key_id {
  220. u8 mode;
  221. u8 id;
  222. union {
  223. struct ieee802154_addr device_addr;
  224. __le32 short_source;
  225. __le64 extended_source;
  226. };
  227. };
  228. #define IEEE802154_LLSEC_KEY_SIZE 16
  229. struct ieee802154_llsec_key {
  230. u8 frame_types;
  231. u32 cmd_frame_ids;
  232. /* TODO replace with NL802154_KEY_SIZE */
  233. u8 key[IEEE802154_LLSEC_KEY_SIZE];
  234. };
  235. struct ieee802154_llsec_key_entry {
  236. struct list_head list;
  237. struct ieee802154_llsec_key_id id;
  238. struct ieee802154_llsec_key *key;
  239. };
  240. struct ieee802154_llsec_params {
  241. bool enabled;
  242. __be32 frame_counter;
  243. u8 out_level;
  244. struct ieee802154_llsec_key_id out_key;
  245. __le64 default_key_source;
  246. __le16 pan_id;
  247. __le64 hwaddr;
  248. __le64 coord_hwaddr;
  249. __le16 coord_shortaddr;
  250. };
  251. struct ieee802154_llsec_table {
  252. struct list_head keys;
  253. struct list_head devices;
  254. struct list_head security_levels;
  255. };
  256. struct ieee802154_llsec_seclevel {
  257. struct list_head list;
  258. u8 frame_type;
  259. u8 cmd_frame_id;
  260. bool device_override;
  261. u32 sec_levels;
  262. };
  263. struct ieee802154_llsec_device {
  264. struct list_head list;
  265. __le16 pan_id;
  266. __le16 short_addr;
  267. __le64 hwaddr;
  268. u32 frame_counter;
  269. bool seclevel_exempt;
  270. u8 key_mode;
  271. struct list_head keys;
  272. };
  273. struct ieee802154_llsec_device_key {
  274. struct list_head list;
  275. struct ieee802154_llsec_key_id key_id;
  276. u32 frame_counter;
  277. };
  278. struct wpan_dev_header_ops {
  279. /* TODO create callback currently assumes ieee802154_mac_cb inside
  280. * skb->cb. This should be changed to give these information as
  281. * parameter.
  282. */
  283. int (*create)(struct sk_buff *skb, struct net_device *dev,
  284. const struct ieee802154_addr *daddr,
  285. const struct ieee802154_addr *saddr,
  286. unsigned int len);
  287. };
  288. struct wpan_dev {
  289. struct wpan_phy *wpan_phy;
  290. int iftype;
  291. /* the remainder of this struct should be private to cfg802154 */
  292. struct list_head list;
  293. struct net_device *netdev;
  294. const struct wpan_dev_header_ops *header_ops;
  295. /* lowpan interface, set when the wpan_dev belongs to one lowpan_dev */
  296. struct net_device *lowpan_dev;
  297. u32 identifier;
  298. /* MAC PIB */
  299. __le16 pan_id;
  300. __le16 short_addr;
  301. __le64 extended_addr;
  302. /* MAC BSN field */
  303. atomic_t bsn;
  304. /* MAC DSN field */
  305. atomic_t dsn;
  306. u8 min_be;
  307. u8 max_be;
  308. u8 csma_retries;
  309. s8 frame_retries;
  310. bool lbt;
  311. bool promiscuous_mode;
  312. /* fallback for acknowledgment bit setting */
  313. bool ackreq;
  314. };
  315. #define to_phy(_dev) container_of(_dev, struct wpan_phy, dev)
  316. static inline int
  317. wpan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  318. const struct ieee802154_addr *daddr,
  319. const struct ieee802154_addr *saddr,
  320. unsigned int len)
  321. {
  322. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  323. return wpan_dev->header_ops->create(skb, dev, daddr, saddr, len);
  324. }
  325. struct wpan_phy *
  326. wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size);
  327. static inline void wpan_phy_set_dev(struct wpan_phy *phy, struct device *dev)
  328. {
  329. phy->dev.parent = dev;
  330. }
  331. int wpan_phy_register(struct wpan_phy *phy);
  332. void wpan_phy_unregister(struct wpan_phy *phy);
  333. void wpan_phy_free(struct wpan_phy *phy);
  334. /* Same semantics as for class_for_each_device */
  335. int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), void *data);
  336. static inline void *wpan_phy_priv(struct wpan_phy *phy)
  337. {
  338. BUG_ON(!phy);
  339. return &phy->priv;
  340. }
  341. struct wpan_phy *wpan_phy_find(const char *str);
  342. static inline void wpan_phy_put(struct wpan_phy *phy)
  343. {
  344. put_device(&phy->dev);
  345. }
  346. static inline const char *wpan_phy_name(struct wpan_phy *phy)
  347. {
  348. return dev_name(&phy->dev);
  349. }
  350. #endif /* __NET_CFG802154_H */