blob: 8e4001b8f76469ec454a9f5e3e3d86dce9c68ff1 [file] [log] [blame]
Harald Welte0597f262005-08-09 19:58:39 -07001/*
2 * This is a module which is used for logging packets to userspace via
3 * nfetlink.
4 *
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
6 *
7 * Based on the old ipv4-only ipt_ULOG.c:
8 * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Harald Welte0597f262005-08-09 19:58:39 -070013 */
14#include <linux/module.h>
15#include <linux/skbuff.h>
16#include <linux/init.h>
17#include <linux/ip.h>
18#include <linux/ipv6.h>
19#include <linux/netdevice.h>
20#include <linux/netfilter.h>
21#include <linux/netlink.h>
22#include <linux/netfilter/nfnetlink.h>
23#include <linux/netfilter/nfnetlink_log.h>
24#include <linux/spinlock.h>
25#include <linux/sysctl.h>
26#include <linux/proc_fs.h>
27#include <linux/security.h>
28#include <linux/list.h>
29#include <linux/jhash.h>
30#include <linux/random.h>
31#include <net/sock.h>
32
33#include <asm/atomic.h>
34
Harald Weltefbcd9232005-08-09 20:22:10 -070035#ifdef CONFIG_BRIDGE_NETFILTER
36#include "../bridge/br_private.h"
37#endif
38
Holger Eitzenbergerc2db2922006-02-04 02:13:14 -080039#define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
Harald Welte0597f262005-08-09 19:58:39 -070040#define NFULNL_TIMEOUT_DEFAULT 100 /* every second */
41#define NFULNL_QTHRESH_DEFAULT 100 /* 100 packets */
42
43#define PRINTR(x, args...) do { if (net_ratelimit()) \
44 printk(x, ## args); } while (0);
45
46#if 0
47#define UDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
48 __FILE__, __LINE__, __FUNCTION__, \
49 ## args)
50#else
51#define UDEBUG(x, ...)
52#endif
53
54struct nfulnl_instance {
55 struct hlist_node hlist; /* global list of instances */
56 spinlock_t lock;
57 atomic_t use; /* use count */
58
59 unsigned int qlen; /* number of nlmsgs in skb */
60 struct sk_buff *skb; /* pre-allocatd skb */
Harald Welte0597f262005-08-09 19:58:39 -070061 struct timer_list timer;
62 int peer_pid; /* PID of the peer process */
63
64 /* configurable parameters */
65 unsigned int flushtimeout; /* timeout until queue flush */
66 unsigned int nlbufsiz; /* netlink buffer allocation size */
67 unsigned int qthreshold; /* threshold of the queue */
68 u_int32_t copy_range;
Harald Welte0af5f6c2006-03-20 17:15:11 -080069 u_int32_t seq; /* instance-local sequential counter */
Harald Welte0597f262005-08-09 19:58:39 -070070 u_int16_t group_num; /* number of this queue */
Harald Welte0af5f6c2006-03-20 17:15:11 -080071 u_int16_t flags;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080072 u_int8_t copy_mode;
Harald Welte0597f262005-08-09 19:58:39 -070073};
74
75static DEFINE_RWLOCK(instances_lock);
Harald Welte0af5f6c2006-03-20 17:15:11 -080076static atomic_t global_seq;
Harald Welte0597f262005-08-09 19:58:39 -070077
78#define INSTANCE_BUCKETS 16
79static struct hlist_head instance_table[INSTANCE_BUCKETS];
80static unsigned int hash_init;
81
82static inline u_int8_t instance_hashfn(u_int16_t group_num)
83{
84 return ((group_num & 0xff) % INSTANCE_BUCKETS);
85}
86
87static struct nfulnl_instance *
88__instance_lookup(u_int16_t group_num)
89{
90 struct hlist_head *head;
91 struct hlist_node *pos;
92 struct nfulnl_instance *inst;
93
94 UDEBUG("entering (group_num=%u)\n", group_num);
95
96 head = &instance_table[instance_hashfn(group_num)];
97 hlist_for_each_entry(inst, pos, head, hlist) {
98 if (inst->group_num == group_num)
99 return inst;
100 }
101 return NULL;
102}
103
104static inline void
105instance_get(struct nfulnl_instance *inst)
106{
107 atomic_inc(&inst->use);
108}
109
110static struct nfulnl_instance *
111instance_lookup_get(u_int16_t group_num)
112{
113 struct nfulnl_instance *inst;
114
115 read_lock_bh(&instances_lock);
116 inst = __instance_lookup(group_num);
117 if (inst)
118 instance_get(inst);
119 read_unlock_bh(&instances_lock);
120
121 return inst;
122}
123
124static void
125instance_put(struct nfulnl_instance *inst)
126{
127 if (inst && atomic_dec_and_test(&inst->use)) {
128 UDEBUG("kfree(inst=%p)\n", inst);
129 kfree(inst);
Patrick McHardy7d90e862007-03-04 15:59:45 -0800130 module_put(THIS_MODULE);
Harald Welte0597f262005-08-09 19:58:39 -0700131 }
132}
133
134static void nfulnl_timer(unsigned long data);
135
136static struct nfulnl_instance *
137instance_create(u_int16_t group_num, int pid)
138{
139 struct nfulnl_instance *inst;
140
141 UDEBUG("entering (group_num=%u, pid=%d)\n", group_num,
142 pid);
143
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800144 write_lock_bh(&instances_lock);
Harald Welte0597f262005-08-09 19:58:39 -0700145 if (__instance_lookup(group_num)) {
146 inst = NULL;
147 UDEBUG("aborting, instance already exists\n");
148 goto out_unlock;
149 }
150
Harald Welte10dfdc62005-11-03 19:20:07 +0100151 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Harald Welte0597f262005-08-09 19:58:39 -0700152 if (!inst)
153 goto out_unlock;
154
Harald Welte0597f262005-08-09 19:58:39 -0700155 INIT_HLIST_NODE(&inst->hlist);
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800156 spin_lock_init(&inst->lock);
Harald Welte0597f262005-08-09 19:58:39 -0700157 /* needs to be two, since we _put() after creation */
158 atomic_set(&inst->use, 2);
159
Patrick McHardye6f689d2007-03-23 11:16:30 -0700160 setup_timer(&inst->timer, nfulnl_timer, (unsigned long)inst);
Harald Welte0597f262005-08-09 19:58:39 -0700161
162 inst->peer_pid = pid;
163 inst->group_num = group_num;
164
165 inst->qthreshold = NFULNL_QTHRESH_DEFAULT;
166 inst->flushtimeout = NFULNL_TIMEOUT_DEFAULT;
167 inst->nlbufsiz = NFULNL_NLBUFSIZ_DEFAULT;
168 inst->copy_mode = NFULNL_COPY_PACKET;
169 inst->copy_range = 0xffff;
170
171 if (!try_module_get(THIS_MODULE))
172 goto out_free;
173
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800174 hlist_add_head(&inst->hlist,
Harald Welte0597f262005-08-09 19:58:39 -0700175 &instance_table[instance_hashfn(group_num)]);
176
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800177 UDEBUG("newly added node: %p, next=%p\n", &inst->hlist,
Harald Welte0597f262005-08-09 19:58:39 -0700178 inst->hlist.next);
179
180 write_unlock_bh(&instances_lock);
181
182 return inst;
183
184out_free:
185 instance_put(inst);
186out_unlock:
187 write_unlock_bh(&instances_lock);
188 return NULL;
189}
190
191static int __nfulnl_send(struct nfulnl_instance *inst);
192
193static void
Patrick McHardy9afdb002007-03-23 11:12:50 -0700194__instance_destroy(struct nfulnl_instance *inst)
Harald Welte0597f262005-08-09 19:58:39 -0700195{
196 /* first pull it out of the global list */
Harald Welte0597f262005-08-09 19:58:39 -0700197 UDEBUG("removing instance %p (queuenum=%u) from hash\n",
198 inst, inst->group_num);
199
200 hlist_del(&inst->hlist);
201
Harald Welte0597f262005-08-09 19:58:39 -0700202 /* then flush all pending packets from skb */
203
204 spin_lock_bh(&inst->lock);
205 if (inst->skb) {
Michal Miroslawb4d62022007-03-04 16:00:04 -0800206 /* timer "holds" one reference (we have one more) */
207 if (del_timer(&inst->timer))
208 instance_put(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700209 if (inst->qlen)
210 __nfulnl_send(inst);
211 if (inst->skb) {
212 kfree_skb(inst->skb);
213 inst->skb = NULL;
214 }
215 }
216 spin_unlock_bh(&inst->lock);
217
218 /* and finally put the refcount */
219 instance_put(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700220}
221
222static inline void
Harald Welte0597f262005-08-09 19:58:39 -0700223instance_destroy(struct nfulnl_instance *inst)
224{
Patrick McHardy9afdb002007-03-23 11:12:50 -0700225 write_lock_bh(&instances_lock);
226 __instance_destroy(inst);
227 write_unlock_bh(&instances_lock);
Harald Welte0597f262005-08-09 19:58:39 -0700228}
229
230static int
231nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
232 unsigned int range)
233{
234 int status = 0;
235
236 spin_lock_bh(&inst->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800237
Harald Welte0597f262005-08-09 19:58:39 -0700238 switch (mode) {
239 case NFULNL_COPY_NONE:
240 case NFULNL_COPY_META:
241 inst->copy_mode = mode;
242 inst->copy_range = 0;
243 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800244
Harald Welte0597f262005-08-09 19:58:39 -0700245 case NFULNL_COPY_PACKET:
246 inst->copy_mode = mode;
247 /* we're using struct nfattr which has 16bit nfa_len */
248 if (range > 0xffff)
249 inst->copy_range = 0xffff;
250 else
251 inst->copy_range = range;
252 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800253
Harald Welte0597f262005-08-09 19:58:39 -0700254 default:
255 status = -EINVAL;
256 break;
257 }
258
259 spin_unlock_bh(&inst->lock);
260
261 return status;
262}
263
264static int
265nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
266{
267 int status;
268
269 spin_lock_bh(&inst->lock);
270 if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
271 status = -ERANGE;
272 else if (nlbufsiz > 131072)
273 status = -ERANGE;
274 else {
275 inst->nlbufsiz = nlbufsiz;
276 status = 0;
277 }
278 spin_unlock_bh(&inst->lock);
279
280 return status;
281}
282
283static int
284nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
285{
286 spin_lock_bh(&inst->lock);
287 inst->flushtimeout = timeout;
288 spin_unlock_bh(&inst->lock);
289
290 return 0;
291}
292
293static int
294nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
295{
296 spin_lock_bh(&inst->lock);
297 inst->qthreshold = qthresh;
298 spin_unlock_bh(&inst->lock);
299
300 return 0;
301}
302
Harald Welte0af5f6c2006-03-20 17:15:11 -0800303static int
304nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
305{
306 spin_lock_bh(&inst->lock);
Patrick McHardyee433532006-05-19 02:17:18 -0700307 inst->flags = flags;
Harald Welte0af5f6c2006-03-20 17:15:11 -0800308 spin_unlock_bh(&inst->lock);
309
310 return 0;
311}
312
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800313static struct sk_buff *nfulnl_alloc_skb(unsigned int inst_size,
Harald Welte0597f262005-08-09 19:58:39 -0700314 unsigned int pkt_size)
315{
316 struct sk_buff *skb;
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800317 unsigned int n;
Harald Welte0597f262005-08-09 19:58:39 -0700318
319 UDEBUG("entered (%u, %u)\n", inst_size, pkt_size);
320
321 /* alloc skb which should be big enough for a whole multipart
322 * message. WARNING: has to be <= 128k due to slab restrictions */
323
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800324 n = max(inst_size, pkt_size);
325 skb = alloc_skb(n, GFP_ATOMIC);
Harald Welte0597f262005-08-09 19:58:39 -0700326 if (!skb) {
327 PRINTR("nfnetlink_log: can't alloc whole buffer (%u bytes)\n",
328 inst_size);
329
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800330 if (n > pkt_size) {
331 /* try to allocate only as much as we need for current
332 * packet */
Harald Welte0597f262005-08-09 19:58:39 -0700333
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800334 skb = alloc_skb(pkt_size, GFP_ATOMIC);
335 if (!skb)
336 PRINTR("nfnetlink_log: can't even alloc %u "
337 "bytes\n", pkt_size);
338 }
Harald Welte0597f262005-08-09 19:58:39 -0700339 }
340
341 return skb;
342}
343
344static int
345__nfulnl_send(struct nfulnl_instance *inst)
346{
Eric Leblond29c5d4a2007-09-18 13:07:15 -0700347 int status = -1;
Harald Welte0597f262005-08-09 19:58:39 -0700348
Harald Welte0597f262005-08-09 19:58:39 -0700349 if (inst->qlen > 1)
Eric Leblond29c5d4a2007-09-18 13:07:15 -0700350 NLMSG_PUT(inst->skb, 0, 0,
351 NLMSG_DONE,
352 sizeof(struct nfgenmsg));
Harald Welte0597f262005-08-09 19:58:39 -0700353
354 status = nfnetlink_unicast(inst->skb, inst->peer_pid, MSG_DONTWAIT);
355 if (status < 0) {
356 UDEBUG("netlink_unicast() failed\n");
357 /* FIXME: statistics */
358 }
359
360 inst->qlen = 0;
361 inst->skb = NULL;
Harald Welte0597f262005-08-09 19:58:39 -0700362
Eric Leblond29c5d4a2007-09-18 13:07:15 -0700363nlmsg_failure:
Harald Welte0597f262005-08-09 19:58:39 -0700364 return status;
365}
366
367static void nfulnl_timer(unsigned long data)
368{
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800369 struct nfulnl_instance *inst = (struct nfulnl_instance *)data;
Harald Welte0597f262005-08-09 19:58:39 -0700370
371 UDEBUG("timer function called, flushing buffer\n");
372
373 spin_lock_bh(&inst->lock);
Michal Miroslaw370e6a82007-03-23 11:12:21 -0700374 if (inst->skb)
375 __nfulnl_send(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700376 spin_unlock_bh(&inst->lock);
Michal Miroslaw05f7b7b2007-03-04 15:58:40 -0800377 instance_put(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700378}
379
Harald Welte0af5f6c2006-03-20 17:15:11 -0800380/* This is an inline function, we don't really care about a long
381 * list of arguments */
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800382static inline int
Harald Welte0597f262005-08-09 19:58:39 -0700383__build_packet_message(struct nfulnl_instance *inst,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800384 const struct sk_buff *skb,
Harald Welte0597f262005-08-09 19:58:39 -0700385 unsigned int data_len,
386 unsigned int pf,
387 unsigned int hooknum,
388 const struct net_device *indev,
389 const struct net_device *outdev,
390 const struct nf_loginfo *li,
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100391 const char *prefix, unsigned int plen)
Harald Welte0597f262005-08-09 19:58:39 -0700392{
Harald Welte0597f262005-08-09 19:58:39 -0700393 struct nfulnl_msg_packet_hdr pmsg;
394 struct nlmsghdr *nlh;
395 struct nfgenmsg *nfmsg;
Al Viro98a4a862006-11-08 00:26:51 -0800396 __be32 tmp_uint;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700397 sk_buff_data_t old_tail = inst->skb->tail;
Harald Welte0597f262005-08-09 19:58:39 -0700398
399 UDEBUG("entered\n");
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800400
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800401 nlh = NLMSG_PUT(inst->skb, 0, 0,
Harald Welte0597f262005-08-09 19:58:39 -0700402 NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET,
403 sizeof(struct nfgenmsg));
404 nfmsg = NLMSG_DATA(nlh);
405 nfmsg->nfgen_family = pf;
406 nfmsg->version = NFNETLINK_V0;
407 nfmsg->res_id = htons(inst->group_num);
408
Al Virofebf0a42006-11-03 00:59:17 -0800409 pmsg.hw_protocol = skb->protocol;
Harald Welte0597f262005-08-09 19:58:39 -0700410 pmsg.hook = hooknum;
411
412 NFA_PUT(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg);
413
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100414 if (prefix)
415 NFA_PUT(inst->skb, NFULA_PREFIX, plen, prefix);
Harald Welte0597f262005-08-09 19:58:39 -0700416
417 if (indev) {
418 tmp_uint = htonl(indev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700419#ifndef CONFIG_BRIDGE_NETFILTER
Harald Welte0597f262005-08-09 19:58:39 -0700420 NFA_PUT(inst->skb, NFULA_IFINDEX_INDEV, sizeof(tmp_uint),
421 &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700422#else
423 if (pf == PF_BRIDGE) {
424 /* Case 1: outdev is physical input device, we need to
425 * look for bridge group (when called from
426 * netfilter_bridge) */
427 NFA_PUT(inst->skb, NFULA_IFINDEX_PHYSINDEV,
428 sizeof(tmp_uint), &tmp_uint);
429 /* this is the bridge group "brX" */
430 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
431 NFA_PUT(inst->skb, NFULA_IFINDEX_INDEV,
432 sizeof(tmp_uint), &tmp_uint);
433 } else {
434 /* Case 2: indev is bridge group, we need to look for
435 * physical device (when called from ipv4) */
436 NFA_PUT(inst->skb, NFULA_IFINDEX_INDEV,
437 sizeof(tmp_uint), &tmp_uint);
438 if (skb->nf_bridge && skb->nf_bridge->physindev) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800439 tmp_uint =
Harald Weltefbcd9232005-08-09 20:22:10 -0700440 htonl(skb->nf_bridge->physindev->ifindex);
441 NFA_PUT(inst->skb, NFULA_IFINDEX_PHYSINDEV,
442 sizeof(tmp_uint), &tmp_uint);
443 }
444 }
445#endif
Harald Welte0597f262005-08-09 19:58:39 -0700446 }
447
448 if (outdev) {
449 tmp_uint = htonl(outdev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700450#ifndef CONFIG_BRIDGE_NETFILTER
Harald Welte0597f262005-08-09 19:58:39 -0700451 NFA_PUT(inst->skb, NFULA_IFINDEX_OUTDEV, sizeof(tmp_uint),
452 &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700453#else
454 if (pf == PF_BRIDGE) {
455 /* Case 1: outdev is physical output device, we need to
456 * look for bridge group (when called from
457 * netfilter_bridge) */
458 NFA_PUT(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
459 sizeof(tmp_uint), &tmp_uint);
460 /* this is the bridge group "brX" */
461 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
462 NFA_PUT(inst->skb, NFULA_IFINDEX_OUTDEV,
463 sizeof(tmp_uint), &tmp_uint);
464 } else {
465 /* Case 2: indev is a bridge group, we need to look
466 * for physical device (when called from ipv4) */
467 NFA_PUT(inst->skb, NFULA_IFINDEX_OUTDEV,
468 sizeof(tmp_uint), &tmp_uint);
Patrick McHardyba5dcee2007-03-06 20:24:53 -0800469 if (skb->nf_bridge && skb->nf_bridge->physoutdev) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800470 tmp_uint =
Harald Weltefbcd9232005-08-09 20:22:10 -0700471 htonl(skb->nf_bridge->physoutdev->ifindex);
472 NFA_PUT(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
473 sizeof(tmp_uint), &tmp_uint);
474 }
475 }
476#endif
Harald Welte0597f262005-08-09 19:58:39 -0700477 }
478
Thomas Graf82e91ff2006-11-09 15:19:14 -0800479 if (skb->mark) {
480 tmp_uint = htonl(skb->mark);
Harald Welte0597f262005-08-09 19:58:39 -0700481 NFA_PUT(inst->skb, NFULA_MARK, sizeof(tmp_uint), &tmp_uint);
482 }
483
484 if (indev && skb->dev && skb->dev->hard_header_parse) {
485 struct nfulnl_msg_packet_hw phw;
Al Viro98a4a862006-11-08 00:26:51 -0800486 int len = skb->dev->hard_header_parse((struct sk_buff *)skb,
Harald Welte0597f262005-08-09 19:58:39 -0700487 phw.hw_addr);
Al Viro98a4a862006-11-08 00:26:51 -0800488 phw.hw_addrlen = htons(len);
Harald Welte0597f262005-08-09 19:58:39 -0700489 NFA_PUT(inst->skb, NFULA_HWADDR, sizeof(phw), &phw);
490 }
491
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700492 if (skb->tstamp.tv64) {
Harald Welte0597f262005-08-09 19:58:39 -0700493 struct nfulnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700494 struct timeval tv = ktime_to_timeval(skb->tstamp);
495 ts.sec = cpu_to_be64(tv.tv_sec);
496 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte0597f262005-08-09 19:58:39 -0700497
498 NFA_PUT(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts);
499 }
500
501 /* UID */
502 if (skb->sk) {
503 read_lock_bh(&skb->sk->sk_callback_lock);
504 if (skb->sk->sk_socket && skb->sk->sk_socket->file) {
Al Viro98a4a862006-11-08 00:26:51 -0800505 __be32 uid = htonl(skb->sk->sk_socket->file->f_uid);
Harald Welte0597f262005-08-09 19:58:39 -0700506 /* need to unlock here since NFA_PUT may goto */
507 read_unlock_bh(&skb->sk->sk_callback_lock);
508 NFA_PUT(inst->skb, NFULA_UID, sizeof(uid), &uid);
509 } else
510 read_unlock_bh(&skb->sk->sk_callback_lock);
511 }
512
Harald Welte0af5f6c2006-03-20 17:15:11 -0800513 /* local sequence number */
514 if (inst->flags & NFULNL_CFG_F_SEQ) {
515 tmp_uint = htonl(inst->seq++);
516 NFA_PUT(inst->skb, NFULA_SEQ, sizeof(tmp_uint), &tmp_uint);
517 }
518 /* global sequence number */
519 if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) {
Patrick McHardy7fdeaf62006-11-14 19:47:09 -0800520 tmp_uint = htonl(atomic_inc_return(&global_seq));
Harald Welte0af5f6c2006-03-20 17:15:11 -0800521 NFA_PUT(inst->skb, NFULA_SEQ_GLOBAL, sizeof(tmp_uint), &tmp_uint);
522 }
523
Harald Welte0597f262005-08-09 19:58:39 -0700524 if (data_len) {
525 struct nfattr *nfa;
526 int size = NFA_LENGTH(data_len);
527
528 if (skb_tailroom(inst->skb) < (int)NFA_SPACE(data_len)) {
529 printk(KERN_WARNING "nfnetlink_log: no tailroom!\n");
530 goto nlmsg_failure;
531 }
532
533 nfa = (struct nfattr *)skb_put(inst->skb, NFA_ALIGN(size));
534 nfa->nfa_type = NFULA_PAYLOAD;
535 nfa->nfa_len = size;
536
537 if (skb_copy_bits(skb, 0, NFA_DATA(nfa), data_len))
538 BUG();
539 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800540
Harald Welte0597f262005-08-09 19:58:39 -0700541 nlh->nlmsg_len = inst->skb->tail - old_tail;
542 return 0;
543
544nlmsg_failure:
545 UDEBUG("nlmsg_failure\n");
546nfattr_failure:
547 PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
548 return -1;
549}
550
551#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
552
553static struct nf_loginfo default_loginfo = {
554 .type = NF_LOG_TYPE_ULOG,
555 .u = {
556 .ulog = {
557 .copy_len = 0xffff,
558 .group = 0,
559 .qthreshold = 1,
560 },
561 },
562};
563
564/* log handler for internal netfilter logging api */
565static void
566nfulnl_log_packet(unsigned int pf,
567 unsigned int hooknum,
568 const struct sk_buff *skb,
569 const struct net_device *in,
570 const struct net_device *out,
571 const struct nf_loginfo *li_user,
572 const char *prefix)
573{
574 unsigned int size, data_len;
575 struct nfulnl_instance *inst;
576 const struct nf_loginfo *li;
577 unsigned int qthreshold;
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100578 unsigned int plen;
Harald Welte0597f262005-08-09 19:58:39 -0700579
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800580 if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
Harald Welte0597f262005-08-09 19:58:39 -0700581 li = li_user;
582 else
583 li = &default_loginfo;
584
585 inst = instance_lookup_get(li->u.ulog.group);
586 if (!inst)
Harald Welte0597f262005-08-09 19:58:39 -0700587 return;
Harald Welte0597f262005-08-09 19:58:39 -0700588
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100589 plen = 0;
590 if (prefix)
Patrick McHardy881dbfe2007-03-06 20:24:35 -0800591 plen = strlen(prefix) + 1;
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100592
Harald Welte0597f262005-08-09 19:58:39 -0700593 /* all macros expand to constant values at compile time */
594 /* FIXME: do we want to make the size calculation conditional based on
595 * what is actually present? way more branches and checks, but more
596 * memory efficient... */
597 size = NLMSG_SPACE(sizeof(struct nfgenmsg))
598 + NFA_SPACE(sizeof(struct nfulnl_msg_packet_hdr))
599 + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
600 + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700601#ifdef CONFIG_BRIDGE_NETFILTER
602 + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
603 + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
604#endif
Harald Welte0597f262005-08-09 19:58:39 -0700605 + NFA_SPACE(sizeof(u_int32_t)) /* mark */
606 + NFA_SPACE(sizeof(u_int32_t)) /* uid */
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100607 + NFA_SPACE(plen) /* prefix */
Harald Welte0597f262005-08-09 19:58:39 -0700608 + NFA_SPACE(sizeof(struct nfulnl_msg_packet_hw))
609 + NFA_SPACE(sizeof(struct nfulnl_msg_packet_timestamp));
610
611 UDEBUG("initial size=%u\n", size);
612
613 spin_lock_bh(&inst->lock);
614
Harald Welte0af5f6c2006-03-20 17:15:11 -0800615 if (inst->flags & NFULNL_CFG_F_SEQ)
616 size += NFA_SPACE(sizeof(u_int32_t));
617 if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
618 size += NFA_SPACE(sizeof(u_int32_t));
619
Harald Welte0597f262005-08-09 19:58:39 -0700620 qthreshold = inst->qthreshold;
621 /* per-rule qthreshold overrides per-instance */
622 if (qthreshold > li->u.ulog.qthreshold)
623 qthreshold = li->u.ulog.qthreshold;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800624
Harald Welte0597f262005-08-09 19:58:39 -0700625 switch (inst->copy_mode) {
626 case NFULNL_COPY_META:
627 case NFULNL_COPY_NONE:
628 data_len = 0;
629 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800630
Harald Welte0597f262005-08-09 19:58:39 -0700631 case NFULNL_COPY_PACKET:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800632 if (inst->copy_range == 0
Harald Welte0597f262005-08-09 19:58:39 -0700633 || inst->copy_range > skb->len)
634 data_len = skb->len;
635 else
636 data_len = inst->copy_range;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800637
Harald Welte0597f262005-08-09 19:58:39 -0700638 size += NFA_SPACE(data_len);
639 UDEBUG("copy_packet, therefore size now %u\n", size);
640 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800641
Harald Welte0597f262005-08-09 19:58:39 -0700642 default:
Michal Miroslaw55b5a912007-03-23 11:11:05 -0700643 goto unlock_and_release;
Harald Welte0597f262005-08-09 19:58:39 -0700644 }
645
Michal Miroslaw55b5a912007-03-23 11:11:05 -0700646 if (inst->qlen >= qthreshold ||
Eric Leblond29c5d4a2007-09-18 13:07:15 -0700647 (inst->skb && size >
648 skb_tailroom(inst->skb) - sizeof(struct nfgenmsg))) {
Harald Welte0597f262005-08-09 19:58:39 -0700649 /* either the queue len is too high or we don't have
650 * enough room in the skb left. flush to userspace. */
651 UDEBUG("flushing old skb\n");
652
Michal Miroslawb4d62022007-03-04 16:00:04 -0800653 /* timer "holds" one reference (we have another one) */
654 if (del_timer(&inst->timer))
655 instance_put(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700656 __nfulnl_send(inst);
Michal Miroslaw55b5a912007-03-23 11:11:05 -0700657 }
Harald Welte0597f262005-08-09 19:58:39 -0700658
Michal Miroslaw55b5a912007-03-23 11:11:05 -0700659 if (!inst->skb) {
660 inst->skb = nfulnl_alloc_skb(inst->nlbufsiz, size);
661 if (!inst->skb)
Harald Welte0597f262005-08-09 19:58:39 -0700662 goto alloc_failure;
Harald Welte0597f262005-08-09 19:58:39 -0700663 }
664
665 UDEBUG("qlen %d, qthreshold %d\n", inst->qlen, qthreshold);
666 inst->qlen++;
667
668 __build_packet_message(inst, skb, data_len, pf,
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100669 hooknum, in, out, li, prefix, plen);
Harald Welte0597f262005-08-09 19:58:39 -0700670
671 /* timer_pending always called within inst->lock, so there
672 * is no chance of a race here */
673 if (!timer_pending(&inst->timer)) {
674 instance_get(inst);
675 inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
676 add_timer(&inst->timer);
677 }
Harald Welte0597f262005-08-09 19:58:39 -0700678
Michal Miroslawed32abe2007-03-04 15:58:15 -0800679unlock_and_release:
680 spin_unlock_bh(&inst->lock);
681 instance_put(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700682 return;
683
684alloc_failure:
Harald Welte0597f262005-08-09 19:58:39 -0700685 UDEBUG("error allocating skb\n");
686 /* FIXME: statistics */
Michal Miroslawed32abe2007-03-04 15:58:15 -0800687 goto unlock_and_release;
Harald Welte0597f262005-08-09 19:58:39 -0700688}
689
690static int
691nfulnl_rcv_nl_event(struct notifier_block *this,
692 unsigned long event, void *ptr)
693{
694 struct netlink_notify *n = ptr;
695
696 if (event == NETLINK_URELEASE &&
697 n->protocol == NETLINK_NETFILTER && n->pid) {
698 int i;
699
700 /* destroy all instances for this pid */
701 write_lock_bh(&instances_lock);
702 for (i = 0; i < INSTANCE_BUCKETS; i++) {
703 struct hlist_node *tmp, *t2;
704 struct nfulnl_instance *inst;
705 struct hlist_head *head = &instance_table[i];
706
707 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
708 UDEBUG("node = %p\n", inst);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200709 if ((n->net == &init_net) &&
710 (n->pid == inst->peer_pid))
Harald Welte0597f262005-08-09 19:58:39 -0700711 __instance_destroy(inst);
712 }
713 }
714 write_unlock_bh(&instances_lock);
715 }
716 return NOTIFY_DONE;
717}
718
719static struct notifier_block nfulnl_rtnl_notifier = {
720 .notifier_call = nfulnl_rcv_nl_event,
721};
722
723static int
724nfulnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700725 struct nlmsghdr *nlh, struct nfattr *nfqa[])
Harald Welte0597f262005-08-09 19:58:39 -0700726{
727 return -ENOTSUPP;
728}
729
730static struct nf_logger nfulnl_logger = {
731 .name = "nfnetlink_log",
732 .logfn = &nfulnl_log_packet,
733 .me = THIS_MODULE,
734};
735
736static const int nfula_min[NFULA_MAX] = {
737 [NFULA_PACKET_HDR-1] = sizeof(struct nfulnl_msg_packet_hdr),
738 [NFULA_MARK-1] = sizeof(u_int32_t),
739 [NFULA_TIMESTAMP-1] = sizeof(struct nfulnl_msg_packet_timestamp),
740 [NFULA_IFINDEX_INDEV-1] = sizeof(u_int32_t),
741 [NFULA_IFINDEX_OUTDEV-1]= sizeof(u_int32_t),
Harald Welte0af5f6c2006-03-20 17:15:11 -0800742 [NFULA_IFINDEX_PHYSINDEV-1] = sizeof(u_int32_t),
743 [NFULA_IFINDEX_PHYSOUTDEV-1] = sizeof(u_int32_t),
Harald Welte0597f262005-08-09 19:58:39 -0700744 [NFULA_HWADDR-1] = sizeof(struct nfulnl_msg_packet_hw),
745 [NFULA_PAYLOAD-1] = 0,
746 [NFULA_PREFIX-1] = 0,
747 [NFULA_UID-1] = sizeof(u_int32_t),
Harald Welte0af5f6c2006-03-20 17:15:11 -0800748 [NFULA_SEQ-1] = sizeof(u_int32_t),
749 [NFULA_SEQ_GLOBAL-1] = sizeof(u_int32_t),
Harald Welte0597f262005-08-09 19:58:39 -0700750};
751
752static const int nfula_cfg_min[NFULA_CFG_MAX] = {
753 [NFULA_CFG_CMD-1] = sizeof(struct nfulnl_msg_config_cmd),
754 [NFULA_CFG_MODE-1] = sizeof(struct nfulnl_msg_config_mode),
755 [NFULA_CFG_TIMEOUT-1] = sizeof(u_int32_t),
756 [NFULA_CFG_QTHRESH-1] = sizeof(u_int32_t),
757 [NFULA_CFG_NLBUFSIZ-1] = sizeof(u_int32_t),
Harald Welte0af5f6c2006-03-20 17:15:11 -0800758 [NFULA_CFG_FLAGS-1] = sizeof(u_int16_t),
Harald Welte0597f262005-08-09 19:58:39 -0700759};
760
761static int
762nfulnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700763 struct nlmsghdr *nlh, struct nfattr *nfula[])
Harald Welte0597f262005-08-09 19:58:39 -0700764{
765 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
766 u_int16_t group_num = ntohs(nfmsg->res_id);
767 struct nfulnl_instance *inst;
768 int ret = 0;
769
770 UDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
771
772 if (nfattr_bad_size(nfula, NFULA_CFG_MAX, nfula_cfg_min)) {
773 UDEBUG("bad attribute size\n");
774 return -EINVAL;
775 }
776
777 inst = instance_lookup_get(group_num);
778 if (nfula[NFULA_CFG_CMD-1]) {
779 u_int8_t pf = nfmsg->nfgen_family;
780 struct nfulnl_msg_config_cmd *cmd;
781 cmd = NFA_DATA(nfula[NFULA_CFG_CMD-1]);
782 UDEBUG("found CFG_CMD for\n");
783
784 switch (cmd->command) {
785 case NFULNL_CFG_CMD_BIND:
786 if (inst) {
787 ret = -EBUSY;
788 goto out_put;
789 }
790
791 inst = instance_create(group_num,
792 NETLINK_CB(skb).pid);
793 if (!inst) {
794 ret = -EINVAL;
Michal Miroslawf414c162007-03-23 11:11:31 -0700795 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700796 }
797 break;
798 case NFULNL_CFG_CMD_UNBIND:
799 if (!inst) {
800 ret = -ENODEV;
Michal Miroslawf414c162007-03-23 11:11:31 -0700801 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700802 }
803
804 if (inst->peer_pid != NETLINK_CB(skb).pid) {
805 ret = -EPERM;
806 goto out_put;
807 }
808
809 instance_destroy(inst);
Michal Miroslaw9a36e8c2007-03-23 11:11:48 -0700810 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700811 case NFULNL_CFG_CMD_PF_BIND:
812 UDEBUG("registering log handler for pf=%u\n", pf);
813 ret = nf_log_register(pf, &nfulnl_logger);
814 break;
815 case NFULNL_CFG_CMD_PF_UNBIND:
816 UDEBUG("unregistering log handler for pf=%u\n", pf);
817 /* This is a bug and a feature. We cannot unregister
818 * other handlers, like nfnetlink_inst can */
819 nf_log_unregister_pf(pf);
820 break;
821 default:
822 ret = -EINVAL;
823 break;
824 }
Michal Miroslawdd167042007-03-04 15:59:20 -0800825
826 if (!inst)
827 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700828 } else {
829 if (!inst) {
830 UDEBUG("no config command, and no instance for "
831 "group=%u pid=%u =>ENOENT\n",
832 group_num, NETLINK_CB(skb).pid);
833 ret = -ENOENT;
Michal Miroslawf414c162007-03-23 11:11:31 -0700834 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700835 }
836
837 if (inst->peer_pid != NETLINK_CB(skb).pid) {
838 UDEBUG("no config command, and wrong pid\n");
839 ret = -EPERM;
840 goto out_put;
841 }
842 }
843
844 if (nfula[NFULA_CFG_MODE-1]) {
845 struct nfulnl_msg_config_mode *params;
846 params = NFA_DATA(nfula[NFULA_CFG_MODE-1]);
847
848 nfulnl_set_mode(inst, params->copy_mode,
Al Virod1208b92006-11-03 00:58:41 -0800849 ntohl(params->copy_range));
Harald Welte0597f262005-08-09 19:58:39 -0700850 }
851
852 if (nfula[NFULA_CFG_TIMEOUT-1]) {
Al Viro98a4a862006-11-08 00:26:51 -0800853 __be32 timeout =
854 *(__be32 *)NFA_DATA(nfula[NFULA_CFG_TIMEOUT-1]);
Harald Welte0597f262005-08-09 19:58:39 -0700855
856 nfulnl_set_timeout(inst, ntohl(timeout));
857 }
858
859 if (nfula[NFULA_CFG_NLBUFSIZ-1]) {
Al Viro98a4a862006-11-08 00:26:51 -0800860 __be32 nlbufsiz =
861 *(__be32 *)NFA_DATA(nfula[NFULA_CFG_NLBUFSIZ-1]);
Harald Welte0597f262005-08-09 19:58:39 -0700862
863 nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
864 }
865
866 if (nfula[NFULA_CFG_QTHRESH-1]) {
Al Viro7ac00a22006-11-03 00:58:17 -0800867 __be32 qthresh =
868 *(__be32 *)NFA_DATA(nfula[NFULA_CFG_QTHRESH-1]);
Harald Welte0597f262005-08-09 19:58:39 -0700869
870 nfulnl_set_qthresh(inst, ntohl(qthresh));
871 }
872
Harald Welte0af5f6c2006-03-20 17:15:11 -0800873 if (nfula[NFULA_CFG_FLAGS-1]) {
Al Viro98a4a862006-11-08 00:26:51 -0800874 __be16 flags =
875 *(__be16 *)NFA_DATA(nfula[NFULA_CFG_FLAGS-1]);
Patrick McHardyee433532006-05-19 02:17:18 -0700876 nfulnl_set_flags(inst, ntohs(flags));
Harald Welte0af5f6c2006-03-20 17:15:11 -0800877 }
878
Harald Welte0597f262005-08-09 19:58:39 -0700879out_put:
880 instance_put(inst);
Michal Miroslawdd167042007-03-04 15:59:20 -0800881out:
Harald Welte0597f262005-08-09 19:58:39 -0700882 return ret;
883}
884
885static struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
886 [NFULNL_MSG_PACKET] = { .call = nfulnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800887 .attr_count = NFULA_MAX, },
Harald Welte0597f262005-08-09 19:58:39 -0700888 [NFULNL_MSG_CONFIG] = { .call = nfulnl_recv_config,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800889 .attr_count = NFULA_CFG_MAX, },
Harald Welte0597f262005-08-09 19:58:39 -0700890};
891
892static struct nfnetlink_subsystem nfulnl_subsys = {
893 .name = "log",
894 .subsys_id = NFNL_SUBSYS_ULOG,
895 .cb_count = NFULNL_MSG_MAX,
Harald Welte0597f262005-08-09 19:58:39 -0700896 .cb = nfulnl_cb,
897};
898
899#ifdef CONFIG_PROC_FS
900struct iter_state {
901 unsigned int bucket;
902};
903
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700904static struct hlist_node *get_first(struct iter_state *st)
Harald Welte0597f262005-08-09 19:58:39 -0700905{
Harald Welte0597f262005-08-09 19:58:39 -0700906 if (!st)
907 return NULL;
908
909 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
910 if (!hlist_empty(&instance_table[st->bucket]))
911 return instance_table[st->bucket].first;
912 }
913 return NULL;
914}
915
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700916static struct hlist_node *get_next(struct iter_state *st, struct hlist_node *h)
Harald Welte0597f262005-08-09 19:58:39 -0700917{
Harald Welte0597f262005-08-09 19:58:39 -0700918 h = h->next;
919 while (!h) {
920 if (++st->bucket >= INSTANCE_BUCKETS)
921 return NULL;
922
923 h = instance_table[st->bucket].first;
924 }
925 return h;
926}
927
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700928static struct hlist_node *get_idx(struct iter_state *st, loff_t pos)
Harald Welte0597f262005-08-09 19:58:39 -0700929{
930 struct hlist_node *head;
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700931 head = get_first(st);
Harald Welte0597f262005-08-09 19:58:39 -0700932
933 if (head)
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700934 while (pos && (head = get_next(st, head)))
Harald Welte0597f262005-08-09 19:58:39 -0700935 pos--;
936 return pos ? NULL : head;
937}
938
939static void *seq_start(struct seq_file *seq, loff_t *pos)
940{
941 read_lock_bh(&instances_lock);
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700942 return get_idx(seq->private, *pos);
Harald Welte0597f262005-08-09 19:58:39 -0700943}
944
945static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
946{
947 (*pos)++;
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700948 return get_next(s->private, v);
Harald Welte0597f262005-08-09 19:58:39 -0700949}
950
951static void seq_stop(struct seq_file *s, void *v)
952{
953 read_unlock_bh(&instances_lock);
954}
955
956static int seq_show(struct seq_file *s, void *v)
957{
958 const struct nfulnl_instance *inst = v;
959
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800960 return seq_printf(s, "%5d %6d %5d %1d %5d %6d %2d\n",
Harald Welte0597f262005-08-09 19:58:39 -0700961 inst->group_num,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800962 inst->peer_pid, inst->qlen,
Harald Welte0597f262005-08-09 19:58:39 -0700963 inst->copy_mode, inst->copy_range,
964 inst->flushtimeout, atomic_read(&inst->use));
965}
966
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700967static const struct seq_operations nful_seq_ops = {
Harald Welte0597f262005-08-09 19:58:39 -0700968 .start = seq_start,
969 .next = seq_next,
970 .stop = seq_stop,
971 .show = seq_show,
972};
973
974static int nful_open(struct inode *inode, struct file *file)
975{
976 struct seq_file *seq;
977 struct iter_state *is;
978 int ret;
979
Harald Welte10dfdc62005-11-03 19:20:07 +0100980 is = kzalloc(sizeof(*is), GFP_KERNEL);
Harald Welte0597f262005-08-09 19:58:39 -0700981 if (!is)
982 return -ENOMEM;
Harald Welte0597f262005-08-09 19:58:39 -0700983 ret = seq_open(file, &nful_seq_ops);
984 if (ret < 0)
985 goto out_free;
986 seq = file->private_data;
987 seq->private = is;
988 return ret;
989out_free:
990 kfree(is);
991 return ret;
992}
993
Arjan van de Venda7071d2007-02-12 00:55:36 -0800994static const struct file_operations nful_file_ops = {
Harald Welte0597f262005-08-09 19:58:39 -0700995 .owner = THIS_MODULE,
996 .open = nful_open,
997 .read = seq_read,
998 .llseek = seq_lseek,
999 .release = seq_release_private,
1000};
1001
1002#endif /* PROC_FS */
1003
Patrick McHardy32292a72006-04-06 14:11:30 -07001004static int __init nfnetlink_log_init(void)
Harald Welte0597f262005-08-09 19:58:39 -07001005{
1006 int i, status = -ENOMEM;
1007#ifdef CONFIG_PROC_FS
1008 struct proc_dir_entry *proc_nful;
1009#endif
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001010
Harald Welte0597f262005-08-09 19:58:39 -07001011 for (i = 0; i < INSTANCE_BUCKETS; i++)
1012 INIT_HLIST_HEAD(&instance_table[i]);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001013
Harald Welte0597f262005-08-09 19:58:39 -07001014 /* it's not really all that important to have a random value, so
1015 * we can do this from the init function, even if there hasn't
1016 * been that much entropy yet */
1017 get_random_bytes(&hash_init, sizeof(hash_init));
1018
1019 netlink_register_notifier(&nfulnl_rtnl_notifier);
1020 status = nfnetlink_subsys_register(&nfulnl_subsys);
1021 if (status < 0) {
1022 printk(KERN_ERR "log: failed to create netlink socket\n");
1023 goto cleanup_netlink_notifier;
1024 }
1025
1026#ifdef CONFIG_PROC_FS
1027 proc_nful = create_proc_entry("nfnetlink_log", 0440,
1028 proc_net_netfilter);
1029 if (!proc_nful)
1030 goto cleanup_subsys;
1031 proc_nful->proc_fops = &nful_file_ops;
1032#endif
Harald Welte0597f262005-08-09 19:58:39 -07001033 return status;
1034
Harald Welte0597f262005-08-09 19:58:39 -07001035#ifdef CONFIG_PROC_FS
Harald Welte0597f262005-08-09 19:58:39 -07001036cleanup_subsys:
Harald Welte0597f262005-08-09 19:58:39 -07001037 nfnetlink_subsys_unregister(&nfulnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -07001038#endif
Harald Welte0597f262005-08-09 19:58:39 -07001039cleanup_netlink_notifier:
1040 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
1041 return status;
1042}
1043
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001044static void __exit nfnetlink_log_fini(void)
Harald Welte0597f262005-08-09 19:58:39 -07001045{
Patrick McHardye92ad992007-02-12 11:11:55 -08001046 nf_log_unregister(&nfulnl_logger);
Patrick McHardy32292a72006-04-06 14:11:30 -07001047#ifdef CONFIG_PROC_FS
1048 remove_proc_entry("nfnetlink_log", proc_net_netfilter);
1049#endif
1050 nfnetlink_subsys_unregister(&nfulnl_subsys);
1051 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
Harald Welte0597f262005-08-09 19:58:39 -07001052}
1053
1054MODULE_DESCRIPTION("netfilter userspace logging");
1055MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1056MODULE_LICENSE("GPL");
Harald Weltef682fae2005-08-09 20:20:34 -07001057MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
Harald Welte0597f262005-08-09 19:58:39 -07001058
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001059module_init(nfnetlink_log_init);
1060module_exit(nfnetlink_log_fini);