]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/ipv4/netfilter/ipt_ULOG.c
[NET]: convert network timestamps to ktime_t
[mirror_ubuntu-jammy-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>
5 *
6 * 2000/09/22 ulog-cprange feature added
e905a9ed 7 * 2001/01/04 in-kernel queue as proposed by Sebastian Zander
1da177e4 8 * <zander@fokus.gmd.de>
e905a9ed 9 * 2001/01/30 per-rule nlgroup conflicts with global queue.
1da177e4
LT
10 * nlgroup now global (sysctl)
11 * 2001/04/19 ulog-queue reworked, now fixed buffer size specified at
12 * module loadtime -HW
13 * 2002/07/07 remove broken nflog_rcv() function -HW
14 * 2002/08/29 fix shifted/unshifted nlgroup bug -HW
15 * 2002/10/30 fix uninitialized mac_len field - <Anders K. Pedersen>
16 * 2004/10/25 fix erroneous calculation of 'len' parameter to NLMSG_PUT
17 * resulting in bogus 'error during NLMSG_PUT' messages.
18 *
19 * (C) 1999-2001 Paul `Rusty' Russell
20 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
21 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2 as
24 * published by the Free Software Foundation.
25 *
e905a9ed
YH
26 * This module accepts two parameters:
27 *
1da177e4
LT
28 * nlbufsiz:
29 * The parameter specifies how big the buffer for each netlink multicast
30 * group is. e.g. If you say nlbufsiz=8192, up to eight kb of packets will
31 * get accumulated in the kernel until they are sent to userspace. It is
32 * NOT possible to allocate more than 128kB, and it is strongly discouraged,
33 * because atomically allocating 128kB inside the network rx softirq is not
34 * reliable. Please also keep in mind that this buffer size is allocated for
35 * each nlgroup you are using, so the total kernel memory usage increases
36 * by that factor.
37 *
c2db2924
HE
38 * Actually you should use nlbufsiz a bit smaller than PAGE_SIZE, since
39 * nlbufsiz is used with alloc_skb, which adds another
40 * sizeof(struct skb_shared_info). Use NLMSG_GOODSIZE instead.
41 *
1da177e4
LT
42 * flushtimeout:
43 * Specify, after how many hundredths of a second the queue should be
44 * flushed even if it is not full yet.
45 *
46 * ipt_ULOG.c,v 1.22 2002/10/30 09:07:31 laforge Exp
47 */
48
49#include <linux/module.h>
1da177e4
LT
50#include <linux/spinlock.h>
51#include <linux/socket.h>
52#include <linux/skbuff.h>
53#include <linux/kernel.h>
54#include <linux/timer.h>
55#include <linux/netlink.h>
56#include <linux/netdevice.h>
57#include <linux/mm.h>
58#include <linux/moduleparam.h>
59#include <linux/netfilter.h>
6709dbbb 60#include <linux/netfilter/x_tables.h>
1da177e4 61#include <linux/netfilter_ipv4/ipt_ULOG.h>
1da177e4
LT
62#include <net/sock.h>
63#include <linux/bitops.h>
01102e7c 64#include <asm/unaligned.h>
1da177e4
LT
65
66MODULE_LICENSE("GPL");
67MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
68MODULE_DESCRIPTION("iptables userspace logging module");
4fdb3bb7 69MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NFLOG);
1da177e4
LT
70
71#define ULOG_NL_EVENT 111 /* Harald's favorite number */
72#define ULOG_MAXNLGROUPS 32 /* numer of nlgroups */
73
74#if 0
75#define DEBUGP(format, args...) printk("%s:%s:" format, \
e905a9ed 76 __FILE__, __FUNCTION__ , ## args)
1da177e4
LT
77#else
78#define DEBUGP(format, args...)
79#endif
80
81#define PRINTR(format, args...) do { if (net_ratelimit()) printk(format , ## args); } while (0)
82
c2db2924 83static unsigned int nlbufsiz = NLMSG_GOODSIZE;
e7be6994 84module_param(nlbufsiz, uint, 0400);
1da177e4
LT
85MODULE_PARM_DESC(nlbufsiz, "netlink buffer size");
86
87static unsigned int flushtimeout = 10;
e7be6994 88module_param(flushtimeout, uint, 0600);
1da177e4
LT
89MODULE_PARM_DESC(flushtimeout, "buffer flush timeout (hundredths of a second)");
90
e7be6994
PM
91static int nflog = 1;
92module_param(nflog, bool, 0400);
1da177e4
LT
93MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
94
95/* global data structures */
96
97typedef struct {
98 unsigned int qlen; /* number of nlmsgs' in the skb */
99 struct nlmsghdr *lastnlh; /* netlink header of last msg in skb */
100 struct sk_buff *skb; /* the pre-allocated skb */
101 struct timer_list timer; /* the timer function */
102} ulog_buff_t;
103
104static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS]; /* array of buffers */
105
e45b1be8
PM
106static struct sock *nflognl; /* our socket */
107static DEFINE_SPINLOCK(ulog_lock); /* spinlock */
1da177e4
LT
108
109/* send one ulog_buff_t to userspace */
110static void ulog_send(unsigned int nlgroupnum)
111{
112 ulog_buff_t *ub = &ulog_buffers[nlgroupnum];
113
114 if (timer_pending(&ub->timer)) {
115 DEBUGP("ipt_ULOG: ulog_send: timer was pending, deleting\n");
116 del_timer(&ub->timer);
117 }
118
dcb7cd97
MH
119 if (!ub->skb) {
120 DEBUGP("ipt_ULOG: ulog_send: nothing to send\n");
121 return;
122 }
123
1da177e4
LT
124 /* last nlmsg needs NLMSG_DONE */
125 if (ub->qlen > 1)
126 ub->lastnlh->nlmsg_type = NLMSG_DONE;
127
ac6d439d
PM
128 NETLINK_CB(ub->skb).dst_group = nlgroupnum + 1;
129 DEBUGP("ipt_ULOG: throwing %d packets to netlink group %u\n",
130 ub->qlen, nlgroupnum + 1);
131 netlink_broadcast(nflognl, ub->skb, 0, nlgroupnum + 1, GFP_ATOMIC);
1da177e4
LT
132
133 ub->qlen = 0;
134 ub->skb = NULL;
135 ub->lastnlh = NULL;
1da177e4
LT
136}
137
138
139/* timer function to flush queue in flushtimeout time */
140static void ulog_timer(unsigned long data)
141{
142 DEBUGP("ipt_ULOG: timer function called, calling ulog_send\n");
143
144 /* lock to protect against somebody modifying our structure
145 * from ipt_ulog_target at the same time */
e45b1be8 146 spin_lock_bh(&ulog_lock);
1da177e4 147 ulog_send(data);
e45b1be8 148 spin_unlock_bh(&ulog_lock);
1da177e4
LT
149}
150
151static struct sk_buff *ulog_alloc_skb(unsigned int size)
152{
153 struct sk_buff *skb;
ad2ad0f9 154 unsigned int n;
1da177e4
LT
155
156 /* alloc skb which should be big enough for a whole
157 * multipart message. WARNING: has to be <= 131000
158 * due to slab allocator restrictions */
159
ad2ad0f9
PM
160 n = max(size, nlbufsiz);
161 skb = alloc_skb(n, GFP_ATOMIC);
1da177e4 162 if (!skb) {
ad2ad0f9 163 PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n", n);
1da177e4 164
ad2ad0f9 165 if (n > size) {
e905a9ed 166 /* try to allocate only as much as we need for
ad2ad0f9 167 * current packet */
1da177e4 168
ad2ad0f9
PM
169 skb = alloc_skb(size, GFP_ATOMIC);
170 if (!skb)
171 PRINTR("ipt_ULOG: can't even allocate %ub\n",
172 size);
173 }
1da177e4
LT
174 }
175
176 return skb;
177}
178
179static void ipt_ulog_packet(unsigned int hooknum,
180 const struct sk_buff *skb,
181 const struct net_device *in,
182 const struct net_device *out,
183 const struct ipt_ulog_info *loginfo,
184 const char *prefix)
185{
186 ulog_buff_t *ub;
187 ulog_packet_msg_t *pm;
188 size_t size, copy_len;
189 struct nlmsghdr *nlh;
b7aa0bf7 190 struct timeval tv;
1da177e4
LT
191
192 /* ffs == find first bit set, necessary because userspace
193 * is already shifting groupnumber, but we need unshifted.
194 * ffs() returns [1..32], we need [0..31] */
195 unsigned int groupnum = ffs(loginfo->nl_group) - 1;
196
197 /* calculate the size of the skb needed */
198 if ((loginfo->copy_range == 0) ||
199 (loginfo->copy_range > skb->len)) {
200 copy_len = skb->len;
201 } else {
202 copy_len = loginfo->copy_range;
203 }
204
205 size = NLMSG_SPACE(sizeof(*pm) + copy_len);
206
207 ub = &ulog_buffers[groupnum];
e905a9ed 208
e45b1be8 209 spin_lock_bh(&ulog_lock);
1da177e4
LT
210
211 if (!ub->skb) {
212 if (!(ub->skb = ulog_alloc_skb(size)))
213 goto alloc_failure;
214 } else if (ub->qlen >= loginfo->qthreshold ||
215 size > skb_tailroom(ub->skb)) {
e905a9ed 216 /* either the queue len is too high or we don't have
1da177e4
LT
217 * enough room in nlskb left. send it to userspace. */
218
219 ulog_send(groupnum);
220
221 if (!(ub->skb = ulog_alloc_skb(size)))
222 goto alloc_failure;
223 }
224
e905a9ed 225 DEBUGP("ipt_ULOG: qlen %d, qthreshold %d\n", ub->qlen,
1da177e4
LT
226 loginfo->qthreshold);
227
228 /* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */
e905a9ed 229 nlh = NLMSG_PUT(ub->skb, 0, ub->qlen, ULOG_NL_EVENT,
1da177e4
LT
230 sizeof(*pm)+copy_len);
231 ub->qlen++;
232
233 pm = NLMSG_DATA(nlh);
234
235 /* We might not have a timestamp, get one */
b7aa0bf7 236 if (skb->tstamp.tv64 == 0)
a61bbcf2 237 __net_timestamp((struct sk_buff *)skb);
1da177e4
LT
238
239 /* copy hook, prefix, timestamp, payload, etc. */
240 pm->data_len = copy_len;
b7aa0bf7
ED
241 tv = ktime_to_timeval(skb->tstamp);
242 put_unaligned(tv.tv_sec, &pm->timestamp_sec);
243 put_unaligned(tv.tv_usec, &pm->timestamp_usec);
01102e7c 244 put_unaligned(skb->mark, &pm->mark);
1da177e4
LT
245 pm->hook = hooknum;
246 if (prefix != NULL)
247 strncpy(pm->prefix, prefix, sizeof(pm->prefix));
248 else if (loginfo->prefix[0] != '\0')
249 strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
250 else
251 *(pm->prefix) = '\0';
252
253 if (in && in->hard_header_len > 0
254 && skb->mac.raw != (void *) skb->nh.iph
255 && in->hard_header_len <= ULOG_MAC_LEN) {
256 memcpy(pm->mac, skb->mac.raw, in->hard_header_len);
257 pm->mac_len = in->hard_header_len;
258 } else
259 pm->mac_len = 0;
260
261 if (in)
262 strncpy(pm->indev_name, in->name, sizeof(pm->indev_name));
263 else
264 pm->indev_name[0] = '\0';
265
266 if (out)
267 strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
268 else
269 pm->outdev_name[0] = '\0';
270
271 /* copy_len <= skb->len, so can't fail. */
272 if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0)
273 BUG();
e905a9ed 274
1da177e4
LT
275 /* check if we are building multi-part messages */
276 if (ub->qlen > 1) {
277 ub->lastnlh->nlmsg_flags |= NLM_F_MULTI;
278 }
279
280 ub->lastnlh = nlh;
281
282 /* if timer isn't already running, start it */
283 if (!timer_pending(&ub->timer)) {
284 ub->timer.expires = jiffies + flushtimeout * HZ / 100;
285 add_timer(&ub->timer);
286 }
287
288 /* if threshold is reached, send message to userspace */
289 if (ub->qlen >= loginfo->qthreshold) {
290 if (loginfo->qthreshold > 1)
291 nlh->nlmsg_type = NLMSG_DONE;
292 ulog_send(groupnum);
293 }
294
e45b1be8 295 spin_unlock_bh(&ulog_lock);
1da177e4
LT
296
297 return;
298
299nlmsg_failure:
300 PRINTR("ipt_ULOG: error during NLMSG_PUT\n");
301
302alloc_failure:
303 PRINTR("ipt_ULOG: Error building netlink message\n");
304
e45b1be8 305 spin_unlock_bh(&ulog_lock);
1da177e4
LT
306}
307
308static unsigned int ipt_ulog_target(struct sk_buff **pskb,
309 const struct net_device *in,
310 const struct net_device *out,
311 unsigned int hooknum,
c4986734 312 const struct xt_target *target,
fe1cb108 313 const void *targinfo)
1da177e4
LT
314{
315 struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
316
317 ipt_ulog_packet(hooknum, *pskb, in, out, loginfo, NULL);
e905a9ed 318
6709dbbb 319 return XT_CONTINUE;
1da177e4 320}
e905a9ed 321
608c8e4f
HW
322static void ipt_logfn(unsigned int pf,
323 unsigned int hooknum,
1da177e4
LT
324 const struct sk_buff *skb,
325 const struct net_device *in,
326 const struct net_device *out,
608c8e4f 327 const struct nf_loginfo *li,
1da177e4
LT
328 const char *prefix)
329{
608c8e4f
HW
330 struct ipt_ulog_info loginfo;
331
332 if (!li || li->type != NF_LOG_TYPE_ULOG) {
333 loginfo.nl_group = ULOG_DEFAULT_NLGROUP;
334 loginfo.copy_range = 0;
335 loginfo.qthreshold = ULOG_DEFAULT_QTHRESHOLD;
336 loginfo.prefix[0] = '\0';
337 } else {
338 loginfo.nl_group = li->u.ulog.group;
339 loginfo.copy_range = li->u.ulog.copy_len;
340 loginfo.qthreshold = li->u.ulog.qthreshold;
341 strlcpy(loginfo.prefix, prefix, sizeof(loginfo.prefix));
342 }
1da177e4
LT
343
344 ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix);
345}
346
347static int ipt_ulog_checkentry(const char *tablename,
2e4e6a17 348 const void *e,
c4986734 349 const struct xt_target *target,
1da177e4 350 void *targinfo,
1da177e4
LT
351 unsigned int hookmask)
352{
353 struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
354
1da177e4
LT
355 if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') {
356 DEBUGP("ipt_ULOG: prefix term %i\n",
357 loginfo->prefix[sizeof(loginfo->prefix) - 1]);
358 return 0;
359 }
1da177e4
LT
360 if (loginfo->qthreshold > ULOG_MAX_QLEN) {
361 DEBUGP("ipt_ULOG: queue threshold %i > MAX_QLEN\n",
362 loginfo->qthreshold);
363 return 0;
364 }
1da177e4
LT
365 return 1;
366}
367
6709dbbb 368static struct xt_target ipt_ulog_reg = {
1da177e4 369 .name = "ULOG",
6709dbbb 370 .family = AF_INET,
1da177e4 371 .target = ipt_ulog_target,
1d5cd909 372 .targetsize = sizeof(struct ipt_ulog_info),
1da177e4
LT
373 .checkentry = ipt_ulog_checkentry,
374 .me = THIS_MODULE,
375};
376
608c8e4f
HW
377static struct nf_logger ipt_ulog_logger = {
378 .name = "ipt_ULOG",
1d5cd909 379 .logfn = ipt_logfn,
608c8e4f
HW
380 .me = THIS_MODULE,
381};
382
65b4b4e8 383static int __init ipt_ulog_init(void)
1da177e4 384{
e1fd0586 385 int ret, i;
1da177e4
LT
386
387 DEBUGP("ipt_ULOG: init module\n");
388
e7be6994 389 if (nlbufsiz > 128*1024) {
1da177e4
LT
390 printk("Netlink buffer has to be <= 128kB\n");
391 return -EINVAL;
392 }
393
394 /* initialize ulog_buffers */
395 for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
396 init_timer(&ulog_buffers[i].timer);
397 ulog_buffers[i].timer.function = ulog_timer;
398 ulog_buffers[i].timer.data = i;
399 }
400
06628607 401 nflognl = netlink_kernel_create(NETLINK_NFLOG, ULOG_MAXNLGROUPS, NULL,
e905a9ed 402 THIS_MODULE);
1da177e4
LT
403 if (!nflognl)
404 return -ENOMEM;
405
6709dbbb 406 ret = xt_register_target(&ipt_ulog_reg);
e1fd0586 407 if (ret < 0) {
1da177e4 408 sock_release(nflognl->sk_socket);
e1fd0586 409 return ret;
1da177e4
LT
410 }
411 if (nflog)
608c8e4f 412 nf_log_register(PF_INET, &ipt_ulog_logger);
e905a9ed 413
1da177e4
LT
414 return 0;
415}
416
65b4b4e8 417static void __exit ipt_ulog_fini(void)
1da177e4
LT
418{
419 ulog_buff_t *ub;
420 int i;
421
422 DEBUGP("ipt_ULOG: cleanup_module\n");
423
424 if (nflog)
e92ad99c 425 nf_log_unregister(&ipt_ulog_logger);
6709dbbb 426 xt_unregister_target(&ipt_ulog_reg);
1da177e4
LT
427 sock_release(nflognl->sk_socket);
428
429 /* remove pending timers and free allocated skb's */
430 for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
431 ub = &ulog_buffers[i];
432 if (timer_pending(&ub->timer)) {
433 DEBUGP("timer was pending, deleting\n");
434 del_timer(&ub->timer);
435 }
436
437 if (ub->skb) {
438 kfree_skb(ub->skb);
439 ub->skb = NULL;
440 }
441 }
1da177e4
LT
442}
443
65b4b4e8
AM
444module_init(ipt_ulog_init);
445module_exit(ipt_ulog_fini);