Blame view

net/netfilter/core.c 7.82 KB
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
1
  /* netfilter.c: look after the filters for various protocols.
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
2
3
4
5
6
7
   * Heavily influenced by the old firewall.c by David Bonn and Alan Cox.
   *
   * Thanks to Rob `CmdrTaco' Malda for not influencing this code in any
   * way.
   *
   * Rusty Russell (C)2000 -- This code is GPL.
f229f6ce4   Patrick McHardy   netfilter: add my...
8
   * Patrick McHardy (c) 2006-2012
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
9
   */
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
10
11
12
13
14
15
16
17
18
19
20
21
  #include <linux/kernel.h>
  #include <linux/netfilter.h>
  #include <net/protocol.h>
  #include <linux/init.h>
  #include <linux/skbuff.h>
  #include <linux/wait.h>
  #include <linux/module.h>
  #include <linux/interrupt.h>
  #include <linux/if.h>
  #include <linux/netdevice.h>
  #include <linux/inetdevice.h>
  #include <linux/proc_fs.h>
d486dd1fb   Patrick McHardy   [NETFILTER]: Swit...
22
  #include <linux/mutex.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
23
  #include <linux/slab.h>
457c4cbc5   Eric W. Biederman   [NET]: Make /proc...
24
  #include <net/net_namespace.h>
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
25
26
27
  #include <net/sock.h>
  
  #include "nf_internals.h"
d486dd1fb   Patrick McHardy   [NETFILTER]: Swit...
28
  static DEFINE_MUTEX(afinfo_mutex);
bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
29

0906a372f   Arnd Bergmann   net/netfilter: __...
30
  const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
31
  EXPORT_SYMBOL(nf_afinfo);
2a7851bff   Florian Westphal   netfilter: add nf...
32
33
  const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly;
  EXPORT_SYMBOL_GPL(nf_ipv6_ops);
bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
34

1e796fda0   Patrick McHardy   [NETFILTER]: cons...
35
  int nf_register_afinfo(const struct nf_afinfo *afinfo)
bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
36
  {
d486dd1fb   Patrick McHardy   [NETFILTER]: Swit...
37
38
39
40
41
  	int err;
  
  	err = mutex_lock_interruptible(&afinfo_mutex);
  	if (err < 0)
  		return err;
a9b3cd7f3   Stephen Hemminger   rcu: convert uses...
42
  	RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo);
d486dd1fb   Patrick McHardy   [NETFILTER]: Swit...
43
  	mutex_unlock(&afinfo_mutex);
bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
44
45
46
  	return 0;
  }
  EXPORT_SYMBOL_GPL(nf_register_afinfo);
1e796fda0   Patrick McHardy   [NETFILTER]: cons...
47
  void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
48
  {
d486dd1fb   Patrick McHardy   [NETFILTER]: Swit...
49
  	mutex_lock(&afinfo_mutex);
a9b3cd7f3   Stephen Hemminger   rcu: convert uses...
50
  	RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL);
d486dd1fb   Patrick McHardy   [NETFILTER]: Swit...
51
  	mutex_unlock(&afinfo_mutex);
bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
52
53
54
  	synchronize_rcu();
  }
  EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
7e9c6eeb1   Jan Engelhardt   netfilter: Introd...
55
  struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
56
  EXPORT_SYMBOL(nf_hooks);
a2d7ec58a   Eric Dumazet   netfilter: use ju...
57
58
  
  #if defined(CONFIG_JUMP_LABEL)
c5905afb0   Ingo Molnar   static keys: Intr...
59
  struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
a2d7ec58a   Eric Dumazet   netfilter: use ju...
60
61
  EXPORT_SYMBOL(nf_hooks_needed);
  #endif
fd706d695   Patrick McHardy   [NETFILTER]: Swit...
62
  static DEFINE_MUTEX(nf_hook_mutex);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
63
64
65
  
  int nf_register_hook(struct nf_hook_ops *reg)
  {
4c6109795   Li Zefan   [NETFILTER]: repl...
66
  	struct nf_hook_ops *elem;
fd706d695   Patrick McHardy   [NETFILTER]: Swit...
67
  	int err;
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
68

fd706d695   Patrick McHardy   [NETFILTER]: Swit...
69
70
71
  	err = mutex_lock_interruptible(&nf_hook_mutex);
  	if (err < 0)
  		return err;
4c6109795   Li Zefan   [NETFILTER]: repl...
72
73
  	list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
  		if (reg->priority < elem->priority)
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
74
75
  			break;
  	}
4c6109795   Li Zefan   [NETFILTER]: repl...
76
  	list_add_rcu(&reg->list, elem->list.prev);
fd706d695   Patrick McHardy   [NETFILTER]: Swit...
77
  	mutex_unlock(&nf_hook_mutex);
a2d7ec58a   Eric Dumazet   netfilter: use ju...
78
  #if defined(CONFIG_JUMP_LABEL)
c5905afb0   Ingo Molnar   static keys: Intr...
79
  	static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
a2d7ec58a   Eric Dumazet   netfilter: use ju...
80
  #endif
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
81
82
83
84
85
86
  	return 0;
  }
  EXPORT_SYMBOL(nf_register_hook);
  
  void nf_unregister_hook(struct nf_hook_ops *reg)
  {
fd706d695   Patrick McHardy   [NETFILTER]: Swit...
87
  	mutex_lock(&nf_hook_mutex);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
88
  	list_del_rcu(&reg->list);
fd706d695   Patrick McHardy   [NETFILTER]: Swit...
89
  	mutex_unlock(&nf_hook_mutex);
a2d7ec58a   Eric Dumazet   netfilter: use ju...
90
  #if defined(CONFIG_JUMP_LABEL)
c5905afb0   Ingo Molnar   static keys: Intr...
91
  	static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
a2d7ec58a   Eric Dumazet   netfilter: use ju...
92
  #endif
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
93
94
95
  	synchronize_net();
  }
  EXPORT_SYMBOL(nf_unregister_hook);
972d1cb14   Patrick McHardy   [NETFILTER]: Add ...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
  {
  	unsigned int i;
  	int err = 0;
  
  	for (i = 0; i < n; i++) {
  		err = nf_register_hook(&reg[i]);
  		if (err)
  			goto err;
  	}
  	return err;
  
  err:
  	if (i > 0)
  		nf_unregister_hooks(reg, i);
  	return err;
  }
  EXPORT_SYMBOL(nf_register_hooks);
  
  void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
  {
f68c53015   Changli Gao   netfilter: unregi...
117
118
  	while (n-- > 0)
  		nf_unregister_hook(&reg[n]);
972d1cb14   Patrick McHardy   [NETFILTER]: Add ...
119
120
  }
  EXPORT_SYMBOL(nf_unregister_hooks);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
121
  unsigned int nf_iterate(struct list_head *head,
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
122
  			struct sk_buff *skb,
76108cea0   Jan Engelhardt   netfilter: Use un...
123
  			unsigned int hook,
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
124
125
  			const struct net_device *indev,
  			const struct net_device *outdev,
2a6decfd8   Michael Wang   netfilter: pass '...
126
  			struct nf_hook_ops **elemp,
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
127
128
129
130
131
132
133
134
135
  			int (*okfn)(struct sk_buff *),
  			int hook_thresh)
  {
  	unsigned int verdict;
  
  	/*
  	 * The caller must not block between calls to this
  	 * function because of risk of continuing from deleted element.
  	 */
2a6decfd8   Michael Wang   netfilter: pass '...
136
137
  	list_for_each_entry_continue_rcu((*elemp), head, list) {
  		if (hook_thresh > (*elemp)->priority)
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
138
139
140
  			continue;
  
  		/* Optimization: we don't need to hold module
601e68e10   YOSHIFUJI Hideaki   [NETFILTER]: Fix ...
141
  		   reference here, since function can't sleep. --RR */
de9963f0f   Patrick McHardy   netfilter: nf_ite...
142
  repeat:
2a6decfd8   Michael Wang   netfilter: pass '...
143
  		verdict = (*elemp)->hook(hook, skb, indev, outdev, okfn);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
144
145
146
147
148
149
  		if (verdict != NF_ACCEPT) {
  #ifdef CONFIG_NETFILTER_DEBUG
  			if (unlikely((verdict & NF_VERDICT_MASK)
  							> NF_MAX_VERDICT)) {
  				NFDEBUG("Evil return from %p(%u).
  ",
2a6decfd8   Michael Wang   netfilter: pass '...
150
  					(*elemp)->hook, hook);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
151
152
153
  				continue;
  			}
  #endif
2a6decfd8   Michael Wang   netfilter: pass '...
154
  			if (verdict != NF_REPEAT)
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
155
  				return verdict;
de9963f0f   Patrick McHardy   netfilter: nf_ite...
156
  			goto repeat;
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
157
158
159
160
161
162
163
164
  		}
  	}
  	return NF_ACCEPT;
  }
  
  
  /* Returns 1 if okfn() needs to be executed by the caller,
   * -EPERM for NF_DROP, 0 otherwise. */
76108cea0   Jan Engelhardt   netfilter: Use un...
165
  int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
166
167
168
169
170
  		 struct net_device *indev,
  		 struct net_device *outdev,
  		 int (*okfn)(struct sk_buff *),
  		 int hook_thresh)
  {
2a6decfd8   Michael Wang   netfilter: pass '...
171
  	struct nf_hook_ops *elem;
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
172
173
174
175
176
  	unsigned int verdict;
  	int ret = 0;
  
  	/* We may already have this, but read-locks nest anyway */
  	rcu_read_lock();
2a6decfd8   Michael Wang   netfilter: pass '...
177
  	elem = list_entry_rcu(&nf_hooks[pf][hook], struct nf_hook_ops, list);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
178
  next_hook:
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
179
  	verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
180
181
182
  			     outdev, &elem, okfn, hook_thresh);
  	if (verdict == NF_ACCEPT || verdict == NF_STOP) {
  		ret = 1;
da6836500   Eric Paris   netfilter: allow ...
183
  	} else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
184
  		kfree_skb(skb);
f615df76e   Florian Westphal   netfilter: reduce...
185
  		ret = NF_DROP_GETERR(verdict);
da6836500   Eric Paris   netfilter: allow ...
186
187
  		if (ret == 0)
  			ret = -EPERM;
f9c639905   Patrick McHardy   [NETFILTER]: remo...
188
  	} else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
1c15b6770   Michael Wang   netfilter: pass '...
189
190
  		int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
  						verdict >> NF_VERDICT_QBITS);
563e12326   Florian Westphal   netfilter: do not...
191
192
  		if (err < 0) {
  			if (err == -ECANCELED)
06cdb6349   Florian Westphal   netfilter: nfnetl...
193
  				goto next_hook;
563e12326   Florian Westphal   netfilter: do not...
194
  			if (err == -ESRCH &&
94b27cc36   Florian Westphal   netfilter: allow ...
195
196
  			   (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
  				goto next_hook;
06cdb6349   Florian Westphal   netfilter: nfnetl...
197
198
  			kfree_skb(skb);
  		}
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
199
  	}
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
200
201
202
203
  	rcu_read_unlock();
  	return ret;
  }
  EXPORT_SYMBOL(nf_hook_slow);
37d418792   Herbert Xu   [NETFILTER]: Do n...
204
  int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
205
  {
37d418792   Herbert Xu   [NETFILTER]: Do n...
206
  	if (writable_len > skb->len)
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
207
208
209
  		return 0;
  
  	/* Not exclusive use of packet?  Must copy. */
37d418792   Herbert Xu   [NETFILTER]: Do n...
210
211
212
213
214
215
216
217
218
219
220
221
  	if (!skb_cloned(skb)) {
  		if (writable_len <= skb_headlen(skb))
  			return 1;
  	} else if (skb_clone_writable(skb, writable_len))
  		return 1;
  
  	if (writable_len <= skb_headlen(skb))
  		writable_len = 0;
  	else
  		writable_len -= skb_headlen(skb);
  
  	return !!__pskb_pull_tail(skb, writable_len);
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
222
223
  }
  EXPORT_SYMBOL(skb_make_writable);
c0cd11566   Igor Maravić   net:netfilter: us...
224
  #if IS_ENABLED(CONFIG_NF_CONNTRACK)
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
225
226
227
  /* This does not belong here, but locally generated errors need it if connection
     tracking in use: without this, connection may not be in hash table, and hence
     manufactured ICMP or RST packets will not be associated with it. */
0e60ebe04   Eric Dumazet   netfilter: add __...
228
  void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu __read_mostly;
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
229
230
231
232
233
  EXPORT_SYMBOL(ip_ct_attach);
  
  void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
  {
  	void (*attach)(struct sk_buff *, struct sk_buff *);
c3a47ab3e   Patrick McHardy   [NETFILTER]: Prop...
234
235
236
237
238
239
  	if (skb->nfct) {
  		rcu_read_lock();
  		attach = rcu_dereference(ip_ct_attach);
  		if (attach)
  			attach(new, skb);
  		rcu_read_unlock();
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
240
241
242
  	}
  }
  EXPORT_SYMBOL(nf_ct_attach);
de6e05c49   Yasuyuki Kozakai   [NETFILTER]: nf_c...
243

0e60ebe04   Eric Dumazet   netfilter: add __...
244
  void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
de6e05c49   Yasuyuki Kozakai   [NETFILTER]: nf_c...
245
246
247
248
249
250
251
252
253
254
255
256
257
  EXPORT_SYMBOL(nf_ct_destroy);
  
  void nf_conntrack_destroy(struct nf_conntrack *nfct)
  {
  	void (*destroy)(struct nf_conntrack *);
  
  	rcu_read_lock();
  	destroy = rcu_dereference(nf_ct_destroy);
  	BUG_ON(destroy == NULL);
  	destroy(nfct);
  	rcu_read_unlock();
  }
  EXPORT_SYMBOL(nf_conntrack_destroy);
9cb017665   Pablo Neira Ayuso   netfilter: add gl...
258

5a05fae5c   Pablo Neira Ayuso   netfilter: nfq_ct...
259
  struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly;
9cb017665   Pablo Neira Ayuso   netfilter: add gl...
260
  EXPORT_SYMBOL_GPL(nfq_ct_hook);
d584a61a9   Pablo Neira Ayuso   netfilter: nfnetl...
261
262
  struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly;
  EXPORT_SYMBOL_GPL(nfq_ct_nat_hook);
de6e05c49   Yasuyuki Kozakai   [NETFILTER]: nf_c...
263
  #endif /* CONFIG_NF_CONNTRACK */
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
264

c7232c997   Patrick McHardy   netfilter: add pr...
265
266
267
268
  #ifdef CONFIG_NF_NAT_NEEDED
  void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
  EXPORT_SYMBOL(nf_nat_decode_session_hook);
  #endif
f3c1a44a2   Gao feng   netfilter: make /...
269
270
271
272
273
  static int __net_init netfilter_net_init(struct net *net)
  {
  #ifdef CONFIG_PROC_FS
  	net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
  						net->proc_net);
12202fa75   Pablo Neira Ayuso   netfilter: remove...
274
275
276
  	if (!net->nf.proc_netfilter) {
  		if (!net_eq(net, &init_net))
  			pr_err("cannot create netfilter proc entry");
f3c1a44a2   Gao feng   netfilter: make /...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  		return -ENOMEM;
  	}
  #endif
  	return 0;
  }
  
  static void __net_exit netfilter_net_exit(struct net *net)
  {
  	remove_proc_entry("netfilter", net->proc_net);
  }
  
  static struct pernet_operations netfilter_net_ops = {
  	.init = netfilter_net_init,
  	.exit = netfilter_net_exit,
  };
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
292
293
294
  void __init netfilter_init(void)
  {
  	int i, h;
7e9c6eeb1   Jan Engelhardt   netfilter: Introd...
295
  	for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
296
297
298
  		for (h = 0; h < NF_MAX_HOOKS; h++)
  			INIT_LIST_HEAD(&nf_hooks[i][h]);
  	}
f3c1a44a2   Gao feng   netfilter: make /...
299
  	if (register_pernet_subsys(&netfilter_net_ops) < 0)
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
300
  		panic("cannot create netfilter proc entry");
f6ebe77f9   Harald Welte   [NETFILTER]: spli...
301

f6ebe77f9   Harald Welte   [NETFILTER]: spli...
302
303
304
  	if (netfilter_log_init() < 0)
  		panic("cannot initialize nf_log");
  }