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