Imported Upstream version 16.04
[deb_dpdk.git] / examples / ipsec-secgw / ipip.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef __IPIP_H__
35 #define __IPIP_H__
36
37 #include <stdint.h>
38 #include <netinet/in.h>
39 #include <netinet/ip.h>
40
41 #include <rte_mbuf.h>
42
43 #define IPV6_VERSION (6)
44
45 static inline  struct ip *
46 ip4ip_outbound(struct rte_mbuf *m, uint32_t offset, uint32_t src, uint32_t dst)
47 {
48         struct ip *inip, *outip;
49
50         inip = rte_pktmbuf_mtod(m, struct ip*);
51
52         IPSEC_ASSERT(inip->ip_v == IPVERSION || inip->ip_v == IPV6_VERSION);
53
54         offset += sizeof(struct ip);
55
56         outip = (struct ip *)rte_pktmbuf_prepend(m, offset);
57
58         IPSEC_ASSERT(outip != NULL);
59
60         /* Per RFC4301 5.1.2.1 */
61         outip->ip_v = IPVERSION;
62         outip->ip_hl = 5;
63         outip->ip_tos = inip->ip_tos;
64         outip->ip_len = htons(rte_pktmbuf_data_len(m));
65
66         outip->ip_id = 0;
67         outip->ip_off = 0;
68
69         outip->ip_ttl = IPDEFTTL;
70         outip->ip_p = IPPROTO_ESP;
71
72         outip->ip_src.s_addr = src;
73         outip->ip_dst.s_addr = dst;
74
75         return outip;
76 }
77
78 static inline int
79 ip4ip_inbound(struct rte_mbuf *m, uint32_t offset)
80 {
81         struct ip *inip;
82         struct ip *outip;
83
84         outip = rte_pktmbuf_mtod(m, struct ip*);
85
86         IPSEC_ASSERT(outip->ip_v == IPVERSION);
87
88         offset += sizeof(struct ip);
89         inip = (struct ip *)rte_pktmbuf_adj(m, offset);
90         IPSEC_ASSERT(inip->ip_v == IPVERSION || inip->ip_v == IPV6_VERSION);
91
92         /* Check packet is still bigger than IP header (inner) */
93         IPSEC_ASSERT(rte_pktmbuf_pkt_len(m) > sizeof(struct ip));
94
95         /* RFC4301 5.1.2.1 Note 6 */
96         if ((inip->ip_tos & htons(IPTOS_ECN_ECT0 | IPTOS_ECN_ECT1)) &&
97                         ((outip->ip_tos & htons(IPTOS_ECN_CE)) == IPTOS_ECN_CE))
98                 inip->ip_tos |= htons(IPTOS_ECN_CE);
99
100         return 0;
101 }
102
103 #endif /* __IPIP_H__ */