blob: 2548ed401d959ac6c645f42fd0b8a55519751a40 [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>
Neale Ranns918c1612019-02-21 23:34:59 -080024#include <vnet/ipsec/ipsec_io.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025
Ed Warnickecb9cada2015-12-08 15:45:58 -070026#define foreach_esp_decrypt_next \
27_(DROP, "error-drop") \
28_(IP4_INPUT, "ip4-input") \
Matus Fabian694265d2016-08-10 01:55:36 -070029_(IP6_INPUT, "ip6-input") \
30_(IPSEC_GRE_INPUT, "ipsec-gre-input")
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
32#define _(v, s) ESP_DECRYPT_NEXT_##v,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070033typedef enum
34{
Ed Warnickecb9cada2015-12-08 15:45:58 -070035 foreach_esp_decrypt_next
36#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070037 ESP_DECRYPT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070038} esp_decrypt_next_t;
39
40
41#define foreach_esp_decrypt_error \
42 _(RX_PKTS, "ESP pkts received") \
43 _(NO_BUFFER, "No buffer (packed dropped)") \
44 _(DECRYPTION_FAILED, "ESP decryption failed") \
45 _(INTEG_ERROR, "Integrity check failed") \
Matus Fabianefc33302016-07-11 07:08:55 -070046 _(REPLAY, "SA replayed packet") \
47 _(NOT_IP, "Not IP packet (dropped)")
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
49
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070050typedef enum
51{
Ed Warnickecb9cada2015-12-08 15:45:58 -070052#define _(sym,str) ESP_DECRYPT_ERROR_##sym,
53 foreach_esp_decrypt_error
54#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070055 ESP_DECRYPT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070056} esp_decrypt_error_t;
57
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070058static char *esp_decrypt_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070059#define _(sym,string) string,
60 foreach_esp_decrypt_error
61#undef _
62};
63
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070064typedef struct
65{
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 ipsec_crypto_alg_t crypto_alg;
67 ipsec_integ_alg_t integ_alg;
68} esp_decrypt_trace_t;
69
70/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070071static u8 *
72format_esp_decrypt_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070073{
74 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
75 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070076 esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
78 s = format (s, "esp: crypto %U integrity %U",
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070079 format_ipsec_crypto_alg, t->crypto_alg,
80 format_ipsec_integ_alg, t->integ_alg);
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 return s;
82}
83
84always_inline void
“mukeshyadav1984”72454dd2017-11-28 10:52:34 -080085esp_decrypt_cbc (ipsec_crypto_alg_t alg,
86 u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087{
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080088 ipsec_proto_main_t *em = &ipsec_proto_main;
Damjan Marion586afd72017-04-05 19:18:20 +020089 u32 thread_index = vlib_get_thread_index ();
Marco Varlesef616d102017-11-09 15:16:20 +010090#if OPENSSL_VERSION_NUMBER >= 0x10100000L
91 EVP_CIPHER_CTX *ctx = em->per_thread_data[thread_index].decrypt_ctx;
92#else
Damjan Marion586afd72017-04-05 19:18:20 +020093 EVP_CIPHER_CTX *ctx = &(em->per_thread_data[thread_index].decrypt_ctx);
Marco Varlesef616d102017-11-09 15:16:20 +010094#endif
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070095 const EVP_CIPHER *cipher = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 int out_len;
97
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070098 ASSERT (alg < IPSEC_CRYPTO_N_ALG);
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800100 if (PREDICT_FALSE (em->ipsec_proto_main_crypto_algs[alg].type == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101 return;
102
Damjan Marion586afd72017-04-05 19:18:20 +0200103 if (PREDICT_FALSE
104 (alg != em->per_thread_data[thread_index].last_decrypt_alg))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700105 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800106 cipher = em->ipsec_proto_main_crypto_algs[alg].type;
Damjan Marion586afd72017-04-05 19:18:20 +0200107 em->per_thread_data[thread_index].last_decrypt_alg = alg;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700108 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700110 EVP_DecryptInit_ex (ctx, cipher, NULL, key, iv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700112 EVP_DecryptUpdate (ctx, out, &out_len, in, in_len);
113 EVP_DecryptFinal_ex (ctx, out + out_len, &out_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114}
115
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200116always_inline uword
117esp_decrypt_inline (vlib_main_t * vm,
118 vlib_node_runtime_t * node, vlib_frame_t * from_frame,
119 int is_ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120{
121 u32 n_left_from, *from, next_index, *to_next;
122 ipsec_main_t *im = &ipsec_main;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800123 ipsec_proto_main_t *em = &ipsec_proto_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700124 u32 *recycle = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 from = vlib_frame_vector_args (from_frame);
126 n_left_from = from_frame->n_vectors;
Damjan Marion586afd72017-04-05 19:18:20 +0200127 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700129 ipsec_alloc_empty_buffers (vm, im);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
Damjan Marion586afd72017-04-05 19:18:20 +0200131 u32 *empty_buffers = im->empty_buffers[thread_index];
Matus Fabian141977f2016-07-04 05:36:52 -0700132
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700133 if (PREDICT_FALSE (vec_len (empty_buffers) < n_left_from))
134 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100135 vlib_node_increment_counter (vm, node->node_index,
136 ESP_DECRYPT_ERROR_NO_BUFFER, n_left_from);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700137 goto free_buffers_and_exit;
138 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
140 next_index = node->cached_next_index;
141
142 while (n_left_from > 0)
143 {
144 u32 n_left_to_next;
145
146 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
147
148 while (n_left_from > 0 && n_left_to_next > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700149 {
150 u32 i_bi0, o_bi0 = (u32) ~ 0, next0;
151 vlib_buffer_t *i_b0;
152 vlib_buffer_t *o_b0 = 0;
153 esp_header_t *esp0;
154 ipsec_sa_t *sa0;
155 u32 sa_index0 = ~0;
156 u32 seq;
157 ip4_header_t *ih4 = 0, *oh4 = 0;
158 ip6_header_t *ih6 = 0, *oh6 = 0;
159 u8 tunnel_mode = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700161 i_bi0 = from[0];
162 from += 1;
163 n_left_from -= 1;
164 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700166 next0 = ESP_DECRYPT_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700168 i_b0 = vlib_get_buffer (vm, i_bi0);
169 esp0 = vlib_buffer_get_current (i_b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100171 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700172 sa0 = pool_elt_at_index (im->sad, sa_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700174 seq = clib_host_to_net_u32 (esp0->seq);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700176 /* anti-replay check */
177 if (sa0->use_anti_replay)
178 {
179 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700181 if (PREDICT_TRUE (sa0->use_esn))
182 rv = esp_replay_check_esn (sa0, seq);
183 else
184 rv = esp_replay_check (sa0, seq);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700186 if (PREDICT_FALSE (rv))
187 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100188 vlib_node_increment_counter (vm, node->node_index,
189 ESP_DECRYPT_ERROR_REPLAY, 1);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700190 o_bi0 = i_bi0;
Sergio Gonzalez Monroy73c01102016-09-06 14:06:06 +0100191 to_next[0] = o_bi0;
192 to_next += 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700193 goto trace;
194 }
195 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Neale Rannseba31ec2019-02-17 18:04:27 +0000197 vlib_increment_combined_counter
198 (&ipsec_sa_counters, thread_index, sa_index0,
199 1, i_b0->current_length);
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000200
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700201 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
202 {
203 u8 sig[64];
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800204 int icv_size =
205 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
Dave Barachb7b92992018-10-17 10:38:51 -0400206 clib_memset (sig, 0, sizeof (sig));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700207 u8 *icv =
208 vlib_buffer_get_current (i_b0) + i_b0->current_length -
209 icv_size;
210 i_b0->current_length -= icv_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
Neale Ranns8d7c5022019-02-06 01:41:05 -0800212 hmac_calc (sa0->integ_alg, sa0->integ_key.data,
213 sa0->integ_key.len, (u8 *) esp0,
214 i_b0->current_length, sig, sa0->use_esn,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700215 sa0->seq_hi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700217 if (PREDICT_FALSE (memcmp (icv, sig, icv_size)))
218 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100219 vlib_node_increment_counter (vm, node->node_index,
220 ESP_DECRYPT_ERROR_INTEG_ERROR,
221 1);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700222 o_bi0 = i_bi0;
Sergio Gonzalez Monroy73c01102016-09-06 14:06:06 +0100223 to_next[0] = o_bi0;
224 to_next += 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700225 goto trace;
226 }
227 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700229 if (PREDICT_TRUE (sa0->use_anti_replay))
230 {
231 if (PREDICT_TRUE (sa0->use_esn))
232 esp_replay_advance_esn (sa0, seq);
233 else
234 esp_replay_advance (sa0, seq);
235 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700237 /* grab free buffer */
238 uword last_empty_buffer = vec_len (empty_buffers) - 1;
239 o_bi0 = empty_buffers[last_empty_buffer];
Sergio Gonzalez Monroy73c01102016-09-06 14:06:06 +0100240 to_next[0] = o_bi0;
241 to_next += 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700242 o_b0 = vlib_get_buffer (vm, o_bi0);
243 vlib_prefetch_buffer_with_index (vm,
244 empty_buffers[last_empty_buffer -
245 1], STORE);
246 _vec_len (empty_buffers) = last_empty_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700248 /* add old buffer to the recycle list */
249 vec_add1 (recycle, i_bi0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
“mukeshyadav1984”72454dd2017-11-28 10:52:34 -0800251 if ((sa0->crypto_alg >= IPSEC_CRYPTO_ALG_AES_CBC_128 &&
252 sa0->crypto_alg <= IPSEC_CRYPTO_ALG_AES_CBC_256) ||
253 (sa0->crypto_alg >= IPSEC_CRYPTO_ALG_DES_CBC &&
254 sa0->crypto_alg <= IPSEC_CRYPTO_ALG_3DES_CBC))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700255 {
“mukeshyadav1984”72454dd2017-11-28 10:52:34 -0800256 const int BLOCK_SIZE =
257 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].block_size;;
258 const int IV_SIZE =
259 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].iv_size;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700260 esp_footer_t *f0;
261 u8 ip_hdr_size = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700263 int blocks =
264 (i_b0->current_length - sizeof (esp_header_t) -
265 IV_SIZE) / BLOCK_SIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700267 o_b0->current_data = sizeof (ethernet_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700269 /* transport mode */
270 if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6))
271 {
272 tunnel_mode = 0;
Szymon Sliwa8640dbd2018-03-19 15:14:31 +0000273
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200274 if (is_ip6)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700275 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200276 ih6 =
277 (ip6_header_t *) ((u8 *) esp0 -
278 sizeof (ip6_header_t));
279 ip_hdr_size = sizeof (ip6_header_t);
280 oh6 = vlib_buffer_get_current (o_b0);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700281 }
282 else
283 {
jackiechen19859f6957d2018-12-06 03:10:31 +0800284 if (sa0->udp_encap)
285 {
286 ih4 =
287 (ip4_header_t *) ((u8 *) esp0 -
288 sizeof (udp_header_t) -
289 sizeof (ip4_header_t));
290 }
291 else
292 {
293 ih4 =
294 (ip4_header_t *) ((u8 *) esp0 -
295 sizeof (ip4_header_t));
296 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700297 oh4 = vlib_buffer_get_current (o_b0);
298 ip_hdr_size = sizeof (ip4_header_t);
299 }
300 }
Matus Fabianefc33302016-07-11 07:08:55 -0700301
“mukeshyadav1984”72454dd2017-11-28 10:52:34 -0800302 esp_decrypt_cbc (sa0->crypto_alg,
303 esp0->data + IV_SIZE,
304 (u8 *) vlib_buffer_get_current (o_b0) +
305 ip_hdr_size, BLOCK_SIZE * blocks,
Neale Ranns8d7c5022019-02-06 01:41:05 -0800306 sa0->crypto_key.data, esp0->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
“mukeshyadav1984”72454dd2017-11-28 10:52:34 -0800308 o_b0->current_length = (blocks * BLOCK_SIZE) - 2 + ip_hdr_size;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700309 o_b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
310 f0 =
311 (esp_footer_t *) ((u8 *) vlib_buffer_get_current (o_b0) +
312 o_b0->current_length);
313 o_b0->current_length -= f0->pad_length;
Matus Fabianefc33302016-07-11 07:08:55 -0700314
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700315 /* tunnel mode */
316 if (PREDICT_TRUE (tunnel_mode))
317 {
318 if (PREDICT_TRUE (f0->next_header == IP_PROTOCOL_IP_IN_IP))
Matus Fabian694265d2016-08-10 01:55:36 -0700319 {
320 next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
321 oh4 = vlib_buffer_get_current (o_b0);
322 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700323 else if (f0->next_header == IP_PROTOCOL_IPV6)
324 next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
325 else
326 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100327 vlib_node_increment_counter (vm, node->node_index,
328 ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
329 1);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700330 o_b0 = 0;
331 goto trace;
332 }
333 }
334 /* transport mode */
335 else
336 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200337 if (is_ip6)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700338 {
339 next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
340 oh6->ip_version_traffic_class_and_flow_label =
341 ih6->ip_version_traffic_class_and_flow_label;
342 oh6->protocol = f0->next_header;
343 oh6->hop_limit = ih6->hop_limit;
344 oh6->src_address.as_u64[0] = ih6->src_address.as_u64[0];
345 oh6->src_address.as_u64[1] = ih6->src_address.as_u64[1];
346 oh6->dst_address.as_u64[0] = ih6->dst_address.as_u64[0];
347 oh6->dst_address.as_u64[1] = ih6->dst_address.as_u64[1];
348 oh6->payload_length =
349 clib_host_to_net_u16 (vlib_buffer_length_in_chain
350 (vm,
351 o_b0) - sizeof (ip6_header_t));
352 }
353 else
354 {
355 next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
356 oh4->ip_version_and_header_length = 0x45;
357 oh4->tos = ih4->tos;
358 oh4->fragment_id = 0;
359 oh4->flags_and_fragment_offset = 0;
360 oh4->ttl = ih4->ttl;
361 oh4->protocol = f0->next_header;
362 oh4->src_address.as_u32 = ih4->src_address.as_u32;
363 oh4->dst_address.as_u32 = ih4->dst_address.as_u32;
364 oh4->length =
365 clib_host_to_net_u16 (vlib_buffer_length_in_chain
366 (vm, o_b0));
367 oh4->checksum = ip4_header_checksum (oh4);
368 }
369 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370
Matus Fabian694265d2016-08-10 01:55:36 -0700371 /* for IPSec-GRE tunnel next node is ipsec-gre-input */
372 if (PREDICT_FALSE
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100373 ((vnet_buffer (i_b0)->ipsec.flags) &
Matus Fabian694265d2016-08-10 01:55:36 -0700374 IPSEC_FLAG_IPSEC_GRE_TUNNEL))
375 next0 = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT;
376
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700377 vnet_buffer (o_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Matthew Smith02e14b52017-09-14 09:05:35 -0500378 vnet_buffer (o_b0)->sw_if_index[VLIB_RX] =
379 vnet_buffer (i_b0)->sw_if_index[VLIB_RX];
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700380 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700382 trace:
383 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
384 {
385 if (o_b0)
386 {
387 o_b0->flags |= VLIB_BUFFER_IS_TRACED;
388 o_b0->trace_index = i_b0->trace_index;
Damjan Marion3f54b182016-08-16 11:27:02 +0200389 esp_decrypt_trace_t *tr =
390 vlib_add_trace (vm, node, o_b0, sizeof (*tr));
391 tr->crypto_alg = sa0->crypto_alg;
392 tr->integ_alg = sa0->integ_alg;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700393 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700394 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395
396 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700397 n_left_to_next, o_bi0, next0);
398 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
400 }
Klement Sekera2e02ba02018-11-30 14:37:03 +0100401 vlib_node_increment_counter (vm, node->node_index,
402 ESP_DECRYPT_ERROR_RX_PKTS,
403 from_frame->n_vectors);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200404
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405
406free_buffers_and_exit:
Damjan Marion3f54b182016-08-16 11:27:02 +0200407 if (recycle)
408 vlib_buffer_free (vm, recycle, vec_len (recycle));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700409 vec_free (recycle);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 return from_frame->n_vectors;
411}
412
Klement Sekerab8f35442018-10-29 13:38:19 +0100413VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm,
414 vlib_node_runtime_t * node,
415 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200416{
417 return esp_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
418}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700420/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200421VLIB_REGISTER_NODE (esp4_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200422 .name = "esp4-decrypt",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 .vector_size = sizeof (u32),
424 .format_trace = format_esp_decrypt_trace,
425 .type = VLIB_NODE_TYPE_INTERNAL,
426
427 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
428 .error_strings = esp_decrypt_error_strings,
429
430 .n_next_nodes = ESP_DECRYPT_N_NEXT,
431 .next_nodes = {
432#define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
433 foreach_esp_decrypt_next
434#undef _
435 },
436};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700437/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
Klement Sekerab8f35442018-10-29 13:38:19 +0100439VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm,
440 vlib_node_runtime_t * node,
441 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200442{
443 return esp_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
444}
445
446/* *INDENT-OFF* */
447VLIB_REGISTER_NODE (esp6_decrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200448 .name = "esp6-decrypt",
449 .vector_size = sizeof (u32),
450 .format_trace = format_esp_decrypt_trace,
451 .type = VLIB_NODE_TYPE_INTERNAL,
452
453 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
454 .error_strings = esp_decrypt_error_strings,
455
456 .n_next_nodes = ESP_DECRYPT_N_NEXT,
457 .next_nodes = {
458#define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
459 foreach_esp_decrypt_next
460#undef _
461 },
462};
463/* *INDENT-ON* */
464
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700465/*
466 * fd.io coding-style-patch-verification: ON
467 *
468 * Local Variables:
469 * eval: (c-set-style "gnu")
470 * End:
471 */