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