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