2 +++ b/drivers/net/imq.c
5 + * Pseudo-driver for the intermediate queue device.
7 + * This program is free software; you can redistribute it and/or
8 + * modify it under the terms of the GNU General Public License
9 + * as published by the Free Software Foundation; either version
10 + * 2 of the License, or (at your option) any later version.
12 + * Authors: Patrick McHardy, <kaber@trash.net>
14 + * The first version was written by Martin Devera, <devik@cdi.cz>
16 + * Credits: Jan Rafaj <imq2t@cedric.vabo.cz>
17 + * - Update patch to 2.4.21
18 + * Sebastian Strollo <sstrollo@nortelnetworks.com>
19 + * - Fix "Dead-loop on netdevice imq"-issue
20 + * Marcel Sebek <sebek64@post.cz>
21 + * - Update to 2.6.2-rc1
23 + * After some time of inactivity there is a group taking care
24 + * of IMQ again: http://www.linuximq.net
27 + * 2004/06/30 - New version of IMQ patch to kernels <=2.6.7
28 + * including the following changes:
30 + * - Correction of ipv6 support "+"s issue (Hasso Tepper)
31 + * - Correction of imq_init_devs() issue that resulted in
32 + * kernel OOPS unloading IMQ as module (Norbert Buchmuller)
33 + * - Addition of functionality to choose number of IMQ devices
34 + * during kernel config (Andre Correa)
35 + * - Addition of functionality to choose how IMQ hooks on
36 + * PRE and POSTROUTING (after or before NAT) (Andre Correa)
37 + * - Cosmetic corrections (Norbert Buchmuller) (Andre Correa)
40 + * 2005/12/16 - IMQ versions between 2.6.7 and 2.6.13 were
41 + * released with almost no problems. 2.6.14-x was released
42 + * with some important changes: nfcache was removed; After
43 + * some weeks of trouble we figured out that some IMQ fields
44 + * in skb were missing in skbuff.c - skb_clone and copy_skb_header.
45 + * These functions are correctly patched by this new patch version.
47 + * Thanks for all who helped to figure out all the problems with
48 + * 2.6.14.x: Patrick McHardy, Rune Kock, VeNoMouS, Max CtRiX,
49 + * Kevin Shanahan, Richard Lucassen, Valery Dachev (hopefully
50 + * I didn't forget anybody). I apologize again for my lack of time.
53 + * 2008/06/17 - 2.6.25 - Changed imq.c to use qdisc_run() instead
54 + * of qdisc_restart() and moved qdisc_run() to tasklet to avoid
55 + * recursive locking. New initialization routines to fix 'rmmod' not
56 + * working anymore. Used code from ifb.c. (Jussi Kivilinna)
58 + * 2008/08/06 - 2.6.26 - (JK)
59 + * - Replaced tasklet with 'netif_schedule()'.
60 + * - Cleaned up and added comments for imq_nf_queue().
63 + * - Add skb_save_cb/skb_restore_cb helper functions for backuping
64 + * control buffer. This is needed because qdisc-layer on kernels
65 + * 2.6.27 and newer overwrite control buffer. (Jussi Kivilinna)
66 + * - Add better locking for IMQ device. Hopefully this will solve
67 + * SMP issues. (Jussi Kivilinna)
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 --- a/drivers/net/Kconfig
572 +++ b/drivers/net/Kconfig
573 @@ -109,6 +109,129 @@
574 To compile this driver as a module, choose M here: the module
575 will be called eql. If unsure, say N.
578 + tristate "IMQ (intermediate queueing device) support"
579 + depends on NETDEVICES && NETFILTER
581 + The IMQ device(s) is used as placeholder for QoS queueing
582 + disciplines. Every packet entering/leaving the IP stack can be
583 + directed through the IMQ device where it's enqueued/dequeued to the
584 + attached qdisc. This allows you to treat network devices as classes
585 + and distribute bandwidth among them. Iptables is used to specify
586 + through which IMQ device, if any, packets travel.
588 + More information at: http://www.linuximq.net/
590 + To compile this driver as a module, choose M here: the module
591 + will be called imq. If unsure, say N.
594 + prompt "IMQ behavior (PRE/POSTROUTING)"
596 + default IMQ_BEHAVIOR_AB
599 + This settings defines how IMQ behaves in respect to its
600 + hooking in PREROUTING and POSTROUTING.
602 + IMQ can work in any of the following ways:
604 + PREROUTING | POSTROUTING
605 + -----------------|-------------------
606 + #1 After NAT | After NAT
607 + #2 After NAT | Before NAT
608 + #3 Before NAT | After NAT
609 + #4 Before NAT | Before NAT
611 + The default behavior is to hook before NAT on PREROUTING
612 + and after NAT on POSTROUTING (#3).
614 + This settings are specially usefull when trying to use IMQ
615 + to shape NATed clients.
617 + More information can be found at: www.linuximq.net
619 + If not sure leave the default settings alone.
621 +config IMQ_BEHAVIOR_AA
624 + This settings defines how IMQ behaves in respect to its
625 + hooking in PREROUTING and POSTROUTING.
627 + Choosing this option will make IMQ hook like this:
629 + PREROUTING: After NAT
630 + POSTROUTING: After NAT
632 + More information can be found at: www.linuximq.net
634 + If not sure leave the default settings alone.
636 +config IMQ_BEHAVIOR_AB
639 + This settings defines how IMQ behaves in respect to its
640 + hooking in PREROUTING and POSTROUTING.
642 + Choosing this option will make IMQ hook like this:
644 + PREROUTING: After NAT
645 + POSTROUTING: Before NAT
647 + More information can be found at: www.linuximq.net
649 + If not sure leave the default settings alone.
651 +config IMQ_BEHAVIOR_BA
654 + This settings defines how IMQ behaves in respect to its
655 + hooking in PREROUTING and POSTROUTING.
657 + Choosing this option will make IMQ hook like this:
659 + PREROUTING: Before NAT
660 + POSTROUTING: After NAT
662 + More information can be found at: www.linuximq.net
664 + If not sure leave the default settings alone.
666 +config IMQ_BEHAVIOR_BB
669 + This settings defines how IMQ behaves in respect to its
670 + hooking in PREROUTING and POSTROUTING.
672 + Choosing this option will make IMQ hook like this:
674 + PREROUTING: Before NAT
675 + POSTROUTING: Before NAT
677 + More information can be found at: www.linuximq.net
679 + If not sure leave the default settings alone.
685 + int "Number of IMQ devices"
691 + This settings defines how many IMQ devices will be
694 + The default value is 16.
696 + More information can be found at: www.linuximq.net
698 + If not sure leave the default settings alone.
701 tristate "Universal TUN/TAP device driver support"
703 --- a/drivers/net/Makefile
704 +++ b/drivers/net/Makefile
706 obj-$(CONFIG_XEN_NETDEV_FRONTEND) += xen-netfront.o
708 obj-$(CONFIG_DUMMY) += dummy.o
709 +obj-$(CONFIG_IMQ) += imq.o
710 obj-$(CONFIG_IFB) += ifb.o
711 obj-$(CONFIG_MACVLAN) += macvlan.o
712 obj-$(CONFIG_DE600) += de600.o
714 +++ b/include/linux/imq.h
719 +/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
720 +#define IMQ_F_BITS 5
722 +#define IMQ_F_IFMASK 0x0f
723 +#define IMQ_F_ENQUEUE 0x10
725 +#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1)
730 +++ b/include/linux/netfilter_ipv4/ipt_IMQ.h
735 +/* Backwards compatibility for old userspace */
736 +#include <linux/netfilter/xt_IMQ.h>
738 +#define ipt_imq_info xt_imq_info
740 +#endif /* _IPT_IMQ_H */
743 +++ b/include/linux/netfilter_ipv6/ip6t_IMQ.h
748 +/* Backwards compatibility for old userspace */
749 +#include <linux/netfilter/xt_IMQ.h>
751 +#define ip6t_imq_info xt_imq_info
753 +#endif /* _IP6T_IMQ_H */
755 --- a/include/linux/skbuff.h
756 +++ b/include/linux/skbuff.h
758 #include <linux/rcupdate.h>
759 #include <linux/dmaengine.h>
760 #include <linux/hrtimer.h>
761 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
762 +#include <linux/imq.h>
765 #define HAVE_ALLOC_SKB /* For the drivers to know */
766 #define HAVE_ALIGNABLE_SKB /* Ditto 8) */
768 * first. This is owned by whoever has the skb queued ATM.
771 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
778 struct nf_conntrack *nfct;
779 struct sk_buff *nfct_reasm;
781 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
782 + struct nf_queue_entry *nf_queue_entry;
784 #ifdef CONFIG_BRIDGE_NETFILTER
785 struct nf_bridge_info *nf_bridge;
788 __u8 do_not_encrypt:1;
790 /* 0/13/14 bit hole */
791 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
792 + __u8 imq_flags:IMQ_F_BITS;
795 #ifdef CONFIG_NET_DMA
796 dma_cookie_t dma_cookie;
798 enum dma_data_direction dir);
802 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
803 +extern int skb_save_cb(struct sk_buff *skb);
804 +extern int skb_restore_cb(struct sk_buff *skb);
807 extern void kfree_skb(struct sk_buff *skb);
808 extern void __kfree_skb(struct sk_buff *skb);
809 extern struct sk_buff *__alloc_skb(unsigned int size,
810 @@ -1804,6 +1822,10 @@
811 dst->nfct_reasm = src->nfct_reasm;
812 nf_conntrack_get_reasm(src->nfct_reasm);
814 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
815 + dst->imq_flags = src->imq_flags;
816 + dst->nf_queue_entry = src->nf_queue_entry;
818 #ifdef CONFIG_BRIDGE_NETFILTER
819 dst->nf_bridge = src->nf_bridge;
820 nf_bridge_get(src->nf_bridge);
824 #include <net/net_namespace.h>
825 #include <net/sock.h>
826 #include <linux/rtnetlink.h>
827 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
828 +#include <linux/imq.h>
830 #include <linux/proc_fs.h>
831 #include <linux/seq_file.h>
832 #include <linux/stat.h>
833 @@ -1655,7 +1658,11 @@
834 struct netdev_queue *txq)
836 if (likely(!skb->next)) {
837 - if (!list_empty(&ptype_all))
838 + if (!list_empty(&ptype_all)
839 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
840 + && !(skb->imq_flags & IMQ_F_ENQUEUE)
843 dev_queue_xmit_nit(skb, dev);
845 if (netif_needs_gso(dev, skb)) {
846 @@ -1746,8 +1753,7 @@
847 return (u16) (((u64) hash * dev->real_num_tx_queues) >> 32);
850 -static struct netdev_queue *dev_pick_tx(struct net_device *dev,
851 - struct sk_buff *skb)
852 +struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb)
856 @@ -1759,6 +1765,7 @@
857 skb_set_queue_mapping(skb, queue_index);
858 return netdev_get_tx_queue(dev, queue_index);
860 +EXPORT_SYMBOL(dev_pick_tx);
863 * dev_queue_xmit - transmit a buffer
864 --- a/include/linux/netdevice.h
865 +++ b/include/linux/netdevice.h
867 extern int dev_open(struct net_device *dev);
868 extern int dev_close(struct net_device *dev);
869 extern void dev_disable_lro(struct net_device *dev);
870 +extern struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb);
871 extern int dev_queue_xmit(struct sk_buff *skb);
872 extern int register_netdevice(struct net_device *dev);
873 extern void unregister_netdevice(struct net_device *dev);
875 +++ b/include/linux/netfilter/xt_IMQ.h
880 +struct xt_imq_info {
881 + unsigned int todev; /* target imq device */
884 +#endif /* _XT_IMQ_H */
886 --- a/include/net/netfilter/nf_queue.h
887 +++ b/include/net/netfilter/nf_queue.h
889 struct net_device *indev;
890 struct net_device *outdev;
891 int (*okfn)(struct sk_buff *);
893 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
894 + int (*next_outfn)(struct nf_queue_entry *entry,
895 + unsigned int queuenum);
896 + unsigned int next_queuenum;
900 #define nf_queue_entry_reroute(x) ((void *)x + sizeof(struct nf_queue_entry))
902 const struct nf_queue_handler *qh);
903 extern void nf_unregister_queue_handlers(const struct nf_queue_handler *qh);
904 extern void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
905 +extern void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
907 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
908 +extern void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
909 +extern void nf_unregister_queue_imq_handler(void);
912 #endif /* _NF_QUEUE_H */
913 --- a/net/core/skbuff.c
914 +++ b/net/core/skbuff.c
917 static struct kmem_cache *skbuff_head_cache __read_mostly;
918 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
919 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
920 +static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
923 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
924 struct pipe_buffer *buf)
929 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
930 +/* Control buffer save/restore for IMQ devices */
931 +struct skb_cb_table {
937 +static DEFINE_SPINLOCK(skb_cb_store_lock);
939 +int skb_save_cb(struct sk_buff *skb)
941 + struct skb_cb_table *next;
943 + next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
947 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
949 + memcpy(next->cb, skb->cb, sizeof(skb->cb));
950 + next->cb_next = skb->cb_next;
952 + atomic_set(&next->refcnt, 1);
954 + skb->cb_next = next;
957 +EXPORT_SYMBOL(skb_save_cb);
959 +int skb_restore_cb(struct sk_buff *skb)
961 + struct skb_cb_table *next;
966 + next = skb->cb_next;
968 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
970 + memcpy(skb->cb, next->cb, sizeof(skb->cb));
971 + skb->cb_next = next->cb_next;
973 + spin_lock(&skb_cb_store_lock);
975 + if (atomic_dec_and_test(&next->refcnt)) {
976 + kmem_cache_free(skbuff_cb_store_cache, next);
979 + spin_unlock(&skb_cb_store_lock);
983 +EXPORT_SYMBOL(skb_restore_cb);
985 +static void skb_copy_stored_cb(struct sk_buff *new, struct sk_buff *old)
987 + struct skb_cb_table *next;
989 + if (!old->cb_next) {
994 + spin_lock(&skb_cb_store_lock);
996 + next = old->cb_next;
997 + atomic_inc(&next->refcnt);
998 + new->cb_next = next;
1000 + spin_unlock(&skb_cb_store_lock);
1004 /* Pipe buffer operations for a socket. */
1005 static struct pipe_buf_operations sock_pipe_buf_ops = {
1006 @@ -381,6 +458,15 @@
1008 skb->destructor(skb);
1010 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1011 + /* This should not happen. When it does, avoid memleak by restoring
1012 + the chain of cb-backups. */
1013 + while(skb->cb_next != NULL) {
1014 + printk(KERN_WARNING "kfree_skb: skb->cb_next: %08x\n",
1016 + skb_restore_cb(skb);
1019 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1020 nf_conntrack_put(skb->nfct);
1021 nf_conntrack_put_reasm(skb->nfct_reasm);
1023 new->sp = secpath_get(old->sp);
1025 memcpy(new->cb, old->cb, sizeof(old->cb));
1026 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1027 + skb_copy_stored_cb(new, old);
1029 new->csum_start = old->csum_start;
1030 new->csum_offset = old->csum_offset;
1031 new->local_df = old->local_df;
1032 @@ -2397,6 +2486,13 @@
1034 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1036 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1037 + skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1038 + sizeof(struct skb_cb_table),
1040 + SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1046 --- a/net/netfilter/Kconfig
1047 +++ b/net/netfilter/Kconfig
1048 @@ -357,6 +357,18 @@
1050 To compile it as a module, choose M here. If unsure, say N.
1052 +config NETFILTER_XT_TARGET_IMQ
1053 + tristate '"IMQ" target support'
1054 + depends on NETFILTER_XTABLES
1055 + depends on IP_NF_MANGLE || IP6_NF_MANGLE
1057 + default m if NETFILTER_ADVANCED=n
1059 + This option adds a `IMQ' target which is used to specify if and
1060 + to which imq device packets should get enqueued/dequeued.
1062 + To compile it as a module, choose M here. If unsure, say N.
1064 config NETFILTER_XT_TARGET_MARK
1065 tristate '"MARK" target support'
1066 default m if NETFILTER_ADVANCED=n
1067 --- a/net/netfilter/Makefile
1068 +++ b/net/netfilter/Makefile
1070 obj-$(CONFIG_NETFILTER_XT_TARGET_CONNMARK) += xt_CONNMARK.o
1071 obj-$(CONFIG_NETFILTER_XT_TARGET_CONNSECMARK) += xt_CONNSECMARK.o
1072 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
1073 +obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
1074 obj-$(CONFIG_NETFILTER_XT_TARGET_MARK) += xt_MARK.o
1075 obj-$(CONFIG_NETFILTER_XT_TARGET_NFLOG) += xt_NFLOG.o
1076 obj-$(CONFIG_NETFILTER_XT_TARGET_NFQUEUE) += xt_NFQUEUE.o
1077 --- a/net/netfilter/nf_queue.c
1078 +++ b/net/netfilter/nf_queue.c
1081 static DEFINE_MUTEX(queue_handler_mutex);
1083 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1084 +static const struct nf_queue_handler *queue_imq_handler;
1086 +void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
1088 + mutex_lock(&queue_handler_mutex);
1089 + rcu_assign_pointer(queue_imq_handler, qh);
1090 + mutex_unlock(&queue_handler_mutex);
1092 +EXPORT_SYMBOL(nf_register_queue_imq_handler);
1094 +void nf_unregister_queue_imq_handler(void)
1096 + mutex_lock(&queue_handler_mutex);
1097 + rcu_assign_pointer(queue_imq_handler, NULL);
1098 + mutex_unlock(&queue_handler_mutex);
1100 +EXPORT_SYMBOL(nf_unregister_queue_imq_handler);
1103 /* return EBUSY when somebody else is registered, return EEXIST if the
1104 * same handler is registered, return 0 in case of success. */
1105 int nf_register_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh)
1108 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
1110 -static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1111 +void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1113 /* Release those devices we held, or Alexey will kill me. */
1116 /* Drop reference to owner of hook which queued us. */
1117 module_put(entry->elem->owner);
1119 +EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
1122 * Any packet that leaves via this function must come back
1123 @@ -121,12 +142,26 @@
1125 const struct nf_afinfo *afinfo;
1126 const struct nf_queue_handler *qh;
1127 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1128 + const struct nf_queue_handler *qih = NULL;
1131 /* QUEUE == DROP if noone is waiting, to be safe. */
1134 qh = rcu_dereference(queue_handler[pf]);
1135 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1136 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1137 + if (pf == PF_INET || pf == PF_INET6)
1139 + if (pf == PF_INET)
1141 + qih = rcu_dereference(queue_imq_handler);
1149 afinfo = nf_get_afinfo(pf);
1150 @@ -145,6 +180,10 @@
1154 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1155 + .next_outfn = qh ? qh->outfn : NULL,
1156 + .next_queuenum = queuenum,
1160 /* If it's going away, ignore hook. */
1161 @@ -170,8 +209,19 @@
1164 afinfo->saveroute(skb, entry);
1166 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1168 + status = qih->outfn(entry, queuenum);
1169 + goto imq_skip_queue;
1173 status = qh->outfn(entry, queuenum);
1175 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1182 +++ b/net/netfilter/xt_IMQ.c
1185 + * This target marks packets to be enqueued to an imq device
1187 +#include <linux/module.h>
1188 +#include <linux/skbuff.h>
1189 +#include <linux/netfilter/x_tables.h>
1190 +#include <linux/netfilter/xt_IMQ.h>
1191 +#include <linux/imq.h>
1193 +static unsigned int imq_target(struct sk_buff *pskb,
1194 + const struct xt_target_param *par)
1196 + const struct xt_imq_info *mr = par->targinfo;
1198 + pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
1200 + return XT_CONTINUE;
1203 +static bool imq_checkentry(const struct xt_tgchk_param *par)
1205 + struct xt_imq_info *mr = par->targinfo;
1207 + if (mr->todev > IMQ_MAX_DEVS - 1) {
1208 + printk(KERN_WARNING
1209 + "IMQ: invalid device specified, highest is %u\n",
1210 + IMQ_MAX_DEVS - 1);
1217 +static struct xt_target xt_imq_reg[] __read_mostly = {
1220 + .family = AF_INET,
1221 + .checkentry = imq_checkentry,
1222 + .target = imq_target,
1223 + .targetsize = sizeof(struct xt_imq_info),
1224 + .table = "mangle",
1229 + .family = AF_INET6,
1230 + .checkentry = imq_checkentry,
1231 + .target = imq_target,
1232 + .targetsize = sizeof(struct xt_imq_info),
1233 + .table = "mangle",
1238 +static int __init imq_init(void)
1240 + return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1243 +static void __exit imq_fini(void)
1245 + xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1248 +module_init(imq_init);
1249 +module_exit(imq_fini);
1251 +MODULE_AUTHOR("http://www.linuximq.net");
1252 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
1253 +MODULE_LICENSE("GPL");
1254 +MODULE_ALIAS("ipt_IMQ");
1255 +MODULE_ALIAS("ip6t_IMQ");