linux/generic: add imq patches for linux kernels 2.6.35,36,37. (closes #8211)
[openwrt.git] / target / linux / generic / patches-2.6.37 / 150-netfilter_imq.patch
1 --- /dev/null
2 +++ b/drivers/net/imq.c
3 @@ -0,0 +1,638 @@
4 +/*
5 + *             Pseudo-driver for the intermediate queue device.
6 + *
7 + *             This program is free software; you can redistribute it and/or
8 + *             modify it under the terms of the GNU General Public License
9 + *             as published by the Free Software Foundation; either version
10 + *             2 of the License, or (at your option) any later version.
11 + *
12 + * Authors:    Patrick McHardy, <kaber@trash.net>
13 + *
14 + *            The first version was written by Martin Devera, <devik@cdi.cz>
15 + *
16 + * Credits:    Jan Rafaj <imq2t@cedric.vabo.cz>
17 + *              - Update patch to 2.4.21
18 + *             Sebastian Strollo <sstrollo@nortelnetworks.com>
19 + *              - Fix "Dead-loop on netdevice imq"-issue
20 + *             Marcel Sebek <sebek64@post.cz>
21 + *              - Update to 2.6.2-rc1
22 + *
23 + *            After some time of inactivity there is a group taking care
24 + *            of IMQ again: http://www.linuximq.net
25 + *
26 + *
27 + *            2004/06/30 - New version of IMQ patch to kernels <=2.6.7
28 + *             including the following changes:
29 + *
30 + *            - Correction of ipv6 support "+"s issue (Hasso Tepper)
31 + *            - Correction of imq_init_devs() issue that resulted in
32 + *            kernel OOPS unloading IMQ as module (Norbert Buchmuller)
33 + *            - Addition of functionality to choose number of IMQ devices
34 + *            during kernel config (Andre Correa)
35 + *            - Addition of functionality to choose how IMQ hooks on
36 + *            PRE and POSTROUTING (after or before NAT) (Andre Correa)
37 + *            - Cosmetic corrections (Norbert Buchmuller) (Andre Correa)
38 + *
39 + *
40 + *             2005/12/16 - IMQ versions between 2.6.7 and 2.6.13 were
41 + *             released with almost no problems. 2.6.14-x was released
42 + *             with some important changes: nfcache was removed; After
43 + *             some weeks of trouble we figured out that some IMQ fields
44 + *             in skb were missing in skbuff.c - skb_clone and copy_skb_header.
45 + *             These functions are correctly patched by this new patch version.
46 + *
47 + *             Thanks for all who helped to figure out all the problems with
48 + *             2.6.14.x: Patrick McHardy, Rune Kock, VeNoMouS, Max CtRiX,
49 + *             Kevin Shanahan, Richard Lucassen, Valery Dachev (hopefully
50 + *             I didn't forget anybody). I apologize again for my lack of time.
51 + *
52 + *
53 + *             2008/06/17 - 2.6.25 - Changed imq.c to use qdisc_run() instead 
54 + *             of qdisc_restart() and moved qdisc_run() to tasklet to avoid
55 + *             recursive locking. New initialization routines to fix 'rmmod' not
56 + *             working anymore. Used code from ifb.c. (Jussi Kivilinna)
57 + *
58 + *             2008/08/06 - 2.6.26 - (JK)
59 + *              - Replaced tasklet with 'netif_schedule()'.
60 + *              - Cleaned up and added comments for imq_nf_queue().
61 + *
62 + *             2009/04/12
63 + *              - Add skb_save_cb/skb_restore_cb helper functions for backuping
64 + *                control buffer. This is needed because qdisc-layer on kernels
65 + *                2.6.27 and newer overwrite control buffer. (Jussi Kivilinna)
66 + *              - Add better locking for IMQ device. Hopefully this will solve
67 + *                SMP issues. (Jussi Kivilinna)
68 + *              - Port to 2.6.27
69 + *              - Port to 2.6.28
70 + *              - Port to 2.6.29 + fix rmmod not working
71 + *
72 + *             2009/04/20 - (Jussi Kivilinna)
73 + *              - Use netdevice feature flags to avoid extra packet handling
74 + *                by core networking layer and possibly increase performance.
75 + *
76 + *             2009/09/26 - (Jussi Kivilinna)
77 + *              - Add imq_nf_reinject_lockless to fix deadlock with
78 + *                imq_nf_queue/imq_nf_reinject.
79 + *
80 + *             2009/12/08 - (Jussi Kivilinna)
81 + *              - Port to 2.6.32
82 + *              - Add check for skb->nf_queue_entry==NULL in imq_dev_xmit()
83 + *              - Also add better error checking for skb->nf_queue_entry usage
84 + *
85 + *             2010/02/25 - (Jussi Kivilinna)
86 + *              - Port to 2.6.33
87 + *
88 + *             2010/08/12 - (Jussi Kivilinna)
89 + *              - Port to 2.6.35
90 + *
91 + *            Also, many thanks to pablo Sebastian Greco for making the initial
92 + *            patch and to those who helped the testing.
93 + *
94 + *             More info at: http://www.linuximq.net/ (Andre Correa)
95 + */
96 +
97 +#include <linux/module.h>
98 +#include <linux/kernel.h>
99 +#include <linux/moduleparam.h>
100 +#include <linux/list.h>
101 +#include <linux/skbuff.h>
102 +#include <linux/netdevice.h>
103 +#include <linux/etherdevice.h>
104 +#include <linux/rtnetlink.h>
105 +#include <linux/if_arp.h>
106 +#include <linux/netfilter.h>
107 +#include <linux/netfilter_ipv4.h>
108 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
109 +       #include <linux/netfilter_ipv6.h>
110 +#endif
111 +#include <linux/imq.h>
112 +#include <net/pkt_sched.h>
113 +#include <net/netfilter/nf_queue.h>
114 +
115 +static nf_hookfn imq_nf_hook;
116 +
117 +static struct nf_hook_ops imq_ingress_ipv4 = {
118 +       .hook           = imq_nf_hook,
119 +       .owner          = THIS_MODULE,
120 +       .pf             = PF_INET,
121 +       .hooknum        = NF_INET_PRE_ROUTING,
122 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
123 +       .priority       = NF_IP_PRI_MANGLE + 1
124 +#else
125 +       .priority       = NF_IP_PRI_NAT_DST + 1
126 +#endif
127 +};
128 +
129 +static struct nf_hook_ops imq_egress_ipv4 = {
130 +       .hook           = imq_nf_hook,
131 +       .owner          = THIS_MODULE,
132 +       .pf             = PF_INET,
133 +       .hooknum        = NF_INET_POST_ROUTING,
134 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
135 +       .priority       = NF_IP_PRI_LAST
136 +#else
137 +       .priority       = NF_IP_PRI_NAT_SRC - 1
138 +#endif
139 +};
140 +
141 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
142 +static struct nf_hook_ops imq_ingress_ipv6 = {
143 +       .hook           = imq_nf_hook,
144 +       .owner          = THIS_MODULE,
145 +       .pf             = PF_INET6,
146 +       .hooknum        = NF_INET_PRE_ROUTING,
147 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
148 +       .priority       = NF_IP6_PRI_MANGLE + 1
149 +#else
150 +       .priority       = NF_IP6_PRI_NAT_DST + 1
151 +#endif
152 +};
153 +
154 +static struct nf_hook_ops imq_egress_ipv6 = {
155 +       .hook           = imq_nf_hook,
156 +       .owner          = THIS_MODULE,
157 +       .pf             = PF_INET6,
158 +       .hooknum        = NF_INET_POST_ROUTING,
159 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
160 +       .priority       = NF_IP6_PRI_LAST
161 +#else
162 +       .priority       = NF_IP6_PRI_NAT_SRC - 1
163 +#endif
164 +};
165 +#endif
166 +
167 +#if defined(CONFIG_IMQ_NUM_DEVS)
168 +static unsigned int numdevs = CONFIG_IMQ_NUM_DEVS;
169 +#else
170 +static unsigned int numdevs = IMQ_MAX_DEVS;
171 +#endif
172 +
173 +static DEFINE_SPINLOCK(imq_nf_queue_lock);
174 +
175 +static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
176 +
177 +
178 +static struct net_device_stats *imq_get_stats(struct net_device *dev)
179 +{
180 +       return &dev->stats;
181 +}
182 +
183 +/* called for packets kfree'd in qdiscs at places other than enqueue */
184 +static void imq_skb_destructor(struct sk_buff *skb)
185 +{
186 +       struct nf_queue_entry *entry = skb->nf_queue_entry;
187 +
188 +       skb->nf_queue_entry = NULL;
189 +
190 +       if (entry) {
191 +               nf_queue_entry_release_refs(entry);
192 +               kfree(entry);
193 +       }
194 +
195 +       skb_restore_cb(skb); /* kfree backup */
196 +}
197 +
198 +/* locking not needed when called from imq_nf_queue */
199 +static void imq_nf_reinject_lockless(struct nf_queue_entry *entry,
200 +                                               unsigned int verdict)
201 +{
202 +       int status;
203 +
204 +       if (!entry->next_outfn) {
205 +               nf_reinject(entry, verdict);
206 +               return;
207 +       }
208 +
209 +       status = entry->next_outfn(entry, entry->next_queuenum);
210 +       if (status < 0) {
211 +               nf_queue_entry_release_refs(entry);
212 +               kfree_skb(entry->skb);
213 +               kfree(entry);
214 +       }
215 +}
216 +
217 +static void imq_nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
218 +{
219 +       int status;
220 +
221 +       if (!entry->next_outfn) {
222 +               spin_lock_bh(&imq_nf_queue_lock);
223 +               nf_reinject(entry, verdict);
224 +               spin_unlock_bh(&imq_nf_queue_lock);
225 +               return;
226 +       }
227 +
228 +       rcu_read_lock();
229 +       local_bh_disable();
230 +       status = entry->next_outfn(entry, entry->next_queuenum);
231 +       local_bh_enable();
232 +       if (status < 0) {
233 +               nf_queue_entry_release_refs(entry);
234 +               kfree_skb(entry->skb);
235 +               kfree(entry);
236 +       }
237 +
238 +       rcu_read_unlock();
239 +}
240 +
241 +static netdev_tx_t imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
242 +{
243 +       struct nf_queue_entry *entry = skb->nf_queue_entry;
244 +
245 +       skb->nf_queue_entry = NULL;
246 +       dev->trans_start = jiffies;
247 +
248 +       dev->stats.tx_bytes += skb->len;
249 +       dev->stats.tx_packets++;
250 +
251 +       if (entry == NULL) {
252 +               /* We don't know what is going on here.. packet is queued for
253 +                * imq device, but (probably) not by us.
254 +                *
255 +                * If this packet was not send here by imq_nf_queue(), then
256 +                * skb_save_cb() was not used and skb_free() should not show:
257 +                *   WARNING: IMQ: kfree_skb: skb->cb_next:..
258 +                * and/or
259 +                *   WARNING: IMQ: kfree_skb: skb->nf_queue_entry...
260 +                *
261 +                * However if this message is shown, then IMQ is somehow broken
262 +                * and you should report this to linuximq.net.
263 +                */
264 +
265 +               /* imq_dev_xmit is black hole that eats all packets, report that
266 +                * we eat this packet happily and increase dropped counters.
267 +                */
268 +
269 +               dev->stats.tx_dropped++;
270 +               dev_kfree_skb(skb);
271 +
272 +               return NETDEV_TX_OK;
273 +       }
274 +
275 +       skb_restore_cb(skb); /* restore skb->cb */
276 +
277 +       skb->imq_flags = 0;
278 +       skb->destructor = NULL;
279 +
280 +       imq_nf_reinject(entry, NF_ACCEPT);
281 +
282 +       return NETDEV_TX_OK;
283 +}
284 +
285 +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num)
286 +{
287 +       struct net_device *dev;
288 +       struct sk_buff *skb_orig, *skb, *skb_shared;
289 +       struct Qdisc *q;
290 +       struct netdev_queue *txq;
291 +       int users, index;
292 +       int retval = -EINVAL;
293 +
294 +       index = entry->skb->imq_flags & IMQ_F_IFMASK;
295 +       if (unlikely(index > numdevs - 1)) {
296 +               if (net_ratelimit())
297 +                       printk(KERN_WARNING
298 +                              "IMQ: invalid device specified, highest is %u\n",
299 +                              numdevs - 1);
300 +               retval = -EINVAL;
301 +               goto out;
302 +       }
303 +
304 +       /* check for imq device by index from cache */
305 +       dev = imq_devs_cache[index];
306 +       if (unlikely(!dev)) {
307 +               char buf[8];
308 +
309 +               /* get device by name and cache result */
310 +               snprintf(buf, sizeof(buf), "imq%d", index);
311 +               dev = dev_get_by_name(&init_net, buf);
312 +               if (!dev) {
313 +                       /* not found ?!*/
314 +                       BUG();
315 +                       retval = -ENODEV;
316 +                       goto out;
317 +               }
318 +
319 +               imq_devs_cache[index] = dev;
320 +               dev_put(dev);
321 +       }
322 +
323 +       if (unlikely(!(dev->flags & IFF_UP))) {
324 +               entry->skb->imq_flags = 0;
325 +               imq_nf_reinject_lockless(entry, NF_ACCEPT);
326 +               retval = 0;
327 +               goto out;
328 +       }
329 +       dev->last_rx = jiffies;
330 +
331 +       skb = entry->skb;
332 +       skb_orig = NULL;
333 +
334 +       /* skb has owner? => make clone */
335 +       if (unlikely(skb->destructor)) {
336 +               skb_orig = skb;
337 +               skb = skb_clone(skb, GFP_ATOMIC);
338 +               if (!skb) {
339 +                       retval = -ENOMEM;
340 +                       goto out;
341 +               }
342 +               entry->skb = skb;
343 +       }
344 +
345 +       skb->nf_queue_entry = entry;
346 +
347 +       dev->stats.rx_bytes += skb->len;
348 +       dev->stats.rx_packets++;
349 +
350 +       txq = dev_pick_tx(dev, skb);
351 +
352 +       q = rcu_dereference(txq->qdisc);
353 +       if (unlikely(!q->enqueue))
354 +               goto packet_not_eaten_by_imq_dev;
355 +
356 +       spin_lock_bh(qdisc_lock(q));
357 +
358 +       users = atomic_read(&skb->users);
359 +
360 +       skb_shared = skb_get(skb); /* increase reference count by one */
361 +       skb_save_cb(skb_shared); /* backup skb->cb, as qdisc layer will
362 +                                       overwrite it */
363 +       qdisc_enqueue_root(skb_shared, q); /* might kfree_skb */
364 +
365 +       if (likely(atomic_read(&skb_shared->users) == users + 1)) {
366 +               kfree_skb(skb_shared); /* decrease reference count by one */
367 +
368 +               skb->destructor = &imq_skb_destructor;
369 +
370 +               /* cloned? */
371 +               if (skb_orig)
372 +                       kfree_skb(skb_orig); /* free original */
373 +
374 +               spin_unlock_bh(qdisc_lock(q));
375 +
376 +               /* schedule qdisc dequeue */
377 +               __netif_schedule(q);
378 +
379 +               retval = 0;
380 +               goto out;
381 +       } else {
382 +               skb_restore_cb(skb_shared); /* restore skb->cb */
383 +               skb->nf_queue_entry = NULL;
384 +               /* qdisc dropped packet and decreased skb reference count of
385 +                * skb, so we don't really want to and try refree as that would
386 +                * actually destroy the skb. */
387 +               spin_unlock_bh(qdisc_lock(q));
388 +               goto packet_not_eaten_by_imq_dev;
389 +       }
390 +
391 +packet_not_eaten_by_imq_dev:
392 +       /* cloned? restore original */
393 +       if (skb_orig) {
394 +               kfree_skb(skb);
395 +               entry->skb = skb_orig;
396 +       }
397 +       retval = -1;
398 +out:
399 +       return retval;
400 +}
401 +
402 +static struct nf_queue_handler nfqh = {
403 +       .name  = "imq",
404 +       .outfn = imq_nf_queue,
405 +};
406 +
407 +static unsigned int imq_nf_hook(unsigned int hook, struct sk_buff *pskb,
408 +                               const struct net_device *indev,
409 +                               const struct net_device *outdev,
410 +                               int (*okfn)(struct sk_buff *))
411 +{
412 +       if (pskb->imq_flags & IMQ_F_ENQUEUE)
413 +               return NF_QUEUE;
414 +
415 +       return NF_ACCEPT;
416 +}
417 +
418 +static int imq_close(struct net_device *dev)
419 +{
420 +       netif_stop_queue(dev);
421 +       return 0;
422 +}
423 +
424 +static int imq_open(struct net_device *dev)
425 +{
426 +       netif_start_queue(dev);
427 +       return 0;
428 +}
429 +
430 +static const struct net_device_ops imq_netdev_ops = {
431 +       .ndo_open               = imq_open,
432 +       .ndo_stop               = imq_close,
433 +       .ndo_start_xmit         = imq_dev_xmit,
434 +       .ndo_get_stats          = imq_get_stats,
435 +};
436 +
437 +static void imq_setup(struct net_device *dev)
438 +{
439 +       dev->netdev_ops         = &imq_netdev_ops;
440 +       dev->type               = ARPHRD_VOID;
441 +       dev->mtu                = 16000;
442 +       dev->tx_queue_len       = 11000;
443 +       dev->flags              = IFF_NOARP;
444 +       dev->features           = NETIF_F_SG | NETIF_F_FRAGLIST |
445 +                                 NETIF_F_GSO | NETIF_F_HW_CSUM |
446 +                                 NETIF_F_HIGHDMA;
447 +       dev->priv_flags         &= ~IFF_XMIT_DST_RELEASE;
448 +}
449 +
450 +static int imq_validate(struct nlattr *tb[], struct nlattr *data[])
451 +{
452 +       int ret = 0;
453 +
454 +       if (tb[IFLA_ADDRESS]) {
455 +               if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
456 +                       ret = -EINVAL;
457 +                       goto end;
458 +               }
459 +               if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
460 +                       ret = -EADDRNOTAVAIL;
461 +                       goto end;
462 +               }
463 +       }
464 +       return 0;
465 +end:
466 +       printk(KERN_WARNING "IMQ: imq_validate failed (%d)\n", ret);
467 +       return ret;
468 +}
469 +
470 +static struct rtnl_link_ops imq_link_ops __read_mostly = {
471 +       .kind           = "imq",
472 +       .priv_size      = 0,
473 +       .setup          = imq_setup,
474 +       .validate       = imq_validate,
475 +};
476 +
477 +static int __init imq_init_hooks(void)
478 +{
479 +       int err;
480 +
481 +       nf_register_queue_imq_handler(&nfqh);
482 +
483 +       err = nf_register_hook(&imq_ingress_ipv4);
484 +       if (err)
485 +               goto err1;
486 +
487 +       err = nf_register_hook(&imq_egress_ipv4);
488 +       if (err)
489 +               goto err2;
490 +
491 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
492 +       err = nf_register_hook(&imq_ingress_ipv6);
493 +       if (err)
494 +               goto err3;
495 +
496 +       err = nf_register_hook(&imq_egress_ipv6);
497 +       if (err)
498 +               goto err4;
499 +#endif
500 +
501 +       return 0;
502 +
503 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
504 +err4:
505 +       nf_unregister_hook(&imq_ingress_ipv6);
506 +err3:
507 +       nf_unregister_hook(&imq_egress_ipv4);
508 +#endif
509 +err2:
510 +       nf_unregister_hook(&imq_ingress_ipv4);
511 +err1:
512 +       nf_unregister_queue_imq_handler();
513 +       return err;
514 +}
515 +
516 +static int __init imq_init_one(int index)
517 +{
518 +       struct net_device *dev;
519 +       int ret;
520 +
521 +       dev = alloc_netdev(0, "imq%d", imq_setup);
522 +       if (!dev)
523 +               return -ENOMEM;
524 +
525 +       ret = dev_alloc_name(dev, dev->name);
526 +       if (ret < 0)
527 +               goto fail;
528 +
529 +       dev->rtnl_link_ops = &imq_link_ops;
530 +       ret = register_netdevice(dev);
531 +       if (ret < 0)
532 +               goto fail;
533 +
534 +       return 0;
535 +fail:
536 +       free_netdev(dev);
537 +       return ret;
538 +}
539 +
540 +static int __init imq_init_devs(void)
541 +{
542 +       int err, i;
543 +
544 +       if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) {
545 +               printk(KERN_ERR "IMQ: numdevs has to be betweed 1 and %u\n",
546 +                      IMQ_MAX_DEVS);
547 +               return -EINVAL;
548 +       }
549 +
550 +       rtnl_lock();
551 +       err = __rtnl_link_register(&imq_link_ops);
552 +
553 +       for (i = 0; i < numdevs && !err; i++)
554 +               err = imq_init_one(i);
555 +
556 +       if (err) {
557 +               __rtnl_link_unregister(&imq_link_ops);
558 +               memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
559 +       }
560 +       rtnl_unlock();
561 +
562 +       return err;
563 +}
564 +
565 +static int __init imq_init_module(void)
566 +{
567 +       int err;
568 +
569 +#if defined(CONFIG_IMQ_NUM_DEVS)
570 +       BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS > 16);
571 +       BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS < 2);
572 +       BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS - 1 > IMQ_F_IFMASK);
573 +#endif
574 +
575 +       err = imq_init_devs();
576 +       if (err) {
577 +               printk(KERN_ERR "IMQ: Error trying imq_init_devs(net)\n");
578 +               return err;
579 +       }
580 +
581 +       err = imq_init_hooks();
582 +       if (err) {
583 +               printk(KERN_ERR "IMQ: Error trying imq_init_hooks()\n");
584 +               rtnl_link_unregister(&imq_link_ops);
585 +               memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
586 +               return err;
587 +       }
588 +
589 +       printk(KERN_INFO "IMQ driver loaded successfully.\n");
590 +
591 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
592 +       printk(KERN_INFO "\tHooking IMQ before NAT on PREROUTING.\n");
593 +#else
594 +       printk(KERN_INFO "\tHooking IMQ after NAT on PREROUTING.\n");
595 +#endif
596 +#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
597 +       printk(KERN_INFO "\tHooking IMQ before NAT on POSTROUTING.\n");
598 +#else
599 +       printk(KERN_INFO "\tHooking IMQ after NAT on POSTROUTING.\n");
600 +#endif
601 +
602 +       return 0;
603 +}
604 +
605 +static void __exit imq_unhook(void)
606 +{
607 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
608 +       nf_unregister_hook(&imq_ingress_ipv6);
609 +       nf_unregister_hook(&imq_egress_ipv6);
610 +#endif
611 +       nf_unregister_hook(&imq_ingress_ipv4);
612 +       nf_unregister_hook(&imq_egress_ipv4);
613 +
614 +       nf_unregister_queue_imq_handler();
615 +}
616 +
617 +static void __exit imq_cleanup_devs(void)
618 +{
619 +       rtnl_link_unregister(&imq_link_ops);
620 +       memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
621 +}
622 +
623 +static void __exit imq_exit_module(void)
624 +{
625 +       imq_unhook();
626 +       imq_cleanup_devs();
627 +       printk(KERN_INFO "IMQ driver unloaded successfully.\n");
628 +}
629 +
630 +module_init(imq_init_module);
631 +module_exit(imq_exit_module);
632 +
633 +module_param(numdevs, int, 0);
634 +MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will "
635 +                       "be created)");
636 +MODULE_AUTHOR("http://www.linuximq.net");
637 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See "
638 +                       "http://www.linuximq.net/ for more information.");
639 +MODULE_LICENSE("GPL");
640 +MODULE_ALIAS_RTNL_LINK("imq");
641 +
642 --- a/drivers/net/Kconfig
643 +++ b/drivers/net/Kconfig
644 @@ -124,6 +124,129 @@ config EQUALIZER
645           To compile this driver as a module, choose M here: the module
646           will be called eql.  If unsure, say N.
647  
648 +config IMQ
649 +       tristate "IMQ (intermediate queueing device) support"
650 +       depends on NETDEVICES && NETFILTER
651 +       ---help---
652 +         The IMQ device(s) is used as placeholder for QoS queueing
653 +         disciplines. Every packet entering/leaving the IP stack can be
654 +         directed through the IMQ device where it's enqueued/dequeued to the
655 +         attached qdisc. This allows you to treat network devices as classes
656 +         and distribute bandwidth among them. Iptables is used to specify
657 +         through which IMQ device, if any, packets travel.
658 +
659 +         More information at: http://www.linuximq.net/
660 +
661 +         To compile this driver as a module, choose M here: the module
662 +         will be called imq.  If unsure, say N.
663 +
664 +choice
665 +       prompt "IMQ behavior (PRE/POSTROUTING)"
666 +       depends on IMQ
667 +       default IMQ_BEHAVIOR_AB
668 +       help
669 +
670 +               This settings defines how IMQ behaves in respect to its
671 +               hooking in PREROUTING and POSTROUTING.
672 +
673 +               IMQ can work in any of the following ways:
674 +
675 +                   PREROUTING   |      POSTROUTING
676 +               -----------------|-------------------
677 +               #1  After NAT    |      After NAT
678 +               #2  After NAT    |      Before NAT
679 +               #3  Before NAT   |      After NAT
680 +               #4  Before NAT   |      Before NAT
681 +
682 +               The default behavior is to hook before NAT on PREROUTING
683 +               and after NAT on POSTROUTING (#3).
684 +
685 +               This settings are specially usefull when trying to use IMQ
686 +               to shape NATed clients.
687 +
688 +               More information can be found at: www.linuximq.net
689 +
690 +               If not sure leave the default settings alone.
691 +
692 +config IMQ_BEHAVIOR_AA
693 +       bool "IMQ AA"
694 +       help
695 +               This settings defines how IMQ behaves in respect to its
696 +               hooking in PREROUTING and POSTROUTING.
697 +
698 +               Choosing this option will make IMQ hook like this:
699 +
700 +               PREROUTING:   After NAT
701 +               POSTROUTING:  After NAT
702 +
703 +               More information can be found at: www.linuximq.net
704 +
705 +               If not sure leave the default settings alone.
706 +
707 +config IMQ_BEHAVIOR_AB
708 +       bool "IMQ AB"
709 +       help
710 +               This settings defines how IMQ behaves in respect to its
711 +               hooking in PREROUTING and POSTROUTING.
712 +
713 +               Choosing this option will make IMQ hook like this:
714 +
715 +               PREROUTING:   After NAT
716 +               POSTROUTING:  Before NAT
717 +
718 +               More information can be found at: www.linuximq.net
719 +
720 +               If not sure leave the default settings alone.
721 +
722 +config IMQ_BEHAVIOR_BA
723 +       bool "IMQ BA"
724 +       help
725 +               This settings defines how IMQ behaves in respect to its
726 +               hooking in PREROUTING and POSTROUTING.
727 +
728 +               Choosing this option will make IMQ hook like this:
729 +
730 +               PREROUTING:   Before NAT
731 +               POSTROUTING:  After NAT
732 +
733 +               More information can be found at: www.linuximq.net
734 +
735 +               If not sure leave the default settings alone.
736 +
737 +config IMQ_BEHAVIOR_BB
738 +       bool "IMQ BB"
739 +       help
740 +               This settings defines how IMQ behaves in respect to its
741 +               hooking in PREROUTING and POSTROUTING.
742 +
743 +               Choosing this option will make IMQ hook like this:
744 +
745 +               PREROUTING:   Before NAT
746 +               POSTROUTING:  Before NAT
747 +
748 +               More information can be found at: www.linuximq.net
749 +
750 +               If not sure leave the default settings alone.
751 +
752 +endchoice
753 +
754 +config IMQ_NUM_DEVS
755 +
756 +       int "Number of IMQ devices"
757 +       range 2 16
758 +       depends on IMQ
759 +       default "16"
760 +       help
761 +
762 +               This settings defines how many IMQ devices will be
763 +               created.
764 +
765 +               The default value is 16.
766 +
767 +               More information can be found at: www.linuximq.net
768 +
769 +               If not sure leave the default settings alone.
770 +
771  config TUN
772         tristate "Universal TUN/TAP device driver support"
773         select CRC32
774 --- a/drivers/net/Makefile
775 +++ b/drivers/net/Makefile
776 @@ -173,6 +173,7 @@ obj-$(CONFIG_SLHC) += slhc.o
777  obj-$(CONFIG_XEN_NETDEV_FRONTEND) += xen-netfront.o
778  
779  obj-$(CONFIG_DUMMY) += dummy.o
780 +obj-$(CONFIG_IMQ) += imq.o
781  obj-$(CONFIG_IFB) += ifb.o
782  obj-$(CONFIG_MACVLAN) += macvlan.o
783  obj-$(CONFIG_MACVTAP) += macvtap.o
784 --- /dev/null
785 +++ b/include/linux/imq.h
786 @@ -0,0 +1,13 @@
787 +#ifndef _IMQ_H
788 +#define _IMQ_H
789 +
790 +/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
791 +#define IMQ_F_BITS     5
792 +
793 +#define IMQ_F_IFMASK   0x0f
794 +#define IMQ_F_ENQUEUE  0x10
795 +
796 +#define IMQ_MAX_DEVS   (IMQ_F_IFMASK + 1)
797 +
798 +#endif /* _IMQ_H */
799 +
800 --- a/include/linux/netdevice.h
801 +++ b/include/linux/netdevice.h
802 @@ -1323,6 +1323,7 @@ extern int                dev_alloc_name(struct net_de
803  extern int             dev_open(struct net_device *dev);
804  extern int             dev_close(struct net_device *dev);
805  extern void            dev_disable_lro(struct net_device *dev);
806 +extern struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb);
807  extern int             dev_queue_xmit(struct sk_buff *skb);
808  extern int             register_netdevice(struct net_device *dev);
809  extern void            unregister_netdevice_queue(struct net_device *dev,
810 --- /dev/null
811 +++ b/include/linux/netfilter/xt_IMQ.h
812 @@ -0,0 +1,9 @@
813 +#ifndef _XT_IMQ_H
814 +#define _XT_IMQ_H
815 +
816 +struct xt_imq_info {
817 +       unsigned int todev;     /* target imq device */
818 +};
819 +
820 +#endif /* _XT_IMQ_H */
821 +
822 --- /dev/null
823 +++ b/include/linux/netfilter_ipv4/ipt_IMQ.h
824 @@ -0,0 +1,10 @@
825 +#ifndef _IPT_IMQ_H
826 +#define _IPT_IMQ_H
827 +
828 +/* Backwards compatibility for old userspace */
829 +#include <linux/netfilter/xt_IMQ.h>
830 +
831 +#define ipt_imq_info xt_imq_info
832 +
833 +#endif /* _IPT_IMQ_H */
834 +
835 --- /dev/null
836 +++ b/include/linux/netfilter_ipv6/ip6t_IMQ.h
837 @@ -0,0 +1,10 @@
838 +#ifndef _IP6T_IMQ_H
839 +#define _IP6T_IMQ_H
840 +
841 +/* Backwards compatibility for old userspace */
842 +#include <linux/netfilter/xt_IMQ.h>
843 +
844 +#define ip6t_imq_info xt_imq_info
845 +
846 +#endif /* _IP6T_IMQ_H */
847 +
848 --- a/include/linux/skbuff.h
849 +++ b/include/linux/skbuff.h
850 @@ -29,6 +29,9 @@
851  #include <linux/rcupdate.h>
852  #include <linux/dmaengine.h>
853  #include <linux/hrtimer.h>
854 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
855 +#include <linux/imq.h>
856 +#endif
857  
858  /* Don't change this without changing skb_csum_unnecessary! */
859  #define CHECKSUM_NONE 0
860 @@ -328,6 +331,9 @@ struct sk_buff {
861          * first. This is owned by whoever has the skb queued ATM.
862          */
863         char                    cb[48] __aligned(8);
864 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
865 +       void                    *cb_next;
866 +#endif
867  
868         unsigned long           _skb_refdst;
869  #ifdef CONFIG_XFRM
870 @@ -364,6 +370,9 @@ struct sk_buff {
871         struct nf_conntrack     *nfct;
872         struct sk_buff          *nfct_reasm;
873  #endif
874 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
875 +       struct nf_queue_entry   *nf_queue_entry;
876 +#endif
877  #ifdef CONFIG_BRIDGE_NETFILTER
878         struct nf_bridge_info   *nf_bridge;
879  #endif
880 @@ -390,6 +399,10 @@ struct sk_buff {
881  
882         /* 0/14 bit hole */
883  
884 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
885 +       __u8                    imq_flags:IMQ_F_BITS;
886 +#endif
887 +
888  #ifdef CONFIG_NET_DMA
889         dma_cookie_t            dma_cookie;
890  #endif
891 @@ -476,6 +489,12 @@ static inline struct rtable *skb_rtable(
892         return (struct rtable *)skb_dst(skb);
893  }
894  
895 +
896 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
897 +extern int skb_save_cb(struct sk_buff *skb);
898 +extern int skb_restore_cb(struct sk_buff *skb);
899 +#endif
900 +
901  extern void kfree_skb(struct sk_buff *skb);
902  extern void consume_skb(struct sk_buff *skb);
903  extern void           __kfree_skb(struct sk_buff *skb);
904 @@ -2098,6 +2117,10 @@ static inline void __nf_copy(struct sk_b
905         dst->nfct_reasm = src->nfct_reasm;
906         nf_conntrack_get_reasm(src->nfct_reasm);
907  #endif
908 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
909 +       dst->imq_flags = src->imq_flags;
910 +       dst->nf_queue_entry = src->nf_queue_entry;
911 +#endif
912  #ifdef CONFIG_BRIDGE_NETFILTER
913         dst->nf_bridge  = src->nf_bridge;
914         nf_bridge_get(src->nf_bridge);
915 --- a/include/net/netfilter/nf_queue.h
916 +++ b/include/net/netfilter/nf_queue.h
917 @@ -13,6 +13,12 @@ struct nf_queue_entry {
918         struct net_device       *indev;
919         struct net_device       *outdev;
920         int                     (*okfn)(struct sk_buff *);
921 +
922 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
923 +       int                     (*next_outfn)(struct nf_queue_entry *entry,
924 +                                             unsigned int queuenum);
925 +       unsigned int            next_queuenum;
926 +#endif
927  };
928  
929  #define nf_queue_entry_reroute(x) ((void *)x + sizeof(struct nf_queue_entry))
930 @@ -30,5 +36,11 @@ extern int nf_unregister_queue_handler(u
931                                        const struct nf_queue_handler *qh);
932  extern void nf_unregister_queue_handlers(const struct nf_queue_handler *qh);
933  extern void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
934 +extern void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
935 +
936 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
937 +extern void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
938 +extern void nf_unregister_queue_imq_handler(void);
939 +#endif
940  
941  #endif /* _NF_QUEUE_H */
942 --- a/net/core/dev.c
943 +++ b/net/core/dev.c
944 @@ -98,6 +98,9 @@
945  #include <net/net_namespace.h>
946  #include <net/sock.h>
947  #include <linux/rtnetlink.h>
948 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
949 +#include <linux/imq.h>
950 +#endif
951  #include <linux/proc_fs.h>
952  #include <linux/seq_file.h>
953  #include <linux/stat.h>
954 @@ -1995,7 +1998,11 @@ int dev_hard_start_xmit(struct sk_buff *
955         int rc = NETDEV_TX_OK;
956  
957         if (likely(!skb->next)) {
958 -               if (!list_empty(&ptype_all))
959 +               if (!list_empty(&ptype_all)
960 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
961 +                       && !(skb->imq_flags & IMQ_F_ENQUEUE)
962 +#endif
963 +                  )
964                         dev_queue_xmit_nit(skb, dev);
965  
966                 /*
967 @@ -2119,8 +2126,7 @@ static inline u16 dev_cap_txqueue(struct
968         return queue_index;
969  }
970  
971 -static struct netdev_queue *dev_pick_tx(struct net_device *dev,
972 -                                       struct sk_buff *skb)
973 +struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb)
974  {
975         int queue_index;
976         const struct net_device_ops *ops = dev->netdev_ops;
977 @@ -2149,6 +2155,7 @@ static struct netdev_queue *dev_pick_tx(
978         skb_set_queue_mapping(skb, queue_index);
979         return netdev_get_tx_queue(dev, queue_index);
980  }
981 +EXPORT_SYMBOL(dev_pick_tx);
982  
983  static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
984                                  struct net_device *dev,
985 --- a/net/core/skbuff.c
986 +++ b/net/core/skbuff.c
987 @@ -72,6 +72,9 @@
988  
989  static struct kmem_cache *skbuff_head_cache __read_mostly;
990  static struct kmem_cache *skbuff_fclone_cache __read_mostly;
991 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
992 +static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
993 +#endif
994  
995  static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
996                                   struct pipe_buffer *buf)
997 @@ -91,6 +94,83 @@ static int sock_pipe_buf_steal(struct pi
998         return 1;
999  }
1000  
1001 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1002 +/* Control buffer save/restore for IMQ devices */
1003 +struct skb_cb_table {
1004 +       void                    *cb_next;
1005 +       atomic_t                refcnt;
1006 +       char                    cb[48];
1007 +};
1008 +
1009 +static DEFINE_SPINLOCK(skb_cb_store_lock);
1010 +
1011 +int skb_save_cb(struct sk_buff *skb)
1012 +{
1013 +       struct skb_cb_table *next;
1014 +
1015 +       next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
1016 +       if (!next)
1017 +               return -ENOMEM;
1018 +
1019 +       BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1020 +
1021 +       memcpy(next->cb, skb->cb, sizeof(skb->cb));
1022 +       next->cb_next = skb->cb_next;
1023 +
1024 +       atomic_set(&next->refcnt, 1);
1025 +
1026 +       skb->cb_next = next;
1027 +       return 0;
1028 +}
1029 +EXPORT_SYMBOL(skb_save_cb);
1030 +
1031 +int skb_restore_cb(struct sk_buff *skb)
1032 +{
1033 +       struct skb_cb_table *next;
1034 +
1035 +       if (!skb->cb_next)
1036 +               return 0;
1037 +
1038 +       next = skb->cb_next;
1039 +
1040 +       BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1041 +
1042 +       memcpy(skb->cb, next->cb, sizeof(skb->cb));
1043 +       skb->cb_next = next->cb_next;
1044 +
1045 +       spin_lock(&skb_cb_store_lock);
1046 +
1047 +       if (atomic_dec_and_test(&next->refcnt)) {
1048 +               kmem_cache_free(skbuff_cb_store_cache, next);
1049 +       }
1050 +
1051 +       spin_unlock(&skb_cb_store_lock);
1052 +
1053 +       return 0;
1054 +}
1055 +EXPORT_SYMBOL(skb_restore_cb);
1056 +
1057 +static void skb_copy_stored_cb(struct sk_buff *new, const struct sk_buff *__old)
1058 +{
1059 +       struct skb_cb_table *next;
1060 +       struct sk_buff *old;
1061 +
1062 +       if (!__old->cb_next) {
1063 +               new->cb_next = NULL;
1064 +               return;
1065 +       }
1066 +
1067 +       spin_lock(&skb_cb_store_lock);
1068 +
1069 +       old = (struct sk_buff *)__old;
1070 +
1071 +       next = old->cb_next;
1072 +       atomic_inc(&next->refcnt);
1073 +       new->cb_next = next;
1074 +
1075 +       spin_unlock(&skb_cb_store_lock);
1076 +}
1077 +#endif
1078  
1079  /* Pipe buffer operations for a socket. */
1080  static const struct pipe_buf_operations sock_pipe_buf_ops = {
1081 @@ -378,6 +458,26 @@ static void skb_release_head_state(struc
1082                 WARN_ON(in_irq());
1083                 skb->destructor(skb);
1084         }
1085 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1086 +       /* This should not happen. When it does, avoid memleak by restoring
1087 +       the chain of cb-backups. */
1088 +       while(skb->cb_next != NULL) {
1089 +               if (net_ratelimit())
1090 +                       printk(KERN_WARNING "IMQ: kfree_skb: skb->cb_next: "
1091 +                               "%08x\n", (unsigned int)skb->cb_next);
1092 +
1093 +               skb_restore_cb(skb);
1094 +       }
1095 +       /* This should not happen either, nf_queue_entry is nullified in
1096 +        * imq_dev_xmit(). If we have non-NULL nf_queue_entry then we are
1097 +        * leaking entry pointers, maybe memory. We don't know if this is
1098 +        * pointer to already freed memory, or should this be freed.
1099 +        * If this happens we need to add refcounting, etc for nf_queue_entry.
1100 +        */
1101 +       if (skb->nf_queue_entry && net_ratelimit())
1102 +               printk(KERN_WARNING
1103 +                               "IMQ: kfree_skb: skb->nf_queue_entry != NULL");
1104 +#endif
1105  #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1106         nf_conntrack_put(skb->nfct);
1107         nf_conntrack_put_reasm(skb->nfct_reasm);
1108 @@ -514,6 +614,9 @@ static void __copy_skb_header(struct sk_
1109         new->sp                 = secpath_get(old->sp);
1110  #endif
1111         memcpy(new->cb, old->cb, sizeof(old->cb));
1112 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1113 +       skb_copy_stored_cb(new, old);
1114 +#endif
1115         new->csum               = old->csum;
1116         new->local_df           = old->local_df;
1117         new->pkt_type           = old->pkt_type;
1118 @@ -2765,6 +2868,13 @@ void __init skb_init(void)
1119                                                 0,
1120                                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1121                                                 NULL);
1122 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1123 +       skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1124 +                                                 sizeof(struct skb_cb_table),
1125 +                                                 0,
1126 +                                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1127 +                                                 NULL);
1128 +#endif
1129  }
1130  
1131  /**
1132 --- a/net/netfilter/Kconfig
1133 +++ b/net/netfilter/Kconfig
1134 @@ -455,6 +455,18 @@ config NETFILTER_XT_TARGET_LED
1135           For more information on the LEDs available on your system, see
1136           Documentation/leds-class.txt
1137  
1138 +config NETFILTER_XT_TARGET_IMQ
1139 +        tristate '"IMQ" target support'
1140 +       depends on NETFILTER_XTABLES
1141 +       depends on IP_NF_MANGLE || IP6_NF_MANGLE
1142 +       select IMQ
1143 +       default m if NETFILTER_ADVANCED=n
1144 +        help
1145 +          This option adds a `IMQ' target which is used to specify if and
1146 +          to which imq device packets should get enqueued/dequeued.
1147 +
1148 +          To compile it as a module, choose M here.  If unsure, say N.
1149 +
1150  config NETFILTER_XT_TARGET_MARK
1151         tristate '"MARK" target support'
1152         depends on NETFILTER_ADVANCED
1153 --- a/net/netfilter/Makefile
1154 +++ b/net/netfilter/Makefile
1155 @@ -51,6 +51,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_CONNSEC
1156  obj-$(CONFIG_NETFILTER_XT_TARGET_CT) += xt_CT.o
1157  obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
1158  obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
1159 +obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
1160  obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
1161  obj-$(CONFIG_NETFILTER_XT_TARGET_NFLOG) += xt_NFLOG.o
1162  obj-$(CONFIG_NETFILTER_XT_TARGET_NFQUEUE) += xt_NFQUEUE.o
1163 --- a/net/netfilter/nf_queue.c
1164 +++ b/net/netfilter/nf_queue.c
1165 @@ -22,6 +22,26 @@ static const struct nf_queue_handler __r
1166  
1167  static DEFINE_MUTEX(queue_handler_mutex);
1168  
1169 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1170 +static const struct nf_queue_handler *queue_imq_handler;
1171 +
1172 +void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
1173 +{
1174 +       mutex_lock(&queue_handler_mutex);
1175 +       rcu_assign_pointer(queue_imq_handler, qh);
1176 +       mutex_unlock(&queue_handler_mutex);
1177 +}
1178 +EXPORT_SYMBOL(nf_register_queue_imq_handler);
1179 +
1180 +void nf_unregister_queue_imq_handler(void)
1181 +{
1182 +       mutex_lock(&queue_handler_mutex);
1183 +       rcu_assign_pointer(queue_imq_handler, NULL);
1184 +       mutex_unlock(&queue_handler_mutex);
1185 +}
1186 +EXPORT_SYMBOL(nf_unregister_queue_imq_handler);
1187 +#endif
1188 +
1189  /* return EBUSY when somebody else is registered, return EEXIST if the
1190   * same handler is registered, return 0 in case of success. */
1191  int nf_register_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh)
1192 @@ -82,7 +102,7 @@ void nf_unregister_queue_handlers(const
1193  }
1194  EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
1195  
1196 -static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1197 +void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1198  {
1199         /* Release those devices we held, or Alexey will kill me. */
1200         if (entry->indev)
1201 @@ -102,6 +122,7 @@ static void nf_queue_entry_release_refs(
1202         /* Drop reference to owner of hook which queued us. */
1203         module_put(entry->elem->owner);
1204  }
1205 +EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
1206  
1207  /*
1208   * Any packet that leaves via this function must come back
1209 @@ -123,12 +144,26 @@ static int __nf_queue(struct sk_buff *sk
1210  #endif
1211         const struct nf_afinfo *afinfo;
1212         const struct nf_queue_handler *qh;
1213 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1214 +       const struct nf_queue_handler *qih = NULL;
1215 +#endif
1216  
1217         /* QUEUE == DROP if noone is waiting, to be safe. */
1218         rcu_read_lock();
1219  
1220         qh = rcu_dereference(queue_handler[pf]);
1221 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1222 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1223 +       if (pf == PF_INET || pf == PF_INET6)
1224 +#else
1225 +       if (pf == PF_INET)
1226 +#endif
1227 +               qih = rcu_dereference(queue_imq_handler);
1228 +
1229 +       if (!qh && !qih)
1230 +#else /* !IMQ */
1231         if (!qh)
1232 +#endif
1233                 goto err_unlock;
1234  
1235         afinfo = nf_get_afinfo(pf);
1236 @@ -147,6 +182,10 @@ static int __nf_queue(struct sk_buff *sk
1237                 .indev  = indev,
1238                 .outdev = outdev,
1239                 .okfn   = okfn,
1240 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1241 +               .next_outfn = qh ? qh->outfn : NULL,
1242 +               .next_queuenum = queuenum,
1243 +#endif
1244         };
1245  
1246         /* If it's going away, ignore hook. */
1247 @@ -173,8 +212,19 @@ static int __nf_queue(struct sk_buff *sk
1248  #endif
1249         skb_dst_force(skb);
1250         afinfo->saveroute(skb, entry);
1251 +
1252 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1253 +       if (qih) {
1254 +               status = qih->outfn(entry, queuenum);
1255 +               goto imq_skip_queue;
1256 +       }
1257 +#endif
1258 +
1259         status = qh->outfn(entry, queuenum);
1260  
1261 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1262 +imq_skip_queue:
1263 +#endif
1264         rcu_read_unlock();
1265  
1266         if (status < 0) {
1267 --- /dev/null
1268 +++ b/net/netfilter/xt_IMQ.c
1269 @@ -0,0 +1,73 @@
1270 +/*
1271 + * This target marks packets to be enqueued to an imq device
1272 + */
1273 +#include <linux/module.h>
1274 +#include <linux/skbuff.h>
1275 +#include <linux/netfilter/x_tables.h>
1276 +#include <linux/netfilter/xt_IMQ.h>
1277 +#include <linux/imq.h>
1278 +
1279 +static unsigned int imq_target(struct sk_buff *pskb,
1280 +                               const struct xt_action_param *par)
1281 +{
1282 +       const struct xt_imq_info *mr = par->targinfo;
1283 +
1284 +       pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
1285 +
1286 +       return XT_CONTINUE;
1287 +}
1288 +
1289 +static int imq_checkentry(const struct xt_tgchk_param *par)
1290 +{
1291 +       struct xt_imq_info *mr = par->targinfo;
1292 +
1293 +       if (mr->todev > IMQ_MAX_DEVS - 1) {
1294 +               printk(KERN_WARNING
1295 +                      "IMQ: invalid device specified, highest is %u\n",
1296 +                      IMQ_MAX_DEVS - 1);
1297 +               return -EINVAL;
1298 +       }
1299 +
1300 +       return 0;
1301 +}
1302 +
1303 +static struct xt_target xt_imq_reg[] __read_mostly = {
1304 +       {
1305 +               .name           = "IMQ",
1306 +               .family         = AF_INET,
1307 +               .checkentry     = imq_checkentry,
1308 +               .target         = imq_target,
1309 +               .targetsize     = sizeof(struct xt_imq_info),
1310 +               .table          = "mangle",
1311 +               .me             = THIS_MODULE
1312 +       },
1313 +       {
1314 +               .name           = "IMQ",
1315 +               .family         = AF_INET6,
1316 +               .checkentry     = imq_checkentry,
1317 +               .target         = imq_target,
1318 +               .targetsize     = sizeof(struct xt_imq_info),
1319 +               .table          = "mangle",
1320 +               .me             = THIS_MODULE
1321 +       },
1322 +};
1323 +
1324 +static int __init imq_init(void)
1325 +{
1326 +       return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1327 +}
1328 +
1329 +static void __exit imq_fini(void)
1330 +{
1331 +       xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1332 +}
1333 +
1334 +module_init(imq_init);
1335 +module_exit(imq_fini);
1336 +
1337 +MODULE_AUTHOR("http://www.linuximq.net");
1338 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
1339 +MODULE_LICENSE("GPL");
1340 +MODULE_ALIAS("ipt_IMQ");
1341 +MODULE_ALIAS("ip6t_IMQ");
1342 +