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