xref: /openbmc/linux/include/net/rtnetlink.h (revision 0569e31f1bc2f50613ba4c219f3ecc0d1174d841)
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 {
13*0569e31fSNikolay Aleksandrov 	RTNL_FLAG_DOIT_UNLOCKED = BIT(0),
1462256f98SFlorian Westphal };
1562256f98SFlorian Westphal 
1612dc5c2cSNikolay Aleksandrov enum rtnl_kinds {
1712dc5c2cSNikolay Aleksandrov 	RTNL_KIND_NEW,
1812dc5c2cSNikolay Aleksandrov 	RTNL_KIND_DEL,
1912dc5c2cSNikolay Aleksandrov 	RTNL_KIND_GET,
2012dc5c2cSNikolay Aleksandrov 	RTNL_KIND_SET
2112dc5c2cSNikolay Aleksandrov };
222e9ea3e3SNikolay Aleksandrov #define RTNL_KIND_MASK 0x3
232e9ea3e3SNikolay Aleksandrov 
242e9ea3e3SNikolay Aleksandrov static inline enum rtnl_kinds rtnl_msgtype_kind(int msgtype)
252e9ea3e3SNikolay Aleksandrov {
262e9ea3e3SNikolay Aleksandrov 	return msgtype & RTNL_KIND_MASK;
272e9ea3e3SNikolay Aleksandrov }
2812dc5c2cSNikolay Aleksandrov 
29efb48ccfSJoe Perches void rtnl_register(int protocol, int msgtype,
30b97bac64SFlorian Westphal 		   rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
31e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, int protocol, int msgtype,
32e4202511SFlorian Westphal 			 rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
33efb48ccfSJoe Perches int rtnl_unregister(int protocol, int msgtype);
34efb48ccfSJoe Perches void rtnl_unregister_all(int protocol);
35e2849863SThomas Graf 
363a6c2b41SPatrick McHardy static inline int rtnl_msg_family(const struct nlmsghdr *nlh)
37c454673dSThomas Graf {
38c454673dSThomas Graf 	if (nlmsg_len(nlh) >= sizeof(struct rtgenmsg))
39c454673dSThomas Graf 		return ((struct rtgenmsg *) nlmsg_data(nlh))->rtgen_family;
40c454673dSThomas Graf 	else
41c454673dSThomas Graf 		return AF_UNSPEC;
42c454673dSThomas Graf }
43c454673dSThomas Graf 
4438f7b870SPatrick McHardy /**
4538f7b870SPatrick McHardy  *	struct rtnl_link_ops - rtnetlink link operations
4638f7b870SPatrick McHardy  *
4738f7b870SPatrick McHardy  *	@list: Used internally
4838f7b870SPatrick McHardy  *	@kind: Identifier
493a5ca857SMartin Willi  *	@netns_refund: Physical device, move to init_net on netns exit
5038f7b870SPatrick McHardy  *	@maxtype: Highest device specific netlink attribute number
5138f7b870SPatrick McHardy  *	@policy: Netlink policy for device specific attribute validation
5238f7b870SPatrick McHardy  *	@validate: Optional validation function for netlink/changelink parameters
538c713dc9SJohannes Berg  *	@alloc: netdev allocation function, can be %NULL and is then used
548c713dc9SJohannes Berg  *		in place of alloc_netdev_mqs(), in this case @priv_size
558c713dc9SJohannes Berg  *		and @setup are unused. Returns a netdev or ERR_PTR().
5638f7b870SPatrick McHardy  *	@priv_size: sizeof net_device private space
5738f7b870SPatrick McHardy  *	@setup: net_device setup function
5838f7b870SPatrick McHardy  *	@newlink: Function for configuring and registering a new device
5938f7b870SPatrick McHardy  *	@changelink: Function for changing parameters of an existing device
6038f7b870SPatrick McHardy  *	@dellink: Function to remove a device
6138f7b870SPatrick McHardy  *	@get_size: Function to calculate required room for dumping device
6238f7b870SPatrick McHardy  *		   specific netlink attributes
6338f7b870SPatrick McHardy  *	@fill_info: Function to dump device specific netlink attributes
649b17876fSstephen hemminger  *	@get_xstats_size: Function to calculate required room for dumping device
6538f7b870SPatrick McHardy  *			  specific statistics
6638f7b870SPatrick McHardy  *	@fill_xstats: Function to dump device specific statistics
67d40156aaSJiri Pirko  *	@get_num_tx_queues: Function to determine number of transmit queues
68d40156aaSJiri Pirko  *			    to create when creating a new device.
69d40156aaSJiri Pirko  *	@get_num_rx_queues: Function to determine number of receive queues
70d40156aaSJiri Pirko  *			    to create when creating a new device.
71d37512a2SNicolas Dichtel  *	@get_link_net: Function to get the i/o netns of the device
7297a47facSNikolay Aleksandrov  *	@get_linkxstats_size: Function to calculate the required room for
7397a47facSNikolay Aleksandrov  *			      dumping device-specific extended link stats
7497a47facSNikolay Aleksandrov  *	@fill_linkxstats: Function to dump device-specific extended link stats
7538f7b870SPatrick McHardy  */
7638f7b870SPatrick McHardy struct rtnl_link_ops {
7738f7b870SPatrick McHardy 	struct list_head	list;
7838f7b870SPatrick McHardy 
7938f7b870SPatrick McHardy 	const char		*kind;
8038f7b870SPatrick McHardy 
8138f7b870SPatrick McHardy 	size_t			priv_size;
828c713dc9SJohannes Berg 	struct net_device	*(*alloc)(struct nlattr *tb[],
838c713dc9SJohannes Berg 					  const char *ifname,
848c713dc9SJohannes Berg 					  unsigned char name_assign_type,
858c713dc9SJohannes Berg 					  unsigned int num_tx_queues,
868c713dc9SJohannes Berg 					  unsigned int num_rx_queues);
8738f7b870SPatrick McHardy 	void			(*setup)(struct net_device *dev);
8838f7b870SPatrick McHardy 
893a5ca857SMartin Willi 	bool			netns_refund;
90ccf8dbcdSKees Cook 	unsigned int		maxtype;
9138f7b870SPatrick McHardy 	const struct nla_policy	*policy;
9238f7b870SPatrick McHardy 	int			(*validate)(struct nlattr *tb[],
93a8b8a889SMatthias Schiffer 					    struct nlattr *data[],
94a8b8a889SMatthias Schiffer 					    struct netlink_ext_ack *extack);
9538f7b870SPatrick McHardy 
9681adee47SEric W. Biederman 	int			(*newlink)(struct net *src_net,
9781adee47SEric W. Biederman 					   struct net_device *dev,
9838f7b870SPatrick McHardy 					   struct nlattr *tb[],
997a3f4a18SMatthias Schiffer 					   struct nlattr *data[],
1007a3f4a18SMatthias Schiffer 					   struct netlink_ext_ack *extack);
10138f7b870SPatrick McHardy 	int			(*changelink)(struct net_device *dev,
10238f7b870SPatrick McHardy 					      struct nlattr *tb[],
103ad744b22SMatthias Schiffer 					      struct nlattr *data[],
104ad744b22SMatthias Schiffer 					      struct netlink_ext_ack *extack);
10523289a37SEric Dumazet 	void			(*dellink)(struct net_device *dev,
10623289a37SEric Dumazet 					   struct list_head *head);
10738f7b870SPatrick McHardy 
10838f7b870SPatrick McHardy 	size_t			(*get_size)(const struct net_device *dev);
10938f7b870SPatrick McHardy 	int			(*fill_info)(struct sk_buff *skb,
11038f7b870SPatrick McHardy 					     const struct net_device *dev);
11138f7b870SPatrick McHardy 
11238f7b870SPatrick McHardy 	size_t			(*get_xstats_size)(const struct net_device *dev);
11338f7b870SPatrick McHardy 	int			(*fill_xstats)(struct sk_buff *skb,
11438f7b870SPatrick McHardy 					       const struct net_device *dev);
115d40156aaSJiri Pirko 	unsigned int		(*get_num_tx_queues)(void);
116d40156aaSJiri Pirko 	unsigned int		(*get_num_rx_queues)(void);
117ba7d49b1SJiri Pirko 
118ccf8dbcdSKees Cook 	unsigned int		slave_maxtype;
119ba7d49b1SJiri Pirko 	const struct nla_policy	*slave_policy;
120ba7d49b1SJiri Pirko 	int			(*slave_changelink)(struct net_device *dev,
121ba7d49b1SJiri Pirko 						    struct net_device *slave_dev,
122ba7d49b1SJiri Pirko 						    struct nlattr *tb[],
12317dd0ec4SMatthias Schiffer 						    struct nlattr *data[],
12417dd0ec4SMatthias Schiffer 						    struct netlink_ext_ack *extack);
125ba7d49b1SJiri Pirko 	size_t			(*get_slave_size)(const struct net_device *dev,
126ba7d49b1SJiri Pirko 						  const struct net_device *slave_dev);
127ba7d49b1SJiri Pirko 	int			(*fill_slave_info)(struct sk_buff *skb,
128ba7d49b1SJiri Pirko 						   const struct net_device *dev,
129ba7d49b1SJiri Pirko 						   const struct net_device *slave_dev);
130d37512a2SNicolas Dichtel 	struct net		*(*get_link_net)(const struct net_device *dev);
13180e73cc5SNikolay Aleksandrov 	size_t			(*get_linkxstats_size)(const struct net_device *dev,
13280e73cc5SNikolay Aleksandrov 						       int attr);
13397a47facSNikolay Aleksandrov 	int			(*fill_linkxstats)(struct sk_buff *skb,
13497a47facSNikolay Aleksandrov 						   const struct net_device *dev,
13580e73cc5SNikolay Aleksandrov 						   int *prividx, int attr);
13638f7b870SPatrick McHardy };
13738f7b870SPatrick McHardy 
138efb48ccfSJoe Perches int __rtnl_link_register(struct rtnl_link_ops *ops);
139efb48ccfSJoe Perches void __rtnl_link_unregister(struct rtnl_link_ops *ops);
14038f7b870SPatrick McHardy 
141efb48ccfSJoe Perches int rtnl_link_register(struct rtnl_link_ops *ops);
142efb48ccfSJoe Perches void rtnl_link_unregister(struct rtnl_link_ops *ops);
14338f7b870SPatrick McHardy 
144f8ff182cSThomas Graf /**
145f8ff182cSThomas Graf  * 	struct rtnl_af_ops - rtnetlink address family operations
146f8ff182cSThomas Graf  *
147f8ff182cSThomas Graf  *	@list: Used internally
148f8ff182cSThomas Graf  * 	@family: Address family
149f8ff182cSThomas Graf  * 	@fill_link_af: Function to fill IFLA_AF_SPEC with address family
150f8ff182cSThomas Graf  * 		       specific netlink attributes.
151f8ff182cSThomas Graf  * 	@get_link_af_size: Function to calculate size of address family specific
1529b17876fSstephen hemminger  * 			   netlink attributes.
153cf7afbfeSThomas Graf  *	@validate_link_af: Validate a IFLA_AF_SPEC attribute, must check attr
154cf7afbfeSThomas Graf  *			   for invalid configuration settings.
155cf7afbfeSThomas Graf  * 	@set_link_af: Function to parse a IFLA_AF_SPEC attribute and modify
156f8ff182cSThomas Graf  *		      net_device accordingly.
157f8ff182cSThomas Graf  */
158f8ff182cSThomas Graf struct rtnl_af_ops {
159f8ff182cSThomas Graf 	struct list_head	list;
160f8ff182cSThomas Graf 	int			family;
161f8ff182cSThomas Graf 
162f8ff182cSThomas Graf 	int			(*fill_link_af)(struct sk_buff *skb,
163d5566fd7SSowmini Varadhan 						const struct net_device *dev,
164d5566fd7SSowmini Varadhan 						u32 ext_filter_mask);
165b1974ed0SArad, Ronen 	size_t			(*get_link_af_size)(const struct net_device *dev,
166b1974ed0SArad, Ronen 						    u32 ext_filter_mask);
167f8ff182cSThomas Graf 
168cf7afbfeSThomas Graf 	int			(*validate_link_af)(const struct net_device *dev,
1698679c31eSRocco Yue 						    const struct nlattr *attr,
1708679c31eSRocco Yue 						    struct netlink_ext_ack *extack);
171cf7afbfeSThomas Graf 	int			(*set_link_af)(struct net_device *dev,
1723583a4e8SStephen Hemminger 					       const struct nlattr *attr,
1733583a4e8SStephen Hemminger 					       struct netlink_ext_ack *extack);
174aefb4d4aSRobert Shearman 	int			(*fill_stats_af)(struct sk_buff *skb,
175aefb4d4aSRobert Shearman 						 const struct net_device *dev);
176aefb4d4aSRobert Shearman 	size_t			(*get_stats_af_size)(const struct net_device *dev);
177f8ff182cSThomas Graf };
178f8ff182cSThomas Graf 
1793678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops);
180efb48ccfSJoe Perches void rtnl_af_unregister(struct rtnl_af_ops *ops);
181f8ff182cSThomas Graf 
182efb48ccfSJoe Perches struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]);
18378ebb0d0SThomas Graf struct net_device *rtnl_create_link(struct net *net, const char *ifname,
1845517750fSTom Gundersen 				    unsigned char name_assign_type,
185efb48ccfSJoe Perches 				    const struct rtnl_link_ops *ops,
186d0522f1cSDavid Ahern 				    struct nlattr *tb[],
187d0522f1cSDavid Ahern 				    struct netlink_ext_ack *extack);
188614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev);
189efb48ccfSJoe Perches int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm);
190f8ff182cSThomas Graf 
191fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
192fceb6435SJohannes Berg 			struct netlink_ext_ack *exterr);
193c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid);
194e7199288SPavel Emelianov 
19538f7b870SPatrick McHardy #define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind)
19638f7b870SPatrick McHardy 
197e2849863SThomas Graf #endif
198