Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6/libata-dev.git] / net / ipv4 / netfilter / ipt_ULOG.c
blob7d168dcbd135c7bf47c7772038454965311e9715
1 /*
2 * netfilter module for userspace packet logging daemons
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>
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.
12 * This module accepts two parameters:
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.
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.
28 * flushtimeout:
29 * Specify, after how many hundredths of a second the queue should be
30 * flushed even if it is not full yet.
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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);
57 #define ULOG_NL_EVENT 111 /* Harald's favorite number */
58 #define ULOG_MAXNLGROUPS 32 /* numer of nlgroups */
60 static unsigned int nlbufsiz = NLMSG_GOODSIZE;
61 module_param(nlbufsiz, uint, 0400);
62 MODULE_PARM_DESC(nlbufsiz, "netlink buffer size");
64 static unsigned int flushtimeout = 10;
65 module_param(flushtimeout, uint, 0600);
66 MODULE_PARM_DESC(flushtimeout, "buffer flush timeout (hundredths of a second)");
68 static bool nflog = true;
69 module_param(nflog, bool, 0400);
70 MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
72 /* global data structures */
74 typedef struct {
75 unsigned int qlen; /* number of nlmsgs' in the skb */
76 struct nlmsghdr *lastnlh; /* netlink header of last msg in skb */
77 struct sk_buff *skb; /* the pre-allocated skb */
78 struct timer_list timer; /* the timer function */
79 } ulog_buff_t;
81 static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS]; /* array of buffers */
83 static struct sock *nflognl; /* our socket */
84 static DEFINE_SPINLOCK(ulog_lock); /* spinlock */
86 /* send one ulog_buff_t to userspace */
87 static void ulog_send(unsigned int nlgroupnum)
89 ulog_buff_t *ub = &ulog_buffers[nlgroupnum];
91 pr_debug("ulog_send: timer is deleting\n");
92 del_timer(&ub->timer);
94 if (!ub->skb) {
95 pr_debug("ulog_send: nothing to send\n");
96 return;
99 /* last nlmsg needs NLMSG_DONE */
100 if (ub->qlen > 1)
101 ub->lastnlh->nlmsg_type = NLMSG_DONE;
103 NETLINK_CB(ub->skb).dst_group = nlgroupnum + 1;
104 pr_debug("throwing %d packets to netlink group %u\n",
105 ub->qlen, nlgroupnum + 1);
106 netlink_broadcast(nflognl, ub->skb, 0, nlgroupnum + 1, GFP_ATOMIC);
108 ub->qlen = 0;
109 ub->skb = NULL;
110 ub->lastnlh = NULL;
114 /* timer function to flush queue in flushtimeout time */
115 static void ulog_timer(unsigned long data)
117 pr_debug("timer function called, calling ulog_send\n");
119 /* lock to protect against somebody modifying our structure
120 * from ipt_ulog_target at the same time */
121 spin_lock_bh(&ulog_lock);
122 ulog_send(data);
123 spin_unlock_bh(&ulog_lock);
126 static struct sk_buff *ulog_alloc_skb(unsigned int size)
128 struct sk_buff *skb;
129 unsigned int n;
131 /* alloc skb which should be big enough for a whole
132 * multipart message. WARNING: has to be <= 131000
133 * due to slab allocator restrictions */
135 n = max(size, nlbufsiz);
136 skb = alloc_skb(n, GFP_ATOMIC | __GFP_NOWARN);
137 if (!skb) {
138 if (n > size) {
139 /* try to allocate only as much as we need for
140 * current packet */
142 skb = alloc_skb(size, GFP_ATOMIC);
143 if (!skb)
144 pr_debug("cannot even allocate %ub\n", size);
148 return skb;
151 static void ipt_ulog_packet(unsigned int hooknum,
152 const struct sk_buff *skb,
153 const struct net_device *in,
154 const struct net_device *out,
155 const struct ipt_ulog_info *loginfo,
156 const char *prefix)
158 ulog_buff_t *ub;
159 ulog_packet_msg_t *pm;
160 size_t size, copy_len;
161 struct nlmsghdr *nlh;
162 struct timeval tv;
164 /* ffs == find first bit set, necessary because userspace
165 * is already shifting groupnumber, but we need unshifted.
166 * ffs() returns [1..32], we need [0..31] */
167 unsigned int groupnum = ffs(loginfo->nl_group) - 1;
169 /* calculate the size of the skb needed */
170 if (loginfo->copy_range == 0 || loginfo->copy_range > skb->len)
171 copy_len = skb->len;
172 else
173 copy_len = loginfo->copy_range;
175 size = NLMSG_SPACE(sizeof(*pm) + copy_len);
177 ub = &ulog_buffers[groupnum];
179 spin_lock_bh(&ulog_lock);
181 if (!ub->skb) {
182 if (!(ub->skb = ulog_alloc_skb(size)))
183 goto alloc_failure;
184 } else if (ub->qlen >= loginfo->qthreshold ||
185 size > skb_tailroom(ub->skb)) {
186 /* either the queue len is too high or we don't have
187 * enough room in nlskb left. send it to userspace. */
189 ulog_send(groupnum);
191 if (!(ub->skb = ulog_alloc_skb(size)))
192 goto alloc_failure;
195 pr_debug("qlen %d, qthreshold %Zu\n", ub->qlen, loginfo->qthreshold);
197 nlh = nlmsg_put(ub->skb, 0, ub->qlen, ULOG_NL_EVENT,
198 sizeof(*pm)+copy_len, 0);
199 if (!nlh) {
200 pr_debug("error during nlmsg_put\n");
201 goto out_unlock;
203 ub->qlen++;
205 pm = nlmsg_data(nlh);
207 /* We might not have a timestamp, get one */
208 if (skb->tstamp.tv64 == 0)
209 __net_timestamp((struct sk_buff *)skb);
211 /* copy hook, prefix, timestamp, payload, etc. */
212 pm->data_len = copy_len;
213 tv = ktime_to_timeval(skb->tstamp);
214 put_unaligned(tv.tv_sec, &pm->timestamp_sec);
215 put_unaligned(tv.tv_usec, &pm->timestamp_usec);
216 put_unaligned(skb->mark, &pm->mark);
217 pm->hook = hooknum;
218 if (prefix != NULL)
219 strncpy(pm->prefix, prefix, sizeof(pm->prefix));
220 else if (loginfo->prefix[0] != '\0')
221 strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
222 else
223 *(pm->prefix) = '\0';
225 if (in && in->hard_header_len > 0 &&
226 skb->mac_header != skb->network_header &&
227 in->hard_header_len <= ULOG_MAC_LEN) {
228 memcpy(pm->mac, skb_mac_header(skb), in->hard_header_len);
229 pm->mac_len = in->hard_header_len;
230 } else
231 pm->mac_len = 0;
233 if (in)
234 strncpy(pm->indev_name, in->name, sizeof(pm->indev_name));
235 else
236 pm->indev_name[0] = '\0';
238 if (out)
239 strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
240 else
241 pm->outdev_name[0] = '\0';
243 /* copy_len <= skb->len, so can't fail. */
244 if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0)
245 BUG();
247 /* check if we are building multi-part messages */
248 if (ub->qlen > 1)
249 ub->lastnlh->nlmsg_flags |= NLM_F_MULTI;
251 ub->lastnlh = nlh;
253 /* if timer isn't already running, start it */
254 if (!timer_pending(&ub->timer)) {
255 ub->timer.expires = jiffies + flushtimeout * HZ / 100;
256 add_timer(&ub->timer);
259 /* if threshold is reached, send message to userspace */
260 if (ub->qlen >= loginfo->qthreshold) {
261 if (loginfo->qthreshold > 1)
262 nlh->nlmsg_type = NLMSG_DONE;
263 ulog_send(groupnum);
265 out_unlock:
266 spin_unlock_bh(&ulog_lock);
268 return;
270 alloc_failure:
271 pr_debug("Error building netlink message\n");
272 spin_unlock_bh(&ulog_lock);
275 static unsigned int
276 ulog_tg(struct sk_buff *skb, const struct xt_action_param *par)
278 ipt_ulog_packet(par->hooknum, skb, par->in, par->out,
279 par->targinfo, NULL);
280 return XT_CONTINUE;
283 static void ipt_logfn(u_int8_t pf,
284 unsigned int hooknum,
285 const struct sk_buff *skb,
286 const struct net_device *in,
287 const struct net_device *out,
288 const struct nf_loginfo *li,
289 const char *prefix)
291 struct ipt_ulog_info loginfo;
293 if (!li || li->type != NF_LOG_TYPE_ULOG) {
294 loginfo.nl_group = ULOG_DEFAULT_NLGROUP;
295 loginfo.copy_range = 0;
296 loginfo.qthreshold = ULOG_DEFAULT_QTHRESHOLD;
297 loginfo.prefix[0] = '\0';
298 } else {
299 loginfo.nl_group = li->u.ulog.group;
300 loginfo.copy_range = li->u.ulog.copy_len;
301 loginfo.qthreshold = li->u.ulog.qthreshold;
302 strlcpy(loginfo.prefix, prefix, sizeof(loginfo.prefix));
305 ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix);
308 static int ulog_tg_check(const struct xt_tgchk_param *par)
310 const struct ipt_ulog_info *loginfo = par->targinfo;
312 if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') {
313 pr_debug("prefix not null-terminated\n");
314 return -EINVAL;
316 if (loginfo->qthreshold > ULOG_MAX_QLEN) {
317 pr_debug("queue threshold %Zu > MAX_QLEN\n",
318 loginfo->qthreshold);
319 return -EINVAL;
321 return 0;
324 #ifdef CONFIG_COMPAT
325 struct compat_ipt_ulog_info {
326 compat_uint_t nl_group;
327 compat_size_t copy_range;
328 compat_size_t qthreshold;
329 char prefix[ULOG_PREFIX_LEN];
332 static void ulog_tg_compat_from_user(void *dst, const void *src)
334 const struct compat_ipt_ulog_info *cl = src;
335 struct ipt_ulog_info l = {
336 .nl_group = cl->nl_group,
337 .copy_range = cl->copy_range,
338 .qthreshold = cl->qthreshold,
341 memcpy(l.prefix, cl->prefix, sizeof(l.prefix));
342 memcpy(dst, &l, sizeof(l));
345 static int ulog_tg_compat_to_user(void __user *dst, const void *src)
347 const struct ipt_ulog_info *l = src;
348 struct compat_ipt_ulog_info cl = {
349 .nl_group = l->nl_group,
350 .copy_range = l->copy_range,
351 .qthreshold = l->qthreshold,
354 memcpy(cl.prefix, l->prefix, sizeof(cl.prefix));
355 return copy_to_user(dst, &cl, sizeof(cl)) ? -EFAULT : 0;
357 #endif /* CONFIG_COMPAT */
359 static struct xt_target ulog_tg_reg __read_mostly = {
360 .name = "ULOG",
361 .family = NFPROTO_IPV4,
362 .target = ulog_tg,
363 .targetsize = sizeof(struct ipt_ulog_info),
364 .checkentry = ulog_tg_check,
365 #ifdef CONFIG_COMPAT
366 .compatsize = sizeof(struct compat_ipt_ulog_info),
367 .compat_from_user = ulog_tg_compat_from_user,
368 .compat_to_user = ulog_tg_compat_to_user,
369 #endif
370 .me = THIS_MODULE,
373 static struct nf_logger ipt_ulog_logger __read_mostly = {
374 .name = "ipt_ULOG",
375 .logfn = ipt_logfn,
376 .me = THIS_MODULE,
379 static int __init ulog_tg_init(void)
381 int ret, i;
382 struct netlink_kernel_cfg cfg = {
383 .groups = ULOG_MAXNLGROUPS,
386 pr_debug("init module\n");
388 if (nlbufsiz > 128*1024) {
389 pr_warning("Netlink buffer has to be <= 128kB\n");
390 return -EINVAL;
393 /* initialize ulog_buffers */
394 for (i = 0; i < ULOG_MAXNLGROUPS; i++)
395 setup_timer(&ulog_buffers[i].timer, ulog_timer, i);
397 nflognl = netlink_kernel_create(&init_net, NETLINK_NFLOG, &cfg);
398 if (!nflognl)
399 return -ENOMEM;
401 ret = xt_register_target(&ulog_tg_reg);
402 if (ret < 0) {
403 netlink_kernel_release(nflognl);
404 return ret;
406 if (nflog)
407 nf_log_register(NFPROTO_IPV4, &ipt_ulog_logger);
409 return 0;
412 static void __exit ulog_tg_exit(void)
414 ulog_buff_t *ub;
415 int i;
417 pr_debug("cleanup_module\n");
419 if (nflog)
420 nf_log_unregister(&ipt_ulog_logger);
421 xt_unregister_target(&ulog_tg_reg);
422 netlink_kernel_release(nflognl);
424 /* remove pending timers and free allocated skb's */
425 for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
426 ub = &ulog_buffers[i];
427 pr_debug("timer is deleting\n");
428 del_timer(&ub->timer);
430 if (ub->skb) {
431 kfree_skb(ub->skb);
432 ub->skb = NULL;
437 module_init(ulog_tg_init);
438 module_exit(ulog_tg_exit);