blob: bd6e7641f4a88cdbddf1e58e37b3cd72dcfcf16a [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
Damjan Marionc59b9a22019-03-19 15:38:40 +010046#define foreach_esp_encrypt_error \
47 _(RX_PKTS, "ESP pkts received") \
Fan Zhangf5395782020-04-29 14:00:03 +010048 _(POST_RX_PKTS, "ESP-post pkts received") \
Damjan Marionc59b9a22019-03-19 15:38:40 +010049 _(SEQ_CYCLED, "sequence number cycled (packet dropped)") \
50 _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
Fan Zhangf5395782020-04-29 14:00:03 +010051 _(CRYPTO_QUEUE_FULL, "crypto queue full (packet dropped)") \
Filip Tehlarefcad1a2020-02-04 09:36:04 +000052 _(NO_BUFFERS, "no buffers (packet dropped)") \
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070054typedef enum
55{
Ed Warnickecb9cada2015-12-08 15:45:58 -070056#define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
57 foreach_esp_encrypt_error
58#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070059 ESP_ENCRYPT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070060} esp_encrypt_error_t;
61
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070062static char *esp_encrypt_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070063#define _(sym,string) string,
64 foreach_esp_encrypt_error
65#undef _
66};
67
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070068typedef struct
69{
Neale Ranns8d7c5022019-02-06 01:41:05 -080070 u32 sa_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 u32 spi;
72 u32 seq;
Neale Ranns6afaae12019-07-17 15:07:14 +000073 u32 sa_seq_hi;
Klement Sekera4b089f22018-04-17 18:04:57 +020074 u8 udp_encap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070075 ipsec_crypto_alg_t crypto_alg;
76 ipsec_integ_alg_t integ_alg;
77} esp_encrypt_trace_t;
78
Fan Zhangf5395782020-04-29 14:00:03 +010079typedef struct
80{
81 u32 next_index;
82} esp_encrypt_post_trace_t;
83
Ed Warnickecb9cada2015-12-08 15:45:58 -070084/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070085static u8 *
86format_esp_encrypt_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087{
88 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
89 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070090 esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020092 s =
93 format (s,
Neale Ranns6afaae12019-07-17 15:07:14 +000094 "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
95 t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
96 format_ipsec_crypto_alg,
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020097 t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
98 t->udp_encap ? " udp-encap-enabled" : "");
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 return s;
100}
101
Fan Zhangf5395782020-04-29 14:00:03 +0100102static u8 *
103format_esp_post_encrypt_trace (u8 * s, va_list * args)
104{
105 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
106 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
107 esp_encrypt_post_trace_t *t = va_arg (*args, esp_encrypt_post_trace_t *);
108
109 s = format (s, "esp-post: next node index %u", t->next_index);
110 return s;
111}
112
Damjan Marionc59b9a22019-03-19 15:38:40 +0100113/* pad packet in input buffer */
114static_always_inline u8 *
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000115esp_add_footer_and_icv (vlib_main_t * vm, vlib_buffer_t ** last,
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500116 u8 esp_align, u8 icv_sz,
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000117 u16 * next, 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
Damjan Mariond97918e2019-04-25 18:28:31 +0200295typedef struct
296{
297 u32 salt;
298 u64 iv;
299} __clib_packed esp_gcm_nonce_t;
300
301STATIC_ASSERT_SIZEOF (esp_gcm_nonce_t, 12);
302
Fan Zhangf5395782020-04-29 14:00:03 +0100303static_always_inline u32
304esp_encrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
305 ipsec_sa_t * sa0, vlib_buffer_t * b,
306 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
307 u32 start_len, u16 * n_ch)
308{
309 vnet_crypto_op_chunk_t *ch;
310 vlib_buffer_t *cb = b;
311 u32 n_chunks = 1;
312 u32 total_len;
313 vec_add2 (ptd->chunks, ch, 1);
314 total_len = ch->len = start_len;
315 ch->src = ch->dst = start;
316 cb = vlib_get_buffer (vm, cb->next_buffer);
317
318 while (1)
319 {
320 vec_add2 (ptd->chunks, ch, 1);
321 n_chunks += 1;
322 if (lb == cb)
323 total_len += ch->len = cb->current_length - icv_sz;
324 else
325 total_len += ch->len = cb->current_length;
326 ch->src = ch->dst = vlib_buffer_get_current (cb);
327
328 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
329 break;
330
331 cb = vlib_get_buffer (vm, cb->next_buffer);
332 }
333
334 if (n_ch)
335 *n_ch = n_chunks;
336
337 return total_len;
338}
339
340static_always_inline u32
341esp_encrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
342 ipsec_sa_t * sa0, vlib_buffer_t * b,
343 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
344 u32 start_len, u8 * digest, u16 * n_ch)
345{
346 vnet_crypto_op_chunk_t *ch;
347 vlib_buffer_t *cb = b;
348 u32 n_chunks = 1;
349 u32 total_len;
350 vec_add2 (ptd->chunks, ch, 1);
351 total_len = ch->len = start_len;
352 ch->src = start;
353 cb = vlib_get_buffer (vm, cb->next_buffer);
354
355 while (1)
356 {
357 vec_add2 (ptd->chunks, ch, 1);
358 n_chunks += 1;
359 if (lb == cb)
360 {
361 total_len += ch->len = cb->current_length - icv_sz;
362 if (ipsec_sa_is_set_USE_ESN (sa0))
363 {
364 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
365 clib_memcpy_fast (digest, &seq_hi, sizeof (seq_hi));
366 ch->len += sizeof (seq_hi);
367 total_len += sizeof (seq_hi);
368 }
369 }
370 else
371 total_len += ch->len = cb->current_length;
372 ch->src = vlib_buffer_get_current (cb);
373
374 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
375 break;
376
377 cb = vlib_get_buffer (vm, cb->next_buffer);
378 }
379
380 if (n_ch)
381 *n_ch = n_chunks;
382
383 return total_len;
384}
385
386always_inline void
387esp_prepare_sync_op (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
388 vnet_crypto_op_t ** crypto_ops,
389 vnet_crypto_op_t ** integ_ops, ipsec_sa_t * sa0,
390 u8 * payload, u16 payload_len, u8 iv_sz, u8 icv_sz,
391 vlib_buffer_t ** bufs, vlib_buffer_t ** b,
392 vlib_buffer_t * lb, u32 hdr_len, esp_header_t * esp,
393 esp_gcm_nonce_t * nonce)
394{
395 if (sa0->crypto_enc_op_id)
396 {
397 vnet_crypto_op_t *op;
398 vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
399 vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
400
401 op->src = op->dst = payload;
402 op->key_index = sa0->crypto_key_index;
403 op->len = payload_len - icv_sz;
404 op->user_data = b - bufs;
405
406 if (ipsec_sa_is_set_IS_AEAD (sa0))
407 {
408 /*
409 * construct the AAD in a scratch space in front
410 * of the IP header.
411 */
412 op->aad = payload - hdr_len - sizeof (esp_aead_t);
413 op->aad_len = esp_aad_fill (op->aad, esp, sa0);
414
415 op->tag = payload + op->len;
416 op->tag_len = 16;
417
418 u64 *iv = (u64 *) (payload - iv_sz);
419 nonce->salt = sa0->salt;
420 nonce->iv = *iv = clib_host_to_net_u64 (sa0->gcm_iv_counter++);
421 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);
450 op->user_data = b - bufs;
451
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
474static_always_inline int
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,
478 esp_header_t * esp, u8 * payload, u32 payload_len,
Fan Zhang18f0e312020-10-19 13:08:34 +0100479 u8 iv_sz, u8 icv_sz, u32 bi, u16 next, u32 hdr_len,
Fan Zhangf5395782020-04-29 14:00:03 +0100480 u16 async_next, vlib_buffer_t * lb)
481{
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
496 /* aead */
497 if (ipsec_sa_is_set_IS_AEAD (sa))
498 {
499 esp_gcm_nonce_t *nonce;
500 u64 *pkt_iv = (u64 *) (payload - iv_sz);
501
502 aad = payload - hdr_len - sizeof (esp_aead_t);
503 esp_aad_fill (aad, esp, sa);
504 nonce = (esp_gcm_nonce_t *) (aad - sizeof (*nonce));
505 nonce->salt = sa->salt;
506 nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa->gcm_iv_counter++);
507 iv = (u8 *) nonce;
508 key_index = sa->crypto_key_index;
509
510 if (lb != b)
511 {
512 /* chain */
513 flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
514 tag = vlib_buffer_get_tail (lb) - icv_sz;
515 crypto_total_len = esp_encrypt_chain_crypto (vm, ptd, sa, b, lb,
516 icv_sz, payload,
517 payload_len, 0);
518 }
519 goto out;
520 }
521
522 /* cipher then hash */
523 iv = payload - iv_sz;
524 integ_start_offset = crypto_start_offset - iv_sz - sizeof (esp_header_t);
525 integ_total_len += iv_sz + sizeof (esp_header_t);
526 flag |= VNET_CRYPTO_OP_FLAG_INIT_IV;
527 key_index = sa->linked_key_index;
528
529 if (b != lb)
530 {
531 flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
532 crypto_total_len = esp_encrypt_chain_crypto (vm, ptd, sa, b, lb,
533 icv_sz, payload,
534 payload_len, 0);
535 tag = vlib_buffer_get_tail (lb) - icv_sz;
536 integ_total_len = esp_encrypt_chain_integ (vm, ptd, sa, b, lb, icv_sz,
537 payload - iv_sz -
538 sizeof (esp_header_t),
539 payload_len + iv_sz +
540 sizeof (esp_header_t),
541 tag, 0);
542 }
543 else if (ipsec_sa_is_set_USE_ESN (sa) && !ipsec_sa_is_set_IS_AEAD (sa))
544 {
545 u32 seq_hi = clib_net_to_host_u32 (sa->seq_hi);
546 clib_memcpy_fast (tag, &seq_hi, sizeof (seq_hi));
547 integ_total_len += sizeof (seq_hi);
548 }
549
550out:
551 return vnet_crypto_async_add_to_frame (vm, async_frame, key_index,
552 crypto_total_len,
553 integ_total_len - crypto_total_len,
554 crypto_start_offset,
555 integ_start_offset, bi, async_next,
556 iv, tag, aad, flag);
557}
558
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200559always_inline uword
Neale Ranns4a58e492020-12-21 13:19:10 +0000560esp_encrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
561 vlib_frame_t *frame, vnet_link_t lt, int is_tun,
Fan Zhangf5395782020-04-29 14:00:03 +0100562 u16 async_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564 ipsec_main_t *im = &ipsec_main;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100565 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
566 u32 *from = vlib_frame_vector_args (frame);
567 u32 n_left = frame->n_vectors;
568 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Damjan Marionc98275f2019-03-06 14:05:01 +0100569 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
Damjan Mariond97918e2019-04-25 18:28:31 +0200570 esp_gcm_nonce_t nonces[VLIB_FRAME_SIZE], *nonce = nonces;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100571 u32 thread_index = vm->thread_index;
572 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
573 u32 current_sa_index = ~0, current_sa_packets = 0;
574 u32 current_sa_bytes = 0, spi = 0;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500575 u8 esp_align = 4, iv_sz = 0, icv_sz = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100576 ipsec_sa_t *sa0 = 0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000577 vlib_buffer_t *lb;
578 vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
579 vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
Fan Zhangf5395782020-04-29 14:00:03 +0100580 vnet_crypto_async_frame_t *async_frame = 0;
581 int is_async = im->async_mode;
582 vnet_crypto_async_op_id_t last_async_op = ~0;
Neale Ranns4a58e492020-12-21 13:19:10 +0000583 u16 drop_next =
584 (lt == VNET_LINK_IP6 ? ESP_ENCRYPT_NEXT_DROP6 :
585 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_DROP4 :
586 ESP_ENCRYPT_NEXT_DROP_MPLS));
587 u16 handoff_next = (lt == VNET_LINK_IP6 ?
588 ESP_ENCRYPT_NEXT_HANDOFF6 :
589 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_HANDOFF4 :
590 ESP_ENCRYPT_NEXT_HANDOFF_MPLS));
Fan Zhang18f0e312020-10-19 13:08:34 +0100591 u16 n_async_drop = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
Damjan Marionc59b9a22019-03-19 15:38:40 +0100593 vlib_get_buffers (vm, from, b, n_left);
Fan Zhangf5395782020-04-29 14:00:03 +0100594 if (!is_async)
595 {
596 vec_reset_length (ptd->crypto_ops);
597 vec_reset_length (ptd->integ_ops);
598 vec_reset_length (ptd->chained_crypto_ops);
599 vec_reset_length (ptd->chained_integ_ops);
600 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000601 vec_reset_length (ptd->chunks);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100602
603 while (n_left > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700604 {
Zhiyong Yang1cff6432019-04-30 05:33:53 -0400605 u32 sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100606 dpo_id_t *dpo;
607 esp_header_t *esp;
608 u8 *payload, *next_hdr_ptr;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000609 u16 payload_len, payload_len_total, n_bufs;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400610 u32 hdr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700611
Damjan Marionc59b9a22019-03-19 15:38:40 +0100612 if (n_left > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700613 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100614 u8 *p;
615 vlib_prefetch_buffer_header (b[2], LOAD);
616 p = vlib_buffer_get_current (b[1]);
617 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
618 p -= CLIB_CACHE_LINE_BYTES;
619 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000620 /* speculate that the trailer goes in the first buffer */
621 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]),
622 CLIB_CACHE_LINE_BYTES, LOAD);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700623 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624
Neale Ranns25edf142019-03-22 08:12:48 +0000625 if (is_tun)
626 {
627 /* we are on a ipsec tunnel's feature arc */
Neale Ranns28287212019-12-16 00:53:11 +0000628 vnet_buffer (b[0])->ipsec.sad_index =
629 sa_index0 = ipsec_tun_protect_get_sa_out
630 (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
Neale Ranns25edf142019-03-22 08:12:48 +0000631 }
632 else
633 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
634
635 if (sa_index0 != current_sa_index)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100636 {
Damjan Marion21f265f2019-06-05 15:45:50 +0200637 if (current_sa_packets)
638 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
639 current_sa_index,
640 current_sa_packets,
641 current_sa_bytes);
642 current_sa_packets = current_sa_bytes = 0;
643
Damjan Marionc59b9a22019-03-19 15:38:40 +0100644 sa0 = pool_elt_at_index (im->sad, sa_index0);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000645
646 /* fetch the second cacheline ASAP */
647 CLIB_PREFETCH (sa0->cacheline1, CLIB_CACHE_LINE_BYTES, LOAD);
648
Damjan Marionc59b9a22019-03-19 15:38:40 +0100649 current_sa_index = sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100650 spi = clib_net_to_host_u32 (sa0->spi);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500651 esp_align = sa0->esp_block_align;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200652 icv_sz = sa0->integ_icv_size;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100653 iv_sz = sa0->crypto_iv_size;
Fan Zhangf5395782020-04-29 14:00:03 +0100654
655 /* submit frame when op_id is different then the old one */
656 if (is_async && sa0->crypto_async_enc_op_id != last_async_op)
657 {
658 if (async_frame && async_frame->n_elts)
659 {
Fan Zhang18f0e312020-10-19 13:08:34 +0100660 if (vnet_crypto_async_submit_open_frame (vm, async_frame))
661 esp_async_recycle_failed_submit (async_frame, b, from,
662 nexts, &n_async_drop,
663 drop_next,
664 ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR);
Fan Zhangf5395782020-04-29 14:00:03 +0100665 }
666 async_frame =
667 vnet_crypto_async_get_frame (vm, sa0->crypto_async_enc_op_id);
668 last_async_op = sa0->crypto_async_enc_op_id;
669 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100670 }
671
Neale Rannsf62a8c02019-04-02 08:13:33 +0000672 if (PREDICT_FALSE (~0 == sa0->encrypt_thread_index))
673 {
674 /* this is the first packet to use this SA, claim the SA
675 * for this thread. this could happen simultaneously on
676 * another thread */
677 clib_atomic_cmp_and_swap (&sa0->encrypt_thread_index, ~0,
678 ipsec_sa_assign_thread (thread_index));
679 }
680
Fan Zhang153e41a2020-11-18 09:46:39 +0000681 if (PREDICT_FALSE (thread_index != sa0->encrypt_thread_index))
Neale Rannsf62a8c02019-04-02 08:13:33 +0000682 {
Fan Zhang18f0e312020-10-19 13:08:34 +0100683 esp_set_next_index (is_async, from, nexts, from[b - bufs],
Neale Ranns4a58e492020-12-21 13:19:10 +0000684 &n_async_drop, handoff_next, next);
Neale Rannsf62a8c02019-04-02 08:13:33 +0000685 goto trace;
686 }
687
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000688 lb = b[0];
689 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
690 if (n_bufs == 0)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100691 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000692 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_NO_BUFFERS];
Fan Zhang18f0e312020-10-19 13:08:34 +0100693 esp_set_next_index (is_async, from, nexts, from[b - bufs],
694 &n_async_drop, drop_next, next);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100695 goto trace;
696 }
697
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000698 if (n_bufs > 1)
699 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000700 /* find last buffer in the chain */
701 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
702 lb = vlib_get_buffer (vm, lb->next_buffer);
703 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000704
Damjan Marionc59b9a22019-03-19 15:38:40 +0100705 if (PREDICT_FALSE (esp_seq_advance (sa0)))
706 {
707 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED];
Fan Zhang18f0e312020-10-19 13:08:34 +0100708 esp_set_next_index (is_async, from, nexts, from[b - bufs],
709 &n_async_drop, drop_next, next);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100710 goto trace;
711 }
712
713 /* space for IV */
714 hdr_len = iv_sz;
715
Damjan Mariond709cbc2019-03-26 13:16:42 +0100716 if (ipsec_sa_is_set_IS_TUNNEL (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100717 {
718 payload = vlib_buffer_get_current (b[0]);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500719 next_hdr_ptr = esp_add_footer_and_icv (vm, &lb, esp_align, icv_sz,
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000720 next, node,
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000721 buffer_data_size,
722 vlib_buffer_length_in_chain
723 (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000724 if (!next_hdr_ptr)
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000725 {
726 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_NO_BUFFERS];
Fan Zhang18f0e312020-10-19 13:08:34 +0100727 esp_set_next_index (is_async, from, nexts, from[b - bufs],
728 &n_async_drop, drop_next, next);
Filip Tehlare4e8c6b2020-02-13 07:49:30 +0000729 goto trace;
730 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000731 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000732 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000733 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100734
735 /* ESP header */
736 hdr_len += sizeof (*esp);
737 esp = (esp_header_t *) (payload - hdr_len);
738
739 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100740 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100741 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100742 hdr_len += sizeof (udp_header_t);
743 esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000744 payload_len_total + hdr_len);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100745 }
746
747 /* IP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100748 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100749 {
750 ip6_header_t *ip6;
751 u16 len = sizeof (ip6_header_t);
752 hdr_len += len;
753 ip6 = (ip6_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000754 clib_memcpy_fast (ip6, &sa0->ip6_hdr, sizeof (ip6_header_t));
755
Neale Ranns4a58e492020-12-21 13:19:10 +0000756 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000757 {
758 *next_hdr_ptr = IP_PROTOCOL_IPV6;
759 tunnel_encap_fixup_6o6 (sa0->tunnel_flags,
760 (const ip6_header_t *) payload,
761 ip6);
762 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000763 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000764 {
765 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
766 tunnel_encap_fixup_4o6 (sa0->tunnel_flags,
767 (const ip4_header_t *) payload,
768 ip6);
769 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000770 else if (VNET_LINK_MPLS == lt)
771 {
772 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
773 tunnel_encap_fixup_mplso6 (
774 sa0->tunnel_flags, (const mpls_unicast_header_t *) payload,
775 ip6);
776 }
777 else
778 ASSERT (0);
779
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000780 len = payload_len_total + hdr_len - len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100781 ip6->payload_length = clib_net_to_host_u16 (len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100782 }
783 else
784 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100785 ip4_header_t *ip4;
786 u16 len = sizeof (ip4_header_t);
787 hdr_len += len;
788 ip4 = (ip4_header_t *) (payload - hdr_len);
Neale Ranns041add72020-01-02 04:06:10 +0000789 clib_memcpy_fast (ip4, &sa0->ip4_hdr, sizeof (ip4_header_t));
790
Neale Ranns4a58e492020-12-21 13:19:10 +0000791 if (VNET_LINK_IP6 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000792 {
793 *next_hdr_ptr = IP_PROTOCOL_IPV6;
794 tunnel_encap_fixup_6o4_w_chksum (sa0->tunnel_flags,
795 (const ip6_header_t *)
796 payload, ip4);
797 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000798 else if (VNET_LINK_IP4 == lt)
Neale Ranns041add72020-01-02 04:06:10 +0000799 {
800 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
801 tunnel_encap_fixup_4o4_w_chksum (sa0->tunnel_flags,
802 (const ip4_header_t *)
803 payload, ip4);
804 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000805 else if (VNET_LINK_MPLS == lt)
806 {
807 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
808 tunnel_encap_fixup_mplso4_w_chksum (
809 sa0->tunnel_flags, (const mpls_unicast_header_t *) payload,
810 ip4);
811 }
812 else
813 ASSERT (0);
814
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000815 len = payload_len_total + hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100816 esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100817 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100818
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000819 dpo = &sa0->dpo;
Neale Ranns25edf142019-03-22 08:12:48 +0000820 if (!is_tun)
821 {
822 next[0] = dpo->dpoi_next_node;
823 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
824 }
Neale Ranns4ec36c52020-03-31 09:21:29 -0400825 else
826 next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Damjan Marionc98275f2019-03-06 14:05:01 +0100827 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100828 else /* transport mode */
Damjan Marionc98275f2019-03-06 14:05:01 +0100829 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100830 u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
Neale Ranns02950402019-12-20 00:54:57 +0000831 ip6_ext_header_t *ext_hdr;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100832 udp_header_t *udp = 0;
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400833 u16 udp_len = 0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100834 u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
Damjan Marionc98275f2019-03-06 14:05:01 +0100835
Neale Ranns4a58e492020-12-21 13:19:10 +0000836 ip_len =
837 (VNET_LINK_IP6 == lt ?
838 esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
839 ip4_header_bytes ((ip4_header_t *) old_ip_hdr));
Damjan Marionc98275f2019-03-06 14:05:01 +0100840
Damjan Marionc59b9a22019-03-19 15:38:40 +0100841 vlib_buffer_advance (b[0], ip_len);
842 payload = vlib_buffer_get_current (b[0]);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500843 next_hdr_ptr = esp_add_footer_and_icv (vm, &lb, esp_align, icv_sz,
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000844 next, node,
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000845 buffer_data_size,
846 vlib_buffer_length_in_chain
847 (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000848 if (!next_hdr_ptr)
Fan Zhang18f0e312020-10-19 13:08:34 +0100849 {
850 esp_set_next_index (is_async, from, nexts, from[b - bufs],
851 &n_async_drop, drop_next, next);
852 goto trace;
853 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000854
855 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000856 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000857 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100858
859 /* ESP header */
860 hdr_len += sizeof (*esp);
861 esp = (esp_header_t *) (payload - hdr_len);
862
863 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100864 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100865 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100866 hdr_len += sizeof (udp_header_t);
867 udp = (udp_header_t *) (payload - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100868 }
869
Damjan Marionc59b9a22019-03-19 15:38:40 +0100870 /* IP header */
871 hdr_len += ip_len;
872 ip_hdr = payload - hdr_len;
873
874 /* L2 header */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800875 if (!is_tun)
876 {
877 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
878 hdr_len += l2_len;
879 l2_hdr = payload - hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100880
Neale Rannsc87b66c2019-02-07 07:26:12 -0800881 /* copy l2 and ip header */
882 clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
883 }
884 else
885 l2_len = 0;
886
Neale Ranns4a58e492020-12-21 13:19:10 +0000887 if (VNET_LINK_IP6 == lt)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100888 {
Neale Ranns02950402019-12-20 00:54:57 +0000889 ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
890 if (PREDICT_TRUE (NULL == ext_hdr))
891 {
892 *next_hdr_ptr = ip6->protocol;
893 ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
894 }
895 else
896 {
897 *next_hdr_ptr = ext_hdr->next_hdr;
898 ext_hdr->next_hdr = IP_PROTOCOL_IPSEC_ESP;
899 }
Neale Rannsd207fd72019-04-18 17:18:12 -0700900 ip6->payload_length =
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000901 clib_host_to_net_u16 (payload_len_total + hdr_len - l2_len -
Neale Ranns02950402019-12-20 00:54:57 +0000902 sizeof (ip6_header_t));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100903 }
Neale Ranns4a58e492020-12-21 13:19:10 +0000904 else if (VNET_LINK_IP4 == lt)
Damjan Marionc98275f2019-03-06 14:05:01 +0100905 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100906 u16 len;
Neale Ranns02950402019-12-20 00:54:57 +0000907 ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100908 *next_hdr_ptr = ip4->protocol;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000909 len = payload_len_total + hdr_len - l2_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100910 if (udp)
911 {
912 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400913 udp_len = len - ip_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100914 }
915 else
916 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100917 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100918
Neale Ranns02950402019-12-20 00:54:57 +0000919 clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
920
Alexander Chernavinb0d2eda2020-03-25 10:56:52 -0400921 if (udp)
922 {
923 esp_fill_udp_hdr (sa0, udp, udp_len);
924 }
925
Neale Ranns4ec36c52020-03-31 09:21:29 -0400926 next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Damjan Marionc98275f2019-03-06 14:05:01 +0100927 }
928
PiotrX Kleskifdca4dd2020-05-05 14:14:22 +0200929 if (lb != b[0])
930 {
931 crypto_ops = &ptd->chained_crypto_ops;
932 integ_ops = &ptd->chained_integ_ops;
933 }
934 else
935 {
936 crypto_ops = &ptd->crypto_ops;
937 integ_ops = &ptd->integ_ops;
938 }
939
Damjan Marionc59b9a22019-03-19 15:38:40 +0100940 esp->spi = spi;
941 esp->seq = clib_net_to_host_u32 (sa0->seq);
Damjan Marionc98275f2019-03-06 14:05:01 +0100942
Fan Zhangf5395782020-04-29 14:00:03 +0100943 if (is_async)
Damjan Marionc98275f2019-03-06 14:05:01 +0100944 {
Fan Zhangf5395782020-04-29 14:00:03 +0100945 if (PREDICT_FALSE (sa0->crypto_async_enc_op_id == 0))
Fan Zhang18f0e312020-10-19 13:08:34 +0100946 {
947 esp_set_next_index (is_async, from, nexts, from[b - bufs],
948 &n_async_drop, drop_next, next);
949 goto trace;
950 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000951
Fan Zhangf5395782020-04-29 14:00:03 +0100952 if (esp_prepare_async_frame (vm, ptd, &async_frame, sa0, b[0], esp,
953 payload, payload_len, iv_sz,
Fan Zhang18f0e312020-10-19 13:08:34 +0100954 icv_sz, from[b - bufs], next[0],
955 hdr_len, async_next, lb))
Neale Ranns47feb112019-04-11 15:14:07 +0000956 {
Fan Zhang18f0e312020-10-19 13:08:34 +0100957 /* The fail only caused by submission, free the whole frame. */
958 if (async_frame->n_elts)
959 esp_async_recycle_failed_submit (async_frame, b, from, nexts,
960 &n_async_drop, drop_next,
961 ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR);
962 b[0]->error = ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR;
963 esp_set_next_index (1, from, nexts, from[b - bufs],
964 &n_async_drop, drop_next, next);
Fan Zhangf5395782020-04-29 14:00:03 +0100965 goto trace;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000966 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100967 }
Fan Zhangf5395782020-04-29 14:00:03 +0100968 else
Damjan Marionc98275f2019-03-06 14:05:01 +0100969 {
Fan Zhangf5395782020-04-29 14:00:03 +0100970 esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, payload,
971 payload_len, iv_sz, icv_sz, bufs, b, lb,
972 hdr_len, esp, nonce++);
Damjan Marionc98275f2019-03-06 14:05:01 +0100973 }
974
Damjan Marionc59b9a22019-03-19 15:38:40 +0100975 vlib_buffer_advance (b[0], 0LL - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100976
Damjan Marionc59b9a22019-03-19 15:38:40 +0100977 current_sa_packets += 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000978 current_sa_bytes += payload_len_total;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100979
980 trace:
981 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
Damjan Marionc98275f2019-03-06 14:05:01 +0100982 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100983 esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
984 sizeof (*tr));
985 tr->sa_index = sa_index0;
986 tr->spi = sa0->spi;
Neale Ranns6afaae12019-07-17 15:07:14 +0000987 tr->seq = sa0->seq;
988 tr->sa_seq_hi = sa0->seq_hi;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100989 tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100990 tr->crypto_alg = sa0->crypto_alg;
991 tr->integ_alg = sa0->integ_alg;
Damjan Marionc98275f2019-03-06 14:05:01 +0100992 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100993 /* next */
Damjan Marionc59b9a22019-03-19 15:38:40 +0100994 n_left -= 1;
Damjan Marionc98275f2019-03-06 14:05:01 +0100995 next += 1;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100996 b += 1;
Damjan Marionc98275f2019-03-06 14:05:01 +0100997 }
998
Damjan Marionc59b9a22019-03-19 15:38:40 +0100999 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1000 current_sa_index, current_sa_packets,
1001 current_sa_bytes);
Fan Zhangf5395782020-04-29 14:00:03 +01001002 if (!is_async)
1003 {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001004 esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts, drop_next);
Fan Zhangf5395782020-04-29 14:00:03 +01001005 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, bufs, nexts,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001006 ptd->chunks, drop_next);
Filip Tehlarefcad1a2020-02-04 09:36:04 +00001007
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001008 esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts, drop_next);
Fan Zhangf5395782020-04-29 14:00:03 +01001009 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, bufs, nexts,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001010 ptd->chunks, drop_next);
Fan Zhangf5395782020-04-29 14:00:03 +01001011 }
Fan Zhang18f0e312020-10-19 13:08:34 +01001012 else
Fan Zhangf5395782020-04-29 14:00:03 +01001013 {
Fan Zhang18f0e312020-10-19 13:08:34 +01001014 if (async_frame && async_frame->n_elts)
1015 {
1016 if (vnet_crypto_async_submit_open_frame (vm, async_frame) < 0)
1017 esp_async_recycle_failed_submit (async_frame, b, from, nexts,
1018 &n_async_drop, drop_next,
1019 ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR);
1020 }
1021 vlib_node_increment_counter (vm, node->node_index,
1022 ESP_ENCRYPT_ERROR_RX_PKTS,
1023 frame->n_vectors);
1024 if (n_async_drop)
1025 vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_async_drop);
1026
1027 return frame->n_vectors;
Fan Zhangf5395782020-04-29 14:00:03 +01001028 }
Damjan Marionc59b9a22019-03-19 15:38:40 +01001029
1030 vlib_node_increment_counter (vm, node->node_index,
1031 ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors);
1032
1033 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1034 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035}
1036
Fan Zhangf5395782020-04-29 14:00:03 +01001037always_inline uword
1038esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1039 vlib_frame_t * frame)
1040{
1041 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1042 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1043 u32 *from = vlib_frame_vector_args (frame);
1044 u32 n_left = frame->n_vectors;
1045
1046 vlib_get_buffers (vm, from, b, n_left);
1047
1048 if (n_left >= 4)
1049 {
1050 vlib_prefetch_buffer_header (b[0], LOAD);
1051 vlib_prefetch_buffer_header (b[1], LOAD);
1052 vlib_prefetch_buffer_header (b[2], LOAD);
1053 vlib_prefetch_buffer_header (b[3], LOAD);
1054 }
1055
1056 while (n_left > 8)
1057 {
1058 vlib_prefetch_buffer_header (b[4], LOAD);
1059 vlib_prefetch_buffer_header (b[5], LOAD);
1060 vlib_prefetch_buffer_header (b[6], LOAD);
1061 vlib_prefetch_buffer_header (b[7], LOAD);
1062
1063 next[0] = (esp_post_data (b[0]))->next_index;
1064 next[1] = (esp_post_data (b[1]))->next_index;
1065 next[2] = (esp_post_data (b[2]))->next_index;
1066 next[3] = (esp_post_data (b[3]))->next_index;
1067
1068 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
1069 {
1070 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
1071 {
1072 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1073 sizeof (*tr));
1074 tr->next_index = next[0];
1075 }
1076 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
1077 {
1078 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1],
1079 sizeof (*tr));
1080 tr->next_index = next[1];
1081 }
1082 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
1083 {
1084 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2],
1085 sizeof (*tr));
1086 tr->next_index = next[2];
1087 }
1088 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
1089 {
1090 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3],
1091 sizeof (*tr));
1092 tr->next_index = next[3];
1093 }
1094 }
1095
1096 b += 4;
1097 next += 4;
1098 n_left -= 4;
1099 }
1100
1101 while (n_left > 0)
1102 {
1103 next[0] = (esp_post_data (b[0]))->next_index;
1104 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1105 {
1106 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1107 sizeof (*tr));
1108 tr->next_index = next[0];
1109 }
1110
1111 b += 1;
1112 next += 1;
1113 n_left -= 1;
1114 }
1115
1116 vlib_node_increment_counter (vm, node->node_index,
1117 ESP_ENCRYPT_ERROR_POST_RX_PKTS,
1118 frame->n_vectors);
1119 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1120 return frame->n_vectors;
1121}
1122
Klement Sekerab8f35442018-10-29 13:38:19 +01001123VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
1124 vlib_node_runtime_t * node,
1125 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001126{
Neale Ranns4a58e492020-12-21 13:19:10 +00001127 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001128 esp_encrypt_async_next.esp4_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001129}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001131/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001132VLIB_REGISTER_NODE (esp4_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001133 .name = "esp4-encrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001134 .vector_size = sizeof (u32),
1135 .format_trace = format_esp_encrypt_trace,
1136 .type = VLIB_NODE_TYPE_INTERNAL,
1137
Neale Ranns4a58e492020-12-21 13:19:10 +00001138 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001139 .error_strings = esp_encrypt_error_strings,
1140
1141 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Ranns4a58e492020-12-21 13:19:10 +00001142 .next_nodes = { [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1143 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1144 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1145 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-handoff",
1146 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-handoff",
1147 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "error-drop",
1148 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output" },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001149};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001150/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151
Fan Zhangf5395782020-04-29 14:00:03 +01001152VLIB_NODE_FN (esp4_encrypt_post_node) (vlib_main_t * vm,
1153 vlib_node_runtime_t * node,
1154 vlib_frame_t * from_frame)
1155{
1156 return esp_encrypt_post_inline (vm, node, from_frame);
1157}
1158
1159/* *INDENT-OFF* */
1160VLIB_REGISTER_NODE (esp4_encrypt_post_node) = {
1161 .name = "esp4-encrypt-post",
1162 .vector_size = sizeof (u32),
1163 .format_trace = format_esp_post_encrypt_trace,
1164 .type = VLIB_NODE_TYPE_INTERNAL,
1165 .sibling_of = "esp4-encrypt",
1166
1167 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1168 .error_strings = esp_encrypt_error_strings,
1169};
1170/* *INDENT-ON* */
1171
Klement Sekerab8f35442018-10-29 13:38:19 +01001172VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
1173 vlib_node_runtime_t * node,
1174 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001175{
Neale Ranns4a58e492020-12-21 13:19:10 +00001176 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 0,
Fan Zhangf5395782020-04-29 14:00:03 +01001177 esp_encrypt_async_next.esp6_post_next);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001178}
1179
1180/* *INDENT-OFF* */
1181VLIB_REGISTER_NODE (esp6_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001182 .name = "esp6-encrypt",
1183 .vector_size = sizeof (u32),
1184 .format_trace = format_esp_encrypt_trace,
1185 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001186 .sibling_of = "esp4-encrypt",
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001187
1188 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1189 .error_strings = esp_encrypt_error_strings,
Klement Sekerabe5a5dd2018-10-09 16:05:48 +02001190};
1191/* *INDENT-ON* */
1192
Fan Zhangf5395782020-04-29 14:00:03 +01001193VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm,
1194 vlib_node_runtime_t * node,
1195 vlib_frame_t * from_frame)
1196{
1197 return esp_encrypt_post_inline (vm, node, from_frame);
1198}
1199
1200/* *INDENT-OFF* */
1201VLIB_REGISTER_NODE (esp6_encrypt_post_node) = {
1202 .name = "esp6-encrypt-post",
1203 .vector_size = sizeof (u32),
1204 .format_trace = format_esp_post_encrypt_trace,
1205 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001206 .sibling_of = "esp4-encrypt",
Fan Zhangf5395782020-04-29 14:00:03 +01001207
1208 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1209 .error_strings = esp_encrypt_error_strings,
1210};
1211/* *INDENT-ON* */
1212
Neale Ranns25edf142019-03-22 08:12:48 +00001213VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
1214 vlib_node_runtime_t * node,
1215 vlib_frame_t * from_frame)
1216{
Neale Ranns4a58e492020-12-21 13:19:10 +00001217 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001218 esp_encrypt_async_next.esp4_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001219}
1220
1221/* *INDENT-OFF* */
1222VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
1223 .name = "esp4-encrypt-tun",
1224 .vector_size = sizeof (u32),
1225 .format_trace = format_esp_encrypt_trace,
1226 .type = VLIB_NODE_TYPE_INTERNAL,
1227
1228 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1229 .error_strings = esp_encrypt_error_strings,
Neale Rannsd7603d92019-03-28 08:56:10 +00001230
Neale Rannsf62a8c02019-04-02 08:13:33 +00001231 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001232 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001233 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1234 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001235 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001236 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001237 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1238 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001239 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001240 },
Neale Ranns25edf142019-03-22 08:12:48 +00001241};
1242
Fan Zhangf5395782020-04-29 14:00:03 +01001243VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm,
1244 vlib_node_runtime_t * node,
1245 vlib_frame_t * from_frame)
1246{
1247 return esp_encrypt_post_inline (vm, node, from_frame);
1248}
1249
1250/* *INDENT-OFF* */
1251VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = {
1252 .name = "esp4-encrypt-tun-post",
1253 .vector_size = sizeof (u32),
1254 .format_trace = format_esp_post_encrypt_trace,
1255 .type = VLIB_NODE_TYPE_INTERNAL,
1256 .sibling_of = "esp4-encrypt-tun",
1257
1258 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1259 .error_strings = esp_encrypt_error_strings,
1260};
Neale Ranns25edf142019-03-22 08:12:48 +00001261/* *INDENT-ON* */
1262
1263VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
1264 vlib_node_runtime_t * node,
1265 vlib_frame_t * from_frame)
1266{
Neale Ranns4a58e492020-12-21 13:19:10 +00001267 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 1,
Fan Zhangf5395782020-04-29 14:00:03 +01001268 esp_encrypt_async_next.esp6_tun_post_next);
Neale Ranns25edf142019-03-22 08:12:48 +00001269}
1270
1271/* *INDENT-OFF* */
1272VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
1273 .name = "esp6-encrypt-tun",
1274 .vector_size = sizeof (u32),
1275 .format_trace = format_esp_encrypt_trace,
1276 .type = VLIB_NODE_TYPE_INTERNAL,
1277
1278 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1279 .error_strings = esp_encrypt_error_strings,
Neale Rannsd7603d92019-03-28 08:56:10 +00001280
Neale Rannsf62a8c02019-04-02 08:13:33 +00001281 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +00001282 .next_nodes = {
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001283 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1284 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
Neale Ranns4a58e492020-12-21 13:19:10 +00001285 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1286 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
Neale Rannsb1fd80f2020-05-12 13:33:56 +00001287 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
Neale Ranns4a58e492020-12-21 13:19:10 +00001288 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
Neale Ranns4ec36c52020-03-31 09:21:29 -04001289 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
Neale Rannsd7603d92019-03-28 08:56:10 +00001290 },
Neale Ranns25edf142019-03-22 08:12:48 +00001291};
1292
Neale Ranns25edf142019-03-22 08:12:48 +00001293/* *INDENT-ON* */
1294
Fan Zhangf5395782020-04-29 14:00:03 +01001295VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm,
1296 vlib_node_runtime_t * node,
1297 vlib_frame_t * from_frame)
1298{
1299 return esp_encrypt_post_inline (vm, node, from_frame);
1300}
1301
1302/* *INDENT-OFF* */
1303VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = {
1304 .name = "esp6-encrypt-tun-post",
1305 .vector_size = sizeof (u32),
1306 .format_trace = format_esp_post_encrypt_trace,
1307 .type = VLIB_NODE_TYPE_INTERNAL,
Neale Ranns4a58e492020-12-21 13:19:10 +00001308 .sibling_of = "esp-mpls-encrypt-tun",
Fan Zhangf5395782020-04-29 14:00:03 +01001309
Neale Ranns4a58e492020-12-21 13:19:10 +00001310 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
Fan Zhangf5395782020-04-29 14:00:03 +01001311 .error_strings = esp_encrypt_error_strings,
1312};
1313/* *INDENT-ON* */
1314
Neale Ranns4a58e492020-12-21 13:19:10 +00001315VLIB_NODE_FN (esp_mpls_encrypt_tun_node)
1316(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1317{
1318 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_MPLS, 1,
1319 esp_encrypt_async_next.esp_mpls_tun_post_next);
1320}
1321
1322VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_node) = {
1323 .name = "esp-mpls-encrypt-tun",
1324 .vector_size = sizeof (u32),
1325 .format_trace = format_esp_encrypt_trace,
1326 .type = VLIB_NODE_TYPE_INTERNAL,
1327
1328 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1329 .error_strings = esp_encrypt_error_strings,
1330
1331 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1332 .next_nodes = {
1333 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1334 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1335 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1336 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1337 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1338 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1339 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1340 },
1341};
1342
1343VLIB_NODE_FN (esp_mpls_encrypt_tun_post_node)
1344(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1345{
1346 return esp_encrypt_post_inline (vm, node, from_frame);
1347}
1348
1349VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_post_node) = {
1350 .name = "esp-mpls-encrypt-tun-post",
1351 .vector_size = sizeof (u32),
1352 .format_trace = format_esp_post_encrypt_trace,
1353 .type = VLIB_NODE_TYPE_INTERNAL,
1354 .sibling_of = "esp-mpls-encrypt-tun",
1355
1356 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1357 .error_strings = esp_encrypt_error_strings,
1358};
1359
Matthew Smith401aedf2019-07-08 14:45:04 -05001360typedef struct
1361{
1362 u32 sa_index;
1363} esp_no_crypto_trace_t;
1364
1365static u8 *
1366format_esp_no_crypto_trace (u8 * s, va_list * args)
1367{
1368 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1369 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1370 esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *);
1371
1372 s = format (s, "esp-no-crypto: sa-index %u", t->sa_index);
1373
1374 return s;
1375}
1376
1377enum
1378{
1379 ESP_NO_CRYPTO_NEXT_DROP,
1380 ESP_NO_CRYPTO_N_NEXT,
1381};
1382
1383enum
1384{
1385 ESP_NO_CRYPTO_ERROR_RX_PKTS,
1386};
1387
1388static char *esp_no_crypto_error_strings[] = {
1389 "Outbound ESP packets received",
1390};
1391
1392always_inline uword
1393esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1394 vlib_frame_t * frame)
1395{
1396 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Matthew Smith401aedf2019-07-08 14:45:04 -05001397 u32 *from = vlib_frame_vector_args (frame);
1398 u32 n_left = frame->n_vectors;
1399
1400 vlib_get_buffers (vm, from, b, n_left);
1401
1402 while (n_left > 0)
1403 {
Matthew Smith401aedf2019-07-08 14:45:04 -05001404 u32 sa_index0;
1405
1406 /* packets are always going to be dropped, but get the sa_index */
Neale Ranns4ec36c52020-03-31 09:21:29 -04001407 sa_index0 = ipsec_tun_protect_get_sa_out
1408 (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
Matthew Smith401aedf2019-07-08 14:45:04 -05001409
1410 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1411 {
1412 esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0],
1413 sizeof (*tr));
1414 tr->sa_index = sa_index0;
1415 }
1416
1417 n_left -= 1;
Matthew Smith401aedf2019-07-08 14:45:04 -05001418 b += 1;
1419 }
1420
1421 vlib_node_increment_counter (vm, node->node_index,
1422 ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors);
1423
Neale Ranns4ec36c52020-03-31 09:21:29 -04001424 vlib_buffer_enqueue_to_single_next (vm, node, from,
1425 ESP_NO_CRYPTO_NEXT_DROP,
1426 frame->n_vectors);
Matthew Smith401aedf2019-07-08 14:45:04 -05001427
1428 return frame->n_vectors;
1429}
1430
1431VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm,
1432 vlib_node_runtime_t * node,
1433 vlib_frame_t * from_frame)
1434{
1435 return esp_no_crypto_inline (vm, node, from_frame);
1436}
1437
1438/* *INDENT-OFF* */
1439VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) =
1440{
1441 .name = "esp4-no-crypto",
1442 .vector_size = sizeof (u32),
1443 .format_trace = format_esp_no_crypto_trace,
1444 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
1445 .error_strings = esp_no_crypto_error_strings,
1446 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
1447 .next_nodes = {
1448 [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop",
1449 },
1450};
1451
Matthew Smith401aedf2019-07-08 14:45:04 -05001452VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm,
1453 vlib_node_runtime_t * node,
1454 vlib_frame_t * from_frame)
1455{
1456 return esp_no_crypto_inline (vm, node, from_frame);
1457}
1458
1459/* *INDENT-OFF* */
1460VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) =
1461{
1462 .name = "esp6-no-crypto",
1463 .vector_size = sizeof (u32),
1464 .format_trace = format_esp_no_crypto_trace,
1465 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
1466 .error_strings = esp_no_crypto_error_strings,
1467 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
1468 .next_nodes = {
1469 [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop",
1470 },
1471};
Matthew Smith401aedf2019-07-08 14:45:04 -05001472/* *INDENT-ON* */
1473
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001474/*
1475 * fd.io coding-style-patch-verification: ON
1476 *
1477 * Local Variables:
1478 * eval: (c-set-style "gnu")
1479 * End:
1480 */