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