2 * Copyright (c) 2010 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 #include <linux/if_arp.h>
10 #include <linux/if_ether.h>
12 #include <linux/if_tunnel.h>
13 #include <linux/if_vlan.h>
15 #include <linux/in_route.h>
16 #include <linux/jhash.h>
17 #include <linux/kernel.h>
18 #include <linux/version.h>
20 #include <net/dsfield.h>
23 #include <net/inet_ecn.h>
25 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
28 #include <net/protocol.h>
29 #include <net/route.h>
34 #include "openvswitch/gre.h"
37 #include "vport-generic.h"
39 /* The absolute minimum fragment size. Note that there are many other
40 * definitions of the minimum MTU. */
43 /* The GRE header is composed of a series of sections: a base and then a variable
44 * number of options. */
45 #define GRE_HEADER_SECTION 4
47 struct mutable_config {
50 unsigned char eth_addr[ETH_ALEN];
52 struct gre_port_config port_config;
54 int tunnel_hlen; /* Tunnel header length. */
58 struct tbl_node tbl_node;
62 /* Protected by RCU. */
63 struct mutable_config *mutable;
66 struct vport_ops gre_vport_ops;
68 /* Protected by RCU. */
69 static struct tbl *port_table;
71 /* These are just used as an optimization: they don't require any kind of
72 * synchronization because we could have just as easily read the value before
73 * the port change happened. */
74 static unsigned int key_local_remote_ports;
75 static unsigned int key_remote_ports;
76 static unsigned int local_remote_ports;
77 static unsigned int remote_ports;
79 static inline struct gre_vport *
80 gre_vport_priv(const struct vport *vport)
82 return vport_priv(vport);
85 static inline struct vport *
86 gre_vport_to_vport(const struct gre_vport *gre_vport)
88 return vport_from_priv(gre_vport);
91 static inline struct gre_vport *
92 gre_vport_table_cast(const struct tbl_node *node)
94 return container_of(node, struct gre_vport, tbl_node);
99 free_config(struct rcu_head *rcu)
101 struct mutable_config *c = container_of(rcu, struct mutable_config, rcu);
106 assign_config_rcu(struct vport *vport, struct mutable_config *new_config)
108 struct gre_vport *gre_vport = gre_vport_priv(vport);
109 struct mutable_config *old_config;
111 old_config = rcu_dereference(gre_vport->mutable);
112 rcu_assign_pointer(gre_vport->mutable, new_config);
113 call_rcu(&old_config->rcu, free_config);
116 static unsigned int *
117 find_port_pool(const struct mutable_config *mutable)
119 if (mutable->port_config.flags & GRE_F_IN_KEY_MATCH) {
120 if (mutable->port_config.saddr)
121 return &local_remote_ports;
123 return &remote_ports;
125 if (mutable->port_config.saddr)
126 return &key_local_remote_ports;
128 return &key_remote_ports;
139 struct port_lookup_key {
140 u32 vals[4]; /* Contains enum lookup_key keys. */
141 const struct mutable_config *mutable;
144 /* Modifies 'target' to store the rcu_dereferenced pointer that was used to do
145 * the comparision. */
147 port_cmp(const struct tbl_node *node, void *target)
149 const struct gre_vport *gre_vport = gre_vport_table_cast(node);
150 struct port_lookup_key *lookup = target;
152 lookup->mutable = rcu_dereference(gre_vport->mutable);
154 return ((lookup->mutable->port_config.flags & GRE_F_IN_KEY_MATCH) ==
155 lookup->vals[LOOKUP_KEY_MATCH]) &&
156 lookup->mutable->port_config.daddr == lookup->vals[LOOKUP_DADDR] &&
157 lookup->mutable->port_config.in_key == lookup->vals[LOOKUP_KEY] &&
158 lookup->mutable->port_config.saddr == lookup->vals[LOOKUP_SADDR];
162 port_hash(struct port_lookup_key *lookup)
164 return jhash2(lookup->vals, ARRAY_SIZE(lookup->vals), 0);
168 add_port(struct vport *vport)
170 struct gre_vport *gre_vport = gre_vport_priv(vport);
171 struct port_lookup_key lookup;
175 struct tbl *new_table;
177 new_table = tbl_create(0);
181 rcu_assign_pointer(port_table, new_table);
183 } else if (tbl_count(port_table) > tbl_n_buckets(port_table)) {
184 struct tbl *old_table = port_table;
185 struct tbl *new_table;
187 new_table = tbl_expand(old_table);
188 if (IS_ERR(new_table))
189 return PTR_ERR(new_table);
191 rcu_assign_pointer(port_table, new_table);
192 tbl_deferred_destroy(old_table, NULL);
195 lookup.vals[LOOKUP_SADDR] = gre_vport->mutable->port_config.saddr;
196 lookup.vals[LOOKUP_DADDR] = gre_vport->mutable->port_config.daddr;
197 lookup.vals[LOOKUP_KEY] = gre_vport->mutable->port_config.in_key;
198 lookup.vals[LOOKUP_KEY_MATCH] = gre_vport->mutable->port_config.flags & GRE_F_IN_KEY_MATCH;
200 err = tbl_insert(port_table, &gre_vport->tbl_node, port_hash(&lookup));
204 (*find_port_pool(gre_vport->mutable))++;
210 del_port(struct vport *vport)
212 struct gre_vport *gre_vport = gre_vport_priv(vport);
215 err = tbl_remove(port_table, &gre_vport->tbl_node);
219 (*find_port_pool(gre_vport->mutable))--;
224 #define FIND_PORT_KEY (1 << 0)
225 #define FIND_PORT_MATCH (1 << 1)
226 #define FIND_PORT_ANY (FIND_PORT_KEY | FIND_PORT_MATCH)
228 static struct vport *
229 find_port(__be32 saddr, __be32 daddr, __be32 key, int port_type,
230 const struct mutable_config **mutable)
232 struct port_lookup_key lookup;
233 struct tbl *table = rcu_dereference(port_table);
234 struct tbl_node *tbl_node;
239 lookup.vals[LOOKUP_SADDR] = saddr;
240 lookup.vals[LOOKUP_DADDR] = daddr;
242 if (port_type & FIND_PORT_KEY) {
243 lookup.vals[LOOKUP_KEY] = key;
244 lookup.vals[LOOKUP_KEY_MATCH] = 0;
246 if (key_local_remote_ports) {
247 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
252 if (key_remote_ports) {
253 lookup.vals[LOOKUP_SADDR] = 0;
255 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
259 lookup.vals[LOOKUP_SADDR] = saddr;
263 if (port_type & FIND_PORT_MATCH) {
264 lookup.vals[LOOKUP_KEY] = 0;
265 lookup.vals[LOOKUP_KEY_MATCH] = GRE_F_IN_KEY_MATCH;
267 if (local_remote_ports) {
268 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
274 lookup.vals[LOOKUP_SADDR] = 0;
276 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
285 *mutable = lookup.mutable;
286 return gre_vport_to_vport(gre_vport_table_cast(tbl_node));
290 check_ipv4_address(__be32 addr)
292 if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr)
293 || ipv4_is_loopback(addr) || ipv4_is_zeronet(addr))
300 ipv4_should_icmp(struct sk_buff *skb)
302 struct iphdr *old_iph = ip_hdr(skb);
304 /* Don't respond to L2 broadcast. */
305 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
308 /* Don't respond to L3 broadcast or invalid addresses. */
309 if (!check_ipv4_address(old_iph->daddr) ||
310 !check_ipv4_address(old_iph->saddr))
313 /* Only respond to the first fragment. */
314 if (old_iph->frag_off & htons(IP_OFFSET))
317 /* Don't respond to ICMP error messages. */
318 if (old_iph->protocol == IPPROTO_ICMP) {
319 u8 icmp_type, *icmp_typep;
321 icmp_typep = skb_header_pointer(skb, (u8 *)old_iph +
322 (old_iph->ihl << 2) +
323 offsetof(struct icmphdr, type) -
324 skb->data, sizeof(icmp_type),
330 if (*icmp_typep > NR_ICMP_TYPES
331 || (*icmp_typep <= ICMP_PARAMETERPROB
332 && *icmp_typep != ICMP_ECHOREPLY
333 && *icmp_typep != ICMP_ECHO))
341 ipv4_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
342 unsigned int mtu, unsigned int payload_length)
344 struct iphdr *iph, *old_iph = ip_hdr(skb);
345 struct icmphdr *icmph;
348 iph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
349 icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
350 payload = skb_put(nskb, payload_length);
354 iph->ihl = sizeof(struct iphdr) >> 2;
355 iph->tos = (old_iph->tos & IPTOS_TOS_MASK) |
356 IPTOS_PREC_INTERNETCONTROL;
357 iph->tot_len = htons(sizeof(struct iphdr)
358 + sizeof(struct icmphdr)
360 get_random_bytes(&iph->id, sizeof(iph->id));
363 iph->protocol = IPPROTO_ICMP;
364 iph->daddr = old_iph->saddr;
365 iph->saddr = old_iph->daddr;
370 icmph->type = ICMP_DEST_UNREACH;
371 icmph->code = ICMP_FRAG_NEEDED;
372 icmph->un.gateway = htonl(mtu);
375 nskb->csum = csum_partial((u8 *)icmph, sizeof(struct icmphdr), 0);
376 nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_iph - skb->data,
377 payload, payload_length,
379 icmph->checksum = csum_fold(nskb->csum);
382 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
384 ipv6_should_icmp(struct sk_buff *skb)
386 struct ipv6hdr *old_ipv6h = ipv6_hdr(skb);
388 int payload_off = (u8 *)(old_ipv6h + 1) - skb->data;
389 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
391 /* Check source address is valid. */
392 addr_type = ipv6_addr_type(&old_ipv6h->saddr);
393 if (addr_type & IPV6_ADDR_MULTICAST || addr_type == IPV6_ADDR_ANY)
396 /* Don't reply to unspecified addresses. */
397 if (ipv6_addr_type(&old_ipv6h->daddr) == IPV6_ADDR_ANY)
400 /* Don't respond to ICMP error messages. */
401 payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr);
405 if (nexthdr == NEXTHDR_ICMP) {
406 u8 icmp_type, *icmp_typep;
408 icmp_typep = skb_header_pointer(skb, payload_off +
409 offsetof(struct icmp6hdr,
411 sizeof(icmp_type), &icmp_type);
413 if (!icmp_typep || !(*icmp_typep & ICMPV6_INFOMSG_MASK))
421 ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb, unsigned int mtu,
422 unsigned int payload_length)
424 struct ipv6hdr *ipv6h, *old_ipv6h = ipv6_hdr(skb);
425 struct icmp6hdr *icmp6h;
428 ipv6h = (struct ipv6hdr *)skb_put(nskb, sizeof(struct ipv6hdr));
429 icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
430 payload = skb_put(nskb, payload_length);
435 memset(&ipv6h->flow_lbl, 0, sizeof(ipv6h->flow_lbl));
436 ipv6h->payload_len = htons(sizeof(struct icmp6hdr)
438 ipv6h->nexthdr = NEXTHDR_ICMP;
439 ipv6h->hop_limit = IPV6_DEFAULT_HOPLIMIT;
440 ipv6_addr_copy(&ipv6h->daddr, &old_ipv6h->saddr);
441 ipv6_addr_copy(&ipv6h->saddr, &old_ipv6h->daddr);
444 icmp6h->icmp6_type = ICMPV6_PKT_TOOBIG;
445 icmp6h->icmp6_code = 0;
446 icmp6h->icmp6_cksum = 0;
447 icmp6h->icmp6_mtu = htonl(mtu);
449 nskb->csum = csum_partial((u8 *)icmp6h, sizeof(struct icmp6hdr), 0);
450 nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_ipv6h - skb->data,
451 payload, payload_length,
453 icmp6h->icmp6_cksum = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
454 sizeof(struct icmp6hdr)
456 ipv6h->nexthdr, nskb->csum);
461 send_frag_needed(struct vport *vport, const struct mutable_config *mutable,
462 struct sk_buff *skb, unsigned int mtu, __be32 flow_key)
464 unsigned int eth_hdr_len = ETH_HLEN;
465 unsigned int total_length = 0, header_length = 0, payload_length;
466 struct ethhdr *eh, *old_eh = eth_hdr(skb);
467 struct sk_buff *nskb;
470 if (skb->protocol == htons(ETH_P_IP)) {
471 if (mtu < IP_MIN_MTU)
474 if (!ipv4_should_icmp(skb))
477 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
478 else if (skb->protocol == htons(ETH_P_IPV6)) {
479 if (mtu < IPV6_MIN_MTU)
482 /* In theory we should do PMTUD on IPv6 multicast messages but
483 * we don't have an address to send from so just fragment. */
484 if (ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST)
487 if (!ipv6_should_icmp(skb))
495 if (old_eh->h_proto == htons(ETH_P_8021Q))
496 eth_hdr_len = VLAN_ETH_HLEN;
498 payload_length = skb->len - eth_hdr_len;
499 if (skb->protocol == htons(ETH_P_IP)) {
500 header_length = sizeof(struct iphdr) + sizeof(struct icmphdr);
501 total_length = min_t(unsigned int, header_length +
502 payload_length, 576);
504 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
506 header_length = sizeof(struct ipv6hdr) +
507 sizeof(struct icmp6hdr);
508 total_length = min_t(unsigned int, header_length +
509 payload_length, IPV6_MIN_MTU);
513 total_length = min(total_length, mutable->mtu);
514 payload_length = total_length - header_length;
516 nskb = dev_alloc_skb(NET_IP_ALIGN + eth_hdr_len + header_length +
521 skb_reserve(nskb, NET_IP_ALIGN);
523 /* Ethernet / VLAN */
524 eh = (struct ethhdr *)skb_put(nskb, eth_hdr_len);
525 memcpy(eh->h_dest, old_eh->h_source, ETH_ALEN);
526 memcpy(eh->h_source, mutable->eth_addr, ETH_ALEN);
527 nskb->protocol = eh->h_proto = old_eh->h_proto;
528 if (old_eh->h_proto == htons(ETH_P_8021Q)) {
529 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)eh;
531 vh->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
532 vh->h_vlan_encapsulated_proto = skb->protocol;
534 skb_reset_mac_header(nskb);
537 if (skb->protocol == htons(ETH_P_IP))
538 ipv4_build_icmp(skb, nskb, mtu, payload_length);
539 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
541 ipv6_build_icmp(skb, nskb, mtu, payload_length);
544 /* Assume that flow based keys are symmetric with respect to input
545 * and output and use the key that we were going to put on the
546 * outgoing packet for the fake received packet. If the keys are
547 * not symmetric then PMTUD needs to be disabled since we won't have
548 * any way of synthesizing packets. */
549 if (mutable->port_config.flags & GRE_F_IN_KEY_MATCH &&
550 mutable->port_config.flags & GRE_F_OUT_KEY_ACTION)
551 OVS_CB(nskb)->tun_id = flow_key;
553 compute_ip_summed(nskb, false);
554 vport_receive(vport, nskb);
559 static struct sk_buff *
560 check_headroom(struct sk_buff *skb, int headroom)
562 if (skb_headroom(skb) < headroom ||
563 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
564 struct sk_buff *nskb = skb_realloc_headroom(skb, headroom);
567 return ERR_PTR(-ENOMEM);
570 set_skb_csum_bits(skb, nskb);
573 skb_set_owner_w(nskb, skb->sk);
583 create_gre_header(struct sk_buff *skb, const struct mutable_config *mutable)
585 struct iphdr *iph = ip_hdr(skb);
586 __be16 *flags = (__be16 *)(iph + 1);
587 __be16 *protocol = flags + 1;
588 __be32 *options = (__be32 *)((u8 *)iph + mutable->tunnel_hlen
589 - GRE_HEADER_SECTION);
591 *protocol = htons(ETH_P_TEB);
594 /* Work backwards over the options so the checksum is last. */
595 if (mutable->port_config.out_key ||
596 mutable->port_config.flags & GRE_F_OUT_KEY_ACTION) {
599 if (mutable->port_config.flags & GRE_F_OUT_KEY_ACTION)
600 *options = OVS_CB(skb)->tun_id;
602 *options = mutable->port_config.out_key;
607 if (mutable->port_config.flags & GRE_F_OUT_CSUM) {
611 *(__sum16 *)options = csum_fold(skb_checksum(skb,
612 sizeof(struct iphdr),
613 skb->len - sizeof(struct iphdr),
619 check_checksum(struct sk_buff *skb)
621 struct iphdr *iph = ip_hdr(skb);
622 __be16 flags = *(__be16 *)(iph + 1);
625 if (flags & GRE_CSUM) {
626 switch (skb->ip_summed) {
627 case CHECKSUM_COMPLETE:
628 csum = csum_fold(skb->csum);
636 csum = __skb_checksum_complete(skb);
637 skb->ip_summed = CHECKSUM_COMPLETE;
646 parse_gre_header(struct iphdr *iph, __be16 *flags, __be32 *key)
648 /* IP and ICMP protocol handlers check that the IHL is valid. */
649 __be16 *flagsp = (__be16 *)((u8 *)iph + (iph->ihl << 2));
650 __be16 *protocol = flagsp + 1;
651 __be32 *options = (__be32 *)(protocol + 1);
656 if (*flags & (GRE_VERSION | GRE_ROUTING))
659 if (*protocol != htons(ETH_P_TEB))
662 hdr_len = GRE_HEADER_SECTION;
664 if (*flags & GRE_CSUM) {
665 hdr_len += GRE_HEADER_SECTION;
669 if (*flags & GRE_KEY) {
670 hdr_len += GRE_HEADER_SECTION;
677 if (*flags & GRE_SEQ)
678 hdr_len += GRE_HEADER_SECTION;
684 ecn_encapsulate(u8 tos, struct sk_buff *skb)
688 if (skb->protocol == htons(ETH_P_IP))
689 inner = ((struct iphdr *)skb_network_header(skb))->tos;
690 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
691 else if (skb->protocol == htons(ETH_P_IPV6))
692 inner = ipv6_get_dsfield((struct ipv6hdr *)skb_network_header(skb));
697 return INET_ECN_encapsulate(tos, inner);
701 ecn_decapsulate(u8 tos, struct sk_buff *skb)
703 if (INET_ECN_is_ce(tos)) {
704 __be16 protocol = skb->protocol;
705 unsigned int nw_header = skb_network_header(skb) - skb->data;
707 if (skb->protocol == htons(ETH_P_8021Q)) {
708 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
711 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
712 nw_header += VLAN_HLEN;
715 if (protocol == htons(ETH_P_IP)) {
716 if (unlikely(!pskb_may_pull(skb, nw_header
717 + sizeof(struct iphdr))))
720 IP_ECN_set_ce((struct iphdr *)(nw_header + skb->data));
722 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
723 else if (protocol == htons(ETH_P_IPV6)) {
724 if (unlikely(!pskb_may_pull(skb, nw_header
725 + sizeof(struct ipv6hdr))))
728 IP6_ECN_set_ce((struct ipv6hdr *)(nw_header
735 static struct sk_buff *
736 handle_gso(struct sk_buff *skb)
738 if (skb_is_gso(skb)) {
739 struct sk_buff *nskb = skb_gso_segment(skb, NETIF_F_SG);
749 handle_csum_offload(struct sk_buff *skb)
751 if (skb->ip_summed == CHECKSUM_PARTIAL)
752 return skb_checksum_help(skb);
754 skb->ip_summed = CHECKSUM_NONE;
759 /* Called with rcu_read_lock. */
761 gre_err(struct sk_buff *skb, u32 info)
764 const struct mutable_config *mutable;
765 const int type = icmp_hdr(skb)->type;
766 const int code = icmp_hdr(skb)->code;
767 int mtu = ntohs(icmp_hdr(skb)->un.frag.mtu);
772 int tunnel_hdr_len, tot_hdr_len;
773 unsigned int orig_mac_header;
774 unsigned int orig_nw_header;
776 if (type != ICMP_DEST_UNREACH || code != ICMP_FRAG_NEEDED)
779 /* The mimimum size packet that we would actually be able to process:
780 * encapsulating IP header, minimum GRE header, Ethernet header,
781 * inner IPv4 header. */
782 if (!pskb_may_pull(skb, sizeof(struct iphdr) + GRE_HEADER_SECTION +
783 ETH_HLEN + sizeof(struct iphdr)))
786 iph = (struct iphdr *)skb->data;
788 tunnel_hdr_len = parse_gre_header(iph, &flags, &key);
789 if (tunnel_hdr_len < 0)
792 vport = find_port(iph->saddr, iph->daddr, key, FIND_PORT_ANY, &mutable);
796 /* Packets received by this function were previously sent by us, so
797 * any comparisons should be to the output values, not the input.
798 * However, it's not really worth it to have a hash table based on
799 * output keys (especially since ICMP error handling of tunneled packets
800 * isn't that reliable anyways). Therefore, we do a lookup based on the
801 * out key as if it were the in key and then check to see if the input
802 * and output keys are the same. */
803 if (mutable->port_config.in_key != mutable->port_config.out_key)
806 if (!!(mutable->port_config.flags & GRE_F_IN_KEY_MATCH) !=
807 !!(mutable->port_config.flags & GRE_F_OUT_KEY_ACTION))
810 if ((mutable->port_config.flags & GRE_F_OUT_CSUM) && !(flags & GRE_CSUM))
813 tunnel_hdr_len += iph->ihl << 2;
815 orig_mac_header = skb_mac_header(skb) - skb->data;
816 orig_nw_header = skb_network_header(skb) - skb->data;
817 skb_set_mac_header(skb, tunnel_hdr_len);
819 tot_hdr_len = tunnel_hdr_len + ETH_HLEN;
821 skb->protocol = eth_hdr(skb)->h_proto;
822 if (skb->protocol == htons(ETH_P_8021Q)) {
823 tot_hdr_len += VLAN_HLEN;
824 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
827 skb_set_network_header(skb, tot_hdr_len);
830 if (skb->protocol == htons(ETH_P_IP))
831 tot_hdr_len += sizeof(struct iphdr);
832 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
833 else if (skb->protocol == htons(ETH_P_IPV6))
834 tot_hdr_len += sizeof(struct ipv6hdr);
839 if (!pskb_may_pull(skb, tot_hdr_len))
842 if (skb->protocol == htons(ETH_P_IP)) {
843 if (mtu < IP_MIN_MTU) {
844 if (ntohs(ip_hdr(skb)->tot_len) >= IP_MIN_MTU)
851 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
852 else if (skb->protocol == htons(ETH_P_IPV6)) {
853 if (mtu < IPV6_MIN_MTU) {
854 unsigned int packet_length = sizeof(struct ipv6hdr) +
855 ntohs(ipv6_hdr(skb)->payload_len);
857 if (packet_length >= IPV6_MIN_MTU
858 || ntohs(ipv6_hdr(skb)->payload_len) == 0)
866 __pskb_pull(skb, tunnel_hdr_len);
867 send_frag_needed(vport, mutable, skb, mtu, key);
868 skb_push(skb, tunnel_hdr_len);
871 skb_set_mac_header(skb, orig_mac_header);
872 skb_set_network_header(skb, orig_nw_header);
873 skb->protocol = htons(ETH_P_IP);
876 /* Called with rcu_read_lock. */
878 gre_rcv(struct sk_buff *skb)
881 const struct mutable_config *mutable;
887 if (!pskb_may_pull(skb, GRE_HEADER_SECTION + ETH_HLEN))
890 if (!check_checksum(skb))
895 hdr_len = parse_gre_header(iph, &flags, &key);
899 vport = find_port(iph->daddr, iph->saddr, key, FIND_PORT_ANY, &mutable);
901 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
905 if ((mutable->port_config.flags & GRE_F_IN_CSUM) && !(flags & GRE_CSUM)) {
906 vport_record_error(vport, VPORT_E_RX_CRC);
910 if (!pskb_pull(skb, hdr_len) || !pskb_may_pull(skb, ETH_HLEN)) {
911 vport_record_error(vport, VPORT_E_RX_ERROR);
915 skb->pkt_type = PACKET_HOST;
916 skb->protocol = eth_type_trans(skb, skb->dev);
917 skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
922 skb_reset_network_header(skb);
924 ecn_decapsulate(iph->tos, skb);
926 if (mutable->port_config.flags & GRE_F_IN_KEY_MATCH)
927 OVS_CB(skb)->tun_id = key;
929 OVS_CB(skb)->tun_id = 0;
931 skb_push(skb, ETH_HLEN);
932 compute_ip_summed(skb, false);
934 vport_receive(vport, skb);
944 build_packet(struct vport *vport, const struct mutable_config *mutable,
945 struct iphdr *iph, struct rtable *rt, int max_headroom, int mtu,
949 struct iphdr *new_iph;
950 int orig_len = skb->len;
951 __be16 frag_off = iph->frag_off;
953 skb = check_headroom(skb, max_headroom);
954 if (unlikely(IS_ERR(skb)))
957 err = handle_csum_offload(skb);
961 if (skb->protocol == htons(ETH_P_IP)) {
962 struct iphdr *old_iph = ip_hdr(skb);
964 if ((old_iph->frag_off & htons(IP_DF)) &&
965 mtu < ntohs(old_iph->tot_len)) {
966 if (send_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
971 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
972 else if (skb->protocol == htons(ETH_P_IPV6)) {
973 unsigned int packet_length = skb->len - ETH_HLEN
974 - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
976 /* IPv6 requires PMTUD if the packet is above the minimum MTU. */
977 if (packet_length > IPV6_MIN_MTU)
978 frag_off = htons(IP_DF);
980 if (mtu < packet_length) {
981 if (send_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
987 skb_reset_transport_header(skb);
988 new_iph = (struct iphdr *)skb_push(skb, mutable->tunnel_hlen);
989 skb_reset_network_header(skb);
991 memcpy(new_iph, iph, sizeof(struct iphdr));
992 new_iph->frag_off = frag_off;
993 ip_select_ident(new_iph, &rt->u.dst, NULL);
995 create_gre_header(skb, mutable);
997 /* Allow our local IP stack to fragment the outer packet even if the
998 * DF bit is set as a last resort. */
1001 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1002 IPCB(skb)->flags = 0;
1004 err = ip_local_out(skb);
1005 if (likely(net_xmit_eval(err) == 0))
1008 vport_record_error(vport, VPORT_E_TX_ERROR);
1015 vport_record_error(vport, VPORT_E_TX_DROPPED);
1021 gre_send(struct vport *vport, struct sk_buff *skb)
1023 struct gre_vport *gre_vport = gre_vport_priv(vport);
1024 const struct mutable_config *mutable = rcu_dereference(gre_vport->mutable);
1026 struct iphdr *old_iph;
1033 /* Validate the protocol headers before we try to use them. */
1034 if (skb->protocol == htons(ETH_P_8021Q)) {
1035 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
1038 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1039 skb_set_network_header(skb, VLAN_ETH_HLEN);
1042 if (skb->protocol == htons(ETH_P_IP)) {
1043 if (unlikely(!pskb_may_pull(skb, skb_network_header(skb)
1044 + sizeof(struct iphdr) - skb->data)))
1047 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1048 else if (skb->protocol == htons(ETH_P_IPV6)) {
1049 if (unlikely(!pskb_may_pull(skb, skb_network_header(skb)
1050 + sizeof(struct ipv6hdr) - skb->data)))
1054 old_iph = ip_hdr(skb);
1056 iph.tos = mutable->port_config.tos;
1057 if (mutable->port_config.flags & GRE_F_TOS_INHERIT) {
1058 if (skb->protocol == htons(ETH_P_IP))
1059 iph.tos = old_iph->tos;
1060 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1061 else if (skb->protocol == htons(ETH_P_IPV6))
1062 iph.tos = ipv6_get_dsfield(ipv6_hdr(skb));
1065 iph.tos = ecn_encapsulate(iph.tos, skb);
1068 struct flowi fl = { .nl_u = { .ip4_u =
1069 { .daddr = mutable->port_config.daddr,
1070 .saddr = mutable->port_config.saddr,
1071 .tos = RT_TOS(iph.tos) } },
1072 .proto = IPPROTO_GRE };
1074 if (ip_route_output_key(&init_net, &rt, &fl))
1078 iph.ttl = mutable->port_config.ttl;
1079 if (mutable->port_config.flags & GRE_F_TTL_INHERIT) {
1080 if (skb->protocol == htons(ETH_P_IP))
1081 iph.ttl = old_iph->ttl;
1082 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1083 else if (skb->protocol == htons(ETH_P_IPV6))
1084 iph.ttl = ipv6_hdr(skb)->hop_limit;
1088 iph.ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
1090 iph.frag_off = (mutable->port_config.flags & GRE_F_PMTUD) ? htons(IP_DF) : 0;
1092 mtu = dst_mtu(&rt->u.dst)
1094 - mutable->tunnel_hlen
1095 - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
1099 if (skb->protocol == htons(ETH_P_IP)) {
1100 iph.frag_off |= old_iph->frag_off & htons(IP_DF);
1101 mtu = max(mtu, IP_MIN_MTU);
1103 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1104 else if (skb->protocol == htons(ETH_P_IPV6))
1105 mtu = max(mtu, IPV6_MIN_MTU);
1109 iph.ihl = sizeof(struct iphdr) >> 2;
1110 iph.protocol = IPPROTO_GRE;
1111 iph.daddr = rt->rt_dst;
1112 iph.saddr = rt->rt_src;
1117 skb_dst_set(skb, &rt->u.dst);
1119 /* If we are doing GSO on a pskb it is better to make sure that the
1120 * headroom is correct now. We will only have to copy the portion in
1121 * the linear data area and GSO will preserve headroom when it creates
1122 * the segments. This is particularly beneficial on Xen where we get
1123 * lots of GSO pskbs. Conversely, we delay copying if it is just to
1124 * get our own writable clone because GSO may do the copy for us. */
1125 max_headroom = LL_RESERVED_SPACE(rt->u.dst.dev) + rt->u.dst.header_len
1126 + mutable->tunnel_hlen;
1128 if (skb_headroom(skb) < max_headroom) {
1129 skb = check_headroom(skb, max_headroom);
1130 if (unlikely(IS_ERR(skb))) {
1131 vport_record_error(vport, VPORT_E_TX_DROPPED);
1136 forward_ip_summed(skb);
1137 vswitch_skb_checksum_setup(skb);
1139 skb = handle_gso(skb);
1140 if (unlikely(IS_ERR(skb))) {
1141 vport_record_error(vport, VPORT_E_TX_DROPPED);
1145 /* Process GSO segments. Try to do any work for the entire packet that
1146 * doesn't involve actually writing to it before this point. */
1149 struct sk_buff *next_skb = skb->next;
1152 orig_len += build_packet(vport, mutable, &iph, rt, max_headroom, mtu, skb);
1161 vport_record_error(vport, VPORT_E_TX_ERROR);
1166 static struct net_protocol gre_protocol_handlers = {
1168 .err_handler = gre_err,
1176 err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
1178 printk(KERN_WARNING "openvswitch: cannot register gre protocol handler\n");
1186 tbl_destroy(port_table, NULL);
1187 inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
1191 set_config(const struct vport *cur_vport, struct mutable_config *mutable,
1192 const void __user *uconfig)
1194 const struct vport *old_vport;
1195 const struct mutable_config *old_mutable;
1198 if (copy_from_user(&mutable->port_config, uconfig, sizeof(struct gre_port_config)))
1201 if (mutable->port_config.daddr == 0)
1204 if (mutable->port_config.flags & GRE_F_IN_KEY_MATCH) {
1205 port_type = FIND_PORT_MATCH;
1206 mutable->port_config.in_key = 0;
1208 port_type = FIND_PORT_KEY;
1210 old_vport = find_port(mutable->port_config.saddr,
1211 mutable->port_config.daddr,
1212 mutable->port_config.in_key, port_type,
1215 if (old_vport && old_vport != cur_vport)
1218 if (mutable->port_config.flags & GRE_F_OUT_KEY_ACTION)
1219 mutable->port_config.out_key = 0;
1221 mutable->tunnel_hlen = sizeof(struct iphdr) + GRE_HEADER_SECTION;
1223 if (mutable->port_config.flags & GRE_F_OUT_CSUM)
1224 mutable->tunnel_hlen += GRE_HEADER_SECTION;
1226 if (mutable->port_config.out_key ||
1227 mutable->port_config.flags & GRE_F_OUT_KEY_ACTION)
1228 mutable->tunnel_hlen += GRE_HEADER_SECTION;
1233 static struct vport *
1234 gre_create(const char *name, const void __user *config)
1236 struct vport *vport;
1237 struct gre_vport *gre_vport;
1240 vport = vport_alloc(sizeof(struct gre_vport), &gre_vport_ops);
1241 if (IS_ERR(vport)) {
1242 err = PTR_ERR(vport);
1246 gre_vport = gre_vport_priv(vport);
1248 strcpy(gre_vport->name, name);
1250 gre_vport->mutable = kmalloc(sizeof(struct mutable_config), GFP_KERNEL);
1251 if (!gre_vport->mutable) {
1253 goto error_free_vport;
1256 vport_gen_rand_ether_addr(gre_vport->mutable->eth_addr);
1257 gre_vport->mutable->mtu = ETH_DATA_LEN;
1259 err = set_config(NULL, gre_vport->mutable, config);
1261 goto error_free_mutable;
1263 err = add_port(vport);
1265 goto error_free_mutable;
1270 kfree(gre_vport->mutable);
1274 return ERR_PTR(err);
1278 gre_modify(struct vport *vport, const void __user *config)
1280 struct gre_vport *gre_vport = gre_vport_priv(vport);
1281 struct mutable_config *mutable;
1283 int update_hash = 0;
1285 mutable = kmemdup(gre_vport->mutable, sizeof(struct mutable_config), GFP_KERNEL);
1291 err = set_config(vport, mutable, config);
1295 /* Only remove the port from the hash table if something that would
1296 * affect the lookup has changed. */
1297 if (gre_vport->mutable->port_config.saddr != mutable->port_config.saddr ||
1298 gre_vport->mutable->port_config.daddr != mutable->port_config.daddr ||
1299 gre_vport->mutable->port_config.in_key != mutable->port_config.in_key ||
1300 (gre_vport->mutable->port_config.flags & GRE_F_IN_KEY_MATCH) !=
1301 (mutable->port_config.flags & GRE_F_IN_KEY_MATCH))
1305 /* This update is not atomic but the lookup uses the config, which
1306 * serves as an inherent double check. */
1308 err = del_port(vport);
1313 assign_config_rcu(vport, mutable);
1316 err = add_port(vport);
1330 gre_destroy(struct vport *vport)
1332 struct gre_vport *gre_vport = gre_vport_priv(vport);
1334 const struct mutable_config *old_mutable;
1336 /* Do a hash table lookup to make sure that the port exists. It should
1337 * exist but might not if a modify failed earlier. */
1338 if (gre_vport->mutable->port_config.flags & GRE_F_IN_KEY_MATCH)
1339 port_type = FIND_PORT_MATCH;
1341 port_type = FIND_PORT_KEY;
1343 if (vport == find_port(gre_vport->mutable->port_config.saddr,
1344 gre_vport->mutable->port_config.daddr,
1345 gre_vport->mutable->port_config.in_key, port_type, &old_mutable))
1348 kfree(gre_vport->mutable);
1355 gre_set_mtu(struct vport *vport, int mtu)
1357 struct gre_vport *gre_vport = gre_vport_priv(vport);
1358 struct mutable_config *mutable;
1360 mutable = kmemdup(gre_vport->mutable, sizeof(struct mutable_config), GFP_KERNEL);
1365 assign_config_rcu(vport, mutable);
1371 gre_set_addr(struct vport *vport, const unsigned char *addr)
1373 struct gre_vport *gre_vport = gre_vport_priv(vport);
1374 struct mutable_config *mutable;
1376 mutable = kmemdup(gre_vport->mutable, sizeof(struct mutable_config), GFP_KERNEL);
1380 memcpy(mutable->eth_addr, addr, ETH_ALEN);
1381 assign_config_rcu(vport, mutable);
1388 gre_get_name(const struct vport *vport)
1390 const struct gre_vport *gre_vport = gre_vport_priv(vport);
1391 return gre_vport->name;
1394 static const unsigned char *
1395 gre_get_addr(const struct vport *vport)
1397 const struct gre_vport *gre_vport = gre_vport_priv(vport);
1398 return rcu_dereference(gre_vport->mutable)->eth_addr;
1402 gre_get_mtu(const struct vport *vport)
1404 const struct gre_vport *gre_vport = gre_vport_priv(vport);
1405 return rcu_dereference(gre_vport->mutable)->mtu;
1408 struct vport_ops gre_vport_ops = {
1410 .flags = VPORT_F_GEN_STATS | VPORT_F_TUN_ID,
1413 .create = gre_create,
1414 .modify = gre_modify,
1415 .destroy = gre_destroy,
1416 .set_mtu = gre_set_mtu,
1417 .set_addr = gre_set_addr,
1418 .get_name = gre_get_name,
1419 .get_addr = gre_get_addr,
1420 .get_dev_flags = vport_gen_get_dev_flags,
1421 .is_running = vport_gen_is_running,
1422 .get_operstate = vport_gen_get_operstate,
1423 .get_mtu = gre_get_mtu,