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