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 ovs_key_ipv4_tunnel *tun_key)
49 int len = GRE_HEADER_SECTION;
51 if (tun_key->tun_flags & OVS_TNL_F_KEY)
52 len += GRE_HEADER_SECTION;
53 if (tun_key->tun_flags & OVS_TNL_F_CSUM)
54 len += GRE_HEADER_SECTION;
58 static int gre64_hdr_len(const struct ovs_key_ipv4_tunnel *tun_key)
60 /* Set key for GRE64 tunnels, even when key if is zero. */
61 int len = GRE_HEADER_SECTION + /* GRE Hdr */
62 GRE_HEADER_SECTION + /* GRE Key */
63 GRE_HEADER_SECTION; /* GRE SEQ */
65 if (tun_key->tun_flags & OVS_TNL_F_CSUM)
66 len += GRE_HEADER_SECTION;
71 /* Returns the least-significant 32 bits of a __be64. */
72 static __be32 be64_get_low32(__be64 x)
75 return (__force __be32)x;
77 return (__force __be32)((__force u64)x >> 32);
81 static __be32 be64_get_high32(__be64 x)
84 return (__force __be32)((__force u64)x >> 32);
86 return (__force __be32)x;
90 static void __gre_build_header(struct sk_buff *skb,
94 const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
95 __be32 *options = (__be32 *)(skb_network_header(skb) + tunnel_hlen
96 - GRE_HEADER_SECTION);
97 struct gre_base_hdr *greh = (struct gre_base_hdr *) skb_transport_header(skb);
98 struct dst_entry *dst = skb_dst(skb);
100 greh->protocol = htons(ETH_P_TEB);
103 /* Work backwards over the options so the checksum is last. */
104 if (tun_key->tun_flags & OVS_TNL_F_KEY || is_gre64) {
105 greh->flags |= GRE_KEY;
107 /* Set higher 32 bits to seq. */
108 *options = be64_get_high32(tun_key->tun_id);
110 greh->flags |= GRE_SEQ;
112 *options = be64_get_low32(tun_key->tun_id);
116 if (tun_key->tun_flags & OVS_TNL_F_CSUM) {
117 greh->flags |= GRE_CSUM;
119 *(__sum16 *)options = csum_fold(skb_checksum(skb,
120 skb_transport_offset(skb),
121 skb->len - skb_transport_offset(skb),
125 * Allow our local IP stack to fragment the outer packet even if the
126 * DF bit is set as a last resort. We also need to force selection of
127 * an IP ID here because Linux will otherwise leave it at 0 if the
128 * packet originally had DF set.
131 __ip_select_ident(ip_hdr(skb), dst, 0);
134 static void gre_build_header(const struct vport *vport,
138 __gre_build_header(skb, tunnel_hlen, false);
141 static void gre64_build_header(const struct vport *vport,
145 __gre_build_header(skb, tunnel_hlen, true);
148 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
151 return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
153 return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
157 static int parse_header(struct iphdr *iph, __be16 *flags, __be64 *tun_id,
160 /* IP and ICMP protocol handlers check that the IHL is valid. */
161 struct gre_base_hdr *greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2));
162 __be32 *options = (__be32 *)(greh + 1);
165 *flags = greh->flags;
167 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
170 if (unlikely(greh->protocol != htons(ETH_P_TEB)))
173 hdr_len = GRE_HEADER_SECTION;
175 if (greh->flags & GRE_CSUM) {
176 hdr_len += GRE_HEADER_SECTION;
180 if (greh->flags & GRE_KEY) {
185 hdr_len += GRE_HEADER_SECTION;
188 if (greh->flags & GRE_SEQ) {
195 *tun_id = key_to_tunnel_id(gre_key, seq);
198 /* Ignore GRE seq if there is no key present. */
202 if (greh->flags & GRE_SEQ)
203 hdr_len += GRE_HEADER_SECTION;
208 static bool check_checksum(struct sk_buff *skb)
210 struct iphdr *iph = ip_hdr(skb);
211 struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
214 if (greh->flags & GRE_CSUM) {
215 switch (skb->ip_summed) {
216 case CHECKSUM_COMPLETE:
217 csum = csum_fold(skb->csum);
225 csum = __skb_checksum_complete(skb);
226 skb->ip_summed = CHECKSUM_COMPLETE;
234 static u32 gre_flags_to_tunnel_flags(__be16 gre_flags, bool is_gre64)
236 u32 tunnel_flags = 0;
238 if (gre_flags & GRE_KEY || is_gre64)
239 tunnel_flags = OVS_TNL_F_KEY;
241 if (gre_flags & GRE_CSUM)
242 tunnel_flags |= OVS_TNL_F_CSUM;
247 /* Called with rcu_read_lock and BH disabled. */
248 static int gre_rcv(struct sk_buff *skb)
250 struct ovs_net *ovs_net;
254 struct ovs_key_ipv4_tunnel tun_key;
260 if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
262 if (unlikely(!check_checksum(skb)))
265 hdr_len = parse_header(ip_hdr(skb), &gre_flags, &key, &is_gre64);
266 if (unlikely(hdr_len < 0))
269 ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
271 vport = rcu_dereference(ovs_net->vport_net.gre64_vport);
273 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
274 if (unlikely(!vport))
277 if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
281 tnl_flags = gre_flags_to_tunnel_flags(gre_flags, is_gre64);
282 tnl_tun_key_init(&tun_key, iph, key, tnl_flags);
283 OVS_CB(skb)->tun_key = &tun_key;
285 __skb_pull(skb, hdr_len);
286 skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
288 ovs_tnl_rcv(vport, skb);
296 static const struct net_protocol gre_protocol_handlers = {
298 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
305 static int gre_init(void)
313 err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
315 pr_warn("cannot register gre protocol handler\n");
320 static void gre_exit(void)
327 inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
331 static const struct tnl_ops gre_tnl_ops = {
332 .ipproto = IPPROTO_GRE,
333 .hdr_len = gre_hdr_len,
334 .build_header = gre_build_header,
337 static struct vport *gre_create(const struct vport_parms *parms)
339 struct net *net = ovs_dp_get_net(parms->dp);
340 struct ovs_net *ovs_net;
343 ovs_net = net_generic(net, ovs_net_id);
344 if (rtnl_dereference(ovs_net->vport_net.gre_vport))
345 return ERR_PTR(-EEXIST);
347 vport = ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
349 rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
353 static void gre_tnl_destroy(struct vport *vport)
355 struct net *net = ovs_dp_get_net(vport->dp);
356 struct ovs_net *ovs_net;
358 ovs_net = net_generic(net, ovs_net_id);
360 rcu_assign_pointer(ovs_net->vport_net.gre_vport, NULL);
361 ovs_tnl_destroy(vport);
364 const struct vport_ops ovs_gre_vport_ops = {
365 .type = OVS_VPORT_TYPE_GRE,
366 .flags = VPORT_F_TUN_ID,
369 .create = gre_create,
370 .destroy = gre_tnl_destroy,
371 .get_name = ovs_tnl_get_name,
372 .send = ovs_tnl_send,
376 static const struct tnl_ops gre64_tnl_ops = {
377 .ipproto = IPPROTO_GRE,
378 .hdr_len = gre64_hdr_len,
379 .build_header = gre64_build_header,
382 static struct vport *gre64_create(const struct vport_parms *parms)
384 struct net *net = ovs_dp_get_net(parms->dp);
385 struct ovs_net *ovs_net;
388 ovs_net = net_generic(net, ovs_net_id);
389 if (rtnl_dereference(ovs_net->vport_net.gre64_vport))
390 return ERR_PTR(-EEXIST);
392 vport = ovs_tnl_create(parms, &ovs_gre64_vport_ops, &gre64_tnl_ops);
394 rcu_assign_pointer(ovs_net->vport_net.gre64_vport, vport);
399 static void gre64_tnl_destroy(struct vport *vport)
401 struct net *net = ovs_dp_get_net(vport->dp);
402 struct ovs_net *ovs_net;
404 ovs_net = net_generic(net, ovs_net_id);
406 rcu_assign_pointer(ovs_net->vport_net.gre64_vport, NULL);
407 ovs_tnl_destroy(vport);
410 const struct vport_ops ovs_gre64_vport_ops = {
411 .type = OVS_VPORT_TYPE_GRE64,
412 .flags = VPORT_F_TUN_ID,
415 .create = gre64_create,
416 .destroy = gre64_tnl_destroy,
417 .get_name = ovs_tnl_get_name,
418 .send = ovs_tnl_send,