8ff7cd79f7a121b897bce75dab9946a3384e0de4
[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 #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,
41                       __be16 df, bool xnet)
42 {
43         int pkt_len = skb->len;
44         struct iphdr *iph;
45         int err;
46
47         nf_reset(skb);
48         secpath_reset(skb);
49         skb_clear_hash(skb);
50         skb_dst_drop(skb);
51         skb_dst_set(skb, &rt_dst(rt));
52 #if 0
53         /* Do not clear ovs_skb_cb.  It will be done in gso code. */
54         memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
55 #endif
56
57         /* Push down and install the IP header. */
58         __skb_push(skb, sizeof(struct iphdr));
59         skb_reset_network_header(skb);
60
61         iph = ip_hdr(skb);
62
63         iph->version    =       4;
64         iph->ihl        =       sizeof(struct iphdr) >> 2;
65         iph->frag_off   =       df;
66         iph->protocol   =       proto;
67         iph->tos        =       tos;
68         iph->daddr      =       dst;
69         iph->saddr      =       src;
70         iph->ttl        =       ttl;
71
72 #ifdef HAVE_IP_SELECT_IDENT_USING_DST_ENTRY
73         __ip_select_ident(iph, &rt_dst(rt), (skb_shinfo(skb)->gso_segs ?: 1) - 1);
74 #else
75         __ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1);
76 #endif
77
78         err = ip_local_out(skb);
79         if (unlikely(net_xmit_eval(err)))
80                 pkt_len = 0;
81         return pkt_len;
82 }
83 EXPORT_SYMBOL_GPL(rpl_iptunnel_xmit);
84
85 struct sk_buff *ovs_iptunnel_handle_offloads(struct sk_buff *skb,
86                                              bool csum_help, int gso_type_mask,
87                                              void (*fix_segment)(struct sk_buff *))
88 {
89         int err;
90
91         if (likely(!skb_is_encapsulated(skb))) {
92                 skb_reset_inner_headers(skb);
93 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
94                 skb->encapsulation = 1;
95 #endif
96         } else if (skb_is_gso(skb)) {
97                 err = -ENOSYS;
98                 goto error;
99         }
100
101         if (gso_type_mask)
102                 fix_segment = NULL;
103
104         OVS_GSO_CB(skb)->fix_segment = fix_segment;
105
106         if (skb_is_gso(skb)) {
107                 err = skb_unclone(skb, GFP_ATOMIC);
108                 if (unlikely(err))
109                         goto error;
110                 skb_shinfo(skb)->gso_type |= gso_type_mask;
111                 return skb;
112         }
113
114 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
115         /* If packet is not gso and we are resolving any partial checksum,
116          * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL
117          * on the outer header without confusing devices that implement
118          * NETIF_F_IP_CSUM with encapsulation.
119          */
120         if (csum_help)
121                 skb->encapsulation = 0;
122 #endif
123
124         if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
125                 err = skb_checksum_help(skb);
126                 if (unlikely(err))
127                         goto error;
128         } else if (skb->ip_summed != CHECKSUM_PARTIAL)
129                 skb->ip_summed = CHECKSUM_NONE;
130
131         return skb;
132 error:
133         kfree_skb(skb);
134         return ERR_PTR(err);
135 }
136 EXPORT_SYMBOL_GPL(ovs_iptunnel_handle_offloads);
137
138 int rpl_iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
139 {
140         if (unlikely(!pskb_may_pull(skb, hdr_len)))
141                 return -ENOMEM;
142
143         skb_pull_rcsum(skb, hdr_len);
144
145         if (inner_proto == htons(ETH_P_TEB)) {
146                 struct ethhdr *eh;
147
148                 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
149                         return -ENOMEM;
150
151                 eh = (struct ethhdr *)skb->data;
152
153                 if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN))
154                         skb->protocol = eh->h_proto;
155                 else
156                         skb->protocol = htons(ETH_P_802_2);
157
158         } else {
159                 skb->protocol = inner_proto;
160         }
161
162         nf_reset(skb);
163         secpath_reset(skb);
164         skb_clear_hash(skb);
165         skb_dst_drop(skb);
166         vlan_set_tci(skb, 0);
167         skb_set_queue_mapping(skb, 0);
168         skb->pkt_type = PACKET_HOST;
169         return 0;
170 }
171 EXPORT_SYMBOL_GPL(rpl_iptunnel_pull_header);
172
173 #endif
174
175 bool ovs_skb_is_encapsulated(struct sk_buff *skb)
176 {
177         /* checking for inner protocol should be sufficient on newer kernel, but
178          * old kernel just set encapsulation bit.
179          */
180         return ovs_skb_get_inner_protocol(skb) || skb_encapsulation(skb);
181 }
182 EXPORT_SYMBOL_GPL(ovs_skb_is_encapsulated);