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/inetdevice.h>
21 #include <linux/proc_fs.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <net/net_namespace.h>
27 #include "nf_internals.h"
29 static DEFINE_MUTEX(afinfo_mutex
);
31 const struct nf_afinfo __rcu
*nf_afinfo
[NFPROTO_NUMPROTO
] __read_mostly
;
32 EXPORT_SYMBOL(nf_afinfo
);
33 const struct nf_ipv6_ops __rcu
*nf_ipv6_ops __read_mostly
;
34 EXPORT_SYMBOL_GPL(nf_ipv6_ops
);
36 int nf_register_afinfo(const struct nf_afinfo
*afinfo
)
38 mutex_lock(&afinfo_mutex
);
39 RCU_INIT_POINTER(nf_afinfo
[afinfo
->family
], afinfo
);
40 mutex_unlock(&afinfo_mutex
);
43 EXPORT_SYMBOL_GPL(nf_register_afinfo
);
45 void nf_unregister_afinfo(const struct nf_afinfo
*afinfo
)
47 mutex_lock(&afinfo_mutex
);
48 RCU_INIT_POINTER(nf_afinfo
[afinfo
->family
], NULL
);
49 mutex_unlock(&afinfo_mutex
);
52 EXPORT_SYMBOL_GPL(nf_unregister_afinfo
);
54 struct list_head nf_hooks
[NFPROTO_NUMPROTO
][NF_MAX_HOOKS
] __read_mostly
;
55 EXPORT_SYMBOL(nf_hooks
);
57 #ifdef HAVE_JUMP_LABEL
58 struct static_key nf_hooks_needed
[NFPROTO_NUMPROTO
][NF_MAX_HOOKS
];
59 EXPORT_SYMBOL(nf_hooks_needed
);
62 static DEFINE_MUTEX(nf_hook_mutex
);
64 int nf_register_hook(struct nf_hook_ops
*reg
)
66 struct nf_hook_ops
*elem
;
68 mutex_lock(&nf_hook_mutex
);
69 list_for_each_entry(elem
, &nf_hooks
[reg
->pf
][reg
->hooknum
], list
) {
70 if (reg
->priority
< elem
->priority
)
73 list_add_rcu(®
->list
, elem
->list
.prev
);
74 mutex_unlock(&nf_hook_mutex
);
75 #ifdef HAVE_JUMP_LABEL
76 static_key_slow_inc(&nf_hooks_needed
[reg
->pf
][reg
->hooknum
]);
80 EXPORT_SYMBOL(nf_register_hook
);
82 void nf_unregister_hook(struct nf_hook_ops
*reg
)
84 mutex_lock(&nf_hook_mutex
);
85 list_del_rcu(®
->list
);
86 mutex_unlock(&nf_hook_mutex
);
87 #ifdef HAVE_JUMP_LABEL
88 static_key_slow_dec(&nf_hooks_needed
[reg
->pf
][reg
->hooknum
]);
92 EXPORT_SYMBOL(nf_unregister_hook
);
94 int nf_register_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
99 for (i
= 0; i
< n
; i
++) {
100 err
= nf_register_hook(®
[i
]);
108 nf_unregister_hooks(reg
, i
);
111 EXPORT_SYMBOL(nf_register_hooks
);
113 void nf_unregister_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
116 nf_unregister_hook(®
[n
]);
118 EXPORT_SYMBOL(nf_unregister_hooks
);
120 unsigned int nf_iterate(struct list_head
*head
,
123 const struct net_device
*indev
,
124 const struct net_device
*outdev
,
125 struct nf_hook_ops
**elemp
,
126 int (*okfn
)(struct sk_buff
*),
129 unsigned int verdict
;
132 * The caller must not block between calls to this
133 * function because of risk of continuing from deleted element.
135 list_for_each_entry_continue_rcu((*elemp
), head
, list
) {
136 if (hook_thresh
> (*elemp
)->priority
)
139 /* Optimization: we don't need to hold module
140 reference here, since function can't sleep. --RR */
142 verdict
= (*elemp
)->hook(*elemp
, skb
, indev
, outdev
, okfn
);
143 if (verdict
!= NF_ACCEPT
) {
144 #ifdef CONFIG_NETFILTER_DEBUG
145 if (unlikely((verdict
& NF_VERDICT_MASK
)
147 NFDEBUG("Evil return from %p(%u).\n",
148 (*elemp
)->hook
, hook
);
152 if (verdict
!= NF_REPEAT
)
161 /* Returns 1 if okfn() needs to be executed by the caller,
162 * -EPERM for NF_DROP, 0 otherwise. */
163 int nf_hook_slow(u_int8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
164 struct net_device
*indev
,
165 struct net_device
*outdev
,
166 int (*okfn
)(struct sk_buff
*),
169 struct nf_hook_ops
*elem
;
170 unsigned int verdict
;
173 /* We may already have this, but read-locks nest anyway */
176 elem
= list_entry_rcu(&nf_hooks
[pf
][hook
], struct nf_hook_ops
, list
);
178 verdict
= nf_iterate(&nf_hooks
[pf
][hook
], skb
, hook
, indev
,
179 outdev
, &elem
, okfn
, hook_thresh
);
180 if (verdict
== NF_ACCEPT
|| verdict
== NF_STOP
) {
182 } else if ((verdict
& NF_VERDICT_MASK
) == NF_DROP
) {
184 ret
= NF_DROP_GETERR(verdict
);
187 } else if ((verdict
& NF_VERDICT_MASK
) == NF_QUEUE
) {
188 int err
= nf_queue(skb
, elem
, pf
, hook
, indev
, outdev
, okfn
,
189 verdict
>> NF_VERDICT_QBITS
);
191 if (err
== -ECANCELED
)
194 (verdict
& NF_VERDICT_FLAG_QUEUE_BYPASS
))
202 EXPORT_SYMBOL(nf_hook_slow
);
205 int skb_make_writable(struct sk_buff
*skb
, unsigned int writable_len
)
207 if (writable_len
> skb
->len
)
210 /* Not exclusive use of packet? Must copy. */
211 if (!skb_cloned(skb
)) {
212 if (writable_len
<= skb_headlen(skb
))
214 } else if (skb_clone_writable(skb
, writable_len
))
217 if (writable_len
<= skb_headlen(skb
))
220 writable_len
-= skb_headlen(skb
);
222 return !!__pskb_pull_tail(skb
, writable_len
);
224 EXPORT_SYMBOL(skb_make_writable
);
226 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
227 /* This does not belong here, but locally generated errors need it if connection
228 tracking in use: without this, connection may not be in hash table, and hence
229 manufactured ICMP or RST packets will not be associated with it. */
230 void (*ip_ct_attach
)(struct sk_buff
*, const struct sk_buff
*)
232 EXPORT_SYMBOL(ip_ct_attach
);
234 void nf_ct_attach(struct sk_buff
*new, const struct sk_buff
*skb
)
236 void (*attach
)(struct sk_buff
*, const struct sk_buff
*);
240 attach
= rcu_dereference(ip_ct_attach
);
246 EXPORT_SYMBOL(nf_ct_attach
);
248 void (*nf_ct_destroy
)(struct nf_conntrack
*) __rcu __read_mostly
;
249 EXPORT_SYMBOL(nf_ct_destroy
);
251 void nf_conntrack_destroy(struct nf_conntrack
*nfct
)
253 void (*destroy
)(struct nf_conntrack
*);
256 destroy
= rcu_dereference(nf_ct_destroy
);
257 BUG_ON(destroy
== NULL
);
261 EXPORT_SYMBOL(nf_conntrack_destroy
);
263 struct nfq_ct_hook __rcu
*nfq_ct_hook __read_mostly
;
264 EXPORT_SYMBOL_GPL(nfq_ct_hook
);
266 struct nfq_ct_nat_hook __rcu
*nfq_ct_nat_hook __read_mostly
;
267 EXPORT_SYMBOL_GPL(nfq_ct_nat_hook
);
269 #endif /* CONFIG_NF_CONNTRACK */
271 #ifdef CONFIG_NF_NAT_NEEDED
272 void (*nf_nat_decode_session_hook
)(struct sk_buff
*, struct flowi
*);
273 EXPORT_SYMBOL(nf_nat_decode_session_hook
);
276 static int __net_init
netfilter_net_init(struct net
*net
)
278 #ifdef CONFIG_PROC_FS
279 net
->nf
.proc_netfilter
= proc_net_mkdir(net
, "netfilter",
281 if (!net
->nf
.proc_netfilter
) {
282 if (!net_eq(net
, &init_net
))
283 pr_err("cannot create netfilter proc entry");
291 static void __net_exit
netfilter_net_exit(struct net
*net
)
293 remove_proc_entry("netfilter", net
->proc_net
);
296 static struct pernet_operations netfilter_net_ops
= {
297 .init
= netfilter_net_init
,
298 .exit
= netfilter_net_exit
,
301 int __init
netfilter_init(void)
305 for (i
= 0; i
< ARRAY_SIZE(nf_hooks
); i
++) {
306 for (h
= 0; h
< NF_MAX_HOOKS
; h
++)
307 INIT_LIST_HEAD(&nf_hooks
[i
][h
]);
310 ret
= register_pernet_subsys(&netfilter_net_ops
);
314 ret
= netfilter_log_init();
320 unregister_pernet_subsys(&netfilter_net_ops
);