datapath: Always initialize fix_segment for GSO packet.
[cascardo/ovs.git] / datapath / linux / compat / gre.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/version.h>
20 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,12,0)
21
22 #include <linux/kconfig.h>
23 #if IS_ENABLED(CONFIG_NET_IPGRE_DEMUX)
24
25 #include <linux/module.h>
26 #include <linux/if.h>
27 #include <linux/if_tunnel.h>
28 #include <linux/icmp.h>
29 #include <linux/in.h>
30 #include <linux/ip.h>
31 #include <linux/kernel.h>
32 #include <linux/kmod.h>
33 #include <linux/netdevice.h>
34 #include <linux/skbuff.h>
35 #include <linux/spinlock.h>
36
37 #include <net/gre.h>
38 #include <net/icmp.h>
39 #include <net/protocol.h>
40 #include <net/route.h>
41 #include <net/xfrm.h>
42
43 #include "gso.h"
44
45 #ifndef HAVE_GRE_CISCO_REGISTER
46
47 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
48
49 #define GREPROTO_CISCO          0
50 #define GREPROTO_MAX            1
51
52 struct gre_protocol {
53         int  (*handler)(struct sk_buff *skb);
54 };
55 static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
56
57 static int gre_rcv(struct sk_buff *skb)
58 {
59         const struct gre_protocol *proto;
60         u8 ver;
61         int ret;
62
63         if (!pskb_may_pull(skb, 12))
64                 goto drop;
65
66         ver = skb->data[1] & 0x7f;
67         if (ver >= GREPROTO_MAX)
68                 goto drop;
69
70         rcu_read_lock();
71         proto = rcu_dereference(gre_proto[ver]);
72         if (!proto || !proto->handler)
73                 goto drop_unlock;
74         ret = proto->handler(skb);
75         rcu_read_unlock();
76         return ret;
77
78 drop_unlock:
79         rcu_read_unlock();
80 drop:
81         kfree_skb(skb);
82         return NET_RX_DROP;
83 }
84
85 static const struct net_protocol net_gre_protocol = {
86         .handler     = gre_rcv,
87         .netns_ok    = 1,
88 };
89
90 static int gre_add_protocol(const struct gre_protocol *proto, u8 version)
91 {
92         if (version >= GREPROTO_MAX)
93                 return -EINVAL;
94
95         if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
96                 pr_err("%s: cannot register gre protocol handler\n", __func__);
97                 return -EAGAIN;
98         }
99
100         return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ?
101                 0 : -EBUSY;
102 }
103
104 static int gre_del_protocol(const struct gre_protocol *proto, u8 version)
105 {
106         int ret;
107
108         if (version >= GREPROTO_MAX)
109                 return -EINVAL;
110
111         ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ?
112                 0 : -EBUSY;
113
114         if (ret)
115                 return ret;
116
117         synchronize_net();
118
119         ret = inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
120         if (ret)
121                 return ret;
122
123         return 0;
124 }
125
126 #endif
127
128 static __sum16 check_checksum(struct sk_buff *skb)
129 {
130         __sum16 csum = 0;
131
132         switch (skb->ip_summed) {
133         case CHECKSUM_COMPLETE:
134                 csum = csum_fold(skb->csum);
135
136                 if (!csum)
137                         break;
138                 /* Fall through. */
139
140         case CHECKSUM_NONE:
141                 skb->csum = 0;
142                 csum = __skb_checksum_complete(skb);
143                 skb->ip_summed = CHECKSUM_COMPLETE;
144                 break;
145         }
146
147         return csum;
148 }
149
150 static int parse_gre_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
151                             bool *csum_err)
152 {
153         unsigned int ip_hlen = ip_hdrlen(skb);
154         struct gre_base_hdr *greh;
155         __be32 *options;
156         int hdr_len;
157
158         if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr))))
159                 return -EINVAL;
160
161         greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen);
162         if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
163                 return -EINVAL;
164
165         tpi->flags = gre_flags_to_tnl_flags(greh->flags);
166         hdr_len = ip_gre_calc_hlen(tpi->flags);
167
168         if (!pskb_may_pull(skb, hdr_len))
169                 return -EINVAL;
170
171         greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen);
172         tpi->proto = greh->protocol;
173
174         options = (__be32 *)(greh + 1);
175         if (greh->flags & GRE_CSUM) {
176                 if (check_checksum(skb)) {
177                         *csum_err = true;
178                         return -EINVAL;
179                 }
180                 options++;
181         }
182
183         if (greh->flags & GRE_KEY) {
184                 tpi->key = *options;
185                 options++;
186         } else
187                 tpi->key = 0;
188
189         if (unlikely(greh->flags & GRE_SEQ)) {
190                 tpi->seq = *options;
191                 options++;
192         } else
193                 tpi->seq = 0;
194
195         /* WCCP version 1 and 2 protocol decoding.
196          * - Change protocol to IP
197          * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
198          */
199         if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
200                 tpi->proto = htons(ETH_P_IP);
201                 if ((*(u8 *)options & 0xF0) != 0x40) {
202                         hdr_len += 4;
203                         if (!pskb_may_pull(skb, hdr_len))
204                                 return -EINVAL;
205                 }
206         }
207
208         return iptunnel_pull_header(skb, hdr_len, tpi->proto);
209 }
210
211 static struct gre_cisco_protocol __rcu *gre_cisco_proto;
212 static int gre_cisco_rcv(struct sk_buff *skb)
213 {
214         struct tnl_ptk_info tpi;
215         bool csum_err = false;
216         struct gre_cisco_protocol *proto;
217
218         rcu_read_lock();
219         proto = rcu_dereference(gre_cisco_proto);
220         if (!proto)
221                 goto drop;
222
223         if (parse_gre_header(skb, &tpi, &csum_err) < 0)
224                 goto drop;
225         proto->handler(skb, &tpi);
226         rcu_read_unlock();
227         return 0;
228
229 drop:
230         rcu_read_unlock();
231         kfree_skb(skb);
232         return 0;
233 }
234
235 static const struct gre_protocol ipgre_protocol = {
236         .handler        =       gre_cisco_rcv,
237 };
238
239 int gre_cisco_register(struct gre_cisco_protocol *newp)
240 {
241         int err;
242
243         err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
244         if (err) {
245                 pr_warn("%s: cannot register gre_cisco protocol handler\n", __func__);
246                 return err;
247         }
248
249
250         return (cmpxchg((struct gre_cisco_protocol **)&gre_cisco_proto, NULL, newp) == NULL) ?
251                 0 : -EBUSY;
252 }
253
254 int gre_cisco_unregister(struct gre_cisco_protocol *proto)
255 {
256         int ret;
257
258         ret = (cmpxchg((struct gre_cisco_protocol **)&gre_cisco_proto, proto, NULL) == proto) ?
259                 0 : -EINVAL;
260
261         if (ret)
262                 return ret;
263
264         synchronize_net();
265         ret = gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
266         return ret;
267 }
268
269 #endif /* !HAVE_GRE_CISCO_REGISTER */
270
271 /* GRE TX side. */
272 static void gre_csum_fix(struct sk_buff *skb)
273 {
274         struct gre_base_hdr *greh;
275         __be32 *options;
276         int gre_offset = skb_transport_offset(skb);
277
278         greh = (struct gre_base_hdr *)skb_transport_header(skb);
279         options = ((__be32 *)greh + 1);
280
281         *options = 0;
282         *(__sum16 *)options = csum_fold(skb_checksum(skb, gre_offset,
283                                                      skb->len - gre_offset, 0));
284 }
285
286 struct sk_buff *gre_handle_offloads(struct sk_buff *skb, bool gre_csum)
287 {
288         int err;
289
290         skb_reset_inner_headers(skb);
291
292         if (skb_is_gso(skb)) {
293                 if (gre_csum)
294                         OVS_GSO_CB(skb)->fix_segment = gre_csum_fix;
295                 else
296                         OVS_GSO_CB(skb)->fix_segment = NULL;
297         } else {
298                 if (skb->ip_summed == CHECKSUM_PARTIAL && gre_csum) {
299                         err = skb_checksum_help(skb);
300                         if (err)
301                                 goto error;
302
303                 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
304                         skb->ip_summed = CHECKSUM_NONE;
305         }
306         return skb;
307 error:
308         kfree_skb(skb);
309         return ERR_PTR(err);
310 }
311
312 static bool is_gre_gso(struct sk_buff *skb)
313 {
314         return skb_is_gso(skb);
315 }
316
317 void gre_build_header(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
318                       int hdr_len)
319 {
320         struct gre_base_hdr *greh;
321
322         __skb_push(skb, hdr_len);
323
324         greh = (struct gre_base_hdr *)skb->data;
325         greh->flags = tnl_flags_to_gre_flags(tpi->flags);
326         greh->protocol = tpi->proto;
327
328         if (tpi->flags & (TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_SEQ)) {
329                 __be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4);
330
331                 if (tpi->flags & TUNNEL_SEQ) {
332                         *ptr = tpi->seq;
333                         ptr--;
334                 }
335                 if (tpi->flags & TUNNEL_KEY) {
336                         *ptr = tpi->key;
337                         ptr--;
338                 }
339                 if (tpi->flags & TUNNEL_CSUM && !is_gre_gso(skb)) {
340                         *ptr = 0;
341                         *(__sum16 *)ptr = csum_fold(skb_checksum(skb, 0,
342                                                 skb->len, 0));
343                 }
344         }
345 }
346
347 #endif /* CONFIG_NET_IPGRE_DEMUX */
348
349 #endif /* 3.12 */