1 diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
2 index f184fb5..0e08522 100644
3 --- a/drivers/net/Kconfig
4 +++ b/drivers/net/Kconfig
5 @@ -234,6 +234,125 @@ config RIONET_RX_SIZE
10 + tristate "IMQ (intermediate queueing device) support"
11 + depends on NETDEVICES && NETFILTER
13 + The IMQ device(s) is used as placeholder for QoS queueing
14 + disciplines. Every packet entering/leaving the IP stack can be
15 + directed through the IMQ device where it's enqueued/dequeued to the
16 + attached qdisc. This allows you to treat network devices as classes
17 + and distribute bandwidth among them. Iptables is used to specify
18 + through which IMQ device, if any, packets travel.
20 + More information at: https://github.com/imq/linuximq
22 + To compile this driver as a module, choose M here: the module
23 + will be called imq. If unsure, say N.
26 + prompt "IMQ behavior (PRE/POSTROUTING)"
28 + default IMQ_BEHAVIOR_AB
30 + This setting defines how IMQ behaves in respect to its
31 + hooking in PREROUTING and POSTROUTING.
33 + IMQ can work in any of the following ways:
35 + PREROUTING | POSTROUTING
36 + -----------------|-------------------
37 + #1 After NAT | After NAT
38 + #2 After NAT | Before NAT
39 + #3 Before NAT | After NAT
40 + #4 Before NAT | Before NAT
42 + The default behavior is to hook before NAT on PREROUTING
43 + and after NAT on POSTROUTING (#3).
45 + This settings are specially usefull when trying to use IMQ
46 + to shape NATed clients.
48 + More information can be found at: https://github.com/imq/linuximq
50 + If not sure leave the default settings alone.
52 +config IMQ_BEHAVIOR_AA
55 + This setting defines how IMQ behaves in respect to its
56 + hooking in PREROUTING and POSTROUTING.
58 + Choosing this option will make IMQ hook like this:
60 + PREROUTING: After NAT
61 + POSTROUTING: After NAT
63 + More information can be found at: https://github.com/imq/linuximq
65 + If not sure leave the default settings alone.
67 +config IMQ_BEHAVIOR_AB
70 + This setting defines how IMQ behaves in respect to its
71 + hooking in PREROUTING and POSTROUTING.
73 + Choosing this option will make IMQ hook like this:
75 + PREROUTING: After NAT
76 + POSTROUTING: Before NAT
78 + More information can be found at: https://github.com/imq/linuximq
80 + If not sure leave the default settings alone.
82 +config IMQ_BEHAVIOR_BA
85 + This setting defines how IMQ behaves in respect to its
86 + hooking in PREROUTING and POSTROUTING.
88 + Choosing this option will make IMQ hook like this:
90 + PREROUTING: Before NAT
91 + POSTROUTING: After NAT
93 + More information can be found at: https://github.com/imq/linuximq
95 + If not sure leave the default settings alone.
97 +config IMQ_BEHAVIOR_BB
100 + This setting defines how IMQ behaves in respect to its
101 + hooking in PREROUTING and POSTROUTING.
103 + Choosing this option will make IMQ hook like this:
105 + PREROUTING: Before NAT
106 + POSTROUTING: Before NAT
108 + More information can be found at: https://github.com/imq/linuximq
110 + If not sure leave the default settings alone.
115 + int "Number of IMQ devices"
120 + This setting defines how many IMQ devices will be created.
122 + The default value is 16.
124 + More information can be found at: https://github.com/imq/linuximq
126 + If not sure leave the default settings alone.
129 tristate "Universal TUN/TAP device driver support"
131 diff --git a/drivers/net/Makefile b/drivers/net/Makefile
132 index 900b0c5..e093402 100644
133 --- a/drivers/net/Makefile
134 +++ b/drivers/net/Makefile
135 @@ -10,6 +10,7 @@ obj-$(CONFIG_IPVLAN) += ipvlan/
136 obj-$(CONFIG_DUMMY) += dummy.o
137 obj-$(CONFIG_EQUALIZER) += eql.o
138 obj-$(CONFIG_IFB) += ifb.o
139 +obj-$(CONFIG_IMQ) += imq.o
140 obj-$(CONFIG_MACSEC) += macsec.o
141 obj-$(CONFIG_MACVLAN) += macvlan.o
142 obj-$(CONFIG_MACVTAP) += macvtap.o
143 diff --git a/drivers/net/imq.c b/drivers/net/imq.c
145 index 0000000..f80258f
147 +++ b/drivers/net/imq.c
150 + * Pseudo-driver for the intermediate queue device.
152 + * This program is free software; you can redistribute it and/or
153 + * modify it under the terms of the GNU General Public License
154 + * as published by the Free Software Foundation; either version
155 + * 2 of the License, or (at your option) any later version.
157 + * Authors: Patrick McHardy, <kaber@trash.net>
159 + * The first version was written by Martin Devera, <devik@cdi.cz>
164 +#include <linux/module.h>
165 +#include <linux/kernel.h>
166 +#include <linux/moduleparam.h>
167 +#include <linux/list.h>
168 +#include <linux/skbuff.h>
169 +#include <linux/netdevice.h>
170 +#include <linux/etherdevice.h>
171 +#include <linux/rtnetlink.h>
172 +#include <linux/if_arp.h>
173 +#include <linux/netfilter.h>
174 +#include <linux/netfilter_ipv4.h>
175 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
176 + #include <linux/netfilter_ipv6.h>
178 +#include <linux/imq.h>
179 +#include <net/pkt_sched.h>
180 +#include <net/netfilter/nf_queue.h>
181 +#include <net/sock.h>
182 +#include <linux/ip.h>
183 +#include <linux/ipv6.h>
184 +#include <linux/if_vlan.h>
185 +#include <linux/if_pppox.h>
187 +#include <net/ipv6.h>
189 +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num);
191 +static nf_hookfn imq_nf_hook;
193 +static struct nf_hook_ops imq_ops[] = {
195 + /* imq_ingress_ipv4 */
196 + .hook = imq_nf_hook,
198 + .hooknum = NF_INET_PRE_ROUTING,
199 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
200 + .priority = NF_IP_PRI_MANGLE + 1,
202 + .priority = NF_IP_PRI_NAT_DST + 1,
206 + /* imq_egress_ipv4 */
207 + .hook = imq_nf_hook,
209 + .hooknum = NF_INET_POST_ROUTING,
210 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
211 + .priority = NF_IP_PRI_LAST,
213 + .priority = NF_IP_PRI_NAT_SRC - 1,
216 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
218 + /* imq_ingress_ipv6 */
219 + .hook = imq_nf_hook,
221 + .hooknum = NF_INET_PRE_ROUTING,
222 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
223 + .priority = NF_IP6_PRI_MANGLE + 1,
225 + .priority = NF_IP6_PRI_NAT_DST + 1,
229 + /* imq_egress_ipv6 */
230 + .hook = imq_nf_hook,
232 + .hooknum = NF_INET_POST_ROUTING,
233 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
234 + .priority = NF_IP6_PRI_LAST,
236 + .priority = NF_IP6_PRI_NAT_SRC - 1,
242 +#if defined(CONFIG_IMQ_NUM_DEVS)
243 +static int numdevs = CONFIG_IMQ_NUM_DEVS;
245 +static int numdevs = IMQ_MAX_DEVS;
248 +static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
250 +#define IMQ_MAX_QUEUES 32
251 +static int numqueues = 1;
252 +static u32 imq_hashrnd;
253 +static int imq_dev_accurate_stats = 1;
255 +static inline __be16 pppoe_proto(const struct sk_buff *skb)
257 + return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
258 + sizeof(struct pppoe_hdr)));
261 +static u16 imq_hash(struct net_device *dev, struct sk_buff *skb)
263 + unsigned int pull_len;
264 + u16 protocol = skb->protocol;
276 + switch (protocol) {
277 + case htons(ETH_P_8021Q): {
278 + if (unlikely(skb_pull(skb, VLAN_HLEN) == NULL))
281 + pull_len += VLAN_HLEN;
282 + skb->network_header += VLAN_HLEN;
284 + protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
288 + case htons(ETH_P_PPP_SES): {
289 + if (unlikely(skb_pull(skb, PPPOE_SES_HLEN) == NULL))
292 + pull_len += PPPOE_SES_HLEN;
293 + skb->network_header += PPPOE_SES_HLEN;
295 + protocol = pppoe_proto(skb);
299 + case htons(ETH_P_IP): {
300 + const struct iphdr *iph = ip_hdr(skb);
302 + if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr))))
305 + addr1 = iph->daddr;
306 + addr2 = iph->saddr;
308 + ip_proto = !(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) ?
310 + ihl = ip_hdrlen(skb);
314 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
315 + case htons(ETH_P_IPV6): {
316 + const struct ipv6hdr *iph = ipv6_hdr(skb);
319 + if (unlikely(!pskb_may_pull(skb, sizeof(struct ipv6hdr))))
322 + addr1 = iph->daddr.s6_addr32[3];
323 + addr2 = iph->saddr.s6_addr32[3];
324 + ihl = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &ip_proto,
326 + if (unlikely(ihl < 0))
334 + if (pull_len != 0) {
335 + skb_push(skb, pull_len);
336 + skb->network_header -= pull_len;
339 + return (u16)(ntohs(protocol) % dev->real_num_tx_queues);
343 + swap(addr1, addr2);
345 + switch (ip_proto) {
352 + case IPPROTO_UDPLITE: {
353 + if (likely(skb_copy_bits(skb, ihl, &ports.in32, 4) >= 0)) {
354 + if (ports.in16[0] > ports.in16[1])
355 + swap(ports.in16[0], ports.in16[1]);
365 + if (pull_len != 0) {
366 + skb_push(skb, pull_len);
367 + skb->network_header -= pull_len;
370 + hash = jhash_3words(addr1, addr2, ports.in32, imq_hashrnd ^ ip_proto);
372 + return (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
375 +static inline bool sk_tx_queue_recorded(struct sock *sk)
377 + return (sk_tx_queue_get(sk) >= 0);
380 +static struct netdev_queue *imq_select_queue(struct net_device *dev,
381 + struct sk_buff *skb)
383 + u16 queue_index = 0;
386 + if (likely(dev->real_num_tx_queues == 1))
389 + /* IMQ can be receiving ingress or engress packets. */
391 + /* Check first for if rx_queue is set */
392 + if (skb_rx_queue_recorded(skb)) {
393 + queue_index = skb_get_rx_queue(skb);
397 + /* Check if socket has tx_queue set */
398 + if (sk_tx_queue_recorded(skb->sk)) {
399 + queue_index = sk_tx_queue_get(skb->sk);
403 + /* Try use socket hash */
404 + if (skb->sk && skb->sk->sk_hash) {
405 + hash = skb->sk->sk_hash;
407 + (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
411 + /* Generate hash from packet data */
412 + queue_index = imq_hash(dev, skb);
415 + if (unlikely(queue_index >= dev->real_num_tx_queues))
416 + queue_index = (u16)((u32)queue_index % dev->real_num_tx_queues);
418 + skb_set_queue_mapping(skb, queue_index);
419 + return netdev_get_tx_queue(dev, queue_index);
422 +static struct net_device_stats *imq_get_stats(struct net_device *dev)
424 + return &dev->stats;
427 +/* called for packets kfree'd in qdiscs at places other than enqueue */
428 +static void imq_skb_destructor(struct sk_buff *skb)
430 + struct nf_queue_entry *entry = skb->nf_queue_entry;
432 + skb->nf_queue_entry = NULL;
435 + nf_queue_entry_release_refs(entry);
439 + skb_restore_cb(skb); /* kfree backup */
442 +static void imq_done_check_queue_mapping(struct sk_buff *skb,
443 + struct net_device *dev)
445 + unsigned int queue_index;
447 + /* Don't let queue_mapping be left too large after exiting IMQ */
448 + if (likely(skb->dev != dev && skb->dev != NULL)) {
449 + queue_index = skb_get_queue_mapping(skb);
450 + if (unlikely(queue_index >= skb->dev->real_num_tx_queues)) {
451 + queue_index = (u16)((u32)queue_index %
452 + skb->dev->real_num_tx_queues);
453 + skb_set_queue_mapping(skb, queue_index);
456 + /* skb->dev was IMQ device itself or NULL, be on safe side and
457 + * just clear queue mapping.
459 + skb_set_queue_mapping(skb, 0);
463 +static netdev_tx_t imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
465 + struct nf_queue_entry *entry = skb->nf_queue_entry;
467 + skb->nf_queue_entry = NULL;
468 + dev->trans_start = jiffies;
470 + dev->stats.tx_bytes += skb->len;
471 + dev->stats.tx_packets++;
473 + if (unlikely(entry == NULL)) {
474 + /* We don't know what is going on here.. packet is queued for
475 + * imq device, but (probably) not by us.
477 + * If this packet was not send here by imq_nf_queue(), then
478 + * skb_save_cb() was not used and skb_free() should not show:
479 + * WARNING: IMQ: kfree_skb: skb->cb_next:..
481 + * WARNING: IMQ: kfree_skb: skb->nf_queue_entry...
483 + * However if this message is shown, then IMQ is somehow broken
484 + * and you should report this to linuximq.net.
487 + /* imq_dev_xmit is black hole that eats all packets, report that
488 + * we eat this packet happily and increase dropped counters.
491 + dev->stats.tx_dropped++;
492 + dev_kfree_skb(skb);
494 + return NETDEV_TX_OK;
497 + skb_restore_cb(skb); /* restore skb->cb */
499 + skb->imq_flags = 0;
500 + skb->destructor = NULL;
502 + imq_done_check_queue_mapping(skb, dev);
504 + nf_reinject(entry, NF_ACCEPT);
506 + return NETDEV_TX_OK;
509 +static struct net_device *get_imq_device_by_index(int index)
511 + struct net_device *dev = NULL;
515 + /* get device by name and cache result */
516 + snprintf(buf, sizeof(buf), "imq%d", index);
518 + /* Search device from all namespaces. */
519 + for_each_net(net) {
520 + dev = dev_get_by_name(net, buf);
525 + if (WARN_ON_ONCE(dev == NULL)) {
526 + /* IMQ device not found. Exotic config? */
527 + return ERR_PTR(-ENODEV);
530 + imq_devs_cache[index] = dev;
536 +static struct nf_queue_entry *nf_queue_entry_dup(struct nf_queue_entry *e)
538 + struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
540 + nf_queue_entry_get_refs(entry);
546 +#ifdef CONFIG_BRIDGE_NETFILTER
547 +/* When called from bridge netfilter, skb->data must point to MAC header
548 + * before calling skb_gso_segment(). Else, original MAC header is lost
549 + * and segmented skbs will be sent to wrong destination.
551 +static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
553 + if (skb->nf_bridge)
554 + __skb_push(skb, skb->network_header - skb->mac_header);
557 +static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
559 + if (skb->nf_bridge)
560 + __skb_pull(skb, skb->network_header - skb->mac_header);
563 +#define nf_bridge_adjust_skb_data(s) do {} while (0)
564 +#define nf_bridge_adjust_segmented_data(s) do {} while (0)
567 +static void free_entry(struct nf_queue_entry *entry)
569 + nf_queue_entry_release_refs(entry);
573 +static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev);
575 +static int __imq_nf_queue_gso(struct nf_queue_entry *entry,
576 + struct net_device *dev, struct sk_buff *skb)
579 + struct nf_queue_entry *entry_seg;
581 + nf_bridge_adjust_segmented_data(skb);
583 + if (skb->next == NULL) { /* last packet, no need to copy entry */
584 + struct sk_buff *gso_skb = entry->skb;
586 + ret = __imq_nf_queue(entry, dev);
588 + entry->skb = gso_skb;
594 + entry_seg = nf_queue_entry_dup(entry);
596 + entry_seg->skb = skb;
597 + ret = __imq_nf_queue(entry_seg, dev);
599 + free_entry(entry_seg);
604 +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num)
606 + struct sk_buff *skb, *segs;
607 + struct net_device *dev;
608 + unsigned int queued;
609 + int index, retval, err;
611 + index = entry->skb->imq_flags & IMQ_F_IFMASK;
612 + if (unlikely(index > numdevs - 1)) {
613 + if (net_ratelimit())
614 + pr_warn("IMQ: invalid device specified, highest is %u\n",
620 + /* check for imq device by index from cache */
621 + dev = imq_devs_cache[index];
622 + if (unlikely(!dev)) {
623 + dev = get_imq_device_by_index(index);
625 + retval = PTR_ERR(dev);
630 + if (unlikely(!(dev->flags & IFF_UP))) {
631 + entry->skb->imq_flags = 0;
632 + retval = -ECANCELED;
636 + /* Since 3.10.x, GSO handling moved here as result of upstream commit
637 + * a5fedd43d5f6c94c71053a66e4c3d2e35f1731a2 (netfilter: move
638 + * skb_gso_segment into nfnetlink_queue module).
640 + * Following code replicates the gso handling from
641 + * 'net/netfilter/nfnetlink_queue_core.c':nfqnl_enqueue_packet().
646 + switch (entry->state.pf) {
648 + skb->protocol = htons(ETH_P_IP);
651 + skb->protocol = htons(ETH_P_IPV6);
655 + if (!skb_is_gso(entry->skb))
656 + return __imq_nf_queue(entry, dev);
658 + nf_bridge_adjust_skb_data(skb);
659 + segs = skb_gso_segment(skb, 0);
660 + /* Does not use PTR_ERR to limit the number of error codes that can be
661 + * returned by nf_queue. For instance, callers rely on -ECANCELED to
662 + * mean 'ignore this hook'.
670 + struct sk_buff *nskb = segs->next;
671 + if (nskb && nskb->next)
672 + nskb->cb_next = NULL;
674 + err = __imq_nf_queue_gso(entry, dev, segs);
683 + if (err) /* some segments are already queued */
690 + nf_bridge_adjust_segmented_data(skb);
696 +static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev)
698 + struct sk_buff *skb_orig, *skb, *skb_shared, *skb_popd;
700 + struct netdev_queue *txq;
701 + spinlock_t *root_lock;
703 + int retval = -EINVAL;
704 + unsigned int orig_queue_index;
706 + dev->last_rx = jiffies;
711 + /* skb has owner? => make clone */
712 + if (unlikely(skb->destructor)) {
714 + skb = skb_clone(skb, GFP_ATOMIC);
715 + if (unlikely(!skb)) {
719 + skb->cb_next = NULL;
723 + dev->stats.rx_bytes += skb->len;
724 + dev->stats.rx_packets++;
727 + /* skb->dev == NULL causes problems, try the find cause. */
728 + if (net_ratelimit()) {
729 + dev_warn(&dev->dev,
730 + "received packet with skb->dev == NULL\n");
737 + /* Disables softirqs for lock below */
738 + rcu_read_lock_bh();
740 + /* Multi-queue selection */
741 + orig_queue_index = skb_get_queue_mapping(skb);
742 + txq = imq_select_queue(dev, skb);
744 + q = rcu_dereference(txq->qdisc);
745 + if (unlikely(!q->enqueue))
746 + goto packet_not_eaten_by_imq_dev;
748 + skb->nf_queue_entry = entry;
749 + root_lock = qdisc_lock(q);
750 + spin_lock(root_lock);
752 + users = atomic_read(&skb->users);
754 + skb_shared = skb_get(skb); /* increase reference count by one */
756 + /* backup skb->cb, as qdisc layer will overwrite it */
757 + skb_save_cb(skb_shared);
758 + qdisc_enqueue_root(skb_shared, q); /* might kfree_skb */
759 + if (likely(atomic_read(&skb_shared->users) == users + 1)) {
762 + kfree_skb(skb_shared); /* decrease reference count by one */
764 + skb->destructor = &imq_skb_destructor;
766 + skb_popd = qdisc_dequeue_skb(q, &validate);
769 + if (unlikely(skb_orig))
770 + kfree_skb(skb_orig); /* free original */
772 + spin_unlock(root_lock);
775 + /* schedule qdisc dequeue */
776 + __netif_schedule(q);
778 + if (likely(skb_popd)) {
779 + /* Note that we validate skb (GSO, checksum, ...) outside of locks */
781 + skb_popd = validate_xmit_skb_list(skb_popd, dev);
785 + int cpu = smp_processor_id(); /* ok because BHs are off */
787 + txq = skb_get_tx_queue(dev, skb_popd);
789 + IMQ device will not be frozen or stoped, and it always be successful.
790 + So we need not check its status and return value to accelerate.
792 + if (imq_dev_accurate_stats && txq->xmit_lock_owner != cpu) {
793 + HARD_TX_LOCK(dev, txq, cpu);
794 + if (!netif_xmit_frozen_or_stopped(txq)) {
795 + dev_hard_start_xmit(skb_popd, dev, txq, &dummy_ret);
797 + HARD_TX_UNLOCK(dev, txq);
799 + if (!netif_xmit_frozen_or_stopped(txq)) {
800 + dev_hard_start_xmit(skb_popd, dev, txq, &dummy_ret);
805 + /* No ready skb, then schedule it */
806 + __netif_schedule(q);
809 + rcu_read_unlock_bh();
813 + skb_restore_cb(skb_shared); /* restore skb->cb */
814 + skb->nf_queue_entry = NULL;
816 + * qdisc dropped packet and decreased skb reference count of
817 + * skb, so we don't really want to and try refree as that would
818 + * actually destroy the skb.
820 + spin_unlock(root_lock);
821 + goto packet_not_eaten_by_imq_dev;
824 +packet_not_eaten_by_imq_dev:
825 + skb_set_queue_mapping(skb, orig_queue_index);
826 + rcu_read_unlock_bh();
828 + /* cloned? restore original */
829 + if (unlikely(skb_orig)) {
831 + entry->skb = skb_orig;
837 +static unsigned int imq_nf_hook(void *priv,
838 + struct sk_buff *skb,
839 + const struct nf_hook_state *state)
841 + return (skb->imq_flags & IMQ_F_ENQUEUE) ? NF_IMQ_QUEUE : NF_ACCEPT;
844 +static int imq_close(struct net_device *dev)
846 + netif_stop_queue(dev);
850 +static int imq_open(struct net_device *dev)
852 + netif_start_queue(dev);
856 +static const struct net_device_ops imq_netdev_ops = {
857 + .ndo_open = imq_open,
858 + .ndo_stop = imq_close,
859 + .ndo_start_xmit = imq_dev_xmit,
860 + .ndo_get_stats = imq_get_stats,
863 +static void imq_setup(struct net_device *dev)
865 + dev->netdev_ops = &imq_netdev_ops;
866 + dev->type = ARPHRD_VOID;
867 + dev->mtu = 16000; /* too small? */
868 + dev->tx_queue_len = 11000; /* too big? */
869 + dev->flags = IFF_NOARP;
870 + dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
871 + NETIF_F_GSO | NETIF_F_HW_CSUM |
873 + dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE |
874 + IFF_TX_SKB_SHARING);
877 +static int imq_validate(struct nlattr *tb[], struct nlattr *data[])
881 + if (tb[IFLA_ADDRESS]) {
882 + if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
886 + if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
887 + ret = -EADDRNOTAVAIL;
893 + pr_warn("IMQ: imq_validate failed (%d)\n", ret);
897 +static struct rtnl_link_ops imq_link_ops __read_mostly = {
900 + .setup = imq_setup,
901 + .validate = imq_validate,
904 +static const struct nf_queue_handler imq_nfqh = {
905 + .outfn = imq_nf_queue,
908 +static int __init imq_init_hooks(void)
912 + nf_register_queue_imq_handler(&imq_nfqh);
914 + ret = nf_register_hooks(imq_ops, ARRAY_SIZE(imq_ops));
916 + nf_unregister_queue_imq_handler();
921 +static int __init imq_init_one(int index)
923 + struct net_device *dev;
926 + dev = alloc_netdev_mq(0, "imq%d", NET_NAME_UNKNOWN, imq_setup, numqueues);
930 + ret = dev_alloc_name(dev, dev->name);
934 + dev->rtnl_link_ops = &imq_link_ops;
935 + ret = register_netdevice(dev);
945 +static int __init imq_init_devs(void)
949 + if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) {
950 + pr_err("IMQ: numdevs has to be betweed 1 and %u\n",
955 + if (numqueues < 1 || numqueues > IMQ_MAX_QUEUES) {
956 + pr_err("IMQ: numqueues has to be betweed 1 and %u\n",
961 + get_random_bytes(&imq_hashrnd, sizeof(imq_hashrnd));
964 + err = __rtnl_link_register(&imq_link_ops);
966 + for (i = 0; i < numdevs && !err; i++)
967 + err = imq_init_one(i);
970 + __rtnl_link_unregister(&imq_link_ops);
971 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
978 +static int __init imq_init_module(void)
982 +#if defined(CONFIG_IMQ_NUM_DEVS)
983 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS > 16);
984 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS < 2);
985 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS - 1 > IMQ_F_IFMASK);
988 + err = imq_init_devs();
990 + pr_err("IMQ: Error trying imq_init_devs(net)\n");
994 + err = imq_init_hooks();
996 + pr_err(KERN_ERR "IMQ: Error trying imq_init_hooks()\n");
997 + rtnl_link_unregister(&imq_link_ops);
998 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
1002 + pr_info("IMQ driver loaded successfully. (numdevs = %d, numqueues = %d, imq_dev_accurate_stats = %d)\n",
1003 + numdevs, numqueues, imq_dev_accurate_stats);
1005 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
1006 + pr_info("\tHooking IMQ before NAT on PREROUTING.\n");
1008 + pr_info("\tHooking IMQ after NAT on PREROUTING.\n");
1010 +#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
1011 + pr_info("\tHooking IMQ before NAT on POSTROUTING.\n");
1013 + pr_info("\tHooking IMQ after NAT on POSTROUTING.\n");
1019 +static void __exit imq_unhook(void)
1021 + nf_unregister_hooks(imq_ops, ARRAY_SIZE(imq_ops));
1022 + nf_unregister_queue_imq_handler();
1025 +static void __exit imq_cleanup_devs(void)
1027 + rtnl_link_unregister(&imq_link_ops);
1028 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
1031 +static void __exit imq_exit_module(void)
1034 + imq_cleanup_devs();
1035 + pr_info("IMQ driver unloaded successfully.\n");
1038 +module_init(imq_init_module);
1039 +module_exit(imq_exit_module);
1041 +module_param(numdevs, int, 0);
1042 +module_param(numqueues, int, 0);
1043 +module_param(imq_dev_accurate_stats, int, 0);
1044 +MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will be created)");
1045 +MODULE_PARM_DESC(numqueues, "number of queues per IMQ device");
1046 +MODULE_PARM_DESC(imq_dev_accurate_stats, "Notify if need the accurate imq device stats");
1048 +MODULE_AUTHOR("http://https://github.com/imq/linuximq");
1049 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See https://github.com/imq/linuximq/wiki for more information.");
1050 +MODULE_LICENSE("GPL");
1051 +MODULE_ALIAS_RTNL_LINK("imq");
1052 diff --git a/include/linux/imq.h b/include/linux/imq.h
1053 new file mode 100644
1054 index 0000000..1babb09
1056 +++ b/include/linux/imq.h
1061 +/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
1062 +#define IMQ_F_BITS 5
1064 +#define IMQ_F_IFMASK 0x0f
1065 +#define IMQ_F_ENQUEUE 0x10
1067 +#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1)
1069 +#endif /* _IMQ_H */
1071 diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
1072 index 3143c84..e213b31 100644
1073 --- a/include/linux/netdevice.h
1074 +++ b/include/linux/netdevice.h
1075 @@ -3341,6 +3341,19 @@ static inline void netif_tx_unlock_bh(struct net_device *dev)
1079 +#define HARD_TX_LOCK_BH(dev, txq) { \
1080 + if ((dev->features & NETIF_F_LLTX) == 0) { \
1081 + __netif_tx_lock_bh(txq); \
1085 +#define HARD_TX_UNLOCK_BH(dev, txq) { \
1086 + if ((dev->features & NETIF_F_LLTX) == 0) { \
1087 + __netif_tx_unlock_bh(txq); \
1092 static inline void netif_tx_disable(struct net_device *dev)
1095 diff --git a/include/linux/netfilter/xt_IMQ.h b/include/linux/netfilter/xt_IMQ.h
1096 new file mode 100644
1097 index 0000000..9b07230
1099 +++ b/include/linux/netfilter/xt_IMQ.h
1104 +struct xt_imq_info {
1105 + unsigned int todev; /* target imq device */
1108 +#endif /* _XT_IMQ_H */
1110 diff --git a/include/linux/netfilter_ipv4/ipt_IMQ.h b/include/linux/netfilter_ipv4/ipt_IMQ.h
1111 new file mode 100644
1112 index 0000000..7af320f
1114 +++ b/include/linux/netfilter_ipv4/ipt_IMQ.h
1119 +/* Backwards compatibility for old userspace */
1120 +#include <linux/netfilter/xt_IMQ.h>
1122 +#define ipt_imq_info xt_imq_info
1124 +#endif /* _IPT_IMQ_H */
1126 diff --git a/include/linux/netfilter_ipv6/ip6t_IMQ.h b/include/linux/netfilter_ipv6/ip6t_IMQ.h
1127 new file mode 100644
1128 index 0000000..198ac01
1130 +++ b/include/linux/netfilter_ipv6/ip6t_IMQ.h
1132 +#ifndef _IP6T_IMQ_H
1133 +#define _IP6T_IMQ_H
1135 +/* Backwards compatibility for old userspace */
1136 +#include <linux/netfilter/xt_IMQ.h>
1138 +#define ip6t_imq_info xt_imq_info
1140 +#endif /* _IP6T_IMQ_H */
1142 diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
1143 index 4355129..47914c0 100644
1144 --- a/include/linux/skbuff.h
1145 +++ b/include/linux/skbuff.h
1147 #include <linux/splice.h>
1148 #include <linux/in6.h>
1149 #include <net/flow.h>
1150 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1151 +#include <linux/imq.h>
1155 /* A. Checksumming of received packets by device.
1157 @@ -566,6 +570,9 @@ struct sk_buff {
1158 * first. This is owned by whoever has the skb queued ATM.
1160 char cb[48] __aligned(8);
1161 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1165 unsigned long _skb_refdst;
1166 void (*destructor)(struct sk_buff *skb);
1167 @@ -575,6 +582,9 @@ struct sk_buff {
1168 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1169 struct nf_conntrack *nfct;
1171 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1172 + struct nf_queue_entry *nf_queue_entry;
1174 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1175 struct nf_bridge_info *nf_bridge;
1177 @@ -642,6 +652,9 @@ struct sk_buff {
1178 __u8 inner_protocol_type:1;
1179 __u8 remcsum_offload:1;
1180 /* 3 or 5 bit hole */
1181 + #if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1182 + __u8 imq_flags:IMQ_F_BITS;
1185 #ifdef CONFIG_NET_SCHED
1186 __u16 tc_index; /* traffic control index */
1187 @@ -798,6 +811,12 @@ void kfree_skb_list(struct sk_buff *segs);
1188 void skb_tx_error(struct sk_buff *skb);
1189 void consume_skb(struct sk_buff *skb);
1190 void __kfree_skb(struct sk_buff *skb);
1192 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1193 +int skb_save_cb(struct sk_buff *skb);
1194 +int skb_restore_cb(struct sk_buff *skb);
1197 extern struct kmem_cache *skbuff_head_cache;
1199 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
1200 @@ -3344,6 +3363,10 @@ static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src,
1202 dst->nfctinfo = src->nfctinfo;
1204 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1205 + dst->imq_flags = src->imq_flags;
1206 + dst->nf_queue_entry = src->nf_queue_entry;
1208 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1209 dst->nf_bridge = src->nf_bridge;
1210 nf_bridge_get(src->nf_bridge);
1211 diff --git a/include/net/netfilter/nf_queue.h b/include/net/netfilter/nf_queue.h
1212 index 9c5638a..b173aa7 100644
1213 --- a/include/net/netfilter/nf_queue.h
1214 +++ b/include/net/netfilter/nf_queue.h
1215 @@ -31,6 +31,12 @@ struct nf_queue_handler {
1216 void nf_register_queue_handler(const struct nf_queue_handler *qh);
1217 void nf_unregister_queue_handler(void);
1218 void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
1219 +void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
1221 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1222 +void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
1223 +void nf_unregister_queue_imq_handler(void);
1226 void nf_queue_entry_get_refs(struct nf_queue_entry *entry);
1227 void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
1228 diff --git a/include/net/pkt_sched.h b/include/net/pkt_sched.h
1229 index 401038d..4668849 100644
1230 --- a/include/net/pkt_sched.h
1231 +++ b/include/net/pkt_sched.h
1232 @@ -104,6 +104,8 @@ int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
1234 void __qdisc_run(struct Qdisc *q);
1236 +struct sk_buff *qdisc_dequeue_skb(struct Qdisc *q, bool *validate);
1238 static inline void qdisc_run(struct Qdisc *q)
1240 if (qdisc_run_begin(q))
1241 diff --git a/include/net/sch_generic.h b/include/net/sch_generic.h
1242 index b2a8e63..d9feaa3 100644
1243 --- a/include/net/sch_generic.h
1244 +++ b/include/net/sch_generic.h
1245 @@ -506,6 +506,12 @@ static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
1246 return sch->enqueue(skb, sch);
1249 +static inline int qdisc_enqueue_root(struct sk_buff *skb, struct Qdisc *sch)
1251 + qdisc_skb_cb(skb)->pkt_len = skb->len;
1252 + return qdisc_enqueue(skb, sch) & NET_XMIT_MASK;
1255 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
1257 return q->flags & TCQ_F_CPUSTATS;
1258 diff --git a/include/uapi/linux/netfilter.h b/include/uapi/linux/netfilter.h
1259 index d93f949..23fb6d1 100644
1260 --- a/include/uapi/linux/netfilter.h
1261 +++ b/include/uapi/linux/netfilter.h
1266 -#define NF_MAX_VERDICT NF_STOP
1267 +#define NF_IMQ_QUEUE 6
1268 +#define NF_MAX_VERDICT NF_IMQ_QUEUE
1270 /* we overload the higher bits for encoding auxiliary data such as the queue
1271 * number or errno values. Not nice, but better than additional function
1272 diff --git a/net/core/dev.c b/net/core/dev.c
1273 index ae00b89..1cdcd02 100644
1274 --- a/net/core/dev.c
1275 +++ b/net/core/dev.c
1277 #include <linux/hrtimer.h>
1278 #include <linux/netfilter_ingress.h>
1279 #include <linux/sctp.h>
1280 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1281 +#include <linux/imq.h>
1284 #include "net-sysfs.h"
1286 @@ -2705,7 +2708,12 @@ static int xmit_one(struct sk_buff *skb, struct net_device *dev,
1290 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1291 + if ((!list_empty(&ptype_all) || !list_empty(&dev->ptype_all)) &&
1292 + !(skb->imq_flags & IMQ_F_ENQUEUE))
1294 if (!list_empty(&ptype_all) || !list_empty(&dev->ptype_all))
1296 dev_queue_xmit_nit(skb, dev);
1299 @@ -2743,6 +2751,7 @@ out:
1303 +EXPORT_SYMBOL(dev_hard_start_xmit);
1305 static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
1306 netdev_features_t features)
1307 @@ -2831,6 +2840,7 @@ struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *d
1311 +EXPORT_SYMBOL(validate_xmit_skb_list);
1313 static void qdisc_pkt_len_init(struct sk_buff *skb)
1315 diff --git a/net/core/skbuff.c b/net/core/skbuff.c
1316 index b2df375..bc3c51e 100644
1317 --- a/net/core/skbuff.c
1318 +++ b/net/core/skbuff.c
1320 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
1321 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
1322 EXPORT_SYMBOL(sysctl_max_skb_frags);
1323 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1324 +static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
1327 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1328 +/* Control buffer save/restore for IMQ devices */
1329 +struct skb_cb_table {
1330 + char cb[48] __aligned(8);
1335 +static DEFINE_SPINLOCK(skb_cb_store_lock);
1337 +int skb_save_cb(struct sk_buff *skb)
1339 + struct skb_cb_table *next;
1341 + next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
1345 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1347 + memcpy(next->cb, skb->cb, sizeof(skb->cb));
1348 + next->cb_next = skb->cb_next;
1350 + atomic_set(&next->refcnt, 1);
1352 + skb->cb_next = next;
1355 +EXPORT_SYMBOL(skb_save_cb);
1357 +int skb_restore_cb(struct sk_buff *skb)
1359 + struct skb_cb_table *next;
1361 + if (!skb->cb_next)
1364 + next = skb->cb_next;
1366 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1368 + memcpy(skb->cb, next->cb, sizeof(skb->cb));
1369 + skb->cb_next = next->cb_next;
1371 + spin_lock(&skb_cb_store_lock);
1373 + if (atomic_dec_and_test(&next->refcnt))
1374 + kmem_cache_free(skbuff_cb_store_cache, next);
1376 + spin_unlock(&skb_cb_store_lock);
1380 +EXPORT_SYMBOL(skb_restore_cb);
1382 +static void skb_copy_stored_cb(struct sk_buff * , const struct sk_buff * ) __attribute__ ((unused));
1383 +static void skb_copy_stored_cb(struct sk_buff *new, const struct sk_buff *__old)
1385 + struct skb_cb_table *next;
1386 + struct sk_buff *old;
1388 + if (!__old->cb_next) {
1389 + new->cb_next = NULL;
1393 + spin_lock(&skb_cb_store_lock);
1395 + old = (struct sk_buff *)__old;
1397 + next = old->cb_next;
1398 + atomic_inc(&next->refcnt);
1399 + new->cb_next = next;
1401 + spin_unlock(&skb_cb_store_lock);
1406 * skb_panic - private function for out-of-line support
1407 @@ -643,6 +724,28 @@ static void skb_release_head_state(struct sk_buff *skb)
1409 skb->destructor(skb);
1411 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1413 + * This should not happen. When it does, avoid memleak by restoring
1414 + * the chain of cb-backups.
1416 + while (skb->cb_next != NULL) {
1417 + if (net_ratelimit())
1418 + pr_warn("IMQ: kfree_skb: skb->cb_next: %08x\n",
1419 + (unsigned int)(uintptr_t)skb->cb_next);
1421 + skb_restore_cb(skb);
1424 + * This should not happen either, nf_queue_entry is nullified in
1425 + * imq_dev_xmit(). If we have non-NULL nf_queue_entry then we are
1426 + * leaking entry pointers, maybe memory. We don't know if this is
1427 + * pointer to already freed memory, or should this be freed.
1428 + * If this happens we need to add refcounting, etc for nf_queue_entry.
1430 + if (skb->nf_queue_entry && net_ratelimit())
1431 + pr_warn("%s\n", "IMQ: kfree_skb: skb->nf_queue_entry != NULL");
1433 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1434 nf_conntrack_put(skb->nfct);
1436 @@ -765,6 +868,10 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1437 new->sp = secpath_get(old->sp);
1439 __nf_copy(new, old, false);
1440 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1441 + new->cb_next = NULL;
1442 + /*skb_copy_stored_cb(new, old);*/
1445 /* Note : this field could be in headers_start/headers_end section
1446 * It is not yet because we do not want to have a 16 bit hole
1447 @@ -3325,6 +3432,13 @@ void __init skb_init(void)
1449 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1451 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1452 + skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1453 + sizeof(struct skb_cb_table),
1455 + SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1461 diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
1462 index e6a7bd15..c81496e 100644
1463 --- a/net/ipv6/ip6_output.c
1464 +++ b/net/ipv6/ip6_output.c
1465 @@ -65,9 +65,6 @@ static int ip6_finish_output2(struct net *net, struct sock *sk, struct sk_buff *
1466 struct in6_addr *nexthop;
1469 - skb->protocol = htons(ETH_P_IPV6);
1472 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
1473 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
1475 @@ -142,6 +139,13 @@ int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
1480 + * IMQ-patch: moved setting skb->dev and skb->protocol from
1481 + * ip6_finish_output2 to fix crashing at netif_skb_features().
1483 + skb->protocol = htons(ETH_P_IPV6);
1486 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
1487 net, sk, skb, NULL, dev,
1489 diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig
1490 index 4692782..dce47db 100644
1491 --- a/net/netfilter/Kconfig
1492 +++ b/net/netfilter/Kconfig
1493 @@ -785,6 +785,18 @@ config NETFILTER_XT_TARGET_LOG
1495 To compile it as a module, choose M here. If unsure, say N.
1497 +config NETFILTER_XT_TARGET_IMQ
1498 + tristate '"IMQ" target support'
1499 + depends on NETFILTER_XTABLES
1500 + depends on IP_NF_MANGLE || IP6_NF_MANGLE
1502 + default m if NETFILTER_ADVANCED=n
1504 + This option adds a `IMQ' target which is used to specify if and
1505 + to which imq device packets should get enqueued/dequeued.
1507 + To compile it as a module, choose M here. If unsure, say N.
1509 config NETFILTER_XT_TARGET_MARK
1510 tristate '"MARK" target support'
1511 depends on NETFILTER_ADVANCED
1512 diff --git a/net/netfilter/Makefile b/net/netfilter/Makefile
1513 index 7638c36..614ad8a 100644
1514 --- a/net/netfilter/Makefile
1515 +++ b/net/netfilter/Makefile
1516 @@ -108,6 +108,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_CT) += xt_CT.o
1517 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
1518 obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
1519 obj-$(CONFIG_NETFILTER_XT_TARGET_HMARK) += xt_HMARK.o
1520 +obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
1521 obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
1522 obj-$(CONFIG_NETFILTER_XT_TARGET_LOG) += xt_LOG.o
1523 obj-$(CONFIG_NETFILTER_XT_TARGET_NETMAP) += xt_NETMAP.o
1524 diff --git a/net/netfilter/core.c b/net/netfilter/core.c
1525 index f39276d..9877a27 100644
1526 --- a/net/netfilter/core.c
1527 +++ b/net/netfilter/core.c
1528 @@ -311,9 +311,11 @@ next_hook:
1529 ret = NF_DROP_GETERR(verdict);
1532 - } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
1533 + } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE ||
1534 + (verdict & NF_VERDICT_MASK) == NF_IMQ_QUEUE) {
1535 int err = nf_queue(skb, elem, state,
1536 - verdict >> NF_VERDICT_QBITS);
1537 + verdict >> NF_VERDICT_QBITS,
1538 + verdict & NF_VERDICT_MASK);
1540 if (err == -ESRCH &&
1541 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
1542 diff --git a/net/netfilter/nf_internals.h b/net/netfilter/nf_internals.h
1543 index 0655225..25d4141 100644
1544 --- a/net/netfilter/nf_internals.h
1545 +++ b/net/netfilter/nf_internals.h
1546 @@ -18,7 +18,7 @@ unsigned int nf_iterate(struct list_head *head, struct sk_buff *skb,
1549 int nf_queue(struct sk_buff *skb, struct nf_hook_ops *elem,
1550 - struct nf_hook_state *state, unsigned int queuenum);
1551 + struct nf_hook_state *state, unsigned int queuenum, unsigned int queuetype);
1552 void nf_queue_nf_hook_drop(struct net *net, struct nf_hook_ops *ops);
1553 int __init netfilter_queue_init(void);
1555 diff --git a/net/netfilter/nf_queue.c b/net/netfilter/nf_queue.c
1556 index 5baa8e2..9740e8c 100644
1557 --- a/net/netfilter/nf_queue.c
1558 +++ b/net/netfilter/nf_queue.c
1561 static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
1563 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1564 +static const struct nf_queue_handler __rcu *queue_imq_handler __read_mostly;
1566 +void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
1568 + rcu_assign_pointer(queue_imq_handler, qh);
1570 +EXPORT_SYMBOL_GPL(nf_register_queue_imq_handler);
1572 +void nf_unregister_queue_imq_handler(void)
1574 + RCU_INIT_POINTER(queue_imq_handler, NULL);
1575 + synchronize_rcu();
1577 +EXPORT_SYMBOL_GPL(nf_unregister_queue_imq_handler);
1580 /* return EBUSY when somebody else is registered, return EEXIST if the
1581 * same handler is registered, return 0 in case of success. */
1582 void nf_register_queue_handler(const struct nf_queue_handler *qh)
1583 @@ -116,7 +133,8 @@ void nf_queue_nf_hook_drop(struct net *net, struct nf_hook_ops *ops)
1584 int nf_queue(struct sk_buff *skb,
1585 struct nf_hook_ops *elem,
1586 struct nf_hook_state *state,
1587 - unsigned int queuenum)
1588 + unsigned int queuenum,
1589 + unsigned int queuetype)
1591 int status = -ENOENT;
1592 struct nf_queue_entry *entry = NULL;
1593 @@ -124,7 +142,17 @@ int nf_queue(struct sk_buff *skb,
1594 const struct nf_queue_handler *qh;
1596 /* QUEUE == DROP if no one is waiting, to be safe. */
1597 - qh = rcu_dereference(queue_handler);
1598 + if (queuetype == NF_IMQ_QUEUE) {
1599 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1600 + qh = rcu_dereference(queue_imq_handler);
1606 + qh = rcu_dereference(queue_handler);
1612 @@ -199,8 +227,10 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
1616 + case NF_IMQ_QUEUE:
1617 err = nf_queue(skb, elem, &entry->state,
1618 - verdict >> NF_VERDICT_QBITS);
1619 + verdict >> NF_VERDICT_QBITS,
1620 + verdict & NF_VERDICT_MASK);
1622 if (err == -ESRCH &&
1623 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
1624 diff --git a/net/netfilter/xt_IMQ.c b/net/netfilter/xt_IMQ.c
1625 new file mode 100644
1626 index 0000000..86d7b84
1628 +++ b/net/netfilter/xt_IMQ.c
1631 + * This target marks packets to be enqueued to an imq device
1633 +#include <linux/module.h>
1634 +#include <linux/skbuff.h>
1635 +#include <linux/netfilter/x_tables.h>
1636 +#include <linux/netfilter/xt_IMQ.h>
1637 +#include <linux/imq.h>
1639 +static unsigned int imq_target(struct sk_buff *pskb,
1640 + const struct xt_action_param *par)
1642 + const struct xt_imq_info *mr = par->targinfo;
1644 + pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
1646 + return XT_CONTINUE;
1649 +static int imq_checkentry(const struct xt_tgchk_param *par)
1651 + struct xt_imq_info *mr = par->targinfo;
1653 + if (mr->todev > IMQ_MAX_DEVS - 1) {
1654 + pr_warn("IMQ: invalid device specified, highest is %u\n",
1655 + IMQ_MAX_DEVS - 1);
1662 +static struct xt_target xt_imq_reg[] __read_mostly = {
1665 + .family = AF_INET,
1666 + .checkentry = imq_checkentry,
1667 + .target = imq_target,
1668 + .targetsize = sizeof(struct xt_imq_info),
1669 + .table = "mangle",
1674 + .family = AF_INET6,
1675 + .checkentry = imq_checkentry,
1676 + .target = imq_target,
1677 + .targetsize = sizeof(struct xt_imq_info),
1678 + .table = "mangle",
1683 +static int __init imq_init(void)
1685 + return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1688 +static void __exit imq_fini(void)
1690 + xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1693 +module_init(imq_init);
1694 +module_exit(imq_fini);
1696 +MODULE_AUTHOR("http://https://github.com/imq/linuximq");
1697 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See https://github.com/imq/linuximq/wiki for more information.");
1698 +MODULE_LICENSE("GPL");
1699 +MODULE_ALIAS("ipt_IMQ");
1700 +MODULE_ALIAS("ip6t_IMQ");
1702 diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c
1703 index e82a1ad..b55331d 100644
1704 --- a/net/sched/sch_generic.c
1705 +++ b/net/sched/sch_generic.c
1706 @@ -108,6 +108,14 @@ static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
1710 +struct sk_buff *qdisc_dequeue_skb(struct Qdisc *q, bool *validate)
1714 + return dequeue_skb(q, validate, &packets);
1716 +EXPORT_SYMBOL(qdisc_dequeue_skb);
1718 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
1719 struct netdev_queue *dev_queue,