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.
9 #include <linux/kernel.h>
10 #include <linux/netfilter.h>
11 #include <net/protocol.h>
12 #include <linux/init.h>
13 #include <linux/skbuff.h>
14 #include <linux/wait.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
18 #include <linux/netdevice.h>
19 #include <linux/inetdevice.h>
20 #include <linux/proc_fs.h>
21 #include <linux/mutex.h>
22 #include <linux/slab.h>
23 #include <net/net_namespace.h>
26 #include "nf_internals.h"
28 static DEFINE_MUTEX(afinfo_mutex);
30 const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
31 EXPORT_SYMBOL(nf_afinfo);
33 int nf_register_afinfo(const struct nf_afinfo *afinfo)
37 err = mutex_lock_interruptible(&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 #if defined(CONFIG_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 nf_hook_ops *elem;
70 err = mutex_lock_interruptible(&nf_hook_mutex);
73 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
74 if (reg->priority < elem->priority)
77 list_add_rcu(®->list, elem->list.prev);
78 mutex_unlock(&nf_hook_mutex);
79 #if defined(CONFIG_JUMP_LABEL)
80 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
84 EXPORT_SYMBOL(nf_register_hook);
86 void nf_unregister_hook(struct nf_hook_ops *reg)
88 mutex_lock(&nf_hook_mutex);
89 list_del_rcu(®->list);
90 mutex_unlock(&nf_hook_mutex);
91 #if defined(CONFIG_JUMP_LABEL)
92 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
96 EXPORT_SYMBOL(nf_unregister_hook);
98 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
103 for (i = 0; i < n; i++) {
104 err = nf_register_hook(®[i]);
112 nf_unregister_hooks(reg, i);
115 EXPORT_SYMBOL(nf_register_hooks);
117 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
120 nf_unregister_hook(®[n]);
122 EXPORT_SYMBOL(nf_unregister_hooks);
124 unsigned int nf_iterate(struct list_head *head,
127 const struct net_device *indev,
128 const struct net_device *outdev,
129 struct nf_hook_ops **elemp,
130 int (*okfn)(struct sk_buff *),
133 unsigned int verdict;
136 * The caller must not block between calls to this
137 * function because of risk of continuing from deleted element.
139 list_for_each_entry_continue_rcu((*elemp), head, list) {
140 if (hook_thresh > (*elemp)->priority)
143 /* Optimization: we don't need to hold module
144 reference here, since function can't sleep. --RR */
146 verdict = (*elemp)->hook(hook, skb, indev, outdev, okfn);
147 if (verdict != NF_ACCEPT) {
148 #ifdef CONFIG_NETFILTER_DEBUG
149 if (unlikely((verdict & NF_VERDICT_MASK)
151 NFDEBUG("Evil return from %p(%u).\n",
152 (*elemp)->hook, hook);
156 if (verdict != NF_REPEAT)
165 /* Returns 1 if okfn() needs to be executed by the caller,
166 * -EPERM for NF_DROP, 0 otherwise. */
167 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
168 struct net_device *indev,
169 struct net_device *outdev,
170 int (*okfn)(struct sk_buff *),
173 struct nf_hook_ops *elem;
174 unsigned int verdict;
177 /* We may already have this, but read-locks nest anyway */
180 elem = list_entry_rcu(&nf_hooks[pf][hook], struct nf_hook_ops, list);
182 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
183 outdev, &elem, okfn, hook_thresh);
184 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
186 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
188 ret = NF_DROP_GETERR(verdict);
191 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
192 int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
193 verdict >> NF_VERDICT_QBITS);
195 if (err == -ECANCELED)
198 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
206 EXPORT_SYMBOL(nf_hook_slow);
209 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
211 if (writable_len > skb->len)
214 /* Not exclusive use of packet? Must copy. */
215 if (!skb_cloned(skb)) {
216 if (writable_len <= skb_headlen(skb))
218 } else if (skb_clone_writable(skb, writable_len))
221 if (writable_len <= skb_headlen(skb))
224 writable_len -= skb_headlen(skb);
226 return !!__pskb_pull_tail(skb, writable_len);
228 EXPORT_SYMBOL(skb_make_writable);
230 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
231 /* This does not belong here, but locally generated errors need it if connection
232 tracking in use: without this, connection may not be in hash table, and hence
233 manufactured ICMP or RST packets will not be associated with it. */
234 void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu __read_mostly;
235 EXPORT_SYMBOL(ip_ct_attach);
237 void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
239 void (*attach)(struct sk_buff *, struct sk_buff *);
243 attach = rcu_dereference(ip_ct_attach);
249 EXPORT_SYMBOL(nf_ct_attach);
251 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
252 EXPORT_SYMBOL(nf_ct_destroy);
254 void nf_conntrack_destroy(struct nf_conntrack *nfct)
256 void (*destroy)(struct nf_conntrack *);
259 destroy = rcu_dereference(nf_ct_destroy);
260 BUG_ON(destroy == NULL);
264 EXPORT_SYMBOL(nf_conntrack_destroy);
266 struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly;
267 EXPORT_SYMBOL_GPL(nfq_ct_hook);
269 struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly;
270 EXPORT_SYMBOL_GPL(nfq_ct_nat_hook);
272 #endif /* CONFIG_NF_CONNTRACK */
274 #ifdef CONFIG_NF_NAT_NEEDED
275 void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
276 EXPORT_SYMBOL(nf_nat_decode_session_hook);
279 #ifdef CONFIG_PROC_FS
280 struct proc_dir_entry *proc_net_netfilter;
281 EXPORT_SYMBOL(proc_net_netfilter);
284 void __init netfilter_init(void)
287 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
288 for (h = 0; h < NF_MAX_HOOKS; h++)
289 INIT_LIST_HEAD(&nf_hooks[i][h]);
292 #ifdef CONFIG_PROC_FS
293 proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net);
294 if (!proc_net_netfilter)
295 panic("cannot create netfilter proc entry");
298 if (netfilter_log_init() < 0)
299 panic("cannot initialize nf_log");