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