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)
52 len = GRE_HEADER_SECTION;
54 if (mutable->flags & TNL_F_CSUM)
55 len += GRE_HEADER_SECTION;
57 if (mutable->out_key || mutable->flags & TNL_F_OUT_KEY_ACTION) {
58 len += GRE_HEADER_SECTION;
59 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64)
60 len += GRE_HEADER_SECTION;
66 /* Returns the least-significant 32 bits of a __be64. */
67 static __be32 be64_get_low32(__be64 x)
70 return (__force __be32)x;
72 return (__force __be32)((__force u64)x >> 32);
76 static __be32 be64_get_high32(__be64 x)
79 return (__force __be32)((__force u64)x >> 32);
81 return (__force __be32)x;
85 static void gre_build_header(const struct vport *vport,
86 const struct tnl_mutable_config *mutable,
89 struct gre_base_hdr *greh = header;
90 __be32 *options = (__be32 *)(greh + 1);
92 greh->protocol = htons(ETH_P_TEB);
95 if (mutable->flags & TNL_F_CSUM) {
96 greh->flags |= GRE_CSUM;
101 if (mutable->flags & TNL_F_OUT_KEY_ACTION) {
102 greh->flags |= GRE_KEY;
103 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64)
104 greh->flags |= GRE_SEQ;
106 } else if (mutable->out_key) {
107 greh->flags |= GRE_KEY;
108 *options = be64_get_low32(mutable->out_key);
109 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64) {
111 *options = be64_get_high32(mutable->out_key);
112 greh->flags |= GRE_SEQ;
117 static struct sk_buff *gre_update_header(const struct vport *vport,
118 const struct tnl_mutable_config *mutable,
119 struct dst_entry *dst,
122 __be32 *options = (__be32 *)(skb_network_header(skb) + mutable->tunnel_hlen
123 - GRE_HEADER_SECTION);
125 /* Work backwards over the options so the checksum is last. */
126 if (mutable->flags & TNL_F_OUT_KEY_ACTION) {
127 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64) {
128 /* Set higher 32 bits to seq. */
129 *options = be64_get_high32(OVS_CB(skb)->tun_id);
132 *options = be64_get_low32(OVS_CB(skb)->tun_id);
134 } else if (mutable->out_key) {
136 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64)
140 if (mutable->flags & TNL_F_CSUM)
141 *(__sum16 *)options = csum_fold(skb_checksum(skb,
142 skb_transport_offset(skb),
143 skb->len - skb_transport_offset(skb),
146 * Allow our local IP stack to fragment the outer packet even if the
147 * DF bit is set as a last resort. We also need to force selection of
148 * an IP ID here because Linux will otherwise leave it at 0 if the
149 * packet originally had DF set.
152 __ip_select_ident(ip_hdr(skb), dst, 0);
157 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
160 return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
162 return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
166 static int parse_header(struct iphdr *iph, __be16 *flags, __be64 *tun_id,
169 /* IP and ICMP protocol handlers check that the IHL is valid. */
170 struct gre_base_hdr *greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2));
171 __be32 *options = (__be32 *)(greh + 1);
174 *flags = greh->flags;
176 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
179 if (unlikely(greh->protocol != htons(ETH_P_TEB)))
182 hdr_len = GRE_HEADER_SECTION;
184 if (greh->flags & GRE_CSUM) {
185 hdr_len += GRE_HEADER_SECTION;
189 if (greh->flags & GRE_KEY) {
194 hdr_len += GRE_HEADER_SECTION;
197 if (greh->flags & GRE_SEQ) {
199 *tunnel_type = TNL_T_PROTO_GRE64;
202 *tunnel_type = TNL_T_PROTO_GRE;
204 *tun_id = key_to_tunnel_id(gre_key, seq);
208 if (greh->flags & GRE_SEQ)
209 hdr_len += GRE_HEADER_SECTION;
214 /* Called with rcu_read_lock and BH disabled. */
215 static void gre_err(struct sk_buff *skb, u32 info)
218 const struct tnl_mutable_config *mutable;
219 const int type = icmp_hdr(skb)->type;
220 const int code = icmp_hdr(skb)->code;
221 int mtu = ntohs(icmp_hdr(skb)->un.frag.mtu);
227 int tunnel_hdr_len, tot_hdr_len;
228 unsigned int orig_mac_header;
229 unsigned int orig_nw_header;
231 if (type != ICMP_DEST_UNREACH || code != ICMP_FRAG_NEEDED)
235 * The mimimum size packet that we would actually be able to process:
236 * encapsulating IP header, minimum GRE header, Ethernet header,
239 if (!pskb_may_pull(skb, sizeof(struct iphdr) + GRE_HEADER_SECTION +
240 ETH_HLEN + sizeof(struct iphdr)))
243 iph = (struct iphdr *)skb->data;
244 if (ipv4_is_multicast(iph->daddr))
247 tunnel_hdr_len = parse_header(iph, &flags, &key, &tunnel_type);
248 if (tunnel_hdr_len < 0)
251 vport = ovs_tnl_find_port(dev_net(skb->dev), iph->saddr, iph->daddr, key,
252 tunnel_type, &mutable);
257 * Packets received by this function were previously sent by us, so
258 * any comparisons should be to the output values, not the input.
259 * However, it's not really worth it to have a hash table based on
260 * output keys (especially since ICMP error handling of tunneled packets
261 * isn't that reliable anyways). Therefore, we do a lookup based on the
262 * out key as if it were the in key and then check to see if the input
263 * and output keys are the same.
265 if (mutable->key.in_key != mutable->out_key)
268 if (!!(mutable->flags & TNL_F_IN_KEY_MATCH) !=
269 !!(mutable->flags & TNL_F_OUT_KEY_ACTION))
272 if ((mutable->flags & TNL_F_CSUM) && !(flags & GRE_CSUM))
275 tunnel_hdr_len += iph->ihl << 2;
277 orig_mac_header = skb_mac_header(skb) - skb->data;
278 orig_nw_header = skb_network_header(skb) - skb->data;
279 skb_set_mac_header(skb, tunnel_hdr_len);
281 tot_hdr_len = tunnel_hdr_len + ETH_HLEN;
283 skb->protocol = eth_hdr(skb)->h_proto;
284 if (skb->protocol == htons(ETH_P_8021Q)) {
285 tot_hdr_len += VLAN_HLEN;
286 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
289 skb_set_network_header(skb, tot_hdr_len);
292 if (skb->protocol == htons(ETH_P_IP))
293 tot_hdr_len += sizeof(struct iphdr);
294 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
295 else if (skb->protocol == htons(ETH_P_IPV6))
296 tot_hdr_len += sizeof(struct ipv6hdr);
301 if (!pskb_may_pull(skb, tot_hdr_len))
304 if (skb->protocol == htons(ETH_P_IP)) {
305 if (mtu < IP_MIN_MTU) {
306 if (ntohs(ip_hdr(skb)->tot_len) >= IP_MIN_MTU)
313 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
314 else if (skb->protocol == htons(ETH_P_IPV6)) {
315 if (mtu < IPV6_MIN_MTU) {
316 unsigned int packet_length = sizeof(struct ipv6hdr) +
317 ntohs(ipv6_hdr(skb)->payload_len);
319 if (packet_length >= IPV6_MIN_MTU
320 || ntohs(ipv6_hdr(skb)->payload_len) == 0)
328 __skb_pull(skb, tunnel_hdr_len);
329 ovs_tnl_frag_needed(vport, mutable, skb, mtu, key);
330 __skb_push(skb, tunnel_hdr_len);
333 skb_set_mac_header(skb, orig_mac_header);
334 skb_set_network_header(skb, orig_nw_header);
335 skb->protocol = htons(ETH_P_IP);
338 static bool check_checksum(struct sk_buff *skb)
340 struct iphdr *iph = ip_hdr(skb);
341 struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
344 if (greh->flags & GRE_CSUM) {
345 switch (skb->ip_summed) {
346 case CHECKSUM_COMPLETE:
347 csum = csum_fold(skb->csum);
355 csum = __skb_checksum_complete(skb);
356 skb->ip_summed = CHECKSUM_COMPLETE;
364 /* Called with rcu_read_lock and BH disabled. */
365 static int gre_rcv(struct sk_buff *skb)
368 const struct tnl_mutable_config *mutable;
375 if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
377 if (unlikely(!check_checksum(skb)))
380 hdr_len = parse_header(ip_hdr(skb), &flags, &key, &tunnel_type);
381 if (unlikely(hdr_len < 0))
384 if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
388 vport = ovs_tnl_find_port(dev_net(skb->dev), iph->daddr, iph->saddr, key,
389 tunnel_type, &mutable);
390 if (unlikely(!vport)) {
391 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
395 if (mutable->flags & TNL_F_IN_KEY_MATCH)
396 OVS_CB(skb)->tun_id = key;
398 OVS_CB(skb)->tun_id = 0;
400 __skb_pull(skb, hdr_len);
401 skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
403 ovs_tnl_rcv(vport, skb, iph->tos);
411 static const struct tnl_ops gre_tnl_ops = {
412 .tunnel_type = TNL_T_PROTO_GRE,
413 .ipproto = IPPROTO_GRE,
414 .hdr_len = gre_hdr_len,
415 .build_header = gre_build_header,
416 .update_header = gre_update_header,
419 static struct vport *gre_create(const struct vport_parms *parms)
421 return ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
424 static const struct tnl_ops gre64_tnl_ops = {
425 .tunnel_type = TNL_T_PROTO_GRE64,
426 .ipproto = IPPROTO_GRE,
427 .hdr_len = gre_hdr_len,
428 .build_header = gre_build_header,
429 .update_header = gre_update_header,
432 static struct vport *gre_create64(const struct vport_parms *parms)
434 return ovs_tnl_create(parms, &ovs_gre64_vport_ops, &gre64_tnl_ops);
437 static const struct net_protocol gre_protocol_handlers = {
439 .err_handler = gre_err,
440 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
447 static int gre_init(void)
455 err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
457 pr_warn("cannot register gre protocol handler\n");
462 static void gre_exit(void)
469 inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
472 const struct vport_ops ovs_gre_vport_ops = {
473 .type = OVS_VPORT_TYPE_GRE,
474 .flags = VPORT_F_TUN_ID,
477 .create = gre_create,
478 .destroy = ovs_tnl_destroy,
479 .set_addr = ovs_tnl_set_addr,
480 .get_name = ovs_tnl_get_name,
481 .get_addr = ovs_tnl_get_addr,
482 .get_options = ovs_tnl_get_options,
483 .set_options = ovs_tnl_set_options,
484 .get_dev_flags = ovs_vport_gen_get_dev_flags,
485 .is_running = ovs_vport_gen_is_running,
486 .get_operstate = ovs_vport_gen_get_operstate,
487 .send = ovs_tnl_send,
490 const struct vport_ops ovs_gre64_vport_ops = {
491 .type = OVS_VPORT_TYPE_GRE64,
492 .flags = VPORT_F_TUN_ID,
495 .create = gre_create64,
496 .destroy = ovs_tnl_destroy,
497 .set_addr = ovs_tnl_set_addr,
498 .get_name = ovs_tnl_get_name,
499 .get_addr = ovs_tnl_get_addr,
500 .get_options = ovs_tnl_get_options,
501 .set_options = ovs_tnl_set_options,
502 .get_dev_flags = ovs_vport_gen_get_dev_flags,
503 .is_running = ovs_vport_gen_is_running,
504 .get_operstate = ovs_vport_gen_get_operstate,
505 .send = ovs_tnl_send,