]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-imq.patch
i686 updates
[packages/kernel.git] / kernel-imq.patch
CommitLineData
a3f10207
AM
1diff -Naupr linux-4.10_orig/drivers/net/imq.c linux-4.10/drivers/net/imq.c
2--- linux-4.10_orig/drivers/net/imq.c 1970-01-01 07:00:00.000000000 +0700
3+++ linux-4.10/drivers/net/imq.c 2017-02-28 19:03:58.883221583 +0700
4@@ -0,0 +1,945 @@
2380c486
JR
5+/*
6+ * Pseudo-driver for the intermediate queue device.
7+ *
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.
12+ *
13+ * Authors: Patrick McHardy, <kaber@trash.net>
14+ *
15+ * The first version was written by Martin Devera, <devik@cdi.cz>
16+ *
a3f10207 17+ * See Credits.txt
2380c486
JR
18+ */
19+
20+#include <linux/module.h>
21+#include <linux/kernel.h>
22+#include <linux/moduleparam.h>
7f07242b 23+#include <linux/list.h>
2380c486
JR
24+#include <linux/skbuff.h>
25+#include <linux/netdevice.h>
7f07242b 26+#include <linux/etherdevice.h>
2380c486
JR
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>
33+#endif
34+#include <linux/imq.h>
35+#include <net/pkt_sched.h>
36+#include <net/netfilter/nf_queue.h>
f6396b7e
AM
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>
42+#include <net/ip.h>
43+#include <net/ipv6.h>
44+
e2d28598 45+static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num);
2380c486 46+
fa14fc87 47+static nf_hookfn imq_nf_hook;
2380c486 48+
f6396b7e
AM
49+static struct nf_hook_ops imq_ops[] = {
50+ {
51+ /* imq_ingress_ipv4 */
52+ .hook = imq_nf_hook,
f6396b7e
AM
53+ .pf = PF_INET,
54+ .hooknum = NF_INET_PRE_ROUTING,
2380c486 55+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
f6396b7e 56+ .priority = NF_IP_PRI_MANGLE + 1,
2380c486 57+#else
f6396b7e 58+ .priority = NF_IP_PRI_NAT_DST + 1,
2380c486 59+#endif
f6396b7e
AM
60+ },
61+ {
62+ /* imq_egress_ipv4 */
63+ .hook = imq_nf_hook,
f6396b7e
AM
64+ .pf = PF_INET,
65+ .hooknum = NF_INET_POST_ROUTING,
2380c486 66+#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
f6396b7e 67+ .priority = NF_IP_PRI_LAST,
2380c486 68+#else
f6396b7e 69+ .priority = NF_IP_PRI_NAT_SRC - 1,
2380c486 70+#endif
f6396b7e 71+ },
2380c486 72+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
f6396b7e
AM
73+ {
74+ /* imq_ingress_ipv6 */
75+ .hook = imq_nf_hook,
f6396b7e
AM
76+ .pf = PF_INET6,
77+ .hooknum = NF_INET_PRE_ROUTING,
2380c486 78+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
f6396b7e 79+ .priority = NF_IP6_PRI_MANGLE + 1,
2380c486 80+#else
f6396b7e 81+ .priority = NF_IP6_PRI_NAT_DST + 1,
2380c486 82+#endif
f6396b7e
AM
83+ },
84+ {
85+ /* imq_egress_ipv6 */
86+ .hook = imq_nf_hook,
f6396b7e
AM
87+ .pf = PF_INET6,
88+ .hooknum = NF_INET_POST_ROUTING,
2380c486 89+#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
f6396b7e 90+ .priority = NF_IP6_PRI_LAST,
2380c486 91+#else
f6396b7e 92+ .priority = NF_IP6_PRI_NAT_SRC - 1,
2380c486 93+#endif
f6396b7e 94+ },
2380c486 95+#endif
f6396b7e 96+};
2380c486
JR
97+
98+#if defined(CONFIG_IMQ_NUM_DEVS)
f6396b7e 99+static int numdevs = CONFIG_IMQ_NUM_DEVS;
2380c486 100+#else
f6396b7e 101+static int numdevs = IMQ_MAX_DEVS;
2380c486
JR
102+#endif
103+
104+static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
105+
a168f21d
AM
106+#define IMQ_MAX_QUEUES 32
107+static int numqueues = 1;
f6396b7e 108+static u32 imq_hashrnd;
fa14fc87 109+static int imq_dev_accurate_stats = 1;
f6396b7e
AM
110+
111+static inline __be16 pppoe_proto(const struct sk_buff *skb)
112+{
113+ return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
114+ sizeof(struct pppoe_hdr)));
115+}
116+
117+static u16 imq_hash(struct net_device *dev, struct sk_buff *skb)
118+{
119+ unsigned int pull_len;
120+ u16 protocol = skb->protocol;
121+ u32 addr1, addr2;
122+ u32 hash, ihl = 0;
123+ union {
124+ u16 in16[2];
125+ u32 in32;
126+ } ports;
127+ u8 ip_proto;
128+
129+ pull_len = 0;
130+
131+recheck:
132+ switch (protocol) {
133+ case htons(ETH_P_8021Q): {
134+ if (unlikely(skb_pull(skb, VLAN_HLEN) == NULL))
135+ goto other;
136+
137+ pull_len += VLAN_HLEN;
138+ skb->network_header += VLAN_HLEN;
139+
140+ protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
141+ goto recheck;
142+ }
143+
144+ case htons(ETH_P_PPP_SES): {
145+ if (unlikely(skb_pull(skb, PPPOE_SES_HLEN) == NULL))
146+ goto other;
147+
148+ pull_len += PPPOE_SES_HLEN;
149+ skb->network_header += PPPOE_SES_HLEN;
150+
151+ protocol = pppoe_proto(skb);
152+ goto recheck;
153+ }
154+
155+ case htons(ETH_P_IP): {
156+ const struct iphdr *iph = ip_hdr(skb);
157+
158+ if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr))))
159+ goto other;
160+
161+ addr1 = iph->daddr;
162+ addr2 = iph->saddr;
163+
164+ ip_proto = !(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) ?
165+ iph->protocol : 0;
166+ ihl = ip_hdrlen(skb);
167+
168+ break;
169+ }
170+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
171+ case htons(ETH_P_IPV6): {
172+ const struct ipv6hdr *iph = ipv6_hdr(skb);
4bf69007
AM
173+ __be16 fo = 0;
174+
f6396b7e
AM
175+ if (unlikely(!pskb_may_pull(skb, sizeof(struct ipv6hdr))))
176+ goto other;
177+
178+ addr1 = iph->daddr.s6_addr32[3];
179+ addr2 = iph->saddr.s6_addr32[3];
514e5dae
AM
180+ ihl = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &ip_proto,
181+ &fo);
f6396b7e
AM
182+ if (unlikely(ihl < 0))
183+ goto other;
184+
185+ break;
186+ }
187+#endif
188+ default:
189+other:
190+ if (pull_len != 0) {
191+ skb_push(skb, pull_len);
192+ skb->network_header -= pull_len;
193+ }
194+
195+ return (u16)(ntohs(protocol) % dev->real_num_tx_queues);
196+ }
197+
198+ if (addr1 > addr2)
199+ swap(addr1, addr2);
200+
201+ switch (ip_proto) {
202+ case IPPROTO_TCP:
203+ case IPPROTO_UDP:
204+ case IPPROTO_DCCP:
205+ case IPPROTO_ESP:
206+ case IPPROTO_AH:
207+ case IPPROTO_SCTP:
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]);
212+ break;
213+ }
214+ /* fall-through */
215+ }
216+ default:
217+ ports.in32 = 0;
218+ break;
219+ }
220+
221+ if (pull_len != 0) {
222+ skb_push(skb, pull_len);
223+ skb->network_header -= pull_len;
224+ }
225+
226+ hash = jhash_3words(addr1, addr2, ports.in32, imq_hashrnd ^ ip_proto);
227+
228+ return (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
229+}
230+
231+static inline bool sk_tx_queue_recorded(struct sock *sk)
232+{
233+ return (sk_tx_queue_get(sk) >= 0);
234+}
235+
236+static struct netdev_queue *imq_select_queue(struct net_device *dev,
237+ struct sk_buff *skb)
238+{
239+ u16 queue_index = 0;
240+ u32 hash;
241+
242+ if (likely(dev->real_num_tx_queues == 1))
243+ goto out;
244+
245+ /* IMQ can be receiving ingress or engress packets. */
246+
247+ /* Check first for if rx_queue is set */
248+ if (skb_rx_queue_recorded(skb)) {
249+ queue_index = skb_get_rx_queue(skb);
250+ goto out;
251+ }
252+
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);
256+ goto out;
257+ }
258+
259+ /* Try use socket hash */
260+ if (skb->sk && skb->sk->sk_hash) {
261+ hash = skb->sk->sk_hash;
262+ queue_index =
263+ (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
264+ goto out;
265+ }
266+
267+ /* Generate hash from packet data */
268+ queue_index = imq_hash(dev, skb);
269+
270+out:
271+ if (unlikely(queue_index >= dev->real_num_tx_queues))
272+ queue_index = (u16)((u32)queue_index % dev->real_num_tx_queues);
273+
a168f21d 274+ skb_set_queue_mapping(skb, queue_index);
f6396b7e
AM
275+ return netdev_get_tx_queue(dev, queue_index);
276+}
277+
a168f21d
AM
278+static struct net_device_stats *imq_get_stats(struct net_device *dev)
279+{
280+ return &dev->stats;
281+}
282+
283+/* called for packets kfree'd in qdiscs at places other than enqueue */
284+static void imq_skb_destructor(struct sk_buff *skb)
285+{
286+ struct nf_queue_entry *entry = skb->nf_queue_entry;
287+
288+ skb->nf_queue_entry = NULL;
289+
290+ if (entry) {
291+ nf_queue_entry_release_refs(entry);
292+ kfree(entry);
293+ }
294+
295+ skb_restore_cb(skb); /* kfree backup */
296+}
297+
298+static void imq_done_check_queue_mapping(struct sk_buff *skb,
299+ struct net_device *dev)
300+{
301+ unsigned int queue_index;
302+
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);
310+ }
311+ } else {
312+ /* skb->dev was IMQ device itself or NULL, be on safe side and
313+ * just clear queue mapping.
314+ */
315+ skb_set_queue_mapping(skb, 0);
316+ }
317+}
318+
319+static netdev_tx_t imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
320+{
321+ struct nf_queue_entry *entry = skb->nf_queue_entry;
322+
a3f10207
AM
323+ rcu_read_lock();
324+
a168f21d 325+ skb->nf_queue_entry = NULL;
0776672e 326+ netif_trans_update(dev);
a168f21d
AM
327+
328+ dev->stats.tx_bytes += skb->len;
329+ dev->stats.tx_packets++;
330+
331+ if (unlikely(entry == NULL)) {
332+ /* We don't know what is going on here.. packet is queued for
333+ * imq device, but (probably) not by us.
334+ *
335+ * If this packet was not send here by imq_nf_queue(), then
336+ * skb_save_cb() was not used and skb_free() should not show:
337+ * WARNING: IMQ: kfree_skb: skb->cb_next:..
338+ * and/or
339+ * WARNING: IMQ: kfree_skb: skb->nf_queue_entry...
340+ *
341+ * However if this message is shown, then IMQ is somehow broken
342+ * and you should report this to linuximq.net.
343+ */
344+
345+ /* imq_dev_xmit is black hole that eats all packets, report that
346+ * we eat this packet happily and increase dropped counters.
347+ */
348+
349+ dev->stats.tx_dropped++;
350+ dev_kfree_skb(skb);
351+
a3f10207 352+ rcu_read_unlock();
a168f21d
AM
353+ return NETDEV_TX_OK;
354+ }
355+
356+ skb_restore_cb(skb); /* restore skb->cb */
357+
358+ skb->imq_flags = 0;
359+ skb->destructor = NULL;
360+
361+ imq_done_check_queue_mapping(skb, dev);
362+
363+ nf_reinject(entry, NF_ACCEPT);
364+
a3f10207 365+ rcu_read_unlock();
a168f21d
AM
366+ return NETDEV_TX_OK;
367+}
368+
3b94b3c4
AM
369+static struct net_device *get_imq_device_by_index(int index)
370+{
371+ struct net_device *dev = NULL;
372+ struct net *net;
373+ char buf[8];
374+
375+ /* get device by name and cache result */
376+ snprintf(buf, sizeof(buf), "imq%d", index);
377+
378+ /* Search device from all namespaces. */
379+ for_each_net(net) {
380+ dev = dev_get_by_name(net, buf);
381+ if (dev)
382+ break;
383+ }
384+
385+ if (WARN_ON_ONCE(dev == NULL)) {
386+ /* IMQ device not found. Exotic config? */
387+ return ERR_PTR(-ENODEV);
388+ }
389+
390+ imq_devs_cache[index] = dev;
391+ dev_put(dev);
392+
393+ return dev;
394+}
395+
e2d28598 396+static struct nf_queue_entry *nf_queue_entry_dup(struct nf_queue_entry *e)
2380c486 397+{
e2d28598
JR
398+ struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
399+ if (entry) {
deb242c8 400+ nf_queue_entry_get_refs(entry);
a3f10207 401+ return entry;
e2d28598
JR
402+ }
403+ return NULL;
404+}
405+
406+#ifdef CONFIG_BRIDGE_NETFILTER
407+/* When called from bridge netfilter, skb->data must point to MAC header
408+ * before calling skb_gso_segment(). Else, original MAC header is lost
409+ * and segmented skbs will be sent to wrong destination.
410+ */
411+static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
412+{
413+ if (skb->nf_bridge)
414+ __skb_push(skb, skb->network_header - skb->mac_header);
415+}
416+
417+static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
418+{
419+ if (skb->nf_bridge)
420+ __skb_pull(skb, skb->network_header - skb->mac_header);
421+}
422+#else
423+#define nf_bridge_adjust_skb_data(s) do {} while (0)
424+#define nf_bridge_adjust_segmented_data(s) do {} while (0)
425+#endif
426+
427+static void free_entry(struct nf_queue_entry *entry)
428+{
429+ nf_queue_entry_release_refs(entry);
430+ kfree(entry);
431+}
432+
433+static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev);
434+
435+static int __imq_nf_queue_gso(struct nf_queue_entry *entry,
436+ struct net_device *dev, struct sk_buff *skb)
437+{
438+ int ret = -ENOMEM;
439+ struct nf_queue_entry *entry_seg;
440+
441+ nf_bridge_adjust_segmented_data(skb);
442+
443+ if (skb->next == NULL) { /* last packet, no need to copy entry */
444+ struct sk_buff *gso_skb = entry->skb;
445+ entry->skb = skb;
446+ ret = __imq_nf_queue(entry, dev);
447+ if (ret)
448+ entry->skb = gso_skb;
449+ return ret;
450+ }
451+
452+ skb->next = NULL;
453+
454+ entry_seg = nf_queue_entry_dup(entry);
455+ if (entry_seg) {
456+ entry_seg->skb = skb;
457+ ret = __imq_nf_queue(entry_seg, dev);
458+ if (ret)
459+ free_entry(entry_seg);
460+ }
461+ return ret;
462+}
463+
464+static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num)
465+{
466+ struct sk_buff *skb, *segs;
2380c486 467+ struct net_device *dev;
e2d28598
JR
468+ unsigned int queued;
469+ int index, retval, err;
7f07242b 470+
471+ index = entry->skb->imq_flags & IMQ_F_IFMASK;
472+ if (unlikely(index > numdevs - 1)) {
473+ if (net_ratelimit())
514e5dae
AM
474+ pr_warn("IMQ: invalid device specified, highest is %u\n",
475+ numdevs - 1);
7f07242b 476+ retval = -EINVAL;
e2d28598 477+ goto out_no_dev;
7f07242b 478+ }
2380c486
JR
479+
480+ /* check for imq device by index from cache */
481+ dev = imq_devs_cache[index];
7f07242b 482+ if (unlikely(!dev)) {
3b94b3c4
AM
483+ dev = get_imq_device_by_index(index);
484+ if (IS_ERR(dev)) {
485+ retval = PTR_ERR(dev);
e2d28598 486+ goto out_no_dev;
2380c486 487+ }
2380c486
JR
488+ }
489+
7f07242b 490+ if (unlikely(!(dev->flags & IFF_UP))) {
2380c486 491+ entry->skb->imq_flags = 0;
e2d28598
JR
492+ retval = -ECANCELED;
493+ goto out_no_dev;
2380c486 494+ }
e2d28598 495+
e2d28598
JR
496+ /* Since 3.10.x, GSO handling moved here as result of upstream commit
497+ * a5fedd43d5f6c94c71053a66e4c3d2e35f1731a2 (netfilter: move
498+ * skb_gso_segment into nfnetlink_queue module).
499+ *
500+ * Following code replicates the gso handling from
501+ * 'net/netfilter/nfnetlink_queue_core.c':nfqnl_enqueue_packet().
502+ */
503+
504+ skb = entry->skb;
505+
fa14fc87 506+ switch (entry->state.pf) {
e2d28598
JR
507+ case NFPROTO_IPV4:
508+ skb->protocol = htons(ETH_P_IP);
509+ break;
510+ case NFPROTO_IPV6:
511+ skb->protocol = htons(ETH_P_IPV6);
512+ break;
513+ }
514+
fa14fc87
JR
515+ if (!skb_is_gso(entry->skb))
516+ return __imq_nf_queue(entry, dev);
517+
e2d28598
JR
518+ nf_bridge_adjust_skb_data(skb);
519+ segs = skb_gso_segment(skb, 0);
520+ /* Does not use PTR_ERR to limit the number of error codes that can be
521+ * returned by nf_queue. For instance, callers rely on -ECANCELED to
522+ * mean 'ignore this hook'.
523+ */
524+ err = -ENOBUFS;
525+ if (IS_ERR(segs))
526+ goto out_err;
527+ queued = 0;
528+ err = 0;
529+ do {
530+ struct sk_buff *nskb = segs->next;
531+ if (nskb && nskb->next)
532+ nskb->cb_next = NULL;
533+ if (err == 0)
534+ err = __imq_nf_queue_gso(entry, dev, segs);
535+ if (err == 0)
536+ queued++;
537+ else
538+ kfree_skb(segs);
539+ segs = nskb;
540+ } while (segs);
541+
542+ if (queued) {
543+ if (err) /* some segments are already queued */
544+ free_entry(entry);
545+ kfree_skb(skb);
546+ return 0;
547+ }
548+
549+out_err:
550+ nf_bridge_adjust_segmented_data(skb);
551+ retval = err;
552+out_no_dev:
553+ return retval;
554+}
555+
556+static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev)
557+{
fa14fc87 558+ struct sk_buff *skb_orig, *skb, *skb_shared, *skb_popd;
e2d28598 559+ struct Qdisc *q;
36cbf1a5 560+ struct sk_buff *to_free = NULL;
e2d28598
JR
561+ struct netdev_queue *txq;
562+ spinlock_t *root_lock;
563+ int users;
564+ int retval = -EINVAL;
565+ unsigned int orig_queue_index;
566+
2380c486
JR
567+ dev->last_rx = jiffies;
568+
7f07242b 569+ skb = entry->skb;
570+ skb_orig = NULL;
571+
572+ /* skb has owner? => make clone */
573+ if (unlikely(skb->destructor)) {
574+ skb_orig = skb;
575+ skb = skb_clone(skb, GFP_ATOMIC);
f6396b7e 576+ if (unlikely(!skb)) {
7f07242b 577+ retval = -ENOMEM;
578+ goto out;
579+ }
e2d28598 580+ skb->cb_next = NULL;
7f07242b 581+ entry->skb = skb;
2380c486 582+ }
2380c486 583+
7f07242b 584+ dev->stats.rx_bytes += skb->len;
2380c486
JR
585+ dev->stats.rx_packets++;
586+
a168f21d
AM
587+ if (!skb->dev) {
588+ /* skb->dev == NULL causes problems, try the find cause. */
589+ if (net_ratelimit()) {
590+ dev_warn(&dev->dev,
591+ "received packet with skb->dev == NULL\n");
592+ dump_stack();
593+ }
594+
595+ skb->dev = dev;
596+ }
597+
f6396b7e
AM
598+ /* Disables softirqs for lock below */
599+ rcu_read_lock_bh();
600+
601+ /* Multi-queue selection */
a168f21d 602+ orig_queue_index = skb_get_queue_mapping(skb);
f6396b7e 603+ txq = imq_select_queue(dev, skb);
2380c486 604+
7f07242b 605+ q = rcu_dereference(txq->qdisc);
606+ if (unlikely(!q->enqueue))
607+ goto packet_not_eaten_by_imq_dev;
2380c486 608+
c2c0f25c 609+ skb->nf_queue_entry = entry;
f6396b7e
AM
610+ root_lock = qdisc_lock(q);
611+ spin_lock(root_lock);
7f07242b 612+
613+ users = atomic_read(&skb->users);
614+
615+ skb_shared = skb_get(skb); /* increase reference count by one */
514e5dae
AM
616+
617+ /* backup skb->cb, as qdisc layer will overwrite it */
618+ skb_save_cb(skb_shared);
36cbf1a5 619+ qdisc_enqueue_root(skb_shared, q, &to_free); /* might kfree_skb */
7f07242b 620+ if (likely(atomic_read(&skb_shared->users) == users + 1)) {
fa14fc87
JR
621+ bool validate;
622+
7f07242b 623+ kfree_skb(skb_shared); /* decrease reference count by one */
624+
625+ skb->destructor = &imq_skb_destructor;
626+
fa14fc87
JR
627+ skb_popd = qdisc_dequeue_skb(q, &validate);
628+
7f07242b 629+ /* cloned? */
f6396b7e 630+ if (unlikely(skb_orig))
7f07242b 631+ kfree_skb(skb_orig); /* free original */
632+
f6396b7e 633+ spin_unlock(root_lock);
7f07242b 634+
fa14fc87 635+#if 0
7f07242b 636+ /* schedule qdisc dequeue */
637+ __netif_schedule(q);
fa14fc87
JR
638+#else
639+ if (likely(skb_popd)) {
640+ /* Note that we validate skb (GSO, checksum, ...) outside of locks */
641+ if (validate)
642+ skb_popd = validate_xmit_skb_list(skb_popd, dev);
36cbf1a5 643+
fa14fc87
JR
644+ if (skb_popd) {
645+ int dummy_ret;
646+ int cpu = smp_processor_id(); /* ok because BHs are off */
647+
648+ txq = skb_get_tx_queue(dev, skb_popd);
36cbf1a5 649+ /*
fa14fc87
JR
650+ IMQ device will not be frozen or stoped, and it always be successful.
651+ So we need not check its status and return value to accelerate.
652+ */
653+ if (imq_dev_accurate_stats && txq->xmit_lock_owner != cpu) {
654+ HARD_TX_LOCK(dev, txq, cpu);
c2c0f25c
AM
655+ if (!netif_xmit_frozen_or_stopped(txq)) {
656+ dev_hard_start_xmit(skb_popd, dev, txq, &dummy_ret);
657+ }
fa14fc87
JR
658+ HARD_TX_UNLOCK(dev, txq);
659+ } else {
c2c0f25c
AM
660+ if (!netif_xmit_frozen_or_stopped(txq)) {
661+ dev_hard_start_xmit(skb_popd, dev, txq, &dummy_ret);
662+ }
fa14fc87
JR
663+ }
664+ }
c2c0f25c
AM
665+ } else {
666+ /* No ready skb, then schedule it */
667+ __netif_schedule(q);
fa14fc87
JR
668+ }
669+#endif
670+ rcu_read_unlock_bh();
7f07242b 671+ retval = 0;
672+ goto out;
673+ } else {
674+ skb_restore_cb(skb_shared); /* restore skb->cb */
14f08cd0 675+ skb->nf_queue_entry = NULL;
514e5dae
AM
676+ /*
677+ * qdisc dropped packet and decreased skb reference count of
7f07242b 678+ * skb, so we don't really want to and try refree as that would
514e5dae
AM
679+ * actually destroy the skb.
680+ */
f6396b7e 681+ spin_unlock(root_lock);
7f07242b 682+ goto packet_not_eaten_by_imq_dev;
683+ }
684+
685+packet_not_eaten_by_imq_dev:
a168f21d 686+ skb_set_queue_mapping(skb, orig_queue_index);
f6396b7e
AM
687+ rcu_read_unlock_bh();
688+
7f07242b 689+ /* cloned? restore original */
f6396b7e 690+ if (unlikely(skb_orig)) {
7f07242b 691+ kfree_skb(skb);
692+ entry->skb = skb_orig;
693+ }
694+ retval = -1;
695+out:
36cbf1a5
AM
696+ if (unlikely(to_free)) {
697+ kfree_skb_list(to_free);
698+ }
7f07242b 699+ return retval;
2380c486 700+}
deb242c8 701+static unsigned int imq_nf_hook(void *priv,
fa14fc87
JR
702+ struct sk_buff *skb,
703+ const struct nf_hook_state *state)
2380c486 704+{
fa14fc87 705+ return (skb->imq_flags & IMQ_F_ENQUEUE) ? NF_IMQ_QUEUE : NF_ACCEPT;
2380c486
JR
706+}
707+
708+static int imq_close(struct net_device *dev)
709+{
2380c486 710+ netif_stop_queue(dev);
2380c486
JR
711+ return 0;
712+}
713+
714+static int imq_open(struct net_device *dev)
715+{
2380c486 716+ netif_start_queue(dev);
2380c486
JR
717+ return 0;
718+}
719+
a3f10207
AM
720+static struct device_type imq_device_type = {
721+ .name = "imq",
722+};
723+
7f07242b 724+static const struct net_device_ops imq_netdev_ops = {
725+ .ndo_open = imq_open,
726+ .ndo_stop = imq_close,
727+ .ndo_start_xmit = imq_dev_xmit,
728+ .ndo_get_stats = imq_get_stats,
729+};
730+
2380c486
JR
731+static void imq_setup(struct net_device *dev)
732+{
7f07242b 733+ dev->netdev_ops = &imq_netdev_ops;
3b94b3c4
AM
734+ dev->type = ARPHRD_VOID;
735+ dev->mtu = 16000; /* too small? */
736+ dev->tx_queue_len = 11000; /* too big? */
737+ dev->flags = IFF_NOARP;
738+ dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
7f07242b 739+ NETIF_F_GSO | NETIF_F_HW_CSUM |
740+ NETIF_F_HIGHDMA;
3b94b3c4
AM
741+ dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE |
742+ IFF_TX_SKB_SHARING);
7f07242b 743+}
744+
745+static int imq_validate(struct nlattr *tb[], struct nlattr *data[])
746+{
747+ int ret = 0;
748+
749+ if (tb[IFLA_ADDRESS]) {
750+ if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
751+ ret = -EINVAL;
752+ goto end;
753+ }
754+ if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
755+ ret = -EADDRNOTAVAIL;
756+ goto end;
757+ }
758+ }
759+ return 0;
760+end:
514e5dae 761+ pr_warn("IMQ: imq_validate failed (%d)\n", ret);
7f07242b 762+ return ret;
2380c486
JR
763+}
764+
765+static struct rtnl_link_ops imq_link_ops __read_mostly = {
766+ .kind = "imq",
7f07242b 767+ .priv_size = 0,
2380c486 768+ .setup = imq_setup,
7f07242b 769+ .validate = imq_validate,
2380c486
JR
770+};
771+
f6396b7e 772+static const struct nf_queue_handler imq_nfqh = {
f6396b7e
AM
773+ .outfn = imq_nf_queue,
774+};
775+
2380c486
JR
776+static int __init imq_init_hooks(void)
777+{
f6396b7e 778+ int ret;
2380c486 779+
f6396b7e 780+ nf_register_queue_imq_handler(&imq_nfqh);
2380c486 781+
f6396b7e
AM
782+ ret = nf_register_hooks(imq_ops, ARRAY_SIZE(imq_ops));
783+ if (ret < 0)
784+ nf_unregister_queue_imq_handler();
2380c486 785+
f6396b7e 786+ return ret;
2380c486
JR
787+}
788+
a3f10207
AM
789+#ifdef CONFIG_LOCKDEP
790+ static struct lock_class_key imq_netdev_addr_lock_key;
791+
792+ static void __init imq_dev_set_lockdep_one(struct net_device *dev,
793+ struct netdev_queue *txq, void *arg)
794+ {
795+ /*
796+ * the IMQ transmit locks can be taken recursively,
797+ * for example with one IMQ rule for input- and one for
798+ * output network devices in iptables!
799+ * until we find a better solution ignore them.
800+ */
801+ lockdep_set_novalidate_class(&txq->_xmit_lock);
802+ }
803+
804+ static void imq_dev_set_lockdep_class(struct net_device *dev)
805+ {
806+ lockdep_set_class_and_name(&dev->addr_list_lock,
807+ &imq_netdev_addr_lock_key, "_xmit_addr_IMQ");
808+ netdev_for_each_tx_queue(dev, imq_dev_set_lockdep_one, NULL);
809+}
810+#else
811+ static inline void imq_dev_set_lockdep_class(struct net_device *dev)
812+ {
813+ }
814+#endif
815+
2380c486
JR
816+static int __init imq_init_one(int index)
817+{
818+ struct net_device *dev;
819+ int ret;
820+
a1e1a866 821+ dev = alloc_netdev_mq(0, "imq%d", NET_NAME_UNKNOWN, imq_setup, numqueues);
2380c486
JR
822+ if (!dev)
823+ return -ENOMEM;
824+
825+ ret = dev_alloc_name(dev, dev->name);
826+ if (ret < 0)
827+ goto fail;
828+
829+ dev->rtnl_link_ops = &imq_link_ops;
a3f10207 830+ SET_NETDEV_DEVTYPE(dev, &imq_device_type);
2380c486
JR
831+ ret = register_netdevice(dev);
832+ if (ret < 0)
833+ goto fail;
834+
a3f10207
AM
835+ imq_dev_set_lockdep_class(dev);
836+
2380c486
JR
837+ return 0;
838+fail:
839+ free_netdev(dev);
840+ return ret;
841+}
842+
843+static int __init imq_init_devs(void)
844+{
845+ int err, i;
846+
7f07242b 847+ if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) {
514e5dae 848+ pr_err("IMQ: numdevs has to be betweed 1 and %u\n",
2380c486
JR
849+ IMQ_MAX_DEVS);
850+ return -EINVAL;
851+ }
852+
f6396b7e 853+ if (numqueues < 1 || numqueues > IMQ_MAX_QUEUES) {
514e5dae 854+ pr_err("IMQ: numqueues has to be betweed 1 and %u\n",
f6396b7e
AM
855+ IMQ_MAX_QUEUES);
856+ return -EINVAL;
857+ }
858+
859+ get_random_bytes(&imq_hashrnd, sizeof(imq_hashrnd));
860+
2380c486
JR
861+ rtnl_lock();
862+ err = __rtnl_link_register(&imq_link_ops);
863+
864+ for (i = 0; i < numdevs && !err; i++)
865+ err = imq_init_one(i);
866+
867+ if (err) {
868+ __rtnl_link_unregister(&imq_link_ops);
869+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
870+ }
871+ rtnl_unlock();
872+
873+ return err;
874+}
875+
876+static int __init imq_init_module(void)
877+{
878+ int err;
879+
7f07242b 880+#if defined(CONFIG_IMQ_NUM_DEVS)
881+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS > 16);
882+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS < 2);
883+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS - 1 > IMQ_F_IFMASK);
884+#endif
885+
2380c486
JR
886+ err = imq_init_devs();
887+ if (err) {
514e5dae 888+ pr_err("IMQ: Error trying imq_init_devs(net)\n");
2380c486
JR
889+ return err;
890+ }
891+
892+ err = imq_init_hooks();
893+ if (err) {
514e5dae 894+ pr_err(KERN_ERR "IMQ: Error trying imq_init_hooks()\n");
2380c486
JR
895+ rtnl_link_unregister(&imq_link_ops);
896+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
897+ return err;
898+ }
899+
fa14fc87
JR
900+ pr_info("IMQ driver loaded successfully. (numdevs = %d, numqueues = %d, imq_dev_accurate_stats = %d)\n",
901+ numdevs, numqueues, imq_dev_accurate_stats);
2380c486
JR
902+
903+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
514e5dae 904+ pr_info("\tHooking IMQ before NAT on PREROUTING.\n");
2380c486 905+#else
514e5dae 906+ pr_info("\tHooking IMQ after NAT on PREROUTING.\n");
2380c486
JR
907+#endif
908+#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
514e5dae 909+ pr_info("\tHooking IMQ before NAT on POSTROUTING.\n");
2380c486 910+#else
514e5dae 911+ pr_info("\tHooking IMQ after NAT on POSTROUTING.\n");
2380c486
JR
912+#endif
913+
914+ return 0;
915+}
916+
917+static void __exit imq_unhook(void)
918+{
f6396b7e 919+ nf_unregister_hooks(imq_ops, ARRAY_SIZE(imq_ops));
7f07242b 920+ nf_unregister_queue_imq_handler();
2380c486
JR
921+}
922+
923+static void __exit imq_cleanup_devs(void)
924+{
925+ rtnl_link_unregister(&imq_link_ops);
926+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
927+}
928+
929+static void __exit imq_exit_module(void)
930+{
931+ imq_unhook();
932+ imq_cleanup_devs();
514e5dae 933+ pr_info("IMQ driver unloaded successfully.\n");
2380c486
JR
934+}
935+
936+module_init(imq_init_module);
937+module_exit(imq_exit_module);
938+
939+module_param(numdevs, int, 0);
f6396b7e 940+module_param(numqueues, int, 0);
fa14fc87 941+module_param(imq_dev_accurate_stats, int, 0);
514e5dae 942+MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will be created)");
f6396b7e 943+MODULE_PARM_DESC(numqueues, "number of queues per IMQ device");
fa14fc87
JR
944+MODULE_PARM_DESC(imq_dev_accurate_stats, "Notify if need the accurate imq device stats");
945+
d5ed51b7 946+MODULE_AUTHOR("https://github.com/imq/linuximq");
fa14fc87 947+MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See https://github.com/imq/linuximq/wiki for more information.");
2380c486
JR
948+MODULE_LICENSE("GPL");
949+MODULE_ALIAS_RTNL_LINK("imq");
a3f10207
AM
950diff -Naupr linux-4.10_orig/drivers/net/Kconfig linux-4.10/drivers/net/Kconfig
951--- linux-4.10_orig/drivers/net/Kconfig 2017-02-20 05:34:00.000000000 +0700
952+++ linux-4.10/drivers/net/Kconfig 2017-02-28 18:44:55.978280593 +0700
953@@ -260,6 +260,125 @@ config RIONET_RX_SIZE
954 depends on RIONET
955 default "128"
956
957+config IMQ
958+ tristate "IMQ (intermediate queueing device) support"
959+ depends on NETDEVICES && NETFILTER
960+ ---help---
961+ The IMQ device(s) is used as placeholder for QoS queueing
962+ disciplines. Every packet entering/leaving the IP stack can be
963+ directed through the IMQ device where it's enqueued/dequeued to the
964+ attached qdisc. This allows you to treat network devices as classes
965+ and distribute bandwidth among them. Iptables is used to specify
966+ through which IMQ device, if any, packets travel.
967+
968+ More information at: https://github.com/imq/linuximq
969+
970+ To compile this driver as a module, choose M here: the module
971+ will be called imq. If unsure, say N.
972+
973+choice
974+ prompt "IMQ behavior (PRE/POSTROUTING)"
975+ depends on IMQ
976+ default IMQ_BEHAVIOR_AB
977+ help
978+ This setting defines how IMQ behaves in respect to its
979+ hooking in PREROUTING and POSTROUTING.
980+
981+ IMQ can work in any of the following ways:
982+
983+ PREROUTING | POSTROUTING
984+ -----------------|-------------------
985+ #1 After NAT | After NAT
986+ #2 After NAT | Before NAT
987+ #3 Before NAT | After NAT
988+ #4 Before NAT | Before NAT
989+
990+ The default behavior is to hook before NAT on PREROUTING
991+ and after NAT on POSTROUTING (#3).
992+
993+ This settings are specially usefull when trying to use IMQ
994+ to shape NATed clients.
995+
996+ More information can be found at: https://github.com/imq/linuximq
997+
998+ If not sure leave the default settings alone.
999+
1000+config IMQ_BEHAVIOR_AA
1001+ bool "IMQ AA"
1002+ help
1003+ This setting defines how IMQ behaves in respect to its
1004+ hooking in PREROUTING and POSTROUTING.
1005+
1006+ Choosing this option will make IMQ hook like this:
1007+
1008+ PREROUTING: After NAT
1009+ POSTROUTING: After NAT
1010+
1011+ More information can be found at: https://github.com/imq/linuximq
1012+
1013+ If not sure leave the default settings alone.
1014+
1015+config IMQ_BEHAVIOR_AB
1016+ bool "IMQ AB"
1017+ help
1018+ This setting defines how IMQ behaves in respect to its
1019+ hooking in PREROUTING and POSTROUTING.
1020+
1021+ Choosing this option will make IMQ hook like this:
1022+
1023+ PREROUTING: After NAT
1024+ POSTROUTING: Before NAT
1025+
1026+ More information can be found at: https://github.com/imq/linuximq
1027+
1028+ If not sure leave the default settings alone.
1029+
1030+config IMQ_BEHAVIOR_BA
1031+ bool "IMQ BA"
1032+ help
1033+ This setting defines how IMQ behaves in respect to its
1034+ hooking in PREROUTING and POSTROUTING.
1035+
1036+ Choosing this option will make IMQ hook like this:
1037+
1038+ PREROUTING: Before NAT
1039+ POSTROUTING: After NAT
1040+
1041+ More information can be found at: https://github.com/imq/linuximq
1042+
1043+ If not sure leave the default settings alone.
1044+
1045+config IMQ_BEHAVIOR_BB
1046+ bool "IMQ BB"
1047+ help
1048+ This setting defines how IMQ behaves in respect to its
1049+ hooking in PREROUTING and POSTROUTING.
1050+
1051+ Choosing this option will make IMQ hook like this:
1052+
1053+ PREROUTING: Before NAT
1054+ POSTROUTING: Before NAT
1055+
1056+ More information can be found at: https://github.com/imq/linuximq
1057+
1058+ If not sure leave the default settings alone.
1059+
1060+endchoice
1061+
1062+config IMQ_NUM_DEVS
1063+ int "Number of IMQ devices"
1064+ range 2 16
1065+ depends on IMQ
1066+ default "16"
1067+ help
1068+ This setting defines how many IMQ devices will be created.
1069+
1070+ The default value is 16.
1071+
1072+ More information can be found at: https://github.com/imq/linuximq
1073+
1074+ If not sure leave the default settings alone.
1075+
1076 config TUN
1077 tristate "Universal TUN/TAP device driver support"
1078 depends on INET
1079diff -Naupr linux-4.10_orig/drivers/net/Makefile linux-4.10/drivers/net/Makefile
1080--- linux-4.10_orig/drivers/net/Makefile 2017-02-20 05:34:00.000000000 +0700
1081+++ linux-4.10/drivers/net/Makefile 2017-02-28 18:44:55.978280593 +0700
1082@@ -11,6 +11,7 @@ obj-$(CONFIG_DUMMY) += dummy.o
1083 obj-$(CONFIG_EQUALIZER) += eql.o
1084 obj-$(CONFIG_IFB) += ifb.o
1085 obj-$(CONFIG_MACSEC) += macsec.o
1086+obj-$(CONFIG_IMQ) += imq.o
1087 obj-$(CONFIG_MACVLAN) += macvlan.o
1088 obj-$(CONFIG_MACVTAP) += macvtap.o
1089 obj-$(CONFIG_MII) += mii.o
1090diff -Naupr linux-4.10_orig/include/linux/imq.h linux-4.10/include/linux/imq.h
1091--- linux-4.10_orig/include/linux/imq.h 1970-01-01 07:00:00.000000000 +0700
1092+++ linux-4.10/include/linux/imq.h 2017-02-28 18:44:55.978280593 +0700
7f07242b 1093@@ -0,0 +1,13 @@
2380c486
JR
1094+#ifndef _IMQ_H
1095+#define _IMQ_H
1096+
7f07242b 1097+/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
1098+#define IMQ_F_BITS 5
1099+
1100+#define IMQ_F_IFMASK 0x0f
1101+#define IMQ_F_ENQUEUE 0x10
2380c486 1102+
7f07242b 1103+#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1)
2380c486
JR
1104+
1105+#endif /* _IMQ_H */
2380c486 1106+
a3f10207
AM
1107diff -Naupr linux-4.10_orig/include/linux/netdevice.h linux-4.10/include/linux/netdevice.h
1108--- linux-4.10_orig/include/linux/netdevice.h 2017-02-20 05:34:00.000000000 +0700
1109+++ linux-4.10/include/linux/netdevice.h 2017-02-28 18:44:55.978280593 +0700
1110@@ -3604,6 +3604,19 @@ static inline void netif_tx_unlock_bh(st
fa14fc87
JR
1111 } \
1112 }
1113
1114+#define HARD_TX_LOCK_BH(dev, txq) { \
1115+ if ((dev->features & NETIF_F_LLTX) == 0) { \
1116+ __netif_tx_lock_bh(txq); \
1117+ } \
1118+}
1119+
1120+#define HARD_TX_UNLOCK_BH(dev, txq) { \
1121+ if ((dev->features & NETIF_F_LLTX) == 0) { \
1122+ __netif_tx_unlock_bh(txq); \
1123+ } \
1124+}
1125+
1126+
1127 static inline void netif_tx_disable(struct net_device *dev)
1128 {
1129 unsigned int i;
a3f10207
AM
1130diff -Naupr linux-4.10_orig/include/linux/netfilter/xt_IMQ.h linux-4.10/include/linux/netfilter/xt_IMQ.h
1131--- linux-4.10_orig/include/linux/netfilter/xt_IMQ.h 1970-01-01 07:00:00.000000000 +0700
1132+++ linux-4.10/include/linux/netfilter/xt_IMQ.h 2017-02-28 18:44:55.981613941 +0700
7f07242b 1133@@ -0,0 +1,9 @@
1134+#ifndef _XT_IMQ_H
1135+#define _XT_IMQ_H
1136+
1137+struct xt_imq_info {
2380c486
JR
1138+ unsigned int todev; /* target imq device */
1139+};
1140+
7f07242b 1141+#endif /* _XT_IMQ_H */
1142+
a3f10207
AM
1143diff -Naupr linux-4.10_orig/include/linux/netfilter_ipv4/ipt_IMQ.h linux-4.10/include/linux/netfilter_ipv4/ipt_IMQ.h
1144--- linux-4.10_orig/include/linux/netfilter_ipv4/ipt_IMQ.h 1970-01-01 07:00:00.000000000 +0700
1145+++ linux-4.10/include/linux/netfilter_ipv4/ipt_IMQ.h 2017-02-28 18:44:55.981613941 +0700
7f07242b 1146@@ -0,0 +1,10 @@
1147+#ifndef _IPT_IMQ_H
1148+#define _IPT_IMQ_H
1149+
1150+/* Backwards compatibility for old userspace */
1151+#include <linux/netfilter/xt_IMQ.h>
1152+
1153+#define ipt_imq_info xt_imq_info
1154+
2380c486 1155+#endif /* _IPT_IMQ_H */
7f07242b 1156+
a3f10207
AM
1157diff -Naupr linux-4.10_orig/include/linux/netfilter_ipv6/ip6t_IMQ.h linux-4.10/include/linux/netfilter_ipv6/ip6t_IMQ.h
1158--- linux-4.10_orig/include/linux/netfilter_ipv6/ip6t_IMQ.h 1970-01-01 07:00:00.000000000 +0700
1159+++ linux-4.10/include/linux/netfilter_ipv6/ip6t_IMQ.h 2017-02-28 18:44:55.981613941 +0700
7f07242b 1160@@ -0,0 +1,10 @@
2380c486
JR
1161+#ifndef _IP6T_IMQ_H
1162+#define _IP6T_IMQ_H
1163+
7f07242b 1164+/* Backwards compatibility for old userspace */
1165+#include <linux/netfilter/xt_IMQ.h>
1166+
1167+#define ip6t_imq_info xt_imq_info
2380c486
JR
1168+
1169+#endif /* _IP6T_IMQ_H */
7f07242b 1170+
a3f10207
AM
1171diff -Naupr linux-4.10_orig/include/linux/skbuff.h linux-4.10/include/linux/skbuff.h
1172--- linux-4.10_orig/include/linux/skbuff.h 2017-02-20 05:34:00.000000000 +0700
1173+++ linux-4.10/include/linux/skbuff.h 2017-02-28 18:44:55.981613941 +0700
d5ed51b7 1174@@ -39,6 +39,10 @@
c2c0f25c 1175 #include <linux/in6.h>
d5ed51b7 1176 #include <linux/if_packet.h>
79b8bda9 1177 #include <net/flow.h>
7f07242b 1178+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1179+#include <linux/imq.h>
1180+#endif
79b8bda9 1181+
7f07242b 1182
0776672e
AM
1183 /* The interface for checksum offload between the stack and networking drivers
1184 * is as follows...
a3f10207 1185@@ -661,6 +665,9 @@ struct sk_buff {
7f07242b 1186 * first. This is owned by whoever has the skb queued ATM.
1187 */
ca0faea1 1188 char cb[48] __aligned(8);
7f07242b 1189+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1190+ void *cb_next;
1191+#endif
1192
f6396b7e 1193 unsigned long _skb_refdst;
fa14fc87 1194 void (*destructor)(struct sk_buff *skb);
a3f10207 1195@@ -670,6 +677,9 @@ struct sk_buff {
f6b6e03d
AM
1196 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1197 struct nf_conntrack *nfct;
2380c486
JR
1198 #endif
1199+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
fa14fc87 1200+ struct nf_queue_entry *nf_queue_entry;
2380c486 1201+#endif
e26ee53e 1202 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
2380c486
JR
1203 struct nf_bridge_info *nf_bridge;
1204 #endif
a3f10207 1205@@ -750,6 +760,9 @@ struct sk_buff {
36cbf1a5
AM
1206 __u8 offload_fwd_mark:1;
1207 #endif
1208 /* 2, 4 or 5 bit hole */
1209+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1210+ __u8 imq_flags:IMQ_F_BITS;
1211+#endif
7f07242b 1212
fa14fc87
JR
1213 #ifdef CONFIG_NET_SCHED
1214 __u16 tc_index; /* traffic control index */
a3f10207 1215@@ -910,6 +923,12 @@ void kfree_skb_list(struct sk_buff *segs
fa14fc87
JR
1216 void skb_tx_error(struct sk_buff *skb);
1217 void consume_skb(struct sk_buff *skb);
1218 void __kfree_skb(struct sk_buff *skb);
7f07242b 1219+
1220+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
fa14fc87
JR
1221+int skb_save_cb(struct sk_buff *skb);
1222+int skb_restore_cb(struct sk_buff *skb);
7f07242b 1223+#endif
1224+
fa14fc87
JR
1225 extern struct kmem_cache *skbuff_head_cache;
1226
1227 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
a3f10207 1228@@ -3607,6 +3626,10 @@ static inline void __nf_copy(struct sk_b
e26ee53e
JR
1229 if (copy)
1230 dst->nfctinfo = src->nfctinfo;
2380c486
JR
1231 #endif
1232+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
fa14fc87
JR
1233+ dst->imq_flags = src->imq_flags;
1234+ dst->nf_queue_entry = src->nf_queue_entry;
2380c486 1235+#endif
e26ee53e 1236 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
2380c486
JR
1237 dst->nf_bridge = src->nf_bridge;
1238 nf_bridge_get(src->nf_bridge);
a3f10207
AM
1239diff -Naupr linux-4.10_orig/include/net/netfilter/nf_queue.h linux-4.10/include/net/netfilter/nf_queue.h
1240--- linux-4.10_orig/include/net/netfilter/nf_queue.h 2017-02-20 05:34:00.000000000 +0700
1241+++ linux-4.10/include/net/netfilter/nf_queue.h 2017-02-28 18:44:55.981613941 +0700
1242@@ -31,6 +31,12 @@ struct nf_queue_handler {
0776672e
AM
1243 void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh);
1244 void nf_unregister_queue_handler(struct net *net);
daf32129 1245 void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
fa14fc87 1246+void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
7f07242b 1247+
1248+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
fa14fc87
JR
1249+void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
1250+void nf_unregister_queue_imq_handler(void);
7f07242b 1251+#endif
1252
dd5340b2 1253 void nf_queue_entry_get_refs(struct nf_queue_entry *entry);
e2d28598 1254 void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
a3f10207
AM
1255diff -Naupr linux-4.10_orig/include/net/pkt_sched.h linux-4.10/include/net/pkt_sched.h
1256--- linux-4.10_orig/include/net/pkt_sched.h 2017-02-20 05:34:00.000000000 +0700
1257+++ linux-4.10/include/net/pkt_sched.h 2017-02-28 18:44:55.981613941 +0700
1258@@ -107,6 +107,8 @@ int sch_direct_xmit(struct sk_buff *skb,
fa14fc87
JR
1259
1260 void __qdisc_run(struct Qdisc *q);
1261
1262+struct sk_buff *qdisc_dequeue_skb(struct Qdisc *q, bool *validate);
1263+
1264 static inline void qdisc_run(struct Qdisc *q)
1265 {
1266 if (qdisc_run_begin(q))
a3f10207
AM
1267diff -Naupr linux-4.10_orig/include/net/sch_generic.h linux-4.10/include/net/sch_generic.h
1268--- linux-4.10_orig/include/net/sch_generic.h 2017-02-20 05:34:00.000000000 +0700
1269+++ linux-4.10/include/net/sch_generic.h 2017-02-28 18:44:55.981613941 +0700
1270@@ -518,6 +518,13 @@ static inline int qdisc_enqueue(struct s
d5ed51b7 1271 return sch->enqueue(skb, sch, to_free);
c2c0f25c
AM
1272 }
1273
d5ed51b7
JR
1274+static inline int qdisc_enqueue_root(struct sk_buff *skb, struct Qdisc *sch,
1275+ struct sk_buff **to_free)
c2c0f25c
AM
1276+{
1277+ qdisc_skb_cb(skb)->pkt_len = skb->len;
d5ed51b7 1278+ return qdisc_enqueue(skb, sch, to_free) & NET_XMIT_MASK;
c2c0f25c
AM
1279+}
1280+
1281 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
1282 {
1283 return q->flags & TCQ_F_CPUSTATS;
a3f10207
AM
1284diff -Naupr linux-4.10_orig/include/uapi/linux/netfilter.h linux-4.10/include/uapi/linux/netfilter.h
1285--- linux-4.10_orig/include/uapi/linux/netfilter.h 2017-02-20 05:34:00.000000000 +0700
1286+++ linux-4.10/include/uapi/linux/netfilter.h 2017-02-28 18:44:55.981613941 +0700
c2c0f25c 1287@@ -14,7 +14,8 @@
514e5dae
AM
1288 #define NF_QUEUE 3
1289 #define NF_REPEAT 4
68ebd1d1 1290 #define NF_STOP 5 /* Deprecated, for userspace nf_queue compatibility. */
514e5dae
AM
1291-#define NF_MAX_VERDICT NF_STOP
1292+#define NF_IMQ_QUEUE 6
1293+#define NF_MAX_VERDICT NF_IMQ_QUEUE
1294
1295 /* we overload the higher bits for encoding auxiliary data such as the queue
1296 * number or errno values. Not nice, but better than additional function
a3f10207
AM
1297diff -Naupr linux-4.10_orig/net/core/dev.c linux-4.10/net/core/dev.c
1298--- linux-4.10_orig/net/core/dev.c 2017-02-20 05:34:00.000000000 +0700
1299+++ linux-4.10/net/core/dev.c 2017-02-28 18:44:55.984947288 +0700
1300@@ -140,6 +140,9 @@
1301 #include <linux/hrtimer.h>
c2c0f25c 1302 #include <linux/netfilter_ingress.h>
746b5ec8 1303 #include <linux/crash_dump.h>
2380c486
JR
1304+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1305+#include <linux/imq.h>
1306+#endif
514e5dae
AM
1307
1308 #include "net-sysfs.h"
1309
a3f10207 1310@@ -2881,7 +2884,12 @@ static int xmit_one(struct sk_buff *skb,
36cbf1a5
AM
1311 unsigned int len;
1312 int rc;
1313
1314+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1315+ if ((!list_empty(&ptype_all) || !list_empty(&dev->ptype_all)) &&
1316+ !(skb->imq_flags & IMQ_F_ENQUEUE))
1317+#else
1318 if (!list_empty(&ptype_all) || !list_empty(&dev->ptype_all))
1319+#endif
1320 dev_queue_xmit_nit(skb, dev);
1321
1322 len = skb->len;
a3f10207 1323@@ -2920,6 +2928,8 @@ out:
02c6f3d5
JR
1324 return skb;
1325 }
02c6f3d5 1326
36cbf1a5
AM
1327+EXPORT_SYMBOL_GPL(dev_hard_start_xmit);
1328+
02c6f3d5
JR
1329 static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
1330 netdev_features_t features)
36cbf1a5 1331 {
a3f10207
AM
1332diff -Naupr linux-4.10_orig/net/core/skbuff.c linux-4.10/net/core/skbuff.c
1333--- linux-4.10_orig/net/core/skbuff.c 2017-02-20 05:34:00.000000000 +0700
1334+++ linux-4.10/net/core/skbuff.c 2017-02-28 18:44:55.984947288 +0700
1335@@ -82,6 +82,87 @@ struct kmem_cache *skbuff_head_cache __r
7f07242b 1336 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
fd71b1b3
JR
1337 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
1338 EXPORT_SYMBOL(sysctl_max_skb_frags);
7f07242b 1339+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1340+static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
fa14fc87 1341+#endif
9d7f2d61 1342+
fa14fc87 1343+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
7f07242b 1344+/* Control buffer save/restore for IMQ devices */
1345+struct skb_cb_table {
f6396b7e 1346+ char cb[48] __aligned(8);
7f07242b 1347+ void *cb_next;
1348+ atomic_t refcnt;
7f07242b 1349+};
2380c486 1350+
7f07242b 1351+static DEFINE_SPINLOCK(skb_cb_store_lock);
1352+
1353+int skb_save_cb(struct sk_buff *skb)
2380c486 1354+{
7f07242b 1355+ struct skb_cb_table *next;
1356+
1357+ next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
1358+ if (!next)
1359+ return -ENOMEM;
2380c486 1360+
7f07242b 1361+ BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
2380c486 1362+
7f07242b 1363+ memcpy(next->cb, skb->cb, sizeof(skb->cb));
1364+ next->cb_next = skb->cb_next;
1365+
1366+ atomic_set(&next->refcnt, 1);
1367+
1368+ skb->cb_next = next;
1369+ return 0;
2380c486 1370+}
7f07242b 1371+EXPORT_SYMBOL(skb_save_cb);
2380c486 1372+
7f07242b 1373+int skb_restore_cb(struct sk_buff *skb)
2380c486 1374+{
7f07242b 1375+ struct skb_cb_table *next;
2380c486 1376+
7f07242b 1377+ if (!skb->cb_next)
2380c486 1378+ return 0;
7f07242b 1379+
1380+ next = skb->cb_next;
1381+
1382+ BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1383+
1384+ memcpy(skb->cb, next->cb, sizeof(skb->cb));
1385+ skb->cb_next = next->cb_next;
1386+
1387+ spin_lock(&skb_cb_store_lock);
1388+
f6396b7e 1389+ if (atomic_dec_and_test(&next->refcnt))
7f07242b 1390+ kmem_cache_free(skbuff_cb_store_cache, next);
2380c486 1391+
7f07242b 1392+ spin_unlock(&skb_cb_store_lock);
1393+
1394+ return 0;
2380c486 1395+}
7f07242b 1396+EXPORT_SYMBOL(skb_restore_cb);
2380c486 1397+
c2c0f25c 1398+static void skb_copy_stored_cb(struct sk_buff * , const struct sk_buff * ) __attribute__ ((unused));
14f08cd0 1399+static void skb_copy_stored_cb(struct sk_buff *new, const struct sk_buff *__old)
7f07242b 1400+{
1401+ struct skb_cb_table *next;
14f08cd0 1402+ struct sk_buff *old;
7f07242b 1403+
14f08cd0 1404+ if (!__old->cb_next) {
1405+ new->cb_next = NULL;
7f07242b 1406+ return;
1407+ }
1408+
1409+ spin_lock(&skb_cb_store_lock);
1410+
14f08cd0 1411+ old = (struct sk_buff *)__old;
1412+
7f07242b 1413+ next = old->cb_next;
1414+ atomic_inc(&next->refcnt);
1415+ new->cb_next = next;
1416+
1417+ spin_unlock(&skb_cb_store_lock);
1418+}
1419+#endif
1420
9d7f2d61
JR
1421 /**
1422 * skb_panic - private function for out-of-line support
a3f10207 1423@@ -654,6 +735,28 @@ static void skb_release_head_state(struc
7f07242b 1424 WARN_ON(in_irq());
1425 skb->destructor(skb);
1426 }
1427+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
4bf69007
AM
1428+ /*
1429+ * This should not happen. When it does, avoid memleak by restoring
1430+ * the chain of cb-backups.
1431+ */
f6396b7e 1432+ while (skb->cb_next != NULL) {
14f08cd0 1433+ if (net_ratelimit())
514e5dae 1434+ pr_warn("IMQ: kfree_skb: skb->cb_next: %08x\n",
c2c0f25c 1435+ (unsigned int)(uintptr_t)skb->cb_next);
14f08cd0 1436+
7f07242b 1437+ skb_restore_cb(skb);
1438+ }
4bf69007
AM
1439+ /*
1440+ * This should not happen either, nf_queue_entry is nullified in
14f08cd0 1441+ * imq_dev_xmit(). If we have non-NULL nf_queue_entry then we are
1442+ * leaking entry pointers, maybe memory. We don't know if this is
1443+ * pointer to already freed memory, or should this be freed.
1444+ * If this happens we need to add refcounting, etc for nf_queue_entry.
1445+ */
1446+ if (skb->nf_queue_entry && net_ratelimit())
514e5dae 1447+ pr_warn("%s\n", "IMQ: kfree_skb: skb->nf_queue_entry != NULL");
7f07242b 1448+#endif
0acdeb19 1449 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
7f07242b 1450 nf_conntrack_put(skb->nfct);
7af23471 1451 #endif
a3f10207 1452@@ -843,6 +946,10 @@ static void __copy_skb_header(struct sk_
7f07242b 1453 new->sp = secpath_get(old->sp);
1454 #endif
fa14fc87 1455 __nf_copy(new, old, false);
7f07242b 1456+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
e2d28598
JR
1457+ new->cb_next = NULL;
1458+ /*skb_copy_stored_cb(new, old);*/
7f07242b 1459+#endif
e26ee53e
JR
1460
1461 /* Note : this field could be in headers_start/headers_end section
fa14fc87 1462 * It is not yet because we do not want to have a 16 bit hole
a3f10207 1463@@ -3465,6 +3572,13 @@ void __init skb_init(void)
7f07242b 1464 0,
1465 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1466 NULL);
1467+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1468+ skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1469+ sizeof(struct skb_cb_table),
1470+ 0,
1471+ SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1472+ NULL);
1473+#endif
1474 }
1475
1476 /**
a3f10207
AM
1477diff -Naupr linux-4.10_orig/net/ipv6/ip6_output.c linux-4.10/net/ipv6/ip6_output.c
1478--- linux-4.10_orig/net/ipv6/ip6_output.c 2017-02-20 05:34:00.000000000 +0700
1479+++ linux-4.10/net/ipv6/ip6_output.c 2017-02-28 18:44:55.988280636 +0700
1480@@ -67,9 +67,6 @@ static int ip6_finish_output2(struct net
514e5dae
AM
1481 struct in6_addr *nexthop;
1482 int ret;
a168f21d
AM
1483
1484- skb->protocol = htons(ETH_P_IPV6);
1485- skb->dev = dev;
1486-
1487 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
1488 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
1489
a3f10207 1490@@ -159,6 +156,13 @@ int ip6_output(struct net *net, struct s
a168f21d 1491 return 0;
f6396b7e 1492 }
a168f21d 1493
514e5dae 1494+ /*
fa14fc87
JR
1495+ * IMQ-patch: moved setting skb->dev and skb->protocol from
1496+ * ip6_finish_output2 to fix crashing at netif_skb_features().
1497+ */
a168f21d
AM
1498+ skb->protocol = htons(ETH_P_IPV6);
1499+ skb->dev = dev;
1500+
deb242c8
JR
1501 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
1502 net, sk, skb, NULL, dev,
a168f21d 1503 ip6_finish_output,
a3f10207
AM
1504diff -Naupr linux-4.10_orig/net/netfilter/core.c linux-4.10/net/netfilter/core.c
1505--- linux-4.10_orig/net/netfilter/core.c 2017-02-20 05:34:00.000000000 +0700
1506+++ linux-4.10/net/netfilter/core.c 2017-02-28 18:44:55.988280636 +0700
1507@@ -318,6 +318,11 @@ int nf_hook_slow(struct sk_buff *skb, st
1508 if (ret == 0)
1509 ret = -EPERM;
1510 return ret;
1511+ case NF_IMQ_QUEUE:
1512+ ret = nf_queue(skb, state, &entry, verdict);
1513+ if (ret == -ECANCELED)
1514+ continue;
1515+ return ret;
1516 case NF_QUEUE:
1517 ret = nf_queue(skb, state, &entry, verdict);
1518 if (ret == 1 && entry)
1519diff -Naupr linux-4.10_orig/net/netfilter/Kconfig linux-4.10/net/netfilter/Kconfig
1520--- linux-4.10_orig/net/netfilter/Kconfig 2017-02-20 05:34:00.000000000 +0700
1521+++ linux-4.10/net/netfilter/Kconfig 2017-02-28 18:44:55.988280636 +0700
1522@@ -852,6 +852,18 @@ config NETFILTER_XT_TARGET_LOG
4bf69007
AM
1523
1524 To compile it as a module, choose M here. If unsure, say N.
7f07242b 1525
1526+config NETFILTER_XT_TARGET_IMQ
1527+ tristate '"IMQ" target support'
1528+ depends on NETFILTER_XTABLES
1529+ depends on IP_NF_MANGLE || IP6_NF_MANGLE
1530+ select IMQ
1531+ default m if NETFILTER_ADVANCED=n
1532+ help
1533+ This option adds a `IMQ' target which is used to specify if and
1534+ to which imq device packets should get enqueued/dequeued.
2380c486 1535+
7f07242b 1536+ To compile it as a module, choose M here. If unsure, say N.
1537+
1538 config NETFILTER_XT_TARGET_MARK
1539 tristate '"MARK" target support'
f6396b7e 1540 depends on NETFILTER_ADVANCED
a3f10207
AM
1541diff -Naupr linux-4.10_orig/net/netfilter/Makefile linux-4.10/net/netfilter/Makefile
1542--- linux-4.10_orig/net/netfilter/Makefile 2017-02-20 05:34:00.000000000 +0700
1543+++ linux-4.10/net/netfilter/Makefile 2017-02-28 18:44:55.988280636 +0700
1544@@ -125,6 +125,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_CT) +=
7f07242b 1545 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
1546 obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
42d62c0a 1547 obj-$(CONFIG_NETFILTER_XT_TARGET_HMARK) += xt_HMARK.o
4bf69007 1548+obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
14f08cd0 1549 obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
42d62c0a 1550 obj-$(CONFIG_NETFILTER_XT_TARGET_LOG) += xt_LOG.o
514e5dae 1551 obj-$(CONFIG_NETFILTER_XT_TARGET_NETMAP) += xt_NETMAP.o
a3f10207
AM
1552diff -Naupr linux-4.10_orig/net/netfilter/nf_queue.c linux-4.10/net/netfilter/nf_queue.c
1553--- linux-4.10_orig/net/netfilter/nf_queue.c 2017-02-20 05:34:00.000000000 +0700
1554+++ linux-4.10/net/netfilter/nf_queue.c 2017-02-28 18:44:55.988280636 +0700
0776672e
AM
1555@@ -27,6 +27,23 @@
1556 * receives, no matter what.
514e5dae 1557 */
7f07242b 1558
1559+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
514e5dae 1560+static const struct nf_queue_handler __rcu *queue_imq_handler __read_mostly;
7f07242b 1561+
1562+void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
2380c486 1563+{
7f07242b 1564+ rcu_assign_pointer(queue_imq_handler, qh);
2380c486 1565+}
f6396b7e 1566+EXPORT_SYMBOL_GPL(nf_register_queue_imq_handler);
2380c486 1567+
7f07242b 1568+void nf_unregister_queue_imq_handler(void)
2380c486 1569+{
514e5dae
AM
1570+ RCU_INIT_POINTER(queue_imq_handler, NULL);
1571+ synchronize_rcu();
2380c486 1572+}
f6396b7e 1573+EXPORT_SYMBOL_GPL(nf_unregister_queue_imq_handler);
7f07242b 1574+#endif
2380c486 1575+
7f07242b 1576 /* return EBUSY when somebody else is registered, return EEXIST if the
1577 * same handler is registered, return 0 in case of success. */
0776672e 1578 void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh)
a3f10207 1579@@ -108,16 +125,28 @@ void nf_queue_nf_hook_drop(struct net *n
36cbf1a5
AM
1580 }
1581
1582 static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
a3f10207
AM
1583- struct nf_hook_entry *hook_entry, unsigned int queuenum)
1584+ struct nf_hook_entry *hook_entry, unsigned int verdict)
f6396b7e 1585 {
7af23471 1586 int status = -ENOENT;
f6396b7e 1587 struct nf_queue_entry *entry = NULL;
36cbf1a5
AM
1588 const struct nf_afinfo *afinfo;
1589 const struct nf_queue_handler *qh;
0776672e 1590 struct net *net = state->net;
36cbf1a5
AM
1591+ unsigned int queuetype = verdict & NF_VERDICT_MASK;
1592+ unsigned int queuenum = verdict >> NF_VERDICT_QBITS;
7f07242b 1593
deb242c8 1594 /* QUEUE == DROP if no one is waiting, to be safe. */
0776672e 1595- qh = rcu_dereference(net->nf.queue_handler);
a168f21d 1596+ if (queuetype == NF_IMQ_QUEUE) {
7f07242b 1597+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
a3f10207 1598+ qh = rcu_dereference(queue_imq_handler);
7af23471 1599+#else
a3f10207
AM
1600+ BUG();
1601+ goto err_unlock;
7f07242b 1602+#endif
a168f21d 1603+ } else {
0776672e 1604+ qh = rcu_dereference(net->nf.queue_handler);
a168f21d
AM
1605+ }
1606+
7af23471
JR
1607 if (!qh) {
1608 status = -ESRCH;
dd5340b2 1609 goto err;
a3f10207
AM
1610@@ -164,8 +193,17 @@ int nf_queue(struct sk_buff *skb, struct
1611 struct nf_hook_entry *entry = *entryp;
1612 int ret;
1613
1614- ret = __nf_queue(skb, state, entry, verdict >> NF_VERDICT_QBITS);
1615+ ret = __nf_queue(skb, state, entry, verdict);
1616 if (ret < 0) {
1617+
1618+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1619+ /* IMQ Bypass */
1620+ if (ret == -ECANCELED && skb->imq_flags == 0) {
1621+ *entryp = rcu_dereference(entry->next);
1622+ return 1;
1623+ }
1624+#endif
1625+
1626 if (ret == -ESRCH &&
1627 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) {
1628 *entryp = rcu_dereference(entry->next);
1629@@ -232,6 +270,7 @@ okfn:
a168f21d
AM
1630 local_bh_enable();
1631 break;
f6396b7e 1632 case NF_QUEUE:
a168f21d 1633+ case NF_IMQ_QUEUE:
36cbf1a5
AM
1634 err = nf_queue(skb, &entry->state, &hook_entry, verdict);
1635 if (err == 1) {
1636 if (hook_entry)
a3f10207
AM
1637diff -Naupr linux-4.10_orig/net/netfilter/xt_IMQ.c linux-4.10/net/netfilter/xt_IMQ.c
1638--- linux-4.10_orig/net/netfilter/xt_IMQ.c 1970-01-01 07:00:00.000000000 +0700
1639+++ linux-4.10/net/netfilter/xt_IMQ.c 2017-02-28 18:44:55.988280636 +0700
514e5dae 1640@@ -0,0 +1,72 @@
2380c486
JR
1641+/*
1642+ * This target marks packets to be enqueued to an imq device
1643+ */
1644+#include <linux/module.h>
1645+#include <linux/skbuff.h>
7f07242b 1646+#include <linux/netfilter/x_tables.h>
1647+#include <linux/netfilter/xt_IMQ.h>
2380c486
JR
1648+#include <linux/imq.h>
1649+
1650+static unsigned int imq_target(struct sk_buff *pskb,
f6396b7e 1651+ const struct xt_action_param *par)
2380c486 1652+{
7f07242b 1653+ const struct xt_imq_info *mr = par->targinfo;
2380c486 1654+
7f07242b 1655+ pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
2380c486
JR
1656+
1657+ return XT_CONTINUE;
1658+}
1659+
f6396b7e 1660+static int imq_checkentry(const struct xt_tgchk_param *par)
2380c486 1661+{
7f07242b 1662+ struct xt_imq_info *mr = par->targinfo;
2380c486 1663+
7f07242b 1664+ if (mr->todev > IMQ_MAX_DEVS - 1) {
514e5dae
AM
1665+ pr_warn("IMQ: invalid device specified, highest is %u\n",
1666+ IMQ_MAX_DEVS - 1);
f6396b7e 1667+ return -EINVAL;
2380c486
JR
1668+ }
1669+
f6396b7e 1670+ return 0;
2380c486
JR
1671+}
1672+
7f07242b 1673+static struct xt_target xt_imq_reg[] __read_mostly = {
1674+ {
1675+ .name = "IMQ",
1676+ .family = AF_INET,
1677+ .checkentry = imq_checkentry,
1678+ .target = imq_target,
1679+ .targetsize = sizeof(struct xt_imq_info),
1680+ .table = "mangle",
1681+ .me = THIS_MODULE
1682+ },
1683+ {
1684+ .name = "IMQ",
1685+ .family = AF_INET6,
1686+ .checkentry = imq_checkentry,
1687+ .target = imq_target,
1688+ .targetsize = sizeof(struct xt_imq_info),
1689+ .table = "mangle",
1690+ .me = THIS_MODULE
1691+ },
2380c486
JR
1692+};
1693+
7f07242b 1694+static int __init imq_init(void)
2380c486 1695+{
7f07242b 1696+ return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
2380c486
JR
1697+}
1698+
7f07242b 1699+static void __exit imq_fini(void)
2380c486 1700+{
7f07242b 1701+ xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
2380c486
JR
1702+}
1703+
7f07242b 1704+module_init(imq_init);
1705+module_exit(imq_fini);
2380c486 1706+
0776672e 1707+MODULE_AUTHOR("https://github.com/imq/linuximq");
fa14fc87 1708+MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See https://github.com/imq/linuximq/wiki for more information.");
2380c486 1709+MODULE_LICENSE("GPL");
7f07242b 1710+MODULE_ALIAS("ipt_IMQ");
1711+MODULE_ALIAS("ip6t_IMQ");
2380c486 1712+
a3f10207
AM
1713diff -Naupr linux-4.10_orig/net/sched/sch_generic.c linux-4.10/net/sched/sch_generic.c
1714--- linux-4.10_orig/net/sched/sch_generic.c 2017-02-20 05:34:00.000000000 +0700
1715+++ linux-4.10/net/sched/sch_generic.c 2017-02-28 18:44:55.988280636 +0700
1716@@ -154,6 +154,14 @@ bulk:
fa14fc87
JR
1717 return skb;
1718 }
1719
1720+struct sk_buff *qdisc_dequeue_skb(struct Qdisc *q, bool *validate)
1721+{
1722+ int packets;
1723+
1724+ return dequeue_skb(q, validate, &packets);
1725+}
1726+EXPORT_SYMBOL(qdisc_dequeue_skb);
1727+
0776672e
AM
1728 /*
1729 * Transmit possibly several skbs, and handle the return status as
d5ed51b7 1730 * required. Owning running seqcount bit guarantees that
This page took 0.435223 seconds and 4 git commands to generate.