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 static LIST_HEAD(nf_hook_list
);
232 static int _nf_register_hook(struct nf_hook_ops
*reg
)
234 struct net
*net
, *last
;
238 ret
= nf_register_net_hook(net
, reg
);
239 if (ret
&& ret
!= -ENOENT
)
242 list_add_tail(®
->list
, &nf_hook_list
);
250 nf_unregister_net_hook(net
, reg
);
255 int nf_register_hook(struct nf_hook_ops
*reg
)
260 ret
= _nf_register_hook(reg
);
265 EXPORT_SYMBOL(nf_register_hook
);
267 static void _nf_unregister_hook(struct nf_hook_ops
*reg
)
271 list_del(®
->list
);
273 nf_unregister_net_hook(net
, reg
);
276 void nf_unregister_hook(struct nf_hook_ops
*reg
)
279 _nf_unregister_hook(reg
);
282 EXPORT_SYMBOL(nf_unregister_hook
);
284 int nf_register_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
289 for (i
= 0; i
< n
; i
++) {
290 err
= nf_register_hook(®
[i
]);
298 nf_unregister_hooks(reg
, i
);
301 EXPORT_SYMBOL(nf_register_hooks
);
303 /* Caller MUST take rtnl_lock() */
304 int _nf_register_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
309 for (i
= 0; i
< n
; i
++) {
310 err
= _nf_register_hook(®
[i
]);
318 _nf_unregister_hooks(reg
, i
);
321 EXPORT_SYMBOL(_nf_register_hooks
);
323 void nf_unregister_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
326 nf_unregister_hook(®
[n
]);
328 EXPORT_SYMBOL(nf_unregister_hooks
);
330 /* Caller MUST take rtnl_lock */
331 void _nf_unregister_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
334 _nf_unregister_hook(®
[n
]);
336 EXPORT_SYMBOL(_nf_unregister_hooks
);
338 /* Returns 1 if okfn() needs to be executed by the caller,
339 * -EPERM for NF_DROP, 0 otherwise. Caller must hold rcu_read_lock. */
340 int nf_hook_slow(struct sk_buff
*skb
, struct nf_hook_state
*state
,
341 struct nf_hook_entry
*entry
)
343 unsigned int verdict
;
347 verdict
= nf_hook_entry_hookfn(entry
, skb
, state
);
348 switch (verdict
& NF_VERDICT_MASK
) {
350 entry
= rcu_dereference(entry
->next
);
354 ret
= NF_DROP_GETERR(verdict
);
359 ret
= nf_queue(skb
, state
, &entry
, verdict
);
360 if (ret
== 1 && entry
)
364 /* Implicit handling for NF_STOLEN, as well as any other
365 * non conventional verdicts.
373 EXPORT_SYMBOL(nf_hook_slow
);
376 int skb_make_writable(struct sk_buff
*skb
, unsigned int writable_len
)
378 if (writable_len
> skb
->len
)
381 /* Not exclusive use of packet? Must copy. */
382 if (!skb_cloned(skb
)) {
383 if (writable_len
<= skb_headlen(skb
))
385 } else if (skb_clone_writable(skb
, writable_len
))
388 if (writable_len
<= skb_headlen(skb
))
391 writable_len
-= skb_headlen(skb
);
393 return !!__pskb_pull_tail(skb
, writable_len
);
395 EXPORT_SYMBOL(skb_make_writable
);
397 /* This needs to be compiled in any case to avoid dependencies between the
398 * nfnetlink_queue code and nf_conntrack.
400 struct nfnl_ct_hook __rcu
*nfnl_ct_hook __read_mostly
;
401 EXPORT_SYMBOL_GPL(nfnl_ct_hook
);
403 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
404 /* This does not belong here, but locally generated errors need it if connection
405 tracking in use: without this, connection may not be in hash table, and hence
406 manufactured ICMP or RST packets will not be associated with it. */
407 void (*ip_ct_attach
)(struct sk_buff
*, const struct sk_buff
*)
409 EXPORT_SYMBOL(ip_ct_attach
);
411 void nf_ct_attach(struct sk_buff
*new, const struct sk_buff
*skb
)
413 void (*attach
)(struct sk_buff
*, const struct sk_buff
*);
417 attach
= rcu_dereference(ip_ct_attach
);
423 EXPORT_SYMBOL(nf_ct_attach
);
425 void (*nf_ct_destroy
)(struct nf_conntrack
*) __rcu __read_mostly
;
426 EXPORT_SYMBOL(nf_ct_destroy
);
428 void nf_conntrack_destroy(struct nf_conntrack
*nfct
)
430 void (*destroy
)(struct nf_conntrack
*);
433 destroy
= rcu_dereference(nf_ct_destroy
);
434 BUG_ON(destroy
== NULL
);
438 EXPORT_SYMBOL(nf_conntrack_destroy
);
440 /* Built-in default zone used e.g. by modules. */
441 const struct nf_conntrack_zone nf_ct_zone_dflt
= {
442 .id
= NF_CT_DEFAULT_ZONE_ID
,
443 .dir
= NF_CT_DEFAULT_ZONE_DIR
,
445 EXPORT_SYMBOL_GPL(nf_ct_zone_dflt
);
446 #endif /* CONFIG_NF_CONNTRACK */
448 #ifdef CONFIG_NF_NAT_NEEDED
449 void (*nf_nat_decode_session_hook
)(struct sk_buff
*, struct flowi
*);
450 EXPORT_SYMBOL(nf_nat_decode_session_hook
);
453 static int nf_register_hook_list(struct net
*net
)
455 struct nf_hook_ops
*elem
;
459 list_for_each_entry(elem
, &nf_hook_list
, list
) {
460 ret
= nf_register_net_hook(net
, elem
);
461 if (ret
&& ret
!= -ENOENT
)
468 list_for_each_entry_continue_reverse(elem
, &nf_hook_list
, list
)
469 nf_unregister_net_hook(net
, elem
);
474 static void nf_unregister_hook_list(struct net
*net
)
476 struct nf_hook_ops
*elem
;
479 list_for_each_entry(elem
, &nf_hook_list
, list
)
480 nf_unregister_net_hook(net
, elem
);
484 static int __net_init
netfilter_net_init(struct net
*net
)
488 for (i
= 0; i
< ARRAY_SIZE(net
->nf
.hooks
); i
++) {
489 for (h
= 0; h
< NF_MAX_HOOKS
; h
++)
490 RCU_INIT_POINTER(net
->nf
.hooks
[i
][h
], NULL
);
493 #ifdef CONFIG_PROC_FS
494 net
->nf
.proc_netfilter
= proc_net_mkdir(net
, "netfilter",
496 if (!net
->nf
.proc_netfilter
) {
497 if (!net_eq(net
, &init_net
))
498 pr_err("cannot create netfilter proc entry");
503 ret
= nf_register_hook_list(net
);
505 remove_proc_entry("netfilter", net
->proc_net
);
510 static void __net_exit
netfilter_net_exit(struct net
*net
)
512 nf_unregister_hook_list(net
);
513 remove_proc_entry("netfilter", net
->proc_net
);
516 static struct pernet_operations netfilter_net_ops
= {
517 .init
= netfilter_net_init
,
518 .exit
= netfilter_net_exit
,
521 int __init
netfilter_init(void)
525 ret
= register_pernet_subsys(&netfilter_net_ops
);
529 ret
= netfilter_log_init();
535 unregister_pernet_subsys(&netfilter_net_ops
);