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