blob: b085184d9b45a384003750ff89ef60ecfe86fb17 [file] [log] [blame]
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001/* netfilter.c: look after the filters for various protocols.
Harald Weltef6ebe772005-08-09 20:21:49 -07002 * Heavily influenced by the old firewall.c by David Bonn and Alan Cox.
3 *
4 * Thanks to Rob `CmdrTaco' Malda for not influencing this code in any
5 * way.
6 *
7 * Rusty Russell (C)2000 -- This code is GPL.
Harald Weltef6ebe772005-08-09 20:21:49 -07008 */
Harald Weltef6ebe772005-08-09 20:21:49 -07009#include <linux/kernel.h>
10#include <linux/netfilter.h>
11#include <net/protocol.h>
12#include <linux/init.h>
13#include <linux/skbuff.h>
14#include <linux/wait.h>
15#include <linux/module.h>
16#include <linux/interrupt.h>
17#include <linux/if.h>
18#include <linux/netdevice.h>
19#include <linux/inetdevice.h>
20#include <linux/proc_fs.h>
Patrick McHardyd486dd12007-02-12 11:09:55 -080021#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020023#include <net/net_namespace.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070024#include <net/sock.h>
25
26#include "nf_internals.h"
27
Patrick McHardyd486dd12007-02-12 11:09:55 -080028static DEFINE_MUTEX(afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070029
Arnd Bergmann0906a372010-03-09 20:59:15 +010030const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
Patrick McHardybce80322006-04-06 14:18:09 -070031EXPORT_SYMBOL(nf_afinfo);
32
Patrick McHardy1e796fd2007-12-17 22:42:27 -080033int nf_register_afinfo(const struct nf_afinfo *afinfo)
Patrick McHardybce80322006-04-06 14:18:09 -070034{
Patrick McHardyd486dd12007-02-12 11:09:55 -080035 int err;
36
37 err = mutex_lock_interruptible(&afinfo_mutex);
38 if (err < 0)
39 return err;
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +000040 RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo);
Patrick McHardyd486dd12007-02-12 11:09:55 -080041 mutex_unlock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070042 return 0;
43}
44EXPORT_SYMBOL_GPL(nf_register_afinfo);
45
Patrick McHardy1e796fd2007-12-17 22:42:27 -080046void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
Patrick McHardybce80322006-04-06 14:18:09 -070047{
Patrick McHardyd486dd12007-02-12 11:09:55 -080048 mutex_lock(&afinfo_mutex);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +000049 RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL);
Patrick McHardyd486dd12007-02-12 11:09:55 -080050 mutex_unlock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070051 synchronize_rcu();
52}
53EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
54
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020055struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -070056EXPORT_SYMBOL(nf_hooks);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000057
58#if defined(CONFIG_JUMP_LABEL)
Ingo Molnarc5905af2012-02-24 08:31:31 +010059struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000060EXPORT_SYMBOL(nf_hooks_needed);
61#endif
62
Patrick McHardyfd706d62007-02-12 11:10:14 -080063static DEFINE_MUTEX(nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070064
65int nf_register_hook(struct nf_hook_ops *reg)
66{
Li Zefan4c610972007-12-04 23:22:26 -080067 struct nf_hook_ops *elem;
Patrick McHardyfd706d62007-02-12 11:10:14 -080068 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -070069
Patrick McHardyfd706d62007-02-12 11:10:14 -080070 err = mutex_lock_interruptible(&nf_hook_mutex);
71 if (err < 0)
72 return err;
Li Zefan4c610972007-12-04 23:22:26 -080073 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
74 if (reg->priority < elem->priority)
Harald Weltef6ebe772005-08-09 20:21:49 -070075 break;
76 }
Li Zefan4c610972007-12-04 23:22:26 -080077 list_add_rcu(&reg->list, elem->list.prev);
Patrick McHardyfd706d62007-02-12 11:10:14 -080078 mutex_unlock(&nf_hook_mutex);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000079#if defined(CONFIG_JUMP_LABEL)
Ingo Molnarc5905af2012-02-24 08:31:31 +010080 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000081#endif
Harald Weltef6ebe772005-08-09 20:21:49 -070082 return 0;
83}
84EXPORT_SYMBOL(nf_register_hook);
85
86void nf_unregister_hook(struct nf_hook_ops *reg)
87{
Patrick McHardyfd706d62007-02-12 11:10:14 -080088 mutex_lock(&nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070089 list_del_rcu(&reg->list);
Patrick McHardyfd706d62007-02-12 11:10:14 -080090 mutex_unlock(&nf_hook_mutex);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000091#if defined(CONFIG_JUMP_LABEL)
Ingo Molnarc5905af2012-02-24 08:31:31 +010092 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000093#endif
Harald Weltef6ebe772005-08-09 20:21:49 -070094 synchronize_net();
95}
96EXPORT_SYMBOL(nf_unregister_hook);
97
Patrick McHardy972d1cb2006-04-06 14:09:12 -070098int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
99{
100 unsigned int i;
101 int err = 0;
102
103 for (i = 0; i < n; i++) {
104 err = nf_register_hook(&reg[i]);
105 if (err)
106 goto err;
107 }
108 return err;
109
110err:
111 if (i > 0)
112 nf_unregister_hooks(reg, i);
113 return err;
114}
115EXPORT_SYMBOL(nf_register_hooks);
116
117void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
118{
Changli Gaof68c5302010-10-04 22:24:12 +0200119 while (n-- > 0)
120 nf_unregister_hook(&reg[n]);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700121}
122EXPORT_SYMBOL(nf_unregister_hooks);
123
Harald Weltef6ebe772005-08-09 20:21:49 -0700124unsigned int nf_iterate(struct list_head *head,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700125 struct sk_buff *skb,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200126 unsigned int hook,
Harald Weltef6ebe772005-08-09 20:21:49 -0700127 const struct net_device *indev,
128 const struct net_device *outdev,
Michael Wang2a6decf2012-08-22 19:59:57 +0000129 struct nf_hook_ops **elemp,
Harald Weltef6ebe772005-08-09 20:21:49 -0700130 int (*okfn)(struct sk_buff *),
131 int hook_thresh)
132{
133 unsigned int verdict;
134
135 /*
136 * The caller must not block between calls to this
137 * function because of risk of continuing from deleted element.
138 */
Michael Wang2a6decf2012-08-22 19:59:57 +0000139 list_for_each_entry_continue_rcu((*elemp), head, list) {
140 if (hook_thresh > (*elemp)->priority)
Harald Weltef6ebe772005-08-09 20:21:49 -0700141 continue;
142
143 /* Optimization: we don't need to hold module
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800144 reference here, since function can't sleep. --RR */
Patrick McHardyde9963f2011-02-14 17:35:07 +0100145repeat:
Michael Wang2a6decf2012-08-22 19:59:57 +0000146 verdict = (*elemp)->hook(hook, skb, indev, outdev, okfn);
Harald Weltef6ebe772005-08-09 20:21:49 -0700147 if (verdict != NF_ACCEPT) {
148#ifdef CONFIG_NETFILTER_DEBUG
149 if (unlikely((verdict & NF_VERDICT_MASK)
150 > NF_MAX_VERDICT)) {
151 NFDEBUG("Evil return from %p(%u).\n",
Michael Wang2a6decf2012-08-22 19:59:57 +0000152 (*elemp)->hook, hook);
Harald Weltef6ebe772005-08-09 20:21:49 -0700153 continue;
154 }
155#endif
Michael Wang2a6decf2012-08-22 19:59:57 +0000156 if (verdict != NF_REPEAT)
Harald Weltef6ebe772005-08-09 20:21:49 -0700157 return verdict;
Patrick McHardyde9963f2011-02-14 17:35:07 +0100158 goto repeat;
Harald Weltef6ebe772005-08-09 20:21:49 -0700159 }
160 }
161 return NF_ACCEPT;
162}
163
164
165/* Returns 1 if okfn() needs to be executed by the caller,
166 * -EPERM for NF_DROP, 0 otherwise. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200167int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Harald Weltef6ebe772005-08-09 20:21:49 -0700168 struct net_device *indev,
169 struct net_device *outdev,
170 int (*okfn)(struct sk_buff *),
171 int hook_thresh)
172{
Michael Wang2a6decf2012-08-22 19:59:57 +0000173 struct nf_hook_ops *elem;
Harald Weltef6ebe772005-08-09 20:21:49 -0700174 unsigned int verdict;
175 int ret = 0;
176
177 /* We may already have this, but read-locks nest anyway */
178 rcu_read_lock();
179
Michael Wang2a6decf2012-08-22 19:59:57 +0000180 elem = list_entry_rcu(&nf_hooks[pf][hook], struct nf_hook_ops, list);
Harald Weltef6ebe772005-08-09 20:21:49 -0700181next_hook:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700182 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
Harald Weltef6ebe772005-08-09 20:21:49 -0700183 outdev, &elem, okfn, hook_thresh);
184 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
185 ret = 1;
Eric Parisda683652010-11-16 11:52:38 +0000186 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700187 kfree_skb(skb);
Florian Westphalf615df72011-01-18 15:52:14 +0100188 ret = NF_DROP_GETERR(verdict);
Eric Parisda683652010-11-16 11:52:38 +0000189 if (ret == 0)
190 ret = -EPERM;
Patrick McHardyf9c63992007-12-05 01:27:46 -0800191 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
Michael Wang1c15b672012-08-22 20:00:06 +0000192 int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
193 verdict >> NF_VERDICT_QBITS);
Florian Westphal563e1232011-10-31 12:20:16 +0100194 if (err < 0) {
195 if (err == -ECANCELED)
Florian Westphal06cdb632011-01-18 15:28:38 +0100196 goto next_hook;
Florian Westphal563e1232011-10-31 12:20:16 +0100197 if (err == -ESRCH &&
Florian Westphal94b27cc2011-01-18 16:08:30 +0100198 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
199 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100200 kfree_skb(skb);
201 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700202 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700203 rcu_read_unlock();
204 return ret;
205}
206EXPORT_SYMBOL(nf_hook_slow);
207
208
Herbert Xu37d41872007-10-14 00:39:18 -0700209int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700210{
Herbert Xu37d41872007-10-14 00:39:18 -0700211 if (writable_len > skb->len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700212 return 0;
213
214 /* Not exclusive use of packet? Must copy. */
Herbert Xu37d41872007-10-14 00:39:18 -0700215 if (!skb_cloned(skb)) {
216 if (writable_len <= skb_headlen(skb))
217 return 1;
218 } else if (skb_clone_writable(skb, writable_len))
219 return 1;
Harald Weltef6ebe772005-08-09 20:21:49 -0700220
Herbert Xu37d41872007-10-14 00:39:18 -0700221 if (writable_len <= skb_headlen(skb))
222 writable_len = 0;
223 else
224 writable_len -= skb_headlen(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700225
Herbert Xu37d41872007-10-14 00:39:18 -0700226 return !!__pskb_pull_tail(skb, writable_len);
Harald Weltef6ebe772005-08-09 20:21:49 -0700227}
228EXPORT_SYMBOL(skb_make_writable);
229
Igor Maravićc0cd1152011-12-12 02:58:24 +0000230#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Harald Weltef6ebe772005-08-09 20:21:49 -0700231/* This does not belong here, but locally generated errors need it if connection
232 tracking in use: without this, connection may not be in hash table, and hence
233 manufactured ICMP or RST packets will not be associated with it. */
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100234void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -0700235EXPORT_SYMBOL(ip_ct_attach);
236
237void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
238{
239 void (*attach)(struct sk_buff *, struct sk_buff *);
240
Patrick McHardyc3a47ab2007-02-12 11:09:19 -0800241 if (skb->nfct) {
242 rcu_read_lock();
243 attach = rcu_dereference(ip_ct_attach);
244 if (attach)
245 attach(new, skb);
246 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700247 }
248}
249EXPORT_SYMBOL(nf_ct_attach);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700250
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100251void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700252EXPORT_SYMBOL(nf_ct_destroy);
253
254void nf_conntrack_destroy(struct nf_conntrack *nfct)
255{
256 void (*destroy)(struct nf_conntrack *);
257
258 rcu_read_lock();
259 destroy = rcu_dereference(nf_ct_destroy);
260 BUG_ON(destroy == NULL);
261 destroy(nfct);
262 rcu_read_unlock();
263}
264EXPORT_SYMBOL(nf_conntrack_destroy);
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200265
Pablo Neira Ayuso5a05fae2012-06-20 20:50:31 +0200266struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200267EXPORT_SYMBOL_GPL(nfq_ct_hook);
268
Pablo Neira Ayusod584a612012-06-20 20:52:31 +0200269struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly;
270EXPORT_SYMBOL_GPL(nfq_ct_nat_hook);
271
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700272#endif /* CONFIG_NF_CONNTRACK */
Harald Weltef6ebe772005-08-09 20:21:49 -0700273
Patrick McHardyc7232c92012-08-26 19:14:06 +0200274#ifdef CONFIG_NF_NAT_NEEDED
275void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
276EXPORT_SYMBOL(nf_nat_decode_session_hook);
277#endif
278
Harald Weltef6ebe772005-08-09 20:21:49 -0700279#ifdef CONFIG_PROC_FS
280struct proc_dir_entry *proc_net_netfilter;
281EXPORT_SYMBOL(proc_net_netfilter);
282#endif
283
Gao fengf3c1a442013-03-24 23:50:39 +0000284static int __net_init netfilter_net_init(struct net *net)
285{
286#ifdef CONFIG_PROC_FS
287 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
288 net->proc_net);
289 if (net_eq(net, &init_net)) {
290 if (!net->nf.proc_netfilter)
291 return -ENOMEM;
292 else
293 proc_net_netfilter = net->nf.proc_netfilter;
294 } else if (!net->nf.proc_netfilter) {
295 pr_err("cannot create netfilter proc entry");
296 return -ENOMEM;
297 }
298#endif
299 return 0;
300}
301
302static void __net_exit netfilter_net_exit(struct net *net)
303{
304 remove_proc_entry("netfilter", net->proc_net);
305}
306
307static struct pernet_operations netfilter_net_ops = {
308 .init = netfilter_net_init,
309 .exit = netfilter_net_exit,
310};
311
Harald Weltef6ebe772005-08-09 20:21:49 -0700312void __init netfilter_init(void)
313{
314 int i, h;
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200315 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700316 for (h = 0; h < NF_MAX_HOOKS; h++)
317 INIT_LIST_HEAD(&nf_hooks[i][h]);
318 }
319
Gao fengf3c1a442013-03-24 23:50:39 +0000320 if (register_pernet_subsys(&netfilter_net_ops) < 0)
Harald Weltef6ebe772005-08-09 20:21:49 -0700321 panic("cannot create netfilter proc entry");
Harald Weltef6ebe772005-08-09 20:21:49 -0700322
Harald Weltef6ebe772005-08-09 20:21:49 -0700323 if (netfilter_log_init() < 0)
324 panic("cannot initialize nf_log");
325}