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 <sys/types.h>
38 #include <netinet/in.h>
39 #include <netinet/ip.h>
43 #include <rte_common.h>
44 #include <rte_memcpy.h>
45 #include <rte_crypto.h>
46 #include <rte_cryptodev.h>
47 #include <rte_random.h>
53 #define IP_ESP_HDR_SZ (sizeof(struct ip) + sizeof(struct esp_hdr))
56 random_iv_u64(uint64_t *buf, uint16_t n)
58 unsigned left = n & 0x7;
61 RTE_ASSERT((n & 0x3) == 0);
63 for (i = 0; i < (n >> 3); i++)
67 *((uint32_t *)&buf[i]) = (uint32_t)lrand48();
72 esp4_tunnel_inbound_pre_crypto(struct rte_mbuf *m, struct ipsec_sa *sa,
73 struct rte_crypto_op *cop)
76 struct rte_crypto_sym_op *sym_cop;
78 RTE_ASSERT(m != NULL);
79 RTE_ASSERT(sa != NULL);
80 RTE_ASSERT(cop != NULL);
82 payload_len = rte_pktmbuf_pkt_len(m) - IP_ESP_HDR_SZ - sa->iv_len -
85 if ((payload_len & (sa->block_size - 1)) || (payload_len <= 0)) {
86 RTE_LOG(DEBUG, IPSEC_ESP, "payload %d not multiple of %u\n",
87 payload_len, sa->block_size);
91 sym_cop = (struct rte_crypto_sym_op *)(cop + 1);
94 sym_cop->cipher.data.offset = IP_ESP_HDR_SZ + sa->iv_len;
95 sym_cop->cipher.data.length = payload_len;
97 sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, void*,
99 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
101 sym_cop->cipher.iv.length = sa->iv_len;
103 sym_cop->auth.data.offset = sizeof(struct ip);
104 if (sa->auth_algo == RTE_CRYPTO_AUTH_AES_GCM)
105 sym_cop->auth.data.length = sizeof(struct esp_hdr);
107 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
108 sa->iv_len + payload_len;
110 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*,
111 rte_pktmbuf_pkt_len(m) - sa->digest_len);
112 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
113 rte_pktmbuf_pkt_len(m) - sa->digest_len);
114 sym_cop->auth.digest.length = sa->digest_len;
120 esp4_tunnel_inbound_post_crypto(struct rte_mbuf *m, struct ipsec_sa *sa,
121 struct rte_crypto_op *cop)
123 uint8_t *nexthdr, *pad_len;
127 RTE_ASSERT(m != NULL);
128 RTE_ASSERT(sa != NULL);
129 RTE_ASSERT(cop != NULL);
131 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
132 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");
136 nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
137 rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
138 pad_len = nexthdr - 1;
140 padding = pad_len - *pad_len;
141 for (i = 0; i < *pad_len; i++) {
142 if (padding[i] != i) {
143 RTE_LOG(ERR, IPSEC_ESP, "invalid pad_len field\n");
148 if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) {
149 RTE_LOG(ERR, IPSEC_ESP,
150 "failed to remove pad_len + digest\n");
154 return ip4ip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
158 esp4_tunnel_outbound_pre_crypto(struct rte_mbuf *m, struct ipsec_sa *sa,
159 struct rte_crypto_op *cop)
161 uint16_t pad_payload_len, pad_len;
166 struct rte_crypto_sym_op *sym_cop;
168 RTE_ASSERT(m != NULL);
169 RTE_ASSERT(sa != NULL);
170 RTE_ASSERT(cop != NULL);
173 pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) + 2,
175 pad_len = pad_payload_len - rte_pktmbuf_pkt_len(m);
177 rte_prefetch0(rte_pktmbuf_mtod_offset(m, void *,
178 rte_pktmbuf_pkt_len(m)));
180 /* Check maximum packet size */
181 if (unlikely(IP_ESP_HDR_SZ + sa->iv_len + pad_payload_len +
182 sa->digest_len > IP_MAXPACKET)) {
183 RTE_LOG(DEBUG, IPSEC_ESP, "ipsec packet is too big\n");
187 padding = rte_pktmbuf_append(m, pad_len + sa->digest_len);
189 RTE_ASSERT(padding != NULL);
191 ip = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
194 esp = (struct esp_hdr *)(ip + 1);
196 esp->seq = htonl(sa->seq++);
198 RTE_LOG(DEBUG, IPSEC_ESP, "pktlen %u\n", rte_pktmbuf_pkt_len(m));
200 /* Fill pad_len using default sequential scheme */
201 for (i = 0; i < pad_len - 2; i++)
204 padding[pad_len - 2] = pad_len - 2;
205 padding[pad_len - 1] = IPPROTO_IPIP;
207 sym_cop = (struct rte_crypto_sym_op *)(cop + 1);
210 sym_cop->cipher.data.offset = IP_ESP_HDR_SZ + sa->iv_len;
211 sym_cop->cipher.data.length = pad_payload_len;
213 sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
215 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
217 sym_cop->cipher.iv.length = sa->iv_len;
219 sym_cop->auth.data.offset = sizeof(struct ip);
220 sym_cop->auth.data.length = sizeof(struct esp_hdr) + sa->iv_len +
223 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
224 IP_ESP_HDR_SZ + sa->iv_len + pad_payload_len);
225 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
226 IP_ESP_HDR_SZ + sa->iv_len + pad_payload_len);
227 sym_cop->auth.digest.length = sa->digest_len;
229 if (sa->cipher_algo == RTE_CRYPTO_CIPHER_AES_CBC)
230 random_iv_u64((uint64_t *)sym_cop->cipher.iv.data,
231 sym_cop->cipher.iv.length);
237 esp4_tunnel_outbound_post_crypto(struct rte_mbuf *m __rte_unused,
238 struct ipsec_sa *sa __rte_unused,
239 struct rte_crypto_op *cop)
241 RTE_ASSERT(m != NULL);
242 RTE_ASSERT(sa != NULL);
243 RTE_ASSERT(cop != NULL);
245 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
246 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");