[kernel] generic-2.6: even newer IMQ patch for 2.6.25 and 2.6.26
[15.05/openwrt.git] / target / linux / generic-2.6 / patches-2.6.26 / 150-netfilter_imq.patch
1
2
3 ---
4
5  drivers/net/Kconfig                     |  123 ++++++++
6  drivers/net/Makefile                    |    1 
7  drivers/net/imq.c                       |  474 +++++++++++++++++++++++++++++++
8  include/linux/imq.h                     |    9 +
9  include/linux/netfilter_ipv4/ipt_IMQ.h  |    8 +
10  include/linux/netfilter_ipv6/ip6t_IMQ.h |    8 +
11  include/linux/skbuff.h                  |    8 +
12  net/core/dev.c                          |    9 +
13  net/ipv4/netfilter/Kconfig              |   11 +
14  net/ipv4/netfilter/Makefile             |    1 
15  net/ipv4/netfilter/ipt_IMQ.c            |   69 +++++
16  net/ipv6/netfilter/Kconfig              |    9 +
17  net/ipv6/netfilter/Makefile             |    1 
18  net/ipv6/netfilter/ip6t_IMQ.c           |   69 +++++
19  net/sched/sch_generic.c                 |    1 
20  15 files changed, 800 insertions(+), 1 deletions(-)
21  create mode 100644 drivers/net/imq.c
22  create mode 100644 include/linux/imq.h
23  create mode 100644 include/linux/netfilter_ipv4/ipt_IMQ.h
24  create mode 100644 include/linux/netfilter_ipv6/ip6t_IMQ.h
25  create mode 100644 net/ipv4/netfilter/ipt_IMQ.c
26  create mode 100644 net/ipv6/netfilter/ip6t_IMQ.c
27
28
29 --- a/drivers/net/Kconfig
30 +++ b/drivers/net/Kconfig
31 @@ -117,6 +117,129 @@
32           To compile this driver as a module, choose M here: the module
33           will be called eql.  If unsure, say N.
34  
35 +config IMQ
36 +       tristate "IMQ (intermediate queueing device) support"
37 +       depends on NETDEVICES && NETFILTER
38 +       ---help---
39 +         The IMQ device(s) is used as placeholder for QoS queueing
40 +         disciplines. Every packet entering/leaving the IP stack can be
41 +         directed through the IMQ device where it's enqueued/dequeued to the
42 +         attached qdisc. This allows you to treat network devices as classes
43 +         and distribute bandwidth among them. Iptables is used to specify
44 +         through which IMQ device, if any, packets travel.
45 +
46 +         More information at: http://www.linuximq.net/
47 +
48 +         To compile this driver as a module, choose M here: the module
49 +         will be called imq.  If unsure, say N.
50 +
51 +choice
52 +       prompt "IMQ behavior (PRE/POSTROUTING)"
53 +       depends on IMQ
54 +       default IMQ_BEHAVIOR_BB
55 +       help
56 +
57 +               This settings defines how IMQ behaves in respect to its
58 +               hooking in PREROUTING and POSTROUTING.
59 +
60 +               IMQ can work in any of the following ways:
61 +
62 +                   PREROUTING   |      POSTROUTING
63 +               -----------------|-------------------
64 +               #1  After NAT    |      After NAT
65 +               #2  After NAT    |      Before NAT
66 +               #3  Before NAT   |      After NAT
67 +               #4  Before NAT   |      Before NAT
68 +
69 +               The default behavior is to hook before NAT on PREROUTING
70 +               and after NAT on POSTROUTING (#3).
71 +
72 +               This settings are specially usefull when trying to use IMQ
73 +               to shape NATed clients.
74 +
75 +               More information can be found at: www.linuximq.net
76 +
77 +               If not sure leave the default settings alone.
78 +
79 +config IMQ_BEHAVIOR_AA
80 +       bool "IMQ AA"
81 +       help
82 +               This settings defines how IMQ behaves in respect to its
83 +               hooking in PREROUTING and POSTROUTING.
84 +
85 +               Choosing this option will make IMQ hook like this:
86 +
87 +               PREROUTING:   After NAT
88 +               POSTROUTING:  After NAT
89 +
90 +               More information can be found at: www.linuximq.net
91 +
92 +               If not sure leave the default settings alone.
93 +
94 +config IMQ_BEHAVIOR_AB
95 +       bool "IMQ AB"
96 +       help
97 +               This settings defines how IMQ behaves in respect to its
98 +               hooking in PREROUTING and POSTROUTING.
99 +
100 +               Choosing this option will make IMQ hook like this:
101 +
102 +               PREROUTING:   After NAT
103 +               POSTROUTING:  Before NAT
104 +
105 +               More information can be found at: www.linuximq.net
106 +
107 +               If not sure leave the default settings alone.
108 +
109 +config IMQ_BEHAVIOR_BA
110 +       bool "IMQ BA"
111 +       help
112 +               This settings defines how IMQ behaves in respect to its
113 +               hooking in PREROUTING and POSTROUTING.
114 +
115 +               Choosing this option will make IMQ hook like this:
116 +
117 +               PREROUTING:   Before NAT
118 +               POSTROUTING:  After NAT
119 +
120 +               More information can be found at: www.linuximq.net
121 +
122 +               If not sure leave the default settings alone.
123 +
124 +config IMQ_BEHAVIOR_BB
125 +       bool "IMQ BB"
126 +       help
127 +               This settings defines how IMQ behaves in respect to its
128 +               hooking in PREROUTING and POSTROUTING.
129 +
130 +               Choosing this option will make IMQ hook like this:
131 +
132 +               PREROUTING:   Before NAT
133 +               POSTROUTING:  Before NAT
134 +
135 +               More information can be found at: www.linuximq.net
136 +
137 +               If not sure leave the default settings alone.
138 +
139 +endchoice
140 +
141 +config IMQ_NUM_DEVS
142 +
143 +       int "Number of IMQ devices"
144 +       range 2 16
145 +       depends on IMQ
146 +       default "16"
147 +       help
148 +
149 +               This settings defines how many IMQ devices will be
150 +               created.
151 +
152 +               The default value is 16.
153 +
154 +               More information can be found at: www.linuximq.net
155 +
156 +               If not sure leave the default settings alone.
157 +
158  config TUN
159         tristate "Universal TUN/TAP device driver support"
160         select CRC32
161 --- a/drivers/net/Makefile
162 +++ b/drivers/net/Makefile
163 @@ -142,6 +142,7 @@
164  obj-$(CONFIG_XEN_NETDEV_FRONTEND) += xen-netfront.o
165  
166  obj-$(CONFIG_DUMMY) += dummy.o
167 +obj-$(CONFIG_IMQ) += imq.o
168  obj-$(CONFIG_IFB) += ifb.o
169  obj-$(CONFIG_MACVLAN) += macvlan.o
170  obj-$(CONFIG_DE600) += de600.o
171 --- /dev/null
172 +++ b/drivers/net/imq.c
173 @@ -0,0 +1,474 @@
174 +/*
175 + *             Pseudo-driver for the intermediate queue device.
176 + *
177 + *             This program is free software; you can redistribute it and/or
178 + *             modify it under the terms of the GNU General Public License
179 + *             as published by the Free Software Foundation; either version
180 + *             2 of the License, or (at your option) any later version.
181 + *
182 + * Authors:    Patrick McHardy, <kaber@trash.net>
183 + *
184 + *            The first version was written by Martin Devera, <devik@cdi.cz>
185 + *
186 + * Credits:    Jan Rafaj <imq2t@cedric.vabo.cz>
187 + *              - Update patch to 2.4.21
188 + *             Sebastian Strollo <sstrollo@nortelnetworks.com>
189 + *              - Fix "Dead-loop on netdevice imq"-issue
190 + *             Marcel Sebek <sebek64@post.cz>
191 + *              - Update to 2.6.2-rc1
192 + *
193 + *            After some time of inactivity there is a group taking care
194 + *            of IMQ again: http://www.linuximq.net
195 + *
196 + *
197 + *            2004/06/30 - New version of IMQ patch to kernels <=2.6.7
198 + *             including the following changes:
199 + *
200 + *            - Correction of ipv6 support "+"s issue (Hasso Tepper)
201 + *            - Correction of imq_init_devs() issue that resulted in
202 + *            kernel OOPS unloading IMQ as module (Norbert Buchmuller)
203 + *            - Addition of functionality to choose number of IMQ devices
204 + *            during kernel config (Andre Correa)
205 + *            - Addition of functionality to choose how IMQ hooks on
206 + *            PRE and POSTROUTING (after or before NAT) (Andre Correa)
207 + *            - Cosmetic corrections (Norbert Buchmuller) (Andre Correa)
208 + *
209 + *
210 + *             2005/12/16 - IMQ versions between 2.6.7 and 2.6.13 were
211 + *             released with almost no problems. 2.6.14-x was released
212 + *             with some important changes: nfcache was removed; After
213 + *             some weeks of trouble we figured out that some IMQ fields
214 + *             in skb were missing in skbuff.c - skb_clone and copy_skb_header.
215 + *             These functions are correctly patched by this new patch version.
216 + *
217 + *             Thanks for all who helped to figure out all the problems with
218 + *             2.6.14.x: Patrick McHardy, Rune Kock, VeNoMouS, Max CtRiX,
219 + *             Kevin Shanahan, Richard Lucassen, Valery Dachev (hopefully
220 + *             I didn't forget anybody). I apologize again for my lack of time.
221 + *
222 + *
223 + *             2008/06/07 - Changed imq.c to use qdisc_run() instead of
224 + *             qdisc_restart() and moved qdisc_run() to tasklet to avoid
225 + *             recursive locking. (Jussi Kivilinna)
226 + *
227 + *             2008/06/14 - New initialization routines to fix 'rmmod' not
228 + *             working anymore. Used code from ifb.c (Jussi Kivilinna)
229 + *
230 + *
231 + *             More info at: http://www.linuximq.net/ (Andre Correa)
232 + */
233 +
234 +#include <linux/module.h>
235 +#include <linux/kernel.h>
236 +#include <linux/moduleparam.h>
237 +#include <linux/skbuff.h>
238 +#include <linux/netdevice.h>
239 +#include <linux/rtnetlink.h>
240 +#include <linux/if_arp.h>
241 +#include <linux/netfilter.h>
242 +#include <linux/netfilter_ipv4.h>
243 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
244 +       #include <linux/netfilter_ipv6.h>
245 +#endif
246 +#include <linux/imq.h>
247 +#include <net/pkt_sched.h>
248 +#include <net/netfilter/nf_queue.h>
249 +
250 +struct imq_private {
251 +       struct tasklet_struct tasklet;
252 +       int tasklet_pending;
253 +};
254 +
255 +static nf_hookfn imq_nf_hook;
256 +
257 +static struct nf_hook_ops imq_ingress_ipv4 = {
258 +       .hook           = imq_nf_hook,
259 +       .owner          = THIS_MODULE,
260 +       .pf             = PF_INET,
261 +       .hooknum        = NF_INET_PRE_ROUTING,
262 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
263 +       .priority       = NF_IP_PRI_MANGLE + 1
264 +#else
265 +       .priority       = NF_IP_PRI_NAT_DST + 1
266 +#endif
267 +};
268 +
269 +static struct nf_hook_ops imq_egress_ipv4 = {
270 +       .hook           = imq_nf_hook,
271 +       .owner          = THIS_MODULE,
272 +       .pf             = PF_INET,
273 +       .hooknum        = NF_INET_POST_ROUTING,
274 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
275 +       .priority       = NF_IP_PRI_LAST
276 +#else
277 +       .priority       = NF_IP_PRI_NAT_SRC - 1
278 +#endif
279 +};
280 +
281 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
282 +static struct nf_hook_ops imq_ingress_ipv6 = {
283 +       .hook           = imq_nf_hook,
284 +       .owner          = THIS_MODULE,
285 +       .pf             = PF_INET6,
286 +       .hooknum        = NF_INET_PRE_ROUTING,
287 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
288 +       .priority       = NF_IP6_PRI_MANGLE + 1
289 +#else
290 +       .priority       = NF_IP6_PRI_NAT_DST + 1
291 +#endif
292 +};
293 +
294 +static struct nf_hook_ops imq_egress_ipv6 = {
295 +       .hook           = imq_nf_hook,
296 +       .owner          = THIS_MODULE,
297 +       .pf             = PF_INET6,
298 +       .hooknum        = NF_INET_POST_ROUTING,
299 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
300 +       .priority       = NF_IP6_PRI_LAST
301 +#else
302 +       .priority       = NF_IP6_PRI_NAT_SRC - 1
303 +#endif
304 +};
305 +#endif
306 +
307 +#if defined(CONFIG_IMQ_NUM_DEVS)
308 +static unsigned int numdevs = CONFIG_IMQ_NUM_DEVS;
309 +#else
310 +static unsigned int numdevs = IMQ_MAX_DEVS;
311 +#endif
312 +
313 +static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
314 +
315 +static struct net_device_stats *imq_get_stats(struct net_device *dev)
316 +{
317 +       return &dev->stats;
318 +}
319 +
320 +/* called for packets kfree'd in qdiscs at places other than enqueue */
321 +static void imq_skb_destructor(struct sk_buff *skb)
322 +{
323 +       struct nf_queue_entry *entry = skb->nf_queue_entry;
324 +
325 +       if (entry) {
326 +               if (entry->indev)
327 +                       dev_put(entry->indev);
328 +               if (entry->outdev)
329 +                       dev_put(entry->outdev);
330 +               kfree(entry);
331 +       }
332 +}
333 +
334 +static int imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
335 +{
336 +       dev->stats.tx_bytes += skb->len;
337 +       dev->stats.tx_packets++;
338 +
339 +       skb->imq_flags = 0;
340 +       skb->destructor = NULL;
341 +
342 +       dev->trans_start = jiffies;
343 +       nf_reinject(skb->nf_queue_entry, NF_ACCEPT);
344 +       return 0;
345 +}
346 +
347 +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num)
348 +{
349 +       struct net_device *dev;
350 +       struct imq_private *priv;
351 +       struct sk_buff *skb2 = NULL;
352 +       struct Qdisc *q;
353 +       unsigned int index = entry->skb->imq_flags & IMQ_F_IFMASK;
354 +       int ret = -1;
355 +
356 +       if (index > numdevs)
357 +               return -1;
358 +
359 +       /* check for imq device by index from cache */
360 +       dev = imq_devs_cache[index];
361 +       if (!dev) {
362 +               char buf[8];
363 +
364 +               /* get device by name and cache result */
365 +               snprintf(buf, sizeof(buf), "imq%d", index);
366 +               dev = dev_get_by_name(&init_net, buf);
367 +               if (!dev) {
368 +                       /* not found ?!*/
369 +                       BUG();
370 +                       return -1;
371 +               }
372 +
373 +               imq_devs_cache[index] = dev;
374 +       }
375 +
376 +       priv = netdev_priv(dev);
377 +       if (!(dev->flags & IFF_UP)) {
378 +               entry->skb->imq_flags = 0;
379 +               nf_reinject(entry, NF_ACCEPT);
380 +               return 0;
381 +       }
382 +       dev->last_rx = jiffies;
383 +
384 +       if (entry->skb->destructor) {
385 +               skb2 = entry->skb;
386 +               entry->skb = skb_clone(entry->skb, GFP_ATOMIC);
387 +               if (!entry->skb)
388 +                       return -1;
389 +       }
390 +       entry->skb->nf_queue_entry = entry;
391 +
392 +       dev->stats.rx_bytes += entry->skb->len;
393 +       dev->stats.rx_packets++;
394 +
395 +       spin_lock_bh(&dev->queue_lock);
396 +       q = dev->qdisc;
397 +       if (q->enqueue) {
398 +               q->enqueue(skb_get(entry->skb), q);
399 +               if (skb_shared(entry->skb)) {
400 +                       entry->skb->destructor = imq_skb_destructor;
401 +                       kfree_skb(entry->skb);
402 +                       ret = 0;
403 +               }
404 +       }
405 +       if (!test_and_set_bit(1, &priv->tasklet_pending))
406 +               tasklet_schedule(&priv->tasklet);
407 +       spin_unlock_bh(&dev->queue_lock);
408 +
409 +       if (skb2)
410 +               kfree_skb(ret ? entry->skb : skb2);
411 +
412 +       return ret;
413 +}
414 +
415 +static struct nf_queue_handler nfqh = {
416 +       .name  = "imq",
417 +       .outfn = imq_nf_queue,
418 +};
419 +
420 +static void qdisc_run_tasklet(unsigned long arg)
421 +{
422 +       struct net_device *dev = (struct net_device *)arg;
423 +       struct imq_private *priv = netdev_priv(dev);
424 +
425 +       spin_lock(&dev->queue_lock);
426 +       qdisc_run(dev);
427 +       clear_bit(1, &priv->tasklet_pending);
428 +       spin_unlock(&dev->queue_lock);
429 +}
430 +
431 +static unsigned int imq_nf_hook(unsigned int hook, struct sk_buff *pskb,
432 +                               const struct net_device *indev,
433 +                               const struct net_device *outdev,
434 +                               int (*okfn)(struct sk_buff *))
435 +{
436 +       if (pskb->imq_flags & IMQ_F_ENQUEUE)
437 +               return NF_QUEUE;
438 +
439 +       return NF_ACCEPT;
440 +}
441 +
442 +static int imq_close(struct net_device *dev)
443 +{
444 +       struct imq_private *priv = netdev_priv(dev);
445 +
446 +       tasklet_kill(&priv->tasklet);
447 +       netif_stop_queue(dev);
448 +
449 +       return 0;
450 +}
451 +
452 +static int imq_open(struct net_device *dev)
453 +{
454 +       struct imq_private *priv = netdev_priv(dev);
455 +
456 +       tasklet_init(&priv->tasklet, qdisc_run_tasklet, (unsigned long)dev);
457 +       netif_start_queue(dev);
458 +
459 +       return 0;
460 +}
461 +
462 +static void imq_setup(struct net_device *dev)
463 +{
464 +       dev->hard_start_xmit    = imq_dev_xmit;
465 +       dev->open               = imq_open;
466 +       dev->get_stats          = imq_get_stats;
467 +       dev->stop               = imq_close;
468 +       dev->type               = ARPHRD_VOID;
469 +       dev->mtu                = 16000;
470 +       dev->tx_queue_len       = 11000;
471 +       dev->flags              = IFF_NOARP;
472 +}
473 +
474 +static struct rtnl_link_ops imq_link_ops __read_mostly = {
475 +       .kind           = "imq",
476 +       .priv_size      = sizeof(struct imq_private),
477 +       .setup          = imq_setup,
478 +};
479 +
480 +static int __init imq_init_hooks(void)
481 +{
482 +       int err;
483 +
484 +       err = nf_register_queue_handler(PF_INET, &nfqh);
485 +       if (err)
486 +               goto err1;
487 +
488 +       err = nf_register_hook(&imq_ingress_ipv4);
489 +       if (err)
490 +               goto err2;
491 +
492 +       err = nf_register_hook(&imq_egress_ipv4);
493 +       if (err)
494 +               goto err3;
495 +
496 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
497 +       err = nf_register_queue_handler(PF_INET6, &nfqh);
498 +       if (err)
499 +               goto err4;
500 +
501 +       err = nf_register_hook(&imq_ingress_ipv6);
502 +       if (err)
503 +               goto err5;
504 +
505 +       err = nf_register_hook(&imq_egress_ipv6);
506 +       if (err)
507 +               goto err6;
508 +#endif
509 +
510 +       return 0;
511 +
512 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
513 +err6:
514 +       nf_unregister_hook(&imq_ingress_ipv6);
515 +err5:
516 +       nf_unregister_queue_handler(PF_INET6, &nfqh);
517 +err4:
518 +       nf_unregister_hook(&imq_egress_ipv4);
519 +#endif
520 +err3:
521 +       nf_unregister_hook(&imq_ingress_ipv4);
522 +err2:
523 +       nf_unregister_queue_handler(PF_INET, &nfqh);
524 +err1:
525 +       return err;
526 +}
527 +
528 +static int __init imq_init_one(int index)
529 +{
530 +       struct net_device *dev;
531 +       int ret;
532 +
533 +       dev = alloc_netdev(sizeof(struct imq_private), "imq%d", imq_setup);
534 +       if (!dev)
535 +               return -ENOMEM;
536 +
537 +       ret = dev_alloc_name(dev, dev->name);
538 +       if (ret < 0)
539 +               goto fail;
540 +
541 +       dev->rtnl_link_ops = &imq_link_ops;
542 +       ret = register_netdevice(dev);
543 +       if (ret < 0)
544 +               goto fail;
545 +
546 +       return 0;
547 +fail:
548 +       free_netdev(dev);
549 +       return ret;
550 +}
551 +
552 +static int __init imq_init_devs(void)
553 +{
554 +       int err, i;
555 +
556 +       if (!numdevs || numdevs > IMQ_MAX_DEVS) {
557 +               printk(KERN_ERR "IMQ: numdevs has to be betweed 1 and %u\n",
558 +                      IMQ_MAX_DEVS);
559 +               return -EINVAL;
560 +       }
561 +
562 +       rtnl_lock();
563 +       err = __rtnl_link_register(&imq_link_ops);
564 +
565 +       for (i = 0; i < numdevs && !err; i++)
566 +               err = imq_init_one(i);
567 +
568 +       if (err) {
569 +               __rtnl_link_unregister(&imq_link_ops);
570 +               memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
571 +       }
572 +       rtnl_unlock();
573 +
574 +       return err;
575 +}
576 +
577 +static int __init imq_init_module(void)
578 +{
579 +       int err;
580 +
581 +       err = imq_init_devs();
582 +       if (err) {
583 +               printk(KERN_ERR "IMQ: Error trying imq_init_devs(net)\n");
584 +               return err;
585 +       }
586 +
587 +       err = imq_init_hooks();
588 +       if (err) {
589 +               printk(KERN_ERR "IMQ: Error trying imq_init_hooks()\n");
590 +               rtnl_link_unregister(&imq_link_ops);
591 +               memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
592 +               return err;
593 +       }
594 +
595 +       printk(KERN_INFO "IMQ driver loaded successfully.\n");
596 +
597 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
598 +       printk(KERN_INFO "\tHooking IMQ before NAT on PREROUTING.\n");
599 +#else
600 +       printk(KERN_INFO "\tHooking IMQ after NAT on PREROUTING.\n");
601 +#endif
602 +#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
603 +       printk(KERN_INFO "\tHooking IMQ before NAT on POSTROUTING.\n");
604 +#else
605 +       printk(KERN_INFO "\tHooking IMQ after NAT on POSTROUTING.\n");
606 +#endif
607 +
608 +       return 0;
609 +}
610 +
611 +static void __exit imq_unhook(void)
612 +{
613 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
614 +       nf_unregister_hook(&imq_ingress_ipv6);
615 +       nf_unregister_hook(&imq_egress_ipv6);
616 +       nf_unregister_queue_handler(PF_INET6, &nfqh);
617 +#endif
618 +       nf_unregister_hook(&imq_ingress_ipv4);
619 +       nf_unregister_hook(&imq_egress_ipv4);
620 +       nf_unregister_queue_handler(PF_INET, &nfqh);
621 +}
622 +
623 +static void __exit imq_cleanup_devs(void)
624 +{
625 +       rtnl_link_unregister(&imq_link_ops);
626 +       memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
627 +}
628 +
629 +static void __exit imq_exit_module(void)
630 +{
631 +       imq_unhook();
632 +       imq_cleanup_devs();
633 +       printk(KERN_INFO "IMQ driver unloaded successfully.\n");
634 +}
635 +
636 +module_init(imq_init_module);
637 +module_exit(imq_exit_module);
638 +
639 +module_param(numdevs, int, 0);
640 +MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will "
641 +                       "be created)");
642 +MODULE_AUTHOR("http://www.linuximq.net");
643 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See "
644 +                       "http://www.linuximq.net/ for more information.");
645 +MODULE_LICENSE("GPL");
646 +MODULE_ALIAS_RTNL_LINK("imq");
647 +
648 --- /dev/null
649 +++ b/include/linux/imq.h
650 @@ -0,0 +1,9 @@
651 +#ifndef _IMQ_H
652 +#define _IMQ_H
653 +
654 +#define IMQ_MAX_DEVS   16
655 +
656 +#define IMQ_F_IFMASK   0x7f
657 +#define IMQ_F_ENQUEUE  0x80
658 +
659 +#endif /* _IMQ_H */
660 --- /dev/null
661 +++ b/include/linux/netfilter_ipv4/ipt_IMQ.h
662 @@ -0,0 +1,8 @@
663 +#ifndef _IPT_IMQ_H
664 +#define _IPT_IMQ_H
665 +
666 +struct ipt_imq_info {
667 +       unsigned int todev;     /* target imq device */
668 +};
669 +
670 +#endif /* _IPT_IMQ_H */
671 --- /dev/null
672 +++ b/include/linux/netfilter_ipv6/ip6t_IMQ.h
673 @@ -0,0 +1,8 @@
674 +#ifndef _IP6T_IMQ_H
675 +#define _IP6T_IMQ_H
676 +
677 +struct ip6t_imq_info {
678 +       unsigned int todev;     /* target imq device */
679 +};
680 +
681 +#endif /* _IP6T_IMQ_H */
682 --- a/include/linux/skbuff.h
683 +++ b/include/linux/skbuff.h
684 @@ -300,6 +300,10 @@
685         struct nf_conntrack     *nfct;
686         struct sk_buff          *nfct_reasm;
687  #endif
688 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
689 +       unsigned char                   imq_flags;
690 +       struct nf_queue_entry   *nf_queue_entry;
691 +#endif
692  #ifdef CONFIG_BRIDGE_NETFILTER
693         struct nf_bridge_info   *nf_bridge;
694  #endif
695 @@ -1633,6 +1637,10 @@
696         dst->nfct_reasm = src->nfct_reasm;
697         nf_conntrack_get_reasm(src->nfct_reasm);
698  #endif
699 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
700 +       dst->imq_flags = src->imq_flags;
701 +       dst->nf_queue_entry = src->nf_queue_entry;
702 +#endif
703  #ifdef CONFIG_BRIDGE_NETFILTER
704         dst->nf_bridge  = src->nf_bridge;
705         nf_bridge_get(src->nf_bridge);
706 --- a/net/core/dev.c
707 +++ b/net/core/dev.c
708 @@ -95,6 +95,9 @@
709  #include <net/net_namespace.h>
710  #include <net/sock.h>
711  #include <linux/rtnetlink.h>
712 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
713 +#include <linux/imq.h>
714 +#endif
715  #include <linux/proc_fs.h>
716  #include <linux/seq_file.h>
717  #include <linux/stat.h>
718 @@ -1545,7 +1548,11 @@
719  int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
720  {
721         if (likely(!skb->next)) {
722 -               if (!list_empty(&ptype_all))
723 +               if (!list_empty(&ptype_all)
724 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
725 +                   && !(skb->imq_flags & IMQ_F_ENQUEUE)
726 +#endif
727 +                   )
728                         dev_queue_xmit_nit(skb, dev);
729  
730                 if (netif_needs_gso(dev, skb)) {
731 --- a/net/ipv4/netfilter/Kconfig
732 +++ b/net/ipv4/netfilter/Kconfig
733 @@ -145,6 +145,17 @@
734  
735           To compile it as a module, choose M here.  If unsure, say N.
736  
737 +config IP_NF_TARGET_IMQ
738 +       tristate "IMQ target support"
739 +       depends on IP_NF_MANGLE
740 +       help
741 +         This option adds a `IMQ' target which is used to specify if and
742 +         to which IMQ device packets should get enqueued/dequeued.
743 +
744 +        For more information visit: http://www.linuximq.net/
745 +
746 +         To compile it as a module, choose M here.  If unsure, say N.
747 +
748  config IP_NF_TARGET_REJECT
749         tristate "REJECT target support"
750         depends on IP_NF_FILTER
751 --- a/net/ipv4/netfilter/Makefile
752 +++ b/net/ipv4/netfilter/Makefile
753 @@ -58,6 +58,7 @@
754  obj-$(CONFIG_IP_NF_TARGET_CLUSTERIP) += ipt_CLUSTERIP.o
755  obj-$(CONFIG_IP_NF_TARGET_ECN) += ipt_ECN.o
756  obj-$(CONFIG_IP_NF_TARGET_LOG) += ipt_LOG.o
757 +obj-$(CONFIG_IP_NF_TARGET_IMQ) += ipt_IMQ.o
758  obj-$(CONFIG_IP_NF_TARGET_MASQUERADE) += ipt_MASQUERADE.o
759  obj-$(CONFIG_IP_NF_TARGET_NETMAP) += ipt_NETMAP.o
760  obj-$(CONFIG_IP_NF_TARGET_REDIRECT) += ipt_REDIRECT.o
761 --- /dev/null
762 +++ b/net/ipv4/netfilter/ipt_IMQ.c
763 @@ -0,0 +1,69 @@
764 +/*
765 + * This target marks packets to be enqueued to an imq device
766 + */
767 +#include <linux/module.h>
768 +#include <linux/skbuff.h>
769 +#include <linux/netfilter_ipv4/ip_tables.h>
770 +#include <linux/netfilter_ipv4/ipt_IMQ.h>
771 +#include <linux/imq.h>
772 +
773 +static unsigned int imq_target(struct sk_buff *pskb,
774 +                              const struct net_device *in,
775 +                              const struct net_device *out,
776 +                              unsigned int hooknum,
777 +                              const struct xt_target *target,
778 +                              const void *targinfo)
779 +{
780 +       struct ipt_imq_info *mr = (struct ipt_imq_info *)targinfo;
781 +
782 +       pskb->imq_flags = mr->todev | IMQ_F_ENQUEUE;
783 +
784 +       return XT_CONTINUE;
785 +}
786 +
787 +static bool imq_checkentry(const char *tablename,
788 +                         const void *e,
789 +                         const struct xt_target *target,
790 +                         void *targinfo,
791 +                         unsigned int hook_mask)
792 +{
793 +       struct ipt_imq_info *mr;
794 +
795 +       mr = (struct ipt_imq_info *)targinfo;
796 +
797 +       if (mr->todev > IMQ_MAX_DEVS) {
798 +               printk(KERN_WARNING
799 +                      "IMQ: invalid device specified, highest is %u\n",
800 +                      IMQ_MAX_DEVS);
801 +               return 0;
802 +       }
803 +
804 +       return 1;
805 +}
806 +
807 +static struct xt_target ipt_imq_reg = {
808 +       .name           = "IMQ",
809 +       .family         = AF_INET,
810 +       .target         = imq_target,
811 +       .targetsize     = sizeof(struct ipt_imq_info),
812 +       .checkentry     = imq_checkentry,
813 +       .me             = THIS_MODULE,
814 +       .table          = "mangle"
815 +};
816 +
817 +static int __init init(void)
818 +{
819 +       return xt_register_target(&ipt_imq_reg);
820 +}
821 +
822 +static void __exit fini(void)
823 +{
824 +       xt_unregister_target(&ipt_imq_reg);
825 +}
826 +
827 +module_init(init);
828 +module_exit(fini);
829 +
830 +MODULE_AUTHOR("http://www.linuximq.net");
831 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
832 +MODULE_LICENSE("GPL");
833 --- a/net/ipv6/netfilter/Kconfig
834 +++ b/net/ipv6/netfilter/Kconfig
835 @@ -179,6 +179,15 @@
836  
837           To compile it as a module, choose M here.  If unsure, say N.
838  
839 +config IP6_NF_TARGET_IMQ
840 +       tristate "IMQ target support"
841 +       depends on IP6_NF_MANGLE
842 +       help
843 +          This option adds a `IMQ' target which is used to specify if and
844 +          to which imq device packets should get enqueued/dequeued.
845 +
846 +          To compile it as a module, choose M here.  If unsure, say N.
847 +
848  config IP6_NF_TARGET_HL
849         tristate  'HL (hoplimit) target support'
850         depends on IP6_NF_MANGLE
851 --- a/net/ipv6/netfilter/Makefile
852 +++ b/net/ipv6/netfilter/Makefile
853 @@ -6,6 +6,7 @@
854  obj-$(CONFIG_IP6_NF_IPTABLES) += ip6_tables.o
855  obj-$(CONFIG_IP6_NF_FILTER) += ip6table_filter.o
856  obj-$(CONFIG_IP6_NF_MANGLE) += ip6table_mangle.o
857 +obj-$(CONFIG_IP6_NF_TARGET_IMQ) += ip6t_IMQ.o
858  obj-$(CONFIG_IP6_NF_QUEUE) += ip6_queue.o
859  obj-$(CONFIG_IP6_NF_RAW) += ip6table_raw.o
860  
861 --- /dev/null
862 +++ b/net/ipv6/netfilter/ip6t_IMQ.c
863 @@ -0,0 +1,69 @@
864 +/*
865 + * This target marks packets to be enqueued to an imq device
866 + */
867 +#include <linux/module.h>
868 +#include <linux/skbuff.h>
869 +#include <linux/netfilter_ipv6/ip6_tables.h>
870 +#include <linux/netfilter_ipv6/ip6t_IMQ.h>
871 +#include <linux/imq.h>
872 +
873 +static unsigned int imq_target(struct sk_buff *pskb,
874 +                              const struct net_device *in,
875 +                              const struct net_device *out,
876 +                              unsigned int hooknum,
877 +                              const struct xt_target *target,
878 +                              const void *targinfo)
879 +{
880 +       struct ip6t_imq_info *mr = (struct ip6t_imq_info *)targinfo;
881 +
882 +       pskb->imq_flags = mr->todev | IMQ_F_ENQUEUE;
883 +
884 +       return XT_CONTINUE;
885 +}
886 +
887 +static bool imq_checkentry(const char *tablename,
888 +                         const void *entry,
889 +                         const struct xt_target *target,
890 +                         void *targinfo,
891 +                         unsigned int hook_mask)
892 +{
893 +       struct ip6t_imq_info *mr;
894 +
895 +       mr = (struct ip6t_imq_info *)targinfo;
896 +
897 +       if (mr->todev > IMQ_MAX_DEVS) {
898 +               printk(KERN_WARNING
899 +                      "IMQ: invalid device specified, highest is %u\n",
900 +                      IMQ_MAX_DEVS);
901 +               return 0;
902 +       }
903 +
904 +       return 1;
905 +}
906 +
907 +static struct xt_target ip6t_imq_reg = {
908 +       .name           = "IMQ",
909 +       .family         = AF_INET6,
910 +       .target         = imq_target,
911 +       .targetsize     = sizeof(struct ip6t_imq_info),
912 +       .table          = "mangle",
913 +       .checkentry     = imq_checkentry,
914 +       .me             = THIS_MODULE
915 +};
916 +
917 +static int __init init(void)
918 +{
919 +       return xt_register_target(&ip6t_imq_reg);
920 +}
921 +
922 +static void __exit fini(void)
923 +{
924 +       xt_unregister_target(&ip6t_imq_reg);
925 +}
926 +
927 +module_init(init);
928 +module_exit(fini);
929 +
930 +MODULE_AUTHOR("http://www.linuximq.net");
931 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
932 +MODULE_LICENSE("GPL");
933 --- a/net/sched/sch_generic.c
934 +++ b/net/sched/sch_generic.c
935 @@ -203,6 +203,7 @@
936  
937         clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
938  }
939 +EXPORT_SYMBOL(__qdisc_run);
940  
941  static void dev_watchdog(unsigned long arg)
942  {