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 __rcu **nf_hook_entry_head(struct net *net, const struct nf_hook_ops *reg)
70 if (reg->pf != NFPROTO_NETDEV)
71 return net->nf.hooks[reg->pf]+reg->hooknum;
73 #ifdef CONFIG_NETFILTER_INGRESS
74 if (reg->hooknum == NF_NETDEV_INGRESS) {
75 if (reg->dev && dev_net(reg->dev) == net)
76 return ®->dev->nf_hooks_ingress;
82 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)
84 struct nf_hook_entry __rcu **pp;
85 struct nf_hook_entry *entry, *p;
87 if (reg->pf == NFPROTO_NETDEV) {
88 #ifndef CONFIG_NETFILTER_INGRESS
89 if (reg->hooknum == NF_NETDEV_INGRESS)
92 if (reg->hooknum != NF_NETDEV_INGRESS ||
93 !reg->dev || dev_net(reg->dev) != net)
97 pp = nf_hook_entry_head(net, reg);
101 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
105 nf_hook_entry_init(entry, reg);
107 mutex_lock(&nf_hook_mutex);
109 /* Find the spot in the list */
110 for (; (p = nf_entry_dereference(*pp)) != NULL; pp = &p->next) {
111 if (reg->priority < nf_hook_entry_priority(p))
114 rcu_assign_pointer(entry->next, p);
115 rcu_assign_pointer(*pp, entry);
117 mutex_unlock(&nf_hook_mutex);
118 #ifdef CONFIG_NETFILTER_INGRESS
119 if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS)
120 net_inc_ingress_queue();
122 #ifdef HAVE_JUMP_LABEL
123 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
127 EXPORT_SYMBOL(nf_register_net_hook);
129 static struct nf_hook_entry *
130 __nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
132 struct nf_hook_entry __rcu **pp;
133 struct nf_hook_entry *p;
135 pp = nf_hook_entry_head(net, reg);
136 if (WARN_ON_ONCE(!pp))
139 mutex_lock(&nf_hook_mutex);
140 for (; (p = nf_entry_dereference(*pp)) != NULL; pp = &p->next) {
141 if (nf_hook_entry_ops(p) == reg) {
142 rcu_assign_pointer(*pp, p->next);
146 mutex_unlock(&nf_hook_mutex);
148 WARN(1, "nf_unregister_net_hook: hook not found!\n");
151 #ifdef CONFIG_NETFILTER_INGRESS
152 if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS)
153 net_dec_ingress_queue();
155 #ifdef HAVE_JUMP_LABEL
156 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
162 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
164 struct nf_hook_entry *p = __nf_unregister_net_hook(net, reg);
172 /* other cpu might still process nfqueue verdict that used reg */
173 nfq = nf_queue_nf_hook_drop(net);
178 EXPORT_SYMBOL(nf_unregister_net_hook);
180 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
186 for (i = 0; i < n; i++) {
187 err = nf_register_net_hook(net, ®[i]);
195 nf_unregister_net_hooks(net, reg, i);
198 EXPORT_SYMBOL(nf_register_net_hooks);
200 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
201 unsigned int hookcount)
203 struct nf_hook_entry *to_free[16];
204 unsigned int i, n, nfq;
207 n = min_t(unsigned int, hookcount, ARRAY_SIZE(to_free));
209 for (i = 0; i < n; i++)
210 to_free[i] = __nf_unregister_net_hook(net, ®[i]);
214 /* need 2nd synchronize_net() if nfqueue is used, skb
215 * can get reinjected right before nf_queue_hook_drop()
217 nfq = nf_queue_nf_hook_drop(net);
221 for (i = 0; i < n; i++)
226 } while (hookcount > 0);
228 EXPORT_SYMBOL(nf_unregister_net_hooks);
230 /* Returns 1 if okfn() needs to be executed by the caller,
231 * -EPERM for NF_DROP, 0 otherwise. Caller must hold rcu_read_lock. */
232 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
233 struct nf_hook_entry *entry)
235 unsigned int verdict;
239 verdict = nf_hook_entry_hookfn(entry, skb, state);
240 switch (verdict & NF_VERDICT_MASK) {
242 entry = rcu_dereference(entry->next);
246 ret = NF_DROP_GETERR(verdict);
251 ret = nf_queue(skb, state, &entry, verdict);
252 if (ret == 1 && entry)
256 /* Implicit handling for NF_STOLEN, as well as any other
257 * non conventional verdicts.
265 EXPORT_SYMBOL(nf_hook_slow);
268 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
270 if (writable_len > skb->len)
273 /* Not exclusive use of packet? Must copy. */
274 if (!skb_cloned(skb)) {
275 if (writable_len <= skb_headlen(skb))
277 } else if (skb_clone_writable(skb, writable_len))
280 if (writable_len <= skb_headlen(skb))
283 writable_len -= skb_headlen(skb);
285 return !!__pskb_pull_tail(skb, writable_len);
287 EXPORT_SYMBOL(skb_make_writable);
289 /* This needs to be compiled in any case to avoid dependencies between the
290 * nfnetlink_queue code and nf_conntrack.
292 struct nfnl_ct_hook __rcu *nfnl_ct_hook __read_mostly;
293 EXPORT_SYMBOL_GPL(nfnl_ct_hook);
295 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
296 /* This does not belong here, but locally generated errors need it if connection
297 tracking in use: without this, connection may not be in hash table, and hence
298 manufactured ICMP or RST packets will not be associated with it. */
299 void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *)
301 EXPORT_SYMBOL(ip_ct_attach);
303 void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb)
305 void (*attach)(struct sk_buff *, const struct sk_buff *);
309 attach = rcu_dereference(ip_ct_attach);
315 EXPORT_SYMBOL(nf_ct_attach);
317 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
318 EXPORT_SYMBOL(nf_ct_destroy);
320 void nf_conntrack_destroy(struct nf_conntrack *nfct)
322 void (*destroy)(struct nf_conntrack *);
325 destroy = rcu_dereference(nf_ct_destroy);
326 BUG_ON(destroy == NULL);
330 EXPORT_SYMBOL(nf_conntrack_destroy);
332 /* Built-in default zone used e.g. by modules. */
333 const struct nf_conntrack_zone nf_ct_zone_dflt = {
334 .id = NF_CT_DEFAULT_ZONE_ID,
335 .dir = NF_CT_DEFAULT_ZONE_DIR,
337 EXPORT_SYMBOL_GPL(nf_ct_zone_dflt);
338 #endif /* CONFIG_NF_CONNTRACK */
340 #ifdef CONFIG_NF_NAT_NEEDED
341 void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
342 EXPORT_SYMBOL(nf_nat_decode_session_hook);
345 static int __net_init netfilter_net_init(struct net *net)
349 for (i = 0; i < ARRAY_SIZE(net->nf.hooks); i++) {
350 for (h = 0; h < NF_MAX_HOOKS; h++)
351 RCU_INIT_POINTER(net->nf.hooks[i][h], NULL);
354 #ifdef CONFIG_PROC_FS
355 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
357 if (!net->nf.proc_netfilter) {
358 if (!net_eq(net, &init_net))
359 pr_err("cannot create netfilter proc entry");
368 static void __net_exit netfilter_net_exit(struct net *net)
370 remove_proc_entry("netfilter", net->proc_net);
373 static struct pernet_operations netfilter_net_ops = {
374 .init = netfilter_net_init,
375 .exit = netfilter_net_exit,
378 int __init netfilter_init(void)
382 ret = register_pernet_subsys(&netfilter_net_ops);
386 ret = netfilter_log_init();
392 unregister_pernet_subsys(&netfilter_net_ops);