blob: 2c4da5dafa83804fb9ba112bb2fba5c00ad485d7 [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>
Klement Sekera4b089f22018-04-17 18:04:57 +020021#include <vnet/udp/udp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
Damjan Marion91f17dc2019-03-18 18:59:25 +010023#include <vnet/crypto/crypto.h>
24
Ed Warnickecb9cada2015-12-08 15:45:58 -070025#include <vnet/ipsec/ipsec.h>
26#include <vnet/ipsec/esp.h>
27
Ed Warnickecb9cada2015-12-08 15:45:58 -070028#define foreach_esp_encrypt_next \
29_(DROP, "error-drop") \
Neale Rannsf62a8c02019-04-02 08:13:33 +000030_(HANDOFF, "handoff") \
Ed Warnickecb9cada2015-12-08 15:45:58 -070031_(INTERFACE_OUTPUT, "interface-output")
32
33#define _(v, s) ESP_ENCRYPT_NEXT_##v,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070034typedef enum
35{
Ed Warnickecb9cada2015-12-08 15:45:58 -070036 foreach_esp_encrypt_next
37#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070038 ESP_ENCRYPT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070039} esp_encrypt_next_t;
40
Damjan Marionc59b9a22019-03-19 15:38:40 +010041#define foreach_esp_encrypt_error \
42 _(RX_PKTS, "ESP pkts received") \
43 _(SEQ_CYCLED, "sequence number cycled (packet dropped)") \
44 _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
Filip Tehlarefcad1a2020-02-04 09:36:04 +000045 _(NO_BUFFERS, "no buffers (packet dropped)") \
Damjan Marionc59b9a22019-03-19 15:38:40 +010046 _(NO_TRAILER_SPACE, "no trailer space (packet dropped)")
Ed Warnickecb9cada2015-12-08 15:45:58 -070047
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070048typedef enum
49{
Ed Warnickecb9cada2015-12-08 15:45:58 -070050#define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
51 foreach_esp_encrypt_error
52#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070053 ESP_ENCRYPT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070054} esp_encrypt_error_t;
55
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070056static char *esp_encrypt_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070057#define _(sym,string) string,
58 foreach_esp_encrypt_error
59#undef _
60};
61
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070062typedef struct
63{
Neale Ranns8d7c5022019-02-06 01:41:05 -080064 u32 sa_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070065 u32 spi;
66 u32 seq;
Neale Ranns6afaae12019-07-17 15:07:14 +000067 u32 sa_seq_hi;
Klement Sekera4b089f22018-04-17 18:04:57 +020068 u8 udp_encap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 ipsec_crypto_alg_t crypto_alg;
70 ipsec_integ_alg_t integ_alg;
71} esp_encrypt_trace_t;
72
73/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070074static u8 *
75format_esp_encrypt_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070076{
77 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
78 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070079 esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020081 s =
82 format (s,
Neale Ranns6afaae12019-07-17 15:07:14 +000083 "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
84 t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
85 format_ipsec_crypto_alg,
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020086 t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
87 t->udp_encap ? " udp-encap-enabled" : "");
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 return s;
89}
90
Damjan Marionc59b9a22019-03-19 15:38:40 +010091/* pad packet in input buffer */
92static_always_inline u8 *
Filip Tehlar8cdb1a02019-11-18 22:21:37 +000093esp_add_footer_and_icv (vlib_buffer_t * b, u8 block_size, u8 icv_sz,
94 u16 * next, vlib_node_runtime_t * node,
Filip Tehlarefcad1a2020-02-04 09:36:04 +000095 u16 buffer_data_size, uword total_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -070096{
Damjan Marionc59b9a22019-03-19 15:38:40 +010097 static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = {
98 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
99 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x00, 0x00,
100 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000102 u16 min_length = total_len + sizeof (esp_footer_t);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100103 u16 new_length = round_pow2 (min_length, block_size);
104 u8 pad_bytes = new_length - min_length;
105 esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (b) +
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000106 b->current_length + pad_bytes);
107 u16 tail_sz = sizeof (esp_footer_t) + pad_bytes + icv_sz;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000109 if (b->current_data + tail_sz > buffer_data_size)
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000110 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000111 // TODO alloc new buffer
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000112 b->error = node->errors[ESP_ENCRYPT_ERROR_NO_TRAILER_SPACE];
113 next[0] = ESP_ENCRYPT_NEXT_DROP;
114 return 0;
115 }
116
Damjan Marionc59b9a22019-03-19 15:38:40 +0100117 if (pad_bytes)
Benoît Ganne4505f012019-12-07 09:14:27 -0700118 {
119 ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE);
120 pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes);
121 clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes);
122 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100123
124 f->pad_length = pad_bytes;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000125 b->current_length += tail_sz;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100126 return &f->next_header;
127}
128
129static_always_inline void
130esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp)
131{
Neale Ranns1b582b82019-04-18 19:49:13 -0700132 ip_csum_t sum;
133 u16 old_len;
134
135 len = clib_net_to_host_u16 (len);
136 old_len = ip4->length;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100137
138 if (is_transport)
139 {
140 u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100141
Neale Ranns1b582b82019-04-18 19:49:13 -0700142 sum = ip_csum_update (ip4->checksum, ip4->protocol,
143 prot, ip4_header_t, protocol);
144 ip4->protocol = prot;
145
146 sum = ip_csum_update (sum, old_len, len, ip4_header_t, length);
147 }
148 else
149 sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
150
151 ip4->length = len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100152 ip4->checksum = ip_csum_fold (sum);
153}
154
155static_always_inline void
156esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
157{
158 clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
159 udp->length = clib_net_to_host_u16 (len);
160}
161
162static_always_inline u8
163ext_hdr_is_pre_esp (u8 nexthdr)
164{
165#ifdef CLIB_HAVE_VEC128
166 static const u8x16 ext_hdr_types = {
167 IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
168 IP_PROTOCOL_IPV6_ROUTE,
169 IP_PROTOCOL_IPV6_FRAGMENTATION,
170 };
171
172 return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
173#else
174 return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
175 (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) |
176 (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0);
177#endif
178}
179
180static_always_inline u8
Neale Ranns02950402019-12-20 00:54:57 +0000181esp_get_ip6_hdr_len (ip6_header_t * ip6, ip6_ext_header_t ** ext_hdr)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100182{
183 /* this code assumes that HbH, route and frag headers will be before
184 others, if that is not the case, they will end up encrypted */
Damjan Marionc59b9a22019-03-19 15:38:40 +0100185 u8 len = sizeof (ip6_header_t);
186 ip6_ext_header_t *p;
187
188 /* if next packet doesn't have ext header */
189 if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
Neale Ranns02950402019-12-20 00:54:57 +0000190 {
191 *ext_hdr = NULL;
192 return len;
193 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100194
195 p = (void *) (ip6 + 1);
196 len += ip6_ext_header_len (p);
197
198 while (ext_hdr_is_pre_esp (p->next_hdr))
199 {
200 len += ip6_ext_header_len (p);
201 p = ip6_ext_next_header (p);
202 }
203
Neale Ranns02950402019-12-20 00:54:57 +0000204 *ext_hdr = p;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100205 return len;
206}
207
Damjan Marionc59b9a22019-03-19 15:38:40 +0100208static_always_inline void
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000209esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
210 vnet_crypto_op_t * ops, vlib_buffer_t * b[],
211 u16 * nexts, vnet_crypto_op_chunk_t * chunks)
212{
213 u32 n_fail, n_ops = vec_len (ops);
214 vnet_crypto_op_t *op = ops;
215
216 if (n_ops == 0)
217 return;
218
219 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
220
221 while (n_fail)
222 {
223 ASSERT (op - ops < n_ops);
224
225 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
226 {
227 u32 bi = op->user_data;
228 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
229 nexts[bi] = ESP_ENCRYPT_NEXT_DROP;
230 n_fail--;
231 }
232 op++;
233 }
234}
235
236static_always_inline void
Damjan Marionc59b9a22019-03-19 15:38:40 +0100237esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
238 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts)
239{
240 u32 n_fail, n_ops = vec_len (ops);
241 vnet_crypto_op_t *op = ops;
242
243 if (n_ops == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 return;
245
Damjan Marionc59b9a22019-03-19 15:38:40 +0100246 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Damjan Marionc59b9a22019-03-19 15:38:40 +0100248 while (n_fail)
249 {
250 ASSERT (op - ops < n_ops);
251
252 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
253 {
254 u32 bi = op->user_data;
255 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
256 nexts[bi] = ESP_ENCRYPT_NEXT_DROP;
257 n_fail--;
258 }
259 op++;
260 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261}
262
Damjan Mariond97918e2019-04-25 18:28:31 +0200263typedef struct
264{
265 u32 salt;
266 u64 iv;
267} __clib_packed esp_gcm_nonce_t;
268
269STATIC_ASSERT_SIZEOF (esp_gcm_nonce_t, 12);
270
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200271always_inline uword
Damjan Marionc59b9a22019-03-19 15:38:40 +0100272esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
Neale Ranns25edf142019-03-22 08:12:48 +0000273 vlib_frame_t * frame, int is_ip6, int is_tun)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275 ipsec_main_t *im = &ipsec_main;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100276 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
277 u32 *from = vlib_frame_vector_args (frame);
278 u32 n_left = frame->n_vectors;
279 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Damjan Marionc98275f2019-03-06 14:05:01 +0100280 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
Damjan Mariond97918e2019-04-25 18:28:31 +0200281 esp_gcm_nonce_t nonces[VLIB_FRAME_SIZE], *nonce = nonces;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100282 u32 thread_index = vm->thread_index;
283 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
284 u32 current_sa_index = ~0, current_sa_packets = 0;
285 u32 current_sa_bytes = 0, spi = 0;
286 u8 block_sz = 0, iv_sz = 0, icv_sz = 0;
287 ipsec_sa_t *sa0 = 0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000288 vnet_crypto_op_chunk_t *ch;
289 vlib_buffer_t *lb;
290 vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
291 vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292
Damjan Marionc59b9a22019-03-19 15:38:40 +0100293 vlib_get_buffers (vm, from, b, n_left);
294 vec_reset_length (ptd->crypto_ops);
295 vec_reset_length (ptd->integ_ops);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000296 vec_reset_length (ptd->chained_crypto_ops);
297 vec_reset_length (ptd->chained_integ_ops);
298 vec_reset_length (ptd->chunks);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100299
300 while (n_left > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700301 {
Zhiyong Yang1cff6432019-04-30 05:33:53 -0400302 u32 sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100303 dpo_id_t *dpo;
304 esp_header_t *esp;
305 u8 *payload, *next_hdr_ptr;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000306 u16 payload_len, payload_len_total, n_bufs;
Neale Rannsf62a8c02019-04-02 08:13:33 +0000307 u32 hdr_len, config_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
Damjan Marionc59b9a22019-03-19 15:38:40 +0100309 if (n_left > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700310 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100311 u8 *p;
312 vlib_prefetch_buffer_header (b[2], LOAD);
313 p = vlib_buffer_get_current (b[1]);
314 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
315 p -= CLIB_CACHE_LINE_BYTES;
316 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700317 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318
Neale Ranns25edf142019-03-22 08:12:48 +0000319 if (is_tun)
320 {
321 /* we are on a ipsec tunnel's feature arc */
322 u32 next0;
Neale Rannsf62a8c02019-04-02 08:13:33 +0000323 config_index = b[0]->current_config_index;
Neale Ranns25edf142019-03-22 08:12:48 +0000324 sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0],
325 sizeof
326 (sa_index0));
Neale Rannsf62a8c02019-04-02 08:13:33 +0000327 vnet_buffer (b[0])->ipsec.sad_index = sa_index0;
Neale Ranns25edf142019-03-22 08:12:48 +0000328 next[0] = next0;
329 }
330 else
331 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
332
333 if (sa_index0 != current_sa_index)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100334 {
Damjan Marion21f265f2019-06-05 15:45:50 +0200335 if (current_sa_packets)
336 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
337 current_sa_index,
338 current_sa_packets,
339 current_sa_bytes);
340 current_sa_packets = current_sa_bytes = 0;
341
Damjan Marionc59b9a22019-03-19 15:38:40 +0100342 sa0 = pool_elt_at_index (im->sad, sa_index0);
343 current_sa_index = sa_index0;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100344 spi = clib_net_to_host_u32 (sa0->spi);
345 block_sz = sa0->crypto_block_size;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200346 icv_sz = sa0->integ_icv_size;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100347 iv_sz = sa0->crypto_iv_size;
348 }
349
Neale Rannsf62a8c02019-04-02 08:13:33 +0000350 if (PREDICT_FALSE (~0 == sa0->encrypt_thread_index))
351 {
352 /* this is the first packet to use this SA, claim the SA
353 * for this thread. this could happen simultaneously on
354 * another thread */
355 clib_atomic_cmp_and_swap (&sa0->encrypt_thread_index, ~0,
356 ipsec_sa_assign_thread (thread_index));
357 }
358
359 if (PREDICT_TRUE (thread_index != sa0->encrypt_thread_index))
360 {
361 next[0] = ESP_ENCRYPT_NEXT_HANDOFF;
362 if (is_tun)
363 {
364 b[0]->current_config_index = config_index;
365 }
366 goto trace;
367 }
368
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000369 lb = b[0];
370 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
371 if (n_bufs == 0)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100372 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000373 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_NO_BUFFERS];
Damjan Marionc59b9a22019-03-19 15:38:40 +0100374 next[0] = ESP_ENCRYPT_NEXT_DROP;
375 goto trace;
376 }
377
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000378 if (n_bufs > 1)
379 {
380 crypto_ops = &ptd->chained_crypto_ops;
381 integ_ops = &ptd->chained_integ_ops;
382
383 /* find last buffer in the chain */
384 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
385 lb = vlib_get_buffer (vm, lb->next_buffer);
386 }
387 else
388 {
389 crypto_ops = &ptd->crypto_ops;
390 integ_ops = &ptd->integ_ops;
391 }
392
Damjan Marionc59b9a22019-03-19 15:38:40 +0100393 if (PREDICT_FALSE (esp_seq_advance (sa0)))
394 {
395 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED];
396 next[0] = ESP_ENCRYPT_NEXT_DROP;
397 goto trace;
398 }
399
400 /* space for IV */
401 hdr_len = iv_sz;
402
Damjan Mariond709cbc2019-03-26 13:16:42 +0100403 if (ipsec_sa_is_set_IS_TUNNEL (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100404 {
405 payload = vlib_buffer_get_current (b[0]);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000406 next_hdr_ptr = esp_add_footer_and_icv (lb, block_sz, icv_sz,
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000407 next, node,
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000408 buffer_data_size,
409 vlib_buffer_length_in_chain
410 (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000411 if (!next_hdr_ptr)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100412 goto trace;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000413 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000414 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000415 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100416
417 /* ESP header */
418 hdr_len += sizeof (*esp);
419 esp = (esp_header_t *) (payload - hdr_len);
420
421 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100422 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100423 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100424 hdr_len += sizeof (udp_header_t);
425 esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000426 payload_len_total + hdr_len);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100427 }
428
429 /* IP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100430 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100431 {
432 ip6_header_t *ip6;
433 u16 len = sizeof (ip6_header_t);
434 hdr_len += len;
435 ip6 = (ip6_header_t *) (payload - hdr_len);
436 clib_memcpy_fast (ip6, &sa0->ip6_hdr, len);
Neale Ranns987aea82019-03-27 13:40:35 +0000437 *next_hdr_ptr = (is_ip6 ?
438 IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000439 len = payload_len_total + hdr_len - len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100440 ip6->payload_length = clib_net_to_host_u16 (len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100441 }
442 else
443 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100444 ip4_header_t *ip4;
445 u16 len = sizeof (ip4_header_t);
446 hdr_len += len;
447 ip4 = (ip4_header_t *) (payload - hdr_len);
448 clib_memcpy_fast (ip4, &sa0->ip4_hdr, len);
Neale Ranns987aea82019-03-27 13:40:35 +0000449 *next_hdr_ptr = (is_ip6 ?
450 IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000451 len = payload_len_total + hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100452 esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100453 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100454
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000455 dpo = &sa0->dpo;
Neale Ranns25edf142019-03-22 08:12:48 +0000456 if (!is_tun)
457 {
458 next[0] = dpo->dpoi_next_node;
459 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
460 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100461 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100462 else /* transport mode */
Damjan Marionc98275f2019-03-06 14:05:01 +0100463 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100464 u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
Neale Ranns02950402019-12-20 00:54:57 +0000465 ip6_ext_header_t *ext_hdr;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100466 udp_header_t *udp = 0;
467 u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
Damjan Marionc98275f2019-03-06 14:05:01 +0100468
Damjan Marionc59b9a22019-03-19 15:38:40 +0100469 ip_len = is_ip6 ?
Neale Ranns02950402019-12-20 00:54:57 +0000470 esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
Damjan Marionc59b9a22019-03-19 15:38:40 +0100471 ip4_header_bytes ((ip4_header_t *) old_ip_hdr);
Damjan Marionc98275f2019-03-06 14:05:01 +0100472
Damjan Marionc59b9a22019-03-19 15:38:40 +0100473 vlib_buffer_advance (b[0], ip_len);
474 payload = vlib_buffer_get_current (b[0]);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000475 next_hdr_ptr = esp_add_footer_and_icv (lb, block_sz, icv_sz,
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000476 next, node,
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000477 buffer_data_size,
478 vlib_buffer_length_in_chain
479 (vm, b[0]));
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000480 if (!next_hdr_ptr)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100481 goto trace;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000482
483 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
Filip Tehlar8cdb1a02019-11-18 22:21:37 +0000484 payload_len = b[0]->current_length;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000485 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100486
487 /* ESP header */
488 hdr_len += sizeof (*esp);
489 esp = (esp_header_t *) (payload - hdr_len);
490
491 /* optional UDP header */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100492 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100493 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100494 hdr_len += sizeof (udp_header_t);
495 udp = (udp_header_t *) (payload - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100496 }
497
Damjan Marionc59b9a22019-03-19 15:38:40 +0100498 /* IP header */
499 hdr_len += ip_len;
500 ip_hdr = payload - hdr_len;
501
502 /* L2 header */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800503 if (!is_tun)
504 {
505 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
506 hdr_len += l2_len;
507 l2_hdr = payload - hdr_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100508
Neale Rannsc87b66c2019-02-07 07:26:12 -0800509 /* copy l2 and ip header */
510 clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
511 }
512 else
513 l2_len = 0;
514
Damjan Marionc98275f2019-03-06 14:05:01 +0100515 if (is_ip6)
Damjan Marionc59b9a22019-03-19 15:38:40 +0100516 {
Neale Ranns02950402019-12-20 00:54:57 +0000517 ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
518 if (PREDICT_TRUE (NULL == ext_hdr))
519 {
520 *next_hdr_ptr = ip6->protocol;
521 ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
522 }
523 else
524 {
525 *next_hdr_ptr = ext_hdr->next_hdr;
526 ext_hdr->next_hdr = IP_PROTOCOL_IPSEC_ESP;
527 }
Neale Rannsd207fd72019-04-18 17:18:12 -0700528 ip6->payload_length =
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000529 clib_host_to_net_u16 (payload_len_total + hdr_len - l2_len -
Neale Ranns02950402019-12-20 00:54:57 +0000530 sizeof (ip6_header_t));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100531 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100532 else
Damjan Marionc98275f2019-03-06 14:05:01 +0100533 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100534 u16 len;
Neale Ranns02950402019-12-20 00:54:57 +0000535 ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100536 *next_hdr_ptr = ip4->protocol;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000537 len = payload_len_total + hdr_len - l2_len;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100538 if (udp)
539 {
540 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
541 esp_fill_udp_hdr (sa0, udp, len - ip_len);
542 }
543 else
544 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
Damjan Marionc98275f2019-03-06 14:05:01 +0100545 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100546
Neale Ranns02950402019-12-20 00:54:57 +0000547 clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
548
Neale Rannsc87b66c2019-02-07 07:26:12 -0800549 if (!is_tun)
550 next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
Damjan Marionc98275f2019-03-06 14:05:01 +0100551 }
552
Damjan Marionc59b9a22019-03-19 15:38:40 +0100553 esp->spi = spi;
554 esp->seq = clib_net_to_host_u32 (sa0->seq);
Damjan Marionc98275f2019-03-06 14:05:01 +0100555
Damjan Marion060bfb92019-03-29 13:47:54 +0100556 if (sa0->crypto_enc_op_id)
Damjan Marionc98275f2019-03-06 14:05:01 +0100557 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100558 vnet_crypto_op_t *op;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000559 vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
Damjan Marion060bfb92019-03-29 13:47:54 +0100560 vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000561
Damjan Marionc59b9a22019-03-19 15:38:40 +0100562 op->src = op->dst = payload;
Damjan Mariond1bed682019-04-24 15:20:35 +0200563 op->key_index = sa0->crypto_key_index;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100564 op->len = payload_len - icv_sz;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100565 op->user_data = b - bufs;
Neale Ranns47feb112019-04-11 15:14:07 +0000566
567 if (ipsec_sa_is_set_IS_AEAD (sa0))
568 {
569 /*
570 * construct the AAD in a scratch space in front
571 * of the IP header.
572 */
573 op->aad = payload - hdr_len - sizeof (esp_aead_t);
574
575 esp_aad_fill (op, esp, sa0);
576
577 op->tag = payload + op->len;
578 op->tag_len = 16;
Damjan Mariond97918e2019-04-25 18:28:31 +0200579
580 u64 *iv = (u64 *) (payload - iv_sz);
581 nonce->salt = sa0->salt;
582 nonce->iv = *iv = clib_host_to_net_u64 (sa0->gcm_iv_counter++);
583 op->iv = (u8 *) nonce;
584 nonce++;
585 }
586 else
587 {
588 op->iv = payload - iv_sz;
589 op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
Neale Ranns47feb112019-04-11 15:14:07 +0000590 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000591
592 if (lb != b[0])
593 {
594 /* is chained */
595 vlib_buffer_t *cb = b[0];
596 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
597 op->chunk_index = vec_len (ptd->chunks);
598 op->tag = vlib_buffer_get_tail (lb) - icv_sz;
599 vec_add2 (ptd->chunks, ch, 1);
600 ch->len = payload_len;
601 ch->src = ch->dst = payload;
602 cb = vlib_get_buffer (vm, cb->next_buffer);
603 op->n_chunks = 1;
604
605 while (1)
606 {
607 vec_add2 (ptd->chunks, ch, 1);
608 op->n_chunks += 1;
609 if (lb == cb)
610 ch->len = cb->current_length - icv_sz;
611 else
612 ch->len = cb->current_length;
613 ch->src = ch->dst = vlib_buffer_get_current (cb);
614
615 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
616 break;
617
618 cb = vlib_get_buffer (vm, cb->next_buffer);
619 }
620 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100621 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100622
Damjan Marion060bfb92019-03-29 13:47:54 +0100623 if (sa0->integ_op_id)
Damjan Marionc98275f2019-03-06 14:05:01 +0100624 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100625 vnet_crypto_op_t *op;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000626 vec_add2_aligned (integ_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
Damjan Marion060bfb92019-03-29 13:47:54 +0100627 vnet_crypto_op_init (op, sa0->integ_op_id);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100628 op->src = payload - iv_sz - sizeof (esp_header_t);
Damjan Marion060bfb92019-03-29 13:47:54 +0100629 op->digest = payload + payload_len - icv_sz;
Damjan Mariond1bed682019-04-24 15:20:35 +0200630 op->key_index = sa0->integ_key_index;
Damjan Marion060bfb92019-03-29 13:47:54 +0100631 op->digest_len = icv_sz;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100632 op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100633 op->user_data = b - bufs;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000634
635 if (lb != b[0])
636 {
637 /* is chained */
638 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
639 vlib_buffer_t *cb = b[0];
640 op->chunk_index = vec_len (ptd->chunks);
641 op->digest = vlib_buffer_get_tail (lb) - icv_sz;
642 vec_add2 (ptd->chunks, ch, 1);
643 ch->len = payload_len + iv_sz + sizeof (esp_header_t);
644 ch->src = payload - iv_sz - sizeof (esp_header_t);
645 cb = vlib_get_buffer (vm, cb->next_buffer);
646 op->n_chunks = 1;
647
648 while (1)
649 {
650 vec_add2 (ptd->chunks, ch, 1);
651 op->n_chunks += 1;
652 if (lb == cb)
653 {
654 ch->len = cb->current_length - icv_sz;
655 if (ipsec_sa_is_set_USE_ESN (sa0))
656 {
657 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
658 clib_memcpy_fast (op->digest, &seq_hi,
659 sizeof (seq_hi));
660 ch->len += sizeof (seq_hi);
661 }
662 }
663 else
664 ch->len = cb->current_length;
665 ch->src = vlib_buffer_get_current (cb);
666
667 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
668 break;
669
670 cb = vlib_get_buffer (vm, cb->next_buffer);
671 }
672 }
673 else if (ipsec_sa_is_set_USE_ESN (sa0))
Damjan Marionc98275f2019-03-06 14:05:01 +0100674 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100675 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
Neale Ranns49e7ef62019-04-10 17:24:29 +0000676 clib_memcpy_fast (op->digest, &seq_hi, sizeof (seq_hi));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100677 op->len += sizeof (seq_hi);
Damjan Marionc98275f2019-03-06 14:05:01 +0100678 }
679 }
680
Damjan Marionc59b9a22019-03-19 15:38:40 +0100681 vlib_buffer_advance (b[0], 0LL - hdr_len);
Damjan Marionc98275f2019-03-06 14:05:01 +0100682
Damjan Marionc59b9a22019-03-19 15:38:40 +0100683 current_sa_packets += 1;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000684 current_sa_bytes += payload_len_total;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100685
686 trace:
687 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
Damjan Marionc98275f2019-03-06 14:05:01 +0100688 {
Damjan Marionc59b9a22019-03-19 15:38:40 +0100689 esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
690 sizeof (*tr));
691 tr->sa_index = sa_index0;
692 tr->spi = sa0->spi;
Neale Ranns6afaae12019-07-17 15:07:14 +0000693 tr->seq = sa0->seq;
694 tr->sa_seq_hi = sa0->seq_hi;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100695 tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100696 tr->crypto_alg = sa0->crypto_alg;
697 tr->integ_alg = sa0->integ_alg;
Damjan Marionc98275f2019-03-06 14:05:01 +0100698 }
Damjan Marionc98275f2019-03-06 14:05:01 +0100699 /* next */
Damjan Marionc59b9a22019-03-19 15:38:40 +0100700 n_left -= 1;
Damjan Marionc98275f2019-03-06 14:05:01 +0100701 next += 1;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100702 b += 1;
Damjan Marionc98275f2019-03-06 14:05:01 +0100703 }
704
Damjan Marionc59b9a22019-03-19 15:38:40 +0100705 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
706 current_sa_index, current_sa_packets,
707 current_sa_bytes);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000708
Damjan Marionc59b9a22019-03-19 15:38:40 +0100709 esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000710 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, bufs, nexts,
711 ptd->chunks);
712
Damjan Marionc59b9a22019-03-19 15:38:40 +0100713 esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000714 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, bufs, nexts,
715 ptd->chunks);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100716
717 vlib_node_increment_counter (vm, node->node_index,
718 ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors);
719
720 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
721 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722}
723
Klement Sekerab8f35442018-10-29 13:38:19 +0100724VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
725 vlib_node_runtime_t * node,
726 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200727{
Neale Ranns25edf142019-03-22 08:12:48 +0000728 return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 0);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200729}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700731/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200732VLIB_REGISTER_NODE (esp4_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200733 .name = "esp4-encrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734 .vector_size = sizeof (u32),
735 .format_trace = format_esp_encrypt_trace,
736 .type = VLIB_NODE_TYPE_INTERNAL,
737
738 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
739 .error_strings = esp_encrypt_error_strings,
740
741 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
742 .next_nodes = {
Neale Rannsf62a8c02019-04-02 08:13:33 +0000743 [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop",
744 [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-handoff",
745 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746 },
747};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700748/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
Klement Sekerab8f35442018-10-29 13:38:19 +0100750VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
751 vlib_node_runtime_t * node,
752 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200753{
Neale Ranns25edf142019-03-22 08:12:48 +0000754 return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 0);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200755}
756
757/* *INDENT-OFF* */
758VLIB_REGISTER_NODE (esp6_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200759 .name = "esp6-encrypt",
760 .vector_size = sizeof (u32),
761 .format_trace = format_esp_encrypt_trace,
762 .type = VLIB_NODE_TYPE_INTERNAL,
763
764 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
765 .error_strings = esp_encrypt_error_strings,
766
767 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
768 .next_nodes = {
Neale Rannsf62a8c02019-04-02 08:13:33 +0000769 [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop",
770 [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-handoff",
771 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200772 },
773};
774/* *INDENT-ON* */
775
Neale Ranns25edf142019-03-22 08:12:48 +0000776VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
777 vlib_node_runtime_t * node,
778 vlib_frame_t * from_frame)
779{
780 return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 1);
781}
782
783/* *INDENT-OFF* */
784VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
785 .name = "esp4-encrypt-tun",
786 .vector_size = sizeof (u32),
787 .format_trace = format_esp_encrypt_trace,
788 .type = VLIB_NODE_TYPE_INTERNAL,
789
790 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
791 .error_strings = esp_encrypt_error_strings,
Neale Rannsd7603d92019-03-28 08:56:10 +0000792
Neale Rannsf62a8c02019-04-02 08:13:33 +0000793 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +0000794 .next_nodes = {
795 [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop",
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000796 [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-tun-handoff",
Neale Rannsf62a8c02019-04-02 08:13:33 +0000797 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop",
Neale Rannsd7603d92019-03-28 08:56:10 +0000798 },
Neale Ranns25edf142019-03-22 08:12:48 +0000799};
800
801VNET_FEATURE_INIT (esp4_encrypt_tun_feat_node, static) =
802{
803 .arc_name = "ip4-output",
804 .node_name = "esp4-encrypt-tun",
Neale Rannsea5bb772019-04-02 05:43:34 -0700805 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
Neale Ranns25edf142019-03-22 08:12:48 +0000806};
Neale Rannsc87b66c2019-02-07 07:26:12 -0800807
Neale Rannsb3259832019-09-27 13:32:02 +0000808VNET_FEATURE_INIT (esp6o4_encrypt_tun_feat_node, static) =
809{
810 .arc_name = "ip6-output",
811 .node_name = "esp4-encrypt-tun",
812 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
813};
814
Neale Rannsc87b66c2019-02-07 07:26:12 -0800815VNET_FEATURE_INIT (esp4_ethernet_encrypt_tun_feat_node, static) =
816{
817 .arc_name = "ethernet-output",
818 .node_name = "esp4-encrypt-tun",
819 .runs_before = VNET_FEATURES ("adj-midchain-tx", "adj-midchain-tx-no-count"),
820};
Neale Ranns25edf142019-03-22 08:12:48 +0000821/* *INDENT-ON* */
822
823VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
824 vlib_node_runtime_t * node,
825 vlib_frame_t * from_frame)
826{
827 return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 1);
828}
829
830/* *INDENT-OFF* */
831VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
832 .name = "esp6-encrypt-tun",
833 .vector_size = sizeof (u32),
834 .format_trace = format_esp_encrypt_trace,
835 .type = VLIB_NODE_TYPE_INTERNAL,
836
837 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
838 .error_strings = esp_encrypt_error_strings,
Neale Rannsd7603d92019-03-28 08:56:10 +0000839
Neale Rannsf62a8c02019-04-02 08:13:33 +0000840 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
Neale Rannsd7603d92019-03-28 08:56:10 +0000841 .next_nodes = {
842 [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop",
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000843 [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-tun-handoff",
Neale Rannsf62a8c02019-04-02 08:13:33 +0000844 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop",
Neale Rannsd7603d92019-03-28 08:56:10 +0000845 },
Neale Ranns25edf142019-03-22 08:12:48 +0000846};
847
848VNET_FEATURE_INIT (esp6_encrypt_tun_feat_node, static) =
849{
850 .arc_name = "ip6-output",
851 .node_name = "esp6-encrypt-tun",
Neale Rannsea5bb772019-04-02 05:43:34 -0700852 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
Neale Ranns25edf142019-03-22 08:12:48 +0000853};
Neale Rannsb3259832019-09-27 13:32:02 +0000854
855VNET_FEATURE_INIT (esp4o6_encrypt_tun_feat_node, static) =
856{
857 .arc_name = "ip4-output",
858 .node_name = "esp6-encrypt-tun",
859 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
860};
861
Neale Ranns25edf142019-03-22 08:12:48 +0000862/* *INDENT-ON* */
863
Matthew Smith401aedf2019-07-08 14:45:04 -0500864typedef struct
865{
866 u32 sa_index;
867} esp_no_crypto_trace_t;
868
869static u8 *
870format_esp_no_crypto_trace (u8 * s, va_list * args)
871{
872 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
873 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
874 esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *);
875
876 s = format (s, "esp-no-crypto: sa-index %u", t->sa_index);
877
878 return s;
879}
880
881enum
882{
883 ESP_NO_CRYPTO_NEXT_DROP,
884 ESP_NO_CRYPTO_N_NEXT,
885};
886
887enum
888{
889 ESP_NO_CRYPTO_ERROR_RX_PKTS,
890};
891
892static char *esp_no_crypto_error_strings[] = {
893 "Outbound ESP packets received",
894};
895
896always_inline uword
897esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
898 vlib_frame_t * frame)
899{
900 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
901 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
902 u32 *from = vlib_frame_vector_args (frame);
903 u32 n_left = frame->n_vectors;
904
905 vlib_get_buffers (vm, from, b, n_left);
906
907 while (n_left > 0)
908 {
909 u32 next0;
910 u32 sa_index0;
911
912 /* packets are always going to be dropped, but get the sa_index */
913 sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0],
914 sizeof (sa_index0));
915
916 next[0] = ESP_NO_CRYPTO_NEXT_DROP;
917
918 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
919 {
920 esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0],
921 sizeof (*tr));
922 tr->sa_index = sa_index0;
923 }
924
925 n_left -= 1;
926 next += 1;
927 b += 1;
928 }
929
930 vlib_node_increment_counter (vm, node->node_index,
931 ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors);
932
933 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
934
935 return frame->n_vectors;
936}
937
938VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm,
939 vlib_node_runtime_t * node,
940 vlib_frame_t * from_frame)
941{
942 return esp_no_crypto_inline (vm, node, from_frame);
943}
944
945/* *INDENT-OFF* */
946VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) =
947{
948 .name = "esp4-no-crypto",
949 .vector_size = sizeof (u32),
950 .format_trace = format_esp_no_crypto_trace,
951 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
952 .error_strings = esp_no_crypto_error_strings,
953 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
954 .next_nodes = {
955 [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop",
956 },
957};
958
959VNET_FEATURE_INIT (esp4_no_crypto_tun_feat_node, static) =
960{
961 .arc_name = "ip4-output",
962 .node_name = "esp4-no-crypto",
963 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
964};
965
966VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm,
967 vlib_node_runtime_t * node,
968 vlib_frame_t * from_frame)
969{
970 return esp_no_crypto_inline (vm, node, from_frame);
971}
972
973/* *INDENT-OFF* */
974VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) =
975{
976 .name = "esp6-no-crypto",
977 .vector_size = sizeof (u32),
978 .format_trace = format_esp_no_crypto_trace,
979 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
980 .error_strings = esp_no_crypto_error_strings,
981 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
982 .next_nodes = {
983 [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop",
984 },
985};
986
987VNET_FEATURE_INIT (esp6_no_crypto_tun_feat_node, static) =
988{
989 .arc_name = "ip6-output",
990 .node_name = "esp6-no-crypto",
991 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
992};
993/* *INDENT-ON* */
994
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700995/*
996 * fd.io coding-style-patch-verification: ON
997 *
998 * Local Variables:
999 * eval: (c-set-style "gnu")
1000 * End:
1001 */