]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/patches/linux-3.10.30-imq.patch
asterisk addon: update to 11.13.1
[people/pmueller/ipfire-2.x.git] / src / patches / linux-3.10.30-imq.patch
CommitLineData
2fff1175
AF
1diff -Naur linux-3.10.30.org/drivers/net/imq.c linux-3.10.30/drivers/net/imq.c
2--- linux-3.10.30.org/drivers/net/imq.c 1970-01-01 01:00:00.000000000 +0100
3+++ linux-3.10.30/drivers/net/imq.c 2014-02-14 20:29:05.379402305 +0100
4@@ -0,0 +1,1001 @@
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+ * 2013/09/10 - Jussi Kivilinna <jussi.kivilinna@iki.fi>
139+ * - Fixed GSO handling for 3.10, see imq_nf_queue() for comments.
140+ * - Don't copy skb->cb_next when copying or cloning skbuffs.
141+ *
142+ * Also, many thanks to pablo Sebastian Greco for making the initial
143+ * patch and to those who helped the testing.
144+ *
145+ * More info at: http://www.linuximq.net/ (Andre Correa)
146+ */
147+
148+#include <linux/module.h>
149+#include <linux/kernel.h>
150+#include <linux/moduleparam.h>
151+#include <linux/list.h>
152+#include <linux/skbuff.h>
153+#include <linux/netdevice.h>
154+#include <linux/etherdevice.h>
155+#include <linux/rtnetlink.h>
156+#include <linux/if_arp.h>
157+#include <linux/netfilter.h>
158+#include <linux/netfilter_ipv4.h>
159+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
160+ #include <linux/netfilter_ipv6.h>
161+#endif
162+#include <linux/imq.h>
163+#include <net/pkt_sched.h>
164+#include <net/netfilter/nf_queue.h>
165+#include <net/sock.h>
166+#include <linux/ip.h>
167+#include <linux/ipv6.h>
168+#include <linux/if_vlan.h>
169+#include <linux/if_pppox.h>
170+#include <net/ip.h>
171+#include <net/ipv6.h>
172+
173+static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num);
174+
175+static nf_hookfn imq_nf_hook;
176+
177+static struct nf_hook_ops imq_ops[] = {
178+ {
179+ /* imq_ingress_ipv4 */
180+ .hook = imq_nf_hook,
181+ .owner = THIS_MODULE,
182+ .pf = PF_INET,
183+ .hooknum = NF_INET_PRE_ROUTING,
184+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
185+ .priority = NF_IP_PRI_MANGLE + 1,
186+#else
187+ .priority = NF_IP_PRI_NAT_DST + 1,
188+#endif
189+ },
190+ {
191+ /* imq_egress_ipv4 */
192+ .hook = imq_nf_hook,
193+ .owner = THIS_MODULE,
194+ .pf = PF_INET,
195+ .hooknum = NF_INET_POST_ROUTING,
196+#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
197+ .priority = NF_IP_PRI_LAST,
198+#else
199+ .priority = NF_IP_PRI_NAT_SRC - 1,
200+#endif
201+ },
202+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
203+ {
204+ /* imq_ingress_ipv6 */
205+ .hook = imq_nf_hook,
206+ .owner = THIS_MODULE,
207+ .pf = PF_INET6,
208+ .hooknum = NF_INET_PRE_ROUTING,
209+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
210+ .priority = NF_IP6_PRI_MANGLE + 1,
211+#else
212+ .priority = NF_IP6_PRI_NAT_DST + 1,
213+#endif
214+ },
215+ {
216+ /* imq_egress_ipv6 */
217+ .hook = imq_nf_hook,
218+ .owner = THIS_MODULE,
219+ .pf = PF_INET6,
220+ .hooknum = NF_INET_POST_ROUTING,
221+#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
222+ .priority = NF_IP6_PRI_LAST,
223+#else
224+ .priority = NF_IP6_PRI_NAT_SRC - 1,
225+#endif
226+ },
227+#endif
228+};
229+
230+#if defined(CONFIG_IMQ_NUM_DEVS)
231+static int numdevs = CONFIG_IMQ_NUM_DEVS;
232+#else
233+static int numdevs = IMQ_MAX_DEVS;
234+#endif
235+
236+static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
237+
238+#define IMQ_MAX_QUEUES 32
239+static int numqueues = 1;
240+static u32 imq_hashrnd;
241+
242+static inline __be16 pppoe_proto(const struct sk_buff *skb)
243+{
244+ return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
245+ sizeof(struct pppoe_hdr)));
246+}
247+
248+static u16 imq_hash(struct net_device *dev, struct sk_buff *skb)
249+{
250+ unsigned int pull_len;
251+ u16 protocol = skb->protocol;
252+ u32 addr1, addr2;
253+ u32 hash, ihl = 0;
254+ union {
255+ u16 in16[2];
256+ u32 in32;
257+ } ports;
258+ u8 ip_proto;
259+
260+ pull_len = 0;
261+
262+recheck:
263+ switch (protocol) {
264+ case htons(ETH_P_8021Q): {
265+ if (unlikely(skb_pull(skb, VLAN_HLEN) == NULL))
266+ goto other;
267+
268+ pull_len += VLAN_HLEN;
269+ skb->network_header += VLAN_HLEN;
270+
271+ protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
272+ goto recheck;
273+ }
274+
275+ case htons(ETH_P_PPP_SES): {
276+ if (unlikely(skb_pull(skb, PPPOE_SES_HLEN) == NULL))
277+ goto other;
278+
279+ pull_len += PPPOE_SES_HLEN;
280+ skb->network_header += PPPOE_SES_HLEN;
281+
282+ protocol = pppoe_proto(skb);
283+ goto recheck;
284+ }
285+
286+ case htons(ETH_P_IP): {
287+ const struct iphdr *iph = ip_hdr(skb);
288+
289+ if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr))))
290+ goto other;
291+
292+ addr1 = iph->daddr;
293+ addr2 = iph->saddr;
294+
295+ ip_proto = !(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) ?
296+ iph->protocol : 0;
297+ ihl = ip_hdrlen(skb);
298+
299+ break;
300+ }
301+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
302+ case htons(ETH_P_IPV6): {
303+ const struct ipv6hdr *iph = ipv6_hdr(skb);
304+ __be16 fo = 0;
305+
306+ if (unlikely(!pskb_may_pull(skb, sizeof(struct ipv6hdr))))
307+ goto other;
308+
309+ addr1 = iph->daddr.s6_addr32[3];
310+ addr2 = iph->saddr.s6_addr32[3];
311+ ihl = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &ip_proto,
312+ &fo);
313+ if (unlikely(ihl < 0))
314+ goto other;
315+
316+ break;
317+ }
318+#endif
319+ default:
320+other:
321+ if (pull_len != 0) {
322+ skb_push(skb, pull_len);
323+ skb->network_header -= pull_len;
324+ }
325+
326+ return (u16)(ntohs(protocol) % dev->real_num_tx_queues);
327+ }
328+
329+ if (addr1 > addr2)
330+ swap(addr1, addr2);
331+
332+ switch (ip_proto) {
333+ case IPPROTO_TCP:
334+ case IPPROTO_UDP:
335+ case IPPROTO_DCCP:
336+ case IPPROTO_ESP:
337+ case IPPROTO_AH:
338+ case IPPROTO_SCTP:
339+ case IPPROTO_UDPLITE: {
340+ if (likely(skb_copy_bits(skb, ihl, &ports.in32, 4) >= 0)) {
341+ if (ports.in16[0] > ports.in16[1])
342+ swap(ports.in16[0], ports.in16[1]);
343+ break;
344+ }
345+ /* fall-through */
346+ }
347+ default:
348+ ports.in32 = 0;
349+ break;
350+ }
351+
352+ if (pull_len != 0) {
353+ skb_push(skb, pull_len);
354+ skb->network_header -= pull_len;
355+ }
356+
357+ hash = jhash_3words(addr1, addr2, ports.in32, imq_hashrnd ^ ip_proto);
358+
359+ return (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
360+}
361+
362+static inline bool sk_tx_queue_recorded(struct sock *sk)
363+{
364+ return (sk_tx_queue_get(sk) >= 0);
365+}
366+
367+static struct netdev_queue *imq_select_queue(struct net_device *dev,
368+ struct sk_buff *skb)
369+{
370+ u16 queue_index = 0;
371+ u32 hash;
372+
373+ if (likely(dev->real_num_tx_queues == 1))
374+ goto out;
375+
376+ /* IMQ can be receiving ingress or engress packets. */
377+
378+ /* Check first for if rx_queue is set */
379+ if (skb_rx_queue_recorded(skb)) {
380+ queue_index = skb_get_rx_queue(skb);
381+ goto out;
382+ }
383+
384+ /* Check if socket has tx_queue set */
385+ if (sk_tx_queue_recorded(skb->sk)) {
386+ queue_index = sk_tx_queue_get(skb->sk);
387+ goto out;
388+ }
389+
390+ /* Try use socket hash */
391+ if (skb->sk && skb->sk->sk_hash) {
392+ hash = skb->sk->sk_hash;
393+ queue_index =
394+ (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
395+ goto out;
396+ }
397+
398+ /* Generate hash from packet data */
399+ queue_index = imq_hash(dev, skb);
400+
401+out:
402+ if (unlikely(queue_index >= dev->real_num_tx_queues))
403+ queue_index = (u16)((u32)queue_index % dev->real_num_tx_queues);
404+
405+ skb_set_queue_mapping(skb, queue_index);
406+ return netdev_get_tx_queue(dev, queue_index);
407+}
408+
409+static struct net_device_stats *imq_get_stats(struct net_device *dev)
410+{
411+ return &dev->stats;
412+}
413+
414+/* called for packets kfree'd in qdiscs at places other than enqueue */
415+static void imq_skb_destructor(struct sk_buff *skb)
416+{
417+ struct nf_queue_entry *entry = skb->nf_queue_entry;
418+
419+ skb->nf_queue_entry = NULL;
420+
421+ if (entry) {
422+ nf_queue_entry_release_refs(entry);
423+ kfree(entry);
424+ }
425+
426+ skb_restore_cb(skb); /* kfree backup */
427+}
428+
429+static void imq_done_check_queue_mapping(struct sk_buff *skb,
430+ struct net_device *dev)
431+{
432+ unsigned int queue_index;
433+
434+ /* Don't let queue_mapping be left too large after exiting IMQ */
435+ if (likely(skb->dev != dev && skb->dev != NULL)) {
436+ queue_index = skb_get_queue_mapping(skb);
437+ if (unlikely(queue_index >= skb->dev->real_num_tx_queues)) {
438+ queue_index = (u16)((u32)queue_index %
439+ skb->dev->real_num_tx_queues);
440+ skb_set_queue_mapping(skb, queue_index);
441+ }
442+ } else {
443+ /* skb->dev was IMQ device itself or NULL, be on safe side and
444+ * just clear queue mapping.
445+ */
446+ skb_set_queue_mapping(skb, 0);
447+ }
448+}
449+
450+static netdev_tx_t imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
451+{
452+ struct nf_queue_entry *entry = skb->nf_queue_entry;
453+
454+ skb->nf_queue_entry = NULL;
455+ dev->trans_start = jiffies;
456+
457+ dev->stats.tx_bytes += skb->len;
458+ dev->stats.tx_packets++;
459+
460+ if (unlikely(entry == NULL)) {
461+ /* We don't know what is going on here.. packet is queued for
462+ * imq device, but (probably) not by us.
463+ *
464+ * If this packet was not send here by imq_nf_queue(), then
465+ * skb_save_cb() was not used and skb_free() should not show:
466+ * WARNING: IMQ: kfree_skb: skb->cb_next:..
467+ * and/or
468+ * WARNING: IMQ: kfree_skb: skb->nf_queue_entry...
469+ *
470+ * However if this message is shown, then IMQ is somehow broken
471+ * and you should report this to linuximq.net.
472+ */
473+
474+ /* imq_dev_xmit is black hole that eats all packets, report that
475+ * we eat this packet happily and increase dropped counters.
476+ */
477+
478+ dev->stats.tx_dropped++;
479+ dev_kfree_skb(skb);
480+
481+ return NETDEV_TX_OK;
482+ }
483+
484+ skb_restore_cb(skb); /* restore skb->cb */
485+
486+ skb->imq_flags = 0;
487+ skb->destructor = NULL;
488+
489+ imq_done_check_queue_mapping(skb, dev);
490+
491+ nf_reinject(entry, NF_ACCEPT);
492+
493+ return NETDEV_TX_OK;
494+}
495+
496+static struct net_device *get_imq_device_by_index(int index)
497+{
498+ struct net_device *dev = NULL;
499+ struct net *net;
500+ char buf[8];
501+
502+ /* get device by name and cache result */
503+ snprintf(buf, sizeof(buf), "imq%d", index);
504+
505+ /* Search device from all namespaces. */
506+ for_each_net(net) {
507+ dev = dev_get_by_name(net, buf);
508+ if (dev)
509+ break;
510+ }
511+
512+ if (WARN_ON_ONCE(dev == NULL)) {
513+ /* IMQ device not found. Exotic config? */
514+ return ERR_PTR(-ENODEV);
515+ }
516+
517+ imq_devs_cache[index] = dev;
518+ dev_put(dev);
519+
520+ return dev;
521+}
522+
523+static struct nf_queue_entry *nf_queue_entry_dup(struct nf_queue_entry *e)
524+{
525+ struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
526+ if (entry) {
527+ if (nf_queue_entry_get_refs(entry))
528+ return entry;
529+ kfree(entry);
530+ }
531+ return NULL;
532+}
533+
534+#ifdef CONFIG_BRIDGE_NETFILTER
535+/* When called from bridge netfilter, skb->data must point to MAC header
536+ * before calling skb_gso_segment(). Else, original MAC header is lost
537+ * and segmented skbs will be sent to wrong destination.
538+ */
539+static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
540+{
541+ if (skb->nf_bridge)
542+ __skb_push(skb, skb->network_header - skb->mac_header);
543+}
544+
545+static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
546+{
547+ if (skb->nf_bridge)
548+ __skb_pull(skb, skb->network_header - skb->mac_header);
549+}
550+#else
551+#define nf_bridge_adjust_skb_data(s) do {} while (0)
552+#define nf_bridge_adjust_segmented_data(s) do {} while (0)
553+#endif
554+
555+static void free_entry(struct nf_queue_entry *entry)
556+{
557+ nf_queue_entry_release_refs(entry);
558+ kfree(entry);
559+}
560+
561+static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev);
562+
563+static int __imq_nf_queue_gso(struct nf_queue_entry *entry,
564+ struct net_device *dev, struct sk_buff *skb)
565+{
566+ int ret = -ENOMEM;
567+ struct nf_queue_entry *entry_seg;
568+
569+ nf_bridge_adjust_segmented_data(skb);
570+
571+ if (skb->next == NULL) { /* last packet, no need to copy entry */
572+ struct sk_buff *gso_skb = entry->skb;
573+ entry->skb = skb;
574+ ret = __imq_nf_queue(entry, dev);
575+ if (ret)
576+ entry->skb = gso_skb;
577+ return ret;
578+ }
579+
580+ skb->next = NULL;
581+
582+ entry_seg = nf_queue_entry_dup(entry);
583+ if (entry_seg) {
584+ entry_seg->skb = skb;
585+ ret = __imq_nf_queue(entry_seg, dev);
586+ if (ret)
587+ free_entry(entry_seg);
588+ }
589+ return ret;
590+}
591+
592+static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num)
593+{
594+ struct sk_buff *skb, *segs;
595+ struct net_device *dev;
596+ unsigned int queued;
597+ int index, retval, err;
598+
599+ index = entry->skb->imq_flags & IMQ_F_IFMASK;
600+ if (unlikely(index > numdevs - 1)) {
601+ if (net_ratelimit())
602+ pr_warn("IMQ: invalid device specified, highest is %u\n",
603+ numdevs - 1);
604+ retval = -EINVAL;
605+ goto out_no_dev;
606+ }
607+
608+ /* check for imq device by index from cache */
609+ dev = imq_devs_cache[index];
610+ if (unlikely(!dev)) {
611+ dev = get_imq_device_by_index(index);
612+ if (IS_ERR(dev)) {
613+ retval = PTR_ERR(dev);
614+ goto out_no_dev;
615+ }
616+ }
617+
618+ if (unlikely(!(dev->flags & IFF_UP))) {
619+ entry->skb->imq_flags = 0;
620+ retval = -ECANCELED;
621+ goto out_no_dev;
622+ }
623+
624+ if (!skb_is_gso(entry->skb))
625+ return __imq_nf_queue(entry, dev);
626+
627+ /* Since 3.10.x, GSO handling moved here as result of upstream commit
628+ * a5fedd43d5f6c94c71053a66e4c3d2e35f1731a2 (netfilter: move
629+ * skb_gso_segment into nfnetlink_queue module).
630+ *
631+ * Following code replicates the gso handling from
632+ * 'net/netfilter/nfnetlink_queue_core.c':nfqnl_enqueue_packet().
633+ */
634+
635+ skb = entry->skb;
636+
637+ switch (entry->pf) {
638+ case NFPROTO_IPV4:
639+ skb->protocol = htons(ETH_P_IP);
640+ break;
641+ case NFPROTO_IPV6:
642+ skb->protocol = htons(ETH_P_IPV6);
643+ break;
644+ }
645+
646+ nf_bridge_adjust_skb_data(skb);
647+ segs = skb_gso_segment(skb, 0);
648+ /* Does not use PTR_ERR to limit the number of error codes that can be
649+ * returned by nf_queue. For instance, callers rely on -ECANCELED to
650+ * mean 'ignore this hook'.
651+ */
652+ err = -ENOBUFS;
653+ if (IS_ERR(segs))
654+ goto out_err;
655+ queued = 0;
656+ err = 0;
657+ do {
658+ struct sk_buff *nskb = segs->next;
659+ if (nskb && nskb->next)
660+ nskb->cb_next = NULL;
661+ if (err == 0)
662+ err = __imq_nf_queue_gso(entry, dev, segs);
663+ if (err == 0)
664+ queued++;
665+ else
666+ kfree_skb(segs);
667+ segs = nskb;
668+ } while (segs);
669+
670+ if (queued) {
671+ if (err) /* some segments are already queued */
672+ free_entry(entry);
673+ kfree_skb(skb);
674+ return 0;
675+ }
676+
677+out_err:
678+ nf_bridge_adjust_segmented_data(skb);
679+ retval = err;
680+out_no_dev:
681+ return retval;
682+}
683+
684+static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev)
685+{
686+ struct sk_buff *skb_orig, *skb, *skb_shared;
687+ struct Qdisc *q;
688+ struct netdev_queue *txq;
689+ spinlock_t *root_lock;
690+ int users;
691+ int retval = -EINVAL;
692+ unsigned int orig_queue_index;
693+
694+ dev->last_rx = jiffies;
695+
696+ skb = entry->skb;
697+ skb_orig = NULL;
698+
699+ /* skb has owner? => make clone */
700+ if (unlikely(skb->destructor)) {
701+ skb_orig = skb;
702+ skb = skb_clone(skb, GFP_ATOMIC);
703+ if (unlikely(!skb)) {
704+ retval = -ENOMEM;
705+ goto out;
706+ }
707+ skb->cb_next = NULL;
708+ entry->skb = skb;
709+ }
710+
711+ skb->nf_queue_entry = entry;
712+
713+ dev->stats.rx_bytes += skb->len;
714+ dev->stats.rx_packets++;
715+
716+ if (!skb->dev) {
717+ /* skb->dev == NULL causes problems, try the find cause. */
718+ if (net_ratelimit()) {
719+ dev_warn(&dev->dev,
720+ "received packet with skb->dev == NULL\n");
721+ dump_stack();
722+ }
723+
724+ skb->dev = dev;
725+ }
726+
727+ /* Disables softirqs for lock below */
728+ rcu_read_lock_bh();
729+
730+ /* Multi-queue selection */
731+ orig_queue_index = skb_get_queue_mapping(skb);
732+ txq = imq_select_queue(dev, skb);
733+
734+ q = rcu_dereference(txq->qdisc);
735+ if (unlikely(!q->enqueue))
736+ goto packet_not_eaten_by_imq_dev;
737+
738+ root_lock = qdisc_lock(q);
739+ spin_lock(root_lock);
740+
741+ users = atomic_read(&skb->users);
742+
743+ skb_shared = skb_get(skb); /* increase reference count by one */
744+
745+ /* backup skb->cb, as qdisc layer will overwrite it */
746+ skb_save_cb(skb_shared);
747+ qdisc_enqueue_root(skb_shared, q); /* might kfree_skb */
748+
749+ if (likely(atomic_read(&skb_shared->users) == users + 1)) {
750+ kfree_skb(skb_shared); /* decrease reference count by one */
751+
752+ skb->destructor = &imq_skb_destructor;
753+
754+ /* cloned? */
755+ if (unlikely(skb_orig))
756+ kfree_skb(skb_orig); /* free original */
757+
758+ spin_unlock(root_lock);
759+ rcu_read_unlock_bh();
760+
761+ /* schedule qdisc dequeue */
762+ __netif_schedule(q);
763+
764+ retval = 0;
765+ goto out;
766+ } else {
767+ skb_restore_cb(skb_shared); /* restore skb->cb */
768+ skb->nf_queue_entry = NULL;
769+ /*
770+ * qdisc dropped packet and decreased skb reference count of
771+ * skb, so we don't really want to and try refree as that would
772+ * actually destroy the skb.
773+ */
774+ spin_unlock(root_lock);
775+ goto packet_not_eaten_by_imq_dev;
776+ }
777+
778+packet_not_eaten_by_imq_dev:
779+ skb_set_queue_mapping(skb, orig_queue_index);
780+ rcu_read_unlock_bh();
781+
782+ /* cloned? restore original */
783+ if (unlikely(skb_orig)) {
784+ kfree_skb(skb);
785+ entry->skb = skb_orig;
786+ }
787+ retval = -1;
788+out:
789+ return retval;
790+}
791+
792+static unsigned int imq_nf_hook(unsigned int hook, struct sk_buff *pskb,
793+ const struct net_device *indev,
794+ const struct net_device *outdev,
795+ int (*okfn)(struct sk_buff *))
796+{
797+ return (pskb->imq_flags & IMQ_F_ENQUEUE) ? NF_IMQ_QUEUE : NF_ACCEPT;
798+}
799+
800+static int imq_close(struct net_device *dev)
801+{
802+ netif_stop_queue(dev);
803+ return 0;
804+}
805+
806+static int imq_open(struct net_device *dev)
807+{
808+ netif_start_queue(dev);
809+ return 0;
810+}
811+
812+static const struct net_device_ops imq_netdev_ops = {
813+ .ndo_open = imq_open,
814+ .ndo_stop = imq_close,
815+ .ndo_start_xmit = imq_dev_xmit,
816+ .ndo_get_stats = imq_get_stats,
817+};
818+
819+static void imq_setup(struct net_device *dev)
820+{
821+ dev->netdev_ops = &imq_netdev_ops;
822+ dev->type = ARPHRD_VOID;
823+ dev->mtu = 16000; /* too small? */
824+ dev->tx_queue_len = 11000; /* too big? */
825+ dev->flags = IFF_NOARP;
826+ dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
827+ NETIF_F_GSO | NETIF_F_HW_CSUM |
828+ NETIF_F_HIGHDMA;
829+ dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE |
830+ IFF_TX_SKB_SHARING);
831+}
832+
833+static int imq_validate(struct nlattr *tb[], struct nlattr *data[])
834+{
835+ int ret = 0;
836+
837+ if (tb[IFLA_ADDRESS]) {
838+ if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
839+ ret = -EINVAL;
840+ goto end;
841+ }
842+ if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
843+ ret = -EADDRNOTAVAIL;
844+ goto end;
845+ }
846+ }
847+ return 0;
848+end:
849+ pr_warn("IMQ: imq_validate failed (%d)\n", ret);
850+ return ret;
851+}
852+
853+static struct rtnl_link_ops imq_link_ops __read_mostly = {
854+ .kind = "imq",
855+ .priv_size = 0,
856+ .setup = imq_setup,
857+ .validate = imq_validate,
858+};
859+
860+static const struct nf_queue_handler imq_nfqh = {
861+ .outfn = imq_nf_queue,
862+};
863+
864+static int __init imq_init_hooks(void)
865+{
866+ int ret;
867+
868+ nf_register_queue_imq_handler(&imq_nfqh);
869+
870+ ret = nf_register_hooks(imq_ops, ARRAY_SIZE(imq_ops));
871+ if (ret < 0)
872+ nf_unregister_queue_imq_handler();
873+
874+ return ret;
875+}
876+
877+static int __init imq_init_one(int index)
878+{
879+ struct net_device *dev;
880+ int ret;
881+
882+ dev = alloc_netdev_mq(0, "imq%d", imq_setup, numqueues);
883+ if (!dev)
884+ return -ENOMEM;
885+
886+ ret = dev_alloc_name(dev, dev->name);
887+ if (ret < 0)
888+ goto fail;
889+
890+ dev->rtnl_link_ops = &imq_link_ops;
891+ ret = register_netdevice(dev);
892+ if (ret < 0)
893+ goto fail;
894+
895+ return 0;
896+fail:
897+ free_netdev(dev);
898+ return ret;
899+}
900+
901+static int __init imq_init_devs(void)
902+{
903+ int err, i;
904+
905+ if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) {
906+ pr_err("IMQ: numdevs has to be betweed 1 and %u\n",
907+ IMQ_MAX_DEVS);
908+ return -EINVAL;
909+ }
910+
911+ if (numqueues < 1 || numqueues > IMQ_MAX_QUEUES) {
912+ pr_err("IMQ: numqueues has to be betweed 1 and %u\n",
913+ IMQ_MAX_QUEUES);
914+ return -EINVAL;
915+ }
916+
917+ get_random_bytes(&imq_hashrnd, sizeof(imq_hashrnd));
918+
919+ rtnl_lock();
920+ err = __rtnl_link_register(&imq_link_ops);
921+
922+ for (i = 0; i < numdevs && !err; i++)
923+ err = imq_init_one(i);
924+
925+ if (err) {
926+ __rtnl_link_unregister(&imq_link_ops);
927+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
928+ }
929+ rtnl_unlock();
930+
931+ return err;
932+}
933+
934+static int __init imq_init_module(void)
935+{
936+ int err;
937+
938+#if defined(CONFIG_IMQ_NUM_DEVS)
939+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS > 16);
940+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS < 2);
941+ BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS - 1 > IMQ_F_IFMASK);
942+#endif
943+
944+ err = imq_init_devs();
945+ if (err) {
946+ pr_err("IMQ: Error trying imq_init_devs(net)\n");
947+ return err;
948+ }
949+
950+ err = imq_init_hooks();
951+ if (err) {
952+ pr_err(KERN_ERR "IMQ: Error trying imq_init_hooks()\n");
953+ rtnl_link_unregister(&imq_link_ops);
954+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
955+ return err;
956+ }
957+
958+ pr_info("IMQ driver loaded successfully. (numdevs = %d, numqueues = %d)\n",
959+ numdevs, numqueues);
960+
961+#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
962+ pr_info("\tHooking IMQ before NAT on PREROUTING.\n");
963+#else
964+ pr_info("\tHooking IMQ after NAT on PREROUTING.\n");
965+#endif
966+#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
967+ pr_info("\tHooking IMQ before NAT on POSTROUTING.\n");
968+#else
969+ pr_info("\tHooking IMQ after NAT on POSTROUTING.\n");
970+#endif
971+
972+ return 0;
973+}
974+
975+static void __exit imq_unhook(void)
976+{
977+ nf_unregister_hooks(imq_ops, ARRAY_SIZE(imq_ops));
978+ nf_unregister_queue_imq_handler();
979+}
980+
981+static void __exit imq_cleanup_devs(void)
982+{
983+ rtnl_link_unregister(&imq_link_ops);
984+ memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
985+}
986+
987+static void __exit imq_exit_module(void)
988+{
989+ imq_unhook();
990+ imq_cleanup_devs();
991+ pr_info("IMQ driver unloaded successfully.\n");
992+}
993+
994+module_init(imq_init_module);
995+module_exit(imq_exit_module);
996+
997+module_param(numdevs, int, 0);
998+module_param(numqueues, int, 0);
999+MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will be created)");
1000+MODULE_PARM_DESC(numqueues, "number of queues per IMQ device");
1001+MODULE_AUTHOR("http://www.linuximq.net");
1002+MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
1003+MODULE_LICENSE("GPL");
1004+MODULE_ALIAS_RTNL_LINK("imq");
1005+
1006diff -Naur linux-3.10.30.org/drivers/net/Kconfig linux-3.10.30/drivers/net/Kconfig
1007--- linux-3.10.30.org/drivers/net/Kconfig 2014-02-13 22:48:15.000000000 +0100
1008+++ linux-3.10.30/drivers/net/Kconfig 2014-02-14 20:29:05.379402305 +0100
1009@@ -207,6 +207,125 @@
1010 depends on RIONET
1011 default "128"
1012
1013+config IMQ
1014+ tristate "IMQ (intermediate queueing device) support"
1015+ depends on NETDEVICES && NETFILTER
1016+ ---help---
1017+ The IMQ device(s) is used as placeholder for QoS queueing
1018+ disciplines. Every packet entering/leaving the IP stack can be
1019+ directed through the IMQ device where it's enqueued/dequeued to the
1020+ attached qdisc. This allows you to treat network devices as classes
1021+ and distribute bandwidth among them. Iptables is used to specify
1022+ through which IMQ device, if any, packets travel.
1023+
1024+ More information at: http://www.linuximq.net/
1025+
1026+ To compile this driver as a module, choose M here: the module
1027+ will be called imq. If unsure, say N.
1028+
1029+choice
1030+ prompt "IMQ behavior (PRE/POSTROUTING)"
1031+ depends on IMQ
1032+ default IMQ_BEHAVIOR_AB
1033+ help
1034+ This setting defines how IMQ behaves in respect to its
1035+ hooking in PREROUTING and POSTROUTING.
1036+
1037+ IMQ can work in any of the following ways:
1038+
1039+ PREROUTING | POSTROUTING
1040+ -----------------|-------------------
1041+ #1 After NAT | After NAT
1042+ #2 After NAT | Before NAT
1043+ #3 Before NAT | After NAT
1044+ #4 Before NAT | Before NAT
1045+
1046+ The default behavior is to hook before NAT on PREROUTING
1047+ and after NAT on POSTROUTING (#3).
1048+
1049+ This settings are specially usefull when trying to use IMQ
1050+ to shape NATed clients.
1051+
1052+ More information can be found at: www.linuximq.net
1053+
1054+ If not sure leave the default settings alone.
1055+
1056+config IMQ_BEHAVIOR_AA
1057+ bool "IMQ AA"
1058+ help
1059+ This setting defines how IMQ behaves in respect to its
1060+ hooking in PREROUTING and POSTROUTING.
1061+
1062+ Choosing this option will make IMQ hook like this:
1063+
1064+ PREROUTING: After NAT
1065+ POSTROUTING: After NAT
1066+
1067+ More information can be found at: www.linuximq.net
1068+
1069+ If not sure leave the default settings alone.
1070+
1071+config IMQ_BEHAVIOR_AB
1072+ bool "IMQ AB"
1073+ help
1074+ This setting defines how IMQ behaves in respect to its
1075+ hooking in PREROUTING and POSTROUTING.
1076+
1077+ Choosing this option will make IMQ hook like this:
1078+
1079+ PREROUTING: After NAT
1080+ POSTROUTING: Before NAT
1081+
1082+ More information can be found at: www.linuximq.net
1083+
1084+ If not sure leave the default settings alone.
1085+
1086+config IMQ_BEHAVIOR_BA
1087+ bool "IMQ BA"
1088+ help
1089+ This setting defines how IMQ behaves in respect to its
1090+ hooking in PREROUTING and POSTROUTING.
1091+
1092+ Choosing this option will make IMQ hook like this:
1093+
1094+ PREROUTING: Before NAT
1095+ POSTROUTING: After NAT
1096+
1097+ More information can be found at: www.linuximq.net
1098+
1099+ If not sure leave the default settings alone.
1100+
1101+config IMQ_BEHAVIOR_BB
1102+ bool "IMQ BB"
1103+ help
1104+ This setting defines how IMQ behaves in respect to its
1105+ hooking in PREROUTING and POSTROUTING.
1106+
1107+ Choosing this option will make IMQ hook like this:
1108+
1109+ PREROUTING: Before NAT
1110+ POSTROUTING: Before NAT
1111+
1112+ More information can be found at: www.linuximq.net
1113+
1114+ If not sure leave the default settings alone.
1115+
1116+endchoice
1117+
1118+config IMQ_NUM_DEVS
1119+ int "Number of IMQ devices"
1120+ range 2 16
1121+ depends on IMQ
1122+ default "16"
1123+ help
1124+ This setting defines how many IMQ devices will be created.
1125+
1126+ The default value is 16.
1127+
1128+ More information can be found at: www.linuximq.net
1129+
1130+ If not sure leave the default settings alone.
1131+
1132 config TUN
1133 tristate "Universal TUN/TAP device driver support"
1134 select CRC32
1135diff -Naur linux-3.10.30.org/drivers/net/Makefile linux-3.10.30/drivers/net/Makefile
1136--- linux-3.10.30.org/drivers/net/Makefile 2014-02-13 22:48:15.000000000 +0100
1137+++ linux-3.10.30/drivers/net/Makefile 2014-02-14 20:29:05.379402305 +0100
1138@@ -9,6 +9,7 @@
1139 obj-$(CONFIG_DUMMY) += dummy.o
1140 obj-$(CONFIG_EQUALIZER) += eql.o
1141 obj-$(CONFIG_IFB) += ifb.o
1142+obj-$(CONFIG_IMQ) += imq.o
1143 obj-$(CONFIG_MACVLAN) += macvlan.o
1144 obj-$(CONFIG_MACVTAP) += macvtap.o
1145 obj-$(CONFIG_MII) += mii.o
1146diff -Naur linux-3.10.30.org/include/linux/imq.h linux-3.10.30/include/linux/imq.h
1147--- linux-3.10.30.org/include/linux/imq.h 1970-01-01 01:00:00.000000000 +0100
1148+++ linux-3.10.30/include/linux/imq.h 2014-02-14 20:29:05.379402305 +0100
1149@@ -0,0 +1,13 @@
1150+#ifndef _IMQ_H
1151+#define _IMQ_H
1152+
1153+/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
1154+#define IMQ_F_BITS 5
1155+
1156+#define IMQ_F_IFMASK 0x0f
1157+#define IMQ_F_ENQUEUE 0x10
1158+
1159+#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1)
1160+
1161+#endif /* _IMQ_H */
1162+
1163diff -Naur linux-3.10.30.org/include/linux/netfilter/xt_IMQ.h linux-3.10.30/include/linux/netfilter/xt_IMQ.h
1164--- linux-3.10.30.org/include/linux/netfilter/xt_IMQ.h 1970-01-01 01:00:00.000000000 +0100
1165+++ linux-3.10.30/include/linux/netfilter/xt_IMQ.h 2014-02-14 20:29:05.379402305 +0100
1166@@ -0,0 +1,9 @@
1167+#ifndef _XT_IMQ_H
1168+#define _XT_IMQ_H
1169+
1170+struct xt_imq_info {
1171+ unsigned int todev; /* target imq device */
1172+};
1173+
1174+#endif /* _XT_IMQ_H */
1175+
1176diff -Naur linux-3.10.30.org/include/linux/netfilter_ipv4/ipt_IMQ.h linux-3.10.30/include/linux/netfilter_ipv4/ipt_IMQ.h
1177--- linux-3.10.30.org/include/linux/netfilter_ipv4/ipt_IMQ.h 1970-01-01 01:00:00.000000000 +0100
1178+++ linux-3.10.30/include/linux/netfilter_ipv4/ipt_IMQ.h 2014-02-14 20:29:05.379402305 +0100
1179@@ -0,0 +1,10 @@
1180+#ifndef _IPT_IMQ_H
1181+#define _IPT_IMQ_H
1182+
1183+/* Backwards compatibility for old userspace */
1184+#include <linux/netfilter/xt_IMQ.h>
1185+
1186+#define ipt_imq_info xt_imq_info
1187+
1188+#endif /* _IPT_IMQ_H */
1189+
1190diff -Naur linux-3.10.30.org/include/linux/netfilter_ipv6/ip6t_IMQ.h linux-3.10.30/include/linux/netfilter_ipv6/ip6t_IMQ.h
1191--- linux-3.10.30.org/include/linux/netfilter_ipv6/ip6t_IMQ.h 1970-01-01 01:00:00.000000000 +0100
1192+++ linux-3.10.30/include/linux/netfilter_ipv6/ip6t_IMQ.h 2014-02-14 20:29:05.379402305 +0100
1193@@ -0,0 +1,10 @@
1194+#ifndef _IP6T_IMQ_H
1195+#define _IP6T_IMQ_H
1196+
1197+/* Backwards compatibility for old userspace */
1198+#include <linux/netfilter/xt_IMQ.h>
1199+
1200+#define ip6t_imq_info xt_imq_info
1201+
1202+#endif /* _IP6T_IMQ_H */
1203+
1204diff -Naur linux-3.10.30.org/include/linux/skbuff.h linux-3.10.30/include/linux/skbuff.h
1205--- linux-3.10.30.org/include/linux/skbuff.h 2014-02-13 22:48:15.000000000 +0100
1206+++ linux-3.10.30/include/linux/skbuff.h 2014-02-14 20:29:05.379402305 +0100
1207@@ -33,6 +33,9 @@
1208 #include <linux/dma-mapping.h>
1209 #include <linux/netdev_features.h>
1210 #include <net/flow_keys.h>
1211+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1212+#include <linux/imq.h>
1213+#endif
1214
1215 /* Don't change this without changing skb_csum_unnecessary! */
1216 #define CHECKSUM_NONE 0
1217@@ -414,6 +417,9 @@
1218 * first. This is owned by whoever has the skb queued ATM.
1219 */
1220 char cb[48] __aligned(8);
1221+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1222+ void *cb_next;
1223+#endif
1224
1225 unsigned long _skb_refdst;
1226 #ifdef CONFIG_XFRM
1227@@ -449,6 +455,9 @@
1228 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1229 struct nf_conntrack *nfct;
1230 #endif
1231+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1232+ struct nf_queue_entry *nf_queue_entry;
1233+#endif
1234 #ifdef CONFIG_BRIDGE_NETFILTER
1235 struct nf_bridge_info *nf_bridge;
1236 #endif
1237@@ -487,7 +496,9 @@
1238 __u8 encapsulation:1;
1239 /* 7/9 bit hole (depending on ndisc_nodetype presence) */
1240 kmemcheck_bitfield_end(flags2);
1241-
1242+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1243+ __u8 imq_flags:IMQ_F_BITS;
1244+#endif
1245 #ifdef CONFIG_NET_DMA
1246 dma_cookie_t dma_cookie;
1247 #endif
1248@@ -616,7 +627,10 @@
1249 {
1250 return (struct rtable *)skb_dst(skb);
1251 }
1252-
1253+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1254+extern int skb_save_cb(struct sk_buff *skb);
1255+extern int skb_restore_cb(struct sk_buff *skb);
1256+#endif
1257 extern void kfree_skb(struct sk_buff *skb);
1258 extern void kfree_skb_list(struct sk_buff *segs);
1259 extern void skb_tx_error(struct sk_buff *skb);
1260@@ -2735,6 +2749,10 @@
1261 nf_conntrack_get(src->nfct);
1262 dst->nfctinfo = src->nfctinfo;
1263 #endif
1264+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1265+ dst->imq_flags = src->imq_flags;
1266+ dst->nf_queue_entry = src->nf_queue_entry;
1267+#endif
1268 #ifdef CONFIG_BRIDGE_NETFILTER
1269 dst->nf_bridge = src->nf_bridge;
1270 nf_bridge_get(src->nf_bridge);
1271diff -Naur linux-3.10.30.org/include/net/netfilter/nf_queue.h linux-3.10.30/include/net/netfilter/nf_queue.h
1272--- linux-3.10.30.org/include/net/netfilter/nf_queue.h 2014-02-13 22:48:15.000000000 +0100
1273+++ linux-3.10.30/include/net/netfilter/nf_queue.h 2014-02-14 20:29:05.382736249 +0100
1274@@ -29,6 +29,12 @@
1275 void nf_register_queue_handler(const struct nf_queue_handler *qh);
1276 void nf_unregister_queue_handler(void);
1277 extern void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
1278+extern void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
1279+
1280+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1281+extern void nf_register_queue_imq_handler(const struct nf_queue_handler *qh);
1282+extern void nf_unregister_queue_imq_handler(void);
1283+#endif
1284
1285 bool nf_queue_entry_get_refs(struct nf_queue_entry *entry);
1286 void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
1287diff -Naur linux-3.10.30.org/include/uapi/linux/netfilter.h linux-3.10.30/include/uapi/linux/netfilter.h
1288--- linux-3.10.30.org/include/uapi/linux/netfilter.h 2014-02-13 22:48:15.000000000 +0100
1289+++ linux-3.10.30/include/uapi/linux/netfilter.h 2014-02-14 20:29:05.382736249 +0100
1290@@ -13,7 +13,8 @@
1291 #define NF_QUEUE 3
1292 #define NF_REPEAT 4
1293 #define NF_STOP 5
1294-#define NF_MAX_VERDICT NF_STOP
1295+#define NF_IMQ_QUEUE 6
1296+#define NF_MAX_VERDICT NF_IMQ_QUEUE
1297
1298 /* we overload the higher bits for encoding auxiliary data such as the queue
1299 * number or errno values. Not nice, but better than additional function
1300diff -Naur linux-3.10.30.org/net/core/dev.c linux-3.10.30/net/core/dev.c
1301--- linux-3.10.30.org/net/core/dev.c 2014-02-13 22:48:15.000000000 +0100
1302+++ linux-3.10.30/net/core/dev.c 2014-02-14 20:29:05.382736249 +0100
1303@@ -129,6 +129,9 @@
1304 #include <linux/inetdevice.h>
1305 #include <linux/cpu_rmap.h>
1306 #include <linux/static_key.h>
1307+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1308+#include <linux/imq.h>
1309+#endif
1310
1311 #include "net-sysfs.h"
1312
1313@@ -2573,7 +2576,12 @@
1314 }
1315 }
1316
1317+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1318+ if (!list_empty(&ptype_all) &&
1319+ !(skb->imq_flags & IMQ_F_ENQUEUE))
1320+#else
1321 if (!list_empty(&ptype_all))
1322+#endif
1323 dev_queue_xmit_nit(skb, dev);
1324
1325 skb_len = skb->len;
1326diff -Naur linux-3.10.30.org/net/core/skbuff.c linux-3.10.30/net/core/skbuff.c
1327--- linux-3.10.30.org/net/core/skbuff.c 2014-02-13 22:48:15.000000000 +0100
1328+++ linux-3.10.30/net/core/skbuff.c 2014-02-14 21:47:17.286039229 +0100
1329@@ -73,6 +73,9 @@
1330
1331 struct kmem_cache *skbuff_head_cache __read_mostly;
1332 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
1333+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1334+static struct kmem_cache *skbuff_cb_store_cache __read_mostly;
1335+#endif
1336
1337 /**
1338 * skb_panic - private function for out-of-line support
1339@@ -552,6 +555,29 @@
1340 WARN_ON(in_irq());
1341 skb->destructor(skb);
1342 }
1343+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1344+ /*
1345+ * This should not happen. When it does, avoid memleak by restoring
1346+ * the chain of cb-backups.
1347+ */
1348+ while (skb->cb_next != NULL) {
1349+ if (net_ratelimit())
1350+ pr_warn("IMQ: kfree_skb: skb->cb_next: %08x\n",
1351+ (unsigned int)skb->cb_next);
1352+
1353+ skb_restore_cb(skb);
1354+ }
1355+ /*
1356+ * This should not happen either, nf_queue_entry is nullified in
1357+ * imq_dev_xmit(). If we have non-NULL nf_queue_entry then we are
1358+ * leaking entry pointers, maybe memory. We don't know if this is
1359+ * pointer to already freed memory, or should this be freed.
1360+ * If this happens we need to add refcounting, etc for nf_queue_entry.
1361+ */
1362+ if (skb->nf_queue_entry && net_ratelimit())
1363+ pr_warn("%s\n", "IMQ: kfree_skb: skb->nf_queue_entry != NULL");
1364+#endif
1365+
1366 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1367 nf_conntrack_put(skb->nfct);
1368 #endif
1369@@ -683,6 +709,10 @@
1370 new->sp = secpath_get(old->sp);
1371 #endif
1372 memcpy(new->cb, old->cb, sizeof(old->cb));
1373+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1374+ new->cb_next = NULL;
1375+ /*skb_copy_stored_cb(new, old);*/
1376+#endif
1377 new->csum = old->csum;
1378 new->local_df = old->local_df;
1379 new->pkt_type = old->pkt_type;
1380@@ -3050,6 +3080,15 @@
1381 }
1382 EXPORT_SYMBOL_GPL(skb_gro_receive);
1383
1384+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1385+/* Control buffer save/restore for IMQ devices */
1386+struct skb_cb_table {
1387+ char cb[48] __aligned(8);
1388+ void *cb_next;
1389+ atomic_t refcnt;
1390+};
1391+#endif
1392+
1393 void __init skb_init(void)
1394 {
1395 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1396@@ -3063,6 +3102,13 @@
1397 0,
1398 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1399 NULL);
1400+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1401+ skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache",
1402+ sizeof(struct skb_cb_table),
1403+ 0,
1404+ SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1405+ NULL);
1406+#endif
1407 }
1408
1409 /**
1410@@ -3348,6 +3394,76 @@
1411 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
1412
1413
1414+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1415+static DEFINE_SPINLOCK(skb_cb_store_lock);
1416+
1417+int skb_save_cb(struct sk_buff *skb)
1418+{
1419+ struct skb_cb_table *next;
1420+
1421+ next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
1422+ if (!next)
1423+ return -ENOMEM;
1424+
1425+ BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1426+
1427+ memcpy(next->cb, skb->cb, sizeof(skb->cb));
1428+ next->cb_next = skb->cb_next;
1429+
1430+ atomic_set(&next->refcnt, 1);
1431+
1432+ skb->cb_next = next;
1433+ return 0;
1434+}
1435+EXPORT_SYMBOL(skb_save_cb);
1436+
1437+int skb_restore_cb(struct sk_buff *skb)
1438+{
1439+ struct skb_cb_table *next;
1440+
1441+ if (!skb->cb_next)
1442+ return 0;
1443+
1444+ next = skb->cb_next;
1445+
1446+ BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1447+
1448+ memcpy(skb->cb, next->cb, sizeof(skb->cb));
1449+ skb->cb_next = next->cb_next;
1450+
1451+ spin_lock(&skb_cb_store_lock);
1452+
1453+ if (atomic_dec_and_test(&next->refcnt))
1454+ kmem_cache_free(skbuff_cb_store_cache, next);
1455+
1456+ spin_unlock(&skb_cb_store_lock);
1457+
1458+ return 0;
1459+}
1460+EXPORT_SYMBOL(skb_restore_cb);
1461+
1462+static void skb_copy_stored_cb(struct sk_buff *new, const struct sk_buff *__old)
1463+{
1464+ struct skb_cb_table *next;
1465+ struct sk_buff *old;
1466+
1467+ if (!__old->cb_next) {
1468+ new->cb_next = NULL;
1469+ return;
1470+ }
1471+
1472+ spin_lock(&skb_cb_store_lock);
1473+
1474+ old = (struct sk_buff *)__old;
1475+
1476+ next = old->cb_next;
1477+ atomic_inc(&next->refcnt);
1478+ new->cb_next = next;
1479+
1480+ spin_unlock(&skb_cb_store_lock);
1481+}
1482+#endif
1483+
1484 /**
1485 * skb_partial_csum_set - set up and verify partial csum values for packet
1486 * @skb: the skb to set
1487diff -Naur linux-3.10.30.org/net/ipv6/ip6_output.c linux-3.10.30/net/ipv6/ip6_output.c
1488--- linux-3.10.30.org/net/ipv6/ip6_output.c 2014-02-13 22:48:15.000000000 +0100
1489+++ linux-3.10.30/net/ipv6/ip6_output.c 2014-02-14 20:29:05.392738001 +0100
1490@@ -89,9 +89,6 @@
1491 struct in6_addr *nexthop;
1492 int ret;
1493
1494- skb->protocol = htons(ETH_P_IPV6);
1495- skb->dev = dev;
1496-
1497 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
1498 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
1499
1500@@ -168,6 +165,13 @@
1501 return 0;
1502 }
1503
1504+ /*
1505+ * IMQ-patch: moved setting skb->dev and skb->protocol from
1506+ * ip6_finish_output2 to fix crashing at netif_skb_features().
1507+ */
1508+ skb->protocol = htons(ETH_P_IPV6);
1509+ skb->dev = dev;
1510+
1511 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
1512 ip6_finish_output,
1513 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
1514diff -Naur linux-3.10.30.org/net/ipv6/ip6_output.c.orig linux-3.10.30/net/ipv6/ip6_output.c.orig
1515--- linux-3.10.30.org/net/ipv6/ip6_output.c.orig 1970-01-01 01:00:00.000000000 +0100
1516+++ linux-3.10.30/net/ipv6/ip6_output.c.orig 2014-02-14 20:29:05.392738001 +0100
1517@@ -0,0 +1,1580 @@
1518+/*
1519+ * IPv6 output functions
1520+ * Linux INET6 implementation
1521+ *
1522+ * Authors:
1523+ * Pedro Roque <roque@di.fc.ul.pt>
1524+ *
1525+ * Based on linux/net/ipv4/ip_output.c
1526+ *
1527+ * This program is free software; you can redistribute it and/or
1528+ * modify it under the terms of the GNU General Public License
1529+ * as published by the Free Software Foundation; either version
1530+ * 2 of the License, or (at your option) any later version.
1531+ *
1532+ * Changes:
1533+ * A.N.Kuznetsov : airthmetics in fragmentation.
1534+ * extension headers are implemented.
1535+ * route changes now work.
1536+ * ip6_forward does not confuse sniffers.
1537+ * etc.
1538+ *
1539+ * H. von Brand : Added missing #include <linux/string.h>
1540+ * Imran Patel : frag id should be in NBO
1541+ * Kazunori MIYAZAWA @USAGI
1542+ * : add ip6_append_data and related functions
1543+ * for datagram xmit
1544+ */
1545+
1546+#include <linux/errno.h>
1547+#include <linux/kernel.h>
1548+#include <linux/string.h>
1549+#include <linux/socket.h>
1550+#include <linux/net.h>
1551+#include <linux/netdevice.h>
1552+#include <linux/if_arp.h>
1553+#include <linux/in6.h>
1554+#include <linux/tcp.h>
1555+#include <linux/route.h>
1556+#include <linux/module.h>
1557+#include <linux/slab.h>
1558+
1559+#include <linux/netfilter.h>
1560+#include <linux/netfilter_ipv6.h>
1561+
1562+#include <net/sock.h>
1563+#include <net/snmp.h>
1564+
1565+#include <net/ipv6.h>
1566+#include <net/ndisc.h>
1567+#include <net/protocol.h>
1568+#include <net/ip6_route.h>
1569+#include <net/addrconf.h>
1570+#include <net/rawv6.h>
1571+#include <net/icmp.h>
1572+#include <net/xfrm.h>
1573+#include <net/checksum.h>
1574+#include <linux/mroute6.h>
1575+
1576+int __ip6_local_out(struct sk_buff *skb)
1577+{
1578+ int len;
1579+
1580+ len = skb->len - sizeof(struct ipv6hdr);
1581+ if (len > IPV6_MAXPLEN)
1582+ len = 0;
1583+ ipv6_hdr(skb)->payload_len = htons(len);
1584+
1585+ return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
1586+ skb_dst(skb)->dev, dst_output);
1587+}
1588+
1589+int ip6_local_out(struct sk_buff *skb)
1590+{
1591+ int err;
1592+
1593+ err = __ip6_local_out(skb);
1594+ if (likely(err == 1))
1595+ err = dst_output(skb);
1596+
1597+ return err;
1598+}
1599+EXPORT_SYMBOL_GPL(ip6_local_out);
1600+
1601+static int ip6_finish_output2(struct sk_buff *skb)
1602+{
1603+ struct dst_entry *dst = skb_dst(skb);
1604+ struct net_device *dev = dst->dev;
1605+ struct neighbour *neigh;
1606+ struct in6_addr *nexthop;
1607+ int ret;
1608+
1609+ skb->protocol = htons(ETH_P_IPV6);
1610+ skb->dev = dev;
1611+
1612+ if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
1613+ struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
1614+
1615+ if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(skb->sk) &&
1616+ ((mroute6_socket(dev_net(dev), skb) &&
1617+ !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
1618+ ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
1619+ &ipv6_hdr(skb)->saddr))) {
1620+ struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
1621+
1622+ /* Do not check for IFF_ALLMULTI; multicast routing
1623+ is not supported in any case.
1624+ */
1625+ if (newskb)
1626+ NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
1627+ newskb, NULL, newskb->dev,
1628+ dev_loopback_xmit);
1629+
1630+ if (ipv6_hdr(skb)->hop_limit == 0) {
1631+ IP6_INC_STATS(dev_net(dev), idev,
1632+ IPSTATS_MIB_OUTDISCARDS);
1633+ kfree_skb(skb);
1634+ return 0;
1635+ }
1636+ }
1637+
1638+ IP6_UPD_PO_STATS(dev_net(dev), idev, IPSTATS_MIB_OUTMCAST,
1639+ skb->len);
1640+
1641+ if (IPV6_ADDR_MC_SCOPE(&ipv6_hdr(skb)->daddr) <=
1642+ IPV6_ADDR_SCOPE_NODELOCAL &&
1643+ !(dev->flags & IFF_LOOPBACK)) {
1644+ kfree_skb(skb);
1645+ return 0;
1646+ }
1647+ }
1648+
1649+ rcu_read_lock_bh();
1650+ nexthop = rt6_nexthop((struct rt6_info *)dst);
1651+ neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
1652+ if (unlikely(!neigh))
1653+ neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
1654+ if (!IS_ERR(neigh)) {
1655+ ret = dst_neigh_output(dst, neigh, skb);
1656+ rcu_read_unlock_bh();
1657+ return ret;
1658+ }
1659+ rcu_read_unlock_bh();
1660+
1661+ IP6_INC_STATS(dev_net(dst->dev),
1662+ ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
1663+ kfree_skb(skb);
1664+ return -EINVAL;
1665+}
1666+
1667+static int ip6_finish_output(struct sk_buff *skb)
1668+{
1669+ if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
1670+ dst_allfrag(skb_dst(skb)) ||
1671+ (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size))
1672+ return ip6_fragment(skb, ip6_finish_output2);
1673+ else
1674+ return ip6_finish_output2(skb);
1675+}
1676+
1677+int ip6_output(struct sk_buff *skb)
1678+{
1679+ struct net_device *dev = skb_dst(skb)->dev;
1680+ struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
1681+ if (unlikely(idev->cnf.disable_ipv6)) {
1682+ IP6_INC_STATS(dev_net(dev), idev,
1683+ IPSTATS_MIB_OUTDISCARDS);
1684+ kfree_skb(skb);
1685+ return 0;
1686+ }
1687+
1688+ return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
1689+ ip6_finish_output,
1690+ !(IP6CB(skb)->flags & IP6SKB_REROUTED));
1691+}
1692+
1693+/*
1694+ * xmit an sk_buff (used by TCP, SCTP and DCCP)
1695+ */
1696+
1697+int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
1698+ struct ipv6_txoptions *opt, int tclass)
1699+{
1700+ struct net *net = sock_net(sk);
1701+ struct ipv6_pinfo *np = inet6_sk(sk);
1702+ struct in6_addr *first_hop = &fl6->daddr;
1703+ struct dst_entry *dst = skb_dst(skb);
1704+ struct ipv6hdr *hdr;
1705+ u8 proto = fl6->flowi6_proto;
1706+ int seg_len = skb->len;
1707+ int hlimit = -1;
1708+ u32 mtu;
1709+
1710+ if (opt) {
1711+ unsigned int head_room;
1712+
1713+ /* First: exthdrs may take lots of space (~8K for now)
1714+ MAX_HEADER is not enough.
1715+ */
1716+ head_room = opt->opt_nflen + opt->opt_flen;
1717+ seg_len += head_room;
1718+ head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
1719+
1720+ if (skb_headroom(skb) < head_room) {
1721+ struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
1722+ if (skb2 == NULL) {
1723+ IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
1724+ IPSTATS_MIB_OUTDISCARDS);
1725+ kfree_skb(skb);
1726+ return -ENOBUFS;
1727+ }
1728+ consume_skb(skb);
1729+ skb = skb2;
1730+ skb_set_owner_w(skb, sk);
1731+ }
1732+ if (opt->opt_flen)
1733+ ipv6_push_frag_opts(skb, opt, &proto);
1734+ if (opt->opt_nflen)
1735+ ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
1736+ }
1737+
1738+ skb_push(skb, sizeof(struct ipv6hdr));
1739+ skb_reset_network_header(skb);
1740+ hdr = ipv6_hdr(skb);
1741+
1742+ /*
1743+ * Fill in the IPv6 header
1744+ */
1745+ if (np)
1746+ hlimit = np->hop_limit;
1747+ if (hlimit < 0)
1748+ hlimit = ip6_dst_hoplimit(dst);
1749+
1750+ ip6_flow_hdr(hdr, tclass, fl6->flowlabel);
1751+
1752+ hdr->payload_len = htons(seg_len);
1753+ hdr->nexthdr = proto;
1754+ hdr->hop_limit = hlimit;
1755+
1756+ hdr->saddr = fl6->saddr;
1757+ hdr->daddr = *first_hop;
1758+
1759+ skb->priority = sk->sk_priority;
1760+ skb->mark = sk->sk_mark;
1761+
1762+ mtu = dst_mtu(dst);
1763+ if ((skb->len <= mtu) || skb->local_df || skb_is_gso(skb)) {
1764+ IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
1765+ IPSTATS_MIB_OUT, skb->len);
1766+ return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
1767+ dst->dev, dst_output);
1768+ }
1769+
1770+ skb->dev = dst->dev;
1771+ ipv6_local_error(sk, EMSGSIZE, fl6, mtu);
1772+ IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
1773+ kfree_skb(skb);
1774+ return -EMSGSIZE;
1775+}
1776+
1777+EXPORT_SYMBOL(ip6_xmit);
1778+
1779+static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
1780+{
1781+ struct ip6_ra_chain *ra;
1782+ struct sock *last = NULL;
1783+
1784+ read_lock(&ip6_ra_lock);
1785+ for (ra = ip6_ra_chain; ra; ra = ra->next) {
1786+ struct sock *sk = ra->sk;
1787+ if (sk && ra->sel == sel &&
1788+ (!sk->sk_bound_dev_if ||
1789+ sk->sk_bound_dev_if == skb->dev->ifindex)) {
1790+ if (last) {
1791+ struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1792+ if (skb2)
1793+ rawv6_rcv(last, skb2);
1794+ }
1795+ last = sk;
1796+ }
1797+ }
1798+
1799+ if (last) {
1800+ rawv6_rcv(last, skb);
1801+ read_unlock(&ip6_ra_lock);
1802+ return 1;
1803+ }
1804+ read_unlock(&ip6_ra_lock);
1805+ return 0;
1806+}
1807+
1808+static int ip6_forward_proxy_check(struct sk_buff *skb)
1809+{
1810+ struct ipv6hdr *hdr = ipv6_hdr(skb);
1811+ u8 nexthdr = hdr->nexthdr;
1812+ __be16 frag_off;
1813+ int offset;
1814+
1815+ if (ipv6_ext_hdr(nexthdr)) {
1816+ offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
1817+ if (offset < 0)
1818+ return 0;
1819+ } else
1820+ offset = sizeof(struct ipv6hdr);
1821+
1822+ if (nexthdr == IPPROTO_ICMPV6) {
1823+ struct icmp6hdr *icmp6;
1824+
1825+ if (!pskb_may_pull(skb, (skb_network_header(skb) +
1826+ offset + 1 - skb->data)))
1827+ return 0;
1828+
1829+ icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
1830+
1831+ switch (icmp6->icmp6_type) {
1832+ case NDISC_ROUTER_SOLICITATION:
1833+ case NDISC_ROUTER_ADVERTISEMENT:
1834+ case NDISC_NEIGHBOUR_SOLICITATION:
1835+ case NDISC_NEIGHBOUR_ADVERTISEMENT:
1836+ case NDISC_REDIRECT:
1837+ /* For reaction involving unicast neighbor discovery
1838+ * message destined to the proxied address, pass it to
1839+ * input function.
1840+ */
1841+ return 1;
1842+ default:
1843+ break;
1844+ }
1845+ }
1846+
1847+ /*
1848+ * The proxying router can't forward traffic sent to a link-local
1849+ * address, so signal the sender and discard the packet. This
1850+ * behavior is clarified by the MIPv6 specification.
1851+ */
1852+ if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
1853+ dst_link_failure(skb);
1854+ return -1;
1855+ }
1856+
1857+ return 0;
1858+}
1859+
1860+static inline int ip6_forward_finish(struct sk_buff *skb)
1861+{
1862+ return dst_output(skb);
1863+}
1864+
1865+int ip6_forward(struct sk_buff *skb)
1866+{
1867+ struct dst_entry *dst = skb_dst(skb);
1868+ struct ipv6hdr *hdr = ipv6_hdr(skb);
1869+ struct inet6_skb_parm *opt = IP6CB(skb);
1870+ struct net *net = dev_net(dst->dev);
1871+ u32 mtu;
1872+
1873+ if (net->ipv6.devconf_all->forwarding == 0)
1874+ goto error;
1875+
1876+ if (skb_warn_if_lro(skb))
1877+ goto drop;
1878+
1879+ if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
1880+ IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
1881+ goto drop;
1882+ }
1883+
1884+ if (skb->pkt_type != PACKET_HOST)
1885+ goto drop;
1886+
1887+ skb_forward_csum(skb);
1888+
1889+ /*
1890+ * We DO NOT make any processing on
1891+ * RA packets, pushing them to user level AS IS
1892+ * without ane WARRANTY that application will be able
1893+ * to interpret them. The reason is that we
1894+ * cannot make anything clever here.
1895+ *
1896+ * We are not end-node, so that if packet contains
1897+ * AH/ESP, we cannot make anything.
1898+ * Defragmentation also would be mistake, RA packets
1899+ * cannot be fragmented, because there is no warranty
1900+ * that different fragments will go along one path. --ANK
1901+ */
1902+ if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
1903+ if (ip6_call_ra_chain(skb, ntohs(opt->ra)))
1904+ return 0;
1905+ }
1906+
1907+ /*
1908+ * check and decrement ttl
1909+ */
1910+ if (hdr->hop_limit <= 1) {
1911+ /* Force OUTPUT device used as source address */
1912+ skb->dev = dst->dev;
1913+ icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
1914+ IP6_INC_STATS_BH(net,
1915+ ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
1916+
1917+ kfree_skb(skb);
1918+ return -ETIMEDOUT;
1919+ }
1920+
1921+ /* XXX: idev->cnf.proxy_ndp? */
1922+ if (net->ipv6.devconf_all->proxy_ndp &&
1923+ pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
1924+ int proxied = ip6_forward_proxy_check(skb);
1925+ if (proxied > 0)
1926+ return ip6_input(skb);
1927+ else if (proxied < 0) {
1928+ IP6_INC_STATS(net, ip6_dst_idev(dst),
1929+ IPSTATS_MIB_INDISCARDS);
1930+ goto drop;
1931+ }
1932+ }
1933+
1934+ if (!xfrm6_route_forward(skb)) {
1935+ IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
1936+ goto drop;
1937+ }
1938+ dst = skb_dst(skb);
1939+
1940+ /* IPv6 specs say nothing about it, but it is clear that we cannot
1941+ send redirects to source routed frames.
1942+ We don't send redirects to frames decapsulated from IPsec.
1943+ */
1944+ if (skb->dev == dst->dev && opt->srcrt == 0 && !skb_sec_path(skb)) {
1945+ struct in6_addr *target = NULL;
1946+ struct inet_peer *peer;
1947+ struct rt6_info *rt;
1948+
1949+ /*
1950+ * incoming and outgoing devices are the same
1951+ * send a redirect.
1952+ */
1953+
1954+ rt = (struct rt6_info *) dst;
1955+ if (rt->rt6i_flags & RTF_GATEWAY)
1956+ target = &rt->rt6i_gateway;
1957+ else
1958+ target = &hdr->daddr;
1959+
1960+ peer = inet_getpeer_v6(net->ipv6.peers, &rt->rt6i_dst.addr, 1);
1961+
1962+ /* Limit redirects both by destination (here)
1963+ and by source (inside ndisc_send_redirect)
1964+ */
1965+ if (inet_peer_xrlim_allow(peer, 1*HZ))
1966+ ndisc_send_redirect(skb, target);
1967+ if (peer)
1968+ inet_putpeer(peer);
1969+ } else {
1970+ int addrtype = ipv6_addr_type(&hdr->saddr);
1971+
1972+ /* This check is security critical. */
1973+ if (addrtype == IPV6_ADDR_ANY ||
1974+ addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
1975+ goto error;
1976+ if (addrtype & IPV6_ADDR_LINKLOCAL) {
1977+ icmpv6_send(skb, ICMPV6_DEST_UNREACH,
1978+ ICMPV6_NOT_NEIGHBOUR, 0);
1979+ goto error;
1980+ }
1981+ }
1982+
1983+ mtu = dst_mtu(dst);
1984+ if (mtu < IPV6_MIN_MTU)
1985+ mtu = IPV6_MIN_MTU;
1986+
1987+ if ((!skb->local_df && skb->len > mtu && !skb_is_gso(skb)) ||
1988+ (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu)) {
1989+ /* Again, force OUTPUT device used as source address */
1990+ skb->dev = dst->dev;
1991+ icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1992+ IP6_INC_STATS_BH(net,
1993+ ip6_dst_idev(dst), IPSTATS_MIB_INTOOBIGERRORS);
1994+ IP6_INC_STATS_BH(net,
1995+ ip6_dst_idev(dst), IPSTATS_MIB_FRAGFAILS);
1996+ kfree_skb(skb);
1997+ return -EMSGSIZE;
1998+ }
1999+
2000+ if (skb_cow(skb, dst->dev->hard_header_len)) {
2001+ IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
2002+ goto drop;
2003+ }
2004+
2005+ hdr = ipv6_hdr(skb);
2006+
2007+ /* Mangling hops number delayed to point after skb COW */
2008+
2009+ hdr->hop_limit--;
2010+
2011+ IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
2012+ IP6_ADD_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len);
2013+ return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dst->dev,
2014+ ip6_forward_finish);
2015+
2016+error:
2017+ IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
2018+drop:
2019+ kfree_skb(skb);
2020+ return -EINVAL;
2021+}
2022+
2023+static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
2024+{
2025+ to->pkt_type = from->pkt_type;
2026+ to->priority = from->priority;
2027+ to->protocol = from->protocol;
2028+ skb_dst_drop(to);
2029+ skb_dst_set(to, dst_clone(skb_dst(from)));
2030+ to->dev = from->dev;
2031+ to->mark = from->mark;
2032+
2033+#ifdef CONFIG_NET_SCHED
2034+ to->tc_index = from->tc_index;
2035+#endif
2036+ nf_copy(to, from);
2037+#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
2038+ to->nf_trace = from->nf_trace;
2039+#endif
2040+ skb_copy_secmark(to, from);
2041+}
2042+
2043+int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
2044+{
2045+ struct sk_buff *frag;
2046+ struct rt6_info *rt = (struct rt6_info*)skb_dst(skb);
2047+ struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
2048+ struct ipv6hdr *tmp_hdr;
2049+ struct frag_hdr *fh;
2050+ unsigned int mtu, hlen, left, len;
2051+ int hroom, troom;
2052+ __be32 frag_id = 0;
2053+ int ptr, offset = 0, err=0;
2054+ u8 *prevhdr, nexthdr = 0;
2055+ struct net *net = dev_net(skb_dst(skb)->dev);
2056+
2057+ hlen = ip6_find_1stfragopt(skb, &prevhdr);
2058+ nexthdr = *prevhdr;
2059+
2060+ mtu = ip6_skb_dst_mtu(skb);
2061+
2062+ /* We must not fragment if the socket is set to force MTU discovery
2063+ * or if the skb it not generated by a local socket.
2064+ */
2065+ if (unlikely(!skb->local_df && skb->len > mtu) ||
2066+ (IP6CB(skb)->frag_max_size &&
2067+ IP6CB(skb)->frag_max_size > mtu)) {
2068+ if (skb->sk && dst_allfrag(skb_dst(skb)))
2069+ sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK);
2070+
2071+ skb->dev = skb_dst(skb)->dev;
2072+ icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
2073+ IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2074+ IPSTATS_MIB_FRAGFAILS);
2075+ kfree_skb(skb);
2076+ return -EMSGSIZE;
2077+ }
2078+
2079+ if (np && np->frag_size < mtu) {
2080+ if (np->frag_size)
2081+ mtu = np->frag_size;
2082+ }
2083+ mtu -= hlen + sizeof(struct frag_hdr);
2084+
2085+ if (skb_has_frag_list(skb)) {
2086+ int first_len = skb_pagelen(skb);
2087+ struct sk_buff *frag2;
2088+
2089+ if (first_len - hlen > mtu ||
2090+ ((first_len - hlen) & 7) ||
2091+ skb_cloned(skb))
2092+ goto slow_path;
2093+
2094+ skb_walk_frags(skb, frag) {
2095+ /* Correct geometry. */
2096+ if (frag->len > mtu ||
2097+ ((frag->len & 7) && frag->next) ||
2098+ skb_headroom(frag) < hlen)
2099+ goto slow_path_clean;
2100+
2101+ /* Partially cloned skb? */
2102+ if (skb_shared(frag))
2103+ goto slow_path_clean;
2104+
2105+ BUG_ON(frag->sk);
2106+ if (skb->sk) {
2107+ frag->sk = skb->sk;
2108+ frag->destructor = sock_wfree;
2109+ }
2110+ skb->truesize -= frag->truesize;
2111+ }
2112+
2113+ err = 0;
2114+ offset = 0;
2115+ frag = skb_shinfo(skb)->frag_list;
2116+ skb_frag_list_init(skb);
2117+ /* BUILD HEADER */
2118+
2119+ *prevhdr = NEXTHDR_FRAGMENT;
2120+ tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
2121+ if (!tmp_hdr) {
2122+ IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2123+ IPSTATS_MIB_FRAGFAILS);
2124+ return -ENOMEM;
2125+ }
2126+
2127+ __skb_pull(skb, hlen);
2128+ fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
2129+ __skb_push(skb, hlen);
2130+ skb_reset_network_header(skb);
2131+ memcpy(skb_network_header(skb), tmp_hdr, hlen);
2132+
2133+ ipv6_select_ident(fh, rt);
2134+ fh->nexthdr = nexthdr;
2135+ fh->reserved = 0;
2136+ fh->frag_off = htons(IP6_MF);
2137+ frag_id = fh->identification;
2138+
2139+ first_len = skb_pagelen(skb);
2140+ skb->data_len = first_len - skb_headlen(skb);
2141+ skb->len = first_len;
2142+ ipv6_hdr(skb)->payload_len = htons(first_len -
2143+ sizeof(struct ipv6hdr));
2144+
2145+ dst_hold(&rt->dst);
2146+
2147+ for (;;) {
2148+ /* Prepare header of the next frame,
2149+ * before previous one went down. */
2150+ if (frag) {
2151+ frag->ip_summed = CHECKSUM_NONE;
2152+ skb_reset_transport_header(frag);
2153+ fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
2154+ __skb_push(frag, hlen);
2155+ skb_reset_network_header(frag);
2156+ memcpy(skb_network_header(frag), tmp_hdr,
2157+ hlen);
2158+ offset += skb->len - hlen - sizeof(struct frag_hdr);
2159+ fh->nexthdr = nexthdr;
2160+ fh->reserved = 0;
2161+ fh->frag_off = htons(offset);
2162+ if (frag->next != NULL)
2163+ fh->frag_off |= htons(IP6_MF);
2164+ fh->identification = frag_id;
2165+ ipv6_hdr(frag)->payload_len =
2166+ htons(frag->len -
2167+ sizeof(struct ipv6hdr));
2168+ ip6_copy_metadata(frag, skb);
2169+ }
2170+
2171+ err = output(skb);
2172+ if(!err)
2173+ IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
2174+ IPSTATS_MIB_FRAGCREATES);
2175+
2176+ if (err || !frag)
2177+ break;
2178+
2179+ skb = frag;
2180+ frag = skb->next;
2181+ skb->next = NULL;
2182+ }
2183+
2184+ kfree(tmp_hdr);
2185+
2186+ if (err == 0) {
2187+ IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
2188+ IPSTATS_MIB_FRAGOKS);
2189+ ip6_rt_put(rt);
2190+ return 0;
2191+ }
2192+
2193+ while (frag) {
2194+ skb = frag->next;
2195+ kfree_skb(frag);
2196+ frag = skb;
2197+ }
2198+
2199+ IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
2200+ IPSTATS_MIB_FRAGFAILS);
2201+ ip6_rt_put(rt);
2202+ return err;
2203+
2204+slow_path_clean:
2205+ skb_walk_frags(skb, frag2) {
2206+ if (frag2 == frag)
2207+ break;
2208+ frag2->sk = NULL;
2209+ frag2->destructor = NULL;
2210+ skb->truesize += frag2->truesize;
2211+ }
2212+ }
2213+
2214+slow_path:
2215+ if ((skb->ip_summed == CHECKSUM_PARTIAL) &&
2216+ skb_checksum_help(skb))
2217+ goto fail;
2218+
2219+ left = skb->len - hlen; /* Space per frame */
2220+ ptr = hlen; /* Where to start from */
2221+
2222+ /*
2223+ * Fragment the datagram.
2224+ */
2225+
2226+ *prevhdr = NEXTHDR_FRAGMENT;
2227+ hroom = LL_RESERVED_SPACE(rt->dst.dev);
2228+ troom = rt->dst.dev->needed_tailroom;
2229+
2230+ /*
2231+ * Keep copying data until we run out.
2232+ */
2233+ while(left > 0) {
2234+ len = left;
2235+ /* IF: it doesn't fit, use 'mtu' - the data space left */
2236+ if (len > mtu)
2237+ len = mtu;
2238+ /* IF: we are not sending up to and including the packet end
2239+ then align the next start on an eight byte boundary */
2240+ if (len < left) {
2241+ len &= ~7;
2242+ }
2243+ /*
2244+ * Allocate buffer.
2245+ */
2246+
2247+ if ((frag = alloc_skb(len + hlen + sizeof(struct frag_hdr) +
2248+ hroom + troom, GFP_ATOMIC)) == NULL) {
2249+ NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
2250+ IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2251+ IPSTATS_MIB_FRAGFAILS);
2252+ err = -ENOMEM;
2253+ goto fail;
2254+ }
2255+
2256+ /*
2257+ * Set up data on packet
2258+ */
2259+
2260+ ip6_copy_metadata(frag, skb);
2261+ skb_reserve(frag, hroom);
2262+ skb_put(frag, len + hlen + sizeof(struct frag_hdr));
2263+ skb_reset_network_header(frag);
2264+ fh = (struct frag_hdr *)(skb_network_header(frag) + hlen);
2265+ frag->transport_header = (frag->network_header + hlen +
2266+ sizeof(struct frag_hdr));
2267+
2268+ /*
2269+ * Charge the memory for the fragment to any owner
2270+ * it might possess
2271+ */
2272+ if (skb->sk)
2273+ skb_set_owner_w(frag, skb->sk);
2274+
2275+ /*
2276+ * Copy the packet header into the new buffer.
2277+ */
2278+ skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
2279+
2280+ /*
2281+ * Build fragment header.
2282+ */
2283+ fh->nexthdr = nexthdr;
2284+ fh->reserved = 0;
2285+ if (!frag_id) {
2286+ ipv6_select_ident(fh, rt);
2287+ frag_id = fh->identification;
2288+ } else
2289+ fh->identification = frag_id;
2290+
2291+ /*
2292+ * Copy a block of the IP datagram.
2293+ */
2294+ if (skb_copy_bits(skb, ptr, skb_transport_header(frag), len))
2295+ BUG();
2296+ left -= len;
2297+
2298+ fh->frag_off = htons(offset);
2299+ if (left > 0)
2300+ fh->frag_off |= htons(IP6_MF);
2301+ ipv6_hdr(frag)->payload_len = htons(frag->len -
2302+ sizeof(struct ipv6hdr));
2303+
2304+ ptr += len;
2305+ offset += len;
2306+
2307+ /*
2308+ * Put this fragment into the sending queue.
2309+ */
2310+ err = output(frag);
2311+ if (err)
2312+ goto fail;
2313+
2314+ IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2315+ IPSTATS_MIB_FRAGCREATES);
2316+ }
2317+ IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2318+ IPSTATS_MIB_FRAGOKS);
2319+ consume_skb(skb);
2320+ return err;
2321+
2322+fail:
2323+ IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2324+ IPSTATS_MIB_FRAGFAILS);
2325+ kfree_skb(skb);
2326+ return err;
2327+}
2328+
2329+static inline int ip6_rt_check(const struct rt6key *rt_key,
2330+ const struct in6_addr *fl_addr,
2331+ const struct in6_addr *addr_cache)
2332+{
2333+ return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
2334+ (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache));
2335+}
2336+
2337+static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
2338+ struct dst_entry *dst,
2339+ const struct flowi6 *fl6)
2340+{
2341+ struct ipv6_pinfo *np = inet6_sk(sk);
2342+ struct rt6_info *rt;
2343+
2344+ if (!dst)
2345+ goto out;
2346+
2347+ if (dst->ops->family != AF_INET6) {
2348+ dst_release(dst);
2349+ return NULL;
2350+ }
2351+
2352+ rt = (struct rt6_info *)dst;
2353+ /* Yes, checking route validity in not connected
2354+ * case is not very simple. Take into account,
2355+ * that we do not support routing by source, TOS,
2356+ * and MSG_DONTROUTE --ANK (980726)
2357+ *
2358+ * 1. ip6_rt_check(): If route was host route,
2359+ * check that cached destination is current.
2360+ * If it is network route, we still may
2361+ * check its validity using saved pointer
2362+ * to the last used address: daddr_cache.
2363+ * We do not want to save whole address now,
2364+ * (because main consumer of this service
2365+ * is tcp, which has not this problem),
2366+ * so that the last trick works only on connected
2367+ * sockets.
2368+ * 2. oif also should be the same.
2369+ */
2370+ if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
2371+#ifdef CONFIG_IPV6_SUBTREES
2372+ ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
2373+#endif
2374+ (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex)) {
2375+ dst_release(dst);
2376+ dst = NULL;
2377+ }
2378+
2379+out:
2380+ return dst;
2381+}
2382+
2383+static int ip6_dst_lookup_tail(struct sock *sk,
2384+ struct dst_entry **dst, struct flowi6 *fl6)
2385+{
2386+ struct net *net = sock_net(sk);
2387+#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2388+ struct neighbour *n;
2389+ struct rt6_info *rt;
2390+#endif
2391+ int err;
2392+
2393+ if (*dst == NULL)
2394+ *dst = ip6_route_output(net, sk, fl6);
2395+
2396+ if ((err = (*dst)->error))
2397+ goto out_err_release;
2398+
2399+ if (ipv6_addr_any(&fl6->saddr)) {
2400+ struct rt6_info *rt = (struct rt6_info *) *dst;
2401+ err = ip6_route_get_saddr(net, rt, &fl6->daddr,
2402+ sk ? inet6_sk(sk)->srcprefs : 0,
2403+ &fl6->saddr);
2404+ if (err)
2405+ goto out_err_release;
2406+ }
2407+
2408+#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2409+ /*
2410+ * Here if the dst entry we've looked up
2411+ * has a neighbour entry that is in the INCOMPLETE
2412+ * state and the src address from the flow is
2413+ * marked as OPTIMISTIC, we release the found
2414+ * dst entry and replace it instead with the
2415+ * dst entry of the nexthop router
2416+ */
2417+ rt = (struct rt6_info *) *dst;
2418+ rcu_read_lock_bh();
2419+ n = __ipv6_neigh_lookup_noref(rt->dst.dev, rt6_nexthop(rt));
2420+ err = n && !(n->nud_state & NUD_VALID) ? -EINVAL : 0;
2421+ rcu_read_unlock_bh();
2422+
2423+ if (err) {
2424+ struct inet6_ifaddr *ifp;
2425+ struct flowi6 fl_gw6;
2426+ int redirect;
2427+
2428+ ifp = ipv6_get_ifaddr(net, &fl6->saddr,
2429+ (*dst)->dev, 1);
2430+
2431+ redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
2432+ if (ifp)
2433+ in6_ifa_put(ifp);
2434+
2435+ if (redirect) {
2436+ /*
2437+ * We need to get the dst entry for the
2438+ * default router instead
2439+ */
2440+ dst_release(*dst);
2441+ memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
2442+ memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
2443+ *dst = ip6_route_output(net, sk, &fl_gw6);
2444+ if ((err = (*dst)->error))
2445+ goto out_err_release;
2446+ }
2447+ }
2448+#endif
2449+
2450+ return 0;
2451+
2452+out_err_release:
2453+ if (err == -ENETUNREACH)
2454+ IP6_INC_STATS_BH(net, NULL, IPSTATS_MIB_OUTNOROUTES);
2455+ dst_release(*dst);
2456+ *dst = NULL;
2457+ return err;
2458+}
2459+
2460+/**
2461+ * ip6_dst_lookup - perform route lookup on flow
2462+ * @sk: socket which provides route info
2463+ * @dst: pointer to dst_entry * for result
2464+ * @fl6: flow to lookup
2465+ *
2466+ * This function performs a route lookup on the given flow.
2467+ *
2468+ * It returns zero on success, or a standard errno code on error.
2469+ */
2470+int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6)
2471+{
2472+ *dst = NULL;
2473+ return ip6_dst_lookup_tail(sk, dst, fl6);
2474+}
2475+EXPORT_SYMBOL_GPL(ip6_dst_lookup);
2476+
2477+/**
2478+ * ip6_dst_lookup_flow - perform route lookup on flow with ipsec
2479+ * @sk: socket which provides route info
2480+ * @fl6: flow to lookup
2481+ * @final_dst: final destination address for ipsec lookup
2482+ * @can_sleep: we are in a sleepable context
2483+ *
2484+ * This function performs a route lookup on the given flow.
2485+ *
2486+ * It returns a valid dst pointer on success, or a pointer encoded
2487+ * error code.
2488+ */
2489+struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
2490+ const struct in6_addr *final_dst,
2491+ bool can_sleep)
2492+{
2493+ struct dst_entry *dst = NULL;
2494+ int err;
2495+
2496+ err = ip6_dst_lookup_tail(sk, &dst, fl6);
2497+ if (err)
2498+ return ERR_PTR(err);
2499+ if (final_dst)
2500+ fl6->daddr = *final_dst;
2501+ if (can_sleep)
2502+ fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
2503+
2504+ return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
2505+}
2506+EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
2507+
2508+/**
2509+ * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
2510+ * @sk: socket which provides the dst cache and route info
2511+ * @fl6: flow to lookup
2512+ * @final_dst: final destination address for ipsec lookup
2513+ * @can_sleep: we are in a sleepable context
2514+ *
2515+ * This function performs a route lookup on the given flow with the
2516+ * possibility of using the cached route in the socket if it is valid.
2517+ * It will take the socket dst lock when operating on the dst cache.
2518+ * As a result, this function can only be used in process context.
2519+ *
2520+ * It returns a valid dst pointer on success, or a pointer encoded
2521+ * error code.
2522+ */
2523+struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
2524+ const struct in6_addr *final_dst,
2525+ bool can_sleep)
2526+{
2527+ struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
2528+ int err;
2529+
2530+ dst = ip6_sk_dst_check(sk, dst, fl6);
2531+
2532+ err = ip6_dst_lookup_tail(sk, &dst, fl6);
2533+ if (err)
2534+ return ERR_PTR(err);
2535+ if (final_dst)
2536+ fl6->daddr = *final_dst;
2537+ if (can_sleep)
2538+ fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
2539+
2540+ return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
2541+}
2542+EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
2543+
2544+static inline int ip6_ufo_append_data(struct sock *sk,
2545+ int getfrag(void *from, char *to, int offset, int len,
2546+ int odd, struct sk_buff *skb),
2547+ void *from, int length, int hh_len, int fragheaderlen,
2548+ int transhdrlen, int mtu,unsigned int flags,
2549+ struct rt6_info *rt)
2550+
2551+{
2552+ struct sk_buff *skb;
2553+ int err;
2554+
2555+ /* There is support for UDP large send offload by network
2556+ * device, so create one single skb packet containing complete
2557+ * udp datagram
2558+ */
2559+ if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
2560+ struct frag_hdr fhdr;
2561+
2562+ skb = sock_alloc_send_skb(sk,
2563+ hh_len + fragheaderlen + transhdrlen + 20,
2564+ (flags & MSG_DONTWAIT), &err);
2565+ if (skb == NULL)
2566+ return err;
2567+
2568+ /* reserve space for Hardware header */
2569+ skb_reserve(skb, hh_len);
2570+
2571+ /* create space for UDP/IP header */
2572+ skb_put(skb,fragheaderlen + transhdrlen);
2573+
2574+ /* initialize network header pointer */
2575+ skb_reset_network_header(skb);
2576+
2577+ /* initialize protocol header pointer */
2578+ skb->transport_header = skb->network_header + fragheaderlen;
2579+
2580+ skb->ip_summed = CHECKSUM_PARTIAL;
2581+ skb->csum = 0;
2582+
2583+ /* Specify the length of each IPv6 datagram fragment.
2584+ * It has to be a multiple of 8.
2585+ */
2586+ skb_shinfo(skb)->gso_size = (mtu - fragheaderlen -
2587+ sizeof(struct frag_hdr)) & ~7;
2588+ skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
2589+ ipv6_select_ident(&fhdr, rt);
2590+ skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
2591+ __skb_queue_tail(&sk->sk_write_queue, skb);
2592+ }
2593+
2594+ return skb_append_datato_frags(sk, skb, getfrag, from,
2595+ (length - transhdrlen));
2596+}
2597+
2598+static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
2599+ gfp_t gfp)
2600+{
2601+ return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
2602+}
2603+
2604+static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
2605+ gfp_t gfp)
2606+{
2607+ return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
2608+}
2609+
2610+static void ip6_append_data_mtu(unsigned int *mtu,
2611+ int *maxfraglen,
2612+ unsigned int fragheaderlen,
2613+ struct sk_buff *skb,
2614+ struct rt6_info *rt,
2615+ bool pmtuprobe)
2616+{
2617+ if (!(rt->dst.flags & DST_XFRM_TUNNEL)) {
2618+ if (skb == NULL) {
2619+ /* first fragment, reserve header_len */
2620+ *mtu = *mtu - rt->dst.header_len;
2621+
2622+ } else {
2623+ /*
2624+ * this fragment is not first, the headers
2625+ * space is regarded as data space.
2626+ */
2627+ *mtu = min(*mtu, pmtuprobe ?
2628+ rt->dst.dev->mtu :
2629+ dst_mtu(rt->dst.path));
2630+ }
2631+ *maxfraglen = ((*mtu - fragheaderlen) & ~7)
2632+ + fragheaderlen - sizeof(struct frag_hdr);
2633+ }
2634+}
2635+
2636+int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
2637+ int offset, int len, int odd, struct sk_buff *skb),
2638+ void *from, int length, int transhdrlen,
2639+ int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
2640+ struct rt6_info *rt, unsigned int flags, int dontfrag)
2641+{
2642+ struct inet_sock *inet = inet_sk(sk);
2643+ struct ipv6_pinfo *np = inet6_sk(sk);
2644+ struct inet_cork *cork;
2645+ struct sk_buff *skb, *skb_prev = NULL;
2646+ unsigned int maxfraglen, fragheaderlen, mtu;
2647+ int exthdrlen;
2648+ int dst_exthdrlen;
2649+ int hh_len;
2650+ int copy;
2651+ int err;
2652+ int offset = 0;
2653+ __u8 tx_flags = 0;
2654+
2655+ if (flags&MSG_PROBE)
2656+ return 0;
2657+ cork = &inet->cork.base;
2658+ if (skb_queue_empty(&sk->sk_write_queue)) {
2659+ /*
2660+ * setup for corking
2661+ */
2662+ if (opt) {
2663+ if (WARN_ON(np->cork.opt))
2664+ return -EINVAL;
2665+
2666+ np->cork.opt = kzalloc(opt->tot_len, sk->sk_allocation);
2667+ if (unlikely(np->cork.opt == NULL))
2668+ return -ENOBUFS;
2669+
2670+ np->cork.opt->tot_len = opt->tot_len;
2671+ np->cork.opt->opt_flen = opt->opt_flen;
2672+ np->cork.opt->opt_nflen = opt->opt_nflen;
2673+
2674+ np->cork.opt->dst0opt = ip6_opt_dup(opt->dst0opt,
2675+ sk->sk_allocation);
2676+ if (opt->dst0opt && !np->cork.opt->dst0opt)
2677+ return -ENOBUFS;
2678+
2679+ np->cork.opt->dst1opt = ip6_opt_dup(opt->dst1opt,
2680+ sk->sk_allocation);
2681+ if (opt->dst1opt && !np->cork.opt->dst1opt)
2682+ return -ENOBUFS;
2683+
2684+ np->cork.opt->hopopt = ip6_opt_dup(opt->hopopt,
2685+ sk->sk_allocation);
2686+ if (opt->hopopt && !np->cork.opt->hopopt)
2687+ return -ENOBUFS;
2688+
2689+ np->cork.opt->srcrt = ip6_rthdr_dup(opt->srcrt,
2690+ sk->sk_allocation);
2691+ if (opt->srcrt && !np->cork.opt->srcrt)
2692+ return -ENOBUFS;
2693+
2694+ /* need source address above miyazawa*/
2695+ }
2696+ dst_hold(&rt->dst);
2697+ cork->dst = &rt->dst;
2698+ inet->cork.fl.u.ip6 = *fl6;
2699+ np->cork.hop_limit = hlimit;
2700+ np->cork.tclass = tclass;
2701+ if (rt->dst.flags & DST_XFRM_TUNNEL)
2702+ mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ?
2703+ rt->dst.dev->mtu : dst_mtu(&rt->dst);
2704+ else
2705+ mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ?
2706+ rt->dst.dev->mtu : dst_mtu(rt->dst.path);
2707+ if (np->frag_size < mtu) {
2708+ if (np->frag_size)
2709+ mtu = np->frag_size;
2710+ }
2711+ cork->fragsize = mtu;
2712+ if (dst_allfrag(rt->dst.path))
2713+ cork->flags |= IPCORK_ALLFRAG;
2714+ cork->length = 0;
2715+ exthdrlen = (opt ? opt->opt_flen : 0);
2716+ length += exthdrlen;
2717+ transhdrlen += exthdrlen;
2718+ dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len;
2719+ } else {
2720+ rt = (struct rt6_info *)cork->dst;
2721+ fl6 = &inet->cork.fl.u.ip6;
2722+ opt = np->cork.opt;
2723+ transhdrlen = 0;
2724+ exthdrlen = 0;
2725+ dst_exthdrlen = 0;
2726+ mtu = cork->fragsize;
2727+ }
2728+
2729+ hh_len = LL_RESERVED_SPACE(rt->dst.dev);
2730+
2731+ fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
2732+ (opt ? opt->opt_nflen : 0);
2733+ maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
2734+
2735+ if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
2736+ if (cork->length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
2737+ ipv6_local_error(sk, EMSGSIZE, fl6, mtu-exthdrlen);
2738+ return -EMSGSIZE;
2739+ }
2740+ }
2741+
2742+ /* For UDP, check if TX timestamp is enabled */
2743+ if (sk->sk_type == SOCK_DGRAM)
2744+ sock_tx_timestamp(sk, &tx_flags);
2745+
2746+ /*
2747+ * Let's try using as much space as possible.
2748+ * Use MTU if total length of the message fits into the MTU.
2749+ * Otherwise, we need to reserve fragment header and
2750+ * fragment alignment (= 8-15 octects, in total).
2751+ *
2752+ * Note that we may need to "move" the data from the tail of
2753+ * of the buffer to the new fragment when we split
2754+ * the message.
2755+ *
2756+ * FIXME: It may be fragmented into multiple chunks
2757+ * at once if non-fragmentable extension headers
2758+ * are too large.
2759+ * --yoshfuji
2760+ */
2761+
2762+ if ((length > mtu) && dontfrag && (sk->sk_protocol == IPPROTO_UDP ||
2763+ sk->sk_protocol == IPPROTO_RAW)) {
2764+ ipv6_local_rxpmtu(sk, fl6, mtu-exthdrlen);
2765+ return -EMSGSIZE;
2766+ }
2767+
2768+ skb = skb_peek_tail(&sk->sk_write_queue);
2769+ cork->length += length;
2770+ if (((length > mtu) ||
2771+ (skb && skb_has_frags(skb))) &&
2772+ (sk->sk_protocol == IPPROTO_UDP) &&
2773+ (rt->dst.dev->features & NETIF_F_UFO)) {
2774+ err = ip6_ufo_append_data(sk, getfrag, from, length,
2775+ hh_len, fragheaderlen,
2776+ transhdrlen, mtu, flags, rt);
2777+ if (err)
2778+ goto error;
2779+ return 0;
2780+ }
2781+
2782+ if (!skb)
2783+ goto alloc_new_skb;
2784+
2785+ while (length > 0) {
2786+ /* Check if the remaining data fits into current packet. */
2787+ copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
2788+ if (copy < length)
2789+ copy = maxfraglen - skb->len;
2790+
2791+ if (copy <= 0) {
2792+ char *data;
2793+ unsigned int datalen;
2794+ unsigned int fraglen;
2795+ unsigned int fraggap;
2796+ unsigned int alloclen;
2797+alloc_new_skb:
2798+ /* There's no room in the current skb */
2799+ if (skb)
2800+ fraggap = skb->len - maxfraglen;
2801+ else
2802+ fraggap = 0;
2803+ /* update mtu and maxfraglen if necessary */
2804+ if (skb == NULL || skb_prev == NULL)
2805+ ip6_append_data_mtu(&mtu, &maxfraglen,
2806+ fragheaderlen, skb, rt,
2807+ np->pmtudisc ==
2808+ IPV6_PMTUDISC_PROBE);
2809+
2810+ skb_prev = skb;
2811+
2812+ /*
2813+ * If remaining data exceeds the mtu,
2814+ * we know we need more fragment(s).
2815+ */
2816+ datalen = length + fraggap;
2817+
2818+ if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
2819+ datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;
2820+ if ((flags & MSG_MORE) &&
2821+ !(rt->dst.dev->features&NETIF_F_SG))
2822+ alloclen = mtu;
2823+ else
2824+ alloclen = datalen + fragheaderlen;
2825+
2826+ alloclen += dst_exthdrlen;
2827+
2828+ if (datalen != length + fraggap) {
2829+ /*
2830+ * this is not the last fragment, the trailer
2831+ * space is regarded as data space.
2832+ */
2833+ datalen += rt->dst.trailer_len;
2834+ }
2835+
2836+ alloclen += rt->dst.trailer_len;
2837+ fraglen = datalen + fragheaderlen;
2838+
2839+ /*
2840+ * We just reserve space for fragment header.
2841+ * Note: this may be overallocation if the message
2842+ * (without MSG_MORE) fits into the MTU.
2843+ */
2844+ alloclen += sizeof(struct frag_hdr);
2845+
2846+ if (transhdrlen) {
2847+ skb = sock_alloc_send_skb(sk,
2848+ alloclen + hh_len,
2849+ (flags & MSG_DONTWAIT), &err);
2850+ } else {
2851+ skb = NULL;
2852+ if (atomic_read(&sk->sk_wmem_alloc) <=
2853+ 2 * sk->sk_sndbuf)
2854+ skb = sock_wmalloc(sk,
2855+ alloclen + hh_len, 1,
2856+ sk->sk_allocation);
2857+ if (unlikely(skb == NULL))
2858+ err = -ENOBUFS;
2859+ else {
2860+ /* Only the initial fragment
2861+ * is time stamped.
2862+ */
2863+ tx_flags = 0;
2864+ }
2865+ }
2866+ if (skb == NULL)
2867+ goto error;
2868+ /*
2869+ * Fill in the control structures
2870+ */
2871+ skb->ip_summed = CHECKSUM_NONE;
2872+ skb->csum = 0;
2873+ /* reserve for fragmentation and ipsec header */
2874+ skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
2875+ dst_exthdrlen);
2876+
2877+ if (sk->sk_type == SOCK_DGRAM)
2878+ skb_shinfo(skb)->tx_flags = tx_flags;
2879+
2880+ /*
2881+ * Find where to start putting bytes
2882+ */
2883+ data = skb_put(skb, fraglen);
2884+ skb_set_network_header(skb, exthdrlen);
2885+ data += fragheaderlen;
2886+ skb->transport_header = (skb->network_header +
2887+ fragheaderlen);
2888+ if (fraggap) {
2889+ skb->csum = skb_copy_and_csum_bits(
2890+ skb_prev, maxfraglen,
2891+ data + transhdrlen, fraggap, 0);
2892+ skb_prev->csum = csum_sub(skb_prev->csum,
2893+ skb->csum);
2894+ data += fraggap;
2895+ pskb_trim_unique(skb_prev, maxfraglen);
2896+ }
2897+ copy = datalen - transhdrlen - fraggap;
2898+
2899+ if (copy < 0) {
2900+ err = -EINVAL;
2901+ kfree_skb(skb);
2902+ goto error;
2903+ } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
2904+ err = -EFAULT;
2905+ kfree_skb(skb);
2906+ goto error;
2907+ }
2908+
2909+ offset += copy;
2910+ length -= datalen - fraggap;
2911+ transhdrlen = 0;
2912+ exthdrlen = 0;
2913+ dst_exthdrlen = 0;
2914+
2915+ /*
2916+ * Put the packet on the pending queue
2917+ */
2918+ __skb_queue_tail(&sk->sk_write_queue, skb);
2919+ continue;
2920+ }
2921+
2922+ if (copy > length)
2923+ copy = length;
2924+
2925+ if (!(rt->dst.dev->features&NETIF_F_SG)) {
2926+ unsigned int off;
2927+
2928+ off = skb->len;
2929+ if (getfrag(from, skb_put(skb, copy),
2930+ offset, copy, off, skb) < 0) {
2931+ __skb_trim(skb, off);
2932+ err = -EFAULT;
2933+ goto error;
2934+ }
2935+ } else {
2936+ int i = skb_shinfo(skb)->nr_frags;
2937+ struct page_frag *pfrag = sk_page_frag(sk);
2938+
2939+ err = -ENOMEM;
2940+ if (!sk_page_frag_refill(sk, pfrag))
2941+ goto error;
2942+
2943+ if (!skb_can_coalesce(skb, i, pfrag->page,
2944+ pfrag->offset)) {
2945+ err = -EMSGSIZE;
2946+ if (i == MAX_SKB_FRAGS)
2947+ goto error;
2948+
2949+ __skb_fill_page_desc(skb, i, pfrag->page,
2950+ pfrag->offset, 0);
2951+ skb_shinfo(skb)->nr_frags = ++i;
2952+ get_page(pfrag->page);
2953+ }
2954+ copy = min_t(int, copy, pfrag->size - pfrag->offset);
2955+ if (getfrag(from,
2956+ page_address(pfrag->page) + pfrag->offset,
2957+ offset, copy, skb->len, skb) < 0)
2958+ goto error_efault;
2959+
2960+ pfrag->offset += copy;
2961+ skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
2962+ skb->len += copy;
2963+ skb->data_len += copy;
2964+ skb->truesize += copy;
2965+ atomic_add(copy, &sk->sk_wmem_alloc);
2966+ }
2967+ offset += copy;
2968+ length -= copy;
2969+ }
2970+
2971+ return 0;
2972+
2973+error_efault:
2974+ err = -EFAULT;
2975+error:
2976+ cork->length -= length;
2977+ IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
2978+ return err;
2979+}
2980+EXPORT_SYMBOL_GPL(ip6_append_data);
2981+
2982+static void ip6_cork_release(struct inet_sock *inet, struct ipv6_pinfo *np)
2983+{
2984+ if (np->cork.opt) {
2985+ kfree(np->cork.opt->dst0opt);
2986+ kfree(np->cork.opt->dst1opt);
2987+ kfree(np->cork.opt->hopopt);
2988+ kfree(np->cork.opt->srcrt);
2989+ kfree(np->cork.opt);
2990+ np->cork.opt = NULL;
2991+ }
2992+
2993+ if (inet->cork.base.dst) {
2994+ dst_release(inet->cork.base.dst);
2995+ inet->cork.base.dst = NULL;
2996+ inet->cork.base.flags &= ~IPCORK_ALLFRAG;
2997+ }
2998+ memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
2999+}
3000+
3001+int ip6_push_pending_frames(struct sock *sk)
3002+{
3003+ struct sk_buff *skb, *tmp_skb;
3004+ struct sk_buff **tail_skb;
3005+ struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
3006+ struct inet_sock *inet = inet_sk(sk);
3007+ struct ipv6_pinfo *np = inet6_sk(sk);
3008+ struct net *net = sock_net(sk);
3009+ struct ipv6hdr *hdr;
3010+ struct ipv6_txoptions *opt = np->cork.opt;
3011+ struct rt6_info *rt = (struct rt6_info *)inet->cork.base.dst;
3012+ struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
3013+ unsigned char proto = fl6->flowi6_proto;
3014+ int err = 0;
3015+
3016+ if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
3017+ goto out;
3018+ tail_skb = &(skb_shinfo(skb)->frag_list);
3019+
3020+ /* move skb->data to ip header from ext header */
3021+ if (skb->data < skb_network_header(skb))
3022+ __skb_pull(skb, skb_network_offset(skb));
3023+ while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
3024+ __skb_pull(tmp_skb, skb_network_header_len(skb));
3025+ *tail_skb = tmp_skb;
3026+ tail_skb = &(tmp_skb->next);
3027+ skb->len += tmp_skb->len;
3028+ skb->data_len += tmp_skb->len;
3029+ skb->truesize += tmp_skb->truesize;
3030+ tmp_skb->destructor = NULL;
3031+ tmp_skb->sk = NULL;
3032+ }
3033+
3034+ /* Allow local fragmentation. */
3035+ if (np->pmtudisc < IPV6_PMTUDISC_DO)
3036+ skb->local_df = 1;
3037+
3038+ *final_dst = fl6->daddr;
3039+ __skb_pull(skb, skb_network_header_len(skb));
3040+ if (opt && opt->opt_flen)
3041+ ipv6_push_frag_opts(skb, opt, &proto);
3042+ if (opt && opt->opt_nflen)
3043+ ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
3044+
3045+ skb_push(skb, sizeof(struct ipv6hdr));
3046+ skb_reset_network_header(skb);
3047+ hdr = ipv6_hdr(skb);
3048+
3049+ ip6_flow_hdr(hdr, np->cork.tclass, fl6->flowlabel);
3050+ hdr->hop_limit = np->cork.hop_limit;
3051+ hdr->nexthdr = proto;
3052+ hdr->saddr = fl6->saddr;
3053+ hdr->daddr = *final_dst;
3054+
3055+ skb->priority = sk->sk_priority;
3056+ skb->mark = sk->sk_mark;
3057+
3058+ skb_dst_set(skb, dst_clone(&rt->dst));
3059+ IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
3060+ if (proto == IPPROTO_ICMPV6) {
3061+ struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
3062+
3063+ ICMP6MSGOUT_INC_STATS_BH(net, idev, icmp6_hdr(skb)->icmp6_type);
3064+ ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
3065+ }
3066+
3067+ err = ip6_local_out(skb);
3068+ if (err) {
3069+ if (err > 0)
3070+ err = net_xmit_errno(err);
3071+ if (err)
3072+ goto error;
3073+ }
3074+
3075+out:
3076+ ip6_cork_release(inet, np);
3077+ return err;
3078+error:
3079+ IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
3080+ goto out;
3081+}
3082+EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
3083+
3084+void ip6_flush_pending_frames(struct sock *sk)
3085+{
3086+ struct sk_buff *skb;
3087+
3088+ while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
3089+ if (skb_dst(skb))
3090+ IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
3091+ IPSTATS_MIB_OUTDISCARDS);
3092+ kfree_skb(skb);
3093+ }
3094+
3095+ ip6_cork_release(inet_sk(sk), inet6_sk(sk));
3096+}
3097+EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);
3098diff -Naur linux-3.10.30.org/net/netfilter/core.c linux-3.10.30/net/netfilter/core.c
3099--- linux-3.10.30.org/net/netfilter/core.c 2014-02-13 22:48:15.000000000 +0100
3100+++ linux-3.10.30/net/netfilter/core.c 2014-02-14 20:29:05.392738001 +0100
3101@@ -191,9 +191,11 @@
3102 ret = NF_DROP_GETERR(verdict);
3103 if (ret == 0)
3104 ret = -EPERM;
3105- } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
3106+ } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE ||
3107+ (verdict & NF_VERDICT_MASK) == NF_IMQ_QUEUE) {
3108 int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
3109- verdict >> NF_VERDICT_QBITS);
3110+ verdict >> NF_VERDICT_QBITS,
3111+ verdict & NF_VERDICT_MASK);
3112 if (err < 0) {
3113 if (err == -ECANCELED)
3114 goto next_hook;
3115diff -Naur linux-3.10.30.org/net/netfilter/Kconfig linux-3.10.30/net/netfilter/Kconfig
3116--- linux-3.10.30.org/net/netfilter/Kconfig 2014-02-13 22:48:15.000000000 +0100
3117+++ linux-3.10.30/net/netfilter/Kconfig 2014-02-14 20:29:05.396071847 +0100
3118@@ -641,6 +641,18 @@
3119
3120 To compile it as a module, choose M here. If unsure, say N.
3121
3122+config NETFILTER_XT_TARGET_IMQ
3123+ tristate '"IMQ" target support'
3124+ depends on NETFILTER_XTABLES
3125+ depends on IP_NF_MANGLE || IP6_NF_MANGLE
3126+ select IMQ
3127+ default m if NETFILTER_ADVANCED=n
3128+ help
3129+ This option adds a `IMQ' target which is used to specify if and
3130+ to which imq device packets should get enqueued/dequeued.
3131+
3132+ To compile it as a module, choose M here. If unsure, say N.
3133+
3134 config NETFILTER_XT_TARGET_MARK
3135 tristate '"MARK" target support'
3136 depends on NETFILTER_ADVANCED
3137diff -Naur linux-3.10.30.org/net/netfilter/Makefile linux-3.10.30/net/netfilter/Makefile
3138--- linux-3.10.30.org/net/netfilter/Makefile 2014-02-13 22:48:15.000000000 +0100
3139+++ linux-3.10.30/net/netfilter/Makefile 2014-02-14 20:29:05.396071847 +0100
3140@@ -82,6 +82,7 @@
3141 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
3142 obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
3143 obj-$(CONFIG_NETFILTER_XT_TARGET_HMARK) += xt_HMARK.o
3144+obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o
3145 obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
3146 obj-$(CONFIG_NETFILTER_XT_TARGET_LOG) += xt_LOG.o
3147 obj-$(CONFIG_NETFILTER_XT_TARGET_NETMAP) += xt_NETMAP.o
3148diff -Naur linux-3.10.30.org/net/netfilter/nf_internals.h linux-3.10.30/net/netfilter/nf_internals.h
3149--- linux-3.10.30.org/net/netfilter/nf_internals.h 2014-02-13 22:48:15.000000000 +0100
3150+++ linux-3.10.30/net/netfilter/nf_internals.h 2014-02-14 20:29:05.396071847 +0100
3151@@ -29,7 +29,7 @@
3152 struct net_device *indev,
3153 struct net_device *outdev,
3154 int (*okfn)(struct sk_buff *),
3155- unsigned int queuenum);
3156+ unsigned int queuenum, unsigned int queuetype);
3157 extern int __init netfilter_queue_init(void);
3158
3159 /* nf_log.c */
3160diff -Naur linux-3.10.30.org/net/netfilter/nf_queue.c linux-3.10.30/net/netfilter/nf_queue.c
3161--- linux-3.10.30.org/net/netfilter/nf_queue.c 2014-02-13 22:48:15.000000000 +0100
3162+++ linux-3.10.30/net/netfilter/nf_queue.c 2014-02-14 20:29:05.396071847 +0100
3163@@ -27,6 +27,23 @@
3164 */
3165 static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
3166
3167+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
3168+static const struct nf_queue_handler __rcu *queue_imq_handler __read_mostly;
3169+
3170+void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
3171+{
3172+ rcu_assign_pointer(queue_imq_handler, qh);
3173+}
3174+EXPORT_SYMBOL_GPL(nf_register_queue_imq_handler);
3175+
3176+void nf_unregister_queue_imq_handler(void)
3177+{
3178+ RCU_INIT_POINTER(queue_imq_handler, NULL);
3179+ synchronize_rcu();
3180+}
3181+EXPORT_SYMBOL_GPL(nf_unregister_queue_imq_handler);
3182+#endif
3183+
3184 /* return EBUSY when somebody else is registered, return EEXIST if the
3185 * same handler is registered, return 0 in case of success. */
3186 void nf_register_queue_handler(const struct nf_queue_handler *qh)
3187@@ -105,7 +122,8 @@
3188 struct net_device *indev,
3189 struct net_device *outdev,
3190 int (*okfn)(struct sk_buff *),
3191- unsigned int queuenum)
3192+ unsigned int queuenum,
3193+ unsigned int queuetype)
3194 {
3195 int status = -ENOENT;
3196 struct nf_queue_entry *entry = NULL;
3197@@ -115,7 +133,17 @@
3198 /* QUEUE == DROP if no one is waiting, to be safe. */
3199 rcu_read_lock();
3200
3201- qh = rcu_dereference(queue_handler);
3202+ if (queuetype == NF_IMQ_QUEUE) {
3203+#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
3204+ qh = rcu_dereference(queue_imq_handler);
3205+#else
3206+ BUG();
3207+ goto err_unlock;
3208+#endif
3209+ } else {
3210+ qh = rcu_dereference(queue_handler);
3211+ }
3212+
3213 if (!qh) {
3214 status = -ESRCH;
3215 goto err_unlock;
3216@@ -205,9 +233,11 @@
3217 local_bh_enable();
3218 break;
3219 case NF_QUEUE:
3220+ case NF_IMQ_QUEUE:
3221 err = nf_queue(skb, elem, entry->pf, entry->hook,
3222 entry->indev, entry->outdev, entry->okfn,
3223- verdict >> NF_VERDICT_QBITS);
3224+ verdict >> NF_VERDICT_QBITS,
3225+ verdict & NF_VERDICT_MASK);
3226 if (err < 0) {
3227 if (err == -ECANCELED)
3228 goto next_hook;
3229diff -Naur linux-3.10.30.org/net/netfilter/xt_IMQ.c linux-3.10.30/net/netfilter/xt_IMQ.c
3230--- linux-3.10.30.org/net/netfilter/xt_IMQ.c 1970-01-01 01:00:00.000000000 +0100
3231+++ linux-3.10.30/net/netfilter/xt_IMQ.c 2014-02-14 20:29:05.396071847 +0100
3232@@ -0,0 +1,72 @@
3233+/*
3234+ * This target marks packets to be enqueued to an imq device
3235+ */
3236+#include <linux/module.h>
3237+#include <linux/skbuff.h>
3238+#include <linux/netfilter/x_tables.h>
3239+#include <linux/netfilter/xt_IMQ.h>
3240+#include <linux/imq.h>
3241+
3242+static unsigned int imq_target(struct sk_buff *pskb,
3243+ const struct xt_action_param *par)
3244+{
3245+ const struct xt_imq_info *mr = par->targinfo;
3246+
3247+ pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
3248+
3249+ return XT_CONTINUE;
3250+}
3251+
3252+static int imq_checkentry(const struct xt_tgchk_param *par)
3253+{
3254+ struct xt_imq_info *mr = par->targinfo;
3255+
3256+ if (mr->todev > IMQ_MAX_DEVS - 1) {
3257+ pr_warn("IMQ: invalid device specified, highest is %u\n",
3258+ IMQ_MAX_DEVS - 1);
3259+ return -EINVAL;
3260+ }
3261+
3262+ return 0;
3263+}
3264+
3265+static struct xt_target xt_imq_reg[] __read_mostly = {
3266+ {
3267+ .name = "IMQ",
3268+ .family = AF_INET,
3269+ .checkentry = imq_checkentry,
3270+ .target = imq_target,
3271+ .targetsize = sizeof(struct xt_imq_info),
3272+ .table = "mangle",
3273+ .me = THIS_MODULE
3274+ },
3275+ {
3276+ .name = "IMQ",
3277+ .family = AF_INET6,
3278+ .checkentry = imq_checkentry,
3279+ .target = imq_target,
3280+ .targetsize = sizeof(struct xt_imq_info),
3281+ .table = "mangle",
3282+ .me = THIS_MODULE
3283+ },
3284+};
3285+
3286+static int __init imq_init(void)
3287+{
3288+ return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
3289+}
3290+
3291+static void __exit imq_fini(void)
3292+{
3293+ xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
3294+}
3295+
3296+module_init(imq_init);
3297+module_exit(imq_fini);
3298+
3299+MODULE_AUTHOR("http://www.linuximq.net");
3300+MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information.");
3301+MODULE_LICENSE("GPL");
3302+MODULE_ALIAS("ipt_IMQ");
3303+MODULE_ALIAS("ip6t_IMQ");
3304+