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)
70 + * - Port to 2.6.29 + fix rmmod not working
72 + * 2009/04/20 - (Jussi Kivilinna)
73 + * - Use netdevice feature flags to avoid extra packet handling
74 + * by core networking layer and possibly increase performance.
76 + * 2009/09/26 - (Jussi Kivilinna)
77 + * - Add imq_nf_reinject_lockless to fix deadlock with
78 + * imq_nf_queue/imq_nf_reinject.
80 + * 2009/12/08 - (Jussi Kivilinna)
82 + * - Add check for skb->nf_queue_entry==NULL in imq_dev_xmit()
83 + * - Also add better error checking for skb->nf_queue_entry usage
85 + * 2010/02/25 - (Jussi Kivilinna)
88 + * 2010/08/12 - (Jussi Kivilinna)
91 + * Also, many thanks to pablo Sebastian Greco for making the initial
92 + * patch and to those who helped the testing.
94 + * More info at: http://www.linuximq.net/ (Andre Correa)
97 +#include <linux/module.h>
98 +#include <linux/kernel.h>
99 +#include <linux/moduleparam.h>
100 +#include <linux/list.h>
101 +#include <linux/skbuff.h>
102 +#include <linux/netdevice.h>
103 +#include <linux/etherdevice.h>
104 +#include <linux/rtnetlink.h>
105 +#include <linux/if_arp.h>
106 +#include <linux/netfilter.h>
107 +#include <linux/netfilter_ipv4.h>
108 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
109 + #include <linux/netfilter_ipv6.h>
111 +#include <linux/imq.h>
112 +#include <net/pkt_sched.h>
113 +#include <net/netfilter/nf_queue.h>
115 +static nf_hookfn imq_nf_hook;
117 +static struct nf_hook_ops imq_ingress_ipv4 = {
118 + .hook = imq_nf_hook,
119 + .owner = THIS_MODULE,
121 + .hooknum = NF_INET_PRE_ROUTING,
122 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
123 + .priority = NF_IP_PRI_MANGLE + 1
125 + .priority = NF_IP_PRI_NAT_DST + 1
129 +static struct nf_hook_ops imq_egress_ipv4 = {
130 + .hook = imq_nf_hook,
131 + .owner = THIS_MODULE,
133 + .hooknum = NF_INET_POST_ROUTING,
134 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
135 + .priority = NF_IP_PRI_LAST
137 + .priority = NF_IP_PRI_NAT_SRC - 1
141 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
142 +static struct nf_hook_ops imq_ingress_ipv6 = {
143 + .hook = imq_nf_hook,
144 + .owner = THIS_MODULE,
146 + .hooknum = NF_INET_PRE_ROUTING,
147 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
148 + .priority = NF_IP6_PRI_MANGLE + 1
150 + .priority = NF_IP6_PRI_NAT_DST + 1
154 +static struct nf_hook_ops imq_egress_ipv6 = {
155 + .hook = imq_nf_hook,
156 + .owner = THIS_MODULE,
158 + .hooknum = NF_INET_POST_ROUTING,
159 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
160 + .priority = NF_IP6_PRI_LAST
162 + .priority = NF_IP6_PRI_NAT_SRC - 1
167 +#if defined(CONFIG_IMQ_NUM_DEVS)
168 +static unsigned int numdevs = CONFIG_IMQ_NUM_DEVS;
170 +static unsigned int numdevs = IMQ_MAX_DEVS;
173 +static DEFINE_SPINLOCK(imq_nf_queue_lock);
175 +static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
178 +static struct net_device_stats *imq_get_stats(struct net_device *dev)
180 + return &dev->stats;
183 +/* called for packets kfree'd in qdiscs at places other than enqueue */
184 +static void imq_skb_destructor(struct sk_buff *skb)
186 + struct nf_queue_entry *entry = skb->nf_queue_entry;
188 + skb->nf_queue_entry = NULL;
191 + nf_queue_entry_release_refs(entry);
195 + skb_restore_cb(skb); /* kfree backup */
198 +/* locking not needed when called from imq_nf_queue */
199 +static void imq_nf_reinject_lockless(struct nf_queue_entry *entry,
200 + unsigned int verdict)
204 + if (!entry->next_outfn) {
205 + nf_reinject(entry, verdict);
209 + status = entry->next_outfn(entry, entry->next_queuenum);
211 + nf_queue_entry_release_refs(entry);
212 + kfree_skb(entry->skb);
217 +static void imq_nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
221 + if (!entry->next_outfn) {
222 + spin_lock_bh(&imq_nf_queue_lock);
223 + nf_reinject(entry, verdict);
224 + spin_unlock_bh(&imq_nf_queue_lock);
229 + local_bh_disable();
230 + status = entry->next_outfn(entry, entry->next_queuenum);
233 + nf_queue_entry_release_refs(entry);
234 + kfree_skb(entry->skb);
241 +static netdev_tx_t imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
243 + struct nf_queue_entry *entry = skb->nf_queue_entry;
245 + skb->nf_queue_entry = NULL;
246 + dev->trans_start = jiffies;
248 + dev->stats.tx_bytes += skb->len;
249 + dev->stats.tx_packets++;
251 + if (entry == NULL) {
252 + /* We don't know what is going on here.. packet is queued for
253 + * imq device, but (probably) not by us.
255 + * If this packet was not send here by imq_nf_queue(), then
256 + * skb_save_cb() was not used and skb_free() should not show:
257 + * WARNING: IMQ: kfree_skb: skb->cb_next:..
259 + * WARNING: IMQ: kfree_skb: skb->nf_queue_entry...
261 + * However if this message is shown, then IMQ is somehow broken
262 + * and you should report this to linuximq.net.
265 + /* imq_dev_xmit is black hole that eats all packets, report that
266 + * we eat this packet happily and increase dropped counters.
269 + dev->stats.tx_dropped++;
270 + dev_kfree_skb(skb);
272 + return NETDEV_TX_OK;
275 + skb_restore_cb(skb); /* restore skb->cb */
277 + skb->imq_flags = 0;
278 + skb->destructor = NULL;
280 + imq_nf_reinject(entry, NF_ACCEPT);
282 + return NETDEV_TX_OK;
285 +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num)
287 + struct net_device *dev;
288 + struct sk_buff *skb_orig, *skb, *skb_shared;
290 + struct netdev_queue *txq;
292 + int retval = -EINVAL;
294 + index = entry->skb->imq_flags & IMQ_F_IFMASK;
295 + if (unlikely(index > numdevs - 1)) {
296 + if (net_ratelimit())
297 + printk(KERN_WARNING
298 + "IMQ: invalid device specified, highest is %u\n",
304 + /* check for imq device by index from cache */
305 + dev = imq_devs_cache[index];
306 + if (unlikely(!dev)) {
309 + /* get device by name and cache result */
310 + snprintf(buf, sizeof(buf), "imq%d", index);
311 + dev = dev_get_by_name(&init_net, buf);
319 + imq_devs_cache[index] = dev;
323 + if (unlikely(!(dev->flags & IFF_UP))) {
324 + entry->skb->imq_flags = 0;
325 + imq_nf_reinject_lockless(entry, NF_ACCEPT);
329 + dev->last_rx = jiffies;
334 + /* skb has owner? => make clone */
335 + if (unlikely(skb->destructor)) {
337 + skb = skb_clone(skb, GFP_ATOMIC);
345 + skb->nf_queue_entry = entry;
347 + dev->stats.rx_bytes += skb->len;
348 + dev->stats.rx_packets++;
350 + txq = dev_pick_tx(dev, skb);
352 + q = rcu_dereference(txq->qdisc);
353 + if (unlikely(!q->enqueue))
354 + goto packet_not_eaten_by_imq_dev;
356 + spin_lock_bh(qdisc_lock(q));
358 + users = atomic_read(&skb->users);
360 + skb_shared = skb_get(skb); /* increase reference count by one */
361 + skb_save_cb(skb_shared); /* backup skb->cb, as qdisc layer will
363 + qdisc_enqueue_root(skb_shared, q); /* might kfree_skb */
365 + if (likely(atomic_read(&skb_shared->users) == users + 1)) {
366 + kfree_skb(skb_shared); /* decrease reference count by one */
368 + skb->destructor = &imq_skb_destructor;
372 + kfree_skb(skb_orig); /* free original */
374 + spin_unlock_bh(qdisc_lock(q));
376 + /* schedule qdisc dequeue */
377 + __netif_schedule(q);
382 + skb_restore_cb(skb_shared); /* restore skb->cb */
383 + skb->nf_queue_entry = NULL;
384 + /* qdisc dropped packet and decreased skb reference count of
385 + * skb, so we don't really want to and try refree as that would
386 + * actually destroy the skb. */
387 + spin_unlock_bh(qdisc_lock(q));
388 + goto packet_not_eaten_by_imq_dev;
391 +packet_not_eaten_by_imq_dev:
392 + /* cloned? restore original */
395 + entry->skb = skb_orig;
402 +static struct nf_queue_handler nfqh = {
404 + .outfn = imq_nf_queue,
407 +static unsigned int imq_nf_hook(unsigned int hook, struct sk_buff *pskb,
408 + const struct net_device *indev,
409 + const struct net_device *outdev,
410 + int (*okfn)(struct sk_buff *))
412 + if (pskb->imq_flags & IMQ_F_ENQUEUE)
418 +static int imq_close(struct net_device *dev)
420 + netif_stop_queue(dev);
424 +static int imq_open(struct net_device *dev)
426 + netif_start_queue(dev);
430 +static const struct net_device_ops imq_netdev_ops = {
431 + .ndo_open = imq_open,
432 + .ndo_stop = imq_close,
433 + .ndo_start_xmit = imq_dev_xmit,
434 + .ndo_get_stats = imq_get_stats,
437 +static void imq_setup(struct net_device *dev)
439 + dev->netdev_ops = &imq_netdev_ops;
440 + dev->type = ARPHRD_VOID;
442 + dev->tx_queue_len = 11000;
443 + dev->flags = IFF_NOARP;
444 + dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
445 + NETIF_F_GSO | NETIF_F_HW_CSUM |
447 + dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
450 +static int imq_validate(struct nlattr *tb[], struct nlattr *data[])
454 + if (tb[IFLA_ADDRESS]) {
455 + if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
459 + if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
460 + ret = -EADDRNOTAVAIL;
466 + printk(KERN_WARNING "IMQ: imq_validate failed (%d)\n", ret);
470 +static struct rtnl_link_ops imq_link_ops __read_mostly = {
473 + .setup = imq_setup,
474 + .validate = imq_validate,
477 +static int __init imq_init_hooks(void)
481 + nf_register_queue_imq_handler(&nfqh);
483 + err = nf_register_hook(&imq_ingress_ipv4);
487 + err = nf_register_hook(&imq_egress_ipv4);
491 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
492 + err = nf_register_hook(&imq_ingress_ipv6);
496 + err = nf_register_hook(&imq_egress_ipv6);
503 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
505 + nf_unregister_hook(&imq_ingress_ipv6);
507 + nf_unregister_hook(&imq_egress_ipv4);
510 + nf_unregister_hook(&imq_ingress_ipv4);
512 + nf_unregister_queue_imq_handler();
516 +static int __init imq_init_one(int index)
518 + struct net_device *dev;
521 + dev = alloc_netdev(0, "imq%d", imq_setup);
525 + ret = dev_alloc_name(dev, dev->name);
529 + dev->rtnl_link_ops = &imq_link_ops;
530 + ret = register_netdevice(dev);
540 +static int __init imq_init_devs(void)
544 + if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) {
545 + printk(KERN_ERR "IMQ: numdevs has to be betweed 1 and %u\n",
551 + err = __rtnl_link_register(&imq_link_ops);
553 + for (i = 0; i < numdevs && !err; i++)
554 + err = imq_init_one(i);
557 + __rtnl_link_unregister(&imq_link_ops);
558 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
565 +static int __init imq_init_module(void)
569 +#if defined(CONFIG_IMQ_NUM_DEVS)
570 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS > 16);
571 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS < 2);
572 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS - 1 > IMQ_F_IFMASK);
575 + err = imq_init_devs();
577 + printk(KERN_ERR "IMQ: Error trying imq_init_devs(net)\n");
581 + err = imq_init_hooks();
583 + printk(KERN_ERR "IMQ: Error trying imq_init_hooks()\n");
584 + rtnl_link_unregister(&imq_link_ops);
585 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
589 + printk(KERN_INFO "IMQ driver loaded successfully.\n");
591 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
592 + printk(KERN_INFO "\tHooking IMQ before NAT on PREROUTING.\n");
594 + printk(KERN_INFO "\tHooking IMQ after NAT on PREROUTING.\n");
596 +#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
597 + printk(KERN_INFO "\tHooking IMQ before NAT on POSTROUTING.\n");
599 + printk(KERN_INFO "\tHooking IMQ after NAT on POSTROUTING.\n");
605 +static void __exit imq_unhook(void)
607 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
608 + nf_unregister_hook(&imq_ingress_ipv6);
609 + nf_unregister_hook(&imq_egress_ipv6);
611 + nf_unregister_hook(&imq_ingress_ipv4);
612 + nf_unregister_hook(&imq_egress_ipv4);
614 + nf_unregister_queue_imq_handler();
617 +static void __exit imq_cleanup_devs(void)
619 + rtnl_link_unregister(&imq_link_ops);
620 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
623 +static void __exit imq_exit_module(void)
626 + imq_cleanup_devs();
627 + printk(KERN_INFO "IMQ driver unloaded successfully.\n");
630 +module_init(imq_init_module);
631 +module_exit(imq_exit_module);
633 +module_param(numdevs, int, 0);
634 +MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will "
636 +MODULE_AUTHOR("http://www.linuximq.net");
637 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See "
638 + "http://www.linuximq.net/ for more information.");
639 +MODULE_LICENSE("GPL");
640 +MODULE_ALIAS_RTNL_LINK("imq");
642 --- a/drivers/net/Kconfig
643 +++ b/drivers/net/Kconfig
644 @@ -124,6 +124,129 @@ config EQUALIZER
645 To compile this driver as a module, choose M here: the module
646 will be called eql. If unsure, say N.
649 + tristate "IMQ (intermediate queueing device) support"
650 + depends on NETDEVICES && NETFILTER
652 + The IMQ device(s) is used as placeholder for QoS queueing
653 + disciplines. Every packet entering/leaving the IP stack can be
654 + directed through the IMQ device where it's enqueued/dequeued to the
655 + attached qdisc. This allows you to treat network devices as classes
656 + and distribute bandwidth among them. Iptables is used to specify
657 + through which IMQ device, if any, packets travel.
659 + More information at: http://www.linuximq.net/
661 + To compile this driver as a module, choose M here: the module
662 + will be called imq. If unsure, say N.
665 + prompt "IMQ behavior (PRE/POSTROUTING)"
667 + default IMQ_BEHAVIOR_AB
670 + This settings defines how IMQ behaves in respect to its
671 + hooking in PREROUTING and POSTROUTING.
673 + IMQ can work in any of the following ways:
675 + PREROUTING | POSTROUTING
676 + -----------------|-------------------
677 + #1 After NAT | After NAT
678 + #2 After NAT | Before NAT
679 + #3 Before NAT | After NAT
680 + #4 Before NAT | Before NAT
682 + The default behavior is to hook before NAT on PREROUTING
683 + and after NAT on POSTROUTING (#3).
685 + This settings are specially usefull when trying to use IMQ
686 + to shape NATed clients.
688 + More information can be found at: www.linuximq.net
690 + If not sure leave the default settings alone.
692 +config IMQ_BEHAVIOR_AA
695 + This settings defines how IMQ behaves in respect to its
696 + hooking in PREROUTING and POSTROUTING.
698 + Choosing this option will make IMQ hook like this:
700 + PREROUTING: After NAT
701 + POSTROUTING: After NAT
703 + More information can be found at: www.linuximq.net
705 + If not sure leave the default settings alone.
707 +config IMQ_BEHAVIOR_AB
710 + This settings defines how IMQ behaves in respect to its
711 + hooking in PREROUTING and POSTROUTING.
713 + Choosing this option will make IMQ hook like this:
715 + PREROUTING: After NAT
716 + POSTROUTING: Before NAT
718 + More information can be found at: www.linuximq.net
720 + If not sure leave the default settings alone.
722 +config IMQ_BEHAVIOR_BA
725 + This settings defines how IMQ behaves in respect to its
726 + hooking in PREROUTING and POSTROUTING.
728 + Choosing this option will make IMQ hook like this:
730 + PREROUTING: Before NAT
731 + POSTROUTING: After NAT
733 + More information can be found at: www.linuximq.net
735 + If not sure leave the default settings alone.
737 +config IMQ_BEHAVIOR_BB
740 + This settings defines how IMQ behaves in respect to its
741 + hooking in PREROUTING and POSTROUTING.
743 + Choosing this option will make IMQ hook like this:
745 + PREROUTING: Before NAT
746 + POSTROUTING: Before NAT
748 + More information can be found at: www.linuximq.net
750 + If not sure leave the default settings alone.
756 + int "Number of IMQ devices"
762 + This settings defines how many IMQ devices will be
765 + The default value is 16.
767 + More information can be found at: www.linuximq.net
769 + If not sure leave the default settings alone.
772 tristate "Universal TUN/TAP device driver support"
774 --- a/drivers/net/Makefile
775 +++ b/drivers/net/Makefile
776 @@ -173,6 +173,7 @@ obj-$(CONFIG_SLHC) += slhc.o
777 obj-$(CONFIG_XEN_NETDEV_FRONTEND) += xen-netfront.o
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
785 +++ b/include/linux/imq.h
790 +/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
791 +#define IMQ_F_BITS 5
793 +#define IMQ_F_IFMASK 0x0f
794 +#define IMQ_F_ENQUEUE 0x10
796 +#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1)
800 --- a/include/linux/netdevice.h
801 +++ b/include/linux/netdevice.h
802 @@ -1323,6 +1323,7 @@ extern int dev_alloc_name(struct net_de
803 extern int dev_open(struct net_device *dev);
804 extern int dev_close(struct net_device *dev);
805 extern void dev_disable_lro(struct net_device *dev);
806 +extern struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb);
807 extern int dev_queue_xmit(struct sk_buff *skb);
808 extern int register_netdevice(struct net_device *dev);
809 extern void unregister_netdevice_queue(struct net_device *dev,
811 +++ b/include/linux/netfilter/xt_IMQ.h
816 +struct xt_imq_info {
817 + unsigned int todev; /* target imq device */
820 +#endif /* _XT_IMQ_H */
823 +++ b/include/linux/netfilter_ipv4/ipt_IMQ.h
828 +/* Backwards compatibility for old userspace */
829 +#include <linux/netfilter/xt_IMQ.h>
831 +#define ipt_imq_info xt_imq_info
833 +#endif /* _IPT_IMQ_H */
836 +++ b/include/linux/netfilter_ipv6/ip6t_IMQ.h
841 +/* Backwards compatibility for old userspace */
842 +#include <linux/netfilter/xt_IMQ.h>
844 +#define ip6t_imq_info xt_imq_info
846 +#endif /* _IP6T_IMQ_H */
848 --- a/include/linux/skbuff.h
849 +++ b/include/linux/skbuff.h
851 #include <linux/rcupdate.h>
852 #include <linux/dmaengine.h>
853 #include <linux/hrtimer.h>
854 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
855 +#include <linux/imq.h>
858 /* Don't change this without changing skb_csum_unnecessary! */
859 #define CHECKSUM_NONE 0
860 @@ -328,6 +331,9 @@ struct sk_buff {
861 * first. This is owned by whoever has the skb queued ATM.
863 char cb[48] __aligned(8);
864 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
868 unsigned long _skb_refdst;
870 @@ -364,6 +370,9 @@ struct sk_buff {
871 struct nf_conntrack *nfct;
872 struct sk_buff *nfct_reasm;
874 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
875 + struct nf_queue_entry *nf_queue_entry;
877 #ifdef CONFIG_BRIDGE_NETFILTER
878 struct nf_bridge_info *nf_bridge;
880 @@ -390,6 +399,10 @@ struct sk_buff {
884 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
885 + __u8 imq_flags:IMQ_F_BITS;
888 #ifdef CONFIG_NET_DMA
889 dma_cookie_t dma_cookie;
891 @@ -476,6 +489,12 @@ static inline struct rtable *skb_rtable(
892 return (struct rtable *)skb_dst(skb);
896 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
897 +extern int skb_save_cb(struct sk_buff *skb);
898 +extern int skb_restore_cb(struct sk_buff *skb);
901 extern void kfree_skb(struct sk_buff *skb);
902 extern void consume_skb(struct sk_buff *skb);
903 extern void __kfree_skb(struct sk_buff *skb);
904 @@ -2098,6 +2117,10 @@ static inline void __nf_copy(struct sk_b
905 dst->nfct_reasm = src->nfct_reasm;
906 nf_conntrack_get_reasm(src->nfct_reasm);
908 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
909 + dst->imq_flags = src->imq_flags;
910 + dst->nf_queue_entry = src->nf_queue_entry;
912 #ifdef CONFIG_BRIDGE_NETFILTER
913 dst->nf_bridge = src->nf_bridge;
914 nf_bridge_get(src->nf_bridge);
915 --- a/include/net/netfilter/nf_queue.h
916 +++ b/include/net/netfilter/nf_queue.h
917 @@ -13,6 +13,12 @@ struct nf_queue_entry {
918 struct net_device *indev;
919 struct net_device *outdev;
920 int (*okfn)(struct sk_buff *);
922 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
923 + int (*next_outfn)(struct nf_queue_entry *entry,
924 + unsigned int queuenum);
925 + unsigned int next_queuenum;
929 #define nf_queue_entry_reroute(x) ((void *)x + sizeof(struct nf_queue_entry))
930 @@ -30,5 +36,11 @@ extern int nf_unregister_queue_handler(u
931 const struct nf_queue_handler *qh);
932 extern void nf_unregister_queue_handlers(const struct nf_queue_handler *qh);
933 extern void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
934 +extern void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
936 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
937 +extern void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
938 +extern void nf_unregister_queue_imq_handler(void);
941 #endif /* _NF_QUEUE_H */
945 #include <net/net_namespace.h>
946 #include <net/sock.h>
947 #include <linux/rtnetlink.h>
948 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
949 +#include <linux/imq.h>
951 #include <linux/proc_fs.h>
952 #include <linux/seq_file.h>
953 #include <linux/stat.h>
954 @@ -1995,7 +1998,11 @@ int dev_hard_start_xmit(struct sk_buff *
955 int rc = NETDEV_TX_OK;
957 if (likely(!skb->next)) {
958 - if (!list_empty(&ptype_all))
959 + if (!list_empty(&ptype_all)
960 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
961 + && !(skb->imq_flags & IMQ_F_ENQUEUE)
964 dev_queue_xmit_nit(skb, dev);
967 @@ -2119,8 +2126,7 @@ static inline u16 dev_cap_txqueue(struct
971 -static struct netdev_queue *dev_pick_tx(struct net_device *dev,
972 - struct sk_buff *skb)
973 +struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb)
976 const struct net_device_ops *ops = dev->netdev_ops;
977 @@ -2149,6 +2155,7 @@ static struct netdev_queue *dev_pick_tx(
978 skb_set_queue_mapping(skb, queue_index);
979 return netdev_get_tx_queue(dev, queue_index);
981 +EXPORT_SYMBOL(dev_pick_tx);
983 static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
984 struct net_device *dev,
985 --- a/net/core/skbuff.c
986 +++ b/net/core/skbuff.c
989 static struct kmem_cache *skbuff_head_cache __read_mostly;
990 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
991 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
992 +static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
995 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
996 struct pipe_buffer *buf)
997 @@ -91,6 +94,83 @@ static int sock_pipe_buf_steal(struct pi
1001 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1002 +/* Control buffer save/restore for IMQ devices */
1003 +struct skb_cb_table {
1009 +static DEFINE_SPINLOCK(skb_cb_store_lock);
1011 +int skb_save_cb(struct sk_buff *skb)
1013 + struct skb_cb_table *next;
1015 + next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
1019 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1021 + memcpy(next->cb, skb->cb, sizeof(skb->cb));
1022 + next->cb_next = skb->cb_next;
1024 + atomic_set(&next->refcnt, 1);
1026 + skb->cb_next = next;
1029 +EXPORT_SYMBOL(skb_save_cb);
1031 +int skb_restore_cb(struct sk_buff *skb)
1033 + struct skb_cb_table *next;
1035 + if (!skb->cb_next)
1038 + next = skb->cb_next;
1040 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1042 + memcpy(skb->cb, next->cb, sizeof(skb->cb));
1043 + skb->cb_next = next->cb_next;
1045 + spin_lock(&skb_cb_store_lock);
1047 + if (atomic_dec_and_test(&next->refcnt)) {
1048 + kmem_cache_free(skbuff_cb_store_cache, next);
1051 + spin_unlock(&skb_cb_store_lock);
1055 +EXPORT_SYMBOL(skb_restore_cb);
1057 +static void skb_copy_stored_cb(struct sk_buff *new, const struct sk_buff *__old)
1059 + struct skb_cb_table *next;
1060 + struct sk_buff *old;
1062 + if (!__old->cb_next) {
1063 + new->cb_next = NULL;
1067 + spin_lock(&skb_cb_store_lock);
1069 + old = (struct sk_buff *)__old;
1071 + next = old->cb_next;
1072 + atomic_inc(&next->refcnt);
1073 + new->cb_next = next;
1075 + spin_unlock(&skb_cb_store_lock);
1079 /* Pipe buffer operations for a socket. */
1080 static const struct pipe_buf_operations sock_pipe_buf_ops = {
1081 @@ -378,6 +458,26 @@ static void skb_release_head_state(struc
1083 skb->destructor(skb);
1085 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1086 + /* This should not happen. When it does, avoid memleak by restoring
1087 + the chain of cb-backups. */
1088 + while(skb->cb_next != NULL) {
1089 + if (net_ratelimit())
1090 + printk(KERN_WARNING "IMQ: kfree_skb: skb->cb_next: "
1091 + "%08x\n", (unsigned int)skb->cb_next);
1093 + skb_restore_cb(skb);
1095 + /* This should not happen either, nf_queue_entry is nullified in
1096 + * imq_dev_xmit(). If we have non-NULL nf_queue_entry then we are
1097 + * leaking entry pointers, maybe memory. We don't know if this is
1098 + * pointer to already freed memory, or should this be freed.
1099 + * If this happens we need to add refcounting, etc for nf_queue_entry.
1101 + if (skb->nf_queue_entry && net_ratelimit())
1102 + printk(KERN_WARNING
1103 + "IMQ: kfree_skb: skb->nf_queue_entry != NULL");
1105 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1106 nf_conntrack_put(skb->nfct);
1107 nf_conntrack_put_reasm(skb->nfct_reasm);
1108 @@ -514,6 +614,9 @@ static void __copy_skb_header(struct sk_
1109 new->sp = secpath_get(old->sp);
1111 memcpy(new->cb, old->cb, sizeof(old->cb));
1112 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1113 + skb_copy_stored_cb(new, old);
1115 new->csum = old->csum;
1116 new->local_df = old->local_df;
1117 new->pkt_type = old->pkt_type;
1118 @@ -2765,6 +2868,13 @@ void __init skb_init(void)
1120 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1122 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1123 + skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1124 + sizeof(struct skb_cb_table),
1126 + SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1132 --- a/net/netfilter/Kconfig
1133 +++ b/net/netfilter/Kconfig
1134 @@ -455,6 +455,18 @@ config NETFILTER_XT_TARGET_LED
1135 For more information on the LEDs available on your system, see
1136 Documentation/leds-class.txt
1138 +config NETFILTER_XT_TARGET_IMQ
1139 + tristate '"IMQ" target support'
1140 + depends on NETFILTER_XTABLES
1141 + depends on IP_NF_MANGLE || IP6_NF_MANGLE
1143 + default m if NETFILTER_ADVANCED=n
1145 + This option adds a `IMQ' target which is used to specify if and
1146 + to which imq device packets should get enqueued/dequeued.
1148 + To compile it as a module, choose M here. If unsure, say N.
1150 config NETFILTER_XT_TARGET_MARK
1151 tristate '"MARK" target support'
1152 depends on NETFILTER_ADVANCED
1153 --- a/net/netfilter/Makefile
1154 +++ b/net/netfilter/Makefile
1155 @@ -51,6 +51,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_CONNSEC
1156 obj-$(CONFIG_NETFILTER_XT_TARGET_CT) += xt_CT.o
1157 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
1158 obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
1159 +obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
1160 obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
1161 obj-$(CONFIG_NETFILTER_XT_TARGET_NFLOG) += xt_NFLOG.o
1162 obj-$(CONFIG_NETFILTER_XT_TARGET_NFQUEUE) += xt_NFQUEUE.o
1163 --- a/net/netfilter/nf_queue.c
1164 +++ b/net/netfilter/nf_queue.c
1165 @@ -22,6 +22,26 @@ static const struct nf_queue_handler __r
1167 static DEFINE_MUTEX(queue_handler_mutex);
1169 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1170 +static const struct nf_queue_handler *queue_imq_handler;
1172 +void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
1174 + mutex_lock(&queue_handler_mutex);
1175 + rcu_assign_pointer(queue_imq_handler, qh);
1176 + mutex_unlock(&queue_handler_mutex);
1178 +EXPORT_SYMBOL(nf_register_queue_imq_handler);
1180 +void nf_unregister_queue_imq_handler(void)
1182 + mutex_lock(&queue_handler_mutex);
1183 + rcu_assign_pointer(queue_imq_handler, NULL);
1184 + mutex_unlock(&queue_handler_mutex);
1186 +EXPORT_SYMBOL(nf_unregister_queue_imq_handler);
1189 /* return EBUSY when somebody else is registered, return EEXIST if the
1190 * same handler is registered, return 0 in case of success. */
1191 int nf_register_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh)
1192 @@ -82,7 +102,7 @@ void nf_unregister_queue_handlers(const
1194 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
1196 -static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1197 +void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1199 /* Release those devices we held, or Alexey will kill me. */
1201 @@ -102,6 +122,7 @@ static void nf_queue_entry_release_refs(
1202 /* Drop reference to owner of hook which queued us. */
1203 module_put(entry->elem->owner);
1205 +EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
1208 * Any packet that leaves via this function must come back
1209 @@ -123,12 +144,26 @@ static int __nf_queue(struct sk_buff *sk
1211 const struct nf_afinfo *afinfo;
1212 const struct nf_queue_handler *qh;
1213 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1214 + const struct nf_queue_handler *qih = NULL;
1217 /* QUEUE == DROP if noone is waiting, to be safe. */
1220 qh = rcu_dereference(queue_handler[pf]);
1221 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1222 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1223 + if (pf == PF_INET || pf == PF_INET6)
1225 + if (pf == PF_INET)
1227 + qih = rcu_dereference(queue_imq_handler);
1235 afinfo = nf_get_afinfo(pf);
1236 @@ -147,6 +182,10 @@ static int __nf_queue(struct sk_buff *sk
1240 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1241 + .next_outfn = qh ? qh->outfn : NULL,
1242 + .next_queuenum = queuenum,
1246 /* If it's going away, ignore hook. */
1247 @@ -173,8 +212,19 @@ static int __nf_queue(struct sk_buff *sk
1250 afinfo->saveroute(skb, entry);
1252 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1254 + status = qih->outfn(entry, queuenum);
1255 + goto imq_skip_queue;
1259 status = qh->outfn(entry, queuenum);
1261 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1268 +++ b/net/netfilter/xt_IMQ.c
1271 + * This target marks packets to be enqueued to an imq device
1273 +#include <linux/module.h>
1274 +#include <linux/skbuff.h>
1275 +#include <linux/netfilter/x_tables.h>
1276 +#include <linux/netfilter/xt_IMQ.h>
1277 +#include <linux/imq.h>
1279 +static unsigned int imq_target(struct sk_buff *pskb,
1280 + const struct xt_action_param *par)
1282 + const struct xt_imq_info *mr = par->targinfo;
1284 + pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
1286 + return XT_CONTINUE;
1289 +static int imq_checkentry(const struct xt_tgchk_param *par)
1291 + struct xt_imq_info *mr = par->targinfo;
1293 + if (mr->todev > IMQ_MAX_DEVS - 1) {
1294 + printk(KERN_WARNING
1295 + "IMQ: invalid device specified, highest is %u\n",
1296 + IMQ_MAX_DEVS - 1);
1303 +static struct xt_target xt_imq_reg[] __read_mostly = {
1306 + .family = AF_INET,
1307 + .checkentry = imq_checkentry,
1308 + .target = imq_target,
1309 + .targetsize = sizeof(struct xt_imq_info),
1310 + .table = "mangle",
1315 + .family = AF_INET6,
1316 + .checkentry = imq_checkentry,
1317 + .target = imq_target,
1318 + .targetsize = sizeof(struct xt_imq_info),
1319 + .table = "mangle",
1324 +static int __init imq_init(void)
1326 + return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1329 +static void __exit imq_fini(void)
1331 + xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1334 +module_init(imq_init);
1335 +module_exit(imq_fini);
1337 +MODULE_AUTHOR("http://www.linuximq.net");
1338 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
1339 +MODULE_LICENSE("GPL");
1340 +MODULE_ALIAS("ipt_IMQ");
1341 +MODULE_ALIAS("ip6t_IMQ");