1 diff -uNr --exclude='*~' linux-2.6.27.21/drivers/net/imq.c linux-2.6.27.21-imq/drivers/net/imq.c
2 --- linux-2.6.27.21/drivers/net/imq.c 1970-01-01 02:00:00.000000000 +0200
3 +++ linux-2.6.27.21-imq/drivers/net/imq.c 2009-04-20 23:52:22.712017559 +0300
6 + * Pseudo-driver for the intermediate queue device.
8 + * This program is free software; you can redistribute it and/or
9 + * modify it under the terms of the GNU General Public License
10 + * as published by the Free Software Foundation; either version
11 + * 2 of the License, or (at your option) any later version.
13 + * Authors: Patrick McHardy, <kaber@trash.net>
15 + * The first version was written by Martin Devera, <devik@cdi.cz>
17 + * Credits: Jan Rafaj <imq2t@cedric.vabo.cz>
18 + * - Update patch to 2.4.21
19 + * Sebastian Strollo <sstrollo@nortelnetworks.com>
20 + * - Fix "Dead-loop on netdevice imq"-issue
21 + * Marcel Sebek <sebek64@post.cz>
22 + * - Update to 2.6.2-rc1
24 + * After some time of inactivity there is a group taking care
25 + * of IMQ again: http://www.linuximq.net
28 + * 2004/06/30 - New version of IMQ patch to kernels <=2.6.7
29 + * including the following changes:
31 + * - Correction of ipv6 support "+"s issue (Hasso Tepper)
32 + * - Correction of imq_init_devs() issue that resulted in
33 + * kernel OOPS unloading IMQ as module (Norbert Buchmuller)
34 + * - Addition of functionality to choose number of IMQ devices
35 + * during kernel config (Andre Correa)
36 + * - Addition of functionality to choose how IMQ hooks on
37 + * PRE and POSTROUTING (after or before NAT) (Andre Correa)
38 + * - Cosmetic corrections (Norbert Buchmuller) (Andre Correa)
41 + * 2005/12/16 - IMQ versions between 2.6.7 and 2.6.13 were
42 + * released with almost no problems. 2.6.14-x was released
43 + * with some important changes: nfcache was removed; After
44 + * some weeks of trouble we figured out that some IMQ fields
45 + * in skb were missing in skbuff.c - skb_clone and copy_skb_header.
46 + * These functions are correctly patched by this new patch version.
48 + * Thanks for all who helped to figure out all the problems with
49 + * 2.6.14.x: Patrick McHardy, Rune Kock, VeNoMouS, Max CtRiX,
50 + * Kevin Shanahan, Richard Lucassen, Valery Dachev (hopefully
51 + * I didn't forget anybody). I apologize again for my lack of time.
54 + * 2008/06/17 - 2.6.25 - Changed imq.c to use qdisc_run() instead
55 + * of qdisc_restart() and moved qdisc_run() to tasklet to avoid
56 + * recursive locking. New initialization routines to fix 'rmmod' not
57 + * working anymore. Used code from ifb.c. (Jussi Kivilinna)
59 + * 2008/08/06 - 2.6.26 - (JK)
60 + * - Replaced tasklet with 'netif_schedule()'.
61 + * - Cleaned up and added comments for imq_nf_queue().
64 + * - Add skb_save_cb/skb_restore_cb helper functions for backuping
65 + * control buffer. This is needed because qdisc-layer on kernels
66 + * 2.6.27 and newer overwrite control buffer. (Jussi Kivilinna)
67 + * - Add better locking for IMQ device. Hopefully this will solve
68 + * SMP issues. (Jussi Kivilinna)
71 + * 2009/04/20 - (Jussi Kivilinna)
72 + * - Fix rmmod not working
73 + * - Use netdevice feature flags to avoid extra packet handling
74 + * by core networking layer and possibly increase performance.
76 + * Also, many thanks to pablo Sebastian Greco for making the initial
77 + * patch and to those who helped the testing.
79 + * More info at: http://www.linuximq.net/ (Andre Correa)
82 +#include <linux/module.h>
83 +#include <linux/kernel.h>
84 +#include <linux/moduleparam.h>
85 +#include <linux/list.h>
86 +#include <linux/skbuff.h>
87 +#include <linux/netdevice.h>
88 +#include <linux/etherdevice.h>
89 +#include <linux/rtnetlink.h>
90 +#include <linux/if_arp.h>
91 +#include <linux/netfilter.h>
92 +#include <linux/netfilter_ipv4.h>
93 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
94 + #include <linux/netfilter_ipv6.h>
96 +#include <linux/imq.h>
97 +#include <net/pkt_sched.h>
98 +#include <net/netfilter/nf_queue.h>
100 +static nf_hookfn imq_nf_hook;
102 +static struct nf_hook_ops imq_ingress_ipv4 = {
103 + .hook = imq_nf_hook,
104 + .owner = THIS_MODULE,
106 + .hooknum = NF_INET_PRE_ROUTING,
107 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
108 + .priority = NF_IP_PRI_MANGLE + 1
110 + .priority = NF_IP_PRI_NAT_DST + 1
114 +static struct nf_hook_ops imq_egress_ipv4 = {
115 + .hook = imq_nf_hook,
116 + .owner = THIS_MODULE,
118 + .hooknum = NF_INET_POST_ROUTING,
119 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
120 + .priority = NF_IP_PRI_LAST
122 + .priority = NF_IP_PRI_NAT_SRC - 1
126 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
127 +static struct nf_hook_ops imq_ingress_ipv6 = {
128 + .hook = imq_nf_hook,
129 + .owner = THIS_MODULE,
131 + .hooknum = NF_INET_PRE_ROUTING,
132 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
133 + .priority = NF_IP6_PRI_MANGLE + 1
135 + .priority = NF_IP6_PRI_NAT_DST + 1
139 +static struct nf_hook_ops imq_egress_ipv6 = {
140 + .hook = imq_nf_hook,
141 + .owner = THIS_MODULE,
143 + .hooknum = NF_INET_POST_ROUTING,
144 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
145 + .priority = NF_IP6_PRI_LAST
147 + .priority = NF_IP6_PRI_NAT_SRC - 1
152 +#if defined(CONFIG_IMQ_NUM_DEVS)
153 +static unsigned int numdevs = CONFIG_IMQ_NUM_DEVS;
155 +static unsigned int numdevs = IMQ_MAX_DEVS;
158 +static DEFINE_SPINLOCK(imq_nf_queue_lock);
160 +static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
163 +static struct net_device_stats *imq_get_stats(struct net_device *dev)
165 + return &dev->stats;
168 +/* called for packets kfree'd in qdiscs at places other than enqueue */
169 +static void imq_skb_destructor(struct sk_buff *skb)
171 + struct nf_queue_entry *entry = skb->nf_queue_entry;
174 + nf_queue_entry_release_refs(entry);
178 + skb_restore_cb(skb); /* kfree backup */
181 +static void imq_nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
185 + if (!entry->next_outfn) {
186 + spin_lock_bh(&imq_nf_queue_lock);
187 + nf_reinject(entry, verdict);
188 + spin_unlock_bh(&imq_nf_queue_lock);
193 + local_bh_disable();
194 + status = entry->next_outfn(entry, entry->next_queuenum);
197 + nf_queue_entry_release_refs(entry);
198 + kfree_skb(entry->skb);
205 +static int imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
207 + dev->stats.tx_bytes += skb->len;
208 + dev->stats.tx_packets++;
210 + skb->imq_flags = 0;
211 + skb->destructor = NULL;
213 + skb_restore_cb(skb); /* restore skb->cb */
215 + dev->trans_start = jiffies;
216 + imq_nf_reinject(skb->nf_queue_entry, NF_ACCEPT);
220 +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num)
222 + struct net_device *dev;
223 + struct sk_buff *skb_orig, *skb, *skb_shared;
225 + struct netdev_queue *txq;
227 + int retval = -EINVAL;
229 + index = entry->skb->imq_flags & IMQ_F_IFMASK;
230 + if (unlikely(index > numdevs - 1)) {
231 + if (net_ratelimit())
232 + printk(KERN_WARNING
233 + "IMQ: invalid device specified, highest is %u\n",
239 + /* check for imq device by index from cache */
240 + dev = imq_devs_cache[index];
241 + if (unlikely(!dev)) {
244 + /* get device by name and cache result */
245 + snprintf(buf, sizeof(buf), "imq%d", index);
246 + dev = dev_get_by_name(&init_net, buf);
254 + imq_devs_cache[index] = dev;
258 + if (unlikely(!(dev->flags & IFF_UP))) {
259 + entry->skb->imq_flags = 0;
260 + imq_nf_reinject(entry, NF_ACCEPT);
264 + dev->last_rx = jiffies;
269 + /* skb has owner? => make clone */
270 + if (unlikely(skb->destructor)) {
272 + skb = skb_clone(skb, GFP_ATOMIC);
280 + skb->nf_queue_entry = entry;
282 + dev->stats.rx_bytes += skb->len;
283 + dev->stats.rx_packets++;
285 + txq = dev_pick_tx(dev, skb);
287 + q = rcu_dereference(txq->qdisc);
288 + if (unlikely(!q->enqueue))
289 + goto packet_not_eaten_by_imq_dev;
291 + spin_lock_bh(qdisc_lock(q));
293 + users = atomic_read(&skb->users);
295 + skb_shared = skb_get(skb); /* increase reference count by one */
296 + skb_save_cb(skb_shared); /* backup skb->cb, as qdisc layer will
298 + qdisc_enqueue_root(skb_shared, q); /* might kfree_skb */
300 + if (likely(atomic_read(&skb_shared->users) == users + 1)) {
301 + kfree_skb(skb_shared); /* decrease reference count by one */
303 + skb->destructor = &imq_skb_destructor;
307 + kfree_skb(skb_orig); /* free original */
309 + spin_unlock_bh(qdisc_lock(q));
311 + /* schedule qdisc dequeue */
312 + __netif_schedule(q);
317 + skb_restore_cb(skb_shared); /* restore skb->cb */
318 + /* qdisc dropped packet and decreased skb reference count of
319 + * skb, so we don't really want to and try refree as that would
320 + * actually destroy the skb. */
321 + spin_unlock_bh(qdisc_lock(q));
322 + goto packet_not_eaten_by_imq_dev;
325 +packet_not_eaten_by_imq_dev:
326 + /* cloned? restore original */
329 + entry->skb = skb_orig;
336 +static struct nf_queue_handler nfqh = {
338 + .outfn = imq_nf_queue,
341 +static unsigned int imq_nf_hook(unsigned int hook, struct sk_buff *pskb,
342 + const struct net_device *indev,
343 + const struct net_device *outdev,
344 + int (*okfn)(struct sk_buff *))
346 + if (pskb->imq_flags & IMQ_F_ENQUEUE)
352 +static int imq_close(struct net_device *dev)
354 + netif_stop_queue(dev);
358 +static int imq_open(struct net_device *dev)
360 + netif_start_queue(dev);
364 +static void imq_setup(struct net_device *dev)
366 + dev->hard_start_xmit = imq_dev_xmit;
367 + dev->open = imq_open;
368 + dev->get_stats = imq_get_stats;
369 + dev->stop = imq_close;
370 + dev->type = ARPHRD_VOID;
372 + dev->tx_queue_len = 11000;
373 + dev->flags = IFF_NOARP;
374 + dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
375 + NETIF_F_GSO | NETIF_F_HW_CSUM |
379 +static int imq_validate(struct nlattr *tb[], struct nlattr *data[])
383 + if (tb[IFLA_ADDRESS]) {
384 + if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
388 + if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
389 + ret = -EADDRNOTAVAIL;
395 + printk(KERN_WARNING "IMQ: imq_validate failed (%d)\n", ret);
399 +static struct rtnl_link_ops imq_link_ops __read_mostly = {
402 + .setup = imq_setup,
403 + .validate = imq_validate,
406 +static int __init imq_init_hooks(void)
410 + nf_register_queue_imq_handler(&nfqh);
412 + err = nf_register_hook(&imq_ingress_ipv4);
416 + err = nf_register_hook(&imq_egress_ipv4);
420 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
421 + err = nf_register_hook(&imq_ingress_ipv6);
425 + err = nf_register_hook(&imq_egress_ipv6);
432 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
434 + nf_unregister_hook(&imq_ingress_ipv6);
436 + nf_unregister_hook(&imq_egress_ipv4);
439 + nf_unregister_hook(&imq_ingress_ipv4);
441 + nf_unregister_queue_imq_handler();
445 +static int __init imq_init_one(int index)
447 + struct net_device *dev;
450 + dev = alloc_netdev(0, "imq%d", imq_setup);
454 + ret = dev_alloc_name(dev, dev->name);
458 + dev->rtnl_link_ops = &imq_link_ops;
459 + ret = register_netdevice(dev);
469 +static int __init imq_init_devs(void)
473 + if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) {
474 + printk(KERN_ERR "IMQ: numdevs has to be betweed 1 and %u\n",
480 + err = __rtnl_link_register(&imq_link_ops);
482 + for (i = 0; i < numdevs && !err; i++)
483 + err = imq_init_one(i);
486 + __rtnl_link_unregister(&imq_link_ops);
487 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
494 +static int __init imq_init_module(void)
498 +#if defined(CONFIG_IMQ_NUM_DEVS)
499 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS > 16);
500 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS < 2);
501 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS - 1 > IMQ_F_IFMASK);
504 + err = imq_init_devs();
506 + printk(KERN_ERR "IMQ: Error trying imq_init_devs(net)\n");
510 + err = imq_init_hooks();
512 + printk(KERN_ERR "IMQ: Error trying imq_init_hooks()\n");
513 + rtnl_link_unregister(&imq_link_ops);
514 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
518 + printk(KERN_INFO "IMQ driver loaded successfully.\n");
520 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
521 + printk(KERN_INFO "\tHooking IMQ before NAT on PREROUTING.\n");
523 + printk(KERN_INFO "\tHooking IMQ after NAT on PREROUTING.\n");
525 +#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
526 + printk(KERN_INFO "\tHooking IMQ before NAT on POSTROUTING.\n");
528 + printk(KERN_INFO "\tHooking IMQ after NAT on POSTROUTING.\n");
534 +static void __exit imq_unhook(void)
536 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
537 + nf_unregister_hook(&imq_ingress_ipv6);
538 + nf_unregister_hook(&imq_egress_ipv6);
540 + nf_unregister_hook(&imq_ingress_ipv4);
541 + nf_unregister_hook(&imq_egress_ipv4);
543 + nf_unregister_queue_imq_handler();
546 +static void __exit imq_cleanup_devs(void)
548 + rtnl_link_unregister(&imq_link_ops);
549 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
552 +static void __exit imq_exit_module(void)
555 + imq_cleanup_devs();
556 + printk(KERN_INFO "IMQ driver unloaded successfully.\n");
559 +module_init(imq_init_module);
560 +module_exit(imq_exit_module);
562 +module_param(numdevs, int, 0);
563 +MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will "
565 +MODULE_AUTHOR("http://www.linuximq.net");
566 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See "
567 + "http://www.linuximq.net/ for more information.");
568 +MODULE_LICENSE("GPL");
569 +MODULE_ALIAS_RTNL_LINK("imq");
571 diff -uNr --exclude='*~' linux-2.6.27.21/drivers/net/Kconfig linux-2.6.27.21-imq/drivers/net/Kconfig
572 --- linux-2.6.27.21/drivers/net/Kconfig 2009-03-24 00:04:09.000000000 +0200
573 +++ linux-2.6.27.21-imq/drivers/net/Kconfig 2009-04-20 23:48:35.702017146 +0300
574 @@ -109,6 +109,129 @@
575 To compile this driver as a module, choose M here: the module
576 will be called eql. If unsure, say N.
579 + tristate "IMQ (intermediate queueing device) support"
580 + depends on NETDEVICES && NETFILTER
582 + The IMQ device(s) is used as placeholder for QoS queueing
583 + disciplines. Every packet entering/leaving the IP stack can be
584 + directed through the IMQ device where it's enqueued/dequeued to the
585 + attached qdisc. This allows you to treat network devices as classes
586 + and distribute bandwidth among them. Iptables is used to specify
587 + through which IMQ device, if any, packets travel.
589 + More information at: http://www.linuximq.net/
591 + To compile this driver as a module, choose M here: the module
592 + will be called imq. If unsure, say N.
595 + prompt "IMQ behavior (PRE/POSTROUTING)"
597 + default IMQ_BEHAVIOR_AB
600 + This settings defines how IMQ behaves in respect to its
601 + hooking in PREROUTING and POSTROUTING.
603 + IMQ can work in any of the following ways:
605 + PREROUTING | POSTROUTING
606 + -----------------|-------------------
607 + #1 After NAT | After NAT
608 + #2 After NAT | Before NAT
609 + #3 Before NAT | After NAT
610 + #4 Before NAT | Before NAT
612 + The default behavior is to hook before NAT on PREROUTING
613 + and after NAT on POSTROUTING (#3).
615 + This settings are specially usefull when trying to use IMQ
616 + to shape NATed clients.
618 + More information can be found at: www.linuximq.net
620 + If not sure leave the default settings alone.
622 +config IMQ_BEHAVIOR_AA
625 + This settings defines how IMQ behaves in respect to its
626 + hooking in PREROUTING and POSTROUTING.
628 + Choosing this option will make IMQ hook like this:
630 + PREROUTING: After NAT
631 + POSTROUTING: After NAT
633 + More information can be found at: www.linuximq.net
635 + If not sure leave the default settings alone.
637 +config IMQ_BEHAVIOR_AB
640 + This settings defines how IMQ behaves in respect to its
641 + hooking in PREROUTING and POSTROUTING.
643 + Choosing this option will make IMQ hook like this:
645 + PREROUTING: After NAT
646 + POSTROUTING: Before NAT
648 + More information can be found at: www.linuximq.net
650 + If not sure leave the default settings alone.
652 +config IMQ_BEHAVIOR_BA
655 + This settings defines how IMQ behaves in respect to its
656 + hooking in PREROUTING and POSTROUTING.
658 + Choosing this option will make IMQ hook like this:
660 + PREROUTING: Before NAT
661 + POSTROUTING: After NAT
663 + More information can be found at: www.linuximq.net
665 + If not sure leave the default settings alone.
667 +config IMQ_BEHAVIOR_BB
670 + This settings defines how IMQ behaves in respect to its
671 + hooking in PREROUTING and POSTROUTING.
673 + Choosing this option will make IMQ hook like this:
675 + PREROUTING: Before NAT
676 + POSTROUTING: Before NAT
678 + More information can be found at: www.linuximq.net
680 + If not sure leave the default settings alone.
686 + int "Number of IMQ devices"
692 + This settings defines how many IMQ devices will be
695 + The default value is 16.
697 + More information can be found at: www.linuximq.net
699 + If not sure leave the default settings alone.
702 tristate "Universal TUN/TAP device driver support"
704 diff -uNr --exclude='*~' linux-2.6.27.21/drivers/net/Makefile linux-2.6.27.21-imq/drivers/net/Makefile
705 --- linux-2.6.27.21/drivers/net/Makefile 2009-03-24 00:04:09.000000000 +0200
706 +++ linux-2.6.27.21-imq/drivers/net/Makefile 2009-04-20 23:48:35.702017146 +0300
708 obj-$(CONFIG_XEN_NETDEV_FRONTEND) += xen-netfront.o
710 obj-$(CONFIG_DUMMY) += dummy.o
711 +obj-$(CONFIG_IMQ) += imq.o
712 obj-$(CONFIG_IFB) += ifb.o
713 obj-$(CONFIG_MACVLAN) += macvlan.o
714 obj-$(CONFIG_DE600) += de600.o
715 diff -uNr --exclude='*~' linux-2.6.27.21/include/linux/imq.h linux-2.6.27.21-imq/include/linux/imq.h
716 --- linux-2.6.27.21/include/linux/imq.h 1970-01-01 02:00:00.000000000 +0200
717 +++ linux-2.6.27.21-imq/include/linux/imq.h 2009-04-20 23:48:35.702017146 +0300
722 +/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
723 +#define IMQ_F_BITS 5
725 +#define IMQ_F_IFMASK 0x0f
726 +#define IMQ_F_ENQUEUE 0x10
728 +#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1)
732 diff -uNr --exclude='*~' linux-2.6.27.21/include/linux/netdevice.h linux-2.6.27.21-imq/include/linux/netdevice.h
733 --- linux-2.6.27.21/include/linux/netdevice.h 2009-03-24 00:04:09.000000000 +0200
734 +++ linux-2.6.27.21-imq/include/linux/netdevice.h 2009-04-20 23:48:35.703017245 +0300
736 extern int dev_open(struct net_device *dev);
737 extern int dev_close(struct net_device *dev);
738 extern void dev_disable_lro(struct net_device *dev);
739 +extern struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb);
740 extern int dev_queue_xmit(struct sk_buff *skb);
741 extern int register_netdevice(struct net_device *dev);
742 extern void unregister_netdevice(struct net_device *dev);
743 diff -uNr --exclude='*~' linux-2.6.27.21/include/linux/netfilter/xt_IMQ.h linux-2.6.27.21-imq/include/linux/netfilter/xt_IMQ.h
744 --- linux-2.6.27.21/include/linux/netfilter/xt_IMQ.h 1970-01-01 02:00:00.000000000 +0200
745 +++ linux-2.6.27.21-imq/include/linux/netfilter/xt_IMQ.h 2009-04-20 23:48:35.703017245 +0300
750 +struct xt_imq_info {
751 + unsigned int todev; /* target imq device */
754 +#endif /* _XT_IMQ_H */
756 diff -uNr --exclude='*~' linux-2.6.27.21/include/linux/netfilter_ipv4/ipt_IMQ.h linux-2.6.27.21-imq/include/linux/netfilter_ipv4/ipt_IMQ.h
757 --- linux-2.6.27.21/include/linux/netfilter_ipv4/ipt_IMQ.h 1970-01-01 02:00:00.000000000 +0200
758 +++ linux-2.6.27.21-imq/include/linux/netfilter_ipv4/ipt_IMQ.h 2009-04-20 23:48:35.703017245 +0300
763 +/* Backwards compatibility for old userspace */
764 +#include <linux/netfilter/xt_IMQ.h>
766 +#define ipt_imq_info xt_imq_info
768 +#endif /* _IPT_IMQ_H */
770 diff -uNr --exclude='*~' linux-2.6.27.21/include/linux/netfilter_ipv6/ip6t_IMQ.h linux-2.6.27.21-imq/include/linux/netfilter_ipv6/ip6t_IMQ.h
771 --- linux-2.6.27.21/include/linux/netfilter_ipv6/ip6t_IMQ.h 1970-01-01 02:00:00.000000000 +0200
772 +++ linux-2.6.27.21-imq/include/linux/netfilter_ipv6/ip6t_IMQ.h 2009-04-20 23:48:35.704017144 +0300
777 +/* Backwards compatibility for old userspace */
778 +#include <linux/netfilter/xt_IMQ.h>
780 +#define ip6t_imq_info xt_imq_info
782 +#endif /* _IP6T_IMQ_H */
784 diff -uNr --exclude='*~' linux-2.6.27.21/include/linux/skbuff.h linux-2.6.27.21-imq/include/linux/skbuff.h
785 --- linux-2.6.27.21/include/linux/skbuff.h 2009-03-24 00:04:09.000000000 +0200
786 +++ linux-2.6.27.21-imq/include/linux/skbuff.h 2009-04-20 23:48:35.704017144 +0300
788 #include <linux/rcupdate.h>
789 #include <linux/dmaengine.h>
790 #include <linux/hrtimer.h>
791 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
792 +#include <linux/imq.h>
795 #define HAVE_ALLOC_SKB /* For the drivers to know */
796 #define HAVE_ALIGNABLE_SKB /* Ditto 8) */
798 * first. This is owned by whoever has the skb queued ATM.
801 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
808 struct nf_conntrack *nfct;
809 struct sk_buff *nfct_reasm;
811 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
812 + struct nf_queue_entry *nf_queue_entry;
814 #ifdef CONFIG_BRIDGE_NETFILTER
815 struct nf_bridge_info *nf_bridge;
818 __u8 do_not_encrypt:1;
820 /* 0/13/14 bit hole */
821 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
822 + __u8 imq_flags:IMQ_F_BITS;
825 #ifdef CONFIG_NET_DMA
826 dma_cookie_t dma_cookie;
829 #include <asm/system.h>
832 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
833 +extern int skb_save_cb(struct sk_buff *skb);
834 +extern int skb_restore_cb(struct sk_buff *skb);
837 extern void kfree_skb(struct sk_buff *skb);
838 extern void __kfree_skb(struct sk_buff *skb);
839 extern struct sk_buff *__alloc_skb(unsigned int size,
840 @@ -1633,6 +1651,10 @@
841 dst->nfct_reasm = src->nfct_reasm;
842 nf_conntrack_get_reasm(src->nfct_reasm);
844 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
845 + dst->imq_flags = src->imq_flags;
846 + dst->nf_queue_entry = src->nf_queue_entry;
848 #ifdef CONFIG_BRIDGE_NETFILTER
849 dst->nf_bridge = src->nf_bridge;
850 nf_bridge_get(src->nf_bridge);
851 diff -uNr --exclude='*~' linux-2.6.27.21/include/net/netfilter/nf_queue.h linux-2.6.27.21-imq/include/net/netfilter/nf_queue.h
852 --- linux-2.6.27.21/include/net/netfilter/nf_queue.h 2009-03-24 00:04:09.000000000 +0200
853 +++ linux-2.6.27.21-imq/include/net/netfilter/nf_queue.h 2009-04-20 23:48:35.704017144 +0300
855 struct net_device *indev;
856 struct net_device *outdev;
857 int (*okfn)(struct sk_buff *);
859 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
860 + int (*next_outfn)(struct nf_queue_entry *entry,
861 + unsigned int queuenum);
862 + unsigned int next_queuenum;
866 #define nf_queue_entry_reroute(x) ((void *)x + sizeof(struct nf_queue_entry))
868 const struct nf_queue_handler *qh);
869 extern void nf_unregister_queue_handlers(const struct nf_queue_handler *qh);
870 extern void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
871 +extern void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
873 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
874 +extern void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
875 +extern void nf_unregister_queue_imq_handler(void);
878 #endif /* _NF_QUEUE_H */
879 diff -uNr --exclude='*~' linux-2.6.27.21/net/core/dev.c linux-2.6.27.21-imq/net/core/dev.c
880 --- linux-2.6.27.21/net/core/dev.c 2009-03-24 00:04:09.000000000 +0200
881 +++ linux-2.6.27.21-imq/net/core/dev.c 2009-04-20 23:48:35.706016975 +0300
883 #include <net/net_namespace.h>
884 #include <net/sock.h>
885 #include <linux/rtnetlink.h>
886 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
887 +#include <linux/imq.h>
889 #include <linux/proc_fs.h>
890 #include <linux/seq_file.h>
891 #include <linux/stat.h>
892 @@ -1624,7 +1627,11 @@
893 struct netdev_queue *txq)
895 if (likely(!skb->next)) {
896 - if (!list_empty(&ptype_all))
897 + if (!list_empty(&ptype_all)
898 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
899 + && !(skb->imq_flags & IMQ_F_ENQUEUE)
902 dev_queue_xmit_nit(skb, dev);
904 if (netif_needs_gso(dev, skb)) {
905 @@ -1715,8 +1722,7 @@
906 return (u16) (((u64) hash * dev->real_num_tx_queues) >> 32);
909 -static struct netdev_queue *dev_pick_tx(struct net_device *dev,
910 - struct sk_buff *skb)
911 +struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb)
915 @@ -1728,6 +1734,7 @@
916 skb_set_queue_mapping(skb, queue_index);
917 return netdev_get_tx_queue(dev, queue_index);
919 +EXPORT_SYMBOL(dev_pick_tx);
922 * dev_queue_xmit - transmit a buffer
923 diff -uNr --exclude='*~' linux-2.6.27.21/net/core/skbuff.c linux-2.6.27.21-imq/net/core/skbuff.c
924 --- linux-2.6.27.21/net/core/skbuff.c 2009-03-24 00:04:09.000000000 +0200
925 +++ linux-2.6.27.21-imq/net/core/skbuff.c 2009-04-20 23:48:35.707017089 +0300
928 static struct kmem_cache *skbuff_head_cache __read_mostly;
929 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
930 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
931 +static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
934 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
935 struct pipe_buffer *buf)
940 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
941 +/* Control buffer save/restore for IMQ devices */
942 +struct skb_cb_table {
948 +static DEFINE_SPINLOCK(skb_cb_store_lock);
950 +int skb_save_cb(struct sk_buff *skb)
952 + struct skb_cb_table *next;
954 + next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
958 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
960 + memcpy(next->cb, skb->cb, sizeof(skb->cb));
961 + next->cb_next = skb->cb_next;
963 + atomic_set(&next->refcnt, 1);
965 + skb->cb_next = next;
968 +EXPORT_SYMBOL(skb_save_cb);
970 +int skb_restore_cb(struct sk_buff *skb)
972 + struct skb_cb_table *next;
977 + next = skb->cb_next;
979 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
981 + memcpy(skb->cb, next->cb, sizeof(skb->cb));
982 + skb->cb_next = next->cb_next;
984 + spin_lock(&skb_cb_store_lock);
986 + if (atomic_dec_and_test(&next->refcnt)) {
987 + kmem_cache_free(skbuff_cb_store_cache, next);
990 + spin_unlock(&skb_cb_store_lock);
994 +EXPORT_SYMBOL(skb_restore_cb);
996 +static void skb_copy_stored_cb(struct sk_buff *new, struct sk_buff *old)
998 + struct skb_cb_table *next;
1000 + if (!old->cb_next) {
1005 + spin_lock(&skb_cb_store_lock);
1007 + next = old->cb_next;
1008 + atomic_inc(&next->refcnt);
1009 + new->cb_next = next;
1011 + spin_unlock(&skb_cb_store_lock);
1015 /* Pipe buffer operations for a socket. */
1016 static struct pipe_buf_operations sock_pipe_buf_ops = {
1017 @@ -362,6 +439,15 @@
1019 skb->destructor(skb);
1021 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1022 + /* This should not happen. When it does, avoid memleak by restoring
1023 + the chain of cb-backups. */
1024 + while(skb->cb_next != NULL) {
1025 + printk(KERN_WARNING "kfree_skb: skb->cb_next: %08x\n",
1027 + skb_restore_cb(skb);
1030 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1031 nf_conntrack_put(skb->nfct);
1032 nf_conntrack_put_reasm(skb->nfct_reasm);
1034 new->sp = secpath_get(old->sp);
1036 memcpy(new->cb, old->cb, sizeof(old->cb));
1037 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1038 + skb_copy_stored_cb(new, old);
1040 new->csum_start = old->csum_start;
1041 new->csum_offset = old->csum_offset;
1042 new->local_df = old->local_df;
1043 @@ -2326,6 +2415,13 @@
1045 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1047 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1048 + skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1049 + sizeof(struct skb_cb_table),
1051 + SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1057 diff -uNr --exclude='*~' linux-2.6.27.21/net/netfilter/Kconfig linux-2.6.27.21-imq/net/netfilter/Kconfig
1058 --- linux-2.6.27.21/net/netfilter/Kconfig 2009-03-24 00:04:09.000000000 +0200
1059 +++ linux-2.6.27.21-imq/net/netfilter/Kconfig 2009-04-20 23:48:35.707017089 +0300
1060 @@ -342,6 +342,18 @@
1062 To compile it as a module, choose M here. If unsure, say N.
1064 +config NETFILTER_XT_TARGET_IMQ
1065 + tristate '"IMQ" target support'
1066 + depends on NETFILTER_XTABLES
1067 + depends on IP_NF_MANGLE || IP6_NF_MANGLE
1069 + default m if NETFILTER_ADVANCED=n
1071 + This option adds a `IMQ' target which is used to specify if and
1072 + to which imq device packets should get enqueued/dequeued.
1074 + To compile it as a module, choose M here. If unsure, say N.
1076 config NETFILTER_XT_TARGET_MARK
1077 tristate '"MARK" target support'
1078 depends on NETFILTER_XTABLES
1079 diff -uNr --exclude='*~' linux-2.6.27.21/net/netfilter/Makefile linux-2.6.27.21-imq/net/netfilter/Makefile
1080 --- linux-2.6.27.21/net/netfilter/Makefile 2009-03-24 00:04:09.000000000 +0200
1081 +++ linux-2.6.27.21-imq/net/netfilter/Makefile 2009-04-20 23:48:35.707017089 +0300
1083 obj-$(CONFIG_NETFILTER_XT_TARGET_CONNMARK) += xt_CONNMARK.o
1084 obj-$(CONFIG_NETFILTER_XT_TARGET_CONNSECMARK) += xt_CONNSECMARK.o
1085 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
1086 +obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
1087 obj-$(CONFIG_NETFILTER_XT_TARGET_MARK) += xt_MARK.o
1088 obj-$(CONFIG_NETFILTER_XT_TARGET_NFLOG) += xt_NFLOG.o
1089 obj-$(CONFIG_NETFILTER_XT_TARGET_NFQUEUE) += xt_NFQUEUE.o
1090 diff -uNr --exclude='*~' linux-2.6.27.21/net/netfilter/nf_queue.c linux-2.6.27.21-imq/net/netfilter/nf_queue.c
1091 --- linux-2.6.27.21/net/netfilter/nf_queue.c 2009-03-24 00:04:09.000000000 +0200
1092 +++ linux-2.6.27.21-imq/net/netfilter/nf_queue.c 2009-04-20 23:48:35.708017126 +0300
1095 static DEFINE_MUTEX(queue_handler_mutex);
1097 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1098 +static const struct nf_queue_handler *queue_imq_handler;
1100 +void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
1102 + mutex_lock(&queue_handler_mutex);
1103 + rcu_assign_pointer(queue_imq_handler, qh);
1104 + mutex_unlock(&queue_handler_mutex);
1106 +EXPORT_SYMBOL(nf_register_queue_imq_handler);
1108 +void nf_unregister_queue_imq_handler(void)
1110 + mutex_lock(&queue_handler_mutex);
1111 + rcu_assign_pointer(queue_imq_handler, NULL);
1112 + mutex_unlock(&queue_handler_mutex);
1114 +EXPORT_SYMBOL(nf_unregister_queue_imq_handler);
1117 /* return EBUSY when somebody else is registered, return EEXIST if the
1118 * same handler is registered, return 0 in case of success. */
1119 int nf_register_queue_handler(int pf, const struct nf_queue_handler *qh)
1122 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
1124 -static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1125 +void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1127 /* Release those devices we held, or Alexey will kill me. */
1130 /* Drop reference to owner of hook which queued us. */
1131 module_put(entry->elem->owner);
1133 +EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
1136 * Any packet that leaves via this function must come back
1137 @@ -121,12 +142,26 @@
1139 const struct nf_afinfo *afinfo;
1140 const struct nf_queue_handler *qh;
1141 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1142 + const struct nf_queue_handler *qih = NULL;
1145 /* QUEUE == DROP if noone is waiting, to be safe. */
1148 qh = rcu_dereference(queue_handler[pf]);
1149 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1150 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1151 + if (pf == PF_INET || pf == PF_INET6)
1153 + if (pf == PF_INET)
1155 + qih = rcu_dereference(queue_imq_handler);
1163 afinfo = nf_get_afinfo(pf);
1164 @@ -145,6 +180,10 @@
1168 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1169 + .next_outfn = qh ? qh->outfn : NULL,
1170 + .next_queuenum = queuenum,
1174 /* If it's going away, ignore hook. */
1175 @@ -170,8 +209,19 @@
1178 afinfo->saveroute(skb, entry);
1180 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1182 + status = qih->outfn(entry, queuenum);
1183 + goto imq_skip_queue;
1187 status = qh->outfn(entry, queuenum);
1189 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1195 diff -uNr --exclude='*~' linux-2.6.27.21/net/netfilter/xt_IMQ.c linux-2.6.27.21-imq/net/netfilter/xt_IMQ.c
1196 --- linux-2.6.27.21/net/netfilter/xt_IMQ.c 1970-01-01 02:00:00.000000000 +0200
1197 +++ linux-2.6.27.21-imq/net/netfilter/xt_IMQ.c 2009-04-20 23:48:35.708017126 +0300
1200 + * This target marks packets to be enqueued to an imq device
1202 +#include <linux/module.h>
1203 +#include <linux/skbuff.h>
1204 +#include <linux/netfilter/x_tables.h>
1205 +#include <linux/netfilter/xt_IMQ.h>
1206 +#include <linux/imq.h>
1208 +static unsigned int imq_target(struct sk_buff *pskb,
1209 + const struct net_device *in,
1210 + const struct net_device *out,
1211 + unsigned int hooknum,
1212 + const struct xt_target *target,
1213 + const void *targinfo)
1215 + const struct xt_imq_info *mr = targinfo;
1217 + pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
1219 + return XT_CONTINUE;
1222 +static bool imq_checkentry(const char *tablename,
1223 + const void *entry,
1224 + const struct xt_target *target,
1226 + unsigned int hook_mask)
1228 + struct xt_imq_info *mr = targinfo;
1230 + if (mr->todev > IMQ_MAX_DEVS - 1) {
1231 + printk(KERN_WARNING
1232 + "IMQ: invalid device specified, highest is %u\n",
1233 + IMQ_MAX_DEVS - 1);
1240 +static struct xt_target xt_imq_reg[] __read_mostly = {
1243 + .family = AF_INET,
1244 + .target = imq_target,
1245 + .targetsize = sizeof(struct xt_imq_info),
1246 + .table = "mangle",
1247 + .checkentry = imq_checkentry,
1252 + .family = AF_INET6,
1253 + .target = imq_target,
1254 + .targetsize = sizeof(struct xt_imq_info),
1255 + .table = "mangle",
1256 + .checkentry = imq_checkentry,
1261 +static int __init imq_init(void)
1263 + return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1266 +static void __exit imq_fini(void)
1268 + xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1271 +module_init(imq_init);
1272 +module_exit(imq_fini);
1274 +MODULE_AUTHOR("http://www.linuximq.net");
1275 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
1276 +MODULE_LICENSE("GPL");
1277 +MODULE_ALIAS("ipt_IMQ");
1278 +MODULE_ALIAS("ip6t_IMQ");