]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - net/netfilter/nfnetlink.c
netfilter: nfnetlink: add a missing rcu_read_unlock()
[mirror_ubuntu-kernels.git] / net / netfilter / nfnetlink.c
CommitLineData
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>
1be05ea7 31#include <net/netns/generic.h>
f9e815b3
HW
32#include <linux/netfilter/nfnetlink.h>
33
34MODULE_LICENSE("GPL");
4fdb3bb7
HW
35MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
36MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
4cacc395 37MODULE_DESCRIPTION("Netfilter messages via netlink socket");
f9e815b3 38
9c55d3b5
FW
39#define nfnl_dereference_protected(id) \
40 rcu_dereference_protected(table[(id)].subsys, \
41 lockdep_nfnl_is_held((id)))
42
7b7744e2
KC
43#define NFNL_MAX_ATTR_COUNT 32
44
1be05ea7
FW
45static unsigned int nfnetlink_pernet_id __read_mostly;
46
47struct nfnl_net {
48 struct sock *nfnl;
49};
50
c14b78e7
PNA
51static struct {
52 struct mutex mutex;
53 const struct nfnetlink_subsystem __rcu *subsys;
54} table[NFNL_SUBSYS_COUNT];
f9e815b3 55
ab6c41ee
FW
56static struct lock_class_key nfnl_lockdep_keys[NFNL_SUBSYS_COUNT];
57
58static const char *const nfnl_lockdep_names[NFNL_SUBSYS_COUNT] = {
59 [NFNL_SUBSYS_NONE] = "nfnl_subsys_none",
60 [NFNL_SUBSYS_CTNETLINK] = "nfnl_subsys_ctnetlink",
61 [NFNL_SUBSYS_CTNETLINK_EXP] = "nfnl_subsys_ctnetlink_exp",
62 [NFNL_SUBSYS_QUEUE] = "nfnl_subsys_queue",
63 [NFNL_SUBSYS_ULOG] = "nfnl_subsys_ulog",
64 [NFNL_SUBSYS_OSF] = "nfnl_subsys_osf",
65 [NFNL_SUBSYS_IPSET] = "nfnl_subsys_ipset",
66 [NFNL_SUBSYS_ACCT] = "nfnl_subsys_acct",
67 [NFNL_SUBSYS_CTNETLINK_TIMEOUT] = "nfnl_subsys_cttimeout",
68 [NFNL_SUBSYS_CTHELPER] = "nfnl_subsys_cthelper",
69 [NFNL_SUBSYS_NFTABLES] = "nfnl_subsys_nftables",
70 [NFNL_SUBSYS_NFT_COMPAT] = "nfnl_subsys_nftcompat",
71};
72
03292745
PNA
73static const int nfnl_group2type[NFNLGRP_MAX+1] = {
74 [NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK,
75 [NFNLGRP_CONNTRACK_UPDATE] = NFNL_SUBSYS_CTNETLINK,
76 [NFNLGRP_CONNTRACK_DESTROY] = NFNL_SUBSYS_CTNETLINK,
77 [NFNLGRP_CONNTRACK_EXP_NEW] = NFNL_SUBSYS_CTNETLINK_EXP,
78 [NFNLGRP_CONNTRACK_EXP_UPDATE] = NFNL_SUBSYS_CTNETLINK_EXP,
79 [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP,
97840cb6
PNA
80 [NFNLGRP_NFTABLES] = NFNL_SUBSYS_NFTABLES,
81 [NFNLGRP_ACCT_QUOTA] = NFNL_SUBSYS_ACCT,
33d5a7b1 82 [NFNLGRP_NFTRACE] = NFNL_SUBSYS_NFTABLES,
03292745
PNA
83};
84
1be05ea7
FW
85static struct nfnl_net *nfnl_pernet(struct net *net)
86{
87 return net_generic(net, nfnetlink_pernet_id);
88}
89
c14b78e7 90void nfnl_lock(__u8 subsys_id)
f9e815b3 91{
c14b78e7 92 mutex_lock(&table[subsys_id].mutex);
f9e815b3 93}
e6a7d3c0 94EXPORT_SYMBOL_GPL(nfnl_lock);
f9e815b3 95
c14b78e7 96void nfnl_unlock(__u8 subsys_id)
a3c5029c 97{
c14b78e7 98 mutex_unlock(&table[subsys_id].mutex);
f9e815b3 99}
e6a7d3c0 100EXPORT_SYMBOL_GPL(nfnl_unlock);
f9e815b3 101
0eb5db7a 102#ifdef CONFIG_PROVE_LOCKING
875e0829 103bool lockdep_nfnl_is_held(u8 subsys_id)
0eb5db7a
PM
104{
105 return lockdep_is_held(&table[subsys_id].mutex);
106}
107EXPORT_SYMBOL_GPL(lockdep_nfnl_is_held);
108#endif
109
7c8d4cb4 110int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
f9e815b3 111{
7b7744e2
KC
112 u8 cb_id;
113
114 /* Sanity-check attr_count size to avoid stack buffer overflow. */
115 for (cb_id = 0; cb_id < n->cb_count; cb_id++)
116 if (WARN_ON(n->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT))
117 return -EINVAL;
118
c14b78e7
PNA
119 nfnl_lock(n->subsys_id);
120 if (table[n->subsys_id].subsys) {
121 nfnl_unlock(n->subsys_id);
0ab43f84
HW
122 return -EBUSY;
123 }
c14b78e7
PNA
124 rcu_assign_pointer(table[n->subsys_id].subsys, n);
125 nfnl_unlock(n->subsys_id);
f9e815b3
HW
126
127 return 0;
128}
f4bc177f 129EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
f9e815b3 130
7c8d4cb4 131int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
f9e815b3 132{
c14b78e7
PNA
133 nfnl_lock(n->subsys_id);
134 table[n->subsys_id].subsys = NULL;
135 nfnl_unlock(n->subsys_id);
6b75e3e8 136 synchronize_rcu();
f9e815b3
HW
137 return 0;
138}
f4bc177f 139EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
f9e815b3 140
b745d035 141static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u16 type)
f9e815b3 142{
b745d035 143 u8 subsys_id = NFNL_SUBSYS_ID(type);
f9e815b3 144
ac0f1d98 145 if (subsys_id >= NFNL_SUBSYS_COUNT)
f9e815b3
HW
146 return NULL;
147
c14b78e7 148 return rcu_dereference(table[subsys_id].subsys);
f9e815b3
HW
149}
150
7c8d4cb4 151static inline const struct nfnl_callback *
b745d035 152nfnetlink_find_client(u16 type, const struct nfnetlink_subsystem *ss)
f9e815b3 153{
b745d035 154 u8 cb_id = NFNL_MSG_TYPE(type);
601e68e1 155
67ca3966 156 if (cb_id >= ss->cb_count)
f9e815b3 157 return NULL;
f9e815b3
HW
158
159 return &ss->cb[cb_id];
160}
161
cd8c20b6 162int nfnetlink_has_listeners(struct net *net, unsigned int group)
a2427692 163{
1be05ea7
FW
164 struct nfnl_net *nfnlnet = nfnl_pernet(net);
165
166 return netlink_has_listeners(nfnlnet->nfnl, group);
a2427692
PM
167}
168EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
169
ec464e5d 170int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
95c96174 171 unsigned int group, int echo, gfp_t flags)
f9e815b3 172{
1be05ea7
FW
173 struct nfnl_net *nfnlnet = nfnl_pernet(net);
174
175 return nlmsg_notify(nfnlnet->nfnl, skb, portid, group, echo, flags);
f9e815b3 176}
f4bc177f 177EXPORT_SYMBOL_GPL(nfnetlink_send);
f9e815b3 178
ec464e5d 179int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error)
dd5b6ce6 180{
1be05ea7
FW
181 struct nfnl_net *nfnlnet = nfnl_pernet(net);
182
183 return netlink_set_err(nfnlnet->nfnl, portid, group, error);
dd5b6ce6
PNA
184}
185EXPORT_SYMBOL_GPL(nfnetlink_set_err);
186
ee921183 187int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid)
f9e815b3 188{
1be05ea7 189 struct nfnl_net *nfnlnet = nfnl_pernet(net);
ee921183
PNA
190 int err;
191
1be05ea7 192 err = nlmsg_unicast(nfnlnet->nfnl, skb, portid);
ee921183
PNA
193 if (err == -EAGAIN)
194 err = -ENOBUFS;
195
196 return err;
f9e815b3 197}
f4bc177f 198EXPORT_SYMBOL_GPL(nfnetlink_unicast);
f9e815b3 199
237c609f
FW
200void nfnetlink_broadcast(struct net *net, struct sk_buff *skb, __u32 portid,
201 __u32 group, gfp_t allocation)
202{
1be05ea7
FW
203 struct nfnl_net *nfnlnet = nfnl_pernet(net);
204
205 netlink_broadcast(nfnlnet->nfnl, skb, portid, group, allocation);
237c609f
FW
206}
207EXPORT_SYMBOL_GPL(nfnetlink_broadcast);
208
f9e815b3 209/* Process one complete nfnetlink message. */
2d4bc933
JB
210static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
211 struct netlink_ext_ack *extack)
f9e815b3 212{
cd8c20b6 213 struct net *net = sock_net(skb->sk);
7c8d4cb4
PM
214 const struct nfnl_callback *nc;
215 const struct nfnetlink_subsystem *ss;
1d00a4eb 216 int type, err;
f9e815b3 217
f9e815b3 218 /* All the messages must at least contain nfgenmsg */
573ce260 219 if (nlmsg_len(nlh) < sizeof(struct nfgenmsg))
f9e815b3 220 return 0;
f9e815b3
HW
221
222 type = nlh->nlmsg_type;
e6a7d3c0 223replay:
6b75e3e8 224 rcu_read_lock();
1be05ea7 225
f9e815b3 226 ss = nfnetlink_get_subsys(type);
0ab43f84 227 if (!ss) {
95a5afca 228#ifdef CONFIG_MODULES
6b75e3e8 229 rcu_read_unlock();
37d2e7a2 230 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
6b75e3e8 231 rcu_read_lock();
37d2e7a2 232 ss = nfnetlink_get_subsys(type);
0ab43f84
HW
233 if (!ss)
234#endif
6b75e3e8
ED
235 {
236 rcu_read_unlock();
1d00a4eb 237 return -EINVAL;
6b75e3e8 238 }
0ab43f84 239 }
f9e815b3
HW
240
241 nc = nfnetlink_find_client(type, ss);
6b75e3e8
ED
242 if (!nc) {
243 rcu_read_unlock();
1d00a4eb 244 return -EINVAL;
6b75e3e8 245 }
f9e815b3 246
f9e815b3 247 {
573ce260 248 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1be05ea7 249 struct nfnl_net *nfnlnet = nfnl_pernet(net);
b745d035 250 u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
7b7744e2 251 struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
f49c857f
PNA
252 struct nlattr *attr = (void *)nlh + min_len;
253 int attrlen = nlh->nlmsg_len - min_len;
c14b78e7 254 __u8 subsys_id = NFNL_SUBSYS_ID(type);
a6555365
PNA
255 struct nfnl_info info = {
256 .net = net,
257 .sk = nfnlnet->nfnl,
258 .nlh = nlh,
259 .extack = extack,
260 };
f49c857f 261
7b7744e2
KC
262 /* Sanity-check NFNL_MAX_ATTR_COUNT */
263 if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
264 rcu_read_unlock();
265 return -ENOMEM;
266 }
267
8cb08174
JB
268 err = nla_parse_deprecated(cda, ss->cb[cb_id].attr_count,
269 attr, attrlen,
270 ss->cb[cb_id].policy, extack);
4009e188
TB
271 if (err < 0) {
272 rcu_read_unlock();
f49c857f 273 return err;
4009e188 274 }
601e68e1 275
50f2db9e 276 if (!nc->call) {
6b75e3e8 277 rcu_read_unlock();
50f2db9e
PNA
278 return -EINVAL;
279 }
280
281 switch (nc->type) {
282 case NFNL_CB_RCU:
283 err = nc->call(skb, &info, (const struct nlattr **)cda);
284 rcu_read_unlock();
285 break;
286 case NFNL_CB_MUTEX:
6b75e3e8 287 rcu_read_unlock();
c14b78e7 288 nfnl_lock(subsys_id);
9c55d3b5 289 if (nfnl_dereference_protected(subsys_id) != ss ||
a6555365 290 nfnetlink_find_client(type, ss) != nc) {
6b75e3e8 291 err = -EAGAIN;
50f2db9e 292 break;
a6555365 293 }
50f2db9e 294 err = nc->call(skb, &info, (const struct nlattr **)cda);
c14b78e7 295 nfnl_unlock(subsys_id);
50f2db9e
PNA
296 break;
297 default:
7072a355 298 rcu_read_unlock();
50f2db9e
PNA
299 err = -EINVAL;
300 break;
6b75e3e8 301 }
e6a7d3c0
PNA
302 if (err == -EAGAIN)
303 goto replay;
304 return err;
f9e815b3 305 }
f9e815b3
HW
306}
307
cbb8125e
PNA
308struct nfnl_err {
309 struct list_head head;
310 struct nlmsghdr *nlh;
311 int err;
04ba724b 312 struct netlink_ext_ack extack;
cbb8125e
PNA
313};
314
04ba724b
PNA
315static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err,
316 const struct netlink_ext_ack *extack)
cbb8125e
PNA
317{
318 struct nfnl_err *nfnl_err;
319
320 nfnl_err = kmalloc(sizeof(struct nfnl_err), GFP_KERNEL);
321 if (nfnl_err == NULL)
322 return -ENOMEM;
323
324 nfnl_err->nlh = nlh;
325 nfnl_err->err = err;
04ba724b 326 nfnl_err->extack = *extack;
cbb8125e
PNA
327 list_add_tail(&nfnl_err->head, list);
328
329 return 0;
330}
331
332static void nfnl_err_del(struct nfnl_err *nfnl_err)
333{
334 list_del(&nfnl_err->head);
335 kfree(nfnl_err);
336}
337
338static void nfnl_err_reset(struct list_head *err_list)
339{
340 struct nfnl_err *nfnl_err, *next;
341
342 list_for_each_entry_safe(nfnl_err, next, err_list, head)
343 nfnl_err_del(nfnl_err);
344}
345
346static void nfnl_err_deliver(struct list_head *err_list, struct sk_buff *skb)
347{
348 struct nfnl_err *nfnl_err, *next;
349
350 list_for_each_entry_safe(nfnl_err, next, err_list, head) {
04ba724b
PNA
351 netlink_ack(skb, nfnl_err->nlh, nfnl_err->err,
352 &nfnl_err->extack);
cbb8125e
PNA
353 nfnl_err_del(nfnl_err);
354 }
355}
356
6742b9e3
PNA
357enum {
358 NFNL_BATCH_FAILURE = (1 << 0),
359 NFNL_BATCH_DONE = (1 << 1),
360 NFNL_BATCH_REPLAY = (1 << 2),
361};
362
0628b123 363static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
8c4d4e8b 364 u16 subsys_id, u32 genid)
0628b123 365{
0f816232 366 struct sk_buff *oskb = skb;
0628b123
PNA
367 struct net *net = sock_net(skb->sk);
368 const struct nfnetlink_subsystem *ss;
369 const struct nfnl_callback *nc;
04ba724b 370 struct netlink_ext_ack extack;
4eba8b78 371 LIST_HEAD(err_list);
6742b9e3 372 u32 status;
0628b123
PNA
373 int err;
374
375 if (subsys_id >= NFNL_SUBSYS_COUNT)
2d4bc933 376 return netlink_ack(skb, nlh, -EINVAL, NULL);
0628b123 377replay:
6742b9e3 378 status = 0;
c0391b6a 379replay_abort:
0f816232
DJ
380 skb = netlink_skb_clone(oskb, GFP_KERNEL);
381 if (!skb)
2d4bc933 382 return netlink_ack(oskb, nlh, -ENOMEM, NULL);
0628b123 383
0628b123 384 nfnl_lock(subsys_id);
9c55d3b5 385 ss = nfnl_dereference_protected(subsys_id);
0628b123
PNA
386 if (!ss) {
387#ifdef CONFIG_MODULES
388 nfnl_unlock(subsys_id);
389 request_module("nfnetlink-subsys-%d", subsys_id);
390 nfnl_lock(subsys_id);
9c55d3b5 391 ss = nfnl_dereference_protected(subsys_id);
0628b123
PNA
392 if (!ss)
393#endif
394 {
395 nfnl_unlock(subsys_id);
2d4bc933 396 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
0f816232 397 return kfree_skb(skb);
0628b123
PNA
398 }
399 }
400
ca2f18be 401 if (!ss->valid_genid || !ss->commit || !ss->abort) {
0628b123 402 nfnl_unlock(subsys_id);
2d4bc933 403 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
ecd15dd7 404 return kfree_skb(skb);
0628b123
PNA
405 }
406
be2ab5b4
FW
407 if (!try_module_get(ss->owner)) {
408 nfnl_unlock(subsys_id);
409 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
410 return kfree_skb(skb);
411 }
412
ca2f18be 413 if (!ss->valid_genid(net, genid)) {
be2ab5b4 414 module_put(ss->owner);
8c4d4e8b 415 nfnl_unlock(subsys_id);
2d4bc933 416 netlink_ack(oskb, nlh, -ERESTART, NULL);
8c4d4e8b
PNA
417 return kfree_skb(skb);
418 }
419
f102d66b
FW
420 nfnl_unlock(subsys_id);
421
0628b123
PNA
422 while (skb->len >= nlmsg_total_size(0)) {
423 int msglen, type;
424
8a3d4c36
FW
425 if (fatal_signal_pending(current)) {
426 nfnl_err_reset(&err_list);
427 err = -EINTR;
428 status = NFNL_BATCH_FAILURE;
429 goto done;
430 }
431
04ba724b 432 memset(&extack, 0, sizeof(extack));
0628b123
PNA
433 nlh = nlmsg_hdr(skb);
434 err = 0;
435
c58d6c93
PT
436 if (nlh->nlmsg_len < NLMSG_HDRLEN ||
437 skb->len < nlh->nlmsg_len ||
438 nlmsg_len(nlh) < sizeof(struct nfgenmsg)) {
439 nfnl_err_reset(&err_list);
440 status |= NFNL_BATCH_FAILURE;
441 goto done;
0628b123
PNA
442 }
443
444 /* Only requests are handled by the kernel */
445 if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
446 err = -EINVAL;
447 goto ack;
448 }
449
450 type = nlh->nlmsg_type;
451 if (type == NFNL_MSG_BATCH_BEGIN) {
452 /* Malformed: Batch begin twice */
cbb8125e 453 nfnl_err_reset(&err_list);
6742b9e3 454 status |= NFNL_BATCH_FAILURE;
0628b123
PNA
455 goto done;
456 } else if (type == NFNL_MSG_BATCH_END) {
6742b9e3 457 status |= NFNL_BATCH_DONE;
0628b123
PNA
458 goto done;
459 } else if (type < NLMSG_MIN_TYPE) {
460 err = -EINVAL;
461 goto ack;
462 }
463
464 /* We only accept a batch with messages for the same
465 * subsystem.
466 */
467 if (NFNL_SUBSYS_ID(type) != subsys_id) {
468 err = -EINVAL;
469 goto ack;
470 }
471
472 nc = nfnetlink_find_client(type, ss);
473 if (!nc) {
474 err = -EINVAL;
475 goto ack;
476 }
477
50f2db9e
PNA
478 if (nc->type != NFNL_CB_BATCH) {
479 err = -EINVAL;
480 goto ack;
481 }
482
0628b123
PNA
483 {
484 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
7dab8ee3 485 struct nfnl_net *nfnlnet = nfnl_pernet(net);
7b7744e2 486 struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
0628b123 487 struct nlattr *attr = (void *)nlh + min_len;
50f2db9e 488 u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
0628b123 489 int attrlen = nlh->nlmsg_len - min_len;
7dab8ee3
PNA
490 struct nfnl_info info = {
491 .net = net,
492 .sk = nfnlnet->nfnl,
493 .nlh = nlh,
494 .extack = &extack,
495 };
0628b123 496
7b7744e2
KC
497 /* Sanity-check NFTA_MAX_ATTR */
498 if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
499 err = -ENOMEM;
500 goto ack;
501 }
502
8cb08174
JB
503 err = nla_parse_deprecated(cda,
504 ss->cb[cb_id].attr_count,
505 attr, attrlen,
506 ss->cb[cb_id].policy, NULL);
0628b123
PNA
507 if (err < 0)
508 goto ack;
509
50f2db9e 510 err = nc->call(skb, &info, (const struct nlattr **)cda);
0628b123
PNA
511
512 /* The lock was released to autoload some module, we
513 * have to abort and start from scratch using the
514 * original skb.
515 */
516 if (err == -EAGAIN) {
6742b9e3 517 status |= NFNL_BATCH_REPLAY;
71ad00c5 518 goto done;
0628b123
PNA
519 }
520 }
521ack:
522 if (nlh->nlmsg_flags & NLM_F_ACK || err) {
cbb8125e
PNA
523 /* Errors are delivered once the full batch has been
524 * processed, this avoids that the same error is
525 * reported several times when replaying the batch.
526 */
04ba724b 527 if (nfnl_err_add(&err_list, nlh, err, &extack) < 0) {
cbb8125e
PNA
528 /* We failed to enqueue an error, reset the
529 * list of errors and send OOM to userspace
530 * pointing to the batch header.
531 */
532 nfnl_err_reset(&err_list);
2d4bc933
JB
533 netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM,
534 NULL);
6742b9e3 535 status |= NFNL_BATCH_FAILURE;
cbb8125e
PNA
536 goto done;
537 }
0628b123
PNA
538 /* We don't stop processing the batch on errors, thus,
539 * userspace gets all the errors that the batch
540 * triggers.
541 */
0628b123 542 if (err)
6742b9e3 543 status |= NFNL_BATCH_FAILURE;
0628b123 544 }
71ad00c5 545
0628b123
PNA
546 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
547 if (msglen > skb->len)
548 msglen = skb->len;
549 skb_pull(skb, msglen);
550 }
551done:
6742b9e3 552 if (status & NFNL_BATCH_REPLAY) {
c0391b6a 553 ss->abort(net, oskb, NFNL_ABORT_AUTOLOAD);
6742b9e3 554 nfnl_err_reset(&err_list);
6742b9e3 555 kfree_skb(skb);
be2ab5b4 556 module_put(ss->owner);
6742b9e3
PNA
557 goto replay;
558 } else if (status == NFNL_BATCH_DONE) {
00308791
FW
559 err = ss->commit(net, oskb);
560 if (err == -EAGAIN) {
561 status |= NFNL_BATCH_REPLAY;
562 goto done;
563 } else if (err) {
c0391b6a 564 ss->abort(net, oskb, NFNL_ABORT_NONE);
00308791
FW
565 netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL);
566 }
6742b9e3 567 } else {
c0391b6a
PNA
568 enum nfnl_abort_action abort_action;
569
570 if (status & NFNL_BATCH_FAILURE)
571 abort_action = NFNL_ABORT_NONE;
572 else
573 abort_action = NFNL_ABORT_VALIDATE;
574
575 err = ss->abort(net, oskb, abort_action);
576 if (err == -EAGAIN) {
577 nfnl_err_reset(&err_list);
578 kfree_skb(skb);
579 module_put(ss->owner);
580 status |= NFNL_BATCH_FAILURE;
581 goto replay_abort;
582 }
6742b9e3 583 }
a654de8f
PNA
584 if (ss->cleanup)
585 ss->cleanup(net);
0628b123 586
cbb8125e 587 nfnl_err_deliver(&err_list, oskb);
0f816232 588 kfree_skb(skb);
be2ab5b4 589 module_put(ss->owner);
0628b123
PNA
590}
591
8c4d4e8b
PNA
592static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = {
593 [NFNL_BATCH_GENID] = { .type = NLA_U32 },
594};
595
48656835 596static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
f9e815b3 597{
8c4d4e8b
PNA
598 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
599 struct nlattr *attr = (void *)nlh + min_len;
600 struct nlattr *cda[NFNL_BATCH_MAX + 1];
601 int attrlen = nlh->nlmsg_len - min_len;
48656835 602 struct nfgenmsg *nfgenmsg;
8c4d4e8b
PNA
603 int msglen, err;
604 u32 gen_id = 0;
b745d035 605 u16 res_id;
0628b123 606
48656835
PNA
607 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
608 if (msglen > skb->len)
609 msglen = skb->len;
610
f55ce7b0 611 if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
48656835
PNA
612 return;
613
8cb08174
JB
614 err = nla_parse_deprecated(cda, NFNL_BATCH_MAX, attr, attrlen,
615 nfnl_batch_policy, NULL);
8c4d4e8b 616 if (err < 0) {
2d4bc933 617 netlink_ack(skb, nlh, err, NULL);
8c4d4e8b
PNA
618 return;
619 }
620 if (cda[NFNL_BATCH_GENID])
621 gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID]));
622
48656835
PNA
623 nfgenmsg = nlmsg_data(nlh);
624 skb_pull(skb, msglen);
625 /* Work around old nft using host byte order */
626 if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES)
627 res_id = NFNL_SUBSYS_NFTABLES;
628 else
629 res_id = ntohs(nfgenmsg->res_id);
630
8c4d4e8b 631 nfnetlink_rcv_batch(skb, nlh, res_id, gen_id);
48656835
PNA
632}
633
634static void nfnetlink_rcv(struct sk_buff *skb)
635{
636 struct nlmsghdr *nlh = nlmsg_hdr(skb);
637
f55ce7b0
MJ
638 if (skb->len < NLMSG_HDRLEN ||
639 nlh->nlmsg_len < NLMSG_HDRLEN ||
0628b123
PNA
640 skb->len < nlh->nlmsg_len)
641 return;
642
90f62cf3 643 if (!netlink_net_capable(skb, CAP_NET_ADMIN)) {
2d4bc933 644 netlink_ack(skb, nlh, -EPERM, NULL);
cdbe7c2d
JB
645 return;
646 }
647
48656835
PNA
648 if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN)
649 nfnetlink_rcv_skb_batch(skb, nlh);
650 else
d4ef3835 651 netlink_rcv_skb(skb, nfnetlink_rcv_msg);
f9e815b3
HW
652}
653
03292745 654#ifdef CONFIG_MODULES
023e2cfa 655static int nfnetlink_bind(struct net *net, int group)
03292745
PNA
656{
657 const struct nfnetlink_subsystem *ss;
97840cb6
PNA
658 int type;
659
660 if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX)
62924af2 661 return 0;
97840cb6
PNA
662
663 type = nfnl_group2type[group];
03292745
PNA
664
665 rcu_read_lock();
dbc3617f 666 ss = nfnetlink_get_subsys(type << 8);
03292745 667 rcu_read_unlock();
bfe4bc71 668 if (!ss)
1b0890cd 669 request_module_nowait("nfnetlink-subsys-%d", type);
4f520900 670 return 0;
03292745
PNA
671}
672#endif
673
cd8c20b6 674static int __net_init nfnetlink_net_init(struct net *net)
f9e815b3 675{
1be05ea7 676 struct nfnl_net *nfnlnet = nfnl_pernet(net);
a31f2d17
PNA
677 struct netlink_kernel_cfg cfg = {
678 .groups = NFNLGRP_MAX,
679 .input = nfnetlink_rcv,
03292745
PNA
680#ifdef CONFIG_MODULES
681 .bind = nfnetlink_bind,
682#endif
a31f2d17 683 };
cd8c20b6 684
1be05ea7
FW
685 nfnlnet->nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg);
686 if (!nfnlnet->nfnl)
cd8c20b6 687 return -ENOMEM;
cd8c20b6 688 return 0;
f9e815b3
HW
689}
690
cd8c20b6 691static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
f9e815b3 692{
1be05ea7 693 struct nfnl_net *nfnlnet;
cd8c20b6 694 struct net *net;
f9e815b3 695
1be05ea7
FW
696 list_for_each_entry(net, net_exit_list, exit_list) {
697 nfnlnet = nfnl_pernet(net);
698
699 netlink_kernel_release(nfnlnet->nfnl);
700 }
cd8c20b6 701}
f9e815b3 702
cd8c20b6
AD
703static struct pernet_operations nfnetlink_net_ops = {
704 .init = nfnetlink_net_init,
705 .exit_batch = nfnetlink_net_exit_batch,
1be05ea7
FW
706 .id = &nfnetlink_pernet_id,
707 .size = sizeof(struct nfnl_net),
cd8c20b6
AD
708};
709
710static int __init nfnetlink_init(void)
711{
c14b78e7
PNA
712 int i;
713
97840cb6
PNA
714 for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++)
715 BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE);
716
c14b78e7 717 for (i=0; i<NFNL_SUBSYS_COUNT; i++)
ab6c41ee 718 __mutex_init(&table[i].mutex, nfnl_lockdep_names[i], &nfnl_lockdep_keys[i]);
c14b78e7 719
cd8c20b6 720 return register_pernet_subsys(&nfnetlink_net_ops);
f9e815b3
HW
721}
722
cd8c20b6
AD
723static void __exit nfnetlink_exit(void)
724{
cd8c20b6
AD
725 unregister_pernet_subsys(&nfnetlink_net_ops);
726}
f9e815b3
HW
727module_init(nfnetlink_init);
728module_exit(nfnetlink_exit);