1 /* netfilter.c: look after the filters for various protocols.
2 * Heavily influenced by the old firewall.c by David Bonn and Alan Cox.
4 * Thanks to Rob `CmdrTaco' Malda for not influencing this code in any
7 * Rusty Russell (C)2000 -- This code is GPL.
8 * Patrick McHardy (c) 2006-2012
10 #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>
19 #include <linux/netdevice.h>
20 #include <linux/netfilter_ipv6.h>
21 #include <linux/inetdevice.h>
22 #include <linux/proc_fs.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
25 #include <linux/rcupdate.h>
26 #include <net/net_namespace.h>
29 #include "nf_internals.h"
31 static DEFINE_MUTEX(afinfo_mutex);
33 const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
34 EXPORT_SYMBOL(nf_afinfo);
35 const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly;
36 EXPORT_SYMBOL_GPL(nf_ipv6_ops);
38 DEFINE_PER_CPU(bool, nf_skb_duplicated);
39 EXPORT_SYMBOL_GPL(nf_skb_duplicated);
41 int nf_register_afinfo(const struct nf_afinfo *afinfo)
43 mutex_lock(&afinfo_mutex);
44 RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo);
45 mutex_unlock(&afinfo_mutex);
48 EXPORT_SYMBOL_GPL(nf_register_afinfo);
50 void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
52 mutex_lock(&afinfo_mutex);
53 RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL);
54 mutex_unlock(&afinfo_mutex);
57 EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
59 #ifdef HAVE_JUMP_LABEL
60 struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
61 EXPORT_SYMBOL(nf_hooks_needed);
64 static DEFINE_MUTEX(nf_hook_mutex);
65 #define nf_entry_dereference(e) \
66 rcu_dereference_protected(e, lockdep_is_held(&nf_hook_mutex))
68 static struct nf_hook_entry *nf_hook_entry_head(struct net *net,
69 const struct nf_hook_ops *reg)
71 struct nf_hook_entry *hook_head = NULL;
73 if (reg->pf != NFPROTO_NETDEV)
74 hook_head = nf_entry_dereference(net->nf.hooks[reg->pf]
76 else if (reg->hooknum == NF_NETDEV_INGRESS) {
77 #ifdef CONFIG_NETFILTER_INGRESS
78 if (reg->dev && dev_net(reg->dev) == net)
81 reg->dev->nf_hooks_ingress);
87 /* must hold nf_hook_mutex */
88 static void nf_set_hooks_head(struct net *net, const struct nf_hook_ops *reg,
89 struct nf_hook_entry *entry)
93 /* We already checked in nf_register_net_hook() that this is
96 rcu_assign_pointer(reg->dev->nf_hooks_ingress, entry);
99 rcu_assign_pointer(net->nf.hooks[reg->pf][reg->hooknum],
105 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)
107 struct nf_hook_entry *hooks_entry;
108 struct nf_hook_entry *entry;
110 if (reg->pf == NFPROTO_NETDEV &&
111 (reg->hooknum != NF_NETDEV_INGRESS ||
112 !reg->dev || dev_net(reg->dev) != net))
115 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
119 entry->orig_ops = reg;
123 mutex_lock(&nf_hook_mutex);
124 hooks_entry = nf_hook_entry_head(net, reg);
126 if (hooks_entry && hooks_entry->orig_ops->priority > reg->priority) {
127 /* This is the case where we need to insert at the head */
128 entry->next = hooks_entry;
132 while (hooks_entry &&
133 reg->priority >= hooks_entry->orig_ops->priority &&
134 nf_entry_dereference(hooks_entry->next)) {
135 hooks_entry = nf_entry_dereference(hooks_entry->next);
139 entry->next = nf_entry_dereference(hooks_entry->next);
140 rcu_assign_pointer(hooks_entry->next, entry);
142 nf_set_hooks_head(net, reg, entry);
145 mutex_unlock(&nf_hook_mutex);
146 #ifdef CONFIG_NETFILTER_INGRESS
147 if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS)
148 net_inc_ingress_queue();
150 #ifdef HAVE_JUMP_LABEL
151 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
155 EXPORT_SYMBOL(nf_register_net_hook);
157 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
159 struct nf_hook_entry *hooks_entry;
161 mutex_lock(&nf_hook_mutex);
162 hooks_entry = nf_hook_entry_head(net, reg);
163 if (hooks_entry->orig_ops == reg) {
164 nf_set_hooks_head(net, reg,
165 nf_entry_dereference(hooks_entry->next));
168 while (hooks_entry && nf_entry_dereference(hooks_entry->next)) {
169 struct nf_hook_entry *next =
170 nf_entry_dereference(hooks_entry->next);
171 struct nf_hook_entry *nnext;
173 if (next->orig_ops != reg) {
177 nnext = nf_entry_dereference(next->next);
178 rcu_assign_pointer(hooks_entry->next, nnext);
184 mutex_unlock(&nf_hook_mutex);
186 WARN(1, "nf_unregister_net_hook: hook not found!\n");
189 #ifdef CONFIG_NETFILTER_INGRESS
190 if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS)
191 net_dec_ingress_queue();
193 #ifdef HAVE_JUMP_LABEL
194 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
197 nf_queue_nf_hook_drop(net, hooks_entry);
198 /* other cpu might still process nfqueue verdict that used reg */
202 EXPORT_SYMBOL(nf_unregister_net_hook);
204 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
210 for (i = 0; i < n; i++) {
211 err = nf_register_net_hook(net, ®[i]);
219 nf_unregister_net_hooks(net, reg, i);
222 EXPORT_SYMBOL(nf_register_net_hooks);
224 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
228 nf_unregister_net_hook(net, ®[n]);
230 EXPORT_SYMBOL(nf_unregister_net_hooks);
232 static LIST_HEAD(nf_hook_list);
234 int nf_register_hook(struct nf_hook_ops *reg)
236 struct net *net, *last;
241 ret = nf_register_net_hook(net, reg);
242 if (ret && ret != -ENOENT)
245 list_add_tail(®->list, &nf_hook_list);
254 nf_unregister_net_hook(net, reg);
259 EXPORT_SYMBOL(nf_register_hook);
261 void nf_unregister_hook(struct nf_hook_ops *reg)
266 list_del(®->list);
268 nf_unregister_net_hook(net, reg);
271 EXPORT_SYMBOL(nf_unregister_hook);
273 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
278 for (i = 0; i < n; i++) {
279 err = nf_register_hook(®[i]);
287 nf_unregister_hooks(reg, i);
290 EXPORT_SYMBOL(nf_register_hooks);
292 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
295 nf_unregister_hook(®[n]);
297 EXPORT_SYMBOL(nf_unregister_hooks);
299 unsigned int nf_iterate(struct sk_buff *skb,
300 struct nf_hook_state *state,
301 struct nf_hook_entry **entryp)
303 unsigned int verdict;
306 * The caller must not block between calls to this
307 * function because of risk of continuing from deleted element.
310 if (state->thresh > (*entryp)->ops.priority) {
311 *entryp = rcu_dereference((*entryp)->next);
315 /* Optimization: we don't need to hold module
316 reference here, since function can't sleep. --RR */
318 verdict = (*entryp)->ops.hook((*entryp)->ops.priv, skb, state);
319 if (verdict != NF_ACCEPT) {
320 #ifdef CONFIG_NETFILTER_DEBUG
321 if (unlikely((verdict & NF_VERDICT_MASK)
323 NFDEBUG("Evil return from %p(%u).\n",
324 (*entryp)->ops.hook, state->hook);
325 *entryp = rcu_dereference((*entryp)->next);
329 if (verdict != NF_REPEAT)
333 *entryp = rcu_dereference((*entryp)->next);
339 /* Returns 1 if okfn() needs to be executed by the caller,
340 * -EPERM for NF_DROP, 0 otherwise. Caller must hold rcu_read_lock. */
341 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state)
343 struct nf_hook_entry *entry;
344 unsigned int verdict;
347 entry = rcu_dereference(state->hook_entries);
349 verdict = nf_iterate(skb, state, &entry);
350 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
352 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
354 ret = NF_DROP_GETERR(verdict);
357 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
360 RCU_INIT_POINTER(state->hook_entries, entry);
361 err = nf_queue(skb, state, verdict >> NF_VERDICT_QBITS);
364 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
371 EXPORT_SYMBOL(nf_hook_slow);
374 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
376 if (writable_len > skb->len)
379 /* Not exclusive use of packet? Must copy. */
380 if (!skb_cloned(skb)) {
381 if (writable_len <= skb_headlen(skb))
383 } else if (skb_clone_writable(skb, writable_len))
386 if (writable_len <= skb_headlen(skb))
389 writable_len -= skb_headlen(skb);
391 return !!__pskb_pull_tail(skb, writable_len);
393 EXPORT_SYMBOL(skb_make_writable);
395 /* This needs to be compiled in any case to avoid dependencies between the
396 * nfnetlink_queue code and nf_conntrack.
398 struct nfnl_ct_hook __rcu *nfnl_ct_hook __read_mostly;
399 EXPORT_SYMBOL_GPL(nfnl_ct_hook);
401 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
402 /* This does not belong here, but locally generated errors need it if connection
403 tracking in use: without this, connection may not be in hash table, and hence
404 manufactured ICMP or RST packets will not be associated with it. */
405 void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *)
407 EXPORT_SYMBOL(ip_ct_attach);
409 void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb)
411 void (*attach)(struct sk_buff *, const struct sk_buff *);
415 attach = rcu_dereference(ip_ct_attach);
421 EXPORT_SYMBOL(nf_ct_attach);
423 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
424 EXPORT_SYMBOL(nf_ct_destroy);
426 void nf_conntrack_destroy(struct nf_conntrack *nfct)
428 void (*destroy)(struct nf_conntrack *);
431 destroy = rcu_dereference(nf_ct_destroy);
432 BUG_ON(destroy == NULL);
436 EXPORT_SYMBOL(nf_conntrack_destroy);
438 /* Built-in default zone used e.g. by modules. */
439 const struct nf_conntrack_zone nf_ct_zone_dflt = {
440 .id = NF_CT_DEFAULT_ZONE_ID,
441 .dir = NF_CT_DEFAULT_ZONE_DIR,
443 EXPORT_SYMBOL_GPL(nf_ct_zone_dflt);
444 #endif /* CONFIG_NF_CONNTRACK */
446 #ifdef CONFIG_NF_NAT_NEEDED
447 void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
448 EXPORT_SYMBOL(nf_nat_decode_session_hook);
451 static int nf_register_hook_list(struct net *net)
453 struct nf_hook_ops *elem;
457 list_for_each_entry(elem, &nf_hook_list, list) {
458 ret = nf_register_net_hook(net, elem);
459 if (ret && ret != -ENOENT)
466 list_for_each_entry_continue_reverse(elem, &nf_hook_list, list)
467 nf_unregister_net_hook(net, elem);
472 static void nf_unregister_hook_list(struct net *net)
474 struct nf_hook_ops *elem;
477 list_for_each_entry(elem, &nf_hook_list, list)
478 nf_unregister_net_hook(net, elem);
482 static int __net_init netfilter_net_init(struct net *net)
486 for (i = 0; i < ARRAY_SIZE(net->nf.hooks); i++) {
487 for (h = 0; h < NF_MAX_HOOKS; h++)
488 RCU_INIT_POINTER(net->nf.hooks[i][h], NULL);
491 #ifdef CONFIG_PROC_FS
492 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
494 if (!net->nf.proc_netfilter) {
495 if (!net_eq(net, &init_net))
496 pr_err("cannot create netfilter proc entry");
501 ret = nf_register_hook_list(net);
503 remove_proc_entry("netfilter", net->proc_net);
508 static void __net_exit netfilter_net_exit(struct net *net)
510 nf_unregister_hook_list(net);
511 remove_proc_entry("netfilter", net->proc_net);
514 static struct pernet_operations netfilter_net_ops = {
515 .init = netfilter_net_init,
516 .exit = netfilter_net_exit,
519 int __init netfilter_init(void)
523 ret = register_pernet_subsys(&netfilter_net_ops);
527 ret = netfilter_log_init();
533 unregister_pernet_subsys(&netfilter_net_ops);