Linux 2.6.17.7
[linux/fpc-iii.git] / net / netfilter / nf_queue.c
blobee8f70889f47d752b422b4eb01798c9e99266c7a
1 #include <linux/config.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/proc_fs.h>
6 #include <linux/skbuff.h>
7 #include <linux/netfilter.h>
8 #include <linux/seq_file.h>
9 #include <linux/rcupdate.h>
10 #include <net/protocol.h>
12 #include "nf_internals.h"
14 /*
15 * A queue handler may be registered for each protocol. Each is protected by
16 * long term mutex. The handler must provide an an outfn() to accept packets
17 * for queueing and must reinject all packets it receives, no matter what.
19 static struct nf_queue_handler *queue_handler[NPROTO];
21 static DEFINE_RWLOCK(queue_handler_lock);
23 /* return EBUSY when somebody else is registered, return EEXIST if the
24 * same handler is registered, return 0 in case of success. */
25 int nf_register_queue_handler(int pf, struct nf_queue_handler *qh)
27 int ret;
29 if (pf >= NPROTO)
30 return -EINVAL;
32 write_lock_bh(&queue_handler_lock);
33 if (queue_handler[pf] == qh)
34 ret = -EEXIST;
35 else if (queue_handler[pf])
36 ret = -EBUSY;
37 else {
38 queue_handler[pf] = qh;
39 ret = 0;
41 write_unlock_bh(&queue_handler_lock);
43 return ret;
45 EXPORT_SYMBOL(nf_register_queue_handler);
47 /* The caller must flush their queue before this */
48 int nf_unregister_queue_handler(int pf)
50 if (pf >= NPROTO)
51 return -EINVAL;
53 write_lock_bh(&queue_handler_lock);
54 queue_handler[pf] = NULL;
55 write_unlock_bh(&queue_handler_lock);
57 return 0;
59 EXPORT_SYMBOL(nf_unregister_queue_handler);
61 void nf_unregister_queue_handlers(struct nf_queue_handler *qh)
63 int pf;
65 write_lock_bh(&queue_handler_lock);
66 for (pf = 0; pf < NPROTO; pf++) {
67 if (queue_handler[pf] == qh)
68 queue_handler[pf] = NULL;
70 write_unlock_bh(&queue_handler_lock);
72 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
74 /*
75 * Any packet that leaves via this function must come back
76 * through nf_reinject().
78 int nf_queue(struct sk_buff **skb,
79 struct list_head *elem,
80 int pf, unsigned int hook,
81 struct net_device *indev,
82 struct net_device *outdev,
83 int (*okfn)(struct sk_buff *),
84 unsigned int queuenum)
86 int status;
87 struct nf_info *info;
88 #ifdef CONFIG_BRIDGE_NETFILTER
89 struct net_device *physindev = NULL;
90 struct net_device *physoutdev = NULL;
91 #endif
92 struct nf_afinfo *afinfo;
94 /* QUEUE == DROP if noone is waiting, to be safe. */
95 read_lock(&queue_handler_lock);
96 if (!queue_handler[pf]) {
97 read_unlock(&queue_handler_lock);
98 kfree_skb(*skb);
99 return 1;
102 afinfo = nf_get_afinfo(pf);
103 if (!afinfo) {
104 read_unlock(&queue_handler_lock);
105 kfree_skb(*skb);
106 return 1;
109 info = kmalloc(sizeof(*info) + afinfo->route_key_size, GFP_ATOMIC);
110 if (!info) {
111 if (net_ratelimit())
112 printk(KERN_ERR "OOM queueing packet %p\n",
113 *skb);
114 read_unlock(&queue_handler_lock);
115 kfree_skb(*skb);
116 return 1;
119 *info = (struct nf_info) {
120 (struct nf_hook_ops *)elem, pf, hook, indev, outdev, okfn };
122 /* If it's going away, ignore hook. */
123 if (!try_module_get(info->elem->owner)) {
124 read_unlock(&queue_handler_lock);
125 kfree(info);
126 return 0;
129 /* Bump dev refs so they don't vanish while packet is out */
130 if (indev) dev_hold(indev);
131 if (outdev) dev_hold(outdev);
133 #ifdef CONFIG_BRIDGE_NETFILTER
134 if ((*skb)->nf_bridge) {
135 physindev = (*skb)->nf_bridge->physindev;
136 if (physindev) dev_hold(physindev);
137 physoutdev = (*skb)->nf_bridge->physoutdev;
138 if (physoutdev) dev_hold(physoutdev);
140 #endif
141 afinfo->saveroute(*skb, info);
142 status = queue_handler[pf]->outfn(*skb, info, queuenum,
143 queue_handler[pf]->data);
145 read_unlock(&queue_handler_lock);
147 if (status < 0) {
148 /* James M doesn't say fuck enough. */
149 if (indev) dev_put(indev);
150 if (outdev) dev_put(outdev);
151 #ifdef CONFIG_BRIDGE_NETFILTER
152 if (physindev) dev_put(physindev);
153 if (physoutdev) dev_put(physoutdev);
154 #endif
155 module_put(info->elem->owner);
156 kfree(info);
157 kfree_skb(*skb);
159 return 1;
162 return 1;
165 void nf_reinject(struct sk_buff *skb, struct nf_info *info,
166 unsigned int verdict)
168 struct list_head *elem = &info->elem->list;
169 struct list_head *i;
170 struct nf_afinfo *afinfo;
172 rcu_read_lock();
174 /* Release those devices we held, or Alexey will kill me. */
175 if (info->indev) dev_put(info->indev);
176 if (info->outdev) dev_put(info->outdev);
177 #ifdef CONFIG_BRIDGE_NETFILTER
178 if (skb->nf_bridge) {
179 if (skb->nf_bridge->physindev)
180 dev_put(skb->nf_bridge->physindev);
181 if (skb->nf_bridge->physoutdev)
182 dev_put(skb->nf_bridge->physoutdev);
184 #endif
186 /* Drop reference to owner of hook which queued us. */
187 module_put(info->elem->owner);
189 list_for_each_rcu(i, &nf_hooks[info->pf][info->hook]) {
190 if (i == elem)
191 break;
194 if (i == &nf_hooks[info->pf][info->hook]) {
195 /* The module which sent it to userspace is gone. */
196 NFDEBUG("%s: module disappeared, dropping packet.\n",
197 __FUNCTION__);
198 verdict = NF_DROP;
201 /* Continue traversal iff userspace said ok... */
202 if (verdict == NF_REPEAT) {
203 elem = elem->prev;
204 verdict = NF_ACCEPT;
207 if (verdict == NF_ACCEPT) {
208 afinfo = nf_get_afinfo(info->pf);
209 if (!afinfo || afinfo->reroute(&skb, info) < 0)
210 verdict = NF_DROP;
213 if (verdict == NF_ACCEPT) {
214 next_hook:
215 verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
216 &skb, info->hook,
217 info->indev, info->outdev, &elem,
218 info->okfn, INT_MIN);
221 switch (verdict & NF_VERDICT_MASK) {
222 case NF_ACCEPT:
223 info->okfn(skb);
224 break;
226 case NF_QUEUE:
227 if (!nf_queue(&skb, elem, info->pf, info->hook,
228 info->indev, info->outdev, info->okfn,
229 verdict >> NF_VERDICT_BITS))
230 goto next_hook;
231 break;
233 rcu_read_unlock();
235 if (verdict == NF_DROP)
236 kfree_skb(skb);
238 kfree(info);
239 return;
241 EXPORT_SYMBOL(nf_reinject);
243 #ifdef CONFIG_PROC_FS
244 static void *seq_start(struct seq_file *seq, loff_t *pos)
246 if (*pos >= NPROTO)
247 return NULL;
249 return pos;
252 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
254 (*pos)++;
256 if (*pos >= NPROTO)
257 return NULL;
259 return pos;
262 static void seq_stop(struct seq_file *s, void *v)
267 static int seq_show(struct seq_file *s, void *v)
269 int ret;
270 loff_t *pos = v;
271 struct nf_queue_handler *qh;
273 read_lock_bh(&queue_handler_lock);
274 qh = queue_handler[*pos];
275 if (!qh)
276 ret = seq_printf(s, "%2lld NONE\n", *pos);
277 else
278 ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
279 read_unlock_bh(&queue_handler_lock);
281 return ret;
284 static struct seq_operations nfqueue_seq_ops = {
285 .start = seq_start,
286 .next = seq_next,
287 .stop = seq_stop,
288 .show = seq_show,
291 static int nfqueue_open(struct inode *inode, struct file *file)
293 return seq_open(file, &nfqueue_seq_ops);
296 static struct file_operations nfqueue_file_ops = {
297 .owner = THIS_MODULE,
298 .open = nfqueue_open,
299 .read = seq_read,
300 .llseek = seq_lseek,
301 .release = seq_release,
303 #endif /* PROC_FS */
306 int __init netfilter_queue_init(void)
308 #ifdef CONFIG_PROC_FS
309 struct proc_dir_entry *pde;
311 pde = create_proc_entry("nf_queue", S_IRUGO, proc_net_netfilter);
312 if (!pde)
313 return -1;
314 pde->proc_fops = &nfqueue_file_ops;
315 #endif
316 return 0;