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