OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [ipv4/] [netfilter/] [ipt_ULOG.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * netfilter module for userspace packet logging daemons
3
 *
4
 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.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
 *
17
 * Released under the terms of the GPL
18
 *
19
 * This module accepts two parameters:
20
 *
21
 * nlbufsiz:
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
29
 * by that factor.
30
 *
31
 * flushtimeout:
32
 *   Specify, after how many clock ticks (intel: 100 per second) the queue
33
 * should be flushed even if it is not full yet.
34
 *
35
 * ipt_ULOG.c,v 1.22 2002/10/30 09:07:31 laforge Exp
36
 */
37
 
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>
48
#include <linux/mm.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>
53
#include <net/sock.h>
54
#include <linux/bitops.h>
55
 
56
MODULE_LICENSE("GPL");
57
MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
58
MODULE_DESCRIPTION("IP tables userspace logging module");
59
 
60
#define ULOG_NL_EVENT           111             /* Harald's favorite number */
61
#define ULOG_MAXNLGROUPS        32              /* numer of nlgroups */
62
 
63
#if 0
64
#define DEBUGP(format, args...) printk(__FILE__ ":" __FUNCTION__ ":" \
65
                                       format, ## args)
66
#else
67
#define DEBUGP(format, args...)
68
#endif
69
 
70
#define PRINTR(format, args...) do { if (net_ratelimit()) printk(format, ## args); } while (0)
71
 
72
static unsigned int nlbufsiz = 4096;
73
MODULE_PARM(nlbufsiz, "i");
74
MODULE_PARM_DESC(nlbufsiz, "netlink buffer size");
75
 
76
static unsigned int flushtimeout = 10 * HZ;
77
MODULE_PARM(flushtimeout, "i");
78
MODULE_PARM_DESC(flushtimeout, "buffer flush timeout");
79
 
80
/* global data structures */
81
 
82
typedef struct {
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 */
87
} ulog_buff_t;
88
 
89
static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS];      /* array of buffers */
90
 
91
static struct sock *nflognl;    /* our socket */
92
static size_t qlen;             /* current length of multipart-nlmsg */
93
DECLARE_LOCK(ulog_lock);        /* spinlock */
94
 
95
/* send one ulog_buff_t to userspace */
96
static void ulog_send(unsigned int nlgroupnum)
97
{
98
        ulog_buff_t *ub = &ulog_buffers[nlgroupnum];
99
 
100
        if (timer_pending(&ub->timer)) {
101
                DEBUGP("ipt_ULOG: ulog_send: timer was pending, deleting\n");
102
                del_timer(&ub->timer);
103
        }
104
 
105
        /* last nlmsg needs NLMSG_DONE */
106
        if (ub->qlen > 1)
107
                ub->lastnlh->nlmsg_type = NLMSG_DONE;
108
 
109
        NETLINK_CB(ub->skb).dst_groups = (1 << nlgroupnum);
110
        DEBUGP("ipt_ULOG: throwing %d packets to netlink mask %u\n",
111
                ub->qlen, nlgroup);
112
        netlink_broadcast(nflognl, ub->skb, 0, (1 << nlgroupnum), GFP_ATOMIC);
113
 
114
        ub->qlen = 0;
115
        ub->skb = NULL;
116
        ub->lastnlh = NULL;
117
 
118
}
119
 
120
 
121
/* timer function to flush queue in ULOG_FLUSH_INTERVAL time */
122
static void ulog_timer(unsigned long data)
123
{
124
        DEBUGP("ipt_ULOG: timer function called, calling ulog_send\n");
125
 
126
        /* lock to protect against somebody modifying our structure
127
         * from ipt_ulog_target at the same time */
128
        LOCK_BH(&ulog_lock);
129
        ulog_send(data);
130
        UNLOCK_BH(&ulog_lock);
131
}
132
 
133
struct sk_buff *ulog_alloc_skb(unsigned int size)
134
{
135
        struct sk_buff *skb;
136
 
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 */
140
 
141
        skb = alloc_skb(nlbufsiz, GFP_ATOMIC);
142
        if (!skb) {
143
                PRINTR("ipt_ULOG: can't alloc whole buffer %ub!\n",
144
                        nlbufsiz);
145
 
146
                /* try to allocate only as much as we need for
147
                 * current packet */
148
 
149
                skb = alloc_skb(size, GFP_ATOMIC);
150
                if (!skb)
151
                        PRINTR("ipt_ULOG: can't even allocate %ub\n", size);
152
        }
153
 
154
        return skb;
155
}
156
 
157
static unsigned int ipt_ulog_target(struct sk_buff **pskb,
158
                                    unsigned int hooknum,
159
                                    const struct net_device *in,
160
                                    const struct net_device *out,
161
                                    const void *targinfo, void *userinfo)
162
{
163
        ulog_buff_t *ub;
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;
168
 
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;
173
 
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;
178
        } else {
179
                copy_len = loginfo->copy_range;
180
        }
181
 
182
        size = NLMSG_SPACE(sizeof(*pm) + copy_len);
183
 
184
        ub = &ulog_buffers[groupnum];
185
 
186
        LOCK_BH(&ulog_lock);
187
 
188
        if (!ub->skb) {
189
                if (!(ub->skb = ulog_alloc_skb(size)))
190
                        goto alloc_failure;
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. */
195
 
196
                ulog_send(groupnum);
197
 
198
                if (!(ub->skb = ulog_alloc_skb(size)))
199
                        goto alloc_failure;
200
        }
201
 
202
        DEBUGP("ipt_ULOG: qlen %d, qthreshold %d\n", ub->qlen,
203
                loginfo->qthreshold);
204
 
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));
208
        ub->qlen++;
209
 
210
        pm = NLMSG_DATA(nlh);
211
 
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;
217
        pm->hook = hooknum;
218
        if (loginfo->prefix[0] != '\0')
219
                strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
220
        else
221
                *(pm->prefix) = '\0';
222
 
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;
228
        } else
229
                pm->mac_len = 0;
230
 
231
        if (in)
232
                strncpy(pm->indev_name, in->name, sizeof(pm->indev_name));
233
        else
234
                pm->indev_name[0] = '\0';
235
 
236
        if (out)
237
                strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
238
        else
239
                pm->outdev_name[0] = '\0';
240
 
241
        if (copy_len)
242
                memcpy(pm->payload, (*pskb)->data, copy_len);
243
 
244
        /* check if we are building multi-part messages */
245
        if (ub->qlen > 1) {
246
                ub->lastnlh->nlmsg_flags |= NLM_F_MULTI;
247
        }
248
 
249
        /* if threshold is reached, send message to userspace */
250
        if (qlen >= loginfo->qthreshold) {
251
                if (loginfo->qthreshold > 1)
252
                        nlh->nlmsg_type = NLMSG_DONE;
253
        }
254
 
255
        ub->lastnlh = nlh;
256
 
257
        /* if timer isn't already running, start it */
258
        if (!timer_pending(&ub->timer)) {
259
                ub->timer.expires = jiffies + flushtimeout;
260
                add_timer(&ub->timer);
261
        }
262
 
263
        UNLOCK_BH(&ulog_lock);
264
 
265
        return IPT_CONTINUE;
266
 
267
 
268
nlmsg_failure:
269
        PRINTR("ipt_ULOG: error during NLMSG_PUT\n");
270
 
271
alloc_failure:
272
        PRINTR("ipt_ULOG: Error building netlink message\n");
273
 
274
        UNLOCK_BH(&ulog_lock);
275
 
276
        return IPT_CONTINUE;
277
}
278
 
279
static int ipt_ulog_checkentry(const char *tablename,
280
                               const struct ipt_entry *e,
281
                               void *targinfo,
282
                               unsigned int targinfosize,
283
                               unsigned int hookmask)
284
{
285
        struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) targinfo;
286
 
287
        if (targinfosize != IPT_ALIGN(sizeof(struct ipt_ulog_info))) {
288
                DEBUGP("ipt_ULOG: targinfosize %u != 0\n", targinfosize);
289
                return 0;
290
        }
291
 
292
        if (loginfo->prefix[sizeof(loginfo->prefix) - 1] != '\0') {
293
                DEBUGP("ipt_ULOG: prefix term %i\n",
294
                       loginfo->prefix[sizeof(loginfo->prefix) - 1]);
295
                return 0;
296
        }
297
 
298
        if (loginfo->qthreshold > ULOG_MAX_QLEN) {
299
                DEBUGP("ipt_ULOG: queue threshold %i > MAX_QLEN\n",
300
                        loginfo->qthreshold);
301
                return 0;
302
        }
303
 
304
        return 1;
305
}
306
 
307
static struct ipt_target ipt_ulog_reg =
308
    { {NULL, NULL}, "ULOG", ipt_ulog_target, ipt_ulog_checkentry, NULL,
309
THIS_MODULE
310
};
311
 
312
static int __init init(void)
313
{
314
        int i;
315
 
316
        DEBUGP("ipt_ULOG: init module\n");
317
 
318
        if (nlbufsiz >= 128*1024) {
319
                printk("Netlink buffer has to be <= 128kB\n");
320
                return -EINVAL;
321
        }
322
 
323
        /* initialize ulog_buffers */
324
        for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
325
                init_timer(&ulog_buffers[i].timer);
326
                ulog_buffers[i].timer.function = ulog_timer;
327
                ulog_buffers[i].timer.data = i;
328
        }
329
 
330
        nflognl = netlink_kernel_create(NETLINK_NFLOG, NULL);
331
        if (!nflognl)
332
                return -ENOMEM;
333
 
334
        if (ipt_register_target(&ipt_ulog_reg) != 0) {
335
                sock_release(nflognl->socket);
336
                return -EINVAL;
337
        }
338
 
339
        return 0;
340
}
341
 
342
static void __exit fini(void)
343
{
344
        ulog_buff_t *ub;
345
        int i;
346
 
347
        DEBUGP("ipt_ULOG: cleanup_module\n");
348
 
349
        ipt_unregister_target(&ipt_ulog_reg);
350
        sock_release(nflognl->socket);
351
 
352
        /* remove pending timers and free allocated skb's */
353
        for (i = 0; i < ULOG_MAXNLGROUPS; i++) {
354
                ub = &ulog_buffers[i];
355
                if (timer_pending(&ub->timer)) {
356
                        DEBUGP("timer was pending, deleting\n");
357
                        del_timer(&ub->timer);
358
                }
359
 
360
                if (ub->skb) {
361
                        kfree_skb(ub->skb);
362
                        ub->skb = NULL;
363
                }
364
        }
365
 
366
}
367
 
368
module_init(init);
369
module_exit(fini);

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.