blob: ed9116dc78b5ea7a9d3e067cbae287eef84e9598 [file] [log] [blame]
Harald Weltef6ebe772005-08-09 20:21:49 -07001#include <linux/kernel.h>
2#include <linux/init.h>
3#include <linux/module.h>
4#include <linux/proc_fs.h>
5#include <linux/skbuff.h>
6#include <linux/netfilter.h>
Harald Weltebbd86b9f2005-08-09 20:23:11 -07007#include <linux/seq_file.h>
Harald Weltef6ebe772005-08-09 20:21:49 -07008#include <net/protocol.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -08009#include <net/netfilter/nf_log.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070010
11#include "nf_internals.h"
12
YOSHIFUJI Hideakia5d29262007-07-19 10:44:21 +090013/* Internal logging interface, which relies on the real
Harald Weltef6ebe772005-08-09 20:21:49 -070014 LOG target modules */
15
16#define NF_LOG_PREFIXLEN 128
17
Patrick McHardy7b2f9632007-12-17 22:39:08 -080018static const struct nf_logger *nf_loggers[NPROTO] __read_mostly;
Patrick McHardy9b735342007-02-12 11:11:39 -080019static DEFINE_MUTEX(nf_log_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070020
Harald Welted72367b2005-08-09 20:23:36 -070021/* return EBUSY if somebody else is registered, EEXIST if the same logger
22 * is registred, 0 on success. */
Patrick McHardy7b2f9632007-12-17 22:39:08 -080023int nf_log_register(int pf, const struct nf_logger *logger)
Harald Weltef6ebe772005-08-09 20:21:49 -070024{
Patrick McHardy9b735342007-02-12 11:11:39 -080025 int ret;
Harald Weltef6ebe772005-08-09 20:21:49 -070026
Harald Welte8a61fad2005-08-09 20:23:53 -070027 if (pf >= NPROTO)
28 return -EINVAL;
29
Harald Weltef6ebe772005-08-09 20:21:49 -070030 /* Any setup of logging members must be done before
31 * substituting pointer. */
Patrick McHardy9b735342007-02-12 11:11:39 -080032 ret = mutex_lock_interruptible(&nf_log_mutex);
33 if (ret < 0)
34 return ret;
Harald Welted72367b2005-08-09 20:23:36 -070035
Patrick McHardye92ad992007-02-12 11:11:55 -080036 if (!nf_loggers[pf])
37 rcu_assign_pointer(nf_loggers[pf], logger);
38 else if (nf_loggers[pf] == logger)
Patrick McHardy9b735342007-02-12 11:11:39 -080039 ret = -EEXIST;
40 else
41 ret = -EBUSY;
42
43 mutex_unlock(&nf_log_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070044 return ret;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080045}
Harald Weltef6ebe772005-08-09 20:21:49 -070046EXPORT_SYMBOL(nf_log_register);
47
Patrick McHardy9dc6aa52007-02-12 11:11:24 -080048void nf_log_unregister_pf(int pf)
Harald Weltef6ebe772005-08-09 20:21:49 -070049{
Harald Welte8a61fad2005-08-09 20:23:53 -070050 if (pf >= NPROTO)
Patrick McHardy9dc6aa52007-02-12 11:11:24 -080051 return;
Patrick McHardy9b735342007-02-12 11:11:39 -080052 mutex_lock(&nf_log_mutex);
Patrick McHardye92ad992007-02-12 11:11:55 -080053 rcu_assign_pointer(nf_loggers[pf], NULL);
Patrick McHardy9b735342007-02-12 11:11:39 -080054 mutex_unlock(&nf_log_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070055
56 /* Give time to concurrent readers. */
Patrick McHardya5ea6162007-02-12 11:11:06 -080057 synchronize_rcu();
Harald Weltef6ebe772005-08-09 20:21:49 -070058}
59EXPORT_SYMBOL(nf_log_unregister_pf);
60
Patrick McHardy7b2f9632007-12-17 22:39:08 -080061void nf_log_unregister(const struct nf_logger *logger)
Harald Weltef6ebe772005-08-09 20:21:49 -070062{
63 int i;
64
Patrick McHardy9b735342007-02-12 11:11:39 -080065 mutex_lock(&nf_log_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070066 for (i = 0; i < NPROTO; i++) {
Patrick McHardye92ad992007-02-12 11:11:55 -080067 if (nf_loggers[i] == logger)
68 rcu_assign_pointer(nf_loggers[i], NULL);
Harald Weltef6ebe772005-08-09 20:21:49 -070069 }
Patrick McHardy9b735342007-02-12 11:11:39 -080070 mutex_unlock(&nf_log_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070071
Patrick McHardya5ea6162007-02-12 11:11:06 -080072 synchronize_rcu();
Harald Weltef6ebe772005-08-09 20:21:49 -070073}
Patrick McHardye92ad992007-02-12 11:11:55 -080074EXPORT_SYMBOL(nf_log_unregister);
Harald Weltef6ebe772005-08-09 20:21:49 -070075
76void nf_log_packet(int pf,
77 unsigned int hooknum,
78 const struct sk_buff *skb,
79 const struct net_device *in,
80 const struct net_device *out,
Patrick McHardy7b2f9632007-12-17 22:39:08 -080081 const struct nf_loginfo *loginfo,
Harald Weltef6ebe772005-08-09 20:21:49 -070082 const char *fmt, ...)
83{
84 va_list args;
85 char prefix[NF_LOG_PREFIXLEN];
Patrick McHardy7b2f9632007-12-17 22:39:08 -080086 const struct nf_logger *logger;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080087
Harald Weltef6ebe772005-08-09 20:21:49 -070088 rcu_read_lock();
Patrick McHardye92ad992007-02-12 11:11:55 -080089 logger = rcu_dereference(nf_loggers[pf]);
Harald Weltef6ebe772005-08-09 20:21:49 -070090 if (logger) {
91 va_start(args, fmt);
92 vsnprintf(prefix, sizeof(prefix), fmt, args);
93 va_end(args);
94 /* We must read logging before nf_logfn[pf] */
95 logger->logfn(pf, hooknum, skb, in, out, loginfo, prefix);
96 } else if (net_ratelimit()) {
97 printk(KERN_WARNING "nf_log_packet: can\'t log since "
98 "no backend logging module loaded in! Please either "
99 "load one, or disable logging explicitly\n");
100 }
101 rcu_read_unlock();
102}
103EXPORT_SYMBOL(nf_log_packet);
104
105#ifdef CONFIG_PROC_FS
106static void *seq_start(struct seq_file *seq, loff_t *pos)
107{
108 rcu_read_lock();
109
110 if (*pos >= NPROTO)
111 return NULL;
112
113 return pos;
114}
115
116static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
117{
118 (*pos)++;
119
120 if (*pos >= NPROTO)
121 return NULL;
122
123 return pos;
124}
125
126static void seq_stop(struct seq_file *s, void *v)
127{
128 rcu_read_unlock();
129}
130
131static int seq_show(struct seq_file *s, void *v)
132{
133 loff_t *pos = v;
134 const struct nf_logger *logger;
135
Patrick McHardye92ad992007-02-12 11:11:55 -0800136 logger = rcu_dereference(nf_loggers[*pos]);
Harald Weltef6ebe772005-08-09 20:21:49 -0700137
138 if (!logger)
139 return seq_printf(s, "%2lld NONE\n", *pos);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800140
Harald Weltef6ebe772005-08-09 20:21:49 -0700141 return seq_printf(s, "%2lld %s\n", *pos, logger->name);
142}
143
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700144static const struct seq_operations nflog_seq_ops = {
Harald Weltef6ebe772005-08-09 20:21:49 -0700145 .start = seq_start,
146 .next = seq_next,
147 .stop = seq_stop,
148 .show = seq_show,
149};
150
151static int nflog_open(struct inode *inode, struct file *file)
152{
153 return seq_open(file, &nflog_seq_ops);
154}
155
Arjan van de Venda7071d2007-02-12 00:55:36 -0800156static const struct file_operations nflog_file_ops = {
Harald Weltef6ebe772005-08-09 20:21:49 -0700157 .owner = THIS_MODULE,
158 .open = nflog_open,
159 .read = seq_read,
160 .llseek = seq_lseek,
161 .release = seq_release,
162};
163
164#endif /* PROC_FS */
165
166
167int __init netfilter_log_init(void)
168{
169#ifdef CONFIG_PROC_FS
170 struct proc_dir_entry *pde;
Harald Welte62243922005-08-11 15:30:45 -0700171
Harald Weltef6ebe772005-08-09 20:21:49 -0700172 pde = create_proc_entry("nf_log", S_IRUGO, proc_net_netfilter);
Harald Weltef6ebe772005-08-09 20:21:49 -0700173 if (!pde)
174 return -1;
175
176 pde->proc_fops = &nflog_file_ops;
Harald Welte62243922005-08-11 15:30:45 -0700177#endif
Harald Weltef6ebe772005-08-09 20:21:49 -0700178 return 0;
179}