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