1 // SPDX-License-Identifier: GPL-2.0-only
3 * This is a module which is used for logging packets to userspace via
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>
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>
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"
46 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
47 #include <net/netfilter/nf_conntrack.h>
50 #define NFULNL_COPY_DISABLED 0xff
51 #define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
52 #define NFULNL_TIMEOUT_DEFAULT 100 /* every second */
53 #define NFULNL_QTHRESH_DEFAULT 100 /* 100 packets */
54 /* max packet size is limited by 16-bit struct nfattr nfa_len field */
55 #define NFULNL_COPY_RANGE_MAX (0xFFFF - NLA_HDRLEN)
57 #define PRINTR(x, args...) do { if (net_ratelimit()) \
58 printk(x, ## args); } while (0);
60 struct nfulnl_instance {
61 struct hlist_node hlist; /* global list of instances */
63 refcount_t use; /* use count */
65 unsigned int qlen; /* number of nlmsgs in skb */
66 struct sk_buff *skb; /* pre-allocatd skb */
67 struct timer_list timer;
69 netns_tracker ns_tracker;
70 struct user_namespace *peer_user_ns; /* User namespace of the peer process */
71 u32 peer_portid; /* PORTID of the peer process */
73 /* configurable parameters */
74 unsigned int flushtimeout; /* timeout until queue flush */
75 unsigned int nlbufsiz; /* netlink buffer allocation size */
76 unsigned int qthreshold; /* threshold of the queue */
78 u_int32_t seq; /* instance-local sequential counter */
79 u_int16_t group_num; /* number of this queue */
85 #define INSTANCE_BUCKETS 16
87 static unsigned int nfnl_log_net_id __read_mostly;
90 spinlock_t instances_lock;
91 struct hlist_head instance_table[INSTANCE_BUCKETS];
95 static struct nfnl_log_net *nfnl_log_pernet(struct net *net)
97 return net_generic(net, nfnl_log_net_id);
100 static inline u_int8_t instance_hashfn(u_int16_t group_num)
102 return ((group_num & 0xff) % INSTANCE_BUCKETS);
105 static struct nfulnl_instance *
106 __instance_lookup(struct nfnl_log_net *log, u_int16_t group_num)
108 struct hlist_head *head;
109 struct nfulnl_instance *inst;
111 head = &log->instance_table[instance_hashfn(group_num)];
112 hlist_for_each_entry_rcu(inst, head, hlist) {
113 if (inst->group_num == group_num)
120 instance_get(struct nfulnl_instance *inst)
122 refcount_inc(&inst->use);
125 static struct nfulnl_instance *
126 instance_lookup_get(struct nfnl_log_net *log, u_int16_t group_num)
128 struct nfulnl_instance *inst;
131 inst = __instance_lookup(log, group_num);
132 if (inst && !refcount_inc_not_zero(&inst->use))
134 rcu_read_unlock_bh();
139 static void nfulnl_instance_free_rcu(struct rcu_head *head)
141 struct nfulnl_instance *inst =
142 container_of(head, struct nfulnl_instance, rcu);
144 put_net_track(inst->net, &inst->ns_tracker);
146 module_put(THIS_MODULE);
150 instance_put(struct nfulnl_instance *inst)
152 if (inst && refcount_dec_and_test(&inst->use))
153 call_rcu(&inst->rcu, nfulnl_instance_free_rcu);
156 static void nfulnl_timer(struct timer_list *t);
158 static struct nfulnl_instance *
159 instance_create(struct net *net, u_int16_t group_num,
160 u32 portid, struct user_namespace *user_ns)
162 struct nfulnl_instance *inst;
163 struct nfnl_log_net *log = nfnl_log_pernet(net);
166 spin_lock_bh(&log->instances_lock);
167 if (__instance_lookup(log, group_num)) {
172 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
178 if (!try_module_get(THIS_MODULE)) {
184 INIT_HLIST_NODE(&inst->hlist);
185 spin_lock_init(&inst->lock);
186 /* needs to be two, since we _put() after creation */
187 refcount_set(&inst->use, 2);
189 timer_setup(&inst->timer, nfulnl_timer, 0);
191 inst->net = get_net_track(net, &inst->ns_tracker, GFP_ATOMIC);
192 inst->peer_user_ns = user_ns;
193 inst->peer_portid = portid;
194 inst->group_num = group_num;
196 inst->qthreshold = NFULNL_QTHRESH_DEFAULT;
197 inst->flushtimeout = NFULNL_TIMEOUT_DEFAULT;
198 inst->nlbufsiz = NFULNL_NLBUFSIZ_DEFAULT;
199 inst->copy_mode = NFULNL_COPY_PACKET;
200 inst->copy_range = NFULNL_COPY_RANGE_MAX;
202 hlist_add_head_rcu(&inst->hlist,
203 &log->instance_table[instance_hashfn(group_num)]);
206 spin_unlock_bh(&log->instances_lock);
211 spin_unlock_bh(&log->instances_lock);
215 static void __nfulnl_flush(struct nfulnl_instance *inst);
217 /* called with BH disabled */
219 __instance_destroy(struct nfulnl_instance *inst)
221 /* first pull it out of the global list */
222 hlist_del_rcu(&inst->hlist);
224 /* then flush all pending packets from skb */
226 spin_lock(&inst->lock);
228 /* lockless readers wont be able to use us */
229 inst->copy_mode = NFULNL_COPY_DISABLED;
232 __nfulnl_flush(inst);
233 spin_unlock(&inst->lock);
235 /* and finally put the refcount */
240 instance_destroy(struct nfnl_log_net *log,
241 struct nfulnl_instance *inst)
243 spin_lock_bh(&log->instances_lock);
244 __instance_destroy(inst);
245 spin_unlock_bh(&log->instances_lock);
249 nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
254 spin_lock_bh(&inst->lock);
257 case NFULNL_COPY_NONE:
258 case NFULNL_COPY_META:
259 inst->copy_mode = mode;
260 inst->copy_range = 0;
263 case NFULNL_COPY_PACKET:
264 inst->copy_mode = mode;
266 range = NFULNL_COPY_RANGE_MAX;
267 inst->copy_range = min_t(unsigned int,
268 range, NFULNL_COPY_RANGE_MAX);
276 spin_unlock_bh(&inst->lock);
282 nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
286 spin_lock_bh(&inst->lock);
287 if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
289 else if (nlbufsiz > 131072)
292 inst->nlbufsiz = nlbufsiz;
295 spin_unlock_bh(&inst->lock);
301 nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
303 spin_lock_bh(&inst->lock);
304 inst->flushtimeout = timeout;
305 spin_unlock_bh(&inst->lock);
309 nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
311 spin_lock_bh(&inst->lock);
312 inst->qthreshold = qthresh;
313 spin_unlock_bh(&inst->lock);
317 nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
319 spin_lock_bh(&inst->lock);
321 spin_unlock_bh(&inst->lock);
326 static struct sk_buff *
327 nfulnl_alloc_skb(struct net *net, u32 peer_portid, unsigned int inst_size,
328 unsigned int pkt_size)
333 /* alloc skb which should be big enough for a whole multipart
334 * message. WARNING: has to be <= 128k due to slab restrictions */
336 n = max(inst_size, pkt_size);
337 skb = alloc_skb(n, GFP_ATOMIC | __GFP_NOWARN);
340 /* try to allocate only as much as we need for current
343 skb = alloc_skb(pkt_size, GFP_ATOMIC);
351 __nfulnl_send(struct nfulnl_instance *inst)
353 if (inst->qlen > 1) {
354 struct nlmsghdr *nlh = nlmsg_put(inst->skb, 0, 0,
356 sizeof(struct nfgenmsg),
358 if (WARN_ONCE(!nlh, "bad nlskb size: %u, tailroom %d\n",
359 inst->skb->len, skb_tailroom(inst->skb))) {
360 kfree_skb(inst->skb);
364 nfnetlink_unicast(inst->skb, inst->net, inst->peer_portid);
371 __nfulnl_flush(struct nfulnl_instance *inst)
373 /* timer holds a reference */
374 if (del_timer(&inst->timer))
381 nfulnl_timer(struct timer_list *t)
383 struct nfulnl_instance *inst = from_timer(inst, t, timer);
385 spin_lock_bh(&inst->lock);
388 spin_unlock_bh(&inst->lock);
392 static u32 nfulnl_get_bridge_size(const struct sk_buff *skb)
396 if (!skb_mac_header_was_set(skb))
399 if (skb_vlan_tag_present(skb)) {
400 size += nla_total_size(0); /* nested */
401 size += nla_total_size(sizeof(u16)); /* id */
402 size += nla_total_size(sizeof(u16)); /* tag */
405 if (skb->network_header > skb->mac_header)
406 size += nla_total_size(skb->network_header - skb->mac_header);
411 static int nfulnl_put_bridge(struct nfulnl_instance *inst, const struct sk_buff *skb)
413 if (!skb_mac_header_was_set(skb))
416 if (skb_vlan_tag_present(skb)) {
419 nest = nla_nest_start(inst->skb, NFULA_VLAN);
421 goto nla_put_failure;
423 if (nla_put_be16(inst->skb, NFULA_VLAN_TCI, htons(skb->vlan_tci)) ||
424 nla_put_be16(inst->skb, NFULA_VLAN_PROTO, skb->vlan_proto))
425 goto nla_put_failure;
427 nla_nest_end(inst->skb, nest);
430 if (skb->mac_header < skb->network_header) {
431 int len = (int)(skb->network_header - skb->mac_header);
433 if (nla_put(inst->skb, NFULA_L2HDR, len, skb_mac_header(skb)))
434 goto nla_put_failure;
443 /* This is an inline function, we don't really care about a long
444 * list of arguments */
446 __build_packet_message(struct nfnl_log_net *log,
447 struct nfulnl_instance *inst,
448 const struct sk_buff *skb,
449 unsigned int data_len,
451 unsigned int hooknum,
452 const struct net_device *indev,
453 const struct net_device *outdev,
454 const char *prefix, unsigned int plen,
455 const struct nfnl_ct_hook *nfnl_ct,
456 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
458 struct nfulnl_msg_packet_hdr pmsg;
459 struct nlmsghdr *nlh;
460 sk_buff_data_t old_tail = inst->skb->tail;
462 const unsigned char *hwhdrp;
464 nlh = nfnl_msg_put(inst->skb, 0, 0,
465 nfnl_msg_type(NFNL_SUBSYS_ULOG, NFULNL_MSG_PACKET),
466 0, pf, NFNETLINK_V0, htons(inst->group_num));
470 memset(&pmsg, 0, sizeof(pmsg));
471 pmsg.hw_protocol = skb->protocol;
474 if (nla_put(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg))
475 goto nla_put_failure;
478 nla_put(inst->skb, NFULA_PREFIX, plen, prefix))
479 goto nla_put_failure;
482 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
483 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
484 htonl(indev->ifindex)))
485 goto nla_put_failure;
487 if (pf == PF_BRIDGE) {
488 /* Case 1: outdev is physical input device, we need to
489 * look for bridge group (when called from
490 * netfilter_bridge) */
491 if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
492 htonl(indev->ifindex)) ||
493 /* this is the bridge group "brX" */
494 /* rcu_read_lock()ed by nf_hook_thresh or
497 nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
498 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
499 goto nla_put_failure;
501 struct net_device *physindev;
503 /* Case 2: indev is bridge group, we need to look for
504 * physical device (when called from ipv4) */
505 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
506 htonl(indev->ifindex)))
507 goto nla_put_failure;
509 physindev = nf_bridge_get_physindev(skb);
511 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
512 htonl(physindev->ifindex)))
513 goto nla_put_failure;
519 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
520 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
521 htonl(outdev->ifindex)))
522 goto nla_put_failure;
524 if (pf == PF_BRIDGE) {
525 /* Case 1: outdev is physical output device, we need to
526 * look for bridge group (when called from
527 * netfilter_bridge) */
528 if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
529 htonl(outdev->ifindex)) ||
530 /* this is the bridge group "brX" */
531 /* rcu_read_lock()ed by nf_hook_thresh or
534 nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
535 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
536 goto nla_put_failure;
538 struct net_device *physoutdev;
540 /* Case 2: indev is a bridge group, we need to look
541 * for physical device (when called from ipv4) */
542 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
543 htonl(outdev->ifindex)))
544 goto nla_put_failure;
546 physoutdev = nf_bridge_get_physoutdev(skb);
548 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
549 htonl(physoutdev->ifindex)))
550 goto nla_put_failure;
556 nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
557 goto nla_put_failure;
559 if (indev && skb->dev &&
560 skb_mac_header_was_set(skb) &&
561 skb_mac_header_len(skb) != 0) {
562 struct nfulnl_msg_packet_hw phw;
565 memset(&phw, 0, sizeof(phw));
566 len = dev_parse_header(skb, phw.hw_addr);
568 phw.hw_addrlen = htons(len);
569 if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw))
570 goto nla_put_failure;
574 if (indev && skb_mac_header_was_set(skb)) {
575 if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
576 nla_put_be16(inst->skb, NFULA_HWLEN,
577 htons(skb->dev->hard_header_len)))
578 goto nla_put_failure;
580 hwhdrp = skb_mac_header(skb);
582 if (skb->dev->type == ARPHRD_SIT)
585 if (hwhdrp >= skb->head &&
586 nla_put(inst->skb, NFULA_HWHEADER,
587 skb->dev->hard_header_len, hwhdrp))
588 goto nla_put_failure;
591 if (hooknum <= NF_INET_FORWARD && skb->tstamp) {
592 struct nfulnl_msg_packet_timestamp ts;
593 struct timespec64 kts = ktime_to_timespec64(skb->tstamp);
594 ts.sec = cpu_to_be64(kts.tv_sec);
595 ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC);
597 if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts))
598 goto nla_put_failure;
603 if (sk && sk_fullsock(sk)) {
604 read_lock_bh(&sk->sk_callback_lock);
605 if (sk->sk_socket && sk->sk_socket->file) {
606 struct file *file = sk->sk_socket->file;
607 const struct cred *cred = file->f_cred;
608 struct user_namespace *user_ns = inst->peer_user_ns;
609 __be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
610 __be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
611 read_unlock_bh(&sk->sk_callback_lock);
612 if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
613 nla_put_be32(inst->skb, NFULA_GID, gid))
614 goto nla_put_failure;
616 read_unlock_bh(&sk->sk_callback_lock);
619 /* local sequence number */
620 if ((inst->flags & NFULNL_CFG_F_SEQ) &&
621 nla_put_be32(inst->skb, NFULA_SEQ, htonl(inst->seq++)))
622 goto nla_put_failure;
624 /* global sequence number */
625 if ((inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) &&
626 nla_put_be32(inst->skb, NFULA_SEQ_GLOBAL,
627 htonl(atomic_inc_return(&log->global_seq))))
628 goto nla_put_failure;
630 if (ct && nfnl_ct->build(inst->skb, ct, ctinfo,
631 NFULA_CT, NFULA_CT_INFO) < 0)
632 goto nla_put_failure;
634 if ((pf == NFPROTO_NETDEV || pf == NFPROTO_BRIDGE) &&
635 nfulnl_put_bridge(inst, skb) < 0)
636 goto nla_put_failure;
640 int size = nla_attr_size(data_len);
642 if (skb_tailroom(inst->skb) < nla_total_size(data_len))
643 goto nla_put_failure;
645 nla = skb_put(inst->skb, nla_total_size(data_len));
646 nla->nla_type = NFULA_PAYLOAD;
649 if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
653 nlh->nlmsg_len = inst->skb->tail - old_tail;
657 PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
661 static const struct nf_loginfo default_loginfo = {
662 .type = NF_LOG_TYPE_ULOG,
672 /* log handler for internal netfilter logging api */
674 nfulnl_log_packet(struct net *net,
676 unsigned int hooknum,
677 const struct sk_buff *skb,
678 const struct net_device *in,
679 const struct net_device *out,
680 const struct nf_loginfo *li_user,
684 unsigned int data_len;
685 struct nfulnl_instance *inst;
686 const struct nf_loginfo *li;
687 unsigned int qthreshold;
688 unsigned int plen = 0;
689 struct nfnl_log_net *log = nfnl_log_pernet(net);
690 const struct nfnl_ct_hook *nfnl_ct = NULL;
691 struct nf_conn *ct = NULL;
692 enum ip_conntrack_info ctinfo;
694 if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
697 li = &default_loginfo;
699 inst = instance_lookup_get(log, li->u.ulog.group);
704 plen = strlen(prefix) + 1;
706 /* FIXME: do we want to make the size calculation conditional based on
707 * what is actually present? way more branches and checks, but more
708 * memory efficient... */
709 size = nlmsg_total_size(sizeof(struct nfgenmsg))
710 + nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
711 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
712 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
713 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
714 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
715 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
717 + nla_total_size(sizeof(u_int32_t)) /* mark */
718 + nla_total_size(sizeof(u_int32_t)) /* uid */
719 + nla_total_size(sizeof(u_int32_t)) /* gid */
720 + nla_total_size(plen) /* prefix */
721 + nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
722 + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp))
723 + nla_total_size(sizeof(struct nfgenmsg)); /* NLMSG_DONE */
725 if (in && skb_mac_header_was_set(skb)) {
726 size += nla_total_size(skb->dev->hard_header_len)
727 + nla_total_size(sizeof(u_int16_t)) /* hwtype */
728 + nla_total_size(sizeof(u_int16_t)); /* hwlen */
731 spin_lock_bh(&inst->lock);
733 if (inst->flags & NFULNL_CFG_F_SEQ)
734 size += nla_total_size(sizeof(u_int32_t));
735 if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
736 size += nla_total_size(sizeof(u_int32_t));
737 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
738 if (inst->flags & NFULNL_CFG_F_CONNTRACK) {
739 nfnl_ct = rcu_dereference(nfnl_ct_hook);
740 if (nfnl_ct != NULL) {
741 ct = nf_ct_get(skb, &ctinfo);
743 size += nfnl_ct->build_size(ct);
747 if (pf == NFPROTO_NETDEV || pf == NFPROTO_BRIDGE)
748 size += nfulnl_get_bridge_size(skb);
750 qthreshold = inst->qthreshold;
751 /* per-rule qthreshold overrides per-instance */
752 if (li->u.ulog.qthreshold)
753 if (qthreshold > li->u.ulog.qthreshold)
754 qthreshold = li->u.ulog.qthreshold;
757 switch (inst->copy_mode) {
758 case NFULNL_COPY_META:
759 case NFULNL_COPY_NONE:
763 case NFULNL_COPY_PACKET:
764 data_len = inst->copy_range;
765 if ((li->u.ulog.flags & NF_LOG_F_COPY_LEN) &&
766 (li->u.ulog.copy_len < data_len))
767 data_len = li->u.ulog.copy_len;
769 if (data_len > skb->len)
772 size += nla_total_size(data_len);
775 case NFULNL_COPY_DISABLED:
777 goto unlock_and_release;
780 if (inst->skb && size > skb_tailroom(inst->skb)) {
781 /* either the queue len is too high or we don't have
782 * enough room in the skb left. flush to userspace. */
783 __nfulnl_flush(inst);
787 inst->skb = nfulnl_alloc_skb(net, inst->peer_portid,
788 inst->nlbufsiz, size);
795 __build_packet_message(log, inst, skb, data_len, pf,
796 hooknum, in, out, prefix, plen,
797 nfnl_ct, ct, ctinfo);
799 if (inst->qlen >= qthreshold)
800 __nfulnl_flush(inst);
801 /* timer_pending always called within inst->lock, so there
802 * is no chance of a race here */
803 else if (!timer_pending(&inst->timer)) {
805 inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
806 add_timer(&inst->timer);
810 spin_unlock_bh(&inst->lock);
815 /* FIXME: statistics */
816 goto unlock_and_release;
820 nfulnl_rcv_nl_event(struct notifier_block *this,
821 unsigned long event, void *ptr)
823 struct netlink_notify *n = ptr;
824 struct nfnl_log_net *log = nfnl_log_pernet(n->net);
826 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
829 /* destroy all instances for this portid */
830 spin_lock_bh(&log->instances_lock);
831 for (i = 0; i < INSTANCE_BUCKETS; i++) {
832 struct hlist_node *t2;
833 struct nfulnl_instance *inst;
834 struct hlist_head *head = &log->instance_table[i];
836 hlist_for_each_entry_safe(inst, t2, head, hlist) {
837 if (n->portid == inst->peer_portid)
838 __instance_destroy(inst);
841 spin_unlock_bh(&log->instances_lock);
846 static struct notifier_block nfulnl_rtnl_notifier = {
847 .notifier_call = nfulnl_rcv_nl_event,
850 static int nfulnl_recv_unsupp(struct sk_buff *skb, const struct nfnl_info *info,
851 const struct nlattr * const nfula[])
856 static struct nf_logger nfulnl_logger __read_mostly = {
857 .name = "nfnetlink_log",
858 .type = NF_LOG_TYPE_ULOG,
859 .logfn = nfulnl_log_packet,
863 static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
864 [NFULA_CFG_CMD] = { .len = sizeof(struct nfulnl_msg_config_cmd) },
865 [NFULA_CFG_MODE] = { .len = sizeof(struct nfulnl_msg_config_mode) },
866 [NFULA_CFG_TIMEOUT] = { .type = NLA_U32 },
867 [NFULA_CFG_QTHRESH] = { .type = NLA_U32 },
868 [NFULA_CFG_NLBUFSIZ] = { .type = NLA_U32 },
869 [NFULA_CFG_FLAGS] = { .type = NLA_U16 },
872 static int nfulnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,
873 const struct nlattr * const nfula[])
875 struct nfnl_log_net *log = nfnl_log_pernet(info->net);
876 u_int16_t group_num = ntohs(info->nfmsg->res_id);
877 struct nfulnl_msg_config_cmd *cmd = NULL;
878 struct nfulnl_instance *inst;
882 if (nfula[NFULA_CFG_CMD]) {
883 u_int8_t pf = info->nfmsg->nfgen_family;
884 cmd = nla_data(nfula[NFULA_CFG_CMD]);
886 /* Commands without queue context */
887 switch (cmd->command) {
888 case NFULNL_CFG_CMD_PF_BIND:
889 return nf_log_bind_pf(info->net, pf, &nfulnl_logger);
890 case NFULNL_CFG_CMD_PF_UNBIND:
891 nf_log_unbind_pf(info->net, pf);
896 inst = instance_lookup_get(log, group_num);
897 if (inst && inst->peer_portid != NETLINK_CB(skb).portid) {
902 /* Check if we support these flags in first place, dependencies should
903 * be there too not to break atomicity.
905 if (nfula[NFULA_CFG_FLAGS]) {
906 flags = ntohs(nla_get_be16(nfula[NFULA_CFG_FLAGS]));
908 if ((flags & NFULNL_CFG_F_CONNTRACK) &&
909 !rcu_access_pointer(nfnl_ct_hook)) {
910 #ifdef CONFIG_MODULES
911 nfnl_unlock(NFNL_SUBSYS_ULOG);
912 request_module("ip_conntrack_netlink");
913 nfnl_lock(NFNL_SUBSYS_ULOG);
914 if (rcu_access_pointer(nfnl_ct_hook)) {
925 switch (cmd->command) {
926 case NFULNL_CFG_CMD_BIND:
932 inst = instance_create(info->net, group_num,
933 NETLINK_CB(skb).portid,
934 sk_user_ns(NETLINK_CB(skb).sk));
940 case NFULNL_CFG_CMD_UNBIND:
946 instance_destroy(log, inst);
957 if (nfula[NFULA_CFG_MODE]) {
958 struct nfulnl_msg_config_mode *params =
959 nla_data(nfula[NFULA_CFG_MODE]);
961 nfulnl_set_mode(inst, params->copy_mode,
962 ntohl(params->copy_range));
965 if (nfula[NFULA_CFG_TIMEOUT]) {
966 __be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
968 nfulnl_set_timeout(inst, ntohl(timeout));
971 if (nfula[NFULA_CFG_NLBUFSIZ]) {
972 __be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
974 nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
977 if (nfula[NFULA_CFG_QTHRESH]) {
978 __be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
980 nfulnl_set_qthresh(inst, ntohl(qthresh));
983 if (nfula[NFULA_CFG_FLAGS])
984 nfulnl_set_flags(inst, flags);
992 static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
993 [NFULNL_MSG_PACKET] = {
994 .call = nfulnl_recv_unsupp,
995 .type = NFNL_CB_MUTEX,
996 .attr_count = NFULA_MAX,
998 [NFULNL_MSG_CONFIG] = {
999 .call = nfulnl_recv_config,
1000 .type = NFNL_CB_MUTEX,
1001 .attr_count = NFULA_CFG_MAX,
1002 .policy = nfula_cfg_policy
1006 static const struct nfnetlink_subsystem nfulnl_subsys = {
1008 .subsys_id = NFNL_SUBSYS_ULOG,
1009 .cb_count = NFULNL_MSG_MAX,
1013 #ifdef CONFIG_PROC_FS
1015 struct seq_net_private p;
1016 unsigned int bucket;
1019 static struct hlist_node *get_first(struct net *net, struct iter_state *st)
1021 struct nfnl_log_net *log;
1025 log = nfnl_log_pernet(net);
1027 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
1028 struct hlist_head *head = &log->instance_table[st->bucket];
1030 if (!hlist_empty(head))
1031 return rcu_dereference_bh(hlist_first_rcu(head));
1036 static struct hlist_node *get_next(struct net *net, struct iter_state *st,
1037 struct hlist_node *h)
1039 h = rcu_dereference_bh(hlist_next_rcu(h));
1041 struct nfnl_log_net *log;
1042 struct hlist_head *head;
1044 if (++st->bucket >= INSTANCE_BUCKETS)
1047 log = nfnl_log_pernet(net);
1048 head = &log->instance_table[st->bucket];
1049 h = rcu_dereference_bh(hlist_first_rcu(head));
1054 static struct hlist_node *get_idx(struct net *net, struct iter_state *st,
1057 struct hlist_node *head;
1058 head = get_first(net, st);
1061 while (pos && (head = get_next(net, st, head)))
1063 return pos ? NULL : head;
1066 static void *seq_start(struct seq_file *s, loff_t *pos)
1070 return get_idx(seq_file_net(s), s->private, *pos);
1073 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1076 return get_next(seq_file_net(s), s->private, v);
1079 static void seq_stop(struct seq_file *s, void *v)
1082 rcu_read_unlock_bh();
1085 static int seq_show(struct seq_file *s, void *v)
1087 const struct nfulnl_instance *inst = v;
1089 seq_printf(s, "%5u %6u %5u %1u %5u %6u %2u\n",
1091 inst->peer_portid, inst->qlen,
1092 inst->copy_mode, inst->copy_range,
1093 inst->flushtimeout, refcount_read(&inst->use));
1098 static const struct seq_operations nful_seq_ops = {
1104 #endif /* PROC_FS */
1106 static int __net_init nfnl_log_net_init(struct net *net)
1109 struct nfnl_log_net *log = nfnl_log_pernet(net);
1110 #ifdef CONFIG_PROC_FS
1111 struct proc_dir_entry *proc;
1116 for (i = 0; i < INSTANCE_BUCKETS; i++)
1117 INIT_HLIST_HEAD(&log->instance_table[i]);
1118 spin_lock_init(&log->instances_lock);
1120 #ifdef CONFIG_PROC_FS
1121 proc = proc_create_net("nfnetlink_log", 0440, net->nf.proc_netfilter,
1122 &nful_seq_ops, sizeof(struct iter_state));
1126 root_uid = make_kuid(net->user_ns, 0);
1127 root_gid = make_kgid(net->user_ns, 0);
1128 if (uid_valid(root_uid) && gid_valid(root_gid))
1129 proc_set_user(proc, root_uid, root_gid);
1134 static void __net_exit nfnl_log_net_exit(struct net *net)
1136 struct nfnl_log_net *log = nfnl_log_pernet(net);
1139 #ifdef CONFIG_PROC_FS
1140 remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter);
1142 nf_log_unset(net, &nfulnl_logger);
1143 for (i = 0; i < INSTANCE_BUCKETS; i++)
1144 WARN_ON_ONCE(!hlist_empty(&log->instance_table[i]));
1147 static struct pernet_operations nfnl_log_net_ops = {
1148 .init = nfnl_log_net_init,
1149 .exit = nfnl_log_net_exit,
1150 .id = &nfnl_log_net_id,
1151 .size = sizeof(struct nfnl_log_net),
1154 static int __init nfnetlink_log_init(void)
1158 status = register_pernet_subsys(&nfnl_log_net_ops);
1160 pr_err("failed to register pernet ops\n");
1164 netlink_register_notifier(&nfulnl_rtnl_notifier);
1165 status = nfnetlink_subsys_register(&nfulnl_subsys);
1167 pr_err("failed to create netlink socket\n");
1168 goto cleanup_netlink_notifier;
1171 status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
1173 pr_err("failed to register logger\n");
1174 goto cleanup_subsys;
1180 nfnetlink_subsys_unregister(&nfulnl_subsys);
1181 cleanup_netlink_notifier:
1182 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
1183 unregister_pernet_subsys(&nfnl_log_net_ops);
1188 static void __exit nfnetlink_log_fini(void)
1190 nfnetlink_subsys_unregister(&nfulnl_subsys);
1191 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
1192 unregister_pernet_subsys(&nfnl_log_net_ops);
1193 nf_log_unregister(&nfulnl_logger);
1196 MODULE_DESCRIPTION("netfilter userspace logging");
1197 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1198 MODULE_LICENSE("GPL");
1199 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
1200 MODULE_ALIAS_NF_LOGGER(AF_INET, 1);
1201 MODULE_ALIAS_NF_LOGGER(AF_INET6, 1);
1202 MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 1);
1203 MODULE_ALIAS_NF_LOGGER(3, 1); /* NFPROTO_ARP */
1204 MODULE_ALIAS_NF_LOGGER(5, 1); /* NFPROTO_NETDEV */
1206 module_init(nfnetlink_log_init);
1207 module_exit(nfnetlink_log_fini);