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_ACCOUNT);
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)
123 /* Restrict BPF hook type to force a unique priority, not
124 * shared at attach time.
126 * This is mainly to avoid ordering issues between two
127 * different bpf programs, this doesn't prevent a normal
128 * hook at same priority as a bpf one (we don't want to
129 * prevent defrag, conntrack, iptables etc from attaching).
131 if (reg->priority == orig_ops[i]->priority &&
132 reg->hook_ops_type == NF_HOOK_OP_BPF)
133 return ERR_PTR(-EBUSY);
137 if (alloc_entries > MAX_HOOK_COUNT)
138 return ERR_PTR(-E2BIG);
140 new = allocate_hook_entries_size(alloc_entries);
142 return ERR_PTR(-ENOMEM);
144 new_ops = nf_hook_entries_get_hook_ops(new);
148 while (i < old_entries) {
149 if (orig_ops[i] == &dummy_ops) {
154 if (inserted || reg->priority > orig_ops[i]->priority) {
155 new_ops[nhooks] = (void *)orig_ops[i];
156 new->hooks[nhooks] = old->hooks[i];
159 new_ops[nhooks] = (void *)reg;
160 new->hooks[nhooks].hook = reg->hook;
161 new->hooks[nhooks].priv = reg->priv;
168 new_ops[nhooks] = (void *)reg;
169 new->hooks[nhooks].hook = reg->hook;
170 new->hooks[nhooks].priv = reg->priv;
176 static void hooks_validate(const struct nf_hook_entries *hooks)
178 #ifdef CONFIG_DEBUG_MISC
179 struct nf_hook_ops **orig_ops;
183 orig_ops = nf_hook_entries_get_hook_ops(hooks);
185 for (i = 0; i < hooks->num_hook_entries; i++) {
186 if (orig_ops[i] == &dummy_ops)
189 WARN_ON(orig_ops[i]->priority < prio);
191 if (orig_ops[i]->priority > prio)
192 prio = orig_ops[i]->priority;
197 int nf_hook_entries_insert_raw(struct nf_hook_entries __rcu **pp,
198 const struct nf_hook_ops *reg)
200 struct nf_hook_entries *new_hooks;
201 struct nf_hook_entries *p;
203 p = rcu_dereference_raw(*pp);
204 new_hooks = nf_hook_entries_grow(p, reg);
205 if (IS_ERR(new_hooks))
206 return PTR_ERR(new_hooks);
208 hooks_validate(new_hooks);
210 rcu_assign_pointer(*pp, new_hooks);
212 BUG_ON(p == new_hooks);
213 nf_hook_entries_free(p);
216 EXPORT_SYMBOL_GPL(nf_hook_entries_insert_raw);
219 * __nf_hook_entries_try_shrink - try to shrink hook array
221 * @old -- current hook blob at @pp
222 * @pp -- location of hook blob
224 * Hook unregistration must always succeed, so to-be-removed hooks
225 * are replaced by a dummy one that will just move to next hook.
227 * This counts the current dummy hooks, attempts to allocate new blob,
228 * copies the live hooks, then replaces and discards old one.
232 * Returns address to free, or NULL.
234 static void *__nf_hook_entries_try_shrink(struct nf_hook_entries *old,
235 struct nf_hook_entries __rcu **pp)
237 unsigned int i, j, skip = 0, hook_entries;
238 struct nf_hook_entries *new = NULL;
239 struct nf_hook_ops **orig_ops;
240 struct nf_hook_ops **new_ops;
242 if (WARN_ON_ONCE(!old))
245 orig_ops = nf_hook_entries_get_hook_ops(old);
246 for (i = 0; i < old->num_hook_entries; i++) {
247 if (orig_ops[i] == &dummy_ops)
251 /* if skip == hook_entries all hooks have been removed */
252 hook_entries = old->num_hook_entries;
253 if (skip == hook_entries)
259 hook_entries -= skip;
260 new = allocate_hook_entries_size(hook_entries);
264 new_ops = nf_hook_entries_get_hook_ops(new);
265 for (i = 0, j = 0; i < old->num_hook_entries; i++) {
266 if (orig_ops[i] == &dummy_ops)
268 new->hooks[j] = old->hooks[i];
269 new_ops[j] = (void *)orig_ops[i];
274 rcu_assign_pointer(*pp, new);
278 static struct nf_hook_entries __rcu **
279 nf_hook_entry_head(struct net *net, int pf, unsigned int hooknum,
280 struct net_device *dev)
285 #ifdef CONFIG_NETFILTER_FAMILY_ARP
287 if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_arp) <= hooknum))
289 return net->nf.hooks_arp + hooknum;
291 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
293 if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_bridge) <= hooknum))
295 return net->nf.hooks_bridge + hooknum;
297 #ifdef CONFIG_NETFILTER_INGRESS
299 if (WARN_ON_ONCE(hooknum != NF_INET_INGRESS))
301 if (!dev || dev_net(dev) != net) {
305 return &dev->nf_hooks_ingress;
308 if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_ipv4) <= hooknum))
310 return net->nf.hooks_ipv4 + hooknum;
312 if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_ipv6) <= hooknum))
314 return net->nf.hooks_ipv6 + hooknum;
320 #ifdef CONFIG_NETFILTER_INGRESS
321 if (hooknum == NF_NETDEV_INGRESS) {
322 if (dev && dev_net(dev) == net)
323 return &dev->nf_hooks_ingress;
326 #ifdef CONFIG_NETFILTER_EGRESS
327 if (hooknum == NF_NETDEV_EGRESS) {
328 if (dev && dev_net(dev) == net)
329 return &dev->nf_hooks_egress;
336 static int nf_ingress_check(struct net *net, const struct nf_hook_ops *reg,
339 #ifndef CONFIG_NETFILTER_INGRESS
340 if (reg->hooknum == hooknum)
343 if (reg->hooknum != hooknum ||
344 !reg->dev || dev_net(reg->dev) != net)
350 static inline bool __maybe_unused nf_ingress_hook(const struct nf_hook_ops *reg,
353 if ((pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS) ||
354 (pf == NFPROTO_INET && reg->hooknum == NF_INET_INGRESS))
360 static inline bool __maybe_unused nf_egress_hook(const struct nf_hook_ops *reg,
363 return pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_EGRESS;
366 static void nf_static_key_inc(const struct nf_hook_ops *reg, int pf)
368 #ifdef CONFIG_JUMP_LABEL
371 if (pf == NFPROTO_INET && reg->hooknum == NF_INET_INGRESS) {
373 hooknum = NF_NETDEV_INGRESS;
375 hooknum = reg->hooknum;
377 static_key_slow_inc(&nf_hooks_needed[pf][hooknum]);
381 static void nf_static_key_dec(const struct nf_hook_ops *reg, int pf)
383 #ifdef CONFIG_JUMP_LABEL
386 if (pf == NFPROTO_INET && reg->hooknum == NF_INET_INGRESS) {
388 hooknum = NF_NETDEV_INGRESS;
390 hooknum = reg->hooknum;
392 static_key_slow_dec(&nf_hooks_needed[pf][hooknum]);
396 static int __nf_register_net_hook(struct net *net, int pf,
397 const struct nf_hook_ops *reg)
399 struct nf_hook_entries *p, *new_hooks;
400 struct nf_hook_entries __rcu **pp;
405 #ifndef CONFIG_NETFILTER_INGRESS
406 if (reg->hooknum == NF_NETDEV_INGRESS)
409 #ifndef CONFIG_NETFILTER_EGRESS
410 if (reg->hooknum == NF_NETDEV_EGRESS)
413 if ((reg->hooknum != NF_NETDEV_INGRESS &&
414 reg->hooknum != NF_NETDEV_EGRESS) ||
415 !reg->dev || dev_net(reg->dev) != net)
419 if (reg->hooknum != NF_INET_INGRESS)
422 err = nf_ingress_check(net, reg, NF_INET_INGRESS);
428 pp = nf_hook_entry_head(net, pf, reg->hooknum, reg->dev);
432 mutex_lock(&nf_hook_mutex);
434 p = nf_entry_dereference(*pp);
435 new_hooks = nf_hook_entries_grow(p, reg);
437 if (!IS_ERR(new_hooks)) {
438 hooks_validate(new_hooks);
439 rcu_assign_pointer(*pp, new_hooks);
442 mutex_unlock(&nf_hook_mutex);
443 if (IS_ERR(new_hooks))
444 return PTR_ERR(new_hooks);
446 #ifdef CONFIG_NETFILTER_INGRESS
447 if (nf_ingress_hook(reg, pf))
448 net_inc_ingress_queue();
450 #ifdef CONFIG_NETFILTER_EGRESS
451 if (nf_egress_hook(reg, pf))
452 net_inc_egress_queue();
454 nf_static_key_inc(reg, pf);
456 BUG_ON(p == new_hooks);
457 nf_hook_entries_free(p);
462 * nf_remove_net_hook - remove a hook from blob
464 * @oldp: current address of hook blob
465 * @unreg: hook to unregister
467 * This cannot fail, hook unregistration must always succeed.
468 * Therefore replace the to-be-removed hook with a dummy hook.
470 static bool nf_remove_net_hook(struct nf_hook_entries *old,
471 const struct nf_hook_ops *unreg)
473 struct nf_hook_ops **orig_ops;
476 orig_ops = nf_hook_entries_get_hook_ops(old);
477 for (i = 0; i < old->num_hook_entries; i++) {
478 if (orig_ops[i] != unreg)
480 WRITE_ONCE(old->hooks[i].hook, accept_all);
481 WRITE_ONCE(orig_ops[i], (void *)&dummy_ops);
488 static void __nf_unregister_net_hook(struct net *net, int pf,
489 const struct nf_hook_ops *reg)
491 struct nf_hook_entries __rcu **pp;
492 struct nf_hook_entries *p;
494 pp = nf_hook_entry_head(net, pf, reg->hooknum, reg->dev);
498 mutex_lock(&nf_hook_mutex);
500 p = nf_entry_dereference(*pp);
501 if (WARN_ON_ONCE(!p)) {
502 mutex_unlock(&nf_hook_mutex);
506 if (nf_remove_net_hook(p, reg)) {
507 #ifdef CONFIG_NETFILTER_INGRESS
508 if (nf_ingress_hook(reg, pf))
509 net_dec_ingress_queue();
511 #ifdef CONFIG_NETFILTER_EGRESS
512 if (nf_egress_hook(reg, pf))
513 net_dec_egress_queue();
515 nf_static_key_dec(reg, pf);
517 WARN_ONCE(1, "hook not found, pf %d num %d", pf, reg->hooknum);
520 p = __nf_hook_entries_try_shrink(p, pp);
521 mutex_unlock(&nf_hook_mutex);
525 nf_queue_nf_hook_drop(net);
526 nf_hook_entries_free(p);
529 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
531 if (reg->pf == NFPROTO_INET) {
532 if (reg->hooknum == NF_INET_INGRESS) {
533 __nf_unregister_net_hook(net, NFPROTO_INET, reg);
535 __nf_unregister_net_hook(net, NFPROTO_IPV4, reg);
536 __nf_unregister_net_hook(net, NFPROTO_IPV6, reg);
539 __nf_unregister_net_hook(net, reg->pf, reg);
542 EXPORT_SYMBOL(nf_unregister_net_hook);
544 void nf_hook_entries_delete_raw(struct nf_hook_entries __rcu **pp,
545 const struct nf_hook_ops *reg)
547 struct nf_hook_entries *p;
549 p = rcu_dereference_raw(*pp);
550 if (nf_remove_net_hook(p, reg)) {
551 p = __nf_hook_entries_try_shrink(p, pp);
552 nf_hook_entries_free(p);
555 EXPORT_SYMBOL_GPL(nf_hook_entries_delete_raw);
557 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)
561 if (reg->pf == NFPROTO_INET) {
562 if (reg->hooknum == NF_INET_INGRESS) {
563 err = __nf_register_net_hook(net, NFPROTO_INET, reg);
567 err = __nf_register_net_hook(net, NFPROTO_IPV4, reg);
571 err = __nf_register_net_hook(net, NFPROTO_IPV6, reg);
573 __nf_unregister_net_hook(net, NFPROTO_IPV4, reg);
578 err = __nf_register_net_hook(net, reg->pf, reg);
585 EXPORT_SYMBOL(nf_register_net_hook);
587 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
593 for (i = 0; i < n; i++) {
594 err = nf_register_net_hook(net, ®[i]);
602 nf_unregister_net_hooks(net, reg, i);
605 EXPORT_SYMBOL(nf_register_net_hooks);
607 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
608 unsigned int hookcount)
612 for (i = 0; i < hookcount; i++)
613 nf_unregister_net_hook(net, ®[i]);
615 EXPORT_SYMBOL(nf_unregister_net_hooks);
617 /* Returns 1 if okfn() needs to be executed by the caller,
618 * -EPERM for NF_DROP, 0 otherwise. Caller must hold rcu_read_lock. */
619 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
620 const struct nf_hook_entries *e, unsigned int s)
622 unsigned int verdict;
625 for (; s < e->num_hook_entries; s++) {
626 verdict = nf_hook_entry_hookfn(&e->hooks[s], skb, state);
627 switch (verdict & NF_VERDICT_MASK) {
631 kfree_skb_reason(skb,
632 SKB_DROP_REASON_NETFILTER_DROP);
633 ret = NF_DROP_GETERR(verdict);
638 ret = nf_queue(skb, state, s, verdict);
643 return NF_DROP_GETERR(verdict);
652 EXPORT_SYMBOL(nf_hook_slow);
654 void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state,
655 const struct nf_hook_entries *e)
657 struct sk_buff *skb, *next;
658 struct list_head sublist;
661 INIT_LIST_HEAD(&sublist);
663 list_for_each_entry_safe(skb, next, head, list) {
664 skb_list_del_init(skb);
665 ret = nf_hook_slow(skb, state, e, 0);
667 list_add_tail(&skb->list, &sublist);
669 /* Put passed packets back on main list */
670 list_splice(&sublist, head);
672 EXPORT_SYMBOL(nf_hook_slow_list);
674 /* This needs to be compiled in any case to avoid dependencies between the
675 * nfnetlink_queue code and nf_conntrack.
677 const struct nfnl_ct_hook __rcu *nfnl_ct_hook __read_mostly;
678 EXPORT_SYMBOL_GPL(nfnl_ct_hook);
680 const struct nf_ct_hook __rcu *nf_ct_hook __read_mostly;
681 EXPORT_SYMBOL_GPL(nf_ct_hook);
683 const struct nf_defrag_hook __rcu *nf_defrag_v4_hook __read_mostly;
684 EXPORT_SYMBOL_GPL(nf_defrag_v4_hook);
686 const struct nf_defrag_hook __rcu *nf_defrag_v6_hook __read_mostly;
687 EXPORT_SYMBOL_GPL(nf_defrag_v6_hook);
689 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
690 u8 nf_ctnetlink_has_listener;
691 EXPORT_SYMBOL_GPL(nf_ctnetlink_has_listener);
693 const struct nf_nat_hook __rcu *nf_nat_hook __read_mostly;
694 EXPORT_SYMBOL_GPL(nf_nat_hook);
696 /* This does not belong here, but locally generated errors need it if connection
697 * tracking in use: without this, connection may not be in hash table, and hence
698 * manufactured ICMP or RST packets will not be associated with it.
700 void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb)
702 const struct nf_ct_hook *ct_hook;
706 ct_hook = rcu_dereference(nf_ct_hook);
708 ct_hook->attach(new, skb);
712 EXPORT_SYMBOL(nf_ct_attach);
714 void nf_conntrack_destroy(struct nf_conntrack *nfct)
716 const struct nf_ct_hook *ct_hook;
719 ct_hook = rcu_dereference(nf_ct_hook);
721 ct_hook->destroy(nfct);
726 EXPORT_SYMBOL(nf_conntrack_destroy);
728 void nf_ct_set_closing(struct nf_conntrack *nfct)
730 const struct nf_ct_hook *ct_hook;
736 ct_hook = rcu_dereference(nf_ct_hook);
738 ct_hook->set_closing(nfct);
742 EXPORT_SYMBOL_GPL(nf_ct_set_closing);
744 bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
745 const struct sk_buff *skb)
747 const struct nf_ct_hook *ct_hook;
751 ct_hook = rcu_dereference(nf_ct_hook);
753 ret = ct_hook->get_tuple_skb(dst_tuple, skb);
757 EXPORT_SYMBOL(nf_ct_get_tuple_skb);
759 /* Built-in default zone used e.g. by modules. */
760 const struct nf_conntrack_zone nf_ct_zone_dflt = {
761 .id = NF_CT_DEFAULT_ZONE_ID,
762 .dir = NF_CT_DEFAULT_ZONE_DIR,
764 EXPORT_SYMBOL_GPL(nf_ct_zone_dflt);
765 #endif /* CONFIG_NF_CONNTRACK */
767 static void __net_init
768 __netfilter_net_init(struct nf_hook_entries __rcu **e, int max)
772 for (h = 0; h < max; h++)
773 RCU_INIT_POINTER(e[h], NULL);
776 static int __net_init netfilter_net_init(struct net *net)
778 __netfilter_net_init(net->nf.hooks_ipv4, ARRAY_SIZE(net->nf.hooks_ipv4));
779 __netfilter_net_init(net->nf.hooks_ipv6, ARRAY_SIZE(net->nf.hooks_ipv6));
780 #ifdef CONFIG_NETFILTER_FAMILY_ARP
781 __netfilter_net_init(net->nf.hooks_arp, ARRAY_SIZE(net->nf.hooks_arp));
783 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
784 __netfilter_net_init(net->nf.hooks_bridge, ARRAY_SIZE(net->nf.hooks_bridge));
786 #ifdef CONFIG_PROC_FS
787 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
789 if (!net->nf.proc_netfilter) {
790 if (!net_eq(net, &init_net))
791 pr_err("cannot create netfilter proc entry");
800 static void __net_exit netfilter_net_exit(struct net *net)
802 remove_proc_entry("netfilter", net->proc_net);
805 static struct pernet_operations netfilter_net_ops = {
806 .init = netfilter_net_init,
807 .exit = netfilter_net_exit,
810 int __init netfilter_init(void)
814 ret = register_pernet_subsys(&netfilter_net_ops);
818 ret = netfilter_log_init();
824 unregister_pernet_subsys(&netfilter_net_ops);