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