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>
40 #include <netinet/ip6.h>
44 #include <rte_common.h>
45 #include <rte_memcpy.h>
46 #include <rte_crypto.h>
47 #include <rte_cryptodev.h>
48 #include <rte_random.h>
55 random_iv_u64(uint64_t *buf, uint16_t n)
57 uint32_t left = n & 0x7;
60 RTE_ASSERT((n & 0x3) == 0);
62 for (i = 0; i < (n >> 3); i++)
66 *((uint32_t *)&buf[i]) = (uint32_t)lrand48();
70 esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
71 struct rte_crypto_op *cop)
73 int32_t payload_len, ip_hdr_len;
74 struct rte_crypto_sym_op *sym_cop;
76 RTE_ASSERT(m != NULL);
77 RTE_ASSERT(sa != NULL);
78 RTE_ASSERT(cop != NULL);
83 ip_hdr_len = sizeof(struct ip);
86 ip_hdr_len = sizeof(struct ip6_hdr);
90 payload_len = rte_pktmbuf_pkt_len(m) - ip_hdr_len -
91 sizeof(struct esp_hdr) - sa->iv_len - sa->digest_len;
93 if ((payload_len & (sa->block_size - 1)) || (payload_len <= 0)) {
94 RTE_LOG(DEBUG, IPSEC_ESP, "payload %d not multiple of %u\n",
95 payload_len, sa->block_size);
99 sym_cop = (struct rte_crypto_sym_op *)(cop + 1);
102 sym_cop->cipher.data.offset = ip_hdr_len + sizeof(struct esp_hdr) +
104 sym_cop->cipher.data.length = payload_len;
106 sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, void*,
107 ip_hdr_len + sizeof(struct esp_hdr));
108 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
109 ip_hdr_len + sizeof(struct esp_hdr));
110 sym_cop->cipher.iv.length = sa->iv_len;
112 sym_cop->auth.data.offset = ip_hdr_len;
113 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
114 sa->iv_len + payload_len;
116 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*,
117 rte_pktmbuf_pkt_len(m) - sa->digest_len);
118 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
119 rte_pktmbuf_pkt_len(m) - sa->digest_len);
120 sym_cop->auth.digest.length = sa->digest_len;
126 esp_inbound_post(struct rte_mbuf *m, struct ipsec_sa *sa,
127 struct rte_crypto_op *cop)
129 uint8_t *nexthdr, *pad_len;
133 RTE_ASSERT(m != NULL);
134 RTE_ASSERT(sa != NULL);
135 RTE_ASSERT(cop != NULL);
137 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
138 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");
142 nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
143 rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
144 pad_len = nexthdr - 1;
146 padding = pad_len - *pad_len;
147 for (i = 0; i < *pad_len; i++) {
148 if (padding[i] != i + 1) {
149 RTE_LOG(ERR, IPSEC_ESP, "invalid pad_len field\n");
154 if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) {
155 RTE_LOG(ERR, IPSEC_ESP,
156 "failed to remove pad_len + digest\n");
160 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
166 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
167 struct rte_crypto_op *cop)
169 uint16_t pad_payload_len, pad_len, ip_hdr_len;
175 struct rte_crypto_sym_op *sym_cop;
177 RTE_ASSERT(m != NULL);
178 RTE_ASSERT(sa != NULL);
179 RTE_ASSERT(cop != NULL);
182 pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) + 2,
184 pad_len = pad_payload_len - rte_pktmbuf_pkt_len(m);
189 ip_hdr_len = sizeof(struct ip);
192 ip_hdr_len = sizeof(struct ip6_hdr);
196 /* Check maximum packet size */
197 if (unlikely(ip_hdr_len + sizeof(struct esp_hdr) + sa->iv_len +
198 pad_payload_len + sa->digest_len > IP_MAXPACKET)) {
199 RTE_LOG(ERR, IPSEC_ESP, "ipsec packet is too big\n");
203 padding = rte_pktmbuf_append(m, pad_len + sa->digest_len);
204 if (unlikely(padding == NULL)) {
205 RTE_LOG(ERR, IPSEC_ESP, "not enough mbuf trailing space\n");
208 rte_prefetch0(padding);
212 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
214 esp = (struct esp_hdr *)(ip4 + 1);
217 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
219 esp = (struct esp_hdr *)(ip6 + 1);
222 RTE_LOG(ERR, IPSEC_ESP, "Unsupported SA flags: 0x%x\n",
228 esp->spi = rte_cpu_to_be_32(sa->spi);
229 esp->seq = rte_cpu_to_be_32(sa->seq);
231 if (sa->cipher_algo == RTE_CRYPTO_CIPHER_AES_CBC)
232 random_iv_u64((uint64_t *)(esp + 1), sa->iv_len);
234 /* Fill pad_len using default sequential scheme */
235 for (i = 0; i < pad_len - 2; i++)
237 padding[pad_len - 2] = pad_len - 2;
239 if (RTE_ETH_IS_IPV4_HDR(m->packet_type))
240 padding[pad_len - 1] = IPPROTO_IPIP;
242 padding[pad_len - 1] = IPPROTO_IPV6;
244 sym_cop = (struct rte_crypto_sym_op *)(cop + 1);
247 sym_cop->cipher.data.offset = ip_hdr_len + sizeof(struct esp_hdr) +
249 sym_cop->cipher.data.length = pad_payload_len;
251 sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
252 ip_hdr_len + sizeof(struct esp_hdr));
253 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
254 ip_hdr_len + sizeof(struct esp_hdr));
255 sym_cop->cipher.iv.length = sa->iv_len;
257 sym_cop->auth.data.offset = ip_hdr_len;
258 sym_cop->auth.data.length = sizeof(struct esp_hdr) + sa->iv_len +
261 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
262 rte_pktmbuf_pkt_len(m) - sa->digest_len);
263 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
264 rte_pktmbuf_pkt_len(m) - sa->digest_len);
265 sym_cop->auth.digest.length = sa->digest_len;
271 esp_outbound_post(struct rte_mbuf *m __rte_unused,
272 struct ipsec_sa *sa __rte_unused,
273 struct rte_crypto_op *cop)
275 RTE_ASSERT(m != NULL);
276 RTE_ASSERT(sa != NULL);
277 RTE_ASSERT(cop != NULL);
279 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
280 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");