]> git.proxmox.com Git - ovs.git/blob - datapath/linux/compat/ip_tunnels_core.c
compat: Fixups for newer kernels
[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/if_vlan.h>
23 #include <linux/in.h>
24 #include <linux/in_route.h>
25 #include <linux/inetdevice.h>
26 #include <linux/jhash.h>
27 #include <linux/list.h>
28 #include <linux/kernel.h>
29 #include <linux/version.h>
30 #include <linux/workqueue.h>
31 #include <linux/rculist.h>
32 #include <net/ip_tunnels.h>
33 #include <net/ip6_tunnel.h>
34 #include <net/route.h>
35 #include <net/xfrm.h>
36
37 #include "compat.h"
38 #include "gso.h"
39 #include "vport-netdev.h"
40
41 #ifndef USE_UPSTREAM_TUNNEL
42 void rpl_iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
43 __be32 src, __be32 dst, __u8 proto, __u8 tos, __u8 ttl,
44 __be16 df, bool xnet)
45 {
46 struct net_device *dev = skb->dev;
47 int pkt_len = skb->len - skb_inner_network_offset(skb);
48 struct iphdr *iph;
49 int err;
50
51 skb_scrub_packet(skb, xnet);
52
53 skb_clear_hash(skb);
54 skb_dst_set(skb, &rt->dst);
55
56 #if 0
57 /* Do not clear ovs_skb_cb. It will be done in gso code. */
58 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
59 #endif
60
61 /* Push down and install the IP header. */
62 __skb_push(skb, sizeof(struct iphdr));
63 skb_reset_network_header(skb);
64
65 iph = ip_hdr(skb);
66
67 iph->version = 4;
68 iph->ihl = sizeof(struct iphdr) >> 2;
69 iph->frag_off = df;
70 iph->protocol = proto;
71 iph->tos = tos;
72 iph->daddr = dst;
73 iph->saddr = src;
74 iph->ttl = ttl;
75
76 #ifdef HAVE_IP_SELECT_IDENT_USING_DST_ENTRY
77 __ip_select_ident(iph, &rt->dst, (skb_shinfo(skb)->gso_segs ?: 1) - 1);
78 #elif defined(HAVE_IP_SELECT_IDENT_USING_NET)
79 __ip_select_ident(dev_net(rt->dst.dev), iph,
80 skb_shinfo(skb)->gso_segs ?: 1);
81 #else
82 __ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1);
83 #endif
84
85 err = ip_local_out(dev_net(rt->dst.dev), sk, skb);
86 if (unlikely(net_xmit_eval(err)))
87 pkt_len = 0;
88 iptunnel_xmit_stats(dev, pkt_len);
89 }
90 EXPORT_SYMBOL_GPL(rpl_iptunnel_xmit);
91
92 int ovs_iptunnel_handle_offloads(struct sk_buff *skb,
93 int gso_type_mask,
94 void (*fix_segment)(struct sk_buff *))
95 {
96 int err;
97
98 if (likely(!skb_is_encapsulated(skb))) {
99 skb_reset_inner_headers(skb);
100 skb->encapsulation = 1;
101 } else if (skb_is_gso(skb)) {
102 err = -ENOSYS;
103 goto error;
104 }
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
112 #ifndef USE_UPSTREAM_TUNNEL_GSO
113 if (gso_type_mask)
114 fix_segment = NULL;
115
116 OVS_GSO_CB(skb)->fix_segment = fix_segment;
117 #endif
118 return 0;
119 }
120
121 if (skb->ip_summed != CHECKSUM_PARTIAL) {
122 skb->ip_summed = CHECKSUM_NONE;
123 skb->encapsulation = 0;
124 }
125
126 return 0;
127 error:
128 return err;
129 }
130 EXPORT_SYMBOL_GPL(ovs_iptunnel_handle_offloads);
131
132
133 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
134 struct sk_buff *rpl_iptunnel_handle_offloads(struct sk_buff *skb,
135 bool csum_help,
136 int gso_type_mask)
137 #else
138 int rpl_iptunnel_handle_offloads(struct sk_buff *skb,
139 bool csum_help,
140 int gso_type_mask)
141 #endif
142 {
143 int err;
144
145 if (likely(!skb->encapsulation)) {
146 skb_reset_inner_headers(skb);
147 skb->encapsulation = 1;
148 }
149
150 if (skb_is_gso(skb)) {
151 err = skb_unclone(skb, GFP_ATOMIC);
152 if (unlikely(err))
153 goto error;
154 skb_shinfo(skb)->gso_type |= gso_type_mask;
155 goto out;
156 }
157
158 /* If packet is not gso and we are resolving any partial checksum,
159 * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL
160 * on the outer header without confusing devices that implement
161 * NETIF_F_IP_CSUM with encapsulation.
162 */
163 if (csum_help)
164 skb->encapsulation = 0;
165
166 if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
167 err = skb_checksum_help(skb);
168 if (unlikely(err))
169 goto error;
170 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
171 skb->ip_summed = CHECKSUM_NONE;
172
173 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
174 out:
175 return skb;
176 error:
177 kfree_skb(skb);
178 return ERR_PTR(err);
179 #else
180 out:
181 error:
182 return 0;
183 #endif
184 }
185 EXPORT_SYMBOL_GPL(rpl_iptunnel_handle_offloads);
186
187 int rpl___iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
188 __be16 inner_proto, bool raw_proto, bool xnet)
189 {
190 if (unlikely(!pskb_may_pull(skb, hdr_len)))
191 return -ENOMEM;
192
193 skb_pull_rcsum(skb, hdr_len);
194
195 if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
196 struct ethhdr *eh;
197
198 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
199 return -ENOMEM;
200
201 eh = (struct ethhdr *)skb->data;
202 if (likely(eth_proto_is_802_3(eh->h_proto)))
203 skb->protocol = eh->h_proto;
204 else
205 skb->protocol = htons(ETH_P_802_2);
206
207 } else {
208 skb->protocol = inner_proto;
209 }
210
211 skb_clear_hash_if_not_l4(skb);
212 skb->vlan_tci = 0;
213 skb_set_queue_mapping(skb, 0);
214 skb_scrub_packet(skb, xnet);
215
216 return iptunnel_pull_offloads(skb);
217 }
218 EXPORT_SYMBOL_GPL(rpl___iptunnel_pull_header);
219 #endif /* USE_UPSTREAM_TUNNEL */
220
221 bool ovs_skb_is_encapsulated(struct sk_buff *skb)
222 {
223 /* checking for inner protocol should be sufficient on newer kernel, but
224 * old kernel just set encapsulation bit.
225 */
226 return ovs_skb_get_inner_protocol(skb) || skb->encapsulation;
227 }
228 EXPORT_SYMBOL_GPL(ovs_skb_is_encapsulated);
229
230 /* derived from ip_tunnel_rcv(). */
231 void ovs_ip_tunnel_rcv(struct net_device *dev, struct sk_buff *skb,
232 struct metadata_dst *tun_dst)
233 {
234 struct pcpu_sw_netstats *tstats;
235
236 tstats = this_cpu_ptr((struct pcpu_sw_netstats __percpu *)dev->tstats);
237 u64_stats_update_begin(&tstats->syncp);
238 tstats->rx_packets++;
239 tstats->rx_bytes += skb->len;
240 u64_stats_update_end(&tstats->syncp);
241
242 skb_reset_mac_header(skb);
243 skb_scrub_packet(skb, false);
244 skb->protocol = eth_type_trans(skb, dev);
245 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
246
247 ovs_skb_dst_set(skb, (struct dst_entry *)tun_dst);
248
249 #ifndef USE_UPSTREAM_TUNNEL
250 netdev_port_receive(skb, &tun_dst->u.tun_info);
251 #else
252 netif_rx(skb);
253 #endif
254 }
255
256 #ifndef HAVE_PCPU_SW_NETSTATS
257 #define netdev_stats_to_stats64 rpl_netdev_stats_to_stats64
258 static void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
259 const struct net_device_stats *netdev_stats)
260 {
261 #if BITS_PER_LONG == 64
262 BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
263 memcpy(stats64, netdev_stats, sizeof(*stats64));
264 #else
265 size_t i, n = sizeof(*stats64) / sizeof(u64);
266 const unsigned long *src = (const unsigned long *)netdev_stats;
267 u64 *dst = (u64 *)stats64;
268
269 BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
270 sizeof(*stats64) / sizeof(u64));
271 for (i = 0; i < n; i++)
272 dst[i] = src[i];
273 #endif
274 }
275 #endif
276
277 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,11,0)
278 struct rtnl_link_stats64 *rpl_ip_tunnel_get_stats64(struct net_device *dev,
279 struct rtnl_link_stats64 *tot)
280 #else
281 void rpl_ip_tunnel_get_stats64(struct net_device *dev,
282 struct rtnl_link_stats64 *tot)
283 #endif
284 {
285 int i;
286
287 netdev_stats_to_stats64(tot, &dev->stats);
288
289 for_each_possible_cpu(i) {
290 const struct pcpu_sw_netstats *tstats =
291 per_cpu_ptr((struct pcpu_sw_netstats __percpu *)dev->tstats, i);
292 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
293 unsigned int start;
294
295 do {
296 start = u64_stats_fetch_begin_irq(&tstats->syncp);
297 rx_packets = tstats->rx_packets;
298 tx_packets = tstats->tx_packets;
299 rx_bytes = tstats->rx_bytes;
300 tx_bytes = tstats->tx_bytes;
301 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
302
303 tot->rx_packets += rx_packets;
304 tot->tx_packets += tx_packets;
305 tot->rx_bytes += rx_bytes;
306 tot->tx_bytes += tx_bytes;
307 }
308
309 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,11,0)
310 return tot;
311 #endif
312 }
313
314 void rpl_ip6tunnel_xmit(struct sock *sk, struct sk_buff *skb,
315 struct net_device *dev)
316 {
317 int pkt_len, err;
318
319 pkt_len = skb->len - skb_inner_network_offset(skb);
320 #ifdef HAVE_IP6_LOCAL_OUT_SK
321 err = ip6_local_out_sk(sk, skb);
322 #else
323 err = ip6_local_out(dev_net(skb_dst(skb)->dev), sk, skb);
324 #endif
325 if (net_xmit_eval(err))
326 pkt_len = -1;
327
328 iptunnel_xmit_stats(dev, pkt_len);
329 }
330 EXPORT_SYMBOL_GPL(rpl_ip6tunnel_xmit);