blob: e0504e90a0f0e4d8c28d02c65c91b286af77efb4 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Harald Welte0597f262005-08-09 19:58:39 -070032#include <net/sock.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080033#include <net/netfilter/nf_log.h>
Patrick McHardyd9e15002008-11-18 12:16:52 +010034#include <net/netfilter/nfnetlink_log.h>
Harald Welte0597f262005-08-09 19:58:39 -070035
36#include <asm/atomic.h>
37
Harald Weltefbcd9232005-08-09 20:22:10 -070038#ifdef CONFIG_BRIDGE_NETFILTER
39#include "../bridge/br_private.h"
40#endif
41
Holger Eitzenbergerc2db2922006-02-04 02:13:14 -080042#define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
Eric Leblond2c6764b2009-02-18 15:29:49 +010043#define NFULNL_TIMEOUT_DEFAULT 100 /* every second */
Harald Welte0597f262005-08-09 19:58:39 -070044#define NFULNL_QTHRESH_DEFAULT 100 /* 100 packets */
Michal Miroslaw6b6ec992007-09-28 14:45:52 -070045#define NFULNL_COPY_RANGE_MAX 0xFFFF /* max packet size is limited by 16-bit struct nfattr nfa_len field */
Harald Welte0597f262005-08-09 19:58:39 -070046
47#define PRINTR(x, args...) do { if (net_ratelimit()) \
48 printk(x, ## args); } while (0);
49
Harald Welte0597f262005-08-09 19:58:39 -070050struct nfulnl_instance {
51 struct hlist_node hlist; /* global list of instances */
52 spinlock_t lock;
53 atomic_t use; /* use count */
54
55 unsigned int qlen; /* number of nlmsgs in skb */
56 struct sk_buff *skb; /* pre-allocatd skb */
Harald Welte0597f262005-08-09 19:58:39 -070057 struct timer_list timer;
58 int peer_pid; /* PID of the peer process */
59
60 /* configurable parameters */
61 unsigned int flushtimeout; /* timeout until queue flush */
62 unsigned int nlbufsiz; /* netlink buffer allocation size */
63 unsigned int qthreshold; /* threshold of the queue */
64 u_int32_t copy_range;
Harald Welte0af5f6c2006-03-20 17:15:11 -080065 u_int32_t seq; /* instance-local sequential counter */
Harald Welte0597f262005-08-09 19:58:39 -070066 u_int16_t group_num; /* number of this queue */
Harald Welte0af5f6c2006-03-20 17:15:11 -080067 u_int16_t flags;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080068 u_int8_t copy_mode;
Harald Welte0597f262005-08-09 19:58:39 -070069};
70
71static DEFINE_RWLOCK(instances_lock);
Harald Welte0af5f6c2006-03-20 17:15:11 -080072static atomic_t global_seq;
Harald Welte0597f262005-08-09 19:58:39 -070073
74#define INSTANCE_BUCKETS 16
75static struct hlist_head instance_table[INSTANCE_BUCKETS];
76static unsigned int hash_init;
77
78static inline u_int8_t instance_hashfn(u_int16_t group_num)
79{
80 return ((group_num & 0xff) % INSTANCE_BUCKETS);
81}
82
83static struct nfulnl_instance *
84__instance_lookup(u_int16_t group_num)
85{
86 struct hlist_head *head;
87 struct hlist_node *pos;
88 struct nfulnl_instance *inst;
89
Harald Welte0597f262005-08-09 19:58:39 -070090 head = &instance_table[instance_hashfn(group_num)];
91 hlist_for_each_entry(inst, pos, head, hlist) {
92 if (inst->group_num == group_num)
93 return inst;
94 }
95 return NULL;
96}
97
98static inline void
99instance_get(struct nfulnl_instance *inst)
100{
101 atomic_inc(&inst->use);
102}
103
104static struct nfulnl_instance *
105instance_lookup_get(u_int16_t group_num)
106{
107 struct nfulnl_instance *inst;
108
109 read_lock_bh(&instances_lock);
110 inst = __instance_lookup(group_num);
111 if (inst)
112 instance_get(inst);
113 read_unlock_bh(&instances_lock);
114
115 return inst;
116}
117
118static void
119instance_put(struct nfulnl_instance *inst)
120{
121 if (inst && atomic_dec_and_test(&inst->use)) {
Harald Welte0597f262005-08-09 19:58:39 -0700122 kfree(inst);
Patrick McHardy7d90e862007-03-04 15:59:45 -0800123 module_put(THIS_MODULE);
Harald Welte0597f262005-08-09 19:58:39 -0700124 }
125}
126
127static void nfulnl_timer(unsigned long data);
128
129static struct nfulnl_instance *
130instance_create(u_int16_t group_num, int pid)
131{
132 struct nfulnl_instance *inst;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800133 int err;
Harald Welte0597f262005-08-09 19:58:39 -0700134
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800135 write_lock_bh(&instances_lock);
Harald Welte0597f262005-08-09 19:58:39 -0700136 if (__instance_lookup(group_num)) {
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800137 err = -EEXIST;
Harald Welte0597f262005-08-09 19:58:39 -0700138 goto out_unlock;
139 }
140
Harald Welte10dfdc62005-11-03 19:20:07 +0100141 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800142 if (!inst) {
143 err = -ENOMEM;
Harald Welte0597f262005-08-09 19:58:39 -0700144 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800145 }
Harald Welte0597f262005-08-09 19:58:39 -0700146
Michal Miroslawaace57e2007-09-28 14:45:27 -0700147 if (!try_module_get(THIS_MODULE)) {
148 kfree(inst);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800149 err = -EAGAIN;
Michal Miroslawaace57e2007-09-28 14:45:27 -0700150 goto out_unlock;
151 }
152
Harald Welte0597f262005-08-09 19:58:39 -0700153 INIT_HLIST_NODE(&inst->hlist);
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800154 spin_lock_init(&inst->lock);
Harald Welte0597f262005-08-09 19:58:39 -0700155 /* needs to be two, since we _put() after creation */
156 atomic_set(&inst->use, 2);
157
Patrick McHardye6f689d2007-03-23 11:16:30 -0700158 setup_timer(&inst->timer, nfulnl_timer, (unsigned long)inst);
Harald Welte0597f262005-08-09 19:58:39 -0700159
160 inst->peer_pid = pid;
161 inst->group_num = group_num;
162
163 inst->qthreshold = NFULNL_QTHRESH_DEFAULT;
164 inst->flushtimeout = NFULNL_TIMEOUT_DEFAULT;
165 inst->nlbufsiz = NFULNL_NLBUFSIZ_DEFAULT;
166 inst->copy_mode = NFULNL_COPY_PACKET;
Michal Miroslaw6b6ec992007-09-28 14:45:52 -0700167 inst->copy_range = NFULNL_COPY_RANGE_MAX;
Harald Welte0597f262005-08-09 19:58:39 -0700168
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800169 hlist_add_head(&inst->hlist,
Harald Welte0597f262005-08-09 19:58:39 -0700170 &instance_table[instance_hashfn(group_num)]);
171
Harald Welte0597f262005-08-09 19:58:39 -0700172 write_unlock_bh(&instances_lock);
173
174 return inst;
175
Harald Welte0597f262005-08-09 19:58:39 -0700176out_unlock:
177 write_unlock_bh(&instances_lock);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800178 return ERR_PTR(err);
Harald Welte0597f262005-08-09 19:58:39 -0700179}
180
Michal Miroslawe3567062007-09-28 14:44:21 -0700181static void __nfulnl_flush(struct nfulnl_instance *inst);
Harald Welte0597f262005-08-09 19:58:39 -0700182
183static void
Patrick McHardy9afdb002007-03-23 11:12:50 -0700184__instance_destroy(struct nfulnl_instance *inst)
Harald Welte0597f262005-08-09 19:58:39 -0700185{
186 /* first pull it out of the global list */
Harald Welte0597f262005-08-09 19:58:39 -0700187 hlist_del(&inst->hlist);
188
Harald Welte0597f262005-08-09 19:58:39 -0700189 /* then flush all pending packets from skb */
190
191 spin_lock_bh(&inst->lock);
Michal Miroslawe3567062007-09-28 14:44:21 -0700192 if (inst->skb)
193 __nfulnl_flush(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700194 spin_unlock_bh(&inst->lock);
195
196 /* and finally put the refcount */
197 instance_put(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700198}
199
200static inline void
Harald Welte0597f262005-08-09 19:58:39 -0700201instance_destroy(struct nfulnl_instance *inst)
202{
Patrick McHardy9afdb002007-03-23 11:12:50 -0700203 write_lock_bh(&instances_lock);
204 __instance_destroy(inst);
205 write_unlock_bh(&instances_lock);
Harald Welte0597f262005-08-09 19:58:39 -0700206}
207
208static int
209nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
210 unsigned int range)
211{
212 int status = 0;
213
214 spin_lock_bh(&inst->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800215
Harald Welte0597f262005-08-09 19:58:39 -0700216 switch (mode) {
217 case NFULNL_COPY_NONE:
218 case NFULNL_COPY_META:
219 inst->copy_mode = mode;
220 inst->copy_range = 0;
221 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800222
Harald Welte0597f262005-08-09 19:58:39 -0700223 case NFULNL_COPY_PACKET:
224 inst->copy_mode = mode;
Michal Miroslaw6b6ec992007-09-28 14:45:52 -0700225 inst->copy_range = min_t(unsigned int,
226 range, NFULNL_COPY_RANGE_MAX);
Harald Welte0597f262005-08-09 19:58:39 -0700227 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800228
Harald Welte0597f262005-08-09 19:58:39 -0700229 default:
230 status = -EINVAL;
231 break;
232 }
233
234 spin_unlock_bh(&inst->lock);
235
236 return status;
237}
238
239static int
240nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
241{
242 int status;
243
244 spin_lock_bh(&inst->lock);
245 if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
246 status = -ERANGE;
247 else if (nlbufsiz > 131072)
248 status = -ERANGE;
249 else {
250 inst->nlbufsiz = nlbufsiz;
251 status = 0;
252 }
253 spin_unlock_bh(&inst->lock);
254
255 return status;
256}
257
258static int
259nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
260{
261 spin_lock_bh(&inst->lock);
262 inst->flushtimeout = timeout;
263 spin_unlock_bh(&inst->lock);
264
265 return 0;
266}
267
268static int
269nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
270{
271 spin_lock_bh(&inst->lock);
272 inst->qthreshold = qthresh;
273 spin_unlock_bh(&inst->lock);
274
275 return 0;
276}
277
Harald Welte0af5f6c2006-03-20 17:15:11 -0800278static int
279nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
280{
281 spin_lock_bh(&inst->lock);
Patrick McHardyee433532006-05-19 02:17:18 -0700282 inst->flags = flags;
Harald Welte0af5f6c2006-03-20 17:15:11 -0800283 spin_unlock_bh(&inst->lock);
284
285 return 0;
286}
287
Michal Miroslawc6a8f642007-09-28 14:45:06 -0700288static struct sk_buff *
289nfulnl_alloc_skb(unsigned int inst_size, unsigned int pkt_size)
Harald Welte0597f262005-08-09 19:58:39 -0700290{
291 struct sk_buff *skb;
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800292 unsigned int n;
Harald Welte0597f262005-08-09 19:58:39 -0700293
Harald Welte0597f262005-08-09 19:58:39 -0700294 /* alloc skb which should be big enough for a whole multipart
295 * message. WARNING: has to be <= 128k due to slab restrictions */
296
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800297 n = max(inst_size, pkt_size);
298 skb = alloc_skb(n, GFP_ATOMIC);
Harald Welte0597f262005-08-09 19:58:39 -0700299 if (!skb) {
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200300 pr_notice("nfnetlink_log: can't alloc whole buffer (%u bytes)\n",
Harald Welte0597f262005-08-09 19:58:39 -0700301 inst_size);
302
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800303 if (n > pkt_size) {
304 /* try to allocate only as much as we need for current
305 * packet */
Harald Welte0597f262005-08-09 19:58:39 -0700306
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800307 skb = alloc_skb(pkt_size, GFP_ATOMIC);
308 if (!skb)
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200309 pr_err("nfnetlink_log: can't even alloc %u "
Patrick McHardyad2ad0f2006-02-04 02:13:57 -0800310 "bytes\n", pkt_size);
311 }
Harald Welte0597f262005-08-09 19:58:39 -0700312 }
313
314 return skb;
315}
316
317static int
318__nfulnl_send(struct nfulnl_instance *inst)
319{
Eric Leblond29c5d4a2007-09-18 13:07:15 -0700320 int status = -1;
Harald Welte0597f262005-08-09 19:58:39 -0700321
Harald Welte0597f262005-08-09 19:58:39 -0700322 if (inst->qlen > 1)
Eric Leblond29c5d4a2007-09-18 13:07:15 -0700323 NLMSG_PUT(inst->skb, 0, 0,
324 NLMSG_DONE,
325 sizeof(struct nfgenmsg));
Harald Welte0597f262005-08-09 19:58:39 -0700326
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100327 status = nfnetlink_unicast(inst->skb, &init_net, inst->peer_pid,
328 MSG_DONTWAIT);
Harald Welte0597f262005-08-09 19:58:39 -0700329
330 inst->qlen = 0;
331 inst->skb = NULL;
Harald Welte0597f262005-08-09 19:58:39 -0700332
Eric Leblond29c5d4a2007-09-18 13:07:15 -0700333nlmsg_failure:
Harald Welte0597f262005-08-09 19:58:39 -0700334 return status;
335}
336
Michal Miroslawe3567062007-09-28 14:44:21 -0700337static void
338__nfulnl_flush(struct nfulnl_instance *inst)
339{
340 /* timer holds a reference */
341 if (del_timer(&inst->timer))
342 instance_put(inst);
343 if (inst->skb)
344 __nfulnl_send(inst);
345}
346
Michal Miroslawc6a8f642007-09-28 14:45:06 -0700347static void
348nfulnl_timer(unsigned long data)
Harald Welte0597f262005-08-09 19:58:39 -0700349{
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800350 struct nfulnl_instance *inst = (struct nfulnl_instance *)data;
Harald Welte0597f262005-08-09 19:58:39 -0700351
Harald Welte0597f262005-08-09 19:58:39 -0700352 spin_lock_bh(&inst->lock);
Michal Miroslaw370e6a82007-03-23 11:12:21 -0700353 if (inst->skb)
354 __nfulnl_send(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700355 spin_unlock_bh(&inst->lock);
Michal Miroslaw05f7b7b2007-03-04 15:58:40 -0800356 instance_put(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700357}
358
Harald Welte0af5f6c2006-03-20 17:15:11 -0800359/* This is an inline function, we don't really care about a long
360 * list of arguments */
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800361static inline int
Harald Welte0597f262005-08-09 19:58:39 -0700362__build_packet_message(struct nfulnl_instance *inst,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800363 const struct sk_buff *skb,
Harald Welte0597f262005-08-09 19:58:39 -0700364 unsigned int data_len,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200365 u_int8_t pf,
Harald Welte0597f262005-08-09 19:58:39 -0700366 unsigned int hooknum,
367 const struct net_device *indev,
368 const struct net_device *outdev,
369 const struct nf_loginfo *li,
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100370 const char *prefix, unsigned int plen)
Harald Welte0597f262005-08-09 19:58:39 -0700371{
Harald Welte0597f262005-08-09 19:58:39 -0700372 struct nfulnl_msg_packet_hdr pmsg;
373 struct nlmsghdr *nlh;
374 struct nfgenmsg *nfmsg;
Al Viro98a4a862006-11-08 00:26:51 -0800375 __be32 tmp_uint;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700376 sk_buff_data_t old_tail = inst->skb->tail;
Harald Welte0597f262005-08-09 19:58:39 -0700377
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800378 nlh = NLMSG_PUT(inst->skb, 0, 0,
Harald Welte0597f262005-08-09 19:58:39 -0700379 NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET,
380 sizeof(struct nfgenmsg));
381 nfmsg = NLMSG_DATA(nlh);
382 nfmsg->nfgen_family = pf;
383 nfmsg->version = NFNETLINK_V0;
384 nfmsg->res_id = htons(inst->group_num);
385
Al Virofebf0a42006-11-03 00:59:17 -0800386 pmsg.hw_protocol = skb->protocol;
Harald Welte0597f262005-08-09 19:58:39 -0700387 pmsg.hook = hooknum;
388
Patrick McHardydf6fb862007-09-28 14:37:03 -0700389 NLA_PUT(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg);
Harald Welte0597f262005-08-09 19:58:39 -0700390
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100391 if (prefix)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700392 NLA_PUT(inst->skb, NFULA_PREFIX, plen, prefix);
Harald Welte0597f262005-08-09 19:58:39 -0700393
394 if (indev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700395#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800396 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
397 htonl(indev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700398#else
399 if (pf == PF_BRIDGE) {
400 /* Case 1: outdev is physical input device, we need to
401 * look for bridge group (when called from
402 * netfilter_bridge) */
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800403 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
404 htonl(indev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700405 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000406 /* rcu_read_lock()ed by nf_hook_slow or nf_log_packet */
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800407 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000408 htonl(br_port_get_rcu(indev)->br->dev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700409 } else {
410 /* Case 2: indev is bridge group, we need to look for
411 * physical device (when called from ipv4) */
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800412 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
413 htonl(indev->ifindex));
414 if (skb->nf_bridge && skb->nf_bridge->physindev)
415 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
416 htonl(skb->nf_bridge->physindev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700417 }
418#endif
Harald Welte0597f262005-08-09 19:58:39 -0700419 }
420
421 if (outdev) {
422 tmp_uint = htonl(outdev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700423#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800424 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
425 htonl(outdev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700426#else
427 if (pf == PF_BRIDGE) {
428 /* Case 1: outdev is physical output device, we need to
429 * look for bridge group (when called from
430 * netfilter_bridge) */
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800431 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
432 htonl(outdev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700433 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000434 /* rcu_read_lock()ed by nf_hook_slow or nf_log_packet */
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800435 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000436 htonl(br_port_get_rcu(outdev)->br->dev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700437 } else {
438 /* Case 2: indev is a bridge group, we need to look
439 * for physical device (when called from ipv4) */
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800440 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
441 htonl(outdev->ifindex));
442 if (skb->nf_bridge && skb->nf_bridge->physoutdev)
443 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
444 htonl(skb->nf_bridge->physoutdev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700445 }
446#endif
Harald Welte0597f262005-08-09 19:58:39 -0700447 }
448
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800449 if (skb->mark)
450 NLA_PUT_BE32(inst->skb, NFULA_MARK, htonl(skb->mark));
Harald Welte0597f262005-08-09 19:58:39 -0700451
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700452 if (indev && skb->dev) {
Harald Welte0597f262005-08-09 19:58:39 -0700453 struct nfulnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700454 int len = dev_parse_header(skb, phw.hw_addr);
455 if (len > 0) {
456 phw.hw_addrlen = htons(len);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700457 NLA_PUT(inst->skb, NFULA_HWADDR, sizeof(phw), &phw);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700458 }
Harald Welte0597f262005-08-09 19:58:39 -0700459 }
460
Eric Leblond72961ec2008-07-21 10:02:35 -0700461 if (indev && skb_mac_header_was_set(skb)) {
462 NLA_PUT_BE16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type));
463 NLA_PUT_BE16(inst->skb, NFULA_HWLEN,
464 htons(skb->dev->hard_header_len));
465 NLA_PUT(inst->skb, NFULA_HWHEADER, skb->dev->hard_header_len,
466 skb_mac_header(skb));
467 }
468
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700469 if (skb->tstamp.tv64) {
Harald Welte0597f262005-08-09 19:58:39 -0700470 struct nfulnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700471 struct timeval tv = ktime_to_timeval(skb->tstamp);
472 ts.sec = cpu_to_be64(tv.tv_sec);
473 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte0597f262005-08-09 19:58:39 -0700474
Patrick McHardydf6fb862007-09-28 14:37:03 -0700475 NLA_PUT(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts);
Harald Welte0597f262005-08-09 19:58:39 -0700476 }
477
478 /* UID */
479 if (skb->sk) {
480 read_lock_bh(&skb->sk->sk_callback_lock);
481 if (skb->sk->sk_socket && skb->sk->sk_socket->file) {
David Howellsd76b0d92008-11-14 10:39:25 +1100482 struct file *file = skb->sk->sk_socket->file;
483 __be32 uid = htonl(file->f_cred->fsuid);
484 __be32 gid = htonl(file->f_cred->fsgid);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700485 /* need to unlock here since NLA_PUT may goto */
Harald Welte0597f262005-08-09 19:58:39 -0700486 read_unlock_bh(&skb->sk->sk_callback_lock);
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800487 NLA_PUT_BE32(inst->skb, NFULA_UID, uid);
Patrick McHardy76aa1ce2007-12-17 22:41:52 -0800488 NLA_PUT_BE32(inst->skb, NFULA_GID, gid);
Harald Welte0597f262005-08-09 19:58:39 -0700489 } else
490 read_unlock_bh(&skb->sk->sk_callback_lock);
491 }
492
Harald Welte0af5f6c2006-03-20 17:15:11 -0800493 /* local sequence number */
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800494 if (inst->flags & NFULNL_CFG_F_SEQ)
495 NLA_PUT_BE32(inst->skb, NFULA_SEQ, htonl(inst->seq++));
496
Harald Welte0af5f6c2006-03-20 17:15:11 -0800497 /* global sequence number */
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800498 if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
499 NLA_PUT_BE32(inst->skb, NFULA_SEQ_GLOBAL,
500 htonl(atomic_inc_return(&global_seq)));
Harald Welte0af5f6c2006-03-20 17:15:11 -0800501
Harald Welte0597f262005-08-09 19:58:39 -0700502 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700503 struct nlattr *nla;
504 int size = nla_attr_size(data_len);
Harald Welte0597f262005-08-09 19:58:39 -0700505
Patrick McHardydf6fb862007-09-28 14:37:03 -0700506 if (skb_tailroom(inst->skb) < nla_total_size(data_len)) {
Harald Welte0597f262005-08-09 19:58:39 -0700507 printk(KERN_WARNING "nfnetlink_log: no tailroom!\n");
508 goto nlmsg_failure;
509 }
510
Patrick McHardydf6fb862007-09-28 14:37:03 -0700511 nla = (struct nlattr *)skb_put(inst->skb, nla_total_size(data_len));
512 nla->nla_type = NFULA_PAYLOAD;
513 nla->nla_len = size;
Harald Welte0597f262005-08-09 19:58:39 -0700514
Patrick McHardydf6fb862007-09-28 14:37:03 -0700515 if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
Harald Welte0597f262005-08-09 19:58:39 -0700516 BUG();
517 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800518
Harald Welte0597f262005-08-09 19:58:39 -0700519 nlh->nlmsg_len = inst->skb->tail - old_tail;
520 return 0;
521
522nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -0700523nla_put_failure:
Harald Welte0597f262005-08-09 19:58:39 -0700524 PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
525 return -1;
526}
527
528#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
529
530static struct nf_loginfo default_loginfo = {
531 .type = NF_LOG_TYPE_ULOG,
532 .u = {
533 .ulog = {
534 .copy_len = 0xffff,
535 .group = 0,
536 .qthreshold = 1,
537 },
538 },
539};
540
541/* log handler for internal netfilter logging api */
Eric Leblond5f7340e2008-11-04 14:21:08 +0100542void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200543nfulnl_log_packet(u_int8_t pf,
Harald Welte0597f262005-08-09 19:58:39 -0700544 unsigned int hooknum,
545 const struct sk_buff *skb,
546 const struct net_device *in,
547 const struct net_device *out,
548 const struct nf_loginfo *li_user,
549 const char *prefix)
550{
551 unsigned int size, data_len;
552 struct nfulnl_instance *inst;
553 const struct nf_loginfo *li;
554 unsigned int qthreshold;
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100555 unsigned int plen;
Harald Welte0597f262005-08-09 19:58:39 -0700556
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800557 if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
Harald Welte0597f262005-08-09 19:58:39 -0700558 li = li_user;
559 else
560 li = &default_loginfo;
561
562 inst = instance_lookup_get(li->u.ulog.group);
563 if (!inst)
Harald Welte0597f262005-08-09 19:58:39 -0700564 return;
Harald Welte0597f262005-08-09 19:58:39 -0700565
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100566 plen = 0;
567 if (prefix)
Patrick McHardy881dbfe2007-03-06 20:24:35 -0800568 plen = strlen(prefix) + 1;
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100569
Harald Welte0597f262005-08-09 19:58:39 -0700570 /* FIXME: do we want to make the size calculation conditional based on
571 * what is actually present? way more branches and checks, but more
572 * memory efficient... */
Eric Leblond7000d382008-03-10 16:42:04 -0700573 size = NLMSG_SPACE(sizeof(struct nfgenmsg))
Patrick McHardydf6fb862007-09-28 14:37:03 -0700574 + nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
575 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
576 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700577#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700578 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
579 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700580#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700581 + nla_total_size(sizeof(u_int32_t)) /* mark */
582 + nla_total_size(sizeof(u_int32_t)) /* uid */
Patrick McHardy76aa1ce2007-12-17 22:41:52 -0800583 + nla_total_size(sizeof(u_int32_t)) /* gid */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700584 + nla_total_size(plen) /* prefix */
585 + nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
586 + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp));
Harald Welte0597f262005-08-09 19:58:39 -0700587
Pablo Neira Ayusoeeff9be2009-05-27 15:49:11 +0200588 if (in && skb_mac_header_was_set(skb)) {
589 size += nla_total_size(skb->dev->hard_header_len)
590 + nla_total_size(sizeof(u_int16_t)) /* hwtype */
591 + nla_total_size(sizeof(u_int16_t)); /* hwlen */
592 }
593
Harald Welte0597f262005-08-09 19:58:39 -0700594 spin_lock_bh(&inst->lock);
595
Harald Welte0af5f6c2006-03-20 17:15:11 -0800596 if (inst->flags & NFULNL_CFG_F_SEQ)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700597 size += nla_total_size(sizeof(u_int32_t));
Harald Welte0af5f6c2006-03-20 17:15:11 -0800598 if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700599 size += nla_total_size(sizeof(u_int32_t));
Harald Welte0af5f6c2006-03-20 17:15:11 -0800600
Harald Welte0597f262005-08-09 19:58:39 -0700601 qthreshold = inst->qthreshold;
602 /* per-rule qthreshold overrides per-instance */
Eric Leblond5ca431f2009-02-18 15:29:23 +0100603 if (li->u.ulog.qthreshold)
604 if (qthreshold > li->u.ulog.qthreshold)
605 qthreshold = li->u.ulog.qthreshold;
606
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800607
Harald Welte0597f262005-08-09 19:58:39 -0700608 switch (inst->copy_mode) {
609 case NFULNL_COPY_META:
610 case NFULNL_COPY_NONE:
611 data_len = 0;
612 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800613
Harald Welte0597f262005-08-09 19:58:39 -0700614 case NFULNL_COPY_PACKET:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800615 if (inst->copy_range == 0
Harald Welte0597f262005-08-09 19:58:39 -0700616 || inst->copy_range > skb->len)
617 data_len = skb->len;
618 else
619 data_len = inst->copy_range;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800620
Patrick McHardydf6fb862007-09-28 14:37:03 -0700621 size += nla_total_size(data_len);
Harald Welte0597f262005-08-09 19:58:39 -0700622 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800623
Harald Welte0597f262005-08-09 19:58:39 -0700624 default:
Michal Miroslaw55b5a912007-03-23 11:11:05 -0700625 goto unlock_and_release;
Harald Welte0597f262005-08-09 19:58:39 -0700626 }
627
Michal Miroslawd63b0432007-09-28 14:44:44 -0700628 if (inst->skb &&
629 size > skb_tailroom(inst->skb) - sizeof(struct nfgenmsg)) {
Harald Welte0597f262005-08-09 19:58:39 -0700630 /* either the queue len is too high or we don't have
631 * enough room in the skb left. flush to userspace. */
Michal Miroslawe3567062007-09-28 14:44:21 -0700632 __nfulnl_flush(inst);
Michal Miroslaw55b5a912007-03-23 11:11:05 -0700633 }
Harald Welte0597f262005-08-09 19:58:39 -0700634
Michal Miroslaw55b5a912007-03-23 11:11:05 -0700635 if (!inst->skb) {
636 inst->skb = nfulnl_alloc_skb(inst->nlbufsiz, size);
637 if (!inst->skb)
Harald Welte0597f262005-08-09 19:58:39 -0700638 goto alloc_failure;
Harald Welte0597f262005-08-09 19:58:39 -0700639 }
640
Harald Welte0597f262005-08-09 19:58:39 -0700641 inst->qlen++;
642
643 __build_packet_message(inst, skb, data_len, pf,
Patrick McHardyd7a5c322006-11-29 02:35:34 +0100644 hooknum, in, out, li, prefix, plen);
Harald Welte0597f262005-08-09 19:58:39 -0700645
Michal Miroslawd63b0432007-09-28 14:44:44 -0700646 if (inst->qlen >= qthreshold)
647 __nfulnl_flush(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700648 /* timer_pending always called within inst->lock, so there
649 * is no chance of a race here */
Michal Miroslawd63b0432007-09-28 14:44:44 -0700650 else if (!timer_pending(&inst->timer)) {
Harald Welte0597f262005-08-09 19:58:39 -0700651 instance_get(inst);
652 inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
653 add_timer(&inst->timer);
654 }
Harald Welte0597f262005-08-09 19:58:39 -0700655
Michal Miroslawed32abe2007-03-04 15:58:15 -0800656unlock_and_release:
657 spin_unlock_bh(&inst->lock);
658 instance_put(inst);
Harald Welte0597f262005-08-09 19:58:39 -0700659 return;
660
661alloc_failure:
Harald Welte0597f262005-08-09 19:58:39 -0700662 /* FIXME: statistics */
Michal Miroslawed32abe2007-03-04 15:58:15 -0800663 goto unlock_and_release;
Harald Welte0597f262005-08-09 19:58:39 -0700664}
Eric Leblond5f7340e2008-11-04 14:21:08 +0100665EXPORT_SYMBOL_GPL(nfulnl_log_packet);
Harald Welte0597f262005-08-09 19:58:39 -0700666
667static int
668nfulnl_rcv_nl_event(struct notifier_block *this,
669 unsigned long event, void *ptr)
670{
671 struct netlink_notify *n = ptr;
672
Patrick McHardydee58172009-11-06 17:04:00 +0100673 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
Harald Welte0597f262005-08-09 19:58:39 -0700674 int i;
675
676 /* destroy all instances for this pid */
677 write_lock_bh(&instances_lock);
678 for (i = 0; i < INSTANCE_BUCKETS; i++) {
679 struct hlist_node *tmp, *t2;
680 struct nfulnl_instance *inst;
681 struct hlist_head *head = &instance_table[i];
682
683 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800684 if ((net_eq(n->net, &init_net)) &&
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200685 (n->pid == inst->peer_pid))
Harald Welte0597f262005-08-09 19:58:39 -0700686 __instance_destroy(inst);
687 }
688 }
689 write_unlock_bh(&instances_lock);
690 }
691 return NOTIFY_DONE;
692}
693
694static struct notifier_block nfulnl_rtnl_notifier = {
695 .notifier_call = nfulnl_rcv_nl_event,
696};
697
698static int
699nfulnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200700 const struct nlmsghdr *nlh,
701 const struct nlattr * const nfqa[])
Harald Welte0597f262005-08-09 19:58:39 -0700702{
703 return -ENOTSUPP;
704}
705
Eric Leblondca735b32009-03-16 14:54:21 +0100706static struct nf_logger nfulnl_logger __read_mostly = {
Harald Welte0597f262005-08-09 19:58:39 -0700707 .name = "nfnetlink_log",
708 .logfn = &nfulnl_log_packet,
709 .me = THIS_MODULE,
710};
711
Patrick McHardyfd8281a2007-09-28 14:39:09 -0700712static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
713 [NFULA_CFG_CMD] = { .len = sizeof(struct nfulnl_msg_config_cmd) },
714 [NFULA_CFG_MODE] = { .len = sizeof(struct nfulnl_msg_config_mode) },
715 [NFULA_CFG_TIMEOUT] = { .type = NLA_U32 },
716 [NFULA_CFG_QTHRESH] = { .type = NLA_U32 },
717 [NFULA_CFG_NLBUFSIZ] = { .type = NLA_U32 },
718 [NFULA_CFG_FLAGS] = { .type = NLA_U16 },
Harald Welte0597f262005-08-09 19:58:39 -0700719};
720
721static int
722nfulnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200723 const struct nlmsghdr *nlh,
724 const struct nlattr * const nfula[])
Harald Welte0597f262005-08-09 19:58:39 -0700725{
726 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
727 u_int16_t group_num = ntohs(nfmsg->res_id);
728 struct nfulnl_instance *inst;
Patrick McHardyb7047a12008-03-10 16:44:13 -0700729 struct nfulnl_msg_config_cmd *cmd = NULL;
Harald Welte0597f262005-08-09 19:58:39 -0700730 int ret = 0;
731
Patrick McHardyb7047a12008-03-10 16:44:13 -0700732 if (nfula[NFULA_CFG_CMD]) {
733 u_int8_t pf = nfmsg->nfgen_family;
734 cmd = nla_data(nfula[NFULA_CFG_CMD]);
735
736 /* Commands without queue context */
737 switch (cmd->command) {
738 case NFULNL_CFG_CMD_PF_BIND:
Eric Leblondca735b32009-03-16 14:54:21 +0100739 return nf_log_bind_pf(pf, &nfulnl_logger);
Patrick McHardyb7047a12008-03-10 16:44:13 -0700740 case NFULNL_CFG_CMD_PF_UNBIND:
Eric Leblondca735b32009-03-16 14:54:21 +0100741 nf_log_unbind_pf(pf);
Patrick McHardyb7047a12008-03-10 16:44:13 -0700742 return 0;
743 }
744 }
745
Harald Welte0597f262005-08-09 19:58:39 -0700746 inst = instance_lookup_get(group_num);
Patrick McHardyc05063652007-12-17 22:39:55 -0800747 if (inst && inst->peer_pid != NETLINK_CB(skb).pid) {
748 ret = -EPERM;
749 goto out_put;
750 }
751
Patrick McHardyb7047a12008-03-10 16:44:13 -0700752 if (cmd != NULL) {
Harald Welte0597f262005-08-09 19:58:39 -0700753 switch (cmd->command) {
754 case NFULNL_CFG_CMD_BIND:
755 if (inst) {
756 ret = -EBUSY;
757 goto out_put;
758 }
759
760 inst = instance_create(group_num,
761 NETLINK_CB(skb).pid);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800762 if (IS_ERR(inst)) {
763 ret = PTR_ERR(inst);
Michal Miroslawf414c162007-03-23 11:11:31 -0700764 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700765 }
766 break;
767 case NFULNL_CFG_CMD_UNBIND:
768 if (!inst) {
769 ret = -ENODEV;
Michal Miroslawf414c162007-03-23 11:11:31 -0700770 goto out;
Harald Welte0597f262005-08-09 19:58:39 -0700771 }
772
Harald Welte0597f262005-08-09 19:58:39 -0700773 instance_destroy(inst);
Alexey Dobriyana49c6502010-02-26 17:48:40 +0100774 goto out_put;
Harald Welte0597f262005-08-09 19:58:39 -0700775 default:
Patrick McHardycd21f0a2007-12-17 22:40:19 -0800776 ret = -ENOTSUPP;
Harald Welte0597f262005-08-09 19:58:39 -0700777 break;
778 }
Harald Welte0597f262005-08-09 19:58:39 -0700779 }
780
Patrick McHardydf6fb862007-09-28 14:37:03 -0700781 if (nfula[NFULA_CFG_MODE]) {
Harald Welte0597f262005-08-09 19:58:39 -0700782 struct nfulnl_msg_config_mode *params;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700783 params = nla_data(nfula[NFULA_CFG_MODE]);
Harald Welte0597f262005-08-09 19:58:39 -0700784
Patrick McHardyc05063652007-12-17 22:39:55 -0800785 if (!inst) {
786 ret = -ENODEV;
787 goto out;
788 }
Harald Welte0597f262005-08-09 19:58:39 -0700789 nfulnl_set_mode(inst, params->copy_mode,
Al Virod1208b92006-11-03 00:58:41 -0800790 ntohl(params->copy_range));
Harald Welte0597f262005-08-09 19:58:39 -0700791 }
792
Patrick McHardydf6fb862007-09-28 14:37:03 -0700793 if (nfula[NFULA_CFG_TIMEOUT]) {
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800794 __be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
Harald Welte0597f262005-08-09 19:58:39 -0700795
Patrick McHardyc05063652007-12-17 22:39:55 -0800796 if (!inst) {
797 ret = -ENODEV;
798 goto out;
799 }
Harald Welte0597f262005-08-09 19:58:39 -0700800 nfulnl_set_timeout(inst, ntohl(timeout));
801 }
802
Patrick McHardydf6fb862007-09-28 14:37:03 -0700803 if (nfula[NFULA_CFG_NLBUFSIZ]) {
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800804 __be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
Harald Welte0597f262005-08-09 19:58:39 -0700805
Patrick McHardyc05063652007-12-17 22:39:55 -0800806 if (!inst) {
807 ret = -ENODEV;
808 goto out;
809 }
Harald Welte0597f262005-08-09 19:58:39 -0700810 nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
811 }
812
Patrick McHardydf6fb862007-09-28 14:37:03 -0700813 if (nfula[NFULA_CFG_QTHRESH]) {
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800814 __be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
Harald Welte0597f262005-08-09 19:58:39 -0700815
Patrick McHardyc05063652007-12-17 22:39:55 -0800816 if (!inst) {
817 ret = -ENODEV;
818 goto out;
819 }
Harald Welte0597f262005-08-09 19:58:39 -0700820 nfulnl_set_qthresh(inst, ntohl(qthresh));
821 }
822
Patrick McHardydf6fb862007-09-28 14:37:03 -0700823 if (nfula[NFULA_CFG_FLAGS]) {
Patrick McHardy0dfedd22007-12-17 22:41:35 -0800824 __be16 flags = nla_get_be16(nfula[NFULA_CFG_FLAGS]);
Patrick McHardyc05063652007-12-17 22:39:55 -0800825
826 if (!inst) {
827 ret = -ENODEV;
828 goto out;
829 }
Patrick McHardyee433532006-05-19 02:17:18 -0700830 nfulnl_set_flags(inst, ntohs(flags));
Harald Welte0af5f6c2006-03-20 17:15:11 -0800831 }
832
Harald Welte0597f262005-08-09 19:58:39 -0700833out_put:
834 instance_put(inst);
Michal Miroslawdd167042007-03-04 15:59:20 -0800835out:
Harald Welte0597f262005-08-09 19:58:39 -0700836 return ret;
837}
838
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700839static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
Harald Welte0597f262005-08-09 19:58:39 -0700840 [NFULNL_MSG_PACKET] = { .call = nfulnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800841 .attr_count = NFULA_MAX, },
Harald Welte0597f262005-08-09 19:58:39 -0700842 [NFULNL_MSG_CONFIG] = { .call = nfulnl_recv_config,
Patrick McHardyfd8281a2007-09-28 14:39:09 -0700843 .attr_count = NFULA_CFG_MAX,
844 .policy = nfula_cfg_policy },
Harald Welte0597f262005-08-09 19:58:39 -0700845};
846
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700847static const struct nfnetlink_subsystem nfulnl_subsys = {
Harald Welte0597f262005-08-09 19:58:39 -0700848 .name = "log",
849 .subsys_id = NFNL_SUBSYS_ULOG,
850 .cb_count = NFULNL_MSG_MAX,
Harald Welte0597f262005-08-09 19:58:39 -0700851 .cb = nfulnl_cb,
852};
853
854#ifdef CONFIG_PROC_FS
855struct iter_state {
856 unsigned int bucket;
857};
858
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700859static struct hlist_node *get_first(struct iter_state *st)
Harald Welte0597f262005-08-09 19:58:39 -0700860{
Harald Welte0597f262005-08-09 19:58:39 -0700861 if (!st)
862 return NULL;
863
864 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
865 if (!hlist_empty(&instance_table[st->bucket]))
866 return instance_table[st->bucket].first;
867 }
868 return NULL;
869}
870
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700871static struct hlist_node *get_next(struct iter_state *st, struct hlist_node *h)
Harald Welte0597f262005-08-09 19:58:39 -0700872{
Harald Welte0597f262005-08-09 19:58:39 -0700873 h = h->next;
874 while (!h) {
875 if (++st->bucket >= INSTANCE_BUCKETS)
876 return NULL;
877
878 h = instance_table[st->bucket].first;
879 }
880 return h;
881}
882
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700883static struct hlist_node *get_idx(struct iter_state *st, loff_t pos)
Harald Welte0597f262005-08-09 19:58:39 -0700884{
885 struct hlist_node *head;
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700886 head = get_first(st);
Harald Welte0597f262005-08-09 19:58:39 -0700887
888 if (head)
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700889 while (pos && (head = get_next(st, head)))
Harald Welte0597f262005-08-09 19:58:39 -0700890 pos--;
891 return pos ? NULL : head;
892}
893
894static void *seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemminger4e26fe22008-01-31 04:07:51 -0800895 __acquires(instances_lock)
Harald Welte0597f262005-08-09 19:58:39 -0700896{
897 read_lock_bh(&instances_lock);
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700898 return get_idx(seq->private, *pos);
Harald Welte0597f262005-08-09 19:58:39 -0700899}
900
901static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
902{
903 (*pos)++;
Michal Miroslawf76cdce2007-03-23 11:12:03 -0700904 return get_next(s->private, v);
Harald Welte0597f262005-08-09 19:58:39 -0700905}
906
907static void seq_stop(struct seq_file *s, void *v)
Stephen Hemminger4e26fe22008-01-31 04:07:51 -0800908 __releases(instances_lock)
Harald Welte0597f262005-08-09 19:58:39 -0700909{
910 read_unlock_bh(&instances_lock);
911}
912
913static int seq_show(struct seq_file *s, void *v)
914{
915 const struct nfulnl_instance *inst = v;
916
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800917 return seq_printf(s, "%5d %6d %5d %1d %5d %6d %2d\n",
Harald Welte0597f262005-08-09 19:58:39 -0700918 inst->group_num,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800919 inst->peer_pid, inst->qlen,
Harald Welte0597f262005-08-09 19:58:39 -0700920 inst->copy_mode, inst->copy_range,
921 inst->flushtimeout, atomic_read(&inst->use));
922}
923
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700924static const struct seq_operations nful_seq_ops = {
Harald Welte0597f262005-08-09 19:58:39 -0700925 .start = seq_start,
926 .next = seq_next,
927 .stop = seq_stop,
928 .show = seq_show,
929};
930
931static int nful_open(struct inode *inode, struct file *file)
932{
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700933 return seq_open_private(file, &nful_seq_ops,
934 sizeof(struct iter_state));
Harald Welte0597f262005-08-09 19:58:39 -0700935}
936
Arjan van de Venda7071d2007-02-12 00:55:36 -0800937static const struct file_operations nful_file_ops = {
Harald Welte0597f262005-08-09 19:58:39 -0700938 .owner = THIS_MODULE,
939 .open = nful_open,
940 .read = seq_read,
941 .llseek = seq_lseek,
942 .release = seq_release_private,
943};
944
945#endif /* PROC_FS */
946
Patrick McHardy32292a72006-04-06 14:11:30 -0700947static int __init nfnetlink_log_init(void)
Harald Welte0597f262005-08-09 19:58:39 -0700948{
949 int i, status = -ENOMEM;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800950
Harald Welte0597f262005-08-09 19:58:39 -0700951 for (i = 0; i < INSTANCE_BUCKETS; i++)
952 INIT_HLIST_HEAD(&instance_table[i]);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800953
Harald Welte0597f262005-08-09 19:58:39 -0700954 /* it's not really all that important to have a random value, so
955 * we can do this from the init function, even if there hasn't
956 * been that much entropy yet */
957 get_random_bytes(&hash_init, sizeof(hash_init));
958
959 netlink_register_notifier(&nfulnl_rtnl_notifier);
960 status = nfnetlink_subsys_register(&nfulnl_subsys);
961 if (status < 0) {
962 printk(KERN_ERR "log: failed to create netlink socket\n");
963 goto cleanup_netlink_notifier;
964 }
965
Eric Leblondca735b32009-03-16 14:54:21 +0100966 status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
967 if (status < 0) {
968 printk(KERN_ERR "log: failed to register logger\n");
969 goto cleanup_subsys;
970 }
971
Harald Welte0597f262005-08-09 19:58:39 -0700972#ifdef CONFIG_PROC_FS
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -0700973 if (!proc_create("nfnetlink_log", 0440,
974 proc_net_netfilter, &nful_file_ops))
Eric Leblondca735b32009-03-16 14:54:21 +0100975 goto cleanup_logger;
Harald Welte0597f262005-08-09 19:58:39 -0700976#endif
Harald Welte0597f262005-08-09 19:58:39 -0700977 return status;
978
Harald Welte0597f262005-08-09 19:58:39 -0700979#ifdef CONFIG_PROC_FS
Eric Leblondca735b32009-03-16 14:54:21 +0100980cleanup_logger:
981 nf_log_unregister(&nfulnl_logger);
982#endif
Harald Welte0597f262005-08-09 19:58:39 -0700983cleanup_subsys:
Harald Welte0597f262005-08-09 19:58:39 -0700984 nfnetlink_subsys_unregister(&nfulnl_subsys);
985cleanup_netlink_notifier:
986 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
987 return status;
988}
989
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800990static void __exit nfnetlink_log_fini(void)
Harald Welte0597f262005-08-09 19:58:39 -0700991{
Patrick McHardye92ad992007-02-12 11:11:55 -0800992 nf_log_unregister(&nfulnl_logger);
Patrick McHardy32292a72006-04-06 14:11:30 -0700993#ifdef CONFIG_PROC_FS
994 remove_proc_entry("nfnetlink_log", proc_net_netfilter);
995#endif
996 nfnetlink_subsys_unregister(&nfulnl_subsys);
997 netlink_unregister_notifier(&nfulnl_rtnl_notifier);
Harald Welte0597f262005-08-09 19:58:39 -0700998}
999
1000MODULE_DESCRIPTION("netfilter userspace logging");
1001MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1002MODULE_LICENSE("GPL");
Harald Weltef682fae2005-08-09 20:20:34 -07001003MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
Harald Welte0597f262005-08-09 19:58:39 -07001004
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001005module_init(nfnetlink_log_init);
1006module_exit(nfnetlink_log_fini);