ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ipv4 / netfilter / ipt_ULOG.c
1 /*
2  * netfilter module for userspace packet logging daemons
3  *
4  * (C) 2000-2002 by Harald Welte <laforge@netfilter.org>
5  *
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
12  *            module loadtime -HW
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>
16  *
17  * (C) 1999-2001 Paul `Rusty' Russell
18  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
19  *
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.
23  *
24  * This module accepts two parameters: 
25  * 
26  * nlbufsiz:
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
34  * by that factor.
35  *
36  * flushtimeout:
37  *   Specify, after how many clock ticks (intel: 100 per second) the queue
38  * should be flushed even if it is not full yet.
39  *
40  * ipt_ULOG.c,v 1.22 2002/10/30 09:07:31 laforge Exp
41  */
42
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>
52 #include <linux/mm.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>
57 #include <net/sock.h>
58 #include <linux/bitops.h>
59
60 MODULE_LICENSE("GPL");
61 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
62 MODULE_DESCRIPTION("iptables userspace logging module");
63
64 #define ULOG_NL_EVENT           111             /* Harald's favorite number */
65 #define ULOG_MAXNLGROUPS        32              /* numer of nlgroups */
66
67 #if 0
68 #define DEBUGP(format, args...) printk("%s:%s:" format, \
69                                        __FILE__, __FUNCTION__ , ## args)
70 #else
71 #define DEBUGP(format, args...)
72 #endif
73
74 #define PRINTR(format, args...) do { if (net_ratelimit()) printk(format , ## args); } while (0)
75
76 static unsigned int nlbufsiz = 4096;
77 MODULE_PARM(nlbufsiz, "i");
78 MODULE_PARM_DESC(nlbufsiz, "netlink buffer size");
79
80 static unsigned int flushtimeout = 10 * HZ;
81 MODULE_PARM(flushtimeout, "i");
82 MODULE_PARM_DESC(flushtimeout, "buffer flush timeout");
83
84 static unsigned int nflog = 1;
85 MODULE_PARM(nflog, "i");
86 MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
87
88 /* global data structures */
89
90 typedef struct {
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 */
95 } ulog_buff_t;
96
97 static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS];      /* array of buffers */
98
99 static struct sock *nflognl;    /* our socket */
100 static size_t qlen;             /* current length of multipart-nlmsg */
101 DECLARE_LOCK(ulog_lock);        /* spinlock */
102
103 /* send one ulog_buff_t to userspace */
104 static void ulog_send(unsigned int nlgroupnum)
105 {
106         ulog_buff_t *ub = &ulog_buffers[nlgroupnum];
107
108         if (timer_pending(&ub->timer)) {
109                 DEBUGP("ipt_ULOG: ulog_send: timer was pending, deleting\n");
110                 del_timer(&ub->timer);
111         }
112
113         /* last nlmsg needs NLMSG_DONE */
114         if (ub->qlen > 1)
115                 ub->lastnlh->nlmsg_type = NLMSG_DONE;
116
117         NETLINK_CB(ub->skb).dst_groups = (1 << nlgroupnum);
118         DEBUGP("ipt_ULOG: throwing %d packets to netlink mask %u\n",
119                 ub->qlen, nlgroup);
120         netlink_broadcast(nflognl, ub->skb, 0, (1 << nlgroupnum), GFP_ATOMIC);
121
122         ub->qlen = 0;
123         ub->skb = NULL;
124         ub->lastnlh = NULL;
125
126 }
127
128
129 /* timer function to flush queue in ULOG_FLUSH_INTERVAL time */
130 static void ulog_timer(unsigned long data)
131 {
132         DEBUGP("ipt_ULOG: timer function called, calling ulog_send\n");
133
134         /* lock to protect against somebody modifying our structure
135          * from ipt_ulog_target at the same time */
136         LOCK_BH(&ulog_lock);
137         ulog_send(data);
138         UNLOCK_BH(&ulog_lock);
139 }
140
141 struct sk_buff *ulog_alloc_skb(unsigned int size)
142 {
143         struct sk_buff *skb;
144
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 */
148
149         skb = alloc_skb(nlbufsiz, GFP_ATOMIC);
150         if (!skb) {
151                 PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n",
152                         nlbufsiz);
153
154                 /* try to allocate only as much as we need for 
155                  * current packet */
156
157                 skb = alloc_skb(size, GFP_ATOMIC);
158                 if (!skb)
159                         PRINTR("ipt_ULOG: can't even allocate %ub\n", size);
160         }
161
162         return skb;
163 }
164
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,
170                             const char *prefix)
171 {
172         ulog_buff_t *ub;
173         ulog_packet_msg_t *pm;
174         size_t size, copy_len;
175         struct nlmsghdr *nlh;
176
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;
181
182         /* calculate the size of the skb needed */
183         if ((loginfo->copy_range == 0) ||
184             (loginfo->copy_range > skb->len)) {
185                 copy_len = skb->len;
186         } else {
187                 copy_len = loginfo->copy_range;
188         }
189
190         size = NLMSG_SPACE(sizeof(*pm) + copy_len);
191
192         ub = &ulog_buffers[groupnum];
193         
194         LOCK_BH(&ulog_lock);
195
196         if (!ub->skb) {
197                 if (!(ub->skb = ulog_alloc_skb(size)))
198                         goto alloc_failure;
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. */
203
204                 ulog_send(groupnum);
205
206                 if (!(ub->skb = ulog_alloc_skb(size)))
207                         goto alloc_failure;
208         }
209
210         DEBUGP("ipt_ULOG: qlen %d, qthreshold %d\n", ub->qlen, 
211                 loginfo->qthreshold);
212
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));
216         ub->qlen++;
217
218         pm = NLMSG_DATA(nlh);
219
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;
225         pm->hook = hooknum;
226         if (prefix != NULL)
227                 strncpy(pm->prefix, prefix, sizeof(pm->prefix));
228         else if (loginfo->prefix[0] != '\0')
229                 strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
230         else
231                 *(pm->prefix) = '\0';
232
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;
238         } else
239                 pm->mac_len = 0;
240
241         if (in)
242                 strncpy(pm->indev_name, in->name, sizeof(pm->indev_name));
243         else
244                 pm->indev_name[0] = '\0';
245
246         if (out)
247                 strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
248         else
249                 pm->outdev_name[0] = '\0';
250
251         /* copy_len <= skb->len, so can't fail. */
252         if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0)
253                 BUG();
254         
255         /* check if we are building multi-part messages */
256         if (ub->qlen > 1) {
257                 ub->lastnlh->nlmsg_flags |= NLM_F_MULTI;
258         }
259
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;
264         }
265
266         ub->lastnlh = nlh;
267
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);
272         }
273
274         UNLOCK_BH(&ulog_lock);
275
276         return;
277
278 nlmsg_failure:
279         PRINTR("ipt_ULOG: error during NLMSG_PUT\n");
280
281 alloc_failure:
282         PRINTR("ipt_ULOG: Error building netlink message\n");
283
284         UNLOCK_BH(&ulog_lock);
285 }
286
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)
292 {
293         struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
294
295         ipt_ulog_packet(hooknum, *pskb, in, out, loginfo, NULL);
296  
297         return IPT_CONTINUE;
298 }
299  
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,
304                       const char *prefix)
305 {
306         struct ipt_ulog_info loginfo = { 
307                 .nl_group = ULOG_DEFAULT_NLGROUP,
308                 .copy_range = 0,
309                 .qthreshold = ULOG_DEFAULT_QTHRESHOLD,
310                 .prefix = ""
311         };
312
313         ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix);
314 }
315
316 static int ipt_ulog_checkentry(const char *tablename,
317                                const struct ipt_entry *e,
318                                void *targinfo,
319                                unsigned int targinfosize,
320                                unsigned int hookmask)
321 {
322         struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
323
324         if (targinfosize != IPT_ALIGN(sizeof(struct ipt_ulog_info))) {
325                 DEBUGP("ipt_ULOG: targinfosize %u != 0\n", targinfosize);
326                 return 0;
327         }
328
329         if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') {
330                 DEBUGP("ipt_ULOG: prefix term %i\n",
331                        loginfo->prefix[sizeof(loginfo->prefix) - 1]);
332                 return 0;
333         }
334
335         if (loginfo->qthreshold > ULOG_MAX_QLEN) {
336                 DEBUGP("ipt_ULOG: queue threshold %i > MAX_QLEN\n",
337                         loginfo->qthreshold);
338                 return 0;
339         }
340
341         return 1;
342 }
343
344 static struct ipt_target ipt_ulog_reg = {
345         .name           = "ULOG",
346         .target         = ipt_ulog_target,
347         .checkentry     = ipt_ulog_checkentry,
348         .me             = THIS_MODULE,
349 };
350
351 static int __init init(void)
352 {
353         int i;
354
355         DEBUGP("ipt_ULOG: init module\n");
356
357         if (nlbufsiz >= 128*1024) {
358                 printk("Netlink buffer has to be <= 128kB\n");
359                 return -EINVAL;
360         }
361
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;
367         }
368
369         nflognl = netlink_kernel_create(NETLINK_NFLOG, NULL);
370         if (!nflognl)
371                 return -ENOMEM;
372
373         if (ipt_register_target(&ipt_ulog_reg) != 0) {
374                 sock_release(nflognl->sk_socket);
375                 return -EINVAL;
376         }
377         if (nflog)
378                 nf_log_register(PF_INET, &ipt_logfn);
379         
380         return 0;
381 }
382
383 static void __exit fini(void)
384 {
385         ulog_buff_t *ub;
386         int i;
387
388         DEBUGP("ipt_ULOG: cleanup_module\n");
389
390         if (nflog)
391                 nf_log_unregister(PF_INET, &ipt_logfn);
392         ipt_unregister_target(&ipt_ulog_reg);
393         sock_release(nflognl->sk_socket);
394
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);
401                 }
402
403                 if (ub->skb) {
404                         kfree_skb(ub->skb);
405                         ub->skb = NULL;
406                 }
407         }
408
409 }
410
411 module_init(init);
412 module_exit(fini);