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