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/sock.h>
47#include <linux/bitops.h>
48#include <asm/unaligned.h>
49
50MODULE_LICENSE("GPL");
51MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
52MODULE_DESCRIPTION("iptables userspace logging module");
53MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NFLOG);
54
55#define ULOG_NL_EVENT		111		/* Harald's favorite number */
56#define ULOG_MAXNLGROUPS	32		/* numer of nlgroups */
57
58#define DEBUGP(format, args...)
59
60#define PRINTR(format, args...) do { if (net_ratelimit()) printk(format , ## args); } while (0)
61
62static unsigned int nlbufsiz = NLMSG_GOODSIZE;
63module_param(nlbufsiz, uint, 0400);
64MODULE_PARM_DESC(nlbufsiz, "netlink buffer size");
65
66static unsigned int flushtimeout = 10;
67module_param(flushtimeout, uint, 0600);
68MODULE_PARM_DESC(flushtimeout, "buffer flush timeout (hundredths of a second)");
69
70static int nflog = 1;
71module_param(nflog, bool, 0400);
72MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
73
74/* global data structures */
75
76typedef 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
83static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS];	/* array of buffers */
84
85static struct sock *nflognl;		/* our socket */
86static DEFINE_SPINLOCK(ulog_lock);	/* spinlock */
87
88/* send one ulog_buff_t to userspace */
89static void ulog_send(unsigned int nlgroupnum)
90{
91	ulog_buff_t *ub = &ulog_buffers[nlgroupnum];
92
93	if (timer_pending(&ub->timer)) {
94		DEBUGP("ipt_ULOG: ulog_send: timer was pending, deleting\n");
95		del_timer(&ub->timer);
96	}
97
98	if (!ub->skb) {
99		DEBUGP("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	DEBUGP("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 */
119static void ulog_timer(unsigned long data)
120{
121	DEBUGP("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
130static 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
158static 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) ||
178	    (loginfo->copy_range > skb->len)) {
179		copy_len = skb->len;
180	} else {
181		copy_len = loginfo->copy_range;
182	}
183
184	size = NLMSG_SPACE(sizeof(*pm) + copy_len);
185
186	ub = &ulog_buffers[groupnum];
187
188	spin_lock_bh(&ulog_lock);
189
190	if (!ub->skb) {
191		if (!(ub->skb = ulog_alloc_skb(size)))
192			goto alloc_failure;
193	} else if (ub->qlen >= loginfo->qthreshold ||
194		   size > skb_tailroom(ub->skb)) {
195		/* either the queue len is too high or we don't have
196		 * enough room in nlskb left. send it to userspace. */
197
198		ulog_send(groupnum);
199
200		if (!(ub->skb = ulog_alloc_skb(size)))
201			goto alloc_failure;
202	}
203
204	DEBUGP("ipt_ULOG: qlen %d, qthreshold %d\n", ub->qlen,
205		loginfo->qthreshold);
206
207	/* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */
208	nlh = NLMSG_PUT(ub->skb, 0, ub->qlen, ULOG_NL_EVENT,
209			sizeof(*pm)+copy_len);
210	ub->qlen++;
211
212	pm = NLMSG_DATA(nlh);
213
214	/* We might not have a timestamp, get one */
215	if (skb->tstamp.tv64 == 0)
216		__net_timestamp((struct sk_buff *)skb);
217
218	/* copy hook, prefix, timestamp, payload, etc. */
219	pm->data_len = copy_len;
220	tv = ktime_to_timeval(skb->tstamp);
221	put_unaligned(tv.tv_sec, &pm->timestamp_sec);
222	put_unaligned(tv.tv_usec, &pm->timestamp_usec);
223	put_unaligned(skb->mark, &pm->mark);
224	pm->hook = hooknum;
225	if (prefix != NULL)
226		strncpy(pm->prefix, prefix, sizeof(pm->prefix));
227	else if (loginfo->prefix[0] != '\0')
228		strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
229	else
230		*(pm->prefix) = '\0';
231
232	if (in && in->hard_header_len > 0
233	    && skb->mac_header != skb->network_header
234	    && in->hard_header_len <= ULOG_MAC_LEN) {
235		memcpy(pm->mac, skb_mac_header(skb), in->hard_header_len);
236		pm->mac_len = in->hard_header_len;
237	} else
238		pm->mac_len = 0;
239
240	if (in)
241		strncpy(pm->indev_name, in->name, sizeof(pm->indev_name));
242	else
243		pm->indev_name[0] = '\0';
244
245	if (out)
246		strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
247	else
248		pm->outdev_name[0] = '\0';
249
250	/* copy_len <= skb->len, so can't fail. */
251	if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0)
252		BUG();
253
254	/* check if we are building multi-part messages */
255	if (ub->qlen > 1) {
256		ub->lastnlh->nlmsg_flags |= NLM_F_MULTI;
257	}
258
259	ub->lastnlh = nlh;
260
261	/* if timer isn't already running, start it */
262	if (!timer_pending(&ub->timer)) {
263		ub->timer.expires = jiffies + flushtimeout * HZ / 100;
264		add_timer(&ub->timer);
265	}
266
267	/* if threshold is reached, send message to userspace */
268	if (ub->qlen >= loginfo->qthreshold) {
269		if (loginfo->qthreshold > 1)
270			nlh->nlmsg_type = NLMSG_DONE;
271		ulog_send(groupnum);
272	}
273
274	spin_unlock_bh(&ulog_lock);
275
276	return;
277
278nlmsg_failure:
279	PRINTR("ipt_ULOG: error during NLMSG_PUT\n");
280
281alloc_failure:
282	PRINTR("ipt_ULOG: Error building netlink message\n");
283
284	spin_unlock_bh(&ulog_lock);
285}
286
287static unsigned int ipt_ulog_target(struct sk_buff **pskb,
288				    const struct net_device *in,
289				    const struct net_device *out,
290				    unsigned int hooknum,
291				    const struct xt_target *target,
292				    const void *targinfo)
293{
294	struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
295
296	ipt_ulog_packet(hooknum, *pskb, in, out, loginfo, NULL);
297
298	return XT_CONTINUE;
299}
300
301static void ipt_logfn(unsigned int pf,
302		      unsigned int hooknum,
303		      const struct sk_buff *skb,
304		      const struct net_device *in,
305		      const struct net_device *out,
306		      const struct nf_loginfo *li,
307		      const char *prefix)
308{
309	struct ipt_ulog_info loginfo;
310
311	if (!li || li->type != NF_LOG_TYPE_ULOG) {
312		loginfo.nl_group = ULOG_DEFAULT_NLGROUP;
313		loginfo.copy_range = 0;
314		loginfo.qthreshold = ULOG_DEFAULT_QTHRESHOLD;
315		loginfo.prefix[0] = '\0';
316	} else {
317		loginfo.nl_group = li->u.ulog.group;
318		loginfo.copy_range = li->u.ulog.copy_len;
319		loginfo.qthreshold = li->u.ulog.qthreshold;
320		strlcpy(loginfo.prefix, prefix, sizeof(loginfo.prefix));
321	}
322
323	ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix);
324}
325
326static int ipt_ulog_checkentry(const char *tablename,
327			       const void *e,
328			       const struct xt_target *target,
329			       void *targinfo,
330			       unsigned int hookmask)
331{
332	struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
333
334	if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') {
335		DEBUGP("ipt_ULOG: prefix term %i\n",
336		       loginfo->prefix[sizeof(loginfo->prefix) - 1]);
337		return 0;
338	}
339	if (loginfo->qthreshold > ULOG_MAX_QLEN) {
340		DEBUGP("ipt_ULOG: queue threshold %i > MAX_QLEN\n",
341			loginfo->qthreshold);
342		return 0;
343	}
344	return 1;
345}
346
347#ifdef CONFIG_COMPAT
348struct compat_ipt_ulog_info {
349	compat_uint_t	nl_group;
350	compat_size_t	copy_range;
351	compat_size_t	qthreshold;
352	char		prefix[ULOG_PREFIX_LEN];
353};
354
355static void compat_from_user(void *dst, void *src)
356{
357	struct compat_ipt_ulog_info *cl = src;
358	struct ipt_ulog_info l = {
359		.nl_group	= cl->nl_group,
360		.copy_range	= cl->copy_range,
361		.qthreshold	= cl->qthreshold,
362	};
363
364	memcpy(l.prefix, cl->prefix, sizeof(l.prefix));
365	memcpy(dst, &l, sizeof(l));
366}
367
368static int compat_to_user(void __user *dst, void *src)
369{
370	struct ipt_ulog_info *l = src;
371	struct compat_ipt_ulog_info cl = {
372		.nl_group	= l->nl_group,
373		.copy_range	= l->copy_range,
374		.qthreshold	= l->qthreshold,
375	};
376
377	memcpy(cl.prefix, l->prefix, sizeof(cl.prefix));
378	return copy_to_user(dst, &cl, sizeof(cl)) ? -EFAULT : 0;
379}
380#endif /* CONFIG_COMPAT */
381
382static struct xt_target ipt_ulog_reg = {
383	.name		= "ULOG",
384	.family		= AF_INET,
385	.target		= ipt_ulog_target,
386	.targetsize	= sizeof(struct ipt_ulog_info),
387	.checkentry	= ipt_ulog_checkentry,
388#ifdef CONFIG_COMPAT
389	.compatsize	= sizeof(struct compat_ipt_ulog_info),
390	.compat_from_user = compat_from_user,
391	.compat_to_user	= compat_to_user,
392#endif
393	.me		= THIS_MODULE,
394};
395
396static struct nf_logger ipt_ulog_logger = {
397	.name		= "ipt_ULOG",
398	.logfn		= ipt_logfn,
399	.me		= THIS_MODULE,
400};
401
402static int __init ipt_ulog_init(void)
403{
404	int ret, i;
405
406	DEBUGP("ipt_ULOG: init module\n");
407
408	if (nlbufsiz > 128*1024) {
409		printk("Netlink buffer has to be <= 128kB\n");
410		return -EINVAL;
411	}
412
413	/* initialize ulog_buffers */
414	for (i = 0; i < ULOG_MAXNLGROUPS; i++)
415		setup_timer(&ulog_buffers[i].timer, ulog_timer, i);
416
417	nflognl = netlink_kernel_create(NETLINK_NFLOG, ULOG_MAXNLGROUPS, NULL,
418					NULL, THIS_MODULE);
419	if (!nflognl)
420		return -ENOMEM;
421
422	ret = xt_register_target(&ipt_ulog_reg);
423	if (ret < 0) {
424		sock_release(nflognl->sk_socket);
425		return ret;
426	}
427	if (nflog)
428		nf_log_register(PF_INET, &ipt_ulog_logger);
429
430	return 0;
431}
432
433static void __exit ipt_ulog_fini(void)
434{
435	ulog_buff_t *ub;
436	int i;
437
438	DEBUGP("ipt_ULOG: cleanup_module\n");
439
440	if (nflog)
441		nf_log_unregister(&ipt_ulog_logger);
442	xt_unregister_target(&ipt_ulog_reg);
443	sock_release(nflognl->sk_socket);
444
445	/* remove pending timers and free allocated skb's */
446	for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
447		ub = &ulog_buffers[i];
448		if (timer_pending(&ub->timer)) {
449			DEBUGP("timer was pending, deleting\n");
450			del_timer(&ub->timer);
451		}
452
453		if (ub->skb) {
454			kfree_skb(ub->skb);
455			ub->skb = NULL;
456		}
457	}
458}
459
460module_init(ipt_ulog_init);
461module_exit(ipt_ulog_fini);
462