blob: 46f7fe6cd06c5e3e8d42524511854eafe026828a [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>
21
Damjan Marion91f17dc2019-03-18 18:59:25 +010022#include <vnet/crypto/crypto.h>
23
Ed Warnickecb9cada2015-12-08 15:45:58 -070024#include <vnet/ipsec/ipsec.h>
Neale Ranns28287212019-12-16 00:53:11 +000025#include <vnet/ipsec/ipsec_tun.h>
Neale Ranns93688d72022-08-09 03:34:51 +000026#include <vnet/ipsec/ipsec.api_enum.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027#include <vnet/ipsec/esp.h>
Neale Ranns041add72020-01-02 04:06:10 +000028#include <vnet/tunnel/tunnel_dp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Neale Ranns4a58e492020-12-21 13:19:10 +000030#define foreach_esp_encrypt_next \
31 _ (DROP4, "ip4-drop") \
32 _ (DROP6, "ip6-drop") \
33 _ (DROP_MPLS, "mpls-drop") \
34 _ (HANDOFF4, "handoff4") \
35 _ (HANDOFF6, "handoff6") \
36 _ (HANDOFF_MPLS, "handoff-mpls") \
37 _ (INTERFACE_OUTPUT, "interface-output")
Ed Warnickecb9cada2015-12-08 15:45:58 -070038
39#define _(v, s) ESP_ENCRYPT_NEXT_##v,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070040typedef enum
41{
Ed Warnickecb9cada2015-12-08 15:45:58 -070042 foreach_esp_encrypt_next
43#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070044 ESP_ENCRYPT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070045} esp_encrypt_next_t;
46
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070047typedef struct
48{
Neale Ranns8d7c5022019-02-06 01:41:05 -080049 u32 sa_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 u32 spi;
51 u32 seq;
Neale Ranns6afaae12019-07-17 15:07:14 +000052 u32 sa_seq_hi;
Klement Sekera4b089f22018-04-17 18:04:57 +020053 u8 udp_encap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 ipsec_crypto_alg_t crypto_alg;
55 ipsec_integ_alg_t integ_alg;
56} esp_encrypt_trace_t;
57
Fan Zhangf5395782020-04-29 14:00:03 +010058typedef struct
59{
60 u32 next_index;
61} esp_encrypt_post_trace_t;
62
Neale Ranns93688d72022-08-09 03:34:51 +000063typedef vl_counter_esp_encrypt_enum_t esp_encrypt_error_t;
64
Ed Warnickecb9cada2015-12-08 15:45:58 -070065/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070066static u8 *
67format_esp_encrypt_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
69 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
70 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070071 esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020073 s =
74 format (s,
Neale Ranns6afaae12019-07-17 15:07:14 +000075 "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
76 t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
77 format_ipsec_crypto_alg,
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020078 t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
79 t->udp_encap ? " udp-encap-enabled" : "");
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 return s;
81}
82
Fan Zhangf5395782020-04-29 14:00:03 +010083static u8 *
84format_esp_post_encrypt_trace (u8 * s, va_list * args)
85{
86 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
87 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
88 esp_encrypt_post_trace_t *t = va_arg (*args, esp_encrypt_post_trace_t *);
89
90 s = format (s, "esp-post: next node index %u", t->next_index);
91 return s;
92}
93
Damjan Marionc59b9a22019-03-19 15:38:40 +010094/* pad packet in input buffer */
95static_always_inline u8 *
Neale Rannsf16e9a52021-02-25 19:09:24 +000096esp_add_footer_and_icv (vlib_main_t *vm, vlib_buffer_t **last, u8 esp_align,
97 u8 icv_sz, vlib_node_runtime_t *node,
Filip Tehlarefcad1a2020-02-04 09:36:04 +000098 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;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100152
Neale Ranns1b582b82019-04-18 19:49:13 -0700153 sum = ip_csum_update (ip4->checksum, ip4->protocol,
154 prot, ip4_header_t, protocol);
155 ip4->protocol = prot;
156
157 sum = ip_csum_update (sum, old_len, len, ip4_header_t, length);
158 }
159 else
160 sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
161
162 ip4->length = len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100163 ip4->checksum = ip_csum_fold (sum);
164}
165
166static_always_inline void
167esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
168{
169 clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
170 udp->length = clib_net_to_host_u16 (len);
171}
172
173static_always_inline u8
174ext_hdr_is_pre_esp (u8 nexthdr)
175{
176#ifdef CLIB_HAVE_VEC128
177 static const u8x16 ext_hdr_types = {
178 IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
179 IP_PROTOCOL_IPV6_ROUTE,
180 IP_PROTOCOL_IPV6_FRAGMENTATION,
181 };
182
183 return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
184#else
Piotr Bronowski3a6bc6f2023-02-23 09:56:49 +0000185 return (!(nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ||
186 !(nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) ||
187 !(nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100188#endif
189}
190
191static_always_inline u8
Neale Ranns02950402019-12-20 00:54:57 +0000192esp_get_ip6_hdr_len (ip6_header_t * ip6, ip6_ext_header_t ** ext_hdr)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100193{
194 /* this code assumes that HbH, route and frag headers will be before
195 others, if that is not the case, they will end up encrypted */
Damjan Marionc59b9a22019-03-19 15:38:40 +0100196 u8 len = sizeof (ip6_header_t);
197 ip6_ext_header_t *p;
198
199 /* if next packet doesn't have ext header */
200 if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
Neale Ranns02950402019-12-20 00:54:57 +0000201 {
202 *ext_hdr = NULL;
203 return len;
204 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100205
Ole Troan03092c12021-11-23 15:55:39 +0100206 p = ip6_next_header (ip6);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100207 len += ip6_ext_header_len (p);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100208 while (ext_hdr_is_pre_esp (p->next_hdr))
209 {
210 len += ip6_ext_header_len (p);
211 p = ip6_ext_next_header (p);
212 }
213
Neale Ranns02950402019-12-20 00:54:57 +0000214 *ext_hdr = p;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100215 return len;
216}
217
Benoît Ganne02dfd292022-01-18 15:56:41 +0100218/* IPsec IV generation: IVs requirements differ depending of the
219 * encryption mode: IVs must be unpredictable for AES-CBC whereas it can
220 * be predictable but should never be reused with the same key material
221 * for CTR and GCM.
Benoît Ganne5527a782022-01-18 15:56:41 +0100222 * To avoid reusing the same IVs between multiple VPP instances and between
223 * restarts, we use a properly chosen PRNG to generate IVs. To ensure the IV is
224 * unpredictable for CBC, it is then encrypted using the same key as the
225 * message. You can refer to NIST SP800-38a and NIST SP800-38d for more
226 * details. */
Benoît Ganne02dfd292022-01-18 15:56:41 +0100227static_always_inline void *
228esp_generate_iv (ipsec_sa_t *sa, void *payload, int iv_sz)
229{
230 ASSERT (iv_sz >= sizeof (u64));
231 u64 *iv = (u64 *) (payload - iv_sz);
232 clib_memset_u8 (iv, 0, iv_sz);
Benoît Ganne5527a782022-01-18 15:56:41 +0100233 *iv = clib_pcg64i_random_r (&sa->iv_prng);
Benoît Ganne02dfd292022-01-18 15:56:41 +0100234 return iv;
235}
236
Damjan Marionc59b9a22019-03-19 15:38:40 +0100237static_always_inline void
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000238esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
239 vnet_crypto_op_t * ops, vlib_buffer_t * b[],
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000240 u16 * nexts, vnet_crypto_op_chunk_t * chunks,
241 u16 drop_next)
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000242{
243 u32 n_fail, n_ops = vec_len (ops);
244 vnet_crypto_op_t *op = ops;
245
246 if (n_ops == 0)
247 return;
248
249 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
250
251 while (n_fail)
252 {
253 ASSERT (op - ops < n_ops);
254
255 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
256 {
257 u32 bi = op->user_data;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100258 esp_encrypt_set_next_index (b[bi], node, vm->thread_index,
259 ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR,
260 bi, nexts, drop_next,
261 vnet_buffer (b[bi])->ipsec.sad_index);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000262 n_fail--;
263 }
264 op++;
265 }
266}
267
268static_always_inline void
Damjan Marionc59b9a22019-03-19 15:38:40 +0100269esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000270 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts,
271 u16 drop_next)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100272{
273 u32 n_fail, n_ops = vec_len (ops);
274 vnet_crypto_op_t *op = ops;
275
276 if (n_ops == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277 return;
278
Damjan Marionc59b9a22019-03-19 15:38:40 +0100279 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
Damjan Marionc59b9a22019-03-19 15:38:40 +0100281 while (n_fail)
282 {
283 ASSERT (op - ops < n_ops);
284
285 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
286 {
287 u32 bi = op->user_data;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100288 esp_encrypt_set_next_index (b[bi], node, vm->thread_index,
289 ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR,
290 bi, nexts, drop_next,
291 vnet_buffer (b[bi])->ipsec.sad_index);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100292 n_fail--;
293 }
294 op++;
295 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296}
297
Fan Zhangf5395782020-04-29 14:00:03 +0100298static_always_inline u32
299esp_encrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
300 ipsec_sa_t * sa0, vlib_buffer_t * b,
301 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
302 u32 start_len, u16 * n_ch)
303{
304 vnet_crypto_op_chunk_t *ch;
305 vlib_buffer_t *cb = b;
306 u32 n_chunks = 1;
307 u32 total_len;
308 vec_add2 (ptd->chunks, ch, 1);
309 total_len = ch->len = start_len;
310 ch->src = ch->dst = start;
311 cb = vlib_get_buffer (vm, cb->next_buffer);
312
313 while (1)
314 {
315 vec_add2 (ptd->chunks, ch, 1);
316 n_chunks += 1;
317 if (lb == cb)
318 total_len += ch->len = cb->current_length - icv_sz;
319 else
320 total_len += ch->len = cb->current_length;
321 ch->src = ch->dst = vlib_buffer_get_current (cb);
322
323 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
324 break;
325
326 cb = vlib_get_buffer (vm, cb->next_buffer);
327 }
328
329 if (n_ch)
330 *n_ch = n_chunks;
331
332 return total_len;
333}
334
335static_always_inline u32
336esp_encrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
337 ipsec_sa_t * sa0, vlib_buffer_t * b,
338 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
339 u32 start_len, u8 * digest, u16 * n_ch)
340{
341 vnet_crypto_op_chunk_t *ch;
342 vlib_buffer_t *cb = b;
343 u32 n_chunks = 1;
344 u32 total_len;
345 vec_add2 (ptd->chunks, ch, 1);
346 total_len = ch->len = start_len;
347 ch->src = start;
348 cb = vlib_get_buffer (vm, cb->next_buffer);
349
350 while (1)
351 {
352 vec_add2 (ptd->chunks, ch, 1);
353 n_chunks += 1;
354 if (lb == cb)
355 {
356 total_len += ch->len = cb->current_length - icv_sz;
357 if (ipsec_sa_is_set_USE_ESN (sa0))
358 {
359 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
360 clib_memcpy_fast (digest, &seq_hi, sizeof (seq_hi));
361 ch->len += sizeof (seq_hi);
362 total_len += sizeof (seq_hi);
363 }
364 }
365 else
366 total_len += ch->len = cb->current_length;
367 ch->src = vlib_buffer_get_current (cb);
368
369 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
370 break;
371
372 cb = vlib_get_buffer (vm, cb->next_buffer);
373 }
374
375 if (n_ch)
376 *n_ch = n_chunks;
377
378 return total_len;
379}
380
381always_inline void
Benoît Ganne490b9272021-01-22 18:03:09 +0100382esp_prepare_sync_op (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
383 vnet_crypto_op_t **crypto_ops,
Neale Ranns5b891102021-06-28 13:31:28 +0000384 vnet_crypto_op_t **integ_ops, ipsec_sa_t *sa0, u32 seq_hi,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000385 u8 *payload, u16 payload_len, u8 iv_sz, u8 icv_sz, u32 bi,
386 vlib_buffer_t **b, vlib_buffer_t *lb, u32 hdr_len,
387 esp_header_t *esp)
Fan Zhangf5395782020-04-29 14:00:03 +0100388{
389 if (sa0->crypto_enc_op_id)
390 {
391 vnet_crypto_op_t *op;
392 vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
393 vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
Benoît Ganne02dfd292022-01-18 15:56:41 +0100394 u8 *crypto_start = payload;
395 /* esp_add_footer_and_icv() in esp_encrypt_inline() makes sure we always
396 * have enough space for ESP header and footer which includes ICV */
397 ASSERT (payload_len > icv_sz);
398 u16 crypto_len = payload_len - icv_sz;
Fan Zhangf5395782020-04-29 14:00:03 +0100399
Benoît Ganne02dfd292022-01-18 15:56:41 +0100400 /* generate the IV in front of the payload */
401 void *pkt_iv = esp_generate_iv (sa0, payload, iv_sz);
402
Fan Zhangf5395782020-04-29 14:00:03 +0100403 op->key_index = sa0->crypto_key_index;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000404 op->user_data = bi;
Fan Zhangf5395782020-04-29 14:00:03 +0100405
Benoît Ganne490b9272021-01-22 18:03:09 +0100406 if (ipsec_sa_is_set_IS_CTR (sa0))
Fan Zhangf5395782020-04-29 14:00:03 +0100407 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100408 /* construct nonce in a scratch space in front of the IP header */
409 esp_ctr_nonce_t *nonce =
Benoît Ganne02dfd292022-01-18 15:56:41 +0100410 (esp_ctr_nonce_t *) (pkt_iv - hdr_len - sizeof (*nonce));
Benoît Ganne490b9272021-01-22 18:03:09 +0100411 if (ipsec_sa_is_set_IS_AEAD (sa0))
412 {
413 /* constuct aad in a scratch space in front of the nonce */
414 op->aad = (u8 *) nonce - sizeof (esp_aead_t);
Neale Ranns5b891102021-06-28 13:31:28 +0000415 op->aad_len = esp_aad_fill (op->aad, esp, sa0, seq_hi);
Benoît Ganne02dfd292022-01-18 15:56:41 +0100416 op->tag = payload + crypto_len;
Benoît Ganne490b9272021-01-22 18:03:09 +0100417 op->tag_len = 16;
Benoît Ganne84e66582023-03-10 17:33:03 +0100418 if (PREDICT_FALSE (ipsec_sa_is_set_IS_NULL_GMAC (sa0)))
419 {
420 /* RFC-4543 ENCR_NULL_AUTH_AES_GMAC: IV is part of AAD */
421 crypto_start -= iv_sz;
422 crypto_len += iv_sz;
423 }
Benoît Ganne490b9272021-01-22 18:03:09 +0100424 }
425 else
426 {
427 nonce->ctr = clib_host_to_net_u32 (1);
428 }
Fan Zhangf5395782020-04-29 14:00:03 +0100429
Fan Zhangf5395782020-04-29 14:00:03 +0100430 nonce->salt = sa0->salt;
Benoît Ganne02dfd292022-01-18 15:56:41 +0100431 nonce->iv = *(u64 *) pkt_iv;
Fan Zhangf5395782020-04-29 14:00:03 +0100432 op->iv = (u8 *) nonce;
433 }
434 else
435 {
Benoît Ganne02dfd292022-01-18 15:56:41 +0100436 /* construct zero iv in front of the IP header */
437 op->iv = pkt_iv - hdr_len - iv_sz;
438 clib_memset_u8 (op->iv, 0, iv_sz);
439 /* include iv field in crypto */
440 crypto_start -= iv_sz;
441 crypto_len += iv_sz;
Fan Zhangf5395782020-04-29 14:00:03 +0100442 }
443
Benoît Ganne5527a782022-01-18 15:56:41 +0100444 if (PREDICT_FALSE (lb != b[0]))
Fan Zhangf5395782020-04-29 14:00:03 +0100445 {
446 /* is chained */
447 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
448 op->chunk_index = vec_len (ptd->chunks);
449 op->tag = vlib_buffer_get_tail (lb) - icv_sz;
Benoît Ganne02dfd292022-01-18 15:56:41 +0100450 esp_encrypt_chain_crypto (vm, ptd, sa0, b[0], lb, icv_sz,
451 crypto_start, crypto_len + icv_sz,
452 &op->n_chunks);
453 }
454 else
455 {
456 /* not chained */
457 op->src = op->dst = crypto_start;
458 op->len = crypto_len;
Fan Zhangf5395782020-04-29 14:00:03 +0100459 }
460 }
461
462 if (sa0->integ_op_id)
463 {
464 vnet_crypto_op_t *op;
465 vec_add2_aligned (integ_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
466 vnet_crypto_op_init (op, sa0->integ_op_id);
467 op->src = payload - iv_sz - sizeof (esp_header_t);
468 op->digest = payload + payload_len - icv_sz;
469 op->key_index = sa0->integ_key_index;
470 op->digest_len = icv_sz;
471 op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000472 op->user_data = bi;
Fan Zhangf5395782020-04-29 14:00:03 +0100473
474 if (lb != b[0])
475 {
476 /* is chained */
477 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
478 op->chunk_index = vec_len (ptd->chunks);
479 op->digest = vlib_buffer_get_tail (lb) - icv_sz;
480
481 esp_encrypt_chain_integ (vm, ptd, sa0, b[0], lb, icv_sz,
482 payload - iv_sz - sizeof (esp_header_t),
483 payload_len + iv_sz +
484 sizeof (esp_header_t), op->digest,
485 &op->n_chunks);
486 }
487 else if (ipsec_sa_is_set_USE_ESN (sa0))
488 {
Neale Ranns5b891102021-06-28 13:31:28 +0000489 u32 tmp = clib_net_to_host_u32 (seq_hi);
490 clib_memcpy_fast (op->digest, &tmp, sizeof (seq_hi));
Fan Zhangf5395782020-04-29 14:00:03 +0100491 op->len += sizeof (seq_hi);
492 }
493 }
494}
495
Neale Rannsfc811342021-02-26 10:35:33 +0000496static_always_inline void
497esp_prepare_async_frame (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
498 vnet_crypto_async_frame_t *async_frame,
499 ipsec_sa_t *sa, vlib_buffer_t *b, esp_header_t *esp,
500 u8 *payload, u32 payload_len, u8 iv_sz, u8 icv_sz,
501 u32 bi, u16 next, u32 hdr_len, u16 async_next,
502 vlib_buffer_t *lb)
Fan Zhangf5395782020-04-29 14:00:03 +0100503{
504 esp_post_data_t *post = esp_post_data (b);
505 u8 *tag, *iv, *aad = 0;
506 u8 flag = 0;
Benoît Ganne5527a782022-01-18 15:56:41 +0100507 const u32 key_index = sa->crypto_key_index;
Benoît Ganne02dfd292022-01-18 15:56:41 +0100508 i16 crypto_start_offset, integ_start_offset;
Fan Zhangf5395782020-04-29 14:00:03 +0100509 u16 crypto_total_len, integ_total_len;
510
Fan Zhang18f0e312020-10-19 13:08:34 +0100511 post->next_index = next;
Fan Zhangf5395782020-04-29 14:00:03 +0100512
513 /* crypto */
Benoît Ganne02dfd292022-01-18 15:56:41 +0100514 crypto_start_offset = integ_start_offset = payload - b->data;
Fan Zhangf5395782020-04-29 14:00:03 +0100515 crypto_total_len = integ_total_len = payload_len - icv_sz;
516 tag = payload + crypto_total_len;
517
Benoît Ganne02dfd292022-01-18 15:56:41 +0100518 /* generate the IV in front of the payload */
519 void *pkt_iv = esp_generate_iv (sa, payload, iv_sz);
520
Benoît Ganne490b9272021-01-22 18:03:09 +0100521 if (ipsec_sa_is_set_IS_CTR (sa))
Fan Zhangf5395782020-04-29 14:00:03 +0100522 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100523 /* construct nonce in a scratch space in front of the IP header */
Benoît Ganne02dfd292022-01-18 15:56:41 +0100524 esp_ctr_nonce_t *nonce =
525 (esp_ctr_nonce_t *) (pkt_iv - hdr_len - sizeof (*nonce));
Benoît Ganne490b9272021-01-22 18:03:09 +0100526 if (ipsec_sa_is_set_IS_AEAD (sa))
527 {
528 /* constuct aad in a scratch space in front of the nonce */
529 aad = (u8 *) nonce - sizeof (esp_aead_t);
Neale Ranns5b891102021-06-28 13:31:28 +0000530 esp_aad_fill (aad, esp, sa, sa->seq_hi);
Benoît Ganne84e66582023-03-10 17:33:03 +0100531 if (PREDICT_FALSE (ipsec_sa_is_set_IS_NULL_GMAC (sa)))
532 {
533 /* RFC-4543 ENCR_NULL_AUTH_AES_GMAC: IV is part of AAD */
534 crypto_start_offset -= iv_sz;
535 crypto_total_len += iv_sz;
536 }
Benoît Ganne490b9272021-01-22 18:03:09 +0100537 }
538 else
539 {
540 nonce->ctr = clib_host_to_net_u32 (1);
541 }
542
543 nonce->salt = sa->salt;
Benoît Ganne02dfd292022-01-18 15:56:41 +0100544 nonce->iv = *(u64 *) pkt_iv;
Benoît Ganne490b9272021-01-22 18:03:09 +0100545 iv = (u8 *) nonce;
Fan Zhangf5395782020-04-29 14:00:03 +0100546 }
Benoît Ganne490b9272021-01-22 18:03:09 +0100547 else
Fan Zhangf5395782020-04-29 14:00:03 +0100548 {
Benoît Ganne02dfd292022-01-18 15:56:41 +0100549 /* construct zero iv in front of the IP header */
550 iv = pkt_iv - hdr_len - iv_sz;
551 clib_memset_u8 (iv, 0, iv_sz);
552 /* include iv field in crypto */
553 crypto_start_offset -= iv_sz;
554 crypto_total_len += iv_sz;
Fan Zhangf5395782020-04-29 14:00:03 +0100555 }
556
Benoît Ganne490b9272021-01-22 18:03:09 +0100557 if (lb != b)
558 {
559 /* chain */
560 flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
561 tag = vlib_buffer_get_tail (lb) - icv_sz;
Benoît Ganne02dfd292022-01-18 15:56:41 +0100562 crypto_total_len = esp_encrypt_chain_crypto (
563 vm, ptd, sa, b, lb, icv_sz, b->data + crypto_start_offset,
564 crypto_total_len + icv_sz, 0);
Benoît Ganne490b9272021-01-22 18:03:09 +0100565 }
566
567 if (sa->integ_op_id)
568 {
Benoît Ganne02dfd292022-01-18 15:56:41 +0100569 integ_start_offset -= iv_sz + sizeof (esp_header_t);
Benoît Ganne490b9272021-01-22 18:03:09 +0100570 integ_total_len += iv_sz + sizeof (esp_header_t);
571
572 if (b != lb)
573 {
574 integ_total_len = esp_encrypt_chain_integ (
575 vm, ptd, sa, b, lb, icv_sz,
576 payload - iv_sz - sizeof (esp_header_t),
577 payload_len + iv_sz + sizeof (esp_header_t), tag, 0);
578 }
579 else if (ipsec_sa_is_set_USE_ESN (sa))
580 {
581 u32 seq_hi = clib_net_to_host_u32 (sa->seq_hi);
582 clib_memcpy_fast (tag, &seq_hi, sizeof (seq_hi));
583 integ_total_len += sizeof (seq_hi);
584 }
585 }
586
Neale Rannsfc811342021-02-26 10:35:33 +0000587 /* this always succeeds because we know the frame is not full */
588 vnet_crypto_async_add_to_frame (vm, async_frame, key_index, crypto_total_len,
589 integ_total_len - crypto_total_len,
590 crypto_start_offset, integ_start_offset, bi,
591 async_next, iv, tag, aad, flag);
Fan Zhangf5395782020-04-29 14:00:03 +0100592}
593
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200594always_inline uword
Neale Ranns4a58e492020-12-21 13:19:10 +0000595esp_encrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
596 vlib_frame_t *frame, vnet_link_t lt, int is_tun,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000597 u16 async_next_node)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 ipsec_main_t *im = &ipsec_main;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100600 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
601 u32 *from = vlib_frame_vector_args (frame);
602 u32 n_left = frame->n_vectors;
603 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100604 u32 thread_index = vm->thread_index;
605 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
606 u32 current_sa_index = ~0, current_sa_packets = 0;
607 u32 current_sa_bytes = 0, spi = 0;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500608 u8 esp_align = 4, iv_sz = 0, icv_sz = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100609 ipsec_sa_t *sa0 = 0;
Matthew Smithff719392024-02-12 18:39:21 +0000610 u8 sa_drop_no_crypto = 0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000611 vlib_buffer_t *lb;
612 vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
613 vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
Neale Rannsfc811342021-02-26 10:35:33 +0000614 vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
Fan Zhangf5395782020-04-29 14:00:03 +0100615 int is_async = im->async_mode;
Neale Rannsfc811342021-02-26 10:35:33 +0000616 vnet_crypto_async_op_id_t async_op = ~0;
Neale Ranns4a58e492020-12-21 13:19:10 +0000617 u16 drop_next =
618 (lt == VNET_LINK_IP6 ? ESP_ENCRYPT_NEXT_DROP6 :
619 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_DROP4 :
620 ESP_ENCRYPT_NEXT_DROP_MPLS));
621 u16 handoff_next = (lt == VNET_LINK_IP6 ?
622 ESP_ENCRYPT_NEXT_HANDOFF6 :
623 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_HANDOFF4 :
624 ESP_ENCRYPT_NEXT_HANDOFF_MPLS));
Neale Rannsf16e9a52021-02-25 19:09:24 +0000625 vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
626 u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0;
Damjan Mariondd298e82022-10-12 16:02:18 +0200627 u16 n_async = 0;
628 u16 noop_nexts[VLIB_FRAME_SIZE], n_noop = 0;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000629 u32 sync_bi[VLIB_FRAME_SIZE];
630 u32 noop_bi[VLIB_FRAME_SIZE];
631 esp_encrypt_error_t err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632
Damjan Marionc59b9a22019-03-19 15:38:40 +0100633 vlib_get_buffers (vm, from, b, n_left);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000634
635 vec_reset_length (ptd->crypto_ops);
636 vec_reset_length (ptd->integ_ops);
637 vec_reset_length (ptd->chained_crypto_ops);
638 vec_reset_length (ptd->chained_integ_ops);
Neale Rannsfc811342021-02-26 10:35:33 +0000639 vec_reset_length (ptd->async_frames);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000640 vec_reset_length (ptd->chunks);
Neale Rannsfc811342021-02-26 10:35:33 +0000641 clib_memset (async_frames, 0, sizeof (async_frames));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100642
643 while (n_left > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700644 {
Zhiyong Yang1cff6432019-04-30 05:33:53 -0400645 u32 sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100646 dpo_id_t *dpo;
647 esp_header_t *esp;
648 u8 *payload, *next_hdr_ptr;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000649 u16 payload_len, payload_len_total, n_bufs;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400650 u32 hdr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651
Neale Rannsf16e9a52021-02-25 19:09:24 +0000652 err = ESP_ENCRYPT_ERROR_RX_PKTS;
653
Damjan Marionc59b9a22019-03-19 15:38:40 +0100654 if (n_left > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700655 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100656 u8 *p;
657 vlib_prefetch_buffer_header (b[2], LOAD);
658 p = vlib_buffer_get_current (b[1]);
Damjan Marionaf7fb042021-07-15 11:54:41 +0200659 clib_prefetch_load (p);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100660 p -= CLIB_CACHE_LINE_BYTES;
Damjan Marionaf7fb042021-07-15 11:54:41 +0200661 clib_prefetch_load (p);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000662 /* speculate that the trailer goes in the first buffer */
663 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]),
664 CLIB_CACHE_LINE_BYTES, LOAD);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700665 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666
Neale Ranns25edf142019-03-22 08:12:48 +0000667 if (is_tun)
668 {
669 /* we are on a ipsec tunnel's feature arc */
Neale Ranns28287212019-12-16 00:53:11 +0000670 vnet_buffer (b[0])->ipsec.sad_index =
671 sa_index0 = ipsec_tun_protect_get_sa_out
672 (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000673
674 if (PREDICT_FALSE (INDEX_INVALID == sa_index0))
675 {
676 err = ESP_ENCRYPT_ERROR_NO_PROTECTION;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100677 noop_nexts[n_noop] = drop_next;
678 b[0]->error = node->errors[err];
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000679 goto trace;
680 }
Neale Ranns25edf142019-03-22 08:12:48 +0000681 }
682 else
683 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
684
685 if (sa_index0 != current_sa_index)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100686 {
Damjan Marion21f265f2019-06-05 15:45:50 +0200687 if (current_sa_packets)
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100688 vlib_increment_combined_counter (
689 &ipsec_sa_counters, thread_index, current_sa_index,
690 current_sa_packets, current_sa_bytes);
Damjan Marion21f265f2019-06-05 15:45:50 +0200691 current_sa_packets = current_sa_bytes = 0;
692
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000693 sa0 = ipsec_sa_get (sa_index0);
Matthew Smithdac9e562023-11-16 02:27:29 +0000694 current_sa_index = sa_index0;
Neale Ranns123b5eb2020-10-16 14:03:55 +0000695
Matthew Smithff719392024-02-12 18:39:21 +0000696 sa_drop_no_crypto = ((sa0->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
697 sa0->integ_alg == IPSEC_INTEG_ALG_NONE) &&
698 !ipsec_sa_is_set_NO_ALGO_NO_DROP (sa0));
699
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100700 vlib_prefetch_combined_counter (&ipsec_sa_counters, thread_index,
701 current_sa_index);
702
Neale Ranns123b5eb2020-10-16 14:03:55 +0000703 /* fetch the second cacheline ASAP */
Damjan Marionaf7fb042021-07-15 11:54:41 +0200704 clib_prefetch_load (sa0->cacheline1);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000705
Damjan Marionc59b9a22019-03-19 15:38:40 +0100706 spi = clib_net_to_host_u32 (sa0->spi);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500707 esp_align = sa0->esp_block_align;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200708 icv_sz = sa0->integ_icv_size;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100709 iv_sz = sa0->crypto_iv_size;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000710 is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0);
Neale Rannsfc811342021-02-26 10:35:33 +0000711 }
Fan Zhangf5395782020-04-29 14:00:03 +0100712
Matthew Smithff719392024-02-12 18:39:21 +0000713 if (PREDICT_FALSE (sa_drop_no_crypto != 0))
714 {
715 err = ESP_ENCRYPT_ERROR_NO_ENCRYPTION;
716 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
717 noop_nexts, drop_next, sa_index0);
718 goto trace;
719 }
720
Benoît Ganne5527a782022-01-18 15:56:41 +0100721 if (PREDICT_FALSE ((u16) ~0 == sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +0000722 {
723 /* this is the first packet to use this SA, claim the SA
724 * for this thread. this could happen simultaneously on
725 * another thread */
Neale Ranns1a52d372021-02-04 11:33:32 +0000726 clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
Neale Rannsf62a8c02019-04-02 08:13:33 +0000727 ipsec_sa_assign_thread (thread_index));
728 }
729
Neale Ranns1a52d372021-02-04 11:33:32 +0000730 if (PREDICT_FALSE (thread_index != sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +0000731 {
Neale Rannsaa7d7662021-02-10 08:42:49 +0000732 vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000733 err = ESP_ENCRYPT_ERROR_HANDOFF;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100734 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
735 noop_nexts, handoff_next,
736 current_sa_index);
Neale Rannsf62a8c02019-04-02 08:13:33 +0000737 goto trace;
738 }
739
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000740 lb = b[0];
741 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
742 if (n_bufs == 0)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100743 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000744 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100745 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
746 noop_nexts, drop_next, current_sa_index);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100747 goto trace;
748 }
749
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000750 if (n_bufs > 1)
751 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000752 /* find last buffer in the chain */
753 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
754 lb = vlib_get_buffer (vm, lb->next_buffer);
755 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000756
Damjan Marionc59b9a22019-03-19 15:38:40 +0100757 if (PREDICT_FALSE (esp_seq_advance (sa0)))
758 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000759 err = ESP_ENCRYPT_ERROR_SEQ_CYCLED;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100760 esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop,
761 noop_nexts, drop_next, current_sa_index);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100762 goto trace;
763 }
764
765 /* space for IV */
766 hdr_len = iv_sz;
767
Damjan Mariond709cbc2019-03-26 13:16:42 +0100768 if (ipsec_sa_is_set_IS_TUNNEL (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100769 {
770 payload = vlib_buffer_get_current (b[0]);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000771 next_hdr_ptr = esp_add_footer_and_icv (
772 vm, &lb, esp_align, icv_sz, node, buffer_data_size,
773 vlib_buffer_length_in_chain (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000774 if (!next_hdr_ptr)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000775 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000776 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100777 esp_encrypt_set_next_index (b[0], node, thread_index, err,
778 n_noop, noop_nexts, drop_next,
779 current_sa_index);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000780 goto trace;
781 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000782 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000783 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000784 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100785
786 /* ESP header */
787 hdr_len += sizeof (*esp);
788 esp = (esp_header_t *) (payload - hdr_len);
789
790 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100791 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100792 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100793 hdr_len += sizeof (udp_header_t);
794 esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000795 payload_len_total + hdr_len);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100796 }
797
798 /* IP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100799 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100800 {
801 ip6_header_t *ip6;
802 u16 len = sizeof (ip6_header_t);
803 hdr_len += len;
804 ip6 = (ip6_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000805 clib_memcpy_fast (ip6, &sa0->ip6_hdr, sizeof (ip6_header_t));
806
Neale Ranns4a58e492020-12-21 13:19:10 +0000807 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000808 {
809 *next_hdr_ptr = IP_PROTOCOL_IPV6;
810 tunnel_encap_fixup_6o6 (sa0->tunnel_flags,
811 (const ip6_header_t *) payload,
812 ip6);
813 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000814 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000815 {
816 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
Neale Rannsa91cb452021-02-04 11:02:52 +0000817 tunnel_encap_fixup_4o6 (sa0->tunnel_flags, b[0],
818 (const ip4_header_t *) payload, ip6);
Neale Ranns041add72020-01-02 04:06:10 +0000819 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000820 else if (VNET_LINK_MPLS == lt)
821 {
822 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
823 tunnel_encap_fixup_mplso6 (
Neale Rannsa91cb452021-02-04 11:02:52 +0000824 sa0->tunnel_flags, b[0],
825 (const mpls_unicast_header_t *) payload, ip6);
Neale Ranns4a58e492020-12-21 13:19:10 +0000826 }
827 else
828 ASSERT (0);
829
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000830 len = payload_len_total + hdr_len - len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100831 ip6->payload_length = clib_net_to_host_u16 (len);
Neale Ranns45d6d832021-01-19 13:38:47 +0000832 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Damjan Marionc98275f2019-03-06 14:05:01 +0100833 }
834 else
835 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100836 ip4_header_t *ip4;
837 u16 len = sizeof (ip4_header_t);
838 hdr_len += len;
839 ip4 = (ip4_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000840 clib_memcpy_fast (ip4, &sa0->ip4_hdr, sizeof (ip4_header_t));
841
Neale Ranns4a58e492020-12-21 13:19:10 +0000842 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000843 {
844 *next_hdr_ptr = IP_PROTOCOL_IPV6;
845 tunnel_encap_fixup_6o4_w_chksum (sa0->tunnel_flags,
846 (const ip6_header_t *)
847 payload, ip4);
848 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000849 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000850 {
851 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
852 tunnel_encap_fixup_4o4_w_chksum (sa0->tunnel_flags,
853 (const ip4_header_t *)
854 payload, ip4);
855 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000856 else if (VNET_LINK_MPLS == lt)
857 {
858 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
859 tunnel_encap_fixup_mplso4_w_chksum (
860 sa0->tunnel_flags, (const mpls_unicast_header_t *) payload,
861 ip4);
862 }
863 else
864 ASSERT (0);
865
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000866 len = payload_len_total + hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100867 esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100868 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100869
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000870 dpo = &sa0->dpo;
Neale Ranns25edf142019-03-22 08:12:48 +0000871 if (!is_tun)
872 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000873 sync_next[0] = dpo->dpoi_next_node;
Neale Ranns25edf142019-03-22 08:12:48 +0000874 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
875 }
Neale Ranns4ec36c52020-03-31 09:21:29 -0400876 else
Neale Rannsf16e9a52021-02-25 19:09:24 +0000877 sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000878 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Damjan Marionc98275f2019-03-06 14:05:01 +0100879 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100880 else /* transport mode */
Damjan Marionc98275f2019-03-06 14:05:01 +0100881 {
Ole Troan03092c12021-11-23 15:55:39 +0100882 u8 *l2_hdr, l2_len, *ip_hdr;
883 u16 ip_len;
Neale Ranns02950402019-12-20 00:54:57 +0000884 ip6_ext_header_t *ext_hdr;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100885 udp_header_t *udp = 0;
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400886 u16 udp_len = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100887 u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
Damjan Marionc98275f2019-03-06 14:05:01 +0100888
Ole Troan03092c12021-11-23 15:55:39 +0100889 /*
890 * Get extension header chain length. It might be longer than the
891 * buffer's pre_data area.
892 */
Neale Ranns4a58e492020-12-21 13:19:10 +0000893 ip_len =
894 (VNET_LINK_IP6 == lt ?
895 esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
896 ip4_header_bytes ((ip4_header_t *) old_ip_hdr));
Ole Troan03092c12021-11-23 15:55:39 +0100897 if ((old_ip_hdr - ip_len) < &b[0]->pre_data[0])
898 {
899 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100900 esp_encrypt_set_next_index (b[0], node, thread_index, err,
901 n_noop, noop_nexts, drop_next,
902 current_sa_index);
Ole Troan03092c12021-11-23 15:55:39 +0100903 goto trace;
904 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100905
Damjan Marionc59b9a22019-03-19 15:38:40 +0100906 vlib_buffer_advance (b[0], ip_len);
907 payload = vlib_buffer_get_current (b[0]);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000908 next_hdr_ptr = esp_add_footer_and_icv (
909 vm, &lb, esp_align, icv_sz, node, buffer_data_size,
910 vlib_buffer_length_in_chain (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000911 if (!next_hdr_ptr)
Fan Zhang18f0e312020-10-19 13:08:34 +0100912 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000913 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100914 esp_encrypt_set_next_index (b[0], node, thread_index, err,
915 n_noop, noop_nexts, drop_next,
916 current_sa_index);
Fan Zhang18f0e312020-10-19 13:08:34 +0100917 goto trace;
918 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000919
920 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000921 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000922 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100923
924 /* ESP header */
925 hdr_len += sizeof (*esp);
926 esp = (esp_header_t *) (payload - hdr_len);
927
928 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100929 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100930 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100931 hdr_len += sizeof (udp_header_t);
932 udp = (udp_header_t *) (payload - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100933 }
934
Damjan Marionc59b9a22019-03-19 15:38:40 +0100935 /* IP header */
936 hdr_len += ip_len;
937 ip_hdr = payload - hdr_len;
938
939 /* L2 header */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800940 if (!is_tun)
941 {
942 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
943 hdr_len += l2_len;
944 l2_hdr = payload - hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100945
Neale Rannsc87b66c2019-02-07 07:26:12 -0800946 /* copy l2 and ip header */
947 clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
948 }
949 else
950 l2_len = 0;
951
Matthew Smith6f1eb482022-08-09 22:19:38 +0000952 u16 len;
953 len = payload_len_total + hdr_len - l2_len;
954
Neale Ranns4a58e492020-12-21 13:19:10 +0000955 if (VNET_LINK_IP6 == lt)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100956 {
Neale Ranns02950402019-12-20 00:54:57 +0000957 ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
958 if (PREDICT_TRUE (NULL == ext_hdr))
959 {
960 *next_hdr_ptr = ip6->protocol;
Matthew Smith6f1eb482022-08-09 22:19:38 +0000961 ip6->protocol =
962 (udp) ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
Neale Ranns02950402019-12-20 00:54:57 +0000963 }
964 else
965 {
966 *next_hdr_ptr = ext_hdr->next_hdr;
Matthew Smith6f1eb482022-08-09 22:19:38 +0000967 ext_hdr->next_hdr =
968 (udp) ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
Neale Ranns02950402019-12-20 00:54:57 +0000969 }
Neale Rannsd207fd72019-04-18 17:18:12 -0700970 ip6->payload_length =
Matthew Smith6f1eb482022-08-09 22:19:38 +0000971 clib_host_to_net_u16 (len - sizeof (ip6_header_t));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100972 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000973 else if (VNET_LINK_IP4 == lt)
Damjan Marionc98275f2019-03-06 14:05:01 +0100974 {
Neale Ranns02950402019-12-20 00:54:57 +0000975 ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100976 *next_hdr_ptr = ip4->protocol;
Matthew Smith6f1eb482022-08-09 22:19:38 +0000977 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1,
978 (udp != NULL));
Damjan Marionc98275f2019-03-06 14:05:01 +0100979 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100980
Neale Ranns02950402019-12-20 00:54:57 +0000981 clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
982
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400983 if (udp)
984 {
Matthew Smith6f1eb482022-08-09 22:19:38 +0000985 udp_len = len - ip_len;
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400986 esp_fill_udp_hdr (sa0, udp, udp_len);
987 }
988
Neale Rannsf16e9a52021-02-25 19:09:24 +0000989 sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Damjan Marionc98275f2019-03-06 14:05:01 +0100990 }
991
PiotrX Kleskifdca4dd2020-05-05 14:14:22 +0200992 if (lb != b[0])
993 {
994 crypto_ops = &ptd->chained_crypto_ops;
995 integ_ops = &ptd->chained_integ_ops;
996 }
997 else
998 {
999 crypto_ops = &ptd->crypto_ops;
1000 integ_ops = &ptd->integ_ops;
1001 }
1002
Damjan Marionc59b9a22019-03-19 15:38:40 +01001003 esp->spi = spi;
1004 esp->seq = clib_net_to_host_u32 (sa0->seq);
Damjan Marionc98275f2019-03-06 14:05:01 +01001005
Fan Zhangf5395782020-04-29 14:00:03 +01001006 if (is_async)
Matthew Smith51d56ba2021-06-04 09:18:37 -05001007 {
1008 async_op = sa0->crypto_async_enc_op_id;
1009
1010 /* get a frame for this op if we don't yet have one or it's full
1011 */
1012 if (NULL == async_frames[async_op] ||
1013 vnet_crypto_async_frame_is_full (async_frames[async_op]))
1014 {
1015 async_frames[async_op] =
1016 vnet_crypto_async_get_frame (vm, async_op);
gaoginskxf441b5d2021-06-07 12:07:01 +01001017
1018 if (PREDICT_FALSE (!async_frames[async_op]))
1019 {
1020 err = ESP_ENCRYPT_ERROR_NO_AVAIL_FRAME;
1021 esp_encrypt_set_next_index (b[0], node, thread_index, err,
1022 n_noop, noop_nexts, drop_next,
1023 current_sa_index);
1024 goto trace;
1025 }
1026
Matthew Smith51d56ba2021-06-04 09:18:37 -05001027 /* Save the frame to the list we'll submit at the end */
1028 vec_add1 (ptd->async_frames, async_frames[async_op]);
1029 }
1030
1031 esp_prepare_async_frame (vm, ptd, async_frames[async_op], sa0, b[0],
1032 esp, payload, payload_len, iv_sz, icv_sz,
1033 from[b - bufs], sync_next[0], hdr_len,
1034 async_next_node, lb);
1035 }
Fan Zhangf5395782020-04-29 14:00:03 +01001036 else
Neale Ranns5b891102021-06-28 13:31:28 +00001037 esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, sa0->seq_hi,
1038 payload, payload_len, iv_sz, icv_sz, n_sync, b,
1039 lb, hdr_len, esp);
Damjan Marionc98275f2019-03-06 14:05:01 +01001040
Damjan Marionc59b9a22019-03-19 15:38:40 +01001041 vlib_buffer_advance (b[0], 0LL - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +01001042
Damjan Marionc59b9a22019-03-19 15:38:40 +01001043 current_sa_packets += 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001044 current_sa_bytes += payload_len_total;
Damjan Marionc59b9a22019-03-19 15:38:40 +01001045
1046 trace:
1047 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
Damjan Marionc98275f2019-03-06 14:05:01 +01001048 {
Damjan Marionc59b9a22019-03-19 15:38:40 +01001049 esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
1050 sizeof (*tr));
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00001051 if (INDEX_INVALID == sa_index0)
1052 clib_memset_u8 (tr, 0xff, sizeof (*tr));
1053 else
1054 {
1055 tr->sa_index = sa_index0;
1056 tr->spi = sa0->spi;
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00001057 tr->seq = sa0->seq;
1058 tr->sa_seq_hi = sa0->seq_hi;
1059 tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
1060 tr->crypto_alg = sa0->crypto_alg;
1061 tr->integ_alg = sa0->integ_alg;
1062 }
Damjan Marionc98275f2019-03-06 14:05:01 +01001063 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001064
Damjan Marionc98275f2019-03-06 14:05:01 +01001065 /* next */
Neale Rannsf16e9a52021-02-25 19:09:24 +00001066 if (ESP_ENCRYPT_ERROR_RX_PKTS != err)
1067 {
1068 noop_bi[n_noop] = from[b - bufs];
1069 n_noop++;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001070 }
1071 else if (!is_async)
1072 {
1073 sync_bi[n_sync] = from[b - bufs];
1074 sync_bufs[n_sync] = b[0];
1075 n_sync++;
1076 sync_next++;
1077 }
1078 else
1079 {
1080 n_async++;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001081 }
Damjan Marionc59b9a22019-03-19 15:38:40 +01001082 n_left -= 1;
Damjan Marionc59b9a22019-03-19 15:38:40 +01001083 b += 1;
Damjan Marionc98275f2019-03-06 14:05:01 +01001084 }
1085
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00001086 if (INDEX_INVALID != current_sa_index)
1087 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1088 current_sa_index, current_sa_packets,
1089 current_sa_bytes);
Neale Rannsf16e9a52021-02-25 19:09:24 +00001090 if (n_sync)
Fan Zhangf5395782020-04-29 14:00:03 +01001091 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001092 esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts,
1093 drop_next);
1094 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs,
1095 sync_nexts, ptd->chunks, drop_next);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001096
Neale Rannsf16e9a52021-02-25 19:09:24 +00001097 esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts,
1098 drop_next);
1099 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs,
1100 sync_nexts, ptd->chunks, drop_next);
Neale Rannsfc811342021-02-26 10:35:33 +00001101
Neale Rannsf16e9a52021-02-25 19:09:24 +00001102 vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync);
Fan Zhangf5395782020-04-29 14:00:03 +01001103 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001104 if (n_async)
Fan Zhangf5395782020-04-29 14:00:03 +01001105 {
Neale Rannsfc811342021-02-26 10:35:33 +00001106 /* submit all of the open frames */
1107 vnet_crypto_async_frame_t **async_frame;
1108
1109 vec_foreach (async_frame, ptd->async_frames)
Fan Zhang18f0e312020-10-19 13:08:34 +01001110 {
Neale Rannsfc811342021-02-26 10:35:33 +00001111 if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0)
1112 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001113 n_noop += esp_async_recycle_failed_submit (
1114 vm, *async_frame, node, ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR,
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001115 IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR, n_noop, noop_bi,
Xiaoming Jiang0c1454c2023-05-05 02:28:20 +00001116 noop_nexts, drop_next, true);
Neale Rannsfc811342021-02-26 10:35:33 +00001117 vnet_crypto_async_reset_frame (*async_frame);
1118 vnet_crypto_async_free_frame (vm, *async_frame);
1119 }
Fan Zhang18f0e312020-10-19 13:08:34 +01001120 }
Fan Zhangf5395782020-04-29 14:00:03 +01001121 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001122 if (n_noop)
1123 vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
1124
1125 vlib_node_increment_counter (vm, node->node_index, ESP_ENCRYPT_ERROR_RX_PKTS,
1126 frame->n_vectors);
Damjan Marionc59b9a22019-03-19 15:38:40 +01001127
Damjan Marionc59b9a22019-03-19 15:38:40 +01001128 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129}
1130
Fan Zhangf5395782020-04-29 14:00:03 +01001131always_inline uword
1132esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1133 vlib_frame_t * frame)
1134{
1135 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1136 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1137 u32 *from = vlib_frame_vector_args (frame);
1138 u32 n_left = frame->n_vectors;
1139
1140 vlib_get_buffers (vm, from, b, n_left);
1141
1142 if (n_left >= 4)
1143 {
1144 vlib_prefetch_buffer_header (b[0], LOAD);
1145 vlib_prefetch_buffer_header (b[1], LOAD);
1146 vlib_prefetch_buffer_header (b[2], LOAD);
1147 vlib_prefetch_buffer_header (b[3], LOAD);
1148 }
1149
1150 while (n_left > 8)
1151 {
1152 vlib_prefetch_buffer_header (b[4], LOAD);
1153 vlib_prefetch_buffer_header (b[5], LOAD);
1154 vlib_prefetch_buffer_header (b[6], LOAD);
1155 vlib_prefetch_buffer_header (b[7], LOAD);
1156
1157 next[0] = (esp_post_data (b[0]))->next_index;
1158 next[1] = (esp_post_data (b[1]))->next_index;
1159 next[2] = (esp_post_data (b[2]))->next_index;
1160 next[3] = (esp_post_data (b[3]))->next_index;
1161
1162 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
1163 {
1164 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
1165 {
1166 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1167 sizeof (*tr));
1168 tr->next_index = next[0];
1169 }
1170 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
1171 {
1172 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1],
1173 sizeof (*tr));
1174 tr->next_index = next[1];
1175 }
1176 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
1177 {
1178 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2],
1179 sizeof (*tr));
1180 tr->next_index = next[2];
1181 }
1182 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
1183 {
1184 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3],
1185 sizeof (*tr));
1186 tr->next_index = next[3];
1187 }
1188 }
1189
1190 b += 4;
1191 next += 4;
1192 n_left -= 4;
1193 }
1194
1195 while (n_left > 0)
1196 {
1197 next[0] = (esp_post_data (b[0]))->next_index;
1198 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1199 {
1200 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1201 sizeof (*tr));
1202 tr->next_index = next[0];
1203 }
1204
1205 b += 1;
1206 next += 1;
1207 n_left -= 1;
1208 }
1209
1210 vlib_node_increment_counter (vm, node->node_index,
1211 ESP_ENCRYPT_ERROR_POST_RX_PKTS,
1212 frame->n_vectors);
1213 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1214 return frame->n_vectors;
1215}
1216
Klement Sekerab8f35442018-10-29 13:38:19 +01001217VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
1218 vlib_node_runtime_t * node,
1219 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001220{
Neale Ranns4a58e492020-12-21 13:19:10 +00001221 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001222 esp_encrypt_async_next.esp4_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001223}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001224
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001225/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001226VLIB_REGISTER_NODE (esp4_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001227 .name = "esp4-encrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228 .vector_size = sizeof (u32),
1229 .format_trace = format_esp_encrypt_trace,
1230 .type = VLIB_NODE_TYPE_INTERNAL,
1231
Neale Ranns93688d72022-08-09 03:34:51 +00001232 .n_errors = ESP_ENCRYPT_N_ERROR,
1233 .error_counters = esp_encrypt_error_counters,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001234
1235 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Ranns4a58e492020-12-21 13:19:10 +00001236 .next_nodes = { [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1237 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1238 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1239 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-handoff",
1240 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-handoff",
1241 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "error-drop",
1242 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output" },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001244/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001245
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
1253/* *INDENT-OFF* */
1254VLIB_REGISTER_NODE (esp4_encrypt_post_node) = {
1255 .name = "esp4-encrypt-post",
1256 .vector_size = sizeof (u32),
1257 .format_trace = format_esp_post_encrypt_trace,
1258 .type = VLIB_NODE_TYPE_INTERNAL,
1259 .sibling_of = "esp4-encrypt",
1260
Neale Ranns93688d72022-08-09 03:34:51 +00001261 .n_errors = ESP_ENCRYPT_N_ERROR,
1262 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001263};
1264/* *INDENT-ON* */
1265
Klement Sekerab8f35442018-10-29 13:38:19 +01001266VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
1267 vlib_node_runtime_t * node,
1268 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001269{
Neale Ranns4a58e492020-12-21 13:19:10 +00001270 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001271 esp_encrypt_async_next.esp6_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001272}
1273
1274/* *INDENT-OFF* */
1275VLIB_REGISTER_NODE (esp6_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001276 .name = "esp6-encrypt",
1277 .vector_size = sizeof (u32),
1278 .format_trace = format_esp_encrypt_trace,
1279 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001280 .sibling_of = "esp4-encrypt",
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001281
Neale Ranns93688d72022-08-09 03:34:51 +00001282 .n_errors = ESP_ENCRYPT_N_ERROR,
1283 .error_counters = esp_encrypt_error_counters,
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001284};
1285/* *INDENT-ON* */
1286
Fan Zhangf5395782020-04-29 14:00:03 +01001287VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm,
1288 vlib_node_runtime_t * node,
1289 vlib_frame_t * from_frame)
1290{
1291 return esp_encrypt_post_inline (vm, node, from_frame);
1292}
1293
1294/* *INDENT-OFF* */
1295VLIB_REGISTER_NODE (esp6_encrypt_post_node) = {
1296 .name = "esp6-encrypt-post",
1297 .vector_size = sizeof (u32),
1298 .format_trace = format_esp_post_encrypt_trace,
1299 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001300 .sibling_of = "esp4-encrypt",
Fan Zhangf5395782020-04-29 14:00:03 +01001301
Neale Ranns93688d72022-08-09 03:34:51 +00001302 .n_errors = ESP_ENCRYPT_N_ERROR,
1303 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001304};
1305/* *INDENT-ON* */
1306
Neale Ranns25edf142019-03-22 08:12:48 +00001307VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
1308 vlib_node_runtime_t * node,
1309 vlib_frame_t * from_frame)
1310{
Neale Ranns4a58e492020-12-21 13:19:10 +00001311 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001312 esp_encrypt_async_next.esp4_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001313}
1314
1315/* *INDENT-OFF* */
1316VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
1317 .name = "esp4-encrypt-tun",
1318 .vector_size = sizeof (u32),
1319 .format_trace = format_esp_encrypt_trace,
1320 .type = VLIB_NODE_TYPE_INTERNAL,
1321
Neale Ranns93688d72022-08-09 03:34:51 +00001322 .n_errors = ESP_ENCRYPT_N_ERROR,
1323 .error_counters = esp_encrypt_error_counters,
Neale Rannsd7603d92019-03-28 08:56:10 +00001324
Neale Rannsf62a8c02019-04-02 08:13:33 +00001325 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001326 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001327 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1328 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001329 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001330 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001331 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1332 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001333 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001334 },
Neale Ranns25edf142019-03-22 08:12:48 +00001335};
1336
Fan Zhangf5395782020-04-29 14:00:03 +01001337VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm,
1338 vlib_node_runtime_t * node,
1339 vlib_frame_t * from_frame)
1340{
1341 return esp_encrypt_post_inline (vm, node, from_frame);
1342}
1343
1344/* *INDENT-OFF* */
1345VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = {
1346 .name = "esp4-encrypt-tun-post",
1347 .vector_size = sizeof (u32),
1348 .format_trace = format_esp_post_encrypt_trace,
1349 .type = VLIB_NODE_TYPE_INTERNAL,
1350 .sibling_of = "esp4-encrypt-tun",
1351
Neale Ranns93688d72022-08-09 03:34:51 +00001352 .n_errors = ESP_ENCRYPT_N_ERROR,
1353 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001354};
Neale Ranns25edf142019-03-22 08:12:48 +00001355/* *INDENT-ON* */
1356
1357VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
1358 vlib_node_runtime_t * node,
1359 vlib_frame_t * from_frame)
1360{
Neale Ranns4a58e492020-12-21 13:19:10 +00001361 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001362 esp_encrypt_async_next.esp6_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001363}
1364
1365/* *INDENT-OFF* */
1366VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
1367 .name = "esp6-encrypt-tun",
1368 .vector_size = sizeof (u32),
1369 .format_trace = format_esp_encrypt_trace,
1370 .type = VLIB_NODE_TYPE_INTERNAL,
1371
Neale Ranns93688d72022-08-09 03:34:51 +00001372 .n_errors = ESP_ENCRYPT_N_ERROR,
1373 .error_counters = esp_encrypt_error_counters,
Neale Rannsd7603d92019-03-28 08:56:10 +00001374
Neale Rannsf62a8c02019-04-02 08:13:33 +00001375 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001376 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001377 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1378 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001379 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1380 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001381 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001382 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001383 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001384 },
Neale Ranns25edf142019-03-22 08:12:48 +00001385};
1386
Neale Ranns25edf142019-03-22 08:12:48 +00001387/* *INDENT-ON* */
1388
Fan Zhangf5395782020-04-29 14:00:03 +01001389VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm,
1390 vlib_node_runtime_t * node,
1391 vlib_frame_t * from_frame)
1392{
1393 return esp_encrypt_post_inline (vm, node, from_frame);
1394}
1395
1396/* *INDENT-OFF* */
1397VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = {
1398 .name = "esp6-encrypt-tun-post",
1399 .vector_size = sizeof (u32),
1400 .format_trace = format_esp_post_encrypt_trace,
1401 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Ranns4a58e492020-12-21 13:19:10 +00001402 .sibling_of = "esp-mpls-encrypt-tun",
Fan Zhangf5395782020-04-29 14:00:03 +01001403
Neale Ranns93688d72022-08-09 03:34:51 +00001404 .n_errors = ESP_ENCRYPT_N_ERROR,
1405 .error_counters = esp_encrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001406};
1407/* *INDENT-ON* */
1408
Neale Ranns4a58e492020-12-21 13:19:10 +00001409VLIB_NODE_FN (esp_mpls_encrypt_tun_node)
1410(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1411{
1412 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_MPLS, 1,
1413 esp_encrypt_async_next.esp_mpls_tun_post_next);
1414}
1415
1416VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_node) = {
1417 .name = "esp-mpls-encrypt-tun",
1418 .vector_size = sizeof (u32),
1419 .format_trace = format_esp_encrypt_trace,
1420 .type = VLIB_NODE_TYPE_INTERNAL,
1421
Neale Ranns93688d72022-08-09 03:34:51 +00001422 .n_errors = ESP_ENCRYPT_N_ERROR,
1423 .error_counters = esp_encrypt_error_counters,
Neale Ranns4a58e492020-12-21 13:19:10 +00001424
1425 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1426 .next_nodes = {
1427 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1428 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1429 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1430 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1431 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1432 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1433 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1434 },
1435};
1436
1437VLIB_NODE_FN (esp_mpls_encrypt_tun_post_node)
1438(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1439{
1440 return esp_encrypt_post_inline (vm, node, from_frame);
1441}
1442
1443VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_post_node) = {
1444 .name = "esp-mpls-encrypt-tun-post",
1445 .vector_size = sizeof (u32),
1446 .format_trace = format_esp_post_encrypt_trace,
1447 .type = VLIB_NODE_TYPE_INTERNAL,
1448 .sibling_of = "esp-mpls-encrypt-tun",
1449
Neale Ranns93688d72022-08-09 03:34:51 +00001450 .n_errors = ESP_ENCRYPT_N_ERROR,
1451 .error_counters = esp_encrypt_error_counters,
Neale Ranns4a58e492020-12-21 13:19:10 +00001452};
1453
Neale Ranns2d498302021-02-25 08:38:58 +00001454#ifndef CLIB_MARCH_VARIANT
1455
1456static clib_error_t *
1457esp_encrypt_init (vlib_main_t *vm)
1458{
1459 ipsec_main_t *im = &ipsec_main;
1460
1461 im->esp4_enc_fq_index =
1462 vlib_frame_queue_main_init (esp4_encrypt_node.index, 0);
1463 im->esp6_enc_fq_index =
1464 vlib_frame_queue_main_init (esp6_encrypt_node.index, 0);
1465 im->esp4_enc_tun_fq_index =
1466 vlib_frame_queue_main_init (esp4_encrypt_tun_node.index, 0);
1467 im->esp6_enc_tun_fq_index =
1468 vlib_frame_queue_main_init (esp6_encrypt_tun_node.index, 0);
1469 im->esp_mpls_enc_tun_fq_index =
1470 vlib_frame_queue_main_init (esp_mpls_encrypt_tun_node.index, 0);
1471
1472 return 0;
1473}
1474
1475VLIB_INIT_FUNCTION (esp_encrypt_init);
1476
1477#endif
1478
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001479/*
1480 * fd.io coding-style-patch-verification: ON
1481 *
1482 * Local Variables:
1483 * eval: (c-set-style "gnu")
1484 * End:
1485 */