netfilter: xtables: make use of caller family rather than target family
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
f01ffbd6 46#include <net/netfilter/nf_log.h>
1da177e4
LT
47#include <net/sock.h>
48#include <linux/bitops.h>
01102e7c 49#include <asm/unaligned.h>
1da177e4
LT
50
51MODULE_LICENSE("GPL");
52MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
2ae15b64 53MODULE_DESCRIPTION("Xtables: packet logging to netlink using ULOG");
4fdb3bb7 54MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NFLOG);
1da177e4
LT
55
56#define ULOG_NL_EVENT 111 /* Harald's favorite number */
57#define ULOG_MAXNLGROUPS 32 /* numer of nlgroups */
58
1da177e4
LT
59#define PRINTR(format, args...) do { if (net_ratelimit()) printk(format , ## args); } while (0)
60
c2db2924 61static unsigned int nlbufsiz = NLMSG_GOODSIZE;
e7be6994 62module_param(nlbufsiz, uint, 0400);
1da177e4
LT
63MODULE_PARM_DESC(nlbufsiz, "netlink buffer size");
64
65static unsigned int flushtimeout = 10;
e7be6994 66module_param(flushtimeout, uint, 0600);
1da177e4
LT
67MODULE_PARM_DESC(flushtimeout, "buffer flush timeout (hundredths of a second)");
68
e7be6994
PM
69static int nflog = 1;
70module_param(nflog, bool, 0400);
1da177e4
LT
71MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
72
73/* global data structures */
74
75typedef 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
82static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS]; /* array of buffers */
83
e45b1be8
PM
84static struct sock *nflognl; /* our socket */
85static DEFINE_SPINLOCK(ulog_lock); /* spinlock */
1da177e4
LT
86
87/* send one ulog_buff_t to userspace */
88static void ulog_send(unsigned int nlgroupnum)
89{
90 ulog_buff_t *ub = &ulog_buffers[nlgroupnum];
91
92 if (timer_pending(&ub->timer)) {
0d53778e 93 pr_debug("ipt_ULOG: ulog_send: timer was pending, deleting\n");
1da177e4
LT
94 del_timer(&ub->timer);
95 }
96
dcb7cd97 97 if (!ub->skb) {
0d53778e 98 pr_debug("ipt_ULOG: ulog_send: nothing to send\n");
dcb7cd97
MH
99 return;
100 }
101
1da177e4
LT
102 /* last nlmsg needs NLMSG_DONE */
103 if (ub->qlen > 1)
104 ub->lastnlh->nlmsg_type = NLMSG_DONE;
105
ac6d439d 106 NETLINK_CB(ub->skb).dst_group = nlgroupnum + 1;
0d53778e
PM
107 pr_debug("ipt_ULOG: throwing %d packets to netlink group %u\n",
108 ub->qlen, nlgroupnum + 1);
ac6d439d 109 netlink_broadcast(nflognl, ub->skb, 0, nlgroupnum + 1, GFP_ATOMIC);
1da177e4
LT
110
111 ub->qlen = 0;
112 ub->skb = NULL;
113 ub->lastnlh = NULL;
1da177e4
LT
114}
115
116
117/* timer function to flush queue in flushtimeout time */
118static void ulog_timer(unsigned long data)
119{
0d53778e 120 pr_debug("ipt_ULOG: timer function called, calling ulog_send\n");
1da177e4
LT
121
122 /* lock to protect against somebody modifying our structure
123 * from ipt_ulog_target at the same time */
e45b1be8 124 spin_lock_bh(&ulog_lock);
1da177e4 125 ulog_send(data);
e45b1be8 126 spin_unlock_bh(&ulog_lock);
1da177e4
LT
127}
128
129static struct sk_buff *ulog_alloc_skb(unsigned int size)
130{
131 struct sk_buff *skb;
ad2ad0f9 132 unsigned int n;
1da177e4
LT
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
ad2ad0f9
PM
138 n = max(size, nlbufsiz);
139 skb = alloc_skb(n, GFP_ATOMIC);
1da177e4 140 if (!skb) {
ad2ad0f9 141 PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n", n);
1da177e4 142
ad2ad0f9 143 if (n > size) {
e905a9ed 144 /* try to allocate only as much as we need for
ad2ad0f9 145 * current packet */
1da177e4 146
ad2ad0f9
PM
147 skb = alloc_skb(size, GFP_ATOMIC);
148 if (!skb)
149 PRINTR("ipt_ULOG: can't even allocate %ub\n",
150 size);
151 }
1da177e4
LT
152 }
153
154 return skb;
155}
156
157static 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;
b7aa0bf7 168 struct timeval tv;
1da177e4
LT
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 */
7c4e36bc 176 if (loginfo->copy_range == 0 || loginfo->copy_range > skb->len)
1da177e4 177 copy_len = skb->len;
7c4e36bc 178 else
1da177e4 179 copy_len = loginfo->copy_range;
1da177e4
LT
180
181 size = NLMSG_SPACE(sizeof(*pm) + copy_len);
182
183 ub = &ulog_buffers[groupnum];
e905a9ed 184
e45b1be8 185 spin_lock_bh(&ulog_lock);
1da177e4
LT
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)) {
e905a9ed 192 /* either the queue len is too high or we don't have
1da177e4
LT
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
0d53778e
PM
201 pr_debug("ipt_ULOG: qlen %d, qthreshold %Zu\n", ub->qlen,
202 loginfo->qthreshold);
1da177e4
LT
203
204 /* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */
e905a9ed 205 nlh = NLMSG_PUT(ub->skb, 0, ub->qlen, ULOG_NL_EVENT,
1da177e4
LT
206 sizeof(*pm)+copy_len);
207 ub->qlen++;
208
209 pm = NLMSG_DATA(nlh);
210
211 /* We might not have a timestamp, get one */
b7aa0bf7 212 if (skb->tstamp.tv64 == 0)
a61bbcf2 213 __net_timestamp((struct sk_buff *)skb);
1da177e4
LT
214
215 /* copy hook, prefix, timestamp, payload, etc. */
216 pm->data_len = copy_len;
b7aa0bf7
ED
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);
01102e7c 220 put_unaligned(skb->mark, &pm->mark);
1da177e4
LT
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
3666ed1c
JP
229 if (in && in->hard_header_len > 0 &&
230 skb->mac_header != skb->network_header &&
231 in->hard_header_len <= ULOG_MAC_LEN) {
98e399f8 232 memcpy(pm->mac, skb_mac_header(skb), in->hard_header_len);
1da177e4
LT
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();
e905a9ed 250
1da177e4 251 /* check if we are building multi-part messages */
7c4e36bc 252 if (ub->qlen > 1)
1da177e4 253 ub->lastnlh->nlmsg_flags |= NLM_F_MULTI;
1da177e4
LT
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
e45b1be8 270 spin_unlock_bh(&ulog_lock);
1da177e4
LT
271
272 return;
273
274nlmsg_failure:
275 PRINTR("ipt_ULOG: error during NLMSG_PUT\n");
276
277alloc_failure:
278 PRINTR("ipt_ULOG: Error building netlink message\n");
279
e45b1be8 280 spin_unlock_bh(&ulog_lock);
1da177e4
LT
281}
282
d3c5ee6d 283static unsigned int
7eb35586 284ulog_tg(struct sk_buff *skb, const struct xt_target_param *par)
1da177e4 285{
7eb35586
JE
286 ipt_ulog_packet(par->hooknum, skb, par->in, par->out,
287 par->targinfo, NULL);
6709dbbb 288 return XT_CONTINUE;
1da177e4 289}
e905a9ed 290
76108cea 291static void ipt_logfn(u_int8_t pf,
608c8e4f 292 unsigned int hooknum,
1da177e4
LT
293 const struct sk_buff *skb,
294 const struct net_device *in,
295 const struct net_device *out,
608c8e4f 296 const struct nf_loginfo *li,
1da177e4
LT
297 const char *prefix)
298{
608c8e4f
HW
299 struct ipt_ulog_info loginfo;
300
301 if (!li || li->type != NF_LOG_TYPE_ULOG) {
302 loginfo.nl_group = ULOG_DEFAULT_NLGROUP;
303 loginfo.copy_range = 0;
304 loginfo.qthreshold = ULOG_DEFAULT_QTHRESHOLD;
305 loginfo.prefix[0] = '\0';
306 } else {
307 loginfo.nl_group = li->u.ulog.group;
308 loginfo.copy_range = li->u.ulog.copy_len;
309 loginfo.qthreshold = li->u.ulog.qthreshold;
310 strlcpy(loginfo.prefix, prefix, sizeof(loginfo.prefix));
311 }
1da177e4
LT
312
313 ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix);
314}
315
af5d6dc2 316static bool ulog_tg_check(const struct xt_tgchk_param *par)
1da177e4 317{
af5d6dc2 318 const struct ipt_ulog_info *loginfo = par->targinfo;
1da177e4 319
1da177e4 320 if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') {
0d53778e
PM
321 pr_debug("ipt_ULOG: prefix term %i\n",
322 loginfo->prefix[sizeof(loginfo->prefix) - 1]);
e1931b78 323 return false;
1da177e4 324 }
1da177e4 325 if (loginfo->qthreshold > ULOG_MAX_QLEN) {
0d53778e
PM
326 pr_debug("ipt_ULOG: queue threshold %Zu > MAX_QLEN\n",
327 loginfo->qthreshold);
e1931b78 328 return false;
1da177e4 329 }
e1931b78 330 return true;
1da177e4
LT
331}
332
b076deb8
PM
333#ifdef CONFIG_COMPAT
334struct compat_ipt_ulog_info {
335 compat_uint_t nl_group;
336 compat_size_t copy_range;
337 compat_size_t qthreshold;
338 char prefix[ULOG_PREFIX_LEN];
339};
340
739674fb 341static void ulog_tg_compat_from_user(void *dst, const void *src)
b076deb8 342{
a47362a2 343 const struct compat_ipt_ulog_info *cl = src;
b076deb8
PM
344 struct ipt_ulog_info l = {
345 .nl_group = cl->nl_group,
346 .copy_range = cl->copy_range,
347 .qthreshold = cl->qthreshold,
348 };
349
350 memcpy(l.prefix, cl->prefix, sizeof(l.prefix));
351 memcpy(dst, &l, sizeof(l));
352}
353
739674fb 354static int ulog_tg_compat_to_user(void __user *dst, const void *src)
b076deb8 355{
a47362a2 356 const struct ipt_ulog_info *l = src;
b076deb8
PM
357 struct compat_ipt_ulog_info cl = {
358 .nl_group = l->nl_group,
359 .copy_range = l->copy_range,
360 .qthreshold = l->qthreshold,
361 };
362
363 memcpy(cl.prefix, l->prefix, sizeof(cl.prefix));
364 return copy_to_user(dst, &cl, sizeof(cl)) ? -EFAULT : 0;
365}
366#endif /* CONFIG_COMPAT */
367
d3c5ee6d 368static struct xt_target ulog_tg_reg __read_mostly = {
1da177e4 369 .name = "ULOG",
ee999d8b 370 .family = NFPROTO_IPV4,
d3c5ee6d 371 .target = ulog_tg,
1d5cd909 372 .targetsize = sizeof(struct ipt_ulog_info),
d3c5ee6d 373 .checkentry = ulog_tg_check,
b076deb8
PM
374#ifdef CONFIG_COMPAT
375 .compatsize = sizeof(struct compat_ipt_ulog_info),
d3c5ee6d
JE
376 .compat_from_user = ulog_tg_compat_from_user,
377 .compat_to_user = ulog_tg_compat_to_user,
b076deb8 378#endif
1da177e4
LT
379 .me = THIS_MODULE,
380};
381
ca735b3a 382static struct nf_logger ipt_ulog_logger __read_mostly = {
608c8e4f 383 .name = "ipt_ULOG",
1d5cd909 384 .logfn = ipt_logfn,
608c8e4f
HW
385 .me = THIS_MODULE,
386};
387
d3c5ee6d 388static int __init ulog_tg_init(void)
1da177e4 389{
e1fd0586 390 int ret, i;
1da177e4 391
0d53778e 392 pr_debug("ipt_ULOG: init module\n");
1da177e4 393
e7be6994 394 if (nlbufsiz > 128*1024) {
1da177e4
LT
395 printk("Netlink buffer has to be <= 128kB\n");
396 return -EINVAL;
397 }
398
399 /* initialize ulog_buffers */
e6f689db
PM
400 for (i = 0; i < ULOG_MAXNLGROUPS; i++)
401 setup_timer(&ulog_buffers[i].timer, ulog_timer, i);
1da177e4 402
b4b51029
EB
403 nflognl = netlink_kernel_create(&init_net,
404 NETLINK_NFLOG, ULOG_MAXNLGROUPS, NULL,
af65bdfc 405 NULL, THIS_MODULE);
1da177e4
LT
406 if (!nflognl)
407 return -ENOMEM;
408
d3c5ee6d 409 ret = xt_register_target(&ulog_tg_reg);
e1fd0586 410 if (ret < 0) {
b7c6ba6e 411 netlink_kernel_release(nflognl);
e1fd0586 412 return ret;
1da177e4
LT
413 }
414 if (nflog)
ee999d8b 415 nf_log_register(NFPROTO_IPV4, &ipt_ulog_logger);
e905a9ed 416
1da177e4
LT
417 return 0;
418}
419
d3c5ee6d 420static void __exit ulog_tg_exit(void)
1da177e4
LT
421{
422 ulog_buff_t *ub;
423 int i;
424
0d53778e 425 pr_debug("ipt_ULOG: cleanup_module\n");
1da177e4
LT
426
427 if (nflog)
e92ad99c 428 nf_log_unregister(&ipt_ulog_logger);
d3c5ee6d 429 xt_unregister_target(&ulog_tg_reg);
b7c6ba6e 430 netlink_kernel_release(nflognl);
1da177e4
LT
431
432 /* remove pending timers and free allocated skb's */
433 for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
434 ub = &ulog_buffers[i];
435 if (timer_pending(&ub->timer)) {
0d53778e 436 pr_debug("timer was pending, deleting\n");
1da177e4
LT
437 del_timer(&ub->timer);
438 }
439
440 if (ub->skb) {
441 kfree_skb(ub->skb);
442 ub->skb = NULL;
443 }
444 }
1da177e4
LT
445}
446
d3c5ee6d
JE
447module_init(ulog_tg_init);
448module_exit(ulog_tg_exit);