blob: 16ae5391860641eb3be1bd16f4faa46f6ad125eb [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
Michal Miroslawaace57e2007-09-28 14:45:27 -0700155 if (!try_module_get(THIS_MODULE)) {
156 kfree(inst);
157 goto out_unlock;
158 }
159
Harald Welte0597f262005-08-09 19:58:39 -0700160 INIT_HLIST_NODE(&inst->hlist);
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800161 spin_lock_init(&inst->lock);
Harald Welte0597f262005-08-09 19:58:39 -0700162 /* needs to be two, since we _put() after creation */
163 atomic_set(&inst->use, 2);
164
Patrick McHardye6f689d2007-03-23 11:16:30 -0700165 setup_timer(&inst->timer, nfulnl_timer, (unsigned long)inst);
Harald Welte0597f262005-08-09 19:58:39 -0700166
167 inst->peer_pid = pid;
168 inst->group_num = group_num;
169
170 inst->qthreshold = NFULNL_QTHRESH_DEFAULT;
171 inst->flushtimeout = NFULNL_TIMEOUT_DEFAULT;
172 inst->nlbufsiz = NFULNL_NLBUFSIZ_DEFAULT;
173 inst->copy_mode = NFULNL_COPY_PACKET;
174 inst->copy_range = 0xffff;
175
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800176 hlist_add_head(&inst->hlist,
Harald Welte0597f262005-08-09 19:58:39 -0700177 &instance_table[instance_hashfn(group_num)]);
178
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800179 UDEBUG("newly added node: %p, next=%p\n", &inst->hlist,
Harald Welte0597f262005-08-09 19:58:39 -0700180 inst->hlist.next);
181
182 write_unlock_bh(&instances_lock);
183
184 return inst;
185
Harald Welte0597f262005-08-09 19:58:39 -0700186out_unlock:
187 write_unlock_bh(&instances_lock);
188 return NULL;
189}
190
Michal Miroslawe3567062007-09-28 14:44:21 -0700191static void __nfulnl_flush(struct nfulnl_instance *inst);
Harald Welte0597f262005-08-09 19:58:39 -0700192
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);
Michal Miroslawe3567062007-09-28 14:44:21 -0700205 if (inst->skb)
206 __nfulnl_flush(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700207 spin_unlock_bh(&inst->lock);
208
209 /* and finally put the refcount */
210 instance_put(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700211}
212
213static inline void
Harald Welte0597f262005-08-09 19:58:39 -0700214instance_destroy(struct nfulnl_instance *inst)
215{
Patrick McHardy9afdb002007-03-23 11:12:50 -0700216 write_lock_bh(&instances_lock);
217 __instance_destroy(inst);
218 write_unlock_bh(&instances_lock);
Harald Welte0597f262005-08-09 19:58:39 -0700219}
220
221static int
222nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
223 unsigned int range)
224{
225 int status = 0;
226
227 spin_lock_bh(&inst->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800228
Harald Welte0597f262005-08-09 19:58:39 -0700229 switch (mode) {
230 case NFULNL_COPY_NONE:
231 case NFULNL_COPY_META:
232 inst->copy_mode = mode;
233 inst->copy_range = 0;
234 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800235
Harald Welte0597f262005-08-09 19:58:39 -0700236 case NFULNL_COPY_PACKET:
237 inst->copy_mode = mode;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700238 /* we're using struct nlattr which has 16bit nfa_len */
Harald Welte0597f262005-08-09 19:58:39 -0700239 if (range > 0xffff)
240 inst->copy_range = 0xffff;
241 else
242 inst->copy_range = range;
243 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800244
Harald Welte0597f262005-08-09 19:58:39 -0700245 default:
246 status = -EINVAL;
247 break;
248 }
249
250 spin_unlock_bh(&inst->lock);
251
252 return status;
253}
254
255static int
256nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
257{
258 int status;
259
260 spin_lock_bh(&inst->lock);
261 if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
262 status = -ERANGE;
263 else if (nlbufsiz > 131072)
264 status = -ERANGE;
265 else {
266 inst->nlbufsiz = nlbufsiz;
267 status = 0;
268 }
269 spin_unlock_bh(&inst->lock);
270
271 return status;
272}
273
274static int
275nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
276{
277 spin_lock_bh(&inst->lock);
278 inst->flushtimeout = timeout;
279 spin_unlock_bh(&inst->lock);
280
281 return 0;
282}
283
284static int
285nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
286{
287 spin_lock_bh(&inst->lock);
288 inst->qthreshold = qthresh;
289 spin_unlock_bh(&inst->lock);
290
291 return 0;
292}
293
Harald Welte0af5f6c2006-03-20 17:15:11 -0800294static int
295nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
296{
297 spin_lock_bh(&inst->lock);
Patrick McHardyee433532006-05-19 02:17:18 -0700298 inst->flags = flags;
Harald Welte0af5f6c2006-03-20 17:15:11 -0800299 spin_unlock_bh(&inst->lock);
300
301 return 0;
302}
303
Michal Miroslawc6a8f642007-09-28 14:45:06 -0700304static struct sk_buff *
305nfulnl_alloc_skb(unsigned int inst_size, unsigned int pkt_size)
Harald Welte0597f262005-08-09 19:58:39 -0700306{
307 struct sk_buff *skb;
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800308 unsigned int n;
Harald Welte0597f262005-08-09 19:58:39 -0700309
310 UDEBUG("entered (%u, %u)\n", inst_size, pkt_size);
311
312 /* alloc skb which should be big enough for a whole multipart
313 * message. WARNING: has to be <= 128k due to slab restrictions */
314
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800315 n = max(inst_size, pkt_size);
316 skb = alloc_skb(n, GFP_ATOMIC);
Harald Welte0597f262005-08-09 19:58:39 -0700317 if (!skb) {
318 PRINTR("nfnetlink_log: can't alloc whole buffer (%u bytes)\n",
319 inst_size);
320
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800321 if (n > pkt_size) {
322 /* try to allocate only as much as we need for current
323 * packet */
Harald Welte0597f262005-08-09 19:58:39 -0700324
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800325 skb = alloc_skb(pkt_size, GFP_ATOMIC);
326 if (!skb)
327 PRINTR("nfnetlink_log: can't even alloc %u "
328 "bytes\n", pkt_size);
329 }
Harald Welte0597f262005-08-09 19:58:39 -0700330 }
331
332 return skb;
333}
334
335static int
336__nfulnl_send(struct nfulnl_instance *inst)
337{
Eric Leblond29c5d4a2007-09-18 13:07:15 -0700338 int status = -1;
Harald Welte0597f262005-08-09 19:58:39 -0700339
Harald Welte0597f262005-08-09 19:58:39 -0700340 if (inst->qlen > 1)
Eric Leblond29c5d4a2007-09-18 13:07:15 -0700341 NLMSG_PUT(inst->skb, 0, 0,
342 NLMSG_DONE,
343 sizeof(struct nfgenmsg));
Harald Welte0597f262005-08-09 19:58:39 -0700344
345 status = nfnetlink_unicast(inst->skb, inst->peer_pid, MSG_DONTWAIT);
346 if (status < 0) {
347 UDEBUG("netlink_unicast() failed\n");
348 /* FIXME: statistics */
349 }
350
351 inst->qlen = 0;
352 inst->skb = NULL;
Harald Welte0597f262005-08-09 19:58:39 -0700353
Eric Leblond29c5d4a2007-09-18 13:07:15 -0700354nlmsg_failure:
Harald Welte0597f262005-08-09 19:58:39 -0700355 return status;
356}
357
Michal Miroslawe3567062007-09-28 14:44:21 -0700358static void
359__nfulnl_flush(struct nfulnl_instance *inst)
360{
361 /* timer holds a reference */
362 if (del_timer(&inst->timer))
363 instance_put(inst);
364 if (inst->skb)
365 __nfulnl_send(inst);
366}
367
Michal Miroslawc6a8f642007-09-28 14:45:06 -0700368static void
369nfulnl_timer(unsigned long data)
Harald Welte0597f262005-08-09 19:58:39 -0700370{
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800371 struct nfulnl_instance *inst = (struct nfulnl_instance *)data;
Harald Welte0597f262005-08-09 19:58:39 -0700372
373 UDEBUG("timer function called, flushing buffer\n");
374
375 spin_lock_bh(&inst->lock);
Michal Miroslaw370e6a82007-03-23 11:12:21 -0700376 if (inst->skb)
377 __nfulnl_send(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700378 spin_unlock_bh(&inst->lock);
Michal Miroslaw05f7b7b2007-03-04 15:58:40 -0800379 instance_put(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700380}
381
Harald Welte0af5f6c2006-03-20 17:15:11 -0800382/* This is an inline function, we don't really care about a long
383 * list of arguments */
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800384static inline int
Harald Welte0597f262005-08-09 19:58:39 -0700385__build_packet_message(struct nfulnl_instance *inst,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800386 const struct sk_buff *skb,
Harald Welte0597f262005-08-09 19:58:39 -0700387 unsigned int data_len,
388 unsigned int pf,
389 unsigned int hooknum,
390 const struct net_device *indev,
391 const struct net_device *outdev,
392 const struct nf_loginfo *li,
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100393 const char *prefix, unsigned int plen)
Harald Welte0597f262005-08-09 19:58:39 -0700394{
Harald Welte0597f262005-08-09 19:58:39 -0700395 struct nfulnl_msg_packet_hdr pmsg;
396 struct nlmsghdr *nlh;
397 struct nfgenmsg *nfmsg;
Al Viro98a4a862006-11-08 00:26:51 -0800398 __be32 tmp_uint;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700399 sk_buff_data_t old_tail = inst->skb->tail;
Harald Welte0597f262005-08-09 19:58:39 -0700400
401 UDEBUG("entered\n");
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800402
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800403 nlh = NLMSG_PUT(inst->skb, 0, 0,
Harald Welte0597f262005-08-09 19:58:39 -0700404 NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET,
405 sizeof(struct nfgenmsg));
406 nfmsg = NLMSG_DATA(nlh);
407 nfmsg->nfgen_family = pf;
408 nfmsg->version = NFNETLINK_V0;
409 nfmsg->res_id = htons(inst->group_num);
410
Al Virofebf0a42006-11-03 00:59:17 -0800411 pmsg.hw_protocol = skb->protocol;
Harald Welte0597f262005-08-09 19:58:39 -0700412 pmsg.hook = hooknum;
413
Patrick McHardydf6fb862007-09-28 14:37:03 -0700414 NLA_PUT(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg);
Harald Welte0597f262005-08-09 19:58:39 -0700415
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100416 if (prefix)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700417 NLA_PUT(inst->skb, NFULA_PREFIX, plen, prefix);
Harald Welte0597f262005-08-09 19:58:39 -0700418
419 if (indev) {
420 tmp_uint = htonl(indev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700421#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700422 NLA_PUT(inst->skb, NFULA_IFINDEX_INDEV, sizeof(tmp_uint),
Harald Welte0597f262005-08-09 19:58:39 -0700423 &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700424#else
425 if (pf == PF_BRIDGE) {
426 /* Case 1: outdev is physical input device, we need to
427 * look for bridge group (when called from
428 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700429 NLA_PUT(inst->skb, NFULA_IFINDEX_PHYSINDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700430 sizeof(tmp_uint), &tmp_uint);
431 /* this is the bridge group "brX" */
432 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700433 NLA_PUT(inst->skb, NFULA_IFINDEX_INDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700434 sizeof(tmp_uint), &tmp_uint);
435 } else {
436 /* Case 2: indev is bridge group, we need to look for
437 * physical device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700438 NLA_PUT(inst->skb, NFULA_IFINDEX_INDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700439 sizeof(tmp_uint), &tmp_uint);
440 if (skb->nf_bridge && skb->nf_bridge->physindev) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800441 tmp_uint =
Harald Weltefbcd9232005-08-09 20:22:10 -0700442 htonl(skb->nf_bridge->physindev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700443 NLA_PUT(inst->skb, NFULA_IFINDEX_PHYSINDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700444 sizeof(tmp_uint), &tmp_uint);
445 }
446 }
447#endif
Harald Welte0597f262005-08-09 19:58:39 -0700448 }
449
450 if (outdev) {
451 tmp_uint = htonl(outdev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700452#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700453 NLA_PUT(inst->skb, NFULA_IFINDEX_OUTDEV, sizeof(tmp_uint),
Harald Welte0597f262005-08-09 19:58:39 -0700454 &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700455#else
456 if (pf == PF_BRIDGE) {
457 /* Case 1: outdev is physical output device, we need to
458 * look for bridge group (when called from
459 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700460 NLA_PUT(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700461 sizeof(tmp_uint), &tmp_uint);
462 /* this is the bridge group "brX" */
463 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700464 NLA_PUT(inst->skb, NFULA_IFINDEX_OUTDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700465 sizeof(tmp_uint), &tmp_uint);
466 } else {
467 /* Case 2: indev is a bridge group, we need to look
468 * for physical device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700469 NLA_PUT(inst->skb, NFULA_IFINDEX_OUTDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700470 sizeof(tmp_uint), &tmp_uint);
Patrick McHardyba5dcee2007-03-06 20:24:53 -0800471 if (skb->nf_bridge && skb->nf_bridge->physoutdev) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800472 tmp_uint =
Harald Weltefbcd9232005-08-09 20:22:10 -0700473 htonl(skb->nf_bridge->physoutdev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700474 NLA_PUT(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700475 sizeof(tmp_uint), &tmp_uint);
476 }
477 }
478#endif
Harald Welte0597f262005-08-09 19:58:39 -0700479 }
480
Thomas Graf82e91ff2006-11-09 15:19:14 -0800481 if (skb->mark) {
482 tmp_uint = htonl(skb->mark);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700483 NLA_PUT(inst->skb, NFULA_MARK, sizeof(tmp_uint), &tmp_uint);
Harald Welte0597f262005-08-09 19:58:39 -0700484 }
485
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700486 if (indev && skb->dev) {
Harald Welte0597f262005-08-09 19:58:39 -0700487 struct nfulnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700488 int len = dev_parse_header(skb, phw.hw_addr);
489 if (len > 0) {
490 phw.hw_addrlen = htons(len);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700491 NLA_PUT(inst->skb, NFULA_HWADDR, sizeof(phw), &phw);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700492 }
Harald Welte0597f262005-08-09 19:58:39 -0700493 }
494
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700495 if (skb->tstamp.tv64) {
Harald Welte0597f262005-08-09 19:58:39 -0700496 struct nfulnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700497 struct timeval tv = ktime_to_timeval(skb->tstamp);
498 ts.sec = cpu_to_be64(tv.tv_sec);
499 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte0597f262005-08-09 19:58:39 -0700500
Patrick McHardydf6fb862007-09-28 14:37:03 -0700501 NLA_PUT(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts);
Harald Welte0597f262005-08-09 19:58:39 -0700502 }
503
504 /* UID */
505 if (skb->sk) {
506 read_lock_bh(&skb->sk->sk_callback_lock);
507 if (skb->sk->sk_socket && skb->sk->sk_socket->file) {
Al Viro98a4a862006-11-08 00:26:51 -0800508 __be32 uid = htonl(skb->sk->sk_socket->file->f_uid);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700509 /* need to unlock here since NLA_PUT may goto */
Harald Welte0597f262005-08-09 19:58:39 -0700510 read_unlock_bh(&skb->sk->sk_callback_lock);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700511 NLA_PUT(inst->skb, NFULA_UID, sizeof(uid), &uid);
Harald Welte0597f262005-08-09 19:58:39 -0700512 } else
513 read_unlock_bh(&skb->sk->sk_callback_lock);
514 }
515
Harald Welte0af5f6c2006-03-20 17:15:11 -0800516 /* local sequence number */
517 if (inst->flags & NFULNL_CFG_F_SEQ) {
518 tmp_uint = htonl(inst->seq++);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700519 NLA_PUT(inst->skb, NFULA_SEQ, sizeof(tmp_uint), &tmp_uint);
Harald Welte0af5f6c2006-03-20 17:15:11 -0800520 }
521 /* global sequence number */
522 if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) {
Patrick McHardy7fdeaf62006-11-14 19:47:09 -0800523 tmp_uint = htonl(atomic_inc_return(&global_seq));
Patrick McHardydf6fb862007-09-28 14:37:03 -0700524 NLA_PUT(inst->skb, NFULA_SEQ_GLOBAL, sizeof(tmp_uint), &tmp_uint);
Harald Welte0af5f6c2006-03-20 17:15:11 -0800525 }
526
Harald Welte0597f262005-08-09 19:58:39 -0700527 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700528 struct nlattr *nla;
529 int size = nla_attr_size(data_len);
Harald Welte0597f262005-08-09 19:58:39 -0700530
Patrick McHardydf6fb862007-09-28 14:37:03 -0700531 if (skb_tailroom(inst->skb) < nla_total_size(data_len)) {
Harald Welte0597f262005-08-09 19:58:39 -0700532 printk(KERN_WARNING "nfnetlink_log: no tailroom!\n");
533 goto nlmsg_failure;
534 }
535
Patrick McHardydf6fb862007-09-28 14:37:03 -0700536 nla = (struct nlattr *)skb_put(inst->skb, nla_total_size(data_len));
537 nla->nla_type = NFULA_PAYLOAD;
538 nla->nla_len = size;
Harald Welte0597f262005-08-09 19:58:39 -0700539
Patrick McHardydf6fb862007-09-28 14:37:03 -0700540 if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
Harald Welte0597f262005-08-09 19:58:39 -0700541 BUG();
542 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800543
Harald Welte0597f262005-08-09 19:58:39 -0700544 nlh->nlmsg_len = inst->skb->tail - old_tail;
545 return 0;
546
547nlmsg_failure:
548 UDEBUG("nlmsg_failure\n");
Patrick McHardydf6fb862007-09-28 14:37:03 -0700549nla_put_failure:
Harald Welte0597f262005-08-09 19:58:39 -0700550 PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
551 return -1;
552}
553
554#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
555
556static struct nf_loginfo default_loginfo = {
557 .type = NF_LOG_TYPE_ULOG,
558 .u = {
559 .ulog = {
560 .copy_len = 0xffff,
561 .group = 0,
562 .qthreshold = 1,
563 },
564 },
565};
566
567/* log handler for internal netfilter logging api */
568static void
569nfulnl_log_packet(unsigned int pf,
570 unsigned int hooknum,
571 const struct sk_buff *skb,
572 const struct net_device *in,
573 const struct net_device *out,
574 const struct nf_loginfo *li_user,
575 const char *prefix)
576{
577 unsigned int size, data_len;
578 struct nfulnl_instance *inst;
579 const struct nf_loginfo *li;
580 unsigned int qthreshold;
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100581 unsigned int plen;
Harald Welte0597f262005-08-09 19:58:39 -0700582
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800583 if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
Harald Welte0597f262005-08-09 19:58:39 -0700584 li = li_user;
585 else
586 li = &default_loginfo;
587
588 inst = instance_lookup_get(li->u.ulog.group);
589 if (!inst)
Harald Welte0597f262005-08-09 19:58:39 -0700590 return;
Harald Welte0597f262005-08-09 19:58:39 -0700591
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100592 plen = 0;
593 if (prefix)
Patrick McHardy881dbfe2007-03-06 20:24:35 -0800594 plen = strlen(prefix) + 1;
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100595
Harald Welte0597f262005-08-09 19:58:39 -0700596 /* FIXME: do we want to make the size calculation conditional based on
597 * what is actually present? way more branches and checks, but more
598 * memory efficient... */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700599 size = NLMSG_ALIGN(sizeof(struct nfgenmsg))
600 + nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
601 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
602 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700603#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700604 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
605 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700606#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700607 + nla_total_size(sizeof(u_int32_t)) /* mark */
608 + nla_total_size(sizeof(u_int32_t)) /* uid */
609 + nla_total_size(plen) /* prefix */
610 + nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
611 + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp));
Harald Welte0597f262005-08-09 19:58:39 -0700612
613 UDEBUG("initial size=%u\n", size);
614
615 spin_lock_bh(&inst->lock);
616
Harald Welte0af5f6c2006-03-20 17:15:11 -0800617 if (inst->flags & NFULNL_CFG_F_SEQ)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700618 size += nla_total_size(sizeof(u_int32_t));
Harald Welte0af5f6c2006-03-20 17:15:11 -0800619 if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700620 size += nla_total_size(sizeof(u_int32_t));
Harald Welte0af5f6c2006-03-20 17:15:11 -0800621
Harald Welte0597f262005-08-09 19:58:39 -0700622 qthreshold = inst->qthreshold;
623 /* per-rule qthreshold overrides per-instance */
624 if (qthreshold > li->u.ulog.qthreshold)
625 qthreshold = li->u.ulog.qthreshold;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800626
Harald Welte0597f262005-08-09 19:58:39 -0700627 switch (inst->copy_mode) {
628 case NFULNL_COPY_META:
629 case NFULNL_COPY_NONE:
630 data_len = 0;
631 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800632
Harald Welte0597f262005-08-09 19:58:39 -0700633 case NFULNL_COPY_PACKET:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800634 if (inst->copy_range == 0
Harald Welte0597f262005-08-09 19:58:39 -0700635 || inst->copy_range > skb->len)
636 data_len = skb->len;
637 else
638 data_len = inst->copy_range;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800639
Patrick McHardydf6fb862007-09-28 14:37:03 -0700640 size += nla_total_size(data_len);
Harald Welte0597f262005-08-09 19:58:39 -0700641 UDEBUG("copy_packet, therefore size now %u\n", size);
642 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800643
Harald Welte0597f262005-08-09 19:58:39 -0700644 default:
Michal Miroslaw55b5a912007-03-23 11:11:05 -0700645 goto unlock_and_release;
Harald Welte0597f262005-08-09 19:58:39 -0700646 }
647
Michal Miroslawd63b0432007-09-28 14:44:44 -0700648 if (inst->skb &&
649 size > skb_tailroom(inst->skb) - sizeof(struct nfgenmsg)) {
Harald Welte0597f262005-08-09 19:58:39 -0700650 /* either the queue len is too high or we don't have
651 * enough room in the skb left. flush to userspace. */
652 UDEBUG("flushing old skb\n");
653
Michal Miroslawe3567062007-09-28 14:44:21 -0700654 __nfulnl_flush(inst);
Michal Miroslaw55b5a912007-03-23 11:11:05 -0700655 }
Harald Welte0597f262005-08-09 19:58:39 -0700656
Michal Miroslaw55b5a912007-03-23 11:11:05 -0700657 if (!inst->skb) {
658 inst->skb = nfulnl_alloc_skb(inst->nlbufsiz, size);
659 if (!inst->skb)
Harald Welte0597f262005-08-09 19:58:39 -0700660 goto alloc_failure;
Harald Welte0597f262005-08-09 19:58:39 -0700661 }
662
663 UDEBUG("qlen %d, qthreshold %d\n", inst->qlen, qthreshold);
664 inst->qlen++;
665
666 __build_packet_message(inst, skb, data_len, pf,
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100667 hooknum, in, out, li, prefix, plen);
Harald Welte0597f262005-08-09 19:58:39 -0700668
Michal Miroslawd63b0432007-09-28 14:44:44 -0700669 if (inst->qlen >= qthreshold)
670 __nfulnl_flush(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700671 /* timer_pending always called within inst->lock, so there
672 * is no chance of a race here */
Michal Miroslawd63b0432007-09-28 14:44:44 -0700673 else if (!timer_pending(&inst->timer)) {
Harald Welte0597f262005-08-09 19:58:39 -0700674 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,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700725 struct nlmsghdr *nlh, struct nlattr *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
Patrick McHardyfd8281a2007-09-28 14:39:09 -0700736static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
737 [NFULA_CFG_CMD] = { .len = sizeof(struct nfulnl_msg_config_cmd) },
738 [NFULA_CFG_MODE] = { .len = sizeof(struct nfulnl_msg_config_mode) },
739 [NFULA_CFG_TIMEOUT] = { .type = NLA_U32 },
740 [NFULA_CFG_QTHRESH] = { .type = NLA_U32 },
741 [NFULA_CFG_NLBUFSIZ] = { .type = NLA_U32 },
742 [NFULA_CFG_FLAGS] = { .type = NLA_U16 },
Harald Welte0597f262005-08-09 19:58:39 -0700743};
744
745static int
746nfulnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700747 struct nlmsghdr *nlh, struct nlattr *nfula[])
Harald Welte0597f262005-08-09 19:58:39 -0700748{
749 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
750 u_int16_t group_num = ntohs(nfmsg->res_id);
751 struct nfulnl_instance *inst;
752 int ret = 0;
753
754 UDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
755
Harald Welte0597f262005-08-09 19:58:39 -0700756 inst = instance_lookup_get(group_num);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700757 if (nfula[NFULA_CFG_CMD]) {
Harald Welte0597f262005-08-09 19:58:39 -0700758 u_int8_t pf = nfmsg->nfgen_family;
759 struct nfulnl_msg_config_cmd *cmd;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700760 cmd = nla_data(nfula[NFULA_CFG_CMD]);
Harald Welte0597f262005-08-09 19:58:39 -0700761 UDEBUG("found CFG_CMD for\n");
762
763 switch (cmd->command) {
764 case NFULNL_CFG_CMD_BIND:
765 if (inst) {
766 ret = -EBUSY;
767 goto out_put;
768 }
769
770 inst = instance_create(group_num,
771 NETLINK_CB(skb).pid);
772 if (!inst) {
773 ret = -EINVAL;
Michal Miroslawf414c162007-03-23 11:11:31 -0700774 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700775 }
776 break;
777 case NFULNL_CFG_CMD_UNBIND:
778 if (!inst) {
779 ret = -ENODEV;
Michal Miroslawf414c162007-03-23 11:11:31 -0700780 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700781 }
782
783 if (inst->peer_pid != NETLINK_CB(skb).pid) {
784 ret = -EPERM;
785 goto out_put;
786 }
787
788 instance_destroy(inst);
Michal Miroslaw9a36e8c2007-03-23 11:11:48 -0700789 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700790 case NFULNL_CFG_CMD_PF_BIND:
791 UDEBUG("registering log handler for pf=%u\n", pf);
792 ret = nf_log_register(pf, &nfulnl_logger);
793 break;
794 case NFULNL_CFG_CMD_PF_UNBIND:
795 UDEBUG("unregistering log handler for pf=%u\n", pf);
796 /* This is a bug and a feature. We cannot unregister
797 * other handlers, like nfnetlink_inst can */
798 nf_log_unregister_pf(pf);
799 break;
800 default:
801 ret = -EINVAL;
802 break;
803 }
Michal Miroslawdd167042007-03-04 15:59:20 -0800804
805 if (!inst)
806 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700807 } else {
808 if (!inst) {
809 UDEBUG("no config command, and no instance for "
810 "group=%u pid=%u =>ENOENT\n",
811 group_num, NETLINK_CB(skb).pid);
812 ret = -ENOENT;
Michal Miroslawf414c162007-03-23 11:11:31 -0700813 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700814 }
815
816 if (inst->peer_pid != NETLINK_CB(skb).pid) {
817 UDEBUG("no config command, and wrong pid\n");
818 ret = -EPERM;
819 goto out_put;
820 }
821 }
822
Patrick McHardydf6fb862007-09-28 14:37:03 -0700823 if (nfula[NFULA_CFG_MODE]) {
Harald Welte0597f262005-08-09 19:58:39 -0700824 struct nfulnl_msg_config_mode *params;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700825 params = nla_data(nfula[NFULA_CFG_MODE]);
Harald Welte0597f262005-08-09 19:58:39 -0700826
827 nfulnl_set_mode(inst, params->copy_mode,
Al Virod1208b92006-11-03 00:58:41 -0800828 ntohl(params->copy_range));
Harald Welte0597f262005-08-09 19:58:39 -0700829 }
830
Patrick McHardydf6fb862007-09-28 14:37:03 -0700831 if (nfula[NFULA_CFG_TIMEOUT]) {
Al Viro98a4a862006-11-08 00:26:51 -0800832 __be32 timeout =
Patrick McHardydf6fb862007-09-28 14:37:03 -0700833 *(__be32 *)nla_data(nfula[NFULA_CFG_TIMEOUT]);
Harald Welte0597f262005-08-09 19:58:39 -0700834
835 nfulnl_set_timeout(inst, ntohl(timeout));
836 }
837
Patrick McHardydf6fb862007-09-28 14:37:03 -0700838 if (nfula[NFULA_CFG_NLBUFSIZ]) {
Al Viro98a4a862006-11-08 00:26:51 -0800839 __be32 nlbufsiz =
Patrick McHardydf6fb862007-09-28 14:37:03 -0700840 *(__be32 *)nla_data(nfula[NFULA_CFG_NLBUFSIZ]);
Harald Welte0597f262005-08-09 19:58:39 -0700841
842 nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
843 }
844
Patrick McHardydf6fb862007-09-28 14:37:03 -0700845 if (nfula[NFULA_CFG_QTHRESH]) {
Al Viro7ac00a22006-11-03 00:58:17 -0800846 __be32 qthresh =
Patrick McHardydf6fb862007-09-28 14:37:03 -0700847 *(__be32 *)nla_data(nfula[NFULA_CFG_QTHRESH]);
Harald Welte0597f262005-08-09 19:58:39 -0700848
849 nfulnl_set_qthresh(inst, ntohl(qthresh));
850 }
851
Patrick McHardydf6fb862007-09-28 14:37:03 -0700852 if (nfula[NFULA_CFG_FLAGS]) {
Al Viro98a4a862006-11-08 00:26:51 -0800853 __be16 flags =
Patrick McHardydf6fb862007-09-28 14:37:03 -0700854 *(__be16 *)nla_data(nfula[NFULA_CFG_FLAGS]);
Patrick McHardyee433532006-05-19 02:17:18 -0700855 nfulnl_set_flags(inst, ntohs(flags));
Harald Welte0af5f6c2006-03-20 17:15:11 -0800856 }
857
Harald Welte0597f262005-08-09 19:58:39 -0700858out_put:
859 instance_put(inst);
Michal Miroslawdd167042007-03-04 15:59:20 -0800860out:
Harald Welte0597f262005-08-09 19:58:39 -0700861 return ret;
862}
863
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700864static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
Harald Welte0597f262005-08-09 19:58:39 -0700865 [NFULNL_MSG_PACKET] = { .call = nfulnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800866 .attr_count = NFULA_MAX, },
Harald Welte0597f262005-08-09 19:58:39 -0700867 [NFULNL_MSG_CONFIG] = { .call = nfulnl_recv_config,
Patrick McHardyfd8281a2007-09-28 14:39:09 -0700868 .attr_count = NFULA_CFG_MAX,
869 .policy = nfula_cfg_policy },
Harald Welte0597f262005-08-09 19:58:39 -0700870};
871
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700872static const struct nfnetlink_subsystem nfulnl_subsys = {
Harald Welte0597f262005-08-09 19:58:39 -0700873 .name = "log",
874 .subsys_id = NFNL_SUBSYS_ULOG,
875 .cb_count = NFULNL_MSG_MAX,
Harald Welte0597f262005-08-09 19:58:39 -0700876 .cb = nfulnl_cb,
877};
878
879#ifdef CONFIG_PROC_FS
880struct iter_state {
881 unsigned int bucket;
882};
883
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700884static struct hlist_node *get_first(struct iter_state *st)
Harald Welte0597f262005-08-09 19:58:39 -0700885{
Harald Welte0597f262005-08-09 19:58:39 -0700886 if (!st)
887 return NULL;
888
889 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
890 if (!hlist_empty(&instance_table[st->bucket]))
891 return instance_table[st->bucket].first;
892 }
893 return NULL;
894}
895
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700896static struct hlist_node *get_next(struct iter_state *st, struct hlist_node *h)
Harald Welte0597f262005-08-09 19:58:39 -0700897{
Harald Welte0597f262005-08-09 19:58:39 -0700898 h = h->next;
899 while (!h) {
900 if (++st->bucket >= INSTANCE_BUCKETS)
901 return NULL;
902
903 h = instance_table[st->bucket].first;
904 }
905 return h;
906}
907
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700908static struct hlist_node *get_idx(struct iter_state *st, loff_t pos)
Harald Welte0597f262005-08-09 19:58:39 -0700909{
910 struct hlist_node *head;
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700911 head = get_first(st);
Harald Welte0597f262005-08-09 19:58:39 -0700912
913 if (head)
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700914 while (pos && (head = get_next(st, head)))
Harald Welte0597f262005-08-09 19:58:39 -0700915 pos--;
916 return pos ? NULL : head;
917}
918
919static void *seq_start(struct seq_file *seq, loff_t *pos)
920{
921 read_lock_bh(&instances_lock);
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700922 return get_idx(seq->private, *pos);
Harald Welte0597f262005-08-09 19:58:39 -0700923}
924
925static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
926{
927 (*pos)++;
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700928 return get_next(s->private, v);
Harald Welte0597f262005-08-09 19:58:39 -0700929}
930
931static void seq_stop(struct seq_file *s, void *v)
932{
933 read_unlock_bh(&instances_lock);
934}
935
936static int seq_show(struct seq_file *s, void *v)
937{
938 const struct nfulnl_instance *inst = v;
939
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800940 return seq_printf(s, "%5d %6d %5d %1d %5d %6d %2d\n",
Harald Welte0597f262005-08-09 19:58:39 -0700941 inst->group_num,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800942 inst->peer_pid, inst->qlen,
Harald Welte0597f262005-08-09 19:58:39 -0700943 inst->copy_mode, inst->copy_range,
944 inst->flushtimeout, atomic_read(&inst->use));
945}
946
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700947static const struct seq_operations nful_seq_ops = {
Harald Welte0597f262005-08-09 19:58:39 -0700948 .start = seq_start,
949 .next = seq_next,
950 .stop = seq_stop,
951 .show = seq_show,
952};
953
954static int nful_open(struct inode *inode, struct file *file)
955{
956 struct seq_file *seq;
957 struct iter_state *is;
958 int ret;
959
Harald Welte10dfdc62005-11-03 19:20:07 +0100960 is = kzalloc(sizeof(*is), GFP_KERNEL);
Harald Welte0597f262005-08-09 19:58:39 -0700961 if (!is)
962 return -ENOMEM;
Harald Welte0597f262005-08-09 19:58:39 -0700963 ret = seq_open(file, &nful_seq_ops);
964 if (ret < 0)
965 goto out_free;
966 seq = file->private_data;
967 seq->private = is;
968 return ret;
969out_free:
970 kfree(is);
971 return ret;
972}
973
Arjan van de Venda7071d2007-02-12 00:55:36 -0800974static const struct file_operations nful_file_ops = {
Harald Welte0597f262005-08-09 19:58:39 -0700975 .owner = THIS_MODULE,
976 .open = nful_open,
977 .read = seq_read,
978 .llseek = seq_lseek,
979 .release = seq_release_private,
980};
981
982#endif /* PROC_FS */
983
Patrick McHardy32292a72006-04-06 14:11:30 -0700984static int __init nfnetlink_log_init(void)
Harald Welte0597f262005-08-09 19:58:39 -0700985{
986 int i, status = -ENOMEM;
987#ifdef CONFIG_PROC_FS
988 struct proc_dir_entry *proc_nful;
989#endif
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800990
Harald Welte0597f262005-08-09 19:58:39 -0700991 for (i = 0; i < INSTANCE_BUCKETS; i++)
992 INIT_HLIST_HEAD(&instance_table[i]);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800993
Harald Welte0597f262005-08-09 19:58:39 -0700994 /* it's not really all that important to have a random value, so
995 * we can do this from the init function, even if there hasn't
996 * been that much entropy yet */
997 get_random_bytes(&hash_init, sizeof(hash_init));
998
999 netlink_register_notifier(&nfulnl_rtnl_notifier);
1000 status = nfnetlink_subsys_register(&nfulnl_subsys);
1001 if (status < 0) {
1002 printk(KERN_ERR "log: failed to create netlink socket\n");
1003 goto cleanup_netlink_notifier;
1004 }
1005
1006#ifdef CONFIG_PROC_FS
1007 proc_nful = create_proc_entry("nfnetlink_log", 0440,
1008 proc_net_netfilter);
1009 if (!proc_nful)
1010 goto cleanup_subsys;
1011 proc_nful->proc_fops = &nful_file_ops;
1012#endif
Harald Welte0597f262005-08-09 19:58:39 -07001013 return status;
1014
Harald Welte0597f262005-08-09 19:58:39 -07001015#ifdef CONFIG_PROC_FS
Harald Welte0597f262005-08-09 19:58:39 -07001016cleanup_subsys:
Harald Welte0597f262005-08-09 19:58:39 -07001017 nfnetlink_subsys_unregister(&nfulnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -07001018#endif
Harald Welte0597f262005-08-09 19:58:39 -07001019cleanup_netlink_notifier:
1020 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
1021 return status;
1022}
1023
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001024static void __exit nfnetlink_log_fini(void)
Harald Welte0597f262005-08-09 19:58:39 -07001025{
Patrick McHardye92ad992007-02-12 11:11:55 -08001026 nf_log_unregister(&nfulnl_logger);
Patrick McHardy32292a72006-04-06 14:11:30 -07001027#ifdef CONFIG_PROC_FS
1028 remove_proc_entry("nfnetlink_log", proc_net_netfilter);
1029#endif
1030 nfnetlink_subsys_unregister(&nfulnl_subsys);
1031 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
Harald Welte0597f262005-08-09 19:58:39 -07001032}
1033
1034MODULE_DESCRIPTION("netfilter userspace logging");
1035MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1036MODULE_LICENSE("GPL");
Harald Weltef682fae2005-08-09 20:20:34 -07001037MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
Harald Welte0597f262005-08-09 19:58:39 -07001038
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001039module_init(nfnetlink_log_init);
1040module_exit(nfnetlink_log_fini);