1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/module.h>
4 #include <linux/proc_fs.h>
5 #include <linux/skbuff.h>
6 #include <linux/netfilter.h>
7 #include <linux/seq_file.h>
8 #include <linux/rcupdate.h>
9 #include <net/protocol.h>
11 #include "nf_internals.h"
14 * A queue handler may be registered for each protocol. Each is protected by
15 * long term mutex. The handler must provide an an outfn() to accept packets
16 * for queueing and must reinject all packets it receives, no matter what.
18 static struct nf_queue_handler
*queue_handler
[NPROTO
];
20 static DEFINE_RWLOCK(queue_handler_lock
);
22 /* return EBUSY when somebody else is registered, return EEXIST if the
23 * same handler is registered, return 0 in case of success. */
24 int nf_register_queue_handler(int pf
, struct nf_queue_handler
*qh
)
31 write_lock_bh(&queue_handler_lock
);
32 if (queue_handler
[pf
] == qh
)
34 else if (queue_handler
[pf
])
37 queue_handler
[pf
] = qh
;
40 write_unlock_bh(&queue_handler_lock
);
44 EXPORT_SYMBOL(nf_register_queue_handler
);
46 /* The caller must flush their queue before this */
47 int nf_unregister_queue_handler(int pf
)
52 write_lock_bh(&queue_handler_lock
);
53 queue_handler
[pf
] = NULL
;
54 write_unlock_bh(&queue_handler_lock
);
58 EXPORT_SYMBOL(nf_unregister_queue_handler
);
60 void nf_unregister_queue_handlers(struct nf_queue_handler
*qh
)
64 write_lock_bh(&queue_handler_lock
);
65 for (pf
= 0; pf
< NPROTO
; pf
++) {
66 if (queue_handler
[pf
] == qh
)
67 queue_handler
[pf
] = NULL
;
69 write_unlock_bh(&queue_handler_lock
);
71 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers
);
74 * Any packet that leaves via this function must come back
75 * through nf_reinject().
77 int nf_queue(struct sk_buff
**skb
,
78 struct list_head
*elem
,
79 int pf
, unsigned int hook
,
80 struct net_device
*indev
,
81 struct net_device
*outdev
,
82 int (*okfn
)(struct sk_buff
*),
83 unsigned int queuenum
)
87 #ifdef CONFIG_BRIDGE_NETFILTER
88 struct net_device
*physindev
= NULL
;
89 struct net_device
*physoutdev
= NULL
;
91 struct nf_afinfo
*afinfo
;
93 /* QUEUE == DROP if noone is waiting, to be safe. */
94 read_lock(&queue_handler_lock
);
95 if (!queue_handler
[pf
]) {
96 read_unlock(&queue_handler_lock
);
101 afinfo
= nf_get_afinfo(pf
);
103 read_unlock(&queue_handler_lock
);
108 info
= kmalloc(sizeof(*info
) + afinfo
->route_key_size
, GFP_ATOMIC
);
111 printk(KERN_ERR
"OOM queueing packet %p\n",
113 read_unlock(&queue_handler_lock
);
118 *info
= (struct nf_info
) {
119 (struct nf_hook_ops
*)elem
, pf
, hook
, indev
, outdev
, okfn
};
121 /* If it's going away, ignore hook. */
122 if (!try_module_get(info
->elem
->owner
)) {
123 read_unlock(&queue_handler_lock
);
128 /* Bump dev refs so they don't vanish while packet is out */
129 if (indev
) dev_hold(indev
);
130 if (outdev
) dev_hold(outdev
);
132 #ifdef CONFIG_BRIDGE_NETFILTER
133 if ((*skb
)->nf_bridge
) {
134 physindev
= (*skb
)->nf_bridge
->physindev
;
135 if (physindev
) dev_hold(physindev
);
136 physoutdev
= (*skb
)->nf_bridge
->physoutdev
;
137 if (physoutdev
) dev_hold(physoutdev
);
140 afinfo
->saveroute(*skb
, info
);
141 status
= queue_handler
[pf
]->outfn(*skb
, info
, queuenum
,
142 queue_handler
[pf
]->data
);
144 read_unlock(&queue_handler_lock
);
147 /* James M doesn't say fuck enough. */
148 if (indev
) dev_put(indev
);
149 if (outdev
) dev_put(outdev
);
150 #ifdef CONFIG_BRIDGE_NETFILTER
151 if (physindev
) dev_put(physindev
);
152 if (physoutdev
) dev_put(physoutdev
);
154 module_put(info
->elem
->owner
);
164 void nf_reinject(struct sk_buff
*skb
, struct nf_info
*info
,
165 unsigned int verdict
)
167 struct list_head
*elem
= &info
->elem
->list
;
169 struct nf_afinfo
*afinfo
;
173 /* Release those devices we held, or Alexey will kill me. */
174 if (info
->indev
) dev_put(info
->indev
);
175 if (info
->outdev
) dev_put(info
->outdev
);
176 #ifdef CONFIG_BRIDGE_NETFILTER
177 if (skb
->nf_bridge
) {
178 if (skb
->nf_bridge
->physindev
)
179 dev_put(skb
->nf_bridge
->physindev
);
180 if (skb
->nf_bridge
->physoutdev
)
181 dev_put(skb
->nf_bridge
->physoutdev
);
185 /* Drop reference to owner of hook which queued us. */
186 module_put(info
->elem
->owner
);
188 list_for_each_rcu(i
, &nf_hooks
[info
->pf
][info
->hook
]) {
193 if (i
== &nf_hooks
[info
->pf
][info
->hook
]) {
194 /* The module which sent it to userspace is gone. */
195 NFDEBUG("%s: module disappeared, dropping packet.\n",
200 /* Continue traversal iff userspace said ok... */
201 if (verdict
== NF_REPEAT
) {
206 if (verdict
== NF_ACCEPT
) {
207 afinfo
= nf_get_afinfo(info
->pf
);
208 if (!afinfo
|| afinfo
->reroute(&skb
, info
) < 0)
212 if (verdict
== NF_ACCEPT
) {
214 verdict
= nf_iterate(&nf_hooks
[info
->pf
][info
->hook
],
216 info
->indev
, info
->outdev
, &elem
,
217 info
->okfn
, INT_MIN
);
220 switch (verdict
& NF_VERDICT_MASK
) {
227 if (!nf_queue(&skb
, elem
, info
->pf
, info
->hook
,
228 info
->indev
, info
->outdev
, info
->okfn
,
229 verdict
>> NF_VERDICT_BITS
))
239 EXPORT_SYMBOL(nf_reinject
);
241 #ifdef CONFIG_PROC_FS
242 static void *seq_start(struct seq_file
*seq
, loff_t
*pos
)
250 static void *seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
260 static void seq_stop(struct seq_file
*s
, void *v
)
265 static int seq_show(struct seq_file
*s
, void *v
)
269 struct nf_queue_handler
*qh
;
271 read_lock_bh(&queue_handler_lock
);
272 qh
= queue_handler
[*pos
];
274 ret
= seq_printf(s
, "%2lld NONE\n", *pos
);
276 ret
= seq_printf(s
, "%2lld %s\n", *pos
, qh
->name
);
277 read_unlock_bh(&queue_handler_lock
);
282 static struct seq_operations nfqueue_seq_ops
= {
289 static int nfqueue_open(struct inode
*inode
, struct file
*file
)
291 return seq_open(file
, &nfqueue_seq_ops
);
294 static struct file_operations nfqueue_file_ops
= {
295 .owner
= THIS_MODULE
,
296 .open
= nfqueue_open
,
299 .release
= seq_release
,
304 int __init
netfilter_queue_init(void)
306 #ifdef CONFIG_PROC_FS
307 struct proc_dir_entry
*pde
;
309 pde
= create_proc_entry("nf_queue", S_IRUGO
, proc_net_netfilter
);
312 pde
->proc_fops
= &nfqueue_file_ops
;