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 |
Eric Leblond | 1762527 | 2009-03-23 13:16:53 +0100 | [diff] [blame] | 17 | #define NFLOGGER_NAME_LEN 64 |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 18 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 19 | static const struct nf_logger *nf_loggers[NFPROTO_NUMPROTO] __read_mostly; |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 20 | static struct list_head nf_loggers_l[NFPROTO_NUMPROTO] __read_mostly; |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 21 | static DEFINE_MUTEX(nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 22 | |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 23 | static struct nf_logger *__find_logger(int pf, const char *str_logger) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 24 | { |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 25 | struct nf_logger *t; |
| 26 | |
| 27 | list_for_each_entry(t, &nf_loggers_l[pf], list[pf]) { |
| 28 | if (!strnicmp(str_logger, t->name, strlen(t->name))) |
| 29 | return t; |
| 30 | } |
| 31 | |
| 32 | return NULL; |
| 33 | } |
| 34 | |
| 35 | /* return EEXIST if the same logger is registred, 0 on success. */ |
| 36 | int nf_log_register(u_int8_t pf, struct nf_logger *logger) |
| 37 | { |
| 38 | const struct nf_logger *llog; |
Eric Dumazet | b6f0a36 | 2009-04-15 12:16:19 +0200 | [diff] [blame] | 39 | int i; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 40 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 41 | if (pf >= ARRAY_SIZE(nf_loggers)) |
Harald Welte | 8a61fad | 2005-08-09 20:23:53 -0700 | [diff] [blame] | 42 | return -EINVAL; |
| 43 | |
Eric Dumazet | b6f0a36 | 2009-04-15 12:16:19 +0200 | [diff] [blame] | 44 | for (i = 0; i < ARRAY_SIZE(logger->list); i++) |
| 45 | INIT_LIST_HEAD(&logger->list[i]); |
| 46 | |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 47 | mutex_lock(&nf_log_mutex); |
Harald Welte | d72367b | 2005-08-09 20:23:36 -0700 | [diff] [blame] | 48 | |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 49 | if (pf == NFPROTO_UNSPEC) { |
| 50 | int i; |
| 51 | for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) |
| 52 | list_add_tail(&(logger->list[i]), &(nf_loggers_l[i])); |
| 53 | } else { |
| 54 | /* register at end of list to honor first register win */ |
| 55 | list_add_tail(&logger->list[pf], &nf_loggers_l[pf]); |
| 56 | llog = rcu_dereference(nf_loggers[pf]); |
| 57 | if (llog == NULL) |
| 58 | rcu_assign_pointer(nf_loggers[pf], logger); |
| 59 | } |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 60 | |
| 61 | mutex_unlock(&nf_log_mutex); |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 62 | |
| 63 | return 0; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 64 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 65 | EXPORT_SYMBOL(nf_log_register); |
| 66 | |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 67 | void nf_log_unregister(struct nf_logger *logger) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 68 | { |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 69 | const struct nf_logger *c_logger; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 70 | int i; |
| 71 | |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 72 | mutex_lock(&nf_log_mutex); |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 73 | for (i = 0; i < ARRAY_SIZE(nf_loggers); i++) { |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 74 | c_logger = rcu_dereference(nf_loggers[i]); |
| 75 | if (c_logger == logger) |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 76 | rcu_assign_pointer(nf_loggers[i], NULL); |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 77 | list_del(&logger->list[i]); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 78 | } |
Patrick McHardy | 9b73534 | 2007-02-12 11:11:39 -0800 | [diff] [blame] | 79 | mutex_unlock(&nf_log_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 80 | |
Patrick McHardy | a5ea6169 | 2007-02-12 11:11:06 -0800 | [diff] [blame] | 81 | synchronize_rcu(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 82 | } |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 83 | EXPORT_SYMBOL(nf_log_unregister); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 84 | |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 85 | int nf_log_bind_pf(u_int8_t pf, const struct nf_logger *logger) |
| 86 | { |
| 87 | mutex_lock(&nf_log_mutex); |
| 88 | if (__find_logger(pf, logger->name) == NULL) { |
| 89 | mutex_unlock(&nf_log_mutex); |
| 90 | return -ENOENT; |
| 91 | } |
| 92 | rcu_assign_pointer(nf_loggers[pf], logger); |
| 93 | mutex_unlock(&nf_log_mutex); |
| 94 | return 0; |
| 95 | } |
| 96 | EXPORT_SYMBOL(nf_log_bind_pf); |
| 97 | |
| 98 | void nf_log_unbind_pf(u_int8_t pf) |
| 99 | { |
| 100 | mutex_lock(&nf_log_mutex); |
| 101 | rcu_assign_pointer(nf_loggers[pf], NULL); |
| 102 | mutex_unlock(&nf_log_mutex); |
| 103 | } |
| 104 | EXPORT_SYMBOL(nf_log_unbind_pf); |
| 105 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 106 | void nf_log_packet(u_int8_t pf, |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 107 | unsigned int hooknum, |
| 108 | const struct sk_buff *skb, |
| 109 | const struct net_device *in, |
| 110 | const struct net_device *out, |
Patrick McHardy | 7b2f963 | 2007-12-17 22:39:08 -0800 | [diff] [blame] | 111 | const struct nf_loginfo *loginfo, |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 112 | const char *fmt, ...) |
| 113 | { |
| 114 | va_list args; |
| 115 | char prefix[NF_LOG_PREFIXLEN]; |
Patrick McHardy | 7b2f963 | 2007-12-17 22:39:08 -0800 | [diff] [blame] | 116 | const struct nf_logger *logger; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 117 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 118 | rcu_read_lock(); |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 119 | logger = rcu_dereference(nf_loggers[pf]); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 120 | if (logger) { |
| 121 | va_start(args, fmt); |
| 122 | vsnprintf(prefix, sizeof(prefix), fmt, args); |
| 123 | va_end(args); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 124 | logger->logfn(pf, hooknum, skb, in, out, loginfo, prefix); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 125 | } |
| 126 | rcu_read_unlock(); |
| 127 | } |
| 128 | EXPORT_SYMBOL(nf_log_packet); |
| 129 | |
| 130 | #ifdef CONFIG_PROC_FS |
| 131 | static void *seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | ca7c48c | 2008-01-31 03:53:27 -0800 | [diff] [blame] | 132 | __acquires(RCU) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 133 | { |
| 134 | rcu_read_lock(); |
| 135 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 136 | if (*pos >= ARRAY_SIZE(nf_loggers)) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 137 | return NULL; |
| 138 | |
| 139 | return pos; |
| 140 | } |
| 141 | |
| 142 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 143 | { |
| 144 | (*pos)++; |
| 145 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 146 | if (*pos >= ARRAY_SIZE(nf_loggers)) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 147 | return NULL; |
| 148 | |
| 149 | return pos; |
| 150 | } |
| 151 | |
| 152 | static void seq_stop(struct seq_file *s, void *v) |
Eric Dumazet | ca7c48c | 2008-01-31 03:53:27 -0800 | [diff] [blame] | 153 | __releases(RCU) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 154 | { |
| 155 | rcu_read_unlock(); |
| 156 | } |
| 157 | |
| 158 | static int seq_show(struct seq_file *s, void *v) |
| 159 | { |
| 160 | loff_t *pos = v; |
| 161 | const struct nf_logger *logger; |
Eric Leblond | c7a913c | 2009-03-16 14:55:27 +0100 | [diff] [blame] | 162 | struct nf_logger *t; |
| 163 | int ret; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 164 | |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 165 | logger = rcu_dereference(nf_loggers[*pos]); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 166 | |
| 167 | if (!logger) |
Eric Leblond | c7a913c | 2009-03-16 14:55:27 +0100 | [diff] [blame] | 168 | ret = seq_printf(s, "%2lld NONE (", *pos); |
| 169 | else |
| 170 | ret = seq_printf(s, "%2lld %s (", *pos, logger->name); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 171 | |
Eric Leblond | c7a913c | 2009-03-16 14:55:27 +0100 | [diff] [blame] | 172 | if (ret < 0) |
| 173 | return ret; |
| 174 | |
| 175 | mutex_lock(&nf_log_mutex); |
| 176 | list_for_each_entry(t, &nf_loggers_l[*pos], list[*pos]) { |
| 177 | ret = seq_printf(s, "%s", t->name); |
| 178 | if (ret < 0) { |
| 179 | mutex_unlock(&nf_log_mutex); |
| 180 | return ret; |
| 181 | } |
| 182 | if (&t->list[*pos] != nf_loggers_l[*pos].prev) { |
| 183 | ret = seq_printf(s, ","); |
| 184 | if (ret < 0) { |
| 185 | mutex_unlock(&nf_log_mutex); |
| 186 | return ret; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | mutex_unlock(&nf_log_mutex); |
| 191 | |
| 192 | return seq_printf(s, ")\n"); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 195 | static const struct seq_operations nflog_seq_ops = { |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 196 | .start = seq_start, |
| 197 | .next = seq_next, |
| 198 | .stop = seq_stop, |
| 199 | .show = seq_show, |
| 200 | }; |
| 201 | |
| 202 | static int nflog_open(struct inode *inode, struct file *file) |
| 203 | { |
| 204 | return seq_open(file, &nflog_seq_ops); |
| 205 | } |
| 206 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 207 | static const struct file_operations nflog_file_ops = { |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 208 | .owner = THIS_MODULE, |
| 209 | .open = nflog_open, |
| 210 | .read = seq_read, |
| 211 | .llseek = seq_lseek, |
| 212 | .release = seq_release, |
| 213 | }; |
| 214 | |
Eric Leblond | 1762527 | 2009-03-23 13:16:53 +0100 | [diff] [blame] | 215 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 216 | #endif /* PROC_FS */ |
| 217 | |
Eric Leblond | 1762527 | 2009-03-23 13:16:53 +0100 | [diff] [blame] | 218 | #ifdef CONFIG_SYSCTL |
| 219 | struct ctl_path nf_log_sysctl_path[] = { |
| 220 | { .procname = "net", .ctl_name = CTL_NET, }, |
| 221 | { .procname = "netfilter", .ctl_name = NET_NETFILTER, }, |
| 222 | { .procname = "nf_log", .ctl_name = CTL_UNNUMBERED, }, |
| 223 | { } |
| 224 | }; |
| 225 | |
| 226 | static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3]; |
| 227 | static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1]; |
| 228 | static struct ctl_table_header *nf_log_dir_header; |
| 229 | |
| 230 | static int nf_log_proc_dostring(ctl_table *table, int write, struct file *filp, |
| 231 | void *buffer, size_t *lenp, loff_t *ppos) |
| 232 | { |
| 233 | const struct nf_logger *logger; |
| 234 | int r = 0; |
| 235 | int tindex = (unsigned long)table->extra1; |
| 236 | |
| 237 | if (write) { |
| 238 | if (!strcmp(buffer, "NONE")) { |
| 239 | nf_log_unbind_pf(tindex); |
| 240 | return 0; |
| 241 | } |
| 242 | mutex_lock(&nf_log_mutex); |
| 243 | logger = __find_logger(tindex, buffer); |
| 244 | if (logger == NULL) { |
| 245 | mutex_unlock(&nf_log_mutex); |
| 246 | return -ENOENT; |
| 247 | } |
| 248 | rcu_assign_pointer(nf_loggers[tindex], logger); |
| 249 | mutex_unlock(&nf_log_mutex); |
| 250 | } else { |
Patrick McHardy | 266d07c | 2009-06-13 12:21:10 +0200 | [diff] [blame] | 251 | mutex_lock(&nf_log_mutex); |
| 252 | logger = nf_loggers[tindex]; |
Eric Leblond | 1762527 | 2009-03-23 13:16:53 +0100 | [diff] [blame] | 253 | if (!logger) |
| 254 | table->data = "NONE"; |
| 255 | else |
| 256 | table->data = logger->name; |
| 257 | r = proc_dostring(table, write, filp, buffer, lenp, ppos); |
Patrick McHardy | 266d07c | 2009-06-13 12:21:10 +0200 | [diff] [blame] | 258 | mutex_unlock(&nf_log_mutex); |
Eric Leblond | 1762527 | 2009-03-23 13:16:53 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return r; |
| 262 | } |
| 263 | |
| 264 | static __init int netfilter_log_sysctl_init(void) |
| 265 | { |
| 266 | int i; |
| 267 | |
| 268 | for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) { |
| 269 | snprintf(nf_log_sysctl_fnames[i-NFPROTO_UNSPEC], 3, "%d", i); |
| 270 | nf_log_sysctl_table[i].ctl_name = CTL_UNNUMBERED; |
| 271 | nf_log_sysctl_table[i].procname = |
| 272 | nf_log_sysctl_fnames[i-NFPROTO_UNSPEC]; |
| 273 | nf_log_sysctl_table[i].data = NULL; |
| 274 | nf_log_sysctl_table[i].maxlen = |
| 275 | NFLOGGER_NAME_LEN * sizeof(char); |
| 276 | nf_log_sysctl_table[i].mode = 0644; |
| 277 | nf_log_sysctl_table[i].proc_handler = nf_log_proc_dostring; |
| 278 | nf_log_sysctl_table[i].extra1 = (void *)(unsigned long) i; |
| 279 | } |
| 280 | |
| 281 | nf_log_dir_header = register_sysctl_paths(nf_log_sysctl_path, |
| 282 | nf_log_sysctl_table); |
| 283 | if (!nf_log_dir_header) |
| 284 | return -ENOMEM; |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | #else |
| 289 | static __init int netfilter_log_sysctl_init(void) |
| 290 | { |
| 291 | return 0; |
| 292 | } |
| 293 | #endif /* CONFIG_SYSCTL */ |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 294 | |
| 295 | int __init netfilter_log_init(void) |
| 296 | { |
Eric Leblond | 1762527 | 2009-03-23 13:16:53 +0100 | [diff] [blame] | 297 | int i, r; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 298 | #ifdef CONFIG_PROC_FS |
Denis V. Lunev | 8eeee8b | 2008-03-27 16:55:53 -0700 | [diff] [blame] | 299 | if (!proc_create("nf_log", S_IRUGO, |
| 300 | proc_net_netfilter, &nflog_file_ops)) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 301 | return -1; |
Harald Welte | 6224392 | 2005-08-11 15:30:45 -0700 | [diff] [blame] | 302 | #endif |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 303 | |
Eric Leblond | 1762527 | 2009-03-23 13:16:53 +0100 | [diff] [blame] | 304 | /* Errors will trigger panic, unroll on error is unnecessary. */ |
| 305 | r = netfilter_log_sysctl_init(); |
| 306 | if (r < 0) |
| 307 | return r; |
| 308 | |
Eric Leblond | ca735b3 | 2009-03-16 14:54:21 +0100 | [diff] [blame] | 309 | for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) |
| 310 | INIT_LIST_HEAD(&(nf_loggers_l[i])); |
| 311 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 312 | return 0; |
| 313 | } |