2 * Copyright (c) 2007-2012 Nicira, Inc.
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.
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.
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/skbuff.h>
24 #include <linux/if_tunnel.h>
25 #include <linux/if_vlan.h>
30 #include <net/protocol.h>
37 * The GRE header is composed of a series of sections: a base and then a variable
40 #define GRE_HEADER_SECTION 4
47 static int gre_hdr_len(const struct tnl_mutable_config *mutable,
48 const struct ovs_key_ipv4_tunnel *tun_key)
54 tnl_get_param(mutable, tun_key, &flags, &out_key);
55 len = GRE_HEADER_SECTION;
57 if (flags & TNL_F_CSUM)
58 len += GRE_HEADER_SECTION;
60 /* Set key for GRE64 tunnels, even when key if is zero. */
62 mutable->key.tunnel_type & TNL_T_PROTO_GRE64 ||
63 flags & TNL_F_OUT_KEY_ACTION) {
65 len += GRE_HEADER_SECTION;
66 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64)
67 len += GRE_HEADER_SECTION;
73 /* Returns the least-significant 32 bits of a __be64. */
74 static __be32 be64_get_low32(__be64 x)
77 return (__force __be32)x;
79 return (__force __be32)((__force u64)x >> 32);
83 static __be32 be64_get_high32(__be64 x)
86 return (__force __be32)((__force u64)x >> 32);
88 return (__force __be32)x;
92 static struct sk_buff *gre_build_header(const struct vport *vport,
93 const struct tnl_mutable_config *mutable,
94 struct dst_entry *dst,
100 const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
101 __be32 *options = (__be32 *)(skb_network_header(skb) + tunnel_hlen
102 - GRE_HEADER_SECTION);
103 struct gre_base_hdr *greh = (struct gre_base_hdr *) skb_transport_header(skb);
105 tnl_get_param(mutable, tun_key, &flags, &out_key);
107 greh->protocol = htons(ETH_P_TEB);
110 /* Work backwards over the options so the checksum is last. */
111 if (out_key || flags & TNL_F_OUT_KEY_ACTION ||
112 mutable->key.tunnel_type & TNL_T_PROTO_GRE64) {
113 greh->flags |= GRE_KEY;
114 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64) {
115 /* Set higher 32 bits to seq. */
116 *options = be64_get_high32(out_key);
118 greh->flags |= GRE_SEQ;
120 *options = be64_get_low32(out_key);
124 if (flags & TNL_F_CSUM) {
125 greh->flags |= GRE_CSUM;
127 *(__sum16 *)options = csum_fold(skb_checksum(skb,
128 skb_transport_offset(skb),
129 skb->len - skb_transport_offset(skb),
133 * Allow our local IP stack to fragment the outer packet even if the
134 * DF bit is set as a last resort. We also need to force selection of
135 * an IP ID here because Linux will otherwise leave it at 0 if the
136 * packet originally had DF set.
139 __ip_select_ident(ip_hdr(skb), dst, 0);
144 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
147 return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
149 return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
153 static int parse_header(struct iphdr *iph, __be16 *flags, __be64 *tun_id,
156 /* IP and ICMP protocol handlers check that the IHL is valid. */
157 struct gre_base_hdr *greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2));
158 __be32 *options = (__be32 *)(greh + 1);
161 *flags = greh->flags;
163 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
166 if (unlikely(greh->protocol != htons(ETH_P_TEB)))
169 hdr_len = GRE_HEADER_SECTION;
171 if (greh->flags & GRE_CSUM) {
172 hdr_len += GRE_HEADER_SECTION;
176 if (greh->flags & GRE_KEY) {
181 hdr_len += GRE_HEADER_SECTION;
184 if (greh->flags & GRE_SEQ) {
186 *tunnel_type = TNL_T_PROTO_GRE64;
189 *tunnel_type = TNL_T_PROTO_GRE;
191 *tun_id = key_to_tunnel_id(gre_key, seq);
194 /* Ignore GRE seq if there is no key present. */
195 *tunnel_type = TNL_T_PROTO_GRE;
198 if (greh->flags & GRE_SEQ)
199 hdr_len += GRE_HEADER_SECTION;
204 static bool check_checksum(struct sk_buff *skb)
206 struct iphdr *iph = ip_hdr(skb);
207 struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
210 if (greh->flags & GRE_CSUM) {
211 switch (skb->ip_summed) {
212 case CHECKSUM_COMPLETE:
213 csum = csum_fold(skb->csum);
221 csum = __skb_checksum_complete(skb);
222 skb->ip_summed = CHECKSUM_COMPLETE;
230 static u32 gre_flags_to_tunnel_flags(const struct tnl_mutable_config *mutable,
231 __be16 gre_flags, __be64 *key)
233 u32 tunnel_flags = 0;
235 if (gre_flags & GRE_KEY) {
236 if (mutable->flags & TNL_F_IN_KEY_MATCH ||
238 tunnel_flags = OVS_TNL_F_KEY;
243 if (gre_flags & GRE_CSUM)
244 tunnel_flags |= OVS_TNL_F_CSUM;
249 /* Called with rcu_read_lock and BH disabled. */
250 static int gre_rcv(struct sk_buff *skb)
253 const struct tnl_mutable_config *mutable;
256 struct ovs_key_ipv4_tunnel tun_key;
262 if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
264 if (unlikely(!check_checksum(skb)))
267 hdr_len = parse_header(ip_hdr(skb), &gre_flags, &key, &tunnel_type);
268 if (unlikely(hdr_len < 0))
271 if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
275 vport = ovs_tnl_find_port(dev_net(skb->dev), iph->daddr, iph->saddr, key,
276 tunnel_type, &mutable);
277 if (unlikely(!vport))
280 tnl_flags = gre_flags_to_tunnel_flags(mutable, gre_flags, &key);
281 tnl_tun_key_init(&tun_key, iph, key, tnl_flags);
282 OVS_CB(skb)->tun_key = &tun_key;
284 __skb_pull(skb, hdr_len);
285 skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
287 ovs_tnl_rcv(vport, skb);
295 static const struct tnl_ops gre_tnl_ops = {
296 .tunnel_type = TNL_T_PROTO_GRE,
297 .ipproto = IPPROTO_GRE,
298 .hdr_len = gre_hdr_len,
299 .build_header = gre_build_header,
302 static struct vport *gre_create(const struct vport_parms *parms)
304 return ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
307 static const struct tnl_ops gre64_tnl_ops = {
308 .tunnel_type = TNL_T_PROTO_GRE64,
309 .ipproto = IPPROTO_GRE,
310 .hdr_len = gre_hdr_len,
311 .build_header = gre_build_header,
314 static struct vport *gre_create64(const struct vport_parms *parms)
316 return ovs_tnl_create(parms, &ovs_gre64_vport_ops, &gre64_tnl_ops);
319 static const struct net_protocol gre_protocol_handlers = {
321 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
328 static int gre_init(void)
336 err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
338 pr_warn("cannot register gre protocol handler\n");
343 static void gre_exit(void)
350 inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
353 const struct vport_ops ovs_gre_vport_ops = {
354 .type = OVS_VPORT_TYPE_GRE,
355 .flags = VPORT_F_TUN_ID,
358 .create = gre_create,
359 .destroy = ovs_tnl_destroy,
360 .get_name = ovs_tnl_get_name,
361 .get_options = ovs_tnl_get_options,
362 .set_options = ovs_tnl_set_options,
363 .send = ovs_tnl_send,
366 const struct vport_ops ovs_gre64_vport_ops = {
367 .type = OVS_VPORT_TYPE_GRE64,
368 .flags = VPORT_F_TUN_ID,
371 .create = gre_create64,
372 .destroy = ovs_tnl_destroy,
373 .get_name = ovs_tnl_get_name,
374 .get_options = ovs_tnl_get_options,
375 .set_options = ovs_tnl_set_options,
376 .send = ovs_tnl_send,