examples/ipsec-secgw: update mbuf packet type
[dpdk.git] / examples / ipsec-secgw / ipip.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4
5 #ifndef __IPIP_H__
6 #define __IPIP_H__
7
8 #include <stdint.h>
9 #include <netinet/in.h>
10 #include <netinet/ip.h>
11 #include <netinet/ip6.h>
12
13 #include <rte_mbuf.h>
14
15 static inline void *
16 ipip_outbound(struct rte_mbuf *m, uint32_t offset, uint32_t is_ipv6,
17                 struct ip_addr *src,  struct ip_addr *dst)
18 {
19         struct ip *inip4, *outip4;
20         struct ip6_hdr *inip6, *outip6;
21         uint8_t ds_ecn;
22
23         inip4 = rte_pktmbuf_mtod(m, struct ip *);
24
25         RTE_ASSERT(inip4->ip_v == IPVERSION || inip4->ip_v == IP6_VERSION);
26
27         if (inip4->ip_v == IPVERSION) {
28                 /* XXX This should be done by the forwarding engine instead */
29                 inip4->ip_ttl -= 1;
30                 ds_ecn = inip4->ip_tos;
31         } else {
32                 inip6 = (struct ip6_hdr *)inip4;
33                 /* XXX This should be done by the forwarding engine instead */
34                 inip6->ip6_hops -= 1;
35                 ds_ecn = ntohl(inip6->ip6_flow) >> 20;
36         }
37
38         if (is_ipv6) {
39                 offset += sizeof(struct ip6_hdr);
40                 outip6 = (struct ip6_hdr *)rte_pktmbuf_prepend(m, offset);
41
42                 RTE_ASSERT(outip6 != NULL);
43
44                 /* Per RFC4301 5.1.2.1 */
45                 outip6->ip6_flow = htonl(IP6_VERSION << 28 | ds_ecn << 20);
46                 outip6->ip6_plen = htons(rte_pktmbuf_data_len(m) -
47                                          sizeof(struct ip6_hdr));
48
49                 outip6->ip6_nxt = IPPROTO_ESP;
50                 outip6->ip6_hops = IPDEFTTL;
51
52                 memcpy(&outip6->ip6_src.s6_addr, src, 16);
53                 memcpy(&outip6->ip6_dst.s6_addr, dst, 16);
54
55                 return outip6;
56         }
57
58         offset += sizeof(struct ip);
59         outip4 = (struct ip *)rte_pktmbuf_prepend(m, offset);
60
61         RTE_ASSERT(outip4 != NULL);
62
63         /* Per RFC4301 5.1.2.1 */
64         outip4->ip_v = IPVERSION;
65         outip4->ip_hl = 5;
66         outip4->ip_tos = ds_ecn;
67         outip4->ip_len = htons(rte_pktmbuf_data_len(m));
68
69         outip4->ip_id = 0;
70         outip4->ip_off = 0;
71
72         outip4->ip_ttl = IPDEFTTL;
73         outip4->ip_p = IPPROTO_ESP;
74
75         outip4->ip_src.s_addr = src->ip.ip4;
76         outip4->ip_dst.s_addr = dst->ip.ip4;
77         m->packet_type &= ~RTE_PTYPE_L4_MASK;
78         return outip4;
79 }
80
81 static inline struct ip *
82 ip4ip_outbound(struct rte_mbuf *m, uint32_t offset,
83                 struct ip_addr *src,  struct ip_addr *dst)
84 {
85         return ipip_outbound(m, offset, 0, src, dst);
86 }
87
88 static inline struct ip6_hdr *
89 ip6ip_outbound(struct rte_mbuf *m, uint32_t offset,
90                 struct ip_addr *src,  struct ip_addr *dst)
91 {
92         return ipip_outbound(m, offset, 1, src, dst);
93 }
94
95 static inline void
96 ip4_ecn_setup(struct ip *ip4)
97 {
98         if (ip4->ip_tos & IPTOS_ECN_MASK)
99                 ip4->ip_tos |= IPTOS_ECN_CE;
100 }
101
102 static inline void
103 ip6_ecn_setup(struct ip6_hdr *ip6)
104 {
105         if ((ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK)
106                 ip6->ip6_flow = htonl(ntohl(ip6->ip6_flow) |
107                                         (IPTOS_ECN_CE << 20));
108 }
109
110 static inline void
111 ipip_inbound(struct rte_mbuf *m, uint32_t offset)
112 {
113         struct ip *inip4, *outip4;
114         struct ip6_hdr *inip6, *outip6;
115         uint32_t ip_len, set_ecn;
116
117         outip4 = rte_pktmbuf_mtod(m, struct ip*);
118
119         RTE_ASSERT(outip4->ip_v == IPVERSION || outip4->ip_v == IP6_VERSION);
120
121         if (outip4->ip_v == IPVERSION) {
122                 ip_len = sizeof(struct ip);
123                 set_ecn = ((outip4->ip_tos & IPTOS_ECN_CE) == IPTOS_ECN_CE);
124         } else {
125                 outip6 = (struct ip6_hdr *)outip4;
126                 ip_len = sizeof(struct ip6_hdr);
127                 set_ecn = ntohl(outip6->ip6_flow) >> 20;
128                 set_ecn = ((set_ecn & IPTOS_ECN_CE) == IPTOS_ECN_CE);
129         }
130
131         inip4 = (struct ip *)rte_pktmbuf_adj(m, offset + ip_len);
132         RTE_ASSERT(inip4->ip_v == IPVERSION || inip4->ip_v == IP6_VERSION);
133
134         /* Check packet is still bigger than IP header (inner) */
135         RTE_ASSERT(rte_pktmbuf_pkt_len(m) > ip_len);
136
137         /* RFC4301 5.1.2.1 Note 6 */
138         if (inip4->ip_v == IPVERSION) {
139                 if (set_ecn)
140                         ip4_ecn_setup(inip4);
141                 /* XXX This should be done by the forwarding engine instead */
142                 inip4->ip_ttl -= 1;
143                 m->packet_type &= ~RTE_PTYPE_L4_MASK;
144                 if (inip4->ip_p == IPPROTO_UDP)
145                         m->packet_type |= RTE_PTYPE_L4_UDP;
146                 else if (inip4->ip_p == IPPROTO_TCP)
147                         m->packet_type |= RTE_PTYPE_L4_TCP;
148         } else {
149                 inip6 = (struct ip6_hdr *)inip4;
150                 if (set_ecn)
151                         ip6_ecn_setup(inip6);
152                 /* XXX This should be done by the forwarding engine instead */
153                 inip6->ip6_hops -= 1;
154         }
155 }
156
157 #endif /* __IPIP_H__ */