]>
Commit | Line | Data |
---|---|---|
f6ebe77f HW |
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> | |
bbd86b9f | 7 | #include <linux/seq_file.h> |
f6ebe77f | 8 | #include <net/protocol.h> |
f01ffbd6 | 9 | #include <net/netfilter/nf_log.h> |
f6ebe77f HW |
10 | |
11 | #include "nf_internals.h" | |
12 | ||
a5d29264 | 13 | /* Internal logging interface, which relies on the real |
f6ebe77f HW |
14 | LOG target modules */ |
15 | ||
16 | #define NF_LOG_PREFIXLEN 128 | |
17 | ||
7e9c6eeb | 18 | static const struct nf_logger *nf_loggers[NFPROTO_NUMPROTO] __read_mostly; |
9b73534d | 19 | static DEFINE_MUTEX(nf_log_mutex); |
f6ebe77f | 20 | |
d72367b6 HW |
21 | /* return EBUSY if somebody else is registered, EEXIST if the same logger |
22 | * is registred, 0 on success. */ | |
76108cea | 23 | int nf_log_register(u_int8_t pf, const struct nf_logger *logger) |
f6ebe77f | 24 | { |
9b73534d | 25 | int ret; |
f6ebe77f | 26 | |
7e9c6eeb | 27 | if (pf >= ARRAY_SIZE(nf_loggers)) |
8a61fadb HW |
28 | return -EINVAL; |
29 | ||
f6ebe77f HW |
30 | /* Any setup of logging members must be done before |
31 | * substituting pointer. */ | |
9b73534d PM |
32 | ret = mutex_lock_interruptible(&nf_log_mutex); |
33 | if (ret < 0) | |
34 | return ret; | |
35 | ||
e92ad99c PM |
36 | if (!nf_loggers[pf]) |
37 | rcu_assign_pointer(nf_loggers[pf], logger); | |
38 | else if (nf_loggers[pf] == logger) | |
d72367b6 | 39 | ret = -EEXIST; |
9b73534d PM |
40 | else |
41 | ret = -EBUSY; | |
d72367b6 | 42 | |
9b73534d | 43 | mutex_unlock(&nf_log_mutex); |
f6ebe77f | 44 | return ret; |
601e68e1 | 45 | } |
f6ebe77f HW |
46 | EXPORT_SYMBOL(nf_log_register); |
47 | ||
76108cea | 48 | void nf_log_unregister_pf(u_int8_t pf) |
f6ebe77f | 49 | { |
7e9c6eeb | 50 | if (pf >= ARRAY_SIZE(nf_loggers)) |
9dc6aa5f | 51 | return; |
9b73534d | 52 | mutex_lock(&nf_log_mutex); |
e92ad99c | 53 | rcu_assign_pointer(nf_loggers[pf], NULL); |
9b73534d | 54 | mutex_unlock(&nf_log_mutex); |
f6ebe77f HW |
55 | |
56 | /* Give time to concurrent readers. */ | |
a5ea6169 | 57 | synchronize_rcu(); |
f6ebe77f HW |
58 | } |
59 | EXPORT_SYMBOL(nf_log_unregister_pf); | |
60 | ||
7b2f9631 | 61 | void nf_log_unregister(const struct nf_logger *logger) |
f6ebe77f HW |
62 | { |
63 | int i; | |
64 | ||
9b73534d | 65 | mutex_lock(&nf_log_mutex); |
7e9c6eeb | 66 | for (i = 0; i < ARRAY_SIZE(nf_loggers); i++) { |
e92ad99c PM |
67 | if (nf_loggers[i] == logger) |
68 | rcu_assign_pointer(nf_loggers[i], NULL); | |
f6ebe77f | 69 | } |
9b73534d | 70 | mutex_unlock(&nf_log_mutex); |
f6ebe77f | 71 | |
a5ea6169 | 72 | synchronize_rcu(); |
f6ebe77f | 73 | } |
e92ad99c | 74 | EXPORT_SYMBOL(nf_log_unregister); |
f6ebe77f | 75 | |
76108cea | 76 | void nf_log_packet(u_int8_t pf, |
f6ebe77f HW |
77 | unsigned int hooknum, |
78 | const struct sk_buff *skb, | |
79 | const struct net_device *in, | |
80 | const struct net_device *out, | |
7b2f9631 | 81 | const struct nf_loginfo *loginfo, |
f6ebe77f HW |
82 | const char *fmt, ...) |
83 | { | |
84 | va_list args; | |
85 | char prefix[NF_LOG_PREFIXLEN]; | |
7b2f9631 | 86 | const struct nf_logger *logger; |
601e68e1 | 87 | |
f6ebe77f | 88 | rcu_read_lock(); |
e92ad99c | 89 | logger = rcu_dereference(nf_loggers[pf]); |
f6ebe77f HW |
90 | if (logger) { |
91 | va_start(args, fmt); | |
92 | vsnprintf(prefix, sizeof(prefix), fmt, args); | |
93 | va_end(args); | |
f6ebe77f | 94 | logger->logfn(pf, hooknum, skb, in, out, loginfo, prefix); |
f6ebe77f HW |
95 | } |
96 | rcu_read_unlock(); | |
97 | } | |
98 | EXPORT_SYMBOL(nf_log_packet); | |
99 | ||
100 | #ifdef CONFIG_PROC_FS | |
101 | static void *seq_start(struct seq_file *seq, loff_t *pos) | |
ca7c48ca | 102 | __acquires(RCU) |
f6ebe77f HW |
103 | { |
104 | rcu_read_lock(); | |
105 | ||
7e9c6eeb | 106 | if (*pos >= ARRAY_SIZE(nf_loggers)) |
f6ebe77f HW |
107 | return NULL; |
108 | ||
109 | return pos; | |
110 | } | |
111 | ||
112 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) | |
113 | { | |
114 | (*pos)++; | |
115 | ||
7e9c6eeb | 116 | if (*pos >= ARRAY_SIZE(nf_loggers)) |
f6ebe77f HW |
117 | return NULL; |
118 | ||
119 | return pos; | |
120 | } | |
121 | ||
122 | static void seq_stop(struct seq_file *s, void *v) | |
ca7c48ca | 123 | __releases(RCU) |
f6ebe77f HW |
124 | { |
125 | rcu_read_unlock(); | |
126 | } | |
127 | ||
128 | static int seq_show(struct seq_file *s, void *v) | |
129 | { | |
130 | loff_t *pos = v; | |
131 | const struct nf_logger *logger; | |
132 | ||
e92ad99c | 133 | logger = rcu_dereference(nf_loggers[*pos]); |
f6ebe77f HW |
134 | |
135 | if (!logger) | |
136 | return seq_printf(s, "%2lld NONE\n", *pos); | |
601e68e1 | 137 | |
f6ebe77f HW |
138 | return seq_printf(s, "%2lld %s\n", *pos, logger->name); |
139 | } | |
140 | ||
56b3d975 | 141 | static const struct seq_operations nflog_seq_ops = { |
f6ebe77f HW |
142 | .start = seq_start, |
143 | .next = seq_next, | |
144 | .stop = seq_stop, | |
145 | .show = seq_show, | |
146 | }; | |
147 | ||
148 | static int nflog_open(struct inode *inode, struct file *file) | |
149 | { | |
150 | return seq_open(file, &nflog_seq_ops); | |
151 | } | |
152 | ||
da7071d7 | 153 | static const struct file_operations nflog_file_ops = { |
f6ebe77f HW |
154 | .owner = THIS_MODULE, |
155 | .open = nflog_open, | |
156 | .read = seq_read, | |
157 | .llseek = seq_lseek, | |
158 | .release = seq_release, | |
159 | }; | |
160 | ||
161 | #endif /* PROC_FS */ | |
162 | ||
163 | ||
164 | int __init netfilter_log_init(void) | |
165 | { | |
166 | #ifdef CONFIG_PROC_FS | |
8eeee8b1 DL |
167 | if (!proc_create("nf_log", S_IRUGO, |
168 | proc_net_netfilter, &nflog_file_ops)) | |
f6ebe77f | 169 | return -1; |
62243927 | 170 | #endif |
f6ebe77f HW |
171 | return 0; |
172 | } |