blob: 8c4a9e2218211b977f7a7537db9c1976209c1e66 [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 */
17
18#include <vnet/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
John Lo90430b62020-01-31 23:48:30 -050021#include <vnet/l2/l2_input.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
23#include <vnet/ipsec/ipsec.h>
24#include <vnet/ipsec/esp.h>
Neale Ranns918c1612019-02-21 23:34:59 -080025#include <vnet/ipsec/ipsec_io.h>
Neale Rannsc87b66c2019-02-07 07:26:12 -080026#include <vnet/ipsec/ipsec_tun.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027
Neale Ranns119c0d72020-11-26 13:12:37 +000028#include <vnet/gre/packet.h>
Neale Ranns568acbb2019-12-18 05:54:40 +000029
Neale Ranns4a58e492020-12-21 13:19:10 +000030#define foreach_esp_decrypt_next \
31 _ (DROP, "error-drop") \
32 _ (IP4_INPUT, "ip4-input-no-checksum") \
33 _ (IP6_INPUT, "ip6-input") \
34 _ (L2_INPUT, "l2-input") \
35 _ (MPLS_INPUT, "mpls-input") \
36 _ (HANDOFF, "handoff")
Ed Warnickecb9cada2015-12-08 15:45:58 -070037
38#define _(v, s) ESP_DECRYPT_NEXT_##v,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070039typedef enum
40{
Ed Warnickecb9cada2015-12-08 15:45:58 -070041 foreach_esp_decrypt_next
42#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070043 ESP_DECRYPT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070044} esp_decrypt_next_t;
45
Neale Ranns4a58e492020-12-21 13:19:10 +000046#define foreach_esp_decrypt_post_next \
47 _ (DROP, "error-drop") \
48 _ (IP4_INPUT, "ip4-input-no-checksum") \
49 _ (IP6_INPUT, "ip6-input") \
50 _ (MPLS_INPUT, "mpls-input") \
51 _ (L2_INPUT, "l2-input")
Fan Zhangf5395782020-04-29 14:00:03 +010052
53#define _(v, s) ESP_DECRYPT_POST_NEXT_##v,
54typedef enum
55{
56 foreach_esp_decrypt_post_next
57#undef _
58 ESP_DECRYPT_POST_N_NEXT,
59} esp_decrypt_post_next_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
Neale Rannsf16e9a52021-02-25 19:09:24 +000061#define foreach_esp_decrypt_error \
62 _ (RX_PKTS, "ESP pkts received") \
63 _ (RX_POST_PKTS, "ESP-POST pkts received") \
64 _ (HANDOFF, "hand-off") \
65 _ (DECRYPTION_FAILED, "ESP decryption failed") \
66 _ (INTEG_ERROR, "Integrity check failed") \
67 _ (CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
68 _ (REPLAY, "SA replayed packet") \
69 _ (RUNT, "undersized packet") \
70 _ (NO_BUFFERS, "no buffers (packet dropped)") \
71 _ (OVERSIZED_HEADER, "buffer with oversized header (dropped)") \
72 _ (NO_TAIL_SPACE, "no enough buffer tail space (dropped)") \
73 _ (TUN_NO_PROTO, "no tunnel protocol") \
74 _ (UNSUP_PAYLOAD, "unsupported payload")
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070076typedef enum
77{
Ed Warnickecb9cada2015-12-08 15:45:58 -070078#define _(sym,str) ESP_DECRYPT_ERROR_##sym,
79 foreach_esp_decrypt_error
80#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070081 ESP_DECRYPT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070082} esp_decrypt_error_t;
83
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070084static char *esp_decrypt_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070085#define _(sym,string) string,
86 foreach_esp_decrypt_error
87#undef _
88};
89
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070090typedef struct
91{
Damjan Marionb4fff3a2019-03-25 15:54:40 +010092 u32 seq;
Neale Ranns6afaae12019-07-17 15:07:14 +000093 u32 sa_seq;
94 u32 sa_seq_hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 ipsec_crypto_alg_t crypto_alg;
96 ipsec_integ_alg_t integ_alg;
97} esp_decrypt_trace_t;
98
99/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700100static u8 *
101format_esp_decrypt_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102{
103 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
104 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700105 esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
Neale Ranns6afaae12019-07-17 15:07:14 +0000107 s =
108 format (s,
109 "esp: crypto %U integrity %U pkt-seq %d sa-seq %u sa-seq-hi %u",
110 format_ipsec_crypto_alg, t->crypto_alg, format_ipsec_integ_alg,
111 t->integ_alg, t->seq, t->sa_seq, t->sa_seq_hi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 return s;
113}
114
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100115#define ESP_ENCRYPT_PD_F_FD_TRANSPORT (1 << 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000117static_always_inline void
118esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
119 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts,
120 int e)
121{
122 vnet_crypto_op_t *op = ops;
123 u32 n_fail, n_ops = vec_len (ops);
124
125 if (n_ops == 0)
126 return;
127
128 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
129
130 while (n_fail)
131 {
132 ASSERT (op - ops < n_ops);
133 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
134 {
135 u32 err, bi = op->user_data;
136 if (op->status == VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC)
137 err = e;
138 else
139 err = ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR;
140 b[bi]->error = node->errors[err];
141 nexts[bi] = ESP_DECRYPT_NEXT_DROP;
142 n_fail--;
143 }
144 op++;
145 }
146}
147
148static_always_inline void
149esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
150 vnet_crypto_op_t * ops, vlib_buffer_t * b[],
151 u16 * nexts, vnet_crypto_op_chunk_t * chunks, int e)
152{
153
154 vnet_crypto_op_t *op = ops;
155 u32 n_fail, n_ops = vec_len (ops);
156
Neale Rannsf16e9a52021-02-25 19:09:24 +0000157 if (PREDICT_TRUE (n_ops == 0))
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000158 return;
159
160 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
161
162 while (n_fail)
163 {
164 ASSERT (op - ops < n_ops);
165 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
166 {
167 u32 err, bi = op->user_data;
168 if (op->status == VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC)
169 err = e;
170 else
171 err = ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR;
172 b[bi]->error = node->errors[err];
173 nexts[bi] = ESP_DECRYPT_NEXT_DROP;
174 n_fail--;
175 }
176 op++;
177 }
178}
179
180always_inline void
181esp_remove_tail (vlib_main_t * vm, vlib_buffer_t * b, vlib_buffer_t * last,
182 u16 tail)
183{
184 vlib_buffer_t *before_last = b;
185
186 if (last->current_length > tail)
187 {
188 last->current_length -= tail;
189 return;
190 }
191 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
192
193 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
194 {
195 before_last = b;
196 b = vlib_get_buffer (vm, b->next_buffer);
197 }
198 before_last->current_length -= tail - last->current_length;
199 vlib_buffer_free_one (vm, before_last->next_buffer);
200 before_last->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
201}
202
203/* ICV is splitted in last two buffers so move it to the last buffer and
204 return pointer to it */
205static_always_inline u8 *
206esp_move_icv (vlib_main_t * vm, vlib_buffer_t * first,
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000207 esp_decrypt_packet_data_t * pd,
Fan Zhangf5395782020-04-29 14:00:03 +0100208 esp_decrypt_packet_data2_t * pd2, u16 icv_sz, u16 * dif)
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000209{
210 vlib_buffer_t *before_last, *bp;
Fan Zhangf5395782020-04-29 14:00:03 +0100211 u16 last_sz = pd2->lb->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000212 u16 first_sz = icv_sz - last_sz;
213
214 bp = before_last = first;
215 while (bp->flags & VLIB_BUFFER_NEXT_PRESENT)
216 {
217 before_last = bp;
218 bp = vlib_get_buffer (vm, bp->next_buffer);
219 }
220
Fan Zhangf5395782020-04-29 14:00:03 +0100221 u8 *lb_curr = vlib_buffer_get_current (pd2->lb);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000222 memmove (lb_curr + first_sz, lb_curr, last_sz);
223 clib_memcpy_fast (lb_curr, vlib_buffer_get_tail (before_last) - first_sz,
224 first_sz);
225 before_last->current_length -= first_sz;
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000226 if (before_last == first)
227 pd->current_length -= first_sz;
Fan Zhangf5395782020-04-29 14:00:03 +0100228 clib_memset (vlib_buffer_get_tail (before_last), 0, first_sz);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000229 if (dif)
230 dif[0] = first_sz;
Fan Zhangf5395782020-04-29 14:00:03 +0100231 pd2->lb = before_last;
232 pd2->icv_removed = 1;
233 pd2->free_buffer_index = before_last->next_buffer;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000234 before_last->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
235 return lb_curr;
236}
237
Fan Zhangf5395782020-04-29 14:00:03 +0100238static_always_inline i16
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000239esp_insert_esn (vlib_main_t * vm, ipsec_sa_t * sa,
Fan Zhangf5395782020-04-29 14:00:03 +0100240 esp_decrypt_packet_data2_t * pd2, u32 * data_len,
241 u8 ** digest, u16 * len, vlib_buffer_t * b, u8 * payload)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000242{
243 if (!ipsec_sa_is_set_USE_ESN (sa))
Fan Zhangf5395782020-04-29 14:00:03 +0100244 return 0;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000245
246 /* shift ICV by 4 bytes to insert ESN */
247 u32 seq_hi = clib_host_to_net_u32 (sa->seq_hi);
248 u8 tmp[ESP_MAX_ICV_SIZE], sz = sizeof (sa->seq_hi);
249
Fan Zhangf5395782020-04-29 14:00:03 +0100250 if (pd2->icv_removed)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000251 {
Fan Zhangf5395782020-04-29 14:00:03 +0100252 u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000253 if (space_left >= sz)
254 {
Fan Zhangf5395782020-04-29 14:00:03 +0100255 clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb), &seq_hi, sz);
256 *data_len += sz;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000257 }
258 else
Fan Zhangf5395782020-04-29 14:00:03 +0100259 return sz;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000260
261 len[0] = b->current_length;
262 }
263 else
264 {
265 clib_memcpy_fast (tmp, payload + len[0], ESP_MAX_ICV_SIZE);
266 clib_memcpy_fast (payload + len[0], &seq_hi, sz);
267 clib_memcpy_fast (payload + len[0] + sz, tmp, ESP_MAX_ICV_SIZE);
Fan Zhangf5395782020-04-29 14:00:03 +0100268 *data_len += sz;
269 *digest += sz;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000270 }
Fan Zhangf5395782020-04-29 14:00:03 +0100271 return sz;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000272}
273
274static_always_inline u8 *
275esp_move_icv_esn (vlib_main_t * vm, vlib_buffer_t * first,
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000276 esp_decrypt_packet_data_t * pd,
Fan Zhangf5395782020-04-29 14:00:03 +0100277 esp_decrypt_packet_data2_t * pd2, u16 icv_sz,
278 ipsec_sa_t * sa, u8 * extra_esn, u32 * len)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000279{
280 u16 dif = 0;
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000281 u8 *digest = esp_move_icv (vm, first, pd, pd2, icv_sz, &dif);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000282 if (dif)
Fan Zhangf5395782020-04-29 14:00:03 +0100283 *len -= dif;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000284
285 if (ipsec_sa_is_set_USE_ESN (sa))
286 {
287 u8 sz = sizeof (sa->seq_hi);
288 u32 seq_hi = clib_host_to_net_u32 (sa->seq_hi);
Fan Zhangf5395782020-04-29 14:00:03 +0100289 u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000290
291 if (space_left >= sz)
292 {
Fan Zhangf5395782020-04-29 14:00:03 +0100293 clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb), &seq_hi, sz);
294 *len += sz;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000295 }
296 else
297 {
298 /* no space for ESN at the tail, use the next buffer
299 * (with ICV data) */
Fan Zhangf5395782020-04-29 14:00:03 +0100300 ASSERT (pd2->icv_removed);
301 vlib_buffer_t *tmp = vlib_get_buffer (vm, pd2->free_buffer_index);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000302 clib_memcpy_fast (vlib_buffer_get_current (tmp) - sz, &seq_hi, sz);
303 extra_esn[0] = 1;
304 }
305 }
306 return digest;
307}
308
Fan Zhangf5395782020-04-29 14:00:03 +0100309static_always_inline int
310esp_decrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
311 esp_decrypt_packet_data2_t * pd2,
312 ipsec_sa_t * sa0, vlib_buffer_t * b, u8 icv_sz,
313 u8 * start_src, u32 start_len,
314 u8 ** digest, u16 * n_ch, u32 * integ_total_len)
315{
316 vnet_crypto_op_chunk_t *ch;
317 vlib_buffer_t *cb = vlib_get_buffer (vm, b->next_buffer);
318 u16 n_chunks = 1;
319 u32 total_len;
320 vec_add2 (ptd->chunks, ch, 1);
321 total_len = ch->len = start_len;
322 ch->src = start_src;
323
324 while (1)
325 {
326 vec_add2 (ptd->chunks, ch, 1);
327 n_chunks += 1;
328 ch->src = vlib_buffer_get_current (cb);
329 if (pd2->lb == cb)
330 {
331 if (pd2->icv_removed)
332 ch->len = cb->current_length;
333 else
334 ch->len = cb->current_length - icv_sz;
335 if (ipsec_sa_is_set_USE_ESN (sa0))
336 {
337 u32 seq_hi = clib_host_to_net_u32 (sa0->seq_hi);
338 u8 tmp[ESP_MAX_ICV_SIZE], sz = sizeof (sa0->seq_hi);
339 u8 *esn;
340 vlib_buffer_t *tmp_b;
341 u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
342 if (space_left < sz)
343 {
344 if (pd2->icv_removed)
345 {
346 /* use pre-data area from the last bufer
347 that was removed from the chain */
348 tmp_b = vlib_get_buffer (vm, pd2->free_buffer_index);
349 esn = tmp_b->data - sz;
350 }
351 else
352 {
353 /* no space, need to allocate new buffer */
354 u32 tmp_bi = 0;
355 if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1)
356 return -1;
357 tmp_b = vlib_get_buffer (vm, tmp_bi);
358 esn = tmp_b->data;
359 pd2->free_buffer_index = tmp_bi;
360 }
361 clib_memcpy_fast (esn, &seq_hi, sz);
362
363 vec_add2 (ptd->chunks, ch, 1);
364 n_chunks += 1;
365 ch->src = esn;
366 ch->len = sz;
367 }
368 else
369 {
370 if (pd2->icv_removed)
371 {
372 clib_memcpy_fast (vlib_buffer_get_tail
373 (pd2->lb), &seq_hi, sz);
374 }
375 else
376 {
377 clib_memcpy_fast (tmp, *digest, ESP_MAX_ICV_SIZE);
378 clib_memcpy_fast (*digest, &seq_hi, sz);
379 clib_memcpy_fast (*digest + sz, tmp, ESP_MAX_ICV_SIZE);
380 *digest += sz;
381 }
382 ch->len += sz;
383 }
384 }
385 total_len += ch->len;
386 break;
387 }
388 else
389 total_len += ch->len = cb->current_length;
390
391 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
392 break;
393
394 cb = vlib_get_buffer (vm, cb->next_buffer);
395 }
396
397 if (n_ch)
398 *n_ch = n_chunks;
399 if (integ_total_len)
400 *integ_total_len = total_len;
401
402 return 0;
403}
404
405static_always_inline u32
406esp_decrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000407 esp_decrypt_packet_data_t * pd,
Fan Zhangf5395782020-04-29 14:00:03 +0100408 esp_decrypt_packet_data2_t * pd2,
409 ipsec_sa_t * sa0, vlib_buffer_t * b, u8 icv_sz,
410 u8 * start, u32 start_len, u8 ** tag, u16 * n_ch)
411{
412 vnet_crypto_op_chunk_t *ch;
413 vlib_buffer_t *cb = b;
414 u16 n_chunks = 1;
415 u32 total_len;
416 vec_add2 (ptd->chunks, ch, 1);
417 total_len = ch->len = start_len;
418 ch->src = ch->dst = start;
419 cb = vlib_get_buffer (vm, cb->next_buffer);
420 n_chunks = 1;
421
422 while (1)
423 {
424 vec_add2 (ptd->chunks, ch, 1);
425 n_chunks += 1;
426 ch->src = ch->dst = vlib_buffer_get_current (cb);
427 if (pd2->lb == cb)
428 {
429 if (ipsec_sa_is_set_IS_AEAD (sa0))
430 {
431 if (pd2->lb->current_length < icv_sz)
432 {
433 u16 dif = 0;
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000434 *tag = esp_move_icv (vm, b, pd, pd2, icv_sz, &dif);
Fan Zhangf5395782020-04-29 14:00:03 +0100435
436 /* this chunk does not contain crypto data */
437 n_chunks -= 1;
438 /* and fix previous chunk's length as it might have
439 been changed */
440 ASSERT (n_chunks > 0);
441 if (pd2->lb == b)
442 {
443 total_len -= dif;
444 ch[-1].len -= dif;
445 }
446 else
447 {
448 total_len = total_len + pd2->lb->current_length -
449 ch[-1].len;
450 ch[-1].len = pd2->lb->current_length;
451 }
452 break;
453 }
454 else
455 *tag = vlib_buffer_get_tail (pd2->lb) - icv_sz;
456 }
457
458 if (pd2->icv_removed)
459 total_len += ch->len = cb->current_length;
460 else
461 total_len += ch->len = cb->current_length - icv_sz;
462 }
463 else
464 total_len += ch->len = cb->current_length;
465
466 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
467 break;
468
469 cb = vlib_get_buffer (vm, cb->next_buffer);
470 }
471
472 if (n_ch)
473 *n_ch = n_chunks;
474
475 return total_len;
476}
477
478static_always_inline void
479esp_decrypt_prepare_sync_op (vlib_main_t * vm, vlib_node_runtime_t * node,
480 ipsec_per_thread_data_t * ptd,
481 vnet_crypto_op_t *** crypto_ops,
482 vnet_crypto_op_t *** integ_ops,
483 vnet_crypto_op_t * op,
484 ipsec_sa_t * sa0, u8 * payload,
485 u16 len, u8 icv_sz, u8 iv_sz,
486 esp_decrypt_packet_data_t * pd,
487 esp_decrypt_packet_data2_t * pd2,
488 vlib_buffer_t * b, u16 * next, u32 index)
489{
490 const u8 esp_sz = sizeof (esp_header_t);
491
492 if (PREDICT_TRUE (sa0->integ_op_id != VNET_CRYPTO_OP_NONE))
493 {
494 vnet_crypto_op_init (op, sa0->integ_op_id);
495 op->key_index = sa0->integ_key_index;
496 op->src = payload;
497 op->flags = VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
498 op->user_data = index;
499 op->digest = payload + len;
500 op->digest_len = icv_sz;
501 op->len = len;
502
503 if (pd->is_chain)
504 {
505 /* buffer is chained */
506 op->len = pd->current_length;
507
508 /* special case when ICV is splitted and needs to be reassembled
509 * first -> move it to the last buffer. Also take into account
510 * that ESN needs to be added after encrypted data and may or
511 * may not fit in the tail.*/
512 if (pd2->lb->current_length < icv_sz)
513 {
514 u8 extra_esn = 0;
515 op->digest =
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000516 esp_move_icv_esn (vm, b, pd, pd2, icv_sz, sa0,
Fan Zhangf5395782020-04-29 14:00:03 +0100517 &extra_esn, &op->len);
518
519 if (extra_esn)
520 {
521 /* esn is in the last buffer, that was unlinked from
522 * the chain */
523 op->len = b->current_length;
524 }
525 else
526 {
527 if (pd2->lb == b)
528 {
529 /* we now have a single buffer of crypto data, adjust
530 * the length (second buffer contains only ICV) */
531 *integ_ops = &ptd->integ_ops;
532 *crypto_ops = &ptd->crypto_ops;
533 len = b->current_length;
534 goto out;
535 }
536 }
537 }
538 else
539 op->digest = vlib_buffer_get_tail (pd2->lb) - icv_sz;
540
541 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
542 op->chunk_index = vec_len (ptd->chunks);
543 if (esp_decrypt_chain_integ (vm, ptd, pd2, sa0, b, icv_sz,
544 payload, pd->current_length,
545 &op->digest, &op->n_chunks, 0) < 0)
546 {
547 b->error = node->errors[ESP_DECRYPT_ERROR_NO_BUFFERS];
548 next[0] = ESP_DECRYPT_NEXT_DROP;
549 return;
550 }
551 }
552 else
553 esp_insert_esn (vm, sa0, pd2, &op->len, &op->digest, &len, b,
554 payload);
555 out:
556 vec_add_aligned (*(integ_ops[0]), op, 1, CLIB_CACHE_LINE_BYTES);
557 }
558
559 payload += esp_sz;
560 len -= esp_sz;
561
562 if (sa0->crypto_dec_op_id != VNET_CRYPTO_OP_NONE)
563 {
564 vnet_crypto_op_init (op, sa0->crypto_dec_op_id);
565 op->key_index = sa0->crypto_key_index;
566 op->iv = payload;
567
Benoît Ganne490b9272021-01-22 18:03:09 +0100568 if (ipsec_sa_is_set_IS_CTR (sa0))
Fan Zhangf5395782020-04-29 14:00:03 +0100569 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100570 /* construct nonce in a scratch space in front of the IP header */
571 esp_ctr_nonce_t *nonce =
572 (esp_ctr_nonce_t *) (payload - esp_sz - pd->hdr_sz -
573 sizeof (*nonce));
574 if (ipsec_sa_is_set_IS_AEAD (sa0))
575 {
576 /* constuct aad in a scratch space in front of the nonce */
577 esp_header_t *esp0 = (esp_header_t *) (payload - esp_sz);
578 op->aad = (u8 *) nonce - sizeof (esp_aead_t);
579 op->aad_len = esp_aad_fill (op->aad, esp0, sa0);
580 op->tag = payload + len;
581 op->tag_len = 16;
582 }
583 else
584 {
585 nonce->ctr = clib_host_to_net_u32 (1);
586 }
587 nonce->salt = sa0->salt;
588 ASSERT (sizeof (u64) == iv_sz);
589 nonce->iv = *(u64 *) op->iv;
590 op->iv = (u8 *) nonce;
Fan Zhangf5395782020-04-29 14:00:03 +0100591 }
592 op->src = op->dst = payload += iv_sz;
593 op->len = len - iv_sz;
594 op->user_data = index;
595
596 if (pd->is_chain && (pd2->lb != b))
597 {
598 /* buffer is chained */
599 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
600 op->chunk_index = vec_len (ptd->chunks);
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000601 esp_decrypt_chain_crypto (vm, ptd, pd, pd2, sa0, b, icv_sz,
Fan Zhangf5395782020-04-29 14:00:03 +0100602 payload, len - pd->iv_sz + pd->icv_sz,
603 &op->tag, &op->n_chunks);
604 }
605
606 vec_add_aligned (*(crypto_ops[0]), op, 1, CLIB_CACHE_LINE_BYTES);
607 }
608}
609
Neale Rannsfc811342021-02-26 10:35:33 +0000610static_always_inline esp_decrypt_error_t
611esp_decrypt_prepare_async_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
612 ipsec_per_thread_data_t *ptd,
613 vnet_crypto_async_frame_t *f, ipsec_sa_t *sa0,
614 u8 *payload, u16 len, u8 icv_sz, u8 iv_sz,
615 esp_decrypt_packet_data_t *pd,
616 esp_decrypt_packet_data2_t *pd2, u32 bi,
617 vlib_buffer_t *b, u16 *next, u16 async_next)
Fan Zhangf5395782020-04-29 14:00:03 +0100618{
619 const u8 esp_sz = sizeof (esp_header_t);
620 u32 current_protect_index = vnet_buffer (b)->ipsec.protect_index;
621 esp_decrypt_packet_data_t *async_pd = &(esp_post_data (b))->decrypt_data;
622 esp_decrypt_packet_data2_t *async_pd2 = esp_post_data2 (b);
623 u8 *tag = payload + len, *iv = payload + esp_sz, *aad = 0;
624 u32 key_index;
625 u32 crypto_len, integ_len = 0;
626 i16 crypto_start_offset, integ_start_offset = 0;
627 u8 flags = 0;
628
629 if (!ipsec_sa_is_set_IS_AEAD (sa0))
630 {
631 /* linked algs */
632 key_index = sa0->linked_key_index;
633 integ_start_offset = payload - b->data;
634 integ_len = len;
Benoît Ganne48524a92021-01-22 18:11:37 +0100635 if (PREDICT_TRUE (sa0->integ_op_id != VNET_CRYPTO_OP_NONE))
636 flags |= VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
Fan Zhangf5395782020-04-29 14:00:03 +0100637
638 if (pd->is_chain)
639 {
640 /* buffer is chained */
641 integ_len = pd->current_length;
642
643 /* special case when ICV is splitted and needs to be reassembled
644 * first -> move it to the last buffer. Also take into account
645 * that ESN needs to be added after encrypted data and may or
646 * may not fit in the tail.*/
647 if (pd2->lb->current_length < icv_sz)
648 {
649 u8 extra_esn = 0;
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000650 tag = esp_move_icv_esn (vm, b, pd, pd2, icv_sz, sa0,
Fan Zhangf5395782020-04-29 14:00:03 +0100651 &extra_esn, &integ_len);
652
653 if (extra_esn)
654 {
655 /* esn is in the last buffer, that was unlinked from
656 * the chain */
657 integ_len = b->current_length;
658 }
659 else
660 {
661 if (pd2->lb == b)
662 {
663 /* we now have a single buffer of crypto data, adjust
664 * the length (second buffer contains only ICV) */
665 len = b->current_length;
666 goto out;
667 }
668 }
669 }
670 else
671 tag = vlib_buffer_get_tail (pd2->lb) - icv_sz;
672
673 flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
674 if (esp_decrypt_chain_integ (vm, ptd, pd2, sa0, b, icv_sz, payload,
675 pd->current_length, &tag,
676 0, &integ_len) < 0)
677 {
678 /* allocate buffer failed, will not add to frame and drop */
Neale Rannsfc811342021-02-26 10:35:33 +0000679 return (ESP_DECRYPT_ERROR_NO_BUFFERS);
Fan Zhangf5395782020-04-29 14:00:03 +0100680 }
681 }
682 else
683 esp_insert_esn (vm, sa0, pd2, &integ_len, &tag, &len, b, payload);
684 }
685 else
686 key_index = sa0->crypto_key_index;
687
688out:
689 /* crypto */
690 payload += esp_sz;
691 len -= esp_sz;
692 iv = payload;
693
Benoît Ganne490b9272021-01-22 18:03:09 +0100694 if (ipsec_sa_is_set_IS_CTR (sa0))
Fan Zhangf5395782020-04-29 14:00:03 +0100695 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100696 /* construct nonce in a scratch space in front of the IP header */
697 esp_ctr_nonce_t *nonce =
698 (esp_ctr_nonce_t *) (payload - esp_sz - pd->hdr_sz - sizeof (*nonce));
699 if (ipsec_sa_is_set_IS_AEAD (sa0))
700 {
701 /* constuct aad in a scratch space in front of the nonce */
702 esp_header_t *esp0 = (esp_header_t *) (payload - esp_sz);
703 aad = (u8 *) nonce - sizeof (esp_aead_t);
704 esp_aad_fill (aad, esp0, sa0);
705 tag = payload + len;
706 }
707 else
708 {
709 nonce->ctr = clib_host_to_net_u32 (1);
710 }
711 nonce->salt = sa0->salt;
712 ASSERT (sizeof (u64) == iv_sz);
713 nonce->iv = *(u64 *) iv;
714 iv = (u8 *) nonce;
Fan Zhangf5395782020-04-29 14:00:03 +0100715 }
716
717 crypto_start_offset = (payload += iv_sz) - b->data;
718 crypto_len = len - iv_sz;
719
720 if (pd->is_chain && (pd2->lb != b))
721 {
722 /* buffer is chained */
723 flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
724
PiotrX Kleskia9585fd2020-12-11 15:10:31 +0000725 crypto_len = esp_decrypt_chain_crypto (vm, ptd, pd, pd2, sa0, b, icv_sz,
Fan Zhangf5395782020-04-29 14:00:03 +0100726 payload,
727 len - pd->iv_sz + pd->icv_sz,
728 &tag, 0);
729 }
730
731 *async_pd = *pd;
732 *async_pd2 = *pd2;
733 pd->protect_index = current_protect_index;
Fan Zhangf5395782020-04-29 14:00:03 +0100734
735 /* for AEAD integ_len - crypto_len will be negative, it is ok since it
736 * is ignored by the engine. */
Neale Rannsfc811342021-02-26 10:35:33 +0000737 vnet_crypto_async_add_to_frame (
738 vm, f, key_index, crypto_len, integ_len - crypto_len, crypto_start_offset,
739 integ_start_offset, bi, async_next, iv, tag, aad, flags);
740
741 return (ESP_DECRYPT_ERROR_RX_PKTS);
Fan Zhangf5395782020-04-29 14:00:03 +0100742}
743
744static_always_inline void
745esp_decrypt_post_crypto (vlib_main_t * vm, vlib_node_runtime_t * node,
746 esp_decrypt_packet_data_t * pd,
747 esp_decrypt_packet_data2_t * pd2, vlib_buffer_t * b,
748 u16 * next, int is_ip6, int is_tun, int is_async)
749{
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000750 ipsec_sa_t *sa0 = ipsec_sa_get (pd->sa_index);
Fan Zhangf5395782020-04-29 14:00:03 +0100751 vlib_buffer_t *lb = b;
752 const u8 esp_sz = sizeof (esp_header_t);
753 const u8 tun_flags = IPSEC_SA_FLAG_IS_TUNNEL | IPSEC_SA_FLAG_IS_TUNNEL_V6;
754 u8 pad_length = 0, next_header = 0;
755 u16 icv_sz;
756
757 /*
758 * redo the anti-reply check
759 * in this frame say we have sequence numbers, s, s+1, s+1, s+1
760 * and s and s+1 are in the window. When we did the anti-replay
761 * check above we did so against the state of the window (W),
762 * after packet s-1. So each of the packets in the sequence will be
763 * accepted.
764 * This time s will be cheked against Ws-1, s+1 chceked against Ws
765 * (i.e. the window state is updated/advnaced)
766 * so this time the successive s+! packet will be dropped.
767 * This is a consequence of batching the decrypts. If the
768 * check-dcrypt-advance process was done for each packet it would
769 * be fine. But we batch the decrypts because it's much more efficient
770 * to do so in SW and if we offload to HW and the process is async.
771 *
772 * You're probably thinking, but this means an attacker can send the
773 * above sequence and cause VPP to perform decrpyts that will fail,
774 * and that's true. But if the attacker can determine s (a valid
775 * sequence number in the window) which is non-trivial, it can generate
776 * a sequence s, s+1, s+2, s+3, ... s+n and nothing will prevent any
777 * implementation, sequential or batching, from decrypting these.
778 */
779 if (ipsec_sa_anti_replay_check (sa0, pd->seq))
780 {
781 b->error = node->errors[ESP_DECRYPT_ERROR_REPLAY];
782 next[0] = ESP_DECRYPT_NEXT_DROP;
783 return;
784 }
785
786 ipsec_sa_anti_replay_advance (sa0, pd->seq);
787
788 if (pd->is_chain)
789 {
790 lb = pd2->lb;
791 icv_sz = pd2->icv_removed ? 0 : pd->icv_sz;
792 if (pd2->free_buffer_index)
793 {
794 vlib_buffer_free_one (vm, pd2->free_buffer_index);
795 lb->next_buffer = 0;
796 }
797 if (lb->current_length < sizeof (esp_footer_t) + icv_sz)
798 {
799 /* esp footer is either splitted in two buffers or in the before
800 * last buffer */
801
802 vlib_buffer_t *before_last = b, *bp = b;
803 while (bp->flags & VLIB_BUFFER_NEXT_PRESENT)
804 {
805 before_last = bp;
806 bp = vlib_get_buffer (vm, bp->next_buffer);
807 }
808 u8 *bt = vlib_buffer_get_tail (before_last);
809
810 if (lb->current_length == icv_sz)
811 {
812 esp_footer_t *f = (esp_footer_t *) (bt - sizeof (*f));
813 pad_length = f->pad_length;
814 next_header = f->next_header;
815 }
816 else
817 {
818 pad_length = (bt - 1)[0];
819 next_header = ((u8 *) vlib_buffer_get_current (lb))[0];
820 }
821 }
822 else
823 {
824 esp_footer_t *f =
825 (esp_footer_t *) (lb->data + lb->current_data +
826 lb->current_length - sizeof (esp_footer_t) -
827 icv_sz);
828 pad_length = f->pad_length;
829 next_header = f->next_header;
830 }
831 }
832 else
833 {
834 icv_sz = pd->icv_sz;
835 esp_footer_t *f =
836 (esp_footer_t *) (lb->data + lb->current_data + lb->current_length -
837 sizeof (esp_footer_t) - icv_sz);
838 pad_length = f->pad_length;
839 next_header = f->next_header;
840 }
841
842 u16 adv = pd->iv_sz + esp_sz;
843 u16 tail = sizeof (esp_footer_t) + pad_length + icv_sz;
844 u16 tail_orig = sizeof (esp_footer_t) + pad_length + pd->icv_sz;
845 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
846
847 if ((pd->flags & tun_flags) == 0 && !is_tun) /* transport mode */
848 {
849 u8 udp_sz = (is_ip6 == 0 && pd->flags & IPSEC_SA_FLAG_UDP_ENCAP) ?
850 sizeof (udp_header_t) : 0;
851 u16 ip_hdr_sz = pd->hdr_sz - udp_sz;
852 u8 *old_ip = b->data + pd->current_data - ip_hdr_sz - udp_sz;
853 u8 *ip = old_ip + adv + udp_sz;
854
855 if (is_ip6 && ip_hdr_sz > 64)
856 memmove (ip, old_ip, ip_hdr_sz);
857 else
858 clib_memcpy_le64 (ip, old_ip, ip_hdr_sz);
859
860 b->current_data = pd->current_data + adv - ip_hdr_sz;
861 b->current_length += ip_hdr_sz - adv;
862 esp_remove_tail (vm, b, lb, tail);
863
864 if (is_ip6)
865 {
866 ip6_header_t *ip6 = (ip6_header_t *) ip;
867 u16 len = clib_net_to_host_u16 (ip6->payload_length);
868 len -= adv + tail_orig;
869 ip6->payload_length = clib_host_to_net_u16 (len);
870 ip6->protocol = next_header;
871 next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
872 }
873 else
874 {
875 ip4_header_t *ip4 = (ip4_header_t *) ip;
876 ip_csum_t sum = ip4->checksum;
877 u16 len = clib_net_to_host_u16 (ip4->length);
878 len = clib_host_to_net_u16 (len - adv - tail_orig - udp_sz);
879 sum = ip_csum_update (sum, ip4->protocol, next_header,
880 ip4_header_t, protocol);
881 sum = ip_csum_update (sum, ip4->length, len, ip4_header_t, length);
882 ip4->checksum = ip_csum_fold (sum);
883 ip4->protocol = next_header;
884 ip4->length = len;
885 next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
886 }
887 }
888 else
889 {
890 if (PREDICT_TRUE (next_header == IP_PROTOCOL_IP_IN_IP))
891 {
892 next[0] = ESP_DECRYPT_NEXT_IP4_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 }
897 else if (next_header == IP_PROTOCOL_IPV6)
898 {
899 next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
900 b->current_data = pd->current_data + adv;
901 b->current_length = pd->current_length - adv;
902 esp_remove_tail (vm, b, lb, tail);
903 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000904 else if (next_header == IP_PROTOCOL_MPLS_IN_IP)
905 {
906 next[0] = ESP_DECRYPT_NEXT_MPLS_INPUT;
907 b->current_data = pd->current_data + adv;
908 b->current_length = pd->current_length - adv;
909 esp_remove_tail (vm, b, lb, tail);
910 }
Fan Zhangf5395782020-04-29 14:00:03 +0100911 else
912 {
913 if (is_tun && next_header == IP_PROTOCOL_GRE)
914 {
915 gre_header_t *gre;
916
917 b->current_data = pd->current_data + adv;
918 b->current_length = pd->current_length - adv - tail;
919
920 gre = vlib_buffer_get_current (b);
921
922 vlib_buffer_advance (b, sizeof (*gre));
923
924 switch (clib_net_to_host_u16 (gre->protocol))
925 {
926 case GRE_PROTOCOL_teb:
927 vnet_update_l2_len (b);
928 next[0] = ESP_DECRYPT_NEXT_L2_INPUT;
929 break;
930 case GRE_PROTOCOL_ip4:
931 next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
932 break;
933 case GRE_PROTOCOL_ip6:
934 next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
935 break;
936 default:
937 b->error = node->errors[ESP_DECRYPT_ERROR_UNSUP_PAYLOAD];
938 next[0] = ESP_DECRYPT_NEXT_DROP;
939 break;
940 }
941 }
942 else
943 {
944 next[0] = ESP_DECRYPT_NEXT_DROP;
945 b->error = node->errors[ESP_DECRYPT_ERROR_UNSUP_PAYLOAD];
946 return;
947 }
948 }
949 if (is_tun)
950 {
951 if (ipsec_sa_is_set_IS_PROTECT (sa0))
952 {
953 /*
954 * There are two encap possibilities
955 * 1) the tunnel and ths SA are prodiving encap, i.e. it's
956 * MAC | SA-IP | TUN-IP | ESP | PAYLOAD
957 * implying the SA is in tunnel mode (on a tunnel interface)
958 * 2) only the tunnel provides encap
959 * MAC | TUN-IP | ESP | PAYLOAD
960 * implying the SA is in transport mode.
961 *
962 * For 2) we need only strip the tunnel encap and we're good.
963 * since the tunnel and crypto ecnap (int the tun=protect
964 * object) are the same and we verified above that these match
965 * for 1) we need to strip the SA-IP outer headers, to
966 * reveal the tunnel IP and then check that this matches
967 * the configured tunnel.
968 */
969 const ipsec_tun_protect_t *itp;
970
971 if (is_async)
972 itp = ipsec_tun_protect_get (pd->protect_index);
973 else
974 itp =
975 ipsec_tun_protect_get (vnet_buffer (b)->
976 ipsec.protect_index);
977
978 if (PREDICT_TRUE (next_header == IP_PROTOCOL_IP_IN_IP))
979 {
980 const ip4_header_t *ip4;
981
982 ip4 = vlib_buffer_get_current (b);
983
984 if (!ip46_address_is_equal_v4 (&itp->itp_tun.src,
985 &ip4->dst_address) ||
986 !ip46_address_is_equal_v4 (&itp->itp_tun.dst,
987 &ip4->src_address))
988 {
989 next[0] = ESP_DECRYPT_NEXT_DROP;
990 b->error = node->errors[ESP_DECRYPT_ERROR_TUN_NO_PROTO];
991 }
992 }
993 else if (next_header == IP_PROTOCOL_IPV6)
994 {
995 const ip6_header_t *ip6;
996
997 ip6 = vlib_buffer_get_current (b);
998
999 if (!ip46_address_is_equal_v6 (&itp->itp_tun.src,
1000 &ip6->dst_address) ||
1001 !ip46_address_is_equal_v6 (&itp->itp_tun.dst,
1002 &ip6->src_address))
1003 {
1004 next[0] = ESP_DECRYPT_NEXT_DROP;
1005 b->error = node->errors[ESP_DECRYPT_ERROR_TUN_NO_PROTO];
1006 }
1007 }
1008 }
1009 }
1010 }
1011}
1012
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001013always_inline uword
Neale Rannsf16e9a52021-02-25 19:09:24 +00001014esp_decrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1015 vlib_frame_t *from_frame, int is_ip6, int is_tun,
1016 u16 async_next_node)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018 ipsec_main_t *im = &ipsec_main;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001019 u32 thread_index = vm->thread_index;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001020 u16 len;
1021 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index);
Damjan Marionc98275f2019-03-06 14:05:01 +01001022 u32 *from = vlib_frame_vector_args (from_frame);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001023 u32 n_left = from_frame->n_vectors;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001024 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001025 vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
1026 u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0;
1027 u16 async_nexts[VLIB_FRAME_SIZE], *async_next = async_nexts, n_async = 0;
1028 u16 noop_nexts[VLIB_FRAME_SIZE], *noop_next = noop_nexts, n_noop = 0;
1029 u32 sync_bi[VLIB_FRAME_SIZE];
1030 u32 noop_bi[VLIB_FRAME_SIZE];
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001031 esp_decrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data;
Fan Zhangf5395782020-04-29 14:00:03 +01001032 esp_decrypt_packet_data2_t pkt_data2[VLIB_FRAME_SIZE], *pd2 = pkt_data2;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001033 esp_decrypt_packet_data_t cpd = { };
1034 u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0;
1035 const u8 esp_sz = sizeof (esp_header_t);
1036 ipsec_sa_t *sa0 = 0;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +00001037 vnet_crypto_op_t _op, *op = &_op;
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001038 vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
1039 vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
Fan Zhangf5395782020-04-29 14:00:03 +01001040 int is_async = im->async_mode;
Neale Rannsfc811342021-02-26 10:35:33 +00001041 vnet_crypto_async_op_id_t async_op = ~0;
Neale Rannsfc811342021-02-26 10:35:33 +00001042 vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
Neale Rannsf16e9a52021-02-25 19:09:24 +00001043 esp_decrypt_error_t err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001044
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001045 vlib_get_buffers (vm, from, b, n_left);
Fan Zhangf5395782020-04-29 14:00:03 +01001046 if (!is_async)
1047 {
1048 vec_reset_length (ptd->crypto_ops);
1049 vec_reset_length (ptd->integ_ops);
1050 vec_reset_length (ptd->chained_crypto_ops);
1051 vec_reset_length (ptd->chained_integ_ops);
1052 }
Neale Rannsfc811342021-02-26 10:35:33 +00001053 vec_reset_length (ptd->async_frames);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001054 vec_reset_length (ptd->chunks);
Neale Rannsf16e9a52021-02-25 19:09:24 +00001055 clib_memset (sync_nexts, -1, sizeof (sync_nexts));
Neale Rannsfc811342021-02-26 10:35:33 +00001056 clib_memset (async_frames, 0, sizeof (async_frames));
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001057
1058 while (n_left > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001059 {
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001060 u8 *payload;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061
Neale Rannsf16e9a52021-02-25 19:09:24 +00001062 err = ESP_DECRYPT_ERROR_RX_PKTS;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001063 if (n_left > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001064 {
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001065 u8 *p;
1066 vlib_prefetch_buffer_header (b[2], LOAD);
1067 p = vlib_buffer_get_current (b[1]);
1068 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
1069 p -= CLIB_CACHE_LINE_BYTES;
1070 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
1071 }
1072
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001073 u32 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
1074 if (n_bufs == 0)
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001075 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001076 err = ESP_DECRYPT_ERROR_NO_BUFFERS;
1077 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
1078 ESP_DECRYPT_NEXT_DROP);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001079 goto next;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001080 }
Damjan Marionc98275f2019-03-06 14:05:01 +01001081
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001082 if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index)
Damjan Marionc98275f2019-03-06 14:05:01 +01001083 {
Damjan Marion867dfdd2019-06-05 15:42:54 +02001084 if (current_sa_pkts)
1085 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1086 current_sa_index,
1087 current_sa_pkts,
1088 current_sa_bytes);
1089 current_sa_bytes = current_sa_pkts = 0;
1090
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001091 current_sa_index = vnet_buffer (b[0])->ipsec.sad_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 */
1095 CLIB_PREFETCH (sa0->cacheline1, CLIB_CACHE_LINE_BYTES, LOAD);
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
Neale Rannsfc811342021-02-26 10:35:33 +00001103 if (is_async)
1104 {
1105 async_op = sa0->crypto_async_dec_op_id;
1106
Neale Rannsfc811342021-02-26 10:35:33 +00001107 /* get a frame for this op if we don't yet have one or it's full
1108 */
1109 if (NULL == async_frames[async_op] ||
1110 vnet_crypto_async_frame_is_full (async_frames[async_op]))
1111 {
1112 async_frames[async_op] =
1113 vnet_crypto_async_get_frame (vm, async_op);
1114 /* Save the frame to the list we'll submit at the end */
1115 vec_add1 (ptd->async_frames, async_frames[async_op]);
Fan Zhangf5395782020-04-29 14:00:03 +01001116 }
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001117 }
1118
Neale Ranns1a52d372021-02-04 11:33:32 +00001119 if (PREDICT_FALSE (~0 == sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +00001120 {
1121 /* this is the first packet to use this SA, claim the SA
1122 * for this thread. this could happen simultaneously on
1123 * another thread */
Neale Ranns1a52d372021-02-04 11:33:32 +00001124 clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
Neale Rannsf62a8c02019-04-02 08:13:33 +00001125 ipsec_sa_assign_thread (thread_index));
1126 }
1127
Neale Ranns1a52d372021-02-04 11:33:32 +00001128 if (PREDICT_FALSE (thread_index != sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +00001129 {
Neale Rannsaa7d7662021-02-10 08:42:49 +00001130 vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001131 err = ESP_DECRYPT_ERROR_HANDOFF;
1132 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
1133 ESP_DECRYPT_NEXT_HANDOFF);
Neale Rannsf62a8c02019-04-02 08:13:33 +00001134 goto next;
1135 }
1136
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001137 /* store packet data for next round for easier prefetch */
1138 pd->sa_data = cpd.sa_data;
1139 pd->current_data = b[0]->current_data;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001140 pd->hdr_sz = pd->current_data - vnet_buffer (b[0])->l3_hdr_offset;
1141 payload = b[0]->data + pd->current_data;
Neale Ranns6afaae12019-07-17 15:07:14 +00001142 pd->seq = clib_host_to_net_u32 (((esp_header_t *) payload)->seq);
Fan Zhangf5395782020-04-29 14:00:03 +01001143 pd->is_chain = 0;
1144 pd2->lb = b[0];
1145 pd2->free_buffer_index = 0;
1146 pd2->icv_removed = 0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001147
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001148 if (n_bufs > 1)
1149 {
Fan Zhangf5395782020-04-29 14:00:03 +01001150 pd->is_chain = 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001151 /* find last buffer in the chain */
Fan Zhangf5395782020-04-29 14:00:03 +01001152 while (pd2->lb->flags & VLIB_BUFFER_NEXT_PRESENT)
1153 pd2->lb = vlib_get_buffer (vm, pd2->lb->next_buffer);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001154
1155 crypto_ops = &ptd->chained_crypto_ops;
1156 integ_ops = &ptd->chained_integ_ops;
1157 }
Fan Zhangf5395782020-04-29 14:00:03 +01001158
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001159 pd->current_length = b[0]->current_length;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001160
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001161 /* anti-reply check */
Neale Ranns6afaae12019-07-17 15:07:14 +00001162 if (ipsec_sa_anti_replay_check (sa0, pd->seq))
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001163 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001164 err = ESP_DECRYPT_ERROR_REPLAY;
1165 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
1166 ESP_DECRYPT_NEXT_DROP);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001167 goto next;
1168 }
1169
Damjan Mariona829b132019-04-24 23:39:16 +02001170 if (pd->current_length < cpd.icv_sz + esp_sz + cpd.iv_sz)
1171 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001172 err = ESP_DECRYPT_ERROR_RUNT;
1173 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
1174 ESP_DECRYPT_NEXT_DROP);
Damjan Mariona829b132019-04-24 23:39:16 +02001175 goto next;
1176 }
1177
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001178 len = pd->current_length - cpd.icv_sz;
1179 current_sa_pkts += 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001180 current_sa_bytes += vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001181
Fan Zhangf5395782020-04-29 14:00:03 +01001182 if (is_async)
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001183 {
Neale Rannsfc811342021-02-26 10:35:33 +00001184
1185 err = esp_decrypt_prepare_async_frame (
1186 vm, node, ptd, async_frames[async_op], sa0, payload, len,
Neale Rannsf16e9a52021-02-25 19:09:24 +00001187 cpd.icv_sz, cpd.iv_sz, pd, pd2, from[b - bufs], b[0], async_next,
1188 async_next_node);
Neale Rannsfc811342021-02-26 10:35:33 +00001189 if (ESP_DECRYPT_ERROR_RX_PKTS != err)
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001190 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001191 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
1192 ESP_DECRYPT_NEXT_DROP);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001193 }
Damjan Marionc98275f2019-03-06 14:05:01 +01001194 }
Fan Zhangf5395782020-04-29 14:00:03 +01001195 else
Neale Rannsf16e9a52021-02-25 19:09:24 +00001196 esp_decrypt_prepare_sync_op (
1197 vm, node, ptd, &crypto_ops, &integ_ops, op, sa0, payload, len,
1198 cpd.icv_sz, cpd.iv_sz, pd, pd2, b[0], sync_next, b - bufs);
Damjan Marionc98275f2019-03-06 14:05:01 +01001199 /* next */
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001200 next:
Neale Rannsf16e9a52021-02-25 19:09:24 +00001201 if (ESP_DECRYPT_ERROR_RX_PKTS != err)
1202 {
1203 noop_bi[n_noop] = from[b - bufs];
1204 n_noop++;
1205 noop_next++;
1206 }
1207 else if (!is_async)
1208 {
1209 sync_bi[n_sync] = from[b - bufs];
1210 sync_bufs[n_sync] = b[0];
1211 n_sync++;
1212 sync_next++;
1213 pd += 1;
1214 pd2 += 1;
1215 }
1216 else
1217 {
1218 n_async++;
1219 async_next++;
1220 }
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001221 n_left -= 1;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001222 b += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223 }
Damjan Marionc98275f2019-03-06 14:05:01 +01001224
Neale Ranns02950402019-12-20 00:54:57 +00001225 if (PREDICT_TRUE (~0 != current_sa_index))
1226 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1227 current_sa_index, current_sa_pkts,
1228 current_sa_bytes);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001229
Neale Rannsf16e9a52021-02-25 19:09:24 +00001230 if (n_async)
Fan Zhangf5395782020-04-29 14:00:03 +01001231 {
Neale Rannsfc811342021-02-26 10:35:33 +00001232 /* submit all of the open frames */
1233 vnet_crypto_async_frame_t **async_frame;
1234
1235 vec_foreach (async_frame, ptd->async_frames)
Fan Zhangf5395782020-04-29 14:00:03 +01001236 {
Neale Rannsfc811342021-02-26 10:35:33 +00001237 if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0)
1238 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001239 n_noop += esp_async_recycle_failed_submit (
1240 vm, *async_frame, node, ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR,
1241 n_sync, noop_bi, noop_nexts, ESP_DECRYPT_NEXT_DROP);
Neale Rannsfc811342021-02-26 10:35:33 +00001242 vnet_crypto_async_reset_frame (*async_frame);
1243 vnet_crypto_async_free_frame (vm, *async_frame);
1244 }
Fan Zhangf5395782020-04-29 14:00:03 +01001245 }
Fan Zhangf5395782020-04-29 14:00:03 +01001246 }
Fan Zhangf5395782020-04-29 14:00:03 +01001247
Neale Rannsf16e9a52021-02-25 19:09:24 +00001248 if (n_sync)
1249 {
1250 esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts,
1251 ESP_DECRYPT_ERROR_INTEG_ERROR);
1252 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs,
1253 sync_nexts, ptd->chunks,
1254 ESP_DECRYPT_ERROR_INTEG_ERROR);
1255
1256 esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts,
Fan Zhangf5395782020-04-29 14:00:03 +01001257 ESP_DECRYPT_ERROR_DECRYPTION_FAILED);
Neale Rannsf16e9a52021-02-25 19:09:24 +00001258 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs,
1259 sync_nexts, ptd->chunks,
Fan Zhangf5395782020-04-29 14:00:03 +01001260 ESP_DECRYPT_ERROR_DECRYPTION_FAILED);
1261 }
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001262
1263 /* Post decryption ronud - adjust packet data start and length and next
1264 node */
1265
Neale Rannsf16e9a52021-02-25 19:09:24 +00001266 n_left = n_sync;
1267 sync_next = sync_nexts;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001268 pd = pkt_data;
Fan Zhangf5395782020-04-29 14:00:03 +01001269 pd2 = pkt_data2;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001270 b = sync_bufs;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001271
1272 while (n_left)
1273 {
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001274 if (n_left >= 2)
1275 {
1276 void *data = b[1]->data + pd[1].current_data;
1277
1278 /* buffer metadata */
1279 vlib_prefetch_buffer_header (b[1], LOAD);
1280
1281 /* esp_footer_t */
1282 CLIB_PREFETCH (data + pd[1].current_length - pd[1].icv_sz - 2,
1283 CLIB_CACHE_LINE_BYTES, LOAD);
1284
1285 /* packet headers */
1286 CLIB_PREFETCH (data - CLIB_CACHE_LINE_BYTES,
1287 CLIB_CACHE_LINE_BYTES * 2, LOAD);
1288 }
1289
Christian Hoppsd570e532020-08-25 12:40:40 -04001290 /* save the sa_index as GRE_teb post_crypto changes L2 opaque */
1291 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1292 current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
1293
Neale Rannsf16e9a52021-02-25 19:09:24 +00001294 if (sync_next[0] >= ESP_DECRYPT_N_NEXT)
1295 esp_decrypt_post_crypto (vm, node, pd, pd2, b[0], sync_next, is_ip6,
Fan Zhangf5395782020-04-29 14:00:03 +01001296 is_tun, 0);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001297
Fan Zhangf5395782020-04-29 14:00:03 +01001298 /* trace: */
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001299 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1300 {
1301 esp_decrypt_trace_t *tr;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001302 tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
Neale Rannsc5fe57d2021-02-25 16:01:28 +00001303 sa0 = ipsec_sa_get (current_sa_index);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001304 tr->crypto_alg = sa0->crypto_alg;
1305 tr->integ_alg = sa0->integ_alg;
Neale Ranns6afaae12019-07-17 15:07:14 +00001306 tr->seq = pd->seq;
1307 tr->sa_seq = sa0->last_seq;
1308 tr->sa_seq_hi = sa0->seq_hi;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001309 }
1310
1311 /* next */
1312 n_left -= 1;
Neale Rannsf16e9a52021-02-25 19:09:24 +00001313 sync_next += 1;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001314 pd += 1;
Fan Zhangf5395782020-04-29 14:00:03 +01001315 pd2 += 1;
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001316 b += 1;
1317 }
1318
Neale Rannsf16e9a52021-02-25 19:09:24 +00001319 vlib_node_increment_counter (vm, node->node_index, ESP_DECRYPT_ERROR_RX_PKTS,
1320 from_frame->n_vectors);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001321
Neale Rannsf16e9a52021-02-25 19:09:24 +00001322 if (n_sync)
1323 vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync);
Damjan Marionb4fff3a2019-03-25 15:54:40 +01001324
Neale Rannsf16e9a52021-02-25 19:09:24 +00001325 if (n_noop)
1326 vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
1327
1328 return (from_frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001329}
1330
Fan Zhangf5395782020-04-29 14:00:03 +01001331always_inline uword
1332esp_decrypt_post_inline (vlib_main_t * vm,
1333 vlib_node_runtime_t * node,
1334 vlib_frame_t * from_frame, int is_ip6, int is_tun)
1335{
Fan Zhangf5395782020-04-29 14:00:03 +01001336 u32 *from = vlib_frame_vector_args (from_frame);
1337 u32 n_left = from_frame->n_vectors;
1338 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1339 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1340 vlib_get_buffers (vm, from, b, n_left);
1341
1342 while (n_left > 0)
1343 {
1344 esp_decrypt_packet_data_t *pd = &(esp_post_data (b[0]))->decrypt_data;
1345
1346 if (n_left > 2)
1347 {
1348 vlib_prefetch_buffer_header (b[2], LOAD);
1349 vlib_prefetch_buffer_header (b[1], LOAD);
1350 }
1351
1352 if (!pd->is_chain)
1353 esp_decrypt_post_crypto (vm, node, pd, 0, b[0], next, is_ip6, is_tun,
1354 1);
1355 else
1356 {
1357 esp_decrypt_packet_data2_t *pd2 = esp_post_data2 (b[0]);
1358 esp_decrypt_post_crypto (vm, node, pd, pd2, b[0], next, is_ip6,
1359 is_tun, 1);
1360 }
1361
1362 /*trace: */
1363 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1364 {
Neale Rannsc5fe57d2021-02-25 16:01:28 +00001365 ipsec_sa_t *sa0 = ipsec_sa_get (pd->sa_index);
Fan Zhangf5395782020-04-29 14:00:03 +01001366 esp_decrypt_trace_t *tr;
1367 esp_decrypt_packet_data_t *async_pd =
1368 &(esp_post_data (b[0]))->decrypt_data;
1369 tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
Neale Rannsc5fe57d2021-02-25 16:01:28 +00001370 sa0 = ipsec_sa_get (async_pd->sa_index);
Fan Zhangf5395782020-04-29 14:00:03 +01001371
1372 tr->crypto_alg = sa0->crypto_alg;
1373 tr->integ_alg = sa0->integ_alg;
1374 tr->seq = pd->seq;
1375 tr->sa_seq = sa0->last_seq;
1376 tr->sa_seq_hi = sa0->seq_hi;
1377 }
1378
1379 n_left--;
1380 next++;
1381 b++;
1382 }
1383
1384 n_left = from_frame->n_vectors;
1385 vlib_node_increment_counter (vm, node->node_index,
1386 ESP_DECRYPT_ERROR_RX_POST_PKTS, n_left);
1387
1388 vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left);
1389
1390 return n_left;
1391}
1392
Klement Sekerab8f35442018-10-29 13:38:19 +01001393VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm,
1394 vlib_node_runtime_t * node,
1395 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001396{
Fan Zhangf5395782020-04-29 14:00:03 +01001397 return esp_decrypt_inline (vm, node, from_frame, 0, 0,
1398 esp_decrypt_async_next.esp4_post_next);
1399}
1400
1401VLIB_NODE_FN (esp4_decrypt_post_node) (vlib_main_t * vm,
1402 vlib_node_runtime_t * node,
1403 vlib_frame_t * from_frame)
1404{
1405 return esp_decrypt_post_inline (vm, node, from_frame, 0, 0);
Neale Rannsc87b66c2019-02-07 07:26:12 -08001406}
1407
1408VLIB_NODE_FN (esp4_decrypt_tun_node) (vlib_main_t * vm,
1409 vlib_node_runtime_t * node,
1410 vlib_frame_t * from_frame)
1411{
Fan Zhangf5395782020-04-29 14:00:03 +01001412 return esp_decrypt_inline (vm, node, from_frame, 0, 1,
1413 esp_decrypt_async_next.esp4_tun_post_next);
1414}
1415
1416VLIB_NODE_FN (esp4_decrypt_tun_post_node) (vlib_main_t * vm,
1417 vlib_node_runtime_t * node,
1418 vlib_frame_t * from_frame)
1419{
1420 return esp_decrypt_post_inline (vm, node, from_frame, 0, 1);
Neale Rannsc87b66c2019-02-07 07:26:12 -08001421}
1422
1423VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm,
1424 vlib_node_runtime_t * node,
1425 vlib_frame_t * from_frame)
1426{
Fan Zhangf5395782020-04-29 14:00:03 +01001427 return esp_decrypt_inline (vm, node, from_frame, 1, 0,
1428 esp_decrypt_async_next.esp6_post_next);
1429}
1430
1431VLIB_NODE_FN (esp6_decrypt_post_node) (vlib_main_t * vm,
1432 vlib_node_runtime_t * node,
1433 vlib_frame_t * from_frame)
1434{
1435 return esp_decrypt_post_inline (vm, node, from_frame, 1, 0);
Neale Rannsc87b66c2019-02-07 07:26:12 -08001436}
1437
1438VLIB_NODE_FN (esp6_decrypt_tun_node) (vlib_main_t * vm,
1439 vlib_node_runtime_t * node,
1440 vlib_frame_t * from_frame)
1441{
Fan Zhangf5395782020-04-29 14:00:03 +01001442 return esp_decrypt_inline (vm, node, from_frame, 1, 1,
1443 esp_decrypt_async_next.esp6_tun_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001444}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001445
Fan Zhangf5395782020-04-29 14:00:03 +01001446VLIB_NODE_FN (esp6_decrypt_tun_post_node) (vlib_main_t * vm,
1447 vlib_node_runtime_t * node,
1448 vlib_frame_t * from_frame)
1449{
1450 return esp_decrypt_post_inline (vm, node, from_frame, 1, 1);
1451}
1452
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001453/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001454VLIB_REGISTER_NODE (esp4_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001455 .name = "esp4-decrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001456 .vector_size = sizeof (u32),
1457 .format_trace = format_esp_decrypt_trace,
1458 .type = VLIB_NODE_TYPE_INTERNAL,
1459
1460 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1461 .error_strings = esp_decrypt_error_strings,
1462
1463 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1464 .next_nodes = {
Neale Rannsf62a8c02019-04-02 08:13:33 +00001465 [ESP_DECRYPT_NEXT_DROP] = "ip4-drop",
1466 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1467 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
Neale Ranns4a58e492020-12-21 13:19:10 +00001468 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-drop",
Neale Ranns568acbb2019-12-18 05:54:40 +00001469 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
Neale Rannsf62a8c02019-04-02 08:13:33 +00001470 [ESP_DECRYPT_NEXT_HANDOFF] = "esp4-decrypt-handoff",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001471 },
1472};
1473
Fan Zhangf5395782020-04-29 14:00:03 +01001474VLIB_REGISTER_NODE (esp4_decrypt_post_node) = {
1475 .name = "esp4-decrypt-post",
1476 .vector_size = sizeof (u32),
1477 .format_trace = format_esp_decrypt_trace,
1478 .type = VLIB_NODE_TYPE_INTERNAL,
1479
1480 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1481 .error_strings = esp_decrypt_error_strings,
1482
1483 .sibling_of = "esp4-decrypt",
1484};
1485
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001486VLIB_REGISTER_NODE (esp6_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001487 .name = "esp6-decrypt",
1488 .vector_size = sizeof (u32),
1489 .format_trace = format_esp_decrypt_trace,
1490 .type = VLIB_NODE_TYPE_INTERNAL,
1491
1492 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1493 .error_strings = esp_decrypt_error_strings,
1494
1495 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1496 .next_nodes = {
Neale Rannsf62a8c02019-04-02 08:13:33 +00001497 [ESP_DECRYPT_NEXT_DROP] = "ip6-drop",
1498 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1499 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
Neale Ranns4a58e492020-12-21 13:19:10 +00001500 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-drop",
Neale Ranns568acbb2019-12-18 05:54:40 +00001501 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
Neale Rannsf62a8c02019-04-02 08:13:33 +00001502 [ESP_DECRYPT_NEXT_HANDOFF]= "esp6-decrypt-handoff",
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001503 },
1504};
Neale Rannsc87b66c2019-02-07 07:26:12 -08001505
Fan Zhangf5395782020-04-29 14:00:03 +01001506VLIB_REGISTER_NODE (esp6_decrypt_post_node) = {
1507 .name = "esp6-decrypt-post",
1508 .vector_size = sizeof (u32),
1509 .format_trace = format_esp_decrypt_trace,
1510 .type = VLIB_NODE_TYPE_INTERNAL,
1511
1512 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1513 .error_strings = esp_decrypt_error_strings,
1514
1515 .sibling_of = "esp6-decrypt",
1516};
1517
Neale Rannsc87b66c2019-02-07 07:26:12 -08001518VLIB_REGISTER_NODE (esp4_decrypt_tun_node) = {
1519 .name = "esp4-decrypt-tun",
1520 .vector_size = sizeof (u32),
1521 .format_trace = format_esp_decrypt_trace,
1522 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsc87b66c2019-02-07 07:26:12 -08001523 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1524 .error_strings = esp_decrypt_error_strings,
Neale Rannsf62a8c02019-04-02 08:13:33 +00001525 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1526 .next_nodes = {
1527 [ESP_DECRYPT_NEXT_DROP] = "ip4-drop",
1528 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1529 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
Neale Ranns4a58e492020-12-21 13:19:10 +00001530 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-input",
Neale Ranns568acbb2019-12-18 05:54:40 +00001531 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001532 [ESP_DECRYPT_NEXT_HANDOFF] = "esp4-decrypt-tun-handoff",
Neale Rannsf62a8c02019-04-02 08:13:33 +00001533 },
Neale Rannsc87b66c2019-02-07 07:26:12 -08001534};
1535
Fan Zhangf5395782020-04-29 14:00:03 +01001536VLIB_REGISTER_NODE (esp4_decrypt_tun_post_node) = {
1537 .name = "esp4-decrypt-tun-post",
1538 .vector_size = sizeof (u32),
1539 .format_trace = format_esp_decrypt_trace,
1540 .type = VLIB_NODE_TYPE_INTERNAL,
1541
1542 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1543 .error_strings = esp_decrypt_error_strings,
1544
1545 .sibling_of = "esp4-decrypt-tun",
1546};
1547
Neale Rannsc87b66c2019-02-07 07:26:12 -08001548VLIB_REGISTER_NODE (esp6_decrypt_tun_node) = {
1549 .name = "esp6-decrypt-tun",
1550 .vector_size = sizeof (u32),
1551 .format_trace = format_esp_decrypt_trace,
1552 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsc87b66c2019-02-07 07:26:12 -08001553 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1554 .error_strings = esp_decrypt_error_strings,
Neale Rannsf62a8c02019-04-02 08:13:33 +00001555 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1556 .next_nodes = {
1557 [ESP_DECRYPT_NEXT_DROP] = "ip6-drop",
1558 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1559 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
Neale Ranns4a58e492020-12-21 13:19:10 +00001560 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-input",
Neale Ranns568acbb2019-12-18 05:54:40 +00001561 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
Neale Ranns4a56f4e2019-12-23 04:10:25 +00001562 [ESP_DECRYPT_NEXT_HANDOFF]= "esp6-decrypt-tun-handoff",
Neale Rannsf62a8c02019-04-02 08:13:33 +00001563 },
Neale Rannsc87b66c2019-02-07 07:26:12 -08001564};
Fan Zhangf5395782020-04-29 14:00:03 +01001565
1566VLIB_REGISTER_NODE (esp6_decrypt_tun_post_node) = {
1567 .name = "esp6-decrypt-tun-post",
1568 .vector_size = sizeof (u32),
1569 .format_trace = format_esp_decrypt_trace,
1570 .type = VLIB_NODE_TYPE_INTERNAL,
1571
1572 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1573 .error_strings = esp_decrypt_error_strings,
1574
1575 .sibling_of = "esp6-decrypt-tun",
1576};
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001577/* *INDENT-ON* */
1578
Neale Ranns2d498302021-02-25 08:38:58 +00001579#ifndef CLIB_MARCH_VARIANT
1580
1581static clib_error_t *
1582esp_decrypt_init (vlib_main_t *vm)
1583{
1584 ipsec_main_t *im = &ipsec_main;
1585
1586 im->esp4_dec_fq_index =
1587 vlib_frame_queue_main_init (esp4_decrypt_node.index, 0);
1588 im->esp6_dec_fq_index =
1589 vlib_frame_queue_main_init (esp6_decrypt_node.index, 0);
1590 im->esp4_dec_tun_fq_index =
1591 vlib_frame_queue_main_init (esp4_decrypt_tun_node.index, 0);
1592 im->esp6_dec_tun_fq_index =
1593 vlib_frame_queue_main_init (esp6_decrypt_tun_node.index, 0);
1594
1595 return 0;
1596}
1597
1598VLIB_INIT_FUNCTION (esp_decrypt_init);
1599
1600#endif
1601
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001602/*
1603 * fd.io coding-style-patch-verification: ON
1604 *
1605 * Local Variables:
1606 * eval: (c-set-style "gnu")
1607 * End:
1608 */