File Index
Files and directories
Sign in
FILE

nf_log.c

File preview with syntax highlighting.

linux-6.19.14/net/netfilter/nf_log.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2#include <linux/kernel.h>
  3#include <linux/init.h>
  4#include <linux/module.h>
  5#include <linux/proc_fs.h>
  6#include <linux/skbuff.h>
  7#include <linux/netfilter.h>
  8#include <linux/seq_file.h>
  9#include <net/protocol.h>
 10#include <net/netfilter/nf_log.h>
 11
 12#include "nf_internals.h"
 13
 14/* Internal logging interface, which relies on the real
 15   LOG target modules */
 16
 17#define NFLOGGER_NAME_LEN		64
 18
 19int sysctl_nf_log_all_netns __read_mostly;
 20EXPORT_SYMBOL(sysctl_nf_log_all_netns);
 21
 22static struct nf_logger __rcu *loggers[NFPROTO_NUMPROTO][NF_LOG_TYPE_MAX] __read_mostly;
 23static DEFINE_MUTEX(nf_log_mutex);
 24
 25#define nft_log_dereference(logger) \
 26	rcu_dereference_protected(logger, lockdep_is_held(&nf_log_mutex))
 27
 28static struct nf_logger *__find_logger(int pf, const char *str_logger)
 29{
 30	struct nf_logger *log;
 31	int i;
 32
 33	for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
 34		log = nft_log_dereference(loggers[pf][i]);
 35		if (!log)
 36			continue;
 37
 38		if (!strncasecmp(str_logger, log->name, strlen(log->name)))
 39			return log;
 40	}
 41
 42	return NULL;
 43}
 44
 45int nf_log_set(struct net *net, u_int8_t pf, const struct nf_logger *logger)
 46{
 47	const struct nf_logger *log;
 48
 49	if (pf == NFPROTO_UNSPEC || pf >= ARRAY_SIZE(net->nf.nf_loggers))
 50		return -EOPNOTSUPP;
 51
 52	mutex_lock(&nf_log_mutex);
 53	log = nft_log_dereference(net->nf.nf_loggers[pf]);
 54	if (log == NULL)
 55		rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
 56
 57	mutex_unlock(&nf_log_mutex);
 58
 59	return 0;
 60}
 61EXPORT_SYMBOL(nf_log_set);
 62
 63void nf_log_unset(struct net *net, const struct nf_logger *logger)
 64{
 65	int i;
 66	const struct nf_logger *log;
 67
 68	mutex_lock(&nf_log_mutex);
 69	for (i = 0; i < NFPROTO_NUMPROTO; i++) {
 70		log = nft_log_dereference(net->nf.nf_loggers[i]);
 71		if (log == logger)
 72			RCU_INIT_POINTER(net->nf.nf_loggers[i], NULL);
 73	}
 74	mutex_unlock(&nf_log_mutex);
 75}
 76EXPORT_SYMBOL(nf_log_unset);
 77
 78/* return EEXIST if the same logger is registered, 0 on success. */
 79int nf_log_register(u_int8_t pf, struct nf_logger *logger)
 80{
 81	int i;
 82	int ret = 0;
 83
 84	if (pf >= ARRAY_SIZE(init_net.nf.nf_loggers))
 85		return -EINVAL;
 86
 87	mutex_lock(&nf_log_mutex);
 88
 89	if (pf == NFPROTO_UNSPEC) {
 90		for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
 91			if (rcu_access_pointer(loggers[i][logger->type])) {
 92				ret = -EBUSY;
 93				goto unlock;
 94			}
 95		}
 96		for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
 97			rcu_assign_pointer(loggers[i][logger->type], logger);
 98	} else {
 99		if (rcu_access_pointer(loggers[pf][logger->type])) {
100			ret = -EBUSY;
101			goto unlock;
102		}
103		rcu_assign_pointer(loggers[pf][logger->type], logger);
104	}
105
106unlock:
107	mutex_unlock(&nf_log_mutex);
108	return ret;
109}
110EXPORT_SYMBOL(nf_log_register);
111
112void nf_log_unregister(struct nf_logger *logger)
113{
114	const struct nf_logger *log;
115	int i;
116
117	mutex_lock(&nf_log_mutex);
118	for (i = 0; i < NFPROTO_NUMPROTO; i++) {
119		log = nft_log_dereference(loggers[i][logger->type]);
120		if (log == logger)
121			RCU_INIT_POINTER(loggers[i][logger->type], NULL);
122	}
123	mutex_unlock(&nf_log_mutex);
124	synchronize_rcu();
125}
126EXPORT_SYMBOL(nf_log_unregister);
127
128/**
129 * nf_log_is_registered - Check if any logger is registered for a given
130 * protocol family.
131 *
132 * @pf: Protocol family
133 *
134 * Returns: true if at least one logger is active for @pf, false otherwise.
135 */
136bool nf_log_is_registered(u_int8_t pf)
137{
138	int i;
139
140	if (pf >= NFPROTO_NUMPROTO) {
141		WARN_ON_ONCE(1);
142		return false;
143	}
144
145	for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
146		if (rcu_access_pointer(loggers[pf][i]))
147			return true;
148	}
149
150	return false;
151}
152EXPORT_SYMBOL(nf_log_is_registered);
153
154int nf_log_bind_pf(struct net *net, u_int8_t pf,
155		   const struct nf_logger *logger)
156{
157	if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
158		return -EINVAL;
159	mutex_lock(&nf_log_mutex);
160	if (__find_logger(pf, logger->name) == NULL) {
161		mutex_unlock(&nf_log_mutex);
162		return -ENOENT;
163	}
164	rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
165	mutex_unlock(&nf_log_mutex);
166	return 0;
167}
168EXPORT_SYMBOL(nf_log_bind_pf);
169
170void nf_log_unbind_pf(struct net *net, u_int8_t pf)
171{
172	if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
173		return;
174	mutex_lock(&nf_log_mutex);
175	RCU_INIT_POINTER(net->nf.nf_loggers[pf], NULL);
176	mutex_unlock(&nf_log_mutex);
177}
178EXPORT_SYMBOL(nf_log_unbind_pf);
179
180int nf_logger_find_get(int pf, enum nf_log_type type)
181{
182	struct nf_logger *logger;
183	int ret = -ENOENT;
184
185	if (pf >= ARRAY_SIZE(loggers))
186		return -EINVAL;
187	if (type >= NF_LOG_TYPE_MAX)
188		return -EINVAL;
189
190	if (pf == NFPROTO_INET) {
191		ret = nf_logger_find_get(NFPROTO_IPV4, type);
192		if (ret < 0)
193			return ret;
194
195		ret = nf_logger_find_get(NFPROTO_IPV6, type);
196		if (ret < 0) {
197			nf_logger_put(NFPROTO_IPV4, type);
198			return ret;
199		}
200
201		return 0;
202	}
203
204	rcu_read_lock();
205	logger = rcu_dereference(loggers[pf][type]);
206	if (logger == NULL)
207		goto out;
208
209	if (try_module_get(logger->me))
210		ret = 0;
211out:
212	rcu_read_unlock();
213	return ret;
214}
215EXPORT_SYMBOL_GPL(nf_logger_find_get);
216
217void nf_logger_put(int pf, enum nf_log_type type)
218{
219	struct nf_logger *logger;
220
221	if (pf == NFPROTO_INET) {
222		nf_logger_put(NFPROTO_IPV4, type);
223		nf_logger_put(NFPROTO_IPV6, type);
224		return;
225	}
226
227	rcu_read_lock();
228	logger = rcu_dereference(loggers[pf][type]);
229	if (!logger)
230		WARN_ON_ONCE(1);
231	else
232		module_put(logger->me);
233	rcu_read_unlock();
234}
235EXPORT_SYMBOL_GPL(nf_logger_put);
236
237void nf_log_packet(struct net *net,
238		   u_int8_t pf,
239		   unsigned int hooknum,
240		   const struct sk_buff *skb,
241		   const struct net_device *in,
242		   const struct net_device *out,
243		   const struct nf_loginfo *loginfo,
244		   const char *fmt, ...)
245{
246	va_list args;
247	char prefix[NF_LOG_PREFIXLEN];
248	const struct nf_logger *logger;
249
250	rcu_read_lock();
251	if (loginfo != NULL)
252		logger = rcu_dereference(loggers[pf][loginfo->type]);
253	else
254		logger = rcu_dereference(net->nf.nf_loggers[pf]);
255
256	if (logger) {
257		va_start(args, fmt);
258		vsnprintf(prefix, sizeof(prefix), fmt, args);
259		va_end(args);
260		logger->logfn(net, pf, hooknum, skb, in, out, loginfo, prefix);
261	}
262	rcu_read_unlock();
263}
264EXPORT_SYMBOL(nf_log_packet);
265
266void nf_log_trace(struct net *net,
267		  u_int8_t pf,
268		  unsigned int hooknum,
269		  const struct sk_buff *skb,
270		  const struct net_device *in,
271		  const struct net_device *out,
272		  const struct nf_loginfo *loginfo, const char *fmt, ...)
273{
274	va_list args;
275	char prefix[NF_LOG_PREFIXLEN];
276	const struct nf_logger *logger;
277
278	rcu_read_lock();
279	logger = rcu_dereference(net->nf.nf_loggers[pf]);
280	if (logger) {
281		va_start(args, fmt);
282		vsnprintf(prefix, sizeof(prefix), fmt, args);
283		va_end(args);
284		logger->logfn(net, pf, hooknum, skb, in, out, loginfo, prefix);
285	}
286	rcu_read_unlock();
287}
288EXPORT_SYMBOL(nf_log_trace);
289
290#define S_SIZE (1024 - (sizeof(unsigned int) + 1))
291
292struct nf_log_buf {
293	unsigned int	count;
294	char		buf[S_SIZE + 1];
295};
296static struct nf_log_buf emergency, *emergency_ptr = &emergency;
297
298__printf(2, 3) int nf_log_buf_add(struct nf_log_buf *m, const char *f, ...)
299{
300	va_list args;
301	int len;
302
303	if (likely(m->count < S_SIZE)) {
304		va_start(args, f);
305		len = vsnprintf(m->buf + m->count, S_SIZE - m->count, f, args);
306		va_end(args);
307		if (likely(m->count + len < S_SIZE)) {
308			m->count += len;
309			return 0;
310		}
311	}
312	m->count = S_SIZE;
313	printk_once(KERN_ERR KBUILD_MODNAME " please increase S_SIZE\n");
314	return -1;
315}
316EXPORT_SYMBOL_GPL(nf_log_buf_add);
317
318struct nf_log_buf *nf_log_buf_open(void)
319{
320	struct nf_log_buf *m = kmalloc(sizeof(*m), GFP_ATOMIC);
321
322	if (unlikely(!m)) {
323		local_bh_disable();
324		do {
325			m = xchg(&emergency_ptr, NULL);
326		} while (!m);
327	}
328	m->count = 0;
329	return m;
330}
331EXPORT_SYMBOL_GPL(nf_log_buf_open);
332
333void nf_log_buf_close(struct nf_log_buf *m)
334{
335	m->buf[m->count] = 0;
336	printk("%s\n", m->buf);
337
338	if (likely(m != &emergency))
339		kfree(m);
340	else {
341		emergency_ptr = m;
342		local_bh_enable();
343	}
344}
345EXPORT_SYMBOL_GPL(nf_log_buf_close);
346
347#ifdef CONFIG_PROC_FS
348static void *seq_start(struct seq_file *seq, loff_t *pos)
349{
350	struct net *net = seq_file_net(seq);
351
352	mutex_lock(&nf_log_mutex);
353
354	if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
355		return NULL;
356
357	return pos;
358}
359
360static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
361{
362	struct net *net = seq_file_net(s);
363
364	(*pos)++;
365
366	if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
367		return NULL;
368
369	return pos;
370}
371
372static void seq_stop(struct seq_file *s, void *v)
373{
374	mutex_unlock(&nf_log_mutex);
375}
376
377static int seq_show(struct seq_file *s, void *v)
378{
379	loff_t *pos = v;
380	const struct nf_logger *logger;
381	int i;
382	struct net *net = seq_file_net(s);
383
384	logger = nft_log_dereference(net->nf.nf_loggers[*pos]);
385
386	if (!logger)
387		seq_printf(s, "%2lld NONE (", *pos);
388	else
389		seq_printf(s, "%2lld %s (", *pos, logger->name);
390
391	if (seq_has_overflowed(s))
392		return -ENOSPC;
393
394	for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
395		if (loggers[*pos][i] == NULL)
396			continue;
397
398		logger = nft_log_dereference(loggers[*pos][i]);
399		seq_puts(s, logger->name);
400		if (i == 0 && loggers[*pos][i + 1] != NULL)
401			seq_puts(s, ",");
402
403		if (seq_has_overflowed(s))
404			return -ENOSPC;
405	}
406
407	seq_puts(s, ")\n");
408
409	if (seq_has_overflowed(s))
410		return -ENOSPC;
411	return 0;
412}
413
414static const struct seq_operations nflog_seq_ops = {
415	.start	= seq_start,
416	.next	= seq_next,
417	.stop	= seq_stop,
418	.show	= seq_show,
419};
420#endif /* PROC_FS */
421
422#ifdef CONFIG_SYSCTL
423static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
424static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO];
425static struct ctl_table_header *nf_log_sysctl_fhdr;
426
427static struct ctl_table nf_log_sysctl_ftable[] = {
428	{
429		.procname	= "nf_log_all_netns",
430		.data		= &sysctl_nf_log_all_netns,
431		.maxlen		= sizeof(sysctl_nf_log_all_netns),
432		.mode		= 0644,
433		.proc_handler	= proc_dointvec,
434	},
435};
436
437static int nf_log_proc_dostring(const struct ctl_table *table, int write,
438			 void *buffer, size_t *lenp, loff_t *ppos)
439{
440	const struct nf_logger *logger;
441	char buf[NFLOGGER_NAME_LEN];
442	int r = 0;
443	int tindex = (unsigned long)table->extra1;
444	struct net *net = table->extra2;
445
446	if (write) {
447		struct ctl_table tmp = *table;
448
449		/* proc_dostring() can append to existing strings, so we need to
450		 * initialize it as an empty string.
451		 */
452		buf[0] = '\0';
453		tmp.data = buf;
454		r = proc_dostring(&tmp, write, buffer, lenp, ppos);
455		if (r)
456			return r;
457
458		if (!strcmp(buf, "NONE")) {
459			nf_log_unbind_pf(net, tindex);
460			return 0;
461		}
462		mutex_lock(&nf_log_mutex);
463		logger = __find_logger(tindex, buf);
464		if (logger == NULL) {
465			mutex_unlock(&nf_log_mutex);
466			return -ENOENT;
467		}
468		rcu_assign_pointer(net->nf.nf_loggers[tindex], logger);
469		mutex_unlock(&nf_log_mutex);
470	} else {
471		struct ctl_table tmp = *table;
472
473		tmp.data = buf;
474		mutex_lock(&nf_log_mutex);
475		logger = nft_log_dereference(net->nf.nf_loggers[tindex]);
476		if (!logger)
477			strscpy(buf, "NONE", sizeof(buf));
478		else
479			strscpy(buf, logger->name, sizeof(buf));
480		mutex_unlock(&nf_log_mutex);
481		r = proc_dostring(&tmp, write, buffer, lenp, ppos);
482	}
483
484	return r;
485}
486
487static int netfilter_log_sysctl_init(struct net *net)
488{
489	int i;
490	struct ctl_table *table;
491
492	table = nf_log_sysctl_table;
493	if (!net_eq(net, &init_net)) {
494		table = kmemdup(nf_log_sysctl_table,
495				 sizeof(nf_log_sysctl_table),
496				 GFP_KERNEL);
497		if (!table)
498			goto err_alloc;
499	} else {
500		for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
501			snprintf(nf_log_sysctl_fnames[i],
502				 3, "%d", i);
503			nf_log_sysctl_table[i].procname	=
504				nf_log_sysctl_fnames[i];
505			nf_log_sysctl_table[i].maxlen = NFLOGGER_NAME_LEN;
506			nf_log_sysctl_table[i].mode = 0644;
507			nf_log_sysctl_table[i].proc_handler =
508				nf_log_proc_dostring;
509			nf_log_sysctl_table[i].extra1 =
510				(void *)(unsigned long) i;
511		}
512		nf_log_sysctl_fhdr = register_net_sysctl(net, "net/netfilter",
513							 nf_log_sysctl_ftable);
514		if (!nf_log_sysctl_fhdr)
515			goto err_freg;
516	}
517
518	for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
519		table[i].extra2 = net;
520
521	net->nf.nf_log_dir_header = register_net_sysctl_sz(net,
522							   "net/netfilter/nf_log",
523							   table,
524							   ARRAY_SIZE(nf_log_sysctl_table));
525	if (!net->nf.nf_log_dir_header)
526		goto err_reg;
527
528	return 0;
529
530err_reg:
531	if (!net_eq(net, &init_net))
532		kfree(table);
533	else
534		unregister_net_sysctl_table(nf_log_sysctl_fhdr);
535err_freg:
536err_alloc:
537	return -ENOMEM;
538}
539
540static void netfilter_log_sysctl_exit(struct net *net)
541{
542	const struct ctl_table *table;
543
544	table = net->nf.nf_log_dir_header->ctl_table_arg;
545	unregister_net_sysctl_table(net->nf.nf_log_dir_header);
546	if (!net_eq(net, &init_net))
547		kfree(table);
548	else
549		unregister_net_sysctl_table(nf_log_sysctl_fhdr);
550}
551#else
552static int netfilter_log_sysctl_init(struct net *net)
553{
554	return 0;
555}
556
557static void netfilter_log_sysctl_exit(struct net *net)
558{
559}
560#endif /* CONFIG_SYSCTL */
561
562static int __net_init nf_log_net_init(struct net *net)
563{
564	int ret = -ENOMEM;
565
566#ifdef CONFIG_PROC_FS
567	if (!proc_create_net("nf_log", 0444, net->nf.proc_netfilter,
568			&nflog_seq_ops, sizeof(struct seq_net_private)))
569		return ret;
570#endif
571	ret = netfilter_log_sysctl_init(net);
572	if (ret < 0)
573		goto out_sysctl;
574
575	return 0;
576
577out_sysctl:
578#ifdef CONFIG_PROC_FS
579	remove_proc_entry("nf_log", net->nf.proc_netfilter);
580#endif
581	return ret;
582}
583
584static void __net_exit nf_log_net_exit(struct net *net)
585{
586	netfilter_log_sysctl_exit(net);
587#ifdef CONFIG_PROC_FS
588	remove_proc_entry("nf_log", net->nf.proc_netfilter);
589#endif
590}
591
592static struct pernet_operations nf_log_net_ops = {
593	.init = nf_log_net_init,
594	.exit = nf_log_net_exit,
595};
596
597int __init netfilter_log_init(void)
598{
599	return register_pernet_subsys(&nf_log_net_ops);
600}
~/ Read-only file preview.