blob: 43d292d27e8ba162b6fb15d5c8c8844f6204f6d8 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * esp_decrypt.c : IPSec ESP decrypt 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 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070017#include <vnet/vnet.h>
18#include <vnet/api_errno.h>
19#include <vnet/ip/ip.h>
John Lo90430b62020-01-31 23:48:30 -050020#include <vnet/l2/l2_input.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021
22#include <vnet/ipsec/ipsec.h>
23#include <vnet/ipsec/esp.h>
Neale Ranns918c1612019-02-21 23:34:59 -080024#include <vnet/ipsec/ipsec_io.h>
Neale Rannsc87b66c2019-02-07 07:26:12 -080025#include <vnet/ipsec/ipsec_tun.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
Neale Ranns119c0d72020-11-26 13:12:37 +000027#include <vnet/gre/packet.h>
Neale Ranns568acbb2019-12-18 05:54:40 +000028
Neale Ranns4a58e492020-12-21 13:19:10 +000029#define foreach_esp_decrypt_next \
30 _ (DROP, "error-drop") \
31 _ (IP4_INPUT, "ip4-input-no-checksum") \
32 _ (IP6_INPUT, "ip6-input") \
33 _ (L2_INPUT, "l2-input") \
34 _ (MPLS_INPUT, "mpls-input") \
35 _ (HANDOFF, "handoff")
Ed Warnickecb9cada2015-12-08 15:45:58 -070036
37#define _(v, s) ESP_DECRYPT_NEXT_##v,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070038typedef enum
39{
Ed Warnickecb9cada2015-12-08 15:45:58 -070040 foreach_esp_decrypt_next
41#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070042 ESP_DECRYPT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070043} esp_decrypt_next_t;
44
Neale Ranns4a58e492020-12-21 13:19:10 +000045#define foreach_esp_decrypt_post_next \
46 _ (DROP, "error-drop") \
47 _ (IP4_INPUT, "ip4-input-no-checksum") \
48 _ (IP6_INPUT, "ip6-input") \
49 _ (MPLS_INPUT, "mpls-input") \
50 _ (L2_INPUT, "l2-input")
Fan Zhangf5395782020-04-29 14:00:03 +010051
52#define _(v, s) ESP_DECRYPT_POST_NEXT_##v,
53typedef enum
54{
55 foreach_esp_decrypt_post_next
56#undef _
57 ESP_DECRYPT_POST_N_NEXT,
58} esp_decrypt_post_next_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070060typedef struct
61{
Damjan Marionb4fff3a2019-03-25 15:54:40 +010062 u32 seq;
Neale Ranns6afaae12019-07-17 15:07:14 +000063 u32 sa_seq;
64 u32 sa_seq_hi;
Neale Ranns5b891102021-06-28 13:31:28 +000065 u32 pkt_seq_hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 ipsec_crypto_alg_t crypto_alg;
67 ipsec_integ_alg_t integ_alg;
68} esp_decrypt_trace_t;
69
Neale Ranns93688d72022-08-09 03:34:51 +000070typedef vl_counter_esp_decrypt_enum_t esp_decrypt_error_t;
71
Neale Ranns5b891102021-06-28 13:31:28 +000072/* The number of byres in the hisequence number */
73#define N_HI_ESN_BYTES 4
74
Ed Warnickecb9cada2015-12-08 15:45:58 -070075/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070076static u8 *
77format_esp_decrypt_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070078{
79 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
80 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070081 esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
Neale Ranns5b891102021-06-28 13:31:28 +000083 s = format (s,
84 "esp: crypto %U integrity %U pkt-seq %d sa-seq %u sa-seq-hi %u "
85 "pkt-seq-hi %u",
86 format_ipsec_crypto_alg, t->crypto_alg, format_ipsec_integ_alg,
87 t->integ_alg, t->seq, t->sa_seq, t->sa_seq_hi, t->pkt_seq_hi);
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 return s;
89}
90
Damjan Marionb4fff3a2019-03-25 15:54:40 +010091#define ESP_ENCRYPT_PD_F_FD_TRANSPORT (1 << 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
Filip Tehlarefcad1a2020-02-04 09:36:04 +000093static_always_inline void
94esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
95 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts,
96 int e)
97{
98 vnet_crypto_op_t *op = ops;
99 u32 n_fail, n_ops = vec_len (ops);
100
101 if (n_ops == 0)
102 return;
103
104 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
105
106 while (n_fail)
107 {
108 ASSERT (op - ops < n_ops);
109 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
110 {
111 u32 err, bi = op->user_data;
112 if (op->status == VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC)
113 err = e;
114 else
115 err = ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100116 esp_decrypt_set_next_index (b[bi], node, vm->thread_index, err, bi,
117 nexts, ESP_DECRYPT_NEXT_DROP,
118 vnet_buffer (b[bi])->ipsec.sad_index);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000119 n_fail--;
120 }
121 op++;
122 }
123}
124
125static_always_inline void
126esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
127 vnet_crypto_op_t * ops, vlib_buffer_t * b[],
128 u16 * nexts, vnet_crypto_op_chunk_t * chunks, int e)
129{
130
131 vnet_crypto_op_t *op = ops;
132 u32 n_fail, n_ops = vec_len (ops);
133
Neale Rannsf16e9a52021-02-25 19:09:24 +0000134 if (PREDICT_TRUE (n_ops == 0))
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000135 return;
136
137 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
138
139 while (n_fail)
140 {
141 ASSERT (op - ops < n_ops);
142 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
143 {
144 u32 err, bi = op->user_data;
145 if (op->status == VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC)
146 err = e;
147 else
148 err = ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100149 esp_decrypt_set_next_index (b[bi], node, vm->thread_index, err, bi,
150 nexts, ESP_DECRYPT_NEXT_DROP,
151 vnet_buffer (b[bi])->ipsec.sad_index);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000152 n_fail--;
153 }
154 op++;
155 }
156}
157
158always_inline void
159esp_remove_tail (vlib_main_t * vm, vlib_buffer_t * b, vlib_buffer_t * last,
160 u16 tail)
161{
162 vlib_buffer_t *before_last = b;
163
164 if (last->current_length > tail)
165 {
166 last->current_length -= tail;
167 return;
168 }
169 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
170
171 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
172 {
173 before_last = b;
174 b = vlib_get_buffer (vm, b->next_buffer);
175 }
176 before_last->current_length -= tail - last->current_length;
177 vlib_buffer_free_one (vm, before_last->next_buffer);
178 before_last->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
179}
180
181/* ICV is splitted in last two buffers so move it to the last buffer and
182 return pointer to it */
183static_always_inline u8 *
184esp_move_icv (vlib_main_t * vm, vlib_buffer_t * first,
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000185 esp_decrypt_packet_data_t * pd,
Fan Zhangf5395782020-04-29 14:00:03 +0100186 esp_decrypt_packet_data2_t * pd2, u16 icv_sz, u16 * dif)
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000187{
188 vlib_buffer_t *before_last, *bp;
Fan Zhangf5395782020-04-29 14:00:03 +0100189 u16 last_sz = pd2->lb->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000190 u16 first_sz = icv_sz - last_sz;
191
192 bp = before_last = first;
193 while (bp->flags & VLIB_BUFFER_NEXT_PRESENT)
194 {
195 before_last = bp;
196 bp = vlib_get_buffer (vm, bp->next_buffer);
197 }
198
Fan Zhangf5395782020-04-29 14:00:03 +0100199 u8 *lb_curr = vlib_buffer_get_current (pd2->lb);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000200 memmove (lb_curr + first_sz, lb_curr, last_sz);
201 clib_memcpy_fast (lb_curr, vlib_buffer_get_tail (before_last) - first_sz,
202 first_sz);
203 before_last->current_length -= first_sz;
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000204 if (before_last == first)
205 pd->current_length -= first_sz;
Fan Zhangf5395782020-04-29 14:00:03 +0100206 clib_memset (vlib_buffer_get_tail (before_last), 0, first_sz);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000207 if (dif)
208 dif[0] = first_sz;
Fan Zhangf5395782020-04-29 14:00:03 +0100209 pd2->lb = before_last;
210 pd2->icv_removed = 1;
211 pd2->free_buffer_index = before_last->next_buffer;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000212 before_last->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
213 return lb_curr;
214}
215
Neale Ranns5b891102021-06-28 13:31:28 +0000216static_always_inline u16
217esp_insert_esn (vlib_main_t *vm, ipsec_sa_t *sa, esp_decrypt_packet_data_t *pd,
218 esp_decrypt_packet_data2_t *pd2, u32 *data_len, u8 **digest,
219 u16 *len, vlib_buffer_t *b, u8 *payload)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000220{
221 if (!ipsec_sa_is_set_USE_ESN (sa))
Fan Zhangf5395782020-04-29 14:00:03 +0100222 return 0;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000223 /* shift ICV by 4 bytes to insert ESN */
Neale Ranns5b891102021-06-28 13:31:28 +0000224 u32 seq_hi = clib_host_to_net_u32 (pd->seq_hi);
225 u8 tmp[ESP_MAX_ICV_SIZE];
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000226
Fan Zhangf5395782020-04-29 14:00:03 +0100227 if (pd2->icv_removed)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000228 {
Fan Zhangf5395782020-04-29 14:00:03 +0100229 u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
Neale Ranns5b891102021-06-28 13:31:28 +0000230 if (space_left >= N_HI_ESN_BYTES)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000231 {
Neale Ranns5b891102021-06-28 13:31:28 +0000232 clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb), &seq_hi,
233 N_HI_ESN_BYTES);
234 *data_len += N_HI_ESN_BYTES;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000235 }
236 else
Neale Ranns5b891102021-06-28 13:31:28 +0000237 return N_HI_ESN_BYTES;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000238
239 len[0] = b->current_length;
240 }
241 else
242 {
243 clib_memcpy_fast (tmp, payload + len[0], ESP_MAX_ICV_SIZE);
Neale Ranns5b891102021-06-28 13:31:28 +0000244 clib_memcpy_fast (payload + len[0], &seq_hi, N_HI_ESN_BYTES);
245 clib_memcpy_fast (payload + len[0] + N_HI_ESN_BYTES, tmp,
246 ESP_MAX_ICV_SIZE);
247 *data_len += N_HI_ESN_BYTES;
248 *digest += N_HI_ESN_BYTES;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000249 }
Neale Ranns5b891102021-06-28 13:31:28 +0000250 return N_HI_ESN_BYTES;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000251}
252
253static_always_inline u8 *
254esp_move_icv_esn (vlib_main_t * vm, vlib_buffer_t * first,
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000255 esp_decrypt_packet_data_t * pd,
Fan Zhangf5395782020-04-29 14:00:03 +0100256 esp_decrypt_packet_data2_t * pd2, u16 icv_sz,
257 ipsec_sa_t * sa, u8 * extra_esn, u32 * len)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000258{
259 u16 dif = 0;
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000260 u8 *digest = esp_move_icv (vm, first, pd, pd2, icv_sz, &dif);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000261 if (dif)
Fan Zhangf5395782020-04-29 14:00:03 +0100262 *len -= dif;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000263
264 if (ipsec_sa_is_set_USE_ESN (sa))
265 {
Neale Ranns5b891102021-06-28 13:31:28 +0000266 u32 seq_hi = clib_host_to_net_u32 (pd->seq_hi);
Fan Zhangf5395782020-04-29 14:00:03 +0100267 u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000268
Neale Ranns5b891102021-06-28 13:31:28 +0000269 if (space_left >= N_HI_ESN_BYTES)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000270 {
Neale Ranns5b891102021-06-28 13:31:28 +0000271 clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb), &seq_hi,
272 N_HI_ESN_BYTES);
273 *len += N_HI_ESN_BYTES;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000274 }
275 else
276 {
277 /* no space for ESN at the tail, use the next buffer
278 * (with ICV data) */
Fan Zhangf5395782020-04-29 14:00:03 +0100279 ASSERT (pd2->icv_removed);
280 vlib_buffer_t *tmp = vlib_get_buffer (vm, pd2->free_buffer_index);
Neale Ranns5b891102021-06-28 13:31:28 +0000281 clib_memcpy_fast (vlib_buffer_get_current (tmp) - N_HI_ESN_BYTES,
282 &seq_hi, N_HI_ESN_BYTES);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000283 extra_esn[0] = 1;
284 }
285 }
286 return digest;
287}
288
Fan Zhangf5395782020-04-29 14:00:03 +0100289static_always_inline int
Neale Ranns5b891102021-06-28 13:31:28 +0000290esp_decrypt_chain_integ (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
291 const esp_decrypt_packet_data_t *pd,
292 esp_decrypt_packet_data2_t *pd2, ipsec_sa_t *sa0,
293 vlib_buffer_t *b, u8 icv_sz, u8 *start_src,
294 u32 start_len, u8 **digest, u16 *n_ch,
295 u32 *integ_total_len)
Fan Zhangf5395782020-04-29 14:00:03 +0100296{
297 vnet_crypto_op_chunk_t *ch;
298 vlib_buffer_t *cb = vlib_get_buffer (vm, b->next_buffer);
299 u16 n_chunks = 1;
300 u32 total_len;
301 vec_add2 (ptd->chunks, ch, 1);
302 total_len = ch->len = start_len;
303 ch->src = start_src;
304
305 while (1)
306 {
307 vec_add2 (ptd->chunks, ch, 1);
308 n_chunks += 1;
309 ch->src = vlib_buffer_get_current (cb);
310 if (pd2->lb == cb)
311 {
312 if (pd2->icv_removed)
313 ch->len = cb->current_length;
314 else
315 ch->len = cb->current_length - icv_sz;
316 if (ipsec_sa_is_set_USE_ESN (sa0))
317 {
Neale Ranns5b891102021-06-28 13:31:28 +0000318 u32 seq_hi = clib_host_to_net_u32 (pd->seq_hi);
319 u8 tmp[ESP_MAX_ICV_SIZE];
Fan Zhangf5395782020-04-29 14:00:03 +0100320 u8 *esn;
321 vlib_buffer_t *tmp_b;
322 u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
Neale Ranns5b891102021-06-28 13:31:28 +0000323 if (space_left < N_HI_ESN_BYTES)
Fan Zhangf5395782020-04-29 14:00:03 +0100324 {
325 if (pd2->icv_removed)
326 {
327 /* use pre-data area from the last bufer
328 that was removed from the chain */
329 tmp_b = vlib_get_buffer (vm, pd2->free_buffer_index);
Neale Ranns5b891102021-06-28 13:31:28 +0000330 esn = tmp_b->data - N_HI_ESN_BYTES;
Fan Zhangf5395782020-04-29 14:00:03 +0100331 }
332 else
333 {
334 /* no space, need to allocate new buffer */
335 u32 tmp_bi = 0;
336 if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1)
337 return -1;
338 tmp_b = vlib_get_buffer (vm, tmp_bi);
339 esn = tmp_b->data;
340 pd2->free_buffer_index = tmp_bi;
341 }
Neale Ranns5b891102021-06-28 13:31:28 +0000342 clib_memcpy_fast (esn, &seq_hi, N_HI_ESN_BYTES);
Fan Zhangf5395782020-04-29 14:00:03 +0100343
344 vec_add2 (ptd->chunks, ch, 1);
345 n_chunks += 1;
346 ch->src = esn;
Neale Ranns5b891102021-06-28 13:31:28 +0000347 ch->len = N_HI_ESN_BYTES;
Fan Zhangf5395782020-04-29 14:00:03 +0100348 }
349 else
350 {
351 if (pd2->icv_removed)
352 {
Neale Ranns5b891102021-06-28 13:31:28 +0000353 clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb),
354 &seq_hi, N_HI_ESN_BYTES);
Fan Zhangf5395782020-04-29 14:00:03 +0100355 }
356 else
357 {
358 clib_memcpy_fast (tmp, *digest, ESP_MAX_ICV_SIZE);
Neale Ranns5b891102021-06-28 13:31:28 +0000359 clib_memcpy_fast (*digest, &seq_hi, N_HI_ESN_BYTES);
360 clib_memcpy_fast (*digest + N_HI_ESN_BYTES, tmp,
361 ESP_MAX_ICV_SIZE);
362 *digest += N_HI_ESN_BYTES;
Fan Zhangf5395782020-04-29 14:00:03 +0100363 }
Neale Ranns5b891102021-06-28 13:31:28 +0000364 ch->len += N_HI_ESN_BYTES;
Fan Zhangf5395782020-04-29 14:00:03 +0100365 }
366 }
367 total_len += ch->len;
368 break;
369 }
370 else
371 total_len += ch->len = cb->current_length;
372
373 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
374 break;
375
376 cb = vlib_get_buffer (vm, cb->next_buffer);
377 }
378
379 if (n_ch)
380 *n_ch = n_chunks;
381 if (integ_total_len)
382 *integ_total_len = total_len;
383
384 return 0;
385}
386
387static_always_inline u32
388esp_decrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000389 esp_decrypt_packet_data_t * pd,
Fan Zhangf5395782020-04-29 14:00:03 +0100390 esp_decrypt_packet_data2_t * pd2,
391 ipsec_sa_t * sa0, vlib_buffer_t * b, u8 icv_sz,
392 u8 * start, u32 start_len, u8 ** tag, u16 * n_ch)
393{
394 vnet_crypto_op_chunk_t *ch;
395 vlib_buffer_t *cb = b;
396 u16 n_chunks = 1;
397 u32 total_len;
398 vec_add2 (ptd->chunks, ch, 1);
399 total_len = ch->len = start_len;
400 ch->src = ch->dst = start;
401 cb = vlib_get_buffer (vm, cb->next_buffer);
402 n_chunks = 1;
403
404 while (1)
405 {
406 vec_add2 (ptd->chunks, ch, 1);
407 n_chunks += 1;
408 ch->src = ch->dst = vlib_buffer_get_current (cb);
409 if (pd2->lb == cb)
410 {
411 if (ipsec_sa_is_set_IS_AEAD (sa0))
412 {
413 if (pd2->lb->current_length < icv_sz)
414 {
415 u16 dif = 0;
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000416 *tag = esp_move_icv (vm, b, pd, pd2, icv_sz, &dif);
Fan Zhangf5395782020-04-29 14:00:03 +0100417
418 /* this chunk does not contain crypto data */
419 n_chunks -= 1;
420 /* and fix previous chunk's length as it might have
421 been changed */
422 ASSERT (n_chunks > 0);
423 if (pd2->lb == b)
424 {
425 total_len -= dif;
426 ch[-1].len -= dif;
427 }
428 else
429 {
430 total_len = total_len + pd2->lb->current_length -
431 ch[-1].len;
432 ch[-1].len = pd2->lb->current_length;
433 }
434 break;
435 }
436 else
437 *tag = vlib_buffer_get_tail (pd2->lb) - icv_sz;
438 }
439
440 if (pd2->icv_removed)
441 total_len += ch->len = cb->current_length;
442 else
443 total_len += ch->len = cb->current_length - icv_sz;
444 }
445 else
446 total_len += ch->len = cb->current_length;
447
448 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
449 break;
450
451 cb = vlib_get_buffer (vm, cb->next_buffer);
452 }
453
454 if (n_ch)
455 *n_ch = n_chunks;
456
457 return total_len;
458}
459
460static_always_inline void
461esp_decrypt_prepare_sync_op (vlib_main_t * vm, vlib_node_runtime_t * node,
462 ipsec_per_thread_data_t * ptd,
463 vnet_crypto_op_t *** crypto_ops,
464 vnet_crypto_op_t *** integ_ops,
465 vnet_crypto_op_t * op,
466 ipsec_sa_t * sa0, u8 * payload,
467 u16 len, u8 icv_sz, u8 iv_sz,
468 esp_decrypt_packet_data_t * pd,
469 esp_decrypt_packet_data2_t * pd2,
470 vlib_buffer_t * b, u16 * next, u32 index)
471{
472 const u8 esp_sz = sizeof (esp_header_t);
473
474 if (PREDICT_TRUE (sa0->integ_op_id != VNET_CRYPTO_OP_NONE))
475 {
476 vnet_crypto_op_init (op, sa0->integ_op_id);
477 op->key_index = sa0->integ_key_index;
478 op->src = payload;
479 op->flags = VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
480 op->user_data = index;
481 op->digest = payload + len;
482 op->digest_len = icv_sz;
483 op->len = len;
484
485 if (pd->is_chain)
486 {
487 /* buffer is chained */
488 op->len = pd->current_length;
489
490 /* special case when ICV is splitted and needs to be reassembled
491 * first -> move it to the last buffer. Also take into account
492 * that ESN needs to be added after encrypted data and may or
493 * may not fit in the tail.*/
494 if (pd2->lb->current_length < icv_sz)
495 {
496 u8 extra_esn = 0;
497 op->digest =
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000498 esp_move_icv_esn (vm, b, pd, pd2, icv_sz, sa0,
Fan Zhangf5395782020-04-29 14:00:03 +0100499 &extra_esn, &op->len);
500
501 if (extra_esn)
502 {
503 /* esn is in the last buffer, that was unlinked from
504 * the chain */
505 op->len = b->current_length;
506 }
507 else
508 {
509 if (pd2->lb == b)
510 {
511 /* we now have a single buffer of crypto data, adjust
512 * the length (second buffer contains only ICV) */
513 *integ_ops = &ptd->integ_ops;
514 *crypto_ops = &ptd->crypto_ops;
515 len = b->current_length;
516 goto out;
517 }
518 }
519 }
520 else
521 op->digest = vlib_buffer_get_tail (pd2->lb) - icv_sz;
522
523 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
524 op->chunk_index = vec_len (ptd->chunks);
Neale Ranns5b891102021-06-28 13:31:28 +0000525 if (esp_decrypt_chain_integ (vm, ptd, pd, pd2, sa0, b, icv_sz,
Fan Zhangf5395782020-04-29 14:00:03 +0100526 payload, pd->current_length,
527 &op->digest, &op->n_chunks, 0) < 0)
528 {
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100529 esp_decrypt_set_next_index (
530 b, node, vm->thread_index, ESP_DECRYPT_ERROR_NO_BUFFERS, 0,
531 next, ESP_DECRYPT_NEXT_DROP, pd->sa_index);
Fan Zhangf5395782020-04-29 14:00:03 +0100532 return;
533 }
534 }
535 else
Neale Ranns5b891102021-06-28 13:31:28 +0000536 esp_insert_esn (vm, sa0, pd, pd2, &op->len, &op->digest, &len, b,
Fan Zhangf5395782020-04-29 14:00:03 +0100537 payload);
538 out:
539 vec_add_aligned (*(integ_ops[0]), op, 1, CLIB_CACHE_LINE_BYTES);
540 }
541
542 payload += esp_sz;
543 len -= esp_sz;
544
545 if (sa0->crypto_dec_op_id != VNET_CRYPTO_OP_NONE)
546 {
547 vnet_crypto_op_init (op, sa0->crypto_dec_op_id);
548 op->key_index = sa0->crypto_key_index;
549 op->iv = payload;
550
Benoît Ganne490b9272021-01-22 18:03:09 +0100551 if (ipsec_sa_is_set_IS_CTR (sa0))
Fan Zhangf5395782020-04-29 14:00:03 +0100552 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100553 /* construct nonce in a scratch space in front of the IP header */
554 esp_ctr_nonce_t *nonce =
555 (esp_ctr_nonce_t *) (payload - esp_sz - pd->hdr_sz -
556 sizeof (*nonce));
557 if (ipsec_sa_is_set_IS_AEAD (sa0))
558 {
559 /* constuct aad in a scratch space in front of the nonce */
560 esp_header_t *esp0 = (esp_header_t *) (payload - esp_sz);
561 op->aad = (u8 *) nonce - sizeof (esp_aead_t);
Neale Ranns5b891102021-06-28 13:31:28 +0000562 op->aad_len = esp_aad_fill (op->aad, esp0, sa0, pd->seq_hi);
Benoît Ganne490b9272021-01-22 18:03:09 +0100563 op->tag = payload + len;
564 op->tag_len = 16;
565 }
566 else
567 {
568 nonce->ctr = clib_host_to_net_u32 (1);
569 }
570 nonce->salt = sa0->salt;
571 ASSERT (sizeof (u64) == iv_sz);
572 nonce->iv = *(u64 *) op->iv;
573 op->iv = (u8 *) nonce;
Fan Zhangf5395782020-04-29 14:00:03 +0100574 }
575 op->src = op->dst = payload += iv_sz;
576 op->len = len - iv_sz;
577 op->user_data = index;
578
579 if (pd->is_chain && (pd2->lb != b))
580 {
581 /* buffer is chained */
582 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
583 op->chunk_index = vec_len (ptd->chunks);
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000584 esp_decrypt_chain_crypto (vm, ptd, pd, pd2, sa0, b, icv_sz,
Fan Zhangf5395782020-04-29 14:00:03 +0100585 payload, len - pd->iv_sz + pd->icv_sz,
586 &op->tag, &op->n_chunks);
587 }
588
589 vec_add_aligned (*(crypto_ops[0]), op, 1, CLIB_CACHE_LINE_BYTES);
590 }
591}
592
Neale Rannsfc811342021-02-26 10:35:33 +0000593static_always_inline esp_decrypt_error_t
594esp_decrypt_prepare_async_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
595 ipsec_per_thread_data_t *ptd,
596 vnet_crypto_async_frame_t *f, ipsec_sa_t *sa0,
597 u8 *payload, u16 len, u8 icv_sz, u8 iv_sz,
598 esp_decrypt_packet_data_t *pd,
599 esp_decrypt_packet_data2_t *pd2, u32 bi,
600 vlib_buffer_t *b, u16 *next, u16 async_next)
Fan Zhangf5395782020-04-29 14:00:03 +0100601{
602 const u8 esp_sz = sizeof (esp_header_t);
Fan Zhangf5395782020-04-29 14:00:03 +0100603 esp_decrypt_packet_data_t *async_pd = &(esp_post_data (b))->decrypt_data;
604 esp_decrypt_packet_data2_t *async_pd2 = esp_post_data2 (b);
605 u8 *tag = payload + len, *iv = payload + esp_sz, *aad = 0;
Benoît Ganne5527a782022-01-18 15:56:41 +0100606 const u32 key_index = sa0->crypto_key_index;
Fan Zhangf5395782020-04-29 14:00:03 +0100607 u32 crypto_len, integ_len = 0;
608 i16 crypto_start_offset, integ_start_offset = 0;
609 u8 flags = 0;
610
611 if (!ipsec_sa_is_set_IS_AEAD (sa0))
612 {
613 /* linked algs */
Fan Zhangf5395782020-04-29 14:00:03 +0100614 integ_start_offset = payload - b->data;
615 integ_len = len;
Benoît Ganne48524a92021-01-22 18:11:37 +0100616 if (PREDICT_TRUE (sa0->integ_op_id != VNET_CRYPTO_OP_NONE))
617 flags |= VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
Fan Zhangf5395782020-04-29 14:00:03 +0100618
619 if (pd->is_chain)
620 {
621 /* buffer is chained */
622 integ_len = pd->current_length;
623
624 /* special case when ICV is splitted and needs to be reassembled
625 * first -> move it to the last buffer. Also take into account
626 * that ESN needs to be added after encrypted data and may or
627 * may not fit in the tail.*/
628 if (pd2->lb->current_length < icv_sz)
629 {
630 u8 extra_esn = 0;
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000631 tag = esp_move_icv_esn (vm, b, pd, pd2, icv_sz, sa0,
Fan Zhangf5395782020-04-29 14:00:03 +0100632 &extra_esn, &integ_len);
633
634 if (extra_esn)
635 {
636 /* esn is in the last buffer, that was unlinked from
637 * the chain */
638 integ_len = b->current_length;
639 }
640 else
641 {
642 if (pd2->lb == b)
643 {
644 /* we now have a single buffer of crypto data, adjust
645 * the length (second buffer contains only ICV) */
646 len = b->current_length;
647 goto out;
648 }
649 }
650 }
651 else
652 tag = vlib_buffer_get_tail (pd2->lb) - icv_sz;
653
654 flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
Neale Ranns5b891102021-06-28 13:31:28 +0000655 if (esp_decrypt_chain_integ (vm, ptd, pd, pd2, sa0, b, icv_sz,
656 payload, pd->current_length, &tag, 0,
657 &integ_len) < 0)
Fan Zhangf5395782020-04-29 14:00:03 +0100658 {
659 /* allocate buffer failed, will not add to frame and drop */
Neale Rannsfc811342021-02-26 10:35:33 +0000660 return (ESP_DECRYPT_ERROR_NO_BUFFERS);
Fan Zhangf5395782020-04-29 14:00:03 +0100661 }
662 }
663 else
Neale Ranns5b891102021-06-28 13:31:28 +0000664 esp_insert_esn (vm, sa0, pd, pd2, &integ_len, &tag, &len, b, payload);
Fan Zhangf5395782020-04-29 14:00:03 +0100665 }
Fan Zhangf5395782020-04-29 14:00:03 +0100666
667out:
668 /* crypto */
669 payload += esp_sz;
670 len -= esp_sz;
671 iv = payload;
672
Benoît Ganne490b9272021-01-22 18:03:09 +0100673 if (ipsec_sa_is_set_IS_CTR (sa0))
Fan Zhangf5395782020-04-29 14:00:03 +0100674 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100675 /* construct nonce in a scratch space in front of the IP header */
676 esp_ctr_nonce_t *nonce =
677 (esp_ctr_nonce_t *) (payload - esp_sz - pd->hdr_sz - sizeof (*nonce));
678 if (ipsec_sa_is_set_IS_AEAD (sa0))
679 {
680 /* constuct aad in a scratch space in front of the nonce */
681 esp_header_t *esp0 = (esp_header_t *) (payload - esp_sz);
682 aad = (u8 *) nonce - sizeof (esp_aead_t);
Neale Ranns5b891102021-06-28 13:31:28 +0000683 esp_aad_fill (aad, esp0, sa0, pd->seq_hi);
Benoît Ganne490b9272021-01-22 18:03:09 +0100684 tag = payload + len;
685 }
686 else
687 {
688 nonce->ctr = clib_host_to_net_u32 (1);
689 }
690 nonce->salt = sa0->salt;
691 ASSERT (sizeof (u64) == iv_sz);
692 nonce->iv = *(u64 *) iv;
693 iv = (u8 *) nonce;
Fan Zhangf5395782020-04-29 14:00:03 +0100694 }
695
696 crypto_start_offset = (payload += iv_sz) - b->data;
697 crypto_len = len - iv_sz;
698
699 if (pd->is_chain && (pd2->lb != b))
700 {
701 /* buffer is chained */
702 flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
703
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000704 crypto_len = esp_decrypt_chain_crypto (vm, ptd, pd, pd2, sa0, b, icv_sz,
Fan Zhangf5395782020-04-29 14:00:03 +0100705 payload,
706 len - pd->iv_sz + pd->icv_sz,
707 &tag, 0);
708 }
709
710 *async_pd = *pd;
711 *async_pd2 = *pd2;
Fan Zhangf5395782020-04-29 14:00:03 +0100712
713 /* for AEAD integ_len - crypto_len will be negative, it is ok since it
714 * is ignored by the engine. */
Neale Rannsfc811342021-02-26 10:35:33 +0000715 vnet_crypto_async_add_to_frame (
716 vm, f, key_index, crypto_len, integ_len - crypto_len, crypto_start_offset,
717 integ_start_offset, bi, async_next, iv, tag, aad, flags);
718
719 return (ESP_DECRYPT_ERROR_RX_PKTS);
Fan Zhangf5395782020-04-29 14:00:03 +0100720}
721
722static_always_inline void
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100723esp_decrypt_post_crypto (vlib_main_t *vm, vlib_node_runtime_t *node,
Benoît Ganne98ca76a2022-04-11 18:51:25 +0200724 const u16 *next_by_next_header,
Neale Rannse11203e2021-09-21 12:34:19 +0000725 const esp_decrypt_packet_data_t *pd,
726 const esp_decrypt_packet_data2_t *pd2,
727 vlib_buffer_t *b, u16 *next, int is_ip6, int is_tun,
728 int is_async)
Fan Zhangf5395782020-04-29 14:00:03 +0100729{
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000730 ipsec_sa_t *sa0 = ipsec_sa_get (pd->sa_index);
Fan Zhangf5395782020-04-29 14:00:03 +0100731 vlib_buffer_t *lb = b;
732 const u8 esp_sz = sizeof (esp_header_t);
733 const u8 tun_flags = IPSEC_SA_FLAG_IS_TUNNEL | IPSEC_SA_FLAG_IS_TUNNEL_V6;
734 u8 pad_length = 0, next_header = 0;
735 u16 icv_sz;
736
737 /*
738 * redo the anti-reply check
739 * in this frame say we have sequence numbers, s, s+1, s+1, s+1
740 * and s and s+1 are in the window. When we did the anti-replay
741 * check above we did so against the state of the window (W),
742 * after packet s-1. So each of the packets in the sequence will be
743 * accepted.
744 * This time s will be cheked against Ws-1, s+1 chceked against Ws
745 * (i.e. the window state is updated/advnaced)
746 * so this time the successive s+! packet will be dropped.
747 * This is a consequence of batching the decrypts. If the
748 * check-dcrypt-advance process was done for each packet it would
749 * be fine. But we batch the decrypts because it's much more efficient
750 * to do so in SW and if we offload to HW and the process is async.
751 *
752 * You're probably thinking, but this means an attacker can send the
753 * above sequence and cause VPP to perform decrpyts that will fail,
754 * and that's true. But if the attacker can determine s (a valid
755 * sequence number in the window) which is non-trivial, it can generate
756 * a sequence s, s+1, s+2, s+3, ... s+n and nothing will prevent any
757 * implementation, sequential or batching, from decrypting these.
758 */
Neale Ranns5b891102021-06-28 13:31:28 +0000759 if (ipsec_sa_anti_replay_and_sn_advance (sa0, pd->seq, pd->seq_hi, true,
760 NULL))
Fan Zhangf5395782020-04-29 14:00:03 +0100761 {
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100762 esp_decrypt_set_next_index (b, node, vm->thread_index,
763 ESP_DECRYPT_ERROR_REPLAY, 0, next,
764 ESP_DECRYPT_NEXT_DROP, pd->sa_index);
Fan Zhangf5395782020-04-29 14:00:03 +0100765 return;
766 }
767
Neale Rannse11203e2021-09-21 12:34:19 +0000768 u64 n_lost =
769 ipsec_sa_anti_replay_advance (sa0, vm->thread_index, pd->seq, pd->seq_hi);
770
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100771 vlib_prefetch_simple_counter (&ipsec_sa_err_counters[IPSEC_SA_ERROR_LOST],
772 vm->thread_index, pd->sa_index);
Fan Zhangf5395782020-04-29 14:00:03 +0100773
774 if (pd->is_chain)
775 {
776 lb = pd2->lb;
777 icv_sz = pd2->icv_removed ? 0 : pd->icv_sz;
778 if (pd2->free_buffer_index)
779 {
780 vlib_buffer_free_one (vm, pd2->free_buffer_index);
781 lb->next_buffer = 0;
782 }
783 if (lb->current_length < sizeof (esp_footer_t) + icv_sz)
784 {
785 /* esp footer is either splitted in two buffers or in the before
786 * last buffer */
787
788 vlib_buffer_t *before_last = b, *bp = b;
789 while (bp->flags & VLIB_BUFFER_NEXT_PRESENT)
790 {
791 before_last = bp;
792 bp = vlib_get_buffer (vm, bp->next_buffer);
793 }
794 u8 *bt = vlib_buffer_get_tail (before_last);
795
796 if (lb->current_length == icv_sz)
797 {
798 esp_footer_t *f = (esp_footer_t *) (bt - sizeof (*f));
799 pad_length = f->pad_length;
800 next_header = f->next_header;
801 }
802 else
803 {
804 pad_length = (bt - 1)[0];
805 next_header = ((u8 *) vlib_buffer_get_current (lb))[0];
806 }
807 }
808 else
809 {
810 esp_footer_t *f =
811 (esp_footer_t *) (lb->data + lb->current_data +
812 lb->current_length - sizeof (esp_footer_t) -
813 icv_sz);
814 pad_length = f->pad_length;
815 next_header = f->next_header;
816 }
817 }
818 else
819 {
820 icv_sz = pd->icv_sz;
821 esp_footer_t *f =
822 (esp_footer_t *) (lb->data + lb->current_data + lb->current_length -
823 sizeof (esp_footer_t) - icv_sz);
824 pad_length = f->pad_length;
825 next_header = f->next_header;
826 }
827
828 u16 adv = pd->iv_sz + esp_sz;
829 u16 tail = sizeof (esp_footer_t) + pad_length + icv_sz;
830 u16 tail_orig = sizeof (esp_footer_t) + pad_length + pd->icv_sz;
831 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
832
833 if ((pd->flags & tun_flags) == 0 && !is_tun) /* transport mode */
834 {
835 u8 udp_sz = (is_ip6 == 0 && pd->flags & IPSEC_SA_FLAG_UDP_ENCAP) ?
836 sizeof (udp_header_t) : 0;
837 u16 ip_hdr_sz = pd->hdr_sz - udp_sz;
838 u8 *old_ip = b->data + pd->current_data - ip_hdr_sz - udp_sz;
839 u8 *ip = old_ip + adv + udp_sz;
840
841 if (is_ip6 && ip_hdr_sz > 64)
842 memmove (ip, old_ip, ip_hdr_sz);
843 else
844 clib_memcpy_le64 (ip, old_ip, ip_hdr_sz);
845
846 b->current_data = pd->current_data + adv - ip_hdr_sz;
847 b->current_length += ip_hdr_sz - adv;
848 esp_remove_tail (vm, b, lb, tail);
849
850 if (is_ip6)
851 {
852 ip6_header_t *ip6 = (ip6_header_t *) ip;
853 u16 len = clib_net_to_host_u16 (ip6->payload_length);
854 len -= adv + tail_orig;
855 ip6->payload_length = clib_host_to_net_u16 (len);
856 ip6->protocol = next_header;
857 next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
858 }
859 else
860 {
861 ip4_header_t *ip4 = (ip4_header_t *) ip;
862 ip_csum_t sum = ip4->checksum;
863 u16 len = clib_net_to_host_u16 (ip4->length);
864 len = clib_host_to_net_u16 (len - adv - tail_orig - udp_sz);
865 sum = ip_csum_update (sum, ip4->protocol, next_header,
866 ip4_header_t, protocol);
867 sum = ip_csum_update (sum, ip4->length, len, ip4_header_t, length);
868 ip4->checksum = ip_csum_fold (sum);
869 ip4->protocol = next_header;
870 ip4->length = len;
871 next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
872 }
873 }
874 else
875 {
876 if (PREDICT_TRUE (next_header == IP_PROTOCOL_IP_IN_IP))
877 {
878 next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
879 b->current_data = pd->current_data + adv;
880 b->current_length = pd->current_length - adv;
881 esp_remove_tail (vm, b, lb, tail);
882 }
883 else if (next_header == IP_PROTOCOL_IPV6)
884 {
885 next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
886 b->current_data = pd->current_data + adv;
887 b->current_length = pd->current_length - adv;
888 esp_remove_tail (vm, b, lb, tail);
889 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000890 else if (next_header == IP_PROTOCOL_MPLS_IN_IP)
891 {
892 next[0] = ESP_DECRYPT_NEXT_MPLS_INPUT;
893 b->current_data = pd->current_data + adv;
894 b->current_length = pd->current_length - adv;
895 esp_remove_tail (vm, b, lb, tail);
896 }
Benoît Ganne98ca76a2022-04-11 18:51:25 +0200897 else if (is_tun && next_header == IP_PROTOCOL_GRE)
Fan Zhangf5395782020-04-29 14:00:03 +0100898 {
Benoît Ganne98ca76a2022-04-11 18:51:25 +0200899 gre_header_t *gre;
900
901 b->current_data = pd->current_data + adv;
902 b->current_length = pd->current_length - adv - tail;
903
904 gre = vlib_buffer_get_current (b);
905
906 vlib_buffer_advance (b, sizeof (*gre));
907
908 switch (clib_net_to_host_u16 (gre->protocol))
Fan Zhangf5395782020-04-29 14:00:03 +0100909 {
Benoît Ganne98ca76a2022-04-11 18:51:25 +0200910 case GRE_PROTOCOL_teb:
911 vnet_update_l2_len (b);
912 next[0] = ESP_DECRYPT_NEXT_L2_INPUT;
913 break;
914 case GRE_PROTOCOL_ip4:
915 next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
916 break;
917 case GRE_PROTOCOL_ip6:
918 next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
919 break;
920 default:
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100921 esp_decrypt_set_next_index (
922 b, node, vm->thread_index, ESP_DECRYPT_ERROR_UNSUP_PAYLOAD, 0,
923 next, ESP_DECRYPT_NEXT_DROP, pd->sa_index);
Benoît Ganne98ca76a2022-04-11 18:51:25 +0200924 break;
Fan Zhangf5395782020-04-29 14:00:03 +0100925 }
926 }
Benoît Ganne98ca76a2022-04-11 18:51:25 +0200927 else if ((next[0] = vec_elt (next_by_next_header, next_header)) !=
928 (u16) ~0)
929 {
930 b->current_data = pd->current_data + adv;
931 b->current_length = pd->current_length - adv;
932 esp_remove_tail (vm, b, lb, tail);
933 }
934 else
935 {
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100936 esp_decrypt_set_next_index (b, node, vm->thread_index,
937 ESP_DECRYPT_ERROR_UNSUP_PAYLOAD, 0, next,
938 ESP_DECRYPT_NEXT_DROP, pd->sa_index);
Benoît Ganne98ca76a2022-04-11 18:51:25 +0200939 return;
940 }
941
Fan Zhangf5395782020-04-29 14:00:03 +0100942 if (is_tun)
943 {
944 if (ipsec_sa_is_set_IS_PROTECT (sa0))
945 {
946 /*
947 * There are two encap possibilities
948 * 1) the tunnel and ths SA are prodiving encap, i.e. it's
949 * MAC | SA-IP | TUN-IP | ESP | PAYLOAD
950 * implying the SA is in tunnel mode (on a tunnel interface)
951 * 2) only the tunnel provides encap
952 * MAC | TUN-IP | ESP | PAYLOAD
953 * implying the SA is in transport mode.
954 *
955 * For 2) we need only strip the tunnel encap and we're good.
956 * since the tunnel and crypto ecnap (int the tun=protect
957 * object) are the same and we verified above that these match
958 * for 1) we need to strip the SA-IP outer headers, to
959 * reveal the tunnel IP and then check that this matches
960 * the configured tunnel.
961 */
962 const ipsec_tun_protect_t *itp;
963
Neale Ranns5b891102021-06-28 13:31:28 +0000964 itp =
965 ipsec_tun_protect_get (vnet_buffer (b)->ipsec.protect_index);
Fan Zhangf5395782020-04-29 14:00:03 +0100966
967 if (PREDICT_TRUE (next_header == IP_PROTOCOL_IP_IN_IP))
968 {
969 const ip4_header_t *ip4;
970
971 ip4 = vlib_buffer_get_current (b);
972
973 if (!ip46_address_is_equal_v4 (&itp->itp_tun.src,
974 &ip4->dst_address) ||
975 !ip46_address_is_equal_v4 (&itp->itp_tun.dst,
976 &ip4->src_address))
977 {
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100978 esp_decrypt_set_next_index (
979 b, node, vm->thread_index,
980 ESP_DECRYPT_ERROR_TUN_NO_PROTO, 0, next,
981 ESP_DECRYPT_NEXT_DROP, pd->sa_index);
Fan Zhangf5395782020-04-29 14:00:03 +0100982 }
983 }
984 else if (next_header == IP_PROTOCOL_IPV6)
985 {
986 const ip6_header_t *ip6;
987
988 ip6 = vlib_buffer_get_current (b);
989
990 if (!ip46_address_is_equal_v6 (&itp->itp_tun.src,
991 &ip6->dst_address) ||
992 !ip46_address_is_equal_v6 (&itp->itp_tun.dst,
993 &ip6->src_address))
994 {
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100995 esp_decrypt_set_next_index (
996 b, node, vm->thread_index,
997 ESP_DECRYPT_ERROR_TUN_NO_PROTO, 0, next,
998 ESP_DECRYPT_NEXT_DROP, pd->sa_index);
Fan Zhangf5395782020-04-29 14:00:03 +0100999 }
1000 }
1001 }
1002 }
1003 }
Neale Rannse11203e2021-09-21 12:34:19 +00001004
1005 if (PREDICT_FALSE (n_lost))
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001006 vlib_increment_simple_counter (&ipsec_sa_err_counters[IPSEC_SA_ERROR_LOST],
1007 vm->thread_index, pd->sa_index, n_lost);
Fan Zhangf5395782020-04-29 14:00:03 +01001008}
1009
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001010always_inline uword
Neale Rannsf16e9a52021-02-25 19:09:24 +00001011esp_decrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1012 vlib_frame_t *from_frame, int is_ip6, int is_tun,
1013 u16 async_next_node)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015 ipsec_main_t *im = &ipsec_main;
Benoît Ganne98ca76a2022-04-11 18:51:25 +02001016 const u16 *next_by_next_header = im->next_header_registrations;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001017 u32 thread_index = vm->thread_index;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001018 u16 len;
1019 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index);
Damjan Marionc98275f2019-03-06 14:05:01 +01001020 u32 *from = vlib_frame_vector_args (from_frame);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001021 u32 n_left = from_frame->n_vectors;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001022 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001023 vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
1024 u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0;
Matthew Smith51d56ba2021-06-04 09:18:37 -05001025 u16 async_nexts[VLIB_FRAME_SIZE], *async_next = async_nexts;
Damjan Mariondd298e82022-10-12 16:02:18 +02001026 u16 noop_nexts[VLIB_FRAME_SIZE], n_noop = 0;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001027 u32 sync_bi[VLIB_FRAME_SIZE];
1028 u32 noop_bi[VLIB_FRAME_SIZE];
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001029 esp_decrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data;
Fan Zhangf5395782020-04-29 14:00:03 +01001030 esp_decrypt_packet_data2_t pkt_data2[VLIB_FRAME_SIZE], *pd2 = pkt_data2;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001031 esp_decrypt_packet_data_t cpd = { };
1032 u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0;
1033 const u8 esp_sz = sizeof (esp_header_t);
1034 ipsec_sa_t *sa0 = 0;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +00001035 vnet_crypto_op_t _op, *op = &_op;
Benoît Gannee631ece2021-05-27 18:49:42 +02001036 vnet_crypto_op_t **crypto_ops;
1037 vnet_crypto_op_t **integ_ops;
Fan Zhangf5395782020-04-29 14:00:03 +01001038 int is_async = im->async_mode;
Neale Rannsfc811342021-02-26 10:35:33 +00001039 vnet_crypto_async_op_id_t async_op = ~0;
Neale Rannsfc811342021-02-26 10:35:33 +00001040 vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
Neale Rannsf16e9a52021-02-25 19:09:24 +00001041 esp_decrypt_error_t err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001043 vlib_get_buffers (vm, from, b, n_left);
Fan Zhangf5395782020-04-29 14:00:03 +01001044 if (!is_async)
1045 {
1046 vec_reset_length (ptd->crypto_ops);
1047 vec_reset_length (ptd->integ_ops);
1048 vec_reset_length (ptd->chained_crypto_ops);
1049 vec_reset_length (ptd->chained_integ_ops);
1050 }
Neale Rannsfc811342021-02-26 10:35:33 +00001051 vec_reset_length (ptd->async_frames);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001052 vec_reset_length (ptd->chunks);
Neale Rannsf16e9a52021-02-25 19:09:24 +00001053 clib_memset (sync_nexts, -1, sizeof (sync_nexts));
Neale Rannsfc811342021-02-26 10:35:33 +00001054 clib_memset (async_frames, 0, sizeof (async_frames));
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001055
1056 while (n_left > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001057 {
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001058 u8 *payload;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059
Neale Rannsf16e9a52021-02-25 19:09:24 +00001060 err = ESP_DECRYPT_ERROR_RX_PKTS;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001061 if (n_left > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001062 {
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001063 u8 *p;
1064 vlib_prefetch_buffer_header (b[2], LOAD);
1065 p = vlib_buffer_get_current (b[1]);
Damjan Marionaf7fb042021-07-15 11:54:41 +02001066 clib_prefetch_load (p);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001067 p -= CLIB_CACHE_LINE_BYTES;
Damjan Marionaf7fb042021-07-15 11:54:41 +02001068 clib_prefetch_load (p);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001069 }
1070
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001071 u32 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
1072 if (n_bufs == 0)
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001073 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001074 err = ESP_DECRYPT_ERROR_NO_BUFFERS;
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001075 esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop,
1076 noop_nexts, ESP_DECRYPT_NEXT_DROP,
1077 vnet_buffer (b[0])->ipsec.sad_index);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001078 goto next;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001079 }
Damjan Marionc98275f2019-03-06 14:05:01 +01001080
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001081 if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index)
Damjan Marionc98275f2019-03-06 14:05:01 +01001082 {
Damjan Marion867dfdd2019-06-05 15:42:54 +02001083 if (current_sa_pkts)
1084 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001085 current_sa_index, current_sa_pkts,
Damjan Marion867dfdd2019-06-05 15:42:54 +02001086 current_sa_bytes);
1087 current_sa_bytes = current_sa_pkts = 0;
1088
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001089 current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001090 vlib_prefetch_combined_counter (&ipsec_sa_counters, thread_index,
1091 current_sa_index);
Neale Rannsc5fe57d2021-02-25 16:01:28 +00001092 sa0 = ipsec_sa_get (current_sa_index);
Neale Ranns123b5eb2020-10-16 14:03:55 +00001093
1094 /* fetch the second cacheline ASAP */
Damjan Marionaf7fb042021-07-15 11:54:41 +02001095 clib_prefetch_load (sa0->cacheline1);
Damjan Marion7c22ff72019-04-04 12:25:44 +02001096 cpd.icv_sz = sa0->integ_icv_size;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001097 cpd.iv_sz = sa0->crypto_iv_size;
1098 cpd.flags = sa0->flags;
1099 cpd.sa_index = current_sa_index;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001100 is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0);
Neale Rannsfc811342021-02-26 10:35:33 +00001101 }
Fan Zhangf5395782020-04-29 14:00:03 +01001102
Benoît Ganne5527a782022-01-18 15:56:41 +01001103 if (PREDICT_FALSE ((u16) ~0 == sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +00001104 {
1105 /* this is the first packet to use this SA, claim the SA
1106 * for this thread. this could happen simultaneously on
1107 * another thread */
Neale Ranns1a52d372021-02-04 11:33:32 +00001108 clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
Neale Rannsf62a8c02019-04-02 08:13:33 +00001109 ipsec_sa_assign_thread (thread_index));
1110 }
1111
Neale Ranns1a52d372021-02-04 11:33:32 +00001112 if (PREDICT_FALSE (thread_index != sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +00001113 {
Neale Rannsaa7d7662021-02-10 08:42:49 +00001114 vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001115 err = ESP_DECRYPT_ERROR_HANDOFF;
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001116 esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop,
1117 noop_nexts, ESP_DECRYPT_NEXT_HANDOFF,
1118 current_sa_index);
Neale Rannsf62a8c02019-04-02 08:13:33 +00001119 goto next;
1120 }
1121
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001122 /* store packet data for next round for easier prefetch */
1123 pd->sa_data = cpd.sa_data;
1124 pd->current_data = b[0]->current_data;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001125 pd->hdr_sz = pd->current_data - vnet_buffer (b[0])->l3_hdr_offset;
1126 payload = b[0]->data + pd->current_data;
Neale Ranns6afaae12019-07-17 15:07:14 +00001127 pd->seq = clib_host_to_net_u32 (((esp_header_t *) payload)->seq);
Fan Zhangf5395782020-04-29 14:00:03 +01001128 pd->is_chain = 0;
1129 pd2->lb = b[0];
1130 pd2->free_buffer_index = 0;
1131 pd2->icv_removed = 0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001132
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001133 if (n_bufs > 1)
1134 {
Fan Zhangf5395782020-04-29 14:00:03 +01001135 pd->is_chain = 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001136 /* find last buffer in the chain */
Fan Zhangf5395782020-04-29 14:00:03 +01001137 while (pd2->lb->flags & VLIB_BUFFER_NEXT_PRESENT)
1138 pd2->lb = vlib_get_buffer (vm, pd2->lb->next_buffer);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001139
1140 crypto_ops = &ptd->chained_crypto_ops;
1141 integ_ops = &ptd->chained_integ_ops;
1142 }
Benoît Gannee631ece2021-05-27 18:49:42 +02001143 else
1144 {
1145 crypto_ops = &ptd->crypto_ops;
1146 integ_ops = &ptd->integ_ops;
1147 }
Fan Zhangf5395782020-04-29 14:00:03 +01001148
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001149 pd->current_length = b[0]->current_length;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001150
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001151 /* anti-reply check */
Neale Ranns5b891102021-06-28 13:31:28 +00001152 if (ipsec_sa_anti_replay_and_sn_advance (sa0, pd->seq, ~0, false,
1153 &pd->seq_hi))
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001154 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001155 err = ESP_DECRYPT_ERROR_REPLAY;
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001156 esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop,
1157 noop_nexts, ESP_DECRYPT_NEXT_DROP,
1158 current_sa_index);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001159 goto next;
1160 }
1161
Damjan Mariona829b132019-04-24 23:39:16 +02001162 if (pd->current_length < cpd.icv_sz + esp_sz + cpd.iv_sz)
1163 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001164 err = ESP_DECRYPT_ERROR_RUNT;
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001165 esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop,
1166 noop_nexts, ESP_DECRYPT_NEXT_DROP,
1167 current_sa_index);
Damjan Mariona829b132019-04-24 23:39:16 +02001168 goto next;
1169 }
1170
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001171 len = pd->current_length - cpd.icv_sz;
1172 current_sa_pkts += 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001173 current_sa_bytes += vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001174
Fan Zhangf5395782020-04-29 14:00:03 +01001175 if (is_async)
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001176 {
Matthew Smith51d56ba2021-06-04 09:18:37 -05001177 async_op = sa0->crypto_async_dec_op_id;
1178
1179 /* get a frame for this op if we don't yet have one or it's full
1180 */
1181 if (NULL == async_frames[async_op] ||
1182 vnet_crypto_async_frame_is_full (async_frames[async_op]))
1183 {
1184 async_frames[async_op] =
1185 vnet_crypto_async_get_frame (vm, async_op);
gaoginskxf441b5d2021-06-07 12:07:01 +01001186 if (PREDICT_FALSE (!async_frames[async_op]))
1187 {
1188 err = ESP_DECRYPT_ERROR_NO_AVAIL_FRAME;
1189 esp_decrypt_set_next_index (
1190 b[0], node, thread_index, err, n_noop, noop_nexts,
1191 ESP_DECRYPT_NEXT_DROP, current_sa_index);
1192 goto next;
1193 }
1194
Matthew Smith51d56ba2021-06-04 09:18:37 -05001195 /* Save the frame to the list we'll submit at the end */
1196 vec_add1 (ptd->async_frames, async_frames[async_op]);
1197 }
Neale Rannsfc811342021-02-26 10:35:33 +00001198
1199 err = esp_decrypt_prepare_async_frame (
1200 vm, node, ptd, async_frames[async_op], sa0, payload, len,
Neale Rannsf16e9a52021-02-25 19:09:24 +00001201 cpd.icv_sz, cpd.iv_sz, pd, pd2, from[b - bufs], b[0], async_next,
1202 async_next_node);
Neale Rannsfc811342021-02-26 10:35:33 +00001203 if (ESP_DECRYPT_ERROR_RX_PKTS != err)
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001204 {
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001205 esp_decrypt_set_next_index (
1206 b[0], node, thread_index, err, n_noop, noop_nexts,
1207 ESP_DECRYPT_NEXT_DROP, current_sa_index);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001208 }
Damjan Marionc98275f2019-03-06 14:05:01 +01001209 }
Fan Zhangf5395782020-04-29 14:00:03 +01001210 else
Neale Rannsf16e9a52021-02-25 19:09:24 +00001211 esp_decrypt_prepare_sync_op (
1212 vm, node, ptd, &crypto_ops, &integ_ops, op, sa0, payload, len,
Neale Rannsfe2d23f2022-11-18 04:24:09 +00001213 cpd.icv_sz, cpd.iv_sz, pd, pd2, b[0], sync_next, n_sync);
Damjan Marionc98275f2019-03-06 14:05:01 +01001214 /* next */
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001215 next:
Neale Rannsf16e9a52021-02-25 19:09:24 +00001216 if (ESP_DECRYPT_ERROR_RX_PKTS != err)
1217 {
1218 noop_bi[n_noop] = from[b - bufs];
1219 n_noop++;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001220 }
1221 else if (!is_async)
1222 {
1223 sync_bi[n_sync] = from[b - bufs];
1224 sync_bufs[n_sync] = b[0];
1225 n_sync++;
1226 sync_next++;
1227 pd += 1;
1228 pd2 += 1;
1229 }
1230 else
Matthew Smith51d56ba2021-06-04 09:18:37 -05001231 async_next++;
1232
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001233 n_left -= 1;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001234 b += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001235 }
Damjan Marionc98275f2019-03-06 14:05:01 +01001236
Neale Ranns02950402019-12-20 00:54:57 +00001237 if (PREDICT_TRUE (~0 != current_sa_index))
1238 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1239 current_sa_index, current_sa_pkts,
1240 current_sa_bytes);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001241
Matthew Smith51d56ba2021-06-04 09:18:37 -05001242 /* submit or free all of the open frames */
1243 vnet_crypto_async_frame_t **async_frame;
Neale Rannsfc811342021-02-26 10:35:33 +00001244
Matthew Smith51d56ba2021-06-04 09:18:37 -05001245 vec_foreach (async_frame, ptd->async_frames)
1246 {
1247 /* free frame and move on if no ops were successfully added */
1248 if (PREDICT_FALSE (!(*async_frame)->n_elts))
Fan Zhangf5395782020-04-29 14:00:03 +01001249 {
Matthew Smith51d56ba2021-06-04 09:18:37 -05001250 vnet_crypto_async_free_frame (vm, *async_frame);
1251 continue;
1252 }
1253 if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0)
1254 {
1255 n_noop += esp_async_recycle_failed_submit (
1256 vm, *async_frame, node, ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR,
Arthur de Kerhorad95b062022-11-16 19:12:05 +01001257 IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR, n_noop, noop_bi, noop_nexts,
Xiaoming Jiang0c1454c2023-05-05 02:28:20 +00001258 ESP_DECRYPT_NEXT_DROP, false);
Matthew Smith51d56ba2021-06-04 09:18:37 -05001259 vnet_crypto_async_reset_frame (*async_frame);
1260 vnet_crypto_async_free_frame (vm, *async_frame);
Fan Zhangf5395782020-04-29 14:00:03 +01001261 }
Fan Zhangf5395782020-04-29 14:00:03 +01001262 }
Fan Zhangf5395782020-04-29 14:00:03 +01001263
Neale Rannsf16e9a52021-02-25 19:09:24 +00001264 if (n_sync)
1265 {
1266 esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts,
1267 ESP_DECRYPT_ERROR_INTEG_ERROR);
1268 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs,
1269 sync_nexts, ptd->chunks,
1270 ESP_DECRYPT_ERROR_INTEG_ERROR);
1271
1272 esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts,
Fan Zhangf5395782020-04-29 14:00:03 +01001273 ESP_DECRYPT_ERROR_DECRYPTION_FAILED);
Neale Rannsf16e9a52021-02-25 19:09:24 +00001274 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs,
1275 sync_nexts, ptd->chunks,
Fan Zhangf5395782020-04-29 14:00:03 +01001276 ESP_DECRYPT_ERROR_DECRYPTION_FAILED);
1277 }
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001278
1279 /* Post decryption ronud - adjust packet data start and length and next
1280 node */
1281
Neale Rannsf16e9a52021-02-25 19:09:24 +00001282 n_left = n_sync;
1283 sync_next = sync_nexts;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001284 pd = pkt_data;
Fan Zhangf5395782020-04-29 14:00:03 +01001285 pd2 = pkt_data2;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001286 b = sync_bufs;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001287
1288 while (n_left)
1289 {
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001290 if (n_left >= 2)
1291 {
1292 void *data = b[1]->data + pd[1].current_data;
1293
1294 /* buffer metadata */
1295 vlib_prefetch_buffer_header (b[1], LOAD);
1296
1297 /* esp_footer_t */
1298 CLIB_PREFETCH (data + pd[1].current_length - pd[1].icv_sz - 2,
1299 CLIB_CACHE_LINE_BYTES, LOAD);
1300
1301 /* packet headers */
1302 CLIB_PREFETCH (data - CLIB_CACHE_LINE_BYTES,
1303 CLIB_CACHE_LINE_BYTES * 2, LOAD);
1304 }
1305
Christian Hoppsd570e532020-08-25 12:40:40 -04001306 /* save the sa_index as GRE_teb post_crypto changes L2 opaque */
1307 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1308 current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
1309
Neale Rannsf16e9a52021-02-25 19:09:24 +00001310 if (sync_next[0] >= ESP_DECRYPT_N_NEXT)
Benoît Ganne98ca76a2022-04-11 18:51:25 +02001311 esp_decrypt_post_crypto (vm, node, next_by_next_header, pd, pd2, b[0],
1312 sync_next, is_ip6, is_tun, 0);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001313
Fan Zhangf5395782020-04-29 14:00:03 +01001314 /* trace: */
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001315 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1316 {
1317 esp_decrypt_trace_t *tr;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001318 tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
Neale Rannsc5fe57d2021-02-25 16:01:28 +00001319 sa0 = ipsec_sa_get (current_sa_index);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001320 tr->crypto_alg = sa0->crypto_alg;
1321 tr->integ_alg = sa0->integ_alg;
Neale Ranns6afaae12019-07-17 15:07:14 +00001322 tr->seq = pd->seq;
Neale Ranns5b891102021-06-28 13:31:28 +00001323 tr->sa_seq = sa0->seq;
Neale Ranns6afaae12019-07-17 15:07:14 +00001324 tr->sa_seq_hi = sa0->seq_hi;
Neale Ranns5b891102021-06-28 13:31:28 +00001325 tr->pkt_seq_hi = pd->seq_hi;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001326 }
1327
1328 /* next */
1329 n_left -= 1;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001330 sync_next += 1;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001331 pd += 1;
Fan Zhangf5395782020-04-29 14:00:03 +01001332 pd2 += 1;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001333 b += 1;
1334 }
1335
Neale Rannsf16e9a52021-02-25 19:09:24 +00001336 vlib_node_increment_counter (vm, node->node_index, ESP_DECRYPT_ERROR_RX_PKTS,
1337 from_frame->n_vectors);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001338
Neale Rannsf16e9a52021-02-25 19:09:24 +00001339 if (n_sync)
1340 vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001341
Neale Rannsf16e9a52021-02-25 19:09:24 +00001342 if (n_noop)
1343 vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
1344
1345 return (from_frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001346}
1347
Fan Zhangf5395782020-04-29 14:00:03 +01001348always_inline uword
1349esp_decrypt_post_inline (vlib_main_t * vm,
1350 vlib_node_runtime_t * node,
1351 vlib_frame_t * from_frame, int is_ip6, int is_tun)
1352{
Benoît Ganne98ca76a2022-04-11 18:51:25 +02001353 const ipsec_main_t *im = &ipsec_main;
1354 const u16 *next_by_next_header = im->next_header_registrations;
Fan Zhangf5395782020-04-29 14:00:03 +01001355 u32 *from = vlib_frame_vector_args (from_frame);
1356 u32 n_left = from_frame->n_vectors;
1357 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1358 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1359 vlib_get_buffers (vm, from, b, n_left);
1360
1361 while (n_left > 0)
1362 {
1363 esp_decrypt_packet_data_t *pd = &(esp_post_data (b[0]))->decrypt_data;
1364
1365 if (n_left > 2)
1366 {
1367 vlib_prefetch_buffer_header (b[2], LOAD);
1368 vlib_prefetch_buffer_header (b[1], LOAD);
1369 }
1370
1371 if (!pd->is_chain)
Benoît Ganne98ca76a2022-04-11 18:51:25 +02001372 esp_decrypt_post_crypto (vm, node, next_by_next_header, pd, 0, b[0],
1373 next, is_ip6, is_tun, 1);
Fan Zhangf5395782020-04-29 14:00:03 +01001374 else
1375 {
1376 esp_decrypt_packet_data2_t *pd2 = esp_post_data2 (b[0]);
Benoît Ganne98ca76a2022-04-11 18:51:25 +02001377 esp_decrypt_post_crypto (vm, node, next_by_next_header, pd, pd2,
1378 b[0], next, is_ip6, is_tun, 1);
Fan Zhangf5395782020-04-29 14:00:03 +01001379 }
1380
1381 /*trace: */
1382 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1383 {
Neale Rannsc5fe57d2021-02-25 16:01:28 +00001384 ipsec_sa_t *sa0 = ipsec_sa_get (pd->sa_index);
Fan Zhangf5395782020-04-29 14:00:03 +01001385 esp_decrypt_trace_t *tr;
1386 esp_decrypt_packet_data_t *async_pd =
1387 &(esp_post_data (b[0]))->decrypt_data;
1388 tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
Neale Rannsc5fe57d2021-02-25 16:01:28 +00001389 sa0 = ipsec_sa_get (async_pd->sa_index);
Fan Zhangf5395782020-04-29 14:00:03 +01001390
1391 tr->crypto_alg = sa0->crypto_alg;
1392 tr->integ_alg = sa0->integ_alg;
1393 tr->seq = pd->seq;
Neale Ranns5b891102021-06-28 13:31:28 +00001394 tr->sa_seq = sa0->seq;
Fan Zhangf5395782020-04-29 14:00:03 +01001395 tr->sa_seq_hi = sa0->seq_hi;
1396 }
1397
1398 n_left--;
1399 next++;
1400 b++;
1401 }
1402
1403 n_left = from_frame->n_vectors;
1404 vlib_node_increment_counter (vm, node->node_index,
1405 ESP_DECRYPT_ERROR_RX_POST_PKTS, n_left);
1406
1407 vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left);
1408
1409 return n_left;
1410}
1411
Klement Sekerab8f35442018-10-29 13:38:19 +01001412VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm,
1413 vlib_node_runtime_t * node,
1414 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001415{
Fan Zhangf5395782020-04-29 14:00:03 +01001416 return esp_decrypt_inline (vm, node, from_frame, 0, 0,
1417 esp_decrypt_async_next.esp4_post_next);
1418}
1419
1420VLIB_NODE_FN (esp4_decrypt_post_node) (vlib_main_t * vm,
1421 vlib_node_runtime_t * node,
1422 vlib_frame_t * from_frame)
1423{
1424 return esp_decrypt_post_inline (vm, node, from_frame, 0, 0);
Neale Rannsc87b66c2019-02-07 07:26:12 -08001425}
1426
1427VLIB_NODE_FN (esp4_decrypt_tun_node) (vlib_main_t * vm,
1428 vlib_node_runtime_t * node,
1429 vlib_frame_t * from_frame)
1430{
Fan Zhangf5395782020-04-29 14:00:03 +01001431 return esp_decrypt_inline (vm, node, from_frame, 0, 1,
1432 esp_decrypt_async_next.esp4_tun_post_next);
1433}
1434
1435VLIB_NODE_FN (esp4_decrypt_tun_post_node) (vlib_main_t * vm,
1436 vlib_node_runtime_t * node,
1437 vlib_frame_t * from_frame)
1438{
1439 return esp_decrypt_post_inline (vm, node, from_frame, 0, 1);
Neale Rannsc87b66c2019-02-07 07:26:12 -08001440}
1441
1442VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm,
1443 vlib_node_runtime_t * node,
1444 vlib_frame_t * from_frame)
1445{
Fan Zhangf5395782020-04-29 14:00:03 +01001446 return esp_decrypt_inline (vm, node, from_frame, 1, 0,
1447 esp_decrypt_async_next.esp6_post_next);
1448}
1449
1450VLIB_NODE_FN (esp6_decrypt_post_node) (vlib_main_t * vm,
1451 vlib_node_runtime_t * node,
1452 vlib_frame_t * from_frame)
1453{
1454 return esp_decrypt_post_inline (vm, node, from_frame, 1, 0);
Neale Rannsc87b66c2019-02-07 07:26:12 -08001455}
1456
1457VLIB_NODE_FN (esp6_decrypt_tun_node) (vlib_main_t * vm,
1458 vlib_node_runtime_t * node,
1459 vlib_frame_t * from_frame)
1460{
Fan Zhangf5395782020-04-29 14:00:03 +01001461 return esp_decrypt_inline (vm, node, from_frame, 1, 1,
1462 esp_decrypt_async_next.esp6_tun_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001463}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001464
Fan Zhangf5395782020-04-29 14:00:03 +01001465VLIB_NODE_FN (esp6_decrypt_tun_post_node) (vlib_main_t * vm,
1466 vlib_node_runtime_t * node,
1467 vlib_frame_t * from_frame)
1468{
1469 return esp_decrypt_post_inline (vm, node, from_frame, 1, 1);
1470}
1471
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001472/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001473VLIB_REGISTER_NODE (esp4_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001474 .name = "esp4-decrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001475 .vector_size = sizeof (u32),
1476 .format_trace = format_esp_decrypt_trace,
1477 .type = VLIB_NODE_TYPE_INTERNAL,
1478
Neale Ranns93688d72022-08-09 03:34:51 +00001479 .n_errors = ESP_DECRYPT_N_ERROR,
1480 .error_counters = esp_decrypt_error_counters,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001481
1482 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1483 .next_nodes = {
Neale Rannsf62a8c02019-04-02 08:13:33 +00001484 [ESP_DECRYPT_NEXT_DROP] = "ip4-drop",
1485 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1486 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
Neale Ranns4a58e492020-12-21 13:19:10 +00001487 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-drop",
Neale Ranns568acbb2019-12-18 05:54:40 +00001488 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
Neale Rannsf62a8c02019-04-02 08:13:33 +00001489 [ESP_DECRYPT_NEXT_HANDOFF] = "esp4-decrypt-handoff",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001490 },
1491};
1492
Fan Zhangf5395782020-04-29 14:00:03 +01001493VLIB_REGISTER_NODE (esp4_decrypt_post_node) = {
1494 .name = "esp4-decrypt-post",
1495 .vector_size = sizeof (u32),
1496 .format_trace = format_esp_decrypt_trace,
1497 .type = VLIB_NODE_TYPE_INTERNAL,
1498
Neale Ranns93688d72022-08-09 03:34:51 +00001499 .n_errors = ESP_DECRYPT_N_ERROR,
1500 .error_counters = esp_decrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001501
1502 .sibling_of = "esp4-decrypt",
1503};
1504
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001505VLIB_REGISTER_NODE (esp6_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001506 .name = "esp6-decrypt",
1507 .vector_size = sizeof (u32),
1508 .format_trace = format_esp_decrypt_trace,
1509 .type = VLIB_NODE_TYPE_INTERNAL,
1510
Neale Ranns93688d72022-08-09 03:34:51 +00001511 .n_errors = ESP_DECRYPT_N_ERROR,
1512 .error_counters = esp_decrypt_error_counters,
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001513
1514 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1515 .next_nodes = {
Neale Rannsf62a8c02019-04-02 08:13:33 +00001516 [ESP_DECRYPT_NEXT_DROP] = "ip6-drop",
1517 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1518 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
Neale Ranns4a58e492020-12-21 13:19:10 +00001519 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-drop",
Neale Ranns568acbb2019-12-18 05:54:40 +00001520 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
Neale Rannsf62a8c02019-04-02 08:13:33 +00001521 [ESP_DECRYPT_NEXT_HANDOFF]= "esp6-decrypt-handoff",
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001522 },
1523};
Neale Rannsc87b66c2019-02-07 07:26:12 -08001524
Fan Zhangf5395782020-04-29 14:00:03 +01001525VLIB_REGISTER_NODE (esp6_decrypt_post_node) = {
1526 .name = "esp6-decrypt-post",
1527 .vector_size = sizeof (u32),
1528 .format_trace = format_esp_decrypt_trace,
1529 .type = VLIB_NODE_TYPE_INTERNAL,
1530
Neale Ranns93688d72022-08-09 03:34:51 +00001531 .n_errors = ESP_DECRYPT_N_ERROR,
1532 .error_counters = esp_decrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001533
1534 .sibling_of = "esp6-decrypt",
1535};
1536
Neale Rannsc87b66c2019-02-07 07:26:12 -08001537VLIB_REGISTER_NODE (esp4_decrypt_tun_node) = {
1538 .name = "esp4-decrypt-tun",
1539 .vector_size = sizeof (u32),
1540 .format_trace = format_esp_decrypt_trace,
1541 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Ranns93688d72022-08-09 03:34:51 +00001542 .n_errors = ESP_DECRYPT_N_ERROR,
1543 .error_counters = esp_decrypt_error_counters,
Neale Rannsf62a8c02019-04-02 08:13:33 +00001544 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1545 .next_nodes = {
1546 [ESP_DECRYPT_NEXT_DROP] = "ip4-drop",
1547 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1548 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
Neale Ranns4a58e492020-12-21 13:19:10 +00001549 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-input",
Neale Ranns568acbb2019-12-18 05:54:40 +00001550 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001551 [ESP_DECRYPT_NEXT_HANDOFF] = "esp4-decrypt-tun-handoff",
Neale Rannsf62a8c02019-04-02 08:13:33 +00001552 },
Neale Rannsc87b66c2019-02-07 07:26:12 -08001553};
1554
Fan Zhangf5395782020-04-29 14:00:03 +01001555VLIB_REGISTER_NODE (esp4_decrypt_tun_post_node) = {
1556 .name = "esp4-decrypt-tun-post",
1557 .vector_size = sizeof (u32),
1558 .format_trace = format_esp_decrypt_trace,
1559 .type = VLIB_NODE_TYPE_INTERNAL,
1560
Neale Ranns93688d72022-08-09 03:34:51 +00001561 .n_errors = ESP_DECRYPT_N_ERROR,
1562 .error_counters = esp_decrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001563
1564 .sibling_of = "esp4-decrypt-tun",
1565};
1566
Neale Rannsc87b66c2019-02-07 07:26:12 -08001567VLIB_REGISTER_NODE (esp6_decrypt_tun_node) = {
1568 .name = "esp6-decrypt-tun",
1569 .vector_size = sizeof (u32),
1570 .format_trace = format_esp_decrypt_trace,
1571 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Ranns93688d72022-08-09 03:34:51 +00001572 .n_errors = ESP_DECRYPT_N_ERROR,
1573 .error_counters = esp_decrypt_error_counters,
Neale Rannsf62a8c02019-04-02 08:13:33 +00001574 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1575 .next_nodes = {
1576 [ESP_DECRYPT_NEXT_DROP] = "ip6-drop",
1577 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1578 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
Neale Ranns4a58e492020-12-21 13:19:10 +00001579 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-input",
Neale Ranns568acbb2019-12-18 05:54:40 +00001580 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001581 [ESP_DECRYPT_NEXT_HANDOFF]= "esp6-decrypt-tun-handoff",
Neale Rannsf62a8c02019-04-02 08:13:33 +00001582 },
Neale Rannsc87b66c2019-02-07 07:26:12 -08001583};
Fan Zhangf5395782020-04-29 14:00:03 +01001584
1585VLIB_REGISTER_NODE (esp6_decrypt_tun_post_node) = {
1586 .name = "esp6-decrypt-tun-post",
1587 .vector_size = sizeof (u32),
1588 .format_trace = format_esp_decrypt_trace,
1589 .type = VLIB_NODE_TYPE_INTERNAL,
1590
Neale Ranns93688d72022-08-09 03:34:51 +00001591 .n_errors = ESP_DECRYPT_N_ERROR,
1592 .error_counters = esp_decrypt_error_counters,
Fan Zhangf5395782020-04-29 14:00:03 +01001593
1594 .sibling_of = "esp6-decrypt-tun",
1595};
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001596/* *INDENT-ON* */
1597
Neale Ranns2d498302021-02-25 08:38:58 +00001598#ifndef CLIB_MARCH_VARIANT
1599
1600static clib_error_t *
1601esp_decrypt_init (vlib_main_t *vm)
1602{
1603 ipsec_main_t *im = &ipsec_main;
1604
1605 im->esp4_dec_fq_index =
1606 vlib_frame_queue_main_init (esp4_decrypt_node.index, 0);
1607 im->esp6_dec_fq_index =
1608 vlib_frame_queue_main_init (esp6_decrypt_node.index, 0);
1609 im->esp4_dec_tun_fq_index =
1610 vlib_frame_queue_main_init (esp4_decrypt_tun_node.index, 0);
1611 im->esp6_dec_tun_fq_index =
1612 vlib_frame_queue_main_init (esp6_decrypt_tun_node.index, 0);
1613
1614 return 0;
1615}
1616
1617VLIB_INIT_FUNCTION (esp_decrypt_init);
1618
1619#endif
1620
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001621/*
1622 * fd.io coding-style-patch-verification: ON
1623 *
1624 * Local Variables:
1625 * eval: (c-set-style "gnu")
1626 * End:
1627 */