2 * netfilter module for userspace packet logging daemons
4 * (C) 2000-2002 by Harald Welte <laforge@netfilter.org>
6 * 2000/09/22 ulog-cprange feature added
7 * 2001/01/04 in-kernel queue as proposed by Sebastian Zander
8 * <zander@fokus.gmd.de>
9 * 2001/01/30 per-rule nlgroup conflicts with global queue.
10 * nlgroup now global (sysctl)
11 * 2001/04/19 ulog-queue reworked, now fixed buffer size specified at
13 * 2002/07/07 remove broken nflog_rcv() function -HW
14 * 2002/08/29 fix shifted/unshifted nlgroup bug -HW
15 * 2002/10/30 fix uninitialized mac_len field - <Anders K. Pedersen>
17 * (C) 1999-2001 Paul `Rusty' Russell
18 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License version 2 as
22 * published by the Free Software Foundation.
24 * This module accepts two parameters:
27 * The parameter specifies how big the buffer for each netlink multicast
28 * group is. e.g. If you say nlbufsiz=8192, up to eight kb of packets will
29 * get accumulated in the kernel until they are sent to userspace. It is
30 * NOT possible to allocate more than 128kB, and it is strongly discouraged,
31 * because atomically allocating 128kB inside the network rx softirq is not
32 * reliable. Please also keep in mind that this buffer size is allocated for
33 * each nlgroup you are using, so the total kernel memory usage increases
37 * Specify, after how many clock ticks (intel: 100 per second) the queue
38 * should be flushed even if it is not full yet.
40 * ipt_ULOG.c,v 1.22 2002/10/30 09:07:31 laforge Exp
43 #include <linux/module.h>
44 #include <linux/config.h>
45 #include <linux/spinlock.h>
46 #include <linux/socket.h>
47 #include <linux/skbuff.h>
48 #include <linux/kernel.h>
49 #include <linux/timer.h>
50 #include <linux/netlink.h>
51 #include <linux/netdevice.h>
53 #include <linux/netfilter.h>
54 #include <linux/netfilter_ipv4/ip_tables.h>
55 #include <linux/netfilter_ipv4/ipt_ULOG.h>
56 #include <linux/netfilter_ipv4/lockhelp.h>
58 #include <linux/bitops.h>
60 MODULE_LICENSE("GPL");
61 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
62 MODULE_DESCRIPTION("iptables userspace logging module");
64 #define ULOG_NL_EVENT 111 /* Harald's favorite number */
65 #define ULOG_MAXNLGROUPS 32 /* numer of nlgroups */
68 #define DEBUGP(format, args...) printk("%s:%s:" format, \
69 __FILE__, __FUNCTION__ , ## args)
71 #define DEBUGP(format, args...)
74 #define PRINTR(format, args...) do { if (net_ratelimit()) printk(format , ## args); } while (0)
76 static unsigned int nlbufsiz = 4096;
77 MODULE_PARM(nlbufsiz, "i");
78 MODULE_PARM_DESC(nlbufsiz, "netlink buffer size");
80 static unsigned int flushtimeout = 10 * HZ;
81 MODULE_PARM(flushtimeout, "i");
82 MODULE_PARM_DESC(flushtimeout, "buffer flush timeout");
84 static unsigned int nflog = 1;
85 MODULE_PARM(nflog, "i");
86 MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
88 /* global data structures */
91 unsigned int qlen; /* number of nlmsgs' in the skb */
92 struct nlmsghdr *lastnlh; /* netlink header of last msg in skb */
93 struct sk_buff *skb; /* the pre-allocated skb */
94 struct timer_list timer; /* the timer function */
97 static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS]; /* array of buffers */
99 static struct sock *nflognl; /* our socket */
100 static size_t qlen; /* current length of multipart-nlmsg */
101 DECLARE_LOCK(ulog_lock); /* spinlock */
103 /* send one ulog_buff_t to userspace */
104 static void ulog_send(unsigned int nlgroupnum)
106 ulog_buff_t *ub = &ulog_buffers[nlgroupnum];
108 if (timer_pending(&ub->timer)) {
109 DEBUGP("ipt_ULOG: ulog_send: timer was pending, deleting\n");
110 del_timer(&ub->timer);
113 /* last nlmsg needs NLMSG_DONE */
115 ub->lastnlh->nlmsg_type = NLMSG_DONE;
117 NETLINK_CB(ub->skb).dst_groups = (1 << nlgroupnum);
118 DEBUGP("ipt_ULOG: throwing %d packets to netlink mask %u\n",
120 netlink_broadcast(nflognl, ub->skb, 0, (1 << nlgroupnum), GFP_ATOMIC);
129 /* timer function to flush queue in ULOG_FLUSH_INTERVAL time */
130 static void ulog_timer(unsigned long data)
132 DEBUGP("ipt_ULOG: timer function called, calling ulog_send\n");
134 /* lock to protect against somebody modifying our structure
135 * from ipt_ulog_target at the same time */
138 UNLOCK_BH(&ulog_lock);
141 struct sk_buff *ulog_alloc_skb(unsigned int size)
145 /* alloc skb which should be big enough for a whole
146 * multipart message. WARNING: has to be <= 131000
147 * due to slab allocator restrictions */
149 skb = alloc_skb(nlbufsiz, GFP_ATOMIC);
151 PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n",
154 /* try to allocate only as much as we need for
157 skb = alloc_skb(size, GFP_ATOMIC);
159 PRINTR("ipt_ULOG: can't even allocate %ub\n", size);
165 static void ipt_ulog_packet(unsigned int hooknum,
166 const struct sk_buff *skb,
167 const struct net_device *in,
168 const struct net_device *out,
169 const struct ipt_ulog_info *loginfo,
173 ulog_packet_msg_t *pm;
174 size_t size, copy_len;
175 struct nlmsghdr *nlh;
177 /* ffs == find first bit set, necessary because userspace
178 * is already shifting groupnumber, but we need unshifted.
179 * ffs() returns [1..32], we need [0..31] */
180 unsigned int groupnum = ffs(loginfo->nl_group) - 1;
182 /* calculate the size of the skb needed */
183 if ((loginfo->copy_range == 0) ||
184 (loginfo->copy_range > skb->len)) {
187 copy_len = loginfo->copy_range;
190 size = NLMSG_SPACE(sizeof(*pm) + copy_len);
192 ub = &ulog_buffers[groupnum];
197 if (!(ub->skb = ulog_alloc_skb(size)))
199 } else if (ub->qlen >= loginfo->qthreshold ||
200 size > skb_tailroom(ub->skb)) {
201 /* either the queue len is too high or we don't have
202 * enough room in nlskb left. send it to userspace. */
206 if (!(ub->skb = ulog_alloc_skb(size)))
210 DEBUGP("ipt_ULOG: qlen %d, qthreshold %d\n", ub->qlen,
211 loginfo->qthreshold);
213 /* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */
214 nlh = NLMSG_PUT(ub->skb, 0, ub->qlen, ULOG_NL_EVENT,
215 size - sizeof(*nlh));
218 pm = NLMSG_DATA(nlh);
220 /* copy hook, prefix, timestamp, payload, etc. */
221 pm->data_len = copy_len;
222 pm->timestamp_sec = skb->stamp.tv_sec;
223 pm->timestamp_usec = skb->stamp.tv_usec;
224 pm->mark = skb->nfmark;
227 strncpy(pm->prefix, prefix, sizeof(pm->prefix));
228 else if (loginfo->prefix[0] != '\0')
229 strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
231 *(pm->prefix) = '\0';
233 if (in && in->hard_header_len > 0
234 && skb->mac.raw != (void *) skb->nh.iph
235 && in->hard_header_len <= ULOG_MAC_LEN) {
236 memcpy(pm->mac, skb->mac.raw, in->hard_header_len);
237 pm->mac_len = in->hard_header_len;
242 strncpy(pm->indev_name, in->name, sizeof(pm->indev_name));
244 pm->indev_name[0] = '\0';
247 strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
249 pm->outdev_name[0] = '\0';
251 /* copy_len <= skb->len, so can't fail. */
252 if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0)
255 /* check if we are building multi-part messages */
257 ub->lastnlh->nlmsg_flags |= NLM_F_MULTI;
260 /* if threshold is reached, send message to userspace */
261 if (qlen >= loginfo->qthreshold) {
262 if (loginfo->qthreshold > 1)
263 nlh->nlmsg_type = NLMSG_DONE;
268 /* if timer isn't already running, start it */
269 if (!timer_pending(&ub->timer)) {
270 ub->timer.expires = jiffies + flushtimeout;
271 add_timer(&ub->timer);
274 UNLOCK_BH(&ulog_lock);
279 PRINTR("ipt_ULOG: error during NLMSG_PUT\n");
282 PRINTR("ipt_ULOG: Error building netlink message\n");
284 UNLOCK_BH(&ulog_lock);
287 static unsigned int ipt_ulog_target(struct sk_buff **pskb,
288 const struct net_device *in,
289 const struct net_device *out,
290 unsigned int hooknum,
291 const void *targinfo, void *userinfo)
293 struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
295 ipt_ulog_packet(hooknum, *pskb, in, out, loginfo, NULL);
300 static void ipt_logfn(unsigned int hooknum,
301 const struct sk_buff *skb,
302 const struct net_device *in,
303 const struct net_device *out,
306 struct ipt_ulog_info loginfo = {
307 .nl_group = ULOG_DEFAULT_NLGROUP,
309 .qthreshold = ULOG_DEFAULT_QTHRESHOLD,
313 ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix);
316 static int ipt_ulog_checkentry(const char *tablename,
317 const struct ipt_entry *e,
319 unsigned int targinfosize,
320 unsigned int hookmask)
322 struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
324 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_ulog_info))) {
325 DEBUGP("ipt_ULOG: targinfosize %u != 0\n", targinfosize);
329 if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') {
330 DEBUGP("ipt_ULOG: prefix term %i\n",
331 loginfo->prefix[sizeof(loginfo->prefix) - 1]);
335 if (loginfo->qthreshold > ULOG_MAX_QLEN) {
336 DEBUGP("ipt_ULOG: queue threshold %i > MAX_QLEN\n",
337 loginfo->qthreshold);
344 static struct ipt_target ipt_ulog_reg = {
346 .target = ipt_ulog_target,
347 .checkentry = ipt_ulog_checkentry,
351 static int __init init(void)
355 DEBUGP("ipt_ULOG: init module\n");
357 if (nlbufsiz >= 128*1024) {
358 printk("Netlink buffer has to be <= 128kB\n");
362 /* initialize ulog_buffers */
363 for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
364 init_timer(&ulog_buffers[i].timer);
365 ulog_buffers[i].timer.function = ulog_timer;
366 ulog_buffers[i].timer.data = i;
369 nflognl = netlink_kernel_create(NETLINK_NFLOG, NULL);
373 if (ipt_register_target(&ipt_ulog_reg) != 0) {
374 sock_release(nflognl->sk_socket);
378 nf_log_register(PF_INET, &ipt_logfn);
383 static void __exit fini(void)
388 DEBUGP("ipt_ULOG: cleanup_module\n");
391 nf_log_unregister(PF_INET, &ipt_logfn);
392 ipt_unregister_target(&ipt_ulog_reg);
393 sock_release(nflognl->sk_socket);
395 /* remove pending timers and free allocated skb's */
396 for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
397 ub = &ulog_buffers[i];
398 if (timer_pending(&ub->timer)) {
399 DEBUGP("timer was pending, deleting\n");
400 del_timer(&ub->timer);