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 | |
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> | |
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, \ | |
e905a9ed | 75 | __FILE__, __FUNCTION__ , ## args) |
1da177e4 LT |
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 | ||
dcb7cd97 MH |
118 | if (!ub->skb) { |
119 | DEBUGP("ipt_ULOG: ulog_send: nothing to send\n"); | |
120 | return; | |
121 | } | |
122 | ||
1da177e4 LT |
123 | /* last nlmsg needs NLMSG_DONE */ |
124 | if (ub->qlen > 1) | |
125 | ub->lastnlh->nlmsg_type = NLMSG_DONE; | |
126 | ||
ac6d439d PM |
127 | NETLINK_CB(ub->skb).dst_group = nlgroupnum + 1; |
128 | DEBUGP("ipt_ULOG: throwing %d packets to netlink group %u\n", | |
129 | ub->qlen, nlgroupnum + 1); | |
130 | netlink_broadcast(nflognl, ub->skb, 0, nlgroupnum + 1, GFP_ATOMIC); | |
1da177e4 LT |
131 | |
132 | ub->qlen = 0; | |
133 | ub->skb = NULL; | |
134 | ub->lastnlh = NULL; | |
1da177e4 LT |
135 | } |
136 | ||
137 | ||
138 | /* timer function to flush queue in flushtimeout time */ | |
139 | static void ulog_timer(unsigned long data) | |
140 | { | |
141 | DEBUGP("ipt_ULOG: timer function called, calling ulog_send\n"); | |
142 | ||
143 | /* lock to protect against somebody modifying our structure | |
144 | * from ipt_ulog_target at the same time */ | |
e45b1be8 | 145 | spin_lock_bh(&ulog_lock); |
1da177e4 | 146 | ulog_send(data); |
e45b1be8 | 147 | spin_unlock_bh(&ulog_lock); |
1da177e4 LT |
148 | } |
149 | ||
150 | static struct sk_buff *ulog_alloc_skb(unsigned int size) | |
151 | { | |
152 | struct sk_buff *skb; | |
ad2ad0f9 | 153 | unsigned int n; |
1da177e4 LT |
154 | |
155 | /* alloc skb which should be big enough for a whole | |
156 | * multipart message. WARNING: has to be <= 131000 | |
157 | * due to slab allocator restrictions */ | |
158 | ||
ad2ad0f9 PM |
159 | n = max(size, nlbufsiz); |
160 | skb = alloc_skb(n, GFP_ATOMIC); | |
1da177e4 | 161 | if (!skb) { |
ad2ad0f9 | 162 | PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n", n); |
1da177e4 | 163 | |
ad2ad0f9 | 164 | if (n > size) { |
e905a9ed | 165 | /* try to allocate only as much as we need for |
ad2ad0f9 | 166 | * current packet */ |
1da177e4 | 167 | |
ad2ad0f9 PM |
168 | skb = alloc_skb(size, GFP_ATOMIC); |
169 | if (!skb) | |
170 | PRINTR("ipt_ULOG: can't even allocate %ub\n", | |
171 | size); | |
172 | } | |
1da177e4 LT |
173 | } |
174 | ||
175 | return skb; | |
176 | } | |
177 | ||
178 | static void ipt_ulog_packet(unsigned int hooknum, | |
179 | const struct sk_buff *skb, | |
180 | const struct net_device *in, | |
181 | const struct net_device *out, | |
182 | const struct ipt_ulog_info *loginfo, | |
183 | const char *prefix) | |
184 | { | |
185 | ulog_buff_t *ub; | |
186 | ulog_packet_msg_t *pm; | |
187 | size_t size, copy_len; | |
188 | struct nlmsghdr *nlh; | |
189 | ||
190 | /* ffs == find first bit set, necessary because userspace | |
191 | * is already shifting groupnumber, but we need unshifted. | |
192 | * ffs() returns [1..32], we need [0..31] */ | |
193 | unsigned int groupnum = ffs(loginfo->nl_group) - 1; | |
194 | ||
195 | /* calculate the size of the skb needed */ | |
196 | if ((loginfo->copy_range == 0) || | |
197 | (loginfo->copy_range > skb->len)) { | |
198 | copy_len = skb->len; | |
199 | } else { | |
200 | copy_len = loginfo->copy_range; | |
201 | } | |
202 | ||
203 | size = NLMSG_SPACE(sizeof(*pm) + copy_len); | |
204 | ||
205 | ub = &ulog_buffers[groupnum]; | |
e905a9ed | 206 | |
e45b1be8 | 207 | spin_lock_bh(&ulog_lock); |
1da177e4 LT |
208 | |
209 | if (!ub->skb) { | |
210 | if (!(ub->skb = ulog_alloc_skb(size))) | |
211 | goto alloc_failure; | |
212 | } else if (ub->qlen >= loginfo->qthreshold || | |
213 | size > skb_tailroom(ub->skb)) { | |
e905a9ed | 214 | /* either the queue len is too high or we don't have |
1da177e4 LT |
215 | * enough room in nlskb left. send it to userspace. */ |
216 | ||
217 | ulog_send(groupnum); | |
218 | ||
219 | if (!(ub->skb = ulog_alloc_skb(size))) | |
220 | goto alloc_failure; | |
221 | } | |
222 | ||
e905a9ed | 223 | DEBUGP("ipt_ULOG: qlen %d, qthreshold %d\n", ub->qlen, |
1da177e4 LT |
224 | loginfo->qthreshold); |
225 | ||
226 | /* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */ | |
e905a9ed | 227 | nlh = NLMSG_PUT(ub->skb, 0, ub->qlen, ULOG_NL_EVENT, |
1da177e4 LT |
228 | sizeof(*pm)+copy_len); |
229 | ub->qlen++; | |
230 | ||
231 | pm = NLMSG_DATA(nlh); | |
232 | ||
233 | /* We might not have a timestamp, get one */ | |
a61bbcf2 PM |
234 | if (skb->tstamp.off_sec == 0) |
235 | __net_timestamp((struct sk_buff *)skb); | |
1da177e4 LT |
236 | |
237 | /* copy hook, prefix, timestamp, payload, etc. */ | |
238 | pm->data_len = copy_len; | |
325ed823 HX |
239 | pm->timestamp_sec = skb->tstamp.off_sec; |
240 | pm->timestamp_usec = skb->tstamp.off_usec; | |
82e91ffe | 241 | pm->mark = skb->mark; |
1da177e4 LT |
242 | pm->hook = hooknum; |
243 | if (prefix != NULL) | |
244 | strncpy(pm->prefix, prefix, sizeof(pm->prefix)); | |
245 | else if (loginfo->prefix[0] != '\0') | |
246 | strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix)); | |
247 | else | |
248 | *(pm->prefix) = '\0'; | |
249 | ||
250 | if (in && in->hard_header_len > 0 | |
251 | && skb->mac.raw != (void *) skb->nh.iph | |
252 | && in->hard_header_len <= ULOG_MAC_LEN) { | |
253 | memcpy(pm->mac, skb->mac.raw, in->hard_header_len); | |
254 | pm->mac_len = in->hard_header_len; | |
255 | } else | |
256 | pm->mac_len = 0; | |
257 | ||
258 | if (in) | |
259 | strncpy(pm->indev_name, in->name, sizeof(pm->indev_name)); | |
260 | else | |
261 | pm->indev_name[0] = '\0'; | |
262 | ||
263 | if (out) | |
264 | strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name)); | |
265 | else | |
266 | pm->outdev_name[0] = '\0'; | |
267 | ||
268 | /* copy_len <= skb->len, so can't fail. */ | |
269 | if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0) | |
270 | BUG(); | |
e905a9ed | 271 | |
1da177e4 LT |
272 | /* check if we are building multi-part messages */ |
273 | if (ub->qlen > 1) { | |
274 | ub->lastnlh->nlmsg_flags |= NLM_F_MULTI; | |
275 | } | |
276 | ||
277 | ub->lastnlh = nlh; | |
278 | ||
279 | /* if timer isn't already running, start it */ | |
280 | if (!timer_pending(&ub->timer)) { | |
281 | ub->timer.expires = jiffies + flushtimeout * HZ / 100; | |
282 | add_timer(&ub->timer); | |
283 | } | |
284 | ||
285 | /* if threshold is reached, send message to userspace */ | |
286 | if (ub->qlen >= loginfo->qthreshold) { | |
287 | if (loginfo->qthreshold > 1) | |
288 | nlh->nlmsg_type = NLMSG_DONE; | |
289 | ulog_send(groupnum); | |
290 | } | |
291 | ||
e45b1be8 | 292 | spin_unlock_bh(&ulog_lock); |
1da177e4 LT |
293 | |
294 | return; | |
295 | ||
296 | nlmsg_failure: | |
297 | PRINTR("ipt_ULOG: error during NLMSG_PUT\n"); | |
298 | ||
299 | alloc_failure: | |
300 | PRINTR("ipt_ULOG: Error building netlink message\n"); | |
301 | ||
e45b1be8 | 302 | spin_unlock_bh(&ulog_lock); |
1da177e4 LT |
303 | } |
304 | ||
305 | static unsigned int ipt_ulog_target(struct sk_buff **pskb, | |
306 | const struct net_device *in, | |
307 | const struct net_device *out, | |
308 | unsigned int hooknum, | |
c4986734 | 309 | const struct xt_target *target, |
fe1cb108 | 310 | const void *targinfo) |
1da177e4 LT |
311 | { |
312 | struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo; | |
313 | ||
314 | ipt_ulog_packet(hooknum, *pskb, in, out, loginfo, NULL); | |
e905a9ed | 315 | |
6709dbbb | 316 | return XT_CONTINUE; |
1da177e4 | 317 | } |
e905a9ed | 318 | |
608c8e4f HW |
319 | static void ipt_logfn(unsigned int pf, |
320 | unsigned int hooknum, | |
1da177e4 LT |
321 | const struct sk_buff *skb, |
322 | const struct net_device *in, | |
323 | const struct net_device *out, | |
608c8e4f | 324 | const struct nf_loginfo *li, |
1da177e4 LT |
325 | const char *prefix) |
326 | { | |
608c8e4f HW |
327 | struct ipt_ulog_info loginfo; |
328 | ||
329 | if (!li || li->type != NF_LOG_TYPE_ULOG) { | |
330 | loginfo.nl_group = ULOG_DEFAULT_NLGROUP; | |
331 | loginfo.copy_range = 0; | |
332 | loginfo.qthreshold = ULOG_DEFAULT_QTHRESHOLD; | |
333 | loginfo.prefix[0] = '\0'; | |
334 | } else { | |
335 | loginfo.nl_group = li->u.ulog.group; | |
336 | loginfo.copy_range = li->u.ulog.copy_len; | |
337 | loginfo.qthreshold = li->u.ulog.qthreshold; | |
338 | strlcpy(loginfo.prefix, prefix, sizeof(loginfo.prefix)); | |
339 | } | |
1da177e4 LT |
340 | |
341 | ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix); | |
342 | } | |
343 | ||
344 | static int ipt_ulog_checkentry(const char *tablename, | |
2e4e6a17 | 345 | const void *e, |
c4986734 | 346 | const struct xt_target *target, |
1da177e4 | 347 | void *targinfo, |
1da177e4 LT |
348 | unsigned int hookmask) |
349 | { | |
350 | struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo; | |
351 | ||
1da177e4 LT |
352 | if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') { |
353 | DEBUGP("ipt_ULOG: prefix term %i\n", | |
354 | loginfo->prefix[sizeof(loginfo->prefix) - 1]); | |
355 | return 0; | |
356 | } | |
1da177e4 LT |
357 | if (loginfo->qthreshold > ULOG_MAX_QLEN) { |
358 | DEBUGP("ipt_ULOG: queue threshold %i > MAX_QLEN\n", | |
359 | loginfo->qthreshold); | |
360 | return 0; | |
361 | } | |
1da177e4 LT |
362 | return 1; |
363 | } | |
364 | ||
6709dbbb | 365 | static struct xt_target ipt_ulog_reg = { |
1da177e4 | 366 | .name = "ULOG", |
6709dbbb | 367 | .family = AF_INET, |
1da177e4 | 368 | .target = ipt_ulog_target, |
1d5cd909 | 369 | .targetsize = sizeof(struct ipt_ulog_info), |
1da177e4 LT |
370 | .checkentry = ipt_ulog_checkentry, |
371 | .me = THIS_MODULE, | |
372 | }; | |
373 | ||
608c8e4f HW |
374 | static struct nf_logger ipt_ulog_logger = { |
375 | .name = "ipt_ULOG", | |
1d5cd909 | 376 | .logfn = ipt_logfn, |
608c8e4f HW |
377 | .me = THIS_MODULE, |
378 | }; | |
379 | ||
65b4b4e8 | 380 | static int __init ipt_ulog_init(void) |
1da177e4 | 381 | { |
e1fd0586 | 382 | int ret, i; |
1da177e4 LT |
383 | |
384 | DEBUGP("ipt_ULOG: init module\n"); | |
385 | ||
e7be6994 | 386 | if (nlbufsiz > 128*1024) { |
1da177e4 LT |
387 | printk("Netlink buffer has to be <= 128kB\n"); |
388 | return -EINVAL; | |
389 | } | |
390 | ||
391 | /* initialize ulog_buffers */ | |
392 | for (i = 0; i < ULOG_MAXNLGROUPS; i++) { | |
393 | init_timer(&ulog_buffers[i].timer); | |
394 | ulog_buffers[i].timer.function = ulog_timer; | |
395 | ulog_buffers[i].timer.data = i; | |
396 | } | |
397 | ||
06628607 | 398 | nflognl = netlink_kernel_create(NETLINK_NFLOG, ULOG_MAXNLGROUPS, NULL, |
e905a9ed | 399 | THIS_MODULE); |
1da177e4 LT |
400 | if (!nflognl) |
401 | return -ENOMEM; | |
402 | ||
6709dbbb | 403 | ret = xt_register_target(&ipt_ulog_reg); |
e1fd0586 | 404 | if (ret < 0) { |
1da177e4 | 405 | sock_release(nflognl->sk_socket); |
e1fd0586 | 406 | return ret; |
1da177e4 LT |
407 | } |
408 | if (nflog) | |
608c8e4f | 409 | nf_log_register(PF_INET, &ipt_ulog_logger); |
e905a9ed | 410 | |
1da177e4 LT |
411 | return 0; |
412 | } | |
413 | ||
65b4b4e8 | 414 | static void __exit ipt_ulog_fini(void) |
1da177e4 LT |
415 | { |
416 | ulog_buff_t *ub; | |
417 | int i; | |
418 | ||
419 | DEBUGP("ipt_ULOG: cleanup_module\n"); | |
420 | ||
421 | if (nflog) | |
608c8e4f | 422 | nf_log_unregister_logger(&ipt_ulog_logger); |
6709dbbb | 423 | xt_unregister_target(&ipt_ulog_reg); |
1da177e4 LT |
424 | sock_release(nflognl->sk_socket); |
425 | ||
426 | /* remove pending timers and free allocated skb's */ | |
427 | for (i = 0; i < ULOG_MAXNLGROUPS; i++) { | |
428 | ub = &ulog_buffers[i]; | |
429 | if (timer_pending(&ub->timer)) { | |
430 | DEBUGP("timer was pending, deleting\n"); | |
431 | del_timer(&ub->timer); | |
432 | } | |
433 | ||
434 | if (ub->skb) { | |
435 | kfree_skb(ub->skb); | |
436 | ub->skb = NULL; | |
437 | } | |
438 | } | |
1da177e4 LT |
439 | } |
440 | ||
65b4b4e8 AM |
441 | module_init(ipt_ulog_init); |
442 | module_exit(ipt_ulog_fini); |