[NETFILTER]: split net/core/netfilter.c into net/netfilter/*.c
[deliverable/linux.git] / net / netfilter / nf_log.c
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 <net/protocol.h>
9
10 #include "nf_internals.h"
11
12 /* Internal logging interface, which relies on the real
13 LOG target modules */
14
15 #define NF_LOG_PREFIXLEN 128
16
17 static struct nf_logger *nf_logging[NPROTO]; /* = NULL */
18 static DEFINE_SPINLOCK(nf_log_lock);
19
20 int nf_log_register(int pf, struct nf_logger *logger)
21 {
22 int ret = -EBUSY;
23
24 /* Any setup of logging members must be done before
25 * substituting pointer. */
26 spin_lock(&nf_log_lock);
27 if (!nf_logging[pf]) {
28 rcu_assign_pointer(nf_logging[pf], logger);
29 ret = 0;
30 }
31 spin_unlock(&nf_log_lock);
32 return ret;
33 }
34 EXPORT_SYMBOL(nf_log_register);
35
36 void nf_log_unregister_pf(int pf)
37 {
38 spin_lock(&nf_log_lock);
39 nf_logging[pf] = NULL;
40 spin_unlock(&nf_log_lock);
41
42 /* Give time to concurrent readers. */
43 synchronize_net();
44 }
45 EXPORT_SYMBOL(nf_log_unregister_pf);
46
47 void nf_log_unregister_logger(struct nf_logger *logger)
48 {
49 int i;
50
51 spin_lock(&nf_log_lock);
52 for (i = 0; i < NPROTO; i++) {
53 if (nf_logging[i] == logger)
54 nf_logging[i] = NULL;
55 }
56 spin_unlock(&nf_log_lock);
57
58 synchronize_net();
59 }
60 EXPORT_SYMBOL(nf_log_unregister_logger);
61
62 void nf_log_packet(int pf,
63 unsigned int hooknum,
64 const struct sk_buff *skb,
65 const struct net_device *in,
66 const struct net_device *out,
67 struct nf_loginfo *loginfo,
68 const char *fmt, ...)
69 {
70 va_list args;
71 char prefix[NF_LOG_PREFIXLEN];
72 struct nf_logger *logger;
73
74 rcu_read_lock();
75 logger = rcu_dereference(nf_logging[pf]);
76 if (logger) {
77 va_start(args, fmt);
78 vsnprintf(prefix, sizeof(prefix), fmt, args);
79 va_end(args);
80 /* We must read logging before nf_logfn[pf] */
81 logger->logfn(pf, hooknum, skb, in, out, loginfo, prefix);
82 } else if (net_ratelimit()) {
83 printk(KERN_WARNING "nf_log_packet: can\'t log since "
84 "no backend logging module loaded in! Please either "
85 "load one, or disable logging explicitly\n");
86 }
87 rcu_read_unlock();
88 }
89 EXPORT_SYMBOL(nf_log_packet);
90
91 #ifdef CONFIG_PROC_FS
92 static void *seq_start(struct seq_file *seq, loff_t *pos)
93 {
94 rcu_read_lock();
95
96 if (*pos >= NPROTO)
97 return NULL;
98
99 return pos;
100 }
101
102 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
103 {
104 (*pos)++;
105
106 if (*pos >= NPROTO)
107 return NULL;
108
109 return pos;
110 }
111
112 static void seq_stop(struct seq_file *s, void *v)
113 {
114 rcu_read_unlock();
115 }
116
117 static int seq_show(struct seq_file *s, void *v)
118 {
119 loff_t *pos = v;
120 const struct nf_logger *logger;
121
122 logger = rcu_dereference(nf_logging[*pos]);
123
124 if (!logger)
125 return seq_printf(s, "%2lld NONE\n", *pos);
126
127 return seq_printf(s, "%2lld %s\n", *pos, logger->name);
128 }
129
130 static struct seq_operations nflog_seq_ops = {
131 .start = seq_start,
132 .next = seq_next,
133 .stop = seq_stop,
134 .show = seq_show,
135 };
136
137 static int nflog_open(struct inode *inode, struct file *file)
138 {
139 return seq_open(file, &nflog_seq_ops);
140 }
141
142 static struct file_operations nflog_file_ops = {
143 .owner = THIS_MODULE,
144 .open = nflog_open,
145 .read = seq_read,
146 .llseek = seq_lseek,
147 .release = seq_release,
148 };
149
150 #endif /* PROC_FS */
151
152
153 int __init netfilter_log_init(void)
154 {
155 #ifdef CONFIG_PROC_FS
156 struct proc_dir_entry *pde;
157 pde = create_proc_entry("nf_log", S_IRUGO, proc_net_netfilter);
158 #endif
159 if (!pde)
160 return -1;
161
162 pde->proc_fops = &nflog_file_ops;
163
164 return 0;
165 }
This page took 0.107131 seconds and 6 git commands to generate.