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> |
| 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 | |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 17 | static struct nf_logger *nf_loggers[NPROTO]; |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 18 | static DEFINE_MUTEX(nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 19 | |
Harald Welte | d72367b | 2005-08-09 20:23:36 -0700 | [diff] [blame] | 20 | /* return EBUSY if somebody else is registered, EEXIST if the same logger |
| 21 | * is registred, 0 on success. */ |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 22 | int nf_log_register(int pf, struct nf_logger *logger) |
| 23 | { |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 24 | int ret; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 25 | |
Harald Welte | 8a61fad | 2005-08-09 20:23:53 -0700 | [diff] [blame] | 26 | if (pf >= NPROTO) |
| 27 | return -EINVAL; |
| 28 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 29 | /* Any setup of logging members must be done before |
| 30 | * substituting pointer. */ |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 31 | ret = mutex_lock_interruptible(&nf_log_mutex); |
| 32 | if (ret < 0) |
| 33 | return ret; |
Harald Welte | d72367b | 2005-08-09 20:23:36 -0700 | [diff] [blame] | 34 | |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 35 | if (!nf_loggers[pf]) |
| 36 | rcu_assign_pointer(nf_loggers[pf], logger); |
| 37 | else if (nf_loggers[pf] == logger) |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 38 | ret = -EEXIST; |
| 39 | else |
| 40 | ret = -EBUSY; |
| 41 | |
| 42 | mutex_unlock(&nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 43 | return ret; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 44 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 45 | EXPORT_SYMBOL(nf_log_register); |
| 46 | |
Patrick McHardy | 9dc6aa5 | 2007-02-12 11:11:24 -0800 | [diff] [blame] | 47 | void nf_log_unregister_pf(int pf) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 48 | { |
Harald Welte | 8a61fad | 2005-08-09 20:23:53 -0700 | [diff] [blame] | 49 | if (pf >= NPROTO) |
Patrick McHardy | 9dc6aa5 | 2007-02-12 11:11:24 -0800 | [diff] [blame] | 50 | return; |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 51 | mutex_lock(&nf_log_mutex); |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 52 | rcu_assign_pointer(nf_loggers[pf], NULL); |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 53 | mutex_unlock(&nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 54 | |
| 55 | /* Give time to concurrent readers. */ |
Patrick McHardy | a5ea6169 | 2007-02-12 11:11:06 -0800 | [diff] [blame] | 56 | synchronize_rcu(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 57 | } |
| 58 | EXPORT_SYMBOL(nf_log_unregister_pf); |
| 59 | |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 60 | void nf_log_unregister(struct nf_logger *logger) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 61 | { |
| 62 | int i; |
| 63 | |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 64 | mutex_lock(&nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 65 | for (i = 0; i < NPROTO; i++) { |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 66 | if (nf_loggers[i] == logger) |
| 67 | rcu_assign_pointer(nf_loggers[i], NULL); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 68 | } |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 69 | mutex_unlock(&nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 70 | |
Patrick McHardy | a5ea6169 | 2007-02-12 11:11:06 -0800 | [diff] [blame] | 71 | synchronize_rcu(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 72 | } |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 73 | EXPORT_SYMBOL(nf_log_unregister); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 74 | |
| 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, ...) |
| 82 | { |
| 83 | va_list args; |
| 84 | char prefix[NF_LOG_PREFIXLEN]; |
| 85 | struct nf_logger *logger; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 86 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 87 | rcu_read_lock(); |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 88 | logger = rcu_dereference(nf_loggers[pf]); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 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"); |
| 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) |
| 106 | { |
| 107 | rcu_read_lock(); |
| 108 | |
| 109 | if (*pos >= NPROTO) |
| 110 | return NULL; |
| 111 | |
| 112 | return pos; |
| 113 | } |
| 114 | |
| 115 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 116 | { |
| 117 | (*pos)++; |
| 118 | |
| 119 | if (*pos >= NPROTO) |
| 120 | return NULL; |
| 121 | |
| 122 | return pos; |
| 123 | } |
| 124 | |
| 125 | static void seq_stop(struct seq_file *s, void *v) |
| 126 | { |
| 127 | rcu_read_unlock(); |
| 128 | } |
| 129 | |
| 130 | static int seq_show(struct seq_file *s, void *v) |
| 131 | { |
| 132 | loff_t *pos = v; |
| 133 | const struct nf_logger *logger; |
| 134 | |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 135 | logger = rcu_dereference(nf_loggers[*pos]); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 136 | |
| 137 | if (!logger) |
| 138 | return seq_printf(s, "%2lld NONE\n", *pos); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 139 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 140 | return seq_printf(s, "%2lld %s\n", *pos, logger->name); |
| 141 | } |
| 142 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame^] | 143 | static const struct seq_operations nflog_seq_ops = { |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 144 | .start = seq_start, |
| 145 | .next = seq_next, |
| 146 | .stop = seq_stop, |
| 147 | .show = seq_show, |
| 148 | }; |
| 149 | |
| 150 | static int nflog_open(struct inode *inode, struct file *file) |
| 151 | { |
| 152 | return seq_open(file, &nflog_seq_ops); |
| 153 | } |
| 154 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 155 | static const struct file_operations nflog_file_ops = { |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 156 | .owner = THIS_MODULE, |
| 157 | .open = nflog_open, |
| 158 | .read = seq_read, |
| 159 | .llseek = seq_lseek, |
| 160 | .release = seq_release, |
| 161 | }; |
| 162 | |
| 163 | #endif /* PROC_FS */ |
| 164 | |
| 165 | |
| 166 | int __init netfilter_log_init(void) |
| 167 | { |
| 168 | #ifdef CONFIG_PROC_FS |
| 169 | struct proc_dir_entry *pde; |
Harald Welte | 6224392 | 2005-08-11 15:30:45 -0700 | [diff] [blame] | 170 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 171 | pde = create_proc_entry("nf_log", S_IRUGO, proc_net_netfilter); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 172 | if (!pde) |
| 173 | return -1; |
| 174 | |
| 175 | pde->proc_fops = &nflog_file_ops; |
Harald Welte | 6224392 | 2005-08-11 15:30:45 -0700 | [diff] [blame] | 176 | #endif |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |