Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | #define foreach_esp_decrypt_next \ |
| 26 | _(DROP, "error-drop") \ |
| 27 | _(IP4_INPUT, "ip4-input") \ |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 28 | _(IP6_INPUT, "ip6-input") \ |
| 29 | _(IPSEC_GRE_INPUT, "ipsec-gre-input") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | |
| 31 | #define _(v, s) ESP_DECRYPT_NEXT_##v, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 32 | typedef enum |
| 33 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | foreach_esp_decrypt_next |
| 35 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 36 | ESP_DECRYPT_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | } 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 Fabian | efc3330 | 2016-07-11 07:08:55 -0700 | [diff] [blame] | 45 | _(REPLAY, "SA replayed packet") \ |
| 46 | _(NOT_IP, "Not IP packet (dropped)") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | |
| 48 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 49 | typedef enum |
| 50 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | #define _(sym,str) ESP_DECRYPT_ERROR_##sym, |
| 52 | foreach_esp_decrypt_error |
| 53 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 54 | ESP_DECRYPT_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | } esp_decrypt_error_t; |
| 56 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 57 | static char *esp_decrypt_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | #define _(sym,string) string, |
| 59 | foreach_esp_decrypt_error |
| 60 | #undef _ |
| 61 | }; |
| 62 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 63 | typedef struct |
| 64 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 70 | static u8 * |
| 71 | format_esp_decrypt_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | { |
| 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 75 | esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | |
| 77 | s = format (s, "esp: crypto %U integrity %U", |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 78 | format_ipsec_crypto_alg, t->crypto_alg, |
| 79 | format_ipsec_integ_alg, t->integ_alg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | return s; |
| 81 | } |
| 82 | |
| 83 | always_inline void |
“mukeshyadav1984” | 72454dd | 2017-11-28 10:52:34 -0800 | [diff] [blame^] | 84 | esp_decrypt_cbc (ipsec_crypto_alg_t alg, |
| 85 | u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | { |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 87 | ipsec_proto_main_t *em = &ipsec_proto_main; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 88 | u32 thread_index = vlib_get_thread_index (); |
Marco Varlese | f616d10 | 2017-11-09 15:16:20 +0100 | [diff] [blame] | 89 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 90 | EVP_CIPHER_CTX *ctx = em->per_thread_data[thread_index].decrypt_ctx; |
| 91 | #else |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 92 | EVP_CIPHER_CTX *ctx = &(em->per_thread_data[thread_index].decrypt_ctx); |
Marco Varlese | f616d10 | 2017-11-09 15:16:20 +0100 | [diff] [blame] | 93 | #endif |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 94 | const EVP_CIPHER *cipher = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | int out_len; |
| 96 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 97 | ASSERT (alg < IPSEC_CRYPTO_N_ALG); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 99 | if (PREDICT_FALSE (em->ipsec_proto_main_crypto_algs[alg].type == 0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | return; |
| 101 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 102 | if (PREDICT_FALSE |
| 103 | (alg != em->per_thread_data[thread_index].last_decrypt_alg)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 104 | { |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 105 | cipher = em->ipsec_proto_main_crypto_algs[alg].type; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 106 | em->per_thread_data[thread_index].last_decrypt_alg = alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 107 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 109 | EVP_DecryptInit_ex (ctx, cipher, NULL, key, iv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 111 | EVP_DecryptUpdate (ctx, out, &out_len, in, in_len); |
| 112 | EVP_DecryptFinal_ex (ctx, out + out_len, &out_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | static uword |
| 116 | esp_decrypt_node_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 117 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | { |
| 119 | u32 n_left_from, *from, next_index, *to_next; |
| 120 | ipsec_main_t *im = &ipsec_main; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 121 | ipsec_proto_main_t *em = &ipsec_proto_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 122 | u32 *recycle = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | from = vlib_frame_vector_args (from_frame); |
| 124 | n_left_from = from_frame->n_vectors; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 125 | u32 thread_index = vlib_get_thread_index (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 127 | ipsec_alloc_empty_buffers (vm, im); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 129 | u32 *empty_buffers = im->empty_buffers[thread_index]; |
Matus Fabian | 141977f | 2016-07-04 05:36:52 -0700 | [diff] [blame] | 130 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 131 | if (PREDICT_FALSE (vec_len (empty_buffers) < n_left_from)) |
| 132 | { |
| 133 | vlib_node_increment_counter (vm, esp_decrypt_node.index, |
| 134 | ESP_DECRYPT_ERROR_NO_BUFFER, n_left_from); |
| 135 | goto free_buffers_and_exit; |
| 136 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | |
| 138 | next_index = node->cached_next_index; |
| 139 | |
| 140 | while (n_left_from > 0) |
| 141 | { |
| 142 | u32 n_left_to_next; |
| 143 | |
| 144 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 145 | |
| 146 | while (n_left_from > 0 && n_left_to_next > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 147 | { |
| 148 | u32 i_bi0, o_bi0 = (u32) ~ 0, next0; |
| 149 | vlib_buffer_t *i_b0; |
| 150 | vlib_buffer_t *o_b0 = 0; |
| 151 | esp_header_t *esp0; |
| 152 | ipsec_sa_t *sa0; |
| 153 | u32 sa_index0 = ~0; |
| 154 | u32 seq; |
| 155 | ip4_header_t *ih4 = 0, *oh4 = 0; |
| 156 | ip6_header_t *ih6 = 0, *oh6 = 0; |
| 157 | u8 tunnel_mode = 1; |
| 158 | u8 transport_ip6 = 0; |
Matus Fabian | efc3330 | 2016-07-11 07:08:55 -0700 | [diff] [blame] | 159 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 161 | i_bi0 = from[0]; |
| 162 | from += 1; |
| 163 | n_left_from -= 1; |
| 164 | n_left_to_next -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 166 | next0 = ESP_DECRYPT_NEXT_DROP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 168 | i_b0 = vlib_get_buffer (vm, i_bi0); |
| 169 | esp0 = vlib_buffer_get_current (i_b0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
Damjan Marion | 9c6ae5f | 2016-11-15 23:20:01 +0100 | [diff] [blame] | 171 | sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 172 | sa0 = pool_elt_at_index (im->sad, sa_index0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 174 | seq = clib_host_to_net_u32 (esp0->seq); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 176 | /* anti-replay check */ |
| 177 | if (sa0->use_anti_replay) |
| 178 | { |
| 179 | int rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 181 | if (PREDICT_TRUE (sa0->use_esn)) |
| 182 | rv = esp_replay_check_esn (sa0, seq); |
| 183 | else |
| 184 | rv = esp_replay_check (sa0, seq); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 186 | if (PREDICT_FALSE (rv)) |
| 187 | { |
| 188 | clib_warning ("anti-replay SPI %u seq %u", sa0->spi, seq); |
| 189 | vlib_node_increment_counter (vm, esp_decrypt_node.index, |
| 190 | ESP_DECRYPT_ERROR_REPLAY, 1); |
| 191 | o_bi0 = i_bi0; |
Sergio Gonzalez Monroy | 73c0110 | 2016-09-06 14:06:06 +0100 | [diff] [blame] | 192 | to_next[0] = o_bi0; |
| 193 | to_next += 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 194 | goto trace; |
| 195 | } |
| 196 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | |
Radu Nicolau | cb33dc2 | 2017-02-16 16:49:46 +0000 | [diff] [blame] | 198 | sa0->total_data_size += i_b0->current_length; |
| 199 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 200 | if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE)) |
| 201 | { |
| 202 | u8 sig[64]; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 203 | int icv_size = |
| 204 | em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 205 | memset (sig, 0, sizeof (sig)); |
| 206 | u8 *icv = |
| 207 | vlib_buffer_get_current (i_b0) + i_b0->current_length - |
| 208 | icv_size; |
| 209 | i_b0->current_length -= icv_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 211 | hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len, |
| 212 | (u8 *) esp0, i_b0->current_length, sig, sa0->use_esn, |
| 213 | sa0->seq_hi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 215 | if (PREDICT_FALSE (memcmp (icv, sig, icv_size))) |
| 216 | { |
| 217 | vlib_node_increment_counter (vm, esp_decrypt_node.index, |
| 218 | ESP_DECRYPT_ERROR_INTEG_ERROR, |
| 219 | 1); |
| 220 | o_bi0 = i_bi0; |
Sergio Gonzalez Monroy | 73c0110 | 2016-09-06 14:06:06 +0100 | [diff] [blame] | 221 | to_next[0] = o_bi0; |
| 222 | to_next += 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 223 | goto trace; |
| 224 | } |
| 225 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 227 | if (PREDICT_TRUE (sa0->use_anti_replay)) |
| 228 | { |
| 229 | if (PREDICT_TRUE (sa0->use_esn)) |
| 230 | esp_replay_advance_esn (sa0, seq); |
| 231 | else |
| 232 | esp_replay_advance (sa0, seq); |
| 233 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 235 | /* grab free buffer */ |
| 236 | uword last_empty_buffer = vec_len (empty_buffers) - 1; |
| 237 | o_bi0 = empty_buffers[last_empty_buffer]; |
Sergio Gonzalez Monroy | 73c0110 | 2016-09-06 14:06:06 +0100 | [diff] [blame] | 238 | to_next[0] = o_bi0; |
| 239 | to_next += 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 240 | o_b0 = vlib_get_buffer (vm, o_bi0); |
| 241 | vlib_prefetch_buffer_with_index (vm, |
| 242 | empty_buffers[last_empty_buffer - |
| 243 | 1], STORE); |
| 244 | _vec_len (empty_buffers) = last_empty_buffer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 246 | /* add old buffer to the recycle list */ |
| 247 | vec_add1 (recycle, i_bi0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | |
“mukeshyadav1984” | 72454dd | 2017-11-28 10:52:34 -0800 | [diff] [blame^] | 249 | if ((sa0->crypto_alg >= IPSEC_CRYPTO_ALG_AES_CBC_128 && |
| 250 | sa0->crypto_alg <= IPSEC_CRYPTO_ALG_AES_CBC_256) || |
| 251 | (sa0->crypto_alg >= IPSEC_CRYPTO_ALG_DES_CBC && |
| 252 | sa0->crypto_alg <= IPSEC_CRYPTO_ALG_3DES_CBC)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 253 | { |
“mukeshyadav1984” | 72454dd | 2017-11-28 10:52:34 -0800 | [diff] [blame^] | 254 | const int BLOCK_SIZE = |
| 255 | em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].block_size;; |
| 256 | const int IV_SIZE = |
| 257 | em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].iv_size; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 258 | esp_footer_t *f0; |
| 259 | u8 ip_hdr_size = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 261 | int blocks = |
| 262 | (i_b0->current_length - sizeof (esp_header_t) - |
| 263 | IV_SIZE) / BLOCK_SIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 265 | o_b0->current_data = sizeof (ethernet_header_t); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 267 | /* transport mode */ |
| 268 | if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6)) |
| 269 | { |
| 270 | tunnel_mode = 0; |
| 271 | ih4 = |
| 272 | (ip4_header_t *) (i_b0->data + |
| 273 | sizeof (ethernet_header_t)); |
| 274 | if (PREDICT_TRUE |
| 275 | ((ih4->ip_version_and_header_length & 0xF0) != 0x40)) |
| 276 | { |
| 277 | if (PREDICT_TRUE |
| 278 | ((ih4->ip_version_and_header_length & 0xF0) == |
| 279 | 0x60)) |
| 280 | { |
| 281 | transport_ip6 = 1; |
| 282 | ip_hdr_size = sizeof (ip6_header_t); |
| 283 | ih6 = |
| 284 | (ip6_header_t *) (i_b0->data + |
| 285 | sizeof (ethernet_header_t)); |
| 286 | oh6 = vlib_buffer_get_current (o_b0); |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | vlib_node_increment_counter (vm, |
| 291 | esp_decrypt_node.index, |
| 292 | ESP_DECRYPT_ERROR_NOT_IP, |
| 293 | 1); |
| 294 | o_b0 = 0; |
| 295 | goto trace; |
| 296 | } |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | oh4 = vlib_buffer_get_current (o_b0); |
| 301 | ip_hdr_size = sizeof (ip4_header_t); |
| 302 | } |
| 303 | } |
Matus Fabian | efc3330 | 2016-07-11 07:08:55 -0700 | [diff] [blame] | 304 | |
“mukeshyadav1984” | 72454dd | 2017-11-28 10:52:34 -0800 | [diff] [blame^] | 305 | esp_decrypt_cbc (sa0->crypto_alg, |
| 306 | esp0->data + IV_SIZE, |
| 307 | (u8 *) vlib_buffer_get_current (o_b0) + |
| 308 | ip_hdr_size, BLOCK_SIZE * blocks, |
| 309 | sa0->crypto_key, esp0->data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | |
“mukeshyadav1984” | 72454dd | 2017-11-28 10:52:34 -0800 | [diff] [blame^] | 311 | o_b0->current_length = (blocks * BLOCK_SIZE) - 2 + ip_hdr_size; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 312 | o_b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 313 | f0 = |
| 314 | (esp_footer_t *) ((u8 *) vlib_buffer_get_current (o_b0) + |
| 315 | o_b0->current_length); |
| 316 | o_b0->current_length -= f0->pad_length; |
Matus Fabian | efc3330 | 2016-07-11 07:08:55 -0700 | [diff] [blame] | 317 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 318 | /* tunnel mode */ |
| 319 | if (PREDICT_TRUE (tunnel_mode)) |
| 320 | { |
| 321 | if (PREDICT_TRUE (f0->next_header == IP_PROTOCOL_IP_IN_IP)) |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 322 | { |
| 323 | next0 = ESP_DECRYPT_NEXT_IP4_INPUT; |
| 324 | oh4 = vlib_buffer_get_current (o_b0); |
| 325 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 326 | else if (f0->next_header == IP_PROTOCOL_IPV6) |
| 327 | next0 = ESP_DECRYPT_NEXT_IP6_INPUT; |
| 328 | else |
| 329 | { |
| 330 | clib_warning ("next header: 0x%x", f0->next_header); |
| 331 | vlib_node_increment_counter (vm, esp_decrypt_node.index, |
| 332 | ESP_DECRYPT_ERROR_DECRYPTION_FAILED, |
| 333 | 1); |
| 334 | o_b0 = 0; |
| 335 | goto trace; |
| 336 | } |
| 337 | } |
| 338 | /* transport mode */ |
| 339 | else |
| 340 | { |
| 341 | if (PREDICT_FALSE (transport_ip6)) |
| 342 | { |
| 343 | next0 = ESP_DECRYPT_NEXT_IP6_INPUT; |
| 344 | oh6->ip_version_traffic_class_and_flow_label = |
| 345 | ih6->ip_version_traffic_class_and_flow_label; |
| 346 | oh6->protocol = f0->next_header; |
| 347 | oh6->hop_limit = ih6->hop_limit; |
| 348 | oh6->src_address.as_u64[0] = ih6->src_address.as_u64[0]; |
| 349 | oh6->src_address.as_u64[1] = ih6->src_address.as_u64[1]; |
| 350 | oh6->dst_address.as_u64[0] = ih6->dst_address.as_u64[0]; |
| 351 | oh6->dst_address.as_u64[1] = ih6->dst_address.as_u64[1]; |
| 352 | oh6->payload_length = |
| 353 | clib_host_to_net_u16 (vlib_buffer_length_in_chain |
| 354 | (vm, |
| 355 | o_b0) - sizeof (ip6_header_t)); |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | next0 = ESP_DECRYPT_NEXT_IP4_INPUT; |
| 360 | oh4->ip_version_and_header_length = 0x45; |
| 361 | oh4->tos = ih4->tos; |
| 362 | oh4->fragment_id = 0; |
| 363 | oh4->flags_and_fragment_offset = 0; |
| 364 | oh4->ttl = ih4->ttl; |
| 365 | oh4->protocol = f0->next_header; |
| 366 | oh4->src_address.as_u32 = ih4->src_address.as_u32; |
| 367 | oh4->dst_address.as_u32 = ih4->dst_address.as_u32; |
| 368 | oh4->length = |
| 369 | clib_host_to_net_u16 (vlib_buffer_length_in_chain |
| 370 | (vm, o_b0)); |
| 371 | oh4->checksum = ip4_header_checksum (oh4); |
| 372 | } |
| 373 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 374 | |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 375 | /* for IPSec-GRE tunnel next node is ipsec-gre-input */ |
| 376 | if (PREDICT_FALSE |
Damjan Marion | 9c6ae5f | 2016-11-15 23:20:01 +0100 | [diff] [blame] | 377 | ((vnet_buffer (i_b0)->ipsec.flags) & |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 378 | IPSEC_FLAG_IPSEC_GRE_TUNNEL)) |
| 379 | next0 = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT; |
| 380 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 381 | vnet_buffer (o_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
Matthew Smith | 02e14b5 | 2017-09-14 09:05:35 -0500 | [diff] [blame] | 382 | vnet_buffer (o_b0)->sw_if_index[VLIB_RX] = |
| 383 | vnet_buffer (i_b0)->sw_if_index[VLIB_RX]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 384 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 386 | trace: |
| 387 | if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 388 | { |
| 389 | if (o_b0) |
| 390 | { |
| 391 | o_b0->flags |= VLIB_BUFFER_IS_TRACED; |
| 392 | o_b0->trace_index = i_b0->trace_index; |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 393 | esp_decrypt_trace_t *tr = |
| 394 | vlib_add_trace (vm, node, o_b0, sizeof (*tr)); |
| 395 | tr->crypto_alg = sa0->crypto_alg; |
| 396 | tr->integ_alg = sa0->integ_alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 397 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 398 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | |
| 400 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 401 | n_left_to_next, o_bi0, next0); |
| 402 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 404 | } |
| 405 | vlib_node_increment_counter (vm, esp_decrypt_node.index, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 406 | ESP_DECRYPT_ERROR_RX_PKTS, |
| 407 | from_frame->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | |
| 409 | free_buffers_and_exit: |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 410 | if (recycle) |
| 411 | vlib_buffer_free (vm, recycle, vec_len (recycle)); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 412 | vec_free (recycle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | return from_frame->n_vectors; |
| 414 | } |
| 415 | |
| 416 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 417 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | VLIB_REGISTER_NODE (esp_decrypt_node) = { |
| 419 | .function = esp_decrypt_node_fn, |
| 420 | .name = "esp-decrypt", |
| 421 | .vector_size = sizeof (u32), |
| 422 | .format_trace = format_esp_decrypt_trace, |
| 423 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 424 | |
| 425 | .n_errors = ARRAY_LEN(esp_decrypt_error_strings), |
| 426 | .error_strings = esp_decrypt_error_strings, |
| 427 | |
| 428 | .n_next_nodes = ESP_DECRYPT_N_NEXT, |
| 429 | .next_nodes = { |
| 430 | #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n, |
| 431 | foreach_esp_decrypt_next |
| 432 | #undef _ |
| 433 | }, |
| 434 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 435 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 437 | VLIB_NODE_FUNCTION_MULTIARCH (esp_decrypt_node, esp_decrypt_node_fn) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 438 | /* |
| 439 | * fd.io coding-style-patch-verification: ON |
| 440 | * |
| 441 | * Local Variables: |
| 442 | * eval: (c-set-style "gnu") |
| 443 | * End: |
| 444 | */ |