1 diff -Naupr linux-4.7_orig/drivers/net/imq.c linux-4.7/drivers/net/imq.c
2 --- linux-4.7_orig/drivers/net/imq.c 1970-01-01 07:00:00.000000000 +0700
3 +++ linux-4.7/drivers/net/imq.c 2016-07-26 20:58:55.635901659 +0700
6 + * Pseudo-driver for the intermediate queue device.
8 + * This program is free software; you can redistribute it and/or
9 + * modify it under the terms of the GNU General Public License
10 + * as published by the Free Software Foundation; either version
11 + * 2 of the License, or (at your option) any later version.
13 + * Authors: Patrick McHardy, <kaber@trash.net>
15 + * The first version was written by Martin Devera, <devik@cdi.cz>
20 +#include <linux/module.h>
21 +#include <linux/kernel.h>
22 +#include <linux/moduleparam.h>
23 +#include <linux/list.h>
24 +#include <linux/skbuff.h>
25 +#include <linux/netdevice.h>
26 +#include <linux/etherdevice.h>
27 +#include <linux/rtnetlink.h>
28 +#include <linux/if_arp.h>
29 +#include <linux/netfilter.h>
30 +#include <linux/netfilter_ipv4.h>
31 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32 + #include <linux/netfilter_ipv6.h>
34 +#include <linux/imq.h>
35 +#include <net/pkt_sched.h>
36 +#include <net/netfilter/nf_queue.h>
37 +#include <net/sock.h>
38 +#include <linux/ip.h>
39 +#include <linux/ipv6.h>
40 +#include <linux/if_vlan.h>
41 +#include <linux/if_pppox.h>
43 +#include <net/ipv6.h>
45 +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num);
47 +static nf_hookfn imq_nf_hook;
49 +static struct nf_hook_ops imq_ops[] = {
51 + /* imq_ingress_ipv4 */
52 + .hook = imq_nf_hook,
54 + .hooknum = NF_INET_PRE_ROUTING,
55 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
56 + .priority = NF_IP_PRI_MANGLE + 1,
58 + .priority = NF_IP_PRI_NAT_DST + 1,
62 + /* imq_egress_ipv4 */
63 + .hook = imq_nf_hook,
65 + .hooknum = NF_INET_POST_ROUTING,
66 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
67 + .priority = NF_IP_PRI_LAST,
69 + .priority = NF_IP_PRI_NAT_SRC - 1,
72 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
74 + /* imq_ingress_ipv6 */
75 + .hook = imq_nf_hook,
77 + .hooknum = NF_INET_PRE_ROUTING,
78 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
79 + .priority = NF_IP6_PRI_MANGLE + 1,
81 + .priority = NF_IP6_PRI_NAT_DST + 1,
85 + /* imq_egress_ipv6 */
86 + .hook = imq_nf_hook,
88 + .hooknum = NF_INET_POST_ROUTING,
89 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
90 + .priority = NF_IP6_PRI_LAST,
92 + .priority = NF_IP6_PRI_NAT_SRC - 1,
98 +#if defined(CONFIG_IMQ_NUM_DEVS)
99 +static int numdevs = CONFIG_IMQ_NUM_DEVS;
101 +static int numdevs = IMQ_MAX_DEVS;
104 +static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
106 +#define IMQ_MAX_QUEUES 32
107 +static int numqueues = 1;
108 +static u32 imq_hashrnd;
109 +static int imq_dev_accurate_stats = 1;
111 +static inline __be16 pppoe_proto(const struct sk_buff *skb)
113 + return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
114 + sizeof(struct pppoe_hdr)));
117 +static u16 imq_hash(struct net_device *dev, struct sk_buff *skb)
119 + unsigned int pull_len;
120 + u16 protocol = skb->protocol;
132 + switch (protocol) {
133 + case htons(ETH_P_8021Q): {
134 + if (unlikely(skb_pull(skb, VLAN_HLEN) == NULL))
137 + pull_len += VLAN_HLEN;
138 + skb->network_header += VLAN_HLEN;
140 + protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
144 + case htons(ETH_P_PPP_SES): {
145 + if (unlikely(skb_pull(skb, PPPOE_SES_HLEN) == NULL))
148 + pull_len += PPPOE_SES_HLEN;
149 + skb->network_header += PPPOE_SES_HLEN;
151 + protocol = pppoe_proto(skb);
155 + case htons(ETH_P_IP): {
156 + const struct iphdr *iph = ip_hdr(skb);
158 + if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr))))
161 + addr1 = iph->daddr;
162 + addr2 = iph->saddr;
164 + ip_proto = !(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) ?
166 + ihl = ip_hdrlen(skb);
170 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
171 + case htons(ETH_P_IPV6): {
172 + const struct ipv6hdr *iph = ipv6_hdr(skb);
175 + if (unlikely(!pskb_may_pull(skb, sizeof(struct ipv6hdr))))
178 + addr1 = iph->daddr.s6_addr32[3];
179 + addr2 = iph->saddr.s6_addr32[3];
180 + ihl = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &ip_proto,
182 + if (unlikely(ihl < 0))
190 + if (pull_len != 0) {
191 + skb_push(skb, pull_len);
192 + skb->network_header -= pull_len;
195 + return (u16)(ntohs(protocol) % dev->real_num_tx_queues);
199 + swap(addr1, addr2);
201 + switch (ip_proto) {
208 + case IPPROTO_UDPLITE: {
209 + if (likely(skb_copy_bits(skb, ihl, &ports.in32, 4) >= 0)) {
210 + if (ports.in16[0] > ports.in16[1])
211 + swap(ports.in16[0], ports.in16[1]);
221 + if (pull_len != 0) {
222 + skb_push(skb, pull_len);
223 + skb->network_header -= pull_len;
226 + hash = jhash_3words(addr1, addr2, ports.in32, imq_hashrnd ^ ip_proto);
228 + return (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
231 +static inline bool sk_tx_queue_recorded(struct sock *sk)
233 + return (sk_tx_queue_get(sk) >= 0);
236 +static struct netdev_queue *imq_select_queue(struct net_device *dev,
237 + struct sk_buff *skb)
239 + u16 queue_index = 0;
242 + if (likely(dev->real_num_tx_queues == 1))
245 + /* IMQ can be receiving ingress or engress packets. */
247 + /* Check first for if rx_queue is set */
248 + if (skb_rx_queue_recorded(skb)) {
249 + queue_index = skb_get_rx_queue(skb);
253 + /* Check if socket has tx_queue set */
254 + if (sk_tx_queue_recorded(skb->sk)) {
255 + queue_index = sk_tx_queue_get(skb->sk);
259 + /* Try use socket hash */
260 + if (skb->sk && skb->sk->sk_hash) {
261 + hash = skb->sk->sk_hash;
263 + (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
267 + /* Generate hash from packet data */
268 + queue_index = imq_hash(dev, skb);
271 + if (unlikely(queue_index >= dev->real_num_tx_queues))
272 + queue_index = (u16)((u32)queue_index % dev->real_num_tx_queues);
274 + skb_set_queue_mapping(skb, queue_index);
275 + return netdev_get_tx_queue(dev, queue_index);
278 +static struct net_device_stats *imq_get_stats(struct net_device *dev)
280 + return &dev->stats;
283 +/* called for packets kfree'd in qdiscs at places other than enqueue */
284 +static void imq_skb_destructor(struct sk_buff *skb)
286 + struct nf_queue_entry *entry = skb->nf_queue_entry;
288 + skb->nf_queue_entry = NULL;
291 + nf_queue_entry_release_refs(entry);
295 + skb_restore_cb(skb); /* kfree backup */
298 +static void imq_done_check_queue_mapping(struct sk_buff *skb,
299 + struct net_device *dev)
301 + unsigned int queue_index;
303 + /* Don't let queue_mapping be left too large after exiting IMQ */
304 + if (likely(skb->dev != dev && skb->dev != NULL)) {
305 + queue_index = skb_get_queue_mapping(skb);
306 + if (unlikely(queue_index >= skb->dev->real_num_tx_queues)) {
307 + queue_index = (u16)((u32)queue_index %
308 + skb->dev->real_num_tx_queues);
309 + skb_set_queue_mapping(skb, queue_index);
312 + /* skb->dev was IMQ device itself or NULL, be on safe side and
313 + * just clear queue mapping.
315 + skb_set_queue_mapping(skb, 0);
319 +static netdev_tx_t imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
321 + struct nf_queue_entry *entry = skb->nf_queue_entry;
323 + skb->nf_queue_entry = NULL;
324 + netif_trans_update(dev);
326 + dev->stats.tx_bytes += skb->len;
327 + dev->stats.tx_packets++;
329 + if (unlikely(entry == NULL)) {
330 + /* We don't know what is going on here.. packet is queued for
331 + * imq device, but (probably) not by us.
333 + * If this packet was not send here by imq_nf_queue(), then
334 + * skb_save_cb() was not used and skb_free() should not show:
335 + * WARNING: IMQ: kfree_skb: skb->cb_next:..
337 + * WARNING: IMQ: kfree_skb: skb->nf_queue_entry...
339 + * However if this message is shown, then IMQ is somehow broken
340 + * and you should report this to linuximq.net.
343 + /* imq_dev_xmit is black hole that eats all packets, report that
344 + * we eat this packet happily and increase dropped counters.
347 + dev->stats.tx_dropped++;
348 + dev_kfree_skb(skb);
350 + return NETDEV_TX_OK;
353 + skb_restore_cb(skb); /* restore skb->cb */
355 + skb->imq_flags = 0;
356 + skb->destructor = NULL;
358 + imq_done_check_queue_mapping(skb, dev);
360 + nf_reinject(entry, NF_ACCEPT);
362 + return NETDEV_TX_OK;
365 +static struct net_device *get_imq_device_by_index(int index)
367 + struct net_device *dev = NULL;
371 + /* get device by name and cache result */
372 + snprintf(buf, sizeof(buf), "imq%d", index);
374 + /* Search device from all namespaces. */
375 + for_each_net(net) {
376 + dev = dev_get_by_name(net, buf);
381 + if (WARN_ON_ONCE(dev == NULL)) {
382 + /* IMQ device not found. Exotic config? */
383 + return ERR_PTR(-ENODEV);
386 + imq_devs_cache[index] = dev;
392 +static struct nf_queue_entry *nf_queue_entry_dup(struct nf_queue_entry *e)
394 + struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
396 + nf_queue_entry_get_refs(entry);
402 +#ifdef CONFIG_BRIDGE_NETFILTER
403 +/* When called from bridge netfilter, skb->data must point to MAC header
404 + * before calling skb_gso_segment(). Else, original MAC header is lost
405 + * and segmented skbs will be sent to wrong destination.
407 +static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
409 + if (skb->nf_bridge)
410 + __skb_push(skb, skb->network_header - skb->mac_header);
413 +static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
415 + if (skb->nf_bridge)
416 + __skb_pull(skb, skb->network_header - skb->mac_header);
419 +#define nf_bridge_adjust_skb_data(s) do {} while (0)
420 +#define nf_bridge_adjust_segmented_data(s) do {} while (0)
423 +static void free_entry(struct nf_queue_entry *entry)
425 + nf_queue_entry_release_refs(entry);
429 +static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev);
431 +static int __imq_nf_queue_gso(struct nf_queue_entry *entry,
432 + struct net_device *dev, struct sk_buff *skb)
435 + struct nf_queue_entry *entry_seg;
437 + nf_bridge_adjust_segmented_data(skb);
439 + if (skb->next == NULL) { /* last packet, no need to copy entry */
440 + struct sk_buff *gso_skb = entry->skb;
442 + ret = __imq_nf_queue(entry, dev);
444 + entry->skb = gso_skb;
450 + entry_seg = nf_queue_entry_dup(entry);
452 + entry_seg->skb = skb;
453 + ret = __imq_nf_queue(entry_seg, dev);
455 + free_entry(entry_seg);
460 +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num)
462 + struct sk_buff *skb, *segs;
463 + struct net_device *dev;
464 + unsigned int queued;
465 + int index, retval, err;
467 + index = entry->skb->imq_flags & IMQ_F_IFMASK;
468 + if (unlikely(index > numdevs - 1)) {
469 + if (net_ratelimit())
470 + pr_warn("IMQ: invalid device specified, highest is %u\n",
476 + /* check for imq device by index from cache */
477 + dev = imq_devs_cache[index];
478 + if (unlikely(!dev)) {
479 + dev = get_imq_device_by_index(index);
481 + retval = PTR_ERR(dev);
486 + if (unlikely(!(dev->flags & IFF_UP))) {
487 + entry->skb->imq_flags = 0;
488 + retval = -ECANCELED;
492 + /* Since 3.10.x, GSO handling moved here as result of upstream commit
493 + * a5fedd43d5f6c94c71053a66e4c3d2e35f1731a2 (netfilter: move
494 + * skb_gso_segment into nfnetlink_queue module).
496 + * Following code replicates the gso handling from
497 + * 'net/netfilter/nfnetlink_queue_core.c':nfqnl_enqueue_packet().
502 + switch (entry->state.pf) {
504 + skb->protocol = htons(ETH_P_IP);
507 + skb->protocol = htons(ETH_P_IPV6);
511 + if (!skb_is_gso(entry->skb))
512 + return __imq_nf_queue(entry, dev);
514 + nf_bridge_adjust_skb_data(skb);
515 + segs = skb_gso_segment(skb, 0);
516 + /* Does not use PTR_ERR to limit the number of error codes that can be
517 + * returned by nf_queue. For instance, callers rely on -ECANCELED to
518 + * mean 'ignore this hook'.
526 + struct sk_buff *nskb = segs->next;
527 + if (nskb && nskb->next)
528 + nskb->cb_next = NULL;
530 + err = __imq_nf_queue_gso(entry, dev, segs);
539 + if (err) /* some segments are already queued */
546 + nf_bridge_adjust_segmented_data(skb);
552 +static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev)
554 + struct sk_buff *skb_orig, *skb, *skb_shared, *skb_popd;
556 + struct netdev_queue *txq;
557 + spinlock_t *root_lock;
559 + int retval = -EINVAL;
560 + unsigned int orig_queue_index;
562 + dev->last_rx = jiffies;
567 + /* skb has owner? => make clone */
568 + if (unlikely(skb->destructor)) {
570 + skb = skb_clone(skb, GFP_ATOMIC);
571 + if (unlikely(!skb)) {
575 + skb->cb_next = NULL;
579 + dev->stats.rx_bytes += skb->len;
580 + dev->stats.rx_packets++;
583 + /* skb->dev == NULL causes problems, try the find cause. */
584 + if (net_ratelimit()) {
585 + dev_warn(&dev->dev,
586 + "received packet with skb->dev == NULL\n");
593 + /* Disables softirqs for lock below */
594 + rcu_read_lock_bh();
596 + /* Multi-queue selection */
597 + orig_queue_index = skb_get_queue_mapping(skb);
598 + txq = imq_select_queue(dev, skb);
600 + q = rcu_dereference(txq->qdisc);
601 + if (unlikely(!q->enqueue))
602 + goto packet_not_eaten_by_imq_dev;
604 + skb->nf_queue_entry = entry;
605 + root_lock = qdisc_lock(q);
606 + spin_lock(root_lock);
608 + users = atomic_read(&skb->users);
610 + skb_shared = skb_get(skb); /* increase reference count by one */
612 + /* backup skb->cb, as qdisc layer will overwrite it */
613 + skb_save_cb(skb_shared);
614 + qdisc_enqueue_root(skb_shared, q); /* might kfree_skb */
615 + if (likely(atomic_read(&skb_shared->users) == users + 1)) {
618 + kfree_skb(skb_shared); /* decrease reference count by one */
620 + skb->destructor = &imq_skb_destructor;
622 + skb_popd = qdisc_dequeue_skb(q, &validate);
625 + if (unlikely(skb_orig))
626 + kfree_skb(skb_orig); /* free original */
628 + spin_unlock(root_lock);
631 + /* schedule qdisc dequeue */
632 + __netif_schedule(q);
634 + if (likely(skb_popd)) {
635 + /* Note that we validate skb (GSO, checksum, ...) outside of locks */
637 + skb_popd = validate_xmit_skb_list(skb_popd, dev);
641 + int cpu = smp_processor_id(); /* ok because BHs are off */
643 + txq = skb_get_tx_queue(dev, skb_popd);
645 + IMQ device will not be frozen or stoped, and it always be successful.
646 + So we need not check its status and return value to accelerate.
648 + if (imq_dev_accurate_stats && txq->xmit_lock_owner != cpu) {
649 + HARD_TX_LOCK(dev, txq, cpu);
650 + if (!netif_xmit_frozen_or_stopped(txq)) {
651 + dev_hard_start_xmit(skb_popd, dev, txq, &dummy_ret);
653 + HARD_TX_UNLOCK(dev, txq);
655 + if (!netif_xmit_frozen_or_stopped(txq)) {
656 + dev_hard_start_xmit(skb_popd, dev, txq, &dummy_ret);
661 + /* No ready skb, then schedule it */
662 + __netif_schedule(q);
665 + rcu_read_unlock_bh();
669 + skb_restore_cb(skb_shared); /* restore skb->cb */
670 + skb->nf_queue_entry = NULL;
672 + * qdisc dropped packet and decreased skb reference count of
673 + * skb, so we don't really want to and try refree as that would
674 + * actually destroy the skb.
676 + spin_unlock(root_lock);
677 + goto packet_not_eaten_by_imq_dev;
680 +packet_not_eaten_by_imq_dev:
681 + skb_set_queue_mapping(skb, orig_queue_index);
682 + rcu_read_unlock_bh();
684 + /* cloned? restore original */
685 + if (unlikely(skb_orig)) {
687 + entry->skb = skb_orig;
693 +static unsigned int imq_nf_hook(void *priv,
694 + struct sk_buff *skb,
695 + const struct nf_hook_state *state)
697 + return (skb->imq_flags & IMQ_F_ENQUEUE) ? NF_IMQ_QUEUE : NF_ACCEPT;
700 +static int imq_close(struct net_device *dev)
702 + netif_stop_queue(dev);
706 +static int imq_open(struct net_device *dev)
708 + netif_start_queue(dev);
712 +static const struct net_device_ops imq_netdev_ops = {
713 + .ndo_open = imq_open,
714 + .ndo_stop = imq_close,
715 + .ndo_start_xmit = imq_dev_xmit,
716 + .ndo_get_stats = imq_get_stats,
719 +static void imq_setup(struct net_device *dev)
721 + dev->netdev_ops = &imq_netdev_ops;
722 + dev->type = ARPHRD_VOID;
723 + dev->mtu = 16000; /* too small? */
724 + dev->tx_queue_len = 11000; /* too big? */
725 + dev->flags = IFF_NOARP;
726 + dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
727 + NETIF_F_GSO | NETIF_F_HW_CSUM |
729 + dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE |
730 + IFF_TX_SKB_SHARING);
733 +static int imq_validate(struct nlattr *tb[], struct nlattr *data[])
737 + if (tb[IFLA_ADDRESS]) {
738 + if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
742 + if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
743 + ret = -EADDRNOTAVAIL;
749 + pr_warn("IMQ: imq_validate failed (%d)\n", ret);
753 +static struct rtnl_link_ops imq_link_ops __read_mostly = {
756 + .setup = imq_setup,
757 + .validate = imq_validate,
760 +static const struct nf_queue_handler imq_nfqh = {
761 + .outfn = imq_nf_queue,
764 +static int __init imq_init_hooks(void)
768 + nf_register_queue_imq_handler(&imq_nfqh);
770 + ret = nf_register_hooks(imq_ops, ARRAY_SIZE(imq_ops));
772 + nf_unregister_queue_imq_handler();
777 +static int __init imq_init_one(int index)
779 + struct net_device *dev;
782 + dev = alloc_netdev_mq(0, "imq%d", NET_NAME_UNKNOWN, imq_setup, numqueues);
786 + ret = dev_alloc_name(dev, dev->name);
790 + dev->rtnl_link_ops = &imq_link_ops;
791 + ret = register_netdevice(dev);
801 +static int __init imq_init_devs(void)
805 + if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) {
806 + pr_err("IMQ: numdevs has to be betweed 1 and %u\n",
811 + if (numqueues < 1 || numqueues > IMQ_MAX_QUEUES) {
812 + pr_err("IMQ: numqueues has to be betweed 1 and %u\n",
817 + get_random_bytes(&imq_hashrnd, sizeof(imq_hashrnd));
820 + err = __rtnl_link_register(&imq_link_ops);
822 + for (i = 0; i < numdevs && !err; i++)
823 + err = imq_init_one(i);
826 + __rtnl_link_unregister(&imq_link_ops);
827 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
834 +static int __init imq_init_module(void)
838 +#if defined(CONFIG_IMQ_NUM_DEVS)
839 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS > 16);
840 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS < 2);
841 + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS - 1 > IMQ_F_IFMASK);
844 + err = imq_init_devs();
846 + pr_err("IMQ: Error trying imq_init_devs(net)\n");
850 + err = imq_init_hooks();
852 + pr_err(KERN_ERR "IMQ: Error trying imq_init_hooks()\n");
853 + rtnl_link_unregister(&imq_link_ops);
854 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
858 + pr_info("IMQ driver loaded successfully. (numdevs = %d, numqueues = %d, imq_dev_accurate_stats = %d)\n",
859 + numdevs, numqueues, imq_dev_accurate_stats);
861 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
862 + pr_info("\tHooking IMQ before NAT on PREROUTING.\n");
864 + pr_info("\tHooking IMQ after NAT on PREROUTING.\n");
866 +#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
867 + pr_info("\tHooking IMQ before NAT on POSTROUTING.\n");
869 + pr_info("\tHooking IMQ after NAT on POSTROUTING.\n");
875 +static void __exit imq_unhook(void)
877 + nf_unregister_hooks(imq_ops, ARRAY_SIZE(imq_ops));
878 + nf_unregister_queue_imq_handler();
881 +static void __exit imq_cleanup_devs(void)
883 + rtnl_link_unregister(&imq_link_ops);
884 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
887 +static void __exit imq_exit_module(void)
890 + imq_cleanup_devs();
891 + pr_info("IMQ driver unloaded successfully.\n");
894 +module_init(imq_init_module);
895 +module_exit(imq_exit_module);
897 +module_param(numdevs, int, 0);
898 +module_param(numqueues, int, 0);
899 +module_param(imq_dev_accurate_stats, int, 0);
900 +MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will be created)");
901 +MODULE_PARM_DESC(numqueues, "number of queues per IMQ device");
902 +MODULE_PARM_DESC(imq_dev_accurate_stats, "Notify if need the accurate imq device stats");
904 +MODULE_AUTHOR("http://https://github.com/imq/linuximq");
905 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See https://github.com/imq/linuximq/wiki for more information.");
906 +MODULE_LICENSE("GPL");
907 +MODULE_ALIAS_RTNL_LINK("imq");
908 diff -Naupr linux-4.7_orig/drivers/net/Kconfig linux-4.7/drivers/net/Kconfig
909 --- linux-4.7_orig/drivers/net/Kconfig 2016-07-25 02:23:50.000000000 +0700
910 +++ linux-4.7/drivers/net/Kconfig 2016-07-26 20:58:55.635901659 +0700
911 @@ -258,6 +258,125 @@ config RIONET_RX_SIZE
916 + tristate "IMQ (intermediate queueing device) support"
917 + depends on NETDEVICES && NETFILTER
919 + The IMQ device(s) is used as placeholder for QoS queueing
920 + disciplines. Every packet entering/leaving the IP stack can be
921 + directed through the IMQ device where it's enqueued/dequeued to the
922 + attached qdisc. This allows you to treat network devices as classes
923 + and distribute bandwidth among them. Iptables is used to specify
924 + through which IMQ device, if any, packets travel.
926 + More information at: https://github.com/imq/linuximq
928 + To compile this driver as a module, choose M here: the module
929 + will be called imq. If unsure, say N.
932 + prompt "IMQ behavior (PRE/POSTROUTING)"
934 + default IMQ_BEHAVIOR_AB
936 + This setting defines how IMQ behaves in respect to its
937 + hooking in PREROUTING and POSTROUTING.
939 + IMQ can work in any of the following ways:
941 + PREROUTING | POSTROUTING
942 + -----------------|-------------------
943 + #1 After NAT | After NAT
944 + #2 After NAT | Before NAT
945 + #3 Before NAT | After NAT
946 + #4 Before NAT | Before NAT
948 + The default behavior is to hook before NAT on PREROUTING
949 + and after NAT on POSTROUTING (#3).
951 + This settings are specially usefull when trying to use IMQ
952 + to shape NATed clients.
954 + More information can be found at: https://github.com/imq/linuximq
956 + If not sure leave the default settings alone.
958 +config IMQ_BEHAVIOR_AA
961 + This setting defines how IMQ behaves in respect to its
962 + hooking in PREROUTING and POSTROUTING.
964 + Choosing this option will make IMQ hook like this:
966 + PREROUTING: After NAT
967 + POSTROUTING: After NAT
969 + More information can be found at: https://github.com/imq/linuximq
971 + If not sure leave the default settings alone.
973 +config IMQ_BEHAVIOR_AB
976 + This setting defines how IMQ behaves in respect to its
977 + hooking in PREROUTING and POSTROUTING.
979 + Choosing this option will make IMQ hook like this:
981 + PREROUTING: After NAT
982 + POSTROUTING: Before NAT
984 + More information can be found at: https://github.com/imq/linuximq
986 + If not sure leave the default settings alone.
988 +config IMQ_BEHAVIOR_BA
991 + This setting defines how IMQ behaves in respect to its
992 + hooking in PREROUTING and POSTROUTING.
994 + Choosing this option will make IMQ hook like this:
996 + PREROUTING: Before NAT
997 + POSTROUTING: After NAT
999 + More information can be found at: https://github.com/imq/linuximq
1001 + If not sure leave the default settings alone.
1003 +config IMQ_BEHAVIOR_BB
1006 + This setting defines how IMQ behaves in respect to its
1007 + hooking in PREROUTING and POSTROUTING.
1009 + Choosing this option will make IMQ hook like this:
1011 + PREROUTING: Before NAT
1012 + POSTROUTING: Before NAT
1014 + More information can be found at: https://github.com/imq/linuximq
1016 + If not sure leave the default settings alone.
1020 +config IMQ_NUM_DEVS
1021 + int "Number of IMQ devices"
1026 + This setting defines how many IMQ devices will be created.
1028 + The default value is 16.
1030 + More information can be found at: https://github.com/imq/linuximq
1032 + If not sure leave the default settings alone.
1035 tristate "Universal TUN/TAP device driver support"
1037 diff -Naupr linux-4.7_orig/drivers/net/Makefile linux-4.7/drivers/net/Makefile
1038 --- linux-4.7_orig/drivers/net/Makefile 2016-07-25 02:23:50.000000000 +0700
1039 +++ linux-4.7/drivers/net/Makefile 2016-07-26 20:58:55.635901659 +0700
1040 @@ -11,6 +11,7 @@ obj-$(CONFIG_DUMMY) += dummy.o
1041 obj-$(CONFIG_EQUALIZER) += eql.o
1042 obj-$(CONFIG_IFB) += ifb.o
1043 obj-$(CONFIG_MACSEC) += macsec.o
1044 +obj-$(CONFIG_IMQ) += imq.o
1045 obj-$(CONFIG_MACVLAN) += macvlan.o
1046 obj-$(CONFIG_MACVTAP) += macvtap.o
1047 obj-$(CONFIG_MII) += mii.o
1048 diff -Naupr linux-4.7_orig/include/linux/imq.h linux-4.7/include/linux/imq.h
1049 --- linux-4.7_orig/include/linux/imq.h 1970-01-01 07:00:00.000000000 +0700
1050 +++ linux-4.7/include/linux/imq.h 2016-07-26 20:58:55.639235009 +0700
1055 +/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
1056 +#define IMQ_F_BITS 5
1058 +#define IMQ_F_IFMASK 0x0f
1059 +#define IMQ_F_ENQUEUE 0x10
1061 +#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1)
1063 +#endif /* _IMQ_H */
1065 diff -Naupr linux-4.7_orig/include/linux/netdevice.h linux-4.7/include/linux/netdevice.h
1066 --- linux-4.7_orig/include/linux/netdevice.h 2016-07-25 02:23:50.000000000 +0700
1067 +++ linux-4.7/include/linux/netdevice.h 2016-07-26 20:58:55.639235009 +0700
1068 @@ -3558,6 +3558,19 @@ static inline void netif_tx_unlock_bh(st
1072 +#define HARD_TX_LOCK_BH(dev, txq) { \
1073 + if ((dev->features & NETIF_F_LLTX) == 0) { \
1074 + __netif_tx_lock_bh(txq); \
1078 +#define HARD_TX_UNLOCK_BH(dev, txq) { \
1079 + if ((dev->features & NETIF_F_LLTX) == 0) { \
1080 + __netif_tx_unlock_bh(txq); \
1085 static inline void netif_tx_disable(struct net_device *dev)
1088 diff -Naupr linux-4.7_orig/include/linux/netfilter/xt_IMQ.h linux-4.7/include/linux/netfilter/xt_IMQ.h
1089 --- linux-4.7_orig/include/linux/netfilter/xt_IMQ.h 1970-01-01 07:00:00.000000000 +0700
1090 +++ linux-4.7/include/linux/netfilter/xt_IMQ.h 2016-07-26 20:58:55.639235009 +0700
1095 +struct xt_imq_info {
1096 + unsigned int todev; /* target imq device */
1099 +#endif /* _XT_IMQ_H */
1101 diff -Naupr linux-4.7_orig/include/linux/netfilter_ipv4/ipt_IMQ.h linux-4.7/include/linux/netfilter_ipv4/ipt_IMQ.h
1102 --- linux-4.7_orig/include/linux/netfilter_ipv4/ipt_IMQ.h 1970-01-01 07:00:00.000000000 +0700
1103 +++ linux-4.7/include/linux/netfilter_ipv4/ipt_IMQ.h 2016-07-26 20:58:55.639235009 +0700
1108 +/* Backwards compatibility for old userspace */
1109 +#include <linux/netfilter/xt_IMQ.h>
1111 +#define ipt_imq_info xt_imq_info
1113 +#endif /* _IPT_IMQ_H */
1115 diff -Naupr linux-4.7_orig/include/linux/netfilter_ipv6/ip6t_IMQ.h linux-4.7/include/linux/netfilter_ipv6/ip6t_IMQ.h
1116 --- linux-4.7_orig/include/linux/netfilter_ipv6/ip6t_IMQ.h 1970-01-01 07:00:00.000000000 +0700
1117 +++ linux-4.7/include/linux/netfilter_ipv6/ip6t_IMQ.h 2016-07-26 20:58:55.639235009 +0700
1119 +#ifndef _IP6T_IMQ_H
1120 +#define _IP6T_IMQ_H
1122 +/* Backwards compatibility for old userspace */
1123 +#include <linux/netfilter/xt_IMQ.h>
1125 +#define ip6t_imq_info xt_imq_info
1127 +#endif /* _IP6T_IMQ_H */
1129 diff -Naupr linux-4.7_orig/include/linux/skbuff.h linux-4.7/include/linux/skbuff.h
1130 --- linux-4.7_orig/include/linux/skbuff.h 2016-07-25 02:23:50.000000000 +0700
1131 +++ linux-4.7/include/linux/skbuff.h 2016-07-26 20:58:55.639235009 +0700
1133 #include <linux/splice.h>
1134 #include <linux/in6.h>
1135 #include <net/flow.h>
1136 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1137 +#include <linux/imq.h>
1141 /* The interface for checksum offload between the stack and networking drivers
1143 @@ -647,6 +651,9 @@ struct sk_buff {
1144 * first. This is owned by whoever has the skb queued ATM.
1146 char cb[48] __aligned(8);
1147 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1151 unsigned long _skb_refdst;
1152 void (*destructor)(struct sk_buff *skb);
1153 @@ -656,6 +663,9 @@ struct sk_buff {
1154 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1155 struct nf_conntrack *nfct;
1157 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1158 + struct nf_queue_entry *nf_queue_entry;
1160 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1161 struct nf_bridge_info *nf_bridge;
1163 @@ -723,6 +733,9 @@ struct sk_buff {
1164 __u8 inner_protocol_type:1;
1165 __u8 remcsum_offload:1;
1166 /* 3 or 5 bit hole */
1167 + #if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1168 + __u8 imq_flags:IMQ_F_BITS;
1171 #ifdef CONFIG_NET_SCHED
1172 __u16 tc_index; /* traffic control index */
1173 @@ -879,6 +892,12 @@ void kfree_skb_list(struct sk_buff *segs
1174 void skb_tx_error(struct sk_buff *skb);
1175 void consume_skb(struct sk_buff *skb);
1176 void __kfree_skb(struct sk_buff *skb);
1178 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1179 +int skb_save_cb(struct sk_buff *skb);
1180 +int skb_restore_cb(struct sk_buff *skb);
1183 extern struct kmem_cache *skbuff_head_cache;
1185 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
1186 @@ -3523,6 +3542,10 @@ static inline void __nf_copy(struct sk_b
1188 dst->nfctinfo = src->nfctinfo;
1190 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1191 + dst->imq_flags = src->imq_flags;
1192 + dst->nf_queue_entry = src->nf_queue_entry;
1194 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1195 dst->nf_bridge = src->nf_bridge;
1196 nf_bridge_get(src->nf_bridge);
1197 diff -Naupr linux-4.7_orig/include/net/netfilter/nf_queue.h linux-4.7/include/net/netfilter/nf_queue.h
1198 --- linux-4.7_orig/include/net/netfilter/nf_queue.h 2016-07-25 02:23:50.000000000 +0700
1199 +++ linux-4.7/include/net/netfilter/nf_queue.h 2016-07-26 20:58:55.642568359 +0700
1200 @@ -31,6 +31,12 @@ struct nf_queue_handler {
1201 void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh);
1202 void nf_unregister_queue_handler(struct net *net);
1203 void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
1204 +void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
1206 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1207 +void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
1208 +void nf_unregister_queue_imq_handler(void);
1211 void nf_queue_entry_get_refs(struct nf_queue_entry *entry);
1212 void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
1213 diff -Naupr linux-4.7_orig/include/net/pkt_sched.h linux-4.7/include/net/pkt_sched.h
1214 --- linux-4.7_orig/include/net/pkt_sched.h 2016-07-25 02:23:50.000000000 +0700
1215 +++ linux-4.7/include/net/pkt_sched.h 2016-07-26 20:58:55.642568359 +0700
1216 @@ -105,6 +105,8 @@ int sch_direct_xmit(struct sk_buff *skb,
1218 void __qdisc_run(struct Qdisc *q);
1220 +struct sk_buff *qdisc_dequeue_skb(struct Qdisc *q, bool *validate);
1222 static inline void qdisc_run(struct Qdisc *q)
1224 if (qdisc_run_begin(q))
1225 diff -Naupr linux-4.7_orig/include/net/sch_generic.h linux-4.7/include/net/sch_generic.h
1226 --- linux-4.7_orig/include/net/sch_generic.h 2016-07-25 02:23:50.000000000 +0700
1227 +++ linux-4.7/include/net/sch_generic.h 2016-07-26 20:58:55.642568359 +0700
1228 @@ -523,6 +523,12 @@ static inline int qdisc_enqueue(struct s
1229 return sch->enqueue(skb, sch);
1232 +static inline int qdisc_enqueue_root(struct sk_buff *skb, struct Qdisc *sch)
1234 + qdisc_skb_cb(skb)->pkt_len = skb->len;
1235 + return qdisc_enqueue(skb, sch) & NET_XMIT_MASK;
1238 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
1240 return q->flags & TCQ_F_CPUSTATS;
1241 diff -Naupr linux-4.7_orig/include/uapi/linux/netfilter.h linux-4.7/include/uapi/linux/netfilter.h
1242 --- linux-4.7_orig/include/uapi/linux/netfilter.h 2016-07-25 02:23:50.000000000 +0700
1243 +++ linux-4.7/include/uapi/linux/netfilter.h 2016-07-26 20:58:55.642568359 +0700
1248 -#define NF_MAX_VERDICT NF_STOP
1249 +#define NF_IMQ_QUEUE 6
1250 +#define NF_MAX_VERDICT NF_IMQ_QUEUE
1252 /* we overload the higher bits for encoding auxiliary data such as the queue
1253 * number or errno values. Not nice, but better than additional function
1254 diff -Naupr linux-4.7_orig/net/core/dev.c linux-4.7/net/core/dev.c
1255 --- linux-4.7_orig/net/core/dev.c 2016-07-25 02:23:50.000000000 +0700
1256 +++ linux-4.7/net/core/dev.c 2016-07-26 20:58:55.642568359 +0700
1258 #include <linux/hrtimer.h>
1259 #include <linux/netfilter_ingress.h>
1260 #include <linux/sctp.h>
1261 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1262 +#include <linux/imq.h>
1265 #include "net-sysfs.h"
1267 @@ -2908,7 +2911,12 @@ static int xmit_one(struct sk_buff *skb,
1271 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1272 + if ((!list_empty(&ptype_all) || !list_empty(&dev->ptype_all)) &&
1273 + !(skb->imq_flags & IMQ_F_ENQUEUE))
1275 if (!list_empty(&ptype_all) || !list_empty(&dev->ptype_all))
1277 dev_queue_xmit_nit(skb, dev);
1280 @@ -2946,6 +2954,7 @@ out:
1284 +EXPORT_SYMBOL(dev_hard_start_xmit);
1286 static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
1287 netdev_features_t features)
1288 @@ -3032,6 +3041,7 @@ struct sk_buff *validate_xmit_skb_list(s
1292 +EXPORT_SYMBOL(validate_xmit_skb_list);
1294 static void qdisc_pkt_len_init(struct sk_buff *skb)
1296 diff -Naupr linux-4.7_orig/net/core/skbuff.c linux-4.7/net/core/skbuff.c
1297 --- linux-4.7_orig/net/core/skbuff.c 2016-07-25 02:23:50.000000000 +0700
1298 +++ linux-4.7/net/core/skbuff.c 2016-07-26 20:58:55.645901708 +0700
1299 @@ -81,6 +81,87 @@ struct kmem_cache *skbuff_head_cache __r
1300 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
1301 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
1302 EXPORT_SYMBOL(sysctl_max_skb_frags);
1303 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1304 +static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
1307 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1308 +/* Control buffer save/restore for IMQ devices */
1309 +struct skb_cb_table {
1310 + char cb[48] __aligned(8);
1315 +static DEFINE_SPINLOCK(skb_cb_store_lock);
1317 +int skb_save_cb(struct sk_buff *skb)
1319 + struct skb_cb_table *next;
1321 + next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
1325 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1327 + memcpy(next->cb, skb->cb, sizeof(skb->cb));
1328 + next->cb_next = skb->cb_next;
1330 + atomic_set(&next->refcnt, 1);
1332 + skb->cb_next = next;
1335 +EXPORT_SYMBOL(skb_save_cb);
1337 +int skb_restore_cb(struct sk_buff *skb)
1339 + struct skb_cb_table *next;
1341 + if (!skb->cb_next)
1344 + next = skb->cb_next;
1346 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1348 + memcpy(skb->cb, next->cb, sizeof(skb->cb));
1349 + skb->cb_next = next->cb_next;
1351 + spin_lock(&skb_cb_store_lock);
1353 + if (atomic_dec_and_test(&next->refcnt))
1354 + kmem_cache_free(skbuff_cb_store_cache, next);
1356 + spin_unlock(&skb_cb_store_lock);
1360 +EXPORT_SYMBOL(skb_restore_cb);
1362 +static void skb_copy_stored_cb(struct sk_buff * , const struct sk_buff * ) __attribute__ ((unused));
1363 +static void skb_copy_stored_cb(struct sk_buff *new, const struct sk_buff *__old)
1365 + struct skb_cb_table *next;
1366 + struct sk_buff *old;
1368 + if (!__old->cb_next) {
1369 + new->cb_next = NULL;
1373 + spin_lock(&skb_cb_store_lock);
1375 + old = (struct sk_buff *)__old;
1377 + next = old->cb_next;
1378 + atomic_inc(&next->refcnt);
1379 + new->cb_next = next;
1381 + spin_unlock(&skb_cb_store_lock);
1386 * skb_panic - private function for out-of-line support
1387 @@ -653,6 +734,28 @@ static void skb_release_head_state(struc
1389 skb->destructor(skb);
1391 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1393 + * This should not happen. When it does, avoid memleak by restoring
1394 + * the chain of cb-backups.
1396 + while (skb->cb_next != NULL) {
1397 + if (net_ratelimit())
1398 + pr_warn("IMQ: kfree_skb: skb->cb_next: %08x\n",
1399 + (unsigned int)(uintptr_t)skb->cb_next);
1401 + skb_restore_cb(skb);
1404 + * This should not happen either, nf_queue_entry is nullified in
1405 + * imq_dev_xmit(). If we have non-NULL nf_queue_entry then we are
1406 + * leaking entry pointers, maybe memory. We don't know if this is
1407 + * pointer to already freed memory, or should this be freed.
1408 + * If this happens we need to add refcounting, etc for nf_queue_entry.
1410 + if (skb->nf_queue_entry && net_ratelimit())
1411 + pr_warn("%s\n", "IMQ: kfree_skb: skb->nf_queue_entry != NULL");
1413 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1414 nf_conntrack_put(skb->nfct);
1416 @@ -842,6 +945,10 @@ static void __copy_skb_header(struct sk_
1417 new->sp = secpath_get(old->sp);
1419 __nf_copy(new, old, false);
1420 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1421 + new->cb_next = NULL;
1422 + /*skb_copy_stored_cb(new, old);*/
1425 /* Note : this field could be in headers_start/headers_end section
1426 * It is not yet because we do not want to have a 16 bit hole
1427 @@ -3433,6 +3540,13 @@ void __init skb_init(void)
1429 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1431 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1432 + skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1433 + sizeof(struct skb_cb_table),
1435 + SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1441 diff -Naupr linux-4.7_orig/net/ipv6/ip6_output.c linux-4.7/net/ipv6/ip6_output.c
1442 --- linux-4.7_orig/net/ipv6/ip6_output.c 2016-07-25 02:23:50.000000000 +0700
1443 +++ linux-4.7/net/ipv6/ip6_output.c 2016-07-26 20:58:55.645901708 +0700
1444 @@ -65,9 +65,6 @@ static int ip6_finish_output2(struct net
1445 struct in6_addr *nexthop;
1448 - skb->protocol = htons(ETH_P_IPV6);
1451 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
1452 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
1454 @@ -142,6 +139,13 @@ int ip6_output(struct net *net, struct s
1459 + * IMQ-patch: moved setting skb->dev and skb->protocol from
1460 + * ip6_finish_output2 to fix crashing at netif_skb_features().
1462 + skb->protocol = htons(ETH_P_IPV6);
1465 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
1466 net, sk, skb, NULL, dev,
1468 diff -Naupr linux-4.7_orig/net/netfilter/core.c linux-4.7/net/netfilter/core.c
1469 --- linux-4.7_orig/net/netfilter/core.c 2016-07-25 02:23:50.000000000 +0700
1470 +++ linux-4.7/net/netfilter/core.c 2016-07-26 20:58:55.645901708 +0700
1471 @@ -311,9 +311,11 @@ next_hook:
1472 ret = NF_DROP_GETERR(verdict);
1475 - } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
1476 + } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE ||
1477 + (verdict & NF_VERDICT_MASK) == NF_IMQ_QUEUE) {
1478 int err = nf_queue(skb, elem, state,
1479 - verdict >> NF_VERDICT_QBITS);
1480 + verdict >> NF_VERDICT_QBITS,
1481 + verdict & NF_VERDICT_MASK);
1483 if (err == -ESRCH &&
1484 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
1485 diff -Naupr linux-4.7_orig/net/netfilter/Kconfig linux-4.7/net/netfilter/Kconfig
1486 --- linux-4.7_orig/net/netfilter/Kconfig 2016-07-25 02:23:50.000000000 +0700
1487 +++ linux-4.7/net/netfilter/Kconfig 2016-07-26 20:58:55.645901708 +0700
1488 @@ -807,6 +807,18 @@ config NETFILTER_XT_TARGET_LOG
1490 To compile it as a module, choose M here. If unsure, say N.
1492 +config NETFILTER_XT_TARGET_IMQ
1493 + tristate '"IMQ" target support'
1494 + depends on NETFILTER_XTABLES
1495 + depends on IP_NF_MANGLE || IP6_NF_MANGLE
1497 + default m if NETFILTER_ADVANCED=n
1499 + This option adds a `IMQ' target which is used to specify if and
1500 + to which imq device packets should get enqueued/dequeued.
1502 + To compile it as a module, choose M here. If unsure, say N.
1504 config NETFILTER_XT_TARGET_MARK
1505 tristate '"MARK" target support'
1506 depends on NETFILTER_ADVANCED
1507 diff -Naupr linux-4.7_orig/net/netfilter/Makefile linux-4.7/net/netfilter/Makefile
1508 --- linux-4.7_orig/net/netfilter/Makefile 2016-07-25 02:23:50.000000000 +0700
1509 +++ linux-4.7/net/netfilter/Makefile 2016-07-26 20:58:55.645901708 +0700
1510 @@ -115,6 +115,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_CT) +=
1511 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
1512 obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
1513 obj-$(CONFIG_NETFILTER_XT_TARGET_HMARK) += xt_HMARK.o
1514 +obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
1515 obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
1516 obj-$(CONFIG_NETFILTER_XT_TARGET_LOG) += xt_LOG.o
1517 obj-$(CONFIG_NETFILTER_XT_TARGET_NETMAP) += xt_NETMAP.o
1518 diff -Naupr linux-4.7_orig/net/netfilter/nf_internals.h linux-4.7/net/netfilter/nf_internals.h
1519 --- linux-4.7_orig/net/netfilter/nf_internals.h 2016-07-25 02:23:50.000000000 +0700
1520 +++ linux-4.7/net/netfilter/nf_internals.h 2016-07-26 20:58:55.645901708 +0700
1521 @@ -18,7 +18,7 @@ unsigned int nf_iterate(struct list_head
1524 int nf_queue(struct sk_buff *skb, struct nf_hook_ops *elem,
1525 - struct nf_hook_state *state, unsigned int queuenum);
1526 + struct nf_hook_state *state, unsigned int queuenum, unsigned int queuetype);
1527 void nf_queue_nf_hook_drop(struct net *net, struct nf_hook_ops *ops);
1528 int __init netfilter_queue_init(void);
1530 diff -Naupr linux-4.7_orig/net/netfilter/nf_queue.c linux-4.7/net/netfilter/nf_queue.c
1531 --- linux-4.7_orig/net/netfilter/nf_queue.c 2016-07-25 02:23:50.000000000 +0700
1532 +++ linux-4.7/net/netfilter/nf_queue.c 2016-07-26 20:58:55.649235058 +0700
1534 * receives, no matter what.
1537 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1538 +static const struct nf_queue_handler __rcu *queue_imq_handler __read_mostly;
1540 +void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
1542 + rcu_assign_pointer(queue_imq_handler, qh);
1544 +EXPORT_SYMBOL_GPL(nf_register_queue_imq_handler);
1546 +void nf_unregister_queue_imq_handler(void)
1548 + RCU_INIT_POINTER(queue_imq_handler, NULL);
1549 + synchronize_rcu();
1551 +EXPORT_SYMBOL_GPL(nf_unregister_queue_imq_handler);
1554 /* return EBUSY when somebody else is registered, return EEXIST if the
1555 * same handler is registered, return 0 in case of success. */
1556 void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh)
1557 @@ -114,7 +131,8 @@ void nf_queue_nf_hook_drop(struct net *n
1558 int nf_queue(struct sk_buff *skb,
1559 struct nf_hook_ops *elem,
1560 struct nf_hook_state *state,
1561 - unsigned int queuenum)
1562 + unsigned int queuenum,
1563 + unsigned int queuetype)
1565 int status = -ENOENT;
1566 struct nf_queue_entry *entry = NULL;
1567 @@ -123,7 +141,17 @@ int nf_queue(struct sk_buff *skb,
1568 struct net *net = state->net;
1570 /* QUEUE == DROP if no one is waiting, to be safe. */
1571 - qh = rcu_dereference(net->nf.queue_handler);
1572 + if (queuetype == NF_IMQ_QUEUE) {
1573 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1574 + qh = rcu_dereference(queue_imq_handler);
1580 + qh = rcu_dereference(net->nf.queue_handler);
1586 @@ -198,8 +226,10 @@ void nf_reinject(struct nf_queue_entry *
1590 + case NF_IMQ_QUEUE:
1591 err = nf_queue(skb, elem, &entry->state,
1592 - verdict >> NF_VERDICT_QBITS);
1593 + verdict >> NF_VERDICT_QBITS,
1594 + verdict & NF_VERDICT_MASK);
1596 if (err == -ESRCH &&
1597 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
1598 diff -Naupr linux-4.7_orig/net/netfilter/xt_IMQ.c linux-4.7/net/netfilter/xt_IMQ.c
1599 --- linux-4.7_orig/net/netfilter/xt_IMQ.c 1970-01-01 07:00:00.000000000 +0700
1600 +++ linux-4.7/net/netfilter/xt_IMQ.c 2016-07-26 20:58:55.649235058 +0700
1603 + * This target marks packets to be enqueued to an imq device
1605 +#include <linux/module.h>
1606 +#include <linux/skbuff.h>
1607 +#include <linux/netfilter/x_tables.h>
1608 +#include <linux/netfilter/xt_IMQ.h>
1609 +#include <linux/imq.h>
1611 +static unsigned int imq_target(struct sk_buff *pskb,
1612 + const struct xt_action_param *par)
1614 + const struct xt_imq_info *mr = par->targinfo;
1616 + pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
1618 + return XT_CONTINUE;
1621 +static int imq_checkentry(const struct xt_tgchk_param *par)
1623 + struct xt_imq_info *mr = par->targinfo;
1625 + if (mr->todev > IMQ_MAX_DEVS - 1) {
1626 + pr_warn("IMQ: invalid device specified, highest is %u\n",
1627 + IMQ_MAX_DEVS - 1);
1634 +static struct xt_target xt_imq_reg[] __read_mostly = {
1637 + .family = AF_INET,
1638 + .checkentry = imq_checkentry,
1639 + .target = imq_target,
1640 + .targetsize = sizeof(struct xt_imq_info),
1641 + .table = "mangle",
1646 + .family = AF_INET6,
1647 + .checkentry = imq_checkentry,
1648 + .target = imq_target,
1649 + .targetsize = sizeof(struct xt_imq_info),
1650 + .table = "mangle",
1655 +static int __init imq_init(void)
1657 + return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1660 +static void __exit imq_fini(void)
1662 + xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1665 +module_init(imq_init);
1666 +module_exit(imq_fini);
1668 +MODULE_AUTHOR("https://github.com/imq/linuximq");
1669 +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See https://github.com/imq/linuximq/wiki for more information.");
1670 +MODULE_LICENSE("GPL");
1671 +MODULE_ALIAS("ipt_IMQ");
1672 +MODULE_ALIAS("ip6t_IMQ");
1674 diff -Naupr linux-4.7_orig/net/sched/sch_generic.c linux-4.7/net/sched/sch_generic.c
1675 --- linux-4.7_orig/net/sched/sch_generic.c 2016-07-25 02:23:50.000000000 +0700
1676 +++ linux-4.7/net/sched/sch_generic.c 2016-07-26 20:58:55.649235058 +0700
1677 @@ -110,6 +110,14 @@ static struct sk_buff *dequeue_skb(struc
1681 +struct sk_buff *qdisc_dequeue_skb(struct Qdisc *q, bool *validate)
1685 + return dequeue_skb(q, validate, &packets);
1687 +EXPORT_SYMBOL(qdisc_dequeue_skb);
1690 * Transmit possibly several skbs, and handle the return status as
1691 * required. Holding the __QDISC___STATE_RUNNING bit guarantees that