[PATCH] w1: Userspace communication protocol over connector.
[linux-2.6/verdex.git] / net / netfilter / nf_log.c
blob3e76bd0824a25b7c31e60ea4a5d59e27745f12fc
1 #include <linux/config.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/proc_fs.h>
6 #include <linux/skbuff.h>
7 #include <linux/netfilter.h>
8 #include <linux/seq_file.h>
9 #include <net/protocol.h>
11 #include "nf_internals.h"
13 /* Internal logging interface, which relies on the real
14 LOG target modules */
16 #define NF_LOG_PREFIXLEN 128
18 static struct nf_logger *nf_logging[NPROTO]; /* = NULL */
19 static DEFINE_SPINLOCK(nf_log_lock);
21 /* return EBUSY if somebody else is registered, EEXIST if the same logger
22 * is registred, 0 on success. */
23 int nf_log_register(int pf, struct nf_logger *logger)
25 int ret = -EBUSY;
27 if (pf >= NPROTO)
28 return -EINVAL;
30 /* Any setup of logging members must be done before
31 * substituting pointer. */
32 spin_lock(&nf_log_lock);
33 if (!nf_logging[pf]) {
34 rcu_assign_pointer(nf_logging[pf], logger);
35 ret = 0;
36 } else if (nf_logging[pf] == logger)
37 ret = -EEXIST;
39 spin_unlock(&nf_log_lock);
40 return ret;
42 EXPORT_SYMBOL(nf_log_register);
44 int nf_log_unregister_pf(int pf)
46 if (pf >= NPROTO)
47 return -EINVAL;
49 spin_lock(&nf_log_lock);
50 nf_logging[pf] = NULL;
51 spin_unlock(&nf_log_lock);
53 /* Give time to concurrent readers. */
54 synchronize_net();
56 return 0;
58 EXPORT_SYMBOL(nf_log_unregister_pf);
60 void nf_log_unregister_logger(struct nf_logger *logger)
62 int i;
64 spin_lock(&nf_log_lock);
65 for (i = 0; i < NPROTO; i++) {
66 if (nf_logging[i] == logger)
67 nf_logging[i] = NULL;
69 spin_unlock(&nf_log_lock);
71 synchronize_net();
73 EXPORT_SYMBOL(nf_log_unregister_logger);
75 void nf_log_packet(int pf,
76 unsigned int hooknum,
77 const struct sk_buff *skb,
78 const struct net_device *in,
79 const struct net_device *out,
80 struct nf_loginfo *loginfo,
81 const char *fmt, ...)
83 va_list args;
84 char prefix[NF_LOG_PREFIXLEN];
85 struct nf_logger *logger;
87 rcu_read_lock();
88 logger = rcu_dereference(nf_logging[pf]);
89 if (logger) {
90 va_start(args, fmt);
91 vsnprintf(prefix, sizeof(prefix), fmt, args);
92 va_end(args);
93 /* We must read logging before nf_logfn[pf] */
94 logger->logfn(pf, hooknum, skb, in, out, loginfo, prefix);
95 } else if (net_ratelimit()) {
96 printk(KERN_WARNING "nf_log_packet: can\'t log since "
97 "no backend logging module loaded in! Please either "
98 "load one, or disable logging explicitly\n");
100 rcu_read_unlock();
102 EXPORT_SYMBOL(nf_log_packet);
104 #ifdef CONFIG_PROC_FS
105 static void *seq_start(struct seq_file *seq, loff_t *pos)
107 rcu_read_lock();
109 if (*pos >= NPROTO)
110 return NULL;
112 return pos;
115 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
117 (*pos)++;
119 if (*pos >= NPROTO)
120 return NULL;
122 return pos;
125 static void seq_stop(struct seq_file *s, void *v)
127 rcu_read_unlock();
130 static int seq_show(struct seq_file *s, void *v)
132 loff_t *pos = v;
133 const struct nf_logger *logger;
135 logger = rcu_dereference(nf_logging[*pos]);
137 if (!logger)
138 return seq_printf(s, "%2lld NONE\n", *pos);
140 return seq_printf(s, "%2lld %s\n", *pos, logger->name);
143 static struct seq_operations nflog_seq_ops = {
144 .start = seq_start,
145 .next = seq_next,
146 .stop = seq_stop,
147 .show = seq_show,
150 static int nflog_open(struct inode *inode, struct file *file)
152 return seq_open(file, &nflog_seq_ops);
155 static struct file_operations nflog_file_ops = {
156 .owner = THIS_MODULE,
157 .open = nflog_open,
158 .read = seq_read,
159 .llseek = seq_lseek,
160 .release = seq_release,
163 #endif /* PROC_FS */
166 int __init netfilter_log_init(void)
168 #ifdef CONFIG_PROC_FS
169 struct proc_dir_entry *pde;
171 pde = create_proc_entry("nf_log", S_IRUGO, proc_net_netfilter);
172 if (!pde)
173 return -1;
175 pde->proc_fops = &nflog_file_ops;
176 #endif
177 return 0;