blob: a0e54974e2c90fc581e99d5a9f7c24d99a515c16 [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>
Florian Westphal56768642014-11-13 10:04:16 +010020#include <linux/netfilter_ipv6.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070021#include <linux/inetdevice.h>
22#include <linux/proc_fs.h>
Patrick McHardyd486dd12007-02-12 11:09:55 -080023#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020025#include <net/net_namespace.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070026#include <net/sock.h>
27
28#include "nf_internals.h"
29
Patrick McHardyd486dd12007-02-12 11:09:55 -080030static DEFINE_MUTEX(afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070031
Arnd Bergmann0906a372010-03-09 20:59:15 +010032const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
Patrick McHardybce80322006-04-06 14:18:09 -070033EXPORT_SYMBOL(nf_afinfo);
Florian Westphal2a7851b2013-05-17 03:56:10 +000034const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly;
35EXPORT_SYMBOL_GPL(nf_ipv6_ops);
Patrick McHardybce80322006-04-06 14:18:09 -070036
Patrick McHardy1e796fd2007-12-17 22:42:27 -080037int nf_register_afinfo(const struct nf_afinfo *afinfo)
Patrick McHardybce80322006-04-06 14:18:09 -070038{
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020039 mutex_lock(&afinfo_mutex);
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
Zhouyi Zhoud1c85c22014-08-22 10:40:15 +080058#ifdef HAVE_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{
Pablo Neirae687ad62015-05-13 18:19:38 +020067 struct list_head *nf_hook_list;
Li Zefan4c610972007-12-04 23:22:26 -080068 struct nf_hook_ops *elem;
Harald Weltef6ebe772005-08-09 20:21:49 -070069
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020070 mutex_lock(&nf_hook_mutex);
Pablo Neirae687ad62015-05-13 18:19:38 +020071 switch (reg->pf) {
72 case NFPROTO_NETDEV:
73#ifdef CONFIG_NETFILTER_INGRESS
74 if (reg->hooknum == NF_NETDEV_INGRESS) {
75 BUG_ON(reg->dev == NULL);
76 nf_hook_list = &reg->dev->nf_hooks_ingress;
77 net_inc_ingress_queue();
78 break;
79 }
80#endif
81 /* Fall through. */
82 default:
83 nf_hook_list = &nf_hooks[reg->pf][reg->hooknum];
84 break;
85 }
86
87 list_for_each_entry(elem, nf_hook_list, list) {
Li Zefan4c610972007-12-04 23:22:26 -080088 if (reg->priority < elem->priority)
Harald Weltef6ebe772005-08-09 20:21:49 -070089 break;
90 }
Li Zefan4c610972007-12-04 23:22:26 -080091 list_add_rcu(&reg->list, elem->list.prev);
Patrick McHardyfd706d62007-02-12 11:10:14 -080092 mutex_unlock(&nf_hook_mutex);
Zhouyi Zhoud1c85c22014-08-22 10:40:15 +080093#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +010094 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +000095#endif
Harald Weltef6ebe772005-08-09 20:21:49 -070096 return 0;
97}
98EXPORT_SYMBOL(nf_register_hook);
99
100void nf_unregister_hook(struct nf_hook_ops *reg)
101{
Patrick McHardyfd706d62007-02-12 11:10:14 -0800102 mutex_lock(&nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -0700103 list_del_rcu(&reg->list);
Patrick McHardyfd706d62007-02-12 11:10:14 -0800104 mutex_unlock(&nf_hook_mutex);
Pablo Neirae687ad62015-05-13 18:19:38 +0200105 switch (reg->pf) {
106 case NFPROTO_NETDEV:
107#ifdef CONFIG_NETFILTER_INGRESS
108 if (reg->hooknum == NF_NETDEV_INGRESS) {
109 net_dec_ingress_queue();
110 break;
111 }
112 break;
113#endif
114 default:
115 break;
116 }
Zhouyi Zhoud1c85c22014-08-22 10:40:15 +0800117#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +0100118 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
Eric Dumazeta2d7ec52011-11-18 17:32:46 +0000119#endif
Harald Weltef6ebe772005-08-09 20:21:49 -0700120 synchronize_net();
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500121 nf_queue_nf_hook_drop(reg);
Harald Weltef6ebe772005-08-09 20:21:49 -0700122}
123EXPORT_SYMBOL(nf_unregister_hook);
124
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700125int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
126{
127 unsigned int i;
128 int err = 0;
129
130 for (i = 0; i < n; i++) {
131 err = nf_register_hook(&reg[i]);
132 if (err)
133 goto err;
134 }
135 return err;
136
137err:
138 if (i > 0)
139 nf_unregister_hooks(reg, i);
140 return err;
141}
142EXPORT_SYMBOL(nf_register_hooks);
143
144void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
145{
Changli Gaof68c5302010-10-04 22:24:12 +0200146 while (n-- > 0)
147 nf_unregister_hook(&reg[n]);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700148}
149EXPORT_SYMBOL(nf_unregister_hooks);
150
Harald Weltef6ebe772005-08-09 20:21:49 -0700151unsigned int nf_iterate(struct list_head *head,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700152 struct sk_buff *skb,
David S. Millercfdfab32015-04-03 16:23:58 -0400153 struct nf_hook_state *state,
154 struct nf_hook_ops **elemp)
Harald Weltef6ebe772005-08-09 20:21:49 -0700155{
156 unsigned int verdict;
157
158 /*
159 * The caller must not block between calls to this
160 * function because of risk of continuing from deleted element.
161 */
Michael Wang2a6decf2012-08-22 19:59:57 +0000162 list_for_each_entry_continue_rcu((*elemp), head, list) {
David S. Millercfdfab32015-04-03 16:23:58 -0400163 if (state->thresh > (*elemp)->priority)
Harald Weltef6ebe772005-08-09 20:21:49 -0700164 continue;
165
166 /* Optimization: we don't need to hold module
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800167 reference here, since function can't sleep. --RR */
Patrick McHardyde9963f2011-02-14 17:35:07 +0100168repeat:
David S. Miller238e54c2015-04-03 20:32:56 -0400169 verdict = (*elemp)->hook(*elemp, skb, state);
Harald Weltef6ebe772005-08-09 20:21:49 -0700170 if (verdict != NF_ACCEPT) {
171#ifdef CONFIG_NETFILTER_DEBUG
172 if (unlikely((verdict & NF_VERDICT_MASK)
173 > NF_MAX_VERDICT)) {
174 NFDEBUG("Evil return from %p(%u).\n",
David S. Millercfdfab32015-04-03 16:23:58 -0400175 (*elemp)->hook, state->hook);
Harald Weltef6ebe772005-08-09 20:21:49 -0700176 continue;
177 }
178#endif
Michael Wang2a6decf2012-08-22 19:59:57 +0000179 if (verdict != NF_REPEAT)
Harald Weltef6ebe772005-08-09 20:21:49 -0700180 return verdict;
Patrick McHardyde9963f2011-02-14 17:35:07 +0100181 goto repeat;
Harald Weltef6ebe772005-08-09 20:21:49 -0700182 }
183 }
184 return NF_ACCEPT;
185}
186
187
188/* Returns 1 if okfn() needs to be executed by the caller,
189 * -EPERM for NF_DROP, 0 otherwise. */
David S. Millercfdfab32015-04-03 16:23:58 -0400190int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state)
Harald Weltef6ebe772005-08-09 20:21:49 -0700191{
Michael Wang2a6decf2012-08-22 19:59:57 +0000192 struct nf_hook_ops *elem;
Harald Weltef6ebe772005-08-09 20:21:49 -0700193 unsigned int verdict;
194 int ret = 0;
195
196 /* We may already have this, but read-locks nest anyway */
197 rcu_read_lock();
198
Pablo Neiraf7191482015-05-13 18:19:35 +0200199 elem = list_entry_rcu(state->hook_list, struct nf_hook_ops, list);
Harald Weltef6ebe772005-08-09 20:21:49 -0700200next_hook:
Pablo Neiraf7191482015-05-13 18:19:35 +0200201 verdict = nf_iterate(state->hook_list, skb, state, &elem);
Harald Weltef6ebe772005-08-09 20:21:49 -0700202 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
203 ret = 1;
Eric Parisda683652010-11-16 11:52:38 +0000204 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700205 kfree_skb(skb);
Florian Westphalf615df72011-01-18 15:52:14 +0100206 ret = NF_DROP_GETERR(verdict);
Eric Parisda683652010-11-16 11:52:38 +0000207 if (ret == 0)
208 ret = -EPERM;
Patrick McHardyf9c63992007-12-05 01:27:46 -0800209 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
David S. Millercfdfab32015-04-03 16:23:58 -0400210 int err = nf_queue(skb, elem, state,
211 verdict >> NF_VERDICT_QBITS);
Florian Westphal563e1232011-10-31 12:20:16 +0100212 if (err < 0) {
213 if (err == -ECANCELED)
Florian Westphal06cdb632011-01-18 15:28:38 +0100214 goto next_hook;
Florian Westphal563e1232011-10-31 12:20:16 +0100215 if (err == -ESRCH &&
Florian Westphal94b27cc2011-01-18 16:08:30 +0100216 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
217 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100218 kfree_skb(skb);
219 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700220 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700221 rcu_read_unlock();
222 return ret;
223}
224EXPORT_SYMBOL(nf_hook_slow);
225
226
Herbert Xu37d41872007-10-14 00:39:18 -0700227int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700228{
Herbert Xu37d41872007-10-14 00:39:18 -0700229 if (writable_len > skb->len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700230 return 0;
231
232 /* Not exclusive use of packet? Must copy. */
Herbert Xu37d41872007-10-14 00:39:18 -0700233 if (!skb_cloned(skb)) {
234 if (writable_len <= skb_headlen(skb))
235 return 1;
236 } else if (skb_clone_writable(skb, writable_len))
237 return 1;
Harald Weltef6ebe772005-08-09 20:21:49 -0700238
Herbert Xu37d41872007-10-14 00:39:18 -0700239 if (writable_len <= skb_headlen(skb))
240 writable_len = 0;
241 else
242 writable_len -= skb_headlen(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700243
Herbert Xu37d41872007-10-14 00:39:18 -0700244 return !!__pskb_pull_tail(skb, writable_len);
Harald Weltef6ebe772005-08-09 20:21:49 -0700245}
246EXPORT_SYMBOL(skb_make_writable);
247
Igor Maravićc0cd1152011-12-12 02:58:24 +0000248#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Harald Weltef6ebe772005-08-09 20:21:49 -0700249/* This does not belong here, but locally generated errors need it if connection
250 tracking in use: without this, connection may not be in hash table, and hence
251 manufactured ICMP or RST packets will not be associated with it. */
Patrick McHardy312a0c162013-07-28 22:54:08 +0200252void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *)
253 __rcu __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -0700254EXPORT_SYMBOL(ip_ct_attach);
255
Patrick McHardy312a0c162013-07-28 22:54:08 +0200256void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb)
Harald Weltef6ebe772005-08-09 20:21:49 -0700257{
Patrick McHardy312a0c162013-07-28 22:54:08 +0200258 void (*attach)(struct sk_buff *, const struct sk_buff *);
Harald Weltef6ebe772005-08-09 20:21:49 -0700259
Patrick McHardyc3a47ab2007-02-12 11:09:19 -0800260 if (skb->nfct) {
261 rcu_read_lock();
262 attach = rcu_dereference(ip_ct_attach);
263 if (attach)
264 attach(new, skb);
265 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700266 }
267}
268EXPORT_SYMBOL(nf_ct_attach);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700269
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100270void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700271EXPORT_SYMBOL(nf_ct_destroy);
272
273void nf_conntrack_destroy(struct nf_conntrack *nfct)
274{
275 void (*destroy)(struct nf_conntrack *);
276
277 rcu_read_lock();
278 destroy = rcu_dereference(nf_ct_destroy);
279 BUG_ON(destroy == NULL);
280 destroy(nfct);
281 rcu_read_unlock();
282}
283EXPORT_SYMBOL(nf_conntrack_destroy);
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200284
Pablo Neira Ayuso5a05fae2012-06-20 20:50:31 +0200285struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200286EXPORT_SYMBOL_GPL(nfq_ct_hook);
287
Pablo Neira Ayusod584a612012-06-20 20:52:31 +0200288struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly;
289EXPORT_SYMBOL_GPL(nfq_ct_nat_hook);
290
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700291#endif /* CONFIG_NF_CONNTRACK */
Harald Weltef6ebe772005-08-09 20:21:49 -0700292
Patrick McHardyc7232c92012-08-26 19:14:06 +0200293#ifdef CONFIG_NF_NAT_NEEDED
294void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
295EXPORT_SYMBOL(nf_nat_decode_session_hook);
296#endif
297
Gao fengf3c1a442013-03-24 23:50:39 +0000298static int __net_init netfilter_net_init(struct net *net)
299{
300#ifdef CONFIG_PROC_FS
301 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
302 net->proc_net);
Pablo Neira Ayuso12202fa2013-04-05 19:40:10 +0200303 if (!net->nf.proc_netfilter) {
304 if (!net_eq(net, &init_net))
305 pr_err("cannot create netfilter proc entry");
306
Gao fengf3c1a442013-03-24 23:50:39 +0000307 return -ENOMEM;
308 }
309#endif
310 return 0;
311}
312
313static void __net_exit netfilter_net_exit(struct net *net)
314{
315 remove_proc_entry("netfilter", net->proc_net);
316}
317
318static struct pernet_operations netfilter_net_ops = {
319 .init = netfilter_net_init,
320 .exit = netfilter_net_exit,
321};
322
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +0000323int __init netfilter_init(void)
Harald Weltef6ebe772005-08-09 20:21:49 -0700324{
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +0000325 int i, h, ret;
326
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200327 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700328 for (h = 0; h < NF_MAX_HOOKS; h++)
329 INIT_LIST_HEAD(&nf_hooks[i][h]);
330 }
331
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +0000332 ret = register_pernet_subsys(&netfilter_net_ops);
333 if (ret < 0)
334 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700335
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +0000336 ret = netfilter_log_init();
337 if (ret < 0)
338 goto err_pernet;
339
340 return 0;
341err_pernet:
342 unregister_pernet_subsys(&netfilter_net_ops);
343err:
344 return ret;
Harald Weltef6ebe772005-08-09 20:21:49 -0700345}