kernel: update to 3.10.23.
[people/teissler/ipfire-2.x.git] / src / patches / imq_kernel3.10.23.patch
CommitLineData
bb5f0bf8
AF
1diff -uNr linux-3.9.1/drivers/net/imq.c linux-3.9.1-imqmq/drivers/net/imq.c
2--- linux-3.9.1/drivers/net/imq.c 1970-01-01 02:00:00.000000000 +0200
3+++ linux-3.9.1-imqmq/drivers/net/imq.c 2013-05-08 17:30:41.715552053 +0300
4@@ -0,0 +1,861 @@
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+ *
17+ * Credits: Jan Rafaj <imq2t@cedric.vabo.cz>
18+ * - Update patch to 2.4.21
19+ * Sebastian Strollo <sstrollo@nortelnetworks.com>
20+ * - Fix "Dead-loop on netdevice imq"-issue
21+ * Marcel Sebek <sebek64@post.cz>
22+ * - Update to 2.6.2-rc1
23+ *
24+ * After some time of inactivity there is a group taking care
25+ * of IMQ again: http://www.linuximq.net
26+ *
27+ *
28+ * 2004/06/30 - New version of IMQ patch to kernels <=2.6.7
29+ * including the following changes:
30+ *
31+ * - Correction of ipv6 support "+"s issue (Hasso Tepper)
32+ * - Correction of imq_init_devs() issue that resulted in
33+ * kernel OOPS unloading IMQ as module (Norbert Buchmuller)
34+ * - Addition of functionality to choose number of IMQ devices
35+ * during kernel config (Andre Correa)
36+ * - Addition of functionality to choose how IMQ hooks on
37+ * PRE and POSTROUTING (after or before NAT) (Andre Correa)
38+ * - Cosmetic corrections (Norbert Buchmuller) (Andre Correa)
39+ *
40+ *
41+ * 2005/12/16 - IMQ versions between 2.6.7 and 2.6.13 were
42+ * released with almost no problems. 2.6.14-x was released
43+ * with some important changes: nfcache was removed; After
44+ * some weeks of trouble we figured out that some IMQ fields
45+ * in skb were missing in skbuff.c - skb_clone and copy_skb_header.
46+ * These functions are correctly patched by this new patch version.
47+ *
48+ * Thanks for all who helped to figure out all the problems with
49+ * 2.6.14.x: Patrick McHardy, Rune Kock, VeNoMouS, Max CtRiX,
50+ * Kevin Shanahan, Richard Lucassen, Valery Dachev (hopefully
51+ * I didn't forget anybody). I apologize again for my lack of time.
52+ *
53+ *
54+ * 2008/06/17 - 2.6.25 - Changed imq.c to use qdisc_run() instead
55+ * of qdisc_restart() and moved qdisc_run() to tasklet to avoid
56+ * recursive locking. New initialization routines to fix 'rmmod' not
57+ * working anymore. Used code from ifb.c. (Jussi Kivilinna)
58+ *
59+ * 2008/08/06 - 2.6.26 - (JK)
60+ * - Replaced tasklet with 'netif_schedule()'.
61+ * - Cleaned up and added comments for imq_nf_queue().
62+ *
63+ * 2009/04/12
64+ * - Add skb_save_cb/skb_restore_cb helper functions for backuping
65+ * control buffer. This is needed because qdisc-layer on kernels
66+ * 2.6.27 and newer overwrite control buffer. (Jussi Kivilinna)
67+ * - Add better locking for IMQ device. Hopefully this will solve
68+ * SMP issues. (Jussi Kivilinna)
69+ * - Port to 2.6.27
70+ * - Port to 2.6.28
71+ * - Port to 2.6.29 + fix rmmod not working
72+ *
73+ * 2009/04/20 - (Jussi Kivilinna)
74+ * - Use netdevice feature flags to avoid extra packet handling
75+ * by core networking layer and possibly increase performance.
76+ *
77+ * 2009/09/26 - (Jussi Kivilinna)
78+ * - Add imq_nf_reinject_lockless to fix deadlock with
79+ * imq_nf_queue/imq_nf_reinject.
80+ *
81+ * 2009/12/08 - (Jussi Kivilinna)
82+ * - Port to 2.6.32
83+ * - Add check for skb->nf_queue_entry==NULL in imq_dev_xmit()
84+ * - Also add better error checking for skb->nf_queue_entry usage
85+ *
86+ * 2010/02/25 - (Jussi Kivilinna)
87+ * - Port to 2.6.33
88+ *
89+ * 2010/08/15 - (Jussi Kivilinna)
90+ * - Port to 2.6.35
91+ * - Simplify hook registration by using nf_register_hooks.
92+ * - nf_reinject doesn't need spinlock around it, therefore remove
93+ * imq_nf_reinject function. Other nf_reinject users protect
94+ * their own data with spinlock. With IMQ however all data is
95+ * needed is stored per skbuff, so no locking is needed.
96+ * - Changed IMQ to use 'separate' NF_IMQ_QUEUE instead of
97+ * NF_QUEUE, this allows working coexistance of IMQ and other
98+ * NF_QUEUE users.
99+ * - Make IMQ multi-queue. Number of IMQ device queues can be
100+ * increased with 'numqueues' module parameters. Default number
101+ * of queues is 1, in other words by default IMQ works as
102+ * single-queue device. Multi-queue selection is based on
103+ * IFB multi-queue patch by Changli Gao <xiaosuo@gmail.com>.
104+ *
105+ * 2011/03/18 - (Jussi Kivilinna)
106+ * - Port to 2.6.38
107+ *
108+ * 2011/07/12 - (syoder89@gmail.com)
109+ * - Crash fix that happens when the receiving interface has more
110+ * than one queue (add missing skb_set_queue_mapping in
111+ * imq_select_queue).
112+ *
113+ * 2011/07/26 - (Jussi Kivilinna)
114+ * - Add queue mapping checks for packets exiting IMQ.
115+ * - Port to 3.0
116+ *
117+ * 2011/08/16 - (Jussi Kivilinna)
118+ * - Clear IFF_TX_SKB_SHARING flag that was added for linux 3.0.2
119+ *
120+ * 2011/11/03 - Germano Michel <germanomichel@gmail.com>
121+ * - Fix IMQ for net namespaces
122+ *
123+ * 2011/11/04 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
124+ * - Port to 3.1
125+ * - Clean-up, move 'get imq device pointer by imqX name' to
126+ * separate function from imq_nf_queue().
127+ *
128+ * 2012/01/05 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
129+ * - Port to 3.2
130+ *
131+ * 2012/03/19 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
132+ * - Port to 3.3
133+ *
134+ * 2012/12/12 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
135+ * - Port to 3.7
136+ * - Fix checkpatch.pl warnings
137+ *
138+ * Also, many thanks to pablo Sebastian Greco for making the initial
139+ * patch and to those who helped the testing.
140+ *
141+ * More info at: http://www.linuximq.net/ (Andre Correa)
142+ */
143+
144+#include <linux/module.h>
145+#include <linux/kernel.h>
146+#include <linux/moduleparam.h>
147+#include <linux/list.h>
148+#include <linux/skbuff.h>
149+#include <linux/netdevice.h>
150+#include <linux/etherdevice.h>
151+#include <linux/rtnetlink.h>
152+#include <linux/if_arp.h>
153+#include <linux/netfilter.h>
154+#include <linux/netfilter_ipv4.h>
155+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
156+ #include <linux/netfilter_ipv6.h>
157+#endif
158+#include <linux/imq.h>
159+#include <net/pkt_sched.h>
160+#include <net/netfilter/nf_queue.h>
161+#include <net/sock.h>
162+#include <linux/ip.h>
163+#include <linux/ipv6.h>
164+#include <linux/if_vlan.h>
165+#include <linux/if_pppox.h>
166+#include <net/ip.h>
167+#include <net/ipv6.h>
168+
169+static int imq_nf_queue(struct nf_queue_entry *entry, unsigned int queue_num);
170+
171+static nf_hookfn imq_nf_hook;
172+
173+static struct nf_hook_ops imq_ops[] = {
174+ {
175+ /* imq_ingress_ipv4 */
176+ .hook = imq_nf_hook,
177+ .owner = THIS_MODULE,
178+ .pf = PF_INET,
179+ .hooknum = NF_INET_PRE_ROUTING,
180+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
181+ .priority = NF_IP_PRI_MANGLE + 1,
182+#else
183+ .priority = NF_IP_PRI_NAT_DST + 1,
184+#endif
185+ },
186+ {
187+ /* imq_egress_ipv4 */
188+ .hook = imq_nf_hook,
189+ .owner = THIS_MODULE,
190+ .pf = PF_INET,
191+ .hooknum = NF_INET_POST_ROUTING,
192+#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
193+ .priority = NF_IP_PRI_LAST,
194+#else
195+ .priority = NF_IP_PRI_NAT_SRC - 1,
196+#endif
197+ },
198+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
199+ {
200+ /* imq_ingress_ipv6 */
201+ .hook = imq_nf_hook,
202+ .owner = THIS_MODULE,
203+ .pf = PF_INET6,
204+ .hooknum = NF_INET_PRE_ROUTING,
205+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
206+ .priority = NF_IP6_PRI_MANGLE + 1,
207+#else
208+ .priority = NF_IP6_PRI_NAT_DST + 1,
209+#endif
210+ },
211+ {
212+ /* imq_egress_ipv6 */
213+ .hook = imq_nf_hook,
214+ .owner = THIS_MODULE,
215+ .pf = PF_INET6,
216+ .hooknum = NF_INET_POST_ROUTING,
217+#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
218+ .priority = NF_IP6_PRI_LAST,
219+#else
220+ .priority = NF_IP6_PRI_NAT_SRC - 1,
221+#endif
222+ },
223+#endif
224+};
225+
226+#if defined(CONFIG_IMQ_NUM_DEVS)
227+static int numdevs = CONFIG_IMQ_NUM_DEVS;
228+#else
229+static int numdevs = IMQ_MAX_DEVS;
230+#endif
231+
232+static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
233+
234+#define IMQ_MAX_QUEUES 32
235+static int numqueues = 1;
236+static u32 imq_hashrnd;
237+
238+static inline __be16 pppoe_proto(const struct sk_buff *skb)
239+{
240+ return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
241+ sizeof(struct pppoe_hdr)));
242+}
243+
244+static u16 imq_hash(struct net_device *dev, struct sk_buff *skb)
245+{
246+ unsigned int pull_len;
247+ u16 protocol = skb->protocol;
248+ u32 addr1, addr2;
249+ u32 hash, ihl = 0;
250+ union {
251+ u16 in16[2];
252+ u32 in32;
253+ } ports;
254+ u8 ip_proto;
255+
256+ pull_len = 0;
257+
258+recheck:
259+ switch (protocol) {
260+ case htons(ETH_P_8021Q): {
261+ if (unlikely(skb_pull(skb, VLAN_HLEN) == NULL))
262+ goto other;
263+
264+ pull_len += VLAN_HLEN;
265+ skb->network_header += VLAN_HLEN;
266+
267+ protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
268+ goto recheck;
269+ }
270+
271+ case htons(ETH_P_PPP_SES): {
272+ if (unlikely(skb_pull(skb, PPPOE_SES_HLEN) == NULL))
273+ goto other;
274+
275+ pull_len += PPPOE_SES_HLEN;
276+ skb->network_header += PPPOE_SES_HLEN;
277+
278+ protocol = pppoe_proto(skb);
279+ goto recheck;
280+ }
281+
282+ case htons(ETH_P_IP): {
283+ const struct iphdr *iph = ip_hdr(skb);
284+
285+ if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr))))
286+ goto other;
287+
288+ addr1 = iph->daddr;
289+ addr2 = iph->saddr;
290+
291+ ip_proto = !(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) ?
292+ iph->protocol : 0;
293+ ihl = ip_hdrlen(skb);
294+
295+ break;
296+ }
297+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
298+ case htons(ETH_P_IPV6): {
299+ const struct ipv6hdr *iph = ipv6_hdr(skb);
300+ __be16 fo = 0;
301+
302+ if (unlikely(!pskb_may_pull(skb, sizeof(struct ipv6hdr))))
303+ goto other;
304+
305+ addr1 = iph->daddr.s6_addr32[3];
306+ addr2 = iph->saddr.s6_addr32[3];
307+ ihl = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &ip_proto,
308+ &fo);
309+ if (unlikely(ihl < 0))
310+ goto other;
311+
312+ break;
313+ }
314+#endif
315+ default:
316+other:
317+ if (pull_len != 0) {
318+ skb_push(skb, pull_len);
319+ skb->network_header -= pull_len;
320+ }
321+
322+ return (u16)(ntohs(protocol) % dev->real_num_tx_queues);
323+ }
324+
325+ if (addr1 > addr2)
326+ swap(addr1, addr2);
327+
328+ switch (ip_proto) {
329+ case IPPROTO_TCP:
330+ case IPPROTO_UDP:
331+ case IPPROTO_DCCP:
332+ case IPPROTO_ESP:
333+ case IPPROTO_AH:
334+ case IPPROTO_SCTP:
335+ case IPPROTO_UDPLITE: {
336+ if (likely(skb_copy_bits(skb, ihl, &ports.in32, 4) >= 0)) {
337+ if (ports.in16[0] > ports.in16[1])
338+ swap(ports.in16[0], ports.in16[1]);
339+ break;
340+ }
341+ /* fall-through */
342+ }
343+ default:
344+ ports.in32 = 0;
345+ break;
346+ }
347+
348+ if (pull_len != 0) {
349+ skb_push(skb, pull_len);
350+ skb->network_header -= pull_len;
351+ }
352+
353+ hash = jhash_3words(addr1, addr2, ports.in32, imq_hashrnd ^ ip_proto);
354+
355+ return (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
356+}
357+
358+static inline bool sk_tx_queue_recorded(struct sock *sk)
359+{
360+ return (sk_tx_queue_get(sk) >= 0);
361+}
362+
363+static struct netdev_queue *imq_select_queue(struct net_device *dev,
364+ struct sk_buff *skb)
365+{
366+ u16 queue_index = 0;
367+ u32 hash;
368+
369+ if (likely(dev->real_num_tx_queues == 1))
370+ goto out;
371+
372+ /* IMQ can be receiving ingress or engress packets. */
373+
374+ /* Check first for if rx_queue is set */
375+ if (skb_rx_queue_recorded(skb)) {
376+ queue_index = skb_get_rx_queue(skb);
377+ goto out;
378+ }
379+
380+ /* Check if socket has tx_queue set */
381+ if (sk_tx_queue_recorded(skb->sk)) {
382+ queue_index = sk_tx_queue_get(skb->sk);
383+ goto out;
384+ }
385+
386+ /* Try use socket hash */
387+ if (skb->sk && skb->sk->sk_hash) {
388+ hash = skb->sk->sk_hash;
389+ queue_index =
390+ (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
391+ goto out;
392+ }
393+
394+ /* Generate hash from packet data */
395+ queue_index = imq_hash(dev, skb);
396+
397+out:
398+ if (unlikely(queue_index >= dev->real_num_tx_queues))
399+ queue_index = (u16)((u32)queue_index % dev->real_num_tx_queues);
400+
401+ skb_set_queue_mapping(skb, queue_index);
402+ return netdev_get_tx_queue(dev, queue_index);
403+}
404+
405+static struct net_device_stats *imq_get_stats(struct net_device *dev)
406+{
407+ return &dev->stats;
408+}
409+
410+/* called for packets kfree'd in qdiscs at places other than enqueue */
411+static void imq_skb_destructor(struct sk_buff *skb)
412+{
413+ struct nf_queue_entry *entry = skb->nf_queue_entry;
414+
415+ skb->nf_queue_entry = NULL;
416+
417+ if (entry) {
418+ nf_queue_entry_release_refs(entry);
419+ kfree(entry);
420+ }
421+
422+ skb_restore_cb(skb); /* kfree backup */
423+}
424+
425+static void imq_done_check_queue_mapping(struct sk_buff *skb,
426+ struct net_device *dev)
427+{
428+ unsigned int queue_index;
429+
430+ /* Don't let queue_mapping be left too large after exiting IMQ */
431+ if (likely(skb->dev != dev && skb->dev != NULL)) {
432+ queue_index = skb_get_queue_mapping(skb);
433+ if (unlikely(queue_index >= skb->dev->real_num_tx_queues)) {
434+ queue_index = (u16)((u32)queue_index %
435+ skb->dev->real_num_tx_queues);
436+ skb_set_queue_mapping(skb, queue_index);
437+ }
438+ } else {
439+ /* skb->dev was IMQ device itself or NULL, be on safe side and
440+ * just clear queue mapping.
441+ */
442+ skb_set_queue_mapping(skb, 0);
443+ }
444+}
445+
446+static netdev_tx_t imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
447+{
448+ struct nf_queue_entry *entry = skb->nf_queue_entry;
449+
450+ skb->nf_queue_entry = NULL;
451+ dev->trans_start = jiffies;
452+
453+ dev->stats.tx_bytes += skb->len;
454+ dev->stats.tx_packets++;
455+
456+ if (unlikely(entry == NULL)) {
457+ /* We don't know what is going on here.. packet is queued for
458+ * imq device, but (probably) not by us.
459+ *
460+ * If this packet was not send here by imq_nf_queue(), then
461+ * skb_save_cb() was not used and skb_free() should not show:
462+ * WARNING: IMQ: kfree_skb: skb->cb_next:..
463+ * and/or
464+ * WARNING: IMQ: kfree_skb: skb->nf_queue_entry...
465+ *
466+ * However if this message is shown, then IMQ is somehow broken
467+ * and you should report this to linuximq.net.
468+ */
469+
470+ /* imq_dev_xmit is black hole that eats all packets, report that
471+ * we eat this packet happily and increase dropped counters.
472+ */
473+
474+ dev->stats.tx_dropped++;
475+ dev_kfree_skb(skb);
476+
477+ return NETDEV_TX_OK;
478+ }
479+
480+ skb_restore_cb(skb); /* restore skb->cb */
481+
482+ skb->imq_flags = 0;
483+ skb->destructor = NULL;
484+
485+ imq_done_check_queue_mapping(skb, dev);
486+
487+ nf_reinject(entry, NF_ACCEPT);
488+
489+ return NETDEV_TX_OK;
490+}
491+
492+static struct net_device *get_imq_device_by_index(int index)
493+{
494+ struct net_device *dev = NULL;
495+ struct net *net;
496+ char buf[8];
497+
498+ /* get device by name and cache result */
499+ snprintf(buf, sizeof(buf), "imq%d", index);
500+
501+ /* Search device from all namespaces. */
502+ for_each_net(net) {
503+ dev = dev_get_by_name(net, buf);
504+ if (dev)
505+ break;
506+ }
507+
508+ if (WARN_ON_ONCE(dev == NULL)) {
509+ /* IMQ device not found. Exotic config? */
510+ return ERR_PTR(-ENODEV);
511+ }
512+
513+ imq_devs_cache[index] = dev;
514+ dev_put(dev);
515+
516+ return dev;
517+}
518+
519+static int imq_nf_queue(struct nf_queue_entry *entry, unsigned int queue_num)
520+{
521+ struct net_device *dev;
522+ struct sk_buff *skb_orig, *skb, *skb_shared;
523+ struct Qdisc *q;
524+ struct netdev_queue *txq;
525+ spinlock_t *root_lock;
526+ int users, index;
527+ int retval = -EINVAL;
528+ unsigned int orig_queue_index;
529+
530+ index = entry->skb->imq_flags & IMQ_F_IFMASK;
531+ if (unlikely(index > numdevs - 1)) {
532+ if (net_ratelimit())
533+ pr_warn("IMQ: invalid device specified, highest is %u\n",
534+ numdevs - 1);
535+ retval = -EINVAL;
536+ goto out;
537+ }
538+
539+ /* check for imq device by index from cache */
540+ dev = imq_devs_cache[index];
541+ if (unlikely(!dev)) {
542+ dev = get_imq_device_by_index(index);
543+ if (IS_ERR(dev)) {
544+ retval = PTR_ERR(dev);
545+ goto out;
546+ }
547+ }
548+
549+ if (unlikely(!(dev->flags & IFF_UP))) {
550+ entry->skb->imq_flags = 0;
551+ nf_reinject(entry, NF_ACCEPT);
552+ retval = 0;
553+ goto out;
554+ }
555+ dev->last_rx = jiffies;
556+
557+ skb = entry->skb;
558+ skb_orig = NULL;
559+
560+ /* skb has owner? => make clone */
561+ if (unlikely(skb->destructor)) {
562+ skb_orig = skb;
563+ skb = skb_clone(skb, GFP_ATOMIC);
564+ if (unlikely(!skb)) {
565+ retval = -ENOMEM;
566+ goto out;
567+ }
568+ entry->skb = skb;
569+ }
570+
571+ skb->nf_queue_entry = entry;
572+
573+ dev->stats.rx_bytes += skb->len;
574+ dev->stats.rx_packets++;
575+
576+ if (!skb->dev) {
577+ /* skb->dev == NULL causes problems, try the find cause. */
578+ if (net_ratelimit()) {
579+ dev_warn(&dev->dev,
580+ "received packet with skb->dev == NULL\n");
581+ dump_stack();
582+ }
583+
584+ skb->dev = dev;
585+ }
586+
587+ /* Disables softirqs for lock below */
588+ rcu_read_lock_bh();
589+
590+ /* Multi-queue selection */
591+ orig_queue_index = skb_get_queue_mapping(skb);
592+ txq = imq_select_queue(dev, skb);
593+
594+ q = rcu_dereference(txq->qdisc);
595+ if (unlikely(!q->enqueue))
596+ goto packet_not_eaten_by_imq_dev;
597+
598+ root_lock = qdisc_lock(q);
599+ spin_lock(root_lock);
600+
601+ users = atomic_read(&skb->users);
602+
603+ skb_shared = skb_get(skb); /* increase reference count by one */
604+
605+ /* backup skb->cb, as qdisc layer will overwrite it */
606+ skb_save_cb(skb_shared);
607+ qdisc_enqueue_root(skb_shared, q); /* might kfree_skb */
608+
609+ if (likely(atomic_read(&skb_shared->users) == users + 1)) {
610+ kfree_skb(skb_shared); /* decrease reference count by one */
611+
612+ skb->destructor = &imq_skb_destructor;
613+
614+ /* cloned? */
615+ if (unlikely(skb_orig))
616+ kfree_skb(skb_orig); /* free original */
617+
618+ spin_unlock(root_lock);
619+ rcu_read_unlock_bh();
620+
621+ /* schedule qdisc dequeue */
622+ __netif_schedule(q);
623+
624+ retval = 0;
625+ goto out;
626+ } else {
627+ skb_restore_cb(skb_shared); /* restore skb->cb */
628+ skb->nf_queue_entry = NULL;
629+ /*
630+ * qdisc dropped packet and decreased skb reference count of
631+ * skb, so we don't really want to and try refree as that would
632+ * actually destroy the skb.
633+ */
634+ spin_unlock(root_lock);
635+ goto packet_not_eaten_by_imq_dev;
636+ }
637+
638+packet_not_eaten_by_imq_dev:
639+ skb_set_queue_mapping(skb, orig_queue_index);
640+ rcu_read_unlock_bh();
641+
642+ /* cloned? restore original */
643+ if (unlikely(skb_orig)) {
644+ kfree_skb(skb);
645+ entry->skb = skb_orig;
646+ }
647+ retval = -1;
648+out:
649+ return retval;
650+}
651+
652+static unsigned int imq_nf_hook(unsigned int hook, struct sk_buff *pskb,
653+ const struct net_device *indev,
654+ const struct net_device *outdev,
655+ int (*okfn)(struct sk_buff *))
656+{
657+ return (pskb->imq_flags & IMQ_F_ENQUEUE) ? NF_IMQ_QUEUE : NF_ACCEPT;
658+}
659+
660+static int imq_close(struct net_device *dev)
661+{
662+ netif_stop_queue(dev);
663+ return 0;
664+}
665+
666+static int imq_open(struct net_device *dev)
667+{
668+ netif_start_queue(dev);
669+ return 0;
670+}
671+
672+static const struct net_device_ops imq_netdev_ops = {
673+ .ndo_open = imq_open,
674+ .ndo_stop = imq_close,
675+ .ndo_start_xmit = imq_dev_xmit,
676+ .ndo_get_stats = imq_get_stats,
677+};
678+
679+static void imq_setup(struct net_device *dev)
680+{
681+ dev->netdev_ops = &imq_netdev_ops;
682+ dev->type = ARPHRD_VOID;
683+ dev->mtu = 16000; /* too small? */
684+ dev->tx_queue_len = 11000; /* too big? */
685+ dev->flags = IFF_NOARP;
686+ dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
687+ NETIF_F_GSO | NETIF_F_HW_CSUM |
688+ NETIF_F_HIGHDMA;
689+ dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE |
690+ IFF_TX_SKB_SHARING);
691+}
692+
693+static int imq_validate(struct nlattr *tb[], struct nlattr *data[])
694+{
695+ int ret = 0;
696+
697+ if (tb[IFLA_ADDRESS]) {
698+ if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
699+ ret = -EINVAL;
700+ goto end;
701+ }
702+ if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
703+ ret = -EADDRNOTAVAIL;
704+ goto end;
705+ }
706+ }
707+ return 0;
708+end:
709+ pr_warn("IMQ: imq_validate failed (%d)\n", ret);
710+ return ret;
711+}
712+
713+static struct rtnl_link_ops imq_link_ops __read_mostly = {
714+ .kind = "imq",
715+ .priv_size = 0,
716+ .setup = imq_setup,
717+ .validate = imq_validate,
718+};
719+
720+static const struct nf_queue_handler imq_nfqh = {
721+ .outfn = imq_nf_queue,
722+};
723+
724+static int __init imq_init_hooks(void)
725+{
726+ int ret;
727+
728+ nf_register_queue_imq_handler(&imq_nfqh);
729+
730+ ret = nf_register_hooks(imq_ops, ARRAY_SIZE(imq_ops));
731+ if (ret < 0)
732+ nf_unregister_queue_imq_handler();
733+
734+ return ret;
735+}
736+
737+static int __init imq_init_one(int index)
738+{
739+ struct net_device *dev;
740+ int ret;
741+
742+ dev = alloc_netdev_mq(0, "imq%d", imq_setup, numqueues);
743+ if (!dev)
744+ return -ENOMEM;
745+
746+ ret = dev_alloc_name(dev, dev->name);
747+ if (ret < 0)
748+ goto fail;
749+
750+ dev->rtnl_link_ops = &imq_link_ops;
751+ ret = register_netdevice(dev);
752+ if (ret < 0)
753+ goto fail;
754+
755+ return 0;
756+fail:
757+ free_netdev(dev);
758+ return ret;
759+}
760+
761+static int __init imq_init_devs(void)
762+{
763+ int err, i;
764+
765+ if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) {
766+ pr_err("IMQ: numdevs has to be betweed 1 and %u\n",
767+ IMQ_MAX_DEVS);
768+ return -EINVAL;
769+ }
770+
771+ if (numqueues < 1 || numqueues > IMQ_MAX_QUEUES) {
772+ pr_err("IMQ: numqueues has to be betweed 1 and %u\n",
773+ IMQ_MAX_QUEUES);
774+ return -EINVAL;
775+ }
776+
777+ get_random_bytes(&imq_hashrnd, sizeof(imq_hashrnd));
778+
779+ rtnl_lock();
780+ err = __rtnl_link_register(&imq_link_ops);
781+
782+ for (i = 0; i < numdevs && !err; i++)
783+ err = imq_init_one(i);
784+
785+ if (err) {
786+ __rtnl_link_unregister(&imq_link_ops);
787+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
788+ }
789+ rtnl_unlock();
790+
791+ return err;
792+}
793+
794+static int __init imq_init_module(void)
795+{
796+ int err;
797+
798+#if defined(CONFIG_IMQ_NUM_DEVS)
799+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS > 16);
800+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS < 2);
801+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS - 1 > IMQ_F_IFMASK);
802+#endif
803+
804+ err = imq_init_devs();
805+ if (err) {
806+ pr_err("IMQ: Error trying imq_init_devs(net)\n");
807+ return err;
808+ }
809+
810+ err = imq_init_hooks();
811+ if (err) {
812+ pr_err(KERN_ERR "IMQ: Error trying imq_init_hooks()\n");
813+ rtnl_link_unregister(&imq_link_ops);
814+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
815+ return err;
816+ }
817+
818+ pr_info("IMQ driver loaded successfully. (numdevs = %d, numqueues = %d)\n",
819+ numdevs, numqueues);
820+
821+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
822+ pr_info("\tHooking IMQ before NAT on PREROUTING.\n");
823+#else
824+ pr_info("\tHooking IMQ after NAT on PREROUTING.\n");
825+#endif
826+#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
827+ pr_info("\tHooking IMQ before NAT on POSTROUTING.\n");
828+#else
829+ pr_info("\tHooking IMQ after NAT on POSTROUTING.\n");
830+#endif
831+
832+ return 0;
833+}
834+
835+static void __exit imq_unhook(void)
836+{
837+ nf_unregister_hooks(imq_ops, ARRAY_SIZE(imq_ops));
838+ nf_unregister_queue_imq_handler();
839+}
840+
841+static void __exit imq_cleanup_devs(void)
842+{
843+ rtnl_link_unregister(&imq_link_ops);
844+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
845+}
846+
847+static void __exit imq_exit_module(void)
848+{
849+ imq_unhook();
850+ imq_cleanup_devs();
851+ pr_info("IMQ driver unloaded successfully.\n");
852+}
853+
854+module_init(imq_init_module);
855+module_exit(imq_exit_module);
856+
857+module_param(numdevs, int, 0);
858+module_param(numqueues, int, 0);
859+MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will be created)");
860+MODULE_PARM_DESC(numqueues, "number of queues per IMQ device");
861+MODULE_AUTHOR("http://www.linuximq.net");
862+MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
863+MODULE_LICENSE("GPL");
864+MODULE_ALIAS_RTNL_LINK("imq");
865+
866diff -uNr linux-3.9.1/drivers/net/Kconfig linux-3.9.1-imqmq/drivers/net/Kconfig
867--- linux-3.9.1/drivers/net/Kconfig 2013-05-08 06:58:03.000000000 +0300
868+++ linux-3.9.1-imqmq/drivers/net/Kconfig 2013-05-08 17:30:29.011952562 +0300
869@@ -206,6 +206,125 @@
870 depends on RIONET
871 default "128"
872
873+config IMQ
874+ tristate "IMQ (intermediate queueing device) support"
875+ depends on NETDEVICES && NETFILTER
876+ ---help---
877+ The IMQ device(s) is used as placeholder for QoS queueing
878+ disciplines. Every packet entering/leaving the IP stack can be
879+ directed through the IMQ device where it's enqueued/dequeued to the
880+ attached qdisc. This allows you to treat network devices as classes
881+ and distribute bandwidth among them. Iptables is used to specify
882+ through which IMQ device, if any, packets travel.
883+
884+ More information at: http://www.linuximq.net/
885+
886+ To compile this driver as a module, choose M here: the module
887+ will be called imq. If unsure, say N.
888+
889+choice
890+ prompt "IMQ behavior (PRE/POSTROUTING)"
891+ depends on IMQ
892+ default IMQ_BEHAVIOR_AB
893+ help
894+ This setting defines how IMQ behaves in respect to its
895+ hooking in PREROUTING and POSTROUTING.
896+
897+ IMQ can work in any of the following ways:
898+
899+ PREROUTING | POSTROUTING
900+ -----------------|-------------------
901+ #1 After NAT | After NAT
902+ #2 After NAT | Before NAT
903+ #3 Before NAT | After NAT
904+ #4 Before NAT | Before NAT
905+
906+ The default behavior is to hook before NAT on PREROUTING
907+ and after NAT on POSTROUTING (#3).
908+
909+ This settings are specially usefull when trying to use IMQ
910+ to shape NATed clients.
911+
912+ More information can be found at: www.linuximq.net
913+
914+ If not sure leave the default settings alone.
915+
916+config IMQ_BEHAVIOR_AA
917+ bool "IMQ AA"
918+ help
919+ This setting defines how IMQ behaves in respect to its
920+ hooking in PREROUTING and POSTROUTING.
921+
922+ Choosing this option will make IMQ hook like this:
923+
924+ PREROUTING: After NAT
925+ POSTROUTING: After NAT
926+
927+ More information can be found at: www.linuximq.net
928+
929+ If not sure leave the default settings alone.
930+
931+config IMQ_BEHAVIOR_AB
932+ bool "IMQ AB"
933+ help
934+ This setting defines how IMQ behaves in respect to its
935+ hooking in PREROUTING and POSTROUTING.
936+
937+ Choosing this option will make IMQ hook like this:
938+
939+ PREROUTING: After NAT
940+ POSTROUTING: Before NAT
941+
942+ More information can be found at: www.linuximq.net
943+
944+ If not sure leave the default settings alone.
945+
946+config IMQ_BEHAVIOR_BA
947+ bool "IMQ BA"
948+ help
949+ This setting defines how IMQ behaves in respect to its
950+ hooking in PREROUTING and POSTROUTING.
951+
952+ Choosing this option will make IMQ hook like this:
953+
954+ PREROUTING: Before NAT
955+ POSTROUTING: After NAT
956+
957+ More information can be found at: www.linuximq.net
958+
959+ If not sure leave the default settings alone.
960+
961+config IMQ_BEHAVIOR_BB
962+ bool "IMQ BB"
963+ help
964+ This setting defines how IMQ behaves in respect to its
965+ hooking in PREROUTING and POSTROUTING.
966+
967+ Choosing this option will make IMQ hook like this:
968+
969+ PREROUTING: Before NAT
970+ POSTROUTING: Before NAT
971+
972+ More information can be found at: www.linuximq.net
973+
974+ If not sure leave the default settings alone.
975+
976+endchoice
977+
978+config IMQ_NUM_DEVS
979+ int "Number of IMQ devices"
980+ range 2 16
981+ depends on IMQ
982+ default "16"
983+ help
984+ This setting defines how many IMQ devices will be created.
985+
986+ The default value is 16.
987+
988+ More information can be found at: www.linuximq.net
989+
990+ If not sure leave the default settings alone.
991+
992 config TUN
993 tristate "Universal TUN/TAP device driver support"
994 select CRC32
995diff -uNr linux-3.9.1/drivers/net/Makefile linux-3.9.1-imqmq/drivers/net/Makefile
996--- linux-3.9.1/drivers/net/Makefile 2013-05-08 06:58:03.000000000 +0300
997+++ linux-3.9.1-imqmq/drivers/net/Makefile 2013-05-08 17:30:29.011952562 +0300
998@@ -9,6 +9,7 @@
999 obj-$(CONFIG_DUMMY) += dummy.o
1000 obj-$(CONFIG_EQUALIZER) += eql.o
1001 obj-$(CONFIG_IFB) += ifb.o
1002+obj-$(CONFIG_IMQ) += imq.o
1003 obj-$(CONFIG_MACVLAN) += macvlan.o
1004 obj-$(CONFIG_MACVTAP) += macvtap.o
1005 obj-$(CONFIG_MII) += mii.o
1006diff -uNr linux-3.9.1/include/linux/imq.h linux-3.9.1-imqmq/include/linux/imq.h
1007--- linux-3.9.1/include/linux/imq.h 1970-01-01 02:00:00.000000000 +0200
1008+++ linux-3.9.1-imqmq/include/linux/imq.h 2013-05-08 17:30:29.011952562 +0300
1009@@ -0,0 +1,13 @@
1010+#ifndef _IMQ_H
1011+#define _IMQ_H
1012+
1013+/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
1014+#define IMQ_F_BITS 5
1015+
1016+#define IMQ_F_IFMASK 0x0f
1017+#define IMQ_F_ENQUEUE 0x10
1018+
1019+#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1)
1020+
1021+#endif /* _IMQ_H */
1022+
1023diff -uNr linux-3.9.1/include/linux/netfilter/xt_IMQ.h linux-3.9.1-imqmq/include/linux/netfilter/xt_IMQ.h
1024--- linux-3.9.1/include/linux/netfilter/xt_IMQ.h 1970-01-01 02:00:00.000000000 +0200
1025+++ linux-3.9.1-imqmq/include/linux/netfilter/xt_IMQ.h 2013-05-08 17:30:29.011952562 +0300
1026@@ -0,0 +1,9 @@
1027+#ifndef _XT_IMQ_H
1028+#define _XT_IMQ_H
1029+
1030+struct xt_imq_info {
1031+ unsigned int todev; /* target imq device */
1032+};
1033+
1034+#endif /* _XT_IMQ_H */
1035+
1036diff -uNr linux-3.9.1/include/linux/netfilter_ipv4/ipt_IMQ.h linux-3.9.1-imqmq/include/linux/netfilter_ipv4/ipt_IMQ.h
1037--- linux-3.9.1/include/linux/netfilter_ipv4/ipt_IMQ.h 1970-01-01 02:00:00.000000000 +0200
1038+++ linux-3.9.1-imqmq/include/linux/netfilter_ipv4/ipt_IMQ.h 2013-05-08 17:30:29.011952562 +0300
1039@@ -0,0 +1,10 @@
1040+#ifndef _IPT_IMQ_H
1041+#define _IPT_IMQ_H
1042+
1043+/* Backwards compatibility for old userspace */
1044+#include <linux/netfilter/xt_IMQ.h>
1045+
1046+#define ipt_imq_info xt_imq_info
1047+
1048+#endif /* _IPT_IMQ_H */
1049+
1050diff -uNr linux-3.9.1/include/linux/netfilter_ipv6/ip6t_IMQ.h linux-3.9.1-imqmq/include/linux/netfilter_ipv6/ip6t_IMQ.h
1051--- linux-3.9.1/include/linux/netfilter_ipv6/ip6t_IMQ.h 1970-01-01 02:00:00.000000000 +0200
1052+++ linux-3.9.1-imqmq/include/linux/netfilter_ipv6/ip6t_IMQ.h 2013-05-08 17:30:29.011952562 +0300
1053@@ -0,0 +1,10 @@
1054+#ifndef _IP6T_IMQ_H
1055+#define _IP6T_IMQ_H
1056+
1057+/* Backwards compatibility for old userspace */
1058+#include <linux/netfilter/xt_IMQ.h>
1059+
1060+#define ip6t_imq_info xt_imq_info
1061+
1062+#endif /* _IP6T_IMQ_H */
1063+
bb5f0bf8
AF
1064diff -uNr linux-3.9.1/include/net/netfilter/nf_queue.h linux-3.9.1-imqmq/include/net/netfilter/nf_queue.h
1065--- linux-3.9.1/include/net/netfilter/nf_queue.h 2013-05-08 06:58:03.000000000 +0300
1066+++ linux-3.9.1-imqmq/include/net/netfilter/nf_queue.h 2013-05-08 17:30:29.015285965 +0300
1067@@ -26,5 +26,11 @@
1068 void nf_register_queue_handler(const struct nf_queue_handler *qh);
1069 void nf_unregister_queue_handler(void);
1070 extern void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
1071+extern void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
1072+
1073+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1074+extern void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
1075+extern void nf_unregister_queue_imq_handler(void);
1076+#endif
1077
1078 #endif /* _NF_QUEUE_H */
1079diff -uNr linux-3.9.1/include/uapi/linux/netfilter.h linux-3.9.1-imqmq/include/uapi/linux/netfilter.h
1080--- linux-3.9.1/include/uapi/linux/netfilter.h 2013-05-08 06:58:03.000000000 +0300
1081+++ linux-3.9.1-imqmq/include/uapi/linux/netfilter.h 2013-05-08 17:30:29.015285965 +0300
1082@@ -13,7 +13,8 @@
1083 #define NF_QUEUE 3
1084 #define NF_REPEAT 4
1085 #define NF_STOP 5
1086-#define NF_MAX_VERDICT NF_STOP
1087+#define NF_IMQ_QUEUE 6
1088+#define NF_MAX_VERDICT NF_IMQ_QUEUE
1089
1090 /* we overload the higher bits for encoding auxiliary data such as the queue
1091 * number or errno values. Not nice, but better than additional function
1092diff -uNr linux-3.9.1/net/core/dev.c linux-3.9.1-imqmq/net/core/dev.c
1093--- linux-3.9.1/net/core/dev.c 2013-05-08 06:58:03.000000000 +0300
1094+++ linux-3.9.1-imqmq/net/core/dev.c 2013-05-08 17:30:29.018619368 +0300
1095@@ -129,6 +129,9 @@
1096 #include <linux/inetdevice.h>
1097 #include <linux/cpu_rmap.h>
1098 #include <linux/static_key.h>
1099+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1100+#include <linux/imq.h>
1101+#endif
1102
1103 #include "net-sysfs.h"
1104
1105@@ -2529,7 +2532,12 @@
1106 }
1107 }
1108
1109+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1110+ if (!list_empty(&ptype_all) &&
1111+ !(skb->imq_flags & IMQ_F_ENQUEUE))
1112+#else
1113 if (!list_empty(&ptype_all))
1114+#endif
1115 dev_queue_xmit_nit(skb, dev);
1116
1117 skb_len = skb->len;
1118diff -uNr linux-3.9.1/net/core/skbuff.c linux-3.9.1-imqmq/net/core/skbuff.c
1119--- linux-3.9.1/net/core/skbuff.c 2013-05-08 06:58:03.000000000 +0300
1120+++ linux-3.9.1-imqmq/net/core/skbuff.c 2013-05-08 17:30:29.021952772 +0300
1121@@ -73,6 +73,9 @@
1122
1123 struct kmem_cache *skbuff_head_cache __read_mostly;
1124 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
1125+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1126+static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
1127+#endif
1128
1129 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
1130 struct pipe_buffer *buf)
1131@@ -92,6 +95,82 @@
1132 return 1;
1133 }
1134
1135+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1136+/* Control buffer save/restore for IMQ devices */
1137+struct skb_cb_table {
1138+ char cb[48] __aligned(8);
1139+ void *cb_next;
1140+ atomic_t refcnt;
1141+};
1142+
1143+static DEFINE_SPINLOCK(skb_cb_store_lock);
1144+
1145+int skb_save_cb(struct sk_buff *skb)
1146+{
1147+ struct skb_cb_table *next;
1148+
1149+ next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
1150+ if (!next)
1151+ return -ENOMEM;
1152+
1153+ BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1154+
1155+ memcpy(next->cb, skb->cb, sizeof(skb->cb));
1156+ next->cb_next = skb->cb_next;
1157+
1158+ atomic_set(&next->refcnt, 1);
1159+
1160+ skb->cb_next = next;
1161+ return 0;
1162+}
1163+EXPORT_SYMBOL(skb_save_cb);
1164+
1165+int skb_restore_cb(struct sk_buff *skb)
1166+{
1167+ struct skb_cb_table *next;
1168+
1169+ if (!skb->cb_next)
1170+ return 0;
1171+
1172+ next = skb->cb_next;
1173+
1174+ BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1175+
1176+ memcpy(skb->cb, next->cb, sizeof(skb->cb));
1177+ skb->cb_next = next->cb_next;
1178+
1179+ spin_lock(&skb_cb_store_lock);
1180+
1181+ if (atomic_dec_and_test(&next->refcnt))
1182+ kmem_cache_free(skbuff_cb_store_cache, next);
1183+
1184+ spin_unlock(&skb_cb_store_lock);
1185+
1186+ return 0;
1187+}
1188+EXPORT_SYMBOL(skb_restore_cb);
1189+
1190+static void skb_copy_stored_cb(struct sk_buff *new, const struct sk_buff *__old)
1191+{
1192+ struct skb_cb_table *next;
1193+ struct sk_buff *old;
1194+
1195+ if (!__old->cb_next) {
1196+ new->cb_next = NULL;
1197+ return;
1198+ }
1199+
1200+ spin_lock(&skb_cb_store_lock);
1201+
1202+ old = (struct sk_buff *)__old;
1203+
1204+ next = old->cb_next;
1205+ atomic_inc(&next->refcnt);
1206+ new->cb_next = next;
1207+
1208+ spin_unlock(&skb_cb_store_lock);
1209+}
1210+#endif
1211
1212 /* Pipe buffer operations for a socket. */
1213 static const struct pipe_buf_operations sock_pipe_buf_ops = {
1214@@ -562,6 +641,28 @@
1215 WARN_ON(in_irq());
1216 skb->destructor(skb);
1217 }
1218+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1219+ /*
1220+ * This should not happen. When it does, avoid memleak by restoring
1221+ * the chain of cb-backups.
1222+ */
1223+ while (skb->cb_next != NULL) {
1224+ if (net_ratelimit())
1225+ pr_warn("IMQ: kfree_skb: skb->cb_next: %08x\n",
1226+ (unsigned int)skb->cb_next);
1227+
1228+ skb_restore_cb(skb);
1229+ }
1230+ /*
1231+ * This should not happen either, nf_queue_entry is nullified in
1232+ * imq_dev_xmit(). If we have non-NULL nf_queue_entry then we are
1233+ * leaking entry pointers, maybe memory. We don't know if this is
1234+ * pointer to already freed memory, or should this be freed.
1235+ * If this happens we need to add refcounting, etc for nf_queue_entry.
1236+ */
1237+ if (skb->nf_queue_entry && net_ratelimit())
1238+ pr_warn("%s\n", "IMQ: kfree_skb: skb->nf_queue_entry != NULL");
1239+#endif
1240 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1241 nf_conntrack_put(skb->nfct);
1242 #endif
1243@@ -683,6 +784,9 @@
1244 new->sp = secpath_get(old->sp);
1245 #endif
1246 memcpy(new->cb, old->cb, sizeof(old->cb));
1247+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1248+ skb_copy_stored_cb(new, old);
1249+#endif
1250 new->csum = old->csum;
1251 new->local_df = old->local_df;
1252 new->pkt_type = old->pkt_type;
1253@@ -3053,6 +3157,13 @@
1254 0,
1255 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1256 NULL);
1257+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1258+ skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1259+ sizeof(struct skb_cb_table),
1260+ 0,
1261+ SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1262+ NULL);
1263+#endif
1264 }
1265
1266 /**
1267diff -uNr linux-3.9.1/net/ipv6/ip6_output.c linux-3.9.1-imqmq/net/ipv6/ip6_output.c
1268--- linux-3.9.1/net/ipv6/ip6_output.c 2013-05-08 06:58:03.000000000 +0300
1269+++ linux-3.9.1-imqmq/net/ipv6/ip6_output.c 2013-05-08 17:30:29.021952772 +0300
1270@@ -89,9 +89,6 @@
1271 struct in6_addr *nexthop;
1272 int ret;
1273
1274- skb->protocol = htons(ETH_P_IPV6);
1275- skb->dev = dev;
1276-
1277 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
1278 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
1279
1280@@ -167,6 +164,13 @@
1281 return 0;
1282 }
1283
1284+ /*
1285+ * IMQ-patch: moved setting skb->dev and skb->protocol from
1286+ * ip6_finish_output2 to fix crashing at netif_skb_features().
1287+ */
1288+ skb->protocol = htons(ETH_P_IPV6);
1289+ skb->dev = dev;
1290+
1291 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
1292 ip6_finish_output,
1293 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
1294diff -uNr linux-3.9.1/net/netfilter/core.c linux-3.9.1-imqmq/net/netfilter/core.c
1295--- linux-3.9.1/net/netfilter/core.c 2013-05-08 06:58:03.000000000 +0300
1296+++ linux-3.9.1-imqmq/net/netfilter/core.c 2013-05-08 17:30:29.025286174 +0300
1297@@ -188,9 +188,11 @@
1298 ret = NF_DROP_GETERR(verdict);
1299 if (ret == 0)
1300 ret = -EPERM;
1301- } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
1302+ } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE ||
1303+ (verdict & NF_VERDICT_MASK) == NF_IMQ_QUEUE) {
1304 int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
1305- verdict >> NF_VERDICT_QBITS);
1306+ verdict >> NF_VERDICT_QBITS,
1307+ verdict & NF_VERDICT_MASK);
1308 if (err < 0) {
1309 if (err == -ECANCELED)
1310 goto next_hook;
1311diff -uNr linux-3.9.1/net/netfilter/Kconfig linux-3.9.1-imqmq/net/netfilter/Kconfig
1312--- linux-3.9.1/net/netfilter/Kconfig 2013-05-08 06:58:03.000000000 +0300
1313+++ linux-3.9.1-imqmq/net/netfilter/Kconfig 2013-05-08 17:30:29.025286174 +0300
1314@@ -641,6 +641,18 @@
1315
1316 To compile it as a module, choose M here. If unsure, say N.
1317
1318+config NETFILTER_XT_TARGET_IMQ
1319+ tristate '"IMQ" target support'
1320+ depends on NETFILTER_XTABLES
1321+ depends on IP_NF_MANGLE || IP6_NF_MANGLE
1322+ select IMQ
1323+ default m if NETFILTER_ADVANCED=n
1324+ help
1325+ This option adds a `IMQ' target which is used to specify if and
1326+ to which imq device packets should get enqueued/dequeued.
1327+
1328+ To compile it as a module, choose M here. If unsure, say N.
1329+
1330 config NETFILTER_XT_TARGET_MARK
1331 tristate '"MARK" target support'
1332 depends on NETFILTER_ADVANCED
1333diff -uNr linux-3.9.1/net/netfilter/Makefile linux-3.9.1-imqmq/net/netfilter/Makefile
1334--- linux-3.9.1/net/netfilter/Makefile 2013-05-08 06:58:03.000000000 +0300
1335+++ linux-3.9.1-imqmq/net/netfilter/Makefile 2013-05-08 17:30:29.025286174 +0300
1336@@ -82,6 +82,7 @@
1337 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
1338 obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
1339 obj-$(CONFIG_NETFILTER_XT_TARGET_HMARK) += xt_HMARK.o
1340+obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
1341 obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
1342 obj-$(CONFIG_NETFILTER_XT_TARGET_LOG) += xt_LOG.o
1343 obj-$(CONFIG_NETFILTER_XT_TARGET_NETMAP) += xt_NETMAP.o
1344diff -uNr linux-3.9.1/net/netfilter/nf_internals.h linux-3.9.1-imqmq/net/netfilter/nf_internals.h
1345--- linux-3.9.1/net/netfilter/nf_internals.h 2013-05-08 06:58:03.000000000 +0300
1346+++ linux-3.9.1-imqmq/net/netfilter/nf_internals.h 2013-05-08 17:30:29.025286174 +0300
1347@@ -29,7 +29,7 @@
1348 struct net_device *indev,
1349 struct net_device *outdev,
1350 int (*okfn)(struct sk_buff *),
1351- unsigned int queuenum);
1352+ unsigned int queuenum, unsigned int queuetype);
1353 extern int __init netfilter_queue_init(void);
1354
1355 /* nf_log.c */
1356diff -uNr linux-3.9.1/net/netfilter/nf_queue.c linux-3.9.1-imqmq/net/netfilter/nf_queue.c
1357--- linux-3.9.1/net/netfilter/nf_queue.c 2013-05-08 06:58:03.000000000 +0300
1358+++ linux-3.9.1-imqmq/net/netfilter/nf_queue.c 2013-05-08 17:30:29.025286174 +0300
1359@@ -22,6 +22,23 @@
1360 */
1361 static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
1362
1363+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1364+static const struct nf_queue_handler __rcu *queue_imq_handler __read_mostly;
1365+
1366+void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
1367+{
1368+ rcu_assign_pointer(queue_imq_handler, qh);
1369+}
1370+EXPORT_SYMBOL_GPL(nf_register_queue_imq_handler);
1371+
1372+void nf_unregister_queue_imq_handler(void)
1373+{
1374+ RCU_INIT_POINTER(queue_imq_handler, NULL);
1375+ synchronize_rcu();
1376+}
1377+EXPORT_SYMBOL_GPL(nf_unregister_queue_imq_handler);
1378+#endif
1379+
1380 /* return EBUSY when somebody else is registered, return EEXIST if the
1381 * same handler is registered, return 0 in case of success. */
1382 void nf_register_queue_handler(const struct nf_queue_handler *qh)
1383@@ -71,7 +89,8 @@
1384 struct net_device *indev,
1385 struct net_device *outdev,
1386 int (*okfn)(struct sk_buff *),
1387- unsigned int queuenum)
1388+ unsigned int queuenum,
1389+ unsigned int queuetype)
1390 {
1391 int status = -ENOENT;
1392 struct nf_queue_entry *entry = NULL;
1393@@ -85,7 +104,17 @@
1394 /* QUEUE == DROP if no one is waiting, to be safe. */
1395 rcu_read_lock();
1396
1397- qh = rcu_dereference(queue_handler);
1398+ if (queuetype == NF_IMQ_QUEUE) {
1399+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1400+ qh = rcu_dereference(queue_imq_handler);
1401+#else
1402+ BUG();
1403+ goto err_unlock;
1404+#endif
1405+ } else {
1406+ qh = rcu_dereference(queue_handler);
1407+ }
1408+
1409 if (!qh) {
1410 status = -ESRCH;
1411 goto err_unlock;
1412@@ -233,9 +261,11 @@
1413 local_bh_enable();
1414 break;
1415 case NF_QUEUE:
1416+ case NF_IMQ_QUEUE:
1417 err = nf_queue(skb, elem, entry->pf, entry->hook,
1418 entry->indev, entry->outdev, entry->okfn,
1419- verdict >> NF_VERDICT_QBITS);
1420+ verdict >> NF_VERDICT_QBITS,
1421+ verdict & NF_VERDICT_MASK);
1422 if (err < 0) {
1423 if (err == -ECANCELED)
1424 goto next_hook;
1425diff -uNr linux-3.9.1/net/netfilter/xt_IMQ.c linux-3.9.1-imqmq/net/netfilter/xt_IMQ.c
1426--- linux-3.9.1/net/netfilter/xt_IMQ.c 1970-01-01 02:00:00.000000000 +0200
1427+++ linux-3.9.1-imqmq/net/netfilter/xt_IMQ.c 2013-05-08 17:30:29.025286174 +0300
1428@@ -0,0 +1,72 @@
1429+/*
1430+ * This target marks packets to be enqueued to an imq device
1431+ */
1432+#include <linux/module.h>
1433+#include <linux/skbuff.h>
1434+#include <linux/netfilter/x_tables.h>
1435+#include <linux/netfilter/xt_IMQ.h>
1436+#include <linux/imq.h>
1437+
1438+static unsigned int imq_target(struct sk_buff *pskb,
1439+ const struct xt_action_param *par)
1440+{
1441+ const struct xt_imq_info *mr = par->targinfo;
1442+
1443+ pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
1444+
1445+ return XT_CONTINUE;
1446+}
1447+
1448+static int imq_checkentry(const struct xt_tgchk_param *par)
1449+{
1450+ struct xt_imq_info *mr = par->targinfo;
1451+
1452+ if (mr->todev > IMQ_MAX_DEVS - 1) {
1453+ pr_warn("IMQ: invalid device specified, highest is %u\n",
1454+ IMQ_MAX_DEVS - 1);
1455+ return -EINVAL;
1456+ }
1457+
1458+ return 0;
1459+}
1460+
1461+static struct xt_target xt_imq_reg[] __read_mostly = {
1462+ {
1463+ .name = "IMQ",
1464+ .family = AF_INET,
1465+ .checkentry = imq_checkentry,
1466+ .target = imq_target,
1467+ .targetsize = sizeof(struct xt_imq_info),
1468+ .table = "mangle",
1469+ .me = THIS_MODULE
1470+ },
1471+ {
1472+ .name = "IMQ",
1473+ .family = AF_INET6,
1474+ .checkentry = imq_checkentry,
1475+ .target = imq_target,
1476+ .targetsize = sizeof(struct xt_imq_info),
1477+ .table = "mangle",
1478+ .me = THIS_MODULE
1479+ },
1480+};
1481+
1482+static int __init imq_init(void)
1483+{
1484+ return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1485+}
1486+
1487+static void __exit imq_fini(void)
1488+{
1489+ xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1490+}
1491+
1492+module_init(imq_init);
1493+module_exit(imq_fini);
1494+
1495+MODULE_AUTHOR("http://www.linuximq.net");
1496+MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
1497+MODULE_LICENSE("GPL");
1498+MODULE_ALIAS("ipt_IMQ");
1499+MODULE_ALIAS("ip6t_IMQ");
1500+
be33adfb
AF
1501diff -uNr linux-3.10.23/include/linux/skbuff.h linux-3.10.23-imqmq/include/linux/skbuff.h
1502--- linux-3.10.23/include/linux/skbuff.h 2013-12-08 17:17:21.000000000 +0100
1503+++ linux-3.10.23-imqmq/include/linux/skbuff.h 2013-12-09 15:49:26.329991968 +0100
1504@@ -33,6 +33,9 @@
1505 #include <linux/dma-mapping.h>
1506 #include <linux/netdev_features.h>
1507 #include <net/flow_keys.h>
1508+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1509+#include <linux/imq.h>
1510+#endif
1511
1512 /* Don't change this without changing skb_csum_unnecessary! */
1513 #define CHECKSUM_NONE 0
1514@@ -414,6 +417,9 @@
1515 * first. This is owned by whoever has the skb queued ATM.
1516 */
1517 char cb[48] __aligned(8);
1518+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1519+ void *cb_next;
1520+#endif
1521
1522 unsigned long _skb_refdst;
1523 #ifdef CONFIG_XFRM
1524@@ -449,6 +455,9 @@
1525 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1526 struct nf_conntrack *nfct;
1527 #endif
1528+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1529+ struct nf_queue_entry *nf_queue_entry;
1530+#endif
1531 #ifdef CONFIG_BRIDGE_NETFILTER
1532 struct nf_bridge_info *nf_bridge;
1533 #endif
1534@@ -488,6 +497,10 @@
1535 /* 7/9 bit hole (depending on ndisc_nodetype presence) */
1536 kmemcheck_bitfield_end(flags2);
1537
1538+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1539+ __u8 imq_flags:IMQ_F_BITS;
1540+#endif
1541+
1542 #ifdef CONFIG_NET_DMA
1543 dma_cookie_t dma_cookie;
1544 #endif
1545@@ -617,6 +630,12 @@
1546 return (struct rtable *)skb_dst(skb);
1547 }
1548
1549+
1550+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1551+extern int skb_save_cb(struct sk_buff *skb);
1552+extern int skb_restore_cb(struct sk_buff *skb);
1553+#endif
1554+
1555 extern void kfree_skb(struct sk_buff *skb);
1556 extern void kfree_skb_list(struct sk_buff *segs);
1557 extern void skb_tx_error(struct sk_buff *skb);
1558@@ -2730,6 +2749,10 @@
1559 nf_conntrack_get(src->nfct);
1560 dst->nfctinfo = src->nfctinfo;
1561 #endif
1562+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1563+ dst->imq_flags = src->imq_flags;
1564+ dst->nf_queue_entry = src->nf_queue_entry;
1565+#endif
1566 #ifdef CONFIG_BRIDGE_NETFILTER
1567 dst->nf_bridge = src->nf_bridge;
1568 nf_bridge_get(src->nf_bridge);