Merge branch 'next' of git://git.monstr.eu/linux-2.6-microblaze
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / core.c
blobb4e8ff05b3014434242941165babe201822d9770
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
5 * way.
7 * Rusty Russell (C)2000 -- This code is GPL.
8 */
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>
17 #include <linux/if.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>
24 #include <net/sock.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)
35 int err;
37 err = mutex_lock_interruptible(&afinfo_mutex);
38 if (err < 0)
39 return err;
40 RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo);
41 mutex_unlock(&afinfo_mutex);
42 return 0;
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);
51 synchronize_rcu();
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 jump_label_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
60 EXPORT_SYMBOL(nf_hooks_needed);
61 #endif
63 static DEFINE_MUTEX(nf_hook_mutex);
65 int nf_register_hook(struct nf_hook_ops *reg)
67 struct nf_hook_ops *elem;
68 int err;
70 err = mutex_lock_interruptible(&nf_hook_mutex);
71 if (err < 0)
72 return err;
73 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
74 if (reg->priority < elem->priority)
75 break;
77 list_add_rcu(&reg->list, elem->list.prev);
78 mutex_unlock(&nf_hook_mutex);
79 #if defined(CONFIG_JUMP_LABEL)
80 jump_label_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
81 #endif
82 return 0;
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(&reg->list);
90 mutex_unlock(&nf_hook_mutex);
91 #if defined(CONFIG_JUMP_LABEL)
92 jump_label_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
93 #endif
94 synchronize_net();
96 EXPORT_SYMBOL(nf_unregister_hook);
98 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
100 unsigned int i;
101 int err = 0;
103 for (i = 0; i < n; i++) {
104 err = nf_register_hook(&reg[i]);
105 if (err)
106 goto err;
108 return err;
110 err:
111 if (i > 0)
112 nf_unregister_hooks(reg, i);
113 return err;
115 EXPORT_SYMBOL(nf_register_hooks);
117 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
119 while (n-- > 0)
120 nf_unregister_hook(&reg[n]);
122 EXPORT_SYMBOL(nf_unregister_hooks);
124 unsigned int nf_iterate(struct list_head *head,
125 struct sk_buff *skb,
126 unsigned int hook,
127 const struct net_device *indev,
128 const struct net_device *outdev,
129 struct list_head **i,
130 int (*okfn)(struct sk_buff *),
131 int hook_thresh)
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_continue_rcu(*i, head) {
140 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
142 if (hook_thresh > elem->priority)
143 continue;
145 /* Optimization: we don't need to hold module
146 reference here, since function can't sleep. --RR */
147 repeat:
148 verdict = elem->hook(hook, skb, indev, outdev, okfn);
149 if (verdict != NF_ACCEPT) {
150 #ifdef CONFIG_NETFILTER_DEBUG
151 if (unlikely((verdict & NF_VERDICT_MASK)
152 > NF_MAX_VERDICT)) {
153 NFDEBUG("Evil return from %p(%u).\n",
154 elem->hook, hook);
155 continue;
157 #endif
158 if (verdict != NF_REPEAT)
159 return verdict;
160 goto repeat;
163 return NF_ACCEPT;
167 /* Returns 1 if okfn() needs to be executed by the caller,
168 * -EPERM for NF_DROP, 0 otherwise. */
169 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
170 struct net_device *indev,
171 struct net_device *outdev,
172 int (*okfn)(struct sk_buff *),
173 int hook_thresh)
175 struct list_head *elem;
176 unsigned int verdict;
177 int ret = 0;
179 /* We may already have this, but read-locks nest anyway */
180 rcu_read_lock();
182 elem = &nf_hooks[pf][hook];
183 next_hook:
184 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
185 outdev, &elem, okfn, hook_thresh);
186 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
187 ret = 1;
188 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
189 kfree_skb(skb);
190 ret = NF_DROP_GETERR(verdict);
191 if (ret == 0)
192 ret = -EPERM;
193 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
194 int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
195 verdict >> NF_VERDICT_QBITS);
196 if (err < 0) {
197 if (err == -ECANCELED)
198 goto next_hook;
199 if (err == -ESRCH &&
200 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
201 goto next_hook;
202 kfree_skb(skb);
205 rcu_read_unlock();
206 return ret;
208 EXPORT_SYMBOL(nf_hook_slow);
211 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
213 if (writable_len > skb->len)
214 return 0;
216 /* Not exclusive use of packet? Must copy. */
217 if (!skb_cloned(skb)) {
218 if (writable_len <= skb_headlen(skb))
219 return 1;
220 } else if (skb_clone_writable(skb, writable_len))
221 return 1;
223 if (writable_len <= skb_headlen(skb))
224 writable_len = 0;
225 else
226 writable_len -= skb_headlen(skb);
228 return !!__pskb_pull_tail(skb, writable_len);
230 EXPORT_SYMBOL(skb_make_writable);
232 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
233 /* This does not belong here, but locally generated errors need it if connection
234 tracking in use: without this, connection may not be in hash table, and hence
235 manufactured ICMP or RST packets will not be associated with it. */
236 void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu __read_mostly;
237 EXPORT_SYMBOL(ip_ct_attach);
239 void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
241 void (*attach)(struct sk_buff *, struct sk_buff *);
243 if (skb->nfct) {
244 rcu_read_lock();
245 attach = rcu_dereference(ip_ct_attach);
246 if (attach)
247 attach(new, skb);
248 rcu_read_unlock();
251 EXPORT_SYMBOL(nf_ct_attach);
253 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
254 EXPORT_SYMBOL(nf_ct_destroy);
256 void nf_conntrack_destroy(struct nf_conntrack *nfct)
258 void (*destroy)(struct nf_conntrack *);
260 rcu_read_lock();
261 destroy = rcu_dereference(nf_ct_destroy);
262 BUG_ON(destroy == NULL);
263 destroy(nfct);
264 rcu_read_unlock();
266 EXPORT_SYMBOL(nf_conntrack_destroy);
267 #endif /* CONFIG_NF_CONNTRACK */
269 #ifdef CONFIG_PROC_FS
270 struct proc_dir_entry *proc_net_netfilter;
271 EXPORT_SYMBOL(proc_net_netfilter);
272 #endif
274 void __init netfilter_init(void)
276 int i, h;
277 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
278 for (h = 0; h < NF_MAX_HOOKS; h++)
279 INIT_LIST_HEAD(&nf_hooks[i][h]);
282 #ifdef CONFIG_PROC_FS
283 proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net);
284 if (!proc_net_netfilter)
285 panic("cannot create netfilter proc entry");
286 #endif
288 if (netfilter_queue_init() < 0)
289 panic("cannot initialize nf_queue");
290 if (netfilter_log_init() < 0)
291 panic("cannot initialize nf_log");
294 #ifdef CONFIG_SYSCTL
295 struct ctl_path nf_net_netfilter_sysctl_path[] = {
296 { .procname = "net", },
297 { .procname = "netfilter", },
300 EXPORT_SYMBOL_GPL(nf_net_netfilter_sysctl_path);
301 #endif /* CONFIG_SYSCTL */