2 * netfilter module for userspace packet logging daemons
4 * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
5 * (C) 1999-2001 Paul `Rusty' Russell
6 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This module accepts two parameters:
15 * The parameter specifies how big the buffer for each netlink multicast
16 * group is. e.g. If you say nlbufsiz=8192, up to eight kb of packets will
17 * get accumulated in the kernel until they are sent to userspace. It is
18 * NOT possible to allocate more than 128kB, and it is strongly discouraged,
19 * because atomically allocating 128kB inside the network rx softirq is not
20 * reliable. Please also keep in mind that this buffer size is allocated for
21 * each nlgroup you are using, so the total kernel memory usage increases
24 * Actually you should use nlbufsiz a bit smaller than PAGE_SIZE, since
25 * nlbufsiz is used with alloc_skb, which adds another
26 * sizeof(struct skb_shared_info). Use NLMSG_GOODSIZE instead.
29 * Specify, after how many hundredths of a second the queue should be
30 * flushed even if it is not full yet.
33 #include <linux/module.h>
34 #include <linux/spinlock.h>
35 #include <linux/socket.h>
36 #include <linux/skbuff.h>
37 #include <linux/kernel.h>
38 #include <linux/timer.h>
39 #include <linux/netlink.h>
40 #include <linux/netdevice.h>
42 #include <linux/moduleparam.h>
43 #include <linux/netfilter.h>
44 #include <linux/netfilter/x_tables.h>
45 #include <linux/netfilter_ipv4/ipt_ULOG.h>
47 #include <linux/bitops.h>
48 #include <asm/unaligned.h>
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
52 MODULE_DESCRIPTION("iptables userspace logging module");
53 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_NFLOG
);
55 #define ULOG_NL_EVENT 111 /* Harald's favorite number */
56 #define ULOG_MAXNLGROUPS 32 /* numer of nlgroups */
58 #define PRINTR(format, args...) do { if (net_ratelimit()) printk(format , ## args); } while (0)
60 static unsigned int nlbufsiz
= NLMSG_GOODSIZE
;
61 module_param(nlbufsiz
, uint
, 0400);
62 MODULE_PARM_DESC(nlbufsiz
, "netlink buffer size");
64 static unsigned int flushtimeout
= 10;
65 module_param(flushtimeout
, uint
, 0600);
66 MODULE_PARM_DESC(flushtimeout
, "buffer flush timeout (hundredths of a second)");
69 module_param(nflog
, bool, 0400);
70 MODULE_PARM_DESC(nflog
, "register as internal netfilter logging module");
72 /* global data structures */
75 unsigned int qlen
; /* number of nlmsgs' in the skb */
76 struct nlmsghdr
*lastnlh
; /* netlink header of last msg in skb */
77 struct sk_buff
*skb
; /* the pre-allocated skb */
78 struct timer_list timer
; /* the timer function */
81 static ulog_buff_t ulog_buffers
[ULOG_MAXNLGROUPS
]; /* array of buffers */
83 static struct sock
*nflognl
; /* our socket */
84 static DEFINE_SPINLOCK(ulog_lock
); /* spinlock */
86 /* send one ulog_buff_t to userspace */
87 static void ulog_send(unsigned int nlgroupnum
)
89 ulog_buff_t
*ub
= &ulog_buffers
[nlgroupnum
];
91 if (timer_pending(&ub
->timer
)) {
92 pr_debug("ipt_ULOG: ulog_send: timer was pending, deleting\n");
93 del_timer(&ub
->timer
);
97 pr_debug("ipt_ULOG: ulog_send: nothing to send\n");
101 /* last nlmsg needs NLMSG_DONE */
103 ub
->lastnlh
->nlmsg_type
= NLMSG_DONE
;
105 NETLINK_CB(ub
->skb
).dst_group
= nlgroupnum
+ 1;
106 pr_debug("ipt_ULOG: throwing %d packets to netlink group %u\n",
107 ub
->qlen
, nlgroupnum
+ 1);
108 netlink_broadcast(nflognl
, ub
->skb
, 0, nlgroupnum
+ 1, GFP_ATOMIC
);
116 /* timer function to flush queue in flushtimeout time */
117 static void ulog_timer(unsigned long data
)
119 pr_debug("ipt_ULOG: timer function called, calling ulog_send\n");
121 /* lock to protect against somebody modifying our structure
122 * from ipt_ulog_target at the same time */
123 spin_lock_bh(&ulog_lock
);
125 spin_unlock_bh(&ulog_lock
);
128 static struct sk_buff
*ulog_alloc_skb(unsigned int size
)
133 /* alloc skb which should be big enough for a whole
134 * multipart message. WARNING: has to be <= 131000
135 * due to slab allocator restrictions */
137 n
= max(size
, nlbufsiz
);
138 skb
= alloc_skb(n
, GFP_ATOMIC
);
140 PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n", n
);
143 /* try to allocate only as much as we need for
146 skb
= alloc_skb(size
, GFP_ATOMIC
);
148 PRINTR("ipt_ULOG: can't even allocate %ub\n",
156 static void ipt_ulog_packet(unsigned int hooknum
,
157 const struct sk_buff
*skb
,
158 const struct net_device
*in
,
159 const struct net_device
*out
,
160 const struct ipt_ulog_info
*loginfo
,
164 ulog_packet_msg_t
*pm
;
165 size_t size
, copy_len
;
166 struct nlmsghdr
*nlh
;
169 /* ffs == find first bit set, necessary because userspace
170 * is already shifting groupnumber, but we need unshifted.
171 * ffs() returns [1..32], we need [0..31] */
172 unsigned int groupnum
= ffs(loginfo
->nl_group
) - 1;
174 /* calculate the size of the skb needed */
175 if (loginfo
->copy_range
== 0 || loginfo
->copy_range
> skb
->len
)
178 copy_len
= loginfo
->copy_range
;
180 size
= NLMSG_SPACE(sizeof(*pm
) + copy_len
);
182 ub
= &ulog_buffers
[groupnum
];
184 spin_lock_bh(&ulog_lock
);
187 if (!(ub
->skb
= ulog_alloc_skb(size
)))
189 } else if (ub
->qlen
>= loginfo
->qthreshold
||
190 size
> skb_tailroom(ub
->skb
)) {
191 /* either the queue len is too high or we don't have
192 * enough room in nlskb left. send it to userspace. */
196 if (!(ub
->skb
= ulog_alloc_skb(size
)))
200 pr_debug("ipt_ULOG: qlen %d, qthreshold %Zu\n", ub
->qlen
,
201 loginfo
->qthreshold
);
203 /* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */
204 nlh
= NLMSG_PUT(ub
->skb
, 0, ub
->qlen
, ULOG_NL_EVENT
,
205 sizeof(*pm
)+copy_len
);
208 pm
= NLMSG_DATA(nlh
);
210 /* We might not have a timestamp, get one */
211 if (skb
->tstamp
.tv64
== 0)
212 __net_timestamp((struct sk_buff
*)skb
);
214 /* copy hook, prefix, timestamp, payload, etc. */
215 pm
->data_len
= copy_len
;
216 tv
= ktime_to_timeval(skb
->tstamp
);
217 put_unaligned(tv
.tv_sec
, &pm
->timestamp_sec
);
218 put_unaligned(tv
.tv_usec
, &pm
->timestamp_usec
);
219 put_unaligned(skb
->mark
, &pm
->mark
);
222 strncpy(pm
->prefix
, prefix
, sizeof(pm
->prefix
));
223 else if (loginfo
->prefix
[0] != '\0')
224 strncpy(pm
->prefix
, loginfo
->prefix
, sizeof(pm
->prefix
));
226 *(pm
->prefix
) = '\0';
228 if (in
&& in
->hard_header_len
> 0
229 && skb
->mac_header
!= skb
->network_header
230 && in
->hard_header_len
<= ULOG_MAC_LEN
) {
231 memcpy(pm
->mac
, skb_mac_header(skb
), in
->hard_header_len
);
232 pm
->mac_len
= in
->hard_header_len
;
237 strncpy(pm
->indev_name
, in
->name
, sizeof(pm
->indev_name
));
239 pm
->indev_name
[0] = '\0';
242 strncpy(pm
->outdev_name
, out
->name
, sizeof(pm
->outdev_name
));
244 pm
->outdev_name
[0] = '\0';
246 /* copy_len <= skb->len, so can't fail. */
247 if (skb_copy_bits(skb
, 0, pm
->payload
, copy_len
) < 0)
250 /* check if we are building multi-part messages */
252 ub
->lastnlh
->nlmsg_flags
|= NLM_F_MULTI
;
256 /* if timer isn't already running, start it */
257 if (!timer_pending(&ub
->timer
)) {
258 ub
->timer
.expires
= jiffies
+ flushtimeout
* HZ
/ 100;
259 add_timer(&ub
->timer
);
262 /* if threshold is reached, send message to userspace */
263 if (ub
->qlen
>= loginfo
->qthreshold
) {
264 if (loginfo
->qthreshold
> 1)
265 nlh
->nlmsg_type
= NLMSG_DONE
;
269 spin_unlock_bh(&ulog_lock
);
274 PRINTR("ipt_ULOG: error during NLMSG_PUT\n");
277 PRINTR("ipt_ULOG: Error building netlink message\n");
279 spin_unlock_bh(&ulog_lock
);
283 ulog_tg(struct sk_buff
*skb
, const struct net_device
*in
,
284 const struct net_device
*out
, unsigned int hooknum
,
285 const struct xt_target
*target
, const void *targinfo
)
287 struct ipt_ulog_info
*loginfo
= (struct ipt_ulog_info
*) targinfo
;
289 ipt_ulog_packet(hooknum
, skb
, in
, out
, loginfo
, NULL
);
294 static void ipt_logfn(unsigned int pf
,
295 unsigned int hooknum
,
296 const struct sk_buff
*skb
,
297 const struct net_device
*in
,
298 const struct net_device
*out
,
299 const struct nf_loginfo
*li
,
302 struct ipt_ulog_info loginfo
;
304 if (!li
|| li
->type
!= NF_LOG_TYPE_ULOG
) {
305 loginfo
.nl_group
= ULOG_DEFAULT_NLGROUP
;
306 loginfo
.copy_range
= 0;
307 loginfo
.qthreshold
= ULOG_DEFAULT_QTHRESHOLD
;
308 loginfo
.prefix
[0] = '\0';
310 loginfo
.nl_group
= li
->u
.ulog
.group
;
311 loginfo
.copy_range
= li
->u
.ulog
.copy_len
;
312 loginfo
.qthreshold
= li
->u
.ulog
.qthreshold
;
313 strlcpy(loginfo
.prefix
, prefix
, sizeof(loginfo
.prefix
));
316 ipt_ulog_packet(hooknum
, skb
, in
, out
, &loginfo
, prefix
);
320 ulog_tg_check(const char *tablename
, const void *e
,
321 const struct xt_target
*target
, void *targinfo
,
322 unsigned int hookmask
)
324 const struct ipt_ulog_info
*loginfo
= targinfo
;
326 if (loginfo
->prefix
[sizeof(loginfo
->prefix
) - 1] != '\0') {
327 pr_debug("ipt_ULOG: prefix term %i\n",
328 loginfo
->prefix
[sizeof(loginfo
->prefix
) - 1]);
331 if (loginfo
->qthreshold
> ULOG_MAX_QLEN
) {
332 pr_debug("ipt_ULOG: queue threshold %Zu > MAX_QLEN\n",
333 loginfo
->qthreshold
);
340 struct compat_ipt_ulog_info
{
341 compat_uint_t nl_group
;
342 compat_size_t copy_range
;
343 compat_size_t qthreshold
;
344 char prefix
[ULOG_PREFIX_LEN
];
347 static void ulog_tg_compat_from_user(void *dst
, void *src
)
349 const struct compat_ipt_ulog_info
*cl
= src
;
350 struct ipt_ulog_info l
= {
351 .nl_group
= cl
->nl_group
,
352 .copy_range
= cl
->copy_range
,
353 .qthreshold
= cl
->qthreshold
,
356 memcpy(l
.prefix
, cl
->prefix
, sizeof(l
.prefix
));
357 memcpy(dst
, &l
, sizeof(l
));
360 static int ulog_tg_compat_to_user(void __user
*dst
, void *src
)
362 const struct ipt_ulog_info
*l
= src
;
363 struct compat_ipt_ulog_info cl
= {
364 .nl_group
= l
->nl_group
,
365 .copy_range
= l
->copy_range
,
366 .qthreshold
= l
->qthreshold
,
369 memcpy(cl
.prefix
, l
->prefix
, sizeof(cl
.prefix
));
370 return copy_to_user(dst
, &cl
, sizeof(cl
)) ? -EFAULT
: 0;
372 #endif /* CONFIG_COMPAT */
374 static struct xt_target ulog_tg_reg __read_mostly
= {
378 .targetsize
= sizeof(struct ipt_ulog_info
),
379 .checkentry
= ulog_tg_check
,
381 .compatsize
= sizeof(struct compat_ipt_ulog_info
),
382 .compat_from_user
= ulog_tg_compat_from_user
,
383 .compat_to_user
= ulog_tg_compat_to_user
,
388 static struct nf_logger ipt_ulog_logger
= {
394 static int __init
ulog_tg_init(void)
398 pr_debug("ipt_ULOG: init module\n");
400 if (nlbufsiz
> 128*1024) {
401 printk("Netlink buffer has to be <= 128kB\n");
405 /* initialize ulog_buffers */
406 for (i
= 0; i
< ULOG_MAXNLGROUPS
; i
++)
407 setup_timer(&ulog_buffers
[i
].timer
, ulog_timer
, i
);
409 nflognl
= netlink_kernel_create(&init_net
,
410 NETLINK_NFLOG
, ULOG_MAXNLGROUPS
, NULL
,
415 ret
= xt_register_target(&ulog_tg_reg
);
417 sock_release(nflognl
->sk_socket
);
421 nf_log_register(PF_INET
, &ipt_ulog_logger
);
426 static void __exit
ulog_tg_exit(void)
431 pr_debug("ipt_ULOG: cleanup_module\n");
434 nf_log_unregister(&ipt_ulog_logger
);
435 xt_unregister_target(&ulog_tg_reg
);
436 sock_release(nflognl
->sk_socket
);
438 /* remove pending timers and free allocated skb's */
439 for (i
= 0; i
< ULOG_MAXNLGROUPS
; i
++) {
440 ub
= &ulog_buffers
[i
];
441 if (timer_pending(&ub
->timer
)) {
442 pr_debug("timer was pending, deleting\n");
443 del_timer(&ub
->timer
);
453 module_init(ulog_tg_init
);
454 module_exit(ulog_tg_exit
);