13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson * Copyright(c) 2016 Intel Corporation
3d299106eSSergio Gonzalez Monroy */
4d299106eSSergio Gonzalez Monroy
5d299106eSSergio Gonzalez Monroy #ifndef __IPIP_H__
6d299106eSSergio Gonzalez Monroy #define __IPIP_H__
7d299106eSSergio Gonzalez Monroy
8d299106eSSergio Gonzalez Monroy #include <stdint.h>
9d299106eSSergio Gonzalez Monroy #include <netinet/in.h>
10d299106eSSergio Gonzalez Monroy #include <netinet/ip.h>
11906257e9SSergio Gonzalez Monroy #include <netinet/ip6.h>
12d299106eSSergio Gonzalez Monroy
13d299106eSSergio Gonzalez Monroy #include <rte_mbuf.h>
14d299106eSSergio Gonzalez Monroy
15906257e9SSergio Gonzalez Monroy static inline void *
ipip_outbound(struct rte_mbuf * m,uint32_t offset,uint32_t is_ipv6,struct ip_addr * src,struct ip_addr * dst)16906257e9SSergio Gonzalez Monroy ipip_outbound(struct rte_mbuf *m, uint32_t offset, uint32_t is_ipv6,
17906257e9SSergio Gonzalez Monroy struct ip_addr *src, struct ip_addr *dst)
18d299106eSSergio Gonzalez Monroy {
19906257e9SSergio Gonzalez Monroy struct ip *inip4, *outip4;
20906257e9SSergio Gonzalez Monroy struct ip6_hdr *inip6, *outip6;
21906257e9SSergio Gonzalez Monroy uint8_t ds_ecn;
22d299106eSSergio Gonzalez Monroy
23906257e9SSergio Gonzalez Monroy inip4 = rte_pktmbuf_mtod(m, struct ip *);
24d299106eSSergio Gonzalez Monroy
25906257e9SSergio Gonzalez Monroy RTE_ASSERT(inip4->ip_v == IPVERSION || inip4->ip_v == IP6_VERSION);
26d299106eSSergio Gonzalez Monroy
27906257e9SSergio Gonzalez Monroy if (inip4->ip_v == IPVERSION) {
28906257e9SSergio Gonzalez Monroy /* XXX This should be done by the forwarding engine instead */
29906257e9SSergio Gonzalez Monroy inip4->ip_ttl -= 1;
30*27d822e8SAkhil Goyal if (inip4->ip_sum >= rte_cpu_to_be_16(0xffff - 0x100))
31*27d822e8SAkhil Goyal inip4->ip_sum += rte_cpu_to_be_16(0x100) + 1;
32*27d822e8SAkhil Goyal else
33*27d822e8SAkhil Goyal inip4->ip_sum += rte_cpu_to_be_16(0x100);
34906257e9SSergio Gonzalez Monroy ds_ecn = inip4->ip_tos;
35906257e9SSergio Gonzalez Monroy } else {
36906257e9SSergio Gonzalez Monroy inip6 = (struct ip6_hdr *)inip4;
37906257e9SSergio Gonzalez Monroy /* XXX This should be done by the forwarding engine instead */
38906257e9SSergio Gonzalez Monroy inip6->ip6_hops -= 1;
39906257e9SSergio Gonzalez Monroy ds_ecn = ntohl(inip6->ip6_flow) >> 20;
40d299106eSSergio Gonzalez Monroy }
41d299106eSSergio Gonzalez Monroy
42906257e9SSergio Gonzalez Monroy if (is_ipv6) {
43906257e9SSergio Gonzalez Monroy offset += sizeof(struct ip6_hdr);
44906257e9SSergio Gonzalez Monroy outip6 = (struct ip6_hdr *)rte_pktmbuf_prepend(m, offset);
45d299106eSSergio Gonzalez Monroy
46906257e9SSergio Gonzalez Monroy RTE_ASSERT(outip6 != NULL);
47d299106eSSergio Gonzalez Monroy
48906257e9SSergio Gonzalez Monroy /* Per RFC4301 5.1.2.1 */
49906257e9SSergio Gonzalez Monroy outip6->ip6_flow = htonl(IP6_VERSION << 28 | ds_ecn << 20);
50b43a8131STomasz Duszynski outip6->ip6_plen = htons(rte_pktmbuf_data_len(m) -
51b43a8131STomasz Duszynski sizeof(struct ip6_hdr));
52906257e9SSergio Gonzalez Monroy
53906257e9SSergio Gonzalez Monroy outip6->ip6_nxt = IPPROTO_ESP;
54906257e9SSergio Gonzalez Monroy outip6->ip6_hops = IPDEFTTL;
55906257e9SSergio Gonzalez Monroy
56906257e9SSergio Gonzalez Monroy memcpy(&outip6->ip6_src.s6_addr, src, 16);
57906257e9SSergio Gonzalez Monroy memcpy(&outip6->ip6_dst.s6_addr, dst, 16);
58906257e9SSergio Gonzalez Monroy
59906257e9SSergio Gonzalez Monroy return outip6;
60906257e9SSergio Gonzalez Monroy }
61d299106eSSergio Gonzalez Monroy
62d299106eSSergio Gonzalez Monroy offset += sizeof(struct ip);
63906257e9SSergio Gonzalez Monroy outip4 = (struct ip *)rte_pktmbuf_prepend(m, offset);
64906257e9SSergio Gonzalez Monroy
65906257e9SSergio Gonzalez Monroy RTE_ASSERT(outip4 != NULL);
66906257e9SSergio Gonzalez Monroy
67906257e9SSergio Gonzalez Monroy /* Per RFC4301 5.1.2.1 */
68906257e9SSergio Gonzalez Monroy outip4->ip_v = IPVERSION;
69906257e9SSergio Gonzalez Monroy outip4->ip_hl = 5;
70906257e9SSergio Gonzalez Monroy outip4->ip_tos = ds_ecn;
71906257e9SSergio Gonzalez Monroy outip4->ip_len = htons(rte_pktmbuf_data_len(m));
72906257e9SSergio Gonzalez Monroy
73906257e9SSergio Gonzalez Monroy outip4->ip_id = 0;
74906257e9SSergio Gonzalez Monroy outip4->ip_off = 0;
75906257e9SSergio Gonzalez Monroy
76906257e9SSergio Gonzalez Monroy outip4->ip_ttl = IPDEFTTL;
77906257e9SSergio Gonzalez Monroy outip4->ip_p = IPPROTO_ESP;
78906257e9SSergio Gonzalez Monroy
7996362fadSSergio Gonzalez Monroy outip4->ip_src.s_addr = src->ip.ip4;
8096362fadSSergio Gonzalez Monroy outip4->ip_dst.s_addr = dst->ip.ip4;
819ebafd1eSAkhil Goyal m->packet_type &= ~RTE_PTYPE_L4_MASK;
82906257e9SSergio Gonzalez Monroy return outip4;
83906257e9SSergio Gonzalez Monroy }
84906257e9SSergio Gonzalez Monroy
85906257e9SSergio Gonzalez Monroy static inline struct ip *
ip4ip_outbound(struct rte_mbuf * m,uint32_t offset,struct ip_addr * src,struct ip_addr * dst)86906257e9SSergio Gonzalez Monroy ip4ip_outbound(struct rte_mbuf *m, uint32_t offset,
87906257e9SSergio Gonzalez Monroy struct ip_addr *src, struct ip_addr *dst)
88906257e9SSergio Gonzalez Monroy {
89906257e9SSergio Gonzalez Monroy return ipip_outbound(m, offset, 0, src, dst);
90906257e9SSergio Gonzalez Monroy }
91906257e9SSergio Gonzalez Monroy
92906257e9SSergio Gonzalez Monroy static inline struct ip6_hdr *
ip6ip_outbound(struct rte_mbuf * m,uint32_t offset,struct ip_addr * src,struct ip_addr * dst)93906257e9SSergio Gonzalez Monroy ip6ip_outbound(struct rte_mbuf *m, uint32_t offset,
94906257e9SSergio Gonzalez Monroy struct ip_addr *src, struct ip_addr *dst)
95906257e9SSergio Gonzalez Monroy {
96906257e9SSergio Gonzalez Monroy return ipip_outbound(m, offset, 1, src, dst);
97906257e9SSergio Gonzalez Monroy }
98906257e9SSergio Gonzalez Monroy
99906257e9SSergio Gonzalez Monroy static inline void
ip4_ecn_setup(struct ip * ip4)100906257e9SSergio Gonzalez Monroy ip4_ecn_setup(struct ip *ip4)
101906257e9SSergio Gonzalez Monroy {
102*27d822e8SAkhil Goyal if (ip4->ip_tos & IPTOS_ECN_MASK) {
103*27d822e8SAkhil Goyal unsigned long sum;
104*27d822e8SAkhil Goyal uint8_t old;
105*27d822e8SAkhil Goyal
106*27d822e8SAkhil Goyal old = ip4->ip_tos;
107906257e9SSergio Gonzalez Monroy ip4->ip_tos |= IPTOS_ECN_CE;
108*27d822e8SAkhil Goyal sum = old + (~(*(uint8_t *)&ip4->ip_tos) & 0xff);
109*27d822e8SAkhil Goyal sum += rte_be_to_cpu_16(ip4->ip_sum);
110*27d822e8SAkhil Goyal sum = (sum & 0xffff) + (sum >> 16);
111*27d822e8SAkhil Goyal ip4->ip_sum = rte_cpu_to_be_16(sum + (sum >> 16));
112*27d822e8SAkhil Goyal }
113906257e9SSergio Gonzalez Monroy }
114906257e9SSergio Gonzalez Monroy
115906257e9SSergio Gonzalez Monroy static inline void
ip6_ecn_setup(struct ip6_hdr * ip6)116906257e9SSergio Gonzalez Monroy ip6_ecn_setup(struct ip6_hdr *ip6)
117906257e9SSergio Gonzalez Monroy {
118906257e9SSergio Gonzalez Monroy if ((ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK)
119906257e9SSergio Gonzalez Monroy ip6->ip6_flow = htonl(ntohl(ip6->ip6_flow) |
120906257e9SSergio Gonzalez Monroy (IPTOS_ECN_CE << 20));
121906257e9SSergio Gonzalez Monroy }
122906257e9SSergio Gonzalez Monroy
123906257e9SSergio Gonzalez Monroy static inline void
ipip_inbound(struct rte_mbuf * m,uint32_t offset)124906257e9SSergio Gonzalez Monroy ipip_inbound(struct rte_mbuf *m, uint32_t offset)
125906257e9SSergio Gonzalez Monroy {
126906257e9SSergio Gonzalez Monroy struct ip *inip4, *outip4;
127906257e9SSergio Gonzalez Monroy struct ip6_hdr *inip6, *outip6;
128906257e9SSergio Gonzalez Monroy uint32_t ip_len, set_ecn;
129906257e9SSergio Gonzalez Monroy
130906257e9SSergio Gonzalez Monroy outip4 = rte_pktmbuf_mtod(m, struct ip*);
131906257e9SSergio Gonzalez Monroy
132906257e9SSergio Gonzalez Monroy RTE_ASSERT(outip4->ip_v == IPVERSION || outip4->ip_v == IP6_VERSION);
133906257e9SSergio Gonzalez Monroy
134906257e9SSergio Gonzalez Monroy if (outip4->ip_v == IPVERSION) {
135906257e9SSergio Gonzalez Monroy ip_len = sizeof(struct ip);
136906257e9SSergio Gonzalez Monroy set_ecn = ((outip4->ip_tos & IPTOS_ECN_CE) == IPTOS_ECN_CE);
137906257e9SSergio Gonzalez Monroy } else {
138906257e9SSergio Gonzalez Monroy outip6 = (struct ip6_hdr *)outip4;
139906257e9SSergio Gonzalez Monroy ip_len = sizeof(struct ip6_hdr);
140906257e9SSergio Gonzalez Monroy set_ecn = ntohl(outip6->ip6_flow) >> 20;
141906257e9SSergio Gonzalez Monroy set_ecn = ((set_ecn & IPTOS_ECN_CE) == IPTOS_ECN_CE);
142906257e9SSergio Gonzalez Monroy }
143906257e9SSergio Gonzalez Monroy
144906257e9SSergio Gonzalez Monroy inip4 = (struct ip *)rte_pktmbuf_adj(m, offset + ip_len);
145906257e9SSergio Gonzalez Monroy RTE_ASSERT(inip4->ip_v == IPVERSION || inip4->ip_v == IP6_VERSION);
146d299106eSSergio Gonzalez Monroy
147d299106eSSergio Gonzalez Monroy /* Check packet is still bigger than IP header (inner) */
148906257e9SSergio Gonzalez Monroy RTE_ASSERT(rte_pktmbuf_pkt_len(m) > ip_len);
149d299106eSSergio Gonzalez Monroy
150d299106eSSergio Gonzalez Monroy /* RFC4301 5.1.2.1 Note 6 */
151906257e9SSergio Gonzalez Monroy if (inip4->ip_v == IPVERSION) {
152906257e9SSergio Gonzalez Monroy if (set_ecn)
153906257e9SSergio Gonzalez Monroy ip4_ecn_setup(inip4);
154906257e9SSergio Gonzalez Monroy /* XXX This should be done by the forwarding engine instead */
155906257e9SSergio Gonzalez Monroy inip4->ip_ttl -= 1;
156*27d822e8SAkhil Goyal if (inip4->ip_sum >= rte_cpu_to_be_16(0xffff - 0x100))
157*27d822e8SAkhil Goyal inip4->ip_sum += rte_cpu_to_be_16(0x100) + 1;
158*27d822e8SAkhil Goyal else
159*27d822e8SAkhil Goyal inip4->ip_sum += rte_cpu_to_be_16(0x100);
1609ebafd1eSAkhil Goyal m->packet_type &= ~RTE_PTYPE_L4_MASK;
1619ebafd1eSAkhil Goyal if (inip4->ip_p == IPPROTO_UDP)
1629ebafd1eSAkhil Goyal m->packet_type |= RTE_PTYPE_L4_UDP;
1639ebafd1eSAkhil Goyal else if (inip4->ip_p == IPPROTO_TCP)
1649ebafd1eSAkhil Goyal m->packet_type |= RTE_PTYPE_L4_TCP;
165906257e9SSergio Gonzalez Monroy } else {
166906257e9SSergio Gonzalez Monroy inip6 = (struct ip6_hdr *)inip4;
167906257e9SSergio Gonzalez Monroy if (set_ecn)
168906257e9SSergio Gonzalez Monroy ip6_ecn_setup(inip6);
169906257e9SSergio Gonzalez Monroy /* XXX This should be done by the forwarding engine instead */
170906257e9SSergio Gonzalez Monroy inip6->ip6_hops -= 1;
171906257e9SSergio Gonzalez Monroy }
172d299106eSSergio Gonzalez Monroy }
173d299106eSSergio Gonzalez Monroy
174d299106eSSergio Gonzalez Monroy #endif /* __IPIP_H__ */
175