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 #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 */
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
;
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 */
73 u_int32_t seq
; /* instance-local sequential counter */
74 u_int16_t group_num
; /* number of this queue */
80 #define INSTANCE_BUCKETS 16
82 static unsigned int nfnl_log_net_id __read_mostly
;
85 spinlock_t instances_lock
;
86 struct hlist_head instance_table
[INSTANCE_BUCKETS
];
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
)
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
;
126 inst
= __instance_lookup(log
, group_num
);
127 if (inst
&& !refcount_inc_not_zero(&inst
->use
))
129 rcu_read_unlock_bh();
134 static void nfulnl_instance_free_rcu(struct rcu_head
*head
)
136 struct nfulnl_instance
*inst
=
137 container_of(head
, struct nfulnl_instance
, rcu
);
141 module_put(THIS_MODULE
);
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
);
161 spin_lock_bh(&log
->instances_lock
);
162 if (__instance_lookup(log
, group_num
)) {
167 inst
= kzalloc(sizeof(*inst
), GFP_ATOMIC
);
173 if (!try_module_get(THIS_MODULE
)) {
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
);
206 spin_unlock_bh(&log
->instances_lock
);
210 static void __nfulnl_flush(struct nfulnl_instance
*inst
);
212 /* called with BH disabled */
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
;
227 __nfulnl_flush(inst
);
228 spin_unlock(&inst
->lock
);
230 /* and finally put the refcount */
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
);
244 nfulnl_set_mode(struct nfulnl_instance
*inst
, u_int8_t mode
,
249 spin_lock_bh(&inst
->lock
);
252 case NFULNL_COPY_NONE
:
253 case NFULNL_COPY_META
:
254 inst
->copy_mode
= mode
;
255 inst
->copy_range
= 0;
258 case NFULNL_COPY_PACKET
:
259 inst
->copy_mode
= mode
;
261 range
= NFULNL_COPY_RANGE_MAX
;
262 inst
->copy_range
= min_t(unsigned int,
263 range
, NFULNL_COPY_RANGE_MAX
);
271 spin_unlock_bh(&inst
->lock
);
277 nfulnl_set_nlbufsiz(struct nfulnl_instance
*inst
, u_int32_t nlbufsiz
)
281 spin_lock_bh(&inst
->lock
);
282 if (nlbufsiz
< NFULNL_NLBUFSIZ_DEFAULT
)
284 else if (nlbufsiz
> 131072)
287 inst
->nlbufsiz
= nlbufsiz
;
290 spin_unlock_bh(&inst
->lock
);
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
);
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
);
312 nfulnl_set_flags(struct nfulnl_instance
*inst
, u_int16_t flags
)
314 spin_lock_bh(&inst
->lock
);
316 spin_unlock_bh(&inst
->lock
);
321 static struct sk_buff
*
322 nfulnl_alloc_skb(struct net
*net
, u32 peer_portid
, unsigned int inst_size
,
323 unsigned int pkt_size
)
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
);
335 /* try to allocate only as much as we need for current
338 skb
= alloc_skb(pkt_size
, GFP_ATOMIC
);
346 __nfulnl_send(struct nfulnl_instance
*inst
)
348 if (inst
->qlen
> 1) {
349 struct nlmsghdr
*nlh
= nlmsg_put(inst
->skb
, 0, 0,
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
);
359 nfnetlink_unicast(inst
->skb
, inst
->net
, inst
->peer_portid
,
367 __nfulnl_flush(struct nfulnl_instance
*inst
)
369 /* timer holds a reference */
370 if (del_timer(&inst
->timer
))
377 nfulnl_timer(struct timer_list
*t
)
379 struct nfulnl_instance
*inst
= from_timer(inst
, t
, timer
);
381 spin_lock_bh(&inst
->lock
);
384 spin_unlock_bh(&inst
->lock
);
388 /* This is an inline function, we don't really care about a long
389 * list of arguments */
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
,
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
;
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);
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
;
424 if (nla_put(inst
->skb
, NFULA_PACKET_HDR
, sizeof(pmsg
), &pmsg
))
425 goto nla_put_failure
;
428 nla_put(inst
->skb
, NFULA_PREFIX
, plen
, prefix
))
429 goto nla_put_failure
;
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
;
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
447 nla_put_be32(inst
->skb
, NFULA_IFINDEX_INDEV
,
448 htonl(br_port_get_rcu(indev
)->br
->dev
->ifindex
)))
449 goto nla_put_failure
;
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
);
461 nla_put_be32(inst
->skb
, NFULA_IFINDEX_PHYSINDEV
,
462 htonl(physindev
->ifindex
)))
463 goto nla_put_failure
;
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
;
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
484 nla_put_be32(inst
->skb
, NFULA_IFINDEX_OUTDEV
,
485 htonl(br_port_get_rcu(outdev
)->br
->dev
->ifindex
)))
486 goto nla_put_failure
;
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
);
498 nla_put_be32(inst
->skb
, NFULA_IFINDEX_PHYSOUTDEV
,
499 htonl(physoutdev
->ifindex
)))
500 goto nla_put_failure
;
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
;
514 memset(&phw
, 0, sizeof(phw
));
515 len
= dev_parse_header(skb
, phw
.hw_addr
);
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
)
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
;
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
;
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
;
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
;
594 if (skb_copy_bits(skb
, 0, nla_data(nla
), data_len
))
598 nlh
->nlmsg_len
= inst
->skb
->tail
- old_tail
;
602 PRINTR(KERN_ERR
"nfnetlink_log: error creating log nlmsg\n");
606 static const struct nf_loginfo default_loginfo
= {
607 .type
= NF_LOG_TYPE_ULOG
,
617 /* log handler for internal netfilter logging api */
619 nfulnl_log_packet(struct net
*net
,
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
,
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
)
642 li
= &default_loginfo
;
644 inst
= instance_lookup_get(log
, li
->u
.ulog
.group
);
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 */
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
);
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
:
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
)
713 size
+= nla_total_size(data_len
);
716 case NFULNL_COPY_DISABLED
:
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
);
728 inst
->skb
= nfulnl_alloc_skb(net
, inst
->peer_portid
,
729 inst
->nlbufsiz
, size
);
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
)) {
746 inst
->timer
.expires
= jiffies
+ (inst
->flushtimeout
*HZ
/100);
747 add_timer(&inst
->timer
);
751 spin_unlock_bh(&inst
->lock
);
756 /* FIXME: statistics */
757 goto unlock_and_release
;
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
) {
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
);
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
)
799 static struct nf_logger nfulnl_logger __read_mostly
= {
800 .name
= "nfnetlink_log",
801 .type
= NF_LOG_TYPE_ULOG
,
802 .logfn
= nfulnl_log_packet
,
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
);
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
);
842 inst
= instance_lookup_get(log
, group_num
);
843 if (inst
&& inst
->peer_portid
!= NETLINK_CB(skb
).portid
) {
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
)) {
871 switch (cmd
->command
) {
872 case NFULNL_CFG_CMD_BIND
:
878 inst
= instance_create(net
, group_num
,
879 NETLINK_CB(skb
).portid
,
880 sk_user_ns(NETLINK_CB(skb
).sk
));
886 case NFULNL_CFG_CMD_UNBIND
:
892 instance_destroy(log
, inst
);
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
);
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
= {
948 .subsys_id
= NFNL_SUBSYS_ULOG
,
949 .cb_count
= NFULNL_MSG_MAX
,
953 #ifdef CONFIG_PROC_FS
955 struct seq_net_private p
;
959 static struct hlist_node
*get_first(struct net
*net
, struct iter_state
*st
)
961 struct nfnl_log_net
*log
;
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
));
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
));
981 struct nfnl_log_net
*log
;
982 struct hlist_head
*head
;
984 if (++st
->bucket
>= INSTANCE_BUCKETS
)
987 log
= nfnl_log_pernet(net
);
988 head
= &log
->instance_table
[st
->bucket
];
989 h
= rcu_dereference_bh(hlist_first_rcu(head
));
994 static struct hlist_node
*get_idx(struct net
*net
, struct iter_state
*st
,
997 struct hlist_node
*head
;
998 head
= get_first(net
, st
);
1001 while (pos
&& (head
= get_next(net
, st
, head
)))
1003 return pos
? NULL
: head
;
1006 static void *seq_start(struct seq_file
*s
, loff_t
*pos
)
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
)
1016 return get_next(seq_file_net(s
), s
->private, v
);
1019 static void seq_stop(struct seq_file
*s
, void *v
)
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",
1031 inst
->peer_portid
, inst
->qlen
,
1032 inst
->copy_mode
, inst
->copy_range
,
1033 inst
->flushtimeout
, refcount_read(&inst
->use
));
1038 static const struct seq_operations nful_seq_ops
= {
1044 #endif /* PROC_FS */
1046 static int __net_init
nfnl_log_net_init(struct net
*net
)
1049 struct nfnl_log_net
*log
= nfnl_log_pernet(net
);
1050 #ifdef CONFIG_PROC_FS
1051 struct proc_dir_entry
*proc
;
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
));
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
);
1074 static void __net_exit
nfnl_log_net_exit(struct net
*net
)
1076 struct nfnl_log_net
*log
= nfnl_log_pernet(net
);
1079 #ifdef CONFIG_PROC_FS
1080 remove_proc_entry("nfnetlink_log", net
->nf
.proc_netfilter
);
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)
1098 status
= register_pernet_subsys(&nfnl_log_net_ops
);
1100 pr_err("failed to register pernet ops\n");
1104 netlink_register_notifier(&nfulnl_rtnl_notifier
);
1105 status
= nfnetlink_subsys_register(&nfulnl_subsys
);
1107 pr_err("failed to create netlink socket\n");
1108 goto cleanup_netlink_notifier
;
1111 status
= nf_log_register(NFPROTO_UNSPEC
, &nfulnl_logger
);
1113 pr_err("failed to register logger\n");
1114 goto 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
);
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
);