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