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);
34 int nf_register_afinfo(const struct nf_afinfo *afinfo)
38 err = mutex_lock_interruptible(&afinfo_mutex);
41 RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo);
42 mutex_unlock(&afinfo_mutex);
45 EXPORT_SYMBOL_GPL(nf_register_afinfo);
47 void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
49 mutex_lock(&afinfo_mutex);
50 RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL);
51 mutex_unlock(&afinfo_mutex);
54 EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
56 struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;
57 EXPORT_SYMBOL(nf_hooks);
59 #if defined(CONFIG_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);
66 int nf_register_hook(struct nf_hook_ops *reg)
68 struct nf_hook_ops *elem;
71 err = mutex_lock_interruptible(&nf_hook_mutex);
74 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
75 if (reg->priority < elem->priority)
78 list_add_rcu(®->list, elem->list.prev);
79 mutex_unlock(&nf_hook_mutex);
80 #if defined(CONFIG_JUMP_LABEL)
81 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
85 EXPORT_SYMBOL(nf_register_hook);
87 void nf_unregister_hook(struct nf_hook_ops *reg)
89 mutex_lock(&nf_hook_mutex);
90 list_del_rcu(®->list);
91 mutex_unlock(&nf_hook_mutex);
92 #if defined(CONFIG_JUMP_LABEL)
93 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
97 EXPORT_SYMBOL(nf_unregister_hook);
99 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
104 for (i = 0; i < n; i++) {
105 err = nf_register_hook(®[i]);
113 nf_unregister_hooks(reg, i);
116 EXPORT_SYMBOL(nf_register_hooks);
118 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
121 nf_unregister_hook(®[n]);
123 EXPORT_SYMBOL(nf_unregister_hooks);
125 unsigned int nf_iterate(struct list_head *head,
128 const struct net_device *indev,
129 const struct net_device *outdev,
130 struct nf_hook_ops **elemp,
131 int (*okfn)(struct sk_buff *),
134 unsigned int verdict;
137 * The caller must not block between calls to this
138 * function because of risk of continuing from deleted element.
140 list_for_each_entry_continue_rcu((*elemp), head, list) {
141 if (hook_thresh > (*elemp)->priority)
144 /* Optimization: we don't need to hold module
145 reference here, since function can't sleep. --RR */
147 verdict = (*elemp)->hook(hook, skb, indev, outdev, okfn);
148 if (verdict != NF_ACCEPT) {
149 #ifdef CONFIG_NETFILTER_DEBUG
150 if (unlikely((verdict & NF_VERDICT_MASK)
152 NFDEBUG("Evil return from %p(%u).\n",
153 (*elemp)->hook, hook);
157 if (verdict != NF_REPEAT)
166 /* Returns 1 if okfn() needs to be executed by the caller,
167 * -EPERM for NF_DROP, 0 otherwise. */
168 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
169 struct net_device *indev,
170 struct net_device *outdev,
171 int (*okfn)(struct sk_buff *),
174 struct nf_hook_ops *elem;
175 unsigned int verdict;
178 /* We may already have this, but read-locks nest anyway */
181 elem = list_entry_rcu(&nf_hooks[pf][hook], struct nf_hook_ops, list);
183 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
184 outdev, &elem, okfn, hook_thresh);
185 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
187 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
189 ret = NF_DROP_GETERR(verdict);
192 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
193 int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
194 verdict >> NF_VERDICT_QBITS);
196 if (err == -ECANCELED)
199 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
207 EXPORT_SYMBOL(nf_hook_slow);
210 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
212 if (writable_len > skb->len)
215 /* Not exclusive use of packet? Must copy. */
216 if (!skb_cloned(skb)) {
217 if (writable_len <= skb_headlen(skb))
219 } else if (skb_clone_writable(skb, writable_len))
222 if (writable_len <= skb_headlen(skb))
225 writable_len -= skb_headlen(skb);
227 return !!__pskb_pull_tail(skb, writable_len);
229 EXPORT_SYMBOL(skb_make_writable);
231 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
232 /* This does not belong here, but locally generated errors need it if connection
233 tracking in use: without this, connection may not be in hash table, and hence
234 manufactured ICMP or RST packets will not be associated with it. */
235 void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu __read_mostly;
236 EXPORT_SYMBOL(ip_ct_attach);
238 void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
240 void (*attach)(struct sk_buff *, struct sk_buff *);
244 attach = rcu_dereference(ip_ct_attach);
250 EXPORT_SYMBOL(nf_ct_attach);
252 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
253 EXPORT_SYMBOL(nf_ct_destroy);
255 void nf_conntrack_destroy(struct nf_conntrack *nfct)
257 void (*destroy)(struct nf_conntrack *);
260 destroy = rcu_dereference(nf_ct_destroy);
261 BUG_ON(destroy == NULL);
265 EXPORT_SYMBOL(nf_conntrack_destroy);
267 struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly;
268 EXPORT_SYMBOL_GPL(nfq_ct_hook);
270 struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly;
271 EXPORT_SYMBOL_GPL(nfq_ct_nat_hook);
273 #endif /* CONFIG_NF_CONNTRACK */
275 #ifdef CONFIG_NF_NAT_NEEDED
276 void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
277 EXPORT_SYMBOL(nf_nat_decode_session_hook);
280 static int __net_init netfilter_net_init(struct net *net)
282 #ifdef CONFIG_PROC_FS
283 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
285 if (!net->nf.proc_netfilter) {
286 if (!net_eq(net, &init_net))
287 pr_err("cannot create netfilter proc entry");
295 static void __net_exit netfilter_net_exit(struct net *net)
297 remove_proc_entry("netfilter", net->proc_net);
300 static struct pernet_operations netfilter_net_ops = {
301 .init = netfilter_net_init,
302 .exit = netfilter_net_exit,
305 void __init netfilter_init(void)
308 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
309 for (h = 0; h < NF_MAX_HOOKS; h++)
310 INIT_LIST_HEAD(&nf_hooks[i][h]);
313 if (register_pernet_subsys(&netfilter_net_ops) < 0)
314 panic("cannot create netfilter proc entry");
316 if (netfilter_log_init() < 0)
317 panic("cannot initialize nf_log");