2 * Copyright (c) 2007-2011 Nicira Networks.
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>
34 #include "vport-generic.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)
51 len = GRE_HEADER_SECTION;
53 if (mutable->flags & TNL_F_CSUM)
54 len += GRE_HEADER_SECTION;
56 if (mutable->out_key || mutable->flags & TNL_F_OUT_KEY_ACTION)
57 len += GRE_HEADER_SECTION;
62 /* Returns the least-significant 32 bits of a __be64. */
63 static __be32 be64_get_low32(__be64 x)
66 return (__force __be32)x;
68 return (__force __be32)((__force u64)x >> 32);
72 static void gre_build_header(const struct vport *vport,
73 const struct tnl_mutable_config *mutable,
76 struct gre_base_hdr *greh = header;
77 __be32 *options = (__be32 *)(greh + 1);
79 greh->protocol = htons(ETH_P_TEB);
82 if (mutable->flags & TNL_F_CSUM) {
83 greh->flags |= GRE_CSUM;
88 if (mutable->out_key || mutable->flags & TNL_F_OUT_KEY_ACTION)
89 greh->flags |= GRE_KEY;
92 *options = be64_get_low32(mutable->out_key);
95 static struct sk_buff *gre_update_header(const struct vport *vport,
96 const struct tnl_mutable_config *mutable,
97 struct dst_entry *dst,
100 __be32 *options = (__be32 *)(skb_network_header(skb) + mutable->tunnel_hlen
101 - GRE_HEADER_SECTION);
103 /* Work backwards over the options so the checksum is last. */
104 if (mutable->flags & TNL_F_OUT_KEY_ACTION)
105 *options = be64_get_low32(OVS_CB(skb)->tun_id);
107 if (mutable->out_key || mutable->flags & TNL_F_OUT_KEY_ACTION)
110 if (mutable->flags & TNL_F_CSUM)
111 *(__sum16 *)options = csum_fold(skb_checksum(skb,
112 skb_transport_offset(skb),
113 skb->len - skb_transport_offset(skb),
116 * Allow our local IP stack to fragment the outer packet even if the
117 * DF bit is set as a last resort. We also need to force selection of
118 * an IP ID here because Linux will otherwise leave it at 0 if the
119 * packet originally had DF set.
122 __ip_select_ident(ip_hdr(skb), dst, 0);
127 /* Zero-extends a __be32 into the least-significant 32 bits of a __be64. */
128 static __be64 be32_extend_to_be64(__be32 x)
131 return (__force __be64)x;
133 return (__force __be64)((__force u64)x << 32);
137 static int parse_header(struct iphdr *iph, __be16 *flags, __be64 *key)
139 /* IP and ICMP protocol handlers check that the IHL is valid. */
140 struct gre_base_hdr *greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2));
141 __be32 *options = (__be32 *)(greh + 1);
144 *flags = greh->flags;
146 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
149 if (unlikely(greh->protocol != htons(ETH_P_TEB)))
152 hdr_len = GRE_HEADER_SECTION;
154 if (greh->flags & GRE_CSUM) {
155 hdr_len += GRE_HEADER_SECTION;
159 if (greh->flags & GRE_KEY) {
160 hdr_len += GRE_HEADER_SECTION;
162 *key = be32_extend_to_be64(*options);
167 if (unlikely(greh->flags & GRE_SEQ))
168 hdr_len += GRE_HEADER_SECTION;
173 /* Called with rcu_read_lock and BH disabled. */
174 static void gre_err(struct sk_buff *skb, u32 info)
177 const struct tnl_mutable_config *mutable;
178 const int type = icmp_hdr(skb)->type;
179 const int code = icmp_hdr(skb)->code;
180 int mtu = ntohs(icmp_hdr(skb)->un.frag.mtu);
185 int tunnel_hdr_len, tot_hdr_len;
186 unsigned int orig_mac_header;
187 unsigned int orig_nw_header;
189 if (type != ICMP_DEST_UNREACH || code != ICMP_FRAG_NEEDED)
193 * The mimimum size packet that we would actually be able to process:
194 * encapsulating IP header, minimum GRE header, Ethernet header,
197 if (!pskb_may_pull(skb, sizeof(struct iphdr) + GRE_HEADER_SECTION +
198 ETH_HLEN + sizeof(struct iphdr)))
201 iph = (struct iphdr *)skb->data;
202 if (ipv4_is_multicast(iph->daddr))
205 tunnel_hdr_len = parse_header(iph, &flags, &key);
206 if (tunnel_hdr_len < 0)
209 vport = ovs_tnl_find_port(iph->saddr, iph->daddr, key, TNL_T_PROTO_GRE,
215 * Packets received by this function were previously sent by us, so
216 * any comparisons should be to the output values, not the input.
217 * However, it's not really worth it to have a hash table based on
218 * output keys (especially since ICMP error handling of tunneled packets
219 * isn't that reliable anyways). Therefore, we do a lookup based on the
220 * out key as if it were the in key and then check to see if the input
221 * and output keys are the same.
223 if (mutable->key.in_key != mutable->out_key)
226 if (!!(mutable->flags & TNL_F_IN_KEY_MATCH) !=
227 !!(mutable->flags & TNL_F_OUT_KEY_ACTION))
230 if ((mutable->flags & TNL_F_CSUM) && !(flags & GRE_CSUM))
233 tunnel_hdr_len += iph->ihl << 2;
235 orig_mac_header = skb_mac_header(skb) - skb->data;
236 orig_nw_header = skb_network_header(skb) - skb->data;
237 skb_set_mac_header(skb, tunnel_hdr_len);
239 tot_hdr_len = tunnel_hdr_len + ETH_HLEN;
241 skb->protocol = eth_hdr(skb)->h_proto;
242 if (skb->protocol == htons(ETH_P_8021Q)) {
243 tot_hdr_len += VLAN_HLEN;
244 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
247 skb_set_network_header(skb, tot_hdr_len);
250 if (skb->protocol == htons(ETH_P_IP))
251 tot_hdr_len += sizeof(struct iphdr);
252 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
253 else if (skb->protocol == htons(ETH_P_IPV6))
254 tot_hdr_len += sizeof(struct ipv6hdr);
259 if (!pskb_may_pull(skb, tot_hdr_len))
262 if (skb->protocol == htons(ETH_P_IP)) {
263 if (mtu < IP_MIN_MTU) {
264 if (ntohs(ip_hdr(skb)->tot_len) >= IP_MIN_MTU)
271 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
272 else if (skb->protocol == htons(ETH_P_IPV6)) {
273 if (mtu < IPV6_MIN_MTU) {
274 unsigned int packet_length = sizeof(struct ipv6hdr) +
275 ntohs(ipv6_hdr(skb)->payload_len);
277 if (packet_length >= IPV6_MIN_MTU
278 || ntohs(ipv6_hdr(skb)->payload_len) == 0)
286 __skb_pull(skb, tunnel_hdr_len);
287 ovs_tnl_frag_needed(vport, mutable, skb, mtu, key);
288 __skb_push(skb, tunnel_hdr_len);
291 skb_set_mac_header(skb, orig_mac_header);
292 skb_set_network_header(skb, orig_nw_header);
293 skb->protocol = htons(ETH_P_IP);
296 static bool check_checksum(struct sk_buff *skb)
298 struct iphdr *iph = ip_hdr(skb);
299 struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
302 if (greh->flags & GRE_CSUM) {
303 switch (skb->ip_summed) {
304 case CHECKSUM_COMPLETE:
305 csum = csum_fold(skb->csum);
313 csum = __skb_checksum_complete(skb);
314 skb->ip_summed = CHECKSUM_COMPLETE;
322 /* Called with rcu_read_lock and BH disabled. */
323 static int gre_rcv(struct sk_buff *skb)
326 const struct tnl_mutable_config *mutable;
332 if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
335 if (unlikely(!check_checksum(skb)))
338 hdr_len = parse_header(ip_hdr(skb), &flags, &key);
339 if (unlikely(hdr_len < 0))
342 if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
346 vport = ovs_tnl_find_port(iph->daddr, iph->saddr, key, TNL_T_PROTO_GRE,
348 if (unlikely(!vport)) {
349 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
353 if (mutable->flags & TNL_F_IN_KEY_MATCH)
354 OVS_CB(skb)->tun_id = key;
356 OVS_CB(skb)->tun_id = 0;
358 __skb_pull(skb, hdr_len);
359 skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
361 ovs_tnl_rcv(vport, skb, iph->tos);
369 static const struct tnl_ops gre_tnl_ops = {
370 .tunnel_type = TNL_T_PROTO_GRE,
371 .ipproto = IPPROTO_GRE,
372 .hdr_len = gre_hdr_len,
373 .build_header = gre_build_header,
374 .update_header = gre_update_header,
377 static struct vport *gre_create(const struct vport_parms *parms)
379 return ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
382 static const struct net_protocol gre_protocol_handlers = {
384 .err_handler = gre_err,
387 static int gre_init(void)
391 err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
393 pr_warn("cannot register gre protocol handler\n");
398 static void gre_exit(void)
400 inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
403 const struct vport_ops ovs_gre_vport_ops = {
404 .type = OVS_VPORT_TYPE_GRE,
405 .flags = VPORT_F_TUN_ID,
408 .create = gre_create,
409 .destroy = ovs_tnl_destroy,
410 .set_addr = ovs_tnl_set_addr,
411 .get_name = ovs_tnl_get_name,
412 .get_addr = ovs_tnl_get_addr,
413 .get_options = ovs_tnl_get_options,
414 .set_options = ovs_tnl_set_options,
415 .get_dev_flags = ovs_vport_gen_get_dev_flags,
416 .is_running = ovs_vport_gen_is_running,
417 .get_operstate = ovs_vport_gen_get_operstate,
418 .send = ovs_tnl_send,