blob: 0d29604e61ff497c8223a439d89443b34c17161e [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)") \
Neale Ranns6fdcc3d2021-10-08 07:30:47 +000053 _ (NO_BUFFERS, "no buffers (packet dropped)") \
54 _ (NO_PROTECTION, "no protecting SA (packet dropped)") \
55 _ (NO_ENCRYPTION, "no Encrypting SA (packet dropped)")
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070057typedef enum
58{
Ed Warnickecb9cada2015-12-08 15:45:58 -070059#define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
60 foreach_esp_encrypt_error
61#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070062 ESP_ENCRYPT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070063} esp_encrypt_error_t;
64
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070065static char *esp_encrypt_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070066#define _(sym,string) string,
67 foreach_esp_encrypt_error
68#undef _
69};
70
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070071typedef struct
72{
Neale Ranns8d7c5022019-02-06 01:41:05 -080073 u32 sa_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 u32 spi;
75 u32 seq;
Neale Ranns6afaae12019-07-17 15:07:14 +000076 u32 sa_seq_hi;
Klement Sekera4b089f22018-04-17 18:04:57 +020077 u8 udp_encap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 ipsec_crypto_alg_t crypto_alg;
79 ipsec_integ_alg_t integ_alg;
80} esp_encrypt_trace_t;
81
Fan Zhangf5395782020-04-29 14:00:03 +010082typedef struct
83{
84 u32 next_index;
85} esp_encrypt_post_trace_t;
86
Ed Warnickecb9cada2015-12-08 15:45:58 -070087/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070088static u8 *
89format_esp_encrypt_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070090{
91 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
92 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070093 esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020095 s =
96 format (s,
Neale Ranns6afaae12019-07-17 15:07:14 +000097 "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
98 t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
99 format_ipsec_crypto_alg,
Guillaume Solignac8f818cc2019-05-15 12:02:33 +0200100 t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
101 t->udp_encap ? " udp-encap-enabled" : "");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 return s;
103}
104
Fan Zhangf5395782020-04-29 14:00:03 +0100105static u8 *
106format_esp_post_encrypt_trace (u8 * s, va_list * args)
107{
108 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
109 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
110 esp_encrypt_post_trace_t *t = va_arg (*args, esp_encrypt_post_trace_t *);
111
112 s = format (s, "esp-post: next node index %u", t->next_index);
113 return s;
114}
115
Damjan Marionc59b9a22019-03-19 15:38:40 +0100116/* pad packet in input buffer */
117static_always_inline u8 *
Neale Rannsf16e9a52021-02-25 19:09:24 +0000118esp_add_footer_and_icv (vlib_main_t *vm, vlib_buffer_t **last, u8 esp_align,
119 u8 icv_sz, vlib_node_runtime_t *node,
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000120 u16 buffer_data_size, uword total_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121{
Damjan Marionc59b9a22019-03-19 15:38:40 +0100122 static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = {
123 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
Milan Lenco7885c742020-08-20 13:23:09 +0200124 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
Damjan Marionc59b9a22019-03-19 15:38:40 +0100125 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000127 u16 min_length = total_len + sizeof (esp_footer_t);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500128 u16 new_length = round_pow2 (min_length, esp_align);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100129 u8 pad_bytes = new_length - min_length;
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000130 esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (last[0]) +
131 last[0]->current_length + pad_bytes);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000132 u16 tail_sz = sizeof (esp_footer_t) + pad_bytes + icv_sz;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133
Benoît Ganne217ba5a2021-06-14 17:23:56 +0200134 if (last[0]->current_data + last[0]->current_length + tail_sz >
135 buffer_data_size)
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000136 {
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000137 u32 tmp_bi = 0;
138 if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1)
139 return 0;
Filip Tehlarc2c1bfd2020-02-13 07:49:30 +0000140
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000141 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
142 last[0]->next_buffer = tmp_bi;
143 last[0]->flags |= VLIB_BUFFER_NEXT_PRESENT;
144 f = (esp_footer_t *) (vlib_buffer_get_current (tmp) + pad_bytes);
145 tmp->current_length += tail_sz;
146 last[0] = tmp;
147 }
148 else
149 last[0]->current_length += tail_sz;
150
151 f->pad_length = pad_bytes;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100152 if (pad_bytes)
Benoît Ganne4505f012019-12-07 09:14:27 -0700153 {
154 ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE);
155 pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes);
156 clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes);
157 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100158
Damjan Marionc59b9a22019-03-19 15:38:40 +0100159 return &f->next_header;
160}
161
162static_always_inline void
163esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp)
164{
Neale Ranns1b582b82019-04-18 19:49:13 -0700165 ip_csum_t sum;
166 u16 old_len;
167
168 len = clib_net_to_host_u16 (len);
169 old_len = ip4->length;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100170
171 if (is_transport)
172 {
173 u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100174
Neale Ranns1b582b82019-04-18 19:49:13 -0700175 sum = ip_csum_update (ip4->checksum, ip4->protocol,
176 prot, ip4_header_t, protocol);
177 ip4->protocol = prot;
178
179 sum = ip_csum_update (sum, old_len, len, ip4_header_t, length);
180 }
181 else
182 sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
183
184 ip4->length = len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100185 ip4->checksum = ip_csum_fold (sum);
186}
187
188static_always_inline void
189esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
190{
191 clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
192 udp->length = clib_net_to_host_u16 (len);
193}
194
195static_always_inline u8
196ext_hdr_is_pre_esp (u8 nexthdr)
197{
198#ifdef CLIB_HAVE_VEC128
199 static const u8x16 ext_hdr_types = {
200 IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
201 IP_PROTOCOL_IPV6_ROUTE,
202 IP_PROTOCOL_IPV6_FRAGMENTATION,
203 };
204
205 return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
206#else
207 return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
208 (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) |
Damjan Mariond0b26602021-10-31 20:02:19 +0100209 ((nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100210#endif
211}
212
213static_always_inline u8
Neale Ranns02950402019-12-20 00:54:57 +0000214esp_get_ip6_hdr_len (ip6_header_t * ip6, ip6_ext_header_t ** ext_hdr)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100215{
216 /* this code assumes that HbH, route and frag headers will be before
217 others, if that is not the case, they will end up encrypted */
Damjan Marionc59b9a22019-03-19 15:38:40 +0100218 u8 len = sizeof (ip6_header_t);
219 ip6_ext_header_t *p;
220
221 /* if next packet doesn't have ext header */
222 if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
Neale Ranns02950402019-12-20 00:54:57 +0000223 {
224 *ext_hdr = NULL;
225 return len;
226 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100227
228 p = (void *) (ip6 + 1);
229 len += ip6_ext_header_len (p);
230
231 while (ext_hdr_is_pre_esp (p->next_hdr))
232 {
233 len += ip6_ext_header_len (p);
234 p = ip6_ext_next_header (p);
235 }
236
Neale Ranns02950402019-12-20 00:54:57 +0000237 *ext_hdr = p;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100238 return len;
239}
240
Damjan Marionc59b9a22019-03-19 15:38:40 +0100241static_always_inline void
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000242esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
243 vnet_crypto_op_t * ops, vlib_buffer_t * b[],
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000244 u16 * nexts, vnet_crypto_op_chunk_t * chunks,
245 u16 drop_next)
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000246{
247 u32 n_fail, n_ops = vec_len (ops);
248 vnet_crypto_op_t *op = ops;
249
250 if (n_ops == 0)
251 return;
252
253 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
254
255 while (n_fail)
256 {
257 ASSERT (op - ops < n_ops);
258
259 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
260 {
261 u32 bi = op->user_data;
262 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000263 nexts[bi] = drop_next;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000264 n_fail--;
265 }
266 op++;
267 }
268}
269
270static_always_inline void
Damjan Marionc59b9a22019-03-19 15:38:40 +0100271esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000272 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts,
273 u16 drop_next)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100274{
275 u32 n_fail, n_ops = vec_len (ops);
276 vnet_crypto_op_t *op = ops;
277
278 if (n_ops == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279 return;
280
Damjan Marionc59b9a22019-03-19 15:38:40 +0100281 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
Damjan Marionc59b9a22019-03-19 15:38:40 +0100283 while (n_fail)
284 {
285 ASSERT (op - ops < n_ops);
286
287 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
288 {
289 u32 bi = op->user_data;
290 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
Neale Rannsb1fd80f2020-05-12 13:33:56 +0000291 nexts[bi] = drop_next;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100292 n_fail--;
293 }
294 op++;
295 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296}
297
Fan Zhangf5395782020-04-29 14:00:03 +0100298static_always_inline u32
299esp_encrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
300 ipsec_sa_t * sa0, vlib_buffer_t * b,
301 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
302 u32 start_len, u16 * n_ch)
303{
304 vnet_crypto_op_chunk_t *ch;
305 vlib_buffer_t *cb = b;
306 u32 n_chunks = 1;
307 u32 total_len;
308 vec_add2 (ptd->chunks, ch, 1);
309 total_len = ch->len = start_len;
310 ch->src = ch->dst = start;
311 cb = vlib_get_buffer (vm, cb->next_buffer);
312
313 while (1)
314 {
315 vec_add2 (ptd->chunks, ch, 1);
316 n_chunks += 1;
317 if (lb == cb)
318 total_len += ch->len = cb->current_length - icv_sz;
319 else
320 total_len += ch->len = cb->current_length;
321 ch->src = ch->dst = vlib_buffer_get_current (cb);
322
323 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
324 break;
325
326 cb = vlib_get_buffer (vm, cb->next_buffer);
327 }
328
329 if (n_ch)
330 *n_ch = n_chunks;
331
332 return total_len;
333}
334
335static_always_inline u32
336esp_encrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
337 ipsec_sa_t * sa0, vlib_buffer_t * b,
338 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
339 u32 start_len, u8 * digest, u16 * n_ch)
340{
341 vnet_crypto_op_chunk_t *ch;
342 vlib_buffer_t *cb = b;
343 u32 n_chunks = 1;
344 u32 total_len;
345 vec_add2 (ptd->chunks, ch, 1);
346 total_len = ch->len = start_len;
347 ch->src = start;
348 cb = vlib_get_buffer (vm, cb->next_buffer);
349
350 while (1)
351 {
352 vec_add2 (ptd->chunks, ch, 1);
353 n_chunks += 1;
354 if (lb == cb)
355 {
356 total_len += ch->len = cb->current_length - icv_sz;
357 if (ipsec_sa_is_set_USE_ESN (sa0))
358 {
359 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
360 clib_memcpy_fast (digest, &seq_hi, sizeof (seq_hi));
361 ch->len += sizeof (seq_hi);
362 total_len += sizeof (seq_hi);
363 }
364 }
365 else
366 total_len += ch->len = cb->current_length;
367 ch->src = vlib_buffer_get_current (cb);
368
369 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
370 break;
371
372 cb = vlib_get_buffer (vm, cb->next_buffer);
373 }
374
375 if (n_ch)
376 *n_ch = n_chunks;
377
378 return total_len;
379}
380
381always_inline void
Benoît Ganne490b9272021-01-22 18:03:09 +0100382esp_prepare_sync_op (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
383 vnet_crypto_op_t **crypto_ops,
Neale Ranns5b891102021-06-28 13:31:28 +0000384 vnet_crypto_op_t **integ_ops, ipsec_sa_t *sa0, u32 seq_hi,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000385 u8 *payload, u16 payload_len, u8 iv_sz, u8 icv_sz, u32 bi,
386 vlib_buffer_t **b, vlib_buffer_t *lb, u32 hdr_len,
387 esp_header_t *esp)
Fan Zhangf5395782020-04-29 14:00:03 +0100388{
389 if (sa0->crypto_enc_op_id)
390 {
391 vnet_crypto_op_t *op;
392 vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
393 vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
394
395 op->src = op->dst = payload;
396 op->key_index = sa0->crypto_key_index;
397 op->len = payload_len - icv_sz;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000398 op->user_data = bi;
Fan Zhangf5395782020-04-29 14:00:03 +0100399
Benoît Ganne490b9272021-01-22 18:03:09 +0100400 if (ipsec_sa_is_set_IS_CTR (sa0))
Fan Zhangf5395782020-04-29 14:00:03 +0100401 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100402 ASSERT (sizeof (u64) == iv_sz);
403 /* construct nonce in a scratch space in front of the IP header */
404 esp_ctr_nonce_t *nonce =
405 (esp_ctr_nonce_t *) (payload - sizeof (u64) - hdr_len -
406 sizeof (*nonce));
407 u64 *pkt_iv = (u64 *) (payload - sizeof (u64));
Fan Zhangf5395782020-04-29 14:00:03 +0100408
Benoît Ganne490b9272021-01-22 18:03:09 +0100409 if (ipsec_sa_is_set_IS_AEAD (sa0))
410 {
411 /* constuct aad in a scratch space in front of the nonce */
412 op->aad = (u8 *) nonce - sizeof (esp_aead_t);
Neale Ranns5b891102021-06-28 13:31:28 +0000413 op->aad_len = esp_aad_fill (op->aad, esp, sa0, seq_hi);
Benoît Ganne490b9272021-01-22 18:03:09 +0100414 op->tag = payload + op->len;
415 op->tag_len = 16;
416 }
417 else
418 {
419 nonce->ctr = clib_host_to_net_u32 (1);
420 }
Fan Zhangf5395782020-04-29 14:00:03 +0100421
Fan Zhangf5395782020-04-29 14:00:03 +0100422 nonce->salt = sa0->salt;
Benoît Ganne490b9272021-01-22 18:03:09 +0100423 nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa0->ctr_iv_counter++);
Fan Zhangf5395782020-04-29 14:00:03 +0100424 op->iv = (u8 *) nonce;
425 }
426 else
427 {
428 op->iv = payload - iv_sz;
429 op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
430 }
431
432 if (lb != b[0])
433 {
434 /* is chained */
435 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
436 op->chunk_index = vec_len (ptd->chunks);
437 op->tag = vlib_buffer_get_tail (lb) - icv_sz;
438 esp_encrypt_chain_crypto (vm, ptd, sa0, b[0], lb, icv_sz, payload,
439 payload_len, &op->n_chunks);
440 }
441 }
442
443 if (sa0->integ_op_id)
444 {
445 vnet_crypto_op_t *op;
446 vec_add2_aligned (integ_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
447 vnet_crypto_op_init (op, sa0->integ_op_id);
448 op->src = payload - iv_sz - sizeof (esp_header_t);
449 op->digest = payload + payload_len - icv_sz;
450 op->key_index = sa0->integ_key_index;
451 op->digest_len = icv_sz;
452 op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000453 op->user_data = bi;
Fan Zhangf5395782020-04-29 14:00:03 +0100454
455 if (lb != b[0])
456 {
457 /* is chained */
458 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
459 op->chunk_index = vec_len (ptd->chunks);
460 op->digest = vlib_buffer_get_tail (lb) - icv_sz;
461
462 esp_encrypt_chain_integ (vm, ptd, sa0, b[0], lb, icv_sz,
463 payload - iv_sz - sizeof (esp_header_t),
464 payload_len + iv_sz +
465 sizeof (esp_header_t), op->digest,
466 &op->n_chunks);
467 }
468 else if (ipsec_sa_is_set_USE_ESN (sa0))
469 {
Neale Ranns5b891102021-06-28 13:31:28 +0000470 u32 tmp = clib_net_to_host_u32 (seq_hi);
471 clib_memcpy_fast (op->digest, &tmp, sizeof (seq_hi));
Fan Zhangf5395782020-04-29 14:00:03 +0100472 op->len += sizeof (seq_hi);
473 }
474 }
475}
476
Neale Rannsfc811342021-02-26 10:35:33 +0000477static_always_inline void
478esp_prepare_async_frame (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
479 vnet_crypto_async_frame_t *async_frame,
480 ipsec_sa_t *sa, vlib_buffer_t *b, esp_header_t *esp,
481 u8 *payload, u32 payload_len, u8 iv_sz, u8 icv_sz,
482 u32 bi, u16 next, u32 hdr_len, u16 async_next,
483 vlib_buffer_t *lb)
Fan Zhangf5395782020-04-29 14:00:03 +0100484{
485 esp_post_data_t *post = esp_post_data (b);
486 u8 *tag, *iv, *aad = 0;
487 u8 flag = 0;
488 u32 key_index;
489 i16 crypto_start_offset, integ_start_offset = 0;
490 u16 crypto_total_len, integ_total_len;
491
Fan Zhang18f0e312020-10-19 13:08:34 +0100492 post->next_index = next;
Fan Zhangf5395782020-04-29 14:00:03 +0100493
494 /* crypto */
495 crypto_start_offset = payload - b->data;
496 crypto_total_len = integ_total_len = payload_len - icv_sz;
497 tag = payload + crypto_total_len;
498
Fan Zhangf5395782020-04-29 14:00:03 +0100499 key_index = sa->linked_key_index;
500
Benoît Ganne490b9272021-01-22 18:03:09 +0100501 if (ipsec_sa_is_set_IS_CTR (sa))
Fan Zhangf5395782020-04-29 14:00:03 +0100502 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100503 ASSERT (sizeof (u64) == iv_sz);
504 /* construct nonce in a scratch space in front of the IP header */
505 esp_ctr_nonce_t *nonce = (esp_ctr_nonce_t *) (payload - sizeof (u64) -
506 hdr_len - sizeof (*nonce));
507 u64 *pkt_iv = (u64 *) (payload - sizeof (u64));
508
509 if (ipsec_sa_is_set_IS_AEAD (sa))
510 {
511 /* constuct aad in a scratch space in front of the nonce */
512 aad = (u8 *) nonce - sizeof (esp_aead_t);
Neale Ranns5b891102021-06-28 13:31:28 +0000513 esp_aad_fill (aad, esp, sa, sa->seq_hi);
Benoît Ganne490b9272021-01-22 18:03:09 +0100514 key_index = sa->crypto_key_index;
515 }
516 else
517 {
518 nonce->ctr = clib_host_to_net_u32 (1);
519 }
520
521 nonce->salt = sa->salt;
522 nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa->ctr_iv_counter++);
523 iv = (u8 *) nonce;
Fan Zhangf5395782020-04-29 14:00:03 +0100524 }
Benoît Ganne490b9272021-01-22 18:03:09 +0100525 else
Fan Zhangf5395782020-04-29 14:00:03 +0100526 {
Benoît Ganne490b9272021-01-22 18:03:09 +0100527 iv = payload - iv_sz;
528 flag |= VNET_CRYPTO_OP_FLAG_INIT_IV;
Fan Zhangf5395782020-04-29 14:00:03 +0100529 }
530
Benoît Ganne490b9272021-01-22 18:03:09 +0100531 if (lb != b)
532 {
533 /* chain */
534 flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
535 tag = vlib_buffer_get_tail (lb) - icv_sz;
536 crypto_total_len = esp_encrypt_chain_crypto (vm, ptd, sa, b, lb, icv_sz,
537 payload, payload_len, 0);
538 }
539
540 if (sa->integ_op_id)
541 {
542 integ_start_offset = crypto_start_offset - iv_sz - sizeof (esp_header_t);
543 integ_total_len += iv_sz + sizeof (esp_header_t);
544
545 if (b != lb)
546 {
547 integ_total_len = esp_encrypt_chain_integ (
548 vm, ptd, sa, b, lb, icv_sz,
549 payload - iv_sz - sizeof (esp_header_t),
550 payload_len + iv_sz + sizeof (esp_header_t), tag, 0);
551 }
552 else if (ipsec_sa_is_set_USE_ESN (sa))
553 {
554 u32 seq_hi = clib_net_to_host_u32 (sa->seq_hi);
555 clib_memcpy_fast (tag, &seq_hi, sizeof (seq_hi));
556 integ_total_len += sizeof (seq_hi);
557 }
558 }
559
Neale Rannsfc811342021-02-26 10:35:33 +0000560 /* this always succeeds because we know the frame is not full */
561 vnet_crypto_async_add_to_frame (vm, async_frame, key_index, crypto_total_len,
562 integ_total_len - crypto_total_len,
563 crypto_start_offset, integ_start_offset, bi,
564 async_next, iv, tag, aad, flag);
Fan Zhangf5395782020-04-29 14:00:03 +0100565}
566
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200567always_inline uword
Neale Ranns4a58e492020-12-21 13:19:10 +0000568esp_encrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
569 vlib_frame_t *frame, vnet_link_t lt, int is_tun,
Neale Rannsf16e9a52021-02-25 19:09:24 +0000570 u16 async_next_node)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 ipsec_main_t *im = &ipsec_main;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100573 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
574 u32 *from = vlib_frame_vector_args (frame);
575 u32 n_left = frame->n_vectors;
576 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100577 u32 thread_index = vm->thread_index;
578 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
579 u32 current_sa_index = ~0, current_sa_packets = 0;
580 u32 current_sa_bytes = 0, spi = 0;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500581 u8 esp_align = 4, iv_sz = 0, icv_sz = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100582 ipsec_sa_t *sa0 = 0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000583 vlib_buffer_t *lb;
584 vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
585 vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
Neale Rannsfc811342021-02-26 10:35:33 +0000586 vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
Fan Zhangf5395782020-04-29 14:00:03 +0100587 int is_async = im->async_mode;
Neale Rannsfc811342021-02-26 10:35:33 +0000588 vnet_crypto_async_op_id_t async_op = ~0;
Neale Ranns4a58e492020-12-21 13:19:10 +0000589 u16 drop_next =
590 (lt == VNET_LINK_IP6 ? ESP_ENCRYPT_NEXT_DROP6 :
591 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_DROP4 :
592 ESP_ENCRYPT_NEXT_DROP_MPLS));
593 u16 handoff_next = (lt == VNET_LINK_IP6 ?
594 ESP_ENCRYPT_NEXT_HANDOFF6 :
595 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_HANDOFF4 :
596 ESP_ENCRYPT_NEXT_HANDOFF_MPLS));
Neale Rannsf16e9a52021-02-25 19:09:24 +0000597 vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
598 u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0;
599 u16 async_nexts[VLIB_FRAME_SIZE], *async_next = async_nexts, n_async = 0;
600 u16 noop_nexts[VLIB_FRAME_SIZE], *noop_next = noop_nexts, n_noop = 0;
601 u32 sync_bi[VLIB_FRAME_SIZE];
602 u32 noop_bi[VLIB_FRAME_SIZE];
603 esp_encrypt_error_t err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604
Damjan Marionc59b9a22019-03-19 15:38:40 +0100605 vlib_get_buffers (vm, from, b, n_left);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000606
607 vec_reset_length (ptd->crypto_ops);
608 vec_reset_length (ptd->integ_ops);
609 vec_reset_length (ptd->chained_crypto_ops);
610 vec_reset_length (ptd->chained_integ_ops);
Neale Rannsfc811342021-02-26 10:35:33 +0000611 vec_reset_length (ptd->async_frames);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000612 vec_reset_length (ptd->chunks);
Neale Rannsfc811342021-02-26 10:35:33 +0000613 clib_memset (async_frames, 0, sizeof (async_frames));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100614
615 while (n_left > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700616 {
Zhiyong Yang1cff6432019-04-30 05:33:53 -0400617 u32 sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100618 dpo_id_t *dpo;
619 esp_header_t *esp;
620 u8 *payload, *next_hdr_ptr;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000621 u16 payload_len, payload_len_total, n_bufs;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400622 u32 hdr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623
Neale Rannsf16e9a52021-02-25 19:09:24 +0000624 err = ESP_ENCRYPT_ERROR_RX_PKTS;
625
Damjan Marionc59b9a22019-03-19 15:38:40 +0100626 if (n_left > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700627 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100628 u8 *p;
629 vlib_prefetch_buffer_header (b[2], LOAD);
630 p = vlib_buffer_get_current (b[1]);
Damjan Marionaf7fb042021-07-15 11:54:41 +0200631 clib_prefetch_load (p);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100632 p -= CLIB_CACHE_LINE_BYTES;
Damjan Marionaf7fb042021-07-15 11:54:41 +0200633 clib_prefetch_load (p);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000634 /* speculate that the trailer goes in the first buffer */
635 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]),
636 CLIB_CACHE_LINE_BYTES, LOAD);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700637 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638
Neale Ranns25edf142019-03-22 08:12:48 +0000639 if (is_tun)
640 {
641 /* we are on a ipsec tunnel's feature arc */
Neale Ranns28287212019-12-16 00:53:11 +0000642 vnet_buffer (b[0])->ipsec.sad_index =
643 sa_index0 = ipsec_tun_protect_get_sa_out
644 (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000645
646 if (PREDICT_FALSE (INDEX_INVALID == sa_index0))
647 {
648 err = ESP_ENCRYPT_ERROR_NO_PROTECTION;
649 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
650 drop_next);
651 goto trace;
652 }
Neale Ranns25edf142019-03-22 08:12:48 +0000653 }
654 else
655 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
656
657 if (sa_index0 != current_sa_index)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100658 {
Damjan Marion21f265f2019-06-05 15:45:50 +0200659 if (current_sa_packets)
660 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
661 current_sa_index,
662 current_sa_packets,
663 current_sa_bytes);
664 current_sa_packets = current_sa_bytes = 0;
665
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000666 sa0 = ipsec_sa_get (sa_index0);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000667
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000668 if (PREDICT_FALSE ((sa0->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
669 sa0->integ_alg == IPSEC_INTEG_ALG_NONE) &&
670 !ipsec_sa_is_set_NO_ALGO_NO_DROP (sa0)))
671 {
672 err = ESP_ENCRYPT_ERROR_NO_ENCRYPTION;
673 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
674 drop_next);
675 goto trace;
676 }
Neale Ranns123b5eb2020-10-16 14:03:55 +0000677 /* fetch the second cacheline ASAP */
Damjan Marionaf7fb042021-07-15 11:54:41 +0200678 clib_prefetch_load (sa0->cacheline1);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000679
Damjan Marionc59b9a22019-03-19 15:38:40 +0100680 current_sa_index = sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100681 spi = clib_net_to_host_u32 (sa0->spi);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500682 esp_align = sa0->esp_block_align;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200683 icv_sz = sa0->integ_icv_size;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100684 iv_sz = sa0->crypto_iv_size;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000685 is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0);
Neale Rannsfc811342021-02-26 10:35:33 +0000686 }
Fan Zhangf5395782020-04-29 14:00:03 +0100687
Neale Ranns1a52d372021-02-04 11:33:32 +0000688 if (PREDICT_FALSE (~0 == sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +0000689 {
690 /* this is the first packet to use this SA, claim the SA
691 * for this thread. this could happen simultaneously on
692 * another thread */
Neale Ranns1a52d372021-02-04 11:33:32 +0000693 clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
Neale Rannsf62a8c02019-04-02 08:13:33 +0000694 ipsec_sa_assign_thread (thread_index));
695 }
696
Neale Ranns1a52d372021-02-04 11:33:32 +0000697 if (PREDICT_FALSE (thread_index != sa0->thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +0000698 {
Neale Rannsaa7d7662021-02-10 08:42:49 +0000699 vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
Neale Rannsf16e9a52021-02-25 19:09:24 +0000700 err = ESP_ENCRYPT_ERROR_HANDOFF;
701 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
702 handoff_next);
Neale Rannsf62a8c02019-04-02 08:13:33 +0000703 goto trace;
704 }
705
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000706 lb = b[0];
707 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
708 if (n_bufs == 0)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100709 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000710 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
711 esp_set_next_index (b[0], node, err, n_noop, noop_nexts, drop_next);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100712 goto trace;
713 }
714
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000715 if (n_bufs > 1)
716 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000717 /* find last buffer in the chain */
718 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
719 lb = vlib_get_buffer (vm, lb->next_buffer);
720 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000721
Damjan Marionc59b9a22019-03-19 15:38:40 +0100722 if (PREDICT_FALSE (esp_seq_advance (sa0)))
723 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000724 err = ESP_ENCRYPT_ERROR_SEQ_CYCLED;
725 esp_set_next_index (b[0], node, err, n_noop, noop_nexts, drop_next);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100726 goto trace;
727 }
728
729 /* space for IV */
730 hdr_len = iv_sz;
731
Damjan Mariond709cbc2019-03-26 13:16:42 +0100732 if (ipsec_sa_is_set_IS_TUNNEL (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100733 {
734 payload = vlib_buffer_get_current (b[0]);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000735 next_hdr_ptr = esp_add_footer_and_icv (
736 vm, &lb, esp_align, icv_sz, node, buffer_data_size,
737 vlib_buffer_length_in_chain (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000738 if (!next_hdr_ptr)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000739 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000740 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
741 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
742 drop_next);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000743 goto trace;
744 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000745 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000746 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000747 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100748
749 /* ESP header */
750 hdr_len += sizeof (*esp);
751 esp = (esp_header_t *) (payload - hdr_len);
752
753 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100754 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100755 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100756 hdr_len += sizeof (udp_header_t);
757 esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000758 payload_len_total + hdr_len);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100759 }
760
761 /* IP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100762 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100763 {
764 ip6_header_t *ip6;
765 u16 len = sizeof (ip6_header_t);
766 hdr_len += len;
767 ip6 = (ip6_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000768 clib_memcpy_fast (ip6, &sa0->ip6_hdr, sizeof (ip6_header_t));
769
Neale Ranns4a58e492020-12-21 13:19:10 +0000770 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000771 {
772 *next_hdr_ptr = IP_PROTOCOL_IPV6;
773 tunnel_encap_fixup_6o6 (sa0->tunnel_flags,
774 (const ip6_header_t *) payload,
775 ip6);
776 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000777 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000778 {
779 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
Neale Rannsa91cb452021-02-04 11:02:52 +0000780 tunnel_encap_fixup_4o6 (sa0->tunnel_flags, b[0],
781 (const ip4_header_t *) payload, ip6);
Neale Ranns041add72020-01-02 04:06:10 +0000782 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000783 else if (VNET_LINK_MPLS == lt)
784 {
785 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
786 tunnel_encap_fixup_mplso6 (
Neale Rannsa91cb452021-02-04 11:02:52 +0000787 sa0->tunnel_flags, b[0],
788 (const mpls_unicast_header_t *) payload, ip6);
Neale Ranns4a58e492020-12-21 13:19:10 +0000789 }
790 else
791 ASSERT (0);
792
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000793 len = payload_len_total + hdr_len - len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100794 ip6->payload_length = clib_net_to_host_u16 (len);
Neale Ranns45d6d832021-01-19 13:38:47 +0000795 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Damjan Marionc98275f2019-03-06 14:05:01 +0100796 }
797 else
798 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100799 ip4_header_t *ip4;
800 u16 len = sizeof (ip4_header_t);
801 hdr_len += len;
802 ip4 = (ip4_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000803 clib_memcpy_fast (ip4, &sa0->ip4_hdr, sizeof (ip4_header_t));
804
Neale Ranns4a58e492020-12-21 13:19:10 +0000805 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000806 {
807 *next_hdr_ptr = IP_PROTOCOL_IPV6;
808 tunnel_encap_fixup_6o4_w_chksum (sa0->tunnel_flags,
809 (const ip6_header_t *)
810 payload, ip4);
811 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000812 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000813 {
814 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
815 tunnel_encap_fixup_4o4_w_chksum (sa0->tunnel_flags,
816 (const ip4_header_t *)
817 payload, ip4);
818 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000819 else if (VNET_LINK_MPLS == lt)
820 {
821 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
822 tunnel_encap_fixup_mplso4_w_chksum (
823 sa0->tunnel_flags, (const mpls_unicast_header_t *) payload,
824 ip4);
825 }
826 else
827 ASSERT (0);
828
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000829 len = payload_len_total + hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100830 esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100831 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100832
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000833 dpo = &sa0->dpo;
Neale Ranns25edf142019-03-22 08:12:48 +0000834 if (!is_tun)
835 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000836 sync_next[0] = dpo->dpoi_next_node;
Neale Ranns25edf142019-03-22 08:12:48 +0000837 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
838 }
Neale Ranns4ec36c52020-03-31 09:21:29 -0400839 else
Neale Rannsf16e9a52021-02-25 19:09:24 +0000840 sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000841 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Damjan Marionc98275f2019-03-06 14:05:01 +0100842 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100843 else /* transport mode */
Damjan Marionc98275f2019-03-06 14:05:01 +0100844 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100845 u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
Neale Ranns02950402019-12-20 00:54:57 +0000846 ip6_ext_header_t *ext_hdr;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100847 udp_header_t *udp = 0;
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400848 u16 udp_len = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100849 u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
Damjan Marionc98275f2019-03-06 14:05:01 +0100850
Neale Ranns4a58e492020-12-21 13:19:10 +0000851 ip_len =
852 (VNET_LINK_IP6 == lt ?
853 esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
854 ip4_header_bytes ((ip4_header_t *) old_ip_hdr));
Damjan Marionc98275f2019-03-06 14:05:01 +0100855
Damjan Marionc59b9a22019-03-19 15:38:40 +0100856 vlib_buffer_advance (b[0], ip_len);
857 payload = vlib_buffer_get_current (b[0]);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000858 next_hdr_ptr = esp_add_footer_and_icv (
859 vm, &lb, esp_align, icv_sz, node, buffer_data_size,
860 vlib_buffer_length_in_chain (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000861 if (!next_hdr_ptr)
Fan Zhang18f0e312020-10-19 13:08:34 +0100862 {
Neale Rannsf16e9a52021-02-25 19:09:24 +0000863 err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
864 esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
865 drop_next);
Fan Zhang18f0e312020-10-19 13:08:34 +0100866 goto trace;
867 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000868
869 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000870 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000871 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100872
873 /* ESP header */
874 hdr_len += sizeof (*esp);
875 esp = (esp_header_t *) (payload - hdr_len);
876
877 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100878 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100879 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100880 hdr_len += sizeof (udp_header_t);
881 udp = (udp_header_t *) (payload - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100882 }
883
Damjan Marionc59b9a22019-03-19 15:38:40 +0100884 /* IP header */
885 hdr_len += ip_len;
886 ip_hdr = payload - hdr_len;
887
888 /* L2 header */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800889 if (!is_tun)
890 {
891 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
892 hdr_len += l2_len;
893 l2_hdr = payload - hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100894
Neale Rannsc87b66c2019-02-07 07:26:12 -0800895 /* copy l2 and ip header */
896 clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
897 }
898 else
899 l2_len = 0;
900
Neale Ranns4a58e492020-12-21 13:19:10 +0000901 if (VNET_LINK_IP6 == lt)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100902 {
Neale Ranns02950402019-12-20 00:54:57 +0000903 ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
904 if (PREDICT_TRUE (NULL == ext_hdr))
905 {
906 *next_hdr_ptr = ip6->protocol;
907 ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
908 }
909 else
910 {
911 *next_hdr_ptr = ext_hdr->next_hdr;
912 ext_hdr->next_hdr = IP_PROTOCOL_IPSEC_ESP;
913 }
Neale Rannsd207fd72019-04-18 17:18:12 -0700914 ip6->payload_length =
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000915 clib_host_to_net_u16 (payload_len_total + hdr_len - l2_len -
Neale Ranns02950402019-12-20 00:54:57 +0000916 sizeof (ip6_header_t));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100917 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000918 else if (VNET_LINK_IP4 == lt)
Damjan Marionc98275f2019-03-06 14:05:01 +0100919 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100920 u16 len;
Neale Ranns02950402019-12-20 00:54:57 +0000921 ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100922 *next_hdr_ptr = ip4->protocol;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000923 len = payload_len_total + hdr_len - l2_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100924 if (udp)
925 {
926 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400927 udp_len = len - ip_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100928 }
929 else
930 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100931 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100932
Neale Ranns02950402019-12-20 00:54:57 +0000933 clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
934
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400935 if (udp)
936 {
937 esp_fill_udp_hdr (sa0, udp, udp_len);
938 }
939
Neale Rannsf16e9a52021-02-25 19:09:24 +0000940 sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Damjan Marionc98275f2019-03-06 14:05:01 +0100941 }
942
PiotrX Kleskifdca4dd2020-05-05 14:14:22 +0200943 if (lb != b[0])
944 {
945 crypto_ops = &ptd->chained_crypto_ops;
946 integ_ops = &ptd->chained_integ_ops;
947 }
948 else
949 {
950 crypto_ops = &ptd->crypto_ops;
951 integ_ops = &ptd->integ_ops;
952 }
953
Damjan Marionc59b9a22019-03-19 15:38:40 +0100954 esp->spi = spi;
955 esp->seq = clib_net_to_host_u32 (sa0->seq);
Damjan Marionc98275f2019-03-06 14:05:01 +0100956
Fan Zhangf5395782020-04-29 14:00:03 +0100957 if (is_async)
Matthew Smith51d56ba2021-06-04 09:18:37 -0500958 {
959 async_op = sa0->crypto_async_enc_op_id;
960
961 /* get a frame for this op if we don't yet have one or it's full
962 */
963 if (NULL == async_frames[async_op] ||
964 vnet_crypto_async_frame_is_full (async_frames[async_op]))
965 {
966 async_frames[async_op] =
967 vnet_crypto_async_get_frame (vm, async_op);
968 /* Save the frame to the list we'll submit at the end */
969 vec_add1 (ptd->async_frames, async_frames[async_op]);
970 }
971
972 esp_prepare_async_frame (vm, ptd, async_frames[async_op], sa0, b[0],
973 esp, payload, payload_len, iv_sz, icv_sz,
974 from[b - bufs], sync_next[0], hdr_len,
975 async_next_node, lb);
976 }
Fan Zhangf5395782020-04-29 14:00:03 +0100977 else
Neale Ranns5b891102021-06-28 13:31:28 +0000978 esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, sa0->seq_hi,
979 payload, payload_len, iv_sz, icv_sz, n_sync, b,
980 lb, hdr_len, esp);
Damjan Marionc98275f2019-03-06 14:05:01 +0100981
Damjan Marionc59b9a22019-03-19 15:38:40 +0100982 vlib_buffer_advance (b[0], 0LL - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100983
Damjan Marionc59b9a22019-03-19 15:38:40 +0100984 current_sa_packets += 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000985 current_sa_bytes += payload_len_total;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100986
987 trace:
988 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
Damjan Marionc98275f2019-03-06 14:05:01 +0100989 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100990 esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
991 sizeof (*tr));
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000992 if (INDEX_INVALID == sa_index0)
993 clib_memset_u8 (tr, 0xff, sizeof (*tr));
994 else
995 {
996 tr->sa_index = sa_index0;
997 tr->spi = sa0->spi;
998 tr->spi = sa0->spi;
999 tr->seq = sa0->seq;
1000 tr->sa_seq_hi = sa0->seq_hi;
1001 tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
1002 tr->crypto_alg = sa0->crypto_alg;
1003 tr->integ_alg = sa0->integ_alg;
1004 }
Damjan Marionc98275f2019-03-06 14:05:01 +01001005 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001006
Damjan Marionc98275f2019-03-06 14:05:01 +01001007 /* next */
Neale Rannsf16e9a52021-02-25 19:09:24 +00001008 if (ESP_ENCRYPT_ERROR_RX_PKTS != err)
1009 {
1010 noop_bi[n_noop] = from[b - bufs];
1011 n_noop++;
1012 noop_next++;
1013 }
1014 else if (!is_async)
1015 {
1016 sync_bi[n_sync] = from[b - bufs];
1017 sync_bufs[n_sync] = b[0];
1018 n_sync++;
1019 sync_next++;
1020 }
1021 else
1022 {
1023 n_async++;
1024 async_next++;
1025 }
Damjan Marionc59b9a22019-03-19 15:38:40 +01001026 n_left -= 1;
Damjan Marionc59b9a22019-03-19 15:38:40 +01001027 b += 1;
Damjan Marionc98275f2019-03-06 14:05:01 +01001028 }
1029
Neale Ranns6fdcc3d2021-10-08 07:30:47 +00001030 if (INDEX_INVALID != current_sa_index)
1031 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1032 current_sa_index, current_sa_packets,
1033 current_sa_bytes);
Neale Rannsf16e9a52021-02-25 19:09:24 +00001034 if (n_sync)
Fan Zhangf5395782020-04-29 14:00:03 +01001035 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001036 esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts,
1037 drop_next);
1038 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs,
1039 sync_nexts, ptd->chunks, drop_next);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001040
Neale Rannsf16e9a52021-02-25 19:09:24 +00001041 esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts,
1042 drop_next);
1043 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs,
1044 sync_nexts, ptd->chunks, drop_next);
Neale Rannsfc811342021-02-26 10:35:33 +00001045
Neale Rannsf16e9a52021-02-25 19:09:24 +00001046 vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync);
Fan Zhangf5395782020-04-29 14:00:03 +01001047 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001048 if (n_async)
Fan Zhangf5395782020-04-29 14:00:03 +01001049 {
Neale Rannsfc811342021-02-26 10:35:33 +00001050 /* submit all of the open frames */
1051 vnet_crypto_async_frame_t **async_frame;
1052
1053 vec_foreach (async_frame, ptd->async_frames)
Fan Zhang18f0e312020-10-19 13:08:34 +01001054 {
Neale Rannsfc811342021-02-26 10:35:33 +00001055 if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0)
1056 {
Neale Rannsf16e9a52021-02-25 19:09:24 +00001057 n_noop += esp_async_recycle_failed_submit (
1058 vm, *async_frame, node, ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR,
1059 n_sync, noop_bi, noop_nexts, drop_next);
Neale Rannsfc811342021-02-26 10:35:33 +00001060 vnet_crypto_async_reset_frame (*async_frame);
1061 vnet_crypto_async_free_frame (vm, *async_frame);
1062 }
Fan Zhang18f0e312020-10-19 13:08:34 +01001063 }
Fan Zhangf5395782020-04-29 14:00:03 +01001064 }
Neale Rannsf16e9a52021-02-25 19:09:24 +00001065 if (n_noop)
1066 vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
1067
1068 vlib_node_increment_counter (vm, node->node_index, ESP_ENCRYPT_ERROR_RX_PKTS,
1069 frame->n_vectors);
Damjan Marionc59b9a22019-03-19 15:38:40 +01001070
Damjan Marionc59b9a22019-03-19 15:38:40 +01001071 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072}
1073
Fan Zhangf5395782020-04-29 14:00:03 +01001074always_inline uword
1075esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1076 vlib_frame_t * frame)
1077{
1078 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1079 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1080 u32 *from = vlib_frame_vector_args (frame);
1081 u32 n_left = frame->n_vectors;
1082
1083 vlib_get_buffers (vm, from, b, n_left);
1084
1085 if (n_left >= 4)
1086 {
1087 vlib_prefetch_buffer_header (b[0], LOAD);
1088 vlib_prefetch_buffer_header (b[1], LOAD);
1089 vlib_prefetch_buffer_header (b[2], LOAD);
1090 vlib_prefetch_buffer_header (b[3], LOAD);
1091 }
1092
1093 while (n_left > 8)
1094 {
1095 vlib_prefetch_buffer_header (b[4], LOAD);
1096 vlib_prefetch_buffer_header (b[5], LOAD);
1097 vlib_prefetch_buffer_header (b[6], LOAD);
1098 vlib_prefetch_buffer_header (b[7], LOAD);
1099
1100 next[0] = (esp_post_data (b[0]))->next_index;
1101 next[1] = (esp_post_data (b[1]))->next_index;
1102 next[2] = (esp_post_data (b[2]))->next_index;
1103 next[3] = (esp_post_data (b[3]))->next_index;
1104
1105 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
1106 {
1107 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
1108 {
1109 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1110 sizeof (*tr));
1111 tr->next_index = next[0];
1112 }
1113 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
1114 {
1115 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1],
1116 sizeof (*tr));
1117 tr->next_index = next[1];
1118 }
1119 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
1120 {
1121 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2],
1122 sizeof (*tr));
1123 tr->next_index = next[2];
1124 }
1125 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
1126 {
1127 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3],
1128 sizeof (*tr));
1129 tr->next_index = next[3];
1130 }
1131 }
1132
1133 b += 4;
1134 next += 4;
1135 n_left -= 4;
1136 }
1137
1138 while (n_left > 0)
1139 {
1140 next[0] = (esp_post_data (b[0]))->next_index;
1141 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1142 {
1143 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1144 sizeof (*tr));
1145 tr->next_index = next[0];
1146 }
1147
1148 b += 1;
1149 next += 1;
1150 n_left -= 1;
1151 }
1152
1153 vlib_node_increment_counter (vm, node->node_index,
1154 ESP_ENCRYPT_ERROR_POST_RX_PKTS,
1155 frame->n_vectors);
1156 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1157 return frame->n_vectors;
1158}
1159
Klement Sekerab8f35442018-10-29 13:38:19 +01001160VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
1161 vlib_node_runtime_t * node,
1162 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001163{
Neale Ranns4a58e492020-12-21 13:19:10 +00001164 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001165 esp_encrypt_async_next.esp4_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001166}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001167
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001168/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001169VLIB_REGISTER_NODE (esp4_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001170 .name = "esp4-encrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171 .vector_size = sizeof (u32),
1172 .format_trace = format_esp_encrypt_trace,
1173 .type = VLIB_NODE_TYPE_INTERNAL,
1174
Neale Ranns4a58e492020-12-21 13:19:10 +00001175 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176 .error_strings = esp_encrypt_error_strings,
1177
1178 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Ranns4a58e492020-12-21 13:19:10 +00001179 .next_nodes = { [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1180 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1181 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1182 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-handoff",
1183 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-handoff",
1184 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "error-drop",
1185 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output" },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001187/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188
Fan Zhangf5395782020-04-29 14:00:03 +01001189VLIB_NODE_FN (esp4_encrypt_post_node) (vlib_main_t * vm,
1190 vlib_node_runtime_t * node,
1191 vlib_frame_t * from_frame)
1192{
1193 return esp_encrypt_post_inline (vm, node, from_frame);
1194}
1195
1196/* *INDENT-OFF* */
1197VLIB_REGISTER_NODE (esp4_encrypt_post_node) = {
1198 .name = "esp4-encrypt-post",
1199 .vector_size = sizeof (u32),
1200 .format_trace = format_esp_post_encrypt_trace,
1201 .type = VLIB_NODE_TYPE_INTERNAL,
1202 .sibling_of = "esp4-encrypt",
1203
1204 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1205 .error_strings = esp_encrypt_error_strings,
1206};
1207/* *INDENT-ON* */
1208
Klement Sekerab8f35442018-10-29 13:38:19 +01001209VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
1210 vlib_node_runtime_t * node,
1211 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001212{
Neale Ranns4a58e492020-12-21 13:19:10 +00001213 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001214 esp_encrypt_async_next.esp6_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001215}
1216
1217/* *INDENT-OFF* */
1218VLIB_REGISTER_NODE (esp6_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001219 .name = "esp6-encrypt",
1220 .vector_size = sizeof (u32),
1221 .format_trace = format_esp_encrypt_trace,
1222 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001223 .sibling_of = "esp4-encrypt",
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001224
1225 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1226 .error_strings = esp_encrypt_error_strings,
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001227};
1228/* *INDENT-ON* */
1229
Fan Zhangf5395782020-04-29 14:00:03 +01001230VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm,
1231 vlib_node_runtime_t * node,
1232 vlib_frame_t * from_frame)
1233{
1234 return esp_encrypt_post_inline (vm, node, from_frame);
1235}
1236
1237/* *INDENT-OFF* */
1238VLIB_REGISTER_NODE (esp6_encrypt_post_node) = {
1239 .name = "esp6-encrypt-post",
1240 .vector_size = sizeof (u32),
1241 .format_trace = format_esp_post_encrypt_trace,
1242 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001243 .sibling_of = "esp4-encrypt",
Fan Zhangf5395782020-04-29 14:00:03 +01001244
1245 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1246 .error_strings = esp_encrypt_error_strings,
1247};
1248/* *INDENT-ON* */
1249
Neale Ranns25edf142019-03-22 08:12:48 +00001250VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
1251 vlib_node_runtime_t * node,
1252 vlib_frame_t * from_frame)
1253{
Neale Ranns4a58e492020-12-21 13:19:10 +00001254 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001255 esp_encrypt_async_next.esp4_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001256}
1257
1258/* *INDENT-OFF* */
1259VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
1260 .name = "esp4-encrypt-tun",
1261 .vector_size = sizeof (u32),
1262 .format_trace = format_esp_encrypt_trace,
1263 .type = VLIB_NODE_TYPE_INTERNAL,
1264
1265 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1266 .error_strings = esp_encrypt_error_strings,
Neale Rannsd7603d92019-03-28 08:56:10 +00001267
Neale Rannsf62a8c02019-04-02 08:13:33 +00001268 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001269 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001270 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1271 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001272 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001273 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001274 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1275 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001276 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001277 },
Neale Ranns25edf142019-03-22 08:12:48 +00001278};
1279
Fan Zhangf5395782020-04-29 14:00:03 +01001280VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm,
1281 vlib_node_runtime_t * node,
1282 vlib_frame_t * from_frame)
1283{
1284 return esp_encrypt_post_inline (vm, node, from_frame);
1285}
1286
1287/* *INDENT-OFF* */
1288VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = {
1289 .name = "esp4-encrypt-tun-post",
1290 .vector_size = sizeof (u32),
1291 .format_trace = format_esp_post_encrypt_trace,
1292 .type = VLIB_NODE_TYPE_INTERNAL,
1293 .sibling_of = "esp4-encrypt-tun",
1294
1295 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1296 .error_strings = esp_encrypt_error_strings,
1297};
Neale Ranns25edf142019-03-22 08:12:48 +00001298/* *INDENT-ON* */
1299
1300VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
1301 vlib_node_runtime_t * node,
1302 vlib_frame_t * from_frame)
1303{
Neale Ranns4a58e492020-12-21 13:19:10 +00001304 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001305 esp_encrypt_async_next.esp6_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001306}
1307
1308/* *INDENT-OFF* */
1309VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
1310 .name = "esp6-encrypt-tun",
1311 .vector_size = sizeof (u32),
1312 .format_trace = format_esp_encrypt_trace,
1313 .type = VLIB_NODE_TYPE_INTERNAL,
1314
1315 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1316 .error_strings = esp_encrypt_error_strings,
Neale Rannsd7603d92019-03-28 08:56:10 +00001317
Neale Rannsf62a8c02019-04-02 08:13:33 +00001318 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001319 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001320 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1321 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001322 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1323 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001324 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001325 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001326 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001327 },
Neale Ranns25edf142019-03-22 08:12:48 +00001328};
1329
Neale Ranns25edf142019-03-22 08:12:48 +00001330/* *INDENT-ON* */
1331
Fan Zhangf5395782020-04-29 14:00:03 +01001332VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm,
1333 vlib_node_runtime_t * node,
1334 vlib_frame_t * from_frame)
1335{
1336 return esp_encrypt_post_inline (vm, node, from_frame);
1337}
1338
1339/* *INDENT-OFF* */
1340VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = {
1341 .name = "esp6-encrypt-tun-post",
1342 .vector_size = sizeof (u32),
1343 .format_trace = format_esp_post_encrypt_trace,
1344 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Ranns4a58e492020-12-21 13:19:10 +00001345 .sibling_of = "esp-mpls-encrypt-tun",
Fan Zhangf5395782020-04-29 14:00:03 +01001346
Neale Ranns4a58e492020-12-21 13:19:10 +00001347 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
Fan Zhangf5395782020-04-29 14:00:03 +01001348 .error_strings = esp_encrypt_error_strings,
1349};
1350/* *INDENT-ON* */
1351
Neale Ranns4a58e492020-12-21 13:19:10 +00001352VLIB_NODE_FN (esp_mpls_encrypt_tun_node)
1353(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1354{
1355 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_MPLS, 1,
1356 esp_encrypt_async_next.esp_mpls_tun_post_next);
1357}
1358
1359VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_node) = {
1360 .name = "esp-mpls-encrypt-tun",
1361 .vector_size = sizeof (u32),
1362 .format_trace = format_esp_encrypt_trace,
1363 .type = VLIB_NODE_TYPE_INTERNAL,
1364
1365 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1366 .error_strings = esp_encrypt_error_strings,
1367
1368 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1369 .next_nodes = {
1370 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1371 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1372 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1373 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1374 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1375 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1376 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1377 },
1378};
1379
1380VLIB_NODE_FN (esp_mpls_encrypt_tun_post_node)
1381(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1382{
1383 return esp_encrypt_post_inline (vm, node, from_frame);
1384}
1385
1386VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_post_node) = {
1387 .name = "esp-mpls-encrypt-tun-post",
1388 .vector_size = sizeof (u32),
1389 .format_trace = format_esp_post_encrypt_trace,
1390 .type = VLIB_NODE_TYPE_INTERNAL,
1391 .sibling_of = "esp-mpls-encrypt-tun",
1392
1393 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1394 .error_strings = esp_encrypt_error_strings,
1395};
1396
Neale Ranns2d498302021-02-25 08:38:58 +00001397#ifndef CLIB_MARCH_VARIANT
1398
1399static clib_error_t *
1400esp_encrypt_init (vlib_main_t *vm)
1401{
1402 ipsec_main_t *im = &ipsec_main;
1403
1404 im->esp4_enc_fq_index =
1405 vlib_frame_queue_main_init (esp4_encrypt_node.index, 0);
1406 im->esp6_enc_fq_index =
1407 vlib_frame_queue_main_init (esp6_encrypt_node.index, 0);
1408 im->esp4_enc_tun_fq_index =
1409 vlib_frame_queue_main_init (esp4_encrypt_tun_node.index, 0);
1410 im->esp6_enc_tun_fq_index =
1411 vlib_frame_queue_main_init (esp6_encrypt_tun_node.index, 0);
1412 im->esp_mpls_enc_tun_fq_index =
1413 vlib_frame_queue_main_init (esp_mpls_encrypt_tun_node.index, 0);
1414
1415 return 0;
1416}
1417
1418VLIB_INIT_FUNCTION (esp_encrypt_init);
1419
1420#endif
1421
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001422/*
1423 * fd.io coding-style-patch-verification: ON
1424 *
1425 * Local Variables:
1426 * eval: (c-set-style "gnu")
1427 * End:
1428 */