datapath: Add support for kernel 3.14.
[sliver-openvswitch.git] / datapath / linux / compat / ip_tunnels_core.c
1 /*
2  * Copyright (c) 2007-2013 Nicira, Inc.
3  *
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.
7  *
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.
12  *
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
16  * 02110-1301, USA
17  */
18
19 #include <linux/version.h>
20 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,12,0)
21
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24 #include <linux/in.h>
25 #include <linux/in_route.h>
26 #include <linux/inetdevice.h>
27 #include <linux/jhash.h>
28 #include <linux/list.h>
29 #include <linux/kernel.h>
30 #include <linux/version.h>
31 #include <linux/workqueue.h>
32 #include <linux/rculist.h>
33 #include <net/ip_tunnels.h>
34 #include <net/route.h>
35 #include <net/xfrm.h>
36
37 #include "compat.h"
38 #include "gso.h"
39
40 int iptunnel_xmit(struct rtable *rt,
41                   struct sk_buff *skb,
42                   __be32 src, __be32 dst, __u8 proto,
43                   __u8 tos, __u8 ttl, __be16 df, bool xnet)
44 {
45         int pkt_len = skb->len;
46         struct iphdr *iph;
47         int err;
48
49         nf_reset(skb);
50         secpath_reset(skb);
51         skb_clear_hash(skb);
52         skb_dst_drop(skb);
53         skb_dst_set(skb, &rt_dst(rt));
54 #if 0
55         /* Do not clear ovs_skb_cb.  It will be done in gso code. */
56         memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
57 #endif
58
59         /* Push down and install the IP header. */
60         __skb_push(skb, sizeof(struct iphdr));
61         skb_reset_network_header(skb);
62
63         iph = ip_hdr(skb);
64
65         iph->version    =       4;
66         iph->ihl        =       sizeof(struct iphdr) >> 2;
67         iph->frag_off   =       df;
68         iph->protocol   =       proto;
69         iph->tos        =       tos;
70         iph->daddr      =       dst;
71         iph->saddr      =       src;
72         iph->ttl        =       ttl;
73         __ip_select_ident(iph, &rt_dst(rt), (skb_shinfo(skb)->gso_segs ?: 1) - 1);
74
75         err = ip_local_out(skb);
76         if (unlikely(net_xmit_eval(err)))
77                 pkt_len = 0;
78         return pkt_len;
79 }
80
81 int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
82 {
83         if (unlikely(!pskb_may_pull(skb, hdr_len)))
84                 return -ENOMEM;
85
86         skb_pull_rcsum(skb, hdr_len);
87
88         if (inner_proto == htons(ETH_P_TEB)) {
89                 struct ethhdr *eh;
90
91                 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
92                         return -ENOMEM;
93
94                 eh = (struct ethhdr *)skb->data;
95
96                 if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN))
97                         skb->protocol = eh->h_proto;
98                 else
99                         skb->protocol = htons(ETH_P_802_2);
100
101         } else {
102                 skb->protocol = inner_proto;
103         }
104
105         nf_reset(skb);
106         secpath_reset(skb);
107         skb_clear_hash(skb);
108         skb_dst_drop(skb);
109         vlan_set_tci(skb, 0);
110         skb_set_queue_mapping(skb, 0);
111         skb->pkt_type = PACKET_HOST;
112         return 0;
113 }
114
115 #endif /* 3.12 */