xref: /linux/include/net/rtnetlink.h (revision ec8c17e5ecb4a5a74069687ccb6d2cfe1851302e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_RTNETLINK_H
3 #define __NET_RTNETLINK_H
4 
5 #include <linux/rtnetlink.h>
6 #include <linux/srcu.h>
7 #include <net/netlink.h>
8 
9 typedef int (*rtnl_doit_func)(struct sk_buff *, struct nlmsghdr *,
10 			      struct netlink_ext_ack *);
11 typedef int (*rtnl_dumpit_func)(struct sk_buff *, struct netlink_callback *);
12 
13 enum rtnl_link_flags {
14 	RTNL_FLAG_DOIT_UNLOCKED		= BIT(0),
15 #define RTNL_FLAG_DOIT_PERNET		RTNL_FLAG_DOIT_UNLOCKED
16 #define RTNL_FLAG_DOIT_PERNET_WIP	RTNL_FLAG_DOIT_UNLOCKED
17 	RTNL_FLAG_BULK_DEL_SUPPORTED	= BIT(1),
18 	RTNL_FLAG_DUMP_UNLOCKED		= BIT(2),
19 	RTNL_FLAG_DUMP_SPLIT_NLM_DONE	= BIT(3),	/* legacy behavior */
20 };
21 
22 enum rtnl_kinds {
23 	RTNL_KIND_NEW,
24 	RTNL_KIND_DEL,
25 	RTNL_KIND_GET,
26 	RTNL_KIND_SET
27 };
28 #define RTNL_KIND_MASK 0x3
29 
rtnl_msgtype_kind(int msgtype)30 static inline enum rtnl_kinds rtnl_msgtype_kind(int msgtype)
31 {
32 	return msgtype & RTNL_KIND_MASK;
33 }
34 
35 /**
36  *	struct rtnl_msg_handler - rtnetlink message type and handlers
37  *
38  *	@owner: NULL for built-in, THIS_MODULE for module
39  *	@protocol: Protocol family or PF_UNSPEC
40  *	@msgtype: rtnetlink message type
41  *	@doit: Function pointer called for each request message
42  *	@dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
43  *	@flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
44  */
45 struct rtnl_msg_handler {
46 	struct module *owner;
47 	int protocol;
48 	int msgtype;
49 	rtnl_doit_func doit;
50 	rtnl_dumpit_func dumpit;
51 	int flags;
52 };
53 
54 void rtnl_unregister_all(int protocol);
55 
56 int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n);
57 void __rtnl_unregister_many(const struct rtnl_msg_handler *handlers, int n);
58 
59 #define rtnl_register_many(handlers)				\
60 	__rtnl_register_many(handlers, ARRAY_SIZE(handlers))
61 #define rtnl_unregister_many(handlers)				\
62 	__rtnl_unregister_many(handlers, ARRAY_SIZE(handlers))
63 
rtnl_msg_family(const struct nlmsghdr * nlh)64 static inline int rtnl_msg_family(const struct nlmsghdr *nlh)
65 {
66 	if (nlmsg_len(nlh) >= sizeof(struct rtgenmsg))
67 		return ((struct rtgenmsg *) nlmsg_data(nlh))->rtgen_family;
68 	else
69 		return AF_UNSPEC;
70 }
71 
72 /**
73  *	struct rtnl_link_ops - rtnetlink link operations
74  *
75  *	@list: Used internally, protected by link_ops_mutex and SRCU
76  *	@srcu: Used internally
77  *	@kind: Identifier
78  *	@netns_refund: Physical device, move to init_net on netns exit
79  *	@peer_type: Peer device specific netlink attribute number (e.g. VETH_INFO_PEER)
80  *	@maxtype: Highest device specific netlink attribute number
81  *	@policy: Netlink policy for device specific attribute validation
82  *	@validate: Optional validation function for netlink/changelink parameters
83  *	@alloc: netdev allocation function, can be %NULL and is then used
84  *		in place of alloc_netdev_mqs(), in this case @priv_size
85  *		and @setup are unused. Returns a netdev or ERR_PTR().
86  *	@priv_size: sizeof net_device private space
87  *	@setup: net_device setup function
88  *	@newlink: Function for configuring and registering a new device
89  *	@changelink: Function for changing parameters of an existing device
90  *	@dellink: Function to remove a device
91  *	@get_size: Function to calculate required room for dumping device
92  *		   specific netlink attributes
93  *	@fill_info: Function to dump device specific netlink attributes
94  *	@get_xstats_size: Function to calculate required room for dumping device
95  *			  specific statistics
96  *	@fill_xstats: Function to dump device specific statistics
97  *	@get_num_tx_queues: Function to determine number of transmit queues
98  *			    to create when creating a new device.
99  *	@get_num_rx_queues: Function to determine number of receive queues
100  *			    to create when creating a new device.
101  *	@get_link_net: Function to get the i/o netns of the device
102  *	@get_linkxstats_size: Function to calculate the required room for
103  *			      dumping device-specific extended link stats
104  *	@fill_linkxstats: Function to dump device-specific extended link stats
105  */
106 struct rtnl_link_ops {
107 	struct list_head	list;
108 	struct srcu_struct	srcu;
109 
110 	const char		*kind;
111 
112 	size_t			priv_size;
113 	struct net_device	*(*alloc)(struct nlattr *tb[],
114 					  const char *ifname,
115 					  unsigned char name_assign_type,
116 					  unsigned int num_tx_queues,
117 					  unsigned int num_rx_queues);
118 	void			(*setup)(struct net_device *dev);
119 
120 	bool			netns_refund;
121 	const u16		peer_type;
122 	unsigned int		maxtype;
123 	const struct nla_policy	*policy;
124 	int			(*validate)(struct nlattr *tb[],
125 					    struct nlattr *data[],
126 					    struct netlink_ext_ack *extack);
127 
128 	int			(*newlink)(struct net *src_net,
129 					   struct net_device *dev,
130 					   struct nlattr *tb[],
131 					   struct nlattr *data[],
132 					   struct netlink_ext_ack *extack);
133 	int			(*changelink)(struct net_device *dev,
134 					      struct nlattr *tb[],
135 					      struct nlattr *data[],
136 					      struct netlink_ext_ack *extack);
137 	void			(*dellink)(struct net_device *dev,
138 					   struct list_head *head);
139 
140 	size_t			(*get_size)(const struct net_device *dev);
141 	int			(*fill_info)(struct sk_buff *skb,
142 					     const struct net_device *dev);
143 
144 	size_t			(*get_xstats_size)(const struct net_device *dev);
145 	int			(*fill_xstats)(struct sk_buff *skb,
146 					       const struct net_device *dev);
147 	unsigned int		(*get_num_tx_queues)(void);
148 	unsigned int		(*get_num_rx_queues)(void);
149 
150 	unsigned int		slave_maxtype;
151 	const struct nla_policy	*slave_policy;
152 	int			(*slave_changelink)(struct net_device *dev,
153 						    struct net_device *slave_dev,
154 						    struct nlattr *tb[],
155 						    struct nlattr *data[],
156 						    struct netlink_ext_ack *extack);
157 	size_t			(*get_slave_size)(const struct net_device *dev,
158 						  const struct net_device *slave_dev);
159 	int			(*fill_slave_info)(struct sk_buff *skb,
160 						   const struct net_device *dev,
161 						   const struct net_device *slave_dev);
162 	struct net		*(*get_link_net)(const struct net_device *dev);
163 	size_t			(*get_linkxstats_size)(const struct net_device *dev,
164 						       int attr);
165 	int			(*fill_linkxstats)(struct sk_buff *skb,
166 						   const struct net_device *dev,
167 						   int *prividx, int attr);
168 };
169 
170 int rtnl_link_register(struct rtnl_link_ops *ops);
171 void rtnl_link_unregister(struct rtnl_link_ops *ops);
172 
173 /**
174  * 	struct rtnl_af_ops - rtnetlink address family operations
175  *
176  *	@list: Used internally, protected by RTNL and SRCU
177  *	@srcu: Used internally
178  * 	@family: Address family
179  * 	@fill_link_af: Function to fill IFLA_AF_SPEC with address family
180  * 		       specific netlink attributes.
181  * 	@get_link_af_size: Function to calculate size of address family specific
182  * 			   netlink attributes.
183  *	@validate_link_af: Validate a IFLA_AF_SPEC attribute, must check attr
184  *			   for invalid configuration settings.
185  * 	@set_link_af: Function to parse a IFLA_AF_SPEC attribute and modify
186  *		      net_device accordingly.
187  */
188 struct rtnl_af_ops {
189 	struct list_head	list;
190 	struct srcu_struct	srcu;
191 
192 	int			family;
193 
194 	int			(*fill_link_af)(struct sk_buff *skb,
195 						const struct net_device *dev,
196 						u32 ext_filter_mask);
197 	size_t			(*get_link_af_size)(const struct net_device *dev,
198 						    u32 ext_filter_mask);
199 
200 	int			(*validate_link_af)(const struct net_device *dev,
201 						    const struct nlattr *attr,
202 						    struct netlink_ext_ack *extack);
203 	int			(*set_link_af)(struct net_device *dev,
204 					       const struct nlattr *attr,
205 					       struct netlink_ext_ack *extack);
206 	int			(*fill_stats_af)(struct sk_buff *skb,
207 						 const struct net_device *dev);
208 	size_t			(*get_stats_af_size)(const struct net_device *dev);
209 };
210 
211 int rtnl_af_register(struct rtnl_af_ops *ops);
212 void rtnl_af_unregister(struct rtnl_af_ops *ops);
213 
214 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]);
215 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
216 				    unsigned char name_assign_type,
217 				    const struct rtnl_link_ops *ops,
218 				    struct nlattr *tb[],
219 				    struct netlink_ext_ack *extack);
220 int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh);
221 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,
222 			u32 portid, const struct nlmsghdr *nlh);
223 
224 int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer,
225 			     struct netlink_ext_ack *exterr);
226 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid);
227 
228 #define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind)
229 
230 #endif
231