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>
35 #include "vport-generic.h"
38 * The GRE header is composed of a series of sections: a base and then a variable
41 #define GRE_HEADER_SECTION 4
48 static int gre_hdr_len(const struct tnl_mutable_config *mutable,
49 const struct ovs_key_ipv4_tunnel *tun_key)
55 tnl_get_param(mutable, tun_key, &flags, &out_key);
56 len = GRE_HEADER_SECTION;
58 if (flags & TNL_F_CSUM)
59 len += GRE_HEADER_SECTION;
61 /* Set key for GRE64 tunnels, even when key if is zero. */
63 mutable->key.tunnel_type & TNL_T_PROTO_GRE64 ||
64 flags & TNL_F_OUT_KEY_ACTION) {
66 len += GRE_HEADER_SECTION;
67 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64)
68 len += GRE_HEADER_SECTION;
74 /* Returns the least-significant 32 bits of a __be64. */
75 static __be32 be64_get_low32(__be64 x)
78 return (__force __be32)x;
80 return (__force __be32)((__force u64)x >> 32);
84 static __be32 be64_get_high32(__be64 x)
87 return (__force __be32)((__force u64)x >> 32);
89 return (__force __be32)x;
93 static struct sk_buff *gre_build_header(const struct vport *vport,
94 const struct tnl_mutable_config *mutable,
95 struct dst_entry *dst,
101 const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
102 __be32 *options = (__be32 *)(skb_network_header(skb) + tunnel_hlen
103 - GRE_HEADER_SECTION);
104 struct gre_base_hdr *greh = (struct gre_base_hdr *) skb_transport_header(skb);
106 tnl_get_param(mutable, tun_key, &flags, &out_key);
108 greh->protocol = htons(ETH_P_TEB);
111 /* Work backwards over the options so the checksum is last. */
112 if (out_key || flags & TNL_F_OUT_KEY_ACTION ||
113 mutable->key.tunnel_type & TNL_T_PROTO_GRE64) {
114 greh->flags |= GRE_KEY;
115 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64) {
116 /* Set higher 32 bits to seq. */
117 *options = be64_get_high32(out_key);
119 greh->flags |= GRE_SEQ;
121 *options = be64_get_low32(out_key);
125 if (flags & TNL_F_CSUM) {
126 greh->flags |= GRE_CSUM;
128 *(__sum16 *)options = csum_fold(skb_checksum(skb,
129 skb_transport_offset(skb),
130 skb->len - skb_transport_offset(skb),
134 * Allow our local IP stack to fragment the outer packet even if the
135 * DF bit is set as a last resort. We also need to force selection of
136 * an IP ID here because Linux will otherwise leave it at 0 if the
137 * packet originally had DF set.
140 __ip_select_ident(ip_hdr(skb), dst, 0);
145 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
148 return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
150 return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
154 static int parse_header(struct iphdr *iph, __be16 *flags, __be64 *tun_id,
157 /* IP and ICMP protocol handlers check that the IHL is valid. */
158 struct gre_base_hdr *greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2));
159 __be32 *options = (__be32 *)(greh + 1);
162 *flags = greh->flags;
164 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
167 if (unlikely(greh->protocol != htons(ETH_P_TEB)))
170 hdr_len = GRE_HEADER_SECTION;
172 if (greh->flags & GRE_CSUM) {
173 hdr_len += GRE_HEADER_SECTION;
177 if (greh->flags & GRE_KEY) {
182 hdr_len += GRE_HEADER_SECTION;
185 if (greh->flags & GRE_SEQ) {
187 *tunnel_type = TNL_T_PROTO_GRE64;
190 *tunnel_type = TNL_T_PROTO_GRE;
192 *tun_id = key_to_tunnel_id(gre_key, seq);
195 /* Ignore GRE seq if there is no key present. */
196 *tunnel_type = TNL_T_PROTO_GRE;
199 if (greh->flags & GRE_SEQ)
200 hdr_len += GRE_HEADER_SECTION;
205 /* Called with rcu_read_lock and BH disabled. */
206 static void gre_err(struct sk_buff *skb, u32 info)
209 const struct tnl_mutable_config *mutable;
210 const int type = icmp_hdr(skb)->type;
211 const int code = icmp_hdr(skb)->code;
212 int mtu = ntohs(icmp_hdr(skb)->un.frag.mtu);
218 int tunnel_hdr_len, tot_hdr_len;
219 unsigned int orig_mac_header;
220 unsigned int orig_nw_header;
222 if (type != ICMP_DEST_UNREACH || code != ICMP_FRAG_NEEDED)
226 * The mimimum size packet that we would actually be able to process:
227 * encapsulating IP header, minimum GRE header, Ethernet header,
230 if (!pskb_may_pull(skb, sizeof(struct iphdr) + GRE_HEADER_SECTION +
231 ETH_HLEN + sizeof(struct iphdr)))
234 iph = (struct iphdr *)skb->data;
235 if (ipv4_is_multicast(iph->daddr))
238 tunnel_hdr_len = parse_header(iph, &flags, &key, &tunnel_type);
239 if (tunnel_hdr_len < 0)
242 vport = ovs_tnl_find_port(dev_net(skb->dev), iph->saddr, iph->daddr, key,
243 tunnel_type, &mutable);
248 * Packets received by this function were previously sent by us, so
249 * any comparisons should be to the output values, not the input.
250 * However, it's not really worth it to have a hash table based on
251 * output keys (especially since ICMP error handling of tunneled packets
252 * isn't that reliable anyways). Therefore, we do a lookup based on the
253 * out key as if it were the in key and then check to see if the input
254 * and output keys are the same.
256 if (mutable->key.in_key != mutable->out_key)
259 if (!!(mutable->flags & TNL_F_IN_KEY_MATCH) !=
260 !!(mutable->flags & TNL_F_OUT_KEY_ACTION))
263 if ((mutable->flags & TNL_F_CSUM) && !(flags & GRE_CSUM))
266 tunnel_hdr_len += iph->ihl << 2;
268 orig_mac_header = skb_mac_header(skb) - skb->data;
269 orig_nw_header = skb_network_header(skb) - skb->data;
270 skb_set_mac_header(skb, tunnel_hdr_len);
272 tot_hdr_len = tunnel_hdr_len + ETH_HLEN;
274 skb->protocol = eth_hdr(skb)->h_proto;
275 if (skb->protocol == htons(ETH_P_8021Q)) {
276 tot_hdr_len += VLAN_HLEN;
277 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
280 skb_set_network_header(skb, tot_hdr_len);
283 if (skb->protocol == htons(ETH_P_IP))
284 tot_hdr_len += sizeof(struct iphdr);
285 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
286 else if (skb->protocol == htons(ETH_P_IPV6))
287 tot_hdr_len += sizeof(struct ipv6hdr);
292 if (!pskb_may_pull(skb, tot_hdr_len))
295 if (skb->protocol == htons(ETH_P_IP)) {
296 if (mtu < IP_MIN_MTU) {
297 if (ntohs(ip_hdr(skb)->tot_len) >= IP_MIN_MTU)
304 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
305 else if (skb->protocol == htons(ETH_P_IPV6)) {
306 if (mtu < IPV6_MIN_MTU) {
307 unsigned int packet_length = sizeof(struct ipv6hdr) +
308 ntohs(ipv6_hdr(skb)->payload_len);
310 if (packet_length >= IPV6_MIN_MTU
311 || ntohs(ipv6_hdr(skb)->payload_len) == 0)
319 __skb_pull(skb, tunnel_hdr_len);
320 ovs_tnl_frag_needed(vport, mutable, skb, mtu);
321 __skb_push(skb, tunnel_hdr_len);
324 skb_set_mac_header(skb, orig_mac_header);
325 skb_set_network_header(skb, orig_nw_header);
326 skb->protocol = htons(ETH_P_IP);
329 static bool check_checksum(struct sk_buff *skb)
331 struct iphdr *iph = ip_hdr(skb);
332 struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
335 if (greh->flags & GRE_CSUM) {
336 switch (skb->ip_summed) {
337 case CHECKSUM_COMPLETE:
338 csum = csum_fold(skb->csum);
346 csum = __skb_checksum_complete(skb);
347 skb->ip_summed = CHECKSUM_COMPLETE;
355 static u32 gre_flags_to_tunnel_flags(const struct tnl_mutable_config *mutable,
356 __be16 gre_flags, __be64 *key)
358 u32 tunnel_flags = 0;
360 if (gre_flags & GRE_KEY) {
361 if (mutable->flags & TNL_F_IN_KEY_MATCH ||
363 tunnel_flags = OVS_TNL_F_KEY;
368 if (gre_flags & GRE_CSUM)
369 tunnel_flags |= OVS_TNL_F_CSUM;
374 /* Called with rcu_read_lock and BH disabled. */
375 static int gre_rcv(struct sk_buff *skb)
378 const struct tnl_mutable_config *mutable;
381 struct ovs_key_ipv4_tunnel tun_key;
387 if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
389 if (unlikely(!check_checksum(skb)))
392 hdr_len = parse_header(ip_hdr(skb), &gre_flags, &key, &tunnel_type);
393 if (unlikely(hdr_len < 0))
396 if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
400 vport = ovs_tnl_find_port(dev_net(skb->dev), iph->daddr, iph->saddr, key,
401 tunnel_type, &mutable);
402 if (unlikely(!vport)) {
403 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
407 tnl_flags = gre_flags_to_tunnel_flags(mutable, gre_flags, &key);
408 tnl_tun_key_init(&tun_key, iph, key, tnl_flags);
409 OVS_CB(skb)->tun_key = &tun_key;
411 __skb_pull(skb, hdr_len);
412 skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
414 ovs_tnl_rcv(vport, skb);
422 static const struct tnl_ops gre_tnl_ops = {
423 .tunnel_type = TNL_T_PROTO_GRE,
424 .ipproto = IPPROTO_GRE,
425 .hdr_len = gre_hdr_len,
426 .build_header = gre_build_header,
429 static struct vport *gre_create(const struct vport_parms *parms)
431 return ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
434 static struct vport *gre_create_ft(const struct vport_parms *parms)
436 return ovs_tnl_create(parms, &ovs_gre_ft_vport_ops, &gre_tnl_ops);
439 static const struct tnl_ops gre64_tnl_ops = {
440 .tunnel_type = TNL_T_PROTO_GRE64,
441 .ipproto = IPPROTO_GRE,
442 .hdr_len = gre_hdr_len,
443 .build_header = gre_build_header,
446 static struct vport *gre_create64(const struct vport_parms *parms)
448 return ovs_tnl_create(parms, &ovs_gre64_vport_ops, &gre64_tnl_ops);
451 static const struct net_protocol gre_protocol_handlers = {
453 .err_handler = gre_err,
454 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
461 static int gre_init(void)
469 err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
471 pr_warn("cannot register gre protocol handler\n");
476 static void gre_exit(void)
483 inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
486 const struct vport_ops ovs_gre_ft_vport_ops = {
487 .type = OVS_VPORT_TYPE_FT_GRE,
488 .flags = VPORT_F_TUN_ID,
491 .create = gre_create_ft,
492 .destroy = ovs_tnl_destroy,
493 .set_addr = ovs_tnl_set_addr,
494 .get_name = ovs_tnl_get_name,
495 .get_addr = ovs_tnl_get_addr,
496 .get_options = ovs_tnl_get_options,
497 .set_options = ovs_tnl_set_options,
498 .get_dev_flags = ovs_vport_gen_get_dev_flags,
499 .is_running = ovs_vport_gen_is_running,
500 .get_operstate = ovs_vport_gen_get_operstate,
501 .send = ovs_tnl_send,
504 const struct vport_ops ovs_gre_vport_ops = {
505 .type = OVS_VPORT_TYPE_GRE,
506 .flags = VPORT_F_TUN_ID,
509 .create = gre_create,
510 .destroy = ovs_tnl_destroy,
511 .set_addr = ovs_tnl_set_addr,
512 .get_name = ovs_tnl_get_name,
513 .get_addr = ovs_tnl_get_addr,
514 .get_options = ovs_tnl_get_options,
515 .set_options = ovs_tnl_set_options,
516 .get_dev_flags = ovs_vport_gen_get_dev_flags,
517 .is_running = ovs_vport_gen_is_running,
518 .get_operstate = ovs_vport_gen_get_operstate,
519 .send = ovs_tnl_send,
522 const struct vport_ops ovs_gre64_vport_ops = {
523 .type = OVS_VPORT_TYPE_GRE64,
524 .flags = VPORT_F_TUN_ID,
527 .create = gre_create64,
528 .destroy = ovs_tnl_destroy,
529 .set_addr = ovs_tnl_set_addr,
530 .get_name = ovs_tnl_get_name,
531 .get_addr = ovs_tnl_get_addr,
532 .get_options = ovs_tnl_get_options,
533 .set_options = ovs_tnl_set_options,
534 .get_dev_flags = ovs_vport_gen_get_dev_flags,
535 .is_running = ovs_vport_gen_is_running,
536 .get_operstate = ovs_vport_gen_get_operstate,
537 .send = ovs_tnl_send,