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