66d5e02523f66356a79d76dad371a5519ba32cbe
[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 "compat.h"
35 #include "gso.h"
36
37 int iptunnel_xmit(struct rtable *rt,
38                   struct sk_buff *skb,
39                   __be32 src, __be32 dst, __u8 proto,
40                   __u8 tos, __u8 ttl, __be16 df)
41 {
42         int pkt_len = skb->len;
43         struct iphdr *iph;
44         int err;
45
46         nf_reset(skb);
47         secpath_reset(skb);
48         skb_clear_rxhash(skb);
49         skb_dst_drop(skb);
50         skb_dst_set(skb, &rt_dst(rt));
51 #if 0
52         /* Do not clear ovs_skb_cb.  It will be done in gso code. */
53         memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
54 #endif
55
56         /* Push down and install the IP header. */
57         __skb_push(skb, sizeof(struct iphdr));
58         skb_reset_network_header(skb);
59
60         iph = ip_hdr(skb);
61
62         iph->version    =       4;
63         iph->ihl        =       sizeof(struct iphdr) >> 2;
64         iph->frag_off   =       df;
65         iph->protocol   =       proto;
66         iph->tos        =       tos;
67         iph->daddr      =       dst;
68         iph->saddr      =       src;
69         iph->ttl        =       ttl;
70         __ip_select_ident(iph, &rt_dst(rt), (skb_shinfo(skb)->gso_segs ?: 1) - 1);
71
72         err = ip_local_out(skb);
73         if (unlikely(net_xmit_eval(err)))
74                 pkt_len = 0;
75         return pkt_len;
76 }
77
78 int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
79 {
80         if (unlikely(!pskb_may_pull(skb, hdr_len)))
81                 return -ENOMEM;
82
83         skb_pull_rcsum(skb, hdr_len);
84
85         if (inner_proto == htons(ETH_P_TEB)) {
86                 struct ethhdr *eh;
87
88                 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
89                         return -ENOMEM;
90
91                 eh = (struct ethhdr *)skb->data;
92
93                 if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN))
94                         skb->protocol = eh->h_proto;
95                 else
96                         skb->protocol = htons(ETH_P_802_2);
97
98         } else {
99                 skb->protocol = inner_proto;
100         }
101
102         nf_reset(skb);
103         secpath_reset(skb);
104         skb_clear_rxhash(skb);
105         skb_dst_drop(skb);
106         vlan_set_tci(skb, 0);
107         skb_set_queue_mapping(skb, 0);
108         skb->pkt_type = PACKET_HOST;
109         return 0;
110 }