1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
9 #include <netinet/in.h>
10 #include <netinet/ip.h>
11 #include <netinet/ip6.h>
16 ipip_outbound(struct rte_mbuf *m, uint32_t offset, uint32_t is_ipv6,
17 struct ip_addr *src, struct ip_addr *dst)
19 struct ip *inip4, *outip4;
20 struct ip6_hdr *inip6, *outip6;
23 inip4 = rte_pktmbuf_mtod(m, struct ip *);
25 RTE_ASSERT(inip4->ip_v == IPVERSION || inip4->ip_v == IP6_VERSION);
27 if (inip4->ip_v == IPVERSION) {
28 /* XXX This should be done by the forwarding engine instead */
30 ds_ecn = inip4->ip_tos;
32 inip6 = (struct ip6_hdr *)inip4;
33 /* XXX This should be done by the forwarding engine instead */
35 ds_ecn = ntohl(inip6->ip6_flow) >> 20;
39 offset += sizeof(struct ip6_hdr);
40 outip6 = (struct ip6_hdr *)rte_pktmbuf_prepend(m, offset);
42 RTE_ASSERT(outip6 != NULL);
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));
49 outip6->ip6_nxt = IPPROTO_ESP;
50 outip6->ip6_hops = IPDEFTTL;
52 memcpy(&outip6->ip6_src.s6_addr, src, 16);
53 memcpy(&outip6->ip6_dst.s6_addr, dst, 16);
58 offset += sizeof(struct ip);
59 outip4 = (struct ip *)rte_pktmbuf_prepend(m, offset);
61 RTE_ASSERT(outip4 != NULL);
63 /* Per RFC4301 5.1.2.1 */
64 outip4->ip_v = IPVERSION;
66 outip4->ip_tos = ds_ecn;
67 outip4->ip_len = htons(rte_pktmbuf_data_len(m));
72 outip4->ip_ttl = IPDEFTTL;
73 outip4->ip_p = IPPROTO_ESP;
75 outip4->ip_src.s_addr = src->ip.ip4;
76 outip4->ip_dst.s_addr = dst->ip.ip4;
81 static inline struct ip *
82 ip4ip_outbound(struct rte_mbuf *m, uint32_t offset,
83 struct ip_addr *src, struct ip_addr *dst)
85 return ipip_outbound(m, offset, 0, src, dst);
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)
92 return ipip_outbound(m, offset, 1, src, dst);
96 ip4_ecn_setup(struct ip *ip4)
98 if (ip4->ip_tos & IPTOS_ECN_MASK)
99 ip4->ip_tos |= IPTOS_ECN_CE;
103 ip6_ecn_setup(struct ip6_hdr *ip6)
105 if ((ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK)
106 ip6->ip6_flow = htonl(ntohl(ip6->ip6_flow) |
107 (IPTOS_ECN_CE << 20));
111 ipip_inbound(struct rte_mbuf *m, uint32_t offset)
113 struct ip *inip4, *outip4;
114 struct ip6_hdr *inip6, *outip6;
115 uint32_t ip_len, set_ecn;
117 outip4 = rte_pktmbuf_mtod(m, struct ip*);
119 RTE_ASSERT(outip4->ip_v == IPVERSION || outip4->ip_v == IP6_VERSION);
121 if (outip4->ip_v == IPVERSION) {
122 ip_len = sizeof(struct ip);
123 set_ecn = ((outip4->ip_tos & IPTOS_ECN_CE) == IPTOS_ECN_CE);
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);
131 inip4 = (struct ip *)rte_pktmbuf_adj(m, offset + ip_len);
132 RTE_ASSERT(inip4->ip_v == IPVERSION || inip4->ip_v == IP6_VERSION);
134 /* Check packet is still bigger than IP header (inner) */
135 RTE_ASSERT(rte_pktmbuf_pkt_len(m) > ip_len);
137 /* RFC4301 5.1.2.1 Note 6 */
138 if (inip4->ip_v == IPVERSION) {
140 ip4_ecn_setup(inip4);
141 /* XXX This should be done by the forwarding engine instead */
144 inip6 = (struct ip6_hdr *)inip4;
146 ip6_ecn_setup(inip6);
147 /* XXX This should be done by the forwarding engine instead */
148 inip6->ip6_hops -= 1;
152 #endif /* __IPIP_H__ */