43418d399d0b9e47e1bfbbc693fbd2d3d045e758
[sliver-openvswitch.git] / datapath / linux / compat / gso.c
1 /*
2  * Copyright (c) 2007-2013 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #include <linux/module.h>
20 #include <linux/if.h>
21 #include <linux/if_tunnel.h>
22 #include <linux/icmp.h>
23 #include <linux/in.h>
24 #include <linux/ip.h>
25 #include <linux/kernel.h>
26 #include <linux/kmod.h>
27 #include <linux/netdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
30
31 #include <net/gre.h>
32 #include <net/icmp.h>
33 #include <net/protocol.h>
34 #include <net/route.h>
35 #include <net/xfrm.h>
36
37 #include "gso.h"
38
39 static __be16 __skb_network_protocol(struct sk_buff *skb)
40 {
41         __be16 type = skb->protocol;
42         int vlan_depth = ETH_HLEN;
43
44         while (type == htons(ETH_P_8021Q) || type == htons(ETH_P_8021AD)) {
45                 struct vlan_hdr *vh;
46
47                 if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN)))
48                         return 0;
49
50                 vh = (struct vlan_hdr *)(skb->data + vlan_depth);
51                 type = vh->h_vlan_encapsulated_proto;
52                 vlan_depth += VLAN_HLEN;
53         }
54
55         return type;
56 }
57
58 static struct sk_buff *tnl_skb_gso_segment(struct sk_buff *skb,
59                                            netdev_features_t features,
60                                            bool tx_path)
61 {
62         struct iphdr *iph = ip_hdr(skb);
63         int pkt_hlen = skb_inner_network_offset(skb); /* inner l2 + tunnel hdr. */
64         int mac_offset = skb_inner_mac_offset(skb);
65         struct sk_buff *skb1 = skb;
66         struct sk_buff *segs;
67         __be16 proto = skb->protocol;
68
69         /* setup whole inner packet to get protocol. */
70         __skb_pull(skb, mac_offset);
71         skb->protocol = __skb_network_protocol(skb);
72
73         /* setup l3 packet to gso, to get around segmentation bug on older kernel.*/
74         __skb_pull(skb, (pkt_hlen - mac_offset));
75         skb_reset_mac_header(skb);
76         skb_reset_network_header(skb);
77         skb_reset_transport_header(skb);
78
79         segs = __skb_gso_segment(skb, 0, tx_path);
80         if (!segs || IS_ERR(segs))
81                 goto free;
82
83         skb = segs;
84         while (skb) {
85                 __skb_push(skb, pkt_hlen);
86                 skb_reset_mac_header(skb);
87                 skb_reset_network_header(skb);
88                 skb_set_transport_header(skb, sizeof(struct iphdr));
89                 skb->mac_len = 0;
90
91                 memcpy(ip_hdr(skb), iph, pkt_hlen);
92                 if (OVS_GSO_CB(skb)->fix_segment)
93                         OVS_GSO_CB(skb)->fix_segment(skb);
94
95                 skb->protocol = proto;
96                 skb = skb->next;
97         }
98 free:
99         consume_skb(skb1);
100         return segs;
101 }
102
103 int rpl_ip_local_out(struct sk_buff *skb)
104 {
105         int ret = NETDEV_TX_OK;
106         int id = -1;
107
108         if (skb_is_gso(skb)) {
109                 struct iphdr *iph;
110
111                 iph = ip_hdr(skb);
112                 id = ntohs(iph->id);
113                 skb = tnl_skb_gso_segment(skb, 0, false);
114                 if (!skb || IS_ERR(skb))
115                         return 0;
116         }  else if (skb->ip_summed == CHECKSUM_PARTIAL) {
117                 int err;
118
119                 err = skb_checksum_help(skb);
120                 if (unlikely(err))
121                         return 0;
122         }
123
124         while (skb) {
125                 struct sk_buff *next_skb = skb->next;
126                 struct iphdr *iph;
127                 int err;
128
129                 skb->next = NULL;
130
131                 iph = ip_hdr(skb);
132                 if (id >= 0)
133                         iph->id = htons(id++);
134
135                 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
136
137 #undef ip_local_out
138                 err = ip_local_out(skb);
139                 if (unlikely(net_xmit_eval(err)))
140                         ret = err;
141
142                 skb = next_skb;
143         }
144         return ret;
145 }