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