14 #include <linux/module.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>
25 #include <linux/sysctl.h>
28 #include <linux/list.h>
30 #include <linux/random.h>
31 #include <linux/slab.h>
38 #ifdef CONFIG_BRIDGE_NETFILTER
39 #include "../bridge/br_private.h"
42 #define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
43 #define NFULNL_TIMEOUT_DEFAULT 100
44 #define NFULNL_QTHRESH_DEFAULT 100
45 #define NFULNL_COPY_RANGE_MAX 0xFFFF
47 #define PRINTR(x, args...) do { if (net_ratelimit()) \
48 printk(x, ## args); } while (0);
76 #define INSTANCE_BUCKETS 16
92 head = &instance_table[instance_hashfn(group_num)];
93 hlist_for_each_entry_rcu(inst, pos, head,
hlist) {
112 inst = __instance_lookup(group_num);
115 rcu_read_unlock_bh();
120 static void nfulnl_instance_free_rcu(
struct rcu_head *head)
133 static void nfulnl_timer(
unsigned long data);
141 spin_lock_bh(&instances_lock);
142 if (__instance_lookup(group_num)) {
159 INIT_HLIST_NODE(&inst->
hlist);
176 hlist_add_head_rcu(&inst->
hlist,
177 &instance_table[instance_hashfn(group_num)]);
179 spin_unlock_bh(&instances_lock);
184 spin_unlock_bh(&instances_lock);
195 hlist_del_rcu(&inst->
hlist);
199 spin_lock(&inst->
lock);
205 __nfulnl_flush(inst);
206 spin_unlock(&inst->
lock);
215 spin_lock_bh(&instances_lock);
216 __instance_destroy(inst);
217 spin_unlock_bh(&instances_lock);
226 spin_lock_bh(&inst->
lock);
246 spin_unlock_bh(&inst->
lock);
256 spin_lock_bh(&inst->
lock);
259 else if (nlbufsiz > 131072)
265 spin_unlock_bh(&inst->
lock);
273 spin_lock_bh(&inst->
lock);
275 spin_unlock_bh(&inst->
lock);
283 spin_lock_bh(&inst->
lock);
285 spin_unlock_bh(&inst->
lock);
293 spin_lock_bh(&inst->
lock);
295 spin_unlock_bh(&inst->
lock);
301 nfulnl_alloc_skb(
unsigned int inst_size,
unsigned int pkt_size)
309 n =
max(inst_size, pkt_size);
318 pr_err(
"nfnetlink_log: can't even alloc %u bytes\n",
331 if (inst->
qlen > 1) {
359 nfulnl_timer(
unsigned long data)
363 spin_lock_bh(&inst->
lock);
366 spin_unlock_bh(&inst->
lock);
388 nlh = nlmsg_put(inst->
skb, 0, 0,
393 nfmsg = nlmsg_data(nlh);
402 goto nla_put_failure;
406 goto nla_put_failure;
409 #ifndef CONFIG_BRIDGE_NETFILTER
412 goto nla_put_failure;
423 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
424 goto nla_put_failure;
430 goto nla_put_failure;
431 if (skb->nf_bridge && skb->nf_bridge->physindev &&
433 htonl(skb->nf_bridge->physindev->ifindex)))
434 goto nla_put_failure;
440 #ifndef CONFIG_BRIDGE_NETFILTER
443 goto nla_put_failure;
454 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
455 goto nla_put_failure;
461 goto nla_put_failure;
462 if (skb->nf_bridge && skb->nf_bridge->physoutdev &&
464 htonl(skb->nf_bridge->physoutdev->ifindex)))
465 goto nla_put_failure;
472 goto nla_put_failure;
474 if (indev && skb->
dev &&
477 int len = dev_parse_header(skb, phw.hw_addr);
479 phw.hw_addrlen =
htons(len);
481 goto nla_put_failure;
485 if (indev && skb_mac_header_was_set(skb)) {
488 htons(skb->
dev->hard_header_len)) ||
490 skb_mac_header(skb)))
491 goto nla_put_failure;
501 goto nla_put_failure;
517 goto nla_put_failure;
525 goto nla_put_failure;
531 goto nla_put_failure;
535 int size = nla_attr_size(data_len);
537 if (skb_tailroom(inst->
skb) < nla_total_size(data_len)) {
558 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
574 unsigned int hooknum,
590 li = &default_loginfo;
592 inst = instance_lookup_get(li->
u.
ulog.group);
598 plen =
strlen(prefix) + 1;
607 #ifdef CONFIG_BRIDGE_NETFILTER
614 + nla_total_size(plen)
618 if (in && skb_mac_header_was_set(skb)) {
619 size += nla_total_size(skb->
dev->hard_header_len)
624 spin_lock_bh(&inst->
lock);
627 size += nla_total_size(
sizeof(
u_int32_t));
629 size += nla_total_size(
sizeof(
u_int32_t));
633 if (li->
u.
ulog.qthreshold)
634 if (qthreshold > li->
u.
ulog.qthreshold)
635 qthreshold = li->
u.
ulog.qthreshold;
651 size += nla_total_size(data_len);
656 goto unlock_and_release;
660 size > skb_tailroom(inst->
skb) -
sizeof(
struct nfgenmsg)) {
663 __nfulnl_flush(inst);
667 inst->
skb = nfulnl_alloc_skb(inst->
nlbufsiz, size);
674 __build_packet_message(inst, skb, data_len, pf,
675 hooknum, in, out, prefix, plen);
677 if (inst->
qlen >= qthreshold)
678 __nfulnl_flush(inst);
681 else if (!timer_pending(&inst->
timer)) {
688 spin_unlock_bh(&inst->
lock);
694 goto unlock_and_release;
708 spin_lock_bh(&instances_lock);
717 __instance_destroy(inst);
720 spin_unlock_bh(&instances_lock);
726 .notifier_call = nfulnl_rcv_nl_event,
730 nfulnl_recv_unsupp(
struct sock *ctnl,
struct sk_buff *skb,
732 const struct nlattr *
const nfqa[])
738 .name =
"nfnetlink_log",
753 nfulnl_recv_config(
struct sock *ctnl,
struct sk_buff *skb,
755 const struct nlattr *
const nfula[])
757 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
765 cmd = nla_data(nfula[NFULA_CFG_CMD]);
777 inst = instance_lookup_get(group_num);
791 inst = instance_create(group_num,
805 instance_destroy(inst);
815 params = nla_data(nfula[NFULA_CFG_MODE]);
832 nfulnl_set_timeout(inst,
ntohl(timeout));
836 __be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
842 nfulnl_set_nlbufsiz(inst,
ntohl(nlbufsiz));
846 __be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
852 nfulnl_set_qthresh(inst,
ntohl(qthresh));
856 __be16 flags = nla_get_be16(nfula[NFULA_CFG_FLAGS]);
862 nfulnl_set_flags(inst,
ntohs(flags));
876 .policy = nfula_cfg_policy },
886 #ifdef CONFIG_PROC_FS
891 static struct hlist_node *get_first(
struct iter_state *
st)
896 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
897 if (!hlist_empty(&instance_table[st->bucket]))
907 if (++st->bucket >= INSTANCE_BUCKETS)
915 static struct hlist_node *get_idx(
struct iter_state *st, loff_t pos)
918 head = get_first(st);
921 while (pos && (head =
get_next(st, head)))
926 static void *seq_start(
struct seq_file *seq, loff_t *pos)
930 return get_idx(
seq->private, *pos);
933 static void *seq_next(
struct seq_file *
s,
void *
v, loff_t *pos)
939 static void seq_stop(
struct seq_file *
s,
void *
v)
942 rcu_read_unlock_bh();
945 static int seq_show(
struct seq_file *
s,
void *
v)
949 return seq_printf(s,
"%5d %6d %5d %1d %5d %6d %2d\n",
963 static int nful_open(
struct inode *
inode,
struct file *file)
966 sizeof(
struct iter_state));
979 static int __init nfnetlink_log_init(
void)
995 goto cleanup_netlink_notifier;
1001 goto cleanup_subsys;
1004 #ifdef CONFIG_PROC_FS
1005 if (!proc_create(
"nfnetlink_log", 0440,
1006 proc_net_netfilter, &nful_file_ops)) {
1008 goto cleanup_logger;
1013 #ifdef CONFIG_PROC_FS
1019 cleanup_netlink_notifier:
1024 static void __exit nfnetlink_log_fini(
void)
1027 #ifdef CONFIG_PROC_FS