xref: /openbmc/linux/include/net/rtnetlink.h (revision f534f658)
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>
6e2849863SThomas Graf #include <net/netlink.h>
7e2849863SThomas Graf 
8c21ef3e3SDavid Ahern typedef int (*rtnl_doit_func)(struct sk_buff *, struct nlmsghdr *,
9c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *);
10e2849863SThomas Graf typedef int (*rtnl_dumpit_func)(struct sk_buff *, struct netlink_callback *);
11e2849863SThomas Graf 
1262256f98SFlorian Westphal enum rtnl_link_flags {
130569e31fSNikolay Aleksandrov 	RTNL_FLAG_DOIT_UNLOCKED		= BIT(0),
14a6cec0bcSNikolay Aleksandrov 	RTNL_FLAG_BULK_DEL_SUPPORTED	= BIT(1),
1562256f98SFlorian Westphal };
1662256f98SFlorian Westphal 
1712dc5c2cSNikolay Aleksandrov enum rtnl_kinds {
1812dc5c2cSNikolay Aleksandrov 	RTNL_KIND_NEW,
1912dc5c2cSNikolay Aleksandrov 	RTNL_KIND_DEL,
2012dc5c2cSNikolay Aleksandrov 	RTNL_KIND_GET,
2112dc5c2cSNikolay Aleksandrov 	RTNL_KIND_SET
2212dc5c2cSNikolay Aleksandrov };
232e9ea3e3SNikolay Aleksandrov #define RTNL_KIND_MASK 0x3
242e9ea3e3SNikolay Aleksandrov 
rtnl_msgtype_kind(int msgtype)252e9ea3e3SNikolay Aleksandrov static inline enum rtnl_kinds rtnl_msgtype_kind(int msgtype)
262e9ea3e3SNikolay Aleksandrov {
272e9ea3e3SNikolay Aleksandrov 	return msgtype & RTNL_KIND_MASK;
282e9ea3e3SNikolay Aleksandrov }
2912dc5c2cSNikolay Aleksandrov 
30efb48ccfSJoe Perches void rtnl_register(int protocol, int msgtype,
31b97bac64SFlorian Westphal 		   rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
32e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, int protocol, int msgtype,
33e4202511SFlorian Westphal 			 rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
34efb48ccfSJoe Perches int rtnl_unregister(int protocol, int msgtype);
35efb48ccfSJoe Perches void rtnl_unregister_all(int protocol);
36e2849863SThomas Graf 
rtnl_msg_family(const struct nlmsghdr * nlh)373a6c2b41SPatrick McHardy static inline int rtnl_msg_family(const struct nlmsghdr *nlh)
38c454673dSThomas Graf {
39c454673dSThomas Graf 	if (nlmsg_len(nlh) >= sizeof(struct rtgenmsg))
40c454673dSThomas Graf 		return ((struct rtgenmsg *) nlmsg_data(nlh))->rtgen_family;
41c454673dSThomas Graf 	else
42c454673dSThomas Graf 		return AF_UNSPEC;
43c454673dSThomas Graf }
44c454673dSThomas Graf 
4538f7b870SPatrick McHardy /**
4638f7b870SPatrick McHardy  *	struct rtnl_link_ops - rtnetlink link operations
4738f7b870SPatrick McHardy  *
4838f7b870SPatrick McHardy  *	@list: Used internally
4938f7b870SPatrick McHardy  *	@kind: Identifier
503a5ca857SMartin Willi  *	@netns_refund: Physical device, move to init_net on netns exit
5138f7b870SPatrick McHardy  *	@maxtype: Highest device specific netlink attribute number
5238f7b870SPatrick McHardy  *	@policy: Netlink policy for device specific attribute validation
5338f7b870SPatrick McHardy  *	@validate: Optional validation function for netlink/changelink parameters
548c713dc9SJohannes Berg  *	@alloc: netdev allocation function, can be %NULL and is then used
558c713dc9SJohannes Berg  *		in place of alloc_netdev_mqs(), in this case @priv_size
568c713dc9SJohannes Berg  *		and @setup are unused. Returns a netdev or ERR_PTR().
5738f7b870SPatrick McHardy  *	@priv_size: sizeof net_device private space
5838f7b870SPatrick McHardy  *	@setup: net_device setup function
5938f7b870SPatrick McHardy  *	@newlink: Function for configuring and registering a new device
6038f7b870SPatrick McHardy  *	@changelink: Function for changing parameters of an existing device
6138f7b870SPatrick McHardy  *	@dellink: Function to remove a device
6238f7b870SPatrick McHardy  *	@get_size: Function to calculate required room for dumping device
6338f7b870SPatrick McHardy  *		   specific netlink attributes
6438f7b870SPatrick McHardy  *	@fill_info: Function to dump device specific netlink attributes
659b17876fSstephen hemminger  *	@get_xstats_size: Function to calculate required room for dumping device
6638f7b870SPatrick McHardy  *			  specific statistics
6738f7b870SPatrick McHardy  *	@fill_xstats: Function to dump device specific statistics
68d40156aaSJiri Pirko  *	@get_num_tx_queues: Function to determine number of transmit queues
69d40156aaSJiri Pirko  *			    to create when creating a new device.
70d40156aaSJiri Pirko  *	@get_num_rx_queues: Function to determine number of receive queues
71d40156aaSJiri Pirko  *			    to create when creating a new device.
72d37512a2SNicolas Dichtel  *	@get_link_net: Function to get the i/o netns of the device
7397a47facSNikolay Aleksandrov  *	@get_linkxstats_size: Function to calculate the required room for
7497a47facSNikolay Aleksandrov  *			      dumping device-specific extended link stats
7597a47facSNikolay Aleksandrov  *	@fill_linkxstats: Function to dump device-specific extended link stats
7638f7b870SPatrick McHardy  */
7738f7b870SPatrick McHardy struct rtnl_link_ops {
7838f7b870SPatrick McHardy 	struct list_head	list;
7938f7b870SPatrick McHardy 
8038f7b870SPatrick McHardy 	const char		*kind;
8138f7b870SPatrick McHardy 
8238f7b870SPatrick McHardy 	size_t			priv_size;
838c713dc9SJohannes Berg 	struct net_device	*(*alloc)(struct nlattr *tb[],
848c713dc9SJohannes Berg 					  const char *ifname,
858c713dc9SJohannes Berg 					  unsigned char name_assign_type,
868c713dc9SJohannes Berg 					  unsigned int num_tx_queues,
878c713dc9SJohannes Berg 					  unsigned int num_rx_queues);
8838f7b870SPatrick McHardy 	void			(*setup)(struct net_device *dev);
8938f7b870SPatrick McHardy 
903a5ca857SMartin Willi 	bool			netns_refund;
91ccf8dbcdSKees Cook 	unsigned int		maxtype;
9238f7b870SPatrick McHardy 	const struct nla_policy	*policy;
9338f7b870SPatrick McHardy 	int			(*validate)(struct nlattr *tb[],
94a8b8a889SMatthias Schiffer 					    struct nlattr *data[],
95a8b8a889SMatthias Schiffer 					    struct netlink_ext_ack *extack);
9638f7b870SPatrick McHardy 
9781adee47SEric W. Biederman 	int			(*newlink)(struct net *src_net,
9881adee47SEric W. Biederman 					   struct net_device *dev,
9938f7b870SPatrick McHardy 					   struct nlattr *tb[],
1007a3f4a18SMatthias Schiffer 					   struct nlattr *data[],
1017a3f4a18SMatthias Schiffer 					   struct netlink_ext_ack *extack);
10238f7b870SPatrick McHardy 	int			(*changelink)(struct net_device *dev,
10338f7b870SPatrick McHardy 					      struct nlattr *tb[],
104ad744b22SMatthias Schiffer 					      struct nlattr *data[],
105ad744b22SMatthias Schiffer 					      struct netlink_ext_ack *extack);
10623289a37SEric Dumazet 	void			(*dellink)(struct net_device *dev,
10723289a37SEric Dumazet 					   struct list_head *head);
10838f7b870SPatrick McHardy 
10938f7b870SPatrick McHardy 	size_t			(*get_size)(const struct net_device *dev);
11038f7b870SPatrick McHardy 	int			(*fill_info)(struct sk_buff *skb,
11138f7b870SPatrick McHardy 					     const struct net_device *dev);
11238f7b870SPatrick McHardy 
11338f7b870SPatrick McHardy 	size_t			(*get_xstats_size)(const struct net_device *dev);
11438f7b870SPatrick McHardy 	int			(*fill_xstats)(struct sk_buff *skb,
11538f7b870SPatrick McHardy 					       const struct net_device *dev);
116d40156aaSJiri Pirko 	unsigned int		(*get_num_tx_queues)(void);
117d40156aaSJiri Pirko 	unsigned int		(*get_num_rx_queues)(void);
118ba7d49b1SJiri Pirko 
119ccf8dbcdSKees Cook 	unsigned int		slave_maxtype;
120ba7d49b1SJiri Pirko 	const struct nla_policy	*slave_policy;
121ba7d49b1SJiri Pirko 	int			(*slave_changelink)(struct net_device *dev,
122ba7d49b1SJiri Pirko 						    struct net_device *slave_dev,
123ba7d49b1SJiri Pirko 						    struct nlattr *tb[],
12417dd0ec4SMatthias Schiffer 						    struct nlattr *data[],
12517dd0ec4SMatthias Schiffer 						    struct netlink_ext_ack *extack);
126ba7d49b1SJiri Pirko 	size_t			(*get_slave_size)(const struct net_device *dev,
127ba7d49b1SJiri Pirko 						  const struct net_device *slave_dev);
128ba7d49b1SJiri Pirko 	int			(*fill_slave_info)(struct sk_buff *skb,
129ba7d49b1SJiri Pirko 						   const struct net_device *dev,
130ba7d49b1SJiri Pirko 						   const struct net_device *slave_dev);
131d37512a2SNicolas Dichtel 	struct net		*(*get_link_net)(const struct net_device *dev);
13280e73cc5SNikolay Aleksandrov 	size_t			(*get_linkxstats_size)(const struct net_device *dev,
13380e73cc5SNikolay Aleksandrov 						       int attr);
13497a47facSNikolay Aleksandrov 	int			(*fill_linkxstats)(struct sk_buff *skb,
13597a47facSNikolay Aleksandrov 						   const struct net_device *dev,
13680e73cc5SNikolay Aleksandrov 						   int *prividx, int attr);
13738f7b870SPatrick McHardy };
13838f7b870SPatrick McHardy 
139efb48ccfSJoe Perches int __rtnl_link_register(struct rtnl_link_ops *ops);
140efb48ccfSJoe Perches void __rtnl_link_unregister(struct rtnl_link_ops *ops);
14138f7b870SPatrick McHardy 
142efb48ccfSJoe Perches int rtnl_link_register(struct rtnl_link_ops *ops);
143efb48ccfSJoe Perches void rtnl_link_unregister(struct rtnl_link_ops *ops);
14438f7b870SPatrick McHardy 
145f8ff182cSThomas Graf /**
146f8ff182cSThomas Graf  * 	struct rtnl_af_ops - rtnetlink address family operations
147f8ff182cSThomas Graf  *
148f8ff182cSThomas Graf  *	@list: Used internally
149f8ff182cSThomas Graf  * 	@family: Address family
150f8ff182cSThomas Graf  * 	@fill_link_af: Function to fill IFLA_AF_SPEC with address family
151f8ff182cSThomas Graf  * 		       specific netlink attributes.
152f8ff182cSThomas Graf  * 	@get_link_af_size: Function to calculate size of address family specific
1539b17876fSstephen hemminger  * 			   netlink attributes.
154cf7afbfeSThomas Graf  *	@validate_link_af: Validate a IFLA_AF_SPEC attribute, must check attr
155cf7afbfeSThomas Graf  *			   for invalid configuration settings.
156cf7afbfeSThomas Graf  * 	@set_link_af: Function to parse a IFLA_AF_SPEC attribute and modify
157f8ff182cSThomas Graf  *		      net_device accordingly.
158f8ff182cSThomas Graf  */
159f8ff182cSThomas Graf struct rtnl_af_ops {
160f8ff182cSThomas Graf 	struct list_head	list;
161f8ff182cSThomas Graf 	int			family;
162f8ff182cSThomas Graf 
163f8ff182cSThomas Graf 	int			(*fill_link_af)(struct sk_buff *skb,
164d5566fd7SSowmini Varadhan 						const struct net_device *dev,
165d5566fd7SSowmini Varadhan 						u32 ext_filter_mask);
166b1974ed0SArad, Ronen 	size_t			(*get_link_af_size)(const struct net_device *dev,
167b1974ed0SArad, Ronen 						    u32 ext_filter_mask);
168f8ff182cSThomas Graf 
169cf7afbfeSThomas Graf 	int			(*validate_link_af)(const struct net_device *dev,
1708679c31eSRocco Yue 						    const struct nlattr *attr,
1718679c31eSRocco Yue 						    struct netlink_ext_ack *extack);
172cf7afbfeSThomas Graf 	int			(*set_link_af)(struct net_device *dev,
1733583a4e8SStephen Hemminger 					       const struct nlattr *attr,
1743583a4e8SStephen Hemminger 					       struct netlink_ext_ack *extack);
175aefb4d4aSRobert Shearman 	int			(*fill_stats_af)(struct sk_buff *skb,
176aefb4d4aSRobert Shearman 						 const struct net_device *dev);
177aefb4d4aSRobert Shearman 	size_t			(*get_stats_af_size)(const struct net_device *dev);
178f8ff182cSThomas Graf };
179f8ff182cSThomas Graf 
1803678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops);
181efb48ccfSJoe Perches void rtnl_af_unregister(struct rtnl_af_ops *ops);
182f8ff182cSThomas Graf 
183efb48ccfSJoe Perches struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]);
18478ebb0d0SThomas Graf struct net_device *rtnl_create_link(struct net *net, const char *ifname,
1855517750fSTom Gundersen 				    unsigned char name_assign_type,
186efb48ccfSJoe Perches 				    const struct rtnl_link_ops *ops,
187d0522f1cSDavid Ahern 				    struct nlattr *tb[],
188d0522f1cSDavid Ahern 				    struct netlink_ext_ack *extack);
189f3a63cceSHangbin Liu int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh);
1901d997f10SHangbin Liu int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,
1911d997f10SHangbin Liu 			u32 portid, const struct nlmsghdr *nlh);
192f8ff182cSThomas Graf 
193*f534f658SJakub Kicinski int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer,
194fceb6435SJohannes Berg 			     struct netlink_ext_ack *exterr);
195c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid);
196e7199288SPavel Emelianov 
19738f7b870SPatrick McHardy #define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind)
19838f7b870SPatrick McHardy 
199e2849863SThomas Graf #endif
200