Root/target/linux/generic-2.6/patches-2.6.30/150-netfilter_imq.patch

1--- /dev/null
2+++ b/drivers/net/imq.c
3@@ -0,0 +1,571 @@
4+/*
5+ * Pseudo-driver for the intermediate queue device.
6+ *
7+ * This program is free software; you can redistribute it and/or
8+ * modify it under the terms of the GNU General Public License
9+ * as published by the Free Software Foundation; either version
10+ * 2 of the License, or (at your option) any later version.
11+ *
12+ * Authors: Patrick McHardy, <kaber@trash.net>
13+ *
14+ * The first version was written by Martin Devera, <devik@cdi.cz>
15+ *
16+ * Credits: Jan Rafaj <imq2t@cedric.vabo.cz>
17+ * - Update patch to 2.4.21
18+ * Sebastian Strollo <sstrollo@nortelnetworks.com>
19+ * - Fix "Dead-loop on netdevice imq"-issue
20+ * Marcel Sebek <sebek64@post.cz>
21+ * - Update to 2.6.2-rc1
22+ *
23+ * After some time of inactivity there is a group taking care
24+ * of IMQ again: http://www.linuximq.net
25+ *
26+ *
27+ * 2004/06/30 - New version of IMQ patch to kernels <=2.6.7
28+ * including the following changes:
29+ *
30+ * - Correction of ipv6 support "+"s issue (Hasso Tepper)
31+ * - Correction of imq_init_devs() issue that resulted in
32+ * kernel OOPS unloading IMQ as module (Norbert Buchmuller)
33+ * - Addition of functionality to choose number of IMQ devices
34+ * during kernel config (Andre Correa)
35+ * - Addition of functionality to choose how IMQ hooks on
36+ * PRE and POSTROUTING (after or before NAT) (Andre Correa)
37+ * - Cosmetic corrections (Norbert Buchmuller) (Andre Correa)
38+ *
39+ *
40+ * 2005/12/16 - IMQ versions between 2.6.7 and 2.6.13 were
41+ * released with almost no problems. 2.6.14-x was released
42+ * with some important changes: nfcache was removed; After
43+ * some weeks of trouble we figured out that some IMQ fields
44+ * in skb were missing in skbuff.c - skb_clone and copy_skb_header.
45+ * These functions are correctly patched by this new patch version.
46+ *
47+ * Thanks for all who helped to figure out all the problems with
48+ * 2.6.14.x: Patrick McHardy, Rune Kock, VeNoMouS, Max CtRiX,
49+ * Kevin Shanahan, Richard Lucassen, Valery Dachev (hopefully
50+ * I didn't forget anybody). I apologize again for my lack of time.
51+ *
52+ *
53+ * 2008/06/17 - 2.6.25 - Changed imq.c to use qdisc_run() instead
54+ * of qdisc_restart() and moved qdisc_run() to tasklet to avoid
55+ * recursive locking. New initialization routines to fix 'rmmod' not
56+ * working anymore. Used code from ifb.c. (Jussi Kivilinna)
57+ *
58+ * 2008/08/06 - 2.6.26 - (JK)
59+ * - Replaced tasklet with 'netif_schedule()'.
60+ * - Cleaned up and added comments for imq_nf_queue().
61+ *
62+ * 2009/04/12
63+ * - Add skb_save_cb/skb_restore_cb helper functions for backuping
64+ * control buffer. This is needed because qdisc-layer on kernels
65+ * 2.6.27 and newer overwrite control buffer. (Jussi Kivilinna)
66+ * - Add better locking for IMQ device. Hopefully this will solve
67+ * SMP issues. (Jussi Kivilinna)
68+ * - Port to 2.6.27
69+ * - Port to 2.6.28
70+ * - Port to 2.6.29 + fix rmmod not working
71+ *
72+ * 2009/04/20 - (Jussi Kivilinna)
73+ * - Use netdevice feature flags to avoid extra packet handling
74+ * by core networking layer and possibly increase performance.
75+ *
76+ * Also, many thanks to pablo Sebastian Greco for making the initial
77+ * patch and to those who helped the testing.
78+ *
79+ * More info at: http://www.linuximq.net/ (Andre Correa)
80+ */
81+
82+#include <linux/module.h>
83+#include <linux/kernel.h>
84+#include <linux/moduleparam.h>
85+#include <linux/list.h>
86+#include <linux/skbuff.h>
87+#include <linux/netdevice.h>
88+#include <linux/etherdevice.h>
89+#include <linux/rtnetlink.h>
90+#include <linux/if_arp.h>
91+#include <linux/netfilter.h>
92+#include <linux/netfilter_ipv4.h>
93+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
94+ #include <linux/netfilter_ipv6.h>
95+#endif
96+#include <linux/imq.h>
97+#include <net/pkt_sched.h>
98+#include <net/netfilter/nf_queue.h>
99+
100+static nf_hookfn imq_nf_hook;
101+
102+static struct nf_hook_ops imq_ingress_ipv4 = {
103+ .hook = imq_nf_hook,
104+ .owner = THIS_MODULE,
105+ .pf = PF_INET,
106+ .hooknum = NF_INET_PRE_ROUTING,
107+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
108+ .priority = NF_IP_PRI_MANGLE + 1
109+#else
110+ .priority = NF_IP_PRI_NAT_DST + 1
111+#endif
112+};
113+
114+static struct nf_hook_ops imq_egress_ipv4 = {
115+ .hook = imq_nf_hook,
116+ .owner = THIS_MODULE,
117+ .pf = PF_INET,
118+ .hooknum = NF_INET_POST_ROUTING,
119+#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
120+ .priority = NF_IP_PRI_LAST
121+#else
122+ .priority = NF_IP_PRI_NAT_SRC - 1
123+#endif
124+};
125+
126+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
127+static struct nf_hook_ops imq_ingress_ipv6 = {
128+ .hook = imq_nf_hook,
129+ .owner = THIS_MODULE,
130+ .pf = PF_INET6,
131+ .hooknum = NF_INET_PRE_ROUTING,
132+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
133+ .priority = NF_IP6_PRI_MANGLE + 1
134+#else
135+ .priority = NF_IP6_PRI_NAT_DST + 1
136+#endif
137+};
138+
139+static struct nf_hook_ops imq_egress_ipv6 = {
140+ .hook = imq_nf_hook,
141+ .owner = THIS_MODULE,
142+ .pf = PF_INET6,
143+ .hooknum = NF_INET_POST_ROUTING,
144+#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
145+ .priority = NF_IP6_PRI_LAST
146+#else
147+ .priority = NF_IP6_PRI_NAT_SRC - 1
148+#endif
149+};
150+#endif
151+
152+#if defined(CONFIG_IMQ_NUM_DEVS)
153+static unsigned int numdevs = CONFIG_IMQ_NUM_DEVS;
154+#else
155+static unsigned int numdevs = IMQ_MAX_DEVS;
156+#endif
157+
158+static DEFINE_SPINLOCK(imq_nf_queue_lock);
159+
160+static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
161+
162+
163+static struct net_device_stats *imq_get_stats(struct net_device *dev)
164+{
165+ return &dev->stats;
166+}
167+
168+/* called for packets kfree'd in qdiscs at places other than enqueue */
169+static void imq_skb_destructor(struct sk_buff *skb)
170+{
171+ struct nf_queue_entry *entry = skb->nf_queue_entry;
172+
173+ if (entry) {
174+ nf_queue_entry_release_refs(entry);
175+ kfree(entry);
176+ }
177+
178+ skb_restore_cb(skb); /* kfree backup */
179+}
180+
181+static void imq_nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
182+{
183+ int status;
184+
185+ if (!entry->next_outfn) {
186+ spin_lock_bh(&imq_nf_queue_lock);
187+ nf_reinject(entry, verdict);
188+ spin_unlock_bh(&imq_nf_queue_lock);
189+ return;
190+ }
191+
192+ rcu_read_lock();
193+ local_bh_disable();
194+ status = entry->next_outfn(entry, entry->next_queuenum);
195+ local_bh_enable();
196+ if (status < 0) {
197+ nf_queue_entry_release_refs(entry);
198+ kfree_skb(entry->skb);
199+ kfree(entry);
200+ }
201+
202+ rcu_read_unlock();
203+}
204+
205+static int imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
206+{
207+ dev->stats.tx_bytes += skb->len;
208+ dev->stats.tx_packets++;
209+
210+ skb->imq_flags = 0;
211+ skb->destructor = NULL;
212+
213+ skb_restore_cb(skb); /* restore skb->cb */
214+
215+ dev->trans_start = jiffies;
216+ imq_nf_reinject(skb->nf_queue_entry, NF_ACCEPT);
217+ return 0;
218+}
219+
220+static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num)
221+{
222+ struct net_device *dev;
223+ struct sk_buff *skb_orig, *skb, *skb_shared;
224+ struct Qdisc *q;
225+ struct netdev_queue *txq;
226+ int users, index;
227+ int retval = -EINVAL;
228+
229+ index = entry->skb->imq_flags & IMQ_F_IFMASK;
230+ if (unlikely(index > numdevs - 1)) {
231+ if (net_ratelimit())
232+ printk(KERN_WARNING
233+ "IMQ: invalid device specified, highest is %u\n",
234+ numdevs - 1);
235+ retval = -EINVAL;
236+ goto out;
237+ }
238+
239+ /* check for imq device by index from cache */
240+ dev = imq_devs_cache[index];
241+ if (unlikely(!dev)) {
242+ char buf[8];
243+
244+ /* get device by name and cache result */
245+ snprintf(buf, sizeof(buf), "imq%d", index);
246+ dev = dev_get_by_name(&init_net, buf);
247+ if (!dev) {
248+ /* not found ?!*/
249+ BUG();
250+ retval = -ENODEV;
251+ goto out;
252+ }
253+
254+ imq_devs_cache[index] = dev;
255+ dev_put(dev);
256+ }
257+
258+ if (unlikely(!(dev->flags & IFF_UP))) {
259+ entry->skb->imq_flags = 0;
260+ imq_nf_reinject(entry, NF_ACCEPT);
261+ retval = 0;
262+ goto out;
263+ }
264+ dev->last_rx = jiffies;
265+
266+ skb = entry->skb;
267+ skb_orig = NULL;
268+
269+ /* skb has owner? => make clone */
270+ if (unlikely(skb->destructor)) {
271+ skb_orig = skb;
272+ skb = skb_clone(skb, GFP_ATOMIC);
273+ if (!skb) {
274+ retval = -ENOMEM;
275+ goto out;
276+ }
277+ entry->skb = skb;
278+ }
279+
280+ skb->nf_queue_entry = entry;
281+
282+ dev->stats.rx_bytes += skb->len;
283+ dev->stats.rx_packets++;
284+
285+ txq = dev_pick_tx(dev, skb);
286+
287+ q = rcu_dereference(txq->qdisc);
288+ if (unlikely(!q->enqueue))
289+ goto packet_not_eaten_by_imq_dev;
290+
291+ spin_lock_bh(qdisc_lock(q));
292+
293+ users = atomic_read(&skb->users);
294+
295+ skb_shared = skb_get(skb); /* increase reference count by one */
296+ skb_save_cb(skb_shared); /* backup skb->cb, as qdisc layer will
297+ overwrite it */
298+ qdisc_enqueue_root(skb_shared, q); /* might kfree_skb */
299+
300+ if (likely(atomic_read(&skb_shared->users) == users + 1)) {
301+ kfree_skb(skb_shared); /* decrease reference count by one */
302+
303+ skb->destructor = &imq_skb_destructor;
304+
305+ /* cloned? */
306+ if (skb_orig)
307+ kfree_skb(skb_orig); /* free original */
308+
309+ spin_unlock_bh(qdisc_lock(q));
310+
311+ /* schedule qdisc dequeue */
312+ __netif_schedule(q);
313+
314+ retval = 0;
315+ goto out;
316+ } else {
317+ skb_restore_cb(skb_shared); /* restore skb->cb */
318+ /* qdisc dropped packet and decreased skb reference count of
319+ * skb, so we don't really want to and try refree as that would
320+ * actually destroy the skb. */
321+ spin_unlock_bh(qdisc_lock(q));
322+ goto packet_not_eaten_by_imq_dev;
323+ }
324+
325+packet_not_eaten_by_imq_dev:
326+ /* cloned? restore original */
327+ if (skb_orig) {
328+ kfree_skb(skb);
329+ entry->skb = skb_orig;
330+ }
331+ retval = -1;
332+out:
333+ return retval;
334+}
335+
336+static struct nf_queue_handler nfqh = {
337+ .name = "imq",
338+ .outfn = imq_nf_queue,
339+};
340+
341+static unsigned int imq_nf_hook(unsigned int hook, struct sk_buff *pskb,
342+ const struct net_device *indev,
343+ const struct net_device *outdev,
344+ int (*okfn)(struct sk_buff *))
345+{
346+ if (pskb->imq_flags & IMQ_F_ENQUEUE)
347+ return NF_QUEUE;
348+
349+ return NF_ACCEPT;
350+}
351+
352+static int imq_close(struct net_device *dev)
353+{
354+ netif_stop_queue(dev);
355+ return 0;
356+}
357+
358+static int imq_open(struct net_device *dev)
359+{
360+ netif_start_queue(dev);
361+ return 0;
362+}
363+
364+static const struct net_device_ops imq_netdev_ops = {
365+ .ndo_open = imq_open,
366+ .ndo_stop = imq_close,
367+ .ndo_start_xmit = imq_dev_xmit,
368+ .ndo_get_stats = imq_get_stats,
369+};
370+
371+static void imq_setup(struct net_device *dev)
372+{
373+ dev->netdev_ops = &imq_netdev_ops;
374+ dev->type = ARPHRD_VOID;
375+ dev->mtu = 16000;
376+ dev->tx_queue_len = 11000;
377+ dev->flags = IFF_NOARP;
378+ dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
379+ NETIF_F_GSO | NETIF_F_HW_CSUM |
380+ NETIF_F_HIGHDMA;
381+}
382+
383+static int imq_validate(struct nlattr *tb[], struct nlattr *data[])
384+{
385+ int ret = 0;
386+
387+ if (tb[IFLA_ADDRESS]) {
388+ if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
389+ ret = -EINVAL;
390+ goto end;
391+ }
392+ if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
393+ ret = -EADDRNOTAVAIL;
394+ goto end;
395+ }
396+ }
397+ return 0;
398+end:
399+ printk(KERN_WARNING "IMQ: imq_validate failed (%d)\n", ret);
400+ return ret;
401+}
402+
403+static struct rtnl_link_ops imq_link_ops __read_mostly = {
404+ .kind = "imq",
405+ .priv_size = 0,
406+ .setup = imq_setup,
407+ .validate = imq_validate,
408+};
409+
410+static int __init imq_init_hooks(void)
411+{
412+ int err;
413+
414+ nf_register_queue_imq_handler(&nfqh);
415+
416+ err = nf_register_hook(&imq_ingress_ipv4);
417+ if (err)
418+ goto err1;
419+
420+ err = nf_register_hook(&imq_egress_ipv4);
421+ if (err)
422+ goto err2;
423+
424+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
425+ err = nf_register_hook(&imq_ingress_ipv6);
426+ if (err)
427+ goto err3;
428+
429+ err = nf_register_hook(&imq_egress_ipv6);
430+ if (err)
431+ goto err4;
432+#endif
433+
434+ return 0;
435+
436+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
437+err4:
438+ nf_unregister_hook(&imq_ingress_ipv6);
439+err3:
440+ nf_unregister_hook(&imq_egress_ipv4);
441+#endif
442+err2:
443+ nf_unregister_hook(&imq_ingress_ipv4);
444+err1:
445+ nf_unregister_queue_imq_handler();
446+ return err;
447+}
448+
449+static int __init imq_init_one(int index)
450+{
451+ struct net_device *dev;
452+ int ret;
453+
454+ dev = alloc_netdev(0, "imq%d", imq_setup);
455+ if (!dev)
456+ return -ENOMEM;
457+
458+ ret = dev_alloc_name(dev, dev->name);
459+ if (ret < 0)
460+ goto fail;
461+
462+ dev->rtnl_link_ops = &imq_link_ops;
463+ ret = register_netdevice(dev);
464+ if (ret < 0)
465+ goto fail;
466+
467+ return 0;
468+fail:
469+ free_netdev(dev);
470+ return ret;
471+}
472+
473+static int __init imq_init_devs(void)
474+{
475+ int err, i;
476+
477+ if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) {
478+ printk(KERN_ERR "IMQ: numdevs has to be betweed 1 and %u\n",
479+ IMQ_MAX_DEVS);
480+ return -EINVAL;
481+ }
482+
483+ rtnl_lock();
484+ err = __rtnl_link_register(&imq_link_ops);
485+
486+ for (i = 0; i < numdevs && !err; i++)
487+ err = imq_init_one(i);
488+
489+ if (err) {
490+ __rtnl_link_unregister(&imq_link_ops);
491+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
492+ }
493+ rtnl_unlock();
494+
495+ return err;
496+}
497+
498+static int __init imq_init_module(void)
499+{
500+ int err;
501+
502+#if defined(CONFIG_IMQ_NUM_DEVS)
503+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS > 16);
504+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS < 2);
505+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS - 1 > IMQ_F_IFMASK);
506+#endif
507+
508+ err = imq_init_devs();
509+ if (err) {
510+ printk(KERN_ERR "IMQ: Error trying imq_init_devs(net)\n");
511+ return err;
512+ }
513+
514+ err = imq_init_hooks();
515+ if (err) {
516+ printk(KERN_ERR "IMQ: Error trying imq_init_hooks()\n");
517+ rtnl_link_unregister(&imq_link_ops);
518+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
519+ return err;
520+ }
521+
522+ printk(KERN_INFO "IMQ driver loaded successfully.\n");
523+
524+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
525+ printk(KERN_INFO "\tHooking IMQ before NAT on PREROUTING.\n");
526+#else
527+ printk(KERN_INFO "\tHooking IMQ after NAT on PREROUTING.\n");
528+#endif
529+#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
530+ printk(KERN_INFO "\tHooking IMQ before NAT on POSTROUTING.\n");
531+#else
532+ printk(KERN_INFO "\tHooking IMQ after NAT on POSTROUTING.\n");
533+#endif
534+
535+ return 0;
536+}
537+
538+static void __exit imq_unhook(void)
539+{
540+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
541+ nf_unregister_hook(&imq_ingress_ipv6);
542+ nf_unregister_hook(&imq_egress_ipv6);
543+#endif
544+ nf_unregister_hook(&imq_ingress_ipv4);
545+ nf_unregister_hook(&imq_egress_ipv4);
546+
547+ nf_unregister_queue_imq_handler();
548+}
549+
550+static void __exit imq_cleanup_devs(void)
551+{
552+ rtnl_link_unregister(&imq_link_ops);
553+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
554+}
555+
556+static void __exit imq_exit_module(void)
557+{
558+ imq_unhook();
559+ imq_cleanup_devs();
560+ printk(KERN_INFO "IMQ driver unloaded successfully.\n");
561+}
562+
563+module_init(imq_init_module);
564+module_exit(imq_exit_module);
565+
566+module_param(numdevs, int, 0);
567+MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will "
568+ "be created)");
569+MODULE_AUTHOR("http://www.linuximq.net");
570+MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See "
571+ "http://www.linuximq.net/ for more information.");
572+MODULE_LICENSE("GPL");
573+MODULE_ALIAS_RTNL_LINK("imq");
574+
575--- a/drivers/net/Kconfig
576+++ b/drivers/net/Kconfig
577@@ -119,6 +119,129 @@ config EQUALIZER
578       To compile this driver as a module, choose M here: the module
579       will be called eql. If unsure, say N.
580 
581+config IMQ
582+ tristate "IMQ (intermediate queueing device) support"
583+ depends on NETDEVICES && NETFILTER
584+ ---help---
585+ The IMQ device(s) is used as placeholder for QoS queueing
586+ disciplines. Every packet entering/leaving the IP stack can be
587+ directed through the IMQ device where it's enqueued/dequeued to the
588+ attached qdisc. This allows you to treat network devices as classes
589+ and distribute bandwidth among them. Iptables is used to specify
590+ through which IMQ device, if any, packets travel.
591+
592+ More information at: http://www.linuximq.net/
593+
594+ To compile this driver as a module, choose M here: the module
595+ will be called imq. If unsure, say N.
596+
597+choice
598+ prompt "IMQ behavior (PRE/POSTROUTING)"
599+ depends on IMQ
600+ default IMQ_BEHAVIOR_AB
601+ help
602+
603+ This settings defines how IMQ behaves in respect to its
604+ hooking in PREROUTING and POSTROUTING.
605+
606+ IMQ can work in any of the following ways:
607+
608+ PREROUTING | POSTROUTING
609+ -----------------|-------------------
610+ #1 After NAT | After NAT
611+ #2 After NAT | Before NAT
612+ #3 Before NAT | After NAT
613+ #4 Before NAT | Before NAT
614+
615+ The default behavior is to hook before NAT on PREROUTING
616+ and after NAT on POSTROUTING (#3).
617+
618+ This settings are specially usefull when trying to use IMQ
619+ to shape NATed clients.
620+
621+ More information can be found at: www.linuximq.net
622+
623+ If not sure leave the default settings alone.
624+
625+config IMQ_BEHAVIOR_AA
626+ bool "IMQ AA"
627+ help
628+ This settings defines how IMQ behaves in respect to its
629+ hooking in PREROUTING and POSTROUTING.
630+
631+ Choosing this option will make IMQ hook like this:
632+
633+ PREROUTING: After NAT
634+ POSTROUTING: After NAT
635+
636+ More information can be found at: www.linuximq.net
637+
638+ If not sure leave the default settings alone.
639+
640+config IMQ_BEHAVIOR_AB
641+ bool "IMQ AB"
642+ help
643+ This settings defines how IMQ behaves in respect to its
644+ hooking in PREROUTING and POSTROUTING.
645+
646+ Choosing this option will make IMQ hook like this:
647+
648+ PREROUTING: After NAT
649+ POSTROUTING: Before NAT
650+
651+ More information can be found at: www.linuximq.net
652+
653+ If not sure leave the default settings alone.
654+
655+config IMQ_BEHAVIOR_BA
656+ bool "IMQ BA"
657+ help
658+ This settings defines how IMQ behaves in respect to its
659+ hooking in PREROUTING and POSTROUTING.
660+
661+ Choosing this option will make IMQ hook like this:
662+
663+ PREROUTING: Before NAT
664+ POSTROUTING: After NAT
665+
666+ More information can be found at: www.linuximq.net
667+
668+ If not sure leave the default settings alone.
669+
670+config IMQ_BEHAVIOR_BB
671+ bool "IMQ BB"
672+ help
673+ This settings defines how IMQ behaves in respect to its
674+ hooking in PREROUTING and POSTROUTING.
675+
676+ Choosing this option will make IMQ hook like this:
677+
678+ PREROUTING: Before NAT
679+ POSTROUTING: Before NAT
680+
681+ More information can be found at: www.linuximq.net
682+
683+ If not sure leave the default settings alone.
684+
685+endchoice
686+
687+config IMQ_NUM_DEVS
688+
689+ int "Number of IMQ devices"
690+ range 2 16
691+ depends on IMQ
692+ default "16"
693+ help
694+
695+ This settings defines how many IMQ devices will be
696+ created.
697+
698+ The default value is 16.
699+
700+ More information can be found at: www.linuximq.net
701+
702+ If not sure leave the default settings alone.
703+
704 config TUN
705     tristate "Universal TUN/TAP device driver support"
706     select CRC32
707--- a/drivers/net/Makefile
708+++ b/drivers/net/Makefile
709@@ -152,6 +152,7 @@ obj-$(CONFIG_SLHC) += slhc.o
710 obj-$(CONFIG_XEN_NETDEV_FRONTEND) += xen-netfront.o
711 
712 obj-$(CONFIG_DUMMY) += dummy.o
713+obj-$(CONFIG_IMQ) += imq.o
714 obj-$(CONFIG_IFB) += ifb.o
715 obj-$(CONFIG_MACVLAN) += macvlan.o
716 obj-$(CONFIG_DE600) += de600.o
717--- /dev/null
718+++ b/include/linux/imq.h
719@@ -0,0 +1,13 @@
720+#ifndef _IMQ_H
721+#define _IMQ_H
722+
723+/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
724+#define IMQ_F_BITS 5
725+
726+#define IMQ_F_IFMASK 0x0f
727+#define IMQ_F_ENQUEUE 0x10
728+
729+#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1)
730+
731+#endif /* _IMQ_H */
732+
733--- /dev/null
734+++ b/include/linux/netfilter_ipv4/ipt_IMQ.h
735@@ -0,0 +1,10 @@
736+#ifndef _IPT_IMQ_H
737+#define _IPT_IMQ_H
738+
739+/* Backwards compatibility for old userspace */
740+#include <linux/netfilter/xt_IMQ.h>
741+
742+#define ipt_imq_info xt_imq_info
743+
744+#endif /* _IPT_IMQ_H */
745+
746--- /dev/null
747+++ b/include/linux/netfilter_ipv6/ip6t_IMQ.h
748@@ -0,0 +1,10 @@
749+#ifndef _IP6T_IMQ_H
750+#define _IP6T_IMQ_H
751+
752+/* Backwards compatibility for old userspace */
753+#include <linux/netfilter/xt_IMQ.h>
754+
755+#define ip6t_imq_info xt_imq_info
756+
757+#endif /* _IP6T_IMQ_H */
758+
759--- a/include/linux/skbuff.h
760+++ b/include/linux/skbuff.h
761@@ -28,6 +28,9 @@
762 #include <linux/rcupdate.h>
763 #include <linux/dmaengine.h>
764 #include <linux/hrtimer.h>
765+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
766+#include <linux/imq.h>
767+#endif
768 
769 /* Don't change this without changing skb_csum_unnecessary! */
770 #define CHECKSUM_NONE 0
771@@ -333,6 +336,9 @@ struct sk_buff {
772      * first. This is owned by whoever has the skb queued ATM.
773      */
774     char cb[48];
775+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
776+ void *cb_next;
777+#endif
778 
779     unsigned int len,
780                 data_len;
781@@ -363,6 +369,9 @@ struct sk_buff {
782     struct nf_conntrack *nfct;
783     struct sk_buff *nfct_reasm;
784 #endif
785+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
786+ struct nf_queue_entry *nf_queue_entry;
787+#endif
788 #ifdef CONFIG_BRIDGE_NETFILTER
789     struct nf_bridge_info *nf_bridge;
790 #endif
791@@ -383,6 +392,9 @@ struct sk_buff {
792     __u8 requeue:1;
793 #endif
794     /* 0/13/14 bit hole */
795+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
796+ __u8 imq_flags:IMQ_F_BITS;
797+#endif
798 
799 #ifdef CONFIG_NET_DMA
800     dma_cookie_t dma_cookie;
801@@ -423,6 +435,12 @@ extern void skb_dma_unmap(struct device
802               enum dma_data_direction dir);
803 #endif
804 
805+
806+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
807+extern int skb_save_cb(struct sk_buff *skb);
808+extern int skb_restore_cb(struct sk_buff *skb);
809+#endif
810+
811 extern void kfree_skb(struct sk_buff *skb);
812 extern void consume_skb(struct sk_buff *skb);
813 extern void __kfree_skb(struct sk_buff *skb);
814@@ -1931,6 +1949,10 @@ static inline void __nf_copy(struct sk_b
815     dst->nfct_reasm = src->nfct_reasm;
816     nf_conntrack_get_reasm(src->nfct_reasm);
817 #endif
818+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
819+ dst->imq_flags = src->imq_flags;
820+ dst->nf_queue_entry = src->nf_queue_entry;
821+#endif
822 #ifdef CONFIG_BRIDGE_NETFILTER
823     dst->nf_bridge = src->nf_bridge;
824     nf_bridge_get(src->nf_bridge);
825--- a/net/core/dev.c
826+++ b/net/core/dev.c
827@@ -96,6 +96,9 @@
828 #include <net/net_namespace.h>
829 #include <net/sock.h>
830 #include <linux/rtnetlink.h>
831+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
832+#include <linux/imq.h>
833+#endif
834 #include <linux/proc_fs.h>
835 #include <linux/seq_file.h>
836 #include <linux/stat.h>
837@@ -1678,7 +1681,11 @@ int dev_hard_start_xmit(struct sk_buff *
838     int rc;
839 
840     if (likely(!skb->next)) {
841- if (!list_empty(&ptype_all))
842+ if (!list_empty(&ptype_all)
843+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
844+ && !(skb->imq_flags & IMQ_F_ENQUEUE)
845+#endif
846+ )
847             dev_queue_xmit_nit(skb, dev);
848 
849         if (netif_needs_gso(dev, skb)) {
850@@ -1749,8 +1756,7 @@ u16 skb_tx_hash(const struct net_device
851 }
852 EXPORT_SYMBOL(skb_tx_hash);
853 
854-static struct netdev_queue *dev_pick_tx(struct net_device *dev,
855- struct sk_buff *skb)
856+struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb)
857 {
858     const struct net_device_ops *ops = dev->netdev_ops;
859     u16 queue_index = 0;
860@@ -1763,6 +1769,7 @@ static struct netdev_queue *dev_pick_tx(
861     skb_set_queue_mapping(skb, queue_index);
862     return netdev_get_tx_queue(dev, queue_index);
863 }
864+EXPORT_SYMBOL(dev_pick_tx);
865 
866 /**
867  * dev_queue_xmit - transmit a buffer
868--- a/include/linux/netdevice.h
869+++ b/include/linux/netdevice.h
870@@ -1102,6 +1102,7 @@ extern int dev_alloc_name(struct net_de
871 extern int dev_open(struct net_device *dev);
872 extern int dev_close(struct net_device *dev);
873 extern void dev_disable_lro(struct net_device *dev);
874+extern struct netdev_queue *dev_pick_tx(struct net_device *dev, struct sk_buff *skb);
875 extern int dev_queue_xmit(struct sk_buff *skb);
876 extern int register_netdevice(struct net_device *dev);
877 extern void unregister_netdevice(struct net_device *dev);
878--- /dev/null
879+++ b/include/linux/netfilter/xt_IMQ.h
880@@ -0,0 +1,9 @@
881+#ifndef _XT_IMQ_H
882+#define _XT_IMQ_H
883+
884+struct xt_imq_info {
885+ unsigned int todev; /* target imq device */
886+};
887+
888+#endif /* _XT_IMQ_H */
889+
890--- a/include/net/netfilter/nf_queue.h
891+++ b/include/net/netfilter/nf_queue.h
892@@ -13,6 +13,12 @@ struct nf_queue_entry {
893     struct net_device *indev;
894     struct net_device *outdev;
895     int (*okfn)(struct sk_buff *);
896+
897+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
898+ int (*next_outfn)(struct nf_queue_entry *entry,
899+ unsigned int queuenum);
900+ unsigned int next_queuenum;
901+#endif
902 };
903 
904 #define nf_queue_entry_reroute(x) ((void *)x + sizeof(struct nf_queue_entry))
905@@ -30,5 +36,11 @@ extern int nf_unregister_queue_handler(u
906                        const struct nf_queue_handler *qh);
907 extern void nf_unregister_queue_handlers(const struct nf_queue_handler *qh);
908 extern void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
909+extern void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
910+
911+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
912+extern void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
913+extern void nf_unregister_queue_imq_handler(void);
914+#endif
915 
916 #endif /* _NF_QUEUE_H */
917--- a/net/core/skbuff.c
918+++ b/net/core/skbuff.c
919@@ -71,6 +71,9 @@
920 
921 static struct kmem_cache *skbuff_head_cache __read_mostly;
922 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
923+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
924+static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
925+#endif
926 
927 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
928                   struct pipe_buffer *buf)
929@@ -90,6 +93,80 @@ static int sock_pipe_buf_steal(struct pi
930     return 1;
931 }
932 
933+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
934+/* Control buffer save/restore for IMQ devices */
935+struct skb_cb_table {
936+ void *cb_next;
937+ atomic_t refcnt;
938+ char cb[48];
939+};
940+
941+static DEFINE_SPINLOCK(skb_cb_store_lock);
942+
943+int skb_save_cb(struct sk_buff *skb)
944+{
945+ struct skb_cb_table *next;
946+
947+ next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
948+ if (!next)
949+ return -ENOMEM;
950+
951+ BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
952+
953+ memcpy(next->cb, skb->cb, sizeof(skb->cb));
954+ next->cb_next = skb->cb_next;
955+
956+ atomic_set(&next->refcnt, 1);
957+
958+ skb->cb_next = next;
959+ return 0;
960+}
961+EXPORT_SYMBOL(skb_save_cb);
962+
963+int skb_restore_cb(struct sk_buff *skb)
964+{
965+ struct skb_cb_table *next;
966+
967+ if (!skb->cb_next)
968+ return 0;
969+
970+ next = skb->cb_next;
971+
972+ BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
973+
974+ memcpy(skb->cb, next->cb, sizeof(skb->cb));
975+ skb->cb_next = next->cb_next;
976+
977+ spin_lock(&skb_cb_store_lock);
978+
979+ if (atomic_dec_and_test(&next->refcnt)) {
980+ kmem_cache_free(skbuff_cb_store_cache, next);
981+ }
982+
983+ spin_unlock(&skb_cb_store_lock);
984+
985+ return 0;
986+}
987+EXPORT_SYMBOL(skb_restore_cb);
988+
989+static void skb_copy_stored_cb(struct sk_buff *new, struct sk_buff *old)
990+{
991+ struct skb_cb_table *next;
992+
993+ if (!old->cb_next) {
994+ new->cb_next = 0;
995+ return;
996+ }
997+
998+ spin_lock(&skb_cb_store_lock);
999+
1000+ next = old->cb_next;
1001+ atomic_inc(&next->refcnt);
1002+ new->cb_next = next;
1003+
1004+ spin_unlock(&skb_cb_store_lock);
1005+}
1006+#endif
1007 
1008 /* Pipe buffer operations for a socket. */
1009 static struct pipe_buf_operations sock_pipe_buf_ops = {
1010@@ -389,6 +466,15 @@ static void skb_release_head_state(struc
1011         WARN_ON(in_irq());
1012         skb->destructor(skb);
1013     }
1014+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1015+ /* This should not happen. When it does, avoid memleak by restoring
1016+ the chain of cb-backups. */
1017+ while(skb->cb_next != NULL) {
1018+ printk(KERN_WARNING "kfree_skb: skb->cb_next: %08x\n",
1019+ skb->cb_next);
1020+ skb_restore_cb(skb);
1021+ }
1022+#endif
1023 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1024     nf_conntrack_put(skb->nfct);
1025     nf_conntrack_put_reasm(skb->nfct_reasm);
1026@@ -526,6 +612,9 @@ static void __copy_skb_header(struct sk_
1027     new->sp = secpath_get(old->sp);
1028 #endif
1029     memcpy(new->cb, old->cb, sizeof(old->cb));
1030+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1031+ skb_copy_stored_cb(new, old);
1032+#endif
1033     new->csum_start = old->csum_start;
1034     new->csum_offset = old->csum_offset;
1035     new->local_df = old->local_df;
1036@@ -2769,6 +2858,13 @@ void __init skb_init(void)
1037                         0,
1038                         SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1039                         NULL);
1040+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1041+ skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1042+ sizeof(struct skb_cb_table),
1043+ 0,
1044+ SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1045+ NULL);
1046+#endif
1047 }
1048 
1049 /**
1050--- a/net/netfilter/Kconfig
1051+++ b/net/netfilter/Kconfig
1052@@ -396,6 +396,18 @@ config NETFILTER_XT_TARGET_LED
1053       For more information on the LEDs available on your system, see
1054       Documentation/leds-class.txt
1055 
1056+config NETFILTER_XT_TARGET_IMQ
1057+ tristate '"IMQ" target support'
1058+ depends on NETFILTER_XTABLES
1059+ depends on IP_NF_MANGLE || IP6_NF_MANGLE
1060+ select IMQ
1061+ default m if NETFILTER_ADVANCED=n
1062+ help
1063+ This option adds a `IMQ' target which is used to specify if and
1064+ to which imq device packets should get enqueued/dequeued.
1065+
1066+ To compile it as a module, choose M here. If unsure, say N.
1067+
1068 config NETFILTER_XT_TARGET_MARK
1069     tristate '"MARK" target support'
1070     default m if NETFILTER_ADVANCED=n
1071--- a/net/netfilter/Makefile
1072+++ b/net/netfilter/Makefile
1073@@ -46,6 +46,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_CONNMAR
1074 obj-$(CONFIG_NETFILTER_XT_TARGET_CONNSECMARK) += xt_CONNSECMARK.o
1075 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
1076 obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
1077+obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
1078 obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
1079 obj-$(CONFIG_NETFILTER_XT_TARGET_MARK) += xt_MARK.o
1080 obj-$(CONFIG_NETFILTER_XT_TARGET_NFLOG) += xt_NFLOG.o
1081--- a/net/netfilter/nf_queue.c
1082+++ b/net/netfilter/nf_queue.c
1083@@ -20,6 +20,26 @@ static const struct nf_queue_handler *qu
1084 
1085 static DEFINE_MUTEX(queue_handler_mutex);
1086 
1087+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1088+static const struct nf_queue_handler *queue_imq_handler;
1089+
1090+void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
1091+{
1092+ mutex_lock(&queue_handler_mutex);
1093+ rcu_assign_pointer(queue_imq_handler, qh);
1094+ mutex_unlock(&queue_handler_mutex);
1095+}
1096+EXPORT_SYMBOL(nf_register_queue_imq_handler);
1097+
1098+void nf_unregister_queue_imq_handler(void)
1099+{
1100+ mutex_lock(&queue_handler_mutex);
1101+ rcu_assign_pointer(queue_imq_handler, NULL);
1102+ mutex_unlock(&queue_handler_mutex);
1103+}
1104+EXPORT_SYMBOL(nf_unregister_queue_imq_handler);
1105+#endif
1106+
1107 /* return EBUSY when somebody else is registered, return EEXIST if the
1108  * same handler is registered, return 0 in case of success. */
1109 int nf_register_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh)
1110@@ -80,7 +100,7 @@ void nf_unregister_queue_handlers(const
1111 }
1112 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
1113 
1114-static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1115+void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
1116 {
1117     /* Release those devices we held, or Alexey will kill me. */
1118     if (entry->indev)
1119@@ -100,6 +120,7 @@ static void nf_queue_entry_release_refs(
1120     /* Drop reference to owner of hook which queued us. */
1121     module_put(entry->elem->owner);
1122 }
1123+EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
1124 
1125 /*
1126  * Any packet that leaves via this function must come back
1127@@ -121,12 +142,26 @@ static int __nf_queue(struct sk_buff *sk
1128 #endif
1129     const struct nf_afinfo *afinfo;
1130     const struct nf_queue_handler *qh;
1131+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1132+ const struct nf_queue_handler *qih = NULL;
1133+#endif
1134 
1135     /* QUEUE == DROP if noone is waiting, to be safe. */
1136     rcu_read_lock();
1137 
1138     qh = rcu_dereference(queue_handler[pf]);
1139+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1140+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1141+ if (pf == PF_INET || pf == PF_INET6)
1142+#else
1143+ if (pf == PF_INET)
1144+#endif
1145+ qih = rcu_dereference(queue_imq_handler);
1146+
1147+ if (!qh && !qih)
1148+#else /* !IMQ */
1149     if (!qh)
1150+#endif
1151         goto err_unlock;
1152 
1153     afinfo = nf_get_afinfo(pf);
1154@@ -145,6 +180,10 @@ static int __nf_queue(struct sk_buff *sk
1155         .indev = indev,
1156         .outdev = outdev,
1157         .okfn = okfn,
1158+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1159+ .next_outfn = qh ? qh->outfn : NULL,
1160+ .next_queuenum = queuenum,
1161+#endif
1162     };
1163 
1164     /* If it's going away, ignore hook. */
1165@@ -170,8 +209,19 @@ static int __nf_queue(struct sk_buff *sk
1166     }
1167 #endif
1168     afinfo->saveroute(skb, entry);
1169+
1170+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1171+ if (qih) {
1172+ status = qih->outfn(entry, queuenum);
1173+ goto imq_skip_queue;
1174+ }
1175+#endif
1176+
1177     status = qh->outfn(entry, queuenum);
1178 
1179+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1180+imq_skip_queue:
1181+#endif
1182     rcu_read_unlock();
1183 
1184     if (status < 0) {
1185--- /dev/null
1186+++ b/net/netfilter/xt_IMQ.c
1187@@ -0,0 +1,73 @@
1188+/*
1189+ * This target marks packets to be enqueued to an imq device
1190+ */
1191+#include <linux/module.h>
1192+#include <linux/skbuff.h>
1193+#include <linux/netfilter/x_tables.h>
1194+#include <linux/netfilter/xt_IMQ.h>
1195+#include <linux/imq.h>
1196+
1197+static unsigned int imq_target(struct sk_buff *pskb,
1198+ const struct xt_target_param *par)
1199+{
1200+ const struct xt_imq_info *mr = par->targinfo;
1201+
1202+ pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
1203+
1204+ return XT_CONTINUE;
1205+}
1206+
1207+static bool imq_checkentry(const struct xt_tgchk_param *par)
1208+{
1209+ struct xt_imq_info *mr = par->targinfo;
1210+
1211+ if (mr->todev > IMQ_MAX_DEVS - 1) {
1212+ printk(KERN_WARNING
1213+ "IMQ: invalid device specified, highest is %u\n",
1214+ IMQ_MAX_DEVS - 1);
1215+ return 0;
1216+ }
1217+
1218+ return 1;
1219+}
1220+
1221+static struct xt_target xt_imq_reg[] __read_mostly = {
1222+ {
1223+ .name = "IMQ",
1224+ .family = AF_INET,
1225+ .checkentry = imq_checkentry,
1226+ .target = imq_target,
1227+ .targetsize = sizeof(struct xt_imq_info),
1228+ .table = "mangle",
1229+ .me = THIS_MODULE
1230+ },
1231+ {
1232+ .name = "IMQ",
1233+ .family = AF_INET6,
1234+ .checkentry = imq_checkentry,
1235+ .target = imq_target,
1236+ .targetsize = sizeof(struct xt_imq_info),
1237+ .table = "mangle",
1238+ .me = THIS_MODULE
1239+ },
1240+};
1241+
1242+static int __init imq_init(void)
1243+{
1244+ return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1245+}
1246+
1247+static void __exit imq_fini(void)
1248+{
1249+ xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
1250+}
1251+
1252+module_init(imq_init);
1253+module_exit(imq_fini);
1254+
1255+MODULE_AUTHOR("http://www.linuximq.net");
1256+MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
1257+MODULE_LICENSE("GPL");
1258+MODULE_ALIAS("ipt_IMQ");
1259+MODULE_ALIAS("ip6t_IMQ");
1260+
1261

Archive Download this file



interactive