4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
36 #include <netinet/ip.h>
37 #include <sys/types.h>
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>
52 #define IP_ESP_HDR_SZ (sizeof(struct ip) + sizeof(struct esp_hdr))
55 random_iv_u64(uint64_t *buf, uint16_t n)
57 unsigned left = n & 0x7;
60 IPSEC_ASSERT((n & 0x3) == 0);
62 for (i = 0; i < (n >> 3); i++)
66 *((uint32_t *)&buf[i]) = (uint32_t)lrand48();
71 esp4_tunnel_inbound_pre_crypto(struct rte_mbuf *m, struct ipsec_sa *sa,
72 struct rte_crypto_op *cop)
75 struct rte_crypto_sym_op *sym_cop;
77 IPSEC_ASSERT(m != NULL);
78 IPSEC_ASSERT(sa != NULL);
79 IPSEC_ASSERT(cop != NULL);
81 payload_len = rte_pktmbuf_pkt_len(m) - IP_ESP_HDR_SZ - sa->iv_len -
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);
90 sym_cop = (struct rte_crypto_sym_op *)(cop + 1);
93 sym_cop->cipher.data.offset = IP_ESP_HDR_SZ + sa->iv_len;
94 sym_cop->cipher.data.length = payload_len;
96 sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, void*,
98 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
100 sym_cop->cipher.iv.length = sa->iv_len;
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);
106 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
107 sa->iv_len + payload_len;
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;
119 esp4_tunnel_inbound_post_crypto(struct rte_mbuf *m, struct ipsec_sa *sa,
120 struct rte_crypto_op *cop)
122 uint8_t *nexthdr, *pad_len;
126 IPSEC_ASSERT(m != NULL);
127 IPSEC_ASSERT(sa != NULL);
128 IPSEC_ASSERT(cop != NULL);
130 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
131 IPSEC_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");
135 nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
136 rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
137 pad_len = nexthdr - 1;
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");
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");
153 return ip4ip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
157 esp4_tunnel_outbound_pre_crypto(struct rte_mbuf *m, struct ipsec_sa *sa,
158 struct rte_crypto_op *cop)
160 uint16_t pad_payload_len, pad_len;
165 struct rte_crypto_sym_op *sym_cop;
167 IPSEC_ASSERT(m != NULL);
168 IPSEC_ASSERT(sa != NULL);
169 IPSEC_ASSERT(cop != NULL);
172 pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) + 2,
174 pad_len = pad_payload_len - rte_pktmbuf_pkt_len(m);
176 rte_prefetch0(rte_pktmbuf_mtod_offset(m, void *,
177 rte_pktmbuf_pkt_len(m)));
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");
186 padding = rte_pktmbuf_append(m, pad_len + sa->digest_len);
188 IPSEC_ASSERT(padding != NULL);
190 ip = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
193 esp = (struct esp_hdr *)(ip + 1);
195 esp->seq = htonl(sa->seq++);
197 IPSEC_LOG(DEBUG, IPSEC_ESP, "pktlen %u\n", rte_pktmbuf_pkt_len(m));
199 /* Fill pad_len using default sequential scheme */
200 for (i = 0; i < pad_len - 2; i++)
203 padding[pad_len - 2] = pad_len - 2;
204 padding[pad_len - 1] = IPPROTO_IPIP;
206 sym_cop = (struct rte_crypto_sym_op *)(cop + 1);
209 sym_cop->cipher.data.offset = IP_ESP_HDR_SZ + sa->iv_len;
210 sym_cop->cipher.data.length = pad_payload_len;
212 sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
214 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
216 sym_cop->cipher.iv.length = sa->iv_len;
218 sym_cop->auth.data.offset = sizeof(struct ip);
219 sym_cop->auth.data.length = sizeof(struct esp_hdr) + sa->iv_len +
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;
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);
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)
240 IPSEC_ASSERT(m != NULL);
241 IPSEC_ASSERT(sa != NULL);
242 IPSEC_ASSERT(cop != NULL);
244 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
245 IPSEC_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");