Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 1 | #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 Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 7 | #include <linux/seq_file.h> |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 8 | #include <net/protocol.h> |
Patrick McHardy | f01ffbd | 2007-12-17 22:38:49 -0800 | [diff] [blame] | 9 | #include <net/netfilter/nf_log.h> |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 10 | |
| 11 | #include "nf_internals.h" |
| 12 | |
YOSHIFUJI Hideaki | a5d2926 | 2007-07-19 10:44:21 +0900 | [diff] [blame] | 13 | /* Internal logging interface, which relies on the real |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 14 | LOG target modules */ |
| 15 | |
| 16 | #define NF_LOG_PREFIXLEN 128 |
| 17 | |
Patrick McHardy | 7b2f963 | 2007-12-17 22:39:08 -0800 | [diff] [blame] | 18 | static const struct nf_logger *nf_loggers[NPROTO] __read_mostly; |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 19 | static DEFINE_MUTEX(nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 20 | |
Harald Welte | d72367b | 2005-08-09 20:23:36 -0700 | [diff] [blame] | 21 | /* return EBUSY if somebody else is registered, EEXIST if the same logger |
| 22 | * is registred, 0 on success. */ |
Patrick McHardy | 7b2f963 | 2007-12-17 22:39:08 -0800 | [diff] [blame] | 23 | int nf_log_register(int pf, const struct nf_logger *logger) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 24 | { |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 25 | int ret; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 26 | |
Harald Welte | 8a61fad | 2005-08-09 20:23:53 -0700 | [diff] [blame] | 27 | if (pf >= NPROTO) |
| 28 | return -EINVAL; |
| 29 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 30 | /* Any setup of logging members must be done before |
| 31 | * substituting pointer. */ |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 32 | ret = mutex_lock_interruptible(&nf_log_mutex); |
| 33 | if (ret < 0) |
| 34 | return ret; |
Harald Welte | d72367b | 2005-08-09 20:23:36 -0700 | [diff] [blame] | 35 | |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 36 | if (!nf_loggers[pf]) |
| 37 | rcu_assign_pointer(nf_loggers[pf], logger); |
| 38 | else if (nf_loggers[pf] == logger) |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 39 | ret = -EEXIST; |
| 40 | else |
| 41 | ret = -EBUSY; |
| 42 | |
| 43 | mutex_unlock(&nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 44 | return ret; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 45 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 46 | EXPORT_SYMBOL(nf_log_register); |
| 47 | |
Patrick McHardy | 9dc6aa5 | 2007-02-12 11:11:24 -0800 | [diff] [blame] | 48 | void nf_log_unregister_pf(int pf) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 49 | { |
Harald Welte | 8a61fad | 2005-08-09 20:23:53 -0700 | [diff] [blame] | 50 | if (pf >= NPROTO) |
Patrick McHardy | 9dc6aa5 | 2007-02-12 11:11:24 -0800 | [diff] [blame] | 51 | return; |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 52 | mutex_lock(&nf_log_mutex); |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 53 | rcu_assign_pointer(nf_loggers[pf], NULL); |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 54 | mutex_unlock(&nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 55 | |
| 56 | /* Give time to concurrent readers. */ |
Patrick McHardy | a5ea616 | 2007-02-12 11:11:06 -0800 | [diff] [blame] | 57 | synchronize_rcu(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 58 | } |
| 59 | EXPORT_SYMBOL(nf_log_unregister_pf); |
| 60 | |
Patrick McHardy | 7b2f963 | 2007-12-17 22:39:08 -0800 | [diff] [blame] | 61 | void nf_log_unregister(const struct nf_logger *logger) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 62 | { |
| 63 | int i; |
| 64 | |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 65 | mutex_lock(&nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 66 | for (i = 0; i < NPROTO; i++) { |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 67 | if (nf_loggers[i] == logger) |
| 68 | rcu_assign_pointer(nf_loggers[i], NULL); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 69 | } |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 70 | mutex_unlock(&nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 71 | |
Patrick McHardy | a5ea616 | 2007-02-12 11:11:06 -0800 | [diff] [blame] | 72 | synchronize_rcu(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 73 | } |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 74 | EXPORT_SYMBOL(nf_log_unregister); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 75 | |
| 76 | void 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 McHardy | 7b2f963 | 2007-12-17 22:39:08 -0800 | [diff] [blame] | 81 | const struct nf_loginfo *loginfo, |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 82 | const char *fmt, ...) |
| 83 | { |
| 84 | va_list args; |
| 85 | char prefix[NF_LOG_PREFIXLEN]; |
Patrick McHardy | 7b2f963 | 2007-12-17 22:39:08 -0800 | [diff] [blame] | 86 | const struct nf_logger *logger; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 87 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 88 | rcu_read_lock(); |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 89 | logger = rcu_dereference(nf_loggers[pf]); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 90 | if (logger) { |
| 91 | va_start(args, fmt); |
| 92 | vsnprintf(prefix, sizeof(prefix), fmt, args); |
| 93 | va_end(args); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 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"); |
| 99 | } |
| 100 | rcu_read_unlock(); |
| 101 | } |
| 102 | EXPORT_SYMBOL(nf_log_packet); |
| 103 | |
| 104 | #ifdef CONFIG_PROC_FS |
| 105 | static void *seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | ca7c48c | 2008-01-31 03:53:27 -0800 | [diff] [blame] | 106 | __acquires(RCU) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 107 | { |
| 108 | rcu_read_lock(); |
| 109 | |
| 110 | if (*pos >= NPROTO) |
| 111 | return NULL; |
| 112 | |
| 113 | return pos; |
| 114 | } |
| 115 | |
| 116 | static 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 | |
| 126 | static void seq_stop(struct seq_file *s, void *v) |
Eric Dumazet | ca7c48c | 2008-01-31 03:53:27 -0800 | [diff] [blame] | 127 | __releases(RCU) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 128 | { |
| 129 | rcu_read_unlock(); |
| 130 | } |
| 131 | |
| 132 | static int seq_show(struct seq_file *s, void *v) |
| 133 | { |
| 134 | loff_t *pos = v; |
| 135 | const struct nf_logger *logger; |
| 136 | |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 137 | logger = rcu_dereference(nf_loggers[*pos]); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 138 | |
| 139 | if (!logger) |
| 140 | return seq_printf(s, "%2lld NONE\n", *pos); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 141 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 142 | return seq_printf(s, "%2lld %s\n", *pos, logger->name); |
| 143 | } |
| 144 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 145 | static const struct seq_operations nflog_seq_ops = { |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 146 | .start = seq_start, |
| 147 | .next = seq_next, |
| 148 | .stop = seq_stop, |
| 149 | .show = seq_show, |
| 150 | }; |
| 151 | |
| 152 | static int nflog_open(struct inode *inode, struct file *file) |
| 153 | { |
| 154 | return seq_open(file, &nflog_seq_ops); |
| 155 | } |
| 156 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 157 | static const struct file_operations nflog_file_ops = { |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 158 | .owner = THIS_MODULE, |
| 159 | .open = nflog_open, |
| 160 | .read = seq_read, |
| 161 | .llseek = seq_lseek, |
| 162 | .release = seq_release, |
| 163 | }; |
| 164 | |
| 165 | #endif /* PROC_FS */ |
| 166 | |
| 167 | |
| 168 | int __init netfilter_log_init(void) |
| 169 | { |
| 170 | #ifdef CONFIG_PROC_FS |
Denis V. Lunev | 8eeee8b | 2008-03-27 16:55:53 -0700 | [diff] [blame] | 171 | if (!proc_create("nf_log", S_IRUGO, |
| 172 | proc_net_netfilter, &nflog_file_ops)) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 173 | return -1; |
Harald Welte | 6224392 | 2005-08-11 15:30:45 -0700 | [diff] [blame] | 174 | #endif |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 175 | return 0; |
| 176 | } |