blob: dd47053874caef13dcd70be1871ec43a03c3ce37 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * esp_encrypt.c : IPSec ESP encrypt node
3 *
4 * Copyright (c) 2015 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
Arthur de Kerhor2f4586d2021-09-22 14:53:24 +020021#include <vnet/interface_output.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
Damjan Marion91f17dc2019-03-18 18:59:25 +010023#include <vnet/crypto/crypto.h>
24
Ed Warnickecb9cada2015-12-08 15:45:58 -070025#include <vnet/ipsec/ipsec.h>
Neale Ranns28287212019-12-16 00:53:11 +000026#include <vnet/ipsec/ipsec_tun.h>
Neale Ranns93688d72022-08-09 03:34:51 +000027#include <vnet/ipsec/ipsec.api_enum.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028#include <vnet/ipsec/esp.h>
Neale Ranns041add72020-01-02 04:06:10 +000029#include <vnet/tunnel/tunnel_dp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
Neale Ranns4a58e492020-12-21 13:19:10 +000031#define foreach_esp_encrypt_next \
32 _ (DROP4, "ip4-drop") \
33 _ (DROP6, "ip6-drop") \
34 _ (DROP_MPLS, "mpls-drop") \
35 _ (HANDOFF4, "handoff4") \
36 _ (HANDOFF6, "handoff6") \
37 _ (HANDOFF_MPLS, "handoff-mpls") \
38 _ (INTERFACE_OUTPUT, "interface-output")
Ed Warnickecb9cada2015-12-08 15:45:58 -070039
40#define _(v, s) ESP_ENCRYPT_NEXT_##v,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070041typedef enum
42{
Ed Warnickecb9cada2015-12-08 15:45:58 -070043 foreach_esp_encrypt_next
44#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070045 ESP_ENCRYPT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070046} esp_encrypt_next_t;
47
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070048typedef struct
49{
Neale Ranns8d7c5022019-02-06 01:41:05 -080050 u32 sa_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 u32 spi;
52 u32 seq;
Neale Ranns6afaae12019-07-17 15:07:14 +000053 u32 sa_seq_hi;
Klement Sekera4b089f22018-04-17 18:04:57 +020054 u8 udp_encap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 ipsec_crypto_alg_t crypto_alg;
56 ipsec_integ_alg_t integ_alg;
57} esp_encrypt_trace_t;
58
Fan Zhangf5395782020-04-29 14:00:03 +010059typedef struct
60{
61 u32 next_index;
62} esp_encrypt_post_trace_t;
63
Neale Ranns93688d72022-08-09 03:34:51 +000064typedef vl_counter_esp_encrypt_enum_t esp_encrypt_error_t;
65
Ed Warnickecb9cada2015-12-08 15:45:58 -070066/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070067static u8 *
68format_esp_encrypt_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069{
70 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
71 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070072 esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070073
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020074 s =
75 format (s,
Neale Ranns6afaae12019-07-17 15:07:14 +000076 "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
77 t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
78 format_ipsec_crypto_alg,
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020079 t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
80 t->udp_encap ? " udp-encap-enabled" : "");
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 return s;
82}
83
Fan Zhangf5395782020-04-29 14:00:03 +010084static u8 *
85format_esp_post_encrypt_trace (u8 * s, va_list * args)
86{
87 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
88 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
89 esp_encrypt_post_trace_t *t = va_arg (*args, esp_encrypt_post_trace_t *);
90
91 s = format (s, "esp-post: next node index %u", t->next_index);
92 return s;
93}
94
Damjan Marionc59b9a22019-03-19 15:38:40 +010095/* pad packet in input buffer */
96static_always_inline u8 *
Neale Rannsf16e9a52021-02-25 19:09:24 +000097esp_add_footer_and_icv (vlib_main_t *vm, vlib_buffer_t **last, u8 esp_align,
Fan Zhangb8cb2232024-03-12 20:39:12 +000098 u8 icv_sz, u16 buffer_data_size, uword total_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -070099{
Damjan Marionc59b9a22019-03-19 15:38:40 +0100100 static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = {
101 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
Milan Lenco7885c742020-08-20 13:23:09 +0200102 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
Damjan Marionc59b9a22019-03-19 15:38:40 +0100103 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000105 u16 min_length = total_len + sizeof (esp_footer_t);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500106 u16 new_length = round_pow2 (min_length, esp_align);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100107 u8 pad_bytes = new_length - min_length;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000108 esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (last[0]) +
109 last[0]->current_length + pad_bytes);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000110 u16 tail_sz = sizeof (esp_footer_t) + pad_bytes + icv_sz;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
Benoît Ganne217ba5a2021-06-14 17:23:56 +0200112 if (last[0]->current_data + last[0]->current_length + tail_sz >
113 buffer_data_size)
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000114 {
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000115 u32 tmp_bi = 0;
116 if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1)
117 return 0;
Filip Tehlarc2c1bfd2020-02-13 07:49:30 +0000118
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000119 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
120 last[0]->next_buffer = tmp_bi;
121 last[0]->flags |= VLIB_BUFFER_NEXT_PRESENT;
122 f = (esp_footer_t *) (vlib_buffer_get_current (tmp) + pad_bytes);
123 tmp->current_length += tail_sz;
124 last[0] = tmp;
125 }
126 else
127 last[0]->current_length += tail_sz;
128
129 f->pad_length = pad_bytes;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100130 if (pad_bytes)
Benoît Ganne4505f012019-12-07 09:14:27 -0700131 {
132 ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE);
133 pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes);
134 clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes);
135 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100136
Damjan Marionc59b9a22019-03-19 15:38:40 +0100137 return &f->next_header;
138}
139
140static_always_inline void
141esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp)
142{
Neale Ranns1b582b82019-04-18 19:49:13 -0700143 ip_csum_t sum;
144 u16 old_len;
145
146 len = clib_net_to_host_u16 (len);
147 old_len = ip4->length;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100148
149 if (is_transport)
150 {
151 u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
Arthur de Kerhor2f4586d2021-09-22 14:53:24 +0200152 sum = ip_csum_update (ip4->checksum, ip4->protocol, prot, ip4_header_t,
153 protocol);
Neale Ranns1b582b82019-04-18 19:49:13 -0700154 ip4->protocol = prot;
Neale Ranns1b582b82019-04-18 19:49:13 -0700155 sum = ip_csum_update (sum, old_len, len, ip4_header_t, length);
156 }
157 else
158 sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
159
160 ip4->length = len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100161 ip4->checksum = ip_csum_fold (sum);
162}
163
164static_always_inline void
165esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
166{
167 clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
168 udp->length = clib_net_to_host_u16 (len);
169}
170
171static_always_inline u8
172ext_hdr_is_pre_esp (u8 nexthdr)
173{
174#ifdef CLIB_HAVE_VEC128
175 static const u8x16 ext_hdr_types = {
176 IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
177 IP_PROTOCOL_IPV6_ROUTE,
178 IP_PROTOCOL_IPV6_FRAGMENTATION,
179 };
180
181 return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
182#else
Piotr Bronowski3a6bc6f2023-02-23 09:56:49 +0000183 return (!(nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ||
184 !(nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) ||
185 !(nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100186#endif
187}
188
189static_always_inline u8
Neale Ranns02950402019-12-20 00:54:57 +0000190esp_get_ip6_hdr_len (ip6_header_t * ip6, ip6_ext_header_t ** ext_hdr)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100191{
192 /* this code assumes that HbH, route and frag headers will be before
193 others, if that is not the case, they will end up encrypted */
Damjan Marionc59b9a22019-03-19 15:38:40 +0100194 u8 len = sizeof (ip6_header_t);
195 ip6_ext_header_t *p;
196
197 /* if next packet doesn't have ext header */
198 if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
Neale Ranns02950402019-12-20 00:54:57 +0000199 {
200 *ext_hdr = NULL;
201 return len;
202 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100203
Ole Troan03092c12021-11-23 15:55:39 +0100204 p = ip6_next_header (ip6);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100205 len += ip6_ext_header_len (p);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100206 while (ext_hdr_is_pre_esp (p->next_hdr))
207 {
208 len += ip6_ext_header_len (p);
209 p = ip6_ext_next_header (p);
210 }
211
Neale Ranns02950402019-12-20 00:54:57 +0000212 *ext_hdr = p;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100213 return len;
214}
215
Benoît Ganne02dfd292022-01-18 15:56:41 +0100216/* IPsec IV generation: IVs requirements differ depending of the
217 * encryption mode: IVs must be unpredictable for AES-CBC whereas it can
218 * be predictable but should never be reused with the same key material
219 * for CTR and GCM.
Benoît Ganne5527a782022-01-18 15:56:41 +0100220 * To avoid reusing the same IVs between multiple VPP instances and between
221 * restarts, we use a properly chosen PRNG to generate IVs. To ensure the IV is
222 * unpredictable for CBC, it is then encrypted using the same key as the
223 * message. You can refer to NIST SP800-38a and NIST SP800-38d for more
224 * details. */
Benoît Ganne02dfd292022-01-18 15:56:41 +0100225static_always_inline void *
226esp_generate_iv (ipsec_sa_t *sa, void *payload, int iv_sz)
227{
228 ASSERT (iv_sz >= sizeof (u64));
229 u64 *iv = (u64 *) (payload - iv_sz);
230 clib_memset_u8 (iv, 0, iv_sz);
Benoît Ganne5527a782022-01-18 15:56:41 +0100231 *iv = clib_pcg64i_random_r (&sa->iv_prng);
Benoît Ganne02dfd292022-01-18 15:56:41 +0100232 return iv;
233}
234
Damjan Marionc59b9a22019-03-19 15:38:40 +0100235static_always_inline void
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000236esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
237 vnet_crypto_op_t * ops, vlib_buffer_t * b[],
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000238 u16 * nexts, vnet_crypto_op_chunk_t * chunks,
239 u16 drop_next)
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000240{
241 u32 n_fail, n_ops = vec_len (ops);
242 vnet_crypto_op_t *op = ops;
243
244 if (n_ops == 0)
245 return;
246
247 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
248
249 while (n_fail)
250 {
251 ASSERT (op - ops < n_ops);
252
253 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
254 {
255 u32 bi = op->user_data;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100256 esp_encrypt_set_next_index (b[bi], node, vm->thread_index,
257 ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR,
258 bi, nexts, drop_next,
259 vnet_buffer (b[bi])->ipsec.sad_index);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000260 n_fail--;
261 }
262 op++;
263 }
264}
265
266static_always_inline void
Damjan Marionc59b9a22019-03-19 15:38:40 +0100267esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000268 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts,
269 u16 drop_next)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100270{
271 u32 n_fail, n_ops = vec_len (ops);
272 vnet_crypto_op_t *op = ops;
273
274 if (n_ops == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275 return;
276
Damjan Marionc59b9a22019-03-19 15:38:40 +0100277 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
Damjan Marionc59b9a22019-03-19 15:38:40 +0100279 while (n_fail)
280 {
281 ASSERT (op - ops < n_ops);
282
283 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
284 {
285 u32 bi = op->user_data;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100286 esp_encrypt_set_next_index (b[bi], node, vm->thread_index,
287 ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR,
288 bi, nexts, drop_next,
289 vnet_buffer (b[bi])->ipsec.sad_index);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100290 n_fail--;
291 }
292 op++;
293 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294}
295
Fan Zhangf5395782020-04-29 14:00:03 +0100296static_always_inline u32
297esp_encrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
298 ipsec_sa_t * sa0, vlib_buffer_t * b,
299 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
300 u32 start_len, u16 * n_ch)
301{
302 vnet_crypto_op_chunk_t *ch;
303 vlib_buffer_t *cb = b;
304 u32 n_chunks = 1;
305 u32 total_len;
306 vec_add2 (ptd->chunks, ch, 1);
307 total_len = ch->len = start_len;
308 ch->src = ch->dst = start;
309 cb = vlib_get_buffer (vm, cb->next_buffer);
310
311 while (1)
312 {
313 vec_add2 (ptd->chunks, ch, 1);
314 n_chunks += 1;
315 if (lb == cb)
316 total_len += ch->len = cb->current_length - icv_sz;
317 else
318 total_len += ch->len = cb->current_length;
319 ch->src = ch->dst = vlib_buffer_get_current (cb);
320
321 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
322 break;
323
324 cb = vlib_get_buffer (vm, cb->next_buffer);
325 }
326
327 if (n_ch)
328 *n_ch = n_chunks;
329
330 return total_len;
331}
332
333static_always_inline u32
334esp_encrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
335 ipsec_sa_t * sa0, vlib_buffer_t * b,
336 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
337 u32 start_len, u8 * digest, u16 * n_ch)
338{
339 vnet_crypto_op_chunk_t *ch;
340 vlib_buffer_t *cb = b;
341 u32 n_chunks = 1;
342 u32 total_len;
343 vec_add2 (ptd->chunks, ch, 1);
344 total_len = ch->len = start_len;
345 ch->src = start;
346 cb = vlib_get_buffer (vm, cb->next_buffer);
347
348 while (1)
349 {
350 vec_add2 (ptd->chunks, ch, 1);
351 n_chunks += 1;
352 if (lb == cb)
353 {
354 total_len += ch->len = cb->current_length - icv_sz;
355 if (ipsec_sa_is_set_USE_ESN (sa0))
356 {
357 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
358 clib_memcpy_fast (digest, &seq_hi, sizeof (seq_hi));
359 ch->len += sizeof (seq_hi);
360 total_len += sizeof (seq_hi);
361 }
362 }
363 else
364 total_len += ch->len = cb->current_length;
365 ch->src = vlib_buffer_get_current (cb);
366
367 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
368 break;
369
370 cb = vlib_get_buffer (vm, cb->next_buffer);
371 }
372
373 if (n_ch)
374 *n_ch = n_chunks;
375
376 return total_len;
377}
378
379always_inline void
Benoît Ganne490b9272021-01-22 18:03:09 +0100380esp_prepare_sync_op (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
381 vnet_crypto_op_t **crypto_ops,
Neale Ranns5b891102021-06-28 13:31:28 +0000382 vnet_crypto_op_t **integ_ops, ipsec_sa_t *sa0, u32 seq_hi,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000383 u8 *payload, u16 payload_len, u8 iv_sz, u8 icv_sz, u32 bi,
384 vlib_buffer_t **b, vlib_buffer_t *lb, u32 hdr_len,
385 esp_header_t *esp)
Fan Zhangf5395782020-04-29 14:00:03 +0100386{
387 if (sa0->crypto_enc_op_id)
388 {
389 vnet_crypto_op_t *op;
390 vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
391 vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
Benoît Ganne02dfd292022-01-18 15:56:41 +0100392 u8 *crypto_start = payload;
393 /* esp_add_footer_and_icv() in esp_encrypt_inline() makes sure we always
394 * have enough space for ESP header and footer which includes ICV */
395 ASSERT (payload_len > icv_sz);
396 u16 crypto_len = payload_len - icv_sz;
Fan Zhangf5395782020-04-29 14:00:03 +0100397
Benoît Ganne02dfd292022-01-18 15:56:41 +0100398 /* generate the IV in front of the payload */
399 void *pkt_iv = esp_generate_iv (sa0, payload, iv_sz);
400
Fan Zhangf5395782020-04-29 14:00:03 +0100401 op->key_index = sa0->crypto_key_index;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000402 op->user_data = bi;
Fan Zhangf5395782020-04-29 14:00:03 +0100403
Benoît Ganne490b9272021-01-22 18:03:09 +0100404 if (ipsec_sa_is_set_IS_CTR (sa0))
Fan Zhangf5395782020-04-29 14:00:03 +0100405 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100406 /* construct nonce in a scratch space in front of the IP header */
407 esp_ctr_nonce_t *nonce =
Benoît Ganne02dfd292022-01-18 15:56:41 +0100408 (esp_ctr_nonce_t *) (pkt_iv - hdr_len - sizeof (*nonce));
Benoît Ganne490b9272021-01-22 18:03:09 +0100409 if (ipsec_sa_is_set_IS_AEAD (sa0))
410 {
411 /* constuct aad in a scratch space in front of the nonce */
412 op->aad = (u8 *) nonce - sizeof (esp_aead_t);
Neale Ranns5b891102021-06-28 13:31:28 +0000413 op->aad_len = esp_aad_fill (op->aad, esp, sa0, seq_hi);
Benoît Ganne02dfd292022-01-18 15:56:41 +0100414 op->tag = payload + crypto_len;
Benoît Ganne490b9272021-01-22 18:03:09 +0100415 op->tag_len = 16;
Benoît Ganne84e66582023-03-10 17:33:03 +0100416 if (PREDICT_FALSE (ipsec_sa_is_set_IS_NULL_GMAC (sa0)))
417 {
418 /* RFC-4543 ENCR_NULL_AUTH_AES_GMAC: IV is part of AAD */
419 crypto_start -= iv_sz;
420 crypto_len += iv_sz;
421 }
Benoît Ganne490b9272021-01-22 18:03:09 +0100422 }
423 else
424 {
425 nonce->ctr = clib_host_to_net_u32 (1);
426 }
Fan Zhangf5395782020-04-29 14:00:03 +0100427
Fan Zhangf5395782020-04-29 14:00:03 +0100428 nonce->salt = sa0->salt;
Benoît Ganne02dfd292022-01-18 15:56:41 +0100429 nonce->iv = *(u64 *) pkt_iv;
Fan Zhangf5395782020-04-29 14:00:03 +0100430 op->iv = (u8 *) nonce;
431 }
432 else
433 {
Benoît Ganne02dfd292022-01-18 15:56:41 +0100434 /* construct zero iv in front of the IP header */
435 op->iv = pkt_iv - hdr_len - iv_sz;
436 clib_memset_u8 (op->iv, 0, iv_sz);
437 /* include iv field in crypto */
438 crypto_start -= iv_sz;
439 crypto_len += iv_sz;
Fan Zhangf5395782020-04-29 14:00:03 +0100440 }
441
Benoît Ganne5527a782022-01-18 15:56:41 +0100442 if (PREDICT_FALSE (lb != b[0]))
Fan Zhangf5395782020-04-29 14:00:03 +0100443 {
444 /* is chained */
445 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
446 op->chunk_index = vec_len (ptd->chunks);
447 op->tag = vlib_buffer_get_tail (lb) - icv_sz;
Benoît Ganne02dfd292022-01-18 15:56:41 +0100448 esp_encrypt_chain_crypto (vm, ptd, sa0, b[0], lb, icv_sz,
449 crypto_start, crypto_len + icv_sz,
450 &op->n_chunks);
451 }
452 else
453 {
454 /* not chained */
455 op->src = op->dst = crypto_start;
456 op->len = crypto_len;
Fan Zhangf5395782020-04-29 14:00:03 +0100457 }
458 }
459
460 if (sa0->integ_op_id)
461 {
462 vnet_crypto_op_t *op;
463 vec_add2_aligned (integ_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
464 vnet_crypto_op_init (op, sa0->integ_op_id);
465 op->src = payload - iv_sz - sizeof (esp_header_t);
466 op->digest = payload + payload_len - icv_sz;
467 op->key_index = sa0->integ_key_index;
468 op->digest_len = icv_sz;
469 op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000470 op->user_data = bi;
Fan Zhangf5395782020-04-29 14:00:03 +0100471
472 if (lb != b[0])
473 {
474 /* is chained */
475 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
476 op->chunk_index = vec_len (ptd->chunks);
477 op->digest = vlib_buffer_get_tail (lb) - icv_sz;
478
479 esp_encrypt_chain_integ (vm, ptd, sa0, b[0], lb, icv_sz,
480 payload - iv_sz - sizeof (esp_header_t),
481 payload_len + iv_sz +
482 sizeof (esp_header_t), op->digest,
483 &op->n_chunks);
484 }
485 else if (ipsec_sa_is_set_USE_ESN (sa0))
486 {
Neale Ranns5b891102021-06-28 13:31:28 +0000487 u32 tmp = clib_net_to_host_u32 (seq_hi);
488 clib_memcpy_fast (op->digest, &tmp, sizeof (seq_hi));
Fan Zhangf5395782020-04-29 14:00:03 +0100489 op->len += sizeof (seq_hi);
490 }
491 }
492}
493
Neale Rannsfc811342021-02-26 10:35:33 +0000494static_always_inline void
495esp_prepare_async_frame (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
496 vnet_crypto_async_frame_t *async_frame,
497 ipsec_sa_t *sa, vlib_buffer_t *b, esp_header_t *esp,
498 u8 *payload, u32 payload_len, u8 iv_sz, u8 icv_sz,
499 u32 bi, u16 next, u32 hdr_len, u16 async_next,
500 vlib_buffer_t *lb)
Fan Zhangf5395782020-04-29 14:00:03 +0100501{
502 esp_post_data_t *post = esp_post_data (b);
503 u8 *tag, *iv, *aad = 0;
504 u8 flag = 0;
Benoît Ganne5527a782022-01-18 15:56:41 +0100505 const u32 key_index = sa->crypto_key_index;
Benoît Ganne02dfd292022-01-18 15:56:41 +0100506 i16 crypto_start_offset, integ_start_offset;
Fan Zhangf5395782020-04-29 14:00:03 +0100507 u16 crypto_total_len, integ_total_len;
508
Fan Zhang18f0e312020-10-19 13:08:34 +0100509 post->next_index = next;
Fan Zhangf5395782020-04-29 14:00:03 +0100510
511 /* crypto */
Benoît Ganne02dfd292022-01-18 15:56:41 +0100512 crypto_start_offset = integ_start_offset = payload - b->data;
Fan Zhangf5395782020-04-29 14:00:03 +0100513 crypto_total_len = integ_total_len = payload_len - icv_sz;
514 tag = payload + crypto_total_len;
515
Benoît Ganne02dfd292022-01-18 15:56:41 +0100516 /* generate the IV in front of the payload */
517 void *pkt_iv = esp_generate_iv (sa, payload, iv_sz);
518
Benoît Ganne490b9272021-01-22 18:03:09 +0100519 if (ipsec_sa_is_set_IS_CTR (sa))
Fan Zhangf5395782020-04-29 14:00:03 +0100520 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100521 /* construct nonce in a scratch space in front of the IP header */
Benoît Ganne02dfd292022-01-18 15:56:41 +0100522 esp_ctr_nonce_t *nonce =
523 (esp_ctr_nonce_t *) (pkt_iv - hdr_len - sizeof (*nonce));
Benoît Ganne490b9272021-01-22 18:03:09 +0100524 if (ipsec_sa_is_set_IS_AEAD (sa))
525 {
526 /* constuct aad in a scratch space in front of the nonce */
527 aad = (u8 *) nonce - sizeof (esp_aead_t);
Neale Ranns5b891102021-06-28 13:31:28 +0000528 esp_aad_fill (aad, esp, sa, sa->seq_hi);
Benoît Ganne84e66582023-03-10 17:33:03 +0100529 if (PREDICT_FALSE (ipsec_sa_is_set_IS_NULL_GMAC (sa)))
530 {
531 /* RFC-4543 ENCR_NULL_AUTH_AES_GMAC: IV is part of AAD */
532 crypto_start_offset -= iv_sz;
533 crypto_total_len += iv_sz;
534 }
Benoît Ganne490b9272021-01-22 18:03:09 +0100535 }
536 else
537 {
538 nonce->ctr = clib_host_to_net_u32 (1);
539 }
540
541 nonce->salt = sa->salt;
Benoît Ganne02dfd292022-01-18 15:56:41 +0100542 nonce->iv = *(u64 *) pkt_iv;
Benoît Ganne490b9272021-01-22 18:03:09 +0100543 iv = (u8 *) nonce;
Fan Zhangf5395782020-04-29 14:00:03 +0100544 }
Benoît Ganne490b9272021-01-22 18:03:09 +0100545 else
Fan Zhangf5395782020-04-29 14:00:03 +0100546 {
Benoît Ganne02dfd292022-01-18 15:56:41 +0100547 /* construct zero iv in front of the IP header */
548 iv = pkt_iv - hdr_len - iv_sz;
549 clib_memset_u8 (iv, 0, iv_sz);
550 /* include iv field in crypto */
551 crypto_start_offset -= iv_sz;
552 crypto_total_len += iv_sz;
Fan Zhangf5395782020-04-29 14:00:03 +0100553 }
554
Benoît Ganne490b9272021-01-22 18:03:09 +0100555 if (lb != b)
556 {
557 /* chain */
558 flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
559 tag = vlib_buffer_get_tail (lb) - icv_sz;
Benoît Ganne02dfd292022-01-18 15:56:41 +0100560 crypto_total_len = esp_encrypt_chain_crypto (
561 vm, ptd, sa, b, lb, icv_sz, b->data + crypto_start_offset,
562 crypto_total_len + icv_sz, 0);
Benoît Ganne490b9272021-01-22 18:03:09 +0100563 }
564
565 if (sa->integ_op_id)
566 {
Benoît Ganne02dfd292022-01-18 15:56:41 +0100567 integ_start_offset -= iv_sz + sizeof (esp_header_t);
Benoît Ganne490b9272021-01-22 18:03:09 +0100568 integ_total_len += iv_sz + sizeof (esp_header_t);
569
570 if (b != lb)
571 {
572 integ_total_len = esp_encrypt_chain_integ (
573 vm, ptd, sa, b, lb, icv_sz,
574 payload - iv_sz - sizeof (esp_header_t),
575 payload_len + iv_sz + sizeof (esp_header_t), tag, 0);
576 }
577 else if (ipsec_sa_is_set_USE_ESN (sa))
578 {
579 u32 seq_hi = clib_net_to_host_u32 (sa->seq_hi);
580 clib_memcpy_fast (tag, &seq_hi, sizeof (seq_hi));
581 integ_total_len += sizeof (seq_hi);
582 }
583 }
584
Neale Rannsfc811342021-02-26 10:35:33 +0000585 /* this always succeeds because we know the frame is not full */
586 vnet_crypto_async_add_to_frame (vm, async_frame, key_index, crypto_total_len,
587 integ_total_len - crypto_total_len,
588 crypto_start_offset, integ_start_offset, bi,
589 async_next, iv, tag, aad, flag);
Fan Zhangf5395782020-04-29 14:00:03 +0100590}
591
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200592always_inline uword
Neale Ranns4a58e492020-12-21 13:19:10 +0000593esp_encrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
594 vlib_frame_t *frame, vnet_link_t lt, int is_tun,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000595 u16 async_next_node)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597 ipsec_main_t *im = &ipsec_main;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100598 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
599 u32 *from = vlib_frame_vector_args (frame);
600 u32 n_left = frame->n_vectors;
601 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100602 u32 thread_index = vm->thread_index;
603 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
604 u32 current_sa_index = ~0, current_sa_packets = 0;
605 u32 current_sa_bytes = 0, spi = 0;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500606 u8 esp_align = 4, iv_sz = 0, icv_sz = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100607 ipsec_sa_t *sa0 = 0;
Matthew Smithff719392024-02-12 18:39:21 +0000608 u8 sa_drop_no_crypto = 0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000609 vlib_buffer_t *lb;
610 vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
611 vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
Neale Rannsfc811342021-02-26 10:35:33 +0000612 vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
Fan Zhangf5395782020-04-29 14:00:03 +0100613 int is_async = im->async_mode;
Neale Rannsfc811342021-02-26 10:35:33 +0000614 vnet_crypto_async_op_id_t async_op = ~0;
Neale Ranns4a58e492020-12-21 13:19:10 +0000615 u16 drop_next =
616 (lt == VNET_LINK_IP6 ? ESP_ENCRYPT_NEXT_DROP6 :
617 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_DROP4 :
618 ESP_ENCRYPT_NEXT_DROP_MPLS));
619 u16 handoff_next = (lt == VNET_LINK_IP6 ?
620 ESP_ENCRYPT_NEXT_HANDOFF6 :
621 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_HANDOFF4 :
622 ESP_ENCRYPT_NEXT_HANDOFF_MPLS));
Neale Rannsf16e9a52021-02-25 19:09:24 +0000623 vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
624 u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0;
Damjan Mariondd298e82022-10-12 16:02:18 +0200625 u16 n_async = 0;
626 u16 noop_nexts[VLIB_FRAME_SIZE], n_noop = 0;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000627 u32 sync_bi[VLIB_FRAME_SIZE];
628 u32 noop_bi[VLIB_FRAME_SIZE];
629 esp_encrypt_error_t err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630
Damjan Marionc59b9a22019-03-19 15:38:40 +0100631 vlib_get_buffers (vm, from, b, n_left);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000632
633 vec_reset_length (ptd->crypto_ops);
634 vec_reset_length (ptd->integ_ops);
635 vec_reset_length (ptd->chained_crypto_ops);
636 vec_reset_length (ptd->chained_integ_ops);
Neale Rannsfc811342021-02-26 10:35:33 +0000637 vec_reset_length (ptd->async_frames);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000638 vec_reset_length (ptd->chunks);
Neale Rannsfc811342021-02-26 10:35:33 +0000639 clib_memset (async_frames, 0, sizeof (async_frames));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100640
641 while (n_left > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700642 {
Zhiyong Yang1cff6432019-04-30 05:33:53 -0400643 u32 sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100644 dpo_id_t *dpo;
645 esp_header_t *esp;
646 u8 *payload, *next_hdr_ptr;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000647 u16 payload_len, payload_len_total, n_bufs;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400648 u32 hdr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649
Neale Rannsf16e9a52021-02-25 19:09:24 +0000650 err = ESP_ENCRYPT_ERROR_RX_PKTS;
651
Damjan Marionc59b9a22019-03-19 15:38:40 +0100652 if (n_left > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700653 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100654 u8 *p;
655 vlib_prefetch_buffer_header (b[2], LOAD);
656 p = vlib_buffer_get_current (b[1]);
Damjan Marionaf7fb042021-07-15 11:54:41 +0200657 clib_prefetch_load (p);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100658 p -= CLIB_CACHE_LINE_BYTES;
Damjan Marionaf7fb042021-07-15 11:54:41 +0200659 clib_prefetch_load (p);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000660 /* speculate that the trailer goes in the first buffer */
661 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]),
662 CLIB_CACHE_LINE_BYTES, LOAD);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700663 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664
Arthur de Kerhor2f4586d2021-09-22 14:53:24 +0200665 vnet_calc_checksums_inline (vm, b[0], b[0]->flags & VNET_BUFFER_F_IS_IP4,
666 b[0]->flags & VNET_BUFFER_F_IS_IP6);
667 vnet_calc_outer_checksums_inline (vm, b[0]);
668
Neale Ranns25edf142019-03-22 08:12:48 +0000669 if (is_tun)
670 {
671 /* we are on a ipsec tunnel's feature arc */
Neale Ranns28287212019-12-16 00:53:11 +0000672 vnet_buffer (b[0])->ipsec.sad_index =
673 sa_index0 = ipsec_tun_protect_get_sa_out
674 (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000675
676 if (PREDICT_FALSE (INDEX_INVALID == sa_index0))
677 {
678 err = ESP_ENCRYPT_ERROR_NO_PROTECTION;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100679 noop_nexts[n_noop] = drop_next;
680 b[0]->error = node->errors[err];
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000681 goto trace;
682 }
Neale Ranns25edf142019-03-22 08:12:48 +0000683 }
684 else
685 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
686
687 if (sa_index0 != current_sa_index)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100688 {
Damjan Marion21f265f2019-06-05 15:45:50 +0200689 if (current_sa_packets)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100690 vlib_increment_combined_counter (
691 &ipsec_sa_counters, thread_index, current_sa_index,
692 current_sa_packets, current_sa_bytes);
Damjan Marion21f265f2019-06-05 15:45:50 +0200693 current_sa_packets = current_sa_bytes = 0;
694
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000695 sa0 = ipsec_sa_get (sa_index0);
Matthew Smithdac9e562023-11-16 02:27:29 +0000696 current_sa_index = sa_index0;
Neale Ranns123b5eb2020-10-16 14:03:55 +0000697
Matthew Smithff719392024-02-12 18:39:21 +0000698 sa_drop_no_crypto = ((sa0->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
699 sa0->integ_alg == IPSEC_INTEG_ALG_NONE) &&
700 !ipsec_sa_is_set_NO_ALGO_NO_DROP (sa0));
701
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100702 vlib_prefetch_combined_counter (&ipsec_sa_counters, thread_index,
703 current_sa_index);
704
Neale Ranns123b5eb2020-10-16 14:03:55 +0000705 /* fetch the second cacheline ASAP */
Damjan Marionaf7fb042021-07-15 11:54:41 +0200706 clib_prefetch_load (sa0->cacheline1);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000707
Damjan Marionc59b9a22019-03-19 15:38:40 +0100708 spi = clib_net_to_host_u32 (sa0->spi);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500709 esp_align = sa0->esp_block_align;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200710 icv_sz = sa0->integ_icv_size;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100711 iv_sz = sa0->crypto_iv_size;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000712 is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0);
Neale Rannsfc811342021-02-26 10:35:33 +0000713 }
Fan Zhangf5395782020-04-29 14:00:03 +0100714
Matthew Smithff719392024-02-12 18:39:21 +0000715 if (PREDICT_FALSE (sa_drop_no_crypto != 0))
716 {
717 err = ESP_ENCRYPT_ERROR_NO_ENCRYPTION;
718 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
719 noop_nexts, drop_next, sa_index0);
720 goto trace;
721 }
722
Benoît Ganne5527a782022-01-18 15:56:41 +0100723 if (PREDICT_FALSE ((u16) ~0 == sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +0000724 {
725 /* this is the first packet to use this SA, claim the SA
726 * for this thread. this could happen simultaneously on
727 * another thread */
Neale Ranns1a52d372021-02-04 11:33:32 +0000728 clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
Neale Rannsf62a8c02019-04-02 08:13:33 +0000729 ipsec_sa_assign_thread (thread_index));
730 }
731
Neale Ranns1a52d372021-02-04 11:33:32 +0000732 if (PREDICT_FALSE (thread_index != sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +0000733 {
Neale Rannsaa7d7662021-02-10 08:42:49 +0000734 vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000735 err = ESP_ENCRYPT_ERROR_HANDOFF;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100736 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
737 noop_nexts, handoff_next,
738 current_sa_index);
Neale Rannsf62a8c02019-04-02 08:13:33 +0000739 goto trace;
740 }
741
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000742 lb = b[0];
743 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
744 if (n_bufs == 0)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100745 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000746 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100747 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
748 noop_nexts, drop_next, current_sa_index);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100749 goto trace;
750 }
751
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000752 if (n_bufs > 1)
753 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000754 /* find last buffer in the chain */
755 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
756 lb = vlib_get_buffer (vm, lb->next_buffer);
757 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000758
Damjan Marionc59b9a22019-03-19 15:38:40 +0100759 if (PREDICT_FALSE (esp_seq_advance (sa0)))
760 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000761 err = ESP_ENCRYPT_ERROR_SEQ_CYCLED;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100762 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
763 noop_nexts, drop_next, current_sa_index);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100764 goto trace;
765 }
766
767 /* space for IV */
768 hdr_len = iv_sz;
769
Damjan Mariond709cbc2019-03-26 13:16:42 +0100770 if (ipsec_sa_is_set_IS_TUNNEL (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100771 {
772 payload = vlib_buffer_get_current (b[0]);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000773 next_hdr_ptr = esp_add_footer_and_icv (
Fan Zhangb8cb2232024-03-12 20:39:12 +0000774 vm, &lb, esp_align, icv_sz, buffer_data_size,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000775 vlib_buffer_length_in_chain (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000776 if (!next_hdr_ptr)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000777 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000778 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100779 esp_encrypt_set_next_index (b[0], node, thread_index, err,
780 n_noop, noop_nexts, drop_next,
781 current_sa_index);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000782 goto trace;
783 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000784 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000785 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000786 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100787
788 /* ESP header */
789 hdr_len += sizeof (*esp);
790 esp = (esp_header_t *) (payload - hdr_len);
791
792 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100793 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100794 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100795 hdr_len += sizeof (udp_header_t);
796 esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000797 payload_len_total + hdr_len);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100798 }
799
800 /* IP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100801 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100802 {
803 ip6_header_t *ip6;
804 u16 len = sizeof (ip6_header_t);
805 hdr_len += len;
806 ip6 = (ip6_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000807 clib_memcpy_fast (ip6, &sa0->ip6_hdr, sizeof (ip6_header_t));
808
Neale Ranns4a58e492020-12-21 13:19:10 +0000809 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000810 {
811 *next_hdr_ptr = IP_PROTOCOL_IPV6;
812 tunnel_encap_fixup_6o6 (sa0->tunnel_flags,
813 (const ip6_header_t *) payload,
814 ip6);
815 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000816 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000817 {
818 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
Neale Rannsa91cb452021-02-04 11:02:52 +0000819 tunnel_encap_fixup_4o6 (sa0->tunnel_flags, b[0],
820 (const ip4_header_t *) payload, ip6);
Neale Ranns041add72020-01-02 04:06:10 +0000821 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000822 else if (VNET_LINK_MPLS == lt)
823 {
824 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
825 tunnel_encap_fixup_mplso6 (
Neale Rannsa91cb452021-02-04 11:02:52 +0000826 sa0->tunnel_flags, b[0],
827 (const mpls_unicast_header_t *) payload, ip6);
Neale Ranns4a58e492020-12-21 13:19:10 +0000828 }
829 else
830 ASSERT (0);
831
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000832 len = payload_len_total + hdr_len - len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100833 ip6->payload_length = clib_net_to_host_u16 (len);
Neale Ranns45d6d832021-01-19 13:38:47 +0000834 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Damjan Marionc98275f2019-03-06 14:05:01 +0100835 }
836 else
837 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100838 ip4_header_t *ip4;
839 u16 len = sizeof (ip4_header_t);
840 hdr_len += len;
841 ip4 = (ip4_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000842 clib_memcpy_fast (ip4, &sa0->ip4_hdr, sizeof (ip4_header_t));
843
Neale Ranns4a58e492020-12-21 13:19:10 +0000844 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000845 {
846 *next_hdr_ptr = IP_PROTOCOL_IPV6;
847 tunnel_encap_fixup_6o4_w_chksum (sa0->tunnel_flags,
848 (const ip6_header_t *)
849 payload, ip4);
850 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000851 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000852 {
853 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
854 tunnel_encap_fixup_4o4_w_chksum (sa0->tunnel_flags,
855 (const ip4_header_t *)
856 payload, ip4);
857 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000858 else if (VNET_LINK_MPLS == lt)
859 {
860 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
861 tunnel_encap_fixup_mplso4_w_chksum (
862 sa0->tunnel_flags, (const mpls_unicast_header_t *) payload,
863 ip4);
864 }
865 else
866 ASSERT (0);
867
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000868 len = payload_len_total + hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100869 esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100870 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100871
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000872 dpo = &sa0->dpo;
Neale Ranns25edf142019-03-22 08:12:48 +0000873 if (!is_tun)
874 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000875 sync_next[0] = dpo->dpoi_next_node;
Neale Ranns25edf142019-03-22 08:12:48 +0000876 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
877 }
Neale Ranns4ec36c52020-03-31 09:21:29 -0400878 else
Neale Rannsf16e9a52021-02-25 19:09:24 +0000879 sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000880 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Damjan Marionc98275f2019-03-06 14:05:01 +0100881 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100882 else /* transport mode */
Damjan Marionc98275f2019-03-06 14:05:01 +0100883 {
Ole Troan03092c12021-11-23 15:55:39 +0100884 u8 *l2_hdr, l2_len, *ip_hdr;
885 u16 ip_len;
Neale Ranns02950402019-12-20 00:54:57 +0000886 ip6_ext_header_t *ext_hdr;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100887 udp_header_t *udp = 0;
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400888 u16 udp_len = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100889 u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
Damjan Marionc98275f2019-03-06 14:05:01 +0100890
Ole Troan03092c12021-11-23 15:55:39 +0100891 /*
892 * Get extension header chain length. It might be longer than the
893 * buffer's pre_data area.
894 */
Neale Ranns4a58e492020-12-21 13:19:10 +0000895 ip_len =
896 (VNET_LINK_IP6 == lt ?
897 esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
898 ip4_header_bytes ((ip4_header_t *) old_ip_hdr));
Ole Troan03092c12021-11-23 15:55:39 +0100899 if ((old_ip_hdr - ip_len) < &b[0]->pre_data[0])
900 {
901 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100902 esp_encrypt_set_next_index (b[0], node, thread_index, err,
903 n_noop, noop_nexts, drop_next,
904 current_sa_index);
Ole Troan03092c12021-11-23 15:55:39 +0100905 goto trace;
906 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100907
Damjan Marionc59b9a22019-03-19 15:38:40 +0100908 vlib_buffer_advance (b[0], ip_len);
909 payload = vlib_buffer_get_current (b[0]);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000910 next_hdr_ptr = esp_add_footer_and_icv (
Fan Zhangb8cb2232024-03-12 20:39:12 +0000911 vm, &lb, esp_align, icv_sz, buffer_data_size,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000912 vlib_buffer_length_in_chain (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000913 if (!next_hdr_ptr)
Fan Zhang18f0e312020-10-19 13:08:34 +0100914 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000915 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100916 esp_encrypt_set_next_index (b[0], node, thread_index, err,
917 n_noop, noop_nexts, drop_next,
918 current_sa_index);
Fan Zhang18f0e312020-10-19 13:08:34 +0100919 goto trace;
920 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000921
922 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000923 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000924 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100925
926 /* ESP header */
927 hdr_len += sizeof (*esp);
928 esp = (esp_header_t *) (payload - hdr_len);
929
930 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100931 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100932 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100933 hdr_len += sizeof (udp_header_t);
934 udp = (udp_header_t *) (payload - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100935 }
936
Damjan Marionc59b9a22019-03-19 15:38:40 +0100937 /* IP header */
938 hdr_len += ip_len;
939 ip_hdr = payload - hdr_len;
940
941 /* L2 header */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800942 if (!is_tun)
943 {
944 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
945 hdr_len += l2_len;
946 l2_hdr = payload - hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100947
Neale Rannsc87b66c2019-02-07 07:26:12 -0800948 /* copy l2 and ip header */
949 clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
950 }
951 else
952 l2_len = 0;
953
Matthew Smith6f1eb482022-08-09 22:19:38 +0000954 u16 len;
955 len = payload_len_total + hdr_len - l2_len;
956
Neale Ranns4a58e492020-12-21 13:19:10 +0000957 if (VNET_LINK_IP6 == lt)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100958 {
Neale Ranns02950402019-12-20 00:54:57 +0000959 ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
960 if (PREDICT_TRUE (NULL == ext_hdr))
961 {
962 *next_hdr_ptr = ip6->protocol;
Matthew Smith6f1eb482022-08-09 22:19:38 +0000963 ip6->protocol =
964 (udp) ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
Neale Ranns02950402019-12-20 00:54:57 +0000965 }
966 else
967 {
968 *next_hdr_ptr = ext_hdr->next_hdr;
Matthew Smith6f1eb482022-08-09 22:19:38 +0000969 ext_hdr->next_hdr =
970 (udp) ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
Neale Ranns02950402019-12-20 00:54:57 +0000971 }
Neale Rannsd207fd72019-04-18 17:18:12 -0700972 ip6->payload_length =
Matthew Smith6f1eb482022-08-09 22:19:38 +0000973 clib_host_to_net_u16 (len - sizeof (ip6_header_t));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100974 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000975 else if (VNET_LINK_IP4 == lt)
Damjan Marionc98275f2019-03-06 14:05:01 +0100976 {
Neale Ranns02950402019-12-20 00:54:57 +0000977 ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100978 *next_hdr_ptr = ip4->protocol;
Matthew Smith6f1eb482022-08-09 22:19:38 +0000979 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1,
980 (udp != NULL));
Damjan Marionc98275f2019-03-06 14:05:01 +0100981 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100982
Neale Ranns02950402019-12-20 00:54:57 +0000983 clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
984
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400985 if (udp)
986 {
Matthew Smith6f1eb482022-08-09 22:19:38 +0000987 udp_len = len - ip_len;
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400988 esp_fill_udp_hdr (sa0, udp, udp_len);
989 }
990
Neale Rannsf16e9a52021-02-25 19:09:24 +0000991 sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Damjan Marionc98275f2019-03-06 14:05:01 +0100992 }
993
PiotrX Kleskifdca4dd2020-05-05 14:14:22 +0200994 if (lb != b[0])
995 {
996 crypto_ops = &ptd->chained_crypto_ops;
997 integ_ops = &ptd->chained_integ_ops;
998 }
999 else
1000 {
1001 crypto_ops = &ptd->crypto_ops;
1002 integ_ops = &ptd->integ_ops;
1003 }
1004
Damjan Marionc59b9a22019-03-19 15:38:40 +01001005 esp->spi = spi;
1006 esp->seq = clib_net_to_host_u32 (sa0->seq);
Damjan Marionc98275f2019-03-06 14:05:01 +01001007
Fan Zhangf5395782020-04-29 14:00:03 +01001008 if (is_async)
Matthew Smith51d56ba2021-06-04 09:18:37 -05001009 {
1010 async_op = sa0->crypto_async_enc_op_id;
1011
1012 /* get a frame for this op if we don't yet have one or it's full
1013 */
1014 if (NULL == async_frames[async_op] ||
1015 vnet_crypto_async_frame_is_full (async_frames[async_op]))
1016 {
1017 async_frames[async_op] =
1018 vnet_crypto_async_get_frame (vm, async_op);
gaoginskxf441b5d2021-06-07 12:07:01 +01001019
1020 if (PREDICT_FALSE (!async_frames[async_op]))
1021 {
1022 err = ESP_ENCRYPT_ERROR_NO_AVAIL_FRAME;
1023 esp_encrypt_set_next_index (b[0], node, thread_index, err,
1024 n_noop, noop_nexts, drop_next,
1025 current_sa_index);
1026 goto trace;
1027 }
1028
Matthew Smith51d56ba2021-06-04 09:18:37 -05001029 /* Save the frame to the list we'll submit at the end */
1030 vec_add1 (ptd->async_frames, async_frames[async_op]);
1031 }
1032
1033 esp_prepare_async_frame (vm, ptd, async_frames[async_op], sa0, b[0],
1034 esp, payload, payload_len, iv_sz, icv_sz,
1035 from[b - bufs], sync_next[0], hdr_len,
1036 async_next_node, lb);
1037 }
Fan Zhangf5395782020-04-29 14:00:03 +01001038 else
Neale Ranns5b891102021-06-28 13:31:28 +00001039 esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, sa0->seq_hi,
1040 payload, payload_len, iv_sz, icv_sz, n_sync, b,
1041 lb, hdr_len, esp);
Damjan Marionc98275f2019-03-06 14:05:01 +01001042
Damjan Marionc59b9a22019-03-19 15:38:40 +01001043 vlib_buffer_advance (b[0], 0LL - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +01001044
Damjan Marionc59b9a22019-03-19 15:38:40 +01001045 current_sa_packets += 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001046 current_sa_bytes += payload_len_total;
Damjan Marionc59b9a22019-03-19 15:38:40 +01001047
1048 trace:
1049 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
Damjan Marionc98275f2019-03-06 14:05:01 +01001050 {
Damjan Marionc59b9a22019-03-19 15:38:40 +01001051 esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
1052 sizeof (*tr));
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00001053 if (INDEX_INVALID == sa_index0)
1054 clib_memset_u8 (tr, 0xff, sizeof (*tr));
1055 else
1056 {
1057 tr->sa_index = sa_index0;
1058 tr->spi = sa0->spi;
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00001059 tr->seq = sa0->seq;
1060 tr->sa_seq_hi = sa0->seq_hi;
1061 tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
1062 tr->crypto_alg = sa0->crypto_alg;
1063 tr->integ_alg = sa0->integ_alg;
1064 }
Damjan Marionc98275f2019-03-06 14:05:01 +01001065 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001066
Damjan Marionc98275f2019-03-06 14:05:01 +01001067 /* next */
Neale Rannsf16e9a52021-02-25 19:09:24 +00001068 if (ESP_ENCRYPT_ERROR_RX_PKTS != err)
1069 {
1070 noop_bi[n_noop] = from[b - bufs];
1071 n_noop++;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001072 }
1073 else if (!is_async)
1074 {
1075 sync_bi[n_sync] = from[b - bufs];
1076 sync_bufs[n_sync] = b[0];
1077 n_sync++;
1078 sync_next++;
1079 }
1080 else
1081 {
1082 n_async++;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001083 }
Damjan Marionc59b9a22019-03-19 15:38:40 +01001084 n_left -= 1;
Damjan Marionc59b9a22019-03-19 15:38:40 +01001085 b += 1;
Damjan Marionc98275f2019-03-06 14:05:01 +01001086 }
1087
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00001088 if (INDEX_INVALID != current_sa_index)
1089 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1090 current_sa_index, current_sa_packets,
1091 current_sa_bytes);
Neale Rannsf16e9a52021-02-25 19:09:24 +00001092 if (n_sync)
Fan Zhangf5395782020-04-29 14:00:03 +01001093 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001094 esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts,
1095 drop_next);
1096 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs,
1097 sync_nexts, ptd->chunks, drop_next);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001098
Neale Rannsf16e9a52021-02-25 19:09:24 +00001099 esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts,
1100 drop_next);
1101 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs,
1102 sync_nexts, ptd->chunks, drop_next);
Neale Rannsfc811342021-02-26 10:35:33 +00001103
Neale Rannsf16e9a52021-02-25 19:09:24 +00001104 vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync);
Fan Zhangf5395782020-04-29 14:00:03 +01001105 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001106 if (n_async)
Fan Zhangf5395782020-04-29 14:00:03 +01001107 {
Neale Rannsfc811342021-02-26 10:35:33 +00001108 /* submit all of the open frames */
1109 vnet_crypto_async_frame_t **async_frame;
1110
1111 vec_foreach (async_frame, ptd->async_frames)
Fan Zhang18f0e312020-10-19 13:08:34 +01001112 {
Neale Rannsfc811342021-02-26 10:35:33 +00001113 if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0)
1114 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001115 n_noop += esp_async_recycle_failed_submit (
1116 vm, *async_frame, node, ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR,
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001117 IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR, n_noop, noop_bi,
Xiaoming Jiang0c1454c2023-05-05 02:28:20 +00001118 noop_nexts, drop_next, true);
Neale Rannsfc811342021-02-26 10:35:33 +00001119 vnet_crypto_async_reset_frame (*async_frame);
1120 vnet_crypto_async_free_frame (vm, *async_frame);
1121 }
Fan Zhang18f0e312020-10-19 13:08:34 +01001122 }
Fan Zhangf5395782020-04-29 14:00:03 +01001123 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001124 if (n_noop)
1125 vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
1126
1127 vlib_node_increment_counter (vm, node->node_index, ESP_ENCRYPT_ERROR_RX_PKTS,
1128 frame->n_vectors);
Damjan Marionc59b9a22019-03-19 15:38:40 +01001129
Damjan Marionc59b9a22019-03-19 15:38:40 +01001130 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131}
1132
Fan Zhangf5395782020-04-29 14:00:03 +01001133always_inline uword
1134esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1135 vlib_frame_t * frame)
1136{
1137 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1138 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1139 u32 *from = vlib_frame_vector_args (frame);
1140 u32 n_left = frame->n_vectors;
1141
1142 vlib_get_buffers (vm, from, b, n_left);
1143
1144 if (n_left >= 4)
1145 {
1146 vlib_prefetch_buffer_header (b[0], LOAD);
1147 vlib_prefetch_buffer_header (b[1], LOAD);
1148 vlib_prefetch_buffer_header (b[2], LOAD);
1149 vlib_prefetch_buffer_header (b[3], LOAD);
1150 }
1151
1152 while (n_left > 8)
1153 {
1154 vlib_prefetch_buffer_header (b[4], LOAD);
1155 vlib_prefetch_buffer_header (b[5], LOAD);
1156 vlib_prefetch_buffer_header (b[6], LOAD);
1157 vlib_prefetch_buffer_header (b[7], LOAD);
1158
1159 next[0] = (esp_post_data (b[0]))->next_index;
1160 next[1] = (esp_post_data (b[1]))->next_index;
1161 next[2] = (esp_post_data (b[2]))->next_index;
1162 next[3] = (esp_post_data (b[3]))->next_index;
1163
1164 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
1165 {
1166 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
1167 {
1168 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1169 sizeof (*tr));
1170 tr->next_index = next[0];
1171 }
1172 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
1173 {
1174 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1],
1175 sizeof (*tr));
1176 tr->next_index = next[1];
1177 }
1178 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
1179 {
1180 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2],
1181 sizeof (*tr));
1182 tr->next_index = next[2];
1183 }
1184 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
1185 {
1186 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3],
1187 sizeof (*tr));
1188 tr->next_index = next[3];
1189 }
1190 }
1191
1192 b += 4;
1193 next += 4;
1194 n_left -= 4;
1195 }
1196
1197 while (n_left > 0)
1198 {
1199 next[0] = (esp_post_data (b[0]))->next_index;
1200 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1201 {
1202 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1203 sizeof (*tr));
1204 tr->next_index = next[0];
1205 }
1206
1207 b += 1;
1208 next += 1;
1209 n_left -= 1;
1210 }
1211
1212 vlib_node_increment_counter (vm, node->node_index,
1213 ESP_ENCRYPT_ERROR_POST_RX_PKTS,
1214 frame->n_vectors);
1215 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1216 return frame->n_vectors;
1217}
1218
Klement Sekerab8f35442018-10-29 13:38:19 +01001219VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
1220 vlib_node_runtime_t * node,
1221 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001222{
Neale Ranns4a58e492020-12-21 13:19:10 +00001223 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001224 esp_encrypt_async_next.esp4_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001225}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001227VLIB_REGISTER_NODE (esp4_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001228 .name = "esp4-encrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001229 .vector_size = sizeof (u32),
1230 .format_trace = format_esp_encrypt_trace,
1231 .type = VLIB_NODE_TYPE_INTERNAL,
1232
Neale Ranns93688d72022-08-09 03:34:51 +00001233 .n_errors = ESP_ENCRYPT_N_ERROR,
1234 .error_counters = esp_encrypt_error_counters,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001235
1236 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Ranns4a58e492020-12-21 13:19:10 +00001237 .next_nodes = { [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1238 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1239 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1240 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-handoff",
1241 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-handoff",
1242 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "error-drop",
1243 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output" },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001244};
1245
Fan Zhangf5395782020-04-29 14:00:03 +01001246VLIB_NODE_FN (esp4_encrypt_post_node) (vlib_main_t * vm,
1247 vlib_node_runtime_t * node,
1248 vlib_frame_t * from_frame)
1249{
1250 return esp_encrypt_post_inline (vm, node, from_frame);
1251}
1252
Fan Zhangf5395782020-04-29 14:00:03 +01001253VLIB_REGISTER_NODE (esp4_encrypt_post_node) = {
1254 .name = "esp4-encrypt-post",
1255 .vector_size = sizeof (u32),
1256 .format_trace = format_esp_post_encrypt_trace,
1257 .type = VLIB_NODE_TYPE_INTERNAL,
1258 .sibling_of = "esp4-encrypt",
1259
Neale Ranns93688d72022-08-09 03:34:51 +00001260 .n_errors = ESP_ENCRYPT_N_ERROR,
1261 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001262};
Fan Zhangf5395782020-04-29 14:00:03 +01001263
Klement Sekerab8f35442018-10-29 13:38:19 +01001264VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
1265 vlib_node_runtime_t * node,
1266 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001267{
Neale Ranns4a58e492020-12-21 13:19:10 +00001268 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001269 esp_encrypt_async_next.esp6_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001270}
1271
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001272VLIB_REGISTER_NODE (esp6_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001273 .name = "esp6-encrypt",
1274 .vector_size = sizeof (u32),
1275 .format_trace = format_esp_encrypt_trace,
1276 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001277 .sibling_of = "esp4-encrypt",
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001278
Neale Ranns93688d72022-08-09 03:34:51 +00001279 .n_errors = ESP_ENCRYPT_N_ERROR,
1280 .error_counters = esp_encrypt_error_counters,
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001281};
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001282
Fan Zhangf5395782020-04-29 14:00:03 +01001283VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm,
1284 vlib_node_runtime_t * node,
1285 vlib_frame_t * from_frame)
1286{
1287 return esp_encrypt_post_inline (vm, node, from_frame);
1288}
1289
Fan Zhangf5395782020-04-29 14:00:03 +01001290VLIB_REGISTER_NODE (esp6_encrypt_post_node) = {
1291 .name = "esp6-encrypt-post",
1292 .vector_size = sizeof (u32),
1293 .format_trace = format_esp_post_encrypt_trace,
1294 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001295 .sibling_of = "esp4-encrypt",
Fan Zhangf5395782020-04-29 14:00:03 +01001296
Neale Ranns93688d72022-08-09 03:34:51 +00001297 .n_errors = ESP_ENCRYPT_N_ERROR,
1298 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001299};
Fan Zhangf5395782020-04-29 14:00:03 +01001300
Neale Ranns25edf142019-03-22 08:12:48 +00001301VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
1302 vlib_node_runtime_t * node,
1303 vlib_frame_t * from_frame)
1304{
Neale Ranns4a58e492020-12-21 13:19:10 +00001305 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001306 esp_encrypt_async_next.esp4_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001307}
1308
Neale Ranns25edf142019-03-22 08:12:48 +00001309VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
1310 .name = "esp4-encrypt-tun",
1311 .vector_size = sizeof (u32),
1312 .format_trace = format_esp_encrypt_trace,
1313 .type = VLIB_NODE_TYPE_INTERNAL,
1314
Neale Ranns93688d72022-08-09 03:34:51 +00001315 .n_errors = ESP_ENCRYPT_N_ERROR,
1316 .error_counters = esp_encrypt_error_counters,
Neale Rannsd7603d92019-03-28 08:56:10 +00001317
Neale Rannsf62a8c02019-04-02 08:13:33 +00001318 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001319 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001320 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1321 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001322 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001323 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001324 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1325 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001326 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001327 },
Neale Ranns25edf142019-03-22 08:12:48 +00001328};
1329
Fan Zhangf5395782020-04-29 14:00:03 +01001330VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm,
1331 vlib_node_runtime_t * node,
1332 vlib_frame_t * from_frame)
1333{
1334 return esp_encrypt_post_inline (vm, node, from_frame);
1335}
1336
Fan Zhangf5395782020-04-29 14:00:03 +01001337VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = {
1338 .name = "esp4-encrypt-tun-post",
1339 .vector_size = sizeof (u32),
1340 .format_trace = format_esp_post_encrypt_trace,
1341 .type = VLIB_NODE_TYPE_INTERNAL,
1342 .sibling_of = "esp4-encrypt-tun",
1343
Neale Ranns93688d72022-08-09 03:34:51 +00001344 .n_errors = ESP_ENCRYPT_N_ERROR,
1345 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001346};
Neale Ranns25edf142019-03-22 08:12:48 +00001347
1348VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
1349 vlib_node_runtime_t * node,
1350 vlib_frame_t * from_frame)
1351{
Neale Ranns4a58e492020-12-21 13:19:10 +00001352 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001353 esp_encrypt_async_next.esp6_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001354}
1355
Neale Ranns25edf142019-03-22 08:12:48 +00001356VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
1357 .name = "esp6-encrypt-tun",
1358 .vector_size = sizeof (u32),
1359 .format_trace = format_esp_encrypt_trace,
1360 .type = VLIB_NODE_TYPE_INTERNAL,
1361
Neale Ranns93688d72022-08-09 03:34:51 +00001362 .n_errors = ESP_ENCRYPT_N_ERROR,
1363 .error_counters = esp_encrypt_error_counters,
Neale Rannsd7603d92019-03-28 08:56:10 +00001364
Neale Rannsf62a8c02019-04-02 08:13:33 +00001365 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001366 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001367 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1368 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001369 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1370 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001371 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001372 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001373 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001374 },
Neale Ranns25edf142019-03-22 08:12:48 +00001375};
1376
Neale Ranns25edf142019-03-22 08:12:48 +00001377
Fan Zhangf5395782020-04-29 14:00:03 +01001378VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm,
1379 vlib_node_runtime_t * node,
1380 vlib_frame_t * from_frame)
1381{
1382 return esp_encrypt_post_inline (vm, node, from_frame);
1383}
1384
Fan Zhangf5395782020-04-29 14:00:03 +01001385VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = {
1386 .name = "esp6-encrypt-tun-post",
1387 .vector_size = sizeof (u32),
1388 .format_trace = format_esp_post_encrypt_trace,
1389 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Ranns4a58e492020-12-21 13:19:10 +00001390 .sibling_of = "esp-mpls-encrypt-tun",
Fan Zhangf5395782020-04-29 14:00:03 +01001391
Neale Ranns93688d72022-08-09 03:34:51 +00001392 .n_errors = ESP_ENCRYPT_N_ERROR,
1393 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001394};
Fan Zhangf5395782020-04-29 14:00:03 +01001395
Neale Ranns4a58e492020-12-21 13:19:10 +00001396VLIB_NODE_FN (esp_mpls_encrypt_tun_node)
1397(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1398{
1399 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_MPLS, 1,
1400 esp_encrypt_async_next.esp_mpls_tun_post_next);
1401}
1402
1403VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_node) = {
1404 .name = "esp-mpls-encrypt-tun",
1405 .vector_size = sizeof (u32),
1406 .format_trace = format_esp_encrypt_trace,
1407 .type = VLIB_NODE_TYPE_INTERNAL,
1408
Neale Ranns93688d72022-08-09 03:34:51 +00001409 .n_errors = ESP_ENCRYPT_N_ERROR,
1410 .error_counters = esp_encrypt_error_counters,
Neale Ranns4a58e492020-12-21 13:19:10 +00001411
1412 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1413 .next_nodes = {
1414 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1415 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1416 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1417 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1418 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1419 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1420 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1421 },
1422};
1423
1424VLIB_NODE_FN (esp_mpls_encrypt_tun_post_node)
1425(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1426{
1427 return esp_encrypt_post_inline (vm, node, from_frame);
1428}
1429
1430VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_post_node) = {
1431 .name = "esp-mpls-encrypt-tun-post",
1432 .vector_size = sizeof (u32),
1433 .format_trace = format_esp_post_encrypt_trace,
1434 .type = VLIB_NODE_TYPE_INTERNAL,
1435 .sibling_of = "esp-mpls-encrypt-tun",
1436
Neale Ranns93688d72022-08-09 03:34:51 +00001437 .n_errors = ESP_ENCRYPT_N_ERROR,
1438 .error_counters = esp_encrypt_error_counters,
Neale Ranns4a58e492020-12-21 13:19:10 +00001439};
1440
Neale Ranns2d498302021-02-25 08:38:58 +00001441#ifndef CLIB_MARCH_VARIANT
1442
1443static clib_error_t *
1444esp_encrypt_init (vlib_main_t *vm)
1445{
1446 ipsec_main_t *im = &ipsec_main;
1447
1448 im->esp4_enc_fq_index =
1449 vlib_frame_queue_main_init (esp4_encrypt_node.index, 0);
1450 im->esp6_enc_fq_index =
1451 vlib_frame_queue_main_init (esp6_encrypt_node.index, 0);
1452 im->esp4_enc_tun_fq_index =
1453 vlib_frame_queue_main_init (esp4_encrypt_tun_node.index, 0);
1454 im->esp6_enc_tun_fq_index =
1455 vlib_frame_queue_main_init (esp6_encrypt_tun_node.index, 0);
1456 im->esp_mpls_enc_tun_fq_index =
1457 vlib_frame_queue_main_init (esp_mpls_encrypt_tun_node.index, 0);
1458
1459 return 0;
1460}
1461
1462VLIB_INIT_FUNCTION (esp_encrypt_init);
1463
1464#endif
1465
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001466/*
1467 * fd.io coding-style-patch-verification: ON
1468 *
1469 * Local Variables:
1470 * eval: (c-set-style "gnu")
1471 * End:
1472 */