datapath: Correct byte order annotations.
[sliver-openvswitch.git] / datapath / vport-gre.c
1 /*
2  * Copyright (c) 2010 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/if.h>
12 #include <linux/skbuff.h>
13 #include <linux/ip.h>
14 #include <linux/if_tunnel.h>
15 #include <linux/if_vlan.h>
16 #include <linux/in.h>
17
18 #include <net/icmp.h>
19 #include <net/ip.h>
20 #include <net/protocol.h>
21
22 #include "tunnel.h"
23 #include "vport.h"
24 #include "vport-generic.h"
25
26 /*
27  * The GRE header is composed of a series of sections: a base and then a variable
28  * number of options.
29  */
30 #define GRE_HEADER_SECTION 4
31
32 struct gre_base_hdr {
33         __be16 flags;
34         __be16 protocol;
35 };
36
37 static int gre_hdr_len(const struct tnl_port_config *port_config)
38 {
39         int len;
40
41         len = GRE_HEADER_SECTION;
42
43         if (port_config->flags & TNL_F_CSUM)
44                 len += GRE_HEADER_SECTION;
45
46         if (port_config->out_key ||
47             port_config->flags & TNL_F_OUT_KEY_ACTION)
48                 len += GRE_HEADER_SECTION;
49
50         return len;
51 }
52
53 /* Returns the least-significant 32 bits of a __be64. */
54 static __be32 be64_get_low32(__be64 x)
55 {
56 #ifdef __BIG_ENDIAN
57         return (__force __be32)x;
58 #else
59         return (__force __be32)((__force u64)x >> 32);
60 #endif
61 }
62
63 static void gre_build_header(const struct vport *vport,
64                              const struct tnl_mutable_config *mutable,
65                              void *header)
66 {
67         struct gre_base_hdr *greh = header;
68         __be32 *options = (__be32 *)(greh + 1);
69
70         greh->protocol = htons(ETH_P_TEB);
71         greh->flags = 0;
72
73         if (mutable->port_config.flags & TNL_F_CSUM) {
74                 greh->flags |= GRE_CSUM;
75                 *options = 0;
76                 options++;
77         }
78
79         if (mutable->port_config.out_key ||
80             mutable->port_config.flags & TNL_F_OUT_KEY_ACTION)
81                 greh->flags |= GRE_KEY;
82
83         if (mutable->port_config.out_key)
84                 *options = be64_get_low32(mutable->port_config.out_key);
85 }
86
87 static struct sk_buff *gre_update_header(const struct vport *vport,
88                                          const struct tnl_mutable_config *mutable,
89                                          struct dst_entry *dst,
90                                          struct sk_buff *skb)
91 {
92         __be32 *options = (__be32 *)(skb_network_header(skb) + mutable->tunnel_hlen
93                                                - GRE_HEADER_SECTION);
94
95         /* Work backwards over the options so the checksum is last. */
96         if (mutable->port_config.flags & TNL_F_OUT_KEY_ACTION) {
97                 *options = be64_get_low32(OVS_CB(skb)->tun_id);
98                 options--;
99         }
100
101         if (mutable->port_config.flags & TNL_F_CSUM)
102                 *(__sum16 *)options = csum_fold(skb_checksum(skb,
103                                                 skb_transport_offset(skb),
104                                                 skb->len - skb_transport_offset(skb),
105                                                 0));
106         /*
107          * Allow our local IP stack to fragment the outer packet even if the
108          * DF bit is set as a last resort.
109          */
110         skb->local_df = 1;
111
112         return skb;
113 }
114
115 /* Zero-extends a __be32 into the least-significant 32 bits of a __be64. */
116 static __be64 be32_extend_to_be64(__be32 x)
117 {
118 #ifdef __BIG_ENDIAN
119         return (__force __be64)x;
120 #else
121         return (__force __be64)((__force u64)x << 32);
122 #endif
123 }
124
125 static int parse_header(struct iphdr *iph, __be16 *flags, __be64 *key)
126 {
127         /* IP and ICMP protocol handlers check that the IHL is valid. */
128         struct gre_base_hdr *greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2));
129         __be32 *options = (__be32 *)(greh + 1);
130         int hdr_len;
131
132         *flags = greh->flags;
133
134         if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
135                 return -EINVAL;
136
137         if (unlikely(greh->protocol != htons(ETH_P_TEB)))
138                 return -EINVAL;
139
140         hdr_len = GRE_HEADER_SECTION;
141
142         if (greh->flags & GRE_CSUM) {
143                 hdr_len += GRE_HEADER_SECTION;
144                 options++;
145         }
146
147         if (greh->flags & GRE_KEY) {
148                 hdr_len += GRE_HEADER_SECTION;
149
150                 *key = be32_extend_to_be64(*options);
151                 options++;
152         } else
153                 *key = 0;
154
155         if (unlikely(greh->flags & GRE_SEQ))
156                 hdr_len += GRE_HEADER_SECTION;
157
158         return hdr_len;
159 }
160
161 /* Called with rcu_read_lock and BH disabled. */
162 static void gre_err(struct sk_buff *skb, u32 info)
163 {
164         struct vport *vport;
165         const struct tnl_mutable_config *mutable;
166         const int type = icmp_hdr(skb)->type;
167         const int code = icmp_hdr(skb)->code;
168         int mtu = ntohs(icmp_hdr(skb)->un.frag.mtu);
169
170         struct iphdr *iph;
171         __be16 flags;
172         __be64 key;
173         int tunnel_hdr_len, tot_hdr_len;
174         unsigned int orig_mac_header;
175         unsigned int orig_nw_header;
176
177         if (type != ICMP_DEST_UNREACH || code != ICMP_FRAG_NEEDED)
178                 return;
179
180         /*
181          * The mimimum size packet that we would actually be able to process:
182          * encapsulating IP header, minimum GRE header, Ethernet header,
183          * inner IPv4 header.
184          */
185         if (!pskb_may_pull(skb, sizeof(struct iphdr) + GRE_HEADER_SECTION +
186                                 ETH_HLEN + sizeof(struct iphdr)))
187                 return;
188
189         iph = (struct iphdr *)skb->data;
190
191         tunnel_hdr_len = parse_header(iph, &flags, &key);
192         if (tunnel_hdr_len < 0)
193                 return;
194
195         vport = tnl_find_port(iph->saddr, iph->daddr, key,
196                               TNL_T_PROTO_GRE | TNL_T_KEY_EITHER, &mutable);
197         if (!vport)
198                 return;
199
200         /*
201          * Packets received by this function were previously sent by us, so
202          * any comparisons should be to the output values, not the input.
203          * However, it's not really worth it to have a hash table based on
204          * output keys (especially since ICMP error handling of tunneled packets
205          * isn't that reliable anyways).  Therefore, we do a lookup based on the
206          * out key as if it were the in key and then check to see if the input
207          * and output keys are the same.
208          */
209         if (mutable->port_config.in_key != mutable->port_config.out_key)
210                 return;
211
212         if (!!(mutable->port_config.flags & TNL_F_IN_KEY_MATCH) !=
213             !!(mutable->port_config.flags & TNL_F_OUT_KEY_ACTION))
214                 return;
215
216         if ((mutable->port_config.flags & TNL_F_CSUM) && !(flags & GRE_CSUM))
217                 return;
218
219         tunnel_hdr_len += iph->ihl << 2;
220
221         orig_mac_header = skb_mac_header(skb) - skb->data;
222         orig_nw_header = skb_network_header(skb) - skb->data;
223         skb_set_mac_header(skb, tunnel_hdr_len);
224
225         tot_hdr_len = tunnel_hdr_len + ETH_HLEN;
226
227         skb->protocol = eth_hdr(skb)->h_proto;
228         if (skb->protocol == htons(ETH_P_8021Q)) {
229                 tot_hdr_len += VLAN_HLEN;
230                 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
231         }
232
233         skb_set_network_header(skb, tot_hdr_len);
234         mtu -= tot_hdr_len;
235
236         if (skb->protocol == htons(ETH_P_IP))
237                 tot_hdr_len += sizeof(struct iphdr);
238 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
239         else if (skb->protocol == htons(ETH_P_IPV6))
240                 tot_hdr_len += sizeof(struct ipv6hdr);
241 #endif
242         else
243                 goto out;
244
245         if (!pskb_may_pull(skb, tot_hdr_len))
246                 goto out;
247
248         if (skb->protocol == htons(ETH_P_IP)) {
249                 if (mtu < IP_MIN_MTU) {
250                         if (ntohs(ip_hdr(skb)->tot_len) >= IP_MIN_MTU)
251                                 mtu = IP_MIN_MTU;
252                         else
253                                 goto out;
254                 }
255
256         }
257 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
258         else if (skb->protocol == htons(ETH_P_IPV6)) {
259                 if (mtu < IPV6_MIN_MTU) {
260                         unsigned int packet_length = sizeof(struct ipv6hdr) +
261                                               ntohs(ipv6_hdr(skb)->payload_len);
262
263                         if (packet_length >= IPV6_MIN_MTU
264                             || ntohs(ipv6_hdr(skb)->payload_len) == 0)
265                                 mtu = IPV6_MIN_MTU;
266                         else
267                                 goto out;
268                 }
269         }
270 #endif
271
272         __skb_pull(skb, tunnel_hdr_len);
273         tnl_frag_needed(vport, mutable, skb, mtu, key);
274         __skb_push(skb, tunnel_hdr_len);
275
276 out:
277         skb_set_mac_header(skb, orig_mac_header);
278         skb_set_network_header(skb, orig_nw_header);
279         skb->protocol = htons(ETH_P_IP);
280 }
281
282 static bool check_checksum(struct sk_buff *skb)
283 {
284         struct iphdr *iph = ip_hdr(skb);
285         struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
286         __sum16 csum = 0;
287
288         if (greh->flags & GRE_CSUM) {
289                 switch (skb->ip_summed) {
290                 case CHECKSUM_COMPLETE:
291                         csum = csum_fold(skb->csum);
292
293                         if (!csum)
294                                 break;
295                         /* Fall through. */
296
297                 case CHECKSUM_NONE:
298                         skb->csum = 0;
299                         csum = __skb_checksum_complete(skb);
300                         skb->ip_summed = CHECKSUM_COMPLETE;
301                         break;
302                 }
303         }
304
305         return (csum == 0);
306 }
307
308 /* Called with rcu_read_lock and BH disabled. */
309 static int gre_rcv(struct sk_buff *skb)
310 {
311         struct vport *vport;
312         const struct tnl_mutable_config *mutable;
313         int hdr_len;
314         struct iphdr *iph;
315         __be16 flags;
316         __be64 key;
317
318         if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
319                 goto error;
320
321         if (unlikely(!check_checksum(skb)))
322                 goto error;
323
324         hdr_len = parse_header(ip_hdr(skb), &flags, &key);
325         if (unlikely(hdr_len < 0))
326                 goto error;
327
328         if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
329                 goto error;
330
331         iph = ip_hdr(skb);
332         vport = tnl_find_port(iph->daddr, iph->saddr, key,
333                               TNL_T_PROTO_GRE | TNL_T_KEY_EITHER, &mutable);
334         if (unlikely(!vport)) {
335                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
336                 goto error;
337         }
338
339         if (mutable->port_config.flags & TNL_F_IN_KEY_MATCH)
340                 OVS_CB(skb)->tun_id = key;
341         else
342                 OVS_CB(skb)->tun_id = 0;
343
344         __skb_pull(skb, hdr_len);
345         skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
346
347         tnl_rcv(vport, skb);
348         return 0;
349
350 error:
351         kfree_skb(skb);
352         return 0;
353 }
354
355 static const struct tnl_ops gre_tnl_ops = {
356         .tunnel_type    = TNL_T_PROTO_GRE,
357         .ipproto        = IPPROTO_GRE,
358         .hdr_len        = gre_hdr_len,
359         .build_header   = gre_build_header,
360         .update_header  = gre_update_header,
361 };
362
363 static struct vport *gre_create(const struct vport_parms *parms)
364 {
365         return tnl_create(parms, &gre_vport_ops, &gre_tnl_ops);
366 }
367
368 static const struct net_protocol gre_protocol_handlers = {
369         .handler        =       gre_rcv,
370         .err_handler    =       gre_err,
371 };
372
373 static int gre_init(void)
374 {
375         int err;
376
377         err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
378         if (err)
379                 pr_warn("cannot register gre protocol handler\n");
380
381         return err;
382 }
383
384 static void gre_exit(void)
385 {
386         inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
387 }
388
389 const struct vport_ops gre_vport_ops = {
390         .type           = "gre",
391         .flags          = VPORT_F_GEN_STATS | VPORT_F_TUN_ID,
392         .init           = gre_init,
393         .exit           = gre_exit,
394         .create         = gre_create,
395         .modify         = tnl_modify,
396         .destroy        = tnl_destroy,
397         .set_mtu        = tnl_set_mtu,
398         .set_addr       = tnl_set_addr,
399         .get_name       = tnl_get_name,
400         .get_addr       = tnl_get_addr,
401         .get_dev_flags  = vport_gen_get_dev_flags,
402         .is_running     = vport_gen_is_running,
403         .get_operstate  = vport_gen_get_operstate,
404         .get_mtu        = tnl_get_mtu,
405         .send           = tnl_send,
406 };