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