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