ieee802154_i.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2007-2012 Siemens AG
  4. *
  5. * Written by:
  6. * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
  7. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  8. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  9. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  10. */
  11. #ifndef __IEEE802154_I_H
  12. #define __IEEE802154_I_H
  13. #include <linux/interrupt.h>
  14. #include <linux/mutex.h>
  15. #include <linux/hrtimer.h>
  16. #include <net/cfg802154.h>
  17. #include <net/mac802154.h>
  18. #include <net/nl802154.h>
  19. #include <net/ieee802154_netdev.h>
  20. #include "llsec.h"
  21. enum ieee802154_ongoing {
  22. IEEE802154_IS_SCANNING = BIT(0),
  23. IEEE802154_IS_BEACONING = BIT(1),
  24. IEEE802154_IS_ASSOCIATING = BIT(2),
  25. };
  26. /* mac802154 device private data */
  27. struct ieee802154_local {
  28. struct ieee802154_hw hw;
  29. const struct ieee802154_ops *ops;
  30. /* hardware address filter */
  31. struct ieee802154_hw_addr_filt addr_filt;
  32. /* ieee802154 phy */
  33. struct wpan_phy *phy;
  34. int open_count;
  35. /* As in mac80211 slaves list is modified:
  36. * 1) under the RTNL
  37. * 2) protected by slaves_mtx;
  38. * 3) in an RCU manner
  39. *
  40. * So atomic readers can use any of this protection methods.
  41. */
  42. struct list_head interfaces;
  43. struct mutex iflist_mtx;
  44. /* Data related workqueue */
  45. struct workqueue_struct *workqueue;
  46. /* MAC commands related workqueue */
  47. struct workqueue_struct *mac_wq;
  48. struct hrtimer ifs_timer;
  49. /* Scanning */
  50. u8 scan_page;
  51. u8 scan_channel;
  52. struct ieee802154_beacon_req_frame scan_beacon_req;
  53. struct cfg802154_scan_request __rcu *scan_req;
  54. struct delayed_work scan_work;
  55. /* Beaconing */
  56. unsigned int beacon_interval;
  57. struct ieee802154_beacon_frame beacon;
  58. struct cfg802154_beacon_request __rcu *beacon_req;
  59. struct delayed_work beacon_work;
  60. /* Asynchronous tasks */
  61. struct list_head rx_beacon_list;
  62. struct work_struct rx_beacon_work;
  63. struct list_head rx_mac_cmd_list;
  64. struct work_struct rx_mac_cmd_work;
  65. /* Association */
  66. struct ieee802154_pan_device *assoc_dev;
  67. struct completion assoc_done;
  68. __le16 assoc_addr;
  69. u8 assoc_status;
  70. struct work_struct assoc_work;
  71. bool started;
  72. bool suspended;
  73. unsigned long ongoing;
  74. struct tasklet_struct tasklet;
  75. struct sk_buff_head skb_queue;
  76. struct sk_buff *tx_skb;
  77. struct work_struct sync_tx_work;
  78. /* A negative Linux error code or a null/positive MLME error status */
  79. int tx_result;
  80. };
  81. enum {
  82. IEEE802154_RX_MSG = 1,
  83. };
  84. enum ieee802154_sdata_state_bits {
  85. SDATA_STATE_RUNNING,
  86. };
  87. /* Slave interface definition.
  88. *
  89. * Slaves represent typical network interfaces available from userspace.
  90. * Each ieee802154 device/transceiver may have several slaves and able
  91. * to be associated with several networks at the same time.
  92. */
  93. struct ieee802154_sub_if_data {
  94. struct list_head list; /* the ieee802154_priv->slaves list */
  95. struct wpan_dev wpan_dev;
  96. struct ieee802154_local *local;
  97. struct net_device *dev;
  98. /* Each interface starts and works in nominal state at a given filtering
  99. * level given by iface_default_filtering, which is set once for all at
  100. * the interface creation and should not evolve over time. For some MAC
  101. * operations however, the filtering level may change temporarily, as
  102. * reflected in the required_filtering field. The actual filtering at
  103. * the PHY level may be different and is shown in struct wpan_phy.
  104. */
  105. enum ieee802154_filtering_level iface_default_filtering;
  106. enum ieee802154_filtering_level required_filtering;
  107. unsigned long state;
  108. char name[IFNAMSIZ];
  109. /* protects sec from concurrent access by netlink. access by
  110. * encrypt/decrypt/header_create safe without additional protection.
  111. */
  112. struct mutex sec_mtx;
  113. struct mac802154_llsec sec;
  114. };
  115. /* utility functions/constants */
  116. extern const void *const mac802154_wpan_phy_privid; /* for wpan_phy privid */
  117. static inline struct ieee802154_local *
  118. hw_to_local(struct ieee802154_hw *hw)
  119. {
  120. return container_of(hw, struct ieee802154_local, hw);
  121. }
  122. static inline struct ieee802154_sub_if_data *
  123. IEEE802154_DEV_TO_SUB_IF(const struct net_device *dev)
  124. {
  125. return netdev_priv(dev);
  126. }
  127. static inline struct ieee802154_sub_if_data *
  128. IEEE802154_WPAN_DEV_TO_SUB_IF(struct wpan_dev *wpan_dev)
  129. {
  130. return container_of(wpan_dev, struct ieee802154_sub_if_data, wpan_dev);
  131. }
  132. static inline bool
  133. ieee802154_sdata_running(struct ieee802154_sub_if_data *sdata)
  134. {
  135. return test_bit(SDATA_STATE_RUNNING, &sdata->state);
  136. }
  137. static inline int ieee802154_get_mac_cmd(struct sk_buff *skb, u8 *mac_cmd)
  138. {
  139. struct ieee802154_mac_cmd_pl mac_pl;
  140. int ret;
  141. if (mac_cb(skb)->type != IEEE802154_FC_TYPE_MAC_CMD)
  142. return -EINVAL;
  143. ret = ieee802154_mac_cmd_pl_pull(skb, &mac_pl);
  144. if (ret)
  145. return ret;
  146. *mac_cmd = mac_pl.cmd_id;
  147. return 0;
  148. }
  149. extern struct ieee802154_mlme_ops mac802154_mlme_wpan;
  150. void ieee802154_rx(struct ieee802154_local *local, struct sk_buff *skb);
  151. void ieee802154_xmit_sync_worker(struct work_struct *work);
  152. int ieee802154_sync_and_hold_queue(struct ieee802154_local *local);
  153. int ieee802154_mlme_op_pre(struct ieee802154_local *local);
  154. int ieee802154_mlme_tx(struct ieee802154_local *local,
  155. struct ieee802154_sub_if_data *sdata,
  156. struct sk_buff *skb);
  157. int ieee802154_mlme_tx_locked(struct ieee802154_local *local,
  158. struct ieee802154_sub_if_data *sdata,
  159. struct sk_buff *skb);
  160. void ieee802154_mlme_op_post(struct ieee802154_local *local);
  161. int ieee802154_mlme_tx_one(struct ieee802154_local *local,
  162. struct ieee802154_sub_if_data *sdata,
  163. struct sk_buff *skb);
  164. int ieee802154_mlme_tx_one_locked(struct ieee802154_local *local,
  165. struct ieee802154_sub_if_data *sdata,
  166. struct sk_buff *skb);
  167. netdev_tx_t
  168. ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev);
  169. netdev_tx_t
  170. ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev);
  171. enum hrtimer_restart ieee802154_xmit_ifs_timer(struct hrtimer *timer);
  172. /**
  173. * ieee802154_hold_queue - hold ieee802154 queue
  174. * @local: main mac object
  175. *
  176. * Hold a queue by incrementing an atomic counter and requesting the netif
  177. * queues to be stopped. The queues cannot be woken up while the counter has not
  178. * been reset with as any ieee802154_release_queue() calls as needed.
  179. */
  180. void ieee802154_hold_queue(struct ieee802154_local *local);
  181. /**
  182. * ieee802154_release_queue - release ieee802154 queue
  183. * @local: main mac object
  184. *
  185. * Release a queue which is held by decrementing an atomic counter and wake it
  186. * up only if the counter reaches 0.
  187. */
  188. void ieee802154_release_queue(struct ieee802154_local *local);
  189. /**
  190. * ieee802154_disable_queue - disable ieee802154 queue
  191. * @local: main mac object
  192. *
  193. * When trying to sync the Tx queue, we cannot just stop the queue
  194. * (which is basically a bit being set without proper lock handling)
  195. * because it would be racy. We actually need to call netif_tx_disable()
  196. * instead, which is done by this helper. Restarting the queue can
  197. * however still be done with a regular wake call.
  198. */
  199. void ieee802154_disable_queue(struct ieee802154_local *local);
  200. /* MIB callbacks */
  201. void mac802154_dev_set_page_channel(struct net_device *dev, u8 page, u8 chan);
  202. int mac802154_get_params(struct net_device *dev,
  203. struct ieee802154_llsec_params *params);
  204. int mac802154_set_params(struct net_device *dev,
  205. const struct ieee802154_llsec_params *params,
  206. int changed);
  207. int mac802154_add_key(struct net_device *dev,
  208. const struct ieee802154_llsec_key_id *id,
  209. const struct ieee802154_llsec_key *key);
  210. int mac802154_del_key(struct net_device *dev,
  211. const struct ieee802154_llsec_key_id *id);
  212. int mac802154_add_dev(struct net_device *dev,
  213. const struct ieee802154_llsec_device *llsec_dev);
  214. int mac802154_del_dev(struct net_device *dev, __le64 dev_addr);
  215. int mac802154_add_devkey(struct net_device *dev,
  216. __le64 device_addr,
  217. const struct ieee802154_llsec_device_key *key);
  218. int mac802154_del_devkey(struct net_device *dev,
  219. __le64 device_addr,
  220. const struct ieee802154_llsec_device_key *key);
  221. int mac802154_add_seclevel(struct net_device *dev,
  222. const struct ieee802154_llsec_seclevel *sl);
  223. int mac802154_del_seclevel(struct net_device *dev,
  224. const struct ieee802154_llsec_seclevel *sl);
  225. void mac802154_lock_table(struct net_device *dev);
  226. void mac802154_get_table(struct net_device *dev,
  227. struct ieee802154_llsec_table **t);
  228. void mac802154_unlock_table(struct net_device *dev);
  229. int mac802154_wpan_update_llsec(struct net_device *dev);
  230. /* PAN management handling */
  231. void mac802154_scan_worker(struct work_struct *work);
  232. int mac802154_trigger_scan_locked(struct ieee802154_sub_if_data *sdata,
  233. struct cfg802154_scan_request *request);
  234. int mac802154_abort_scan_locked(struct ieee802154_local *local,
  235. struct ieee802154_sub_if_data *sdata);
  236. int mac802154_process_beacon(struct ieee802154_local *local,
  237. struct sk_buff *skb,
  238. u8 page, u8 channel);
  239. void mac802154_rx_beacon_worker(struct work_struct *work);
  240. static inline bool mac802154_is_scanning(struct ieee802154_local *local)
  241. {
  242. return test_bit(IEEE802154_IS_SCANNING, &local->ongoing);
  243. }
  244. void mac802154_beacon_worker(struct work_struct *work);
  245. int mac802154_send_beacons_locked(struct ieee802154_sub_if_data *sdata,
  246. struct cfg802154_beacon_request *request);
  247. int mac802154_stop_beacons_locked(struct ieee802154_local *local,
  248. struct ieee802154_sub_if_data *sdata);
  249. static inline bool mac802154_is_beaconing(struct ieee802154_local *local)
  250. {
  251. return test_bit(IEEE802154_IS_BEACONING, &local->ongoing);
  252. }
  253. void mac802154_rx_mac_cmd_worker(struct work_struct *work);
  254. int mac802154_perform_association(struct ieee802154_sub_if_data *sdata,
  255. struct ieee802154_pan_device *coord,
  256. __le16 *short_addr);
  257. int mac802154_process_association_resp(struct ieee802154_sub_if_data *sdata,
  258. struct sk_buff *skb);
  259. static inline bool mac802154_is_associating(struct ieee802154_local *local)
  260. {
  261. return test_bit(IEEE802154_IS_ASSOCIATING, &local->ongoing);
  262. }
  263. int mac802154_send_disassociation_notif(struct ieee802154_sub_if_data *sdata,
  264. struct ieee802154_pan_device *target,
  265. u8 reason);
  266. int mac802154_process_disassociation_notif(struct ieee802154_sub_if_data *sdata,
  267. struct sk_buff *skb);
  268. int mac802154_process_association_req(struct ieee802154_sub_if_data *sdata,
  269. struct sk_buff *skb);
  270. /* interface handling */
  271. int ieee802154_iface_init(void);
  272. void ieee802154_iface_exit(void);
  273. void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata);
  274. struct net_device *
  275. ieee802154_if_add(struct ieee802154_local *local, const char *name,
  276. unsigned char name_assign_type, enum nl802154_iftype type,
  277. __le64 extended_addr);
  278. void ieee802154_remove_interfaces(struct ieee802154_local *local);
  279. void ieee802154_stop_device(struct ieee802154_local *local);
  280. #endif /* __IEEE802154_I_H */