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 entry
->orig_ops
= reg
;
109 mutex_lock(&nf_hook_mutex
);
111 /* Find the spot in the list */
112 while ((p
= nf_entry_dereference(*pp
)) != NULL
) {
113 if (reg
->priority
< p
->orig_ops
->priority
)
117 rcu_assign_pointer(entry
->next
, p
);
118 rcu_assign_pointer(*pp
, entry
);
120 mutex_unlock(&nf_hook_mutex
);
121 #ifdef CONFIG_NETFILTER_INGRESS
122 if (reg
->pf
== NFPROTO_NETDEV
&& reg
->hooknum
== NF_NETDEV_INGRESS
)
123 net_inc_ingress_queue();
125 #ifdef HAVE_JUMP_LABEL
126 static_key_slow_inc(&nf_hooks_needed
[reg
->pf
][reg
->hooknum
]);
130 EXPORT_SYMBOL(nf_register_net_hook
);
132 void nf_unregister_net_hook(struct net
*net
, const struct nf_hook_ops
*reg
)
134 struct nf_hook_entry __rcu
**pp
;
135 struct nf_hook_entry
*p
;
137 pp
= nf_hook_entry_head(net
, reg
);
138 if (WARN_ON_ONCE(!pp
))
141 mutex_lock(&nf_hook_mutex
);
142 while ((p
= nf_entry_dereference(*pp
)) != NULL
) {
143 if (p
->orig_ops
== reg
) {
144 rcu_assign_pointer(*pp
, p
->next
);
149 mutex_unlock(&nf_hook_mutex
);
151 WARN(1, "nf_unregister_net_hook: hook not found!\n");
154 #ifdef CONFIG_NETFILTER_INGRESS
155 if (reg
->pf
== NFPROTO_NETDEV
&& reg
->hooknum
== NF_NETDEV_INGRESS
)
156 net_dec_ingress_queue();
158 #ifdef HAVE_JUMP_LABEL
159 static_key_slow_dec(&nf_hooks_needed
[reg
->pf
][reg
->hooknum
]);
162 nf_queue_nf_hook_drop(net
, p
);
163 /* other cpu might still process nfqueue verdict that used reg */
167 EXPORT_SYMBOL(nf_unregister_net_hook
);
169 int nf_register_net_hooks(struct net
*net
, const struct nf_hook_ops
*reg
,
175 for (i
= 0; i
< n
; i
++) {
176 err
= nf_register_net_hook(net
, ®
[i
]);
184 nf_unregister_net_hooks(net
, reg
, i
);
187 EXPORT_SYMBOL(nf_register_net_hooks
);
189 void nf_unregister_net_hooks(struct net
*net
, const struct nf_hook_ops
*reg
,
193 nf_unregister_net_hook(net
, ®
[n
]);
195 EXPORT_SYMBOL(nf_unregister_net_hooks
);
197 static LIST_HEAD(nf_hook_list
);
199 static int _nf_register_hook(struct nf_hook_ops
*reg
)
201 struct net
*net
, *last
;
205 ret
= nf_register_net_hook(net
, reg
);
206 if (ret
&& ret
!= -ENOENT
)
209 list_add_tail(®
->list
, &nf_hook_list
);
217 nf_unregister_net_hook(net
, reg
);
222 int nf_register_hook(struct nf_hook_ops
*reg
)
227 ret
= _nf_register_hook(reg
);
232 EXPORT_SYMBOL(nf_register_hook
);
234 static void _nf_unregister_hook(struct nf_hook_ops
*reg
)
238 list_del(®
->list
);
240 nf_unregister_net_hook(net
, reg
);
243 void nf_unregister_hook(struct nf_hook_ops
*reg
)
246 _nf_unregister_hook(reg
);
249 EXPORT_SYMBOL(nf_unregister_hook
);
251 int nf_register_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
256 for (i
= 0; i
< n
; i
++) {
257 err
= nf_register_hook(®
[i
]);
265 nf_unregister_hooks(reg
, i
);
268 EXPORT_SYMBOL(nf_register_hooks
);
270 /* Caller MUST take rtnl_lock() */
271 int _nf_register_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
276 for (i
= 0; i
< n
; i
++) {
277 err
= _nf_register_hook(®
[i
]);
285 _nf_unregister_hooks(reg
, i
);
288 EXPORT_SYMBOL(_nf_register_hooks
);
290 void nf_unregister_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
293 nf_unregister_hook(®
[n
]);
295 EXPORT_SYMBOL(nf_unregister_hooks
);
297 /* Caller MUST take rtnl_lock */
298 void _nf_unregister_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
301 _nf_unregister_hook(®
[n
]);
303 EXPORT_SYMBOL(_nf_unregister_hooks
);
305 /* Returns 1 if okfn() needs to be executed by the caller,
306 * -EPERM for NF_DROP, 0 otherwise. Caller must hold rcu_read_lock. */
307 int nf_hook_slow(struct sk_buff
*skb
, struct nf_hook_state
*state
,
308 struct nf_hook_entry
*entry
)
310 unsigned int verdict
;
314 verdict
= entry
->ops
.hook(entry
->ops
.priv
, skb
, state
);
315 switch (verdict
& NF_VERDICT_MASK
) {
317 entry
= rcu_dereference(entry
->next
);
321 ret
= NF_DROP_GETERR(verdict
);
326 ret
= nf_queue(skb
, state
, &entry
, verdict
);
327 if (ret
== 1 && entry
)
331 /* Implicit handling for NF_STOLEN, as well as any other
332 * non conventional verdicts.
340 EXPORT_SYMBOL(nf_hook_slow
);
343 int skb_make_writable(struct sk_buff
*skb
, unsigned int writable_len
)
345 if (writable_len
> skb
->len
)
348 /* Not exclusive use of packet? Must copy. */
349 if (!skb_cloned(skb
)) {
350 if (writable_len
<= skb_headlen(skb
))
352 } else if (skb_clone_writable(skb
, writable_len
))
355 if (writable_len
<= skb_headlen(skb
))
358 writable_len
-= skb_headlen(skb
);
360 return !!__pskb_pull_tail(skb
, writable_len
);
362 EXPORT_SYMBOL(skb_make_writable
);
364 /* This needs to be compiled in any case to avoid dependencies between the
365 * nfnetlink_queue code and nf_conntrack.
367 struct nfnl_ct_hook __rcu
*nfnl_ct_hook __read_mostly
;
368 EXPORT_SYMBOL_GPL(nfnl_ct_hook
);
370 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
371 /* This does not belong here, but locally generated errors need it if connection
372 tracking in use: without this, connection may not be in hash table, and hence
373 manufactured ICMP or RST packets will not be associated with it. */
374 void (*ip_ct_attach
)(struct sk_buff
*, const struct sk_buff
*)
376 EXPORT_SYMBOL(ip_ct_attach
);
378 void nf_ct_attach(struct sk_buff
*new, const struct sk_buff
*skb
)
380 void (*attach
)(struct sk_buff
*, const struct sk_buff
*);
384 attach
= rcu_dereference(ip_ct_attach
);
390 EXPORT_SYMBOL(nf_ct_attach
);
392 void (*nf_ct_destroy
)(struct nf_conntrack
*) __rcu __read_mostly
;
393 EXPORT_SYMBOL(nf_ct_destroy
);
395 void nf_conntrack_destroy(struct nf_conntrack
*nfct
)
397 void (*destroy
)(struct nf_conntrack
*);
400 destroy
= rcu_dereference(nf_ct_destroy
);
401 BUG_ON(destroy
== NULL
);
405 EXPORT_SYMBOL(nf_conntrack_destroy
);
407 /* Built-in default zone used e.g. by modules. */
408 const struct nf_conntrack_zone nf_ct_zone_dflt
= {
409 .id
= NF_CT_DEFAULT_ZONE_ID
,
410 .dir
= NF_CT_DEFAULT_ZONE_DIR
,
412 EXPORT_SYMBOL_GPL(nf_ct_zone_dflt
);
413 #endif /* CONFIG_NF_CONNTRACK */
415 #ifdef CONFIG_NF_NAT_NEEDED
416 void (*nf_nat_decode_session_hook
)(struct sk_buff
*, struct flowi
*);
417 EXPORT_SYMBOL(nf_nat_decode_session_hook
);
420 static int nf_register_hook_list(struct net
*net
)
422 struct nf_hook_ops
*elem
;
426 list_for_each_entry(elem
, &nf_hook_list
, list
) {
427 ret
= nf_register_net_hook(net
, elem
);
428 if (ret
&& ret
!= -ENOENT
)
435 list_for_each_entry_continue_reverse(elem
, &nf_hook_list
, list
)
436 nf_unregister_net_hook(net
, elem
);
441 static void nf_unregister_hook_list(struct net
*net
)
443 struct nf_hook_ops
*elem
;
446 list_for_each_entry(elem
, &nf_hook_list
, list
)
447 nf_unregister_net_hook(net
, elem
);
451 static int __net_init
netfilter_net_init(struct net
*net
)
455 for (i
= 0; i
< ARRAY_SIZE(net
->nf
.hooks
); i
++) {
456 for (h
= 0; h
< NF_MAX_HOOKS
; h
++)
457 RCU_INIT_POINTER(net
->nf
.hooks
[i
][h
], NULL
);
460 #ifdef CONFIG_PROC_FS
461 net
->nf
.proc_netfilter
= proc_net_mkdir(net
, "netfilter",
463 if (!net
->nf
.proc_netfilter
) {
464 if (!net_eq(net
, &init_net
))
465 pr_err("cannot create netfilter proc entry");
470 ret
= nf_register_hook_list(net
);
472 remove_proc_entry("netfilter", net
->proc_net
);
477 static void __net_exit
netfilter_net_exit(struct net
*net
)
479 nf_unregister_hook_list(net
);
480 remove_proc_entry("netfilter", net
->proc_net
);
483 static struct pernet_operations netfilter_net_ops
= {
484 .init
= netfilter_net_init
,
485 .exit
= netfilter_net_exit
,
488 int __init
netfilter_init(void)
492 ret
= register_pernet_subsys(&netfilter_net_ops
);
496 ret
= netfilter_log_init();
502 unregister_pernet_subsys(&netfilter_net_ops
);