]>
Commit | Line | Data |
---|---|---|
f9e815b3 HW |
1 | /* Netfilter messages via netlink socket. Allows for user space |
2 | * protocol helpers and general trouble making from userspace. | |
3 | * | |
4 | * (C) 2001 by Jay Schulist <jschlst@samba.org>, | |
5 | * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org> | |
8c4d4e8b | 6 | * (C) 2005-2017 by Pablo Neira Ayuso <pablo@netfilter.org> |
f9e815b3 HW |
7 | * |
8 | * Initial netfilter messages via netlink development funded and | |
9 | * generally made possible by Network Robots, Inc. (www.networkrobots.com) | |
10 | * | |
11 | * Further development of this code funded by Astaro AG (http://www.astaro.com) | |
12 | * | |
13 | * This software may be used and distributed according to the terms | |
14 | * of the GNU General Public License, incorporated herein by reference. | |
15 | */ | |
16 | ||
f9e815b3 HW |
17 | #include <linux/module.h> |
18 | #include <linux/types.h> | |
19 | #include <linux/socket.h> | |
20 | #include <linux/kernel.h> | |
f9e815b3 HW |
21 | #include <linux/string.h> |
22 | #include <linux/sockios.h> | |
23 | #include <linux/net.h> | |
f9e815b3 | 24 | #include <linux/skbuff.h> |
7c0f6ba6 | 25 | #include <linux/uaccess.h> |
f9e815b3 HW |
26 | #include <net/sock.h> |
27 | #include <linux/init.h> | |
8a3d4c36 | 28 | #include <linux/sched/signal.h> |
f9e815b3 | 29 | |
573ce260 | 30 | #include <net/netlink.h> |
f9e815b3 HW |
31 | #include <linux/netfilter/nfnetlink.h> |
32 | ||
33 | MODULE_LICENSE("GPL"); | |
4fdb3bb7 HW |
34 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
35 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER); | |
f9e815b3 | 36 | |
9c55d3b5 FW |
37 | #define nfnl_dereference_protected(id) \ |
38 | rcu_dereference_protected(table[(id)].subsys, \ | |
39 | lockdep_nfnl_is_held((id))) | |
40 | ||
7b7744e2 KC |
41 | #define NFNL_MAX_ATTR_COUNT 32 |
42 | ||
c14b78e7 PNA |
43 | static struct { |
44 | struct mutex mutex; | |
45 | const struct nfnetlink_subsystem __rcu *subsys; | |
46 | } table[NFNL_SUBSYS_COUNT]; | |
f9e815b3 | 47 | |
03292745 PNA |
48 | static const int nfnl_group2type[NFNLGRP_MAX+1] = { |
49 | [NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK, | |
50 | [NFNLGRP_CONNTRACK_UPDATE] = NFNL_SUBSYS_CTNETLINK, | |
51 | [NFNLGRP_CONNTRACK_DESTROY] = NFNL_SUBSYS_CTNETLINK, | |
52 | [NFNLGRP_CONNTRACK_EXP_NEW] = NFNL_SUBSYS_CTNETLINK_EXP, | |
53 | [NFNLGRP_CONNTRACK_EXP_UPDATE] = NFNL_SUBSYS_CTNETLINK_EXP, | |
54 | [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP, | |
97840cb6 PNA |
55 | [NFNLGRP_NFTABLES] = NFNL_SUBSYS_NFTABLES, |
56 | [NFNLGRP_ACCT_QUOTA] = NFNL_SUBSYS_ACCT, | |
33d5a7b1 | 57 | [NFNLGRP_NFTRACE] = NFNL_SUBSYS_NFTABLES, |
03292745 PNA |
58 | }; |
59 | ||
c14b78e7 | 60 | void nfnl_lock(__u8 subsys_id) |
f9e815b3 | 61 | { |
c14b78e7 | 62 | mutex_lock(&table[subsys_id].mutex); |
f9e815b3 | 63 | } |
e6a7d3c0 | 64 | EXPORT_SYMBOL_GPL(nfnl_lock); |
f9e815b3 | 65 | |
c14b78e7 | 66 | void nfnl_unlock(__u8 subsys_id) |
a3c5029c | 67 | { |
c14b78e7 | 68 | mutex_unlock(&table[subsys_id].mutex); |
f9e815b3 | 69 | } |
e6a7d3c0 | 70 | EXPORT_SYMBOL_GPL(nfnl_unlock); |
f9e815b3 | 71 | |
0eb5db7a | 72 | #ifdef CONFIG_PROVE_LOCKING |
875e0829 | 73 | bool lockdep_nfnl_is_held(u8 subsys_id) |
0eb5db7a PM |
74 | { |
75 | return lockdep_is_held(&table[subsys_id].mutex); | |
76 | } | |
77 | EXPORT_SYMBOL_GPL(lockdep_nfnl_is_held); | |
78 | #endif | |
79 | ||
7c8d4cb4 | 80 | int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n) |
f9e815b3 | 81 | { |
7b7744e2 KC |
82 | u8 cb_id; |
83 | ||
84 | /* Sanity-check attr_count size to avoid stack buffer overflow. */ | |
85 | for (cb_id = 0; cb_id < n->cb_count; cb_id++) | |
86 | if (WARN_ON(n->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT)) | |
87 | return -EINVAL; | |
88 | ||
c14b78e7 PNA |
89 | nfnl_lock(n->subsys_id); |
90 | if (table[n->subsys_id].subsys) { | |
91 | nfnl_unlock(n->subsys_id); | |
0ab43f84 HW |
92 | return -EBUSY; |
93 | } | |
c14b78e7 PNA |
94 | rcu_assign_pointer(table[n->subsys_id].subsys, n); |
95 | nfnl_unlock(n->subsys_id); | |
f9e815b3 HW |
96 | |
97 | return 0; | |
98 | } | |
f4bc177f | 99 | EXPORT_SYMBOL_GPL(nfnetlink_subsys_register); |
f9e815b3 | 100 | |
7c8d4cb4 | 101 | int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n) |
f9e815b3 | 102 | { |
c14b78e7 PNA |
103 | nfnl_lock(n->subsys_id); |
104 | table[n->subsys_id].subsys = NULL; | |
105 | nfnl_unlock(n->subsys_id); | |
6b75e3e8 | 106 | synchronize_rcu(); |
f9e815b3 HW |
107 | return 0; |
108 | } | |
f4bc177f | 109 | EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister); |
f9e815b3 | 110 | |
b745d035 | 111 | static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u16 type) |
f9e815b3 | 112 | { |
b745d035 | 113 | u8 subsys_id = NFNL_SUBSYS_ID(type); |
f9e815b3 | 114 | |
ac0f1d98 | 115 | if (subsys_id >= NFNL_SUBSYS_COUNT) |
f9e815b3 HW |
116 | return NULL; |
117 | ||
c14b78e7 | 118 | return rcu_dereference(table[subsys_id].subsys); |
f9e815b3 HW |
119 | } |
120 | ||
7c8d4cb4 | 121 | static inline const struct nfnl_callback * |
b745d035 | 122 | nfnetlink_find_client(u16 type, const struct nfnetlink_subsystem *ss) |
f9e815b3 | 123 | { |
b745d035 | 124 | u8 cb_id = NFNL_MSG_TYPE(type); |
601e68e1 | 125 | |
67ca3966 | 126 | if (cb_id >= ss->cb_count) |
f9e815b3 | 127 | return NULL; |
f9e815b3 HW |
128 | |
129 | return &ss->cb[cb_id]; | |
130 | } | |
131 | ||
cd8c20b6 | 132 | int nfnetlink_has_listeners(struct net *net, unsigned int group) |
a2427692 | 133 | { |
cd8c20b6 | 134 | return netlink_has_listeners(net->nfnl, group); |
a2427692 PM |
135 | } |
136 | EXPORT_SYMBOL_GPL(nfnetlink_has_listeners); | |
137 | ||
ec464e5d | 138 | int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid, |
95c96174 | 139 | unsigned int group, int echo, gfp_t flags) |
f9e815b3 | 140 | { |
ec464e5d | 141 | return nlmsg_notify(net->nfnl, skb, portid, group, echo, flags); |
f9e815b3 | 142 | } |
f4bc177f | 143 | EXPORT_SYMBOL_GPL(nfnetlink_send); |
f9e815b3 | 144 | |
ec464e5d | 145 | int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error) |
dd5b6ce6 | 146 | { |
ec464e5d | 147 | return netlink_set_err(net->nfnl, portid, group, error); |
dd5b6ce6 PNA |
148 | } |
149 | EXPORT_SYMBOL_GPL(nfnetlink_set_err); | |
150 | ||
ec464e5d PM |
151 | int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid, |
152 | int flags) | |
f9e815b3 | 153 | { |
ec464e5d | 154 | return netlink_unicast(net->nfnl, skb, portid, flags); |
f9e815b3 | 155 | } |
f4bc177f | 156 | EXPORT_SYMBOL_GPL(nfnetlink_unicast); |
f9e815b3 HW |
157 | |
158 | /* Process one complete nfnetlink message. */ | |
2d4bc933 JB |
159 | static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
160 | struct netlink_ext_ack *extack) | |
f9e815b3 | 161 | { |
cd8c20b6 | 162 | struct net *net = sock_net(skb->sk); |
7c8d4cb4 PM |
163 | const struct nfnl_callback *nc; |
164 | const struct nfnetlink_subsystem *ss; | |
1d00a4eb | 165 | int type, err; |
f9e815b3 | 166 | |
f9e815b3 | 167 | /* All the messages must at least contain nfgenmsg */ |
573ce260 | 168 | if (nlmsg_len(nlh) < sizeof(struct nfgenmsg)) |
f9e815b3 | 169 | return 0; |
f9e815b3 HW |
170 | |
171 | type = nlh->nlmsg_type; | |
e6a7d3c0 | 172 | replay: |
6b75e3e8 | 173 | rcu_read_lock(); |
f9e815b3 | 174 | ss = nfnetlink_get_subsys(type); |
0ab43f84 | 175 | if (!ss) { |
95a5afca | 176 | #ifdef CONFIG_MODULES |
6b75e3e8 | 177 | rcu_read_unlock(); |
37d2e7a2 | 178 | request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type)); |
6b75e3e8 | 179 | rcu_read_lock(); |
37d2e7a2 | 180 | ss = nfnetlink_get_subsys(type); |
0ab43f84 HW |
181 | if (!ss) |
182 | #endif | |
6b75e3e8 ED |
183 | { |
184 | rcu_read_unlock(); | |
1d00a4eb | 185 | return -EINVAL; |
6b75e3e8 | 186 | } |
0ab43f84 | 187 | } |
f9e815b3 HW |
188 | |
189 | nc = nfnetlink_find_client(type, ss); | |
6b75e3e8 ED |
190 | if (!nc) { |
191 | rcu_read_unlock(); | |
1d00a4eb | 192 | return -EINVAL; |
6b75e3e8 | 193 | } |
f9e815b3 | 194 | |
f9e815b3 | 195 | { |
573ce260 | 196 | int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); |
b745d035 | 197 | u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); |
7b7744e2 | 198 | struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1]; |
f49c857f PNA |
199 | struct nlattr *attr = (void *)nlh + min_len; |
200 | int attrlen = nlh->nlmsg_len - min_len; | |
c14b78e7 | 201 | __u8 subsys_id = NFNL_SUBSYS_ID(type); |
f49c857f | 202 | |
7b7744e2 KC |
203 | /* Sanity-check NFNL_MAX_ATTR_COUNT */ |
204 | if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) { | |
205 | rcu_read_unlock(); | |
206 | return -ENOMEM; | |
207 | } | |
208 | ||
fceb6435 | 209 | err = nla_parse(cda, ss->cb[cb_id].attr_count, attr, attrlen, |
fe52145f | 210 | ss->cb[cb_id].policy, extack); |
4009e188 TB |
211 | if (err < 0) { |
212 | rcu_read_unlock(); | |
f49c857f | 213 | return err; |
4009e188 | 214 | } |
601e68e1 | 215 | |
6b75e3e8 | 216 | if (nc->call_rcu) { |
7b8002a1 | 217 | err = nc->call_rcu(net, net->nfnl, skb, nlh, |
04ba724b PNA |
218 | (const struct nlattr **)cda, |
219 | extack); | |
6b75e3e8 ED |
220 | rcu_read_unlock(); |
221 | } else { | |
222 | rcu_read_unlock(); | |
c14b78e7 | 223 | nfnl_lock(subsys_id); |
9c55d3b5 | 224 | if (nfnl_dereference_protected(subsys_id) != ss || |
6b75e3e8 ED |
225 | nfnetlink_find_client(type, ss) != nc) |
226 | err = -EAGAIN; | |
59560a38 | 227 | else if (nc->call) |
7b8002a1 | 228 | err = nc->call(net, net->nfnl, skb, nlh, |
04ba724b PNA |
229 | (const struct nlattr **)cda, |
230 | extack); | |
59560a38 TB |
231 | else |
232 | err = -EINVAL; | |
c14b78e7 | 233 | nfnl_unlock(subsys_id); |
6b75e3e8 | 234 | } |
e6a7d3c0 PNA |
235 | if (err == -EAGAIN) |
236 | goto replay; | |
237 | return err; | |
f9e815b3 | 238 | } |
f9e815b3 HW |
239 | } |
240 | ||
cbb8125e PNA |
241 | struct nfnl_err { |
242 | struct list_head head; | |
243 | struct nlmsghdr *nlh; | |
244 | int err; | |
04ba724b | 245 | struct netlink_ext_ack extack; |
cbb8125e PNA |
246 | }; |
247 | ||
04ba724b PNA |
248 | static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err, |
249 | const struct netlink_ext_ack *extack) | |
cbb8125e PNA |
250 | { |
251 | struct nfnl_err *nfnl_err; | |
252 | ||
253 | nfnl_err = kmalloc(sizeof(struct nfnl_err), GFP_KERNEL); | |
254 | if (nfnl_err == NULL) | |
255 | return -ENOMEM; | |
256 | ||
257 | nfnl_err->nlh = nlh; | |
258 | nfnl_err->err = err; | |
04ba724b | 259 | nfnl_err->extack = *extack; |
cbb8125e PNA |
260 | list_add_tail(&nfnl_err->head, list); |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | static void nfnl_err_del(struct nfnl_err *nfnl_err) | |
266 | { | |
267 | list_del(&nfnl_err->head); | |
268 | kfree(nfnl_err); | |
269 | } | |
270 | ||
271 | static void nfnl_err_reset(struct list_head *err_list) | |
272 | { | |
273 | struct nfnl_err *nfnl_err, *next; | |
274 | ||
275 | list_for_each_entry_safe(nfnl_err, next, err_list, head) | |
276 | nfnl_err_del(nfnl_err); | |
277 | } | |
278 | ||
279 | static void nfnl_err_deliver(struct list_head *err_list, struct sk_buff *skb) | |
280 | { | |
281 | struct nfnl_err *nfnl_err, *next; | |
282 | ||
283 | list_for_each_entry_safe(nfnl_err, next, err_list, head) { | |
04ba724b PNA |
284 | netlink_ack(skb, nfnl_err->nlh, nfnl_err->err, |
285 | &nfnl_err->extack); | |
cbb8125e PNA |
286 | nfnl_err_del(nfnl_err); |
287 | } | |
288 | } | |
289 | ||
6742b9e3 PNA |
290 | enum { |
291 | NFNL_BATCH_FAILURE = (1 << 0), | |
292 | NFNL_BATCH_DONE = (1 << 1), | |
293 | NFNL_BATCH_REPLAY = (1 << 2), | |
294 | }; | |
295 | ||
0628b123 | 296 | static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, |
8c4d4e8b | 297 | u16 subsys_id, u32 genid) |
0628b123 | 298 | { |
0f816232 | 299 | struct sk_buff *oskb = skb; |
0628b123 PNA |
300 | struct net *net = sock_net(skb->sk); |
301 | const struct nfnetlink_subsystem *ss; | |
302 | const struct nfnl_callback *nc; | |
04ba724b | 303 | struct netlink_ext_ack extack; |
4eba8b78 | 304 | LIST_HEAD(err_list); |
6742b9e3 | 305 | u32 status; |
0628b123 PNA |
306 | int err; |
307 | ||
308 | if (subsys_id >= NFNL_SUBSYS_COUNT) | |
2d4bc933 | 309 | return netlink_ack(skb, nlh, -EINVAL, NULL); |
0628b123 | 310 | replay: |
6742b9e3 PNA |
311 | status = 0; |
312 | ||
0f816232 DJ |
313 | skb = netlink_skb_clone(oskb, GFP_KERNEL); |
314 | if (!skb) | |
2d4bc933 | 315 | return netlink_ack(oskb, nlh, -ENOMEM, NULL); |
0628b123 | 316 | |
0628b123 | 317 | nfnl_lock(subsys_id); |
9c55d3b5 | 318 | ss = nfnl_dereference_protected(subsys_id); |
0628b123 PNA |
319 | if (!ss) { |
320 | #ifdef CONFIG_MODULES | |
321 | nfnl_unlock(subsys_id); | |
322 | request_module("nfnetlink-subsys-%d", subsys_id); | |
323 | nfnl_lock(subsys_id); | |
9c55d3b5 | 324 | ss = nfnl_dereference_protected(subsys_id); |
0628b123 PNA |
325 | if (!ss) |
326 | #endif | |
327 | { | |
328 | nfnl_unlock(subsys_id); | |
2d4bc933 | 329 | netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL); |
0f816232 | 330 | return kfree_skb(skb); |
0628b123 PNA |
331 | } |
332 | } | |
333 | ||
ca2f18be | 334 | if (!ss->valid_genid || !ss->commit || !ss->abort) { |
0628b123 | 335 | nfnl_unlock(subsys_id); |
2d4bc933 | 336 | netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL); |
ecd15dd7 | 337 | return kfree_skb(skb); |
0628b123 PNA |
338 | } |
339 | ||
be2ab5b4 FW |
340 | if (!try_module_get(ss->owner)) { |
341 | nfnl_unlock(subsys_id); | |
342 | netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL); | |
343 | return kfree_skb(skb); | |
344 | } | |
345 | ||
ca2f18be | 346 | if (!ss->valid_genid(net, genid)) { |
be2ab5b4 | 347 | module_put(ss->owner); |
8c4d4e8b | 348 | nfnl_unlock(subsys_id); |
2d4bc933 | 349 | netlink_ack(oskb, nlh, -ERESTART, NULL); |
8c4d4e8b PNA |
350 | return kfree_skb(skb); |
351 | } | |
352 | ||
f102d66b FW |
353 | nfnl_unlock(subsys_id); |
354 | ||
0628b123 PNA |
355 | while (skb->len >= nlmsg_total_size(0)) { |
356 | int msglen, type; | |
357 | ||
8a3d4c36 FW |
358 | if (fatal_signal_pending(current)) { |
359 | nfnl_err_reset(&err_list); | |
360 | err = -EINTR; | |
361 | status = NFNL_BATCH_FAILURE; | |
362 | goto done; | |
363 | } | |
364 | ||
04ba724b | 365 | memset(&extack, 0, sizeof(extack)); |
0628b123 PNA |
366 | nlh = nlmsg_hdr(skb); |
367 | err = 0; | |
368 | ||
c58d6c93 PT |
369 | if (nlh->nlmsg_len < NLMSG_HDRLEN || |
370 | skb->len < nlh->nlmsg_len || | |
371 | nlmsg_len(nlh) < sizeof(struct nfgenmsg)) { | |
372 | nfnl_err_reset(&err_list); | |
373 | status |= NFNL_BATCH_FAILURE; | |
374 | goto done; | |
0628b123 PNA |
375 | } |
376 | ||
377 | /* Only requests are handled by the kernel */ | |
378 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) { | |
379 | err = -EINVAL; | |
380 | goto ack; | |
381 | } | |
382 | ||
383 | type = nlh->nlmsg_type; | |
384 | if (type == NFNL_MSG_BATCH_BEGIN) { | |
385 | /* Malformed: Batch begin twice */ | |
cbb8125e | 386 | nfnl_err_reset(&err_list); |
6742b9e3 | 387 | status |= NFNL_BATCH_FAILURE; |
0628b123 PNA |
388 | goto done; |
389 | } else if (type == NFNL_MSG_BATCH_END) { | |
6742b9e3 | 390 | status |= NFNL_BATCH_DONE; |
0628b123 PNA |
391 | goto done; |
392 | } else if (type < NLMSG_MIN_TYPE) { | |
393 | err = -EINVAL; | |
394 | goto ack; | |
395 | } | |
396 | ||
397 | /* We only accept a batch with messages for the same | |
398 | * subsystem. | |
399 | */ | |
400 | if (NFNL_SUBSYS_ID(type) != subsys_id) { | |
401 | err = -EINVAL; | |
402 | goto ack; | |
403 | } | |
404 | ||
405 | nc = nfnetlink_find_client(type, ss); | |
406 | if (!nc) { | |
407 | err = -EINVAL; | |
408 | goto ack; | |
409 | } | |
410 | ||
411 | { | |
412 | int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); | |
b745d035 | 413 | u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); |
7b7744e2 | 414 | struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1]; |
0628b123 PNA |
415 | struct nlattr *attr = (void *)nlh + min_len; |
416 | int attrlen = nlh->nlmsg_len - min_len; | |
417 | ||
7b7744e2 KC |
418 | /* Sanity-check NFTA_MAX_ATTR */ |
419 | if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) { | |
420 | err = -ENOMEM; | |
421 | goto ack; | |
422 | } | |
423 | ||
fceb6435 JB |
424 | err = nla_parse(cda, ss->cb[cb_id].attr_count, attr, |
425 | attrlen, ss->cb[cb_id].policy, NULL); | |
0628b123 PNA |
426 | if (err < 0) |
427 | goto ack; | |
428 | ||
429 | if (nc->call_batch) { | |
633c9a84 | 430 | err = nc->call_batch(net, net->nfnl, skb, nlh, |
04ba724b PNA |
431 | (const struct nlattr **)cda, |
432 | &extack); | |
0628b123 PNA |
433 | } |
434 | ||
435 | /* The lock was released to autoload some module, we | |
436 | * have to abort and start from scratch using the | |
437 | * original skb. | |
438 | */ | |
439 | if (err == -EAGAIN) { | |
6742b9e3 | 440 | status |= NFNL_BATCH_REPLAY; |
71ad00c5 | 441 | goto done; |
0628b123 PNA |
442 | } |
443 | } | |
444 | ack: | |
445 | if (nlh->nlmsg_flags & NLM_F_ACK || err) { | |
cbb8125e PNA |
446 | /* Errors are delivered once the full batch has been |
447 | * processed, this avoids that the same error is | |
448 | * reported several times when replaying the batch. | |
449 | */ | |
04ba724b | 450 | if (nfnl_err_add(&err_list, nlh, err, &extack) < 0) { |
cbb8125e PNA |
451 | /* We failed to enqueue an error, reset the |
452 | * list of errors and send OOM to userspace | |
453 | * pointing to the batch header. | |
454 | */ | |
455 | nfnl_err_reset(&err_list); | |
2d4bc933 JB |
456 | netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM, |
457 | NULL); | |
6742b9e3 | 458 | status |= NFNL_BATCH_FAILURE; |
cbb8125e PNA |
459 | goto done; |
460 | } | |
0628b123 PNA |
461 | /* We don't stop processing the batch on errors, thus, |
462 | * userspace gets all the errors that the batch | |
463 | * triggers. | |
464 | */ | |
0628b123 | 465 | if (err) |
6742b9e3 | 466 | status |= NFNL_BATCH_FAILURE; |
0628b123 | 467 | } |
71ad00c5 | 468 | |
0628b123 PNA |
469 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); |
470 | if (msglen > skb->len) | |
471 | msglen = skb->len; | |
472 | skb_pull(skb, msglen); | |
473 | } | |
474 | done: | |
6742b9e3 | 475 | if (status & NFNL_BATCH_REPLAY) { |
f102d66b | 476 | ss->abort(net, oskb); |
6742b9e3 | 477 | nfnl_err_reset(&err_list); |
6742b9e3 | 478 | kfree_skb(skb); |
be2ab5b4 | 479 | module_put(ss->owner); |
6742b9e3 PNA |
480 | goto replay; |
481 | } else if (status == NFNL_BATCH_DONE) { | |
00308791 FW |
482 | err = ss->commit(net, oskb); |
483 | if (err == -EAGAIN) { | |
484 | status |= NFNL_BATCH_REPLAY; | |
485 | goto done; | |
486 | } else if (err) { | |
487 | ss->abort(net, oskb); | |
488 | netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL); | |
489 | } | |
6742b9e3 | 490 | } else { |
5913beaf | 491 | ss->abort(net, oskb); |
6742b9e3 | 492 | } |
a654de8f PNA |
493 | if (ss->cleanup) |
494 | ss->cleanup(net); | |
0628b123 | 495 | |
cbb8125e | 496 | nfnl_err_deliver(&err_list, oskb); |
0f816232 | 497 | kfree_skb(skb); |
be2ab5b4 | 498 | module_put(ss->owner); |
0628b123 PNA |
499 | } |
500 | ||
8c4d4e8b PNA |
501 | static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = { |
502 | [NFNL_BATCH_GENID] = { .type = NLA_U32 }, | |
503 | }; | |
504 | ||
48656835 | 505 | static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh) |
f9e815b3 | 506 | { |
8c4d4e8b PNA |
507 | int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); |
508 | struct nlattr *attr = (void *)nlh + min_len; | |
509 | struct nlattr *cda[NFNL_BATCH_MAX + 1]; | |
510 | int attrlen = nlh->nlmsg_len - min_len; | |
48656835 | 511 | struct nfgenmsg *nfgenmsg; |
8c4d4e8b PNA |
512 | int msglen, err; |
513 | u32 gen_id = 0; | |
b745d035 | 514 | u16 res_id; |
0628b123 | 515 | |
48656835 PNA |
516 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); |
517 | if (msglen > skb->len) | |
518 | msglen = skb->len; | |
519 | ||
f55ce7b0 | 520 | if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg)) |
48656835 PNA |
521 | return; |
522 | ||
fceb6435 JB |
523 | err = nla_parse(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy, |
524 | NULL); | |
8c4d4e8b | 525 | if (err < 0) { |
2d4bc933 | 526 | netlink_ack(skb, nlh, err, NULL); |
8c4d4e8b PNA |
527 | return; |
528 | } | |
529 | if (cda[NFNL_BATCH_GENID]) | |
530 | gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID])); | |
531 | ||
48656835 PNA |
532 | nfgenmsg = nlmsg_data(nlh); |
533 | skb_pull(skb, msglen); | |
534 | /* Work around old nft using host byte order */ | |
535 | if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES) | |
536 | res_id = NFNL_SUBSYS_NFTABLES; | |
537 | else | |
538 | res_id = ntohs(nfgenmsg->res_id); | |
539 | ||
8c4d4e8b | 540 | nfnetlink_rcv_batch(skb, nlh, res_id, gen_id); |
48656835 PNA |
541 | } |
542 | ||
543 | static void nfnetlink_rcv(struct sk_buff *skb) | |
544 | { | |
545 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
546 | ||
f55ce7b0 MJ |
547 | if (skb->len < NLMSG_HDRLEN || |
548 | nlh->nlmsg_len < NLMSG_HDRLEN || | |
0628b123 PNA |
549 | skb->len < nlh->nlmsg_len) |
550 | return; | |
551 | ||
90f62cf3 | 552 | if (!netlink_net_capable(skb, CAP_NET_ADMIN)) { |
2d4bc933 | 553 | netlink_ack(skb, nlh, -EPERM, NULL); |
cdbe7c2d JB |
554 | return; |
555 | } | |
556 | ||
48656835 PNA |
557 | if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN) |
558 | nfnetlink_rcv_skb_batch(skb, nlh); | |
559 | else | |
d4ef3835 | 560 | netlink_rcv_skb(skb, nfnetlink_rcv_msg); |
f9e815b3 HW |
561 | } |
562 | ||
03292745 | 563 | #ifdef CONFIG_MODULES |
023e2cfa | 564 | static int nfnetlink_bind(struct net *net, int group) |
03292745 PNA |
565 | { |
566 | const struct nfnetlink_subsystem *ss; | |
97840cb6 PNA |
567 | int type; |
568 | ||
569 | if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX) | |
62924af2 | 570 | return 0; |
97840cb6 PNA |
571 | |
572 | type = nfnl_group2type[group]; | |
03292745 PNA |
573 | |
574 | rcu_read_lock(); | |
dbc3617f | 575 | ss = nfnetlink_get_subsys(type << 8); |
03292745 | 576 | rcu_read_unlock(); |
bfe4bc71 RGB |
577 | if (!ss) |
578 | request_module("nfnetlink-subsys-%d", type); | |
4f520900 | 579 | return 0; |
03292745 PNA |
580 | } |
581 | #endif | |
582 | ||
cd8c20b6 | 583 | static int __net_init nfnetlink_net_init(struct net *net) |
f9e815b3 | 584 | { |
cd8c20b6 | 585 | struct sock *nfnl; |
a31f2d17 PNA |
586 | struct netlink_kernel_cfg cfg = { |
587 | .groups = NFNLGRP_MAX, | |
588 | .input = nfnetlink_rcv, | |
03292745 PNA |
589 | #ifdef CONFIG_MODULES |
590 | .bind = nfnetlink_bind, | |
591 | #endif | |
a31f2d17 | 592 | }; |
cd8c20b6 | 593 | |
9f00d977 | 594 | nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg); |
cd8c20b6 AD |
595 | if (!nfnl) |
596 | return -ENOMEM; | |
597 | net->nfnl_stash = nfnl; | |
cf778b00 | 598 | rcu_assign_pointer(net->nfnl, nfnl); |
cd8c20b6 | 599 | return 0; |
f9e815b3 HW |
600 | } |
601 | ||
cd8c20b6 | 602 | static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list) |
f9e815b3 | 603 | { |
cd8c20b6 | 604 | struct net *net; |
f9e815b3 | 605 | |
cd8c20b6 | 606 | list_for_each_entry(net, net_exit_list, exit_list) |
a9b3cd7f | 607 | RCU_INIT_POINTER(net->nfnl, NULL); |
cd8c20b6 AD |
608 | synchronize_net(); |
609 | list_for_each_entry(net, net_exit_list, exit_list) | |
610 | netlink_kernel_release(net->nfnl_stash); | |
611 | } | |
f9e815b3 | 612 | |
cd8c20b6 AD |
613 | static struct pernet_operations nfnetlink_net_ops = { |
614 | .init = nfnetlink_net_init, | |
615 | .exit_batch = nfnetlink_net_exit_batch, | |
616 | }; | |
617 | ||
618 | static int __init nfnetlink_init(void) | |
619 | { | |
c14b78e7 PNA |
620 | int i; |
621 | ||
97840cb6 PNA |
622 | for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++) |
623 | BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE); | |
624 | ||
c14b78e7 PNA |
625 | for (i=0; i<NFNL_SUBSYS_COUNT; i++) |
626 | mutex_init(&table[i].mutex); | |
627 | ||
cd8c20b6 | 628 | return register_pernet_subsys(&nfnetlink_net_ops); |
f9e815b3 HW |
629 | } |
630 | ||
cd8c20b6 AD |
631 | static void __exit nfnetlink_exit(void) |
632 | { | |
cd8c20b6 AD |
633 | unregister_pernet_subsys(&nfnetlink_net_ops); |
634 | } | |
f9e815b3 HW |
635 | module_init(nfnetlink_init); |
636 | module_exit(nfnetlink_exit); |