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 <net/net_namespace.h>
28 #include "nf_internals.h"
30 static DEFINE_MUTEX(afinfo_mutex);
32 const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
33 EXPORT_SYMBOL(nf_afinfo);
34 const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly;
35 EXPORT_SYMBOL_GPL(nf_ipv6_ops);
37 int nf_register_afinfo(const struct nf_afinfo *afinfo)
39 mutex_lock(&afinfo_mutex);
40 RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo);
41 mutex_unlock(&afinfo_mutex);
44 EXPORT_SYMBOL_GPL(nf_register_afinfo);
46 void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
48 mutex_lock(&afinfo_mutex);
49 RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL);
50 mutex_unlock(&afinfo_mutex);
53 EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
55 struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;
56 EXPORT_SYMBOL(nf_hooks);
58 #ifdef HAVE_JUMP_LABEL
59 struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
60 EXPORT_SYMBOL(nf_hooks_needed);
63 static DEFINE_MUTEX(nf_hook_mutex);
65 int nf_register_hook(struct nf_hook_ops *reg)
67 struct list_head *nf_hook_list;
68 struct nf_hook_ops *elem;
70 mutex_lock(&nf_hook_mutex);
73 #ifdef CONFIG_NETFILTER_INGRESS
74 if (reg->hooknum == NF_NETDEV_INGRESS) {
75 BUG_ON(reg->dev == NULL);
76 nf_hook_list = ®->dev->nf_hooks_ingress;
77 net_inc_ingress_queue();
83 nf_hook_list = &nf_hooks[reg->pf][reg->hooknum];
87 list_for_each_entry(elem, nf_hook_list, list) {
88 if (reg->priority < elem->priority)
91 list_add_rcu(®->list, elem->list.prev);
92 mutex_unlock(&nf_hook_mutex);
93 #ifdef HAVE_JUMP_LABEL
94 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
98 EXPORT_SYMBOL(nf_register_hook);
100 void nf_unregister_hook(struct nf_hook_ops *reg)
102 mutex_lock(&nf_hook_mutex);
103 list_del_rcu(®->list);
104 mutex_unlock(&nf_hook_mutex);
107 #ifdef CONFIG_NETFILTER_INGRESS
108 if (reg->hooknum == NF_NETDEV_INGRESS) {
109 net_dec_ingress_queue();
117 #ifdef HAVE_JUMP_LABEL
118 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
121 nf_queue_nf_hook_drop(reg);
123 EXPORT_SYMBOL(nf_unregister_hook);
125 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
130 for (i = 0; i < n; i++) {
131 err = nf_register_hook(®[i]);
139 nf_unregister_hooks(reg, i);
142 EXPORT_SYMBOL(nf_register_hooks);
144 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
147 nf_unregister_hook(®[n]);
149 EXPORT_SYMBOL(nf_unregister_hooks);
151 unsigned int nf_iterate(struct list_head *head,
153 struct nf_hook_state *state,
154 struct nf_hook_ops **elemp)
156 unsigned int verdict;
159 * The caller must not block between calls to this
160 * function because of risk of continuing from deleted element.
162 list_for_each_entry_continue_rcu((*elemp), head, list) {
163 if (state->thresh > (*elemp)->priority)
166 /* Optimization: we don't need to hold module
167 reference here, since function can't sleep. --RR */
169 verdict = (*elemp)->hook(*elemp, skb, state);
170 if (verdict != NF_ACCEPT) {
171 #ifdef CONFIG_NETFILTER_DEBUG
172 if (unlikely((verdict & NF_VERDICT_MASK)
174 NFDEBUG("Evil return from %p(%u).\n",
175 (*elemp)->hook, state->hook);
179 if (verdict != NF_REPEAT)
188 /* Returns 1 if okfn() needs to be executed by the caller,
189 * -EPERM for NF_DROP, 0 otherwise. */
190 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state)
192 struct nf_hook_ops *elem;
193 unsigned int verdict;
196 /* We may already have this, but read-locks nest anyway */
199 elem = list_entry_rcu(state->hook_list, struct nf_hook_ops, list);
201 verdict = nf_iterate(state->hook_list, skb, state, &elem);
202 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
204 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
206 ret = NF_DROP_GETERR(verdict);
209 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
210 int err = nf_queue(skb, elem, state,
211 verdict >> NF_VERDICT_QBITS);
213 if (err == -ECANCELED)
216 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
224 EXPORT_SYMBOL(nf_hook_slow);
227 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
229 if (writable_len > skb->len)
232 /* Not exclusive use of packet? Must copy. */
233 if (!skb_cloned(skb)) {
234 if (writable_len <= skb_headlen(skb))
236 } else if (skb_clone_writable(skb, writable_len))
239 if (writable_len <= skb_headlen(skb))
242 writable_len -= skb_headlen(skb);
244 return !!__pskb_pull_tail(skb, writable_len);
246 EXPORT_SYMBOL(skb_make_writable);
248 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
249 /* This does not belong here, but locally generated errors need it if connection
250 tracking in use: without this, connection may not be in hash table, and hence
251 manufactured ICMP or RST packets will not be associated with it. */
252 void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *)
254 EXPORT_SYMBOL(ip_ct_attach);
256 void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb)
258 void (*attach)(struct sk_buff *, const struct sk_buff *);
262 attach = rcu_dereference(ip_ct_attach);
268 EXPORT_SYMBOL(nf_ct_attach);
270 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
271 EXPORT_SYMBOL(nf_ct_destroy);
273 void nf_conntrack_destroy(struct nf_conntrack *nfct)
275 void (*destroy)(struct nf_conntrack *);
278 destroy = rcu_dereference(nf_ct_destroy);
279 BUG_ON(destroy == NULL);
283 EXPORT_SYMBOL(nf_conntrack_destroy);
285 struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly;
286 EXPORT_SYMBOL_GPL(nfq_ct_hook);
288 struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly;
289 EXPORT_SYMBOL_GPL(nfq_ct_nat_hook);
291 #endif /* CONFIG_NF_CONNTRACK */
293 #ifdef CONFIG_NF_NAT_NEEDED
294 void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
295 EXPORT_SYMBOL(nf_nat_decode_session_hook);
298 static int __net_init netfilter_net_init(struct net *net)
300 #ifdef CONFIG_PROC_FS
301 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
303 if (!net->nf.proc_netfilter) {
304 if (!net_eq(net, &init_net))
305 pr_err("cannot create netfilter proc entry");
313 static void __net_exit netfilter_net_exit(struct net *net)
315 remove_proc_entry("netfilter", net->proc_net);
318 static struct pernet_operations netfilter_net_ops = {
319 .init = netfilter_net_init,
320 .exit = netfilter_net_exit,
323 int __init netfilter_init(void)
327 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
328 for (h = 0; h < NF_MAX_HOOKS; h++)
329 INIT_LIST_HEAD(&nf_hooks[i][h]);
332 ret = register_pernet_subsys(&netfilter_net_ops);
336 ret = netfilter_log_init();
342 unregister_pernet_subsys(&netfilter_net_ops);