2 * Copyright (c) 2007-2013 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
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/in_route.h>
24 #include <linux/inetdevice.h>
25 #include <linux/jhash.h>
26 #include <linux/list.h>
27 #include <linux/kernel.h>
28 #include <linux/version.h>
29 #include <linux/workqueue.h>
30 #include <linux/rculist.h>
31 #include <net/ip_tunnels.h>
32 #include <net/route.h>
38 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
39 int rpl_iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
40 __be32 src, __be32 dst, __u8 proto, __u8 tos, __u8 ttl,
43 int pkt_len = skb->len;
51 skb_dst_set(skb, &rt_dst(rt));
53 /* Do not clear ovs_skb_cb. It will be done in gso code. */
54 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
57 /* Push down and install the IP header. */
58 __skb_push(skb, sizeof(struct iphdr));
59 skb_reset_network_header(skb);
64 iph->ihl = sizeof(struct iphdr) >> 2;
66 iph->protocol = proto;
72 #ifdef HAVE_IP_SELECT_IDENT_USING_DST_ENTRY
73 __ip_select_ident(iph, &rt_dst(rt), (skb_shinfo(skb)->gso_segs ?: 1) - 1);
75 __ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1);
78 err = ip_local_out(skb);
79 if (unlikely(net_xmit_eval(err)))
84 struct sk_buff *ovs_iptunnel_handle_offloads(struct sk_buff *skb,
85 bool csum_help, int gso_type_mask,
86 void (*fix_segment)(struct sk_buff *))
90 if (likely(!skb_is_encapsulated(skb))) {
91 skb_reset_inner_headers(skb);
92 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
93 skb->encapsulation = 1;
95 } else if (skb_is_gso(skb)) {
103 OVS_GSO_CB(skb)->fix_segment = fix_segment;
105 if (skb_is_gso(skb)) {
106 err = skb_unclone(skb, GFP_ATOMIC);
109 skb_shinfo(skb)->gso_type |= gso_type_mask;
113 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
114 /* If packet is not gso and we are resolving any partial checksum,
115 * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL
116 * on the outer header without confusing devices that implement
117 * NETIF_F_IP_CSUM with encapsulation.
120 skb->encapsulation = 0;
123 if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
124 err = skb_checksum_help(skb);
127 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
128 skb->ip_summed = CHECKSUM_NONE;
136 int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
138 if (unlikely(!pskb_may_pull(skb, hdr_len)))
141 skb_pull_rcsum(skb, hdr_len);
143 if (inner_proto == htons(ETH_P_TEB)) {
146 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
149 eh = (struct ethhdr *)skb->data;
151 if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN))
152 skb->protocol = eh->h_proto;
154 skb->protocol = htons(ETH_P_802_2);
157 skb->protocol = inner_proto;
164 vlan_set_tci(skb, 0);
165 skb_set_queue_mapping(skb, 0);
166 skb->pkt_type = PACKET_HOST;
172 bool skb_is_encapsulated(struct sk_buff *skb)
174 /* checking for inner protocol should be sufficient on newer kernel, but
175 * old kernel just set encapsulation bit.
177 /* XXX: set inner protocol for all tunnel in OVS. */
178 return ovs_skb_get_inner_protocol(skb) || skb_encapsulation(skb);