]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - src/patches/imq_kernel3.10.patch
mountkernfs: fix mount of /sys and /proc without initrd.
[people/teissler/ipfire-2.x.git] / src / patches / imq_kernel3.10.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+
1064diff -uNr linux-3.9.1/include/linux/skbuff.h linux-3.9.1-imqmq/include/linux/skbuff.h
1065--- linux-3.9.1/include/linux/skbuff.h 2013-05-08 06:58:03.000000000 +0300
1066+++ linux-3.9.1-imqmq/include/linux/skbuff.h 2013-05-08 17:30:29.015285965 +0300
1067@@ -32,6 +32,9 @@
1068 #include <linux/dma-mapping.h>
1069 #include <linux/netdev_features.h>
1070 #include <net/flow_keys.h>
1071+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1072+#include <linux/imq.h>
1073+#endif
1074
1075 /* Don't change this without changing skb_csum_unnecessary! */
1076 #define CHECKSUM_NONE 0
1077@@ -415,6 +418,9 @@
1078 * first. This is owned by whoever has the skb queued ATM.
1079 */
1080 char cb[48] __aligned(8);
1081+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1082+ void *cb_next;
1083+#endif
1084
1085 unsigned long _skb_refdst;
1086 #ifdef CONFIG_XFRM
1087@@ -453,6 +459,9 @@
1088 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
1089 struct sk_buff *nfct_reasm;
1090 #endif
1091+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1092+ struct nf_queue_entry *nf_queue_entry;
1093+#endif
1094 #ifdef CONFIG_BRIDGE_NETFILTER
1095 struct nf_bridge_info *nf_bridge;
1096 #endif
1097@@ -491,6 +500,10 @@
1098 /* 7/9 bit hole (depending on ndisc_nodetype presence) */
1099 kmemcheck_bitfield_end(flags2);
1100
1101+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1102+ __u8 imq_flags:IMQ_F_BITS;
1103+#endif
1104+
1105 #ifdef CONFIG_NET_DMA
1106 dma_cookie_t dma_cookie;
1107 #endif
1108@@ -586,6 +599,12 @@
1109 return (struct rtable *)skb_dst(skb);
1110 }
1111
1112+
1113+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1114+extern int skb_save_cb(struct sk_buff *skb);
1115+extern int skb_restore_cb(struct sk_buff *skb);
1116+#endif
1117+
1118 extern void kfree_skb(struct sk_buff *skb);
1119 extern void skb_tx_error(struct sk_buff *skb);
1120 extern void consume_skb(struct sk_buff *skb);
1121@@ -2662,6 +2681,10 @@
1122 dst->nfct_reasm = src->nfct_reasm;
1123 nf_conntrack_get_reasm(src->nfct_reasm);
1124 #endif
1125+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1126+ dst->imq_flags = src->imq_flags;
1127+ dst->nf_queue_entry = src->nf_queue_entry;
1128+#endif
1129 #ifdef CONFIG_BRIDGE_NETFILTER
1130 dst->nf_bridge = src->nf_bridge;
1131 nf_bridge_get(src->nf_bridge);
1132diff -uNr linux-3.9.1/include/net/netfilter/nf_queue.h linux-3.9.1-imqmq/include/net/netfilter/nf_queue.h
1133--- linux-3.9.1/include/net/netfilter/nf_queue.h 2013-05-08 06:58:03.000000000 +0300
1134+++ linux-3.9.1-imqmq/include/net/netfilter/nf_queue.h 2013-05-08 17:30:29.015285965 +0300
1135@@ -26,5 +26,11 @@
1136 void nf_register_queue_handler(const struct nf_queue_handler *qh);
1137 void nf_unregister_queue_handler(void);
1138 extern void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
1139+extern void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
1140+
1141+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1142+extern void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
1143+extern void nf_unregister_queue_imq_handler(void);
1144+#endif
1145
1146 #endif /* _NF_QUEUE_H */
1147diff -uNr linux-3.9.1/include/uapi/linux/netfilter.h linux-3.9.1-imqmq/include/uapi/linux/netfilter.h
1148--- linux-3.9.1/include/uapi/linux/netfilter.h 2013-05-08 06:58:03.000000000 +0300
1149+++ linux-3.9.1-imqmq/include/uapi/linux/netfilter.h 2013-05-08 17:30:29.015285965 +0300
1150@@ -13,7 +13,8 @@
1151 #define NF_QUEUE 3
1152 #define NF_REPEAT 4
1153 #define NF_STOP 5
1154-#define NF_MAX_VERDICT NF_STOP
1155+#define NF_IMQ_QUEUE 6
1156+#define NF_MAX_VERDICT NF_IMQ_QUEUE
1157
1158 /* we overload the higher bits for encoding auxiliary data such as the queue
1159 * number or errno values. Not nice, but better than additional function
1160diff -uNr linux-3.9.1/net/core/dev.c linux-3.9.1-imqmq/net/core/dev.c
1161--- linux-3.9.1/net/core/dev.c 2013-05-08 06:58:03.000000000 +0300
1162+++ linux-3.9.1-imqmq/net/core/dev.c 2013-05-08 17:30:29.018619368 +0300
1163@@ -129,6 +129,9 @@
1164 #include <linux/inetdevice.h>
1165 #include <linux/cpu_rmap.h>
1166 #include <linux/static_key.h>
1167+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1168+#include <linux/imq.h>
1169+#endif
1170
1171 #include "net-sysfs.h"
1172
1173@@ -2529,7 +2532,12 @@
1174 }
1175 }
1176
1177+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1178+ if (!list_empty(&ptype_all) &&
1179+ !(skb->imq_flags & IMQ_F_ENQUEUE))
1180+#else
1181 if (!list_empty(&ptype_all))
1182+#endif
1183 dev_queue_xmit_nit(skb, dev);
1184
1185 skb_len = skb->len;
1186diff -uNr linux-3.9.1/net/core/skbuff.c linux-3.9.1-imqmq/net/core/skbuff.c
1187--- linux-3.9.1/net/core/skbuff.c 2013-05-08 06:58:03.000000000 +0300
1188+++ linux-3.9.1-imqmq/net/core/skbuff.c 2013-05-08 17:30:29.021952772 +0300
1189@@ -73,6 +73,9 @@
1190
1191 struct kmem_cache *skbuff_head_cache __read_mostly;
1192 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
1193+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1194+static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
1195+#endif
1196
1197 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
1198 struct pipe_buffer *buf)
1199@@ -92,6 +95,82 @@
1200 return 1;
1201 }
1202
1203+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1204+/* Control buffer save/restore for IMQ devices */
1205+struct skb_cb_table {
1206+ char cb[48] __aligned(8);
1207+ void *cb_next;
1208+ atomic_t refcnt;
1209+};
1210+
1211+static DEFINE_SPINLOCK(skb_cb_store_lock);
1212+
1213+int skb_save_cb(struct sk_buff *skb)
1214+{
1215+ struct skb_cb_table *next;
1216+
1217+ next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
1218+ if (!next)
1219+ return -ENOMEM;
1220+
1221+ BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1222+
1223+ memcpy(next->cb, skb->cb, sizeof(skb->cb));
1224+ next->cb_next = skb->cb_next;
1225+
1226+ atomic_set(&next->refcnt, 1);
1227+
1228+ skb->cb_next = next;
1229+ return 0;
1230+}
1231+EXPORT_SYMBOL(skb_save_cb);
1232+
1233+int skb_restore_cb(struct sk_buff *skb)
1234+{
1235+ struct skb_cb_table *next;
1236+
1237+ if (!skb->cb_next)
1238+ return 0;
1239+
1240+ next = skb->cb_next;
1241+
1242+ BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1243+
1244+ memcpy(skb->cb, next->cb, sizeof(skb->cb));
1245+ skb->cb_next = next->cb_next;
1246+
1247+ spin_lock(&skb_cb_store_lock);
1248+
1249+ if (atomic_dec_and_test(&next->refcnt))
1250+ kmem_cache_free(skbuff_cb_store_cache, next);
1251+
1252+ spin_unlock(&skb_cb_store_lock);
1253+
1254+ return 0;
1255+}
1256+EXPORT_SYMBOL(skb_restore_cb);
1257+
1258+static void skb_copy_stored_cb(struct sk_buff *new, const struct sk_buff *__old)
1259+{
1260+ struct skb_cb_table *next;
1261+ struct sk_buff *old;
1262+
1263+ if (!__old->cb_next) {
1264+ new->cb_next = NULL;
1265+ return;
1266+ }
1267+
1268+ spin_lock(&skb_cb_store_lock);
1269+
1270+ old = (struct sk_buff *)__old;
1271+
1272+ next = old->cb_next;
1273+ atomic_inc(&next->refcnt);
1274+ new->cb_next = next;
1275+
1276+ spin_unlock(&skb_cb_store_lock);
1277+}
1278+#endif
1279
1280 /* Pipe buffer operations for a socket. */
1281 static const struct pipe_buf_operations sock_pipe_buf_ops = {
1282@@ -562,6 +641,28 @@
1283 WARN_ON(in_irq());
1284 skb->destructor(skb);
1285 }
1286+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1287+ /*
1288+ * This should not happen. When it does, avoid memleak by restoring
1289+ * the chain of cb-backups.
1290+ */
1291+ while (skb->cb_next != NULL) {
1292+ if (net_ratelimit())
1293+ pr_warn("IMQ: kfree_skb: skb->cb_next: %08x\n",
1294+ (unsigned int)skb->cb_next);
1295+
1296+ skb_restore_cb(skb);
1297+ }
1298+ /*
1299+ * This should not happen either, nf_queue_entry is nullified in
1300+ * imq_dev_xmit(). If we have non-NULL nf_queue_entry then we are
1301+ * leaking entry pointers, maybe memory. We don't know if this is
1302+ * pointer to already freed memory, or should this be freed.
1303+ * If this happens we need to add refcounting, etc for nf_queue_entry.
1304+ */
1305+ if (skb->nf_queue_entry && net_ratelimit())
1306+ pr_warn("%s\n", "IMQ: kfree_skb: skb->nf_queue_entry != NULL");
1307+#endif
1308 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1309 nf_conntrack_put(skb->nfct);
1310 #endif
1311@@ -683,6 +784,9 @@
1312 new->sp = secpath_get(old->sp);
1313 #endif
1314 memcpy(new->cb, old->cb, sizeof(old->cb));
1315+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1316+ skb_copy_stored_cb(new, old);
1317+#endif
1318 new->csum = old->csum;
1319 new->local_df = old->local_df;
1320 new->pkt_type = old->pkt_type;
1321@@ -3053,6 +3157,13 @@
1322 0,
1323 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1324 NULL);
1325+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1326+ skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1327+ sizeof(struct skb_cb_table),
1328+ 0,
1329+ SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1330+ NULL);
1331+#endif
1332 }
1333
1334 /**
1335diff -uNr linux-3.9.1/net/ipv6/ip6_output.c linux-3.9.1-imqmq/net/ipv6/ip6_output.c
1336--- linux-3.9.1/net/ipv6/ip6_output.c 2013-05-08 06:58:03.000000000 +0300
1337+++ linux-3.9.1-imqmq/net/ipv6/ip6_output.c 2013-05-08 17:30:29.021952772 +0300
1338@@ -89,9 +89,6 @@
1339 struct in6_addr *nexthop;
1340 int ret;
1341
1342- skb->protocol = htons(ETH_P_IPV6);
1343- skb->dev = dev;
1344-
1345 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
1346 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
1347
1348@@ -167,6 +164,13 @@
1349 return 0;
1350 }
1351
1352+ /*
1353+ * IMQ-patch: moved setting skb->dev and skb->protocol from
1354+ * ip6_finish_output2 to fix crashing at netif_skb_features().
1355+ */
1356+ skb->protocol = htons(ETH_P_IPV6);
1357+ skb->dev = dev;
1358+
1359 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
1360 ip6_finish_output,
1361 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
1362diff -uNr linux-3.9.1/net/netfilter/core.c linux-3.9.1-imqmq/net/netfilter/core.c
1363--- linux-3.9.1/net/netfilter/core.c 2013-05-08 06:58:03.000000000 +0300
1364+++ linux-3.9.1-imqmq/net/netfilter/core.c 2013-05-08 17:30:29.025286174 +0300
1365@@ -188,9 +188,11 @@
1366 ret = NF_DROP_GETERR(verdict);
1367 if (ret == 0)
1368 ret = -EPERM;
1369- } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
1370+ } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE ||
1371+ (verdict & NF_VERDICT_MASK) == NF_IMQ_QUEUE) {
1372 int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
1373- verdict >> NF_VERDICT_QBITS);
1374+ verdict >> NF_VERDICT_QBITS,
1375+ verdict & NF_VERDICT_MASK);
1376 if (err < 0) {
1377 if (err == -ECANCELED)
1378 goto next_hook;
1379diff -uNr linux-3.9.1/net/netfilter/Kconfig linux-3.9.1-imqmq/net/netfilter/Kconfig
1380--- linux-3.9.1/net/netfilter/Kconfig 2013-05-08 06:58:03.000000000 +0300
1381+++ linux-3.9.1-imqmq/net/netfilter/Kconfig 2013-05-08 17:30:29.025286174 +0300
1382@@ -641,6 +641,18 @@
1383
1384 To compile it as a module, choose M here. If unsure, say N.
1385
1386+config NETFILTER_XT_TARGET_IMQ
1387+ tristate '"IMQ" target support'
1388+ depends on NETFILTER_XTABLES
1389+ depends on IP_NF_MANGLE || IP6_NF_MANGLE
1390+ select IMQ
1391+ default m if NETFILTER_ADVANCED=n
1392+ help
1393+ This option adds a `IMQ' target which is used to specify if and
1394+ to which imq device packets should get enqueued/dequeued.
1395+
1396+ To compile it as a module, choose M here. If unsure, say N.
1397+
1398 config NETFILTER_XT_TARGET_MARK
1399 tristate '"MARK" target support'
1400 depends on NETFILTER_ADVANCED
1401diff -uNr linux-3.9.1/net/netfilter/Makefile linux-3.9.1-imqmq/net/netfilter/Makefile
1402--- linux-3.9.1/net/netfilter/Makefile 2013-05-08 06:58:03.000000000 +0300
1403+++ linux-3.9.1-imqmq/net/netfilter/Makefile 2013-05-08 17:30:29.025286174 +0300
1404@@ -82,6 +82,7 @@
1405 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
1406 obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
1407 obj-$(CONFIG_NETFILTER_XT_TARGET_HMARK) += xt_HMARK.o
1408+obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
1409 obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
1410 obj-$(CONFIG_NETFILTER_XT_TARGET_LOG) += xt_LOG.o
1411 obj-$(CONFIG_NETFILTER_XT_TARGET_NETMAP) += xt_NETMAP.o
1412diff -uNr linux-3.9.1/net/netfilter/nf_internals.h linux-3.9.1-imqmq/net/netfilter/nf_internals.h
1413--- linux-3.9.1/net/netfilter/nf_internals.h 2013-05-08 06:58:03.000000000 +0300
1414+++ linux-3.9.1-imqmq/net/netfilter/nf_internals.h 2013-05-08 17:30:29.025286174 +0300
1415@@ -29,7 +29,7 @@
1416 struct net_device *indev,
1417 struct net_device *outdev,
1418 int (*okfn)(struct sk_buff *),
1419- unsigned int queuenum);
1420+ unsigned int queuenum, unsigned int queuetype);
1421 extern int __init netfilter_queue_init(void);
1422
1423 /* nf_log.c */
1424diff -uNr linux-3.9.1/net/netfilter/nf_queue.c linux-3.9.1-imqmq/net/netfilter/nf_queue.c
1425--- linux-3.9.1/net/netfilter/nf_queue.c 2013-05-08 06:58:03.000000000 +0300
1426+++ linux-3.9.1-imqmq/net/netfilter/nf_queue.c 2013-05-08 17:30:29.025286174 +0300
1427@@ -22,6 +22,23 @@
1428 */
1429 static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
1430
1431+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1432+static const struct nf_queue_handler __rcu *queue_imq_handler __read_mostly;
1433+
1434+void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
1435+{
1436+ rcu_assign_pointer(queue_imq_handler, qh);
1437+}
1438+EXPORT_SYMBOL_GPL(nf_register_queue_imq_handler);
1439+
1440+void nf_unregister_queue_imq_handler(void)
1441+{
1442+ RCU_INIT_POINTER(queue_imq_handler, NULL);
1443+ synchronize_rcu();
1444+}
1445+EXPORT_SYMBOL_GPL(nf_unregister_queue_imq_handler);
1446+#endif
1447+
1448 /* return EBUSY when somebody else is registered, return EEXIST if the
1449 * same handler is registered, return 0 in case of success. */
1450 void nf_register_queue_handler(const struct nf_queue_handler *qh)
1451@@ -71,7 +89,8 @@
1452 struct net_device *indev,
1453 struct net_device *outdev,
1454 int (*okfn)(struct sk_buff *),
1455- unsigned int queuenum)
1456+ unsigned int queuenum,
1457+ unsigned int queuetype)
1458 {
1459 int status = -ENOENT;
1460 struct nf_queue_entry *entry = NULL;
1461@@ -85,7 +104,17 @@
1462 /* QUEUE == DROP if no one is waiting, to be safe. */
1463 rcu_read_lock();
1464
1465- qh = rcu_dereference(queue_handler);
1466+ if (queuetype == NF_IMQ_QUEUE) {
1467+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1468+ qh = rcu_dereference(queue_imq_handler);
1469+#else
1470+ BUG();
1471+ goto err_unlock;
1472+#endif
1473+ } else {
1474+ qh = rcu_dereference(queue_handler);
1475+ }
1476+
1477 if (!qh) {
1478 status = -ESRCH;
1479 goto err_unlock;
1480@@ -233,9 +261,11 @@
1481 local_bh_enable();
1482 break;
1483 case NF_QUEUE:
1484+ case NF_IMQ_QUEUE:
1485 err = nf_queue(skb, elem, entry->pf, entry->hook,
1486 entry->indev, entry->outdev, entry->okfn,
1487- verdict >> NF_VERDICT_QBITS);
1488+ verdict >> NF_VERDICT_QBITS,
1489+ verdict & NF_VERDICT_MASK);
1490 if (err < 0) {
1491 if (err == -ECANCELED)
1492 goto next_hook;
1493diff -uNr linux-3.9.1/net/netfilter/xt_IMQ.c linux-3.9.1-imqmq/net/netfilter/xt_IMQ.c
1494--- linux-3.9.1/net/netfilter/xt_IMQ.c 1970-01-01 02:00:00.000000000 +0200
1495+++ linux-3.9.1-imqmq/net/netfilter/xt_IMQ.c 2013-05-08 17:30:29.025286174 +0300
1496@@ -0,0 +1,72 @@
1497+/*
1498+ * This target marks packets to be enqueued to an imq device
1499+ */
1500+#include <linux/module.h>
1501+#include <linux/skbuff.h>
1502+#include <linux/netfilter/x_tables.h>
1503+#include <linux/netfilter/xt_IMQ.h>
1504+#include <linux/imq.h>
1505+
1506+static unsigned int imq_target(struct sk_buff *pskb,
1507+ const struct xt_action_param *par)
1508+{
1509+ const struct xt_imq_info *mr = par->targinfo;
1510+
1511+ pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
1512+
1513+ return XT_CONTINUE;
1514+}
1515+
1516+static int imq_checkentry(const struct xt_tgchk_param *par)
1517+{
1518+ struct xt_imq_info *mr = par->targinfo;
1519+
1520+ if (mr->todev > IMQ_MAX_DEVS - 1) {
1521+ pr_warn("IMQ: invalid device specified, highest is %u\n",
1522+ IMQ_MAX_DEVS - 1);
1523+ return -EINVAL;
1524+ }
1525+
1526+ return 0;
1527+}
1528+
1529+static struct xt_target xt_imq_reg[] __read_mostly = {
1530+ {
1531+ .name = "IMQ",
1532+ .family = AF_INET,
1533+ .checkentry = imq_checkentry,
1534+ .target = imq_target,
1535+ .targetsize = sizeof(struct xt_imq_info),
1536+ .table = "mangle",
1537+ .me = THIS_MODULE
1538+ },
1539+ {
1540+ .name = "IMQ",
1541+ .family = AF_INET6,
1542+ .checkentry = imq_checkentry,
1543+ .target = imq_target,
1544+ .targetsize = sizeof(struct xt_imq_info),
1545+ .table = "mangle",
1546+ .me = THIS_MODULE
1547+ },
1548+};
1549+
1550+static int __init imq_init(void)
1551+{
1552+ return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1553+}
1554+
1555+static void __exit imq_fini(void)
1556+{
1557+ xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1558+}
1559+
1560+module_init(imq_init);
1561+module_exit(imq_fini);
1562+
1563+MODULE_AUTHOR("http://www.linuximq.net");
1564+MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
1565+MODULE_LICENSE("GPL");
1566+MODULE_ALIAS("ipt_IMQ");
1567+MODULE_ALIAS("ip6t_IMQ");
1568+