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 #include <netinet/ip6.h> 41 42 #include <rte_mbuf.h> 43 44 static inline void * 45 ipip_outbound(struct rte_mbuf *m, uint32_t offset, uint32_t is_ipv6, 46 struct ip_addr *src, struct ip_addr *dst) 47 { 48 struct ip *inip4, *outip4; 49 struct ip6_hdr *inip6, *outip6; 50 uint8_t ds_ecn; 51 52 inip4 = rte_pktmbuf_mtod(m, struct ip *); 53 54 RTE_ASSERT(inip4->ip_v == IPVERSION || inip4->ip_v == IP6_VERSION); 55 56 if (inip4->ip_v == IPVERSION) { 57 /* XXX This should be done by the forwarding engine instead */ 58 inip4->ip_ttl -= 1; 59 ds_ecn = inip4->ip_tos; 60 } else { 61 inip6 = (struct ip6_hdr *)inip4; 62 /* XXX This should be done by the forwarding engine instead */ 63 inip6->ip6_hops -= 1; 64 ds_ecn = ntohl(inip6->ip6_flow) >> 20; 65 } 66 67 if (is_ipv6) { 68 offset += sizeof(struct ip6_hdr); 69 outip6 = (struct ip6_hdr *)rte_pktmbuf_prepend(m, offset); 70 71 RTE_ASSERT(outip6 != NULL); 72 73 /* Per RFC4301 5.1.2.1 */ 74 outip6->ip6_flow = htonl(IP6_VERSION << 28 | ds_ecn << 20); 75 outip6->ip6_plen = htons(rte_pktmbuf_data_len(m) - 76 sizeof(struct ip6_hdr)); 77 78 outip6->ip6_nxt = IPPROTO_ESP; 79 outip6->ip6_hops = IPDEFTTL; 80 81 memcpy(&outip6->ip6_src.s6_addr, src, 16); 82 memcpy(&outip6->ip6_dst.s6_addr, dst, 16); 83 84 return outip6; 85 } 86 87 offset += sizeof(struct ip); 88 outip4 = (struct ip *)rte_pktmbuf_prepend(m, offset); 89 90 RTE_ASSERT(outip4 != NULL); 91 92 /* Per RFC4301 5.1.2.1 */ 93 outip4->ip_v = IPVERSION; 94 outip4->ip_hl = 5; 95 outip4->ip_tos = ds_ecn; 96 outip4->ip_len = htons(rte_pktmbuf_data_len(m)); 97 98 outip4->ip_id = 0; 99 outip4->ip_off = 0; 100 101 outip4->ip_ttl = IPDEFTTL; 102 outip4->ip_p = IPPROTO_ESP; 103 104 outip4->ip_src.s_addr = src->ip.ip4; 105 outip4->ip_dst.s_addr = dst->ip.ip4; 106 107 return outip4; 108 } 109 110 static inline struct ip * 111 ip4ip_outbound(struct rte_mbuf *m, uint32_t offset, 112 struct ip_addr *src, struct ip_addr *dst) 113 { 114 return ipip_outbound(m, offset, 0, src, dst); 115 } 116 117 static inline struct ip6_hdr * 118 ip6ip_outbound(struct rte_mbuf *m, uint32_t offset, 119 struct ip_addr *src, struct ip_addr *dst) 120 { 121 return ipip_outbound(m, offset, 1, src, dst); 122 } 123 124 static inline void 125 ip4_ecn_setup(struct ip *ip4) 126 { 127 if (ip4->ip_tos & IPTOS_ECN_MASK) 128 ip4->ip_tos |= IPTOS_ECN_CE; 129 } 130 131 static inline void 132 ip6_ecn_setup(struct ip6_hdr *ip6) 133 { 134 if ((ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK) 135 ip6->ip6_flow = htonl(ntohl(ip6->ip6_flow) | 136 (IPTOS_ECN_CE << 20)); 137 } 138 139 static inline void 140 ipip_inbound(struct rte_mbuf *m, uint32_t offset) 141 { 142 struct ip *inip4, *outip4; 143 struct ip6_hdr *inip6, *outip6; 144 uint32_t ip_len, set_ecn; 145 146 outip4 = rte_pktmbuf_mtod(m, struct ip*); 147 148 RTE_ASSERT(outip4->ip_v == IPVERSION || outip4->ip_v == IP6_VERSION); 149 150 if (outip4->ip_v == IPVERSION) { 151 ip_len = sizeof(struct ip); 152 set_ecn = ((outip4->ip_tos & IPTOS_ECN_CE) == IPTOS_ECN_CE); 153 } else { 154 outip6 = (struct ip6_hdr *)outip4; 155 ip_len = sizeof(struct ip6_hdr); 156 set_ecn = ntohl(outip6->ip6_flow) >> 20; 157 set_ecn = ((set_ecn & IPTOS_ECN_CE) == IPTOS_ECN_CE); 158 } 159 160 inip4 = (struct ip *)rte_pktmbuf_adj(m, offset + ip_len); 161 RTE_ASSERT(inip4->ip_v == IPVERSION || inip4->ip_v == IP6_VERSION); 162 163 /* Check packet is still bigger than IP header (inner) */ 164 RTE_ASSERT(rte_pktmbuf_pkt_len(m) > ip_len); 165 166 /* RFC4301 5.1.2.1 Note 6 */ 167 if (inip4->ip_v == IPVERSION) { 168 if (set_ecn) 169 ip4_ecn_setup(inip4); 170 /* XXX This should be done by the forwarding engine instead */ 171 inip4->ip_ttl -= 1; 172 } else { 173 inip6 = (struct ip6_hdr *)inip4; 174 if (set_ecn) 175 ip6_ecn_setup(inip6); 176 /* XXX This should be done by the forwarding engine instead */ 177 inip6->ip6_hops -= 1; 178 } 179 } 180 181 #endif /* __IPIP_H__ */ 182