blob: 1fbab0cdd302bdafe199d434fc10f2f6401ef6a8 [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.
Patrick McHardyf229f6c2013-04-06 15:24:29 +02008 * Patrick McHardy (c) 2006-2012
Harald Weltef6ebe772005-08-09 20:21:49 -07009 */
Harald Weltef6ebe772005-08-09 20:21:49 -070010#include <linux/kernel.h>
11#include <linux/netfilter.h>
12#include <net/protocol.h>
13#include <linux/init.h>
14#include <linux/skbuff.h>
15#include <linux/wait.h>
16#include <linux/module.h>
17#include <linux/interrupt.h>
18#include <linux/if.h>
19#include <linux/netdevice.h>
20#include <linux/inetdevice.h>
21#include <linux/proc_fs.h>
Patrick McHardyd486dd12007-02-12 11:09:55 -080022#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020024#include <net/net_namespace.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070025#include <net/sock.h>
26
27#include "nf_internals.h"
28
Patrick McHardyd486dd12007-02-12 11:09:55 -080029static DEFINE_MUTEX(afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070030
Arnd Bergmann0906a372010-03-09 20:59:15 +010031const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
Patrick McHardybce80322006-04-06 14:18:09 -070032EXPORT_SYMBOL(nf_afinfo);
Florian Westphal2a7851b2013-05-17 03:56:10 +000033const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly;
34EXPORT_SYMBOL_GPL(nf_ipv6_ops);
Patrick McHardybce80322006-04-06 14:18:09 -070035
Patrick McHardy1e796fd2007-12-17 22:42:27 -080036int nf_register_afinfo(const struct nf_afinfo *afinfo)
Patrick McHardybce80322006-04-06 14:18:09 -070037{
Patrick McHardyd486dd12007-02-12 11:09:55 -080038 int err;
39
40 err = mutex_lock_interruptible(&afinfo_mutex);
41 if (err < 0)
42 return err;
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +000043 RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo);
Patrick McHardyd486dd12007-02-12 11:09:55 -080044 mutex_unlock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070045 return 0;
46}
47EXPORT_SYMBOL_GPL(nf_register_afinfo);
48
Patrick McHardy1e796fd2007-12-17 22:42:27 -080049void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
Patrick McHardybce80322006-04-06 14:18:09 -070050{
Patrick McHardyd486dd12007-02-12 11:09:55 -080051 mutex_lock(&afinfo_mutex);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +000052 RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL);
Patrick McHardyd486dd12007-02-12 11:09:55 -080053 mutex_unlock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070054 synchronize_rcu();
55}
56EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
57
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020058struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -070059EXPORT_SYMBOL(nf_hooks);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000060
61#if defined(CONFIG_JUMP_LABEL)
Ingo Molnarc5905af2012-02-24 08:31:31 +010062struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000063EXPORT_SYMBOL(nf_hooks_needed);
64#endif
65
Patrick McHardyfd706d62007-02-12 11:10:14 -080066static DEFINE_MUTEX(nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070067
68int nf_register_hook(struct nf_hook_ops *reg)
69{
Li Zefan4c610972007-12-04 23:22:26 -080070 struct nf_hook_ops *elem;
Patrick McHardyfd706d62007-02-12 11:10:14 -080071 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -070072
Patrick McHardyfd706d62007-02-12 11:10:14 -080073 err = mutex_lock_interruptible(&nf_hook_mutex);
74 if (err < 0)
75 return err;
Li Zefan4c610972007-12-04 23:22:26 -080076 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
77 if (reg->priority < elem->priority)
Harald Weltef6ebe772005-08-09 20:21:49 -070078 break;
79 }
Li Zefan4c610972007-12-04 23:22:26 -080080 list_add_rcu(&reg->list, elem->list.prev);
Patrick McHardyfd706d62007-02-12 11:10:14 -080081 mutex_unlock(&nf_hook_mutex);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000082#if defined(CONFIG_JUMP_LABEL)
Ingo Molnarc5905af2012-02-24 08:31:31 +010083 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000084#endif
Harald Weltef6ebe772005-08-09 20:21:49 -070085 return 0;
86}
87EXPORT_SYMBOL(nf_register_hook);
88
89void nf_unregister_hook(struct nf_hook_ops *reg)
90{
Patrick McHardyfd706d62007-02-12 11:10:14 -080091 mutex_lock(&nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070092 list_del_rcu(&reg->list);
Patrick McHardyfd706d62007-02-12 11:10:14 -080093 mutex_unlock(&nf_hook_mutex);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000094#if defined(CONFIG_JUMP_LABEL)
Ingo Molnarc5905af2012-02-24 08:31:31 +010095 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000096#endif
Harald Weltef6ebe772005-08-09 20:21:49 -070097 synchronize_net();
98}
99EXPORT_SYMBOL(nf_unregister_hook);
100
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700101int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
102{
103 unsigned int i;
104 int err = 0;
105
106 for (i = 0; i < n; i++) {
107 err = nf_register_hook(&reg[i]);
108 if (err)
109 goto err;
110 }
111 return err;
112
113err:
114 if (i > 0)
115 nf_unregister_hooks(reg, i);
116 return err;
117}
118EXPORT_SYMBOL(nf_register_hooks);
119
120void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
121{
Changli Gaof68c5302010-10-04 22:24:12 +0200122 while (n-- > 0)
123 nf_unregister_hook(&reg[n]);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700124}
125EXPORT_SYMBOL(nf_unregister_hooks);
126
Harald Weltef6ebe772005-08-09 20:21:49 -0700127unsigned int nf_iterate(struct list_head *head,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700128 struct sk_buff *skb,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200129 unsigned int hook,
Harald Weltef6ebe772005-08-09 20:21:49 -0700130 const struct net_device *indev,
131 const struct net_device *outdev,
Michael Wang2a6decf2012-08-22 19:59:57 +0000132 struct nf_hook_ops **elemp,
Harald Weltef6ebe772005-08-09 20:21:49 -0700133 int (*okfn)(struct sk_buff *),
134 int hook_thresh)
135{
136 unsigned int verdict;
137
138 /*
139 * The caller must not block between calls to this
140 * function because of risk of continuing from deleted element.
141 */
Michael Wang2a6decf2012-08-22 19:59:57 +0000142 list_for_each_entry_continue_rcu((*elemp), head, list) {
143 if (hook_thresh > (*elemp)->priority)
Harald Weltef6ebe772005-08-09 20:21:49 -0700144 continue;
145
146 /* Optimization: we don't need to hold module
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800147 reference here, since function can't sleep. --RR */
Patrick McHardyde9963f2011-02-14 17:35:07 +0100148repeat:
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200149 verdict = (*elemp)->hook(*elemp, skb, indev, outdev, okfn);
Harald Weltef6ebe772005-08-09 20:21:49 -0700150 if (verdict != NF_ACCEPT) {
151#ifdef CONFIG_NETFILTER_DEBUG
152 if (unlikely((verdict & NF_VERDICT_MASK)
153 > NF_MAX_VERDICT)) {
154 NFDEBUG("Evil return from %p(%u).\n",
Michael Wang2a6decf2012-08-22 19:59:57 +0000155 (*elemp)->hook, hook);
Harald Weltef6ebe772005-08-09 20:21:49 -0700156 continue;
157 }
158#endif
Michael Wang2a6decf2012-08-22 19:59:57 +0000159 if (verdict != NF_REPEAT)
Harald Weltef6ebe772005-08-09 20:21:49 -0700160 return verdict;
Patrick McHardyde9963f2011-02-14 17:35:07 +0100161 goto repeat;
Harald Weltef6ebe772005-08-09 20:21:49 -0700162 }
163 }
164 return NF_ACCEPT;
165}
166
167
168/* Returns 1 if okfn() needs to be executed by the caller,
169 * -EPERM for NF_DROP, 0 otherwise. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200170int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Harald Weltef6ebe772005-08-09 20:21:49 -0700171 struct net_device *indev,
172 struct net_device *outdev,
173 int (*okfn)(struct sk_buff *),
174 int hook_thresh)
175{
Michael Wang2a6decf2012-08-22 19:59:57 +0000176 struct nf_hook_ops *elem;
Harald Weltef6ebe772005-08-09 20:21:49 -0700177 unsigned int verdict;
178 int ret = 0;
179
180 /* We may already have this, but read-locks nest anyway */
181 rcu_read_lock();
182
Michael Wang2a6decf2012-08-22 19:59:57 +0000183 elem = list_entry_rcu(&nf_hooks[pf][hook], struct nf_hook_ops, list);
Harald Weltef6ebe772005-08-09 20:21:49 -0700184next_hook:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700185 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
Harald Weltef6ebe772005-08-09 20:21:49 -0700186 outdev, &elem, okfn, hook_thresh);
187 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
188 ret = 1;
Eric Parisda683652010-11-16 11:52:38 +0000189 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700190 kfree_skb(skb);
Florian Westphalf615df72011-01-18 15:52:14 +0100191 ret = NF_DROP_GETERR(verdict);
Eric Parisda683652010-11-16 11:52:38 +0000192 if (ret == 0)
193 ret = -EPERM;
Patrick McHardyf9c63992007-12-05 01:27:46 -0800194 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
Michael Wang1c15b672012-08-22 20:00:06 +0000195 int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
196 verdict >> NF_VERDICT_QBITS);
Florian Westphal563e1232011-10-31 12:20:16 +0100197 if (err < 0) {
198 if (err == -ECANCELED)
Florian Westphal06cdb632011-01-18 15:28:38 +0100199 goto next_hook;
Florian Westphal563e1232011-10-31 12:20:16 +0100200 if (err == -ESRCH &&
Florian Westphal94b27cc2011-01-18 16:08:30 +0100201 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
202 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100203 kfree_skb(skb);
204 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700205 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700206 rcu_read_unlock();
207 return ret;
208}
209EXPORT_SYMBOL(nf_hook_slow);
210
211
Herbert Xu37d41872007-10-14 00:39:18 -0700212int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700213{
Herbert Xu37d41872007-10-14 00:39:18 -0700214 if (writable_len > skb->len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700215 return 0;
216
217 /* Not exclusive use of packet? Must copy. */
Herbert Xu37d41872007-10-14 00:39:18 -0700218 if (!skb_cloned(skb)) {
219 if (writable_len <= skb_headlen(skb))
220 return 1;
221 } else if (skb_clone_writable(skb, writable_len))
222 return 1;
Harald Weltef6ebe772005-08-09 20:21:49 -0700223
Herbert Xu37d41872007-10-14 00:39:18 -0700224 if (writable_len <= skb_headlen(skb))
225 writable_len = 0;
226 else
227 writable_len -= skb_headlen(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700228
Herbert Xu37d41872007-10-14 00:39:18 -0700229 return !!__pskb_pull_tail(skb, writable_len);
Harald Weltef6ebe772005-08-09 20:21:49 -0700230}
231EXPORT_SYMBOL(skb_make_writable);
232
Igor Maravićc0cd1152011-12-12 02:58:24 +0000233#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Harald Weltef6ebe772005-08-09 20:21:49 -0700234/* This does not belong here, but locally generated errors need it if connection
235 tracking in use: without this, connection may not be in hash table, and hence
236 manufactured ICMP or RST packets will not be associated with it. */
Patrick McHardy312a0c162013-07-28 22:54:08 +0200237void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *)
238 __rcu __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -0700239EXPORT_SYMBOL(ip_ct_attach);
240
Patrick McHardy312a0c162013-07-28 22:54:08 +0200241void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb)
Harald Weltef6ebe772005-08-09 20:21:49 -0700242{
Patrick McHardy312a0c162013-07-28 22:54:08 +0200243 void (*attach)(struct sk_buff *, const struct sk_buff *);
Harald Weltef6ebe772005-08-09 20:21:49 -0700244
Patrick McHardyc3a47ab2007-02-12 11:09:19 -0800245 if (skb->nfct) {
246 rcu_read_lock();
247 attach = rcu_dereference(ip_ct_attach);
248 if (attach)
249 attach(new, skb);
250 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700251 }
252}
253EXPORT_SYMBOL(nf_ct_attach);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700254
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100255void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700256EXPORT_SYMBOL(nf_ct_destroy);
257
258void nf_conntrack_destroy(struct nf_conntrack *nfct)
259{
260 void (*destroy)(struct nf_conntrack *);
261
262 rcu_read_lock();
263 destroy = rcu_dereference(nf_ct_destroy);
264 BUG_ON(destroy == NULL);
265 destroy(nfct);
266 rcu_read_unlock();
267}
268EXPORT_SYMBOL(nf_conntrack_destroy);
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200269
Pablo Neira Ayuso5a05fae2012-06-20 20:50:31 +0200270struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200271EXPORT_SYMBOL_GPL(nfq_ct_hook);
272
Pablo Neira Ayusod584a612012-06-20 20:52:31 +0200273struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly;
274EXPORT_SYMBOL_GPL(nfq_ct_nat_hook);
275
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700276#endif /* CONFIG_NF_CONNTRACK */
Harald Weltef6ebe772005-08-09 20:21:49 -0700277
Patrick McHardyc7232c92012-08-26 19:14:06 +0200278#ifdef CONFIG_NF_NAT_NEEDED
279void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
280EXPORT_SYMBOL(nf_nat_decode_session_hook);
281#endif
282
Gao fengf3c1a442013-03-24 23:50:39 +0000283static int __net_init netfilter_net_init(struct net *net)
284{
285#ifdef CONFIG_PROC_FS
286 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
287 net->proc_net);
Pablo Neira Ayuso12202fa2013-04-05 19:40:10 +0200288 if (!net->nf.proc_netfilter) {
289 if (!net_eq(net, &init_net))
290 pr_err("cannot create netfilter proc entry");
291
Gao fengf3c1a442013-03-24 23:50:39 +0000292 return -ENOMEM;
293 }
294#endif
295 return 0;
296}
297
298static void __net_exit netfilter_net_exit(struct net *net)
299{
300 remove_proc_entry("netfilter", net->proc_net);
301}
302
303static struct pernet_operations netfilter_net_ops = {
304 .init = netfilter_net_init,
305 .exit = netfilter_net_exit,
306};
307
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +0000308int __init netfilter_init(void)
Harald Weltef6ebe772005-08-09 20:21:49 -0700309{
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +0000310 int i, h, ret;
311
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200312 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700313 for (h = 0; h < NF_MAX_HOOKS; h++)
314 INIT_LIST_HEAD(&nf_hooks[i][h]);
315 }
316
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +0000317 ret = register_pernet_subsys(&netfilter_net_ops);
318 if (ret < 0)
319 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700320
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +0000321 ret = netfilter_log_init();
322 if (ret < 0)
323 goto err_pernet;
324
325 return 0;
326err_pernet:
327 unregister_pernet_subsys(&netfilter_net_ops);
328err:
329 return ret;
Harald Weltef6ebe772005-08-09 20:21:49 -0700330}