datapath: tunnel: Do not use inner ip-header-id for tunnel ip-header-id.
[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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/in.h>
22 #include <linux/in_route.h>
23 #include <linux/inetdevice.h>
24 #include <linux/jhash.h>
25 #include <linux/list.h>
26 #include <linux/kernel.h>
27 #include <linux/version.h>
28 #include <linux/workqueue.h>
29 #include <linux/rculist.h>
30 #include <net/ip_tunnels.h>
31 #include <net/route.h>
32 #include <net/xfrm.h>
33
34 #include "checksum.h"
35 #include "compat.h"
36 #include "gso.h"
37
38 int iptunnel_xmit(struct net *net, struct rtable *rt,
39                   struct sk_buff *skb,
40                   __be32 src, __be32 dst, __u8 proto,
41                   __u8 tos, __u8 ttl, __be16 df)
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_rxhash(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         __ip_select_ident(iph, &rt_dst(rt), (skb_shinfo(skb)->gso_segs ?: 1) - 1);
72
73         err = ip_local_out(skb);
74         if (unlikely(net_xmit_eval(err)))
75                 pkt_len = 0;
76         return pkt_len;
77 }
78
79 int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
80 {
81         if (unlikely(!pskb_may_pull(skb, hdr_len)))
82                 return -ENOMEM;
83
84         skb_pull_rcsum(skb, hdr_len);
85
86         if (inner_proto == htons(ETH_P_TEB)) {
87                 struct ethhdr *eh;
88
89                 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
90                         return -ENOMEM;
91
92                 eh = (struct ethhdr *)skb->data;
93
94                 if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN))
95                         skb->protocol = eh->h_proto;
96                 else
97                         skb->protocol = htons(ETH_P_802_2);
98
99         } else {
100                 skb->protocol = inner_proto;
101         }
102
103         if (unlikely(compute_ip_summed(skb, false)))
104                 return -EPROTO;
105
106         nf_reset(skb);
107         secpath_reset(skb);
108         skb_clear_rxhash(skb);
109         skb_dst_drop(skb);
110         vlan_set_tci(skb, 0);
111         skb_set_queue_mapping(skb, 0);
112         skb->pkt_type = PACKET_HOST;
113         return 0;
114 }