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