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 if (inip4->ip_sum >= rte_cpu_to_be_16(0xffff - 0x100))
31 inip4->ip_sum += rte_cpu_to_be_16(0x100) + 1;
33 inip4->ip_sum += rte_cpu_to_be_16(0x100);
34 ds_ecn = inip4->ip_tos;
36 inip6 = (struct ip6_hdr *)inip4;
37 /* XXX This should be done by the forwarding engine instead */
39 ds_ecn = ntohl(inip6->ip6_flow) >> 20;
43 offset += sizeof(struct ip6_hdr);
44 outip6 = (struct ip6_hdr *)rte_pktmbuf_prepend(m, offset);
46 RTE_ASSERT(outip6 != NULL);
48 /* Per RFC4301 5.1.2.1 */
49 outip6->ip6_flow = htonl(IP6_VERSION << 28 | ds_ecn << 20);
50 outip6->ip6_plen = htons(rte_pktmbuf_data_len(m) -
51 sizeof(struct ip6_hdr));
53 outip6->ip6_nxt = IPPROTO_ESP;
54 outip6->ip6_hops = IPDEFTTL;
56 memcpy(&outip6->ip6_src.s6_addr, src, 16);
57 memcpy(&outip6->ip6_dst.s6_addr, dst, 16);
62 offset += sizeof(struct ip);
63 outip4 = (struct ip *)rte_pktmbuf_prepend(m, offset);
65 RTE_ASSERT(outip4 != NULL);
67 /* Per RFC4301 5.1.2.1 */
68 outip4->ip_v = IPVERSION;
70 outip4->ip_tos = ds_ecn;
71 outip4->ip_len = htons(rte_pktmbuf_data_len(m));
76 outip4->ip_ttl = IPDEFTTL;
77 outip4->ip_p = IPPROTO_ESP;
79 outip4->ip_src.s_addr = src->ip.ip4;
80 outip4->ip_dst.s_addr = dst->ip.ip4;
81 m->packet_type &= ~RTE_PTYPE_L4_MASK;
85 static inline struct ip *
86 ip4ip_outbound(struct rte_mbuf *m, uint32_t offset,
87 struct ip_addr *src, struct ip_addr *dst)
89 return ipip_outbound(m, offset, 0, src, dst);
92 static inline struct ip6_hdr *
93 ip6ip_outbound(struct rte_mbuf *m, uint32_t offset,
94 struct ip_addr *src, struct ip_addr *dst)
96 return ipip_outbound(m, offset, 1, src, dst);
100 ip4_ecn_setup(struct ip *ip4)
102 if (ip4->ip_tos & IPTOS_ECN_MASK) {
107 ip4->ip_tos |= IPTOS_ECN_CE;
108 sum = old + (~(*(uint8_t *)&ip4->ip_tos) & 0xff);
109 sum += rte_be_to_cpu_16(ip4->ip_sum);
110 sum = (sum & 0xffff) + (sum >> 16);
111 ip4->ip_sum = rte_cpu_to_be_16(sum + (sum >> 16));
116 ip6_ecn_setup(struct ip6_hdr *ip6)
118 if ((ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK)
119 ip6->ip6_flow = htonl(ntohl(ip6->ip6_flow) |
120 (IPTOS_ECN_CE << 20));
124 ipip_inbound(struct rte_mbuf *m, uint32_t offset)
126 struct ip *inip4, *outip4;
127 struct ip6_hdr *inip6, *outip6;
128 uint32_t ip_len, set_ecn;
130 outip4 = rte_pktmbuf_mtod(m, struct ip*);
132 RTE_ASSERT(outip4->ip_v == IPVERSION || outip4->ip_v == IP6_VERSION);
134 if (outip4->ip_v == IPVERSION) {
135 ip_len = sizeof(struct ip);
136 set_ecn = ((outip4->ip_tos & IPTOS_ECN_CE) == IPTOS_ECN_CE);
138 outip6 = (struct ip6_hdr *)outip4;
139 ip_len = sizeof(struct ip6_hdr);
140 set_ecn = ntohl(outip6->ip6_flow) >> 20;
141 set_ecn = ((set_ecn & IPTOS_ECN_CE) == IPTOS_ECN_CE);
144 inip4 = (struct ip *)rte_pktmbuf_adj(m, offset + ip_len);
145 RTE_ASSERT(inip4->ip_v == IPVERSION || inip4->ip_v == IP6_VERSION);
147 /* Check packet is still bigger than IP header (inner) */
148 RTE_ASSERT(rte_pktmbuf_pkt_len(m) > ip_len);
150 /* RFC4301 5.1.2.1 Note 6 */
151 if (inip4->ip_v == IPVERSION) {
153 ip4_ecn_setup(inip4);
154 /* XXX This should be done by the forwarding engine instead */
156 if (inip4->ip_sum >= rte_cpu_to_be_16(0xffff - 0x100))
157 inip4->ip_sum += rte_cpu_to_be_16(0x100) + 1;
159 inip4->ip_sum += rte_cpu_to_be_16(0x100);
160 m->packet_type &= ~RTE_PTYPE_L4_MASK;
161 if (inip4->ip_p == IPPROTO_UDP)
162 m->packet_type |= RTE_PTYPE_L4_UDP;
163 else if (inip4->ip_p == IPPROTO_TCP)
164 m->packet_type |= RTE_PTYPE_L4_TCP;
166 inip6 = (struct ip6_hdr *)inip4;
168 ip6_ecn_setup(inip6);
169 /* XXX This should be done by the forwarding engine instead */
170 inip6->ip6_hops -= 1;
174 #endif /* __IPIP_H__ */