]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netfilter/nfnetlink_log.c
ipvs: Improve robustness to the ipvs sysctl
[mirror_ubuntu-jammy-kernel.git] / net / netfilter / nfnetlink_log.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
0597f268
HW
2/*
3 * This is a module which is used for logging packets to userspace via
4 * nfetlink.
5 *
6 * (C) 2005 by Harald Welte <laforge@netfilter.org>
f229f6ce 7 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
0597f268
HW
8 *
9 * Based on the old ipv4-only ipt_ULOG.c:
10 * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
0597f268 11 */
beacd3e8
ML
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
0597f268
HW
15#include <linux/module.h>
16#include <linux/skbuff.h>
e035edd1 17#include <linux/if_arp.h>
0597f268
HW
18#include <linux/init.h>
19#include <linux/ip.h>
20#include <linux/ipv6.h>
21#include <linux/netdevice.h>
22#include <linux/netfilter.h>
c737b7c4 23#include <linux/netfilter_bridge.h>
573ce260 24#include <net/netlink.h>
0597f268
HW
25#include <linux/netfilter/nfnetlink.h>
26#include <linux/netfilter/nfnetlink_log.h>
a29a9a58 27#include <linux/netfilter/nf_conntrack_common.h>
0597f268
HW
28#include <linux/spinlock.h>
29#include <linux/sysctl.h>
30#include <linux/proc_fs.h>
31#include <linux/security.h>
32#include <linux/list.h>
5a0e3ad6 33#include <linux/slab.h>
0597f268 34#include <net/sock.h>
f01ffbd6 35#include <net/netfilter/nf_log.h>
9368a53c 36#include <net/netns/generic.h>
0597f268 37
60063497 38#include <linux/atomic.h>
b54ab92b
RE
39#include <linux/refcount.h>
40
0597f268 41
1109a90c 42#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
fbcd923c
HW
43#include "../bridge/br_private.h"
44#endif
45
ce20cdf4 46#define NFULNL_COPY_DISABLED 0xff
c2db2924 47#define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
2c6764b7 48#define NFULNL_TIMEOUT_DEFAULT 100 /* every second */
0597f268 49#define NFULNL_QTHRESH_DEFAULT 100 /* 100 packets */
c1e7dc91
FW
50/* max packet size is limited by 16-bit struct nfattr nfa_len field */
51#define NFULNL_COPY_RANGE_MAX (0xFFFF - NLA_HDRLEN)
0597f268
HW
52
53#define PRINTR(x, args...) do { if (net_ratelimit()) \
54 printk(x, ## args); } while (0);
55
0597f268
HW
56struct nfulnl_instance {
57 struct hlist_node hlist; /* global list of instances */
58 spinlock_t lock;
b54ab92b 59 refcount_t use; /* use count */
0597f268
HW
60
61 unsigned int qlen; /* number of nlmsgs in skb */
62 struct sk_buff *skb; /* pre-allocatd skb */
0597f268 63 struct timer_list timer;
9368a53c 64 struct net *net;
9eea9515 65 struct user_namespace *peer_user_ns; /* User namespace of the peer process */
cc6bc448 66 u32 peer_portid; /* PORTID of the peer process */
0597f268
HW
67
68 /* configurable parameters */
69 unsigned int flushtimeout; /* timeout until queue flush */
70 unsigned int nlbufsiz; /* netlink buffer allocation size */
71 unsigned int qthreshold; /* threshold of the queue */
72 u_int32_t copy_range;
0af5f6c1 73 u_int32_t seq; /* instance-local sequential counter */
0597f268 74 u_int16_t group_num; /* number of this queue */
0af5f6c1 75 u_int16_t flags;
601e68e1 76 u_int8_t copy_mode;
bed1be20 77 struct rcu_head rcu;
0597f268
HW
78};
79
0597f268 80#define INSTANCE_BUCKETS 16
0597f268 81
c7d03a00 82static unsigned int nfnl_log_net_id __read_mostly;
9368a53c
G
83
84struct nfnl_log_net {
85 spinlock_t instances_lock;
86 struct hlist_head instance_table[INSTANCE_BUCKETS];
87 atomic_t global_seq;
88};
89
90static struct nfnl_log_net *nfnl_log_pernet(struct net *net)
91{
92 return net_generic(net, nfnl_log_net_id);
93}
94
0597f268
HW
95static inline u_int8_t instance_hashfn(u_int16_t group_num)
96{
97 return ((group_num & 0xff) % INSTANCE_BUCKETS);
98}
99
100static struct nfulnl_instance *
9368a53c 101__instance_lookup(struct nfnl_log_net *log, u_int16_t group_num)
0597f268
HW
102{
103 struct hlist_head *head;
0597f268
HW
104 struct nfulnl_instance *inst;
105
9368a53c 106 head = &log->instance_table[instance_hashfn(group_num)];
b67bfe0d 107 hlist_for_each_entry_rcu(inst, head, hlist) {
0597f268
HW
108 if (inst->group_num == group_num)
109 return inst;
110 }
111 return NULL;
112}
113
114static inline void
115instance_get(struct nfulnl_instance *inst)
116{
b54ab92b 117 refcount_inc(&inst->use);
0597f268
HW
118}
119
120static struct nfulnl_instance *
9368a53c 121instance_lookup_get(struct nfnl_log_net *log, u_int16_t group_num)
0597f268
HW
122{
123 struct nfulnl_instance *inst;
124
bed1be20 125 rcu_read_lock_bh();
9368a53c 126 inst = __instance_lookup(log, group_num);
b54ab92b 127 if (inst && !refcount_inc_not_zero(&inst->use))
f5c5440d 128 inst = NULL;
bed1be20 129 rcu_read_unlock_bh();
0597f268
HW
130
131 return inst;
132}
133
bed1be20
ED
134static void nfulnl_instance_free_rcu(struct rcu_head *head)
135{
9368a53c
G
136 struct nfulnl_instance *inst =
137 container_of(head, struct nfulnl_instance, rcu);
138
139 put_net(inst->net);
140 kfree(inst);
bed1be20
ED
141 module_put(THIS_MODULE);
142}
143
0597f268
HW
144static void
145instance_put(struct nfulnl_instance *inst)
146{
b54ab92b 147 if (inst && refcount_dec_and_test(&inst->use))
c8d1da40 148 call_rcu(&inst->rcu, nfulnl_instance_free_rcu);
0597f268
HW
149}
150
e99e88a9 151static void nfulnl_timer(struct timer_list *t);
0597f268
HW
152
153static struct nfulnl_instance *
9368a53c 154instance_create(struct net *net, u_int16_t group_num,
cc6bc448 155 u32 portid, struct user_namespace *user_ns)
0597f268
HW
156{
157 struct nfulnl_instance *inst;
9368a53c 158 struct nfnl_log_net *log = nfnl_log_pernet(net);
baab2ce7 159 int err;
0597f268 160
9368a53c
G
161 spin_lock_bh(&log->instances_lock);
162 if (__instance_lookup(log, group_num)) {
baab2ce7 163 err = -EEXIST;
0597f268
HW
164 goto out_unlock;
165 }
166
10dfdc69 167 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
baab2ce7
PM
168 if (!inst) {
169 err = -ENOMEM;
0597f268 170 goto out_unlock;
baab2ce7 171 }
0597f268 172
aace57e0
MM
173 if (!try_module_get(THIS_MODULE)) {
174 kfree(inst);
baab2ce7 175 err = -EAGAIN;
aace57e0
MM
176 goto out_unlock;
177 }
178
0597f268 179 INIT_HLIST_NODE(&inst->hlist);
181a46a5 180 spin_lock_init(&inst->lock);
0597f268 181 /* needs to be two, since we _put() after creation */
b54ab92b 182 refcount_set(&inst->use, 2);
0597f268 183
e99e88a9 184 timer_setup(&inst->timer, nfulnl_timer, 0);
0597f268 185
9368a53c 186 inst->net = get_net(net);
9eea9515 187 inst->peer_user_ns = user_ns;
15e47304 188 inst->peer_portid = portid;
0597f268
HW
189 inst->group_num = group_num;
190
191 inst->qthreshold = NFULNL_QTHRESH_DEFAULT;
192 inst->flushtimeout = NFULNL_TIMEOUT_DEFAULT;
193 inst->nlbufsiz = NFULNL_NLBUFSIZ_DEFAULT;
194 inst->copy_mode = NFULNL_COPY_PACKET;
6b6ec99a 195 inst->copy_range = NFULNL_COPY_RANGE_MAX;
0597f268 196
f5c5440d 197 hlist_add_head_rcu(&inst->hlist,
9368a53c
G
198 &log->instance_table[instance_hashfn(group_num)]);
199
0597f268 200
9368a53c 201 spin_unlock_bh(&log->instances_lock);
0597f268
HW
202
203 return inst;
204
0597f268 205out_unlock:
9368a53c 206 spin_unlock_bh(&log->instances_lock);
baab2ce7 207 return ERR_PTR(err);
0597f268
HW
208}
209
e3567061 210static void __nfulnl_flush(struct nfulnl_instance *inst);
0597f268 211
f5c5440d 212/* called with BH disabled */
0597f268 213static void
9afdb00c 214__instance_destroy(struct nfulnl_instance *inst)
0597f268
HW
215{
216 /* first pull it out of the global list */
f5c5440d 217 hlist_del_rcu(&inst->hlist);
0597f268 218
0597f268
HW
219 /* then flush all pending packets from skb */
220
f5c5440d
ED
221 spin_lock(&inst->lock);
222
223 /* lockless readers wont be able to use us */
224 inst->copy_mode = NFULNL_COPY_DISABLED;
225
e3567061
MM
226 if (inst->skb)
227 __nfulnl_flush(inst);
f5c5440d 228 spin_unlock(&inst->lock);
0597f268
HW
229
230 /* and finally put the refcount */
231 instance_put(inst);
0597f268
HW
232}
233
0597f268 234static inline void
9368a53c
G
235instance_destroy(struct nfnl_log_net *log,
236 struct nfulnl_instance *inst)
0597f268 237{
9368a53c 238 spin_lock_bh(&log->instances_lock);
9afdb00c 239 __instance_destroy(inst);
9368a53c 240 spin_unlock_bh(&log->instances_lock);
0597f268
HW
241}
242
243static int
244nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
245 unsigned int range)
246{
247 int status = 0;
248
249 spin_lock_bh(&inst->lock);
601e68e1 250
0597f268
HW
251 switch (mode) {
252 case NFULNL_COPY_NONE:
253 case NFULNL_COPY_META:
254 inst->copy_mode = mode;
255 inst->copy_range = 0;
256 break;
601e68e1 257
0597f268
HW
258 case NFULNL_COPY_PACKET:
259 inst->copy_mode = mode;
c1e7dc91
FW
260 if (range == 0)
261 range = NFULNL_COPY_RANGE_MAX;
6b6ec99a
MM
262 inst->copy_range = min_t(unsigned int,
263 range, NFULNL_COPY_RANGE_MAX);
0597f268 264 break;
601e68e1 265
0597f268
HW
266 default:
267 status = -EINVAL;
268 break;
269 }
270
271 spin_unlock_bh(&inst->lock);
272
273 return status;
274}
275
276static int
277nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
278{
279 int status;
280
281 spin_lock_bh(&inst->lock);
282 if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
283 status = -ERANGE;
284 else if (nlbufsiz > 131072)
285 status = -ERANGE;
286 else {
287 inst->nlbufsiz = nlbufsiz;
288 status = 0;
289 }
290 spin_unlock_bh(&inst->lock);
291
292 return status;
293}
294
23509fcd 295static void
0597f268
HW
296nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
297{
298 spin_lock_bh(&inst->lock);
299 inst->flushtimeout = timeout;
300 spin_unlock_bh(&inst->lock);
0597f268
HW
301}
302
23509fcd 303static void
0597f268
HW
304nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
305{
306 spin_lock_bh(&inst->lock);
307 inst->qthreshold = qthresh;
308 spin_unlock_bh(&inst->lock);
0597f268
HW
309}
310
0af5f6c1
HW
311static int
312nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
313{
314 spin_lock_bh(&inst->lock);
ee433530 315 inst->flags = flags;
0af5f6c1
HW
316 spin_unlock_bh(&inst->lock);
317
318 return 0;
319}
320
c6a8f648 321static struct sk_buff *
afff14f6
G
322nfulnl_alloc_skb(struct net *net, u32 peer_portid, unsigned int inst_size,
323 unsigned int pkt_size)
0597f268
HW
324{
325 struct sk_buff *skb;
ad2ad0f9 326 unsigned int n;
0597f268 327
0597f268
HW
328 /* alloc skb which should be big enough for a whole multipart
329 * message. WARNING: has to be <= 128k due to slab restrictions */
330
ad2ad0f9 331 n = max(inst_size, pkt_size);
0813fbc9 332 skb = alloc_skb(n, GFP_ATOMIC | __GFP_NOWARN);
0597f268 333 if (!skb) {
ad2ad0f9
PM
334 if (n > pkt_size) {
335 /* try to allocate only as much as we need for current
336 * packet */
0597f268 337
905f0a73 338 skb = alloc_skb(pkt_size, GFP_ATOMIC);
ad2ad0f9 339 }
0597f268
HW
340 }
341
342 return skb;
343}
344
b51d3fa3 345static void
0597f268
HW
346__nfulnl_send(struct nfulnl_instance *inst)
347{
d550d095
DM
348 if (inst->qlen > 1) {
349 struct nlmsghdr *nlh = nlmsg_put(inst->skb, 0, 0,
350 NLMSG_DONE,
351 sizeof(struct nfgenmsg),
352 0);
b51d3fa3
HL
353 if (WARN_ONCE(!nlh, "bad nlskb size: %u, tailroom %d\n",
354 inst->skb->len, skb_tailroom(inst->skb))) {
355 kfree_skb(inst->skb);
d550d095 356 goto out;
b51d3fa3 357 }
d550d095 358 }
b51d3fa3
HL
359 nfnetlink_unicast(inst->skb, inst->net, inst->peer_portid,
360 MSG_DONTWAIT);
361out:
0597f268
HW
362 inst->qlen = 0;
363 inst->skb = NULL;
0597f268
HW
364}
365
e3567061
MM
366static void
367__nfulnl_flush(struct nfulnl_instance *inst)
368{
369 /* timer holds a reference */
370 if (del_timer(&inst->timer))
371 instance_put(inst);
372 if (inst->skb)
373 __nfulnl_send(inst);
374}
375
c6a8f648 376static void
e99e88a9 377nfulnl_timer(struct timer_list *t)
0597f268 378{
e99e88a9 379 struct nfulnl_instance *inst = from_timer(inst, t, timer);
0597f268 380
0597f268 381 spin_lock_bh(&inst->lock);
370e6a87
MM
382 if (inst->skb)
383 __nfulnl_send(inst);
0597f268 384 spin_unlock_bh(&inst->lock);
05f7b7b3 385 instance_put(inst);
0597f268
HW
386}
387
0af5f6c1
HW
388/* This is an inline function, we don't really care about a long
389 * list of arguments */
601e68e1 390static inline int
9368a53c
G
391__build_packet_message(struct nfnl_log_net *log,
392 struct nfulnl_instance *inst,
601e68e1 393 const struct sk_buff *skb,
0597f268 394 unsigned int data_len,
76108cea 395 u_int8_t pf,
0597f268
HW
396 unsigned int hooknum,
397 const struct net_device *indev,
398 const struct net_device *outdev,
a29a9a58
KM
399 const char *prefix, unsigned int plen,
400 const struct nfnl_ct_hook *nfnl_ct,
401 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
0597f268 402{
0597f268
HW
403 struct nfulnl_msg_packet_hdr pmsg;
404 struct nlmsghdr *nlh;
405 struct nfgenmsg *nfmsg;
27a884dc 406 sk_buff_data_t old_tail = inst->skb->tail;
0626af31 407 struct sock *sk;
0c36b48b 408 const unsigned char *hwhdrp;
0597f268 409
d550d095 410 nlh = nlmsg_put(inst->skb, 0, 0,
dedb67c4 411 nfnl_msg_type(NFNL_SUBSYS_ULOG, NFULNL_MSG_PACKET),
d550d095
DM
412 sizeof(struct nfgenmsg), 0);
413 if (!nlh)
414 return -1;
415 nfmsg = nlmsg_data(nlh);
0597f268
HW
416 nfmsg->nfgen_family = pf;
417 nfmsg->version = NFNETLINK_V0;
418 nfmsg->res_id = htons(inst->group_num);
419
e4d091d7 420 memset(&pmsg, 0, sizeof(pmsg));
febf0a43 421 pmsg.hw_protocol = skb->protocol;
0597f268
HW
422 pmsg.hook = hooknum;
423
1db20a52
DM
424 if (nla_put(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg))
425 goto nla_put_failure;
0597f268 426
1db20a52
DM
427 if (prefix &&
428 nla_put(inst->skb, NFULA_PREFIX, plen, prefix))
429 goto nla_put_failure;
0597f268
HW
430
431 if (indev) {
1109a90c 432#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1db20a52
DM
433 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
434 htonl(indev->ifindex)))
435 goto nla_put_failure;
fbcd923c
HW
436#else
437 if (pf == PF_BRIDGE) {
438 /* Case 1: outdev is physical input device, we need to
439 * look for bridge group (when called from
440 * netfilter_bridge) */
1db20a52
DM
441 if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
442 htonl(indev->ifindex)) ||
fbcd923c 443 /* this is the bridge group "brX" */
e2361cb9
AC
444 /* rcu_read_lock()ed by nf_hook_thresh or
445 * nf_log_packet.
446 */
1db20a52
DM
447 nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
448 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
449 goto nla_put_failure;
fbcd923c 450 } else {
c737b7c4
FW
451 struct net_device *physindev;
452
fbcd923c
HW
453 /* Case 2: indev is bridge group, we need to look for
454 * physical device (when called from ipv4) */
1db20a52
DM
455 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
456 htonl(indev->ifindex)))
457 goto nla_put_failure;
c737b7c4
FW
458
459 physindev = nf_bridge_get_physindev(skb);
460 if (physindev &&
1db20a52 461 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
c737b7c4 462 htonl(physindev->ifindex)))
1db20a52 463 goto nla_put_failure;
fbcd923c
HW
464 }
465#endif
0597f268
HW
466 }
467
468 if (outdev) {
1109a90c 469#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1db20a52
DM
470 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
471 htonl(outdev->ifindex)))
472 goto nla_put_failure;
fbcd923c
HW
473#else
474 if (pf == PF_BRIDGE) {
475 /* Case 1: outdev is physical output device, we need to
476 * look for bridge group (when called from
477 * netfilter_bridge) */
1db20a52
DM
478 if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
479 htonl(outdev->ifindex)) ||
fbcd923c 480 /* this is the bridge group "brX" */
e2361cb9
AC
481 /* rcu_read_lock()ed by nf_hook_thresh or
482 * nf_log_packet.
483 */
1db20a52
DM
484 nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
485 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
486 goto nla_put_failure;
fbcd923c 487 } else {
c737b7c4
FW
488 struct net_device *physoutdev;
489
fbcd923c
HW
490 /* Case 2: indev is a bridge group, we need to look
491 * for physical device (when called from ipv4) */
1db20a52
DM
492 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
493 htonl(outdev->ifindex)))
494 goto nla_put_failure;
c737b7c4
FW
495
496 physoutdev = nf_bridge_get_physoutdev(skb);
497 if (physoutdev &&
1db20a52 498 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
c737b7c4 499 htonl(physoutdev->ifindex)))
1db20a52 500 goto nla_put_failure;
fbcd923c
HW
501 }
502#endif
0597f268
HW
503 }
504
1db20a52
DM
505 if (skb->mark &&
506 nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
507 goto nla_put_failure;
0597f268 508
2c38de4c
NC
509 if (indev && skb->dev &&
510 skb->mac_header != skb->network_header) {
0597f268 511 struct nfulnl_msg_packet_hw phw;
e4d091d7
DC
512 int len;
513
514 memset(&phw, 0, sizeof(phw));
515 len = dev_parse_header(skb, phw.hw_addr);
b95cce35
SH
516 if (len > 0) {
517 phw.hw_addrlen = htons(len);
1db20a52
DM
518 if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw))
519 goto nla_put_failure;
b95cce35 520 }
0597f268
HW
521 }
522
72961ecf 523 if (indev && skb_mac_header_was_set(skb)) {
2dba62c3 524 if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
1db20a52 525 nla_put_be16(inst->skb, NFULA_HWLEN,
0c36b48b
BH
526 htons(skb->dev->hard_header_len)))
527 goto nla_put_failure;
528
529 hwhdrp = skb_mac_header(skb);
530
531 if (skb->dev->type == ARPHRD_SIT)
532 hwhdrp -= ETH_HLEN;
533
534 if (hwhdrp >= skb->head &&
535 nla_put(inst->skb, NFULA_HWHEADER,
536 skb->dev->hard_header_len, hwhdrp))
1db20a52 537 goto nla_put_failure;
72961ecf
EL
538 }
539
916f6efa 540 if (hooknum <= NF_INET_FORWARD && skb->tstamp) {
0597f268 541 struct nfulnl_msg_packet_timestamp ts;
f6389ecb
AB
542 struct timespec64 kts = ktime_to_timespec64(skb->tstamp);
543 ts.sec = cpu_to_be64(kts.tv_sec);
544 ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC);
0597f268 545
1db20a52
DM
546 if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts))
547 goto nla_put_failure;
0597f268
HW
548 }
549
550 /* UID */
0626af31 551 sk = skb->sk;
a8399231 552 if (sk && sk_fullsock(sk)) {
0626af31
ED
553 read_lock_bh(&sk->sk_callback_lock);
554 if (sk->sk_socket && sk->sk_socket->file) {
555 struct file *file = sk->sk_socket->file;
437589a7
LT
556 const struct cred *cred = file->f_cred;
557 struct user_namespace *user_ns = inst->peer_user_ns;
558 __be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
559 __be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
0626af31 560 read_unlock_bh(&sk->sk_callback_lock);
1db20a52
DM
561 if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
562 nla_put_be32(inst->skb, NFULA_GID, gid))
563 goto nla_put_failure;
0597f268 564 } else
0626af31 565 read_unlock_bh(&sk->sk_callback_lock);
0597f268
HW
566 }
567
0af5f6c1 568 /* local sequence number */
1db20a52
DM
569 if ((inst->flags & NFULNL_CFG_F_SEQ) &&
570 nla_put_be32(inst->skb, NFULA_SEQ, htonl(inst->seq++)))
571 goto nla_put_failure;
0dfedd28 572
0af5f6c1 573 /* global sequence number */
1db20a52
DM
574 if ((inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) &&
575 nla_put_be32(inst->skb, NFULA_SEQ_GLOBAL,
9368a53c 576 htonl(atomic_inc_return(&log->global_seq))))
1db20a52 577 goto nla_put_failure;
0af5f6c1 578
a29a9a58
KM
579 if (ct && nfnl_ct->build(inst->skb, ct, ctinfo,
580 NFULA_CT, NFULA_CT_INFO) < 0)
581 goto nla_put_failure;
582
0597f268 583 if (data_len) {
df6fb868
PM
584 struct nlattr *nla;
585 int size = nla_attr_size(data_len);
0597f268 586
82251615
PNA
587 if (skb_tailroom(inst->skb) < nla_total_size(data_len))
588 goto nla_put_failure;
0597f268 589
4df864c1 590 nla = skb_put(inst->skb, nla_total_size(data_len));
df6fb868
PM
591 nla->nla_type = NFULA_PAYLOAD;
592 nla->nla_len = size;
0597f268 593
df6fb868 594 if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
0597f268
HW
595 BUG();
596 }
601e68e1 597
0597f268
HW
598 nlh->nlmsg_len = inst->skb->tail - old_tail;
599 return 0;
600
df6fb868 601nla_put_failure:
0597f268
HW
602 PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
603 return -1;
604}
605
549d2d41 606static const struct nf_loginfo default_loginfo = {
0597f268
HW
607 .type = NF_LOG_TYPE_ULOG,
608 .u = {
609 .ulog = {
610 .copy_len = 0xffff,
611 .group = 0,
612 .qthreshold = 1,
613 },
614 },
615};
616
617/* log handler for internal netfilter logging api */
ce20cdf4 618static void
8cdb46da
HS
619nfulnl_log_packet(struct net *net,
620 u_int8_t pf,
0597f268
HW
621 unsigned int hooknum,
622 const struct sk_buff *skb,
623 const struct net_device *in,
624 const struct net_device *out,
625 const struct nf_loginfo *li_user,
626 const char *prefix)
627{
a29a9a58
KM
628 size_t size;
629 unsigned int data_len;
0597f268
HW
630 struct nfulnl_instance *inst;
631 const struct nf_loginfo *li;
632 unsigned int qthreshold;
ce20cdf4 633 unsigned int plen = 0;
9368a53c 634 struct nfnl_log_net *log = nfnl_log_pernet(net);
a29a9a58
KM
635 const struct nfnl_ct_hook *nfnl_ct = NULL;
636 struct nf_conn *ct = NULL;
637 enum ip_conntrack_info uninitialized_var(ctinfo);
0597f268 638
601e68e1 639 if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
0597f268
HW
640 li = li_user;
641 else
642 li = &default_loginfo;
643
9368a53c 644 inst = instance_lookup_get(log, li->u.ulog.group);
0597f268 645 if (!inst)
0597f268 646 return;
0597f268 647
d7a5c324 648 if (prefix)
881dbfe8 649 plen = strlen(prefix) + 1;
d7a5c324 650
0597f268
HW
651 /* FIXME: do we want to make the size calculation conditional based on
652 * what is actually present? way more branches and checks, but more
653 * memory efficient... */
573ce260 654 size = nlmsg_total_size(sizeof(struct nfgenmsg))
df6fb868
PM
655 + nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
656 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
657 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
1109a90c 658#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
df6fb868
PM
659 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
660 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
fbcd923c 661#endif
df6fb868
PM
662 + nla_total_size(sizeof(u_int32_t)) /* mark */
663 + nla_total_size(sizeof(u_int32_t)) /* uid */
76aa1ce1 664 + nla_total_size(sizeof(u_int32_t)) /* gid */
df6fb868
PM
665 + nla_total_size(plen) /* prefix */
666 + nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
9dfa1dfe
FW
667 + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp))
668 + nla_total_size(sizeof(struct nfgenmsg)); /* NLMSG_DONE */
0597f268 669
eeff9bee
PNA
670 if (in && skb_mac_header_was_set(skb)) {
671 size += nla_total_size(skb->dev->hard_header_len)
672 + nla_total_size(sizeof(u_int16_t)) /* hwtype */
673 + nla_total_size(sizeof(u_int16_t)); /* hwlen */
674 }
675
0597f268
HW
676 spin_lock_bh(&inst->lock);
677
0af5f6c1 678 if (inst->flags & NFULNL_CFG_F_SEQ)
df6fb868 679 size += nla_total_size(sizeof(u_int32_t));
0af5f6c1 680 if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
df6fb868 681 size += nla_total_size(sizeof(u_int32_t));
a29a9a58
KM
682 if (inst->flags & NFULNL_CFG_F_CONNTRACK) {
683 nfnl_ct = rcu_dereference(nfnl_ct_hook);
684 if (nfnl_ct != NULL) {
685 ct = nfnl_ct->get_ct(skb, &ctinfo);
686 if (ct != NULL)
687 size += nfnl_ct->build_size(ct);
688 }
689 }
0af5f6c1 690
0597f268
HW
691 qthreshold = inst->qthreshold;
692 /* per-rule qthreshold overrides per-instance */
5ca431f9
EL
693 if (li->u.ulog.qthreshold)
694 if (qthreshold > li->u.ulog.qthreshold)
695 qthreshold = li->u.ulog.qthreshold;
696
601e68e1 697
0597f268
HW
698 switch (inst->copy_mode) {
699 case NFULNL_COPY_META:
700 case NFULNL_COPY_NONE:
701 data_len = 0;
702 break;
601e68e1 703
0597f268 704 case NFULNL_COPY_PACKET:
7643507f
VP
705 data_len = inst->copy_range;
706 if ((li->u.ulog.flags & NF_LOG_F_COPY_LEN) &&
707 (li->u.ulog.copy_len < data_len))
708 data_len = li->u.ulog.copy_len;
709
710 if (data_len > skb->len)
0597f268 711 data_len = skb->len;
601e68e1 712
df6fb868 713 size += nla_total_size(data_len);
0597f268 714 break;
601e68e1 715
f5c5440d 716 case NFULNL_COPY_DISABLED:
0597f268 717 default:
55b5a91e 718 goto unlock_and_release;
0597f268
HW
719 }
720
9dfa1dfe 721 if (inst->skb && size > skb_tailroom(inst->skb)) {
0597f268
HW
722 /* either the queue len is too high or we don't have
723 * enough room in the skb left. flush to userspace. */
e3567061 724 __nfulnl_flush(inst);
55b5a91e 725 }
0597f268 726
55b5a91e 727 if (!inst->skb) {
afff14f6
G
728 inst->skb = nfulnl_alloc_skb(net, inst->peer_portid,
729 inst->nlbufsiz, size);
55b5a91e 730 if (!inst->skb)
0597f268 731 goto alloc_failure;
0597f268
HW
732 }
733
0597f268
HW
734 inst->qlen++;
735
9368a53c 736 __build_packet_message(log, inst, skb, data_len, pf,
a29a9a58
KM
737 hooknum, in, out, prefix, plen,
738 nfnl_ct, ct, ctinfo);
0597f268 739
d63b043d
MM
740 if (inst->qlen >= qthreshold)
741 __nfulnl_flush(inst);
0597f268
HW
742 /* timer_pending always called within inst->lock, so there
743 * is no chance of a race here */
d63b043d 744 else if (!timer_pending(&inst->timer)) {
0597f268
HW
745 instance_get(inst);
746 inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
747 add_timer(&inst->timer);
748 }
0597f268 749
ed32abea
MM
750unlock_and_release:
751 spin_unlock_bh(&inst->lock);
752 instance_put(inst);
0597f268
HW
753 return;
754
755alloc_failure:
0597f268 756 /* FIXME: statistics */
ed32abea 757 goto unlock_and_release;
0597f268
HW
758}
759
760static int
761nfulnl_rcv_nl_event(struct notifier_block *this,
762 unsigned long event, void *ptr)
763{
764 struct netlink_notify *n = ptr;
9368a53c 765 struct nfnl_log_net *log = nfnl_log_pernet(n->net);
0597f268 766
dee5817e 767 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
0597f268
HW
768 int i;
769
15e47304 770 /* destroy all instances for this portid */
9368a53c 771 spin_lock_bh(&log->instances_lock);
0597f268 772 for (i = 0; i < INSTANCE_BUCKETS; i++) {
b67bfe0d 773 struct hlist_node *t2;
0597f268 774 struct nfulnl_instance *inst;
9368a53c 775 struct hlist_head *head = &log->instance_table[i];
0597f268 776
b67bfe0d 777 hlist_for_each_entry_safe(inst, t2, head, hlist) {
9368a53c 778 if (n->portid == inst->peer_portid)
0597f268
HW
779 __instance_destroy(inst);
780 }
781 }
9368a53c 782 spin_unlock_bh(&log->instances_lock);
0597f268
HW
783 }
784 return NOTIFY_DONE;
785}
786
787static struct notifier_block nfulnl_rtnl_notifier = {
788 .notifier_call = nfulnl_rcv_nl_event,
789};
790
7b8002a1
PNA
791static int nfulnl_recv_unsupp(struct net *net, struct sock *ctnl,
792 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
793 const struct nlattr * const nfqa[],
794 struct netlink_ext_ack *extack)
0597f268
HW
795{
796 return -ENOTSUPP;
797}
798
ca735b3a 799static struct nf_logger nfulnl_logger __read_mostly = {
0597f268 800 .name = "nfnetlink_log",
5962815a 801 .type = NF_LOG_TYPE_ULOG,
d4ef3835 802 .logfn = nfulnl_log_packet,
0597f268
HW
803 .me = THIS_MODULE,
804};
805
fd8281ad
PM
806static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
807 [NFULA_CFG_CMD] = { .len = sizeof(struct nfulnl_msg_config_cmd) },
808 [NFULA_CFG_MODE] = { .len = sizeof(struct nfulnl_msg_config_mode) },
809 [NFULA_CFG_TIMEOUT] = { .type = NLA_U32 },
810 [NFULA_CFG_QTHRESH] = { .type = NLA_U32 },
811 [NFULA_CFG_NLBUFSIZ] = { .type = NLA_U32 },
812 [NFULA_CFG_FLAGS] = { .type = NLA_U16 },
0597f268
HW
813};
814
7b8002a1
PNA
815static int nfulnl_recv_config(struct net *net, struct sock *ctnl,
816 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
817 const struct nlattr * const nfula[],
818 struct netlink_ext_ack *extack)
0597f268 819{
d550d095 820 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
0597f268
HW
821 u_int16_t group_num = ntohs(nfmsg->res_id);
822 struct nfulnl_instance *inst;
b7047a1c 823 struct nfulnl_msg_config_cmd *cmd = NULL;
9368a53c 824 struct nfnl_log_net *log = nfnl_log_pernet(net);
0597f268 825 int ret = 0;
c872a2d9 826 u16 flags = 0;
0597f268 827
b7047a1c
PM
828 if (nfula[NFULA_CFG_CMD]) {
829 u_int8_t pf = nfmsg->nfgen_family;
830 cmd = nla_data(nfula[NFULA_CFG_CMD]);
831
832 /* Commands without queue context */
833 switch (cmd->command) {
834 case NFULNL_CFG_CMD_PF_BIND:
30e0c6a6 835 return nf_log_bind_pf(net, pf, &nfulnl_logger);
b7047a1c 836 case NFULNL_CFG_CMD_PF_UNBIND:
30e0c6a6 837 nf_log_unbind_pf(net, pf);
b7047a1c
PM
838 return 0;
839 }
840 }
841
9368a53c 842 inst = instance_lookup_get(log, group_num);
15e47304 843 if (inst && inst->peer_portid != NETLINK_CB(skb).portid) {
c0506365
PM
844 ret = -EPERM;
845 goto out_put;
846 }
847
8cbc8708
PN
848 /* Check if we support these flags in first place, dependencies should
849 * be there too not to break atomicity.
850 */
851 if (nfula[NFULA_CFG_FLAGS]) {
852 flags = ntohs(nla_get_be16(nfula[NFULA_CFG_FLAGS]));
853
854 if ((flags & NFULNL_CFG_F_CONNTRACK) &&
855 !rcu_access_pointer(nfnl_ct_hook)) {
856#ifdef CONFIG_MODULES
857 nfnl_unlock(NFNL_SUBSYS_ULOG);
858 request_module("ip_conntrack_netlink");
859 nfnl_lock(NFNL_SUBSYS_ULOG);
860 if (rcu_access_pointer(nfnl_ct_hook)) {
861 ret = -EAGAIN;
862 goto out_put;
863 }
864#endif
865 ret = -EOPNOTSUPP;
866 goto out_put;
867 }
868 }
869
b7047a1c 870 if (cmd != NULL) {
0597f268
HW
871 switch (cmd->command) {
872 case NFULNL_CFG_CMD_BIND:
873 if (inst) {
874 ret = -EBUSY;
875 goto out_put;
876 }
877
9368a53c 878 inst = instance_create(net, group_num,
15e47304 879 NETLINK_CB(skb).portid,
e32123e5 880 sk_user_ns(NETLINK_CB(skb).sk));
baab2ce7
PM
881 if (IS_ERR(inst)) {
882 ret = PTR_ERR(inst);
f414c16c 883 goto out;
0597f268
HW
884 }
885 break;
886 case NFULNL_CFG_CMD_UNBIND:
887 if (!inst) {
888 ret = -ENODEV;
f414c16c 889 goto out;
0597f268
HW
890 }
891
9368a53c 892 instance_destroy(log, inst);
a49c6503 893 goto out_put;
0597f268 894 default:
cd21f0ac 895 ret = -ENOTSUPP;
eb075954 896 goto out_put;
0597f268 897 }
336a3b3e
PNA
898 } else if (!inst) {
899 ret = -ENODEV;
900 goto out;
0597f268
HW
901 }
902
df6fb868 903 if (nfula[NFULA_CFG_MODE]) {
336a3b3e
PNA
904 struct nfulnl_msg_config_mode *params =
905 nla_data(nfula[NFULA_CFG_MODE]);
0597f268
HW
906
907 nfulnl_set_mode(inst, params->copy_mode,
d1208b99 908 ntohl(params->copy_range));
0597f268
HW
909 }
910
df6fb868 911 if (nfula[NFULA_CFG_TIMEOUT]) {
0dfedd28 912 __be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
0597f268
HW
913
914 nfulnl_set_timeout(inst, ntohl(timeout));
915 }
916
df6fb868 917 if (nfula[NFULA_CFG_NLBUFSIZ]) {
0dfedd28 918 __be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
0597f268
HW
919
920 nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
921 }
922
df6fb868 923 if (nfula[NFULA_CFG_QTHRESH]) {
0dfedd28 924 __be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
0597f268
HW
925
926 nfulnl_set_qthresh(inst, ntohl(qthresh));
927 }
928
8cbc8708 929 if (nfula[NFULA_CFG_FLAGS])
a29a9a58 930 nfulnl_set_flags(inst, flags);
0af5f6c1 931
0597f268
HW
932out_put:
933 instance_put(inst);
dd16704e 934out:
0597f268
HW
935 return ret;
936}
937
7c8d4cb4 938static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
0597f268 939 [NFULNL_MSG_PACKET] = { .call = nfulnl_recv_unsupp,
37d2e7a2 940 .attr_count = NFULA_MAX, },
0597f268 941 [NFULNL_MSG_CONFIG] = { .call = nfulnl_recv_config,
fd8281ad
PM
942 .attr_count = NFULA_CFG_MAX,
943 .policy = nfula_cfg_policy },
0597f268
HW
944};
945
7c8d4cb4 946static const struct nfnetlink_subsystem nfulnl_subsys = {
0597f268
HW
947 .name = "log",
948 .subsys_id = NFNL_SUBSYS_ULOG,
949 .cb_count = NFULNL_MSG_MAX,
0597f268
HW
950 .cb = nfulnl_cb,
951};
952
953#ifdef CONFIG_PROC_FS
954struct iter_state {
9368a53c 955 struct seq_net_private p;
0597f268
HW
956 unsigned int bucket;
957};
958
9368a53c 959static struct hlist_node *get_first(struct net *net, struct iter_state *st)
0597f268 960{
9368a53c 961 struct nfnl_log_net *log;
0597f268
HW
962 if (!st)
963 return NULL;
964
9368a53c
G
965 log = nfnl_log_pernet(net);
966
0597f268 967 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
9368a53c
G
968 struct hlist_head *head = &log->instance_table[st->bucket];
969
970 if (!hlist_empty(head))
971 return rcu_dereference_bh(hlist_first_rcu(head));
0597f268
HW
972 }
973 return NULL;
974}
975
9368a53c
G
976static struct hlist_node *get_next(struct net *net, struct iter_state *st,
977 struct hlist_node *h)
0597f268 978{
0e60ebe0 979 h = rcu_dereference_bh(hlist_next_rcu(h));
0597f268 980 while (!h) {
9368a53c
G
981 struct nfnl_log_net *log;
982 struct hlist_head *head;
983
0597f268
HW
984 if (++st->bucket >= INSTANCE_BUCKETS)
985 return NULL;
986
9368a53c
G
987 log = nfnl_log_pernet(net);
988 head = &log->instance_table[st->bucket];
989 h = rcu_dereference_bh(hlist_first_rcu(head));
0597f268
HW
990 }
991 return h;
992}
993
9368a53c
G
994static struct hlist_node *get_idx(struct net *net, struct iter_state *st,
995 loff_t pos)
0597f268
HW
996{
997 struct hlist_node *head;
9368a53c 998 head = get_first(net, st);
0597f268
HW
999
1000 if (head)
9368a53c 1001 while (pos && (head = get_next(net, st, head)))
0597f268
HW
1002 pos--;
1003 return pos ? NULL : head;
1004}
1005
9368a53c 1006static void *seq_start(struct seq_file *s, loff_t *pos)
bed1be20 1007 __acquires(rcu_bh)
0597f268 1008{
bed1be20 1009 rcu_read_lock_bh();
9368a53c 1010 return get_idx(seq_file_net(s), s->private, *pos);
0597f268
HW
1011}
1012
1013static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1014{
1015 (*pos)++;
9368a53c 1016 return get_next(seq_file_net(s), s->private, v);
0597f268
HW
1017}
1018
1019static void seq_stop(struct seq_file *s, void *v)
bed1be20 1020 __releases(rcu_bh)
0597f268 1021{
bed1be20 1022 rcu_read_unlock_bh();
0597f268
HW
1023}
1024
1025static int seq_show(struct seq_file *s, void *v)
1026{
1027 const struct nfulnl_instance *inst = v;
1028
20a1d165 1029 seq_printf(s, "%5u %6u %5u %1u %5u %6u %2u\n",
1ca9e417
JP
1030 inst->group_num,
1031 inst->peer_portid, inst->qlen,
1032 inst->copy_mode, inst->copy_range,
b54ab92b 1033 inst->flushtimeout, refcount_read(&inst->use));
1ca9e417
JP
1034
1035 return 0;
0597f268
HW
1036}
1037
56b3d975 1038static const struct seq_operations nful_seq_ops = {
0597f268
HW
1039 .start = seq_start,
1040 .next = seq_next,
1041 .stop = seq_stop,
1042 .show = seq_show,
1043};
0597f268
HW
1044#endif /* PROC_FS */
1045
9368a53c 1046static int __net_init nfnl_log_net_init(struct net *net)
0597f268 1047{
9368a53c
G
1048 unsigned int i;
1049 struct nfnl_log_net *log = nfnl_log_pernet(net);
f13f2aee
PW
1050#ifdef CONFIG_PROC_FS
1051 struct proc_dir_entry *proc;
1052 kuid_t root_uid;
1053 kgid_t root_gid;
1054#endif
601e68e1 1055
0597f268 1056 for (i = 0; i < INSTANCE_BUCKETS; i++)
9368a53c
G
1057 INIT_HLIST_HEAD(&log->instance_table[i]);
1058 spin_lock_init(&log->instances_lock);
1059
1060#ifdef CONFIG_PROC_FS
c3506372
CH
1061 proc = proc_create_net("nfnetlink_log", 0440, net->nf.proc_netfilter,
1062 &nful_seq_ops, sizeof(struct iter_state));
f13f2aee 1063 if (!proc)
9368a53c 1064 return -ENOMEM;
f13f2aee
PW
1065
1066 root_uid = make_kuid(net->user_ns, 0);
1067 root_gid = make_kgid(net->user_ns, 0);
1068 if (uid_valid(root_uid) && gid_valid(root_gid))
1069 proc_set_user(proc, root_uid, root_gid);
9368a53c
G
1070#endif
1071 return 0;
1072}
1073
1074static void __net_exit nfnl_log_net_exit(struct net *net)
1075{
613d0776
VA
1076 struct nfnl_log_net *log = nfnl_log_pernet(net);
1077 unsigned int i;
1078
e778f56e 1079#ifdef CONFIG_PROC_FS
9368a53c 1080 remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter);
e778f56e 1081#endif
45c2aff6 1082 nf_log_unset(net, &nfulnl_logger);
613d0776
VA
1083 for (i = 0; i < INSTANCE_BUCKETS; i++)
1084 WARN_ON_ONCE(!hlist_empty(&log->instance_table[i]));
9368a53c
G
1085}
1086
1087static struct pernet_operations nfnl_log_net_ops = {
1088 .init = nfnl_log_net_init,
1089 .exit = nfnl_log_net_exit,
1090 .id = &nfnl_log_net_id,
1091 .size = sizeof(struct nfnl_log_net),
1092};
1093
1094static int __init nfnetlink_log_init(void)
1095{
3bfe0498
FR
1096 int status;
1097
1098 status = register_pernet_subsys(&nfnl_log_net_ops);
1099 if (status < 0) {
1100 pr_err("failed to register pernet ops\n");
1101 goto out;
1102 }
601e68e1 1103
0597f268
HW
1104 netlink_register_notifier(&nfulnl_rtnl_notifier);
1105 status = nfnetlink_subsys_register(&nfulnl_subsys);
1106 if (status < 0) {
beacd3e8 1107 pr_err("failed to create netlink socket\n");
0597f268
HW
1108 goto cleanup_netlink_notifier;
1109 }
1110
ca735b3a
EL
1111 status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
1112 if (status < 0) {
beacd3e8 1113 pr_err("failed to register logger\n");
ca735b3a
EL
1114 goto cleanup_subsys;
1115 }
1116
0597f268
HW
1117 return status;
1118
0597f268 1119cleanup_subsys:
0597f268
HW
1120 nfnetlink_subsys_unregister(&nfulnl_subsys);
1121cleanup_netlink_notifier:
1122 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
3bfe0498
FR
1123 unregister_pernet_subsys(&nfnl_log_net_ops);
1124out:
0597f268
HW
1125 return status;
1126}
1127
65b4b4e8 1128static void __exit nfnetlink_log_fini(void)
0597f268 1129{
32292a7f
PM
1130 nfnetlink_subsys_unregister(&nfulnl_subsys);
1131 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
3bfe0498 1132 unregister_pernet_subsys(&nfnl_log_net_ops);
c83fa196 1133 nf_log_unregister(&nfulnl_logger);
0597f268
HW
1134}
1135
1136MODULE_DESCRIPTION("netfilter userspace logging");
1137MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1138MODULE_LICENSE("GPL");
f682faef 1139MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
fab4085f
PNA
1140MODULE_ALIAS_NF_LOGGER(AF_INET, 1);
1141MODULE_ALIAS_NF_LOGGER(AF_INET6, 1);
1142MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 1);
2497b846 1143MODULE_ALIAS_NF_LOGGER(3, 1); /* NFPROTO_ARP */
a7647080 1144MODULE_ALIAS_NF_LOGGER(5, 1); /* NFPROTO_NETDEV */
0597f268 1145
65b4b4e8
AM
1146module_init(nfnetlink_log_init);
1147module_exit(nfnetlink_log_fini);