Merge tag 'dmaengine-fix-5.2' of git://git.infradead.org/users/vkoul/slave-dma
[linux-2.6/linux-2.6-arm.git] / net / netfilter / nfnetlink_log.c
blob6dee4f9a944cd5971306fe6ea7f96e54659a8a4f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * This is a module which is used for logging packets to userspace via
4 * nfetlink.
6 * (C) 2005 by Harald Welte <laforge@netfilter.org>
7 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
9 * Based on the old ipv4-only ipt_ULOG.c:
10 * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
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>
23 #include <linux/netfilter_bridge.h>
24 #include <net/netlink.h>
25 #include <linux/netfilter/nfnetlink.h>
26 #include <linux/netfilter/nfnetlink_log.h>
27 #include <linux/netfilter/nf_conntrack_common.h>
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>
33 #include <linux/slab.h>
34 #include <net/sock.h>
35 #include <net/netfilter/nf_log.h>
36 #include <net/netns/generic.h>
38 #include <linux/atomic.h>
39 #include <linux/refcount.h>
42 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
43 #include "../bridge/br_private.h"
44 #endif
46 #define NFULNL_COPY_DISABLED 0xff
47 #define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
48 #define NFULNL_TIMEOUT_DEFAULT 100 /* every second */
49 #define NFULNL_QTHRESH_DEFAULT 100 /* 100 packets */
50 /* max packet size is limited by 16-bit struct nfattr nfa_len field */
51 #define NFULNL_COPY_RANGE_MAX (0xFFFF - NLA_HDRLEN)
53 #define PRINTR(x, args...) do { if (net_ratelimit()) \
54 printk(x, ## args); } while (0);
56 struct nfulnl_instance {
57 struct hlist_node hlist; /* global list of instances */
58 spinlock_t lock;
59 refcount_t use; /* use count */
61 unsigned int qlen; /* number of nlmsgs in skb */
62 struct sk_buff *skb; /* pre-allocatd skb */
63 struct timer_list timer;
64 struct net *net;
65 struct user_namespace *peer_user_ns; /* User namespace of the peer process */
66 u32 peer_portid; /* PORTID of the peer process */
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;
73 u_int32_t seq; /* instance-local sequential counter */
74 u_int16_t group_num; /* number of this queue */
75 u_int16_t flags;
76 u_int8_t copy_mode;
77 struct rcu_head rcu;
80 #define INSTANCE_BUCKETS 16
82 static unsigned int nfnl_log_net_id __read_mostly;
84 struct nfnl_log_net {
85 spinlock_t instances_lock;
86 struct hlist_head instance_table[INSTANCE_BUCKETS];
87 atomic_t global_seq;
90 static struct nfnl_log_net *nfnl_log_pernet(struct net *net)
92 return net_generic(net, nfnl_log_net_id);
95 static inline u_int8_t instance_hashfn(u_int16_t group_num)
97 return ((group_num & 0xff) % INSTANCE_BUCKETS);
100 static struct nfulnl_instance *
101 __instance_lookup(struct nfnl_log_net *log, u_int16_t group_num)
103 struct hlist_head *head;
104 struct nfulnl_instance *inst;
106 head = &log->instance_table[instance_hashfn(group_num)];
107 hlist_for_each_entry_rcu(inst, head, hlist) {
108 if (inst->group_num == group_num)
109 return inst;
111 return NULL;
114 static inline void
115 instance_get(struct nfulnl_instance *inst)
117 refcount_inc(&inst->use);
120 static struct nfulnl_instance *
121 instance_lookup_get(struct nfnl_log_net *log, u_int16_t group_num)
123 struct nfulnl_instance *inst;
125 rcu_read_lock_bh();
126 inst = __instance_lookup(log, group_num);
127 if (inst && !refcount_inc_not_zero(&inst->use))
128 inst = NULL;
129 rcu_read_unlock_bh();
131 return inst;
134 static void nfulnl_instance_free_rcu(struct rcu_head *head)
136 struct nfulnl_instance *inst =
137 container_of(head, struct nfulnl_instance, rcu);
139 put_net(inst->net);
140 kfree(inst);
141 module_put(THIS_MODULE);
144 static void
145 instance_put(struct nfulnl_instance *inst)
147 if (inst && refcount_dec_and_test(&inst->use))
148 call_rcu(&inst->rcu, nfulnl_instance_free_rcu);
151 static void nfulnl_timer(struct timer_list *t);
153 static struct nfulnl_instance *
154 instance_create(struct net *net, u_int16_t group_num,
155 u32 portid, struct user_namespace *user_ns)
157 struct nfulnl_instance *inst;
158 struct nfnl_log_net *log = nfnl_log_pernet(net);
159 int err;
161 spin_lock_bh(&log->instances_lock);
162 if (__instance_lookup(log, group_num)) {
163 err = -EEXIST;
164 goto out_unlock;
167 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
168 if (!inst) {
169 err = -ENOMEM;
170 goto out_unlock;
173 if (!try_module_get(THIS_MODULE)) {
174 kfree(inst);
175 err = -EAGAIN;
176 goto out_unlock;
179 INIT_HLIST_NODE(&inst->hlist);
180 spin_lock_init(&inst->lock);
181 /* needs to be two, since we _put() after creation */
182 refcount_set(&inst->use, 2);
184 timer_setup(&inst->timer, nfulnl_timer, 0);
186 inst->net = get_net(net);
187 inst->peer_user_ns = user_ns;
188 inst->peer_portid = portid;
189 inst->group_num = group_num;
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;
195 inst->copy_range = NFULNL_COPY_RANGE_MAX;
197 hlist_add_head_rcu(&inst->hlist,
198 &log->instance_table[instance_hashfn(group_num)]);
201 spin_unlock_bh(&log->instances_lock);
203 return inst;
205 out_unlock:
206 spin_unlock_bh(&log->instances_lock);
207 return ERR_PTR(err);
210 static void __nfulnl_flush(struct nfulnl_instance *inst);
212 /* called with BH disabled */
213 static void
214 __instance_destroy(struct nfulnl_instance *inst)
216 /* first pull it out of the global list */
217 hlist_del_rcu(&inst->hlist);
219 /* then flush all pending packets from skb */
221 spin_lock(&inst->lock);
223 /* lockless readers wont be able to use us */
224 inst->copy_mode = NFULNL_COPY_DISABLED;
226 if (inst->skb)
227 __nfulnl_flush(inst);
228 spin_unlock(&inst->lock);
230 /* and finally put the refcount */
231 instance_put(inst);
234 static inline void
235 instance_destroy(struct nfnl_log_net *log,
236 struct nfulnl_instance *inst)
238 spin_lock_bh(&log->instances_lock);
239 __instance_destroy(inst);
240 spin_unlock_bh(&log->instances_lock);
243 static int
244 nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
245 unsigned int range)
247 int status = 0;
249 spin_lock_bh(&inst->lock);
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;
258 case NFULNL_COPY_PACKET:
259 inst->copy_mode = mode;
260 if (range == 0)
261 range = NFULNL_COPY_RANGE_MAX;
262 inst->copy_range = min_t(unsigned int,
263 range, NFULNL_COPY_RANGE_MAX);
264 break;
266 default:
267 status = -EINVAL;
268 break;
271 spin_unlock_bh(&inst->lock);
273 return status;
276 static int
277 nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
279 int status;
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;
290 spin_unlock_bh(&inst->lock);
292 return status;
295 static void
296 nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
298 spin_lock_bh(&inst->lock);
299 inst->flushtimeout = timeout;
300 spin_unlock_bh(&inst->lock);
303 static void
304 nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
306 spin_lock_bh(&inst->lock);
307 inst->qthreshold = qthresh;
308 spin_unlock_bh(&inst->lock);
311 static int
312 nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
314 spin_lock_bh(&inst->lock);
315 inst->flags = flags;
316 spin_unlock_bh(&inst->lock);
318 return 0;
321 static struct sk_buff *
322 nfulnl_alloc_skb(struct net *net, u32 peer_portid, unsigned int inst_size,
323 unsigned int pkt_size)
325 struct sk_buff *skb;
326 unsigned int n;
328 /* alloc skb which should be big enough for a whole multipart
329 * message. WARNING: has to be <= 128k due to slab restrictions */
331 n = max(inst_size, pkt_size);
332 skb = alloc_skb(n, GFP_ATOMIC | __GFP_NOWARN);
333 if (!skb) {
334 if (n > pkt_size) {
335 /* try to allocate only as much as we need for current
336 * packet */
338 skb = alloc_skb(pkt_size, GFP_ATOMIC);
342 return skb;
345 static void
346 __nfulnl_send(struct nfulnl_instance *inst)
348 if (inst->qlen > 1) {
349 struct nlmsghdr *nlh = nlmsg_put(inst->skb, 0, 0,
350 NLMSG_DONE,
351 sizeof(struct nfgenmsg),
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);
356 goto out;
359 nfnetlink_unicast(inst->skb, inst->net, inst->peer_portid,
360 MSG_DONTWAIT);
361 out:
362 inst->qlen = 0;
363 inst->skb = NULL;
366 static void
367 __nfulnl_flush(struct nfulnl_instance *inst)
369 /* timer holds a reference */
370 if (del_timer(&inst->timer))
371 instance_put(inst);
372 if (inst->skb)
373 __nfulnl_send(inst);
376 static void
377 nfulnl_timer(struct timer_list *t)
379 struct nfulnl_instance *inst = from_timer(inst, t, timer);
381 spin_lock_bh(&inst->lock);
382 if (inst->skb)
383 __nfulnl_send(inst);
384 spin_unlock_bh(&inst->lock);
385 instance_put(inst);
388 /* This is an inline function, we don't really care about a long
389 * list of arguments */
390 static inline int
391 __build_packet_message(struct nfnl_log_net *log,
392 struct nfulnl_instance *inst,
393 const struct sk_buff *skb,
394 unsigned int data_len,
395 u_int8_t pf,
396 unsigned int hooknum,
397 const struct net_device *indev,
398 const struct net_device *outdev,
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)
403 struct nfulnl_msg_packet_hdr pmsg;
404 struct nlmsghdr *nlh;
405 struct nfgenmsg *nfmsg;
406 sk_buff_data_t old_tail = inst->skb->tail;
407 struct sock *sk;
408 const unsigned char *hwhdrp;
410 nlh = nlmsg_put(inst->skb, 0, 0,
411 nfnl_msg_type(NFNL_SUBSYS_ULOG, NFULNL_MSG_PACKET),
412 sizeof(struct nfgenmsg), 0);
413 if (!nlh)
414 return -1;
415 nfmsg = nlmsg_data(nlh);
416 nfmsg->nfgen_family = pf;
417 nfmsg->version = NFNETLINK_V0;
418 nfmsg->res_id = htons(inst->group_num);
420 memset(&pmsg, 0, sizeof(pmsg));
421 pmsg.hw_protocol = skb->protocol;
422 pmsg.hook = hooknum;
424 if (nla_put(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg))
425 goto nla_put_failure;
427 if (prefix &&
428 nla_put(inst->skb, NFULA_PREFIX, plen, prefix))
429 goto nla_put_failure;
431 if (indev) {
432 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
433 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
434 htonl(indev->ifindex)))
435 goto nla_put_failure;
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) */
441 if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
442 htonl(indev->ifindex)) ||
443 /* this is the bridge group "brX" */
444 /* rcu_read_lock()ed by nf_hook_thresh or
445 * nf_log_packet.
447 nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
448 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
449 goto nla_put_failure;
450 } else {
451 struct net_device *physindev;
453 /* Case 2: indev is bridge group, we need to look for
454 * physical device (when called from ipv4) */
455 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
456 htonl(indev->ifindex)))
457 goto nla_put_failure;
459 physindev = nf_bridge_get_physindev(skb);
460 if (physindev &&
461 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
462 htonl(physindev->ifindex)))
463 goto nla_put_failure;
465 #endif
468 if (outdev) {
469 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
470 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
471 htonl(outdev->ifindex)))
472 goto nla_put_failure;
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) */
478 if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
479 htonl(outdev->ifindex)) ||
480 /* this is the bridge group "brX" */
481 /* rcu_read_lock()ed by nf_hook_thresh or
482 * nf_log_packet.
484 nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
485 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
486 goto nla_put_failure;
487 } else {
488 struct net_device *physoutdev;
490 /* Case 2: indev is a bridge group, we need to look
491 * for physical device (when called from ipv4) */
492 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
493 htonl(outdev->ifindex)))
494 goto nla_put_failure;
496 physoutdev = nf_bridge_get_physoutdev(skb);
497 if (physoutdev &&
498 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
499 htonl(physoutdev->ifindex)))
500 goto nla_put_failure;
502 #endif
505 if (skb->mark &&
506 nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
507 goto nla_put_failure;
509 if (indev && skb->dev &&
510 skb->mac_header != skb->network_header) {
511 struct nfulnl_msg_packet_hw phw;
512 int len;
514 memset(&phw, 0, sizeof(phw));
515 len = dev_parse_header(skb, phw.hw_addr);
516 if (len > 0) {
517 phw.hw_addrlen = htons(len);
518 if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw))
519 goto nla_put_failure;
523 if (indev && skb_mac_header_was_set(skb)) {
524 if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
525 nla_put_be16(inst->skb, NFULA_HWLEN,
526 htons(skb->dev->hard_header_len)))
527 goto nla_put_failure;
529 hwhdrp = skb_mac_header(skb);
531 if (skb->dev->type == ARPHRD_SIT)
532 hwhdrp -= ETH_HLEN;
534 if (hwhdrp >= skb->head &&
535 nla_put(inst->skb, NFULA_HWHEADER,
536 skb->dev->hard_header_len, hwhdrp))
537 goto nla_put_failure;
540 if (hooknum <= NF_INET_FORWARD && skb->tstamp) {
541 struct nfulnl_msg_packet_timestamp ts;
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);
546 if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts))
547 goto nla_put_failure;
550 /* UID */
551 sk = skb->sk;
552 if (sk && sk_fullsock(sk)) {
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;
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));
560 read_unlock_bh(&sk->sk_callback_lock);
561 if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
562 nla_put_be32(inst->skb, NFULA_GID, gid))
563 goto nla_put_failure;
564 } else
565 read_unlock_bh(&sk->sk_callback_lock);
568 /* local sequence number */
569 if ((inst->flags & NFULNL_CFG_F_SEQ) &&
570 nla_put_be32(inst->skb, NFULA_SEQ, htonl(inst->seq++)))
571 goto nla_put_failure;
573 /* global sequence number */
574 if ((inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) &&
575 nla_put_be32(inst->skb, NFULA_SEQ_GLOBAL,
576 htonl(atomic_inc_return(&log->global_seq))))
577 goto nla_put_failure;
579 if (ct && nfnl_ct->build(inst->skb, ct, ctinfo,
580 NFULA_CT, NFULA_CT_INFO) < 0)
581 goto nla_put_failure;
583 if (data_len) {
584 struct nlattr *nla;
585 int size = nla_attr_size(data_len);
587 if (skb_tailroom(inst->skb) < nla_total_size(data_len))
588 goto nla_put_failure;
590 nla = skb_put(inst->skb, nla_total_size(data_len));
591 nla->nla_type = NFULA_PAYLOAD;
592 nla->nla_len = size;
594 if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
595 BUG();
598 nlh->nlmsg_len = inst->skb->tail - old_tail;
599 return 0;
601 nla_put_failure:
602 PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
603 return -1;
606 static const struct nf_loginfo default_loginfo = {
607 .type = NF_LOG_TYPE_ULOG,
608 .u = {
609 .ulog = {
610 .copy_len = 0xffff,
611 .group = 0,
612 .qthreshold = 1,
617 /* log handler for internal netfilter logging api */
618 static void
619 nfulnl_log_packet(struct net *net,
620 u_int8_t pf,
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)
628 size_t size;
629 unsigned int data_len;
630 struct nfulnl_instance *inst;
631 const struct nf_loginfo *li;
632 unsigned int qthreshold;
633 unsigned int plen = 0;
634 struct nfnl_log_net *log = nfnl_log_pernet(net);
635 const struct nfnl_ct_hook *nfnl_ct = NULL;
636 struct nf_conn *ct = NULL;
637 enum ip_conntrack_info uninitialized_var(ctinfo);
639 if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
640 li = li_user;
641 else
642 li = &default_loginfo;
644 inst = instance_lookup_get(log, li->u.ulog.group);
645 if (!inst)
646 return;
648 if (prefix)
649 plen = strlen(prefix) + 1;
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... */
654 size = nlmsg_total_size(sizeof(struct nfgenmsg))
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 */
658 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
659 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
660 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
661 #endif
662 + nla_total_size(sizeof(u_int32_t)) /* mark */
663 + nla_total_size(sizeof(u_int32_t)) /* uid */
664 + nla_total_size(sizeof(u_int32_t)) /* gid */
665 + nla_total_size(plen) /* prefix */
666 + nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
667 + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp))
668 + nla_total_size(sizeof(struct nfgenmsg)); /* NLMSG_DONE */
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 */
676 spin_lock_bh(&inst->lock);
678 if (inst->flags & NFULNL_CFG_F_SEQ)
679 size += nla_total_size(sizeof(u_int32_t));
680 if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
681 size += nla_total_size(sizeof(u_int32_t));
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);
691 qthreshold = inst->qthreshold;
692 /* per-rule qthreshold overrides per-instance */
693 if (li->u.ulog.qthreshold)
694 if (qthreshold > li->u.ulog.qthreshold)
695 qthreshold = li->u.ulog.qthreshold;
698 switch (inst->copy_mode) {
699 case NFULNL_COPY_META:
700 case NFULNL_COPY_NONE:
701 data_len = 0;
702 break;
704 case NFULNL_COPY_PACKET:
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;
710 if (data_len > skb->len)
711 data_len = skb->len;
713 size += nla_total_size(data_len);
714 break;
716 case NFULNL_COPY_DISABLED:
717 default:
718 goto unlock_and_release;
721 if (inst->skb && size > skb_tailroom(inst->skb)) {
722 /* either the queue len is too high or we don't have
723 * enough room in the skb left. flush to userspace. */
724 __nfulnl_flush(inst);
727 if (!inst->skb) {
728 inst->skb = nfulnl_alloc_skb(net, inst->peer_portid,
729 inst->nlbufsiz, size);
730 if (!inst->skb)
731 goto alloc_failure;
734 inst->qlen++;
736 __build_packet_message(log, inst, skb, data_len, pf,
737 hooknum, in, out, prefix, plen,
738 nfnl_ct, ct, ctinfo);
740 if (inst->qlen >= qthreshold)
741 __nfulnl_flush(inst);
742 /* timer_pending always called within inst->lock, so there
743 * is no chance of a race here */
744 else if (!timer_pending(&inst->timer)) {
745 instance_get(inst);
746 inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
747 add_timer(&inst->timer);
750 unlock_and_release:
751 spin_unlock_bh(&inst->lock);
752 instance_put(inst);
753 return;
755 alloc_failure:
756 /* FIXME: statistics */
757 goto unlock_and_release;
760 static int
761 nfulnl_rcv_nl_event(struct notifier_block *this,
762 unsigned long event, void *ptr)
764 struct netlink_notify *n = ptr;
765 struct nfnl_log_net *log = nfnl_log_pernet(n->net);
767 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
768 int i;
770 /* destroy all instances for this portid */
771 spin_lock_bh(&log->instances_lock);
772 for (i = 0; i < INSTANCE_BUCKETS; i++) {
773 struct hlist_node *t2;
774 struct nfulnl_instance *inst;
775 struct hlist_head *head = &log->instance_table[i];
777 hlist_for_each_entry_safe(inst, t2, head, hlist) {
778 if (n->portid == inst->peer_portid)
779 __instance_destroy(inst);
782 spin_unlock_bh(&log->instances_lock);
784 return NOTIFY_DONE;
787 static struct notifier_block nfulnl_rtnl_notifier = {
788 .notifier_call = nfulnl_rcv_nl_event,
791 static int nfulnl_recv_unsupp(struct net *net, struct sock *ctnl,
792 struct sk_buff *skb, const struct nlmsghdr *nlh,
793 const struct nlattr * const nfqa[],
794 struct netlink_ext_ack *extack)
796 return -ENOTSUPP;
799 static struct nf_logger nfulnl_logger __read_mostly = {
800 .name = "nfnetlink_log",
801 .type = NF_LOG_TYPE_ULOG,
802 .logfn = nfulnl_log_packet,
803 .me = THIS_MODULE,
806 static 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 },
815 static int nfulnl_recv_config(struct net *net, struct sock *ctnl,
816 struct sk_buff *skb, const struct nlmsghdr *nlh,
817 const struct nlattr * const nfula[],
818 struct netlink_ext_ack *extack)
820 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
821 u_int16_t group_num = ntohs(nfmsg->res_id);
822 struct nfulnl_instance *inst;
823 struct nfulnl_msg_config_cmd *cmd = NULL;
824 struct nfnl_log_net *log = nfnl_log_pernet(net);
825 int ret = 0;
826 u16 flags = 0;
828 if (nfula[NFULA_CFG_CMD]) {
829 u_int8_t pf = nfmsg->nfgen_family;
830 cmd = nla_data(nfula[NFULA_CFG_CMD]);
832 /* Commands without queue context */
833 switch (cmd->command) {
834 case NFULNL_CFG_CMD_PF_BIND:
835 return nf_log_bind_pf(net, pf, &nfulnl_logger);
836 case NFULNL_CFG_CMD_PF_UNBIND:
837 nf_log_unbind_pf(net, pf);
838 return 0;
842 inst = instance_lookup_get(log, group_num);
843 if (inst && inst->peer_portid != NETLINK_CB(skb).portid) {
844 ret = -EPERM;
845 goto out_put;
848 /* Check if we support these flags in first place, dependencies should
849 * be there too not to break atomicity.
851 if (nfula[NFULA_CFG_FLAGS]) {
852 flags = ntohs(nla_get_be16(nfula[NFULA_CFG_FLAGS]));
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;
864 #endif
865 ret = -EOPNOTSUPP;
866 goto out_put;
870 if (cmd != NULL) {
871 switch (cmd->command) {
872 case NFULNL_CFG_CMD_BIND:
873 if (inst) {
874 ret = -EBUSY;
875 goto out_put;
878 inst = instance_create(net, group_num,
879 NETLINK_CB(skb).portid,
880 sk_user_ns(NETLINK_CB(skb).sk));
881 if (IS_ERR(inst)) {
882 ret = PTR_ERR(inst);
883 goto out;
885 break;
886 case NFULNL_CFG_CMD_UNBIND:
887 if (!inst) {
888 ret = -ENODEV;
889 goto out;
892 instance_destroy(log, inst);
893 goto out_put;
894 default:
895 ret = -ENOTSUPP;
896 goto out_put;
898 } else if (!inst) {
899 ret = -ENODEV;
900 goto out;
903 if (nfula[NFULA_CFG_MODE]) {
904 struct nfulnl_msg_config_mode *params =
905 nla_data(nfula[NFULA_CFG_MODE]);
907 nfulnl_set_mode(inst, params->copy_mode,
908 ntohl(params->copy_range));
911 if (nfula[NFULA_CFG_TIMEOUT]) {
912 __be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
914 nfulnl_set_timeout(inst, ntohl(timeout));
917 if (nfula[NFULA_CFG_NLBUFSIZ]) {
918 __be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
920 nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
923 if (nfula[NFULA_CFG_QTHRESH]) {
924 __be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
926 nfulnl_set_qthresh(inst, ntohl(qthresh));
929 if (nfula[NFULA_CFG_FLAGS])
930 nfulnl_set_flags(inst, flags);
932 out_put:
933 instance_put(inst);
934 out:
935 return ret;
938 static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
939 [NFULNL_MSG_PACKET] = { .call = nfulnl_recv_unsupp,
940 .attr_count = NFULA_MAX, },
941 [NFULNL_MSG_CONFIG] = { .call = nfulnl_recv_config,
942 .attr_count = NFULA_CFG_MAX,
943 .policy = nfula_cfg_policy },
946 static const struct nfnetlink_subsystem nfulnl_subsys = {
947 .name = "log",
948 .subsys_id = NFNL_SUBSYS_ULOG,
949 .cb_count = NFULNL_MSG_MAX,
950 .cb = nfulnl_cb,
953 #ifdef CONFIG_PROC_FS
954 struct iter_state {
955 struct seq_net_private p;
956 unsigned int bucket;
959 static struct hlist_node *get_first(struct net *net, struct iter_state *st)
961 struct nfnl_log_net *log;
962 if (!st)
963 return NULL;
965 log = nfnl_log_pernet(net);
967 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
968 struct hlist_head *head = &log->instance_table[st->bucket];
970 if (!hlist_empty(head))
971 return rcu_dereference_bh(hlist_first_rcu(head));
973 return NULL;
976 static struct hlist_node *get_next(struct net *net, struct iter_state *st,
977 struct hlist_node *h)
979 h = rcu_dereference_bh(hlist_next_rcu(h));
980 while (!h) {
981 struct nfnl_log_net *log;
982 struct hlist_head *head;
984 if (++st->bucket >= INSTANCE_BUCKETS)
985 return NULL;
987 log = nfnl_log_pernet(net);
988 head = &log->instance_table[st->bucket];
989 h = rcu_dereference_bh(hlist_first_rcu(head));
991 return h;
994 static struct hlist_node *get_idx(struct net *net, struct iter_state *st,
995 loff_t pos)
997 struct hlist_node *head;
998 head = get_first(net, st);
1000 if (head)
1001 while (pos && (head = get_next(net, st, head)))
1002 pos--;
1003 return pos ? NULL : head;
1006 static void *seq_start(struct seq_file *s, loff_t *pos)
1007 __acquires(rcu_bh)
1009 rcu_read_lock_bh();
1010 return get_idx(seq_file_net(s), s->private, *pos);
1013 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1015 (*pos)++;
1016 return get_next(seq_file_net(s), s->private, v);
1019 static void seq_stop(struct seq_file *s, void *v)
1020 __releases(rcu_bh)
1022 rcu_read_unlock_bh();
1025 static int seq_show(struct seq_file *s, void *v)
1027 const struct nfulnl_instance *inst = v;
1029 seq_printf(s, "%5u %6u %5u %1u %5u %6u %2u\n",
1030 inst->group_num,
1031 inst->peer_portid, inst->qlen,
1032 inst->copy_mode, inst->copy_range,
1033 inst->flushtimeout, refcount_read(&inst->use));
1035 return 0;
1038 static const struct seq_operations nful_seq_ops = {
1039 .start = seq_start,
1040 .next = seq_next,
1041 .stop = seq_stop,
1042 .show = seq_show,
1044 #endif /* PROC_FS */
1046 static int __net_init nfnl_log_net_init(struct net *net)
1048 unsigned int i;
1049 struct nfnl_log_net *log = nfnl_log_pernet(net);
1050 #ifdef CONFIG_PROC_FS
1051 struct proc_dir_entry *proc;
1052 kuid_t root_uid;
1053 kgid_t root_gid;
1054 #endif
1056 for (i = 0; i < INSTANCE_BUCKETS; i++)
1057 INIT_HLIST_HEAD(&log->instance_table[i]);
1058 spin_lock_init(&log->instances_lock);
1060 #ifdef CONFIG_PROC_FS
1061 proc = proc_create_net("nfnetlink_log", 0440, net->nf.proc_netfilter,
1062 &nful_seq_ops, sizeof(struct iter_state));
1063 if (!proc)
1064 return -ENOMEM;
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);
1070 #endif
1071 return 0;
1074 static void __net_exit nfnl_log_net_exit(struct net *net)
1076 struct nfnl_log_net *log = nfnl_log_pernet(net);
1077 unsigned int i;
1079 #ifdef CONFIG_PROC_FS
1080 remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter);
1081 #endif
1082 nf_log_unset(net, &nfulnl_logger);
1083 for (i = 0; i < INSTANCE_BUCKETS; i++)
1084 WARN_ON_ONCE(!hlist_empty(&log->instance_table[i]));
1087 static 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),
1094 static int __init nfnetlink_log_init(void)
1096 int status;
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;
1104 netlink_register_notifier(&nfulnl_rtnl_notifier);
1105 status = nfnetlink_subsys_register(&nfulnl_subsys);
1106 if (status < 0) {
1107 pr_err("failed to create netlink socket\n");
1108 goto cleanup_netlink_notifier;
1111 status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
1112 if (status < 0) {
1113 pr_err("failed to register logger\n");
1114 goto cleanup_subsys;
1117 return status;
1119 cleanup_subsys:
1120 nfnetlink_subsys_unregister(&nfulnl_subsys);
1121 cleanup_netlink_notifier:
1122 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
1123 unregister_pernet_subsys(&nfnl_log_net_ops);
1124 out:
1125 return status;
1128 static void __exit nfnetlink_log_fini(void)
1130 nfnetlink_subsys_unregister(&nfulnl_subsys);
1131 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
1132 unregister_pernet_subsys(&nfnl_log_net_ops);
1133 nf_log_unregister(&nfulnl_logger);
1136 MODULE_DESCRIPTION("netfilter userspace logging");
1137 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1138 MODULE_LICENSE("GPL");
1139 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
1140 MODULE_ALIAS_NF_LOGGER(AF_INET, 1);
1141 MODULE_ALIAS_NF_LOGGER(AF_INET6, 1);
1142 MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 1);
1143 MODULE_ALIAS_NF_LOGGER(3, 1); /* NFPROTO_ARP */
1144 MODULE_ALIAS_NF_LOGGER(5, 1); /* NFPROTO_NETDEV */
1146 module_init(nfnetlink_log_init);
1147 module_exit(nfnetlink_log_fini);