]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/netfilter/ipt_ULOG.c
[NET]: Fix packet timestamping.
[net-next-2.6.git] / net / ipv4 / netfilter / ipt_ULOG.c
CommitLineData
1da177e4
LT
1/*
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 *
38 * flushtimeout:
39 * Specify, after how many hundredths of a second the queue should be
40 * flushed even if it is not full yet.
41 *
42 * ipt_ULOG.c,v 1.22 2002/10/30 09:07:31 laforge Exp
43 */
44
45#include <linux/module.h>
46#include <linux/config.h>
47#include <linux/spinlock.h>
48#include <linux/socket.h>
49#include <linux/skbuff.h>
50#include <linux/kernel.h>
51#include <linux/timer.h>
52#include <linux/netlink.h>
53#include <linux/netdevice.h>
54#include <linux/mm.h>
55#include <linux/moduleparam.h>
56#include <linux/netfilter.h>
57#include <linux/netfilter_ipv4/ip_tables.h>
58#include <linux/netfilter_ipv4/ipt_ULOG.h>
1da177e4
LT
59#include <net/sock.h>
60#include <linux/bitops.h>
61
62MODULE_LICENSE("GPL");
63MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
64MODULE_DESCRIPTION("iptables userspace logging module");
4fdb3bb7 65MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NFLOG);
1da177e4
LT
66
67#define ULOG_NL_EVENT 111 /* Harald's favorite number */
68#define ULOG_MAXNLGROUPS 32 /* numer of nlgroups */
69
70#if 0
71#define DEBUGP(format, args...) printk("%s:%s:" format, \
72 __FILE__, __FUNCTION__ , ## args)
73#else
74#define DEBUGP(format, args...)
75#endif
76
77#define PRINTR(format, args...) do { if (net_ratelimit()) printk(format , ## args); } while (0)
78
79static unsigned int nlbufsiz = 4096;
80module_param(nlbufsiz, uint, 0600); /* FIXME: Check size < 128k --RR */
81MODULE_PARM_DESC(nlbufsiz, "netlink buffer size");
82
83static unsigned int flushtimeout = 10;
84module_param(flushtimeout, int, 0600);
85MODULE_PARM_DESC(flushtimeout, "buffer flush timeout (hundredths of a second)");
86
87static unsigned int nflog = 1;
88module_param(nflog, int, 0400);
89MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
90
91/* global data structures */
92
93typedef struct {
94 unsigned int qlen; /* number of nlmsgs' in the skb */
95 struct nlmsghdr *lastnlh; /* netlink header of last msg in skb */
96 struct sk_buff *skb; /* the pre-allocated skb */
97 struct timer_list timer; /* the timer function */
98} ulog_buff_t;
99
100static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS]; /* array of buffers */
101
e45b1be8
PM
102static struct sock *nflognl; /* our socket */
103static DEFINE_SPINLOCK(ulog_lock); /* spinlock */
1da177e4
LT
104
105/* send one ulog_buff_t to userspace */
106static void ulog_send(unsigned int nlgroupnum)
107{
108 ulog_buff_t *ub = &ulog_buffers[nlgroupnum];
109
110 if (timer_pending(&ub->timer)) {
111 DEBUGP("ipt_ULOG: ulog_send: timer was pending, deleting\n");
112 del_timer(&ub->timer);
113 }
114
115 /* last nlmsg needs NLMSG_DONE */
116 if (ub->qlen > 1)
117 ub->lastnlh->nlmsg_type = NLMSG_DONE;
118
ac6d439d
PM
119 NETLINK_CB(ub->skb).dst_group = nlgroupnum + 1;
120 DEBUGP("ipt_ULOG: throwing %d packets to netlink group %u\n",
121 ub->qlen, nlgroupnum + 1);
122 netlink_broadcast(nflognl, ub->skb, 0, nlgroupnum + 1, GFP_ATOMIC);
1da177e4
LT
123
124 ub->qlen = 0;
125 ub->skb = NULL;
126 ub->lastnlh = NULL;
127
128}
129
130
131/* timer function to flush queue in flushtimeout time */
132static void ulog_timer(unsigned long data)
133{
134 DEBUGP("ipt_ULOG: timer function called, calling ulog_send\n");
135
136 /* lock to protect against somebody modifying our structure
137 * from ipt_ulog_target at the same time */
e45b1be8 138 spin_lock_bh(&ulog_lock);
1da177e4 139 ulog_send(data);
e45b1be8 140 spin_unlock_bh(&ulog_lock);
1da177e4
LT
141}
142
143static struct sk_buff *ulog_alloc_skb(unsigned int size)
144{
145 struct sk_buff *skb;
146
147 /* alloc skb which should be big enough for a whole
148 * multipart message. WARNING: has to be <= 131000
149 * due to slab allocator restrictions */
150
151 skb = alloc_skb(nlbufsiz, GFP_ATOMIC);
152 if (!skb) {
153 PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n",
154 nlbufsiz);
155
156 /* try to allocate only as much as we need for
157 * current packet */
158
159 skb = alloc_skb(size, GFP_ATOMIC);
160 if (!skb)
161 PRINTR("ipt_ULOG: can't even allocate %ub\n", size);
162 }
163
164 return skb;
165}
166
167static void ipt_ulog_packet(unsigned int hooknum,
168 const struct sk_buff *skb,
169 const struct net_device *in,
170 const struct net_device *out,
171 const struct ipt_ulog_info *loginfo,
172 const char *prefix)
173{
174 ulog_buff_t *ub;
175 ulog_packet_msg_t *pm;
176 size_t size, copy_len;
177 struct nlmsghdr *nlh;
178
179 /* ffs == find first bit set, necessary because userspace
180 * is already shifting groupnumber, but we need unshifted.
181 * ffs() returns [1..32], we need [0..31] */
182 unsigned int groupnum = ffs(loginfo->nl_group) - 1;
183
184 /* calculate the size of the skb needed */
185 if ((loginfo->copy_range == 0) ||
186 (loginfo->copy_range > skb->len)) {
187 copy_len = skb->len;
188 } else {
189 copy_len = loginfo->copy_range;
190 }
191
192 size = NLMSG_SPACE(sizeof(*pm) + copy_len);
193
194 ub = &ulog_buffers[groupnum];
195
e45b1be8 196 spin_lock_bh(&ulog_lock);
1da177e4
LT
197
198 if (!ub->skb) {
199 if (!(ub->skb = ulog_alloc_skb(size)))
200 goto alloc_failure;
201 } else if (ub->qlen >= loginfo->qthreshold ||
202 size > skb_tailroom(ub->skb)) {
203 /* either the queue len is too high or we don't have
204 * enough room in nlskb left. send it to userspace. */
205
206 ulog_send(groupnum);
207
208 if (!(ub->skb = ulog_alloc_skb(size)))
209 goto alloc_failure;
210 }
211
212 DEBUGP("ipt_ULOG: qlen %d, qthreshold %d\n", ub->qlen,
213 loginfo->qthreshold);
214
215 /* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */
216 nlh = NLMSG_PUT(ub->skb, 0, ub->qlen, ULOG_NL_EVENT,
217 sizeof(*pm)+copy_len);
218 ub->qlen++;
219
220 pm = NLMSG_DATA(nlh);
221
222 /* We might not have a timestamp, get one */
a61bbcf2
PM
223 if (skb->tstamp.off_sec == 0)
224 __net_timestamp((struct sk_buff *)skb);
1da177e4
LT
225
226 /* copy hook, prefix, timestamp, payload, etc. */
227 pm->data_len = copy_len;
325ed823
HX
228 pm->timestamp_sec = skb->tstamp.off_sec;
229 pm->timestamp_usec = skb->tstamp.off_usec;
1da177e4
LT
230 pm->mark = skb->nfmark;
231 pm->hook = hooknum;
232 if (prefix != NULL)
233 strncpy(pm->prefix, prefix, sizeof(pm->prefix));
234 else if (loginfo->prefix[0] != '\0')
235 strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
236 else
237 *(pm->prefix) = '\0';
238
239 if (in && in->hard_header_len > 0
240 && skb->mac.raw != (void *) skb->nh.iph
241 && in->hard_header_len <= ULOG_MAC_LEN) {
242 memcpy(pm->mac, skb->mac.raw, in->hard_header_len);
243 pm->mac_len = in->hard_header_len;
244 } else
245 pm->mac_len = 0;
246
247 if (in)
248 strncpy(pm->indev_name, in->name, sizeof(pm->indev_name));
249 else
250 pm->indev_name[0] = '\0';
251
252 if (out)
253 strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
254 else
255 pm->outdev_name[0] = '\0';
256
257 /* copy_len <= skb->len, so can't fail. */
258 if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0)
259 BUG();
260
261 /* check if we are building multi-part messages */
262 if (ub->qlen > 1) {
263 ub->lastnlh->nlmsg_flags |= NLM_F_MULTI;
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 * HZ / 100;
271 add_timer(&ub->timer);
272 }
273
274 /* if threshold is reached, send message to userspace */
275 if (ub->qlen >= loginfo->qthreshold) {
276 if (loginfo->qthreshold > 1)
277 nlh->nlmsg_type = NLMSG_DONE;
278 ulog_send(groupnum);
279 }
280
e45b1be8 281 spin_unlock_bh(&ulog_lock);
1da177e4
LT
282
283 return;
284
285nlmsg_failure:
286 PRINTR("ipt_ULOG: error during NLMSG_PUT\n");
287
288alloc_failure:
289 PRINTR("ipt_ULOG: Error building netlink message\n");
290
e45b1be8 291 spin_unlock_bh(&ulog_lock);
1da177e4
LT
292}
293
294static unsigned int ipt_ulog_target(struct sk_buff **pskb,
295 const struct net_device *in,
296 const struct net_device *out,
297 unsigned int hooknum,
298 const void *targinfo, void *userinfo)
299{
300 struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
301
302 ipt_ulog_packet(hooknum, *pskb, in, out, loginfo, NULL);
303
304 return IPT_CONTINUE;
305}
306
608c8e4f
HW
307static void ipt_logfn(unsigned int pf,
308 unsigned int hooknum,
1da177e4
LT
309 const struct sk_buff *skb,
310 const struct net_device *in,
311 const struct net_device *out,
608c8e4f 312 const struct nf_loginfo *li,
1da177e4
LT
313 const char *prefix)
314{
608c8e4f
HW
315 struct ipt_ulog_info loginfo;
316
317 if (!li || li->type != NF_LOG_TYPE_ULOG) {
318 loginfo.nl_group = ULOG_DEFAULT_NLGROUP;
319 loginfo.copy_range = 0;
320 loginfo.qthreshold = ULOG_DEFAULT_QTHRESHOLD;
321 loginfo.prefix[0] = '\0';
322 } else {
323 loginfo.nl_group = li->u.ulog.group;
324 loginfo.copy_range = li->u.ulog.copy_len;
325 loginfo.qthreshold = li->u.ulog.qthreshold;
326 strlcpy(loginfo.prefix, prefix, sizeof(loginfo.prefix));
327 }
1da177e4
LT
328
329 ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix);
330}
331
332static int ipt_ulog_checkentry(const char *tablename,
333 const struct ipt_entry *e,
334 void *targinfo,
335 unsigned int targinfosize,
336 unsigned int hookmask)
337{
338 struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
339
340 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_ulog_info))) {
341 DEBUGP("ipt_ULOG: targinfosize %u != 0\n", targinfosize);
342 return 0;
343 }
344
345 if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') {
346 DEBUGP("ipt_ULOG: prefix term %i\n",
347 loginfo->prefix[sizeof(loginfo->prefix) - 1]);
348 return 0;
349 }
350
351 if (loginfo->qthreshold > ULOG_MAX_QLEN) {
352 DEBUGP("ipt_ULOG: queue threshold %i > MAX_QLEN\n",
353 loginfo->qthreshold);
354 return 0;
355 }
356
357 return 1;
358}
359
360static struct ipt_target ipt_ulog_reg = {
361 .name = "ULOG",
362 .target = ipt_ulog_target,
363 .checkentry = ipt_ulog_checkentry,
364 .me = THIS_MODULE,
365};
366
608c8e4f
HW
367static struct nf_logger ipt_ulog_logger = {
368 .name = "ipt_ULOG",
369 .logfn = &ipt_logfn,
370 .me = THIS_MODULE,
371};
372
1da177e4
LT
373static int __init init(void)
374{
375 int i;
376
377 DEBUGP("ipt_ULOG: init module\n");
378
379 if (nlbufsiz >= 128*1024) {
380 printk("Netlink buffer has to be <= 128kB\n");
381 return -EINVAL;
382 }
383
384 /* initialize ulog_buffers */
385 for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
386 init_timer(&ulog_buffers[i].timer);
387 ulog_buffers[i].timer.function = ulog_timer;
388 ulog_buffers[i].timer.data = i;
389 }
390
06628607
PM
391 nflognl = netlink_kernel_create(NETLINK_NFLOG, ULOG_MAXNLGROUPS, NULL,
392 THIS_MODULE);
1da177e4
LT
393 if (!nflognl)
394 return -ENOMEM;
395
396 if (ipt_register_target(&ipt_ulog_reg) != 0) {
397 sock_release(nflognl->sk_socket);
398 return -EINVAL;
399 }
400 if (nflog)
608c8e4f 401 nf_log_register(PF_INET, &ipt_ulog_logger);
1da177e4
LT
402
403 return 0;
404}
405
406static void __exit fini(void)
407{
408 ulog_buff_t *ub;
409 int i;
410
411 DEBUGP("ipt_ULOG: cleanup_module\n");
412
413 if (nflog)
608c8e4f 414 nf_log_unregister_logger(&ipt_ulog_logger);
1da177e4
LT
415 ipt_unregister_target(&ipt_ulog_reg);
416 sock_release(nflognl->sk_socket);
417
418 /* remove pending timers and free allocated skb's */
419 for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
420 ub = &ulog_buffers[i];
421 if (timer_pending(&ub->timer)) {
422 DEBUGP("timer was pending, deleting\n");
423 del_timer(&ub->timer);
424 }
425
426 if (ub->skb) {
427 kfree_skb(ub->skb);
428 ub->skb = NULL;
429 }
430 }
431
432}
433
434module_init(init);
435module_exit(fini);