blob: b1f2ace96f6d2acab781ee38d34671e986f379c3 [file] [log] [blame]
Harald Weltef6ebe772005-08-09 20:21:49 -07001#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>
Harald Weltebbd86b9f2005-08-09 20:23:11 -07007#include <linux/seq_file.h>
Patrick McHardy7a11b982006-02-27 13:03:24 -08008#include <linux/rcupdate.h>
Harald Weltef6ebe772005-08-09 20:21:49 -07009#include <net/protocol.h>
10
11#include "nf_internals.h"
12
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080013/*
Harald Weltef6ebe772005-08-09 20:21:49 -070014 * 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.
17 */
Harald Weltebbd86b9f2005-08-09 20:23:11 -070018static struct nf_queue_handler *queue_handler[NPROTO];
Harald Weltef6ebe772005-08-09 20:21:49 -070019
20static DEFINE_RWLOCK(queue_handler_lock);
21
Harald Welted72367b2005-08-09 20:23:36 -070022/* return EBUSY when somebody else is registered, return EEXIST if the
23 * same handler is registered, return 0 in case of success. */
Harald Weltebbd86b9f2005-08-09 20:23:11 -070024int nf_register_queue_handler(int pf, struct nf_queue_handler *qh)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080025{
Harald Weltef6ebe772005-08-09 20:21:49 -070026 int ret;
27
28 if (pf >= NPROTO)
29 return -EINVAL;
30
31 write_lock_bh(&queue_handler_lock);
Harald Welted72367b2005-08-09 20:23:36 -070032 if (queue_handler[pf] == qh)
33 ret = -EEXIST;
34 else if (queue_handler[pf])
Harald Weltef6ebe772005-08-09 20:21:49 -070035 ret = -EBUSY;
36 else {
Harald Weltebbd86b9f2005-08-09 20:23:11 -070037 queue_handler[pf] = qh;
Harald Weltef6ebe772005-08-09 20:21:49 -070038 ret = 0;
39 }
40 write_unlock_bh(&queue_handler_lock);
41
42 return ret;
43}
44EXPORT_SYMBOL(nf_register_queue_handler);
45
46/* The caller must flush their queue before this */
47int nf_unregister_queue_handler(int pf)
48{
49 if (pf >= NPROTO)
50 return -EINVAL;
51
52 write_lock_bh(&queue_handler_lock);
Harald Weltebbd86b9f2005-08-09 20:23:11 -070053 queue_handler[pf] = NULL;
Harald Weltef6ebe772005-08-09 20:21:49 -070054 write_unlock_bh(&queue_handler_lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080055
Harald Weltef6ebe772005-08-09 20:21:49 -070056 return 0;
57}
58EXPORT_SYMBOL(nf_unregister_queue_handler);
59
Harald Weltebbd86b9f2005-08-09 20:23:11 -070060void nf_unregister_queue_handlers(struct nf_queue_handler *qh)
Harald Weltef6ebe772005-08-09 20:21:49 -070061{
62 int pf;
63
64 write_lock_bh(&queue_handler_lock);
65 for (pf = 0; pf < NPROTO; pf++) {
Harald Weltebbd86b9f2005-08-09 20:23:11 -070066 if (queue_handler[pf] == qh)
67 queue_handler[pf] = NULL;
Harald Weltef6ebe772005-08-09 20:21:49 -070068 }
69 write_unlock_bh(&queue_handler_lock);
70}
71EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
72
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080073/*
74 * Any packet that leaves via this function must come back
Harald Weltef6ebe772005-08-09 20:21:49 -070075 * through nf_reinject().
76 */
Patrick McHardy394f5452006-08-05 00:58:52 -070077static 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)
Harald Weltef6ebe772005-08-09 20:21:49 -070084{
85 int status;
86 struct nf_info *info;
87#ifdef CONFIG_BRIDGE_NETFILTER
88 struct net_device *physindev = NULL;
89 struct net_device *physoutdev = NULL;
90#endif
Patrick McHardybce80322006-04-06 14:18:09 -070091 struct nf_afinfo *afinfo;
Harald Weltef6ebe772005-08-09 20:21:49 -070092
93 /* QUEUE == DROP if noone is waiting, to be safe. */
94 read_lock(&queue_handler_lock);
Patrick McHardye121e9e2006-02-27 13:03:39 -080095 if (!queue_handler[pf]) {
Harald Weltef6ebe772005-08-09 20:21:49 -070096 read_unlock(&queue_handler_lock);
Patrick McHardy394f5452006-08-05 00:58:52 -070097 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -070098 return 1;
99 }
100
Patrick McHardybce80322006-04-06 14:18:09 -0700101 afinfo = nf_get_afinfo(pf);
102 if (!afinfo) {
103 read_unlock(&queue_handler_lock);
Patrick McHardy394f5452006-08-05 00:58:52 -0700104 kfree_skb(skb);
Patrick McHardybce80322006-04-06 14:18:09 -0700105 return 1;
106 }
107
108 info = kmalloc(sizeof(*info) + afinfo->route_key_size, GFP_ATOMIC);
Harald Weltef6ebe772005-08-09 20:21:49 -0700109 if (!info) {
110 if (net_ratelimit())
111 printk(KERN_ERR "OOM queueing packet %p\n",
Patrick McHardy394f5452006-08-05 00:58:52 -0700112 skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700113 read_unlock(&queue_handler_lock);
Patrick McHardy394f5452006-08-05 00:58:52 -0700114 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700115 return 1;
116 }
117
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800118 *info = (struct nf_info) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700119 (struct nf_hook_ops *)elem, pf, hook, indev, outdev, okfn };
120
121 /* If it's going away, ignore hook. */
122 if (!try_module_get(info->elem->owner)) {
123 read_unlock(&queue_handler_lock);
124 kfree(info);
125 return 0;
126 }
127
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);
131
132#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardy394f5452006-08-05 00:58:52 -0700133 if (skb->nf_bridge) {
134 physindev = skb->nf_bridge->physindev;
Harald Weltef6ebe772005-08-09 20:21:49 -0700135 if (physindev) dev_hold(physindev);
Patrick McHardy394f5452006-08-05 00:58:52 -0700136 physoutdev = skb->nf_bridge->physoutdev;
Harald Weltef6ebe772005-08-09 20:21:49 -0700137 if (physoutdev) dev_hold(physoutdev);
138 }
139#endif
Patrick McHardy394f5452006-08-05 00:58:52 -0700140 afinfo->saveroute(skb, info);
141 status = queue_handler[pf]->outfn(skb, info, queuenum,
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700142 queue_handler[pf]->data);
Harald Weltef6ebe772005-08-09 20:21:49 -0700143
Harald Weltef6ebe772005-08-09 20:21:49 -0700144 read_unlock(&queue_handler_lock);
145
146 if (status < 0) {
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);
153#endif
154 module_put(info->elem->owner);
155 kfree(info);
Patrick McHardy394f5452006-08-05 00:58:52 -0700156 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700157
158 return 1;
159 }
160
161 return 1;
162}
163
Patrick McHardy394f5452006-08-05 00:58:52 -0700164int nf_queue(struct sk_buff *skb,
165 struct list_head *elem,
166 int pf, unsigned int hook,
167 struct net_device *indev,
168 struct net_device *outdev,
169 int (*okfn)(struct sk_buff *),
170 unsigned int queuenum)
171{
172 struct sk_buff *segs;
173
174 if (!skb_is_gso(skb))
175 return __nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
176 queuenum);
177
178 switch (pf) {
179 case AF_INET:
180 skb->protocol = htons(ETH_P_IP);
181 break;
182 case AF_INET6:
183 skb->protocol = htons(ETH_P_IPV6);
184 break;
185 }
186
187 segs = skb_gso_segment(skb, 0);
188 kfree_skb(skb);
189 if (unlikely(IS_ERR(segs)))
190 return 1;
191
192 do {
193 struct sk_buff *nskb = segs->next;
194
195 segs->next = NULL;
196 if (!__nf_queue(segs, elem, pf, hook, indev, outdev, okfn,
197 queuenum))
198 kfree_skb(segs);
199 segs = nskb;
200 } while (segs);
201 return 1;
202}
203
Harald Weltef6ebe772005-08-09 20:21:49 -0700204void nf_reinject(struct sk_buff *skb, struct nf_info *info,
205 unsigned int verdict)
206{
207 struct list_head *elem = &info->elem->list;
208 struct list_head *i;
Patrick McHardybce80322006-04-06 14:18:09 -0700209 struct nf_afinfo *afinfo;
Harald Weltef6ebe772005-08-09 20:21:49 -0700210
211 rcu_read_lock();
212
213 /* Release those devices we held, or Alexey will kill me. */
214 if (info->indev) dev_put(info->indev);
215 if (info->outdev) dev_put(info->outdev);
216#ifdef CONFIG_BRIDGE_NETFILTER
217 if (skb->nf_bridge) {
218 if (skb->nf_bridge->physindev)
219 dev_put(skb->nf_bridge->physindev);
220 if (skb->nf_bridge->physoutdev)
221 dev_put(skb->nf_bridge->physoutdev);
222 }
223#endif
224
225 /* Drop reference to owner of hook which queued us. */
226 module_put(info->elem->owner);
227
228 list_for_each_rcu(i, &nf_hooks[info->pf][info->hook]) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800229 if (i == elem)
230 break;
231 }
232
Patrick McHardy45fe4dc2006-02-27 13:03:55 -0800233 if (i == &nf_hooks[info->pf][info->hook]) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700234 /* The module which sent it to userspace is gone. */
235 NFDEBUG("%s: module disappeared, dropping packet.\n",
236 __FUNCTION__);
237 verdict = NF_DROP;
238 }
239
240 /* Continue traversal iff userspace said ok... */
241 if (verdict == NF_REPEAT) {
242 elem = elem->prev;
243 verdict = NF_ACCEPT;
244 }
245
246 if (verdict == NF_ACCEPT) {
Patrick McHardybce80322006-04-06 14:18:09 -0700247 afinfo = nf_get_afinfo(info->pf);
248 if (!afinfo || afinfo->reroute(&skb, info) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800249 verdict = NF_DROP;
250 }
251
252 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700253 next_hook:
254 verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800255 &skb, info->hook,
Harald Weltef6ebe772005-08-09 20:21:49 -0700256 info->indev, info->outdev, &elem,
257 info->okfn, INT_MIN);
258 }
259
260 switch (verdict & NF_VERDICT_MASK) {
261 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700262 case NF_STOP:
Harald Weltef6ebe772005-08-09 20:21:49 -0700263 info->okfn(skb);
Patrick McHardy3bc38712006-07-24 22:52:47 -0700264 case NF_STOLEN:
Harald Weltef6ebe772005-08-09 20:21:49 -0700265 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700266 case NF_QUEUE:
Patrick McHardy394f5452006-08-05 00:58:52 -0700267 if (!__nf_queue(skb, elem, info->pf, info->hook,
268 info->indev, info->outdev, info->okfn,
269 verdict >> NF_VERDICT_BITS))
Harald Weltef6ebe772005-08-09 20:21:49 -0700270 goto next_hook;
271 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700272 default:
273 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700274 }
275 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700276 kfree(info);
277 return;
278}
279EXPORT_SYMBOL(nf_reinject);
280
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700281#ifdef CONFIG_PROC_FS
282static void *seq_start(struct seq_file *seq, loff_t *pos)
283{
284 if (*pos >= NPROTO)
285 return NULL;
286
287 return pos;
288}
289
290static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
291{
292 (*pos)++;
293
294 if (*pos >= NPROTO)
295 return NULL;
296
297 return pos;
298}
299
300static void seq_stop(struct seq_file *s, void *v)
301{
302
303}
304
305static int seq_show(struct seq_file *s, void *v)
306{
307 int ret;
308 loff_t *pos = v;
309 struct nf_queue_handler *qh;
310
311 read_lock_bh(&queue_handler_lock);
312 qh = queue_handler[*pos];
313 if (!qh)
314 ret = seq_printf(s, "%2lld NONE\n", *pos);
315 else
316 ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
317 read_unlock_bh(&queue_handler_lock);
318
319 return ret;
320}
321
322static struct seq_operations nfqueue_seq_ops = {
323 .start = seq_start,
324 .next = seq_next,
325 .stop = seq_stop,
326 .show = seq_show,
327};
328
329static int nfqueue_open(struct inode *inode, struct file *file)
330{
331 return seq_open(file, &nfqueue_seq_ops);
332}
333
Arjan van de Venda7071d2007-02-12 00:55:36 -0800334static const struct file_operations nfqueue_file_ops = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700335 .owner = THIS_MODULE,
336 .open = nfqueue_open,
337 .read = seq_read,
338 .llseek = seq_lseek,
339 .release = seq_release,
340};
341#endif /* PROC_FS */
342
343
Harald Weltef6ebe772005-08-09 20:21:49 -0700344int __init netfilter_queue_init(void)
345{
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700346#ifdef CONFIG_PROC_FS
347 struct proc_dir_entry *pde;
Harald Weltef6ebe772005-08-09 20:21:49 -0700348
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700349 pde = create_proc_entry("nf_queue", S_IRUGO, proc_net_netfilter);
Patrick McHardye02f7d12006-02-27 13:02:52 -0800350 if (!pde)
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700351 return -1;
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700352 pde->proc_fops = &nfqueue_file_ops;
353#endif
Harald Weltef6ebe772005-08-09 20:21:49 -0700354 return 0;
355}
356