xref: /openbmc/linux/net/mac802154/ieee802154_i.h (revision d021d218)
11802d0beSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
20f1556bcSAlexander Aring /*
30f1556bcSAlexander Aring  * Copyright (C) 2007-2012 Siemens AG
40f1556bcSAlexander Aring  *
50f1556bcSAlexander Aring  * Written by:
60f1556bcSAlexander Aring  * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
70f1556bcSAlexander Aring  * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
80f1556bcSAlexander Aring  * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
90f1556bcSAlexander Aring  * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
100f1556bcSAlexander Aring  */
110f1556bcSAlexander Aring #ifndef __IEEE802154_I_H
120f1556bcSAlexander Aring #define __IEEE802154_I_H
130f1556bcSAlexander Aring 
14282ccf6eSFlorian Westphal #include <linux/interrupt.h>
150f1556bcSAlexander Aring #include <linux/mutex.h>
1661f2dcbaSAlexander Aring #include <linux/hrtimer.h>
17d5ae67baSAlexander Aring #include <net/cfg802154.h>
180f1556bcSAlexander Aring #include <net/mac802154.h>
19944742a3SAlexander Aring #include <net/nl802154.h>
200f1556bcSAlexander Aring #include <net/ieee802154_netdev.h>
210f1556bcSAlexander Aring 
220f1556bcSAlexander Aring #include "llsec.h"
230f1556bcSAlexander Aring 
2457588c71SMiquel Raynal enum ieee802154_ongoing {
2557588c71SMiquel Raynal 	IEEE802154_IS_SCANNING = BIT(0),
263accf476SMiquel Raynal 	IEEE802154_IS_BEACONING = BIT(1),
2757588c71SMiquel Raynal };
2857588c71SMiquel Raynal 
290f1556bcSAlexander Aring /* mac802154 device private data */
30a5e1ec53SAlexander Aring struct ieee802154_local {
315a504397SAlexander Aring 	struct ieee802154_hw hw;
3216301861SAlexander Aring 	const struct ieee802154_ops *ops;
330f1556bcSAlexander Aring 
34ac8037c3SAlexander Aring 	/* hardware address filter */
35ac8037c3SAlexander Aring 	struct ieee802154_hw_addr_filt addr_filt;
360f1556bcSAlexander Aring 	/* ieee802154 phy */
370f1556bcSAlexander Aring 	struct wpan_phy *phy;
380f1556bcSAlexander Aring 
390f1556bcSAlexander Aring 	int open_count;
400f1556bcSAlexander Aring 
410f1556bcSAlexander Aring 	/* As in mac80211 slaves list is modified:
420f1556bcSAlexander Aring 	 * 1) under the RTNL
430f1556bcSAlexander Aring 	 * 2) protected by slaves_mtx;
440f1556bcSAlexander Aring 	 * 3) in an RCU manner
450f1556bcSAlexander Aring 	 *
460f1556bcSAlexander Aring 	 * So atomic readers can use any of this protection methods.
470f1556bcSAlexander Aring 	 */
48d98be45bSAlexander Aring 	struct list_head	interfaces;
49d98be45bSAlexander Aring 	struct mutex		iflist_mtx;
500f1556bcSAlexander Aring 
5157588c71SMiquel Raynal 	/* Data related workqueue */
52f7730542SAlexander Aring 	struct workqueue_struct	*workqueue;
5357588c71SMiquel Raynal 	/* MAC commands related workqueue */
5457588c71SMiquel Raynal 	struct workqueue_struct	*mac_wq;
550f1556bcSAlexander Aring 
5661f2dcbaSAlexander Aring 	struct hrtimer ifs_timer;
5761f2dcbaSAlexander Aring 
5857588c71SMiquel Raynal 	/* Scanning */
5957588c71SMiquel Raynal 	u8 scan_page;
6057588c71SMiquel Raynal 	u8 scan_channel;
61e2c3e6f5SMiquel Raynal 	struct ieee802154_beacon_req_frame scan_beacon_req;
6257588c71SMiquel Raynal 	struct cfg802154_scan_request __rcu *scan_req;
6357588c71SMiquel Raynal 	struct delayed_work scan_work;
6457588c71SMiquel Raynal 
653accf476SMiquel Raynal 	/* Beaconing */
663accf476SMiquel Raynal 	unsigned int beacon_interval;
673accf476SMiquel Raynal 	struct ieee802154_beacon_frame beacon;
683accf476SMiquel Raynal 	struct cfg802154_beacon_request __rcu *beacon_req;
693accf476SMiquel Raynal 	struct delayed_work beacon_work;
703accf476SMiquel Raynal 
7157588c71SMiquel Raynal 	/* Asynchronous tasks */
7257588c71SMiquel Raynal 	struct list_head rx_beacon_list;
7357588c71SMiquel Raynal 	struct work_struct rx_beacon_work;
74*d021d218SMiquel Raynal 	struct list_head rx_mac_cmd_list;
75*d021d218SMiquel Raynal 	struct work_struct rx_mac_cmd_work;
7657588c71SMiquel Raynal 
775d65cae4SAlexander Aring 	bool started;
783cf24cf8SAlexander Aring 	bool suspended;
7957588c71SMiquel Raynal 	unsigned long ongoing;
80c5c47e67SAlexander Aring 
81c5c47e67SAlexander Aring 	struct tasklet_struct tasklet;
82c5c47e67SAlexander Aring 	struct sk_buff_head skb_queue;
83c22ff7b4SLennert Buytenhek 
84c22ff7b4SLennert Buytenhek 	struct sk_buff *tx_skb;
85983a974bSMiquel Raynal 	struct work_struct sync_tx_work;
86337e2f86SMiquel Raynal 	/* A negative Linux error code or a null/positive MLME error status */
87337e2f86SMiquel Raynal 	int tx_result;
880f1556bcSAlexander Aring };
890f1556bcSAlexander Aring 
90c5c47e67SAlexander Aring enum {
91c5c47e67SAlexander Aring 	IEEE802154_RX_MSG        = 1,
92c5c47e67SAlexander Aring };
93c5c47e67SAlexander Aring 
940ea3da64SAlexander Aring enum ieee802154_sdata_state_bits {
950ea3da64SAlexander Aring 	SDATA_STATE_RUNNING,
960ea3da64SAlexander Aring };
970ea3da64SAlexander Aring 
980f1556bcSAlexander Aring /* Slave interface definition.
990f1556bcSAlexander Aring  *
1000f1556bcSAlexander Aring  * Slaves represent typical network interfaces available from userspace.
1010f1556bcSAlexander Aring  * Each ieee802154 device/transceiver may have several slaves and able
1020f1556bcSAlexander Aring  * to be associated with several networks at the same time.
1030f1556bcSAlexander Aring  */
104036562f9SAlexander Aring struct ieee802154_sub_if_data {
1050f1556bcSAlexander Aring 	struct list_head list; /* the ieee802154_priv->slaves list */
1060f1556bcSAlexander Aring 
107d5ae67baSAlexander Aring 	struct wpan_dev wpan_dev;
108d5ae67baSAlexander Aring 
10904e850feSAlexander Aring 	struct ieee802154_local *local;
1100f1556bcSAlexander Aring 	struct net_device *dev;
1110f1556bcSAlexander Aring 
112ac8037c3SAlexander Aring 	/* Each interface starts and works in nominal state at a given filtering
113ac8037c3SAlexander Aring 	 * level given by iface_default_filtering, which is set once for all at
114ac8037c3SAlexander Aring 	 * the interface creation and should not evolve over time. For some MAC
115ac8037c3SAlexander Aring 	 * operations however, the filtering level may change temporarily, as
116ac8037c3SAlexander Aring 	 * reflected in the required_filtering field. The actual filtering at
117ac8037c3SAlexander Aring 	 * the PHY level may be different and is shown in struct wpan_phy.
118ac8037c3SAlexander Aring 	 */
119ac8037c3SAlexander Aring 	enum ieee802154_filtering_level iface_default_filtering;
120ac8037c3SAlexander Aring 	enum ieee802154_filtering_level required_filtering;
121ac8037c3SAlexander Aring 
1220ea3da64SAlexander Aring 	unsigned long state;
123d5ae67baSAlexander Aring 	char name[IFNAMSIZ];
1240f1556bcSAlexander Aring 
1250f1556bcSAlexander Aring 	/* protects sec from concurrent access by netlink. access by
1260f1556bcSAlexander Aring 	 * encrypt/decrypt/header_create safe without additional protection.
1270f1556bcSAlexander Aring 	 */
1280f1556bcSAlexander Aring 	struct mutex sec_mtx;
1290f1556bcSAlexander Aring 
1300f1556bcSAlexander Aring 	struct mac802154_llsec sec;
1310f1556bcSAlexander Aring };
1320f1556bcSAlexander Aring 
1336322d50dSAlexander Aring /* utility functions/constants */
1346322d50dSAlexander Aring extern const void *const mac802154_wpan_phy_privid; /*  for wpan_phy privid */
1356322d50dSAlexander Aring 
13660741361SAlexander Aring static inline struct ieee802154_local *
hw_to_local(struct ieee802154_hw * hw)13760741361SAlexander Aring hw_to_local(struct ieee802154_hw *hw)
13860741361SAlexander Aring {
13960741361SAlexander Aring 	return container_of(hw, struct ieee802154_local, hw);
14060741361SAlexander Aring }
14160741361SAlexander Aring 
14259d19cd7SAlexander Aring static inline struct ieee802154_sub_if_data *
IEEE802154_DEV_TO_SUB_IF(const struct net_device * dev)14359d19cd7SAlexander Aring IEEE802154_DEV_TO_SUB_IF(const struct net_device *dev)
14459d19cd7SAlexander Aring {
14559d19cd7SAlexander Aring 	return netdev_priv(dev);
14659d19cd7SAlexander Aring }
14759d19cd7SAlexander Aring 
148b821ecd4SAlexander Aring static inline struct ieee802154_sub_if_data *
IEEE802154_WPAN_DEV_TO_SUB_IF(struct wpan_dev * wpan_dev)149b821ecd4SAlexander Aring IEEE802154_WPAN_DEV_TO_SUB_IF(struct wpan_dev *wpan_dev)
150b821ecd4SAlexander Aring {
151b821ecd4SAlexander Aring 	return container_of(wpan_dev, struct ieee802154_sub_if_data, wpan_dev);
152b821ecd4SAlexander Aring }
153b821ecd4SAlexander Aring 
1540ea3da64SAlexander Aring static inline bool
ieee802154_sdata_running(struct ieee802154_sub_if_data * sdata)1550ea3da64SAlexander Aring ieee802154_sdata_running(struct ieee802154_sub_if_data *sdata)
1560ea3da64SAlexander Aring {
1570ea3da64SAlexander Aring 	return test_bit(SDATA_STATE_RUNNING, &sdata->state);
1580ea3da64SAlexander Aring }
1590ea3da64SAlexander Aring 
ieee802154_get_mac_cmd(struct sk_buff * skb,u8 * mac_cmd)160*d021d218SMiquel Raynal static inline int ieee802154_get_mac_cmd(struct sk_buff *skb, u8 *mac_cmd)
161*d021d218SMiquel Raynal {
162*d021d218SMiquel Raynal 	struct ieee802154_mac_cmd_pl mac_pl;
163*d021d218SMiquel Raynal 	int ret;
164*d021d218SMiquel Raynal 
165*d021d218SMiquel Raynal 	if (mac_cb(skb)->type != IEEE802154_FC_TYPE_MAC_CMD)
166*d021d218SMiquel Raynal 		return -EINVAL;
167*d021d218SMiquel Raynal 
168*d021d218SMiquel Raynal 	ret = ieee802154_mac_cmd_pl_pull(skb, &mac_pl);
169*d021d218SMiquel Raynal 	if (ret)
170*d021d218SMiquel Raynal 		return ret;
171*d021d218SMiquel Raynal 
172*d021d218SMiquel Raynal 	*mac_cmd = mac_pl.cmd_id;
173*d021d218SMiquel Raynal 	return 0;
174*d021d218SMiquel Raynal }
175*d021d218SMiquel Raynal 
1760f1556bcSAlexander Aring extern struct ieee802154_mlme_ops mac802154_mlme_wpan;
1770f1556bcSAlexander Aring 
178d10270ceSVarka Bhadram void ieee802154_rx(struct ieee802154_local *local, struct sk_buff *skb);
179be8c6d86SMiquel Raynal void ieee802154_xmit_sync_worker(struct work_struct *work);
180f0feb349SMiquel Raynal int ieee802154_sync_and_hold_queue(struct ieee802154_local *local);
181ddd9ee7cSMiquel Raynal int ieee802154_mlme_op_pre(struct ieee802154_local *local);
182fbdaa5baSMiquel Raynal int ieee802154_mlme_tx(struct ieee802154_local *local,
183fbdaa5baSMiquel Raynal 		       struct ieee802154_sub_if_data *sdata,
184fbdaa5baSMiquel Raynal 		       struct sk_buff *skb);
185dd180962SMiquel Raynal int ieee802154_mlme_tx_locked(struct ieee802154_local *local,
186dd180962SMiquel Raynal 			      struct ieee802154_sub_if_data *sdata,
187dd180962SMiquel Raynal 			      struct sk_buff *skb);
188ddd9ee7cSMiquel Raynal void ieee802154_mlme_op_post(struct ieee802154_local *local);
189fbdaa5baSMiquel Raynal int ieee802154_mlme_tx_one(struct ieee802154_local *local,
190fbdaa5baSMiquel Raynal 			   struct ieee802154_sub_if_data *sdata,
191fbdaa5baSMiquel Raynal 			   struct sk_buff *skb);
192dd180962SMiquel Raynal int ieee802154_mlme_tx_one_locked(struct ieee802154_local *local,
193dd180962SMiquel Raynal 				  struct ieee802154_sub_if_data *sdata,
194dd180962SMiquel Raynal 				  struct sk_buff *skb);
195e5e584fcSAlexander Aring netdev_tx_t
196e5e584fcSAlexander Aring ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev);
197e5e584fcSAlexander Aring netdev_tx_t
198e5e584fcSAlexander Aring ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev);
19961f2dcbaSAlexander Aring enum hrtimer_restart ieee802154_xmit_ifs_timer(struct hrtimer *timer);
2000f1556bcSAlexander Aring 
20120a19d1dSMiquel Raynal /**
20220a19d1dSMiquel Raynal  * ieee802154_hold_queue - hold ieee802154 queue
20320a19d1dSMiquel Raynal  * @local: main mac object
20420a19d1dSMiquel Raynal  *
20520a19d1dSMiquel Raynal  * Hold a queue by incrementing an atomic counter and requesting the netif
20620a19d1dSMiquel Raynal  * queues to be stopped. The queues cannot be woken up while the counter has not
20720a19d1dSMiquel Raynal  * been reset with as any ieee802154_release_queue() calls as needed.
20820a19d1dSMiquel Raynal  */
20920a19d1dSMiquel Raynal void ieee802154_hold_queue(struct ieee802154_local *local);
21020a19d1dSMiquel Raynal 
21120a19d1dSMiquel Raynal /**
21220a19d1dSMiquel Raynal  * ieee802154_release_queue - release ieee802154 queue
21320a19d1dSMiquel Raynal  * @local: main mac object
21420a19d1dSMiquel Raynal  *
21520a19d1dSMiquel Raynal  * Release a queue which is held by decrementing an atomic counter and wake it
21620a19d1dSMiquel Raynal  * up only if the counter reaches 0.
21720a19d1dSMiquel Raynal  */
21820a19d1dSMiquel Raynal void ieee802154_release_queue(struct ieee802154_local *local);
21920a19d1dSMiquel Raynal 
220a40612f3SMiquel Raynal /**
221a40612f3SMiquel Raynal  * ieee802154_disable_queue - disable ieee802154 queue
222a40612f3SMiquel Raynal  * @local: main mac object
223a40612f3SMiquel Raynal  *
224a40612f3SMiquel Raynal  * When trying to sync the Tx queue, we cannot just stop the queue
225a40612f3SMiquel Raynal  * (which is basically a bit being set without proper lock handling)
226a40612f3SMiquel Raynal  * because it would be racy. We actually need to call netif_tx_disable()
227a40612f3SMiquel Raynal  * instead, which is done by this helper. Restarting the queue can
228a40612f3SMiquel Raynal  * however still be done with a regular wake call.
229a40612f3SMiquel Raynal  */
230a40612f3SMiquel Raynal void ieee802154_disable_queue(struct ieee802154_local *local);
231a40612f3SMiquel Raynal 
2320f1556bcSAlexander Aring /* MIB callbacks */
2330f1556bcSAlexander Aring void mac802154_dev_set_page_channel(struct net_device *dev, u8 page, u8 chan);
2340f1556bcSAlexander Aring 
2350f1556bcSAlexander Aring int mac802154_get_params(struct net_device *dev,
2360f1556bcSAlexander Aring 			 struct ieee802154_llsec_params *params);
2370f1556bcSAlexander Aring int mac802154_set_params(struct net_device *dev,
2380f1556bcSAlexander Aring 			 const struct ieee802154_llsec_params *params,
2390f1556bcSAlexander Aring 			 int changed);
2400f1556bcSAlexander Aring 
2410f1556bcSAlexander Aring int mac802154_add_key(struct net_device *dev,
2420f1556bcSAlexander Aring 		      const struct ieee802154_llsec_key_id *id,
2430f1556bcSAlexander Aring 		      const struct ieee802154_llsec_key *key);
2440f1556bcSAlexander Aring int mac802154_del_key(struct net_device *dev,
2450f1556bcSAlexander Aring 		      const struct ieee802154_llsec_key_id *id);
2460f1556bcSAlexander Aring 
2470f1556bcSAlexander Aring int mac802154_add_dev(struct net_device *dev,
2480f1556bcSAlexander Aring 		      const struct ieee802154_llsec_device *llsec_dev);
2490f1556bcSAlexander Aring int mac802154_del_dev(struct net_device *dev, __le64 dev_addr);
2500f1556bcSAlexander Aring 
2510f1556bcSAlexander Aring int mac802154_add_devkey(struct net_device *dev,
2520f1556bcSAlexander Aring 			 __le64 device_addr,
2530f1556bcSAlexander Aring 			 const struct ieee802154_llsec_device_key *key);
2540f1556bcSAlexander Aring int mac802154_del_devkey(struct net_device *dev,
2550f1556bcSAlexander Aring 			 __le64 device_addr,
2560f1556bcSAlexander Aring 			 const struct ieee802154_llsec_device_key *key);
2570f1556bcSAlexander Aring 
2580f1556bcSAlexander Aring int mac802154_add_seclevel(struct net_device *dev,
2590f1556bcSAlexander Aring 			   const struct ieee802154_llsec_seclevel *sl);
2600f1556bcSAlexander Aring int mac802154_del_seclevel(struct net_device *dev,
2610f1556bcSAlexander Aring 			   const struct ieee802154_llsec_seclevel *sl);
2620f1556bcSAlexander Aring 
2630f1556bcSAlexander Aring void mac802154_lock_table(struct net_device *dev);
2640f1556bcSAlexander Aring void mac802154_get_table(struct net_device *dev,
2650f1556bcSAlexander Aring 			 struct ieee802154_llsec_table **t);
2660f1556bcSAlexander Aring void mac802154_unlock_table(struct net_device *dev);
2670f1556bcSAlexander Aring 
268d77b4852SAlexander Aring int mac802154_wpan_update_llsec(struct net_device *dev);
269d77b4852SAlexander Aring 
27057588c71SMiquel Raynal /* PAN management handling */
27157588c71SMiquel Raynal void mac802154_scan_worker(struct work_struct *work);
27257588c71SMiquel Raynal int mac802154_trigger_scan_locked(struct ieee802154_sub_if_data *sdata,
27357588c71SMiquel Raynal 				  struct cfg802154_scan_request *request);
27457588c71SMiquel Raynal int mac802154_abort_scan_locked(struct ieee802154_local *local,
27557588c71SMiquel Raynal 				struct ieee802154_sub_if_data *sdata);
27657588c71SMiquel Raynal int mac802154_process_beacon(struct ieee802154_local *local,
27757588c71SMiquel Raynal 			     struct sk_buff *skb,
27857588c71SMiquel Raynal 			     u8 page, u8 channel);
27957588c71SMiquel Raynal void mac802154_rx_beacon_worker(struct work_struct *work);
28057588c71SMiquel Raynal 
mac802154_is_scanning(struct ieee802154_local * local)28157588c71SMiquel Raynal static inline bool mac802154_is_scanning(struct ieee802154_local *local)
28257588c71SMiquel Raynal {
28357588c71SMiquel Raynal 	return test_bit(IEEE802154_IS_SCANNING, &local->ongoing);
28457588c71SMiquel Raynal }
28557588c71SMiquel Raynal 
2863accf476SMiquel Raynal void mac802154_beacon_worker(struct work_struct *work);
2873accf476SMiquel Raynal int mac802154_send_beacons_locked(struct ieee802154_sub_if_data *sdata,
2883accf476SMiquel Raynal 				  struct cfg802154_beacon_request *request);
2893accf476SMiquel Raynal int mac802154_stop_beacons_locked(struct ieee802154_local *local,
2903accf476SMiquel Raynal 				  struct ieee802154_sub_if_data *sdata);
2913accf476SMiquel Raynal 
mac802154_is_beaconing(struct ieee802154_local * local)2923accf476SMiquel Raynal static inline bool mac802154_is_beaconing(struct ieee802154_local *local)
2933accf476SMiquel Raynal {
2943accf476SMiquel Raynal 	return test_bit(IEEE802154_IS_BEACONING, &local->ongoing);
2953accf476SMiquel Raynal }
2963accf476SMiquel Raynal 
297*d021d218SMiquel Raynal void mac802154_rx_mac_cmd_worker(struct work_struct *work);
298*d021d218SMiquel Raynal 
299be4fd8e5SAlexander Aring /* interface handling */
300be4fd8e5SAlexander Aring int ieee802154_iface_init(void);
301be4fd8e5SAlexander Aring void ieee802154_iface_exit(void);
302b210b187SAlexander Aring void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata);
303986a8abfSAlexander Aring struct net_device *
304986a8abfSAlexander Aring ieee802154_if_add(struct ieee802154_local *local, const char *name,
3055b4a1039SVarka Bhadram 		  unsigned char name_assign_type, enum nl802154_iftype type,
3065b4a1039SVarka Bhadram 		  __le64 extended_addr);
307592dfbfcSAlexander Aring void ieee802154_remove_interfaces(struct ieee802154_local *local);
308c4227c8aSAlexander Aring void ieee802154_stop_device(struct ieee802154_local *local);
3094a9a816aSAlexander Aring 
3100f1556bcSAlexander Aring #endif /* __IEEE802154_I_H */
311