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>
7 #include <linux/seq_file.h>
8 #include <net/protocol.h>
9 #include <net/netfilter/nf_log.h>
11 #include "nf_internals.h"
13 /* Internal logging interface, which relies on the real
16 #define NF_LOG_PREFIXLEN 128
17 #define NFLOGGER_NAME_LEN 64
19 static struct nf_logger __rcu
*loggers
[NFPROTO_NUMPROTO
][NF_LOG_TYPE_MAX
] __read_mostly
;
20 static DEFINE_MUTEX(nf_log_mutex
);
22 #define nft_log_dereference(logger) \
23 rcu_dereference_protected(logger, lockdep_is_held(&nf_log_mutex))
25 static struct nf_logger
*__find_logger(int pf
, const char *str_logger
)
27 struct nf_logger
*log
;
30 for (i
= 0; i
< NF_LOG_TYPE_MAX
; i
++) {
31 if (loggers
[pf
][i
] == NULL
)
34 log
= nft_log_dereference(loggers
[pf
][i
]);
35 if (!strncasecmp(str_logger
, log
->name
, strlen(log
->name
)))
42 int nf_log_set(struct net
*net
, u_int8_t pf
, const struct nf_logger
*logger
)
44 const struct nf_logger
*log
;
46 if (pf
== NFPROTO_UNSPEC
|| pf
>= ARRAY_SIZE(net
->nf
.nf_loggers
))
49 mutex_lock(&nf_log_mutex
);
50 log
= nft_log_dereference(net
->nf
.nf_loggers
[pf
]);
52 rcu_assign_pointer(net
->nf
.nf_loggers
[pf
], logger
);
54 mutex_unlock(&nf_log_mutex
);
58 EXPORT_SYMBOL(nf_log_set
);
60 void nf_log_unset(struct net
*net
, const struct nf_logger
*logger
)
63 const struct nf_logger
*log
;
65 mutex_lock(&nf_log_mutex
);
66 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++) {
67 log
= nft_log_dereference(net
->nf
.nf_loggers
[i
]);
69 RCU_INIT_POINTER(net
->nf
.nf_loggers
[i
], NULL
);
71 mutex_unlock(&nf_log_mutex
);
74 EXPORT_SYMBOL(nf_log_unset
);
76 /* return EEXIST if the same logger is registered, 0 on success. */
77 int nf_log_register(u_int8_t pf
, struct nf_logger
*logger
)
82 if (pf
>= ARRAY_SIZE(init_net
.nf
.nf_loggers
))
85 mutex_lock(&nf_log_mutex
);
87 if (pf
== NFPROTO_UNSPEC
) {
88 for (i
= NFPROTO_UNSPEC
; i
< NFPROTO_NUMPROTO
; i
++) {
89 if (rcu_access_pointer(loggers
[i
][logger
->type
])) {
94 for (i
= NFPROTO_UNSPEC
; i
< NFPROTO_NUMPROTO
; i
++)
95 rcu_assign_pointer(loggers
[i
][logger
->type
], logger
);
97 if (rcu_access_pointer(loggers
[pf
][logger
->type
])) {
101 rcu_assign_pointer(loggers
[pf
][logger
->type
], logger
);
105 mutex_unlock(&nf_log_mutex
);
108 EXPORT_SYMBOL(nf_log_register
);
110 void nf_log_unregister(struct nf_logger
*logger
)
112 const struct nf_logger
*log
;
115 mutex_lock(&nf_log_mutex
);
116 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++) {
117 log
= nft_log_dereference(loggers
[i
][logger
->type
]);
119 RCU_INIT_POINTER(loggers
[i
][logger
->type
], NULL
);
121 mutex_unlock(&nf_log_mutex
);
124 EXPORT_SYMBOL(nf_log_unregister
);
126 int nf_log_bind_pf(struct net
*net
, u_int8_t pf
,
127 const struct nf_logger
*logger
)
129 if (pf
>= ARRAY_SIZE(net
->nf
.nf_loggers
))
131 mutex_lock(&nf_log_mutex
);
132 if (__find_logger(pf
, logger
->name
) == NULL
) {
133 mutex_unlock(&nf_log_mutex
);
136 rcu_assign_pointer(net
->nf
.nf_loggers
[pf
], logger
);
137 mutex_unlock(&nf_log_mutex
);
140 EXPORT_SYMBOL(nf_log_bind_pf
);
142 void nf_log_unbind_pf(struct net
*net
, u_int8_t pf
)
144 if (pf
>= ARRAY_SIZE(net
->nf
.nf_loggers
))
146 mutex_lock(&nf_log_mutex
);
147 RCU_INIT_POINTER(net
->nf
.nf_loggers
[pf
], NULL
);
148 mutex_unlock(&nf_log_mutex
);
150 EXPORT_SYMBOL(nf_log_unbind_pf
);
152 void nf_logger_request_module(int pf
, enum nf_log_type type
)
154 if (loggers
[pf
][type
] == NULL
)
155 request_module("nf-logger-%u-%u", pf
, type
);
157 EXPORT_SYMBOL_GPL(nf_logger_request_module
);
159 int nf_logger_find_get(int pf
, enum nf_log_type type
)
161 struct nf_logger
*logger
;
164 if (pf
== NFPROTO_INET
) {
165 ret
= nf_logger_find_get(NFPROTO_IPV4
, type
);
169 ret
= nf_logger_find_get(NFPROTO_IPV6
, type
);
171 nf_logger_put(NFPROTO_IPV4
, type
);
178 if (rcu_access_pointer(loggers
[pf
][type
]) == NULL
)
179 request_module("nf-logger-%u-%u", pf
, type
);
182 logger
= rcu_dereference(loggers
[pf
][type
]);
186 if (try_module_get(logger
->me
))
192 EXPORT_SYMBOL_GPL(nf_logger_find_get
);
194 void nf_logger_put(int pf
, enum nf_log_type type
)
196 struct nf_logger
*logger
;
198 if (pf
== NFPROTO_INET
) {
199 nf_logger_put(NFPROTO_IPV4
, type
);
200 nf_logger_put(NFPROTO_IPV6
, type
);
204 BUG_ON(loggers
[pf
][type
] == NULL
);
207 logger
= rcu_dereference(loggers
[pf
][type
]);
208 module_put(logger
->me
);
211 EXPORT_SYMBOL_GPL(nf_logger_put
);
213 void nf_log_packet(struct net
*net
,
215 unsigned int hooknum
,
216 const struct sk_buff
*skb
,
217 const struct net_device
*in
,
218 const struct net_device
*out
,
219 const struct nf_loginfo
*loginfo
,
220 const char *fmt
, ...)
223 char prefix
[NF_LOG_PREFIXLEN
];
224 const struct nf_logger
*logger
;
228 logger
= rcu_dereference(loggers
[pf
][loginfo
->type
]);
230 logger
= rcu_dereference(net
->nf
.nf_loggers
[pf
]);
234 vsnprintf(prefix
, sizeof(prefix
), fmt
, args
);
236 logger
->logfn(net
, pf
, hooknum
, skb
, in
, out
, loginfo
, prefix
);
240 EXPORT_SYMBOL(nf_log_packet
);
242 void nf_log_trace(struct net
*net
,
244 unsigned int hooknum
,
245 const struct sk_buff
*skb
,
246 const struct net_device
*in
,
247 const struct net_device
*out
,
248 const struct nf_loginfo
*loginfo
, const char *fmt
, ...)
251 char prefix
[NF_LOG_PREFIXLEN
];
252 const struct nf_logger
*logger
;
255 logger
= rcu_dereference(net
->nf
.nf_loggers
[pf
]);
258 vsnprintf(prefix
, sizeof(prefix
), fmt
, args
);
260 logger
->logfn(net
, pf
, hooknum
, skb
, in
, out
, loginfo
, prefix
);
264 EXPORT_SYMBOL(nf_log_trace
);
266 #define S_SIZE (1024 - (sizeof(unsigned int) + 1))
270 char buf
[S_SIZE
+ 1];
272 static struct nf_log_buf emergency
, *emergency_ptr
= &emergency
;
274 __printf(2, 3) int nf_log_buf_add(struct nf_log_buf
*m
, const char *f
, ...)
279 if (likely(m
->count
< S_SIZE
)) {
281 len
= vsnprintf(m
->buf
+ m
->count
, S_SIZE
- m
->count
, f
, args
);
283 if (likely(m
->count
+ len
< S_SIZE
)) {
289 printk_once(KERN_ERR KBUILD_MODNAME
" please increase S_SIZE\n");
292 EXPORT_SYMBOL_GPL(nf_log_buf_add
);
294 struct nf_log_buf
*nf_log_buf_open(void)
296 struct nf_log_buf
*m
= kmalloc(sizeof(*m
), GFP_ATOMIC
);
301 m
= xchg(&emergency_ptr
, NULL
);
307 EXPORT_SYMBOL_GPL(nf_log_buf_open
);
309 void nf_log_buf_close(struct nf_log_buf
*m
)
311 m
->buf
[m
->count
] = 0;
312 printk("%s\n", m
->buf
);
314 if (likely(m
!= &emergency
))
321 EXPORT_SYMBOL_GPL(nf_log_buf_close
);
323 #ifdef CONFIG_PROC_FS
324 static void *seq_start(struct seq_file
*seq
, loff_t
*pos
)
326 struct net
*net
= seq_file_net(seq
);
328 mutex_lock(&nf_log_mutex
);
330 if (*pos
>= ARRAY_SIZE(net
->nf
.nf_loggers
))
336 static void *seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
338 struct net
*net
= seq_file_net(s
);
342 if (*pos
>= ARRAY_SIZE(net
->nf
.nf_loggers
))
348 static void seq_stop(struct seq_file
*s
, void *v
)
350 mutex_unlock(&nf_log_mutex
);
353 static int seq_show(struct seq_file
*s
, void *v
)
356 const struct nf_logger
*logger
;
358 struct net
*net
= seq_file_net(s
);
360 logger
= nft_log_dereference(net
->nf
.nf_loggers
[*pos
]);
363 seq_printf(s
, "%2lld NONE (", *pos
);
365 seq_printf(s
, "%2lld %s (", *pos
, logger
->name
);
367 if (seq_has_overflowed(s
))
370 for (i
= 0; i
< NF_LOG_TYPE_MAX
; i
++) {
371 if (loggers
[*pos
][i
] == NULL
)
374 logger
= nft_log_dereference(loggers
[*pos
][i
]);
375 seq_printf(s
, "%s", logger
->name
);
376 if (i
== 0 && loggers
[*pos
][i
+ 1] != NULL
)
379 if (seq_has_overflowed(s
))
383 seq_printf(s
, ")\n");
385 if (seq_has_overflowed(s
))
390 static const struct seq_operations nflog_seq_ops
= {
397 static int nflog_open(struct inode
*inode
, struct file
*file
)
399 return seq_open_net(inode
, file
, &nflog_seq_ops
,
400 sizeof(struct seq_net_private
));
403 static const struct file_operations nflog_file_ops
= {
404 .owner
= THIS_MODULE
,
408 .release
= seq_release_net
,
415 static char nf_log_sysctl_fnames
[NFPROTO_NUMPROTO
-NFPROTO_UNSPEC
][3];
416 static struct ctl_table nf_log_sysctl_table
[NFPROTO_NUMPROTO
+1];
418 static int nf_log_proc_dostring(struct ctl_table
*table
, int write
,
419 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
421 const struct nf_logger
*logger
;
422 char buf
[NFLOGGER_NAME_LEN
];
424 int tindex
= (unsigned long)table
->extra1
;
425 struct net
*net
= table
->extra2
;
428 struct ctl_table tmp
= *table
;
431 r
= proc_dostring(&tmp
, write
, buffer
, lenp
, ppos
);
435 if (!strcmp(buf
, "NONE")) {
436 nf_log_unbind_pf(net
, tindex
);
439 mutex_lock(&nf_log_mutex
);
440 logger
= __find_logger(tindex
, buf
);
441 if (logger
== NULL
) {
442 mutex_unlock(&nf_log_mutex
);
445 rcu_assign_pointer(net
->nf
.nf_loggers
[tindex
], logger
);
446 mutex_unlock(&nf_log_mutex
);
448 mutex_lock(&nf_log_mutex
);
449 logger
= nft_log_dereference(net
->nf
.nf_loggers
[tindex
]);
451 table
->data
= "NONE";
453 table
->data
= logger
->name
;
454 r
= proc_dostring(table
, write
, buffer
, lenp
, ppos
);
455 mutex_unlock(&nf_log_mutex
);
461 static int netfilter_log_sysctl_init(struct net
*net
)
464 struct ctl_table
*table
;
466 table
= nf_log_sysctl_table
;
467 if (!net_eq(net
, &init_net
)) {
468 table
= kmemdup(nf_log_sysctl_table
,
469 sizeof(nf_log_sysctl_table
),
474 for (i
= NFPROTO_UNSPEC
; i
< NFPROTO_NUMPROTO
; i
++) {
475 snprintf(nf_log_sysctl_fnames
[i
],
477 nf_log_sysctl_table
[i
].procname
=
478 nf_log_sysctl_fnames
[i
];
479 nf_log_sysctl_table
[i
].maxlen
= NFLOGGER_NAME_LEN
;
480 nf_log_sysctl_table
[i
].mode
= 0644;
481 nf_log_sysctl_table
[i
].proc_handler
=
482 nf_log_proc_dostring
;
483 nf_log_sysctl_table
[i
].extra1
=
484 (void *)(unsigned long) i
;
488 for (i
= NFPROTO_UNSPEC
; i
< NFPROTO_NUMPROTO
; i
++)
489 table
[i
].extra2
= net
;
491 net
->nf
.nf_log_dir_header
= register_net_sysctl(net
,
492 "net/netfilter/nf_log",
494 if (!net
->nf
.nf_log_dir_header
)
500 if (!net_eq(net
, &init_net
))
506 static void netfilter_log_sysctl_exit(struct net
*net
)
508 struct ctl_table
*table
;
510 table
= net
->nf
.nf_log_dir_header
->ctl_table_arg
;
511 unregister_net_sysctl_table(net
->nf
.nf_log_dir_header
);
512 if (!net_eq(net
, &init_net
))
516 static int netfilter_log_sysctl_init(struct net
*net
)
521 static void netfilter_log_sysctl_exit(struct net
*net
)
524 #endif /* CONFIG_SYSCTL */
526 static int __net_init
nf_log_net_init(struct net
*net
)
530 #ifdef CONFIG_PROC_FS
531 if (!proc_create("nf_log", S_IRUGO
,
532 net
->nf
.proc_netfilter
, &nflog_file_ops
))
535 ret
= netfilter_log_sysctl_init(net
);
542 #ifdef CONFIG_PROC_FS
543 remove_proc_entry("nf_log", net
->nf
.proc_netfilter
);
548 static void __net_exit
nf_log_net_exit(struct net
*net
)
550 netfilter_log_sysctl_exit(net
);
551 #ifdef CONFIG_PROC_FS
552 remove_proc_entry("nf_log", net
->nf
.proc_netfilter
);
556 static struct pernet_operations nf_log_net_ops
= {
557 .init
= nf_log_net_init
,
558 .exit
= nf_log_net_exit
,
561 int __init
netfilter_log_init(void)
563 return register_pernet_subsys(&nf_log_net_ops
);