datapath:compat: Fix build failure on old kernels.
[cascardo/ovs.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
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/in.h>
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>
33 #include <net/xfrm.h>
34
35 #include "compat.h"
36 #include "gso.h"
37
38 #ifndef USE_KERNEL_TUNNEL_API
39 int iptunnel_xmit(struct sock *sk, struct rtable *rt,
40                   struct sk_buff *skb,
41                   __be32 src, __be32 dst, __u8 proto,
42                   __u8 tos, __u8 ttl, __be16 df, bool xnet)
43 {
44         int pkt_len = skb->len;
45         struct iphdr *iph;
46         int err;
47
48         nf_reset(skb);
49         secpath_reset(skb);
50         skb_clear_hash(skb);
51         skb_dst_drop(skb);
52         skb_dst_set(skb, &rt_dst(rt));
53 #if 0
54         /* Do not clear ovs_skb_cb.  It will be done in gso code. */
55         memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
56 #endif
57
58         /* Push down and install the IP header. */
59         __skb_push(skb, sizeof(struct iphdr));
60         skb_reset_network_header(skb);
61
62         iph = ip_hdr(skb);
63
64         iph->version    =       4;
65         iph->ihl        =       sizeof(struct iphdr) >> 2;
66         iph->frag_off   =       df;
67         iph->protocol   =       proto;
68         iph->tos        =       tos;
69         iph->daddr      =       dst;
70         iph->saddr      =       src;
71         iph->ttl        =       ttl;
72
73 #ifdef HAVE_IP_SELECT_IDENT_USING_DST_ENTRY
74         __ip_select_ident(iph, &rt_dst(rt), (skb_shinfo(skb)->gso_segs ?: 1) - 1);
75 #else
76         __ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1);
77 #endif
78
79         err = ip_local_out(skb);
80         if (unlikely(net_xmit_eval(err)))
81                 pkt_len = 0;
82         return pkt_len;
83 }
84
85 int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
86 {
87         if (unlikely(!pskb_may_pull(skb, hdr_len)))
88                 return -ENOMEM;
89
90         skb_pull_rcsum(skb, hdr_len);
91
92         if (inner_proto == htons(ETH_P_TEB)) {
93                 struct ethhdr *eh;
94
95                 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
96                         return -ENOMEM;
97
98                 eh = (struct ethhdr *)skb->data;
99
100                 if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN))
101                         skb->protocol = eh->h_proto;
102                 else
103                         skb->protocol = htons(ETH_P_802_2);
104
105         } else {
106                 skb->protocol = inner_proto;
107         }
108
109         nf_reset(skb);
110         secpath_reset(skb);
111         skb_clear_hash(skb);
112         skb_dst_drop(skb);
113         vlan_set_tci(skb, 0);
114         skb_set_queue_mapping(skb, 0);
115         skb->pkt_type = PACKET_HOST;
116         return 0;
117 }
118
119 #endif
120
121 bool skb_is_encapsulated(struct sk_buff *skb)
122 {
123         /* checking for inner protocol should be sufficient on newer kernel, but
124          * old kernel just set encapsulation bit.
125          */
126         /* XXX: set inner protocol for all tunnel in OVS. */
127         return ovs_skb_get_inner_protocol(skb) || skb_encapsulation(skb);
128 }