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
)
40 err
= mutex_lock_interruptible(&afinfo_mutex
);
43 RCU_INIT_POINTER(nf_afinfo
[afinfo
->family
], afinfo
);
44 mutex_unlock(&afinfo_mutex
);
47 EXPORT_SYMBOL_GPL(nf_register_afinfo
);
49 void nf_unregister_afinfo(const struct nf_afinfo
*afinfo
)
51 mutex_lock(&afinfo_mutex
);
52 RCU_INIT_POINTER(nf_afinfo
[afinfo
->family
], NULL
);
53 mutex_unlock(&afinfo_mutex
);
56 EXPORT_SYMBOL_GPL(nf_unregister_afinfo
);
58 struct list_head nf_hooks
[NFPROTO_NUMPROTO
][NF_MAX_HOOKS
] __read_mostly
;
59 EXPORT_SYMBOL(nf_hooks
);
61 #if defined(CONFIG_JUMP_LABEL)
62 struct static_key nf_hooks_needed
[NFPROTO_NUMPROTO
][NF_MAX_HOOKS
];
63 EXPORT_SYMBOL(nf_hooks_needed
);
66 static DEFINE_MUTEX(nf_hook_mutex
);
68 int nf_register_hook(struct nf_hook_ops
*reg
)
70 struct nf_hook_ops
*elem
;
73 err
= mutex_lock_interruptible(&nf_hook_mutex
);
76 list_for_each_entry(elem
, &nf_hooks
[reg
->pf
][reg
->hooknum
], list
) {
77 if (reg
->priority
< elem
->priority
)
80 list_add_rcu(®
->list
, elem
->list
.prev
);
81 mutex_unlock(&nf_hook_mutex
);
82 #if defined(CONFIG_JUMP_LABEL)
83 static_key_slow_inc(&nf_hooks_needed
[reg
->pf
][reg
->hooknum
]);
87 EXPORT_SYMBOL(nf_register_hook
);
89 void nf_unregister_hook(struct nf_hook_ops
*reg
)
91 mutex_lock(&nf_hook_mutex
);
92 list_del_rcu(®
->list
);
93 mutex_unlock(&nf_hook_mutex
);
94 #if defined(CONFIG_JUMP_LABEL)
95 static_key_slow_dec(&nf_hooks_needed
[reg
->pf
][reg
->hooknum
]);
99 EXPORT_SYMBOL(nf_unregister_hook
);
101 int nf_register_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
106 for (i
= 0; i
< n
; i
++) {
107 err
= nf_register_hook(®
[i
]);
115 nf_unregister_hooks(reg
, i
);
118 EXPORT_SYMBOL(nf_register_hooks
);
120 void nf_unregister_hooks(struct nf_hook_ops
*reg
, unsigned int n
)
123 nf_unregister_hook(®
[n
]);
125 EXPORT_SYMBOL(nf_unregister_hooks
);
127 unsigned int nf_iterate(struct list_head
*head
,
130 const struct net_device
*indev
,
131 const struct net_device
*outdev
,
132 struct nf_hook_ops
**elemp
,
133 int (*okfn
)(struct sk_buff
*),
136 unsigned int verdict
;
139 * The caller must not block between calls to this
140 * function because of risk of continuing from deleted element.
142 list_for_each_entry_continue_rcu((*elemp
), head
, list
) {
143 if (hook_thresh
> (*elemp
)->priority
)
146 /* Optimization: we don't need to hold module
147 reference here, since function can't sleep. --RR */
149 verdict
= (*elemp
)->hook(*elemp
, skb
, indev
, outdev
, okfn
);
150 if (verdict
!= NF_ACCEPT
) {
151 #ifdef CONFIG_NETFILTER_DEBUG
152 if (unlikely((verdict
& NF_VERDICT_MASK
)
154 NFDEBUG("Evil return from %p(%u).\n",
155 (*elemp
)->hook
, hook
);
159 if (verdict
!= NF_REPEAT
)
168 /* Returns 1 if okfn() needs to be executed by the caller,
169 * -EPERM for NF_DROP, 0 otherwise. */
170 int nf_hook_slow(u_int8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
171 struct net_device
*indev
,
172 struct net_device
*outdev
,
173 int (*okfn
)(struct sk_buff
*),
176 struct nf_hook_ops
*elem
;
177 unsigned int verdict
;
180 /* We may already have this, but read-locks nest anyway */
183 elem
= list_entry_rcu(&nf_hooks
[pf
][hook
], struct nf_hook_ops
, list
);
185 verdict
= nf_iterate(&nf_hooks
[pf
][hook
], skb
, hook
, indev
,
186 outdev
, &elem
, okfn
, hook_thresh
);
187 if (verdict
== NF_ACCEPT
|| verdict
== NF_STOP
) {
189 } else if ((verdict
& NF_VERDICT_MASK
) == NF_DROP
) {
191 ret
= NF_DROP_GETERR(verdict
);
194 } else if ((verdict
& NF_VERDICT_MASK
) == NF_QUEUE
) {
195 int err
= nf_queue(skb
, elem
, pf
, hook
, indev
, outdev
, okfn
,
196 verdict
>> NF_VERDICT_QBITS
);
198 if (err
== -ECANCELED
)
201 (verdict
& NF_VERDICT_FLAG_QUEUE_BYPASS
))
209 EXPORT_SYMBOL(nf_hook_slow
);
212 int skb_make_writable(struct sk_buff
*skb
, unsigned int writable_len
)
214 if (writable_len
> skb
->len
)
217 /* Not exclusive use of packet? Must copy. */
218 if (!skb_cloned(skb
)) {
219 if (writable_len
<= skb_headlen(skb
))
221 } else if (skb_clone_writable(skb
, writable_len
))
224 if (writable_len
<= skb_headlen(skb
))
227 writable_len
-= skb_headlen(skb
);
229 return !!__pskb_pull_tail(skb
, writable_len
);
231 EXPORT_SYMBOL(skb_make_writable
);
233 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
234 /* This does not belong here, but locally generated errors need it if connection
235 tracking in use: without this, connection may not be in hash table, and hence
236 manufactured ICMP or RST packets will not be associated with it. */
237 void (*ip_ct_attach
)(struct sk_buff
*, const struct sk_buff
*)
239 EXPORT_SYMBOL(ip_ct_attach
);
241 void nf_ct_attach(struct sk_buff
*new, const struct sk_buff
*skb
)
243 void (*attach
)(struct sk_buff
*, const struct sk_buff
*);
247 attach
= rcu_dereference(ip_ct_attach
);
253 EXPORT_SYMBOL(nf_ct_attach
);
255 void (*nf_ct_destroy
)(struct nf_conntrack
*) __rcu __read_mostly
;
256 EXPORT_SYMBOL(nf_ct_destroy
);
258 void nf_conntrack_destroy(struct nf_conntrack
*nfct
)
260 void (*destroy
)(struct nf_conntrack
*);
263 destroy
= rcu_dereference(nf_ct_destroy
);
264 BUG_ON(destroy
== NULL
);
268 EXPORT_SYMBOL(nf_conntrack_destroy
);
270 struct nfq_ct_hook __rcu
*nfq_ct_hook __read_mostly
;
271 EXPORT_SYMBOL_GPL(nfq_ct_hook
);
273 struct nfq_ct_nat_hook __rcu
*nfq_ct_nat_hook __read_mostly
;
274 EXPORT_SYMBOL_GPL(nfq_ct_nat_hook
);
276 #endif /* CONFIG_NF_CONNTRACK */
278 #ifdef CONFIG_NF_NAT_NEEDED
279 void (*nf_nat_decode_session_hook
)(struct sk_buff
*, struct flowi
*);
280 EXPORT_SYMBOL(nf_nat_decode_session_hook
);
283 static int __net_init
netfilter_net_init(struct net
*net
)
285 #ifdef CONFIG_PROC_FS
286 net
->nf
.proc_netfilter
= proc_net_mkdir(net
, "netfilter",
288 if (!net
->nf
.proc_netfilter
) {
289 if (!net_eq(net
, &init_net
))
290 pr_err("cannot create netfilter proc entry");
298 static void __net_exit
netfilter_net_exit(struct net
*net
)
300 remove_proc_entry("netfilter", net
->proc_net
);
303 static struct pernet_operations netfilter_net_ops
= {
304 .init
= netfilter_net_init
,
305 .exit
= netfilter_net_exit
,
308 int __init
netfilter_init(void)
312 for (i
= 0; i
< ARRAY_SIZE(nf_hooks
); i
++) {
313 for (h
= 0; h
< NF_MAX_HOOKS
; h
++)
314 INIT_LIST_HEAD(&nf_hooks
[i
][h
]);
317 ret
= register_pernet_subsys(&netfilter_net_ops
);
321 ret
= netfilter_log_init();
327 unregister_pernet_subsys(&netfilter_net_ops
);