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 /* Called with rcu_read_lock and BH disabled. */
205 static void gre_err(struct sk_buff *skb, u32 info)
208 const struct tnl_mutable_config *mutable;
209 const int type = icmp_hdr(skb)->type;
210 const int code = icmp_hdr(skb)->code;
211 int mtu = ntohs(icmp_hdr(skb)->un.frag.mtu);
217 int tunnel_hdr_len, tot_hdr_len;
218 unsigned int orig_mac_header;
219 unsigned int orig_nw_header;
221 if (type != ICMP_DEST_UNREACH || code != ICMP_FRAG_NEEDED)
225 * The mimimum size packet that we would actually be able to process:
226 * encapsulating IP header, minimum GRE header, Ethernet header,
229 if (!pskb_may_pull(skb, sizeof(struct iphdr) + GRE_HEADER_SECTION +
230 ETH_HLEN + sizeof(struct iphdr)))
233 iph = (struct iphdr *)skb->data;
234 if (ipv4_is_multicast(iph->daddr))
237 tunnel_hdr_len = parse_header(iph, &flags, &key, &tunnel_type);
238 if (tunnel_hdr_len < 0)
241 vport = ovs_tnl_find_port(dev_net(skb->dev), iph->saddr, iph->daddr, key,
242 tunnel_type, &mutable);
247 * Packets received by this function were previously sent by us, so
248 * any comparisons should be to the output values, not the input.
249 * However, it's not really worth it to have a hash table based on
250 * output keys (especially since ICMP error handling of tunneled packets
251 * isn't that reliable anyways). Therefore, we do a lookup based on the
252 * out key as if it were the in key and then check to see if the input
253 * and output keys are the same.
255 if (mutable->key.in_key != mutable->out_key)
258 if (!!(mutable->flags & TNL_F_IN_KEY_MATCH) !=
259 !!(mutable->flags & TNL_F_OUT_KEY_ACTION))
262 if ((mutable->flags & TNL_F_CSUM) && !(flags & GRE_CSUM))
265 tunnel_hdr_len += iph->ihl << 2;
267 orig_mac_header = skb_mac_header(skb) - skb->data;
268 orig_nw_header = skb_network_header(skb) - skb->data;
269 skb_set_mac_header(skb, tunnel_hdr_len);
271 tot_hdr_len = tunnel_hdr_len + ETH_HLEN;
273 skb->protocol = eth_hdr(skb)->h_proto;
274 if (skb->protocol == htons(ETH_P_8021Q)) {
275 tot_hdr_len += VLAN_HLEN;
276 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
279 skb_set_network_header(skb, tot_hdr_len);
282 if (skb->protocol == htons(ETH_P_IP))
283 tot_hdr_len += sizeof(struct iphdr);
284 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
285 else if (skb->protocol == htons(ETH_P_IPV6))
286 tot_hdr_len += sizeof(struct ipv6hdr);
291 if (!pskb_may_pull(skb, tot_hdr_len))
294 if (skb->protocol == htons(ETH_P_IP)) {
295 if (mtu < IP_MIN_MTU) {
296 if (ntohs(ip_hdr(skb)->tot_len) >= IP_MIN_MTU)
303 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
304 else if (skb->protocol == htons(ETH_P_IPV6)) {
305 if (mtu < IPV6_MIN_MTU) {
306 unsigned int packet_length = sizeof(struct ipv6hdr) +
307 ntohs(ipv6_hdr(skb)->payload_len);
309 if (packet_length >= IPV6_MIN_MTU
310 || ntohs(ipv6_hdr(skb)->payload_len) == 0)
318 __skb_pull(skb, tunnel_hdr_len);
319 ovs_tnl_frag_needed(vport, mutable, skb, mtu);
320 __skb_push(skb, tunnel_hdr_len);
323 skb_set_mac_header(skb, orig_mac_header);
324 skb_set_network_header(skb, orig_nw_header);
325 skb->protocol = htons(ETH_P_IP);
328 static bool check_checksum(struct sk_buff *skb)
330 struct iphdr *iph = ip_hdr(skb);
331 struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
334 if (greh->flags & GRE_CSUM) {
335 switch (skb->ip_summed) {
336 case CHECKSUM_COMPLETE:
337 csum = csum_fold(skb->csum);
345 csum = __skb_checksum_complete(skb);
346 skb->ip_summed = CHECKSUM_COMPLETE;
354 static u32 gre_flags_to_tunnel_flags(const struct tnl_mutable_config *mutable,
355 __be16 gre_flags, __be64 *key)
357 u32 tunnel_flags = 0;
359 if (gre_flags & GRE_KEY) {
360 if (mutable->flags & TNL_F_IN_KEY_MATCH ||
362 tunnel_flags = OVS_TNL_F_KEY;
367 if (gre_flags & GRE_CSUM)
368 tunnel_flags |= OVS_TNL_F_CSUM;
373 /* Called with rcu_read_lock and BH disabled. */
374 static int gre_rcv(struct sk_buff *skb)
377 const struct tnl_mutable_config *mutable;
380 struct ovs_key_ipv4_tunnel tun_key;
386 if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
388 if (unlikely(!check_checksum(skb)))
391 hdr_len = parse_header(ip_hdr(skb), &gre_flags, &key, &tunnel_type);
392 if (unlikely(hdr_len < 0))
395 if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
399 vport = ovs_tnl_find_port(dev_net(skb->dev), iph->daddr, iph->saddr, key,
400 tunnel_type, &mutable);
401 if (unlikely(!vport)) {
402 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
406 tnl_flags = gre_flags_to_tunnel_flags(mutable, gre_flags, &key);
407 tnl_tun_key_init(&tun_key, iph, key, tnl_flags);
408 OVS_CB(skb)->tun_key = &tun_key;
410 __skb_pull(skb, hdr_len);
411 skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
413 ovs_tnl_rcv(vport, skb);
421 static const struct tnl_ops gre_tnl_ops = {
422 .tunnel_type = TNL_T_PROTO_GRE,
423 .ipproto = IPPROTO_GRE,
424 .hdr_len = gre_hdr_len,
425 .build_header = gre_build_header,
428 static struct vport *gre_create(const struct vport_parms *parms)
430 return ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
433 static struct vport *gre_create_ft(const struct vport_parms *parms)
435 return ovs_tnl_create(parms, &ovs_gre_ft_vport_ops, &gre_tnl_ops);
438 static const struct tnl_ops gre64_tnl_ops = {
439 .tunnel_type = TNL_T_PROTO_GRE64,
440 .ipproto = IPPROTO_GRE,
441 .hdr_len = gre_hdr_len,
442 .build_header = gre_build_header,
445 static struct vport *gre_create64(const struct vport_parms *parms)
447 return ovs_tnl_create(parms, &ovs_gre64_vport_ops, &gre64_tnl_ops);
450 static const struct net_protocol gre_protocol_handlers = {
452 .err_handler = gre_err,
453 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
460 static int gre_init(void)
468 err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
470 pr_warn("cannot register gre protocol handler\n");
475 static void gre_exit(void)
482 inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
485 const struct vport_ops ovs_gre_ft_vport_ops = {
486 .type = OVS_VPORT_TYPE_FT_GRE,
487 .flags = VPORT_F_TUN_ID,
490 .create = gre_create_ft,
491 .destroy = ovs_tnl_destroy,
492 .set_addr = ovs_tnl_set_addr,
493 .get_name = ovs_tnl_get_name,
494 .get_addr = ovs_tnl_get_addr,
495 .get_options = ovs_tnl_get_options,
496 .set_options = ovs_tnl_set_options,
497 .send = ovs_tnl_send,
500 const struct vport_ops ovs_gre_vport_ops = {
501 .type = OVS_VPORT_TYPE_GRE,
502 .flags = VPORT_F_TUN_ID,
505 .create = gre_create,
506 .destroy = ovs_tnl_destroy,
507 .set_addr = ovs_tnl_set_addr,
508 .get_name = ovs_tnl_get_name,
509 .get_addr = ovs_tnl_get_addr,
510 .get_options = ovs_tnl_get_options,
511 .set_options = ovs_tnl_set_options,
512 .send = ovs_tnl_send,
515 const struct vport_ops ovs_gre64_vport_ops = {
516 .type = OVS_VPORT_TYPE_GRE64,
517 .flags = VPORT_F_TUN_ID,
520 .create = gre_create64,
521 .destroy = ovs_tnl_destroy,
522 .set_addr = ovs_tnl_set_addr,
523 .get_name = ovs_tnl_get_name,
524 .get_addr = ovs_tnl_get_addr,
525 .get_options = ovs_tnl_get_options,
526 .set_options = ovs_tnl_set_options,
527 .send = ovs_tnl_send,