blob: 214cf674c756f97f6e37cfce27fcca30da4d7e49 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * esp_encrypt.c : IPSec ESP encrypt node
3 *
4 * Copyright (c) 2015 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
21
Damjan Marion91f17dc2019-03-18 18:59:25 +010022#include <vnet/crypto/crypto.h>
23
Ed Warnickecb9cada2015-12-08 15:45:58 -070024#include <vnet/ipsec/ipsec.h>
Neale Ranns28287212019-12-16 00:53:11 +000025#include <vnet/ipsec/ipsec_tun.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026#include <vnet/ipsec/esp.h>
Neale Ranns041add72020-01-02 04:06:10 +000027#include <vnet/tunnel/tunnel_dp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
Neale Ranns4a58e492020-12-21 13:19:10 +000029#define foreach_esp_encrypt_next \
30 _ (DROP4, "ip4-drop") \
31 _ (DROP6, "ip6-drop") \
32 _ (DROP_MPLS, "mpls-drop") \
33 _ (HANDOFF4, "handoff4") \
34 _ (HANDOFF6, "handoff6") \
35 _ (HANDOFF_MPLS, "handoff-mpls") \
36 _ (INTERFACE_OUTPUT, "interface-output")
Ed Warnickecb9cada2015-12-08 15:45:58 -070037
38#define _(v, s) ESP_ENCRYPT_NEXT_##v,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070039typedef enum
40{
Ed Warnickecb9cada2015-12-08 15:45:58 -070041 foreach_esp_encrypt_next
42#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070043 ESP_ENCRYPT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070044} esp_encrypt_next_t;
45
Neale Rannsf16e9a52021-02-25 19:09:24 +000046#define foreach_esp_encrypt_error \
47 _ (RX_PKTS, "ESP pkts received") \
48 _ (POST_RX_PKTS, "ESP-post pkts received") \
49 _ (HANDOFF, "Hand-off") \
50 _ (SEQ_CYCLED, "sequence number cycled (packet dropped)") \
51 _ (CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
52 _ (CRYPTO_QUEUE_FULL, "crypto queue full (packet dropped)") \
53 _ (NO_BUFFERS, "no buffers (packet dropped)")
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070055typedef enum
56{
Ed Warnickecb9cada2015-12-08 15:45:58 -070057#define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
58 foreach_esp_encrypt_error
59#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070060 ESP_ENCRYPT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070061} esp_encrypt_error_t;
62
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070063static char *esp_encrypt_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070064#define _(sym,string) string,
65 foreach_esp_encrypt_error
66#undef _
67};
68
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070069typedef struct
70{
Neale Ranns8d7c5022019-02-06 01:41:05 -080071 u32 sa_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 u32 spi;
73 u32 seq;
Neale Ranns6afaae12019-07-17 15:07:14 +000074 u32 sa_seq_hi;
Klement Sekera4b089f22018-04-17 18:04:57 +020075 u8 udp_encap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 ipsec_crypto_alg_t crypto_alg;
77 ipsec_integ_alg_t integ_alg;
78} esp_encrypt_trace_t;
79
Fan Zhangf5395782020-04-29 14:00:03 +010080typedef struct
81{
82 u32 next_index;
83} esp_encrypt_post_trace_t;
84
Ed Warnickecb9cada2015-12-08 15:45:58 -070085/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070086static u8 *
87format_esp_encrypt_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070088{
89 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
90 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070091 esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020093 s =
94 format (s,
Neale Ranns6afaae12019-07-17 15:07:14 +000095 "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
96 t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
97 format_ipsec_crypto_alg,
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020098 t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
99 t->udp_encap ? " udp-encap-enabled" : "");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 return s;
101}
102
Fan Zhangf5395782020-04-29 14:00:03 +0100103static u8 *
104format_esp_post_encrypt_trace (u8 * s, va_list * args)
105{
106 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
107 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
108 esp_encrypt_post_trace_t *t = va_arg (*args, esp_encrypt_post_trace_t *);
109
110 s = format (s, "esp-post: next node index %u", t->next_index);
111 return s;
112}
113
Damjan Marionc59b9a22019-03-19 15:38:40 +0100114/* pad packet in input buffer */
115static_always_inline u8 *
Neale Rannsf16e9a52021-02-25 19:09:24 +0000116esp_add_footer_and_icv (vlib_main_t *vm, vlib_buffer_t **last, u8 esp_align,
117 u8 icv_sz, vlib_node_runtime_t *node,
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000118 u16 buffer_data_size, uword total_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119{
Damjan Marionc59b9a22019-03-19 15:38:40 +0100120 static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = {
121 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
Milan Lenco7885c742020-08-20 13:23:09 +0200122 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
Damjan Marionc59b9a22019-03-19 15:38:40 +0100123 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000125 u16 min_length = total_len + sizeof (esp_footer_t);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500126 u16 new_length = round_pow2 (min_length, esp_align);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100127 u8 pad_bytes = new_length - min_length;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000128 esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (last[0]) +
129 last[0]->current_length + pad_bytes);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000130 u16 tail_sz = sizeof (esp_footer_t) + pad_bytes + icv_sz;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000132 if (last[0]->current_length + tail_sz > buffer_data_size)
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000133 {
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000134 u32 tmp_bi = 0;
135 if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1)
136 return 0;
Filip Tehlarc2c1bfd2020-02-13 07:49:30 +0000137
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000138 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
139 last[0]->next_buffer = tmp_bi;
140 last[0]->flags |= VLIB_BUFFER_NEXT_PRESENT;
141 f = (esp_footer_t *) (vlib_buffer_get_current (tmp) + pad_bytes);
142 tmp->current_length += tail_sz;
143 last[0] = tmp;
144 }
145 else
146 last[0]->current_length += tail_sz;
147
148 f->pad_length = pad_bytes;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100149 if (pad_bytes)
Benoît Ganne4505f012019-12-07 09:14:27 -0700150 {
151 ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE);
152 pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes);
153 clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes);
154 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100155
Damjan Marionc59b9a22019-03-19 15:38:40 +0100156 return &f->next_header;
157}
158
159static_always_inline void
160esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp)
161{
Neale Ranns1b582b82019-04-18 19:49:13 -0700162 ip_csum_t sum;
163 u16 old_len;
164
165 len = clib_net_to_host_u16 (len);
166 old_len = ip4->length;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100167
168 if (is_transport)
169 {
170 u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100171
Neale Ranns1b582b82019-04-18 19:49:13 -0700172 sum = ip_csum_update (ip4->checksum, ip4->protocol,
173 prot, ip4_header_t, protocol);
174 ip4->protocol = prot;
175
176 sum = ip_csum_update (sum, old_len, len, ip4_header_t, length);
177 }
178 else
179 sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
180
181 ip4->length = len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100182 ip4->checksum = ip_csum_fold (sum);
183}
184
185static_always_inline void
186esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
187{
188 clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
189 udp->length = clib_net_to_host_u16 (len);
190}
191
192static_always_inline u8
193ext_hdr_is_pre_esp (u8 nexthdr)
194{
195#ifdef CLIB_HAVE_VEC128
196 static const u8x16 ext_hdr_types = {
197 IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
198 IP_PROTOCOL_IPV6_ROUTE,
199 IP_PROTOCOL_IPV6_FRAGMENTATION,
200 };
201
202 return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
203#else
204 return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
205 (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) |
206 (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0);
207#endif
208}
209
210static_always_inline u8
Neale Ranns02950402019-12-20 00:54:57 +0000211esp_get_ip6_hdr_len (ip6_header_t * ip6, ip6_ext_header_t ** ext_hdr)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100212{
213 /* this code assumes that HbH, route and frag headers will be before
214 others, if that is not the case, they will end up encrypted */
Damjan Marionc59b9a22019-03-19 15:38:40 +0100215 u8 len = sizeof (ip6_header_t);
216 ip6_ext_header_t *p;
217
218 /* if next packet doesn't have ext header */
219 if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
Neale Ranns02950402019-12-20 00:54:57 +0000220 {
221 *ext_hdr = NULL;
222 return len;
223 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100224
225 p = (void *) (ip6 + 1);
226 len += ip6_ext_header_len (p);
227
228 while (ext_hdr_is_pre_esp (p->next_hdr))
229 {
230 len += ip6_ext_header_len (p);
231 p = ip6_ext_next_header (p);
232 }
233
Neale Ranns02950402019-12-20 00:54:57 +0000234 *ext_hdr = p;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100235 return len;
236}
237
Damjan Marionc59b9a22019-03-19 15:38:40 +0100238static_always_inline void
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000239esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
240 vnet_crypto_op_t * ops, vlib_buffer_t * b[],
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000241 u16 * nexts, vnet_crypto_op_chunk_t * chunks,
242 u16 drop_next)
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000243{
244 u32 n_fail, n_ops = vec_len (ops);
245 vnet_crypto_op_t *op = ops;
246
247 if (n_ops == 0)
248 return;
249
250 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
251
252 while (n_fail)
253 {
254 ASSERT (op - ops < n_ops);
255
256 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
257 {
258 u32 bi = op->user_data;
259 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000260 nexts[bi] = drop_next;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000261 n_fail--;
262 }
263 op++;
264 }
265}
266
267static_always_inline void
Damjan Marionc59b9a22019-03-19 15:38:40 +0100268esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000269 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts,
270 u16 drop_next)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100271{
272 u32 n_fail, n_ops = vec_len (ops);
273 vnet_crypto_op_t *op = ops;
274
275 if (n_ops == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 return;
277
Damjan Marionc59b9a22019-03-19 15:38:40 +0100278 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
Damjan Marionc59b9a22019-03-19 15:38:40 +0100280 while (n_fail)
281 {
282 ASSERT (op - ops < n_ops);
283
284 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
285 {
286 u32 bi = op->user_data;
287 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000288 nexts[bi] = drop_next;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100289 n_fail--;
290 }
291 op++;
292 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293}
294
Fan Zhangf5395782020-04-29 14:00:03 +0100295static_always_inline u32
296esp_encrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
297 ipsec_sa_t * sa0, vlib_buffer_t * b,
298 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
299 u32 start_len, u16 * n_ch)
300{
301 vnet_crypto_op_chunk_t *ch;
302 vlib_buffer_t *cb = b;
303 u32 n_chunks = 1;
304 u32 total_len;
305 vec_add2 (ptd->chunks, ch, 1);
306 total_len = ch->len = start_len;
307 ch->src = ch->dst = start;
308 cb = vlib_get_buffer (vm, cb->next_buffer);
309
310 while (1)
311 {
312 vec_add2 (ptd->chunks, ch, 1);
313 n_chunks += 1;
314 if (lb == cb)
315 total_len += ch->len = cb->current_length - icv_sz;
316 else
317 total_len += ch->len = cb->current_length;
318 ch->src = ch->dst = vlib_buffer_get_current (cb);
319
320 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
321 break;
322
323 cb = vlib_get_buffer (vm, cb->next_buffer);
324 }
325
326 if (n_ch)
327 *n_ch = n_chunks;
328
329 return total_len;
330}
331
332static_always_inline u32
333esp_encrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
334 ipsec_sa_t * sa0, vlib_buffer_t * b,
335 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
336 u32 start_len, u8 * digest, u16 * n_ch)
337{
338 vnet_crypto_op_chunk_t *ch;
339 vlib_buffer_t *cb = b;
340 u32 n_chunks = 1;
341 u32 total_len;
342 vec_add2 (ptd->chunks, ch, 1);
343 total_len = ch->len = start_len;
344 ch->src = start;
345 cb = vlib_get_buffer (vm, cb->next_buffer);
346
347 while (1)
348 {
349 vec_add2 (ptd->chunks, ch, 1);
350 n_chunks += 1;
351 if (lb == cb)
352 {
353 total_len += ch->len = cb->current_length - icv_sz;
354 if (ipsec_sa_is_set_USE_ESN (sa0))
355 {
356 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
357 clib_memcpy_fast (digest, &seq_hi, sizeof (seq_hi));
358 ch->len += sizeof (seq_hi);
359 total_len += sizeof (seq_hi);
360 }
361 }
362 else
363 total_len += ch->len = cb->current_length;
364 ch->src = vlib_buffer_get_current (cb);
365
366 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
367 break;
368
369 cb = vlib_get_buffer (vm, cb->next_buffer);
370 }
371
372 if (n_ch)
373 *n_ch = n_chunks;
374
375 return total_len;
376}
377
378always_inline void
Benoît Ganne490b9272021-01-22 18:03:09 +0100379esp_prepare_sync_op (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
380 vnet_crypto_op_t **crypto_ops,
381 vnet_crypto_op_t **integ_ops, ipsec_sa_t *sa0,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000382 u8 *payload, u16 payload_len, u8 iv_sz, u8 icv_sz, u32 bi,
383 vlib_buffer_t **b, vlib_buffer_t *lb, u32 hdr_len,
384 esp_header_t *esp)
Fan Zhangf5395782020-04-29 14:00:03 +0100385{
386 if (sa0->crypto_enc_op_id)
387 {
388 vnet_crypto_op_t *op;
389 vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
390 vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
391
392 op->src = op->dst = payload;
393 op->key_index = sa0->crypto_key_index;
394 op->len = payload_len - icv_sz;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000395 op->user_data = bi;
Fan Zhangf5395782020-04-29 14:00:03 +0100396
Benoît Ganne490b9272021-01-22 18:03:09 +0100397 if (ipsec_sa_is_set_IS_CTR (sa0))
Fan Zhangf5395782020-04-29 14:00:03 +0100398 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100399 ASSERT (sizeof (u64) == iv_sz);
400 /* construct nonce in a scratch space in front of the IP header */
401 esp_ctr_nonce_t *nonce =
402 (esp_ctr_nonce_t *) (payload - sizeof (u64) - hdr_len -
403 sizeof (*nonce));
404 u64 *pkt_iv = (u64 *) (payload - sizeof (u64));
Fan Zhangf5395782020-04-29 14:00:03 +0100405
Benoît Ganne490b9272021-01-22 18:03:09 +0100406 if (ipsec_sa_is_set_IS_AEAD (sa0))
407 {
408 /* constuct aad in a scratch space in front of the nonce */
409 op->aad = (u8 *) nonce - sizeof (esp_aead_t);
410 op->aad_len = esp_aad_fill (op->aad, esp, sa0);
411 op->tag = payload + op->len;
412 op->tag_len = 16;
413 }
414 else
415 {
416 nonce->ctr = clib_host_to_net_u32 (1);
417 }
Fan Zhangf5395782020-04-29 14:00:03 +0100418
Fan Zhangf5395782020-04-29 14:00:03 +0100419 nonce->salt = sa0->salt;
Benoît Ganne490b9272021-01-22 18:03:09 +0100420 nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa0->ctr_iv_counter++);
Fan Zhangf5395782020-04-29 14:00:03 +0100421 op->iv = (u8 *) nonce;
422 }
423 else
424 {
425 op->iv = payload - iv_sz;
426 op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
427 }
428
429 if (lb != b[0])
430 {
431 /* is chained */
432 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
433 op->chunk_index = vec_len (ptd->chunks);
434 op->tag = vlib_buffer_get_tail (lb) - icv_sz;
435 esp_encrypt_chain_crypto (vm, ptd, sa0, b[0], lb, icv_sz, payload,
436 payload_len, &op->n_chunks);
437 }
438 }
439
440 if (sa0->integ_op_id)
441 {
442 vnet_crypto_op_t *op;
443 vec_add2_aligned (integ_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
444 vnet_crypto_op_init (op, sa0->integ_op_id);
445 op->src = payload - iv_sz - sizeof (esp_header_t);
446 op->digest = payload + payload_len - icv_sz;
447 op->key_index = sa0->integ_key_index;
448 op->digest_len = icv_sz;
449 op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000450 op->user_data = bi;
Fan Zhangf5395782020-04-29 14:00:03 +0100451
452 if (lb != b[0])
453 {
454 /* is chained */
455 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
456 op->chunk_index = vec_len (ptd->chunks);
457 op->digest = vlib_buffer_get_tail (lb) - icv_sz;
458
459 esp_encrypt_chain_integ (vm, ptd, sa0, b[0], lb, icv_sz,
460 payload - iv_sz - sizeof (esp_header_t),
461 payload_len + iv_sz +
462 sizeof (esp_header_t), op->digest,
463 &op->n_chunks);
464 }
465 else if (ipsec_sa_is_set_USE_ESN (sa0))
466 {
467 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
468 clib_memcpy_fast (op->digest, &seq_hi, sizeof (seq_hi));
469 op->len += sizeof (seq_hi);
470 }
471 }
472}
473
Neale Rannsfc811342021-02-26 10:35:33 +0000474static_always_inline void
475esp_prepare_async_frame (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
476 vnet_crypto_async_frame_t *async_frame,
477 ipsec_sa_t *sa, vlib_buffer_t *b, esp_header_t *esp,
478 u8 *payload, u32 payload_len, u8 iv_sz, u8 icv_sz,
479 u32 bi, u16 next, u32 hdr_len, u16 async_next,
480 vlib_buffer_t *lb)
Fan Zhangf5395782020-04-29 14:00:03 +0100481{
482 esp_post_data_t *post = esp_post_data (b);
483 u8 *tag, *iv, *aad = 0;
484 u8 flag = 0;
485 u32 key_index;
486 i16 crypto_start_offset, integ_start_offset = 0;
487 u16 crypto_total_len, integ_total_len;
488
Fan Zhang18f0e312020-10-19 13:08:34 +0100489 post->next_index = next;
Fan Zhangf5395782020-04-29 14:00:03 +0100490
491 /* crypto */
492 crypto_start_offset = payload - b->data;
493 crypto_total_len = integ_total_len = payload_len - icv_sz;
494 tag = payload + crypto_total_len;
495
Fan Zhangf5395782020-04-29 14:00:03 +0100496 key_index = sa->linked_key_index;
497
Benoît Ganne490b9272021-01-22 18:03:09 +0100498 if (ipsec_sa_is_set_IS_CTR (sa))
Fan Zhangf5395782020-04-29 14:00:03 +0100499 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100500 ASSERT (sizeof (u64) == iv_sz);
501 /* construct nonce in a scratch space in front of the IP header */
502 esp_ctr_nonce_t *nonce = (esp_ctr_nonce_t *) (payload - sizeof (u64) -
503 hdr_len - sizeof (*nonce));
504 u64 *pkt_iv = (u64 *) (payload - sizeof (u64));
505
506 if (ipsec_sa_is_set_IS_AEAD (sa))
507 {
508 /* constuct aad in a scratch space in front of the nonce */
509 aad = (u8 *) nonce - sizeof (esp_aead_t);
510 esp_aad_fill (aad, esp, sa);
511 key_index = sa->crypto_key_index;
512 }
513 else
514 {
515 nonce->ctr = clib_host_to_net_u32 (1);
516 }
517
518 nonce->salt = sa->salt;
519 nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa->ctr_iv_counter++);
520 iv = (u8 *) nonce;
Fan Zhangf5395782020-04-29 14:00:03 +0100521 }
Benoît Ganne490b9272021-01-22 18:03:09 +0100522 else
Fan Zhangf5395782020-04-29 14:00:03 +0100523 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100524 iv = payload - iv_sz;
525 flag |= VNET_CRYPTO_OP_FLAG_INIT_IV;
Fan Zhangf5395782020-04-29 14:00:03 +0100526 }
527
Benoît Ganne490b9272021-01-22 18:03:09 +0100528 if (lb != b)
529 {
530 /* chain */
531 flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
532 tag = vlib_buffer_get_tail (lb) - icv_sz;
533 crypto_total_len = esp_encrypt_chain_crypto (vm, ptd, sa, b, lb, icv_sz,
534 payload, payload_len, 0);
535 }
536
537 if (sa->integ_op_id)
538 {
539 integ_start_offset = crypto_start_offset - iv_sz - sizeof (esp_header_t);
540 integ_total_len += iv_sz + sizeof (esp_header_t);
541
542 if (b != lb)
543 {
544 integ_total_len = esp_encrypt_chain_integ (
545 vm, ptd, sa, b, lb, icv_sz,
546 payload - iv_sz - sizeof (esp_header_t),
547 payload_len + iv_sz + sizeof (esp_header_t), tag, 0);
548 }
549 else if (ipsec_sa_is_set_USE_ESN (sa))
550 {
551 u32 seq_hi = clib_net_to_host_u32 (sa->seq_hi);
552 clib_memcpy_fast (tag, &seq_hi, sizeof (seq_hi));
553 integ_total_len += sizeof (seq_hi);
554 }
555 }
556
Neale Rannsfc811342021-02-26 10:35:33 +0000557 /* this always succeeds because we know the frame is not full */
558 vnet_crypto_async_add_to_frame (vm, async_frame, key_index, crypto_total_len,
559 integ_total_len - crypto_total_len,
560 crypto_start_offset, integ_start_offset, bi,
561 async_next, iv, tag, aad, flag);
Fan Zhangf5395782020-04-29 14:00:03 +0100562}
563
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200564always_inline uword
Neale Ranns4a58e492020-12-21 13:19:10 +0000565esp_encrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
566 vlib_frame_t *frame, vnet_link_t lt, int is_tun,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000567 u16 async_next_node)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 ipsec_main_t *im = &ipsec_main;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100570 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
571 u32 *from = vlib_frame_vector_args (frame);
572 u32 n_left = frame->n_vectors;
573 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100574 u32 thread_index = vm->thread_index;
575 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
576 u32 current_sa_index = ~0, current_sa_packets = 0;
577 u32 current_sa_bytes = 0, spi = 0;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500578 u8 esp_align = 4, iv_sz = 0, icv_sz = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100579 ipsec_sa_t *sa0 = 0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000580 vlib_buffer_t *lb;
581 vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
582 vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
Neale Rannsfc811342021-02-26 10:35:33 +0000583 vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
Fan Zhangf5395782020-04-29 14:00:03 +0100584 int is_async = im->async_mode;
Neale Rannsfc811342021-02-26 10:35:33 +0000585 vnet_crypto_async_op_id_t async_op = ~0;
Neale Ranns4a58e492020-12-21 13:19:10 +0000586 u16 drop_next =
587 (lt == VNET_LINK_IP6 ? ESP_ENCRYPT_NEXT_DROP6 :
588 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_DROP4 :
589 ESP_ENCRYPT_NEXT_DROP_MPLS));
590 u16 handoff_next = (lt == VNET_LINK_IP6 ?
591 ESP_ENCRYPT_NEXT_HANDOFF6 :
592 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_HANDOFF4 :
593 ESP_ENCRYPT_NEXT_HANDOFF_MPLS));
Neale Rannsf16e9a52021-02-25 19:09:24 +0000594 vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
595 u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0;
596 u16 async_nexts[VLIB_FRAME_SIZE], *async_next = async_nexts, n_async = 0;
597 u16 noop_nexts[VLIB_FRAME_SIZE], *noop_next = noop_nexts, n_noop = 0;
598 u32 sync_bi[VLIB_FRAME_SIZE];
599 u32 noop_bi[VLIB_FRAME_SIZE];
600 esp_encrypt_error_t err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
Damjan Marionc59b9a22019-03-19 15:38:40 +0100602 vlib_get_buffers (vm, from, b, n_left);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000603
604 vec_reset_length (ptd->crypto_ops);
605 vec_reset_length (ptd->integ_ops);
606 vec_reset_length (ptd->chained_crypto_ops);
607 vec_reset_length (ptd->chained_integ_ops);
Neale Rannsfc811342021-02-26 10:35:33 +0000608 vec_reset_length (ptd->async_frames);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000609 vec_reset_length (ptd->chunks);
Neale Rannsfc811342021-02-26 10:35:33 +0000610 clib_memset (async_frames, 0, sizeof (async_frames));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100611
612 while (n_left > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700613 {
Zhiyong Yang1cff6432019-04-30 05:33:53 -0400614 u32 sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100615 dpo_id_t *dpo;
616 esp_header_t *esp;
617 u8 *payload, *next_hdr_ptr;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000618 u16 payload_len, payload_len_total, n_bufs;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400619 u32 hdr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620
Neale Rannsf16e9a52021-02-25 19:09:24 +0000621 err = ESP_ENCRYPT_ERROR_RX_PKTS;
622
Damjan Marionc59b9a22019-03-19 15:38:40 +0100623 if (n_left > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700624 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100625 u8 *p;
626 vlib_prefetch_buffer_header (b[2], LOAD);
627 p = vlib_buffer_get_current (b[1]);
628 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
629 p -= CLIB_CACHE_LINE_BYTES;
630 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000631 /* speculate that the trailer goes in the first buffer */
632 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]),
633 CLIB_CACHE_LINE_BYTES, LOAD);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700634 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635
Neale Ranns25edf142019-03-22 08:12:48 +0000636 if (is_tun)
637 {
638 /* we are on a ipsec tunnel's feature arc */
Neale Ranns28287212019-12-16 00:53:11 +0000639 vnet_buffer (b[0])->ipsec.sad_index =
640 sa_index0 = ipsec_tun_protect_get_sa_out
641 (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
Neale Ranns25edf142019-03-22 08:12:48 +0000642 }
643 else
644 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
645
646 if (sa_index0 != current_sa_index)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100647 {
Damjan Marion21f265f2019-06-05 15:45:50 +0200648 if (current_sa_packets)
649 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
650 current_sa_index,
651 current_sa_packets,
652 current_sa_bytes);
653 current_sa_packets = current_sa_bytes = 0;
654
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000655 sa0 = ipsec_sa_get (sa_index0);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000656
657 /* fetch the second cacheline ASAP */
658 CLIB_PREFETCH (sa0->cacheline1, CLIB_CACHE_LINE_BYTES, LOAD);
659
Damjan Marionc59b9a22019-03-19 15:38:40 +0100660 current_sa_index = sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100661 spi = clib_net_to_host_u32 (sa0->spi);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500662 esp_align = sa0->esp_block_align;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200663 icv_sz = sa0->integ_icv_size;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100664 iv_sz = sa0->crypto_iv_size;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000665 is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0);
Neale Rannsfc811342021-02-26 10:35:33 +0000666 }
Fan Zhangf5395782020-04-29 14:00:03 +0100667
Neale Rannsfc811342021-02-26 10:35:33 +0000668 if (is_async)
669 {
670 async_op = sa0->crypto_async_enc_op_id;
671
Neale Rannsfc811342021-02-26 10:35:33 +0000672 /* get a frame for this op if we don't yet have one or it's full
673 */
674 if (NULL == async_frames[async_op] ||
675 vnet_crypto_async_frame_is_full (async_frames[async_op]))
676 {
677 async_frames[async_op] =
678 vnet_crypto_async_get_frame (vm, async_op);
679 /* Save the frame to the list we'll submit at the end */
680 vec_add1 (ptd->async_frames, async_frames[async_op]);
Fan Zhangf5395782020-04-29 14:00:03 +0100681 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100682 }
683
Neale Ranns1a52d372021-02-04 11:33:32 +0000684 if (PREDICT_FALSE (~0 == sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +0000685 {
686 /* this is the first packet to use this SA, claim the SA
687 * for this thread. this could happen simultaneously on
688 * another thread */
Neale Ranns1a52d372021-02-04 11:33:32 +0000689 clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
Neale Rannsf62a8c02019-04-02 08:13:33 +0000690 ipsec_sa_assign_thread (thread_index));
691 }
692
Neale Ranns1a52d372021-02-04 11:33:32 +0000693 if (PREDICT_FALSE (thread_index != sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +0000694 {
Neale Rannsaa7d7662021-02-10 08:42:49 +0000695 vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000696 err = ESP_ENCRYPT_ERROR_HANDOFF;
697 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
698 handoff_next);
Neale Rannsf62a8c02019-04-02 08:13:33 +0000699 goto trace;
700 }
701
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000702 lb = b[0];
703 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
704 if (n_bufs == 0)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100705 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000706 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
707 esp_set_next_index (b[0], node, err, n_noop, noop_nexts, drop_next);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100708 goto trace;
709 }
710
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000711 if (n_bufs > 1)
712 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000713 /* find last buffer in the chain */
714 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
715 lb = vlib_get_buffer (vm, lb->next_buffer);
716 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000717
Damjan Marionc59b9a22019-03-19 15:38:40 +0100718 if (PREDICT_FALSE (esp_seq_advance (sa0)))
719 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000720 err = ESP_ENCRYPT_ERROR_SEQ_CYCLED;
721 esp_set_next_index (b[0], node, err, n_noop, noop_nexts, drop_next);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100722 goto trace;
723 }
724
725 /* space for IV */
726 hdr_len = iv_sz;
727
Damjan Mariond709cbc2019-03-26 13:16:42 +0100728 if (ipsec_sa_is_set_IS_TUNNEL (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100729 {
730 payload = vlib_buffer_get_current (b[0]);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000731 next_hdr_ptr = esp_add_footer_and_icv (
732 vm, &lb, esp_align, icv_sz, node, buffer_data_size,
733 vlib_buffer_length_in_chain (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000734 if (!next_hdr_ptr)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000735 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000736 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
737 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
738 drop_next);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000739 goto trace;
740 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000741 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000742 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000743 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100744
745 /* ESP header */
746 hdr_len += sizeof (*esp);
747 esp = (esp_header_t *) (payload - hdr_len);
748
749 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100750 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100751 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100752 hdr_len += sizeof (udp_header_t);
753 esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000754 payload_len_total + hdr_len);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100755 }
756
757 /* IP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100758 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100759 {
760 ip6_header_t *ip6;
761 u16 len = sizeof (ip6_header_t);
762 hdr_len += len;
763 ip6 = (ip6_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000764 clib_memcpy_fast (ip6, &sa0->ip6_hdr, sizeof (ip6_header_t));
765
Neale Ranns4a58e492020-12-21 13:19:10 +0000766 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000767 {
768 *next_hdr_ptr = IP_PROTOCOL_IPV6;
769 tunnel_encap_fixup_6o6 (sa0->tunnel_flags,
770 (const ip6_header_t *) payload,
771 ip6);
772 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000773 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000774 {
775 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
Neale Rannsa91cb452021-02-04 11:02:52 +0000776 tunnel_encap_fixup_4o6 (sa0->tunnel_flags, b[0],
777 (const ip4_header_t *) payload, ip6);
Neale Ranns041add72020-01-02 04:06:10 +0000778 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000779 else if (VNET_LINK_MPLS == lt)
780 {
781 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
782 tunnel_encap_fixup_mplso6 (
Neale Rannsa91cb452021-02-04 11:02:52 +0000783 sa0->tunnel_flags, b[0],
784 (const mpls_unicast_header_t *) payload, ip6);
Neale Ranns4a58e492020-12-21 13:19:10 +0000785 }
786 else
787 ASSERT (0);
788
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000789 len = payload_len_total + hdr_len - len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100790 ip6->payload_length = clib_net_to_host_u16 (len);
Neale Ranns45d6d832021-01-19 13:38:47 +0000791 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Damjan Marionc98275f2019-03-06 14:05:01 +0100792 }
793 else
794 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100795 ip4_header_t *ip4;
796 u16 len = sizeof (ip4_header_t);
797 hdr_len += len;
798 ip4 = (ip4_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000799 clib_memcpy_fast (ip4, &sa0->ip4_hdr, sizeof (ip4_header_t));
800
Neale Ranns4a58e492020-12-21 13:19:10 +0000801 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000802 {
803 *next_hdr_ptr = IP_PROTOCOL_IPV6;
804 tunnel_encap_fixup_6o4_w_chksum (sa0->tunnel_flags,
805 (const ip6_header_t *)
806 payload, ip4);
807 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000808 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000809 {
810 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
811 tunnel_encap_fixup_4o4_w_chksum (sa0->tunnel_flags,
812 (const ip4_header_t *)
813 payload, ip4);
814 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000815 else if (VNET_LINK_MPLS == lt)
816 {
817 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
818 tunnel_encap_fixup_mplso4_w_chksum (
819 sa0->tunnel_flags, (const mpls_unicast_header_t *) payload,
820 ip4);
821 }
822 else
823 ASSERT (0);
824
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000825 len = payload_len_total + hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100826 esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100827 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100828
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000829 dpo = &sa0->dpo;
Neale Ranns25edf142019-03-22 08:12:48 +0000830 if (!is_tun)
831 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000832 sync_next[0] = dpo->dpoi_next_node;
Neale Ranns25edf142019-03-22 08:12:48 +0000833 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
834 }
Neale Ranns4ec36c52020-03-31 09:21:29 -0400835 else
Neale Rannsf16e9a52021-02-25 19:09:24 +0000836 sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000837 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Damjan Marionc98275f2019-03-06 14:05:01 +0100838 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100839 else /* transport mode */
Damjan Marionc98275f2019-03-06 14:05:01 +0100840 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100841 u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
Neale Ranns02950402019-12-20 00:54:57 +0000842 ip6_ext_header_t *ext_hdr;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100843 udp_header_t *udp = 0;
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400844 u16 udp_len = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100845 u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
Damjan Marionc98275f2019-03-06 14:05:01 +0100846
Neale Ranns4a58e492020-12-21 13:19:10 +0000847 ip_len =
848 (VNET_LINK_IP6 == lt ?
849 esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
850 ip4_header_bytes ((ip4_header_t *) old_ip_hdr));
Damjan Marionc98275f2019-03-06 14:05:01 +0100851
Damjan Marionc59b9a22019-03-19 15:38:40 +0100852 vlib_buffer_advance (b[0], ip_len);
853 payload = vlib_buffer_get_current (b[0]);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000854 next_hdr_ptr = esp_add_footer_and_icv (
855 vm, &lb, esp_align, icv_sz, node, buffer_data_size,
856 vlib_buffer_length_in_chain (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000857 if (!next_hdr_ptr)
Fan Zhang18f0e312020-10-19 13:08:34 +0100858 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000859 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
860 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
861 drop_next);
Fan Zhang18f0e312020-10-19 13:08:34 +0100862 goto trace;
863 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000864
865 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000866 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000867 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100868
869 /* ESP header */
870 hdr_len += sizeof (*esp);
871 esp = (esp_header_t *) (payload - hdr_len);
872
873 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100874 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100875 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100876 hdr_len += sizeof (udp_header_t);
877 udp = (udp_header_t *) (payload - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100878 }
879
Damjan Marionc59b9a22019-03-19 15:38:40 +0100880 /* IP header */
881 hdr_len += ip_len;
882 ip_hdr = payload - hdr_len;
883
884 /* L2 header */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800885 if (!is_tun)
886 {
887 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
888 hdr_len += l2_len;
889 l2_hdr = payload - hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100890
Neale Rannsc87b66c2019-02-07 07:26:12 -0800891 /* copy l2 and ip header */
892 clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
893 }
894 else
895 l2_len = 0;
896
Neale Ranns4a58e492020-12-21 13:19:10 +0000897 if (VNET_LINK_IP6 == lt)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100898 {
Neale Ranns02950402019-12-20 00:54:57 +0000899 ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
900 if (PREDICT_TRUE (NULL == ext_hdr))
901 {
902 *next_hdr_ptr = ip6->protocol;
903 ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
904 }
905 else
906 {
907 *next_hdr_ptr = ext_hdr->next_hdr;
908 ext_hdr->next_hdr = IP_PROTOCOL_IPSEC_ESP;
909 }
Neale Rannsd207fd72019-04-18 17:18:12 -0700910 ip6->payload_length =
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000911 clib_host_to_net_u16 (payload_len_total + hdr_len - l2_len -
Neale Ranns02950402019-12-20 00:54:57 +0000912 sizeof (ip6_header_t));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100913 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000914 else if (VNET_LINK_IP4 == lt)
Damjan Marionc98275f2019-03-06 14:05:01 +0100915 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100916 u16 len;
Neale Ranns02950402019-12-20 00:54:57 +0000917 ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100918 *next_hdr_ptr = ip4->protocol;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000919 len = payload_len_total + hdr_len - l2_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100920 if (udp)
921 {
922 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400923 udp_len = len - ip_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100924 }
925 else
926 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100927 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100928
Neale Ranns02950402019-12-20 00:54:57 +0000929 clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
930
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400931 if (udp)
932 {
933 esp_fill_udp_hdr (sa0, udp, udp_len);
934 }
935
Neale Rannsf16e9a52021-02-25 19:09:24 +0000936 sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Damjan Marionc98275f2019-03-06 14:05:01 +0100937 }
938
PiotrX Kleskifdca4dd2020-05-05 14:14:22 +0200939 if (lb != b[0])
940 {
941 crypto_ops = &ptd->chained_crypto_ops;
942 integ_ops = &ptd->chained_integ_ops;
943 }
944 else
945 {
946 crypto_ops = &ptd->crypto_ops;
947 integ_ops = &ptd->integ_ops;
948 }
949
Damjan Marionc59b9a22019-03-19 15:38:40 +0100950 esp->spi = spi;
951 esp->seq = clib_net_to_host_u32 (sa0->seq);
Damjan Marionc98275f2019-03-06 14:05:01 +0100952
Fan Zhangf5395782020-04-29 14:00:03 +0100953 if (is_async)
Neale Rannsfc811342021-02-26 10:35:33 +0000954 esp_prepare_async_frame (vm, ptd, async_frames[async_op], sa0, b[0],
955 esp, payload, payload_len, iv_sz, icv_sz,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000956 from[b - bufs], sync_next[0], hdr_len,
957 async_next_node, lb);
Fan Zhangf5395782020-04-29 14:00:03 +0100958 else
Neale Rannsf16e9a52021-02-25 19:09:24 +0000959 esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, payload,
960 payload_len, iv_sz, icv_sz, n_sync, b, lb,
961 hdr_len, esp);
Damjan Marionc98275f2019-03-06 14:05:01 +0100962
Damjan Marionc59b9a22019-03-19 15:38:40 +0100963 vlib_buffer_advance (b[0], 0LL - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100964
Damjan Marionc59b9a22019-03-19 15:38:40 +0100965 current_sa_packets += 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000966 current_sa_bytes += payload_len_total;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100967
968 trace:
969 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
Damjan Marionc98275f2019-03-06 14:05:01 +0100970 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100971 esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
972 sizeof (*tr));
973 tr->sa_index = sa_index0;
974 tr->spi = sa0->spi;
Neale Ranns6afaae12019-07-17 15:07:14 +0000975 tr->seq = sa0->seq;
976 tr->sa_seq_hi = sa0->seq_hi;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100977 tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100978 tr->crypto_alg = sa0->crypto_alg;
979 tr->integ_alg = sa0->integ_alg;
Damjan Marionc98275f2019-03-06 14:05:01 +0100980 }
Neale Rannsf16e9a52021-02-25 19:09:24 +0000981
Damjan Marionc98275f2019-03-06 14:05:01 +0100982 /* next */
Neale Rannsf16e9a52021-02-25 19:09:24 +0000983 if (ESP_ENCRYPT_ERROR_RX_PKTS != err)
984 {
985 noop_bi[n_noop] = from[b - bufs];
986 n_noop++;
987 noop_next++;
988 }
989 else if (!is_async)
990 {
991 sync_bi[n_sync] = from[b - bufs];
992 sync_bufs[n_sync] = b[0];
993 n_sync++;
994 sync_next++;
995 }
996 else
997 {
998 n_async++;
999 async_next++;
1000 }
Damjan Marionc59b9a22019-03-19 15:38:40 +01001001 n_left -= 1;
Damjan Marionc59b9a22019-03-19 15:38:40 +01001002 b += 1;
Damjan Marionc98275f2019-03-06 14:05:01 +01001003 }
1004
Damjan Marionc59b9a22019-03-19 15:38:40 +01001005 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1006 current_sa_index, current_sa_packets,
1007 current_sa_bytes);
Neale Rannsf16e9a52021-02-25 19:09:24 +00001008 if (n_sync)
Fan Zhangf5395782020-04-29 14:00:03 +01001009 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001010 esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts,
1011 drop_next);
1012 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs,
1013 sync_nexts, ptd->chunks, drop_next);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001014
Neale Rannsf16e9a52021-02-25 19:09:24 +00001015 esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts,
1016 drop_next);
1017 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs,
1018 sync_nexts, ptd->chunks, drop_next);
Neale Rannsfc811342021-02-26 10:35:33 +00001019
Neale Rannsf16e9a52021-02-25 19:09:24 +00001020 vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync);
Fan Zhangf5395782020-04-29 14:00:03 +01001021 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001022 if (n_async)
Fan Zhangf5395782020-04-29 14:00:03 +01001023 {
Neale Rannsfc811342021-02-26 10:35:33 +00001024 /* submit all of the open frames */
1025 vnet_crypto_async_frame_t **async_frame;
1026
1027 vec_foreach (async_frame, ptd->async_frames)
Fan Zhang18f0e312020-10-19 13:08:34 +01001028 {
Neale Rannsfc811342021-02-26 10:35:33 +00001029 if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0)
1030 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001031 n_noop += esp_async_recycle_failed_submit (
1032 vm, *async_frame, node, ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR,
1033 n_sync, noop_bi, noop_nexts, drop_next);
Neale Rannsfc811342021-02-26 10:35:33 +00001034 vnet_crypto_async_reset_frame (*async_frame);
1035 vnet_crypto_async_free_frame (vm, *async_frame);
1036 }
Fan Zhang18f0e312020-10-19 13:08:34 +01001037 }
Fan Zhangf5395782020-04-29 14:00:03 +01001038 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001039 if (n_noop)
1040 vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
1041
1042 vlib_node_increment_counter (vm, node->node_index, ESP_ENCRYPT_ERROR_RX_PKTS,
1043 frame->n_vectors);
Damjan Marionc59b9a22019-03-19 15:38:40 +01001044
Damjan Marionc59b9a22019-03-19 15:38:40 +01001045 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046}
1047
Fan Zhangf5395782020-04-29 14:00:03 +01001048always_inline uword
1049esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1050 vlib_frame_t * frame)
1051{
1052 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1053 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1054 u32 *from = vlib_frame_vector_args (frame);
1055 u32 n_left = frame->n_vectors;
1056
1057 vlib_get_buffers (vm, from, b, n_left);
1058
1059 if (n_left >= 4)
1060 {
1061 vlib_prefetch_buffer_header (b[0], LOAD);
1062 vlib_prefetch_buffer_header (b[1], LOAD);
1063 vlib_prefetch_buffer_header (b[2], LOAD);
1064 vlib_prefetch_buffer_header (b[3], LOAD);
1065 }
1066
1067 while (n_left > 8)
1068 {
1069 vlib_prefetch_buffer_header (b[4], LOAD);
1070 vlib_prefetch_buffer_header (b[5], LOAD);
1071 vlib_prefetch_buffer_header (b[6], LOAD);
1072 vlib_prefetch_buffer_header (b[7], LOAD);
1073
1074 next[0] = (esp_post_data (b[0]))->next_index;
1075 next[1] = (esp_post_data (b[1]))->next_index;
1076 next[2] = (esp_post_data (b[2]))->next_index;
1077 next[3] = (esp_post_data (b[3]))->next_index;
1078
1079 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
1080 {
1081 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
1082 {
1083 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1084 sizeof (*tr));
1085 tr->next_index = next[0];
1086 }
1087 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
1088 {
1089 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1],
1090 sizeof (*tr));
1091 tr->next_index = next[1];
1092 }
1093 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
1094 {
1095 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2],
1096 sizeof (*tr));
1097 tr->next_index = next[2];
1098 }
1099 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
1100 {
1101 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3],
1102 sizeof (*tr));
1103 tr->next_index = next[3];
1104 }
1105 }
1106
1107 b += 4;
1108 next += 4;
1109 n_left -= 4;
1110 }
1111
1112 while (n_left > 0)
1113 {
1114 next[0] = (esp_post_data (b[0]))->next_index;
1115 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1116 {
1117 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1118 sizeof (*tr));
1119 tr->next_index = next[0];
1120 }
1121
1122 b += 1;
1123 next += 1;
1124 n_left -= 1;
1125 }
1126
1127 vlib_node_increment_counter (vm, node->node_index,
1128 ESP_ENCRYPT_ERROR_POST_RX_PKTS,
1129 frame->n_vectors);
1130 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1131 return frame->n_vectors;
1132}
1133
Klement Sekerab8f35442018-10-29 13:38:19 +01001134VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
1135 vlib_node_runtime_t * node,
1136 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001137{
Neale Ranns4a58e492020-12-21 13:19:10 +00001138 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001139 esp_encrypt_async_next.esp4_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001140}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001142/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001143VLIB_REGISTER_NODE (esp4_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001144 .name = "esp4-encrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001145 .vector_size = sizeof (u32),
1146 .format_trace = format_esp_encrypt_trace,
1147 .type = VLIB_NODE_TYPE_INTERNAL,
1148
Neale Ranns4a58e492020-12-21 13:19:10 +00001149 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150 .error_strings = esp_encrypt_error_strings,
1151
1152 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Ranns4a58e492020-12-21 13:19:10 +00001153 .next_nodes = { [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1154 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1155 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1156 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-handoff",
1157 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-handoff",
1158 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "error-drop",
1159 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output" },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001161/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001162
Fan Zhangf5395782020-04-29 14:00:03 +01001163VLIB_NODE_FN (esp4_encrypt_post_node) (vlib_main_t * vm,
1164 vlib_node_runtime_t * node,
1165 vlib_frame_t * from_frame)
1166{
1167 return esp_encrypt_post_inline (vm, node, from_frame);
1168}
1169
1170/* *INDENT-OFF* */
1171VLIB_REGISTER_NODE (esp4_encrypt_post_node) = {
1172 .name = "esp4-encrypt-post",
1173 .vector_size = sizeof (u32),
1174 .format_trace = format_esp_post_encrypt_trace,
1175 .type = VLIB_NODE_TYPE_INTERNAL,
1176 .sibling_of = "esp4-encrypt",
1177
1178 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1179 .error_strings = esp_encrypt_error_strings,
1180};
1181/* *INDENT-ON* */
1182
Klement Sekerab8f35442018-10-29 13:38:19 +01001183VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
1184 vlib_node_runtime_t * node,
1185 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001186{
Neale Ranns4a58e492020-12-21 13:19:10 +00001187 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001188 esp_encrypt_async_next.esp6_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001189}
1190
1191/* *INDENT-OFF* */
1192VLIB_REGISTER_NODE (esp6_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001193 .name = "esp6-encrypt",
1194 .vector_size = sizeof (u32),
1195 .format_trace = format_esp_encrypt_trace,
1196 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001197 .sibling_of = "esp4-encrypt",
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001198
1199 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1200 .error_strings = esp_encrypt_error_strings,
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001201};
1202/* *INDENT-ON* */
1203
Fan Zhangf5395782020-04-29 14:00:03 +01001204VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm,
1205 vlib_node_runtime_t * node,
1206 vlib_frame_t * from_frame)
1207{
1208 return esp_encrypt_post_inline (vm, node, from_frame);
1209}
1210
1211/* *INDENT-OFF* */
1212VLIB_REGISTER_NODE (esp6_encrypt_post_node) = {
1213 .name = "esp6-encrypt-post",
1214 .vector_size = sizeof (u32),
1215 .format_trace = format_esp_post_encrypt_trace,
1216 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001217 .sibling_of = "esp4-encrypt",
Fan Zhangf5395782020-04-29 14:00:03 +01001218
1219 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1220 .error_strings = esp_encrypt_error_strings,
1221};
1222/* *INDENT-ON* */
1223
Neale Ranns25edf142019-03-22 08:12:48 +00001224VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
1225 vlib_node_runtime_t * node,
1226 vlib_frame_t * from_frame)
1227{
Neale Ranns4a58e492020-12-21 13:19:10 +00001228 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001229 esp_encrypt_async_next.esp4_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001230}
1231
1232/* *INDENT-OFF* */
1233VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
1234 .name = "esp4-encrypt-tun",
1235 .vector_size = sizeof (u32),
1236 .format_trace = format_esp_encrypt_trace,
1237 .type = VLIB_NODE_TYPE_INTERNAL,
1238
1239 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1240 .error_strings = esp_encrypt_error_strings,
Neale Rannsd7603d92019-03-28 08:56:10 +00001241
Neale Rannsf62a8c02019-04-02 08:13:33 +00001242 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001243 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001244 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1245 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001246 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001247 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001248 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1249 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001250 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001251 },
Neale Ranns25edf142019-03-22 08:12:48 +00001252};
1253
Fan Zhangf5395782020-04-29 14:00:03 +01001254VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm,
1255 vlib_node_runtime_t * node,
1256 vlib_frame_t * from_frame)
1257{
1258 return esp_encrypt_post_inline (vm, node, from_frame);
1259}
1260
1261/* *INDENT-OFF* */
1262VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = {
1263 .name = "esp4-encrypt-tun-post",
1264 .vector_size = sizeof (u32),
1265 .format_trace = format_esp_post_encrypt_trace,
1266 .type = VLIB_NODE_TYPE_INTERNAL,
1267 .sibling_of = "esp4-encrypt-tun",
1268
1269 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1270 .error_strings = esp_encrypt_error_strings,
1271};
Neale Ranns25edf142019-03-22 08:12:48 +00001272/* *INDENT-ON* */
1273
1274VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
1275 vlib_node_runtime_t * node,
1276 vlib_frame_t * from_frame)
1277{
Neale Ranns4a58e492020-12-21 13:19:10 +00001278 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001279 esp_encrypt_async_next.esp6_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001280}
1281
1282/* *INDENT-OFF* */
1283VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
1284 .name = "esp6-encrypt-tun",
1285 .vector_size = sizeof (u32),
1286 .format_trace = format_esp_encrypt_trace,
1287 .type = VLIB_NODE_TYPE_INTERNAL,
1288
1289 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1290 .error_strings = esp_encrypt_error_strings,
Neale Rannsd7603d92019-03-28 08:56:10 +00001291
Neale Rannsf62a8c02019-04-02 08:13:33 +00001292 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001293 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001294 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1295 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001296 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1297 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001298 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001299 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001300 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001301 },
Neale Ranns25edf142019-03-22 08:12:48 +00001302};
1303
Neale Ranns25edf142019-03-22 08:12:48 +00001304/* *INDENT-ON* */
1305
Fan Zhangf5395782020-04-29 14:00:03 +01001306VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm,
1307 vlib_node_runtime_t * node,
1308 vlib_frame_t * from_frame)
1309{
1310 return esp_encrypt_post_inline (vm, node, from_frame);
1311}
1312
1313/* *INDENT-OFF* */
1314VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = {
1315 .name = "esp6-encrypt-tun-post",
1316 .vector_size = sizeof (u32),
1317 .format_trace = format_esp_post_encrypt_trace,
1318 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Ranns4a58e492020-12-21 13:19:10 +00001319 .sibling_of = "esp-mpls-encrypt-tun",
Fan Zhangf5395782020-04-29 14:00:03 +01001320
Neale Ranns4a58e492020-12-21 13:19:10 +00001321 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
Fan Zhangf5395782020-04-29 14:00:03 +01001322 .error_strings = esp_encrypt_error_strings,
1323};
1324/* *INDENT-ON* */
1325
Neale Ranns4a58e492020-12-21 13:19:10 +00001326VLIB_NODE_FN (esp_mpls_encrypt_tun_node)
1327(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1328{
1329 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_MPLS, 1,
1330 esp_encrypt_async_next.esp_mpls_tun_post_next);
1331}
1332
1333VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_node) = {
1334 .name = "esp-mpls-encrypt-tun",
1335 .vector_size = sizeof (u32),
1336 .format_trace = format_esp_encrypt_trace,
1337 .type = VLIB_NODE_TYPE_INTERNAL,
1338
1339 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1340 .error_strings = esp_encrypt_error_strings,
1341
1342 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1343 .next_nodes = {
1344 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1345 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1346 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1347 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1348 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1349 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1350 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1351 },
1352};
1353
1354VLIB_NODE_FN (esp_mpls_encrypt_tun_post_node)
1355(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1356{
1357 return esp_encrypt_post_inline (vm, node, from_frame);
1358}
1359
1360VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_post_node) = {
1361 .name = "esp-mpls-encrypt-tun-post",
1362 .vector_size = sizeof (u32),
1363 .format_trace = format_esp_post_encrypt_trace,
1364 .type = VLIB_NODE_TYPE_INTERNAL,
1365 .sibling_of = "esp-mpls-encrypt-tun",
1366
1367 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1368 .error_strings = esp_encrypt_error_strings,
1369};
1370
Matthew Smith401aedf2019-07-08 14:45:04 -05001371typedef struct
1372{
1373 u32 sa_index;
1374} esp_no_crypto_trace_t;
1375
1376static u8 *
1377format_esp_no_crypto_trace (u8 * s, va_list * args)
1378{
1379 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1380 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1381 esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *);
1382
1383 s = format (s, "esp-no-crypto: sa-index %u", t->sa_index);
1384
1385 return s;
1386}
1387
1388enum
1389{
1390 ESP_NO_CRYPTO_NEXT_DROP,
1391 ESP_NO_CRYPTO_N_NEXT,
1392};
1393
1394enum
1395{
1396 ESP_NO_CRYPTO_ERROR_RX_PKTS,
1397};
1398
1399static char *esp_no_crypto_error_strings[] = {
1400 "Outbound ESP packets received",
1401};
1402
1403always_inline uword
1404esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1405 vlib_frame_t * frame)
1406{
1407 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Matthew Smith401aedf2019-07-08 14:45:04 -05001408 u32 *from = vlib_frame_vector_args (frame);
1409 u32 n_left = frame->n_vectors;
1410
1411 vlib_get_buffers (vm, from, b, n_left);
1412
1413 while (n_left > 0)
1414 {
Matthew Smith401aedf2019-07-08 14:45:04 -05001415 u32 sa_index0;
1416
1417 /* packets are always going to be dropped, but get the sa_index */
Neale Ranns4ec36c52020-03-31 09:21:29 -04001418 sa_index0 = ipsec_tun_protect_get_sa_out
1419 (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
Matthew Smith401aedf2019-07-08 14:45:04 -05001420
1421 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1422 {
1423 esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0],
1424 sizeof (*tr));
1425 tr->sa_index = sa_index0;
1426 }
1427
1428 n_left -= 1;
Matthew Smith401aedf2019-07-08 14:45:04 -05001429 b += 1;
1430 }
1431
1432 vlib_node_increment_counter (vm, node->node_index,
1433 ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors);
1434
Neale Ranns4ec36c52020-03-31 09:21:29 -04001435 vlib_buffer_enqueue_to_single_next (vm, node, from,
1436 ESP_NO_CRYPTO_NEXT_DROP,
1437 frame->n_vectors);
Matthew Smith401aedf2019-07-08 14:45:04 -05001438
1439 return frame->n_vectors;
1440}
1441
1442VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm,
1443 vlib_node_runtime_t * node,
1444 vlib_frame_t * from_frame)
1445{
1446 return esp_no_crypto_inline (vm, node, from_frame);
1447}
1448
1449/* *INDENT-OFF* */
1450VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) =
1451{
1452 .name = "esp4-no-crypto",
1453 .vector_size = sizeof (u32),
1454 .format_trace = format_esp_no_crypto_trace,
1455 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
1456 .error_strings = esp_no_crypto_error_strings,
1457 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
1458 .next_nodes = {
1459 [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop",
1460 },
1461};
1462
Matthew Smith401aedf2019-07-08 14:45:04 -05001463VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm,
1464 vlib_node_runtime_t * node,
1465 vlib_frame_t * from_frame)
1466{
1467 return esp_no_crypto_inline (vm, node, from_frame);
1468}
1469
1470/* *INDENT-OFF* */
1471VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) =
1472{
1473 .name = "esp6-no-crypto",
1474 .vector_size = sizeof (u32),
1475 .format_trace = format_esp_no_crypto_trace,
1476 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
1477 .error_strings = esp_no_crypto_error_strings,
1478 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
1479 .next_nodes = {
1480 [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop",
1481 },
1482};
Matthew Smith401aedf2019-07-08 14:45:04 -05001483/* *INDENT-ON* */
1484
Neale Ranns2d498302021-02-25 08:38:58 +00001485#ifndef CLIB_MARCH_VARIANT
1486
1487static clib_error_t *
1488esp_encrypt_init (vlib_main_t *vm)
1489{
1490 ipsec_main_t *im = &ipsec_main;
1491
1492 im->esp4_enc_fq_index =
1493 vlib_frame_queue_main_init (esp4_encrypt_node.index, 0);
1494 im->esp6_enc_fq_index =
1495 vlib_frame_queue_main_init (esp6_encrypt_node.index, 0);
1496 im->esp4_enc_tun_fq_index =
1497 vlib_frame_queue_main_init (esp4_encrypt_tun_node.index, 0);
1498 im->esp6_enc_tun_fq_index =
1499 vlib_frame_queue_main_init (esp6_encrypt_tun_node.index, 0);
1500 im->esp_mpls_enc_tun_fq_index =
1501 vlib_frame_queue_main_init (esp_mpls_encrypt_tun_node.index, 0);
1502
1503 return 0;
1504}
1505
1506VLIB_INIT_FUNCTION (esp_encrypt_init);
1507
1508#endif
1509
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001510/*
1511 * fd.io coding-style-patch-verification: ON
1512 *
1513 * Local Variables:
1514 * eval: (c-set-style "gnu")
1515 * End:
1516 */