1 diff -uNr linux-2.6.34/drivers/net/imq.c linux-2.6.34-imq/drivers/net/imq.c
2 --- linux-2.6.34/drivers/net/imq.c 1970-01-01 02:00:00.000000000 +0200
3 +++ linux-2.6.34-imq/drivers/net/imq.c 2010-06-02 10:05:45.752109073 +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 + * - Port to 2.6.29 + fix rmmod not working
73 + * 2009/04/20 - (Jussi Kivilinna)
74 + * - Use netdevice feature flags to avoid extra packet handling
75 + * by core networking layer and possibly increase performance.
77 + * 2009/09/26 - (Jussi Kivilinna)
78 + * - Add imq_nf_reinject_lockless to fix deadlock with
79 + * imq_nf_queue/imq_nf_reinject.
81 + * 2009/12/08 - (Jussi Kivilinna)
83 + * - Add check for skb->nf_queue_entry==NULL in imq_dev_xmit()
84 + * - Also add better error checking for skb->nf_queue_entry usage
86 + * 2010/02/25 - (Jussi Kivilinna)
89 + * Also, many thanks to pablo Sebastian Greco for making the initial
90 + * patch and to those who helped the testing.
92 + * More info at: http://www.linuximq.net/ (Andre Correa)
95 +#include <linux/module.h>
96 +#include <linux/kernel.h>
97 +#include <linux/moduleparam.h>
98 +#include <linux/list.h>
99 +#include <linux/skbuff.h>
100 +#include <linux/netdevice.h>
101 +#include <linux/etherdevice.h>
102 +#include <linux/rtnetlink.h>
103 +#include <linux/if_arp.h>
104 +#include <linux/netfilter.h>
105 +#include <linux/netfilter_ipv4.h>
106 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
107 + #include <linux/netfilter_ipv6.h>
109 +#include <linux/imq.h>
110 +#include <net/pkt_sched.h>
111 +#include <net/netfilter/nf_queue.h>
113 +static nf_hookfn imq_nf_hook;
115 +static struct nf_hook_ops imq_ingress_ipv4 = {
116 + .hook = imq_nf_hook,
117 + .owner = THIS_MODULE,
119 + .hooknum = NF_INET_PRE_ROUTING,
120 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
121 + .priority = NF_IP_PRI_MANGLE + 1
123 + .priority = NF_IP_PRI_NAT_DST + 1
127 +static struct nf_hook_ops imq_egress_ipv4 = {
128 + .hook = imq_nf_hook,
129 + .owner = THIS_MODULE,
131 + .hooknum = NF_INET_POST_ROUTING,
132 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
133 + .priority = NF_IP_PRI_LAST
135 + .priority = NF_IP_PRI_NAT_SRC - 1
139 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
140 +static struct nf_hook_ops imq_ingress_ipv6 = {
141 + .hook = imq_nf_hook,
142 + .owner = THIS_MODULE,
144 + .hooknum = NF_INET_PRE_ROUTING,
145 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
146 + .priority = NF_IP6_PRI_MANGLE + 1
148 + .priority = NF_IP6_PRI_NAT_DST + 1
152 +static struct nf_hook_ops imq_egress_ipv6 = {
153 + .hook = imq_nf_hook,
154 + .owner = THIS_MODULE,
156 + .hooknum = NF_INET_POST_ROUTING,
157 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
158 + .priority = NF_IP6_PRI_LAST
160 + .priority = NF_IP6_PRI_NAT_SRC - 1
165 +#if defined(CONFIG_IMQ_NUM_DEVS)
166 +static unsigned int numdevs = CONFIG_IMQ_NUM_DEVS;
168 +static unsigned int numdevs = IMQ_MAX_DEVS;
171 +static DEFINE_SPINLOCK(imq_nf_queue_lock);
173 +static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
176 +static struct net_device_stats *imq_get_stats(struct net_device *dev)
178 + return &dev->stats;
181 +/* called for packets kfree'd in qdiscs at places other than enqueue */
182 +static void imq_skb_destructor(struct sk_buff *skb)
184 + struct nf_queue_entry *entry = skb->nf_queue_entry;
186 + skb->nf_queue_entry = NULL;
189 + nf_queue_entry_release_refs(entry);
193 + skb_restore_cb(skb); /* kfree backup */
196 +/* locking not needed when called from imq_nf_queue */
197 +static void imq_nf_reinject_lockless(struct nf_queue_entry *entry,
198 + unsigned int verdict)
202 + if (!entry->next_outfn) {
203 + nf_reinject(entry, verdict);
207 + status = entry->next_outfn(entry, entry->next_queuenum);
209 + nf_queue_entry_release_refs(entry);
210 + kfree_skb(entry->skb);
215 +static void imq_nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
219 + if (!entry->next_outfn) {
220 + spin_lock_bh(&imq_nf_queue_lock);
221 + nf_reinject(entry, verdict);
222 + spin_unlock_bh(&imq_nf_queue_lock);
227 + local_bh_disable();
228 + status = entry->next_outfn(entry, entry->next_queuenum);
231 + nf_queue_entry_release_refs(entry);
232 + kfree_skb(entry->skb);
239 +static netdev_tx_t imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
241 + struct nf_queue_entry *entry = skb->nf_queue_entry;
243 + skb->nf_queue_entry = NULL;
244 + dev->trans_start = jiffies;
246 + dev->stats.tx_bytes += skb->len;
247 + dev->stats.tx_packets++;
249 + if (entry == NULL) {
250 + /* We don't know what is going on here.. packet is queued for
251 + * imq device, but (probably) not by us.
253 + * If this packet was not send here by imq_nf_queue(), then
254 + * skb_save_cb() was not used and skb_free() should not show:
255 + * WARNING: IMQ: kfree_skb: skb->cb_next:..
257 + * WARNING: IMQ: kfree_skb: skb->nf_queue_entry...
259 + * However if this message is shown, then IMQ is somehow broken
260 + * and you should report this to linuximq.net.
263 + /* imq_dev_xmit is black hole that eats all packets, report that
264 + * we eat this packet happily and increase dropped counters.
267 + dev->stats.tx_dropped++;
268 + dev_kfree_skb(skb);
270 + return NETDEV_TX_OK;
273 + skb_restore_cb(skb); /* restore skb->cb */
275 + skb->imq_flags = 0;
276 + skb->destructor = NULL;
278 + imq_nf_reinject(entry, NF_ACCEPT);
280 + return NETDEV_TX_OK;
283 +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num)
285 + struct net_device *dev;
286 + struct sk_buff *skb_orig, *skb, *skb_shared;
288 + struct netdev_queue *txq;
290 + int retval = -EINVAL;
292 + index = entry->skb->imq_flags & IMQ_F_IFMASK;
293 + if (unlikely(index > numdevs - 1)) {
294 + if (net_ratelimit())
295 + printk(KERN_WARNING
296 + "IMQ: invalid device specified, highest is %u\n",
302 + /* check for imq device by index from cache */
303 + dev = imq_devs_cache[index];
304 + if (unlikely(!dev)) {
307 + /* get device by name and cache result */
308 + snprintf(buf, sizeof(buf), "imq%d", index);
309 + dev = dev_get_by_name(&init_net, buf);
317 + imq_devs_cache[index] = dev;
321 + if (unlikely(!(dev->flags & IFF_UP))) {
322 + entry->skb->imq_flags = 0;
323 + imq_nf_reinject_lockless(entry, NF_ACCEPT);
327 + dev->last_rx = jiffies;
332 + /* skb has owner? => make clone */
333 + if (unlikely(skb->destructor)) {
335 + skb = skb_clone(skb, GFP_ATOMIC);
343 + skb->nf_queue_entry = entry;
345 + dev->stats.rx_bytes += skb->len;
346 + dev->stats.rx_packets++;
348 + txq = dev_pick_tx(dev, skb);
350 + q = rcu_dereference(txq->qdisc);
351 + if (unlikely(!q->enqueue))
352 + goto packet_not_eaten_by_imq_dev;
354 + spin_lock_bh(qdisc_lock(q));
356 + users = atomic_read(&skb->users);
358 + skb_shared = skb_get(skb); /* increase reference count by one */
359 + skb_save_cb(skb_shared); /* backup skb->cb, as qdisc layer will
361 + qdisc_enqueue_root(skb_shared, q); /* might kfree_skb */
363 + if (likely(atomic_read(&skb_shared->users) == users + 1)) {
364 + kfree_skb(skb_shared); /* decrease reference count by one */
366 + skb->destructor = &imq_skb_destructor;
370 + kfree_skb(skb_orig); /* free original */
372 + spin_unlock_bh(qdisc_lock(q));
374 + /* schedule qdisc dequeue */
375 + __netif_schedule(q);
380 + skb_restore_cb(skb_shared); /* restore skb->cb */
381 + skb->nf_queue_entry = NULL;
382 + /* qdisc dropped packet and decreased skb reference count of
383 + * skb, so we don't really want to and try refree as that would
384 + * actually destroy the skb. */
385 + spin_unlock_bh(qdisc_lock(q));
386 + goto packet_not_eaten_by_imq_dev;
389 +packet_not_eaten_by_imq_dev:
390 + /* cloned? restore original */
393 + entry->skb = skb_orig;
400 +static struct nf_queue_handler nfqh = {
402 + .outfn = imq_nf_queue,
405 +static unsigned int imq_nf_hook(unsigned int hook, struct sk_buff *pskb,
406 + const struct net_device *indev,
407 + const struct net_device *outdev,
408 + int (*okfn)(struct sk_buff *))
410 + if (pskb->imq_flags & IMQ_F_ENQUEUE)
416 +static int imq_close(struct net_device *dev)
418 + netif_stop_queue(dev);
422 +static int imq_open(struct net_device *dev)
424 + netif_start_queue(dev);
428 +static const struct net_device_ops imq_netdev_ops = {
429 + .ndo_open = imq_open,
430 + .ndo_stop = imq_close,
431 + .ndo_start_xmit = imq_dev_xmit,
432 + .ndo_get_stats = imq_get_stats,
435 +static void imq_setup(struct net_device *dev)
437 + dev->netdev_ops = &imq_netdev_ops;
438 + dev->type = ARPHRD_VOID;
440 + dev->tx_queue_len = 11000;
441 + dev->flags = IFF_NOARP;
442 + dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
443 + NETIF_F_GSO | NETIF_F_HW_CSUM |
445 + dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
448 +static int imq_validate(struct nlattr *tb[], struct nlattr *data[])
452 + if (tb[IFLA_ADDRESS]) {
453 + if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
457 + if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
458 + ret = -EADDRNOTAVAIL;
464 + printk(KERN_WARNING "IMQ: imq_validate failed (%d)\n", ret);
468 +static struct rtnl_link_ops imq_link_ops __read_mostly = {
471 + .setup = imq_setup,
472 + .validate = imq_validate,
475 +static int __init imq_init_hooks(void)
479 + nf_register_queue_imq_handler(&nfqh);
481 + err = nf_register_hook(&imq_ingress_ipv4);
485 + err = nf_register_hook(&imq_egress_ipv4);
489 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
490 + err = nf_register_hook(&imq_ingress_ipv6);
494 + err = nf_register_hook(&imq_egress_ipv6);
501 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
503 + nf_unregister_hook(&imq_ingress_ipv6);
505 + nf_unregister_hook(&imq_egress_ipv4);
508 + nf_unregister_hook(&imq_ingress_ipv4);
510 + nf_unregister_queue_imq_handler();
514 +static int __init imq_init_one(int index)
516 + struct net_device *dev;
519 + dev = alloc_netdev(0, "imq%d", imq_setup);
523 + ret = dev_alloc_name(dev, dev->name);
527 + dev->rtnl_link_ops = &imq_link_ops;
528 + ret = register_netdevice(dev);
538 +static int __init imq_init_devs(void)
542 + if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) {
543 + printk(KERN_ERR "IMQ: numdevs has to be betweed 1 and %u\n",
549 + err = __rtnl_link_register(&imq_link_ops);
551 + for (i = 0; i < numdevs && !err; i++)
552 + err = imq_init_one(i);
555 + __rtnl_link_unregister(&imq_link_ops);
556 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
563 +static int __init imq_init_module(void)
567 +#if defined(CONFIG_IMQ_NUM_DEVS)
568 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS > 16);
569 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS < 2);
570 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS - 1 > IMQ_F_IFMASK);
573 + err = imq_init_devs();
575 + printk(KERN_ERR "IMQ: Error trying imq_init_devs(net)\n");
579 + err = imq_init_hooks();
581 + printk(KERN_ERR "IMQ: Error trying imq_init_hooks()\n");
582 + rtnl_link_unregister(&imq_link_ops);
583 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
587 + printk(KERN_INFO "IMQ driver loaded successfully.\n");
589 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
590 + printk(KERN_INFO "\tHooking IMQ before NAT on PREROUTING.\n");
592 + printk(KERN_INFO "\tHooking IMQ after NAT on PREROUTING.\n");
594 +#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
595 + printk(KERN_INFO "\tHooking IMQ before NAT on POSTROUTING.\n");
597 + printk(KERN_INFO "\tHooking IMQ after NAT on POSTROUTING.\n");
603 +static void __exit imq_unhook(void)
605 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
606 + nf_unregister_hook(&imq_ingress_ipv6);
607 + nf_unregister_hook(&imq_egress_ipv6);
609 + nf_unregister_hook(&imq_ingress_ipv4);
610 + nf_unregister_hook(&imq_egress_ipv4);
612 + nf_unregister_queue_imq_handler();
615 +static void __exit imq_cleanup_devs(void)
617 + rtnl_link_unregister(&imq_link_ops);
618 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
621 +static void __exit imq_exit_module(void)
624 + imq_cleanup_devs();
625 + printk(KERN_INFO "IMQ driver unloaded successfully.\n");
628 +module_init(imq_init_module);
629 +module_exit(imq_exit_module);
631 +module_param(numdevs, int, 0);
632 +MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will "
634 +MODULE_AUTHOR("http://www.linuximq.net");
635 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See "
636 + "http://www.linuximq.net/ for more information.");
637 +MODULE_LICENSE("GPL");
638 +MODULE_ALIAS_RTNL_LINK("imq");
640 diff -uNr linux-2.6.34/drivers/net/Kconfig linux-2.6.34-imq/drivers/net/Kconfig
641 --- linux-2.6.34/drivers/net/Kconfig 2010-05-17 00:17:36.000000000 +0300
642 +++ linux-2.6.34-imq/drivers/net/Kconfig 2010-06-02 10:05:45.752109073 +0300
643 @@ -121,6 +121,129 @@
644 To compile this driver as a module, choose M here: the module
645 will be called eql. If unsure, say N.
648 + tristate "IMQ (intermediate queueing device) support"
649 + depends on NETDEVICES && NETFILTER
651 + The IMQ device(s) is used as placeholder for QoS queueing
652 + disciplines. Every packet entering/leaving the IP stack can be
653 + directed through the IMQ device where it's enqueued/dequeued to the
654 + attached qdisc. This allows you to treat network devices as classes
655 + and distribute bandwidth among them. Iptables is used to specify
656 + through which IMQ device, if any, packets travel.
658 + More information at: http://www.linuximq.net/
660 + To compile this driver as a module, choose M here: the module
661 + will be called imq. If unsure, say N.
664 + prompt "IMQ behavior (PRE/POSTROUTING)"
666 + default IMQ_BEHAVIOR_AB
669 + This settings defines how IMQ behaves in respect to its
670 + hooking in PREROUTING and POSTROUTING.
672 + IMQ can work in any of the following ways:
674 + PREROUTING | POSTROUTING
675 + -----------------|-------------------
676 + #1 After NAT | After NAT
677 + #2 After NAT | Before NAT
678 + #3 Before NAT | After NAT
679 + #4 Before NAT | Before NAT
681 + The default behavior is to hook before NAT on PREROUTING
682 + and after NAT on POSTROUTING (#3).
684 + This settings are specially usefull when trying to use IMQ
685 + to shape NATed clients.
687 + More information can be found at: www.linuximq.net
689 + If not sure leave the default settings alone.
691 +config IMQ_BEHAVIOR_AA
694 + This settings defines how IMQ behaves in respect to its
695 + hooking in PREROUTING and POSTROUTING.
697 + Choosing this option will make IMQ hook like this:
699 + PREROUTING: After NAT
700 + POSTROUTING: After NAT
702 + More information can be found at: www.linuximq.net
704 + If not sure leave the default settings alone.
706 +config IMQ_BEHAVIOR_AB
709 + This settings defines how IMQ behaves in respect to its
710 + hooking in PREROUTING and POSTROUTING.
712 + Choosing this option will make IMQ hook like this:
714 + PREROUTING: After NAT
715 + POSTROUTING: Before NAT
717 + More information can be found at: www.linuximq.net
719 + If not sure leave the default settings alone.
721 +config IMQ_BEHAVIOR_BA
724 + This settings defines how IMQ behaves in respect to its
725 + hooking in PREROUTING and POSTROUTING.
727 + Choosing this option will make IMQ hook like this:
729 + PREROUTING: Before NAT
730 + POSTROUTING: After NAT
732 + More information can be found at: www.linuximq.net
734 + If not sure leave the default settings alone.
736 +config IMQ_BEHAVIOR_BB
739 + This settings defines how IMQ behaves in respect to its
740 + hooking in PREROUTING and POSTROUTING.
742 + Choosing this option will make IMQ hook like this:
744 + PREROUTING: Before NAT
745 + POSTROUTING: Before NAT
747 + More information can be found at: www.linuximq.net
749 + If not sure leave the default settings alone.
755 + int "Number of IMQ devices"
761 + This settings defines how many IMQ devices will be
764 + The default value is 16.
766 + More information can be found at: www.linuximq.net
768 + If not sure leave the default settings alone.
771 tristate "Universal TUN/TAP device driver support"
773 diff -uNr linux-2.6.34/drivers/net/Makefile linux-2.6.34-imq/drivers/net/Makefile
774 --- linux-2.6.34/drivers/net/Makefile 2010-05-17 00:17:36.000000000 +0300
775 +++ linux-2.6.34-imq/drivers/net/Makefile 2010-06-02 10:05:45.752109073 +0300
777 obj-$(CONFIG_XEN_NETDEV_FRONTEND) += xen-netfront.o
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 diff -uNr linux-2.6.34/include/linux/imq.h linux-2.6.34-imq/include/linux/imq.h
785 --- linux-2.6.34/include/linux/imq.h 1970-01-01 02:00:00.000000000 +0200
786 +++ linux-2.6.34-imq/include/linux/imq.h 2010-06-02 10:05:45.752109073 +0300
791 +/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
792 +#define IMQ_F_BITS 5
794 +#define IMQ_F_IFMASK 0x0f
795 +#define IMQ_F_ENQUEUE 0x10
797 +#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1)
801 diff -uNr linux-2.6.34/include/linux/netdevice.h linux-2.6.34-imq/include/linux/netdevice.h
802 --- linux-2.6.34/include/linux/netdevice.h 2010-05-17 00:17:36.000000000 +0300
803 +++ linux-2.6.34-imq/include/linux/netdevice.h 2010-06-02 10:05:45.752109073 +0300
804 @@ -1203,6 +1203,7 @@
805 extern int dev_open(struct net_device *dev);
806 extern int dev_close(struct net_device *dev);
807 extern void dev_disable_lro(struct net_device *dev);
808 +extern struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb);
809 extern int dev_queue_xmit(struct sk_buff *skb);
810 extern int register_netdevice(struct net_device *dev);
811 extern void unregister_netdevice_queue(struct net_device *dev,
812 diff -uNr linux-2.6.34/include/linux/netfilter/xt_IMQ.h linux-2.6.34-imq/include/linux/netfilter/xt_IMQ.h
813 --- linux-2.6.34/include/linux/netfilter/xt_IMQ.h 1970-01-01 02:00:00.000000000 +0200
814 +++ linux-2.6.34-imq/include/linux/netfilter/xt_IMQ.h 2010-06-02 10:05:45.752109073 +0300
819 +struct xt_imq_info {
820 + unsigned int todev; /* target imq device */
823 +#endif /* _XT_IMQ_H */
825 diff -uNr linux-2.6.34/include/linux/netfilter_ipv4/ipt_IMQ.h linux-2.6.34-imq/include/linux/netfilter_ipv4/ipt_IMQ.h
826 --- linux-2.6.34/include/linux/netfilter_ipv4/ipt_IMQ.h 1970-01-01 02:00:00.000000000 +0200
827 +++ linux-2.6.34-imq/include/linux/netfilter_ipv4/ipt_IMQ.h 2010-06-02 10:05:45.752109073 +0300
832 +/* Backwards compatibility for old userspace */
833 +#include <linux/netfilter/xt_IMQ.h>
835 +#define ipt_imq_info xt_imq_info
837 +#endif /* _IPT_IMQ_H */
839 diff -uNr linux-2.6.34/include/linux/netfilter_ipv6/ip6t_IMQ.h linux-2.6.34-imq/include/linux/netfilter_ipv6/ip6t_IMQ.h
840 --- linux-2.6.34/include/linux/netfilter_ipv6/ip6t_IMQ.h 1970-01-01 02:00:00.000000000 +0200
841 +++ linux-2.6.34-imq/include/linux/netfilter_ipv6/ip6t_IMQ.h 2010-06-02 10:05:45.752109073 +0300
846 +/* Backwards compatibility for old userspace */
847 +#include <linux/netfilter/xt_IMQ.h>
849 +#define ip6t_imq_info xt_imq_info
851 +#endif /* _IP6T_IMQ_H */
853 diff -uNr linux-2.6.34/include/linux/skbuff.h linux-2.6.34-imq/include/linux/skbuff.h
854 --- linux-2.6.34/include/linux/skbuff.h 2010-05-17 00:17:36.000000000 +0300
855 +++ linux-2.6.34-imq/include/linux/skbuff.h 2010-06-02 10:05:45.752109073 +0300
857 #include <linux/rcupdate.h>
858 #include <linux/dmaengine.h>
859 #include <linux/hrtimer.h>
860 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
861 +#include <linux/imq.h>
864 /* Don't change this without changing skb_csum_unnecessary! */
865 #define CHECKSUM_NONE 0
867 * first. This is owned by whoever has the skb queued ATM.
869 char cb[48] __aligned(8);
870 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
874 unsigned long _skb_dst;
877 struct nf_conntrack *nfct;
878 struct sk_buff *nfct_reasm;
880 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
881 + struct nf_queue_entry *nf_queue_entry;
883 #ifdef CONFIG_BRIDGE_NETFILTER
884 struct nf_bridge_info *nf_bridge;
890 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
891 + __u8 imq_flags:IMQ_F_BITS;
894 #ifdef CONFIG_NET_DMA
895 dma_cookie_t dma_cookie;
898 return (struct rtable *)skb_dst(skb);
902 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
903 +extern int skb_save_cb(struct sk_buff *skb);
904 +extern int skb_restore_cb(struct sk_buff *skb);
907 extern void kfree_skb(struct sk_buff *skb);
908 extern void consume_skb(struct sk_buff *skb);
909 extern void __kfree_skb(struct sk_buff *skb);
910 @@ -1970,6 +1989,10 @@
911 dst->nfct_reasm = src->nfct_reasm;
912 nf_conntrack_get_reasm(src->nfct_reasm);
914 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
915 + dst->imq_flags = src->imq_flags;
916 + dst->nf_queue_entry = src->nf_queue_entry;
918 #ifdef CONFIG_BRIDGE_NETFILTER
919 dst->nf_bridge = src->nf_bridge;
920 nf_bridge_get(src->nf_bridge);
921 diff -uNr linux-2.6.34/include/net/netfilter/nf_queue.h linux-2.6.34-imq/include/net/netfilter/nf_queue.h
922 --- linux-2.6.34/include/net/netfilter/nf_queue.h 2010-05-17 00:17:36.000000000 +0300
923 +++ linux-2.6.34-imq/include/net/netfilter/nf_queue.h 2010-06-02 10:05:45.752109073 +0300
925 struct net_device *indev;
926 struct net_device *outdev;
927 int (*okfn)(struct sk_buff *);
929 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
930 + int (*next_outfn)(struct nf_queue_entry *entry,
931 + unsigned int queuenum);
932 + unsigned int next_queuenum;
936 #define nf_queue_entry_reroute(x) ((void *)x + sizeof(struct nf_queue_entry))
938 const struct nf_queue_handler *qh);
939 extern void nf_unregister_queue_handlers(const struct nf_queue_handler *qh);
940 extern void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
941 +extern void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
943 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
944 +extern void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
945 +extern void nf_unregister_queue_imq_handler(void);
948 #endif /* _NF_QUEUE_H */
949 diff -uNr linux-2.6.34/net/core/dev.c linux-2.6.34-imq/net/core/dev.c
950 --- linux-2.6.34/net/core/dev.c 2010-05-17 00:17:36.000000000 +0300
951 +++ linux-2.6.34-imq/net/core/dev.c 2010-06-02 10:05:45.752109073 +0300
953 #include <net/net_namespace.h>
954 #include <net/sock.h>
955 #include <linux/rtnetlink.h>
956 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
957 +#include <linux/imq.h>
959 #include <linux/proc_fs.h>
960 #include <linux/seq_file.h>
961 #include <linux/stat.h>
962 @@ -1859,7 +1862,11 @@
963 int rc = NETDEV_TX_OK;
965 if (likely(!skb->next)) {
966 - if (!list_empty(&ptype_all))
967 + if (!list_empty(&ptype_all)
968 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
969 + && !(skb->imq_flags & IMQ_F_ENQUEUE)
972 dev_queue_xmit_nit(skb, dev);
974 if (netif_needs_gso(dev, skb)) {
975 @@ -1969,8 +1976,7 @@
979 -static struct netdev_queue *dev_pick_tx(struct net_device *dev,
980 - struct sk_buff *skb)
981 +struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb)
984 struct sock *sk = skb->sk;
985 @@ -2000,6 +2006,7 @@
986 skb_set_queue_mapping(skb, queue_index);
987 return netdev_get_tx_queue(dev, queue_index);
989 +EXPORT_SYMBOL(dev_pick_tx);
991 static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
992 struct net_device *dev,
993 diff -uNr linux-2.6.34/net/core/skbuff.c linux-2.6.34-imq/net/core/skbuff.c
994 --- linux-2.6.34/net/core/skbuff.c 2010-05-17 00:17:36.000000000 +0300
995 +++ linux-2.6.34-imq/net/core/skbuff.c 2010-06-02 10:05:45.752109073 +0300
998 static struct kmem_cache *skbuff_head_cache __read_mostly;
999 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
1000 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1001 +static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
1004 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
1005 struct pipe_buffer *buf)
1010 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1011 +/* Control buffer save/restore for IMQ devices */
1012 +struct skb_cb_table {
1018 +static DEFINE_SPINLOCK(skb_cb_store_lock);
1020 +int skb_save_cb(struct sk_buff *skb)
1022 + struct skb_cb_table *next;
1024 + next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
1028 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1030 + memcpy(next->cb, skb->cb, sizeof(skb->cb));
1031 + next->cb_next = skb->cb_next;
1033 + atomic_set(&next->refcnt, 1);
1035 + skb->cb_next = next;
1038 +EXPORT_SYMBOL(skb_save_cb);
1040 +int skb_restore_cb(struct sk_buff *skb)
1042 + struct skb_cb_table *next;
1044 + if (!skb->cb_next)
1047 + next = skb->cb_next;
1049 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1051 + memcpy(skb->cb, next->cb, sizeof(skb->cb));
1052 + skb->cb_next = next->cb_next;
1054 + spin_lock(&skb_cb_store_lock);
1056 + if (atomic_dec_and_test(&next->refcnt)) {
1057 + kmem_cache_free(skbuff_cb_store_cache, next);
1060 + spin_unlock(&skb_cb_store_lock);
1064 +EXPORT_SYMBOL(skb_restore_cb);
1066 +static void skb_copy_stored_cb(struct sk_buff *new, const struct sk_buff *__old)
1068 + struct skb_cb_table *next;
1069 + struct sk_buff *old;
1071 + if (!__old->cb_next) {
1072 + new->cb_next = NULL;
1076 + spin_lock(&skb_cb_store_lock);
1078 + old = (struct sk_buff *)__old;
1080 + next = old->cb_next;
1081 + atomic_inc(&next->refcnt);
1082 + new->cb_next = next;
1084 + spin_unlock(&skb_cb_store_lock);
1088 /* Pipe buffer operations for a socket. */
1089 static const struct pipe_buf_operations sock_pipe_buf_ops = {
1090 @@ -398,6 +478,26 @@
1092 skb->destructor(skb);
1094 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1095 + /* This should not happen. When it does, avoid memleak by restoring
1096 + the chain of cb-backups. */
1097 + while(skb->cb_next != NULL) {
1098 + if (net_ratelimit())
1099 + printk(KERN_WARNING "IMQ: kfree_skb: skb->cb_next: "
1100 + "%08x\n", (unsigned int)skb->cb_next);
1102 + skb_restore_cb(skb);
1104 + /* This should not happen either, nf_queue_entry is nullified in
1105 + * imq_dev_xmit(). If we have non-NULL nf_queue_entry then we are
1106 + * leaking entry pointers, maybe memory. We don't know if this is
1107 + * pointer to already freed memory, or should this be freed.
1108 + * If this happens we need to add refcounting, etc for nf_queue_entry.
1110 + if (skb->nf_queue_entry && net_ratelimit())
1111 + printk(KERN_WARNING
1112 + "IMQ: kfree_skb: skb->nf_queue_entry != NULL");
1114 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1115 nf_conntrack_put(skb->nfct);
1116 nf_conntrack_put_reasm(skb->nfct_reasm);
1118 new->sp = secpath_get(old->sp);
1120 memcpy(new->cb, old->cb, sizeof(old->cb));
1121 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1122 + skb_copy_stored_cb(new, old);
1124 new->csum = old->csum;
1125 new->local_df = old->local_df;
1126 new->pkt_type = old->pkt_type;
1127 @@ -2779,6 +2882,13 @@
1129 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1131 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1132 + skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1133 + sizeof(struct skb_cb_table),
1135 + SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1141 diff -uNr linux-2.6.34/net/netfilter/Kconfig linux-2.6.34-imq/net/netfilter/Kconfig
1142 --- linux-2.6.34/net/netfilter/Kconfig 2010-05-17 00:17:36.000000000 +0300
1143 +++ linux-2.6.34-imq/net/netfilter/Kconfig 2010-06-02 10:05:45.762097870 +0300
1144 @@ -421,6 +421,18 @@
1145 For more information on the LEDs available on your system, see
1146 Documentation/leds-class.txt
1148 +config NETFILTER_XT_TARGET_IMQ
1149 + tristate '"IMQ" target support'
1150 + depends on NETFILTER_XTABLES
1151 + depends on IP_NF_MANGLE || IP6_NF_MANGLE
1153 + default m if NETFILTER_ADVANCED=n
1155 + This option adds a `IMQ' target which is used to specify if and
1156 + to which imq device packets should get enqueued/dequeued.
1158 + To compile it as a module, choose M here. If unsure, say N.
1160 config NETFILTER_XT_TARGET_MARK
1161 tristate '"MARK" target support'
1162 default m if NETFILTER_ADVANCED=n
1163 diff -uNr linux-2.6.34/net/netfilter/Makefile linux-2.6.34-imq/net/netfilter/Makefile
1164 --- linux-2.6.34/net/netfilter/Makefile 2010-05-17 00:17:36.000000000 +0300
1165 +++ linux-2.6.34-imq/net/netfilter/Makefile 2010-06-02 10:05:45.762097870 +0300
1167 obj-$(CONFIG_NETFILTER_XT_TARGET_CT) += xt_CT.o
1168 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
1169 obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
1170 +obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
1171 obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
1172 obj-$(CONFIG_NETFILTER_XT_TARGET_MARK) += xt_MARK.o
1173 obj-$(CONFIG_NETFILTER_XT_TARGET_NFLOG) += xt_NFLOG.o
1174 diff -uNr linux-2.6.34/net/netfilter/nf_queue.c linux-2.6.34-imq/net/netfilter/nf_queue.c
1175 --- linux-2.6.34/net/netfilter/nf_queue.c 2010-05-17 00:17:36.000000000 +0300
1176 +++ linux-2.6.34-imq/net/netfilter/nf_queue.c 2010-06-02 10:05:45.762097870 +0300
1179 static DEFINE_MUTEX(queue_handler_mutex);
1181 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1182 +static const struct nf_queue_handler *queue_imq_handler;
1184 +void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
1186 + mutex_lock(&queue_handler_mutex);
1187 + rcu_assign_pointer(queue_imq_handler, qh);
1188 + mutex_unlock(&queue_handler_mutex);
1190 +EXPORT_SYMBOL(nf_register_queue_imq_handler);
1192 +void nf_unregister_queue_imq_handler(void)
1194 + mutex_lock(&queue_handler_mutex);
1195 + rcu_assign_pointer(queue_imq_handler, NULL);
1196 + mutex_unlock(&queue_handler_mutex);
1198 +EXPORT_SYMBOL(nf_unregister_queue_imq_handler);
1201 /* return EBUSY when somebody else is registered, return EEXIST if the
1202 * same handler is registered, return 0 in case of success. */
1203 int nf_register_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh)
1206 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
1208 -static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1209 +void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1211 /* Release those devices we held, or Alexey will kill me. */
1214 /* Drop reference to owner of hook which queued us. */
1215 module_put(entry->elem->owner);
1217 +EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
1220 * Any packet that leaves via this function must come back
1221 @@ -122,12 +143,26 @@
1223 const struct nf_afinfo *afinfo;
1224 const struct nf_queue_handler *qh;
1225 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1226 + const struct nf_queue_handler *qih = NULL;
1229 /* QUEUE == DROP if noone is waiting, to be safe. */
1232 qh = rcu_dereference(queue_handler[pf]);
1233 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1234 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1235 + if (pf == PF_INET || pf == PF_INET6)
1237 + if (pf == PF_INET)
1239 + qih = rcu_dereference(queue_imq_handler);
1247 afinfo = nf_get_afinfo(pf);
1248 @@ -146,6 +181,10 @@
1252 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1253 + .next_outfn = qh ? qh->outfn : NULL,
1254 + .next_queuenum = queuenum,
1258 /* If it's going away, ignore hook. */
1259 @@ -171,8 +210,19 @@
1262 afinfo->saveroute(skb, entry);
1264 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1266 + status = qih->outfn(entry, queuenum);
1267 + goto imq_skip_queue;
1271 status = qh->outfn(entry, queuenum);
1273 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1279 diff -uNr linux-2.6.34/net/netfilter/xt_IMQ.c linux-2.6.34-imq/net/netfilter/xt_IMQ.c
1280 --- linux-2.6.34/net/netfilter/xt_IMQ.c 1970-01-01 02:00:00.000000000 +0200
1281 +++ linux-2.6.34-imq/net/netfilter/xt_IMQ.c 2010-06-02 10:05:45.762097870 +0300
1284 + * This target marks packets to be enqueued to an imq device
1286 +#include <linux/module.h>
1287 +#include <linux/skbuff.h>
1288 +#include <linux/netfilter/x_tables.h>
1289 +#include <linux/netfilter/xt_IMQ.h>
1290 +#include <linux/imq.h>
1292 +static unsigned int imq_target(struct sk_buff *pskb,
1293 + const struct xt_target_param *par)
1295 + const struct xt_imq_info *mr = par->targinfo;
1297 + pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
1299 + return XT_CONTINUE;
1302 +static bool imq_checkentry(const struct xt_tgchk_param *par)
1304 + struct xt_imq_info *mr = par->targinfo;
1306 + if (mr->todev > IMQ_MAX_DEVS - 1) {
1307 + printk(KERN_WARNING
1308 + "IMQ: invalid device specified, highest is %u\n",
1309 + IMQ_MAX_DEVS - 1);
1316 +static struct xt_target xt_imq_reg[] __read_mostly = {
1319 + .family = AF_INET,
1320 + .checkentry = imq_checkentry,
1321 + .target = imq_target,
1322 + .targetsize = sizeof(struct xt_imq_info),
1323 + .table = "mangle",
1328 + .family = AF_INET6,
1329 + .checkentry = imq_checkentry,
1330 + .target = imq_target,
1331 + .targetsize = sizeof(struct xt_imq_info),
1332 + .table = "mangle",
1337 +static int __init imq_init(void)
1339 + return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1342 +static void __exit imq_fini(void)
1344 + xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1347 +module_init(imq_init);
1348 +module_exit(imq_fini);
1350 +MODULE_AUTHOR("http://www.linuximq.net");
1351 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
1352 +MODULE_LICENSE("GPL");
1353 +MODULE_ALIAS("ipt_IMQ");
1354 +MODULE_ALIAS("ip6t_IMQ");