xref: /openbmc/linux/include/net/rtnetlink.h (revision 35b72beac967240c0a942a008284475b09440db9)
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 
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 
30*35b72beaSKuniyuki Iwashima struct rtnl_msg_handler {
31*35b72beaSKuniyuki Iwashima 	struct module *owner;
32*35b72beaSKuniyuki Iwashima 	int protocol;
33*35b72beaSKuniyuki Iwashima 	int msgtype;
34*35b72beaSKuniyuki Iwashima 	rtnl_doit_func doit;
35*35b72beaSKuniyuki Iwashima 	rtnl_dumpit_func dumpit;
36*35b72beaSKuniyuki Iwashima 	int flags;
37*35b72beaSKuniyuki Iwashima };
38*35b72beaSKuniyuki Iwashima 
39efb48ccfSJoe Perches void rtnl_register(int protocol, int msgtype,
40b97bac64SFlorian Westphal 		   rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
41e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, int protocol, int msgtype,
42e4202511SFlorian Westphal 			 rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
43efb48ccfSJoe Perches int rtnl_unregister(int protocol, int msgtype);
44efb48ccfSJoe Perches void rtnl_unregister_all(int protocol);
45e2849863SThomas Graf 
46*35b72beaSKuniyuki Iwashima int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n);
47*35b72beaSKuniyuki Iwashima void __rtnl_unregister_many(const struct rtnl_msg_handler *handlers, int n);
48*35b72beaSKuniyuki Iwashima 
49*35b72beaSKuniyuki Iwashima #define rtnl_register_many(handlers)				\
50*35b72beaSKuniyuki Iwashima 	__rtnl_register_many(handlers, ARRAY_SIZE(handlers))
51*35b72beaSKuniyuki Iwashima #define rtnl_unregister_many(handlers)				\
52*35b72beaSKuniyuki Iwashima 	__rtnl_unregister_many(handlers, ARRAY_SIZE(handlers))
53*35b72beaSKuniyuki Iwashima 
543a6c2b41SPatrick McHardy static inline int rtnl_msg_family(const struct nlmsghdr *nlh)
55c454673dSThomas Graf {
56c454673dSThomas Graf 	if (nlmsg_len(nlh) >= sizeof(struct rtgenmsg))
57c454673dSThomas Graf 		return ((struct rtgenmsg *) nlmsg_data(nlh))->rtgen_family;
58c454673dSThomas Graf 	else
59c454673dSThomas Graf 		return AF_UNSPEC;
60c454673dSThomas Graf }
61c454673dSThomas Graf 
6238f7b870SPatrick McHardy /**
6338f7b870SPatrick McHardy  *	struct rtnl_link_ops - rtnetlink link operations
6438f7b870SPatrick McHardy  *
6538f7b870SPatrick McHardy  *	@list: Used internally
6638f7b870SPatrick McHardy  *	@kind: Identifier
673a5ca857SMartin Willi  *	@netns_refund: Physical device, move to init_net on netns exit
6838f7b870SPatrick McHardy  *	@maxtype: Highest device specific netlink attribute number
6938f7b870SPatrick McHardy  *	@policy: Netlink policy for device specific attribute validation
7038f7b870SPatrick McHardy  *	@validate: Optional validation function for netlink/changelink parameters
718c713dc9SJohannes Berg  *	@alloc: netdev allocation function, can be %NULL and is then used
728c713dc9SJohannes Berg  *		in place of alloc_netdev_mqs(), in this case @priv_size
738c713dc9SJohannes Berg  *		and @setup are unused. Returns a netdev or ERR_PTR().
7438f7b870SPatrick McHardy  *	@priv_size: sizeof net_device private space
7538f7b870SPatrick McHardy  *	@setup: net_device setup function
7638f7b870SPatrick McHardy  *	@newlink: Function for configuring and registering a new device
7738f7b870SPatrick McHardy  *	@changelink: Function for changing parameters of an existing device
7838f7b870SPatrick McHardy  *	@dellink: Function to remove a device
7938f7b870SPatrick McHardy  *	@get_size: Function to calculate required room for dumping device
8038f7b870SPatrick McHardy  *		   specific netlink attributes
8138f7b870SPatrick McHardy  *	@fill_info: Function to dump device specific netlink attributes
829b17876fSstephen hemminger  *	@get_xstats_size: Function to calculate required room for dumping device
8338f7b870SPatrick McHardy  *			  specific statistics
8438f7b870SPatrick McHardy  *	@fill_xstats: Function to dump device specific statistics
85d40156aaSJiri Pirko  *	@get_num_tx_queues: Function to determine number of transmit queues
86d40156aaSJiri Pirko  *			    to create when creating a new device.
87d40156aaSJiri Pirko  *	@get_num_rx_queues: Function to determine number of receive queues
88d40156aaSJiri Pirko  *			    to create when creating a new device.
89d37512a2SNicolas Dichtel  *	@get_link_net: Function to get the i/o netns of the device
9097a47facSNikolay Aleksandrov  *	@get_linkxstats_size: Function to calculate the required room for
9197a47facSNikolay Aleksandrov  *			      dumping device-specific extended link stats
9297a47facSNikolay Aleksandrov  *	@fill_linkxstats: Function to dump device-specific extended link stats
9338f7b870SPatrick McHardy  */
9438f7b870SPatrick McHardy struct rtnl_link_ops {
9538f7b870SPatrick McHardy 	struct list_head	list;
9638f7b870SPatrick McHardy 
9738f7b870SPatrick McHardy 	const char		*kind;
9838f7b870SPatrick McHardy 
9938f7b870SPatrick McHardy 	size_t			priv_size;
1008c713dc9SJohannes Berg 	struct net_device	*(*alloc)(struct nlattr *tb[],
1018c713dc9SJohannes Berg 					  const char *ifname,
1028c713dc9SJohannes Berg 					  unsigned char name_assign_type,
1038c713dc9SJohannes Berg 					  unsigned int num_tx_queues,
1048c713dc9SJohannes Berg 					  unsigned int num_rx_queues);
10538f7b870SPatrick McHardy 	void			(*setup)(struct net_device *dev);
10638f7b870SPatrick McHardy 
1073a5ca857SMartin Willi 	bool			netns_refund;
108ccf8dbcdSKees Cook 	unsigned int		maxtype;
10938f7b870SPatrick McHardy 	const struct nla_policy	*policy;
11038f7b870SPatrick McHardy 	int			(*validate)(struct nlattr *tb[],
111a8b8a889SMatthias Schiffer 					    struct nlattr *data[],
112a8b8a889SMatthias Schiffer 					    struct netlink_ext_ack *extack);
11338f7b870SPatrick McHardy 
11481adee47SEric W. Biederman 	int			(*newlink)(struct net *src_net,
11581adee47SEric W. Biederman 					   struct net_device *dev,
11638f7b870SPatrick McHardy 					   struct nlattr *tb[],
1177a3f4a18SMatthias Schiffer 					   struct nlattr *data[],
1187a3f4a18SMatthias Schiffer 					   struct netlink_ext_ack *extack);
11938f7b870SPatrick McHardy 	int			(*changelink)(struct net_device *dev,
12038f7b870SPatrick McHardy 					      struct nlattr *tb[],
121ad744b22SMatthias Schiffer 					      struct nlattr *data[],
122ad744b22SMatthias Schiffer 					      struct netlink_ext_ack *extack);
12323289a37SEric Dumazet 	void			(*dellink)(struct net_device *dev,
12423289a37SEric Dumazet 					   struct list_head *head);
12538f7b870SPatrick McHardy 
12638f7b870SPatrick McHardy 	size_t			(*get_size)(const struct net_device *dev);
12738f7b870SPatrick McHardy 	int			(*fill_info)(struct sk_buff *skb,
12838f7b870SPatrick McHardy 					     const struct net_device *dev);
12938f7b870SPatrick McHardy 
13038f7b870SPatrick McHardy 	size_t			(*get_xstats_size)(const struct net_device *dev);
13138f7b870SPatrick McHardy 	int			(*fill_xstats)(struct sk_buff *skb,
13238f7b870SPatrick McHardy 					       const struct net_device *dev);
133d40156aaSJiri Pirko 	unsigned int		(*get_num_tx_queues)(void);
134d40156aaSJiri Pirko 	unsigned int		(*get_num_rx_queues)(void);
135ba7d49b1SJiri Pirko 
136ccf8dbcdSKees Cook 	unsigned int		slave_maxtype;
137ba7d49b1SJiri Pirko 	const struct nla_policy	*slave_policy;
138ba7d49b1SJiri Pirko 	int			(*slave_changelink)(struct net_device *dev,
139ba7d49b1SJiri Pirko 						    struct net_device *slave_dev,
140ba7d49b1SJiri Pirko 						    struct nlattr *tb[],
14117dd0ec4SMatthias Schiffer 						    struct nlattr *data[],
14217dd0ec4SMatthias Schiffer 						    struct netlink_ext_ack *extack);
143ba7d49b1SJiri Pirko 	size_t			(*get_slave_size)(const struct net_device *dev,
144ba7d49b1SJiri Pirko 						  const struct net_device *slave_dev);
145ba7d49b1SJiri Pirko 	int			(*fill_slave_info)(struct sk_buff *skb,
146ba7d49b1SJiri Pirko 						   const struct net_device *dev,
147ba7d49b1SJiri Pirko 						   const struct net_device *slave_dev);
148d37512a2SNicolas Dichtel 	struct net		*(*get_link_net)(const struct net_device *dev);
14980e73cc5SNikolay Aleksandrov 	size_t			(*get_linkxstats_size)(const struct net_device *dev,
15080e73cc5SNikolay Aleksandrov 						       int attr);
15197a47facSNikolay Aleksandrov 	int			(*fill_linkxstats)(struct sk_buff *skb,
15297a47facSNikolay Aleksandrov 						   const struct net_device *dev,
15380e73cc5SNikolay Aleksandrov 						   int *prividx, int attr);
15438f7b870SPatrick McHardy };
15538f7b870SPatrick McHardy 
156efb48ccfSJoe Perches int __rtnl_link_register(struct rtnl_link_ops *ops);
157efb48ccfSJoe Perches void __rtnl_link_unregister(struct rtnl_link_ops *ops);
15838f7b870SPatrick McHardy 
159efb48ccfSJoe Perches int rtnl_link_register(struct rtnl_link_ops *ops);
160efb48ccfSJoe Perches void rtnl_link_unregister(struct rtnl_link_ops *ops);
16138f7b870SPatrick McHardy 
162f8ff182cSThomas Graf /**
163f8ff182cSThomas Graf  * 	struct rtnl_af_ops - rtnetlink address family operations
164f8ff182cSThomas Graf  *
165f8ff182cSThomas Graf  *	@list: Used internally
166f8ff182cSThomas Graf  * 	@family: Address family
167f8ff182cSThomas Graf  * 	@fill_link_af: Function to fill IFLA_AF_SPEC with address family
168f8ff182cSThomas Graf  * 		       specific netlink attributes.
169f8ff182cSThomas Graf  * 	@get_link_af_size: Function to calculate size of address family specific
1709b17876fSstephen hemminger  * 			   netlink attributes.
171cf7afbfeSThomas Graf  *	@validate_link_af: Validate a IFLA_AF_SPEC attribute, must check attr
172cf7afbfeSThomas Graf  *			   for invalid configuration settings.
173cf7afbfeSThomas Graf  * 	@set_link_af: Function to parse a IFLA_AF_SPEC attribute and modify
174f8ff182cSThomas Graf  *		      net_device accordingly.
175f8ff182cSThomas Graf  */
176f8ff182cSThomas Graf struct rtnl_af_ops {
177f8ff182cSThomas Graf 	struct list_head	list;
178f8ff182cSThomas Graf 	int			family;
179f8ff182cSThomas Graf 
180f8ff182cSThomas Graf 	int			(*fill_link_af)(struct sk_buff *skb,
181d5566fd7SSowmini Varadhan 						const struct net_device *dev,
182d5566fd7SSowmini Varadhan 						u32 ext_filter_mask);
183b1974ed0SArad, Ronen 	size_t			(*get_link_af_size)(const struct net_device *dev,
184b1974ed0SArad, Ronen 						    u32 ext_filter_mask);
185f8ff182cSThomas Graf 
186cf7afbfeSThomas Graf 	int			(*validate_link_af)(const struct net_device *dev,
1878679c31eSRocco Yue 						    const struct nlattr *attr,
1888679c31eSRocco Yue 						    struct netlink_ext_ack *extack);
189cf7afbfeSThomas Graf 	int			(*set_link_af)(struct net_device *dev,
1903583a4e8SStephen Hemminger 					       const struct nlattr *attr,
1913583a4e8SStephen Hemminger 					       struct netlink_ext_ack *extack);
192aefb4d4aSRobert Shearman 	int			(*fill_stats_af)(struct sk_buff *skb,
193aefb4d4aSRobert Shearman 						 const struct net_device *dev);
194aefb4d4aSRobert Shearman 	size_t			(*get_stats_af_size)(const struct net_device *dev);
195f8ff182cSThomas Graf };
196f8ff182cSThomas Graf 
1973678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops);
198efb48ccfSJoe Perches void rtnl_af_unregister(struct rtnl_af_ops *ops);
199f8ff182cSThomas Graf 
200efb48ccfSJoe Perches struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]);
20178ebb0d0SThomas Graf struct net_device *rtnl_create_link(struct net *net, const char *ifname,
2025517750fSTom Gundersen 				    unsigned char name_assign_type,
203efb48ccfSJoe Perches 				    const struct rtnl_link_ops *ops,
204d0522f1cSDavid Ahern 				    struct nlattr *tb[],
205d0522f1cSDavid Ahern 				    struct netlink_ext_ack *extack);
206f3a63cceSHangbin Liu int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh);
2071d997f10SHangbin Liu int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,
2081d997f10SHangbin Liu 			u32 portid, const struct nlmsghdr *nlh);
209f8ff182cSThomas Graf 
210f534f658SJakub Kicinski int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer,
211fceb6435SJohannes Berg 			     struct netlink_ext_ack *exterr);
212c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid);
213e7199288SPavel Emelianov 
21438f7b870SPatrick McHardy #define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind)
21538f7b870SPatrick McHardy 
216e2849863SThomas Graf #endif
217