blob: f6d1ecaed24cf0ac2d1142fe8c44353601e0b337 [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
Jeff Shaw26574dc2024-06-17 16:01:20 -0700592/* Per RFC6935 section 5, the UDP checksum must be computed when originating
593 * an IPv6 UDP packet. The default behavior may be overridden when conditions
594 * defined by RFC6936 are satisfied. This implementation does not satisfy all
595 * the conditions so the checksum must be computed.
596 */
597static_always_inline void
598set_ip6_udp_cksum_offload (vlib_buffer_t *b, i16 l3_hdr_offset,
599 i16 l4_hdr_offset)
600{
601 vnet_buffer (b)->l3_hdr_offset = l3_hdr_offset;
602 vnet_buffer (b)->l4_hdr_offset = l4_hdr_offset;
603 vnet_buffer_offload_flags_set (b, VNET_BUFFER_OFFLOAD_F_UDP_CKSUM);
604 b->flags |= (VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
605 VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
606}
607
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200608always_inline uword
Neale Ranns4a58e492020-12-21 13:19:10 +0000609esp_encrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
610 vlib_frame_t *frame, vnet_link_t lt, int is_tun,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000611 u16 async_next_node)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613 ipsec_main_t *im = &ipsec_main;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100614 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
615 u32 *from = vlib_frame_vector_args (frame);
616 u32 n_left = frame->n_vectors;
617 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100618 u32 thread_index = vm->thread_index;
619 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
620 u32 current_sa_index = ~0, current_sa_packets = 0;
621 u32 current_sa_bytes = 0, spi = 0;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500622 u8 esp_align = 4, iv_sz = 0, icv_sz = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100623 ipsec_sa_t *sa0 = 0;
Matthew Smithff719392024-02-12 18:39:21 +0000624 u8 sa_drop_no_crypto = 0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000625 vlib_buffer_t *lb;
626 vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
627 vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
Neale Rannsfc811342021-02-26 10:35:33 +0000628 vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
Fan Zhangf5395782020-04-29 14:00:03 +0100629 int is_async = im->async_mode;
Neale Rannsfc811342021-02-26 10:35:33 +0000630 vnet_crypto_async_op_id_t async_op = ~0;
Neale Ranns4a58e492020-12-21 13:19:10 +0000631 u16 drop_next =
632 (lt == VNET_LINK_IP6 ? ESP_ENCRYPT_NEXT_DROP6 :
633 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_DROP4 :
634 ESP_ENCRYPT_NEXT_DROP_MPLS));
635 u16 handoff_next = (lt == VNET_LINK_IP6 ?
636 ESP_ENCRYPT_NEXT_HANDOFF6 :
637 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_HANDOFF4 :
638 ESP_ENCRYPT_NEXT_HANDOFF_MPLS));
Neale Rannsf16e9a52021-02-25 19:09:24 +0000639 vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
640 u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0;
Damjan Mariondd298e82022-10-12 16:02:18 +0200641 u16 n_async = 0;
642 u16 noop_nexts[VLIB_FRAME_SIZE], n_noop = 0;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000643 u32 sync_bi[VLIB_FRAME_SIZE];
644 u32 noop_bi[VLIB_FRAME_SIZE];
645 esp_encrypt_error_t err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646
Damjan Marionc59b9a22019-03-19 15:38:40 +0100647 vlib_get_buffers (vm, from, b, n_left);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000648
649 vec_reset_length (ptd->crypto_ops);
650 vec_reset_length (ptd->integ_ops);
651 vec_reset_length (ptd->chained_crypto_ops);
652 vec_reset_length (ptd->chained_integ_ops);
Neale Rannsfc811342021-02-26 10:35:33 +0000653 vec_reset_length (ptd->async_frames);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000654 vec_reset_length (ptd->chunks);
Neale Rannsfc811342021-02-26 10:35:33 +0000655 clib_memset (async_frames, 0, sizeof (async_frames));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100656
657 while (n_left > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700658 {
Zhiyong Yang1cff6432019-04-30 05:33:53 -0400659 u32 sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100660 dpo_id_t *dpo;
661 esp_header_t *esp;
662 u8 *payload, *next_hdr_ptr;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000663 u16 payload_len, payload_len_total, n_bufs;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400664 u32 hdr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665
Neale Rannsf16e9a52021-02-25 19:09:24 +0000666 err = ESP_ENCRYPT_ERROR_RX_PKTS;
667
Damjan Marionc59b9a22019-03-19 15:38:40 +0100668 if (n_left > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700669 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100670 u8 *p;
671 vlib_prefetch_buffer_header (b[2], LOAD);
672 p = vlib_buffer_get_current (b[1]);
Damjan Marionaf7fb042021-07-15 11:54:41 +0200673 clib_prefetch_load (p);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100674 p -= CLIB_CACHE_LINE_BYTES;
Damjan Marionaf7fb042021-07-15 11:54:41 +0200675 clib_prefetch_load (p);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000676 /* speculate that the trailer goes in the first buffer */
677 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]),
678 CLIB_CACHE_LINE_BYTES, LOAD);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700679 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680
Arthur de Kerhor2f4586d2021-09-22 14:53:24 +0200681 vnet_calc_checksums_inline (vm, b[0], b[0]->flags & VNET_BUFFER_F_IS_IP4,
682 b[0]->flags & VNET_BUFFER_F_IS_IP6);
683 vnet_calc_outer_checksums_inline (vm, b[0]);
684
Neale Ranns25edf142019-03-22 08:12:48 +0000685 if (is_tun)
686 {
687 /* we are on a ipsec tunnel's feature arc */
Neale Ranns28287212019-12-16 00:53:11 +0000688 vnet_buffer (b[0])->ipsec.sad_index =
689 sa_index0 = ipsec_tun_protect_get_sa_out
690 (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000691
692 if (PREDICT_FALSE (INDEX_INVALID == sa_index0))
693 {
694 err = ESP_ENCRYPT_ERROR_NO_PROTECTION;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100695 noop_nexts[n_noop] = drop_next;
696 b[0]->error = node->errors[err];
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000697 goto trace;
698 }
Neale Ranns25edf142019-03-22 08:12:48 +0000699 }
700 else
701 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
702
703 if (sa_index0 != current_sa_index)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100704 {
Damjan Marion21f265f2019-06-05 15:45:50 +0200705 if (current_sa_packets)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100706 vlib_increment_combined_counter (
707 &ipsec_sa_counters, thread_index, current_sa_index,
708 current_sa_packets, current_sa_bytes);
Damjan Marion21f265f2019-06-05 15:45:50 +0200709 current_sa_packets = current_sa_bytes = 0;
710
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000711 sa0 = ipsec_sa_get (sa_index0);
Matthew Smithdac9e562023-11-16 02:27:29 +0000712 current_sa_index = sa_index0;
Neale Ranns123b5eb2020-10-16 14:03:55 +0000713
Matthew Smithff719392024-02-12 18:39:21 +0000714 sa_drop_no_crypto = ((sa0->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
715 sa0->integ_alg == IPSEC_INTEG_ALG_NONE) &&
716 !ipsec_sa_is_set_NO_ALGO_NO_DROP (sa0));
717
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100718 vlib_prefetch_combined_counter (&ipsec_sa_counters, thread_index,
719 current_sa_index);
720
Neale Ranns123b5eb2020-10-16 14:03:55 +0000721 /* fetch the second cacheline ASAP */
Damjan Marionaf7fb042021-07-15 11:54:41 +0200722 clib_prefetch_load (sa0->cacheline1);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000723
Damjan Marionc59b9a22019-03-19 15:38:40 +0100724 spi = clib_net_to_host_u32 (sa0->spi);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500725 esp_align = sa0->esp_block_align;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200726 icv_sz = sa0->integ_icv_size;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100727 iv_sz = sa0->crypto_iv_size;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000728 is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0);
Neale Rannsfc811342021-02-26 10:35:33 +0000729 }
Fan Zhangf5395782020-04-29 14:00:03 +0100730
Matthew Smithff719392024-02-12 18:39:21 +0000731 if (PREDICT_FALSE (sa_drop_no_crypto != 0))
732 {
733 err = ESP_ENCRYPT_ERROR_NO_ENCRYPTION;
734 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
735 noop_nexts, drop_next, sa_index0);
736 goto trace;
737 }
738
Benoît Ganne5527a782022-01-18 15:56:41 +0100739 if (PREDICT_FALSE ((u16) ~0 == sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +0000740 {
741 /* this is the first packet to use this SA, claim the SA
742 * for this thread. this could happen simultaneously on
743 * another thread */
Neale Ranns1a52d372021-02-04 11:33:32 +0000744 clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
Neale Rannsf62a8c02019-04-02 08:13:33 +0000745 ipsec_sa_assign_thread (thread_index));
746 }
747
Neale Ranns1a52d372021-02-04 11:33:32 +0000748 if (PREDICT_FALSE (thread_index != sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +0000749 {
Neale Rannsaa7d7662021-02-10 08:42:49 +0000750 vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000751 err = ESP_ENCRYPT_ERROR_HANDOFF;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100752 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
753 noop_nexts, handoff_next,
754 current_sa_index);
Neale Rannsf62a8c02019-04-02 08:13:33 +0000755 goto trace;
756 }
757
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000758 lb = b[0];
759 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
760 if (n_bufs == 0)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100761 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000762 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100763 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
764 noop_nexts, drop_next, current_sa_index);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100765 goto trace;
766 }
767
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000768 if (n_bufs > 1)
769 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000770 /* find last buffer in the chain */
771 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
772 lb = vlib_get_buffer (vm, lb->next_buffer);
773 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000774
Damjan Marionc59b9a22019-03-19 15:38:40 +0100775 if (PREDICT_FALSE (esp_seq_advance (sa0)))
776 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000777 err = ESP_ENCRYPT_ERROR_SEQ_CYCLED;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100778 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
779 noop_nexts, drop_next, current_sa_index);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100780 goto trace;
781 }
782
783 /* space for IV */
784 hdr_len = iv_sz;
785
Damjan Mariond709cbc2019-03-26 13:16:42 +0100786 if (ipsec_sa_is_set_IS_TUNNEL (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100787 {
788 payload = vlib_buffer_get_current (b[0]);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000789 next_hdr_ptr = esp_add_footer_and_icv (
Fan Zhangb8cb2232024-03-12 20:39:12 +0000790 vm, &lb, esp_align, icv_sz, buffer_data_size,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000791 vlib_buffer_length_in_chain (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000792 if (!next_hdr_ptr)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000793 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000794 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100795 esp_encrypt_set_next_index (b[0], node, thread_index, err,
796 n_noop, noop_nexts, drop_next,
797 current_sa_index);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000798 goto trace;
799 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000800 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000801 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000802 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100803
804 /* ESP header */
805 hdr_len += sizeof (*esp);
806 esp = (esp_header_t *) (payload - hdr_len);
807
808 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100809 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100810 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100811 hdr_len += sizeof (udp_header_t);
812 esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000813 payload_len_total + hdr_len);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100814 }
815
816 /* IP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100817 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100818 {
819 ip6_header_t *ip6;
820 u16 len = sizeof (ip6_header_t);
821 hdr_len += len;
822 ip6 = (ip6_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000823 clib_memcpy_fast (ip6, &sa0->ip6_hdr, sizeof (ip6_header_t));
824
Neale Ranns4a58e492020-12-21 13:19:10 +0000825 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000826 {
827 *next_hdr_ptr = IP_PROTOCOL_IPV6;
828 tunnel_encap_fixup_6o6 (sa0->tunnel_flags,
829 (const ip6_header_t *) payload,
830 ip6);
831 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000832 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000833 {
834 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
Neale Rannsa91cb452021-02-04 11:02:52 +0000835 tunnel_encap_fixup_4o6 (sa0->tunnel_flags, b[0],
836 (const ip4_header_t *) payload, ip6);
Neale Ranns041add72020-01-02 04:06:10 +0000837 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000838 else if (VNET_LINK_MPLS == lt)
839 {
840 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
841 tunnel_encap_fixup_mplso6 (
Neale Rannsa91cb452021-02-04 11:02:52 +0000842 sa0->tunnel_flags, b[0],
843 (const mpls_unicast_header_t *) payload, ip6);
Neale Ranns4a58e492020-12-21 13:19:10 +0000844 }
845 else
846 ASSERT (0);
847
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000848 len = payload_len_total + hdr_len - len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100849 ip6->payload_length = clib_net_to_host_u16 (len);
Neale Ranns45d6d832021-01-19 13:38:47 +0000850 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Damjan Marionc98275f2019-03-06 14:05:01 +0100851 }
852 else
853 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100854 ip4_header_t *ip4;
855 u16 len = sizeof (ip4_header_t);
856 hdr_len += len;
857 ip4 = (ip4_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000858 clib_memcpy_fast (ip4, &sa0->ip4_hdr, sizeof (ip4_header_t));
859
Neale Ranns4a58e492020-12-21 13:19:10 +0000860 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000861 {
862 *next_hdr_ptr = IP_PROTOCOL_IPV6;
863 tunnel_encap_fixup_6o4_w_chksum (sa0->tunnel_flags,
864 (const ip6_header_t *)
865 payload, ip4);
866 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000867 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000868 {
869 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
870 tunnel_encap_fixup_4o4_w_chksum (sa0->tunnel_flags,
871 (const ip4_header_t *)
872 payload, ip4);
873 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000874 else if (VNET_LINK_MPLS == lt)
875 {
876 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
877 tunnel_encap_fixup_mplso4_w_chksum (
878 sa0->tunnel_flags, (const mpls_unicast_header_t *) payload,
879 ip4);
880 }
881 else
882 ASSERT (0);
883
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000884 len = payload_len_total + hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100885 esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100886 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100887
Jeff Shaw26574dc2024-06-17 16:01:20 -0700888 if (ipsec_sa_is_set_UDP_ENCAP (sa0) &&
889 ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
890 {
891 i16 l3_off = b[0]->current_data - hdr_len;
892 i16 l4_off = l3_off + sizeof (ip6_header_t);
893
894 set_ip6_udp_cksum_offload (b[0], l3_off, l4_off);
895 }
896
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000897 dpo = &sa0->dpo;
Neale Ranns25edf142019-03-22 08:12:48 +0000898 if (!is_tun)
899 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000900 sync_next[0] = dpo->dpoi_next_node;
Neale Ranns25edf142019-03-22 08:12:48 +0000901 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
902 }
Neale Ranns4ec36c52020-03-31 09:21:29 -0400903 else
Neale Rannsf16e9a52021-02-25 19:09:24 +0000904 sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000905 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Damjan Marionc98275f2019-03-06 14:05:01 +0100906 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100907 else /* transport mode */
Damjan Marionc98275f2019-03-06 14:05:01 +0100908 {
Ole Troan03092c12021-11-23 15:55:39 +0100909 u8 *l2_hdr, l2_len, *ip_hdr;
910 u16 ip_len;
Neale Ranns02950402019-12-20 00:54:57 +0000911 ip6_ext_header_t *ext_hdr;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100912 udp_header_t *udp = 0;
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400913 u16 udp_len = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100914 u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
Damjan Marionc98275f2019-03-06 14:05:01 +0100915
Ole Troan03092c12021-11-23 15:55:39 +0100916 /*
917 * Get extension header chain length. It might be longer than the
918 * buffer's pre_data area.
919 */
Neale Ranns4a58e492020-12-21 13:19:10 +0000920 ip_len =
921 (VNET_LINK_IP6 == lt ?
922 esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
923 ip4_header_bytes ((ip4_header_t *) old_ip_hdr));
Ole Troan03092c12021-11-23 15:55:39 +0100924 if ((old_ip_hdr - ip_len) < &b[0]->pre_data[0])
925 {
926 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100927 esp_encrypt_set_next_index (b[0], node, thread_index, err,
928 n_noop, noop_nexts, drop_next,
929 current_sa_index);
Ole Troan03092c12021-11-23 15:55:39 +0100930 goto trace;
931 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100932
Damjan Marionc59b9a22019-03-19 15:38:40 +0100933 vlib_buffer_advance (b[0], ip_len);
934 payload = vlib_buffer_get_current (b[0]);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000935 next_hdr_ptr = esp_add_footer_and_icv (
Fan Zhangb8cb2232024-03-12 20:39:12 +0000936 vm, &lb, esp_align, icv_sz, buffer_data_size,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000937 vlib_buffer_length_in_chain (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000938 if (!next_hdr_ptr)
Fan Zhang18f0e312020-10-19 13:08:34 +0100939 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000940 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100941 esp_encrypt_set_next_index (b[0], node, thread_index, err,
942 n_noop, noop_nexts, drop_next,
943 current_sa_index);
Fan Zhang18f0e312020-10-19 13:08:34 +0100944 goto trace;
945 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000946
947 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000948 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000949 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100950
951 /* ESP header */
952 hdr_len += sizeof (*esp);
953 esp = (esp_header_t *) (payload - hdr_len);
954
955 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100956 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100957 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100958 hdr_len += sizeof (udp_header_t);
959 udp = (udp_header_t *) (payload - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100960 }
961
Damjan Marionc59b9a22019-03-19 15:38:40 +0100962 /* IP header */
963 hdr_len += ip_len;
964 ip_hdr = payload - hdr_len;
965
966 /* L2 header */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800967 if (!is_tun)
968 {
969 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
970 hdr_len += l2_len;
971 l2_hdr = payload - hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100972
Neale Rannsc87b66c2019-02-07 07:26:12 -0800973 /* copy l2 and ip header */
974 clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
975 }
976 else
977 l2_len = 0;
978
Matthew Smith6f1eb482022-08-09 22:19:38 +0000979 u16 len;
980 len = payload_len_total + hdr_len - l2_len;
981
Neale Ranns4a58e492020-12-21 13:19:10 +0000982 if (VNET_LINK_IP6 == lt)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100983 {
Neale Ranns02950402019-12-20 00:54:57 +0000984 ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
985 if (PREDICT_TRUE (NULL == ext_hdr))
986 {
987 *next_hdr_ptr = ip6->protocol;
Matthew Smith6f1eb482022-08-09 22:19:38 +0000988 ip6->protocol =
989 (udp) ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
Neale Ranns02950402019-12-20 00:54:57 +0000990 }
991 else
992 {
993 *next_hdr_ptr = ext_hdr->next_hdr;
Matthew Smith6f1eb482022-08-09 22:19:38 +0000994 ext_hdr->next_hdr =
995 (udp) ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
Neale Ranns02950402019-12-20 00:54:57 +0000996 }
Neale Rannsd207fd72019-04-18 17:18:12 -0700997 ip6->payload_length =
Matthew Smith6f1eb482022-08-09 22:19:38 +0000998 clib_host_to_net_u16 (len - sizeof (ip6_header_t));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100999 }
Neale Ranns4a58e492020-12-21 13:19:10 +00001000 else if (VNET_LINK_IP4 == lt)
Damjan Marionc98275f2019-03-06 14:05:01 +01001001 {
Neale Ranns02950402019-12-20 00:54:57 +00001002 ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
Damjan Marionc59b9a22019-03-19 15:38:40 +01001003 *next_hdr_ptr = ip4->protocol;
Matthew Smith6f1eb482022-08-09 22:19:38 +00001004 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1,
1005 (udp != NULL));
Damjan Marionc98275f2019-03-06 14:05:01 +01001006 }
Damjan Marionc98275f2019-03-06 14:05:01 +01001007
Neale Ranns02950402019-12-20 00:54:57 +00001008 clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
1009
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -04001010 if (udp)
1011 {
Matthew Smith6f1eb482022-08-09 22:19:38 +00001012 udp_len = len - ip_len;
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -04001013 esp_fill_udp_hdr (sa0, udp, udp_len);
1014 }
1015
Jeff Shaw26574dc2024-06-17 16:01:20 -07001016 if (udp && (VNET_LINK_IP6 == lt))
1017 {
1018 i16 l3_off = b[0]->current_data - hdr_len + l2_len;
1019 i16 l4_off = l3_off + ip_len;
1020
1021 set_ip6_udp_cksum_offload (b[0], l3_off, l4_off);
1022 }
1023
Neale Rannsf16e9a52021-02-25 19:09:24 +00001024 sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Damjan Marionc98275f2019-03-06 14:05:01 +01001025 }
1026
PiotrX Kleskifdca4dd2020-05-05 14:14:22 +02001027 if (lb != b[0])
1028 {
1029 crypto_ops = &ptd->chained_crypto_ops;
1030 integ_ops = &ptd->chained_integ_ops;
1031 }
1032 else
1033 {
1034 crypto_ops = &ptd->crypto_ops;
1035 integ_ops = &ptd->integ_ops;
1036 }
1037
Damjan Marionc59b9a22019-03-19 15:38:40 +01001038 esp->spi = spi;
1039 esp->seq = clib_net_to_host_u32 (sa0->seq);
Damjan Marionc98275f2019-03-06 14:05:01 +01001040
Fan Zhangf5395782020-04-29 14:00:03 +01001041 if (is_async)
Matthew Smith51d56ba2021-06-04 09:18:37 -05001042 {
1043 async_op = sa0->crypto_async_enc_op_id;
1044
1045 /* get a frame for this op if we don't yet have one or it's full
1046 */
1047 if (NULL == async_frames[async_op] ||
1048 vnet_crypto_async_frame_is_full (async_frames[async_op]))
1049 {
1050 async_frames[async_op] =
1051 vnet_crypto_async_get_frame (vm, async_op);
gaoginskxf441b5d2021-06-07 12:07:01 +01001052
1053 if (PREDICT_FALSE (!async_frames[async_op]))
1054 {
1055 err = ESP_ENCRYPT_ERROR_NO_AVAIL_FRAME;
1056 esp_encrypt_set_next_index (b[0], node, thread_index, err,
1057 n_noop, noop_nexts, drop_next,
1058 current_sa_index);
1059 goto trace;
1060 }
1061
Matthew Smith51d56ba2021-06-04 09:18:37 -05001062 /* Save the frame to the list we'll submit at the end */
1063 vec_add1 (ptd->async_frames, async_frames[async_op]);
1064 }
1065
1066 esp_prepare_async_frame (vm, ptd, async_frames[async_op], sa0, b[0],
1067 esp, payload, payload_len, iv_sz, icv_sz,
1068 from[b - bufs], sync_next[0], hdr_len,
1069 async_next_node, lb);
1070 }
Fan Zhangf5395782020-04-29 14:00:03 +01001071 else
Neale Ranns5b891102021-06-28 13:31:28 +00001072 esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, sa0->seq_hi,
1073 payload, payload_len, iv_sz, icv_sz, n_sync, b,
1074 lb, hdr_len, esp);
Damjan Marionc98275f2019-03-06 14:05:01 +01001075
Damjan Marionc59b9a22019-03-19 15:38:40 +01001076 vlib_buffer_advance (b[0], 0LL - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +01001077
Damjan Marionc59b9a22019-03-19 15:38:40 +01001078 current_sa_packets += 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001079 current_sa_bytes += payload_len_total;
Damjan Marionc59b9a22019-03-19 15:38:40 +01001080
1081 trace:
1082 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
Damjan Marionc98275f2019-03-06 14:05:01 +01001083 {
Damjan Marionc59b9a22019-03-19 15:38:40 +01001084 esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
1085 sizeof (*tr));
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00001086 if (INDEX_INVALID == sa_index0)
1087 clib_memset_u8 (tr, 0xff, sizeof (*tr));
1088 else
1089 {
1090 tr->sa_index = sa_index0;
1091 tr->spi = sa0->spi;
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00001092 tr->seq = sa0->seq;
1093 tr->sa_seq_hi = sa0->seq_hi;
1094 tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
1095 tr->crypto_alg = sa0->crypto_alg;
1096 tr->integ_alg = sa0->integ_alg;
1097 }
Damjan Marionc98275f2019-03-06 14:05:01 +01001098 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001099
Damjan Marionc98275f2019-03-06 14:05:01 +01001100 /* next */
Neale Rannsf16e9a52021-02-25 19:09:24 +00001101 if (ESP_ENCRYPT_ERROR_RX_PKTS != err)
1102 {
1103 noop_bi[n_noop] = from[b - bufs];
1104 n_noop++;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001105 }
1106 else if (!is_async)
1107 {
1108 sync_bi[n_sync] = from[b - bufs];
1109 sync_bufs[n_sync] = b[0];
1110 n_sync++;
1111 sync_next++;
1112 }
1113 else
1114 {
1115 n_async++;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001116 }
Damjan Marionc59b9a22019-03-19 15:38:40 +01001117 n_left -= 1;
Damjan Marionc59b9a22019-03-19 15:38:40 +01001118 b += 1;
Damjan Marionc98275f2019-03-06 14:05:01 +01001119 }
1120
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00001121 if (INDEX_INVALID != current_sa_index)
1122 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1123 current_sa_index, current_sa_packets,
1124 current_sa_bytes);
Neale Rannsf16e9a52021-02-25 19:09:24 +00001125 if (n_sync)
Fan Zhangf5395782020-04-29 14:00:03 +01001126 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001127 esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts,
1128 drop_next);
1129 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs,
1130 sync_nexts, ptd->chunks, drop_next);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001131
Neale Rannsf16e9a52021-02-25 19:09:24 +00001132 esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts,
1133 drop_next);
1134 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs,
1135 sync_nexts, ptd->chunks, drop_next);
Neale Rannsfc811342021-02-26 10:35:33 +00001136
Neale Rannsf16e9a52021-02-25 19:09:24 +00001137 vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync);
Fan Zhangf5395782020-04-29 14:00:03 +01001138 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001139 if (n_async)
Fan Zhangf5395782020-04-29 14:00:03 +01001140 {
Neale Rannsfc811342021-02-26 10:35:33 +00001141 /* submit all of the open frames */
1142 vnet_crypto_async_frame_t **async_frame;
1143
1144 vec_foreach (async_frame, ptd->async_frames)
Fan Zhang18f0e312020-10-19 13:08:34 +01001145 {
Neale Rannsfc811342021-02-26 10:35:33 +00001146 if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0)
1147 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001148 n_noop += esp_async_recycle_failed_submit (
1149 vm, *async_frame, node, ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR,
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001150 IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR, n_noop, noop_bi,
Xiaoming Jiang0c1454c2023-05-05 02:28:20 +00001151 noop_nexts, drop_next, true);
Neale Rannsfc811342021-02-26 10:35:33 +00001152 vnet_crypto_async_reset_frame (*async_frame);
1153 vnet_crypto_async_free_frame (vm, *async_frame);
1154 }
Fan Zhang18f0e312020-10-19 13:08:34 +01001155 }
Fan Zhangf5395782020-04-29 14:00:03 +01001156 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001157 if (n_noop)
1158 vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
1159
1160 vlib_node_increment_counter (vm, node->node_index, ESP_ENCRYPT_ERROR_RX_PKTS,
1161 frame->n_vectors);
Damjan Marionc59b9a22019-03-19 15:38:40 +01001162
Damjan Marionc59b9a22019-03-19 15:38:40 +01001163 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001164}
1165
Fan Zhangf5395782020-04-29 14:00:03 +01001166always_inline uword
1167esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1168 vlib_frame_t * frame)
1169{
1170 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1171 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1172 u32 *from = vlib_frame_vector_args (frame);
1173 u32 n_left = frame->n_vectors;
1174
1175 vlib_get_buffers (vm, from, b, n_left);
1176
1177 if (n_left >= 4)
1178 {
1179 vlib_prefetch_buffer_header (b[0], LOAD);
1180 vlib_prefetch_buffer_header (b[1], LOAD);
1181 vlib_prefetch_buffer_header (b[2], LOAD);
1182 vlib_prefetch_buffer_header (b[3], LOAD);
1183 }
1184
1185 while (n_left > 8)
1186 {
1187 vlib_prefetch_buffer_header (b[4], LOAD);
1188 vlib_prefetch_buffer_header (b[5], LOAD);
1189 vlib_prefetch_buffer_header (b[6], LOAD);
1190 vlib_prefetch_buffer_header (b[7], LOAD);
1191
1192 next[0] = (esp_post_data (b[0]))->next_index;
1193 next[1] = (esp_post_data (b[1]))->next_index;
1194 next[2] = (esp_post_data (b[2]))->next_index;
1195 next[3] = (esp_post_data (b[3]))->next_index;
1196
1197 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
1198 {
1199 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
1200 {
1201 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1202 sizeof (*tr));
1203 tr->next_index = next[0];
1204 }
1205 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
1206 {
1207 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1],
1208 sizeof (*tr));
1209 tr->next_index = next[1];
1210 }
1211 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
1212 {
1213 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2],
1214 sizeof (*tr));
1215 tr->next_index = next[2];
1216 }
1217 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
1218 {
1219 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3],
1220 sizeof (*tr));
1221 tr->next_index = next[3];
1222 }
1223 }
1224
1225 b += 4;
1226 next += 4;
1227 n_left -= 4;
1228 }
1229
1230 while (n_left > 0)
1231 {
1232 next[0] = (esp_post_data (b[0]))->next_index;
1233 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1234 {
1235 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1236 sizeof (*tr));
1237 tr->next_index = next[0];
1238 }
1239
1240 b += 1;
1241 next += 1;
1242 n_left -= 1;
1243 }
1244
1245 vlib_node_increment_counter (vm, node->node_index,
1246 ESP_ENCRYPT_ERROR_POST_RX_PKTS,
1247 frame->n_vectors);
1248 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1249 return frame->n_vectors;
1250}
1251
Klement Sekerab8f35442018-10-29 13:38:19 +01001252VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
1253 vlib_node_runtime_t * node,
1254 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001255{
Neale Ranns4a58e492020-12-21 13:19:10 +00001256 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001257 esp_encrypt_async_next.esp4_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001258}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001260VLIB_REGISTER_NODE (esp4_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001261 .name = "esp4-encrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262 .vector_size = sizeof (u32),
1263 .format_trace = format_esp_encrypt_trace,
1264 .type = VLIB_NODE_TYPE_INTERNAL,
1265
Neale Ranns93688d72022-08-09 03:34:51 +00001266 .n_errors = ESP_ENCRYPT_N_ERROR,
1267 .error_counters = esp_encrypt_error_counters,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001268
1269 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Ranns4a58e492020-12-21 13:19:10 +00001270 .next_nodes = { [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1271 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1272 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1273 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-handoff",
1274 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-handoff",
1275 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "error-drop",
1276 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output" },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001277};
1278
Fan Zhangf5395782020-04-29 14:00:03 +01001279VLIB_NODE_FN (esp4_encrypt_post_node) (vlib_main_t * vm,
1280 vlib_node_runtime_t * node,
1281 vlib_frame_t * from_frame)
1282{
1283 return esp_encrypt_post_inline (vm, node, from_frame);
1284}
1285
Fan Zhangf5395782020-04-29 14:00:03 +01001286VLIB_REGISTER_NODE (esp4_encrypt_post_node) = {
1287 .name = "esp4-encrypt-post",
1288 .vector_size = sizeof (u32),
1289 .format_trace = format_esp_post_encrypt_trace,
1290 .type = VLIB_NODE_TYPE_INTERNAL,
1291 .sibling_of = "esp4-encrypt",
1292
Neale Ranns93688d72022-08-09 03:34:51 +00001293 .n_errors = ESP_ENCRYPT_N_ERROR,
1294 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001295};
Fan Zhangf5395782020-04-29 14:00:03 +01001296
Klement Sekerab8f35442018-10-29 13:38:19 +01001297VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
1298 vlib_node_runtime_t * node,
1299 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001300{
Neale Ranns4a58e492020-12-21 13:19:10 +00001301 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001302 esp_encrypt_async_next.esp6_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001303}
1304
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001305VLIB_REGISTER_NODE (esp6_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001306 .name = "esp6-encrypt",
1307 .vector_size = sizeof (u32),
1308 .format_trace = format_esp_encrypt_trace,
1309 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001310 .sibling_of = "esp4-encrypt",
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001311
Neale Ranns93688d72022-08-09 03:34:51 +00001312 .n_errors = ESP_ENCRYPT_N_ERROR,
1313 .error_counters = esp_encrypt_error_counters,
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001314};
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001315
Fan Zhangf5395782020-04-29 14:00:03 +01001316VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm,
1317 vlib_node_runtime_t * node,
1318 vlib_frame_t * from_frame)
1319{
1320 return esp_encrypt_post_inline (vm, node, from_frame);
1321}
1322
Fan Zhangf5395782020-04-29 14:00:03 +01001323VLIB_REGISTER_NODE (esp6_encrypt_post_node) = {
1324 .name = "esp6-encrypt-post",
1325 .vector_size = sizeof (u32),
1326 .format_trace = format_esp_post_encrypt_trace,
1327 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001328 .sibling_of = "esp4-encrypt",
Fan Zhangf5395782020-04-29 14:00:03 +01001329
Neale Ranns93688d72022-08-09 03:34:51 +00001330 .n_errors = ESP_ENCRYPT_N_ERROR,
1331 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001332};
Fan Zhangf5395782020-04-29 14:00:03 +01001333
Neale Ranns25edf142019-03-22 08:12:48 +00001334VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
1335 vlib_node_runtime_t * node,
1336 vlib_frame_t * from_frame)
1337{
Neale Ranns4a58e492020-12-21 13:19:10 +00001338 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001339 esp_encrypt_async_next.esp4_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001340}
1341
Neale Ranns25edf142019-03-22 08:12:48 +00001342VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
1343 .name = "esp4-encrypt-tun",
1344 .vector_size = sizeof (u32),
1345 .format_trace = format_esp_encrypt_trace,
1346 .type = VLIB_NODE_TYPE_INTERNAL,
1347
Neale Ranns93688d72022-08-09 03:34:51 +00001348 .n_errors = ESP_ENCRYPT_N_ERROR,
1349 .error_counters = esp_encrypt_error_counters,
Neale Rannsd7603d92019-03-28 08:56:10 +00001350
Neale Rannsf62a8c02019-04-02 08:13:33 +00001351 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001352 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001353 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1354 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001355 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001356 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001357 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1358 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001359 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001360 },
Neale Ranns25edf142019-03-22 08:12:48 +00001361};
1362
Fan Zhangf5395782020-04-29 14:00:03 +01001363VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm,
1364 vlib_node_runtime_t * node,
1365 vlib_frame_t * from_frame)
1366{
1367 return esp_encrypt_post_inline (vm, node, from_frame);
1368}
1369
Fan Zhangf5395782020-04-29 14:00:03 +01001370VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = {
1371 .name = "esp4-encrypt-tun-post",
1372 .vector_size = sizeof (u32),
1373 .format_trace = format_esp_post_encrypt_trace,
1374 .type = VLIB_NODE_TYPE_INTERNAL,
1375 .sibling_of = "esp4-encrypt-tun",
1376
Neale Ranns93688d72022-08-09 03:34:51 +00001377 .n_errors = ESP_ENCRYPT_N_ERROR,
1378 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001379};
Neale Ranns25edf142019-03-22 08:12:48 +00001380
1381VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
1382 vlib_node_runtime_t * node,
1383 vlib_frame_t * from_frame)
1384{
Neale Ranns4a58e492020-12-21 13:19:10 +00001385 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001386 esp_encrypt_async_next.esp6_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001387}
1388
Neale Ranns25edf142019-03-22 08:12:48 +00001389VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
1390 .name = "esp6-encrypt-tun",
1391 .vector_size = sizeof (u32),
1392 .format_trace = format_esp_encrypt_trace,
1393 .type = VLIB_NODE_TYPE_INTERNAL,
1394
Neale Ranns93688d72022-08-09 03:34:51 +00001395 .n_errors = ESP_ENCRYPT_N_ERROR,
1396 .error_counters = esp_encrypt_error_counters,
Neale Rannsd7603d92019-03-28 08:56:10 +00001397
Neale Rannsf62a8c02019-04-02 08:13:33 +00001398 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001399 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001400 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1401 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001402 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1403 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001404 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001405 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001406 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001407 },
Neale Ranns25edf142019-03-22 08:12:48 +00001408};
1409
Neale Ranns25edf142019-03-22 08:12:48 +00001410
Fan Zhangf5395782020-04-29 14:00:03 +01001411VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm,
1412 vlib_node_runtime_t * node,
1413 vlib_frame_t * from_frame)
1414{
1415 return esp_encrypt_post_inline (vm, node, from_frame);
1416}
1417
Fan Zhangf5395782020-04-29 14:00:03 +01001418VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = {
1419 .name = "esp6-encrypt-tun-post",
1420 .vector_size = sizeof (u32),
1421 .format_trace = format_esp_post_encrypt_trace,
1422 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Ranns4a58e492020-12-21 13:19:10 +00001423 .sibling_of = "esp-mpls-encrypt-tun",
Fan Zhangf5395782020-04-29 14:00:03 +01001424
Neale Ranns93688d72022-08-09 03:34:51 +00001425 .n_errors = ESP_ENCRYPT_N_ERROR,
1426 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001427};
Fan Zhangf5395782020-04-29 14:00:03 +01001428
Neale Ranns4a58e492020-12-21 13:19:10 +00001429VLIB_NODE_FN (esp_mpls_encrypt_tun_node)
1430(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1431{
1432 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_MPLS, 1,
1433 esp_encrypt_async_next.esp_mpls_tun_post_next);
1434}
1435
1436VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_node) = {
1437 .name = "esp-mpls-encrypt-tun",
1438 .vector_size = sizeof (u32),
1439 .format_trace = format_esp_encrypt_trace,
1440 .type = VLIB_NODE_TYPE_INTERNAL,
1441
Neale Ranns93688d72022-08-09 03:34:51 +00001442 .n_errors = ESP_ENCRYPT_N_ERROR,
1443 .error_counters = esp_encrypt_error_counters,
Neale Ranns4a58e492020-12-21 13:19:10 +00001444
1445 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1446 .next_nodes = {
1447 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1448 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1449 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1450 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1451 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1452 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1453 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1454 },
1455};
1456
1457VLIB_NODE_FN (esp_mpls_encrypt_tun_post_node)
1458(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1459{
1460 return esp_encrypt_post_inline (vm, node, from_frame);
1461}
1462
1463VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_post_node) = {
1464 .name = "esp-mpls-encrypt-tun-post",
1465 .vector_size = sizeof (u32),
1466 .format_trace = format_esp_post_encrypt_trace,
1467 .type = VLIB_NODE_TYPE_INTERNAL,
1468 .sibling_of = "esp-mpls-encrypt-tun",
1469
Neale Ranns93688d72022-08-09 03:34:51 +00001470 .n_errors = ESP_ENCRYPT_N_ERROR,
1471 .error_counters = esp_encrypt_error_counters,
Neale Ranns4a58e492020-12-21 13:19:10 +00001472};
1473
Neale Ranns2d498302021-02-25 08:38:58 +00001474#ifndef CLIB_MARCH_VARIANT
1475
1476static clib_error_t *
1477esp_encrypt_init (vlib_main_t *vm)
1478{
1479 ipsec_main_t *im = &ipsec_main;
1480
Dau Doea921162024-04-29 03:00:55 +00001481 im->esp4_enc_fq_index = vlib_frame_queue_main_init (esp4_encrypt_node.index,
1482 im->handoff_queue_size);
1483 im->esp6_enc_fq_index = vlib_frame_queue_main_init (esp6_encrypt_node.index,
1484 im->handoff_queue_size);
1485 im->esp4_enc_tun_fq_index = vlib_frame_queue_main_init (
1486 esp4_encrypt_tun_node.index, im->handoff_queue_size);
1487 im->esp6_enc_tun_fq_index = vlib_frame_queue_main_init (
1488 esp6_encrypt_tun_node.index, im->handoff_queue_size);
1489 im->esp_mpls_enc_tun_fq_index = vlib_frame_queue_main_init (
1490 esp_mpls_encrypt_tun_node.index, im->handoff_queue_size);
Neale Ranns2d498302021-02-25 08:38:58 +00001491
1492 return 0;
1493}
1494
1495VLIB_INIT_FUNCTION (esp_encrypt_init);
1496
1497#endif
1498
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001499/*
1500 * fd.io coding-style-patch-verification: ON
1501 *
1502 * Local Variables:
1503 * eval: (c-set-style "gnu")
1504 * End:
1505 */