1 diff -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
6 + * Pseudo-driver for the intermediate queue device.
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.
13 + * Authors: Patrick McHardy, <kaber@trash.net>
15 + * The first version was written by Martin Devera, <devik@cdi.cz>
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
24 + * After some time of inactivity there is a group taking care
25 + * of IMQ again: http://www.linuximq.net
28 + * 2004/06/30 - New version of IMQ patch to kernels <=2.6.7
29 + * including the following changes:
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)
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.
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.
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)
59 + * 2008/08/06 - 2.6.26 - (JK)
60 + * - Replaced tasklet with 'netif_schedule()'.
61 + * - Cleaned up and added comments for imq_nf_queue().
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)
71 + * - Port to 2.6.29 + fix rmmod not working
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.
77 + * 2009/09/26 - (Jussi Kivilinna)
78 + * - Add imq_nf_reinject_lockless to fix deadlock with
79 + * imq_nf_queue/imq_nf_reinject.
81 + * 2009/12/08 - (Jussi Kivilinna)
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
86 + * 2010/02/25 - (Jussi Kivilinna)
89 + * 2010/08/15 - (Jussi Kivilinna)
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
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>.
105 + * 2011/03/18 - (Jussi Kivilinna)
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).
113 + * 2011/07/26 - (Jussi Kivilinna)
114 + * - Add queue mapping checks for packets exiting IMQ.
117 + * 2011/08/16 - (Jussi Kivilinna)
118 + * - Clear IFF_TX_SKB_SHARING flag that was added for linux 3.0.2
120 + * 2011/11/03 - Germano Michel <germanomichel@gmail.com>
121 + * - Fix IMQ for net namespaces
123 + * 2011/11/04 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
125 + * - Clean-up, move 'get imq device pointer by imqX name' to
126 + * separate function from imq_nf_queue().
128 + * 2012/01/05 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
131 + * 2012/03/19 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
134 + * 2012/12/12 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
136 + * - Fix checkpatch.pl warnings
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.
142 + * Also, many thanks to pablo Sebastian Greco for making the initial
143 + * patch and to those who helped the testing.
145 + * More info at: http://www.linuximq.net/ (Andre Correa)
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>
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>
171 +#include <net/ipv6.h>
173 +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num);
175 +static nf_hookfn imq_nf_hook;
177 +static struct nf_hook_ops imq_ops[] = {
179 + /* imq_ingress_ipv4 */
180 + .hook = imq_nf_hook,
181 + .owner = THIS_MODULE,
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,
187 + .priority = NF_IP_PRI_NAT_DST + 1,
191 + /* imq_egress_ipv4 */
192 + .hook = imq_nf_hook,
193 + .owner = THIS_MODULE,
195 + .hooknum = NF_INET_POST_ROUTING,
196 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
197 + .priority = NF_IP_PRI_LAST,
199 + .priority = NF_IP_PRI_NAT_SRC - 1,
202 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
204 + /* imq_ingress_ipv6 */
205 + .hook = imq_nf_hook,
206 + .owner = THIS_MODULE,
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,
212 + .priority = NF_IP6_PRI_NAT_DST + 1,
216 + /* imq_egress_ipv6 */
217 + .hook = imq_nf_hook,
218 + .owner = THIS_MODULE,
220 + .hooknum = NF_INET_POST_ROUTING,
221 +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA)
222 + .priority = NF_IP6_PRI_LAST,
224 + .priority = NF_IP6_PRI_NAT_SRC - 1,
230 +#if defined(CONFIG_IMQ_NUM_DEVS)
231 +static int numdevs = CONFIG_IMQ_NUM_DEVS;
233 +static int numdevs = IMQ_MAX_DEVS;
236 +static struct net_device *imq_devs_cache[IMQ_MAX_DEVS];
238 +#define IMQ_MAX_QUEUES 32
239 +static int numqueues = 1;
240 +static u32 imq_hashrnd;
242 +static inline __be16 pppoe_proto(const struct sk_buff *skb)
244 + return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
245 + sizeof(struct pppoe_hdr)));
248 +static u16 imq_hash(struct net_device *dev, struct sk_buff *skb)
250 + unsigned int pull_len;
251 + u16 protocol = skb->protocol;
263 + switch (protocol) {
264 + case htons(ETH_P_8021Q): {
265 + if (unlikely(skb_pull(skb, VLAN_HLEN) == NULL))
268 + pull_len += VLAN_HLEN;
269 + skb->network_header += VLAN_HLEN;
271 + protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
275 + case htons(ETH_P_PPP_SES): {
276 + if (unlikely(skb_pull(skb, PPPOE_SES_HLEN) == NULL))
279 + pull_len += PPPOE_SES_HLEN;
280 + skb->network_header += PPPOE_SES_HLEN;
282 + protocol = pppoe_proto(skb);
286 + case htons(ETH_P_IP): {
287 + const struct iphdr *iph = ip_hdr(skb);
289 + if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr))))
292 + addr1 = iph->daddr;
293 + addr2 = iph->saddr;
295 + ip_proto = !(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) ?
297 + ihl = ip_hdrlen(skb);
301 +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
302 + case htons(ETH_P_IPV6): {
303 + const struct ipv6hdr *iph = ipv6_hdr(skb);
306 + if (unlikely(!pskb_may_pull(skb, sizeof(struct ipv6hdr))))
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,
313 + if (unlikely(ihl < 0))
321 + if (pull_len != 0) {
322 + skb_push(skb, pull_len);
323 + skb->network_header -= pull_len;
326 + return (u16)(ntohs(protocol) % dev->real_num_tx_queues);
330 + swap(addr1, addr2);
332 + switch (ip_proto) {
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]);
352 + if (pull_len != 0) {
353 + skb_push(skb, pull_len);
354 + skb->network_header -= pull_len;
357 + hash = jhash_3words(addr1, addr2, ports.in32, imq_hashrnd ^ ip_proto);
359 + return (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
362 +static inline bool sk_tx_queue_recorded(struct sock *sk)
364 + return (sk_tx_queue_get(sk) >= 0);
367 +static struct netdev_queue *imq_select_queue(struct net_device *dev,
368 + struct sk_buff *skb)
370 + u16 queue_index = 0;
373 + if (likely(dev->real_num_tx_queues == 1))
376 + /* IMQ can be receiving ingress or engress packets. */
378 + /* Check first for if rx_queue is set */
379 + if (skb_rx_queue_recorded(skb)) {
380 + queue_index = skb_get_rx_queue(skb);
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);
390 + /* Try use socket hash */
391 + if (skb->sk && skb->sk->sk_hash) {
392 + hash = skb->sk->sk_hash;
394 + (u16)(((u64)hash * dev->real_num_tx_queues) >> 32);
398 + /* Generate hash from packet data */
399 + queue_index = imq_hash(dev, skb);
402 + if (unlikely(queue_index >= dev->real_num_tx_queues))
403 + queue_index = (u16)((u32)queue_index % dev->real_num_tx_queues);
405 + skb_set_queue_mapping(skb, queue_index);
406 + return netdev_get_tx_queue(dev, queue_index);
409 +static struct net_device_stats *imq_get_stats(struct net_device *dev)
411 + return &dev->stats;
414 +/* called for packets kfree'd in qdiscs at places other than enqueue */
415 +static void imq_skb_destructor(struct sk_buff *skb)
417 + struct nf_queue_entry *entry = skb->nf_queue_entry;
419 + skb->nf_queue_entry = NULL;
422 + nf_queue_entry_release_refs(entry);
426 + skb_restore_cb(skb); /* kfree backup */
429 +static void imq_done_check_queue_mapping(struct sk_buff *skb,
430 + struct net_device *dev)
432 + unsigned int queue_index;
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);
443 + /* skb->dev was IMQ device itself or NULL, be on safe side and
444 + * just clear queue mapping.
446 + skb_set_queue_mapping(skb, 0);
450 +static netdev_tx_t imq_dev_xmit(struct sk_buff *skb, struct net_device *dev)
452 + struct nf_queue_entry *entry = skb->nf_queue_entry;
454 + skb->nf_queue_entry = NULL;
455 + dev->trans_start = jiffies;
457 + dev->stats.tx_bytes += skb->len;
458 + dev->stats.tx_packets++;
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.
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:..
468 + * WARNING: IMQ: kfree_skb: skb->nf_queue_entry...
470 + * However if this message is shown, then IMQ is somehow broken
471 + * and you should report this to linuximq.net.
474 + /* imq_dev_xmit is black hole that eats all packets, report that
475 + * we eat this packet happily and increase dropped counters.
478 + dev->stats.tx_dropped++;
479 + dev_kfree_skb(skb);
481 + return NETDEV_TX_OK;
484 + skb_restore_cb(skb); /* restore skb->cb */
486 + skb->imq_flags = 0;
487 + skb->destructor = NULL;
489 + imq_done_check_queue_mapping(skb, dev);
491 + nf_reinject(entry, NF_ACCEPT);
493 + return NETDEV_TX_OK;
496 +static struct net_device *get_imq_device_by_index(int index)
498 + struct net_device *dev = NULL;
502 + /* get device by name and cache result */
503 + snprintf(buf, sizeof(buf), "imq%d", index);
505 + /* Search device from all namespaces. */
506 + for_each_net(net) {
507 + dev = dev_get_by_name(net, buf);
512 + if (WARN_ON_ONCE(dev == NULL)) {
513 + /* IMQ device not found. Exotic config? */
514 + return ERR_PTR(-ENODEV);
517 + imq_devs_cache[index] = dev;
523 +static struct nf_queue_entry *nf_queue_entry_dup(struct nf_queue_entry *e)
525 + struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
527 + if (nf_queue_entry_get_refs(entry))
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.
539 +static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
541 + if (skb->nf_bridge)
542 + __skb_push(skb, skb->network_header - skb->mac_header);
545 +static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
547 + if (skb->nf_bridge)
548 + __skb_pull(skb, skb->network_header - skb->mac_header);
551 +#define nf_bridge_adjust_skb_data(s) do {} while (0)
552 +#define nf_bridge_adjust_segmented_data(s) do {} while (0)
555 +static void free_entry(struct nf_queue_entry *entry)
557 + nf_queue_entry_release_refs(entry);
561 +static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev);
563 +static int __imq_nf_queue_gso(struct nf_queue_entry *entry,
564 + struct net_device *dev, struct sk_buff *skb)
567 + struct nf_queue_entry *entry_seg;
569 + nf_bridge_adjust_segmented_data(skb);
571 + if (skb->next == NULL) { /* last packet, no need to copy entry */
572 + struct sk_buff *gso_skb = entry->skb;
574 + ret = __imq_nf_queue(entry, dev);
576 + entry->skb = gso_skb;
582 + entry_seg = nf_queue_entry_dup(entry);
584 + entry_seg->skb = skb;
585 + ret = __imq_nf_queue(entry_seg, dev);
587 + free_entry(entry_seg);
592 +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num)
594 + struct sk_buff *skb, *segs;
595 + struct net_device *dev;
596 + unsigned int queued;
597 + int index, retval, err;
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",
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);
613 + retval = PTR_ERR(dev);
618 + if (unlikely(!(dev->flags & IFF_UP))) {
619 + entry->skb->imq_flags = 0;
620 + retval = -ECANCELED;
624 + if (!skb_is_gso(entry->skb))
625 + return __imq_nf_queue(entry, dev);
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).
631 + * Following code replicates the gso handling from
632 + * 'net/netfilter/nfnetlink_queue_core.c':nfqnl_enqueue_packet().
637 + switch (entry->pf) {
639 + skb->protocol = htons(ETH_P_IP);
642 + skb->protocol = htons(ETH_P_IPV6);
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'.
658 + struct sk_buff *nskb = segs->next;
659 + if (nskb && nskb->next)
660 + nskb->cb_next = NULL;
662 + err = __imq_nf_queue_gso(entry, dev, segs);
671 + if (err) /* some segments are already queued */
678 + nf_bridge_adjust_segmented_data(skb);
684 +static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev)
686 + struct sk_buff *skb_orig, *skb, *skb_shared;
688 + struct netdev_queue *txq;
689 + spinlock_t *root_lock;
691 + int retval = -EINVAL;
692 + unsigned int orig_queue_index;
694 + dev->last_rx = jiffies;
699 + /* skb has owner? => make clone */
700 + if (unlikely(skb->destructor)) {
702 + skb = skb_clone(skb, GFP_ATOMIC);
703 + if (unlikely(!skb)) {
707 + skb->cb_next = NULL;
711 + skb->nf_queue_entry = entry;
713 + dev->stats.rx_bytes += skb->len;
714 + dev->stats.rx_packets++;
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");
727 + /* Disables softirqs for lock below */
728 + rcu_read_lock_bh();
730 + /* Multi-queue selection */
731 + orig_queue_index = skb_get_queue_mapping(skb);
732 + txq = imq_select_queue(dev, skb);
734 + q = rcu_dereference(txq->qdisc);
735 + if (unlikely(!q->enqueue))
736 + goto packet_not_eaten_by_imq_dev;
738 + root_lock = qdisc_lock(q);
739 + spin_lock(root_lock);
741 + users = atomic_read(&skb->users);
743 + skb_shared = skb_get(skb); /* increase reference count by one */
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 */
749 + if (likely(atomic_read(&skb_shared->users) == users + 1)) {
750 + kfree_skb(skb_shared); /* decrease reference count by one */
752 + skb->destructor = &imq_skb_destructor;
755 + if (unlikely(skb_orig))
756 + kfree_skb(skb_orig); /* free original */
758 + spin_unlock(root_lock);
759 + rcu_read_unlock_bh();
761 + /* schedule qdisc dequeue */
762 + __netif_schedule(q);
767 + skb_restore_cb(skb_shared); /* restore skb->cb */
768 + skb->nf_queue_entry = NULL;
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.
774 + spin_unlock(root_lock);
775 + goto packet_not_eaten_by_imq_dev;
778 +packet_not_eaten_by_imq_dev:
779 + skb_set_queue_mapping(skb, orig_queue_index);
780 + rcu_read_unlock_bh();
782 + /* cloned? restore original */
783 + if (unlikely(skb_orig)) {
785 + entry->skb = skb_orig;
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 *))
797 + return (pskb->imq_flags & IMQ_F_ENQUEUE) ? NF_IMQ_QUEUE : NF_ACCEPT;
800 +static int imq_close(struct net_device *dev)
802 + netif_stop_queue(dev);
806 +static int imq_open(struct net_device *dev)
808 + netif_start_queue(dev);
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,
819 +static void imq_setup(struct net_device *dev)
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 |
829 + dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE |
830 + IFF_TX_SKB_SHARING);
833 +static int imq_validate(struct nlattr *tb[], struct nlattr *data[])
837 + if (tb[IFLA_ADDRESS]) {
838 + if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
842 + if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
843 + ret = -EADDRNOTAVAIL;
849 + pr_warn("IMQ: imq_validate failed (%d)\n", ret);
853 +static struct rtnl_link_ops imq_link_ops __read_mostly = {
856 + .setup = imq_setup,
857 + .validate = imq_validate,
860 +static const struct nf_queue_handler imq_nfqh = {
861 + .outfn = imq_nf_queue,
864 +static int __init imq_init_hooks(void)
868 + nf_register_queue_imq_handler(&imq_nfqh);
870 + ret = nf_register_hooks(imq_ops, ARRAY_SIZE(imq_ops));
872 + nf_unregister_queue_imq_handler();
877 +static int __init imq_init_one(int index)
879 + struct net_device *dev;
882 + dev = alloc_netdev_mq(0, "imq%d", imq_setup, numqueues);
886 + ret = dev_alloc_name(dev, dev->name);
890 + dev->rtnl_link_ops = &imq_link_ops;
891 + ret = register_netdevice(dev);
901 +static int __init imq_init_devs(void)
905 + if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) {
906 + pr_err("IMQ: numdevs has to be betweed 1 and %u\n",
911 + if (numqueues < 1 || numqueues > IMQ_MAX_QUEUES) {
912 + pr_err("IMQ: numqueues has to be betweed 1 and %u\n",
917 + get_random_bytes(&imq_hashrnd, sizeof(imq_hashrnd));
920 + err = __rtnl_link_register(&imq_link_ops);
922 + for (i = 0; i < numdevs && !err; i++)
923 + err = imq_init_one(i);
926 + __rtnl_link_unregister(&imq_link_ops);
927 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
934 +static int __init imq_init_module(void)
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);
944 + err = imq_init_devs();
946 + pr_err("IMQ: Error trying imq_init_devs(net)\n");
950 + err = imq_init_hooks();
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));
958 + pr_info("IMQ driver loaded successfully. (numdevs = %d, numqueues = %d)\n",
959 + numdevs, numqueues);
961 +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB)
962 + pr_info("\tHooking IMQ before NAT on PREROUTING.\n");
964 + pr_info("\tHooking IMQ after NAT on PREROUTING.\n");
966 +#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB)
967 + pr_info("\tHooking IMQ before NAT on POSTROUTING.\n");
969 + pr_info("\tHooking IMQ after NAT on POSTROUTING.\n");
975 +static void __exit imq_unhook(void)
977 + nf_unregister_hooks(imq_ops, ARRAY_SIZE(imq_ops));
978 + nf_unregister_queue_imq_handler();
981 +static void __exit imq_cleanup_devs(void)
983 + rtnl_link_unregister(&imq_link_ops);
984 + memset(imq_devs_cache, 0, sizeof(imq_devs_cache));
987 +static void __exit imq_exit_module(void)
990 + imq_cleanup_devs();
991 + pr_info("IMQ driver unloaded successfully.\n");
994 +module_init(imq_init_module);
995 +module_exit(imq_exit_module);
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");
1006 diff -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 @@
1014 + tristate "IMQ (intermediate queueing device) support"
1015 + depends on NETDEVICES && NETFILTER
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.
1024 + More information at: http://www.linuximq.net/
1026 + To compile this driver as a module, choose M here: the module
1027 + will be called imq. If unsure, say N.
1030 + prompt "IMQ behavior (PRE/POSTROUTING)"
1032 + default IMQ_BEHAVIOR_AB
1034 + This setting defines how IMQ behaves in respect to its
1035 + hooking in PREROUTING and POSTROUTING.
1037 + IMQ can work in any of the following ways:
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
1046 + The default behavior is to hook before NAT on PREROUTING
1047 + and after NAT on POSTROUTING (#3).
1049 + This settings are specially usefull when trying to use IMQ
1050 + to shape NATed clients.
1052 + More information can be found at: www.linuximq.net
1054 + If not sure leave the default settings alone.
1056 +config IMQ_BEHAVIOR_AA
1059 + This setting defines how IMQ behaves in respect to its
1060 + hooking in PREROUTING and POSTROUTING.
1062 + Choosing this option will make IMQ hook like this:
1064 + PREROUTING: After NAT
1065 + POSTROUTING: After NAT
1067 + More information can be found at: www.linuximq.net
1069 + If not sure leave the default settings alone.
1071 +config IMQ_BEHAVIOR_AB
1074 + This setting defines how IMQ behaves in respect to its
1075 + hooking in PREROUTING and POSTROUTING.
1077 + Choosing this option will make IMQ hook like this:
1079 + PREROUTING: After NAT
1080 + POSTROUTING: Before NAT
1082 + More information can be found at: www.linuximq.net
1084 + If not sure leave the default settings alone.
1086 +config IMQ_BEHAVIOR_BA
1089 + This setting defines how IMQ behaves in respect to its
1090 + hooking in PREROUTING and POSTROUTING.
1092 + Choosing this option will make IMQ hook like this:
1094 + PREROUTING: Before NAT
1095 + POSTROUTING: After NAT
1097 + More information can be found at: www.linuximq.net
1099 + If not sure leave the default settings alone.
1101 +config IMQ_BEHAVIOR_BB
1104 + This setting defines how IMQ behaves in respect to its
1105 + hooking in PREROUTING and POSTROUTING.
1107 + Choosing this option will make IMQ hook like this:
1109 + PREROUTING: Before NAT
1110 + POSTROUTING: Before NAT
1112 + More information can be found at: www.linuximq.net
1114 + If not sure leave the default settings alone.
1118 +config IMQ_NUM_DEVS
1119 + int "Number of IMQ devices"
1124 + This setting defines how many IMQ devices will be created.
1126 + The default value is 16.
1128 + More information can be found at: www.linuximq.net
1130 + If not sure leave the default settings alone.
1133 tristate "Universal TUN/TAP device driver support"
1135 diff -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
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
1146 diff -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
1153 +/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */
1154 +#define IMQ_F_BITS 5
1156 +#define IMQ_F_IFMASK 0x0f
1157 +#define IMQ_F_ENQUEUE 0x10
1159 +#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1)
1161 +#endif /* _IMQ_H */
1163 diff -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
1170 +struct xt_imq_info {
1171 + unsigned int todev; /* target imq device */
1174 +#endif /* _XT_IMQ_H */
1176 diff -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
1183 +/* Backwards compatibility for old userspace */
1184 +#include <linux/netfilter/xt_IMQ.h>
1186 +#define ipt_imq_info xt_imq_info
1188 +#endif /* _IPT_IMQ_H */
1190 diff -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
1194 +#ifndef _IP6T_IMQ_H
1195 +#define _IP6T_IMQ_H
1197 +/* Backwards compatibility for old userspace */
1198 +#include <linux/netfilter/xt_IMQ.h>
1200 +#define ip6t_imq_info xt_imq_info
1202 +#endif /* _IP6T_IMQ_H */
1204 diff -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
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>
1215 /* Don't change this without changing skb_csum_unnecessary! */
1216 #define CHECKSUM_NONE 0
1218 * first. This is owned by whoever has the skb queued ATM.
1220 char cb[48] __aligned(8);
1221 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1225 unsigned long _skb_refdst;
1228 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1229 struct nf_conntrack *nfct;
1231 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1232 + struct nf_queue_entry *nf_queue_entry;
1234 #ifdef CONFIG_BRIDGE_NETFILTER
1235 struct nf_bridge_info *nf_bridge;
1238 __u8 encapsulation:1;
1239 /* 7/9 bit hole (depending on ndisc_nodetype presence) */
1240 kmemcheck_bitfield_end(flags2);
1242 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1243 + __u8 imq_flags:IMQ_F_BITS;
1245 #ifdef CONFIG_NET_DMA
1246 dma_cookie_t dma_cookie;
1248 @@ -616,7 +627,10 @@
1250 return (struct rtable *)skb_dst(skb);
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);
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;
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;
1268 #ifdef CONFIG_BRIDGE_NETFILTER
1269 dst->nf_bridge = src->nf_bridge;
1270 nf_bridge_get(src->nf_bridge);
1271 diff -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
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);
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);
1285 bool nf_queue_entry_get_refs(struct nf_queue_entry *entry);
1286 void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
1287 diff -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
1294 -#define NF_MAX_VERDICT NF_STOP
1295 +#define NF_IMQ_QUEUE 6
1296 +#define NF_MAX_VERDICT NF_IMQ_QUEUE
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
1300 diff -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
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>
1311 #include "net-sysfs.h"
1313 @@ -2573,7 +2576,12 @@
1317 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1318 + if (!list_empty(&ptype_all) &&
1319 + !(skb->imq_flags & IMQ_F_ENQUEUE))
1321 if (!list_empty(&ptype_all))
1323 dev_queue_xmit_nit(skb, dev);
1326 diff -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
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;
1338 * skb_panic - private function for out-of-line support
1339 @@ -552,6 +555,29 @@
1341 skb->destructor(skb);
1343 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1345 + * This should not happen. When it does, avoid memleak by restoring
1346 + * the chain of cb-backups.
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);
1353 + skb_restore_cb(skb);
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.
1362 + if (skb->nf_queue_entry && net_ratelimit())
1363 + pr_warn("%s\n", "IMQ: kfree_skb: skb->nf_queue_entry != NULL");
1366 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1367 nf_conntrack_put(skb->nfct);
1369 @@ -683,6 +709,10 @@
1370 new->sp = secpath_get(old->sp);
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);*/
1377 new->csum = old->csum;
1378 new->local_df = old->local_df;
1379 new->pkt_type = old->pkt_type;
1380 @@ -3050,6 +3080,15 @@
1382 EXPORT_SYMBOL_GPL(skb_gro_receive);
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);
1393 void __init skb_init(void)
1395 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1396 @@ -3063,6 +3102,13 @@
1398 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
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),
1404 + SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1410 @@ -3348,6 +3394,76 @@
1411 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
1414 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
1415 +static DEFINE_SPINLOCK(skb_cb_store_lock);
1417 +int skb_save_cb(struct sk_buff *skb)
1419 + struct skb_cb_table *next;
1421 + next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC);
1425 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1427 + memcpy(next->cb, skb->cb, sizeof(skb->cb));
1428 + next->cb_next = skb->cb_next;
1430 + atomic_set(&next->refcnt, 1);
1432 + skb->cb_next = next;
1435 +EXPORT_SYMBOL(skb_save_cb);
1437 +int skb_restore_cb(struct sk_buff *skb)
1439 + struct skb_cb_table *next;
1441 + if (!skb->cb_next)
1444 + next = skb->cb_next;
1446 + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb));
1448 + memcpy(skb->cb, next->cb, sizeof(skb->cb));
1449 + skb->cb_next = next->cb_next;
1451 + spin_lock(&skb_cb_store_lock);
1453 + if (atomic_dec_and_test(&next->refcnt))
1454 + kmem_cache_free(skbuff_cb_store_cache, next);
1456 + spin_unlock(&skb_cb_store_lock);
1460 +EXPORT_SYMBOL(skb_restore_cb);
1462 +static void skb_copy_stored_cb(struct sk_buff *new, const struct sk_buff *__old)
1464 + struct skb_cb_table *next;
1465 + struct sk_buff *old;
1467 + if (!__old->cb_next) {
1468 + new->cb_next = NULL;
1472 + spin_lock(&skb_cb_store_lock);
1474 + old = (struct sk_buff *)__old;
1476 + next = old->cb_next;
1477 + atomic_inc(&next->refcnt);
1478 + new->cb_next = next;
1480 + spin_unlock(&skb_cb_store_lock);
1485 * skb_partial_csum_set - set up and verify partial csum values for packet
1486 * @skb: the skb to set
1487 diff -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
1491 struct in6_addr *nexthop;
1494 - skb->protocol = htons(ETH_P_IPV6);
1497 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
1498 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
1500 @@ -168,6 +165,13 @@
1505 + * IMQ-patch: moved setting skb->dev and skb->protocol from
1506 + * ip6_finish_output2 to fix crashing at netif_skb_features().
1508 + skb->protocol = htons(ETH_P_IPV6);
1511 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
1513 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
1514 diff -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
1519 + * IPv6 output functions
1520 + * Linux INET6 implementation
1523 + * Pedro Roque <roque@di.fc.ul.pt>
1525 + * Based on linux/net/ipv4/ip_output.c
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.
1533 + * A.N.Kuznetsov : airthmetics in fragmentation.
1534 + * extension headers are implemented.
1535 + * route changes now work.
1536 + * ip6_forward does not confuse sniffers.
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
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>
1559 +#include <linux/netfilter.h>
1560 +#include <linux/netfilter_ipv6.h>
1562 +#include <net/sock.h>
1563 +#include <net/snmp.h>
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>
1576 +int __ip6_local_out(struct sk_buff *skb)
1580 + len = skb->len - sizeof(struct ipv6hdr);
1581 + if (len > IPV6_MAXPLEN)
1583 + ipv6_hdr(skb)->payload_len = htons(len);
1585 + return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
1586 + skb_dst(skb)->dev, dst_output);
1589 +int ip6_local_out(struct sk_buff *skb)
1593 + err = __ip6_local_out(skb);
1594 + if (likely(err == 1))
1595 + err = dst_output(skb);
1599 +EXPORT_SYMBOL_GPL(ip6_local_out);
1601 +static int ip6_finish_output2(struct sk_buff *skb)
1603 + struct dst_entry *dst = skb_dst(skb);
1604 + struct net_device *dev = dst->dev;
1605 + struct neighbour *neigh;
1606 + struct in6_addr *nexthop;
1609 + skb->protocol = htons(ETH_P_IPV6);
1612 + if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
1613 + struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
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);
1622 + /* Do not check for IFF_ALLMULTI; multicast routing
1623 + is not supported in any case.
1626 + NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
1627 + newskb, NULL, newskb->dev,
1628 + dev_loopback_xmit);
1630 + if (ipv6_hdr(skb)->hop_limit == 0) {
1631 + IP6_INC_STATS(dev_net(dev), idev,
1632 + IPSTATS_MIB_OUTDISCARDS);
1638 + IP6_UPD_PO_STATS(dev_net(dev), idev, IPSTATS_MIB_OUTMCAST,
1641 + if (IPV6_ADDR_MC_SCOPE(&ipv6_hdr(skb)->daddr) <=
1642 + IPV6_ADDR_SCOPE_NODELOCAL &&
1643 + !(dev->flags & IFF_LOOPBACK)) {
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();
1659 + rcu_read_unlock_bh();
1661 + IP6_INC_STATS(dev_net(dst->dev),
1662 + ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
1667 +static int ip6_finish_output(struct sk_buff *skb)
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);
1674 + return ip6_finish_output2(skb);
1677 +int ip6_output(struct sk_buff *skb)
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);
1688 + return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
1689 + ip6_finish_output,
1690 + !(IP6CB(skb)->flags & IP6SKB_REROUTED));
1694 + * xmit an sk_buff (used by TCP, SCTP and DCCP)
1697 +int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
1698 + struct ipv6_txoptions *opt, int tclass)
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;
1711 + unsigned int head_room;
1713 + /* First: exthdrs may take lots of space (~8K for now)
1714 + MAX_HEADER is not enough.
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);
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);
1730 + skb_set_owner_w(skb, sk);
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);
1738 + skb_push(skb, sizeof(struct ipv6hdr));
1739 + skb_reset_network_header(skb);
1740 + hdr = ipv6_hdr(skb);
1743 + * Fill in the IPv6 header
1746 + hlimit = np->hop_limit;
1748 + hlimit = ip6_dst_hoplimit(dst);
1750 + ip6_flow_hdr(hdr, tclass, fl6->flowlabel);
1752 + hdr->payload_len = htons(seg_len);
1753 + hdr->nexthdr = proto;
1754 + hdr->hop_limit = hlimit;
1756 + hdr->saddr = fl6->saddr;
1757 + hdr->daddr = *first_hop;
1759 + skb->priority = sk->sk_priority;
1760 + skb->mark = sk->sk_mark;
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);
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);
1777 +EXPORT_SYMBOL(ip6_xmit);
1779 +static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
1781 + struct ip6_ra_chain *ra;
1782 + struct sock *last = NULL;
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)) {
1791 + struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1793 + rawv6_rcv(last, skb2);
1800 + rawv6_rcv(last, skb);
1801 + read_unlock(&ip6_ra_lock);
1804 + read_unlock(&ip6_ra_lock);
1808 +static int ip6_forward_proxy_check(struct sk_buff *skb)
1810 + struct ipv6hdr *hdr = ipv6_hdr(skb);
1811 + u8 nexthdr = hdr->nexthdr;
1815 + if (ipv6_ext_hdr(nexthdr)) {
1816 + offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
1820 + offset = sizeof(struct ipv6hdr);
1822 + if (nexthdr == IPPROTO_ICMPV6) {
1823 + struct icmp6hdr *icmp6;
1825 + if (!pskb_may_pull(skb, (skb_network_header(skb) +
1826 + offset + 1 - skb->data)))
1829 + icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
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
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.
1852 + if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
1853 + dst_link_failure(skb);
1860 +static inline int ip6_forward_finish(struct sk_buff *skb)
1862 + return dst_output(skb);
1865 +int ip6_forward(struct sk_buff *skb)
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);
1873 + if (net->ipv6.devconf_all->forwarding == 0)
1876 + if (skb_warn_if_lro(skb))
1879 + if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
1880 + IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
1884 + if (skb->pkt_type != PACKET_HOST)
1887 + skb_forward_csum(skb);
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.
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
1902 + if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
1903 + if (ip6_call_ra_chain(skb, ntohs(opt->ra)))
1908 + * check and decrement ttl
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);
1918 + return -ETIMEDOUT;
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);
1926 + return ip6_input(skb);
1927 + else if (proxied < 0) {
1928 + IP6_INC_STATS(net, ip6_dst_idev(dst),
1929 + IPSTATS_MIB_INDISCARDS);
1934 + if (!xfrm6_route_forward(skb)) {
1935 + IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
1938 + dst = skb_dst(skb);
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.
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;
1950 + * incoming and outgoing devices are the same
1951 + * send a redirect.
1954 + rt = (struct rt6_info *) dst;
1955 + if (rt->rt6i_flags & RTF_GATEWAY)
1956 + target = &rt->rt6i_gateway;
1958 + target = &hdr->daddr;
1960 + peer = inet_getpeer_v6(net->ipv6.peers, &rt->rt6i_dst.addr, 1);
1962 + /* Limit redirects both by destination (here)
1963 + and by source (inside ndisc_send_redirect)
1965 + if (inet_peer_xrlim_allow(peer, 1*HZ))
1966 + ndisc_send_redirect(skb, target);
1968 + inet_putpeer(peer);
1970 + int addrtype = ipv6_addr_type(&hdr->saddr);
1972 + /* This check is security critical. */
1973 + if (addrtype == IPV6_ADDR_ANY ||
1974 + addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
1976 + if (addrtype & IPV6_ADDR_LINKLOCAL) {
1977 + icmpv6_send(skb, ICMPV6_DEST_UNREACH,
1978 + ICMPV6_NOT_NEIGHBOUR, 0);
1983 + mtu = dst_mtu(dst);
1984 + if (mtu < IPV6_MIN_MTU)
1985 + mtu = IPV6_MIN_MTU;
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);
2000 + if (skb_cow(skb, dst->dev->hard_header_len)) {
2001 + IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
2005 + hdr = ipv6_hdr(skb);
2007 + /* Mangling hops number delayed to point after skb COW */
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);
2017 + IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
2023 +static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
2025 + to->pkt_type = from->pkt_type;
2026 + to->priority = from->priority;
2027 + to->protocol = from->protocol;
2029 + skb_dst_set(to, dst_clone(skb_dst(from)));
2030 + to->dev = from->dev;
2031 + to->mark = from->mark;
2033 +#ifdef CONFIG_NET_SCHED
2034 + to->tc_index = from->tc_index;
2036 + nf_copy(to, from);
2037 +#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
2038 + to->nf_trace = from->nf_trace;
2040 + skb_copy_secmark(to, from);
2043 +int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
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;
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);
2057 + hlen = ip6_find_1stfragopt(skb, &prevhdr);
2058 + nexthdr = *prevhdr;
2060 + mtu = ip6_skb_dst_mtu(skb);
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.
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);
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);
2079 + if (np && np->frag_size < mtu) {
2080 + if (np->frag_size)
2081 + mtu = np->frag_size;
2083 + mtu -= hlen + sizeof(struct frag_hdr);
2085 + if (skb_has_frag_list(skb)) {
2086 + int first_len = skb_pagelen(skb);
2087 + struct sk_buff *frag2;
2089 + if (first_len - hlen > mtu ||
2090 + ((first_len - hlen) & 7) ||
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;
2101 + /* Partially cloned skb? */
2102 + if (skb_shared(frag))
2103 + goto slow_path_clean;
2107 + frag->sk = skb->sk;
2108 + frag->destructor = sock_wfree;
2110 + skb->truesize -= frag->truesize;
2115 + frag = skb_shinfo(skb)->frag_list;
2116 + skb_frag_list_init(skb);
2117 + /* BUILD HEADER */
2119 + *prevhdr = NEXTHDR_FRAGMENT;
2120 + tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
2122 + IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2123 + IPSTATS_MIB_FRAGFAILS);
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);
2133 + ipv6_select_ident(fh, rt);
2134 + fh->nexthdr = nexthdr;
2136 + fh->frag_off = htons(IP6_MF);
2137 + frag_id = fh->identification;
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));
2145 + dst_hold(&rt->dst);
2148 + /* Prepare header of the next frame,
2149 + * before previous one went down. */
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,
2158 + offset += skb->len - hlen - sizeof(struct frag_hdr);
2159 + fh->nexthdr = nexthdr;
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 =
2167 + sizeof(struct ipv6hdr));
2168 + ip6_copy_metadata(frag, skb);
2171 + err = output(skb);
2173 + IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
2174 + IPSTATS_MIB_FRAGCREATES);
2187 + IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
2188 + IPSTATS_MIB_FRAGOKS);
2199 + IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
2200 + IPSTATS_MIB_FRAGFAILS);
2205 + skb_walk_frags(skb, frag2) {
2206 + if (frag2 == frag)
2209 + frag2->destructor = NULL;
2210 + skb->truesize += frag2->truesize;
2215 + if ((skb->ip_summed == CHECKSUM_PARTIAL) &&
2216 + skb_checksum_help(skb))
2219 + left = skb->len - hlen; /* Space per frame */
2220 + ptr = hlen; /* Where to start from */
2223 + * Fragment the datagram.
2226 + *prevhdr = NEXTHDR_FRAGMENT;
2227 + hroom = LL_RESERVED_SPACE(rt->dst.dev);
2228 + troom = rt->dst.dev->needed_tailroom;
2231 + * Keep copying data until we run out.
2235 + /* IF: it doesn't fit, use 'mtu' - the data space left */
2238 + /* IF: we are not sending up to and including the packet end
2239 + then align the next start on an eight byte boundary */
2244 + * Allocate buffer.
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);
2257 + * Set up data on packet
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));
2269 + * Charge the memory for the fragment to any owner
2270 + * it might possess
2273 + skb_set_owner_w(frag, skb->sk);
2276 + * Copy the packet header into the new buffer.
2278 + skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
2281 + * Build fragment header.
2283 + fh->nexthdr = nexthdr;
2286 + ipv6_select_ident(fh, rt);
2287 + frag_id = fh->identification;
2289 + fh->identification = frag_id;
2292 + * Copy a block of the IP datagram.
2294 + if (skb_copy_bits(skb, ptr, skb_transport_header(frag), len))
2298 + fh->frag_off = htons(offset);
2300 + fh->frag_off |= htons(IP6_MF);
2301 + ipv6_hdr(frag)->payload_len = htons(frag->len -
2302 + sizeof(struct ipv6hdr));
2308 + * Put this fragment into the sending queue.
2310 + err = output(frag);
2314 + IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2315 + IPSTATS_MIB_FRAGCREATES);
2317 + IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2318 + IPSTATS_MIB_FRAGOKS);
2323 + IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2324 + IPSTATS_MIB_FRAGFAILS);
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)
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));
2337 +static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
2338 + struct dst_entry *dst,
2339 + const struct flowi6 *fl6)
2341 + struct ipv6_pinfo *np = inet6_sk(sk);
2342 + struct rt6_info *rt;
2347 + if (dst->ops->family != AF_INET6) {
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)
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
2368 + * 2. oif also should be the same.
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) ||
2374 + (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex)) {
2383 +static int ip6_dst_lookup_tail(struct sock *sk,
2384 + struct dst_entry **dst, struct flowi6 *fl6)
2386 + struct net *net = sock_net(sk);
2387 +#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2388 + struct neighbour *n;
2389 + struct rt6_info *rt;
2394 + *dst = ip6_route_output(net, sk, fl6);
2396 + if ((err = (*dst)->error))
2397 + goto out_err_release;
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,
2405 + goto out_err_release;
2408 +#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
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
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();
2424 + struct inet6_ifaddr *ifp;
2425 + struct flowi6 fl_gw6;
2428 + ifp = ipv6_get_ifaddr(net, &fl6->saddr,
2431 + redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
2437 + * We need to get the dst entry for the
2438 + * default router instead
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;
2453 + if (err == -ENETUNREACH)
2454 + IP6_INC_STATS_BH(net, NULL, IPSTATS_MIB_OUTNOROUTES);
2455 + dst_release(*dst);
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
2466 + * This function performs a route lookup on the given flow.
2468 + * It returns zero on success, or a standard errno code on error.
2470 +int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6)
2473 + return ip6_dst_lookup_tail(sk, dst, fl6);
2475 +EXPORT_SYMBOL_GPL(ip6_dst_lookup);
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
2484 + * This function performs a route lookup on the given flow.
2486 + * It returns a valid dst pointer on success, or a pointer encoded
2489 +struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
2490 + const struct in6_addr *final_dst,
2493 + struct dst_entry *dst = NULL;
2496 + err = ip6_dst_lookup_tail(sk, &dst, fl6);
2498 + return ERR_PTR(err);
2500 + fl6->daddr = *final_dst;
2502 + fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
2504 + return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
2506 +EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
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
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.
2520 + * It returns a valid dst pointer on success, or a pointer encoded
2523 +struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
2524 + const struct in6_addr *final_dst,
2527 + struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
2530 + dst = ip6_sk_dst_check(sk, dst, fl6);
2532 + err = ip6_dst_lookup_tail(sk, &dst, fl6);
2534 + return ERR_PTR(err);
2536 + fl6->daddr = *final_dst;
2538 + fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
2540 + return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
2542 +EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
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)
2552 + struct sk_buff *skb;
2555 + /* There is support for UDP large send offload by network
2556 + * device, so create one single skb packet containing complete
2559 + if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
2560 + struct frag_hdr fhdr;
2562 + skb = sock_alloc_send_skb(sk,
2563 + hh_len + fragheaderlen + transhdrlen + 20,
2564 + (flags & MSG_DONTWAIT), &err);
2568 + /* reserve space for Hardware header */
2569 + skb_reserve(skb, hh_len);
2571 + /* create space for UDP/IP header */
2572 + skb_put(skb,fragheaderlen + transhdrlen);
2574 + /* initialize network header pointer */
2575 + skb_reset_network_header(skb);
2577 + /* initialize protocol header pointer */
2578 + skb->transport_header = skb->network_header + fragheaderlen;
2580 + skb->ip_summed = CHECKSUM_PARTIAL;
2583 + /* Specify the length of each IPv6 datagram fragment.
2584 + * It has to be a multiple of 8.
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);
2594 + return skb_append_datato_frags(sk, skb, getfrag, from,
2595 + (length - transhdrlen));
2598 +static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
2601 + return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
2604 +static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
2607 + return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
2610 +static void ip6_append_data_mtu(unsigned int *mtu,
2612 + unsigned int fragheaderlen,
2613 + struct sk_buff *skb,
2614 + struct rt6_info *rt,
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;
2624 + * this fragment is not first, the headers
2625 + * space is regarded as data space.
2627 + *mtu = min(*mtu, pmtuprobe ?
2628 + rt->dst.dev->mtu :
2629 + dst_mtu(rt->dst.path));
2631 + *maxfraglen = ((*mtu - fragheaderlen) & ~7)
2632 + + fragheaderlen - sizeof(struct frag_hdr);
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)
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;
2648 + int dst_exthdrlen;
2653 + __u8 tx_flags = 0;
2655 + if (flags&MSG_PROBE)
2657 + cork = &inet->cork.base;
2658 + if (skb_queue_empty(&sk->sk_write_queue)) {
2660 + * setup for corking
2663 + if (WARN_ON(np->cork.opt))
2666 + np->cork.opt = kzalloc(opt->tot_len, sk->sk_allocation);
2667 + if (unlikely(np->cork.opt == NULL))
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;
2674 + np->cork.opt->dst0opt = ip6_opt_dup(opt->dst0opt,
2675 + sk->sk_allocation);
2676 + if (opt->dst0opt && !np->cork.opt->dst0opt)
2679 + np->cork.opt->dst1opt = ip6_opt_dup(opt->dst1opt,
2680 + sk->sk_allocation);
2681 + if (opt->dst1opt && !np->cork.opt->dst1opt)
2684 + np->cork.opt->hopopt = ip6_opt_dup(opt->hopopt,
2685 + sk->sk_allocation);
2686 + if (opt->hopopt && !np->cork.opt->hopopt)
2689 + np->cork.opt->srcrt = ip6_rthdr_dup(opt->srcrt,
2690 + sk->sk_allocation);
2691 + if (opt->srcrt && !np->cork.opt->srcrt)
2694 + /* need source address above miyazawa*/
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);
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;
2711 + cork->fragsize = mtu;
2712 + if (dst_allfrag(rt->dst.path))
2713 + cork->flags |= IPCORK_ALLFRAG;
2715 + exthdrlen = (opt ? opt->opt_flen : 0);
2716 + length += exthdrlen;
2717 + transhdrlen += exthdrlen;
2718 + dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len;
2720 + rt = (struct rt6_info *)cork->dst;
2721 + fl6 = &inet->cork.fl.u.ip6;
2722 + opt = np->cork.opt;
2725 + dst_exthdrlen = 0;
2726 + mtu = cork->fragsize;
2729 + hh_len = LL_RESERVED_SPACE(rt->dst.dev);
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);
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);
2742 + /* For UDP, check if TX timestamp is enabled */
2743 + if (sk->sk_type == SOCK_DGRAM)
2744 + sock_tx_timestamp(sk, &tx_flags);
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).
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
2756 + * FIXME: It may be fragmented into multiple chunks
2757 + * at once if non-fragmentable extension headers
2762 + if ((length > mtu) && dontfrag && (sk->sk_protocol == IPPROTO_UDP ||
2763 + sk->sk_protocol == IPPROTO_RAW)) {
2764 + ipv6_local_rxpmtu(sk, fl6, mtu-exthdrlen);
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);
2783 + goto alloc_new_skb;
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;
2793 + unsigned int datalen;
2794 + unsigned int fraglen;
2795 + unsigned int fraggap;
2796 + unsigned int alloclen;
2798 + /* There's no room in the current skb */
2800 + fraggap = skb->len - maxfraglen;
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,
2808 + IPV6_PMTUDISC_PROBE);
2813 + * If remaining data exceeds the mtu,
2814 + * we know we need more fragment(s).
2816 + datalen = length + fraggap;
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))
2824 + alloclen = datalen + fragheaderlen;
2826 + alloclen += dst_exthdrlen;
2828 + if (datalen != length + fraggap) {
2830 + * this is not the last fragment, the trailer
2831 + * space is regarded as data space.
2833 + datalen += rt->dst.trailer_len;
2836 + alloclen += rt->dst.trailer_len;
2837 + fraglen = datalen + fragheaderlen;
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.
2844 + alloclen += sizeof(struct frag_hdr);
2846 + if (transhdrlen) {
2847 + skb = sock_alloc_send_skb(sk,
2848 + alloclen + hh_len,
2849 + (flags & MSG_DONTWAIT), &err);
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))
2860 + /* Only the initial fragment
2861 + * is time stamped.
2869 + * Fill in the control structures
2871 + skb->ip_summed = CHECKSUM_NONE;
2873 + /* reserve for fragmentation and ipsec header */
2874 + skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
2877 + if (sk->sk_type == SOCK_DGRAM)
2878 + skb_shinfo(skb)->tx_flags = tx_flags;
2881 + * Find where to start putting bytes
2883 + data = skb_put(skb, fraglen);
2884 + skb_set_network_header(skb, exthdrlen);
2885 + data += fragheaderlen;
2886 + skb->transport_header = (skb->network_header +
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,
2895 + pskb_trim_unique(skb_prev, maxfraglen);
2897 + copy = datalen - transhdrlen - fraggap;
2903 + } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
2910 + length -= datalen - fraggap;
2913 + dst_exthdrlen = 0;
2916 + * Put the packet on the pending queue
2918 + __skb_queue_tail(&sk->sk_write_queue, skb);
2922 + if (copy > length)
2925 + if (!(rt->dst.dev->features&NETIF_F_SG)) {
2929 + if (getfrag(from, skb_put(skb, copy),
2930 + offset, copy, off, skb) < 0) {
2931 + __skb_trim(skb, off);
2936 + int i = skb_shinfo(skb)->nr_frags;
2937 + struct page_frag *pfrag = sk_page_frag(sk);
2940 + if (!sk_page_frag_refill(sk, pfrag))
2943 + if (!skb_can_coalesce(skb, i, pfrag->page,
2946 + if (i == MAX_SKB_FRAGS)
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);
2954 + copy = min_t(int, copy, pfrag->size - pfrag->offset);
2956 + page_address(pfrag->page) + pfrag->offset,
2957 + offset, copy, skb->len, skb) < 0)
2958 + goto error_efault;
2960 + pfrag->offset += copy;
2961 + skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
2963 + skb->data_len += copy;
2964 + skb->truesize += copy;
2965 + atomic_add(copy, &sk->sk_wmem_alloc);
2976 + cork->length -= length;
2977 + IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
2980 +EXPORT_SYMBOL_GPL(ip6_append_data);
2982 +static void ip6_cork_release(struct inet_sock *inet, struct ipv6_pinfo *np)
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;
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;
2998 + memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
3001 +int ip6_push_pending_frames(struct sock *sk)
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;
3016 + if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
3018 + tail_skb = &(skb_shinfo(skb)->frag_list);
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;
3034 + /* Allow local fragmentation. */
3035 + if (np->pmtudisc < IPV6_PMTUDISC_DO)
3036 + skb->local_df = 1;
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);
3045 + skb_push(skb, sizeof(struct ipv6hdr));
3046 + skb_reset_network_header(skb);
3047 + hdr = ipv6_hdr(skb);
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;
3055 + skb->priority = sk->sk_priority;
3056 + skb->mark = sk->sk_mark;
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));
3063 + ICMP6MSGOUT_INC_STATS_BH(net, idev, icmp6_hdr(skb)->icmp6_type);
3064 + ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
3067 + err = ip6_local_out(skb);
3070 + err = net_xmit_errno(err);
3076 + ip6_cork_release(inet, np);
3079 + IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
3082 +EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
3084 +void ip6_flush_pending_frames(struct sock *sk)
3086 + struct sk_buff *skb;
3088 + while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
3090 + IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
3091 + IPSTATS_MIB_OUTDISCARDS);
3095 + ip6_cork_release(inet_sk(sk), inet6_sk(sk));
3097 +EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);
3098 diff -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);
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);
3113 if (err == -ECANCELED)
3115 diff -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 @@
3120 To compile it as a module, choose M here. If unsure, say N.
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
3127 + default m if NETFILTER_ADVANCED=n
3129 + This option adds a `IMQ' target which is used to specify if and
3130 + to which imq device packets should get enqueued/dequeued.
3132 + To compile it as a module, choose M here. If unsure, say N.
3134 config NETFILTER_XT_TARGET_MARK
3135 tristate '"MARK" target support'
3136 depends on NETFILTER_ADVANCED
3137 diff -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
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
3148 diff -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
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);
3160 diff -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
3165 static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
3167 +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
3168 +static const struct nf_queue_handler __rcu *queue_imq_handler __read_mostly;
3170 +void nf_register_queue_imq_handler(const struct nf_queue_handler *qh)
3172 + rcu_assign_pointer(queue_imq_handler, qh);
3174 +EXPORT_SYMBOL_GPL(nf_register_queue_imq_handler);
3176 +void nf_unregister_queue_imq_handler(void)
3178 + RCU_INIT_POINTER(queue_imq_handler, NULL);
3179 + synchronize_rcu();
3181 +EXPORT_SYMBOL_GPL(nf_unregister_queue_imq_handler);
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)
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)
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. */
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);
3210 + qh = rcu_dereference(queue_handler);
3216 @@ -205,9 +233,11 @@
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);
3227 if (err == -ECANCELED)
3229 diff -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
3234 + * This target marks packets to be enqueued to an imq device
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>
3242 +static unsigned int imq_target(struct sk_buff *pskb,
3243 + const struct xt_action_param *par)
3245 + const struct xt_imq_info *mr = par->targinfo;
3247 + pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE;
3249 + return XT_CONTINUE;
3252 +static int imq_checkentry(const struct xt_tgchk_param *par)
3254 + struct xt_imq_info *mr = par->targinfo;
3256 + if (mr->todev > IMQ_MAX_DEVS - 1) {
3257 + pr_warn("IMQ: invalid device specified, highest is %u\n",
3258 + IMQ_MAX_DEVS - 1);
3265 +static struct xt_target xt_imq_reg[] __read_mostly = {
3268 + .family = AF_INET,
3269 + .checkentry = imq_checkentry,
3270 + .target = imq_target,
3271 + .targetsize = sizeof(struct xt_imq_info),
3272 + .table = "mangle",
3277 + .family = AF_INET6,
3278 + .checkentry = imq_checkentry,
3279 + .target = imq_target,
3280 + .targetsize = sizeof(struct xt_imq_info),
3281 + .table = "mangle",
3286 +static int __init imq_init(void)
3288 + return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
3291 +static void __exit imq_fini(void)
3293 + xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg));
3296 +module_init(imq_init);
3297 +module_exit(imq_fini);
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");