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
9 #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>
18 #include <linux/netdevice.h>
19 #include <linux/netfilter_ipv6.h>
20 #include <linux/inetdevice.h>
21 #include <linux/proc_fs.h>
22 #include <linux/mutex.h>
24 #include <linux/rcupdate.h>
25 #include <net/net_namespace.h>
26 #include <net/netfilter/nf_queue.h>
29 #include "nf_internals.h"
31 const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly;
32 EXPORT_SYMBOL_GPL(nf_ipv6_ops);
34 DEFINE_PER_CPU(bool, nf_skb_duplicated);
35 EXPORT_SYMBOL_GPL(nf_skb_duplicated);
37 #ifdef CONFIG_JUMP_LABEL
38 struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
39 EXPORT_SYMBOL(nf_hooks_needed);
42 static DEFINE_MUTEX(nf_hook_mutex);
44 /* max hooks per family/hooknum */
45 #define MAX_HOOK_COUNT 1024
47 #define nf_entry_dereference(e) \
48 rcu_dereference_protected(e, lockdep_is_held(&nf_hook_mutex))
50 static struct nf_hook_entries *allocate_hook_entries_size(u16 num)
52 struct nf_hook_entries *e;
53 size_t alloc = sizeof(*e) +
54 sizeof(struct nf_hook_entry) * num +
55 sizeof(struct nf_hook_ops *) * num +
56 sizeof(struct nf_hook_entries_rcu_head);
61 e = kvzalloc(alloc, GFP_KERNEL);
63 e->num_hook_entries = num;
67 static void __nf_hook_entries_free(struct rcu_head *h)
69 struct nf_hook_entries_rcu_head *head;
71 head = container_of(h, struct nf_hook_entries_rcu_head, head);
72 kvfree(head->allocation);
75 static void nf_hook_entries_free(struct nf_hook_entries *e)
77 struct nf_hook_entries_rcu_head *head;
78 struct nf_hook_ops **ops;
84 num = e->num_hook_entries;
85 ops = nf_hook_entries_get_hook_ops(e);
86 head = (void *)&ops[num];
88 call_rcu(&head->head, __nf_hook_entries_free);
91 static unsigned int accept_all(void *priv,
93 const struct nf_hook_state *state)
95 return NF_ACCEPT; /* ACCEPT makes nf_hook_slow call next hook */
98 static const struct nf_hook_ops dummy_ops = {
103 static struct nf_hook_entries *
104 nf_hook_entries_grow(const struct nf_hook_entries *old,
105 const struct nf_hook_ops *reg)
107 unsigned int i, alloc_entries, nhooks, old_entries;
108 struct nf_hook_ops **orig_ops = NULL;
109 struct nf_hook_ops **new_ops;
110 struct nf_hook_entries *new;
111 bool inserted = false;
114 old_entries = old ? old->num_hook_entries : 0;
117 orig_ops = nf_hook_entries_get_hook_ops(old);
119 for (i = 0; i < old_entries; i++) {
120 if (orig_ops[i] != &dummy_ops)
125 if (alloc_entries > MAX_HOOK_COUNT)
126 return ERR_PTR(-E2BIG);
128 new = allocate_hook_entries_size(alloc_entries);
130 return ERR_PTR(-ENOMEM);
132 new_ops = nf_hook_entries_get_hook_ops(new);
136 while (i < old_entries) {
137 if (orig_ops[i] == &dummy_ops) {
142 if (inserted || reg->priority > orig_ops[i]->priority) {
143 new_ops[nhooks] = (void *)orig_ops[i];
144 new->hooks[nhooks] = old->hooks[i];
147 new_ops[nhooks] = (void *)reg;
148 new->hooks[nhooks].hook = reg->hook;
149 new->hooks[nhooks].priv = reg->priv;
156 new_ops[nhooks] = (void *)reg;
157 new->hooks[nhooks].hook = reg->hook;
158 new->hooks[nhooks].priv = reg->priv;
164 static void hooks_validate(const struct nf_hook_entries *hooks)
166 #ifdef CONFIG_DEBUG_MISC
167 struct nf_hook_ops **orig_ops;
171 orig_ops = nf_hook_entries_get_hook_ops(hooks);
173 for (i = 0; i < hooks->num_hook_entries; i++) {
174 if (orig_ops[i] == &dummy_ops)
177 WARN_ON(orig_ops[i]->priority < prio);
179 if (orig_ops[i]->priority > prio)
180 prio = orig_ops[i]->priority;
185 int nf_hook_entries_insert_raw(struct nf_hook_entries __rcu **pp,
186 const struct nf_hook_ops *reg)
188 struct nf_hook_entries *new_hooks;
189 struct nf_hook_entries *p;
191 p = rcu_dereference_raw(*pp);
192 new_hooks = nf_hook_entries_grow(p, reg);
193 if (IS_ERR(new_hooks))
194 return PTR_ERR(new_hooks);
196 hooks_validate(new_hooks);
198 rcu_assign_pointer(*pp, new_hooks);
200 BUG_ON(p == new_hooks);
201 nf_hook_entries_free(p);
204 EXPORT_SYMBOL_GPL(nf_hook_entries_insert_raw);
207 * __nf_hook_entries_try_shrink - try to shrink hook array
209 * @old -- current hook blob at @pp
210 * @pp -- location of hook blob
212 * Hook unregistration must always succeed, so to-be-removed hooks
213 * are replaced by a dummy one that will just move to next hook.
215 * This counts the current dummy hooks, attempts to allocate new blob,
216 * copies the live hooks, then replaces and discards old one.
220 * Returns address to free, or NULL.
222 static void *__nf_hook_entries_try_shrink(struct nf_hook_entries *old,
223 struct nf_hook_entries __rcu **pp)
225 unsigned int i, j, skip = 0, hook_entries;
226 struct nf_hook_entries *new = NULL;
227 struct nf_hook_ops **orig_ops;
228 struct nf_hook_ops **new_ops;
230 if (WARN_ON_ONCE(!old))
233 orig_ops = nf_hook_entries_get_hook_ops(old);
234 for (i = 0; i < old->num_hook_entries; i++) {
235 if (orig_ops[i] == &dummy_ops)
239 /* if skip == hook_entries all hooks have been removed */
240 hook_entries = old->num_hook_entries;
241 if (skip == hook_entries)
247 hook_entries -= skip;
248 new = allocate_hook_entries_size(hook_entries);
252 new_ops = nf_hook_entries_get_hook_ops(new);
253 for (i = 0, j = 0; i < old->num_hook_entries; i++) {
254 if (orig_ops[i] == &dummy_ops)
256 new->hooks[j] = old->hooks[i];
257 new_ops[j] = (void *)orig_ops[i];
262 rcu_assign_pointer(*pp, new);
266 static struct nf_hook_entries __rcu **
267 nf_hook_entry_head(struct net *net, int pf, unsigned int hooknum,
268 struct net_device *dev)
273 #ifdef CONFIG_NETFILTER_FAMILY_ARP
275 if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_arp) <= hooknum))
277 return net->nf.hooks_arp + hooknum;
279 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
281 if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_bridge) <= hooknum))
283 return net->nf.hooks_bridge + hooknum;
285 #ifdef CONFIG_NETFILTER_INGRESS
287 if (WARN_ON_ONCE(hooknum != NF_INET_INGRESS))
289 if (!dev || dev_net(dev) != net) {
293 return &dev->nf_hooks_ingress;
296 if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_ipv4) <= hooknum))
298 return net->nf.hooks_ipv4 + hooknum;
300 if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_ipv6) <= hooknum))
302 return net->nf.hooks_ipv6 + hooknum;
303 #if IS_ENABLED(CONFIG_DECNET)
305 if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_decnet) <= hooknum))
307 return net->nf.hooks_decnet + hooknum;
314 #ifdef CONFIG_NETFILTER_INGRESS
315 if (hooknum == NF_NETDEV_INGRESS) {
316 if (dev && dev_net(dev) == net)
317 return &dev->nf_hooks_ingress;
320 #ifdef CONFIG_NETFILTER_EGRESS
321 if (hooknum == NF_NETDEV_EGRESS) {
322 if (dev && dev_net(dev) == net)
323 return &dev->nf_hooks_egress;
330 static int nf_ingress_check(struct net *net, const struct nf_hook_ops *reg,
333 #ifndef CONFIG_NETFILTER_INGRESS
334 if (reg->hooknum == hooknum)
337 if (reg->hooknum != hooknum ||
338 !reg->dev || dev_net(reg->dev) != net)
344 static inline bool __maybe_unused nf_ingress_hook(const struct nf_hook_ops *reg,
347 if ((pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS) ||
348 (pf == NFPROTO_INET && reg->hooknum == NF_INET_INGRESS))
354 static inline bool __maybe_unused nf_egress_hook(const struct nf_hook_ops *reg,
357 return pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_EGRESS;
360 static void nf_static_key_inc(const struct nf_hook_ops *reg, int pf)
362 #ifdef CONFIG_JUMP_LABEL
365 if (pf == NFPROTO_INET && reg->hooknum == NF_INET_INGRESS) {
367 hooknum = NF_NETDEV_INGRESS;
369 hooknum = reg->hooknum;
371 static_key_slow_inc(&nf_hooks_needed[pf][hooknum]);
375 static void nf_static_key_dec(const struct nf_hook_ops *reg, int pf)
377 #ifdef CONFIG_JUMP_LABEL
380 if (pf == NFPROTO_INET && reg->hooknum == NF_INET_INGRESS) {
382 hooknum = NF_NETDEV_INGRESS;
384 hooknum = reg->hooknum;
386 static_key_slow_dec(&nf_hooks_needed[pf][hooknum]);
390 static int __nf_register_net_hook(struct net *net, int pf,
391 const struct nf_hook_ops *reg)
393 struct nf_hook_entries *p, *new_hooks;
394 struct nf_hook_entries __rcu **pp;
399 #ifndef CONFIG_NETFILTER_INGRESS
400 if (reg->hooknum == NF_NETDEV_INGRESS)
403 #ifndef CONFIG_NETFILTER_EGRESS
404 if (reg->hooknum == NF_NETDEV_EGRESS)
407 if ((reg->hooknum != NF_NETDEV_INGRESS &&
408 reg->hooknum != NF_NETDEV_EGRESS) ||
409 !reg->dev || dev_net(reg->dev) != net)
413 if (reg->hooknum != NF_INET_INGRESS)
416 err = nf_ingress_check(net, reg, NF_INET_INGRESS);
422 pp = nf_hook_entry_head(net, pf, reg->hooknum, reg->dev);
426 mutex_lock(&nf_hook_mutex);
428 p = nf_entry_dereference(*pp);
429 new_hooks = nf_hook_entries_grow(p, reg);
431 if (!IS_ERR(new_hooks))
432 rcu_assign_pointer(*pp, new_hooks);
434 mutex_unlock(&nf_hook_mutex);
435 if (IS_ERR(new_hooks))
436 return PTR_ERR(new_hooks);
438 hooks_validate(new_hooks);
439 #ifdef CONFIG_NETFILTER_INGRESS
440 if (nf_ingress_hook(reg, pf))
441 net_inc_ingress_queue();
443 #ifdef CONFIG_NETFILTER_EGRESS
444 if (nf_egress_hook(reg, pf))
445 net_inc_egress_queue();
447 nf_static_key_inc(reg, pf);
449 BUG_ON(p == new_hooks);
450 nf_hook_entries_free(p);
455 * nf_remove_net_hook - remove a hook from blob
457 * @oldp: current address of hook blob
458 * @unreg: hook to unregister
460 * This cannot fail, hook unregistration must always succeed.
461 * Therefore replace the to-be-removed hook with a dummy hook.
463 static bool nf_remove_net_hook(struct nf_hook_entries *old,
464 const struct nf_hook_ops *unreg)
466 struct nf_hook_ops **orig_ops;
469 orig_ops = nf_hook_entries_get_hook_ops(old);
470 for (i = 0; i < old->num_hook_entries; i++) {
471 if (orig_ops[i] != unreg)
473 WRITE_ONCE(old->hooks[i].hook, accept_all);
474 WRITE_ONCE(orig_ops[i], (void *)&dummy_ops);
481 static void __nf_unregister_net_hook(struct net *net, int pf,
482 const struct nf_hook_ops *reg)
484 struct nf_hook_entries __rcu **pp;
485 struct nf_hook_entries *p;
487 pp = nf_hook_entry_head(net, pf, reg->hooknum, reg->dev);
491 mutex_lock(&nf_hook_mutex);
493 p = nf_entry_dereference(*pp);
494 if (WARN_ON_ONCE(!p)) {
495 mutex_unlock(&nf_hook_mutex);
499 if (nf_remove_net_hook(p, reg)) {
500 #ifdef CONFIG_NETFILTER_INGRESS
501 if (nf_ingress_hook(reg, pf))
502 net_dec_ingress_queue();
504 #ifdef CONFIG_NETFILTER_EGRESS
505 if (nf_egress_hook(reg, pf))
506 net_dec_egress_queue();
508 nf_static_key_dec(reg, pf);
510 WARN_ONCE(1, "hook not found, pf %d num %d", pf, reg->hooknum);
513 p = __nf_hook_entries_try_shrink(p, pp);
514 mutex_unlock(&nf_hook_mutex);
518 nf_queue_nf_hook_drop(net);
519 nf_hook_entries_free(p);
522 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
524 if (reg->pf == NFPROTO_INET) {
525 if (reg->hooknum == NF_INET_INGRESS) {
526 __nf_unregister_net_hook(net, NFPROTO_INET, reg);
528 __nf_unregister_net_hook(net, NFPROTO_IPV4, reg);
529 __nf_unregister_net_hook(net, NFPROTO_IPV6, reg);
532 __nf_unregister_net_hook(net, reg->pf, reg);
535 EXPORT_SYMBOL(nf_unregister_net_hook);
537 void nf_hook_entries_delete_raw(struct nf_hook_entries __rcu **pp,
538 const struct nf_hook_ops *reg)
540 struct nf_hook_entries *p;
542 p = rcu_dereference_raw(*pp);
543 if (nf_remove_net_hook(p, reg)) {
544 p = __nf_hook_entries_try_shrink(p, pp);
545 nf_hook_entries_free(p);
548 EXPORT_SYMBOL_GPL(nf_hook_entries_delete_raw);
550 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)
554 if (reg->pf == NFPROTO_INET) {
555 if (reg->hooknum == NF_INET_INGRESS) {
556 err = __nf_register_net_hook(net, NFPROTO_INET, reg);
560 err = __nf_register_net_hook(net, NFPROTO_IPV4, reg);
564 err = __nf_register_net_hook(net, NFPROTO_IPV6, reg);
566 __nf_unregister_net_hook(net, NFPROTO_IPV4, reg);
571 err = __nf_register_net_hook(net, reg->pf, reg);
578 EXPORT_SYMBOL(nf_register_net_hook);
580 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
586 for (i = 0; i < n; i++) {
587 err = nf_register_net_hook(net, ®[i]);
595 nf_unregister_net_hooks(net, reg, i);
598 EXPORT_SYMBOL(nf_register_net_hooks);
600 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
601 unsigned int hookcount)
605 for (i = 0; i < hookcount; i++)
606 nf_unregister_net_hook(net, ®[i]);
608 EXPORT_SYMBOL(nf_unregister_net_hooks);
610 /* Returns 1 if okfn() needs to be executed by the caller,
611 * -EPERM for NF_DROP, 0 otherwise. Caller must hold rcu_read_lock. */
612 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
613 const struct nf_hook_entries *e, unsigned int s)
615 unsigned int verdict;
618 for (; s < e->num_hook_entries; s++) {
619 verdict = nf_hook_entry_hookfn(&e->hooks[s], skb, state);
620 switch (verdict & NF_VERDICT_MASK) {
625 ret = NF_DROP_GETERR(verdict);
630 ret = nf_queue(skb, state, s, verdict);
635 /* Implicit handling for NF_STOLEN, as well as any other
636 * non conventional verdicts.
644 EXPORT_SYMBOL(nf_hook_slow);
646 void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state,
647 const struct nf_hook_entries *e)
649 struct sk_buff *skb, *next;
650 struct list_head sublist;
653 INIT_LIST_HEAD(&sublist);
655 list_for_each_entry_safe(skb, next, head, list) {
656 skb_list_del_init(skb);
657 ret = nf_hook_slow(skb, state, e, 0);
659 list_add_tail(&skb->list, &sublist);
661 /* Put passed packets back on main list */
662 list_splice(&sublist, head);
664 EXPORT_SYMBOL(nf_hook_slow_list);
666 /* This needs to be compiled in any case to avoid dependencies between the
667 * nfnetlink_queue code and nf_conntrack.
669 const struct nfnl_ct_hook __rcu *nfnl_ct_hook __read_mostly;
670 EXPORT_SYMBOL_GPL(nfnl_ct_hook);
672 const struct nf_ct_hook __rcu *nf_ct_hook __read_mostly;
673 EXPORT_SYMBOL_GPL(nf_ct_hook);
675 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
676 const struct nf_nat_hook __rcu *nf_nat_hook __read_mostly;
677 EXPORT_SYMBOL_GPL(nf_nat_hook);
679 /* This does not belong here, but locally generated errors need it if connection
680 * tracking in use: without this, connection may not be in hash table, and hence
681 * manufactured ICMP or RST packets will not be associated with it.
683 void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb)
685 const struct nf_ct_hook *ct_hook;
689 ct_hook = rcu_dereference(nf_ct_hook);
691 ct_hook->attach(new, skb);
695 EXPORT_SYMBOL(nf_ct_attach);
697 void nf_conntrack_destroy(struct nf_conntrack *nfct)
699 const struct nf_ct_hook *ct_hook;
702 ct_hook = rcu_dereference(nf_ct_hook);
703 BUG_ON(ct_hook == NULL);
704 ct_hook->destroy(nfct);
707 EXPORT_SYMBOL(nf_conntrack_destroy);
709 bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
710 const struct sk_buff *skb)
712 const struct nf_ct_hook *ct_hook;
716 ct_hook = rcu_dereference(nf_ct_hook);
718 ret = ct_hook->get_tuple_skb(dst_tuple, skb);
722 EXPORT_SYMBOL(nf_ct_get_tuple_skb);
724 /* Built-in default zone used e.g. by modules. */
725 const struct nf_conntrack_zone nf_ct_zone_dflt = {
726 .id = NF_CT_DEFAULT_ZONE_ID,
727 .dir = NF_CT_DEFAULT_ZONE_DIR,
729 EXPORT_SYMBOL_GPL(nf_ct_zone_dflt);
730 #endif /* CONFIG_NF_CONNTRACK */
732 static void __net_init
733 __netfilter_net_init(struct nf_hook_entries __rcu **e, int max)
737 for (h = 0; h < max; h++)
738 RCU_INIT_POINTER(e[h], NULL);
741 static int __net_init netfilter_net_init(struct net *net)
743 __netfilter_net_init(net->nf.hooks_ipv4, ARRAY_SIZE(net->nf.hooks_ipv4));
744 __netfilter_net_init(net->nf.hooks_ipv6, ARRAY_SIZE(net->nf.hooks_ipv6));
745 #ifdef CONFIG_NETFILTER_FAMILY_ARP
746 __netfilter_net_init(net->nf.hooks_arp, ARRAY_SIZE(net->nf.hooks_arp));
748 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
749 __netfilter_net_init(net->nf.hooks_bridge, ARRAY_SIZE(net->nf.hooks_bridge));
751 #if IS_ENABLED(CONFIG_DECNET)
752 __netfilter_net_init(net->nf.hooks_decnet, ARRAY_SIZE(net->nf.hooks_decnet));
755 #ifdef CONFIG_PROC_FS
756 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
758 if (!net->nf.proc_netfilter) {
759 if (!net_eq(net, &init_net))
760 pr_err("cannot create netfilter proc entry");
769 static void __net_exit netfilter_net_exit(struct net *net)
771 remove_proc_entry("netfilter", net->proc_net);
774 static struct pernet_operations netfilter_net_ops = {
775 .init = netfilter_net_init,
776 .exit = netfilter_net_exit,
779 int __init netfilter_init(void)
783 ret = register_pernet_subsys(&netfilter_net_ops);
787 ret = netfilter_log_init();
793 unregister_pernet_subsys(&netfilter_net_ops);