datapath: Enable OVS GSO to be used up to 3.18 if necessary.
[cascardo/ovs.git] / datapath / linux / compat / ip_tunnels_core.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
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/in.h>
23 #include <linux/in_route.h>
24 #include <linux/inetdevice.h>
25 #include <linux/jhash.h>
26 #include <linux/list.h>
27 #include <linux/kernel.h>
28 #include <linux/version.h>
29 #include <linux/workqueue.h>
30 #include <linux/rculist.h>
31 #include <net/ip_tunnels.h>
32 #include <net/route.h>
33 #include <net/xfrm.h>
34
35 #include "compat.h"
36 #include "gso.h"
37
38 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
39 int rpl_iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
40                       __be32 src, __be32 dst, __u8 proto, __u8 tos, __u8 ttl,
41                       __be16 df, bool xnet)
42 {
43         int pkt_len = skb->len;
44         struct iphdr *iph;
45         int err;
46
47         nf_reset(skb);
48         secpath_reset(skb);
49         skb_clear_hash(skb);
50         skb_dst_drop(skb);
51         skb_dst_set(skb, &rt_dst(rt));
52 #if 0
53         /* Do not clear ovs_skb_cb.  It will be done in gso code. */
54         memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
55 #endif
56
57         /* Push down and install the IP header. */
58         __skb_push(skb, sizeof(struct iphdr));
59         skb_reset_network_header(skb);
60
61         iph = ip_hdr(skb);
62
63         iph->version    =       4;
64         iph->ihl        =       sizeof(struct iphdr) >> 2;
65         iph->frag_off   =       df;
66         iph->protocol   =       proto;
67         iph->tos        =       tos;
68         iph->daddr      =       dst;
69         iph->saddr      =       src;
70         iph->ttl        =       ttl;
71
72 #ifdef HAVE_IP_SELECT_IDENT_USING_DST_ENTRY
73         __ip_select_ident(iph, &rt_dst(rt), (skb_shinfo(skb)->gso_segs ?: 1) - 1);
74 #else
75         __ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1);
76 #endif
77
78         err = ip_local_out(skb);
79         if (unlikely(net_xmit_eval(err)))
80                 pkt_len = 0;
81         return pkt_len;
82 }
83
84 struct sk_buff *ovs_iptunnel_handle_offloads(struct sk_buff *skb,
85                                              bool csum_help, int gso_type_mask,
86                                              void (*fix_segment)(struct sk_buff *))
87 {
88         int err;
89
90         if (likely(!skb_is_encapsulated(skb))) {
91                 skb_reset_inner_headers(skb);
92 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
93                 skb->encapsulation = 1;
94 #endif
95         } else if (skb_is_gso(skb)) {
96                 err = -ENOSYS;
97                 goto error;
98         }
99
100         if (gso_type_mask)
101                 fix_segment = NULL;
102
103         OVS_GSO_CB(skb)->fix_segment = fix_segment;
104
105         if (skb_is_gso(skb)) {
106                 err = skb_unclone(skb, GFP_ATOMIC);
107                 if (unlikely(err))
108                         goto error;
109                 skb_shinfo(skb)->gso_type |= gso_type_mask;
110                 return skb;
111         }
112
113 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
114         /* If packet is not gso and we are resolving any partial checksum,
115          * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL
116          * on the outer header without confusing devices that implement
117          * NETIF_F_IP_CSUM with encapsulation.
118          */
119         if (csum_help)
120                 skb->encapsulation = 0;
121 #endif
122
123         if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
124                 err = skb_checksum_help(skb);
125                 if (unlikely(err))
126                         goto error;
127         } else if (skb->ip_summed != CHECKSUM_PARTIAL)
128                 skb->ip_summed = CHECKSUM_NONE;
129
130         return skb;
131 error:
132         kfree_skb(skb);
133         return ERR_PTR(err);
134 }
135
136 int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
137 {
138         if (unlikely(!pskb_may_pull(skb, hdr_len)))
139                 return -ENOMEM;
140
141         skb_pull_rcsum(skb, hdr_len);
142
143         if (inner_proto == htons(ETH_P_TEB)) {
144                 struct ethhdr *eh;
145
146                 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
147                         return -ENOMEM;
148
149                 eh = (struct ethhdr *)skb->data;
150
151                 if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN))
152                         skb->protocol = eh->h_proto;
153                 else
154                         skb->protocol = htons(ETH_P_802_2);
155
156         } else {
157                 skb->protocol = inner_proto;
158         }
159
160         nf_reset(skb);
161         secpath_reset(skb);
162         skb_clear_hash(skb);
163         skb_dst_drop(skb);
164         vlan_set_tci(skb, 0);
165         skb_set_queue_mapping(skb, 0);
166         skb->pkt_type = PACKET_HOST;
167         return 0;
168 }
169
170 #endif
171
172 bool skb_is_encapsulated(struct sk_buff *skb)
173 {
174         /* checking for inner protocol should be sufficient on newer kernel, but
175          * old kernel just set encapsulation bit.
176          */
177         /* XXX: set inner protocol for all tunnel in OVS. */
178         return ovs_skb_get_inner_protocol(skb) || skb_encapsulation(skb);
179 }