2 * Copyright (c) 2010, 2011 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/skbuff.h>
14 #include <linux/if_tunnel.h>
15 #include <linux/if_vlan.h>
20 #include <net/protocol.h>
24 #include "vport-generic.h"
27 * The GRE header is composed of a series of sections: a base and then a variable
30 #define GRE_HEADER_SECTION 4
37 static int gre_hdr_len(const struct tnl_mutable_config *mutable)
41 len = GRE_HEADER_SECTION;
43 if (mutable->flags & TNL_F_CSUM)
44 len += GRE_HEADER_SECTION;
46 if (mutable->out_key || mutable->flags & TNL_F_OUT_KEY_ACTION)
47 len += GRE_HEADER_SECTION;
52 /* Returns the least-significant 32 bits of a __be64. */
53 static __be32 be64_get_low32(__be64 x)
56 return (__force __be32)x;
58 return (__force __be32)((__force u64)x >> 32);
62 static void gre_build_header(const struct vport *vport,
63 const struct tnl_mutable_config *mutable,
66 struct gre_base_hdr *greh = header;
67 __be32 *options = (__be32 *)(greh + 1);
69 greh->protocol = htons(ETH_P_TEB);
72 if (mutable->flags & TNL_F_CSUM) {
73 greh->flags |= GRE_CSUM;
78 if (mutable->out_key || mutable->flags & TNL_F_OUT_KEY_ACTION)
79 greh->flags |= GRE_KEY;
82 *options = be64_get_low32(mutable->out_key);
85 static struct sk_buff *gre_update_header(const struct vport *vport,
86 const struct tnl_mutable_config *mutable,
87 struct dst_entry *dst,
90 __be32 *options = (__be32 *)(skb_network_header(skb) + mutable->tunnel_hlen
91 - GRE_HEADER_SECTION);
93 /* Work backwards over the options so the checksum is last. */
94 if (mutable->flags & TNL_F_OUT_KEY_ACTION)
95 *options = be64_get_low32(OVS_CB(skb)->tun_id);
97 if (mutable->out_key || mutable->flags & TNL_F_OUT_KEY_ACTION)
100 if (mutable->flags & TNL_F_CSUM)
101 *(__sum16 *)options = csum_fold(skb_checksum(skb,
102 skb_transport_offset(skb),
103 skb->len - skb_transport_offset(skb),
106 * Allow our local IP stack to fragment the outer packet even if the
107 * DF bit is set as a last resort. We also need to force selection of
108 * an IP ID here because Linux will otherwise leave it at 0 if the
109 * packet originally had DF set.
112 __ip_select_ident(ip_hdr(skb), dst, 0);
117 /* Zero-extends a __be32 into the least-significant 32 bits of a __be64. */
118 static __be64 be32_extend_to_be64(__be32 x)
121 return (__force __be64)x;
123 return (__force __be64)((__force u64)x << 32);
127 static int parse_header(struct iphdr *iph, __be16 *flags, __be64 *key)
129 /* IP and ICMP protocol handlers check that the IHL is valid. */
130 struct gre_base_hdr *greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2));
131 __be32 *options = (__be32 *)(greh + 1);
134 *flags = greh->flags;
136 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
139 if (unlikely(greh->protocol != htons(ETH_P_TEB)))
142 hdr_len = GRE_HEADER_SECTION;
144 if (greh->flags & GRE_CSUM) {
145 hdr_len += GRE_HEADER_SECTION;
149 if (greh->flags & GRE_KEY) {
150 hdr_len += GRE_HEADER_SECTION;
152 *key = be32_extend_to_be64(*options);
157 if (unlikely(greh->flags & GRE_SEQ))
158 hdr_len += GRE_HEADER_SECTION;
163 /* Called with rcu_read_lock and BH disabled. */
164 static void gre_err(struct sk_buff *skb, u32 info)
167 const struct tnl_mutable_config *mutable;
168 const int type = icmp_hdr(skb)->type;
169 const int code = icmp_hdr(skb)->code;
170 int mtu = ntohs(icmp_hdr(skb)->un.frag.mtu);
175 int tunnel_hdr_len, tot_hdr_len;
176 unsigned int orig_mac_header;
177 unsigned int orig_nw_header;
179 if (type != ICMP_DEST_UNREACH || code != ICMP_FRAG_NEEDED)
183 * The mimimum size packet that we would actually be able to process:
184 * encapsulating IP header, minimum GRE header, Ethernet header,
187 if (!pskb_may_pull(skb, sizeof(struct iphdr) + GRE_HEADER_SECTION +
188 ETH_HLEN + sizeof(struct iphdr)))
191 iph = (struct iphdr *)skb->data;
193 tunnel_hdr_len = parse_header(iph, &flags, &key);
194 if (tunnel_hdr_len < 0)
197 vport = tnl_find_port(iph->saddr, iph->daddr, key, TNL_T_PROTO_GRE,
203 * Packets received by this function were previously sent by us, so
204 * any comparisons should be to the output values, not the input.
205 * However, it's not really worth it to have a hash table based on
206 * output keys (especially since ICMP error handling of tunneled packets
207 * isn't that reliable anyways). Therefore, we do a lookup based on the
208 * out key as if it were the in key and then check to see if the input
209 * and output keys are the same.
211 if (mutable->key.in_key != mutable->out_key)
214 if (!!(mutable->flags & TNL_F_IN_KEY_MATCH) !=
215 !!(mutable->flags & TNL_F_OUT_KEY_ACTION))
218 if ((mutable->flags & TNL_F_CSUM) && !(flags & GRE_CSUM))
221 tunnel_hdr_len += iph->ihl << 2;
223 orig_mac_header = skb_mac_header(skb) - skb->data;
224 orig_nw_header = skb_network_header(skb) - skb->data;
225 skb_set_mac_header(skb, tunnel_hdr_len);
227 tot_hdr_len = tunnel_hdr_len + ETH_HLEN;
229 skb->protocol = eth_hdr(skb)->h_proto;
230 if (skb->protocol == htons(ETH_P_8021Q)) {
231 tot_hdr_len += VLAN_HLEN;
232 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
235 skb_set_network_header(skb, tot_hdr_len);
238 if (skb->protocol == htons(ETH_P_IP))
239 tot_hdr_len += sizeof(struct iphdr);
240 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
241 else if (skb->protocol == htons(ETH_P_IPV6))
242 tot_hdr_len += sizeof(struct ipv6hdr);
247 if (!pskb_may_pull(skb, tot_hdr_len))
250 if (skb->protocol == htons(ETH_P_IP)) {
251 if (mtu < IP_MIN_MTU) {
252 if (ntohs(ip_hdr(skb)->tot_len) >= IP_MIN_MTU)
259 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
260 else if (skb->protocol == htons(ETH_P_IPV6)) {
261 if (mtu < IPV6_MIN_MTU) {
262 unsigned int packet_length = sizeof(struct ipv6hdr) +
263 ntohs(ipv6_hdr(skb)->payload_len);
265 if (packet_length >= IPV6_MIN_MTU
266 || ntohs(ipv6_hdr(skb)->payload_len) == 0)
274 __skb_pull(skb, tunnel_hdr_len);
275 tnl_frag_needed(vport, mutable, skb, mtu, key);
276 __skb_push(skb, tunnel_hdr_len);
279 skb_set_mac_header(skb, orig_mac_header);
280 skb_set_network_header(skb, orig_nw_header);
281 skb->protocol = htons(ETH_P_IP);
284 static bool check_checksum(struct sk_buff *skb)
286 struct iphdr *iph = ip_hdr(skb);
287 struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
290 if (greh->flags & GRE_CSUM) {
291 switch (skb->ip_summed) {
292 case CHECKSUM_COMPLETE:
293 csum = csum_fold(skb->csum);
301 csum = __skb_checksum_complete(skb);
302 skb->ip_summed = CHECKSUM_COMPLETE;
310 /* Called with rcu_read_lock and BH disabled. */
311 static int gre_rcv(struct sk_buff *skb)
314 const struct tnl_mutable_config *mutable;
320 if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
323 if (unlikely(!check_checksum(skb)))
326 hdr_len = parse_header(ip_hdr(skb), &flags, &key);
327 if (unlikely(hdr_len < 0))
330 if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
334 vport = tnl_find_port(iph->daddr, iph->saddr, key, TNL_T_PROTO_GRE,
336 if (unlikely(!vport)) {
337 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
341 if (mutable->flags & TNL_F_IN_KEY_MATCH)
342 OVS_CB(skb)->tun_id = key;
344 OVS_CB(skb)->tun_id = 0;
346 __skb_pull(skb, hdr_len);
347 skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
349 tnl_rcv(vport, skb, iph->tos);
357 static const struct tnl_ops gre_tnl_ops = {
358 .tunnel_type = TNL_T_PROTO_GRE,
359 .ipproto = IPPROTO_GRE,
360 .hdr_len = gre_hdr_len,
361 .build_header = gre_build_header,
362 .update_header = gre_update_header,
365 static struct vport *gre_create(const struct vport_parms *parms)
367 return tnl_create(parms, &gre_vport_ops, &gre_tnl_ops);
370 static const struct net_protocol gre_protocol_handlers = {
372 .err_handler = gre_err,
375 static int gre_init(void)
379 err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
381 pr_warn("cannot register gre protocol handler\n");
386 static void gre_exit(void)
388 inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
391 const struct vport_ops gre_vport_ops = {
392 .type = OVS_VPORT_TYPE_GRE,
393 .flags = VPORT_F_TUN_ID,
396 .create = gre_create,
397 .destroy = tnl_destroy,
398 .set_addr = tnl_set_addr,
399 .get_name = tnl_get_name,
400 .get_addr = tnl_get_addr,
401 .get_options = tnl_get_options,
402 .set_options = tnl_set_options,
403 .get_dev_flags = vport_gen_get_dev_flags,
404 .is_running = vport_gen_is_running,
405 .get_operstate = vport_gen_get_operstate,