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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 12 | * This module accepts two parameters: |
| 13 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Holger Eitzenberger | c2db292 | 2006-02-04 02:13:14 -0800 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * flushtimeout: |
| 29 | * Specify, after how many hundredths of a second the queue should be |
| 30 | * flushed even if it is not full yet. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | */ |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 32 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/socket.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/skbuff.h> |
| 38 | #include <linux/kernel.h> |
| 39 | #include <linux/timer.h> |
| 40 | #include <linux/netlink.h> |
| 41 | #include <linux/netdevice.h> |
| 42 | #include <linux/mm.h> |
| 43 | #include <linux/moduleparam.h> |
| 44 | #include <linux/netfilter.h> |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 45 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/netfilter_ipv4/ipt_ULOG.h> |
Patrick McHardy | f01ffbd | 2007-12-17 22:38:49 -0800 | [diff] [blame] | 47 | #include <net/netfilter/nf_log.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <net/sock.h> |
| 49 | #include <linux/bitops.h> |
Patrick McHardy | 01102e7 | 2007-04-12 14:27:03 -0700 | [diff] [blame] | 50 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | MODULE_LICENSE("GPL"); |
| 53 | MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 54 | MODULE_DESCRIPTION("Xtables: packet logging to netlink using ULOG"); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 55 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NFLOG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | #define ULOG_NL_EVENT 111 /* Harald's favorite number */ |
| 58 | #define ULOG_MAXNLGROUPS 32 /* numer of nlgroups */ |
| 59 | |
Holger Eitzenberger | c2db292 | 2006-02-04 02:13:14 -0800 | [diff] [blame] | 60 | static unsigned int nlbufsiz = NLMSG_GOODSIZE; |
Patrick McHardy | e7be699 | 2006-01-05 12:19:46 -0800 | [diff] [blame] | 61 | module_param(nlbufsiz, uint, 0400); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | MODULE_PARM_DESC(nlbufsiz, "netlink buffer size"); |
| 63 | |
| 64 | static unsigned int flushtimeout = 10; |
Patrick McHardy | e7be699 | 2006-01-05 12:19:46 -0800 | [diff] [blame] | 65 | module_param(flushtimeout, uint, 0600); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | MODULE_PARM_DESC(flushtimeout, "buffer flush timeout (hundredths of a second)"); |
| 67 | |
Patrick McHardy | e7be699 | 2006-01-05 12:19:46 -0800 | [diff] [blame] | 68 | static int nflog = 1; |
| 69 | module_param(nflog, bool, 0400); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 83 | static struct sock *nflognl; /* our socket */ |
| 84 | static DEFINE_SPINLOCK(ulog_lock); /* spinlock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 92 | pr_debug("ulog_send: timer was pending, deleting\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | del_timer(&ub->timer); |
| 94 | } |
| 95 | |
Mark Huang | dcb7cd9 | 2006-08-13 18:57:54 -0700 | [diff] [blame] | 96 | if (!ub->skb) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 97 | pr_debug("ulog_send: nothing to send\n"); |
Mark Huang | dcb7cd9 | 2006-08-13 18:57:54 -0700 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | /* last nlmsg needs NLMSG_DONE */ |
| 102 | if (ub->qlen > 1) |
| 103 | ub->lastnlh->nlmsg_type = NLMSG_DONE; |
| 104 | |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 105 | NETLINK_CB(ub->skb).dst_group = nlgroupnum + 1; |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 106 | pr_debug("throwing %d packets to netlink group %u\n", |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 107 | ub->qlen, nlgroupnum + 1); |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 108 | netlink_broadcast(nflognl, ub->skb, 0, nlgroupnum + 1, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | ub->qlen = 0; |
| 111 | ub->skb = NULL; |
| 112 | ub->lastnlh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | /* timer function to flush queue in flushtimeout time */ |
| 117 | static void ulog_timer(unsigned long data) |
| 118 | { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 119 | pr_debug("timer function called, calling ulog_send\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | /* lock to protect against somebody modifying our structure |
| 122 | * from ipt_ulog_target at the same time */ |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 123 | spin_lock_bh(&ulog_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | ulog_send(data); |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 125 | spin_unlock_bh(&ulog_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static struct sk_buff *ulog_alloc_skb(unsigned int size) |
| 129 | { |
| 130 | struct sk_buff *skb; |
Patrick McHardy | ad2ad0f | 2006-02-04 02:13:57 -0800 | [diff] [blame] | 131 | unsigned int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Patrick McHardy | ad2ad0f | 2006-02-04 02:13:57 -0800 | [diff] [blame] | 137 | n = max(size, nlbufsiz); |
| 138 | skb = alloc_skb(n, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if (!skb) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 140 | pr_debug("cannot alloc whole buffer %ub!\n", n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Patrick McHardy | ad2ad0f | 2006-02-04 02:13:57 -0800 | [diff] [blame] | 142 | if (n > size) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 143 | /* try to allocate only as much as we need for |
Patrick McHardy | ad2ad0f | 2006-02-04 02:13:57 -0800 | [diff] [blame] | 144 | * current packet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Patrick McHardy | ad2ad0f | 2006-02-04 02:13:57 -0800 | [diff] [blame] | 146 | skb = alloc_skb(size, GFP_ATOMIC); |
| 147 | if (!skb) |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 148 | pr_debug("cannot even allocate %ub\n", size); |
Patrick McHardy | ad2ad0f | 2006-02-04 02:13:57 -0800 | [diff] [blame] | 149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return skb; |
| 153 | } |
| 154 | |
| 155 | static void ipt_ulog_packet(unsigned int hooknum, |
| 156 | const struct sk_buff *skb, |
| 157 | const struct net_device *in, |
| 158 | const struct net_device *out, |
| 159 | const struct ipt_ulog_info *loginfo, |
| 160 | const char *prefix) |
| 161 | { |
| 162 | ulog_buff_t *ub; |
| 163 | ulog_packet_msg_t *pm; |
| 164 | size_t size, copy_len; |
| 165 | struct nlmsghdr *nlh; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 166 | struct timeval tv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | /* ffs == find first bit set, necessary because userspace |
| 169 | * is already shifting groupnumber, but we need unshifted. |
| 170 | * ffs() returns [1..32], we need [0..31] */ |
| 171 | unsigned int groupnum = ffs(loginfo->nl_group) - 1; |
| 172 | |
| 173 | /* calculate the size of the skb needed */ |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 174 | if (loginfo->copy_range == 0 || loginfo->copy_range > skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | copy_len = skb->len; |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 176 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | copy_len = loginfo->copy_range; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | size = NLMSG_SPACE(sizeof(*pm) + copy_len); |
| 180 | |
| 181 | ub = &ulog_buffers[groupnum]; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 182 | |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 183 | spin_lock_bh(&ulog_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | if (!ub->skb) { |
| 186 | if (!(ub->skb = ulog_alloc_skb(size))) |
| 187 | goto alloc_failure; |
| 188 | } else if (ub->qlen >= loginfo->qthreshold || |
| 189 | size > skb_tailroom(ub->skb)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 190 | /* either the queue len is too high or we don't have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | * enough room in nlskb left. send it to userspace. */ |
| 192 | |
| 193 | ulog_send(groupnum); |
| 194 | |
| 195 | if (!(ub->skb = ulog_alloc_skb(size))) |
| 196 | goto alloc_failure; |
| 197 | } |
| 198 | |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 199 | pr_debug("qlen %d, qthreshold %Zu\n", ub->qlen, loginfo->qthreshold); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | /* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 202 | nlh = NLMSG_PUT(ub->skb, 0, ub->qlen, ULOG_NL_EVENT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | sizeof(*pm)+copy_len); |
| 204 | ub->qlen++; |
| 205 | |
| 206 | pm = NLMSG_DATA(nlh); |
| 207 | |
| 208 | /* We might not have a timestamp, get one */ |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 209 | if (skb->tstamp.tv64 == 0) |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 210 | __net_timestamp((struct sk_buff *)skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | /* copy hook, prefix, timestamp, payload, etc. */ |
| 213 | pm->data_len = copy_len; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 214 | tv = ktime_to_timeval(skb->tstamp); |
| 215 | put_unaligned(tv.tv_sec, &pm->timestamp_sec); |
| 216 | put_unaligned(tv.tv_usec, &pm->timestamp_usec); |
Patrick McHardy | 01102e7 | 2007-04-12 14:27:03 -0700 | [diff] [blame] | 217 | put_unaligned(skb->mark, &pm->mark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | pm->hook = hooknum; |
| 219 | if (prefix != NULL) |
| 220 | strncpy(pm->prefix, prefix, sizeof(pm->prefix)); |
| 221 | else if (loginfo->prefix[0] != '\0') |
| 222 | strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix)); |
| 223 | else |
| 224 | *(pm->prefix) = '\0'; |
| 225 | |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 226 | if (in && in->hard_header_len > 0 && |
| 227 | skb->mac_header != skb->network_header && |
| 228 | in->hard_header_len <= ULOG_MAC_LEN) { |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 229 | memcpy(pm->mac, skb_mac_header(skb), in->hard_header_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | pm->mac_len = in->hard_header_len; |
| 231 | } else |
| 232 | pm->mac_len = 0; |
| 233 | |
| 234 | if (in) |
| 235 | strncpy(pm->indev_name, in->name, sizeof(pm->indev_name)); |
| 236 | else |
| 237 | pm->indev_name[0] = '\0'; |
| 238 | |
| 239 | if (out) |
| 240 | strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name)); |
| 241 | else |
| 242 | pm->outdev_name[0] = '\0'; |
| 243 | |
| 244 | /* copy_len <= skb->len, so can't fail. */ |
| 245 | if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0) |
| 246 | BUG(); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | /* check if we are building multi-part messages */ |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 249 | if (ub->qlen > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | ub->lastnlh->nlmsg_flags |= NLM_F_MULTI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | ub->lastnlh = nlh; |
| 253 | |
| 254 | /* if timer isn't already running, start it */ |
| 255 | if (!timer_pending(&ub->timer)) { |
| 256 | ub->timer.expires = jiffies + flushtimeout * HZ / 100; |
| 257 | add_timer(&ub->timer); |
| 258 | } |
| 259 | |
| 260 | /* if threshold is reached, send message to userspace */ |
| 261 | if (ub->qlen >= loginfo->qthreshold) { |
| 262 | if (loginfo->qthreshold > 1) |
| 263 | nlh->nlmsg_type = NLMSG_DONE; |
| 264 | ulog_send(groupnum); |
| 265 | } |
| 266 | |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 267 | spin_unlock_bh(&ulog_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | return; |
| 270 | |
| 271 | nlmsg_failure: |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 272 | pr_debug("error during NLMSG_PUT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | alloc_failure: |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 274 | pr_debug("Error building netlink message\n"); |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 275 | spin_unlock_bh(&ulog_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 278 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 279 | ulog_tg(struct sk_buff *skb, const struct xt_action_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 281 | ipt_ulog_packet(par->hooknum, skb, par->in, par->out, |
| 282 | par->targinfo, NULL); |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 283 | return XT_CONTINUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 285 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 286 | static void ipt_logfn(u_int8_t pf, |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 287 | unsigned int hooknum, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | const struct sk_buff *skb, |
| 289 | const struct net_device *in, |
| 290 | const struct net_device *out, |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 291 | const struct nf_loginfo *li, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | const char *prefix) |
| 293 | { |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 294 | struct ipt_ulog_info loginfo; |
| 295 | |
| 296 | if (!li || li->type != NF_LOG_TYPE_ULOG) { |
| 297 | loginfo.nl_group = ULOG_DEFAULT_NLGROUP; |
| 298 | loginfo.copy_range = 0; |
| 299 | loginfo.qthreshold = ULOG_DEFAULT_QTHRESHOLD; |
| 300 | loginfo.prefix[0] = '\0'; |
| 301 | } else { |
| 302 | loginfo.nl_group = li->u.ulog.group; |
| 303 | loginfo.copy_range = li->u.ulog.copy_len; |
| 304 | loginfo.qthreshold = li->u.ulog.qthreshold; |
| 305 | strlcpy(loginfo.prefix, prefix, sizeof(loginfo.prefix)); |
| 306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
| 308 | ipt_ulog_packet(hooknum, skb, in, out, &loginfo, prefix); |
| 309 | } |
| 310 | |
Jan Engelhardt | 135367b | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 311 | static int ulog_tg_check(const struct xt_tgchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 313 | const struct ipt_ulog_info *loginfo = par->targinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 316 | pr_debug("prefix not null-terminated\n"); |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 317 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | if (loginfo->qthreshold > ULOG_MAX_QLEN) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 320 | pr_debug("queue threshold %Zu > MAX_QLEN\n", |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 321 | loginfo->qthreshold); |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 322 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 324 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Patrick McHardy | b076deb | 2007-04-12 22:17:05 -0700 | [diff] [blame] | 327 | #ifdef CONFIG_COMPAT |
| 328 | struct compat_ipt_ulog_info { |
| 329 | compat_uint_t nl_group; |
| 330 | compat_size_t copy_range; |
| 331 | compat_size_t qthreshold; |
| 332 | char prefix[ULOG_PREFIX_LEN]; |
| 333 | }; |
| 334 | |
Jan Engelhardt | 739674f | 2009-06-26 08:23:19 +0200 | [diff] [blame] | 335 | static void ulog_tg_compat_from_user(void *dst, const void *src) |
Patrick McHardy | b076deb | 2007-04-12 22:17:05 -0700 | [diff] [blame] | 336 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 337 | const struct compat_ipt_ulog_info *cl = src; |
Patrick McHardy | b076deb | 2007-04-12 22:17:05 -0700 | [diff] [blame] | 338 | struct ipt_ulog_info l = { |
| 339 | .nl_group = cl->nl_group, |
| 340 | .copy_range = cl->copy_range, |
| 341 | .qthreshold = cl->qthreshold, |
| 342 | }; |
| 343 | |
| 344 | memcpy(l.prefix, cl->prefix, sizeof(l.prefix)); |
| 345 | memcpy(dst, &l, sizeof(l)); |
| 346 | } |
| 347 | |
Jan Engelhardt | 739674f | 2009-06-26 08:23:19 +0200 | [diff] [blame] | 348 | static int ulog_tg_compat_to_user(void __user *dst, const void *src) |
Patrick McHardy | b076deb | 2007-04-12 22:17:05 -0700 | [diff] [blame] | 349 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 350 | const struct ipt_ulog_info *l = src; |
Patrick McHardy | b076deb | 2007-04-12 22:17:05 -0700 | [diff] [blame] | 351 | struct compat_ipt_ulog_info cl = { |
| 352 | .nl_group = l->nl_group, |
| 353 | .copy_range = l->copy_range, |
| 354 | .qthreshold = l->qthreshold, |
| 355 | }; |
| 356 | |
| 357 | memcpy(cl.prefix, l->prefix, sizeof(cl.prefix)); |
| 358 | return copy_to_user(dst, &cl, sizeof(cl)) ? -EFAULT : 0; |
| 359 | } |
| 360 | #endif /* CONFIG_COMPAT */ |
| 361 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 362 | static struct xt_target ulog_tg_reg __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | .name = "ULOG", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 364 | .family = NFPROTO_IPV4, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 365 | .target = ulog_tg, |
Patrick McHardy | 1d5cd90 | 2006-03-20 18:01:14 -0800 | [diff] [blame] | 366 | .targetsize = sizeof(struct ipt_ulog_info), |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 367 | .checkentry = ulog_tg_check, |
Patrick McHardy | b076deb | 2007-04-12 22:17:05 -0700 | [diff] [blame] | 368 | #ifdef CONFIG_COMPAT |
| 369 | .compatsize = sizeof(struct compat_ipt_ulog_info), |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 370 | .compat_from_user = ulog_tg_compat_from_user, |
| 371 | .compat_to_user = ulog_tg_compat_to_user, |
Patrick McHardy | b076deb | 2007-04-12 22:17:05 -0700 | [diff] [blame] | 372 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | .me = THIS_MODULE, |
| 374 | }; |
| 375 | |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 376 | static struct nf_logger ipt_ulog_logger __read_mostly = { |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 377 | .name = "ipt_ULOG", |
Patrick McHardy | 1d5cd90 | 2006-03-20 18:01:14 -0800 | [diff] [blame] | 378 | .logfn = ipt_logfn, |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 379 | .me = THIS_MODULE, |
| 380 | }; |
| 381 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 382 | static int __init ulog_tg_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
Jan Engelhardt | e1fd058 | 2007-02-07 15:10:34 -0800 | [diff] [blame] | 384 | int ret, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 386 | pr_debug("init module\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
Patrick McHardy | e7be699 | 2006-01-05 12:19:46 -0800 | [diff] [blame] | 388 | if (nlbufsiz > 128*1024) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 389 | pr_warning("Netlink buffer has to be <= 128kB\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | return -EINVAL; |
| 391 | } |
| 392 | |
| 393 | /* initialize ulog_buffers */ |
Patrick McHardy | e6f689d | 2007-03-23 11:16:30 -0700 | [diff] [blame] | 394 | for (i = 0; i < ULOG_MAXNLGROUPS; i++) |
| 395 | setup_timer(&ulog_buffers[i].timer, ulog_timer, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 397 | nflognl = netlink_kernel_create(&init_net, |
| 398 | NETLINK_NFLOG, ULOG_MAXNLGROUPS, NULL, |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 399 | NULL, THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (!nflognl) |
| 401 | return -ENOMEM; |
| 402 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 403 | ret = xt_register_target(&ulog_tg_reg); |
Jan Engelhardt | e1fd058 | 2007-02-07 15:10:34 -0800 | [diff] [blame] | 404 | if (ret < 0) { |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 405 | netlink_kernel_release(nflognl); |
Jan Engelhardt | e1fd058 | 2007-02-07 15:10:34 -0800 | [diff] [blame] | 406 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | if (nflog) |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 409 | nf_log_register(NFPROTO_IPV4, &ipt_ulog_logger); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 414 | static void __exit ulog_tg_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | { |
| 416 | ulog_buff_t *ub; |
| 417 | int i; |
| 418 | |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 419 | pr_debug("cleanup_module\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | if (nflog) |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 422 | nf_log_unregister(&ipt_ulog_logger); |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 423 | xt_unregister_target(&ulog_tg_reg); |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 424 | netlink_kernel_release(nflognl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 430 | pr_debug("timer was pending, deleting\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | del_timer(&ub->timer); |
| 432 | } |
| 433 | |
| 434 | if (ub->skb) { |
| 435 | kfree_skb(ub->skb); |
| 436 | ub->skb = NULL; |
| 437 | } |
| 438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 441 | module_init(ulog_tg_init); |
| 442 | module_exit(ulog_tg_exit); |