cfg802154.h 11 KB

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