[NETFILTER]: Remove unnecessary synchronize_net() in nf_register_hook
[linux-2.6.22.y-op.git] / net / netfilter / core.c
bloba66bd6af44fa918e57938df76db41a3b4fd6d4be
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.
9 * February 2000: Modified by James Morris to have 1 queue per protocol.
10 * 15-Mar-2000: Added NF_REPEAT --RR.
11 * 08-May-2003: Internal logging interface added by Jozsef Kadlecsik.
13 #include <linux/kernel.h>
14 #include <linux/netfilter.h>
15 #include <net/protocol.h>
16 #include <linux/init.h>
17 #include <linux/skbuff.h>
18 #include <linux/wait.h>
19 #include <linux/module.h>
20 #include <linux/interrupt.h>
21 #include <linux/if.h>
22 #include <linux/netdevice.h>
23 #include <linux/inetdevice.h>
24 #include <linux/proc_fs.h>
25 #include <net/sock.h>
27 #include "nf_internals.h"
29 static DEFINE_SPINLOCK(afinfo_lock);
31 struct nf_afinfo *nf_afinfo[NPROTO] __read_mostly;
32 EXPORT_SYMBOL(nf_afinfo);
34 int nf_register_afinfo(struct nf_afinfo *afinfo)
36 spin_lock(&afinfo_lock);
37 rcu_assign_pointer(nf_afinfo[afinfo->family], afinfo);
38 spin_unlock(&afinfo_lock);
39 return 0;
41 EXPORT_SYMBOL_GPL(nf_register_afinfo);
43 void nf_unregister_afinfo(struct nf_afinfo *afinfo)
45 spin_lock(&afinfo_lock);
46 rcu_assign_pointer(nf_afinfo[afinfo->family], NULL);
47 spin_unlock(&afinfo_lock);
48 synchronize_rcu();
50 EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
52 /* In this code, we can be waiting indefinitely for userspace to
53 * service a packet if a hook returns NF_QUEUE. We could keep a count
54 * of skbuffs queued for userspace, and not deregister a hook unless
55 * this is zero, but that sucks. Now, we simply check when the
56 * packets come back: if the hook is gone, the packet is discarded. */
57 struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS] __read_mostly;
58 EXPORT_SYMBOL(nf_hooks);
59 static DEFINE_SPINLOCK(nf_hook_lock);
61 int nf_register_hook(struct nf_hook_ops *reg)
63 struct list_head *i;
65 spin_lock_bh(&nf_hook_lock);
66 list_for_each(i, &nf_hooks[reg->pf][reg->hooknum]) {
67 if (reg->priority < ((struct nf_hook_ops *)i)->priority)
68 break;
70 list_add_rcu(&reg->list, i->prev);
71 spin_unlock_bh(&nf_hook_lock);
72 return 0;
74 EXPORT_SYMBOL(nf_register_hook);
76 void nf_unregister_hook(struct nf_hook_ops *reg)
78 spin_lock_bh(&nf_hook_lock);
79 list_del_rcu(&reg->list);
80 spin_unlock_bh(&nf_hook_lock);
82 synchronize_net();
84 EXPORT_SYMBOL(nf_unregister_hook);
86 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
88 unsigned int i;
89 int err = 0;
91 for (i = 0; i < n; i++) {
92 err = nf_register_hook(&reg[i]);
93 if (err)
94 goto err;
96 return err;
98 err:
99 if (i > 0)
100 nf_unregister_hooks(reg, i);
101 return err;
103 EXPORT_SYMBOL(nf_register_hooks);
105 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
107 unsigned int i;
109 for (i = 0; i < n; i++)
110 nf_unregister_hook(&reg[i]);
112 EXPORT_SYMBOL(nf_unregister_hooks);
114 unsigned int nf_iterate(struct list_head *head,
115 struct sk_buff **skb,
116 int hook,
117 const struct net_device *indev,
118 const struct net_device *outdev,
119 struct list_head **i,
120 int (*okfn)(struct sk_buff *),
121 int hook_thresh)
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)
133 continue;
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)
141 > NF_MAX_VERDICT)) {
142 NFDEBUG("Evil return from %p(%u).\n",
143 elem->hook, hook);
144 continue;
146 #endif
147 if (verdict != NF_REPEAT)
148 return verdict;
149 *i = (*i)->prev;
152 return NF_ACCEPT;
156 /* Returns 1 if okfn() needs to be executed by the caller,
157 * -EPERM for NF_DROP, 0 otherwise. */
158 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
159 struct net_device *indev,
160 struct net_device *outdev,
161 int (*okfn)(struct sk_buff *),
162 int hook_thresh)
164 struct list_head *elem;
165 unsigned int verdict;
166 int ret = 0;
168 /* We may already have this, but read-locks nest anyway */
169 rcu_read_lock();
171 elem = &nf_hooks[pf][hook];
172 next_hook:
173 verdict = nf_iterate(&nf_hooks[pf][hook], pskb, hook, indev,
174 outdev, &elem, okfn, hook_thresh);
175 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
176 ret = 1;
177 goto unlock;
178 } else if (verdict == NF_DROP) {
179 kfree_skb(*pskb);
180 ret = -EPERM;
181 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
182 NFDEBUG("nf_hook: Verdict = QUEUE.\n");
183 if (!nf_queue(*pskb, elem, pf, hook, indev, outdev, okfn,
184 verdict >> NF_VERDICT_BITS))
185 goto next_hook;
187 unlock:
188 rcu_read_unlock();
189 return ret;
191 EXPORT_SYMBOL(nf_hook_slow);
194 int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len)
196 struct sk_buff *nskb;
198 if (writable_len > (*pskb)->len)
199 return 0;
201 /* Not exclusive use of packet? Must copy. */
202 if (skb_shared(*pskb) || skb_cloned(*pskb))
203 goto copy_skb;
205 return pskb_may_pull(*pskb, writable_len);
207 copy_skb:
208 nskb = skb_copy(*pskb, GFP_ATOMIC);
209 if (!nskb)
210 return 0;
211 BUG_ON(skb_is_nonlinear(nskb));
213 /* Rest of kernel will get very unhappy if we pass it a
214 suddenly-orphaned skbuff */
215 if ((*pskb)->sk)
216 skb_set_owner_w(nskb, (*pskb)->sk);
217 kfree_skb(*pskb);
218 *pskb = nskb;
219 return 1;
221 EXPORT_SYMBOL(skb_make_writable);
223 void nf_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
224 __be32 from, __be32 to, int pseudohdr)
226 __be32 diff[] = { ~from, to };
227 if (skb->ip_summed != CHECKSUM_PARTIAL) {
228 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
229 ~csum_unfold(*sum)));
230 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
231 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
232 ~skb->csum);
233 } else if (pseudohdr)
234 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
235 csum_unfold(*sum)));
237 EXPORT_SYMBOL(nf_proto_csum_replace4);
239 /* This does not belong here, but locally generated errors need it if connection
240 tracking in use: without this, connection may not be in hash table, and hence
241 manufactured ICMP or RST packets will not be associated with it. */
242 void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
243 EXPORT_SYMBOL(ip_ct_attach);
245 void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
247 void (*attach)(struct sk_buff *, struct sk_buff *);
249 if (skb->nfct) {
250 rcu_read_lock();
251 attach = rcu_dereference(ip_ct_attach);
252 if (attach)
253 attach(new, skb);
254 rcu_read_unlock();
257 EXPORT_SYMBOL(nf_ct_attach);
259 #ifdef CONFIG_PROC_FS
260 struct proc_dir_entry *proc_net_netfilter;
261 EXPORT_SYMBOL(proc_net_netfilter);
262 #endif
264 void __init netfilter_init(void)
266 int i, h;
267 for (i = 0; i < NPROTO; i++) {
268 for (h = 0; h < NF_MAX_HOOKS; h++)
269 INIT_LIST_HEAD(&nf_hooks[i][h]);
272 #ifdef CONFIG_PROC_FS
273 proc_net_netfilter = proc_mkdir("netfilter", proc_net);
274 if (!proc_net_netfilter)
275 panic("cannot create netfilter proc entry");
276 #endif
278 if (netfilter_queue_init() < 0)
279 panic("cannot initialize nf_queue");
280 if (netfilter_log_init() < 0)
281 panic("cannot initialize nf_log");