2 * esp_decrypt.c : IPSec ESP decrypt node
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24 #include <vnet/ipsec/ipsec_io.h>
26 #define foreach_esp_decrypt_next \
27 _(DROP, "error-drop") \
28 _(IP4_INPUT, "ip4-input") \
29 _(IP6_INPUT, "ip6-input") \
30 _(IPSEC_GRE_INPUT, "ipsec-gre-input")
32 #define _(v, s) ESP_DECRYPT_NEXT_##v,
35 foreach_esp_decrypt_next
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") \
46 _(REPLAY, "SA replayed packet") \
47 _(NOT_IP, "Not IP packet (dropped)")
52 #define _(sym,str) ESP_DECRYPT_ERROR_##sym,
53 foreach_esp_decrypt_error
56 } esp_decrypt_error_t;
58 static char *esp_decrypt_error_strings[] = {
59 #define _(sym,string) string,
60 foreach_esp_decrypt_error
66 ipsec_crypto_alg_t crypto_alg;
67 ipsec_integ_alg_t integ_alg;
68 } esp_decrypt_trace_t;
70 /* packet trace format function */
72 format_esp_decrypt_trace (u8 * s, va_list * args)
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 *);
76 esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
78 s = format (s, "esp: crypto %U integrity %U",
79 format_ipsec_crypto_alg, t->crypto_alg,
80 format_ipsec_integ_alg, t->integ_alg);
85 esp_decrypt_cbc (ipsec_crypto_alg_t alg,
86 u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv)
88 ipsec_proto_main_t *em = &ipsec_proto_main;
89 u32 thread_index = vlib_get_thread_index ();
90 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
91 EVP_CIPHER_CTX *ctx = em->per_thread_data[thread_index].decrypt_ctx;
93 EVP_CIPHER_CTX *ctx = &(em->per_thread_data[thread_index].decrypt_ctx);
95 const EVP_CIPHER *cipher = NULL;
98 ASSERT (alg < IPSEC_CRYPTO_N_ALG);
100 if (PREDICT_FALSE (em->ipsec_proto_main_crypto_algs[alg].type == 0))
104 (alg != em->per_thread_data[thread_index].last_decrypt_alg))
106 cipher = em->ipsec_proto_main_crypto_algs[alg].type;
107 em->per_thread_data[thread_index].last_decrypt_alg = alg;
110 EVP_DecryptInit_ex (ctx, cipher, NULL, key, iv);
112 EVP_DecryptUpdate (ctx, out, &out_len, in, in_len);
113 EVP_DecryptFinal_ex (ctx, out + out_len, &out_len);
117 esp_decrypt_inline (vlib_main_t * vm,
118 vlib_node_runtime_t * node, vlib_frame_t * from_frame,
121 u32 n_left_from, *from, next_index, *to_next;
122 ipsec_main_t *im = &ipsec_main;
123 ipsec_proto_main_t *em = &ipsec_proto_main;
125 from = vlib_frame_vector_args (from_frame);
126 n_left_from = from_frame->n_vectors;
127 u32 thread_index = vlib_get_thread_index ();
129 ipsec_alloc_empty_buffers (vm, im);
131 u32 *empty_buffers = im->empty_buffers[thread_index];
133 if (PREDICT_FALSE (vec_len (empty_buffers) < n_left_from))
135 vlib_node_increment_counter (vm, node->node_index,
136 ESP_DECRYPT_ERROR_NO_BUFFER, n_left_from);
137 goto free_buffers_and_exit;
140 next_index = node->cached_next_index;
142 while (n_left_from > 0)
146 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
148 while (n_left_from > 0 && n_left_to_next > 0)
150 u32 i_bi0, o_bi0 = (u32) ~ 0, next0;
152 vlib_buffer_t *o_b0 = 0;
157 ip4_header_t *ih4 = 0, *oh4 = 0;
158 ip6_header_t *ih6 = 0, *oh6 = 0;
166 next0 = ESP_DECRYPT_NEXT_DROP;
168 i_b0 = vlib_get_buffer (vm, i_bi0);
169 esp0 = vlib_buffer_get_current (i_b0);
171 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
172 sa0 = pool_elt_at_index (im->sad, sa_index0);
174 seq = clib_host_to_net_u32 (esp0->seq);
176 /* anti-replay check */
177 if (sa0->use_anti_replay)
181 if (PREDICT_TRUE (sa0->use_esn))
182 rv = esp_replay_check_esn (sa0, seq);
184 rv = esp_replay_check (sa0, seq);
186 if (PREDICT_FALSE (rv))
188 vlib_node_increment_counter (vm, node->node_index,
189 ESP_DECRYPT_ERROR_REPLAY, 1);
197 vlib_increment_combined_counter
198 (&ipsec_sa_counters, thread_index, sa_index0,
199 1, i_b0->current_length);
201 if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
205 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
206 clib_memset (sig, 0, sizeof (sig));
208 vlib_buffer_get_current (i_b0) + i_b0->current_length -
210 i_b0->current_length -= icv_size;
212 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,
217 if (PREDICT_FALSE (memcmp (icv, sig, icv_size)))
219 vlib_node_increment_counter (vm, node->node_index,
220 ESP_DECRYPT_ERROR_INTEG_ERROR,
229 if (PREDICT_TRUE (sa0->use_anti_replay))
231 if (PREDICT_TRUE (sa0->use_esn))
232 esp_replay_advance_esn (sa0, seq);
234 esp_replay_advance (sa0, seq);
237 /* grab free buffer */
238 uword last_empty_buffer = vec_len (empty_buffers) - 1;
239 o_bi0 = empty_buffers[last_empty_buffer];
242 o_b0 = vlib_get_buffer (vm, o_bi0);
243 vlib_prefetch_buffer_with_index (vm,
244 empty_buffers[last_empty_buffer -
246 _vec_len (empty_buffers) = last_empty_buffer;
248 /* add old buffer to the recycle list */
249 vec_add1 (recycle, i_bi0);
251 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))
256 const int BLOCK_SIZE =
257 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].block_size;;
259 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].iv_size;
264 (i_b0->current_length - sizeof (esp_header_t) -
265 IV_SIZE) / BLOCK_SIZE;
267 o_b0->current_data = sizeof (ethernet_header_t);
270 if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6))
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);
287 (ip4_header_t *) ((u8 *) esp0 -
288 sizeof (udp_header_t) -
289 sizeof (ip4_header_t));
294 (ip4_header_t *) ((u8 *) esp0 -
295 sizeof (ip4_header_t));
297 oh4 = vlib_buffer_get_current (o_b0);
298 ip_hdr_size = sizeof (ip4_header_t);
302 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,
306 sa0->crypto_key.data, esp0->data);
308 o_b0->current_length = (blocks * BLOCK_SIZE) - 2 + ip_hdr_size;
309 o_b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
311 (esp_footer_t *) ((u8 *) vlib_buffer_get_current (o_b0) +
312 o_b0->current_length);
313 o_b0->current_length -= f0->pad_length;
316 if (PREDICT_TRUE (tunnel_mode))
318 if (PREDICT_TRUE (f0->next_header == IP_PROTOCOL_IP_IN_IP))
320 next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
321 oh4 = vlib_buffer_get_current (o_b0);
323 else if (f0->next_header == IP_PROTOCOL_IPV6)
324 next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
327 vlib_node_increment_counter (vm, node->node_index,
328 ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
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
351 o_b0) - sizeof (ip6_header_t));
355 next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
356 oh4->ip_version_and_header_length = 0x45;
358 oh4->fragment_id = 0;
359 oh4->flags_and_fragment_offset = 0;
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;
365 clib_host_to_net_u16 (vlib_buffer_length_in_chain
367 oh4->checksum = ip4_header_checksum (oh4);
371 /* for IPSec-GRE tunnel next node is ipsec-gre-input */
373 ((vnet_buffer (i_b0)->ipsec.flags) &
374 IPSEC_FLAG_IPSEC_GRE_TUNNEL))
375 next0 = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT;
377 vnet_buffer (o_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
378 vnet_buffer (o_b0)->sw_if_index[VLIB_RX] =
379 vnet_buffer (i_b0)->sw_if_index[VLIB_RX];
383 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
387 o_b0->flags |= VLIB_BUFFER_IS_TRACED;
388 o_b0->trace_index = i_b0->trace_index;
389 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;
396 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
397 n_left_to_next, o_bi0, next0);
399 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
401 vlib_node_increment_counter (vm, node->node_index,
402 ESP_DECRYPT_ERROR_RX_PKTS,
403 from_frame->n_vectors);
406 free_buffers_and_exit:
408 vlib_buffer_free (vm, recycle, vec_len (recycle));
410 return from_frame->n_vectors;
413 VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm,
414 vlib_node_runtime_t * node,
415 vlib_frame_t * from_frame)
417 return esp_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
421 VLIB_REGISTER_NODE (esp4_decrypt_node) = {
422 .name = "esp4-decrypt",
423 .vector_size = sizeof (u32),
424 .format_trace = format_esp_decrypt_trace,
425 .type = VLIB_NODE_TYPE_INTERNAL,
427 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
428 .error_strings = esp_decrypt_error_strings,
430 .n_next_nodes = ESP_DECRYPT_N_NEXT,
432 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
433 foreach_esp_decrypt_next
439 VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm,
440 vlib_node_runtime_t * node,
441 vlib_frame_t * from_frame)
443 return esp_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
447 VLIB_REGISTER_NODE (esp6_decrypt_node) = {
448 .name = "esp6-decrypt",
449 .vector_size = sizeof (u32),
450 .format_trace = format_esp_decrypt_trace,
451 .type = VLIB_NODE_TYPE_INTERNAL,
453 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
454 .error_strings = esp_decrypt_error_strings,
456 .n_next_nodes = ESP_DECRYPT_N_NEXT,
458 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
459 foreach_esp_decrypt_next
466 * fd.io coding-style-patch-verification: ON
469 * eval: (c-set-style "gnu")