]>
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> | |
5 | * | |
6 | * 2000/09/22 ulog-cprange feature added | |
7 | * 2001/01/04 in-kernel queue as proposed by Sebastian Zander | |
8 | * <zander@fokus.gmd.de> | |
9 | * 2001/01/30 per-rule nlgroup conflicts with global queue. | |
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 | * | |
26 | * This module accepts two parameters: | |
27 | * | |
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> | |
60 | #include <linux/netfilter_ipv4/ip_tables.h> | |
61 | #include <linux/netfilter_ipv4/ipt_ULOG.h> | |
1da177e4 LT |
62 | #include <net/sock.h> |
63 | #include <linux/bitops.h> | |
64 | ||
65 | MODULE_LICENSE("GPL"); | |
66 | MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>"); | |
67 | MODULE_DESCRIPTION("iptables userspace logging module"); | |
4fdb3bb7 | 68 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NFLOG); |
1da177e4 LT |
69 | |
70 | #define ULOG_NL_EVENT 111 /* Harald's favorite number */ | |
71 | #define ULOG_MAXNLGROUPS 32 /* numer of nlgroups */ | |
72 | ||
73 | #if 0 | |
74 | #define DEBUGP(format, args...) printk("%s:%s:" format, \ | |
75 | __FILE__, __FUNCTION__ , ## args) | |
76 | #else | |
77 | #define DEBUGP(format, args...) | |
78 | #endif | |
79 | ||
80 | #define PRINTR(format, args...) do { if (net_ratelimit()) printk(format , ## args); } while (0) | |
81 | ||
c2db2924 | 82 | static unsigned int nlbufsiz = NLMSG_GOODSIZE; |
e7be6994 | 83 | module_param(nlbufsiz, uint, 0400); |
1da177e4 LT |
84 | MODULE_PARM_DESC(nlbufsiz, "netlink buffer size"); |
85 | ||
86 | static unsigned int flushtimeout = 10; | |
e7be6994 | 87 | module_param(flushtimeout, uint, 0600); |
1da177e4 LT |
88 | MODULE_PARM_DESC(flushtimeout, "buffer flush timeout (hundredths of a second)"); |
89 | ||
e7be6994 PM |
90 | static int nflog = 1; |
91 | module_param(nflog, bool, 0400); | |
1da177e4 LT |
92 | MODULE_PARM_DESC(nflog, "register as internal netfilter logging module"); |
93 | ||
94 | /* global data structures */ | |
95 | ||
96 | typedef struct { | |
97 | unsigned int qlen; /* number of nlmsgs' in the skb */ | |
98 | struct nlmsghdr *lastnlh; /* netlink header of last msg in skb */ | |
99 | struct sk_buff *skb; /* the pre-allocated skb */ | |
100 | struct timer_list timer; /* the timer function */ | |
101 | } ulog_buff_t; | |
102 | ||
103 | static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS]; /* array of buffers */ | |
104 | ||
e45b1be8 PM |
105 | static struct sock *nflognl; /* our socket */ |
106 | static DEFINE_SPINLOCK(ulog_lock); /* spinlock */ | |
1da177e4 LT |
107 | |
108 | /* send one ulog_buff_t to userspace */ | |
109 | static void ulog_send(unsigned int nlgroupnum) | |
110 | { | |
111 | ulog_buff_t *ub = &ulog_buffers[nlgroupnum]; | |
112 | ||
113 | if (timer_pending(&ub->timer)) { | |
114 | DEBUGP("ipt_ULOG: ulog_send: timer was pending, deleting\n"); | |
115 | del_timer(&ub->timer); | |
116 | } | |
117 | ||
118 | /* last nlmsg needs NLMSG_DONE */ | |
119 | if (ub->qlen > 1) | |
120 | ub->lastnlh->nlmsg_type = NLMSG_DONE; | |
121 | ||
ac6d439d PM |
122 | NETLINK_CB(ub->skb).dst_group = nlgroupnum + 1; |
123 | DEBUGP("ipt_ULOG: throwing %d packets to netlink group %u\n", | |
124 | ub->qlen, nlgroupnum + 1); | |
125 | netlink_broadcast(nflognl, ub->skb, 0, nlgroupnum + 1, GFP_ATOMIC); | |
1da177e4 LT |
126 | |
127 | ub->qlen = 0; | |
128 | ub->skb = NULL; | |
129 | ub->lastnlh = NULL; | |
130 | ||
131 | } | |
132 | ||
133 | ||
134 | /* timer function to flush queue in flushtimeout time */ | |
135 | static void ulog_timer(unsigned long data) | |
136 | { | |
137 | DEBUGP("ipt_ULOG: timer function called, calling ulog_send\n"); | |
138 | ||
139 | /* lock to protect against somebody modifying our structure | |
140 | * from ipt_ulog_target at the same time */ | |
e45b1be8 | 141 | spin_lock_bh(&ulog_lock); |
1da177e4 | 142 | ulog_send(data); |
e45b1be8 | 143 | spin_unlock_bh(&ulog_lock); |
1da177e4 LT |
144 | } |
145 | ||
146 | static struct sk_buff *ulog_alloc_skb(unsigned int size) | |
147 | { | |
148 | struct sk_buff *skb; | |
ad2ad0f9 | 149 | unsigned int n; |
1da177e4 LT |
150 | |
151 | /* alloc skb which should be big enough for a whole | |
152 | * multipart message. WARNING: has to be <= 131000 | |
153 | * due to slab allocator restrictions */ | |
154 | ||
ad2ad0f9 PM |
155 | n = max(size, nlbufsiz); |
156 | skb = alloc_skb(n, GFP_ATOMIC); | |
1da177e4 | 157 | if (!skb) { |
ad2ad0f9 | 158 | PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n", n); |
1da177e4 | 159 | |
ad2ad0f9 PM |
160 | if (n > size) { |
161 | /* try to allocate only as much as we need for | |
162 | * current packet */ | |
1da177e4 | 163 | |
ad2ad0f9 PM |
164 | skb = alloc_skb(size, GFP_ATOMIC); |
165 | if (!skb) | |
166 | PRINTR("ipt_ULOG: can't even allocate %ub\n", | |
167 | size); | |
168 | } | |
1da177e4 LT |
169 | } |
170 | ||
171 | return skb; | |
172 | } | |
173 | ||
174 | static void ipt_ulog_packet(unsigned int hooknum, | |
175 | const struct sk_buff *skb, | |
176 | const struct net_device *in, | |
177 | const struct net_device *out, | |
178 | const struct ipt_ulog_info *loginfo, | |
179 | const char *prefix) | |
180 | { | |
181 | ulog_buff_t *ub; | |
182 | ulog_packet_msg_t *pm; | |
183 | size_t size, copy_len; | |
184 | struct nlmsghdr *nlh; | |
185 | ||
186 | /* ffs == find first bit set, necessary because userspace | |
187 | * is already shifting groupnumber, but we need unshifted. | |
188 | * ffs() returns [1..32], we need [0..31] */ | |
189 | unsigned int groupnum = ffs(loginfo->nl_group) - 1; | |
190 | ||
191 | /* calculate the size of the skb needed */ | |
192 | if ((loginfo->copy_range == 0) || | |
193 | (loginfo->copy_range > skb->len)) { | |
194 | copy_len = skb->len; | |
195 | } else { | |
196 | copy_len = loginfo->copy_range; | |
197 | } | |
198 | ||
199 | size = NLMSG_SPACE(sizeof(*pm) + copy_len); | |
200 | ||
201 | ub = &ulog_buffers[groupnum]; | |
202 | ||
e45b1be8 | 203 | spin_lock_bh(&ulog_lock); |
1da177e4 LT |
204 | |
205 | if (!ub->skb) { | |
206 | if (!(ub->skb = ulog_alloc_skb(size))) | |
207 | goto alloc_failure; | |
208 | } else if (ub->qlen >= loginfo->qthreshold || | |
209 | size > skb_tailroom(ub->skb)) { | |
210 | /* either the queue len is too high or we don't have | |
211 | * enough room in nlskb left. send it to userspace. */ | |
212 | ||
213 | ulog_send(groupnum); | |
214 | ||
215 | if (!(ub->skb = ulog_alloc_skb(size))) | |
216 | goto alloc_failure; | |
217 | } | |
218 | ||
219 | DEBUGP("ipt_ULOG: qlen %d, qthreshold %d\n", ub->qlen, | |
220 | loginfo->qthreshold); | |
221 | ||
222 | /* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */ | |
223 | nlh = NLMSG_PUT(ub->skb, 0, ub->qlen, ULOG_NL_EVENT, | |
224 | sizeof(*pm)+copy_len); | |
225 | ub->qlen++; | |
226 | ||
227 | pm = NLMSG_DATA(nlh); | |
228 | ||
229 | /* We might not have a timestamp, get one */ | |
a61bbcf2 PM |
230 | if (skb->tstamp.off_sec == 0) |
231 | __net_timestamp((struct sk_buff *)skb); | |
1da177e4 LT |
232 | |
233 | /* copy hook, prefix, timestamp, payload, etc. */ | |
234 | pm->data_len = copy_len; | |
325ed823 HX |
235 | pm->timestamp_sec = skb->tstamp.off_sec; |
236 | pm->timestamp_usec = skb->tstamp.off_usec; | |
1da177e4 LT |
237 | pm->mark = skb->nfmark; |
238 | pm->hook = hooknum; | |
239 | if (prefix != NULL) | |
240 | strncpy(pm->prefix, prefix, sizeof(pm->prefix)); | |
241 | else if (loginfo->prefix[0] != '\0') | |
242 | strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix)); | |
243 | else | |
244 | *(pm->prefix) = '\0'; | |
245 | ||
246 | if (in && in->hard_header_len > 0 | |
247 | && skb->mac.raw != (void *) skb->nh.iph | |
248 | && in->hard_header_len <= ULOG_MAC_LEN) { | |
249 | memcpy(pm->mac, skb->mac.raw, in->hard_header_len); | |
250 | pm->mac_len = in->hard_header_len; | |
251 | } else | |
252 | pm->mac_len = 0; | |
253 | ||
254 | if (in) | |
255 | strncpy(pm->indev_name, in->name, sizeof(pm->indev_name)); | |
256 | else | |
257 | pm->indev_name[0] = '\0'; | |
258 | ||
259 | if (out) | |
260 | strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name)); | |
261 | else | |
262 | pm->outdev_name[0] = '\0'; | |
263 | ||
264 | /* copy_len <= skb->len, so can't fail. */ | |
265 | if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0) | |
266 | BUG(); | |
267 | ||
268 | /* check if we are building multi-part messages */ | |
269 | if (ub->qlen > 1) { | |
270 | ub->lastnlh->nlmsg_flags |= NLM_F_MULTI; | |
271 | } | |
272 | ||
273 | ub->lastnlh = nlh; | |
274 | ||
275 | /* if timer isn't already running, start it */ | |
276 | if (!timer_pending(&ub->timer)) { | |
277 | ub->timer.expires = jiffies + flushtimeout * HZ / 100; | |
278 | add_timer(&ub->timer); | |
279 | } | |
280 | ||
281 | /* if threshold is reached, send message to userspace */ | |
282 | if (ub->qlen >= loginfo->qthreshold) { | |
283 | if (loginfo->qthreshold > 1) | |
284 | nlh->nlmsg_type = NLMSG_DONE; | |
285 | ulog_send(groupnum); | |
286 | } | |
287 | ||
e45b1be8 | 288 | spin_unlock_bh(&ulog_lock); |
1da177e4 LT |
289 | |
290 | return; | |
291 | ||
292 | nlmsg_failure: | |
293 | PRINTR("ipt_ULOG: error during NLMSG_PUT\n"); | |
294 | ||
295 | alloc_failure: | |
296 | PRINTR("ipt_ULOG: Error building netlink message\n"); | |
297 | ||
e45b1be8 | 298 | spin_unlock_bh(&ulog_lock); |
1da177e4 LT |
299 | } |
300 | ||
301 | static unsigned int ipt_ulog_target(struct sk_buff **pskb, | |
302 | const struct net_device *in, | |
303 | const struct net_device *out, | |
304 | unsigned int hooknum, | |
c4986734 | 305 | const struct xt_target *target, |
1da177e4 LT |
306 | const void *targinfo, void *userinfo) |
307 | { | |
308 | struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo; | |
309 | ||
310 | ipt_ulog_packet(hooknum, *pskb, in, out, loginfo, NULL); | |
311 | ||
312 | return IPT_CONTINUE; | |
313 | } | |
314 | ||
608c8e4f HW |
315 | static void ipt_logfn(unsigned int pf, |
316 | unsigned int hooknum, | |
1da177e4 LT |
317 | const struct sk_buff *skb, |
318 | const struct net_device *in, | |
319 | const struct net_device *out, | |
608c8e4f | 320 | const struct nf_loginfo *li, |
1da177e4 LT |
321 | const char *prefix) |
322 | { | |
608c8e4f HW |
323 | struct ipt_ulog_info loginfo; |
324 | ||
325 | if (!li || li->type != NF_LOG_TYPE_ULOG) { | |
326 | loginfo.nl_group = ULOG_DEFAULT_NLGROUP; | |
327 | loginfo.copy_range = 0; | |
328 | loginfo.qthreshold = ULOG_DEFAULT_QTHRESHOLD; | |
329 | loginfo.prefix[0] = '\0'; | |
330 | } else { | |
331 | loginfo.nl_group = li->u.ulog.group; | |
332 | loginfo.copy_range = li->u.ulog.copy_len; | |
333 | loginfo.qthreshold = li->u.ulog.qthreshold; | |
334 | strlcpy(loginfo.prefix, prefix, sizeof(loginfo.prefix)); | |
335 | } | |
1da177e4 LT |
336 | |
337 | ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix); | |
338 | } | |
339 | ||
340 | static int ipt_ulog_checkentry(const char *tablename, | |
2e4e6a17 | 341 | const void *e, |
c4986734 | 342 | const struct xt_target *target, |
1da177e4 LT |
343 | void *targinfo, |
344 | unsigned int targinfosize, | |
345 | unsigned int hookmask) | |
346 | { | |
347 | struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo; | |
348 | ||
1da177e4 LT |
349 | if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') { |
350 | DEBUGP("ipt_ULOG: prefix term %i\n", | |
351 | loginfo->prefix[sizeof(loginfo->prefix) - 1]); | |
352 | return 0; | |
353 | } | |
1da177e4 LT |
354 | if (loginfo->qthreshold > ULOG_MAX_QLEN) { |
355 | DEBUGP("ipt_ULOG: queue threshold %i > MAX_QLEN\n", | |
356 | loginfo->qthreshold); | |
357 | return 0; | |
358 | } | |
1da177e4 LT |
359 | return 1; |
360 | } | |
361 | ||
362 | static struct ipt_target ipt_ulog_reg = { | |
363 | .name = "ULOG", | |
364 | .target = ipt_ulog_target, | |
1d5cd909 | 365 | .targetsize = sizeof(struct ipt_ulog_info), |
1da177e4 LT |
366 | .checkentry = ipt_ulog_checkentry, |
367 | .me = THIS_MODULE, | |
368 | }; | |
369 | ||
608c8e4f HW |
370 | static struct nf_logger ipt_ulog_logger = { |
371 | .name = "ipt_ULOG", | |
1d5cd909 | 372 | .logfn = ipt_logfn, |
608c8e4f HW |
373 | .me = THIS_MODULE, |
374 | }; | |
375 | ||
65b4b4e8 | 376 | static int __init ipt_ulog_init(void) |
1da177e4 LT |
377 | { |
378 | int i; | |
379 | ||
380 | DEBUGP("ipt_ULOG: init module\n"); | |
381 | ||
e7be6994 | 382 | if (nlbufsiz > 128*1024) { |
1da177e4 LT |
383 | printk("Netlink buffer has to be <= 128kB\n"); |
384 | return -EINVAL; | |
385 | } | |
386 | ||
387 | /* initialize ulog_buffers */ | |
388 | for (i = 0; i < ULOG_MAXNLGROUPS; i++) { | |
389 | init_timer(&ulog_buffers[i].timer); | |
390 | ulog_buffers[i].timer.function = ulog_timer; | |
391 | ulog_buffers[i].timer.data = i; | |
392 | } | |
393 | ||
06628607 PM |
394 | nflognl = netlink_kernel_create(NETLINK_NFLOG, ULOG_MAXNLGROUPS, NULL, |
395 | THIS_MODULE); | |
1da177e4 LT |
396 | if (!nflognl) |
397 | return -ENOMEM; | |
398 | ||
399 | if (ipt_register_target(&ipt_ulog_reg) != 0) { | |
400 | sock_release(nflognl->sk_socket); | |
401 | return -EINVAL; | |
402 | } | |
403 | if (nflog) | |
608c8e4f | 404 | nf_log_register(PF_INET, &ipt_ulog_logger); |
1da177e4 LT |
405 | |
406 | return 0; | |
407 | } | |
408 | ||
65b4b4e8 | 409 | static void __exit ipt_ulog_fini(void) |
1da177e4 LT |
410 | { |
411 | ulog_buff_t *ub; | |
412 | int i; | |
413 | ||
414 | DEBUGP("ipt_ULOG: cleanup_module\n"); | |
415 | ||
416 | if (nflog) | |
608c8e4f | 417 | nf_log_unregister_logger(&ipt_ulog_logger); |
1da177e4 LT |
418 | ipt_unregister_target(&ipt_ulog_reg); |
419 | sock_release(nflognl->sk_socket); | |
420 | ||
421 | /* remove pending timers and free allocated skb's */ | |
422 | for (i = 0; i < ULOG_MAXNLGROUPS; i++) { | |
423 | ub = &ulog_buffers[i]; | |
424 | if (timer_pending(&ub->timer)) { | |
425 | DEBUGP("timer was pending, deleting\n"); | |
426 | del_timer(&ub->timer); | |
427 | } | |
428 | ||
429 | if (ub->skb) { | |
430 | kfree_skb(ub->skb); | |
431 | ub->skb = NULL; | |
432 | } | |
433 | } | |
434 | ||
435 | } | |
436 | ||
65b4b4e8 AM |
437 | module_init(ipt_ulog_init); |
438 | module_exit(ipt_ulog_fini); |