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 #include <stdint.h> 35 #include <stdlib.h> 36 #include <netinet/ip.h> 37 #include <sys/types.h> 38 #include <sys/stat.h> 39 #include <fcntl.h> 40 #include <unistd.h> 41 42 #include <rte_common.h> 43 #include <rte_memcpy.h> 44 #include <rte_crypto.h> 45 #include <rte_cryptodev.h> 46 #include <rte_random.h> 47 48 #include "ipsec.h" 49 #include "esp.h" 50 #include "ipip.h" 51 52 #define IP_ESP_HDR_SZ (sizeof(struct ip) + sizeof(struct esp_hdr)) 53 54 static inline void 55 random_iv_u64(uint64_t *buf, uint16_t n) 56 { 57 unsigned left = n & 0x7; 58 unsigned i; 59 60 IPSEC_ASSERT((n & 0x3) == 0); 61 62 for (i = 0; i < (n >> 3); i++) 63 buf[i] = rte_rand(); 64 65 if (left) 66 *((uint32_t *)&buf[i]) = (uint32_t)lrand48(); 67 } 68 69 /* IPv4 Tunnel */ 70 int 71 esp4_tunnel_inbound_pre_crypto(struct rte_mbuf *m, struct ipsec_sa *sa, 72 struct rte_crypto_op *cop) 73 { 74 int32_t payload_len; 75 struct rte_crypto_sym_op *sym_cop; 76 77 IPSEC_ASSERT(m != NULL); 78 IPSEC_ASSERT(sa != NULL); 79 IPSEC_ASSERT(cop != NULL); 80 81 payload_len = rte_pktmbuf_pkt_len(m) - IP_ESP_HDR_SZ - sa->iv_len - 82 sa->digest_len; 83 84 if ((payload_len & (sa->block_size - 1)) || (payload_len <= 0)) { 85 IPSEC_LOG(DEBUG, IPSEC_ESP, "payload %d not multiple of %u\n", 86 payload_len, sa->block_size); 87 return -EINVAL; 88 } 89 90 sym_cop = (struct rte_crypto_sym_op *)(cop + 1); 91 92 sym_cop->m_src = m; 93 sym_cop->cipher.data.offset = IP_ESP_HDR_SZ + sa->iv_len; 94 sym_cop->cipher.data.length = payload_len; 95 96 sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, void*, 97 IP_ESP_HDR_SZ); 98 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m, 99 IP_ESP_HDR_SZ); 100 sym_cop->cipher.iv.length = sa->iv_len; 101 102 sym_cop->auth.data.offset = sizeof(struct ip); 103 if (sa->auth_algo == RTE_CRYPTO_AUTH_AES_GCM) 104 sym_cop->auth.data.length = sizeof(struct esp_hdr); 105 else 106 sym_cop->auth.data.length = sizeof(struct esp_hdr) + 107 sa->iv_len + payload_len; 108 109 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*, 110 rte_pktmbuf_pkt_len(m) - sa->digest_len); 111 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m, 112 rte_pktmbuf_pkt_len(m) - sa->digest_len); 113 sym_cop->auth.digest.length = sa->digest_len; 114 115 return 0; 116 } 117 118 int 119 esp4_tunnel_inbound_post_crypto(struct rte_mbuf *m, struct ipsec_sa *sa, 120 struct rte_crypto_op *cop) 121 { 122 uint8_t *nexthdr, *pad_len; 123 uint8_t *padding; 124 uint16_t i; 125 126 IPSEC_ASSERT(m != NULL); 127 IPSEC_ASSERT(sa != NULL); 128 IPSEC_ASSERT(cop != NULL); 129 130 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) { 131 IPSEC_LOG(ERR, IPSEC_ESP, "Failed crypto op\n"); 132 return -1; 133 } 134 135 nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*, 136 rte_pktmbuf_pkt_len(m) - sa->digest_len - 1); 137 pad_len = nexthdr - 1; 138 139 padding = pad_len - *pad_len; 140 for (i = 0; i < *pad_len; i++) { 141 if (padding[i] != i) { 142 IPSEC_LOG(ERR, IPSEC_ESP, "invalid pad_len field\n"); 143 return -EINVAL; 144 } 145 } 146 147 if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) { 148 IPSEC_LOG(ERR, IPSEC_ESP, 149 "failed to remove pad_len + digest\n"); 150 return -EINVAL; 151 } 152 153 return ip4ip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len); 154 } 155 156 int 157 esp4_tunnel_outbound_pre_crypto(struct rte_mbuf *m, struct ipsec_sa *sa, 158 struct rte_crypto_op *cop) 159 { 160 uint16_t pad_payload_len, pad_len; 161 struct ip *ip; 162 struct esp_hdr *esp; 163 int i; 164 char *padding; 165 struct rte_crypto_sym_op *sym_cop; 166 167 IPSEC_ASSERT(m != NULL); 168 IPSEC_ASSERT(sa != NULL); 169 IPSEC_ASSERT(cop != NULL); 170 171 /* Payload length */ 172 pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) + 2, 173 sa->block_size); 174 pad_len = pad_payload_len - rte_pktmbuf_pkt_len(m); 175 176 rte_prefetch0(rte_pktmbuf_mtod_offset(m, void *, 177 rte_pktmbuf_pkt_len(m))); 178 179 /* Check maximum packet size */ 180 if (unlikely(IP_ESP_HDR_SZ + sa->iv_len + pad_payload_len + 181 sa->digest_len > IP_MAXPACKET)) { 182 IPSEC_LOG(DEBUG, IPSEC_ESP, "ipsec packet is too big\n"); 183 return -EINVAL; 184 } 185 186 padding = rte_pktmbuf_append(m, pad_len + sa->digest_len); 187 188 IPSEC_ASSERT(padding != NULL); 189 190 ip = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len, 191 sa->src, sa->dst); 192 193 esp = (struct esp_hdr *)(ip + 1); 194 esp->spi = sa->spi; 195 esp->seq = htonl(sa->seq++); 196 197 IPSEC_LOG(DEBUG, IPSEC_ESP, "pktlen %u\n", rte_pktmbuf_pkt_len(m)); 198 199 /* Fill pad_len using default sequential scheme */ 200 for (i = 0; i < pad_len - 2; i++) 201 padding[i] = i + 1; 202 203 padding[pad_len - 2] = pad_len - 2; 204 padding[pad_len - 1] = IPPROTO_IPIP; 205 206 sym_cop = (struct rte_crypto_sym_op *)(cop + 1); 207 208 sym_cop->m_src = m; 209 sym_cop->cipher.data.offset = IP_ESP_HDR_SZ + sa->iv_len; 210 sym_cop->cipher.data.length = pad_payload_len; 211 212 sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, uint8_t *, 213 IP_ESP_HDR_SZ); 214 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m, 215 IP_ESP_HDR_SZ); 216 sym_cop->cipher.iv.length = sa->iv_len; 217 218 sym_cop->auth.data.offset = sizeof(struct ip); 219 sym_cop->auth.data.length = sizeof(struct esp_hdr) + sa->iv_len + 220 pad_payload_len; 221 222 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *, 223 IP_ESP_HDR_SZ + sa->iv_len + pad_payload_len); 224 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m, 225 IP_ESP_HDR_SZ + sa->iv_len + pad_payload_len); 226 sym_cop->auth.digest.length = sa->digest_len; 227 228 if (sa->cipher_algo == RTE_CRYPTO_CIPHER_AES_CBC) 229 random_iv_u64((uint64_t *)sym_cop->cipher.iv.data, 230 sym_cop->cipher.iv.length); 231 232 return 0; 233 } 234 235 int 236 esp4_tunnel_outbound_post_crypto(struct rte_mbuf *m __rte_unused, 237 struct ipsec_sa *sa __rte_unused, 238 struct rte_crypto_op *cop) 239 { 240 IPSEC_ASSERT(m != NULL); 241 IPSEC_ASSERT(sa != NULL); 242 IPSEC_ASSERT(cop != NULL); 243 244 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) { 245 IPSEC_LOG(ERR, IPSEC_ESP, "Failed crypto op\n"); 246 return -1; 247 } 248 249 return 0; 250 } 251