netfilter: nfnetlink_queue: do not free skb on error
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / core.c
blob0c5b796ef527ea96a71e856de4476cb8e99920bc
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_assign_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_assign_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);
57 static DEFINE_MUTEX(nf_hook_mutex);
59 int nf_register_hook(struct nf_hook_ops *reg)
61 struct nf_hook_ops *elem;
62 int err;
64 err = mutex_lock_interruptible(&nf_hook_mutex);
65 if (err < 0)
66 return err;
67 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
68 if (reg->priority < elem->priority)
69 break;
71 list_add_rcu(&reg->list, elem->list.prev);
72 mutex_unlock(&nf_hook_mutex);
73 return 0;
75 EXPORT_SYMBOL(nf_register_hook);
77 void nf_unregister_hook(struct nf_hook_ops *reg)
79 mutex_lock(&nf_hook_mutex);
80 list_del_rcu(&reg->list);
81 mutex_unlock(&nf_hook_mutex);
83 synchronize_net();
85 EXPORT_SYMBOL(nf_unregister_hook);
87 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
89 unsigned int i;
90 int err = 0;
92 for (i = 0; i < n; i++) {
93 err = nf_register_hook(&reg[i]);
94 if (err)
95 goto err;
97 return err;
99 err:
100 if (i > 0)
101 nf_unregister_hooks(reg, i);
102 return err;
104 EXPORT_SYMBOL(nf_register_hooks);
106 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
108 while (n-- > 0)
109 nf_unregister_hook(&reg[n]);
111 EXPORT_SYMBOL(nf_unregister_hooks);
113 unsigned int nf_iterate(struct list_head *head,
114 struct sk_buff *skb,
115 unsigned int hook,
116 const struct net_device *indev,
117 const struct net_device *outdev,
118 struct list_head **i,
119 int (*okfn)(struct sk_buff *),
120 int hook_thresh)
122 unsigned int verdict;
125 * The caller must not block between calls to this
126 * function because of risk of continuing from deleted element.
128 list_for_each_continue_rcu(*i, head) {
129 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
131 if (hook_thresh > elem->priority)
132 continue;
134 /* Optimization: we don't need to hold module
135 reference here, since function can't sleep. --RR */
136 verdict = elem->hook(hook, skb, indev, outdev, okfn);
137 if (verdict != NF_ACCEPT) {
138 #ifdef CONFIG_NETFILTER_DEBUG
139 if (unlikely((verdict & NF_VERDICT_MASK)
140 > NF_MAX_VERDICT)) {
141 NFDEBUG("Evil return from %p(%u).\n",
142 elem->hook, hook);
143 continue;
145 #endif
146 if (verdict != NF_REPEAT)
147 return verdict;
148 *i = (*i)->prev;
151 return NF_ACCEPT;
155 /* Returns 1 if okfn() needs to be executed by the caller,
156 * -EPERM for NF_DROP, 0 otherwise. */
157 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
158 struct net_device *indev,
159 struct net_device *outdev,
160 int (*okfn)(struct sk_buff *),
161 int hook_thresh)
163 struct list_head *elem;
164 unsigned int verdict;
165 int ret = 0;
167 /* We may already have this, but read-locks nest anyway */
168 rcu_read_lock();
170 elem = &nf_hooks[pf][hook];
171 next_hook:
172 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
173 outdev, &elem, okfn, hook_thresh);
174 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
175 ret = 1;
176 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
177 kfree_skb(skb);
178 ret = -(verdict >> NF_VERDICT_BITS);
179 if (ret == 0)
180 ret = -EPERM;
181 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
182 ret = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
183 verdict >> NF_VERDICT_BITS);
184 if (ret < 0) {
185 if (ret == -ECANCELED)
186 goto next_hook;
187 kfree_skb(skb);
189 ret = 0;
191 rcu_read_unlock();
192 return ret;
194 EXPORT_SYMBOL(nf_hook_slow);
197 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
199 if (writable_len > skb->len)
200 return 0;
202 /* Not exclusive use of packet? Must copy. */
203 if (!skb_cloned(skb)) {
204 if (writable_len <= skb_headlen(skb))
205 return 1;
206 } else if (skb_clone_writable(skb, writable_len))
207 return 1;
209 if (writable_len <= skb_headlen(skb))
210 writable_len = 0;
211 else
212 writable_len -= skb_headlen(skb);
214 return !!__pskb_pull_tail(skb, writable_len);
216 EXPORT_SYMBOL(skb_make_writable);
218 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
219 /* This does not belong here, but locally generated errors need it if connection
220 tracking in use: without this, connection may not be in hash table, and hence
221 manufactured ICMP or RST packets will not be associated with it. */
222 void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu __read_mostly;
223 EXPORT_SYMBOL(ip_ct_attach);
225 void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
227 void (*attach)(struct sk_buff *, struct sk_buff *);
229 if (skb->nfct) {
230 rcu_read_lock();
231 attach = rcu_dereference(ip_ct_attach);
232 if (attach)
233 attach(new, skb);
234 rcu_read_unlock();
237 EXPORT_SYMBOL(nf_ct_attach);
239 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
240 EXPORT_SYMBOL(nf_ct_destroy);
242 void nf_conntrack_destroy(struct nf_conntrack *nfct)
244 void (*destroy)(struct nf_conntrack *);
246 rcu_read_lock();
247 destroy = rcu_dereference(nf_ct_destroy);
248 BUG_ON(destroy == NULL);
249 destroy(nfct);
250 rcu_read_unlock();
252 EXPORT_SYMBOL(nf_conntrack_destroy);
253 #endif /* CONFIG_NF_CONNTRACK */
255 #ifdef CONFIG_PROC_FS
256 struct proc_dir_entry *proc_net_netfilter;
257 EXPORT_SYMBOL(proc_net_netfilter);
258 #endif
260 void __init netfilter_init(void)
262 int i, h;
263 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
264 for (h = 0; h < NF_MAX_HOOKS; h++)
265 INIT_LIST_HEAD(&nf_hooks[i][h]);
268 #ifdef CONFIG_PROC_FS
269 proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net);
270 if (!proc_net_netfilter)
271 panic("cannot create netfilter proc entry");
272 #endif
274 if (netfilter_queue_init() < 0)
275 panic("cannot initialize nf_queue");
276 if (netfilter_log_init() < 0)
277 panic("cannot initialize nf_log");
280 #ifdef CONFIG_SYSCTL
281 struct ctl_path nf_net_netfilter_sysctl_path[] = {
282 { .procname = "net", },
283 { .procname = "netfilter", },
286 EXPORT_SYMBOL_GPL(nf_net_netfilter_sysctl_path);
287 #endif /* CONFIG_SYSCTL */