blob: 0cf31ffb00003f5d0f91ed6b6dced8bf70fe194c [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * esp_decrypt.c : IPSec ESP decrypt node
3 *
4 * Copyright (c) 2015 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
21
22#include <vnet/ipsec/ipsec.h>
23#include <vnet/ipsec/esp.h>
24
Ed Warnickecb9cada2015-12-08 15:45:58 -070025#define foreach_esp_decrypt_next \
26_(DROP, "error-drop") \
27_(IP4_INPUT, "ip4-input") \
Matus Fabian694265d2016-08-10 01:55:36 -070028_(IP6_INPUT, "ip6-input") \
29_(IPSEC_GRE_INPUT, "ipsec-gre-input")
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
31#define _(v, s) ESP_DECRYPT_NEXT_##v,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070032typedef enum
33{
Ed Warnickecb9cada2015-12-08 15:45:58 -070034 foreach_esp_decrypt_next
35#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070036 ESP_DECRYPT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070037} esp_decrypt_next_t;
38
39
40#define foreach_esp_decrypt_error \
41 _(RX_PKTS, "ESP pkts received") \
42 _(NO_BUFFER, "No buffer (packed dropped)") \
43 _(DECRYPTION_FAILED, "ESP decryption failed") \
44 _(INTEG_ERROR, "Integrity check failed") \
Matus Fabianefc33302016-07-11 07:08:55 -070045 _(REPLAY, "SA replayed packet") \
46 _(NOT_IP, "Not IP packet (dropped)")
Ed Warnickecb9cada2015-12-08 15:45:58 -070047
48
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070049typedef enum
50{
Ed Warnickecb9cada2015-12-08 15:45:58 -070051#define _(sym,str) ESP_DECRYPT_ERROR_##sym,
52 foreach_esp_decrypt_error
53#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070054 ESP_DECRYPT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070055} esp_decrypt_error_t;
56
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070057static char *esp_decrypt_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070058#define _(sym,string) string,
59 foreach_esp_decrypt_error
60#undef _
61};
62
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070063typedef struct
64{
Ed Warnickecb9cada2015-12-08 15:45:58 -070065 ipsec_crypto_alg_t crypto_alg;
66 ipsec_integ_alg_t integ_alg;
67} esp_decrypt_trace_t;
68
69/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070070static u8 *
71format_esp_decrypt_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070072{
73 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070075 esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
77 s = format (s, "esp: crypto %U integrity %U",
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070078 format_ipsec_crypto_alg, t->crypto_alg,
79 format_ipsec_integ_alg, t->integ_alg);
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 return s;
81}
82
83always_inline void
“mukeshyadav1984”72454dd2017-11-28 10:52:34 -080084esp_decrypt_cbc (ipsec_crypto_alg_t alg,
85 u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086{
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080087 ipsec_proto_main_t *em = &ipsec_proto_main;
Damjan Marion586afd72017-04-05 19:18:20 +020088 u32 thread_index = vlib_get_thread_index ();
Marco Varlesef616d102017-11-09 15:16:20 +010089#if OPENSSL_VERSION_NUMBER >= 0x10100000L
90 EVP_CIPHER_CTX *ctx = em->per_thread_data[thread_index].decrypt_ctx;
91#else
Damjan Marion586afd72017-04-05 19:18:20 +020092 EVP_CIPHER_CTX *ctx = &(em->per_thread_data[thread_index].decrypt_ctx);
Marco Varlesef616d102017-11-09 15:16:20 +010093#endif
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070094 const EVP_CIPHER *cipher = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 int out_len;
96
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070097 ASSERT (alg < IPSEC_CRYPTO_N_ALG);
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080099 if (PREDICT_FALSE (em->ipsec_proto_main_crypto_algs[alg].type == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 return;
101
Damjan Marion586afd72017-04-05 19:18:20 +0200102 if (PREDICT_FALSE
103 (alg != em->per_thread_data[thread_index].last_decrypt_alg))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700104 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800105 cipher = em->ipsec_proto_main_crypto_algs[alg].type;
Damjan Marion586afd72017-04-05 19:18:20 +0200106 em->per_thread_data[thread_index].last_decrypt_alg = alg;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700107 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700109 EVP_DecryptInit_ex (ctx, cipher, NULL, key, iv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700111 EVP_DecryptUpdate (ctx, out, &out_len, in, in_len);
112 EVP_DecryptFinal_ex (ctx, out + out_len, &out_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113}
114
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200115always_inline uword
116esp_decrypt_inline (vlib_main_t * vm,
117 vlib_node_runtime_t * node, vlib_frame_t * from_frame,
118 int is_ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119{
120 u32 n_left_from, *from, next_index, *to_next;
121 ipsec_main_t *im = &ipsec_main;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800122 ipsec_proto_main_t *em = &ipsec_proto_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700123 u32 *recycle = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 from = vlib_frame_vector_args (from_frame);
125 n_left_from = from_frame->n_vectors;
Damjan Marion586afd72017-04-05 19:18:20 +0200126 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700128 ipsec_alloc_empty_buffers (vm, im);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
Damjan Marion586afd72017-04-05 19:18:20 +0200130 u32 *empty_buffers = im->empty_buffers[thread_index];
Matus Fabian141977f2016-07-04 05:36:52 -0700131
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700132 if (PREDICT_FALSE (vec_len (empty_buffers) < n_left_from))
133 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100134 vlib_node_increment_counter (vm, node->node_index,
135 ESP_DECRYPT_ERROR_NO_BUFFER, n_left_from);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700136 goto free_buffers_and_exit;
137 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
139 next_index = node->cached_next_index;
140
141 while (n_left_from > 0)
142 {
143 u32 n_left_to_next;
144
145 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
146
147 while (n_left_from > 0 && n_left_to_next > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700148 {
149 u32 i_bi0, o_bi0 = (u32) ~ 0, next0;
150 vlib_buffer_t *i_b0;
151 vlib_buffer_t *o_b0 = 0;
152 esp_header_t *esp0;
153 ipsec_sa_t *sa0;
154 u32 sa_index0 = ~0;
155 u32 seq;
156 ip4_header_t *ih4 = 0, *oh4 = 0;
157 ip6_header_t *ih6 = 0, *oh6 = 0;
158 u8 tunnel_mode = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700160 i_bi0 = from[0];
161 from += 1;
162 n_left_from -= 1;
163 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700165 next0 = ESP_DECRYPT_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700167 i_b0 = vlib_get_buffer (vm, i_bi0);
168 esp0 = vlib_buffer_get_current (i_b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100170 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700171 sa0 = pool_elt_at_index (im->sad, sa_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700173 seq = clib_host_to_net_u32 (esp0->seq);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700175 /* anti-replay check */
176 if (sa0->use_anti_replay)
177 {
178 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700180 if (PREDICT_TRUE (sa0->use_esn))
181 rv = esp_replay_check_esn (sa0, seq);
182 else
183 rv = esp_replay_check (sa0, seq);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700185 if (PREDICT_FALSE (rv))
186 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100187 vlib_node_increment_counter (vm, node->node_index,
188 ESP_DECRYPT_ERROR_REPLAY, 1);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700189 o_bi0 = i_bi0;
Sergio Gonzalez Monroy73c01102016-09-06 14:06:06 +0100190 to_next[0] = o_bi0;
191 to_next += 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700192 goto trace;
193 }
194 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195
Neale Rannseba31ec2019-02-17 18:04:27 +0000196 vlib_increment_combined_counter
197 (&ipsec_sa_counters, thread_index, sa_index0,
198 1, i_b0->current_length);
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000199
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700200 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
201 {
202 u8 sig[64];
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800203 int icv_size =
204 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
Dave Barachb7b92992018-10-17 10:38:51 -0400205 clib_memset (sig, 0, sizeof (sig));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700206 u8 *icv =
207 vlib_buffer_get_current (i_b0) + i_b0->current_length -
208 icv_size;
209 i_b0->current_length -= icv_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Neale Ranns8d7c5022019-02-06 01:41:05 -0800211 hmac_calc (sa0->integ_alg, sa0->integ_key.data,
212 sa0->integ_key.len, (u8 *) esp0,
213 i_b0->current_length, sig, sa0->use_esn,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700214 sa0->seq_hi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700216 if (PREDICT_FALSE (memcmp (icv, sig, icv_size)))
217 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100218 vlib_node_increment_counter (vm, node->node_index,
219 ESP_DECRYPT_ERROR_INTEG_ERROR,
220 1);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700221 o_bi0 = i_bi0;
Sergio Gonzalez Monroy73c01102016-09-06 14:06:06 +0100222 to_next[0] = o_bi0;
223 to_next += 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700224 goto trace;
225 }
226 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700228 if (PREDICT_TRUE (sa0->use_anti_replay))
229 {
230 if (PREDICT_TRUE (sa0->use_esn))
231 esp_replay_advance_esn (sa0, seq);
232 else
233 esp_replay_advance (sa0, seq);
234 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700236 /* grab free buffer */
237 uword last_empty_buffer = vec_len (empty_buffers) - 1;
238 o_bi0 = empty_buffers[last_empty_buffer];
Sergio Gonzalez Monroy73c01102016-09-06 14:06:06 +0100239 to_next[0] = o_bi0;
240 to_next += 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700241 o_b0 = vlib_get_buffer (vm, o_bi0);
242 vlib_prefetch_buffer_with_index (vm,
243 empty_buffers[last_empty_buffer -
244 1], STORE);
245 _vec_len (empty_buffers) = last_empty_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700247 /* add old buffer to the recycle list */
248 vec_add1 (recycle, i_bi0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
“mukeshyadav1984”72454dd2017-11-28 10:52:34 -0800250 if ((sa0->crypto_alg >= IPSEC_CRYPTO_ALG_AES_CBC_128 &&
251 sa0->crypto_alg <= IPSEC_CRYPTO_ALG_AES_CBC_256) ||
252 (sa0->crypto_alg >= IPSEC_CRYPTO_ALG_DES_CBC &&
253 sa0->crypto_alg <= IPSEC_CRYPTO_ALG_3DES_CBC))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700254 {
“mukeshyadav1984”72454dd2017-11-28 10:52:34 -0800255 const int BLOCK_SIZE =
256 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].block_size;;
257 const int IV_SIZE =
258 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].iv_size;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700259 esp_footer_t *f0;
260 u8 ip_hdr_size = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700262 int blocks =
263 (i_b0->current_length - sizeof (esp_header_t) -
264 IV_SIZE) / BLOCK_SIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700266 o_b0->current_data = sizeof (ethernet_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700268 /* transport mode */
269 if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6))
270 {
271 tunnel_mode = 0;
Szymon Sliwa8640dbd2018-03-19 15:14:31 +0000272
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200273 if (is_ip6)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700274 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200275 ih6 =
276 (ip6_header_t *) ((u8 *) esp0 -
277 sizeof (ip6_header_t));
278 ip_hdr_size = sizeof (ip6_header_t);
279 oh6 = vlib_buffer_get_current (o_b0);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700280 }
281 else
282 {
jackiechen19859f6957d2018-12-06 03:10:31 +0800283 if (sa0->udp_encap)
284 {
285 ih4 =
286 (ip4_header_t *) ((u8 *) esp0 -
287 sizeof (udp_header_t) -
288 sizeof (ip4_header_t));
289 }
290 else
291 {
292 ih4 =
293 (ip4_header_t *) ((u8 *) esp0 -
294 sizeof (ip4_header_t));
295 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700296 oh4 = vlib_buffer_get_current (o_b0);
297 ip_hdr_size = sizeof (ip4_header_t);
298 }
299 }
Matus Fabianefc33302016-07-11 07:08:55 -0700300
“mukeshyadav1984”72454dd2017-11-28 10:52:34 -0800301 esp_decrypt_cbc (sa0->crypto_alg,
302 esp0->data + IV_SIZE,
303 (u8 *) vlib_buffer_get_current (o_b0) +
304 ip_hdr_size, BLOCK_SIZE * blocks,
Neale Ranns8d7c5022019-02-06 01:41:05 -0800305 sa0->crypto_key.data, esp0->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
“mukeshyadav1984”72454dd2017-11-28 10:52:34 -0800307 o_b0->current_length = (blocks * BLOCK_SIZE) - 2 + ip_hdr_size;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700308 o_b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
309 f0 =
310 (esp_footer_t *) ((u8 *) vlib_buffer_get_current (o_b0) +
311 o_b0->current_length);
312 o_b0->current_length -= f0->pad_length;
Matus Fabianefc33302016-07-11 07:08:55 -0700313
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700314 /* tunnel mode */
315 if (PREDICT_TRUE (tunnel_mode))
316 {
317 if (PREDICT_TRUE (f0->next_header == IP_PROTOCOL_IP_IN_IP))
Matus Fabian694265d2016-08-10 01:55:36 -0700318 {
319 next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
320 oh4 = vlib_buffer_get_current (o_b0);
321 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700322 else if (f0->next_header == IP_PROTOCOL_IPV6)
323 next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
324 else
325 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100326 vlib_node_increment_counter (vm, node->node_index,
327 ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
328 1);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700329 o_b0 = 0;
330 goto trace;
331 }
332 }
333 /* transport mode */
334 else
335 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200336 if (is_ip6)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700337 {
338 next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
339 oh6->ip_version_traffic_class_and_flow_label =
340 ih6->ip_version_traffic_class_and_flow_label;
341 oh6->protocol = f0->next_header;
342 oh6->hop_limit = ih6->hop_limit;
343 oh6->src_address.as_u64[0] = ih6->src_address.as_u64[0];
344 oh6->src_address.as_u64[1] = ih6->src_address.as_u64[1];
345 oh6->dst_address.as_u64[0] = ih6->dst_address.as_u64[0];
346 oh6->dst_address.as_u64[1] = ih6->dst_address.as_u64[1];
347 oh6->payload_length =
348 clib_host_to_net_u16 (vlib_buffer_length_in_chain
349 (vm,
350 o_b0) - sizeof (ip6_header_t));
351 }
352 else
353 {
354 next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
355 oh4->ip_version_and_header_length = 0x45;
356 oh4->tos = ih4->tos;
357 oh4->fragment_id = 0;
358 oh4->flags_and_fragment_offset = 0;
359 oh4->ttl = ih4->ttl;
360 oh4->protocol = f0->next_header;
361 oh4->src_address.as_u32 = ih4->src_address.as_u32;
362 oh4->dst_address.as_u32 = ih4->dst_address.as_u32;
363 oh4->length =
364 clib_host_to_net_u16 (vlib_buffer_length_in_chain
365 (vm, o_b0));
366 oh4->checksum = ip4_header_checksum (oh4);
367 }
368 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369
Matus Fabian694265d2016-08-10 01:55:36 -0700370 /* for IPSec-GRE tunnel next node is ipsec-gre-input */
371 if (PREDICT_FALSE
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100372 ((vnet_buffer (i_b0)->ipsec.flags) &
Matus Fabian694265d2016-08-10 01:55:36 -0700373 IPSEC_FLAG_IPSEC_GRE_TUNNEL))
374 next0 = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT;
375
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700376 vnet_buffer (o_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Matthew Smith02e14b52017-09-14 09:05:35 -0500377 vnet_buffer (o_b0)->sw_if_index[VLIB_RX] =
378 vnet_buffer (i_b0)->sw_if_index[VLIB_RX];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700379 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700381 trace:
382 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
383 {
384 if (o_b0)
385 {
386 o_b0->flags |= VLIB_BUFFER_IS_TRACED;
387 o_b0->trace_index = i_b0->trace_index;
Damjan Marion3f54b182016-08-16 11:27:02 +0200388 esp_decrypt_trace_t *tr =
389 vlib_add_trace (vm, node, o_b0, sizeof (*tr));
390 tr->crypto_alg = sa0->crypto_alg;
391 tr->integ_alg = sa0->integ_alg;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700392 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700393 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
395 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700396 n_left_to_next, o_bi0, next0);
397 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
399 }
Klement Sekera2e02ba02018-11-30 14:37:03 +0100400 vlib_node_increment_counter (vm, node->node_index,
401 ESP_DECRYPT_ERROR_RX_PKTS,
402 from_frame->n_vectors);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200403
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404
405free_buffers_and_exit:
Damjan Marion3f54b182016-08-16 11:27:02 +0200406 if (recycle)
407 vlib_buffer_free (vm, recycle, vec_len (recycle));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700408 vec_free (recycle);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 return from_frame->n_vectors;
410}
411
Klement Sekerab8f35442018-10-29 13:38:19 +0100412VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm,
413 vlib_node_runtime_t * node,
414 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200415{
416 return esp_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
417}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700419/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200420VLIB_REGISTER_NODE (esp4_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200421 .name = "esp4-decrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 .vector_size = sizeof (u32),
423 .format_trace = format_esp_decrypt_trace,
424 .type = VLIB_NODE_TYPE_INTERNAL,
425
426 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
427 .error_strings = esp_decrypt_error_strings,
428
429 .n_next_nodes = ESP_DECRYPT_N_NEXT,
430 .next_nodes = {
431#define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
432 foreach_esp_decrypt_next
433#undef _
434 },
435};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700436/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437
Klement Sekerab8f35442018-10-29 13:38:19 +0100438VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm,
439 vlib_node_runtime_t * node,
440 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200441{
442 return esp_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
443}
444
445/* *INDENT-OFF* */
446VLIB_REGISTER_NODE (esp6_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200447 .name = "esp6-decrypt",
448 .vector_size = sizeof (u32),
449 .format_trace = format_esp_decrypt_trace,
450 .type = VLIB_NODE_TYPE_INTERNAL,
451
452 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
453 .error_strings = esp_decrypt_error_strings,
454
455 .n_next_nodes = ESP_DECRYPT_N_NEXT,
456 .next_nodes = {
457#define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
458 foreach_esp_decrypt_next
459#undef _
460 },
461};
462/* *INDENT-ON* */
463
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700464/*
465 * fd.io coding-style-patch-verification: ON
466 *
467 * Local Variables:
468 * eval: (c-set-style "gnu")
469 * End:
470 */