2 * netfilter module for userspace packet logging daemons
4 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.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>
17 * Released under the terms of the GPL
19 * This module accepts two parameters:
22 * The parameter specifies how big the buffer for each netlink multicast
23 * group is. e.g. If you say nlbufsiz=8192, up to eight kb of packets will
24 * get accumulated in the kernel until they are sent to userspace. It is
25 * NOT possible to allocate more than 128kB, and it is strongly discouraged,
26 * because atomically allocating 128kB inside the network rx softirq is not
27 * reliable. Please also keep in mind that this buffer size is allocated for
28 * each nlgroup you are using, so the total kernel memory usage increases
32 * Specify, after how many clock ticks (intel: 100 per second) the queue
33 * should be flushed even if it is not full yet.
35 * ipt_ULOG.c,v 1.22 2002/10/30 09:07:31 laforge Exp
38 #include <linux/module.h>
39 #include <linux/version.h>
40 #include <linux/config.h>
41 #include <linux/spinlock.h>
42 #include <linux/socket.h>
43 #include <linux/skbuff.h>
44 #include <linux/kernel.h>
45 #include <linux/timer.h>
46 #include <linux/netlink.h>
47 #include <linux/netdevice.h>
49 #include <linux/socket.h>
50 #include <linux/netfilter_ipv4/ip_tables.h>
51 #include <linux/netfilter_ipv4/ipt_ULOG.h>
52 #include <linux/netfilter_ipv4/lockhelp.h>
54 #include <linux/bitops.h>
56 MODULE_LICENSE("GPL");
57 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
58 MODULE_DESCRIPTION("IP tables userspace logging module");
60 #define ULOG_NL_EVENT 111 /* Harald's favorite number */
61 #define ULOG_MAXNLGROUPS 32 /* numer of nlgroups */
64 #define DEBUGP(format, args...) printk(__FILE__ ":" __FUNCTION__ ":" \
67 #define DEBUGP(format, args...)
70 #define PRINTR(format, args...) do { if (net_ratelimit()) printk(format, ## args); } while (0)
72 static unsigned int nlbufsiz
= 4096;
73 MODULE_PARM(nlbufsiz
, "i");
74 MODULE_PARM_DESC(nlbufsiz
, "netlink buffer size");
76 static unsigned int flushtimeout
= 10 * HZ
;
77 MODULE_PARM(flushtimeout
, "i");
78 MODULE_PARM_DESC(flushtimeout
, "buffer flush timeout");
80 /* global data structures */
83 unsigned int qlen
; /* number of nlmsgs' in the skb */
84 struct nlmsghdr
*lastnlh
; /* netlink header of last msg in skb */
85 struct sk_buff
*skb
; /* the pre-allocated skb */
86 struct timer_list timer
; /* the timer function */
89 static ulog_buff_t ulog_buffers
[ULOG_MAXNLGROUPS
]; /* array of buffers */
91 static struct sock
*nflognl
; /* our socket */
92 static size_t qlen
; /* current length of multipart-nlmsg */
93 DECLARE_LOCK(ulog_lock
); /* spinlock */
95 /* send one ulog_buff_t to userspace */
96 static void ulog_send(unsigned int nlgroupnum
)
98 ulog_buff_t
*ub
= &ulog_buffers
[nlgroupnum
];
100 if (timer_pending(&ub
->timer
)) {
101 DEBUGP("ipt_ULOG: ulog_send: timer was pending, deleting\n");
102 del_timer(&ub
->timer
);
105 /* last nlmsg needs NLMSG_DONE */
107 ub
->lastnlh
->nlmsg_type
= NLMSG_DONE
;
109 NETLINK_CB(ub
->skb
).dst_groups
= (1 << nlgroupnum
);
110 DEBUGP("ipt_ULOG: throwing %d packets to netlink mask %u\n",
112 netlink_broadcast(nflognl
, ub
->skb
, 0, (1 << nlgroupnum
), GFP_ATOMIC
);
121 /* timer function to flush queue in ULOG_FLUSH_INTERVAL time */
122 static void ulog_timer(unsigned long data
)
124 DEBUGP("ipt_ULOG: timer function called, calling ulog_send\n");
126 /* lock to protect against somebody modifying our structure
127 * from ipt_ulog_target at the same time */
130 UNLOCK_BH(&ulog_lock
);
133 struct sk_buff
*ulog_alloc_skb(unsigned int size
)
137 /* alloc skb which should be big enough for a whole
138 * multipart message. WARNING: has to be <= 131000
139 * due to slab allocator restrictions */
141 skb
= alloc_skb(nlbufsiz
, GFP_ATOMIC
);
143 PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n",
146 /* try to allocate only as much as we need for
149 skb
= alloc_skb(size
, GFP_ATOMIC
);
151 PRINTR("ipt_ULOG: can't even allocate %ub\n", size
);
157 static unsigned int ipt_ulog_target(struct sk_buff
**pskb
,
158 const struct net_device
*in
,
159 const struct net_device
*out
,
160 unsigned int hooknum
,
161 const void *targinfo
, void *userinfo
)
164 ulog_packet_msg_t
*pm
;
165 size_t size
, copy_len
;
166 struct nlmsghdr
*nlh
;
167 struct ipt_ulog_info
*loginfo
= (struct ipt_ulog_info
*) targinfo
;
169 /* ffs == find first bit set, necessary because userspace
170 * is already shifting groupnumber, but we need unshifted.
171 * ffs() returns [1..32], we need [0..31] */
172 unsigned int groupnum
= ffs(loginfo
->nl_group
) - 1;
174 /* calculate the size of the skb needed */
175 if ((loginfo
->copy_range
== 0) ||
176 (loginfo
->copy_range
> (*pskb
)->len
)) {
177 copy_len
= (*pskb
)->len
;
179 copy_len
= loginfo
->copy_range
;
182 size
= NLMSG_SPACE(sizeof(*pm
) + copy_len
);
184 ub
= &ulog_buffers
[groupnum
];
189 if (!(ub
->skb
= ulog_alloc_skb(size
)))
191 } else if (ub
->qlen
>= loginfo
->qthreshold
||
192 size
> skb_tailroom(ub
->skb
)) {
193 /* either the queue len is too high or we don't have
194 * enough room in nlskb left. send it to userspace. */
198 if (!(ub
->skb
= ulog_alloc_skb(size
)))
202 DEBUGP("ipt_ULOG: qlen %d, qthreshold %d\n", ub
->qlen
,
203 loginfo
->qthreshold
);
205 /* NLMSG_PUT contains a hidden goto nlmsg_failure !!! */
206 nlh
= NLMSG_PUT(ub
->skb
, 0, ub
->qlen
, ULOG_NL_EVENT
,
207 size
- sizeof(*nlh
));
210 pm
= NLMSG_DATA(nlh
);
212 /* copy hook, prefix, timestamp, payload, etc. */
213 pm
->data_len
= copy_len
;
214 pm
->timestamp_sec
= (*pskb
)->stamp
.tv_sec
;
215 pm
->timestamp_usec
= (*pskb
)->stamp
.tv_usec
;
216 pm
->mark
= (*pskb
)->nfmark
;
218 if (loginfo
->prefix
[0] != '\0')
219 strncpy(pm
->prefix
, loginfo
->prefix
, sizeof(pm
->prefix
));
221 *(pm
->prefix
) = '\0';
223 if (in
&& in
->hard_header_len
> 0
224 && (*pskb
)->mac
.raw
!= (void *) (*pskb
)->nh
.iph
225 && in
->hard_header_len
<= ULOG_MAC_LEN
) {
226 memcpy(pm
->mac
, (*pskb
)->mac
.raw
, in
->hard_header_len
);
227 pm
->mac_len
= in
->hard_header_len
;
232 strncpy(pm
->indev_name
, in
->name
, sizeof(pm
->indev_name
));
234 pm
->indev_name
[0] = '\0';
237 strncpy(pm
->outdev_name
, out
->name
, sizeof(pm
->outdev_name
));
239 pm
->outdev_name
[0] = '\0';
241 /* copy_len <= (*pskb)->len, so can't fail. */
242 if (skb_copy_bits(*pskb
, 0, pm
->payload
, copy_len
) < 0)
245 /* check if we are building multi-part messages */
247 ub
->lastnlh
->nlmsg_flags
|= NLM_F_MULTI
;
250 /* if threshold is reached, send message to userspace */
251 if (qlen
>= loginfo
->qthreshold
) {
252 if (loginfo
->qthreshold
> 1)
253 nlh
->nlmsg_type
= NLMSG_DONE
;
258 /* if timer isn't already running, start it */
259 if (!timer_pending(&ub
->timer
)) {
260 ub
->timer
.expires
= jiffies
+ flushtimeout
;
261 add_timer(&ub
->timer
);
264 UNLOCK_BH(&ulog_lock
);
270 PRINTR("ipt_ULOG: error during NLMSG_PUT\n");
273 PRINTR("ipt_ULOG: Error building netlink message\n");
275 UNLOCK_BH(&ulog_lock
);
280 static int ipt_ulog_checkentry(const char *tablename
,
281 const struct ipt_entry
*e
,
283 unsigned int targinfosize
,
284 unsigned int hookmask
)
286 struct ipt_ulog_info
*loginfo
= (struct ipt_ulog_info
*) targinfo
;
288 if (targinfosize
!= IPT_ALIGN(sizeof(struct ipt_ulog_info
))) {
289 DEBUGP("ipt_ULOG: targinfosize %u != 0\n", targinfosize
);
293 if (loginfo
->prefix
[sizeof(loginfo
->prefix
) - 1] != '\0') {
294 DEBUGP("ipt_ULOG: prefix term %i\n",
295 loginfo
->prefix
[sizeof(loginfo
->prefix
) - 1]);
299 if (loginfo
->qthreshold
> ULOG_MAX_QLEN
) {
300 DEBUGP("ipt_ULOG: queue threshold %i > MAX_QLEN\n",
301 loginfo
->qthreshold
);
308 static struct ipt_target ipt_ulog_reg
= {
310 .target
= ipt_ulog_target
,
311 .checkentry
= ipt_ulog_checkentry
,
315 static int __init
init(void)
319 DEBUGP("ipt_ULOG: init module\n");
321 if (nlbufsiz
>= 128*1024) {
322 printk("Netlink buffer has to be <= 128kB\n");
326 /* initialize ulog_buffers */
327 for (i
= 0; i
< ULOG_MAXNLGROUPS
; i
++) {
328 init_timer(&ulog_buffers
[i
].timer
);
329 ulog_buffers
[i
].timer
.function
= ulog_timer
;
330 ulog_buffers
[i
].timer
.data
= i
;
333 nflognl
= netlink_kernel_create(NETLINK_NFLOG
, NULL
);
337 if (ipt_register_target(&ipt_ulog_reg
) != 0) {
338 sock_release(nflognl
->sk_socket
);
345 static void __exit
fini(void)
350 DEBUGP("ipt_ULOG: cleanup_module\n");
352 ipt_unregister_target(&ipt_ulog_reg
);
353 sock_release(nflognl
->sk_socket
);
355 /* remove pending timers and free allocated skb's */
356 for (i
= 0; i
< ULOG_MAXNLGROUPS
; i
++) {
357 ub
= &ulog_buffers
[i
];
358 if (timer_pending(&ub
->timer
)) {
359 DEBUGP("timer was pending, deleting\n");
360 del_timer(&ub
->timer
);