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 <net/net_namespace.h>
25 #include "nf_internals.h"
27 static DEFINE_MUTEX(afinfo_mutex);
29 const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
30 EXPORT_SYMBOL(nf_afinfo);
32 int nf_register_afinfo(const struct nf_afinfo *afinfo)
36 err = mutex_lock_interruptible(&afinfo_mutex);
39 rcu_assign_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_assign_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);
56 static DEFINE_MUTEX(nf_hook_mutex);
58 int nf_register_hook(struct nf_hook_ops *reg)
60 struct nf_hook_ops *elem;
63 err = mutex_lock_interruptible(&nf_hook_mutex);
66 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
67 if (reg->priority < elem->priority)
70 list_add_rcu(®->list, elem->list.prev);
71 mutex_unlock(&nf_hook_mutex);
74 EXPORT_SYMBOL(nf_register_hook);
76 void nf_unregister_hook(struct nf_hook_ops *reg)
78 mutex_lock(&nf_hook_mutex);
79 list_del_rcu(®->list);
80 mutex_unlock(&nf_hook_mutex);
84 EXPORT_SYMBOL(nf_unregister_hook);
86 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
91 for (i = 0; i < n; i++) {
92 err = nf_register_hook(®[i]);
100 nf_unregister_hooks(reg, i);
103 EXPORT_SYMBOL(nf_register_hooks);
105 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
109 for (i = 0; i < n; i++)
110 nf_unregister_hook(®[i]);
112 EXPORT_SYMBOL(nf_unregister_hooks);
114 unsigned int nf_iterate(struct list_head *head,
117 const struct net_device *indev,
118 const struct net_device *outdev,
119 struct list_head **i,
120 int (*okfn)(struct sk_buff *),
123 unsigned int verdict;
126 * The caller must not block between calls to this
127 * function because of risk of continuing from deleted element.
129 list_for_each_continue_rcu(*i, head) {
130 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
132 if (hook_thresh > elem->priority)
135 /* Optimization: we don't need to hold module
136 reference here, since function can't sleep. --RR */
137 verdict = elem->hook(hook, skb, indev, outdev, okfn);
138 if (verdict != NF_ACCEPT) {
139 #ifdef CONFIG_NETFILTER_DEBUG
140 if (unlikely((verdict & NF_VERDICT_MASK)
142 NFDEBUG("Evil return from %p(%u).\n",
147 if (verdict != NF_REPEAT)
156 /* Returns 1 if okfn() needs to be executed by the caller,
157 * -EPERM for NF_DROP, 0 otherwise. */
158 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
159 struct net_device *indev,
160 struct net_device *outdev,
161 int (*okfn)(struct sk_buff *),
164 struct list_head *elem;
165 unsigned int verdict;
168 /* We may already have this, but read-locks nest anyway */
171 elem = &nf_hooks[pf][hook];
173 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
174 outdev, &elem, okfn, hook_thresh);
175 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
178 } else if (verdict == NF_DROP) {
181 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
182 if (!nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
183 verdict >> NF_VERDICT_BITS))
190 EXPORT_SYMBOL(nf_hook_slow);
193 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
195 if (writable_len > skb->len)
198 /* Not exclusive use of packet? Must copy. */
199 if (!skb_cloned(skb)) {
200 if (writable_len <= skb_headlen(skb))
202 } else if (skb_clone_writable(skb, writable_len))
205 if (writable_len <= skb_headlen(skb))
208 writable_len -= skb_headlen(skb);
210 return !!__pskb_pull_tail(skb, writable_len);
212 EXPORT_SYMBOL(skb_make_writable);
214 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
215 /* This does not belong here, but locally generated errors need it if connection
216 tracking in use: without this, connection may not be in hash table, and hence
217 manufactured ICMP or RST packets will not be associated with it. */
218 void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
219 EXPORT_SYMBOL(ip_ct_attach);
221 void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
223 void (*attach)(struct sk_buff *, struct sk_buff *);
227 attach = rcu_dereference(ip_ct_attach);
233 EXPORT_SYMBOL(nf_ct_attach);
235 void (*nf_ct_destroy)(struct nf_conntrack *);
236 EXPORT_SYMBOL(nf_ct_destroy);
238 void nf_conntrack_destroy(struct nf_conntrack *nfct)
240 void (*destroy)(struct nf_conntrack *);
243 destroy = rcu_dereference(nf_ct_destroy);
244 BUG_ON(destroy == NULL);
248 EXPORT_SYMBOL(nf_conntrack_destroy);
249 #endif /* CONFIG_NF_CONNTRACK */
251 #ifdef CONFIG_PROC_FS
252 struct proc_dir_entry *proc_net_netfilter;
253 EXPORT_SYMBOL(proc_net_netfilter);
256 void __init netfilter_init(void)
259 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
260 for (h = 0; h < NF_MAX_HOOKS; h++)
261 INIT_LIST_HEAD(&nf_hooks[i][h]);
264 #ifdef CONFIG_PROC_FS
265 proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net);
266 if (!proc_net_netfilter)
267 panic("cannot create netfilter proc entry");
270 if (netfilter_queue_init() < 0)
271 panic("cannot initialize nf_queue");
272 if (netfilter_log_init() < 0)
273 panic("cannot initialize nf_log");
277 struct ctl_path nf_net_netfilter_sysctl_path[] = {
278 { .procname = "net", .ctl_name = CTL_NET, },
279 { .procname = "netfilter", .ctl_name = NET_NETFILTER, },
282 EXPORT_SYMBOL_GPL(nf_net_netfilter_sysctl_path);
283 #endif /* CONFIG_SYSCTL */