[PATCH] libertas: remove bss_descriptior->networktsf
[linux-2.6/libata-dev.git] / net / netfilter / core.c
bloba523fa4136ed53a7f13e227c0ee6bb8a38ae95f4
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 <net/net_namespace.h>
23 #include <net/sock.h>
25 #include "nf_internals.h"
27 static DEFINE_MUTEX(afinfo_mutex);
29 struct nf_afinfo *nf_afinfo[NPROTO] __read_mostly;
30 EXPORT_SYMBOL(nf_afinfo);
32 int nf_register_afinfo(struct nf_afinfo *afinfo)
34 int err;
36 err = mutex_lock_interruptible(&afinfo_mutex);
37 if (err < 0)
38 return err;
39 rcu_assign_pointer(nf_afinfo[afinfo->family], afinfo);
40 mutex_unlock(&afinfo_mutex);
41 return 0;
43 EXPORT_SYMBOL_GPL(nf_register_afinfo);
45 void nf_unregister_afinfo(struct nf_afinfo *afinfo)
47 mutex_lock(&afinfo_mutex);
48 rcu_assign_pointer(nf_afinfo[afinfo->family], NULL);
49 mutex_unlock(&afinfo_mutex);
50 synchronize_rcu();
52 EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
54 /* In this code, we can be waiting indefinitely for userspace to
55 * service a packet if a hook returns NF_QUEUE. We could keep a count
56 * of skbuffs queued for userspace, and not deregister a hook unless
57 * this is zero, but that sucks. Now, we simply check when the
58 * packets come back: if the hook is gone, the packet is discarded. */
59 struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS] __read_mostly;
60 EXPORT_SYMBOL(nf_hooks);
61 static DEFINE_MUTEX(nf_hook_mutex);
63 int nf_register_hook(struct nf_hook_ops *reg)
65 struct list_head *i;
66 int err;
68 err = mutex_lock_interruptible(&nf_hook_mutex);
69 if (err < 0)
70 return err;
71 list_for_each(i, &nf_hooks[reg->pf][reg->hooknum]) {
72 if (reg->priority < ((struct nf_hook_ops *)i)->priority)
73 break;
75 list_add_rcu(&reg->list, i->prev);
76 mutex_unlock(&nf_hook_mutex);
77 return 0;
79 EXPORT_SYMBOL(nf_register_hook);
81 void nf_unregister_hook(struct nf_hook_ops *reg)
83 mutex_lock(&nf_hook_mutex);
84 list_del_rcu(&reg->list);
85 mutex_unlock(&nf_hook_mutex);
87 synchronize_net();
89 EXPORT_SYMBOL(nf_unregister_hook);
91 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
93 unsigned int i;
94 int err = 0;
96 for (i = 0; i < n; i++) {
97 err = nf_register_hook(&reg[i]);
98 if (err)
99 goto err;
101 return err;
103 err:
104 if (i > 0)
105 nf_unregister_hooks(reg, i);
106 return err;
108 EXPORT_SYMBOL(nf_register_hooks);
110 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
112 unsigned int i;
114 for (i = 0; i < n; i++)
115 nf_unregister_hook(&reg[i]);
117 EXPORT_SYMBOL(nf_unregister_hooks);
119 unsigned int nf_iterate(struct list_head *head,
120 struct sk_buff **skb,
121 int hook,
122 const struct net_device *indev,
123 const struct net_device *outdev,
124 struct list_head **i,
125 int (*okfn)(struct sk_buff *),
126 int hook_thresh)
128 unsigned int verdict;
131 * The caller must not block between calls to this
132 * function because of risk of continuing from deleted element.
134 list_for_each_continue_rcu(*i, head) {
135 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
137 if (hook_thresh > elem->priority)
138 continue;
140 /* Optimization: we don't need to hold module
141 reference here, since function can't sleep. --RR */
142 verdict = elem->hook(hook, skb, indev, outdev, okfn);
143 if (verdict != NF_ACCEPT) {
144 #ifdef CONFIG_NETFILTER_DEBUG
145 if (unlikely((verdict & NF_VERDICT_MASK)
146 > NF_MAX_VERDICT)) {
147 NFDEBUG("Evil return from %p(%u).\n",
148 elem->hook, hook);
149 continue;
151 #endif
152 if (verdict != NF_REPEAT)
153 return verdict;
154 *i = (*i)->prev;
157 return NF_ACCEPT;
161 /* Returns 1 if okfn() needs to be executed by the caller,
162 * -EPERM for NF_DROP, 0 otherwise. */
163 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
164 struct net_device *indev,
165 struct net_device *outdev,
166 int (*okfn)(struct sk_buff *),
167 int hook_thresh)
169 struct list_head *elem;
170 unsigned int verdict;
171 int ret = 0;
173 /* We may already have this, but read-locks nest anyway */
174 rcu_read_lock();
176 elem = &nf_hooks[pf][hook];
177 next_hook:
178 verdict = nf_iterate(&nf_hooks[pf][hook], pskb, hook, indev,
179 outdev, &elem, okfn, hook_thresh);
180 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
181 ret = 1;
182 goto unlock;
183 } else if (verdict == NF_DROP) {
184 kfree_skb(*pskb);
185 ret = -EPERM;
186 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
187 NFDEBUG("nf_hook: Verdict = QUEUE.\n");
188 if (!nf_queue(*pskb, elem, pf, hook, indev, outdev, okfn,
189 verdict >> NF_VERDICT_BITS))
190 goto next_hook;
192 unlock:
193 rcu_read_unlock();
194 return ret;
196 EXPORT_SYMBOL(nf_hook_slow);
199 int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len)
201 struct sk_buff *nskb;
203 if (writable_len > (*pskb)->len)
204 return 0;
206 /* Not exclusive use of packet? Must copy. */
207 if (skb_cloned(*pskb) && !skb_clone_writable(*pskb, writable_len))
208 goto copy_skb;
209 if (skb_shared(*pskb))
210 goto copy_skb;
212 return pskb_may_pull(*pskb, writable_len);
214 copy_skb:
215 nskb = skb_copy(*pskb, GFP_ATOMIC);
216 if (!nskb)
217 return 0;
218 BUG_ON(skb_is_nonlinear(nskb));
220 /* Rest of kernel will get very unhappy if we pass it a
221 suddenly-orphaned skbuff */
222 if ((*pskb)->sk)
223 skb_set_owner_w(nskb, (*pskb)->sk);
224 kfree_skb(*pskb);
225 *pskb = nskb;
226 return 1;
228 EXPORT_SYMBOL(skb_make_writable);
230 void nf_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
231 __be32 from, __be32 to, int pseudohdr)
233 __be32 diff[] = { ~from, to };
234 if (skb->ip_summed != CHECKSUM_PARTIAL) {
235 *sum = csum_fold(csum_partial(diff, sizeof(diff),
236 ~csum_unfold(*sum)));
237 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
238 skb->csum = ~csum_partial(diff, sizeof(diff),
239 ~skb->csum);
240 } else if (pseudohdr)
241 *sum = ~csum_fold(csum_partial(diff, sizeof(diff),
242 csum_unfold(*sum)));
244 EXPORT_SYMBOL(nf_proto_csum_replace4);
246 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
247 /* This does not belong here, but locally generated errors need it if connection
248 tracking in use: without this, connection may not be in hash table, and hence
249 manufactured ICMP or RST packets will not be associated with it. */
250 void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
251 EXPORT_SYMBOL(ip_ct_attach);
253 void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
255 void (*attach)(struct sk_buff *, struct sk_buff *);
257 if (skb->nfct) {
258 rcu_read_lock();
259 attach = rcu_dereference(ip_ct_attach);
260 if (attach)
261 attach(new, skb);
262 rcu_read_unlock();
265 EXPORT_SYMBOL(nf_ct_attach);
267 void (*nf_ct_destroy)(struct nf_conntrack *);
268 EXPORT_SYMBOL(nf_ct_destroy);
270 void nf_conntrack_destroy(struct nf_conntrack *nfct)
272 void (*destroy)(struct nf_conntrack *);
274 rcu_read_lock();
275 destroy = rcu_dereference(nf_ct_destroy);
276 BUG_ON(destroy == NULL);
277 destroy(nfct);
278 rcu_read_unlock();
280 EXPORT_SYMBOL(nf_conntrack_destroy);
281 #endif /* CONFIG_NF_CONNTRACK */
283 #ifdef CONFIG_PROC_FS
284 struct proc_dir_entry *proc_net_netfilter;
285 EXPORT_SYMBOL(proc_net_netfilter);
286 #endif
288 void __init netfilter_init(void)
290 int i, h;
291 for (i = 0; i < NPROTO; i++) {
292 for (h = 0; h < NF_MAX_HOOKS; h++)
293 INIT_LIST_HEAD(&nf_hooks[i][h]);
296 #ifdef CONFIG_PROC_FS
297 proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net);
298 if (!proc_net_netfilter)
299 panic("cannot create netfilter proc entry");
300 #endif
302 if (netfilter_queue_init() < 0)
303 panic("cannot initialize nf_queue");
304 if (netfilter_log_init() < 0)
305 panic("cannot initialize nf_log");