xref: /linux-6.15/include/net/rtnetlink.h (revision 9c0fc091)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2e2849863SThomas Graf #ifndef __NET_RTNETLINK_H
3e2849863SThomas Graf #define __NET_RTNETLINK_H
4e2849863SThomas Graf 
5e2849863SThomas Graf #include <linux/rtnetlink.h>
643c7ce69SKuniyuki Iwashima #include <linux/srcu.h>
7e2849863SThomas Graf #include <net/netlink.h>
8e2849863SThomas Graf 
9c21ef3e3SDavid Ahern typedef int (*rtnl_doit_func)(struct sk_buff *, struct nlmsghdr *,
10c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *);
11e2849863SThomas Graf typedef int (*rtnl_dumpit_func)(struct sk_buff *, struct netlink_callback *);
12e2849863SThomas Graf 
1362256f98SFlorian Westphal enum rtnl_link_flags {
140569e31fSNikolay Aleksandrov 	RTNL_FLAG_DOIT_UNLOCKED		= BIT(0),
1526d8db55SKuniyuki Iwashima #define RTNL_FLAG_DOIT_PERNET		RTNL_FLAG_DOIT_UNLOCKED
16636af13fSKuniyuki Iwashima #define RTNL_FLAG_DOIT_PERNET_WIP	RTNL_FLAG_DOIT_UNLOCKED
17a6cec0bcSNikolay Aleksandrov 	RTNL_FLAG_BULK_DEL_SUPPORTED	= BIT(1),
18386520e0SEric Dumazet 	RTNL_FLAG_DUMP_UNLOCKED		= BIT(2),
195b4b62a1SJakub Kicinski 	RTNL_FLAG_DUMP_SPLIT_NLM_DONE	= BIT(3),	/* legacy behavior */
2062256f98SFlorian Westphal };
2162256f98SFlorian Westphal 
2212dc5c2cSNikolay Aleksandrov enum rtnl_kinds {
2312dc5c2cSNikolay Aleksandrov 	RTNL_KIND_NEW,
2412dc5c2cSNikolay Aleksandrov 	RTNL_KIND_DEL,
2512dc5c2cSNikolay Aleksandrov 	RTNL_KIND_GET,
2612dc5c2cSNikolay Aleksandrov 	RTNL_KIND_SET
2712dc5c2cSNikolay Aleksandrov };
282e9ea3e3SNikolay Aleksandrov #define RTNL_KIND_MASK 0x3
292e9ea3e3SNikolay Aleksandrov 
rtnl_msgtype_kind(int msgtype)302e9ea3e3SNikolay Aleksandrov static inline enum rtnl_kinds rtnl_msgtype_kind(int msgtype)
312e9ea3e3SNikolay Aleksandrov {
322e9ea3e3SNikolay Aleksandrov 	return msgtype & RTNL_KIND_MASK;
332e9ea3e3SNikolay Aleksandrov }
3412dc5c2cSNikolay Aleksandrov 
35e1c6c383SKuniyuki Iwashima /**
36e1c6c383SKuniyuki Iwashima  *	struct rtnl_msg_handler - rtnetlink message type and handlers
37e1c6c383SKuniyuki Iwashima  *
38e1c6c383SKuniyuki Iwashima  *	@owner: NULL for built-in, THIS_MODULE for module
39e1c6c383SKuniyuki Iwashima  *	@protocol: Protocol family or PF_UNSPEC
40e1c6c383SKuniyuki Iwashima  *	@msgtype: rtnetlink message type
41e1c6c383SKuniyuki Iwashima  *	@doit: Function pointer called for each request message
42e1c6c383SKuniyuki Iwashima  *	@dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
43e1c6c383SKuniyuki Iwashima  *	@flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
44e1c6c383SKuniyuki Iwashima  */
4507cc7b0bSKuniyuki Iwashima struct rtnl_msg_handler {
4607cc7b0bSKuniyuki Iwashima 	struct module *owner;
4707cc7b0bSKuniyuki Iwashima 	int protocol;
4807cc7b0bSKuniyuki Iwashima 	int msgtype;
4907cc7b0bSKuniyuki Iwashima 	rtnl_doit_func doit;
5007cc7b0bSKuniyuki Iwashima 	rtnl_dumpit_func dumpit;
5107cc7b0bSKuniyuki Iwashima 	int flags;
5207cc7b0bSKuniyuki Iwashima };
5307cc7b0bSKuniyuki Iwashima 
54efb48ccfSJoe Perches void rtnl_unregister_all(int protocol);
55e2849863SThomas Graf 
5607cc7b0bSKuniyuki Iwashima int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n);
5707cc7b0bSKuniyuki Iwashima void __rtnl_unregister_many(const struct rtnl_msg_handler *handlers, int n);
5807cc7b0bSKuniyuki Iwashima 
5907cc7b0bSKuniyuki Iwashima #define rtnl_register_many(handlers)				\
6007cc7b0bSKuniyuki Iwashima 	__rtnl_register_many(handlers, ARRAY_SIZE(handlers))
6107cc7b0bSKuniyuki Iwashima #define rtnl_unregister_many(handlers)				\
6207cc7b0bSKuniyuki Iwashima 	__rtnl_unregister_many(handlers, ARRAY_SIZE(handlers))
6307cc7b0bSKuniyuki Iwashima 
rtnl_msg_family(const struct nlmsghdr * nlh)643a6c2b41SPatrick McHardy static inline int rtnl_msg_family(const struct nlmsghdr *nlh)
65c454673dSThomas Graf {
66c454673dSThomas Graf 	if (nlmsg_len(nlh) >= sizeof(struct rtgenmsg))
67c454673dSThomas Graf 		return ((struct rtgenmsg *) nlmsg_data(nlh))->rtgen_family;
68c454673dSThomas Graf 	else
69c454673dSThomas Graf 		return AF_UNSPEC;
70c454673dSThomas Graf }
71c454673dSThomas Graf 
7238f7b870SPatrick McHardy /**
7369c7be1bSXiao Liang  * struct rtnl_newlink_params - parameters of rtnl_link_ops::newlink()
7469c7be1bSXiao Liang  *
7569c7be1bSXiao Liang  * @src_net: Source netns of rtnetlink socket
7669c7be1bSXiao Liang  * @link_net: Link netns by IFLA_LINK_NETNSID, NULL if not specified
7769c7be1bSXiao Liang  * @peer_net: Peer netns
7869c7be1bSXiao Liang  * @tb: IFLA_* attributes
7969c7be1bSXiao Liang  * @data: IFLA_INFO_DATA attributes
8069c7be1bSXiao Liang  */
8169c7be1bSXiao Liang struct rtnl_newlink_params {
8269c7be1bSXiao Liang 	struct net *src_net;
8369c7be1bSXiao Liang 	struct net *link_net;
8469c7be1bSXiao Liang 	struct net *peer_net;
8569c7be1bSXiao Liang 	struct nlattr **tb;
8669c7be1bSXiao Liang 	struct nlattr **data;
8769c7be1bSXiao Liang };
8869c7be1bSXiao Liang 
89*cf517ac1SXiao Liang /* Get effective link netns from newlink params. Generally, this is link_net
90*cf517ac1SXiao Liang  * and falls back to src_net. But for compatibility, a driver may * choose to
91*cf517ac1SXiao Liang  * use dev_net(dev) instead.
92*cf517ac1SXiao Liang  */
rtnl_newlink_link_net(struct rtnl_newlink_params * p)93*cf517ac1SXiao Liang static inline struct net *rtnl_newlink_link_net(struct rtnl_newlink_params *p)
94*cf517ac1SXiao Liang {
95*cf517ac1SXiao Liang 	return p->link_net ? : p->src_net;
96*cf517ac1SXiao Liang }
97*cf517ac1SXiao Liang 
98*cf517ac1SXiao Liang /* Get peer netns from newlink params. Fallback to link netns if peer netns is
99*cf517ac1SXiao Liang  * not specified explicitly.
100*cf517ac1SXiao Liang  */
rtnl_newlink_peer_net(struct rtnl_newlink_params * p)101*cf517ac1SXiao Liang static inline struct net *rtnl_newlink_peer_net(struct rtnl_newlink_params *p)
102*cf517ac1SXiao Liang {
103*cf517ac1SXiao Liang 	return p->peer_net ? : rtnl_newlink_link_net(p);
104*cf517ac1SXiao Liang }
105*cf517ac1SXiao Liang 
10669c7be1bSXiao Liang /**
10738f7b870SPatrick McHardy  *	struct rtnl_link_ops - rtnetlink link operations
10838f7b870SPatrick McHardy  *
1096b57ff21SKuniyuki Iwashima  *	@list: Used internally, protected by link_ops_mutex and SRCU
11043c7ce69SKuniyuki Iwashima  *	@srcu: Used internally
11138f7b870SPatrick McHardy  *	@kind: Identifier
1123a5ca857SMartin Willi  *	@netns_refund: Physical device, move to init_net on netns exit
11328690e53SKuniyuki Iwashima  *	@peer_type: Peer device specific netlink attribute number (e.g. VETH_INFO_PEER)
11438f7b870SPatrick McHardy  *	@maxtype: Highest device specific netlink attribute number
11538f7b870SPatrick McHardy  *	@policy: Netlink policy for device specific attribute validation
11638f7b870SPatrick McHardy  *	@validate: Optional validation function for netlink/changelink parameters
1178c713dc9SJohannes Berg  *	@alloc: netdev allocation function, can be %NULL and is then used
1188c713dc9SJohannes Berg  *		in place of alloc_netdev_mqs(), in this case @priv_size
1198c713dc9SJohannes Berg  *		and @setup are unused. Returns a netdev or ERR_PTR().
12038f7b870SPatrick McHardy  *	@priv_size: sizeof net_device private space
12138f7b870SPatrick McHardy  *	@setup: net_device setup function
12238f7b870SPatrick McHardy  *	@newlink: Function for configuring and registering a new device
12338f7b870SPatrick McHardy  *	@changelink: Function for changing parameters of an existing device
12438f7b870SPatrick McHardy  *	@dellink: Function to remove a device
12538f7b870SPatrick McHardy  *	@get_size: Function to calculate required room for dumping device
12638f7b870SPatrick McHardy  *		   specific netlink attributes
12738f7b870SPatrick McHardy  *	@fill_info: Function to dump device specific netlink attributes
1289b17876fSstephen hemminger  *	@get_xstats_size: Function to calculate required room for dumping device
12938f7b870SPatrick McHardy  *			  specific statistics
13038f7b870SPatrick McHardy  *	@fill_xstats: Function to dump device specific statistics
131d40156aaSJiri Pirko  *	@get_num_tx_queues: Function to determine number of transmit queues
132d40156aaSJiri Pirko  *			    to create when creating a new device.
133d40156aaSJiri Pirko  *	@get_num_rx_queues: Function to determine number of receive queues
134d40156aaSJiri Pirko  *			    to create when creating a new device.
135d37512a2SNicolas Dichtel  *	@get_link_net: Function to get the i/o netns of the device
13697a47facSNikolay Aleksandrov  *	@get_linkxstats_size: Function to calculate the required room for
13797a47facSNikolay Aleksandrov  *			      dumping device-specific extended link stats
13897a47facSNikolay Aleksandrov  *	@fill_linkxstats: Function to dump device-specific extended link stats
13938f7b870SPatrick McHardy  */
14038f7b870SPatrick McHardy struct rtnl_link_ops {
14138f7b870SPatrick McHardy 	struct list_head	list;
14243c7ce69SKuniyuki Iwashima 	struct srcu_struct	srcu;
14338f7b870SPatrick McHardy 
14438f7b870SPatrick McHardy 	const char		*kind;
14538f7b870SPatrick McHardy 
14638f7b870SPatrick McHardy 	size_t			priv_size;
1478c713dc9SJohannes Berg 	struct net_device	*(*alloc)(struct nlattr *tb[],
1488c713dc9SJohannes Berg 					  const char *ifname,
1498c713dc9SJohannes Berg 					  unsigned char name_assign_type,
1508c713dc9SJohannes Berg 					  unsigned int num_tx_queues,
1518c713dc9SJohannes Berg 					  unsigned int num_rx_queues);
15238f7b870SPatrick McHardy 	void			(*setup)(struct net_device *dev);
15338f7b870SPatrick McHardy 
1543a5ca857SMartin Willi 	bool			netns_refund;
15528690e53SKuniyuki Iwashima 	const u16		peer_type;
156ccf8dbcdSKees Cook 	unsigned int		maxtype;
15738f7b870SPatrick McHardy 	const struct nla_policy	*policy;
15838f7b870SPatrick McHardy 	int			(*validate)(struct nlattr *tb[],
159a8b8a889SMatthias Schiffer 					    struct nlattr *data[],
160a8b8a889SMatthias Schiffer 					    struct netlink_ext_ack *extack);
16138f7b870SPatrick McHardy 
16269c7be1bSXiao Liang 	int			(*newlink)(struct net_device *dev,
16369c7be1bSXiao Liang 					   struct rtnl_newlink_params *params,
1647a3f4a18SMatthias Schiffer 					   struct netlink_ext_ack *extack);
16538f7b870SPatrick McHardy 	int			(*changelink)(struct net_device *dev,
16638f7b870SPatrick McHardy 					      struct nlattr *tb[],
167ad744b22SMatthias Schiffer 					      struct nlattr *data[],
168ad744b22SMatthias Schiffer 					      struct netlink_ext_ack *extack);
16923289a37SEric Dumazet 	void			(*dellink)(struct net_device *dev,
17023289a37SEric Dumazet 					   struct list_head *head);
17138f7b870SPatrick McHardy 
17238f7b870SPatrick McHardy 	size_t			(*get_size)(const struct net_device *dev);
17338f7b870SPatrick McHardy 	int			(*fill_info)(struct sk_buff *skb,
17438f7b870SPatrick McHardy 					     const struct net_device *dev);
17538f7b870SPatrick McHardy 
17638f7b870SPatrick McHardy 	size_t			(*get_xstats_size)(const struct net_device *dev);
17738f7b870SPatrick McHardy 	int			(*fill_xstats)(struct sk_buff *skb,
17838f7b870SPatrick McHardy 					       const struct net_device *dev);
179d40156aaSJiri Pirko 	unsigned int		(*get_num_tx_queues)(void);
180d40156aaSJiri Pirko 	unsigned int		(*get_num_rx_queues)(void);
181ba7d49b1SJiri Pirko 
182ccf8dbcdSKees Cook 	unsigned int		slave_maxtype;
183ba7d49b1SJiri Pirko 	const struct nla_policy	*slave_policy;
184ba7d49b1SJiri Pirko 	int			(*slave_changelink)(struct net_device *dev,
185ba7d49b1SJiri Pirko 						    struct net_device *slave_dev,
186ba7d49b1SJiri Pirko 						    struct nlattr *tb[],
18717dd0ec4SMatthias Schiffer 						    struct nlattr *data[],
18817dd0ec4SMatthias Schiffer 						    struct netlink_ext_ack *extack);
189ba7d49b1SJiri Pirko 	size_t			(*get_slave_size)(const struct net_device *dev,
190ba7d49b1SJiri Pirko 						  const struct net_device *slave_dev);
191ba7d49b1SJiri Pirko 	int			(*fill_slave_info)(struct sk_buff *skb,
192ba7d49b1SJiri Pirko 						   const struct net_device *dev,
193ba7d49b1SJiri Pirko 						   const struct net_device *slave_dev);
194d37512a2SNicolas Dichtel 	struct net		*(*get_link_net)(const struct net_device *dev);
19580e73cc5SNikolay Aleksandrov 	size_t			(*get_linkxstats_size)(const struct net_device *dev,
19680e73cc5SNikolay Aleksandrov 						       int attr);
19797a47facSNikolay Aleksandrov 	int			(*fill_linkxstats)(struct sk_buff *skb,
19897a47facSNikolay Aleksandrov 						   const struct net_device *dev,
19980e73cc5SNikolay Aleksandrov 						   int *prividx, int attr);
20038f7b870SPatrick McHardy };
20138f7b870SPatrick McHardy 
202efb48ccfSJoe Perches int rtnl_link_register(struct rtnl_link_ops *ops);
203efb48ccfSJoe Perches void rtnl_link_unregister(struct rtnl_link_ops *ops);
20438f7b870SPatrick McHardy 
205f8ff182cSThomas Graf /**
206f8ff182cSThomas Graf  * 	struct rtnl_af_ops - rtnetlink address family operations
207f8ff182cSThomas Graf  *
2086ab0f866SKuniyuki Iwashima  *	@list: Used internally, protected by RTNL and SRCU
2096ab0f866SKuniyuki Iwashima  *	@srcu: Used internally
210f8ff182cSThomas Graf  * 	@family: Address family
211f8ff182cSThomas Graf  * 	@fill_link_af: Function to fill IFLA_AF_SPEC with address family
212f8ff182cSThomas Graf  * 		       specific netlink attributes.
213f8ff182cSThomas Graf  * 	@get_link_af_size: Function to calculate size of address family specific
2149b17876fSstephen hemminger  * 			   netlink attributes.
215cf7afbfeSThomas Graf  *	@validate_link_af: Validate a IFLA_AF_SPEC attribute, must check attr
216cf7afbfeSThomas Graf  *			   for invalid configuration settings.
217cf7afbfeSThomas Graf  * 	@set_link_af: Function to parse a IFLA_AF_SPEC attribute and modify
218f8ff182cSThomas Graf  *		      net_device accordingly.
219f8ff182cSThomas Graf  */
220f8ff182cSThomas Graf struct rtnl_af_ops {
221f8ff182cSThomas Graf 	struct list_head	list;
2226ab0f866SKuniyuki Iwashima 	struct srcu_struct	srcu;
2236ab0f866SKuniyuki Iwashima 
224f8ff182cSThomas Graf 	int			family;
225f8ff182cSThomas Graf 
226f8ff182cSThomas Graf 	int			(*fill_link_af)(struct sk_buff *skb,
227d5566fd7SSowmini Varadhan 						const struct net_device *dev,
228d5566fd7SSowmini Varadhan 						u32 ext_filter_mask);
229b1974ed0SArad, Ronen 	size_t			(*get_link_af_size)(const struct net_device *dev,
230b1974ed0SArad, Ronen 						    u32 ext_filter_mask);
231f8ff182cSThomas Graf 
232cf7afbfeSThomas Graf 	int			(*validate_link_af)(const struct net_device *dev,
2338679c31eSRocco Yue 						    const struct nlattr *attr,
2348679c31eSRocco Yue 						    struct netlink_ext_ack *extack);
235cf7afbfeSThomas Graf 	int			(*set_link_af)(struct net_device *dev,
2363583a4e8SStephen Hemminger 					       const struct nlattr *attr,
2373583a4e8SStephen Hemminger 					       struct netlink_ext_ack *extack);
238aefb4d4aSRobert Shearman 	int			(*fill_stats_af)(struct sk_buff *skb,
239aefb4d4aSRobert Shearman 						 const struct net_device *dev);
240aefb4d4aSRobert Shearman 	size_t			(*get_stats_af_size)(const struct net_device *dev);
241f8ff182cSThomas Graf };
242f8ff182cSThomas Graf 
24326eebdc4SKuniyuki Iwashima int rtnl_af_register(struct rtnl_af_ops *ops);
244efb48ccfSJoe Perches void rtnl_af_unregister(struct rtnl_af_ops *ops);
245f8ff182cSThomas Graf 
246efb48ccfSJoe Perches struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]);
24778ebb0d0SThomas Graf struct net_device *rtnl_create_link(struct net *net, const char *ifname,
2485517750fSTom Gundersen 				    unsigned char name_assign_type,
249efb48ccfSJoe Perches 				    const struct rtnl_link_ops *ops,
250d0522f1cSDavid Ahern 				    struct nlattr *tb[],
251d0522f1cSDavid Ahern 				    struct netlink_ext_ack *extack);
252f3a63cceSHangbin Liu int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh);
2531d997f10SHangbin Liu int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,
2541d997f10SHangbin Liu 			u32 portid, const struct nlmsghdr *nlh);
255f8ff182cSThomas Graf 
256f534f658SJakub Kicinski int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer,
257fceb6435SJohannes Berg 			     struct netlink_ext_ack *exterr);
258c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid);
259e7199288SPavel Emelianov 
26038f7b870SPatrick McHardy #define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind)
26138f7b870SPatrick McHardy 
262e2849863SThomas Graf #endif
263