blob: 4c5f0a117862cdbf240af92836973d87c44983c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * netfilter module for userspace packet logging daemons
3 *
4 * (C) 2000-2004 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 * 2004/10/25 fix erroneous calculation of 'len' parameter to NLMSG_PUT
17 * resulting in bogus 'error during NLMSG_PUT' messages.
18 *
19 * (C) 1999-2001 Paul `Rusty' Russell
20 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
21 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2 as
24 * published by the Free Software Foundation.
25 *
26 * This module accepts two parameters:
27 *
28 * nlbufsiz:
29 * The parameter specifies how big the buffer for each netlink multicast
30 * group is. e.g. If you say nlbufsiz=8192, up to eight kb of packets will
31 * get accumulated in the kernel until they are sent to userspace. It is
32 * NOT possible to allocate more than 128kB, and it is strongly discouraged,
33 * because atomically allocating 128kB inside the network rx softirq is not
34 * reliable. Please also keep in mind that this buffer size is allocated for
35 * each nlgroup you are using, so the total kernel memory usage increases
36 * by that factor.
37 *
Holger Eitzenbergerc2db2922006-02-04 02:13:14 -080038 * Actually you should use nlbufsiz a bit smaller than PAGE_SIZE, since
39 * nlbufsiz is used with alloc_skb, which adds another
40 * sizeof(struct skb_shared_info). Use NLMSG_GOODSIZE instead.
41 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * flushtimeout:
43 * Specify, after how many hundredths of a second the queue should be
44 * flushed even if it is not full yet.
45 *
46 * ipt_ULOG.c,v 1.22 2002/10/30 09:07:31 laforge Exp
47 */
48
49#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/spinlock.h>
51#include <linux/socket.h>
52#include <linux/skbuff.h>
53#include <linux/kernel.h>
54#include <linux/timer.h>
55#include <linux/netlink.h>
56#include <linux/netdevice.h>
57#include <linux/mm.h>
58#include <linux/moduleparam.h>
59#include <linux/netfilter.h>
60#include <linux/netfilter_ipv4/ip_tables.h>
61#include <linux/netfilter_ipv4/ipt_ULOG.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <net/sock.h>
63#include <linux/bitops.h>
64
65MODULE_LICENSE("GPL");
66MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
67MODULE_DESCRIPTION("iptables userspace logging module");
Harald Welte4fdb3bb2005-08-09 19:40:55 -070068MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NFLOG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#define ULOG_NL_EVENT 111 /* Harald's favorite number */
71#define ULOG_MAXNLGROUPS 32 /* numer of nlgroups */
72
73#if 0
74#define DEBUGP(format, args...) printk("%s:%s:" format, \
75 __FILE__, __FUNCTION__ , ## args)
76#else
77#define DEBUGP(format, args...)
78#endif
79
80#define PRINTR(format, args...) do { if (net_ratelimit()) printk(format , ## args); } while (0)
81
Holger Eitzenbergerc2db2922006-02-04 02:13:14 -080082static unsigned int nlbufsiz = NLMSG_GOODSIZE;
Patrick McHardye7be6992006-01-05 12:19:46 -080083module_param(nlbufsiz, uint, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_PARM_DESC(nlbufsiz, "netlink buffer size");
85
86static unsigned int flushtimeout = 10;
Patrick McHardye7be6992006-01-05 12:19:46 -080087module_param(flushtimeout, uint, 0600);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088MODULE_PARM_DESC(flushtimeout, "buffer flush timeout (hundredths of a second)");
89
Patrick McHardye7be6992006-01-05 12:19:46 -080090static int nflog = 1;
91module_param(nflog, bool, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
93
94/* global data structures */
95
96typedef struct {
97 unsigned int qlen; /* number of nlmsgs' in the skb */
98 struct nlmsghdr *lastnlh; /* netlink header of last msg in skb */
99 struct sk_buff *skb; /* the pre-allocated skb */
100 struct timer_list timer; /* the timer function */
101} ulog_buff_t;
102
103static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS]; /* array of buffers */
104
Patrick McHardye45b1be2005-06-21 14:01:30 -0700105static struct sock *nflognl; /* our socket */
106static DEFINE_SPINLOCK(ulog_lock); /* spinlock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/* send one ulog_buff_t to userspace */
109static void ulog_send(unsigned int nlgroupnum)
110{
111 ulog_buff_t *ub = &ulog_buffers[nlgroupnum];
112
113 if (timer_pending(&ub->timer)) {
114 DEBUGP("ipt_ULOG: ulog_send: timer was pending, deleting\n");
115 del_timer(&ub->timer);
116 }
117
Mark Huangdcb7cd92006-08-13 18:57:54 -0700118 if (!ub->skb) {
119 DEBUGP("ipt_ULOG: ulog_send: nothing to send\n");
120 return;
121 }
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /* last nlmsg needs NLMSG_DONE */
124 if (ub->qlen > 1)
125 ub->lastnlh->nlmsg_type = NLMSG_DONE;
126
Patrick McHardyac6d4392005-08-14 19:29:52 -0700127 NETLINK_CB(ub->skb).dst_group = nlgroupnum + 1;
128 DEBUGP("ipt_ULOG: throwing %d packets to netlink group %u\n",
129 ub->qlen, nlgroupnum + 1);
130 netlink_broadcast(nflognl, ub->skb, 0, nlgroupnum + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 ub->qlen = 0;
133 ub->skb = NULL;
134 ub->lastnlh = NULL;
135
136}
137
138
139/* timer function to flush queue in flushtimeout time */
140static void ulog_timer(unsigned long data)
141{
142 DEBUGP("ipt_ULOG: timer function called, calling ulog_send\n");
143
144 /* lock to protect against somebody modifying our structure
145 * from ipt_ulog_target at the same time */
Patrick McHardye45b1be2005-06-21 14:01:30 -0700146 spin_lock_bh(&ulog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 ulog_send(data);
Patrick McHardye45b1be2005-06-21 14:01:30 -0700148 spin_unlock_bh(&ulog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151static struct sk_buff *ulog_alloc_skb(unsigned int size)
152{
153 struct sk_buff *skb;
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800154 unsigned int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /* alloc skb which should be big enough for a whole
157 * multipart message. WARNING: has to be <= 131000
158 * due to slab allocator restrictions */
159
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800160 n = max(size, nlbufsiz);
161 skb = alloc_skb(n, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!skb) {
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800163 PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800165 if (n > size) {
166 /* try to allocate only as much as we need for
167 * current packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800169 skb = alloc_skb(size, GFP_ATOMIC);
170 if (!skb)
171 PRINTR("ipt_ULOG: can't even allocate %ub\n",
172 size);
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
176 return skb;
177}
178
179static void ipt_ulog_packet(unsigned int hooknum,
180 const struct sk_buff *skb,
181 const struct net_device *in,
182 const struct net_device *out,
183 const struct ipt_ulog_info *loginfo,
184 const char *prefix)
185{
186 ulog_buff_t *ub;
187 ulog_packet_msg_t *pm;
188 size_t size, copy_len;
189 struct nlmsghdr *nlh;
190
191 /* ffs == find first bit set, necessary because userspace
192 * is already shifting groupnumber, but we need unshifted.
193 * ffs() returns [1..32], we need [0..31] */
194 unsigned int groupnum = ffs(loginfo->nl_group) - 1;
195
196 /* calculate the size of the skb needed */
197 if ((loginfo->copy_range == 0) ||
198 (loginfo->copy_range > skb->len)) {
199 copy_len = skb->len;
200 } else {
201 copy_len = loginfo->copy_range;
202 }
203
204 size = NLMSG_SPACE(sizeof(*pm) + copy_len);
205
206 ub = &ulog_buffers[groupnum];
207
Patrick McHardye45b1be2005-06-21 14:01:30 -0700208 spin_lock_bh(&ulog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 if (!ub->skb) {
211 if (!(ub->skb = ulog_alloc_skb(size)))
212 goto alloc_failure;
213 } else if (ub->qlen >= loginfo->qthreshold ||
214 size > skb_tailroom(ub->skb)) {
215 /* either the queue len is too high or we don't have
216 * enough room in nlskb left. send it to userspace. */
217
218 ulog_send(groupnum);
219
220 if (!(ub->skb = ulog_alloc_skb(size)))
221 goto alloc_failure;
222 }
223
224 DEBUGP("ipt_ULOG: qlen %d, qthreshold %d\n", ub->qlen,
225 loginfo->qthreshold);
226
227 /* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */
228 nlh = NLMSG_PUT(ub->skb, 0, ub->qlen, ULOG_NL_EVENT,
229 sizeof(*pm)+copy_len);
230 ub->qlen++;
231
232 pm = NLMSG_DATA(nlh);
233
234 /* We might not have a timestamp, get one */
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700235 if (skb->tstamp.off_sec == 0)
236 __net_timestamp((struct sk_buff *)skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /* copy hook, prefix, timestamp, payload, etc. */
239 pm->data_len = copy_len;
Herbert Xu325ed822005-10-03 13:57:23 -0700240 pm->timestamp_sec = skb->tstamp.off_sec;
241 pm->timestamp_usec = skb->tstamp.off_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 pm->mark = skb->nfmark;
243 pm->hook = hooknum;
244 if (prefix != NULL)
245 strncpy(pm->prefix, prefix, sizeof(pm->prefix));
246 else if (loginfo->prefix[0] != '\0')
247 strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
248 else
249 *(pm->prefix) = '\0';
250
251 if (in && in->hard_header_len > 0
252 && skb->mac.raw != (void *) skb->nh.iph
253 && in->hard_header_len <= ULOG_MAC_LEN) {
254 memcpy(pm->mac, skb->mac.raw, in->hard_header_len);
255 pm->mac_len = in->hard_header_len;
256 } else
257 pm->mac_len = 0;
258
259 if (in)
260 strncpy(pm->indev_name, in->name, sizeof(pm->indev_name));
261 else
262 pm->indev_name[0] = '\0';
263
264 if (out)
265 strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
266 else
267 pm->outdev_name[0] = '\0';
268
269 /* copy_len <= skb->len, so can't fail. */
270 if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0)
271 BUG();
272
273 /* check if we are building multi-part messages */
274 if (ub->qlen > 1) {
275 ub->lastnlh->nlmsg_flags |= NLM_F_MULTI;
276 }
277
278 ub->lastnlh = nlh;
279
280 /* if timer isn't already running, start it */
281 if (!timer_pending(&ub->timer)) {
282 ub->timer.expires = jiffies + flushtimeout * HZ / 100;
283 add_timer(&ub->timer);
284 }
285
286 /* if threshold is reached, send message to userspace */
287 if (ub->qlen >= loginfo->qthreshold) {
288 if (loginfo->qthreshold > 1)
289 nlh->nlmsg_type = NLMSG_DONE;
290 ulog_send(groupnum);
291 }
292
Patrick McHardye45b1be2005-06-21 14:01:30 -0700293 spin_unlock_bh(&ulog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 return;
296
297nlmsg_failure:
298 PRINTR("ipt_ULOG: error during NLMSG_PUT\n");
299
300alloc_failure:
301 PRINTR("ipt_ULOG: Error building netlink message\n");
302
Patrick McHardye45b1be2005-06-21 14:01:30 -0700303 spin_unlock_bh(&ulog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306static unsigned int ipt_ulog_target(struct sk_buff **pskb,
307 const struct net_device *in,
308 const struct net_device *out,
309 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800310 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700311 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
314
315 ipt_ulog_packet(hooknum, *pskb, in, out, loginfo, NULL);
316
317 return IPT_CONTINUE;
318}
319
Harald Welte608c8e42005-08-09 19:58:27 -0700320static void ipt_logfn(unsigned int pf,
321 unsigned int hooknum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 const struct sk_buff *skb,
323 const struct net_device *in,
324 const struct net_device *out,
Harald Welte608c8e42005-08-09 19:58:27 -0700325 const struct nf_loginfo *li,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 const char *prefix)
327{
Harald Welte608c8e42005-08-09 19:58:27 -0700328 struct ipt_ulog_info loginfo;
329
330 if (!li || li->type != NF_LOG_TYPE_ULOG) {
331 loginfo.nl_group = ULOG_DEFAULT_NLGROUP;
332 loginfo.copy_range = 0;
333 loginfo.qthreshold = ULOG_DEFAULT_QTHRESHOLD;
334 loginfo.prefix[0] = '\0';
335 } else {
336 loginfo.nl_group = li->u.ulog.group;
337 loginfo.copy_range = li->u.ulog.copy_len;
338 loginfo.qthreshold = li->u.ulog.qthreshold;
339 strlcpy(loginfo.prefix, prefix, sizeof(loginfo.prefix));
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix);
343}
344
345static int ipt_ulog_checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800346 const void *e,
Patrick McHardyc4986732006-03-20 18:02:56 -0800347 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 void *targinfo,
349 unsigned int targinfosize,
350 unsigned int hookmask)
351{
352 struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') {
355 DEBUGP("ipt_ULOG: prefix term %i\n",
356 loginfo->prefix[sizeof(loginfo->prefix) - 1]);
357 return 0;
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (loginfo->qthreshold > ULOG_MAX_QLEN) {
360 DEBUGP("ipt_ULOG: queue threshold %i > MAX_QLEN\n",
361 loginfo->qthreshold);
362 return 0;
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 1;
365}
366
367static struct ipt_target ipt_ulog_reg = {
368 .name = "ULOG",
369 .target = ipt_ulog_target,
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800370 .targetsize = sizeof(struct ipt_ulog_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 .checkentry = ipt_ulog_checkentry,
372 .me = THIS_MODULE,
373};
374
Harald Welte608c8e42005-08-09 19:58:27 -0700375static struct nf_logger ipt_ulog_logger = {
376 .name = "ipt_ULOG",
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800377 .logfn = ipt_logfn,
Harald Welte608c8e42005-08-09 19:58:27 -0700378 .me = THIS_MODULE,
379};
380
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800381static int __init ipt_ulog_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 int i;
384
385 DEBUGP("ipt_ULOG: init module\n");
386
Patrick McHardye7be6992006-01-05 12:19:46 -0800387 if (nlbufsiz > 128*1024) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 printk("Netlink buffer has to be <= 128kB\n");
389 return -EINVAL;
390 }
391
392 /* initialize ulog_buffers */
393 for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
394 init_timer(&ulog_buffers[i].timer);
395 ulog_buffers[i].timer.function = ulog_timer;
396 ulog_buffers[i].timer.data = i;
397 }
398
Patrick McHardy06628602005-08-15 12:33:26 -0700399 nflognl = netlink_kernel_create(NETLINK_NFLOG, ULOG_MAXNLGROUPS, NULL,
400 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (!nflognl)
402 return -ENOMEM;
403
404 if (ipt_register_target(&ipt_ulog_reg) != 0) {
405 sock_release(nflognl->sk_socket);
406 return -EINVAL;
407 }
408 if (nflog)
Harald Welte608c8e42005-08-09 19:58:27 -0700409 nf_log_register(PF_INET, &ipt_ulog_logger);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 return 0;
412}
413
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800414static void __exit ipt_ulog_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 ulog_buff_t *ub;
417 int i;
418
419 DEBUGP("ipt_ULOG: cleanup_module\n");
420
421 if (nflog)
Harald Welte608c8e42005-08-09 19:58:27 -0700422 nf_log_unregister_logger(&ipt_ulog_logger);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 ipt_unregister_target(&ipt_ulog_reg);
424 sock_release(nflognl->sk_socket);
425
426 /* remove pending timers and free allocated skb's */
427 for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
428 ub = &ulog_buffers[i];
429 if (timer_pending(&ub->timer)) {
430 DEBUGP("timer was pending, deleting\n");
431 del_timer(&ub->timer);
432 }
433
434 if (ub->skb) {
435 kfree_skb(ub->skb);
436 ub->skb = NULL;
437 }
438 }
439
440}
441
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800442module_init(ipt_ulog_init);
443module_exit(ipt_ulog_fini);