2  * netfilter module for userspace packet logging daemons
 
   4  * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
 
   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
 
  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.
 
  19  * (C) 1999-2001 Paul `Rusty' Russell
 
  20  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
 
  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.
 
  26  * This module accepts two parameters: 
 
  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
 
  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.
 
  43  *   Specify, after how many hundredths of a second the queue should be
 
  44  *   flushed even if it is not full yet.
 
  46  * ipt_ULOG.c,v 1.22 2002/10/30 09:07:31 laforge Exp
 
  49 #include <linux/module.h>
 
  50 #include <linux/config.h>
 
  51 #include <linux/spinlock.h>
 
  52 #include <linux/socket.h>
 
  53 #include <linux/skbuff.h>
 
  54 #include <linux/kernel.h>
 
  55 #include <linux/timer.h>
 
  56 #include <linux/netlink.h>
 
  57 #include <linux/netdevice.h>
 
  59 #include <linux/moduleparam.h>
 
  60 #include <linux/netfilter.h>
 
  61 #include <linux/netfilter_ipv4/ip_tables.h>
 
  62 #include <linux/netfilter_ipv4/ipt_ULOG.h>
 
  64 #include <linux/bitops.h>
 
  66 MODULE_LICENSE("GPL");
 
  67 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
 
  68 MODULE_DESCRIPTION("iptables userspace logging module");
 
  69 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NFLOG);
 
  71 #define ULOG_NL_EVENT           111             /* Harald's favorite number */
 
  72 #define ULOG_MAXNLGROUPS        32              /* numer of nlgroups */
 
  75 #define DEBUGP(format, args...) printk("%s:%s:" format, \
 
  76                                        __FILE__, __FUNCTION__ , ## args)
 
  78 #define DEBUGP(format, args...)
 
  81 #define PRINTR(format, args...) do { if (net_ratelimit()) printk(format , ## args); } while (0)
 
  83 static unsigned int nlbufsiz = NLMSG_GOODSIZE;
 
  84 module_param(nlbufsiz, uint, 0400);
 
  85 MODULE_PARM_DESC(nlbufsiz, "netlink buffer size");
 
  87 static unsigned int flushtimeout = 10;
 
  88 module_param(flushtimeout, uint, 0600);
 
  89 MODULE_PARM_DESC(flushtimeout, "buffer flush timeout (hundredths of a second)");
 
  92 module_param(nflog, bool, 0400);
 
  93 MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
 
  95 /* global data structures */
 
  98         unsigned int qlen;              /* number of nlmsgs' in the skb */
 
  99         struct nlmsghdr *lastnlh;       /* netlink header of last msg in skb */
 
 100         struct sk_buff *skb;            /* the pre-allocated skb */
 
 101         struct timer_list timer;        /* the timer function */
 
 104 static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS];      /* array of buffers */
 
 106 static struct sock *nflognl;            /* our socket */
 
 107 static DEFINE_SPINLOCK(ulog_lock);      /* spinlock */
 
 109 /* send one ulog_buff_t to userspace */
 
 110 static void ulog_send(unsigned int nlgroupnum)
 
 112         ulog_buff_t *ub = &ulog_buffers[nlgroupnum];
 
 114         if (timer_pending(&ub->timer)) {
 
 115                 DEBUGP("ipt_ULOG: ulog_send: timer was pending, deleting\n");
 
 116                 del_timer(&ub->timer);
 
 119         /* last nlmsg needs NLMSG_DONE */
 
 121                 ub->lastnlh->nlmsg_type = NLMSG_DONE;
 
 123         NETLINK_CB(ub->skb).dst_group = nlgroupnum + 1;
 
 124         DEBUGP("ipt_ULOG: throwing %d packets to netlink group %u\n",
 
 125                 ub->qlen, nlgroupnum + 1);
 
 126         netlink_broadcast(nflognl, ub->skb, 0, nlgroupnum + 1, GFP_ATOMIC);
 
 135 /* timer function to flush queue in flushtimeout time */
 
 136 static void ulog_timer(unsigned long data)
 
 138         DEBUGP("ipt_ULOG: timer function called, calling ulog_send\n");
 
 140         /* lock to protect against somebody modifying our structure
 
 141          * from ipt_ulog_target at the same time */
 
 142         spin_lock_bh(&ulog_lock);
 
 144         spin_unlock_bh(&ulog_lock);
 
 147 static struct sk_buff *ulog_alloc_skb(unsigned int size)
 
 152         /* alloc skb which should be big enough for a whole
 
 153          * multipart message. WARNING: has to be <= 131000
 
 154          * due to slab allocator restrictions */
 
 156         n = max(size, nlbufsiz);
 
 157         skb = alloc_skb(n, GFP_ATOMIC);
 
 159                 PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n", n);
 
 162                         /* try to allocate only as much as we need for 
 
 165                         skb = alloc_skb(size, GFP_ATOMIC);
 
 167                                 PRINTR("ipt_ULOG: can't even allocate %ub\n",
 
 175 static void ipt_ulog_packet(unsigned int hooknum,
 
 176                             const struct sk_buff *skb,
 
 177                             const struct net_device *in,
 
 178                             const struct net_device *out,
 
 179                             const struct ipt_ulog_info *loginfo,
 
 183         ulog_packet_msg_t *pm;
 
 184         size_t size, copy_len;
 
 185         struct nlmsghdr *nlh;
 
 187         /* ffs == find first bit set, necessary because userspace
 
 188          * is already shifting groupnumber, but we need unshifted.
 
 189          * ffs() returns [1..32], we need [0..31] */
 
 190         unsigned int groupnum = ffs(loginfo->nl_group) - 1;
 
 192         /* calculate the size of the skb needed */
 
 193         if ((loginfo->copy_range == 0) ||
 
 194             (loginfo->copy_range > skb->len)) {
 
 197                 copy_len = loginfo->copy_range;
 
 200         size = NLMSG_SPACE(sizeof(*pm) + copy_len);
 
 202         ub = &ulog_buffers[groupnum];
 
 204         spin_lock_bh(&ulog_lock);
 
 207                 if (!(ub->skb = ulog_alloc_skb(size)))
 
 209         } else if (ub->qlen >= loginfo->qthreshold ||
 
 210                    size > skb_tailroom(ub->skb)) {
 
 211                 /* either the queue len is too high or we don't have 
 
 212                  * enough room in nlskb left. send it to userspace. */
 
 216                 if (!(ub->skb = ulog_alloc_skb(size)))
 
 220         DEBUGP("ipt_ULOG: qlen %d, qthreshold %d\n", ub->qlen, 
 
 221                 loginfo->qthreshold);
 
 223         /* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */
 
 224         nlh = NLMSG_PUT(ub->skb, 0, ub->qlen, ULOG_NL_EVENT, 
 
 225                         sizeof(*pm)+copy_len);
 
 228         pm = NLMSG_DATA(nlh);
 
 230         /* We might not have a timestamp, get one */
 
 231         if (skb->tstamp.off_sec == 0)
 
 232                 __net_timestamp((struct sk_buff *)skb);
 
 234         /* copy hook, prefix, timestamp, payload, etc. */
 
 235         pm->data_len = copy_len;
 
 236         pm->timestamp_sec = skb->tstamp.off_sec;
 
 237         pm->timestamp_usec = skb->tstamp.off_usec;
 
 238         pm->mark = skb->nfmark;
 
 241                 strncpy(pm->prefix, prefix, sizeof(pm->prefix));
 
 242         else if (loginfo->prefix[0] != '\0')
 
 243                 strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
 
 245                 *(pm->prefix) = '\0';
 
 247         if (in && in->hard_header_len > 0
 
 248             && skb->mac.raw != (void *) skb->nh.iph
 
 249             && in->hard_header_len <= ULOG_MAC_LEN) {
 
 250                 memcpy(pm->mac, skb->mac.raw, in->hard_header_len);
 
 251                 pm->mac_len = in->hard_header_len;
 
 256                 strncpy(pm->indev_name, in->name, sizeof(pm->indev_name));
 
 258                 pm->indev_name[0] = '\0';
 
 261                 strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
 
 263                 pm->outdev_name[0] = '\0';
 
 265         /* copy_len <= skb->len, so can't fail. */
 
 266         if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0)
 
 269         /* check if we are building multi-part messages */
 
 271                 ub->lastnlh->nlmsg_flags |= NLM_F_MULTI;
 
 276         /* if timer isn't already running, start it */
 
 277         if (!timer_pending(&ub->timer)) {
 
 278                 ub->timer.expires = jiffies + flushtimeout * HZ / 100;
 
 279                 add_timer(&ub->timer);
 
 282         /* if threshold is reached, send message to userspace */
 
 283         if (ub->qlen >= loginfo->qthreshold) {
 
 284                 if (loginfo->qthreshold > 1)
 
 285                         nlh->nlmsg_type = NLMSG_DONE;
 
 289         spin_unlock_bh(&ulog_lock);
 
 294         PRINTR("ipt_ULOG: error during NLMSG_PUT\n");
 
 297         PRINTR("ipt_ULOG: Error building netlink message\n");
 
 299         spin_unlock_bh(&ulog_lock);
 
 302 static unsigned int ipt_ulog_target(struct sk_buff **pskb,
 
 303                                     const struct net_device *in,
 
 304                                     const struct net_device *out,
 
 305                                     unsigned int hooknum,
 
 306                                     const struct xt_target *target,
 
 307                                     const void *targinfo, void *userinfo)
 
 309         struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
 
 311         ipt_ulog_packet(hooknum, *pskb, in, out, loginfo, NULL);
 
 316 static void ipt_logfn(unsigned int pf,
 
 317                       unsigned int hooknum,
 
 318                       const struct sk_buff *skb,
 
 319                       const struct net_device *in,
 
 320                       const struct net_device *out,
 
 321                       const struct nf_loginfo *li,
 
 324         struct ipt_ulog_info loginfo;
 
 326         if (!li || li->type != NF_LOG_TYPE_ULOG) {
 
 327                 loginfo.nl_group = ULOG_DEFAULT_NLGROUP;
 
 328                 loginfo.copy_range = 0;
 
 329                 loginfo.qthreshold = ULOG_DEFAULT_QTHRESHOLD;
 
 330                 loginfo.prefix[0] = '\0';
 
 332                 loginfo.nl_group = li->u.ulog.group;
 
 333                 loginfo.copy_range = li->u.ulog.copy_len;
 
 334                 loginfo.qthreshold = li->u.ulog.qthreshold;
 
 335                 strlcpy(loginfo.prefix, prefix, sizeof(loginfo.prefix));
 
 338         ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix);
 
 341 static int ipt_ulog_checkentry(const char *tablename,
 
 343                                const struct xt_target *target,
 
 345                                unsigned int targinfosize,
 
 346                                unsigned int hookmask)
 
 348         struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
 
 350         if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') {
 
 351                 DEBUGP("ipt_ULOG: prefix term %i\n",
 
 352                        loginfo->prefix[sizeof(loginfo->prefix) - 1]);
 
 355         if (loginfo->qthreshold > ULOG_MAX_QLEN) {
 
 356                 DEBUGP("ipt_ULOG: queue threshold %i > MAX_QLEN\n",
 
 357                         loginfo->qthreshold);
 
 363 static struct ipt_target ipt_ulog_reg = {
 
 365         .target         = ipt_ulog_target,
 
 366         .targetsize     = sizeof(struct ipt_ulog_info),
 
 367         .checkentry     = ipt_ulog_checkentry,
 
 371 static struct nf_logger ipt_ulog_logger = {
 
 377 static int __init ipt_ulog_init(void)
 
 381         DEBUGP("ipt_ULOG: init module\n");
 
 383         if (nlbufsiz > 128*1024) {
 
 384                 printk("Netlink buffer has to be <= 128kB\n");
 
 388         /* initialize ulog_buffers */
 
 389         for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
 
 390                 init_timer(&ulog_buffers[i].timer);
 
 391                 ulog_buffers[i].timer.function = ulog_timer;
 
 392                 ulog_buffers[i].timer.data = i;
 
 395         nflognl = netlink_kernel_create(NETLINK_NFLOG, ULOG_MAXNLGROUPS, NULL,
 
 400         if (ipt_register_target(&ipt_ulog_reg) != 0) {
 
 401                 sock_release(nflognl->sk_socket);
 
 405                 nf_log_register(PF_INET, &ipt_ulog_logger);
 
 410 static void __exit ipt_ulog_fini(void)
 
 415         DEBUGP("ipt_ULOG: cleanup_module\n");
 
 418                 nf_log_unregister_logger(&ipt_ulog_logger);
 
 419         ipt_unregister_target(&ipt_ulog_reg);
 
 420         sock_release(nflognl->sk_socket);
 
 422         /* remove pending timers and free allocated skb's */
 
 423         for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
 
 424                 ub = &ulog_buffers[i];
 
 425                 if (timer_pending(&ub->timer)) {
 
 426                         DEBUGP("timer was pending, deleting\n");
 
 427                         del_timer(&ub->timer);
 
 438 module_init(ipt_ulog_init);
 
 439 module_exit(ipt_ulog_fini);