Blame view

net/netfilter/nf_log.c 7.07 KB
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
1
2
3
4
5
6
  #include <linux/kernel.h>
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/proc_fs.h>
  #include <linux/skbuff.h>
  #include <linux/netfilter.h>
bbd86b9fc   Harald Welte   [NETFILTER]: add ...
7
  #include <linux/seq_file.h>
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
8
  #include <net/protocol.h>
f01ffbd6e   Patrick McHardy   [NETFILTER]: nf_l...
9
  #include <net/netfilter/nf_log.h>
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
10
11
  
  #include "nf_internals.h"
a5d292646   YOSHIFUJI Hideaki   [NET] NETFILTER: ...
12
  /* Internal logging interface, which relies on the real
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
13
14
15
     LOG target modules */
  
  #define NF_LOG_PREFIXLEN		128
176252746   Eric Leblond   netfilter: sysctl...
16
  #define NFLOGGER_NAME_LEN		64
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
17

0906a372f   Arnd Bergmann   net/netfilter: __...
18
  static const struct nf_logger __rcu *nf_loggers[NFPROTO_NUMPROTO] __read_mostly;
ca735b3aa   Eric Leblond   netfilter: use a ...
19
  static struct list_head nf_loggers_l[NFPROTO_NUMPROTO] __read_mostly;
9b73534dc   Patrick McHardy   [NETFILTER]: nf_l...
20
  static DEFINE_MUTEX(nf_log_mutex);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
21

ca735b3aa   Eric Leblond   netfilter: use a ...
22
  static struct nf_logger *__find_logger(int pf, const char *str_logger)
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
23
  {
ca735b3aa   Eric Leblond   netfilter: use a ...
24
  	struct nf_logger *t;
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
25

ca735b3aa   Eric Leblond   netfilter: use a ...
26
27
28
29
  	list_for_each_entry(t, &nf_loggers_l[pf], list[pf]) {
  		if (!strnicmp(str_logger, t->name, strlen(t->name)))
  			return t;
  	}
d72367b6f   Harald Welte   [NETFILTER]: more...
30

ca735b3aa   Eric Leblond   netfilter: use a ...
31
  	return NULL;
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
32
  }
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
33

ca735b3aa   Eric Leblond   netfilter: use a ...
34
35
  /* return EEXIST if the same logger is registred, 0 on success. */
  int nf_log_register(u_int8_t pf, struct nf_logger *logger)
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
36
  {
b56f2d55c   Patrick McHardy   netfilter: use rc...
37
  	const struct nf_logger *llog;
b6f0a3652   Eric Dumazet   netfilter: nf_log...
38
  	int i;
ca735b3aa   Eric Leblond   netfilter: use a ...
39

7e9c6eeb1   Jan Engelhardt   netfilter: Introd...
40
  	if (pf >= ARRAY_SIZE(nf_loggers))
ca735b3aa   Eric Leblond   netfilter: use a ...
41
  		return -EINVAL;
b6f0a3652   Eric Dumazet   netfilter: nf_log...
42
43
  	for (i = 0; i < ARRAY_SIZE(logger->list); i++)
  		INIT_LIST_HEAD(&logger->list[i]);
9b73534dc   Patrick McHardy   [NETFILTER]: nf_l...
44
  	mutex_lock(&nf_log_mutex);
ca735b3aa   Eric Leblond   netfilter: use a ...
45
46
  
  	if (pf == NFPROTO_UNSPEC) {
ca735b3aa   Eric Leblond   netfilter: use a ...
47
48
49
50
51
  		for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
  			list_add_tail(&(logger->list[i]), &(nf_loggers_l[i]));
  	} else {
  		/* register at end of list to honor first register win */
  		list_add_tail(&logger->list[pf], &nf_loggers_l[pf]);
b56f2d55c   Patrick McHardy   netfilter: use rc...
52
53
54
  		llog = rcu_dereference_protected(nf_loggers[pf],
  						 lockdep_is_held(&nf_log_mutex));
  		if (llog == NULL)
ca735b3aa   Eric Leblond   netfilter: use a ...
55
56
  			rcu_assign_pointer(nf_loggers[pf], logger);
  	}
9b73534dc   Patrick McHardy   [NETFILTER]: nf_l...
57
  	mutex_unlock(&nf_log_mutex);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
58

ca735b3aa   Eric Leblond   netfilter: use a ...
59
  	return 0;
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
60
  }
ca735b3aa   Eric Leblond   netfilter: use a ...
61
  EXPORT_SYMBOL(nf_log_register);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
62

ca735b3aa   Eric Leblond   netfilter: use a ...
63
  void nf_log_unregister(struct nf_logger *logger)
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
64
  {
b56f2d55c   Patrick McHardy   netfilter: use rc...
65
  	const struct nf_logger *c_logger;
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
66
  	int i;
9b73534dc   Patrick McHardy   [NETFILTER]: nf_l...
67
  	mutex_lock(&nf_log_mutex);
7e9c6eeb1   Jan Engelhardt   netfilter: Introd...
68
  	for (i = 0; i < ARRAY_SIZE(nf_loggers); i++) {
b56f2d55c   Patrick McHardy   netfilter: use rc...
69
70
71
  		c_logger = rcu_dereference_protected(nf_loggers[i],
  						     lockdep_is_held(&nf_log_mutex));
  		if (c_logger == logger)
e92ad99c7   Patrick McHardy   [NETFILTER]: nf_l...
72
  			rcu_assign_pointer(nf_loggers[i], NULL);
ca735b3aa   Eric Leblond   netfilter: use a ...
73
  		list_del(&logger->list[i]);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
74
  	}
9b73534dc   Patrick McHardy   [NETFILTER]: nf_l...
75
  	mutex_unlock(&nf_log_mutex);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
76

a5ea6169f   Patrick McHardy   [NETFILTER]: nf_l...
77
  	synchronize_rcu();
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
78
  }
e92ad99c7   Patrick McHardy   [NETFILTER]: nf_l...
79
  EXPORT_SYMBOL(nf_log_unregister);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
80

ca735b3aa   Eric Leblond   netfilter: use a ...
81
82
  int nf_log_bind_pf(u_int8_t pf, const struct nf_logger *logger)
  {
9ef0298a8   Jan Engelhardt   netfilter: nf_log...
83
84
  	if (pf >= ARRAY_SIZE(nf_loggers))
  		return -EINVAL;
ca735b3aa   Eric Leblond   netfilter: use a ...
85
86
87
88
89
90
91
92
93
94
95
96
97
  	mutex_lock(&nf_log_mutex);
  	if (__find_logger(pf, logger->name) == NULL) {
  		mutex_unlock(&nf_log_mutex);
  		return -ENOENT;
  	}
  	rcu_assign_pointer(nf_loggers[pf], logger);
  	mutex_unlock(&nf_log_mutex);
  	return 0;
  }
  EXPORT_SYMBOL(nf_log_bind_pf);
  
  void nf_log_unbind_pf(u_int8_t pf)
  {
9ef0298a8   Jan Engelhardt   netfilter: nf_log...
98
99
  	if (pf >= ARRAY_SIZE(nf_loggers))
  		return;
ca735b3aa   Eric Leblond   netfilter: use a ...
100
101
102
103
104
  	mutex_lock(&nf_log_mutex);
  	rcu_assign_pointer(nf_loggers[pf], NULL);
  	mutex_unlock(&nf_log_mutex);
  }
  EXPORT_SYMBOL(nf_log_unbind_pf);
76108cea0   Jan Engelhardt   netfilter: Use un...
105
  void nf_log_packet(u_int8_t pf,
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
106
107
108
109
  		   unsigned int hooknum,
  		   const struct sk_buff *skb,
  		   const struct net_device *in,
  		   const struct net_device *out,
7b2f9631e   Patrick McHardy   [NETFILTER]: nf_l...
110
  		   const struct nf_loginfo *loginfo,
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
111
112
113
114
  		   const char *fmt, ...)
  {
  	va_list args;
  	char prefix[NF_LOG_PREFIXLEN];
7b2f9631e   Patrick McHardy   [NETFILTER]: nf_l...
115
  	const struct nf_logger *logger;
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
116

f6ebe77f9   Harald Welte   [NETFILTER]: spli...
117
  	rcu_read_lock();
e92ad99c7   Patrick McHardy   [NETFILTER]: nf_l...
118
  	logger = rcu_dereference(nf_loggers[pf]);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
119
120
121
122
  	if (logger) {
  		va_start(args, fmt);
  		vsnprintf(prefix, sizeof(prefix), fmt, args);
  		va_end(args);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
123
  		logger->logfn(pf, hooknum, skb, in, out, loginfo, prefix);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
124
125
126
127
128
129
130
131
  	}
  	rcu_read_unlock();
  }
  EXPORT_SYMBOL(nf_log_packet);
  
  #ifdef CONFIG_PROC_FS
  static void *seq_start(struct seq_file *seq, loff_t *pos)
  {
6440fe059   Patrick McHardy   netfilter: nf_log...
132
  	mutex_lock(&nf_log_mutex);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
133

7e9c6eeb1   Jan Engelhardt   netfilter: Introd...
134
  	if (*pos >= ARRAY_SIZE(nf_loggers))
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
135
136
137
138
139
140
141
142
  		return NULL;
  
  	return pos;
  }
  
  static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
  {
  	(*pos)++;
7e9c6eeb1   Jan Engelhardt   netfilter: Introd...
143
  	if (*pos >= ARRAY_SIZE(nf_loggers))
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
144
145
146
147
148
149
150
  		return NULL;
  
  	return pos;
  }
  
  static void seq_stop(struct seq_file *s, void *v)
  {
6440fe059   Patrick McHardy   netfilter: nf_log...
151
  	mutex_unlock(&nf_log_mutex);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
152
153
154
155
156
157
  }
  
  static int seq_show(struct seq_file *s, void *v)
  {
  	loff_t *pos = v;
  	const struct nf_logger *logger;
c7a913cd5   Eric Leblond   netfilter: print ...
158
159
  	struct nf_logger *t;
  	int ret;
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
160

6440fe059   Patrick McHardy   netfilter: nf_log...
161
  	logger = nf_loggers[*pos];
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
162
163
  
  	if (!logger)
c7a913cd5   Eric Leblond   netfilter: print ...
164
165
166
167
168
169
  		ret = seq_printf(s, "%2lld NONE (", *pos);
  	else
  		ret = seq_printf(s, "%2lld %s (", *pos, logger->name);
  
  	if (ret < 0)
  		return ret;
c7a913cd5   Eric Leblond   netfilter: print ...
170
171
  	list_for_each_entry(t, &nf_loggers_l[*pos], list[*pos]) {
  		ret = seq_printf(s, "%s", t->name);
6440fe059   Patrick McHardy   netfilter: nf_log...
172
  		if (ret < 0)
c7a913cd5   Eric Leblond   netfilter: print ...
173
  			return ret;
c7a913cd5   Eric Leblond   netfilter: print ...
174
175
  		if (&t->list[*pos] != nf_loggers_l[*pos].prev) {
  			ret = seq_printf(s, ",");
6440fe059   Patrick McHardy   netfilter: nf_log...
176
  			if (ret < 0)
c7a913cd5   Eric Leblond   netfilter: print ...
177
  				return ret;
c7a913cd5   Eric Leblond   netfilter: print ...
178
179
  		}
  	}
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
180

c7a913cd5   Eric Leblond   netfilter: print ...
181
182
  	return seq_printf(s, ")
  ");
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
183
  }
56b3d975b   Philippe De Muyter   [NET]: Make all i...
184
  static const struct seq_operations nflog_seq_ops = {
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
185
186
187
188
189
190
191
192
193
194
  	.start	= seq_start,
  	.next	= seq_next,
  	.stop	= seq_stop,
  	.show	= seq_show,
  };
  
  static int nflog_open(struct inode *inode, struct file *file)
  {
  	return seq_open(file, &nflog_seq_ops);
  }
da7071d7e   Arjan van de Ven   [PATCH] mark stru...
195
  static const struct file_operations nflog_file_ops = {
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
196
197
198
199
200
201
  	.owner	 = THIS_MODULE,
  	.open	 = nflog_open,
  	.read	 = seq_read,
  	.llseek	 = seq_lseek,
  	.release = seq_release,
  };
176252746   Eric Leblond   netfilter: sysctl...
202

f6ebe77f9   Harald Welte   [NETFILTER]: spli...
203
  #endif /* PROC_FS */
176252746   Eric Leblond   netfilter: sysctl...
204
  #ifdef CONFIG_SYSCTL
249556192   Patrick McHardy   netfilter: nf_log...
205
  static struct ctl_path nf_log_sysctl_path[] = {
f8572d8f2   Eric W. Biederman   sysctl net: Remov...
206
207
208
  	{ .procname = "net", },
  	{ .procname = "netfilter", },
  	{ .procname = "nf_log", },
176252746   Eric Leblond   netfilter: sysctl...
209
210
211
212
213
214
  	{ }
  };
  
  static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
  static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1];
  static struct ctl_table_header *nf_log_dir_header;
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
215

8d65af789   Alexey Dobriyan   sysctl: remove "s...
216
  static int nf_log_proc_dostring(ctl_table *table, int write,
249556192   Patrick McHardy   netfilter: nf_log...
217
  			 void __user *buffer, size_t *lenp, loff_t *ppos)
176252746   Eric Leblond   netfilter: sysctl...
218
219
  {
  	const struct nf_logger *logger;
249556192   Patrick McHardy   netfilter: nf_log...
220
221
  	char buf[NFLOGGER_NAME_LEN];
  	size_t size = *lenp;
176252746   Eric Leblond   netfilter: sysctl...
222
223
224
225
  	int r = 0;
  	int tindex = (unsigned long)table->extra1;
  
  	if (write) {
249556192   Patrick McHardy   netfilter: nf_log...
226
227
228
229
230
231
  		if (size > sizeof(buf))
  			size = sizeof(buf);
  		if (copy_from_user(buf, buffer, size))
  			return -EFAULT;
  
  		if (!strcmp(buf, "NONE")) {
176252746   Eric Leblond   netfilter: sysctl...
232
233
234
235
  			nf_log_unbind_pf(tindex);
  			return 0;
  		}
  		mutex_lock(&nf_log_mutex);
249556192   Patrick McHardy   netfilter: nf_log...
236
  		logger = __find_logger(tindex, buf);
176252746   Eric Leblond   netfilter: sysctl...
237
238
239
240
241
242
243
  		if (logger == NULL) {
  			mutex_unlock(&nf_log_mutex);
  			return -ENOENT;
  		}
  		rcu_assign_pointer(nf_loggers[tindex], logger);
  		mutex_unlock(&nf_log_mutex);
  	} else {
266d07cb1   Patrick McHardy   netfilter: nf_log...
244
245
  		mutex_lock(&nf_log_mutex);
  		logger = nf_loggers[tindex];
176252746   Eric Leblond   netfilter: sysctl...
246
247
248
249
  		if (!logger)
  			table->data = "NONE";
  		else
  			table->data = logger->name;
8d65af789   Alexey Dobriyan   sysctl: remove "s...
250
  		r = proc_dostring(table, write, buffer, lenp, ppos);
266d07cb1   Patrick McHardy   netfilter: nf_log...
251
  		mutex_unlock(&nf_log_mutex);
176252746   Eric Leblond   netfilter: sysctl...
252
253
254
255
256
257
  	}
  
  	return r;
  }
  
  static __init int netfilter_log_sysctl_init(void)
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
258
  {
ca735b3aa   Eric Leblond   netfilter: use a ...
259
  	int i;
176252746   Eric Leblond   netfilter: sysctl...
260
261
262
  
  	for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
  		snprintf(nf_log_sysctl_fnames[i-NFPROTO_UNSPEC], 3, "%d", i);
176252746   Eric Leblond   netfilter: sysctl...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  		nf_log_sysctl_table[i].procname	=
  			nf_log_sysctl_fnames[i-NFPROTO_UNSPEC];
  		nf_log_sysctl_table[i].data = NULL;
  		nf_log_sysctl_table[i].maxlen =
  			NFLOGGER_NAME_LEN * sizeof(char);
  		nf_log_sysctl_table[i].mode = 0644;
  		nf_log_sysctl_table[i].proc_handler = nf_log_proc_dostring;
  		nf_log_sysctl_table[i].extra1 = (void *)(unsigned long) i;
  	}
  
  	nf_log_dir_header = register_sysctl_paths(nf_log_sysctl_path,
  				       nf_log_sysctl_table);
  	if (!nf_log_dir_header)
  		return -ENOMEM;
  
  	return 0;
  }
  #else
  static __init int netfilter_log_sysctl_init(void)
  {
  	return 0;
  }
  #endif /* CONFIG_SYSCTL */
  
  int __init netfilter_log_init(void)
  {
  	int i, r;
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
290
  #ifdef CONFIG_PROC_FS
8eeee8b15   Denis V. Lunev   [NETFILTER]: Repl...
291
292
  	if (!proc_create("nf_log", S_IRUGO,
  			 proc_net_netfilter, &nflog_file_ops))
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
293
  		return -1;
622439270   Harald Welte   [NETFILTER]: Fix ...
294
  #endif
ca735b3aa   Eric Leblond   netfilter: use a ...
295

176252746   Eric Leblond   netfilter: sysctl...
296
297
298
299
  	/* Errors will trigger panic, unroll on error is unnecessary. */
  	r = netfilter_log_sysctl_init();
  	if (r < 0)
  		return r;
ca735b3aa   Eric Leblond   netfilter: use a ...
300
301
  	for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
  		INIT_LIST_HEAD(&(nf_loggers_l[i]));
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
302
303
  	return 0;
  }