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