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 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 115 | always_inline uword |
| 116 | esp_decrypt_inline (vlib_main_t * vm, |
| 117 | vlib_node_runtime_t * node, vlib_frame_t * from_frame, |
| 118 | int is_ip6) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | { |
| 120 | u32 n_left_from, *from, next_index, *to_next; |
| 121 | ipsec_main_t *im = &ipsec_main; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 122 | ipsec_proto_main_t *em = &ipsec_proto_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 123 | u32 *recycle = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | from = vlib_frame_vector_args (from_frame); |
| 125 | n_left_from = from_frame->n_vectors; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 126 | u32 thread_index = vlib_get_thread_index (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 128 | ipsec_alloc_empty_buffers (vm, im); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 130 | u32 *empty_buffers = im->empty_buffers[thread_index]; |
Matus Fabian | 141977f | 2016-07-04 05:36:52 -0700 | [diff] [blame] | 131 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 132 | if (PREDICT_FALSE (vec_len (empty_buffers) < n_left_from)) |
| 133 | { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 134 | if (is_ip6) |
| 135 | vlib_node_increment_counter (vm, esp6_decrypt_node.index, |
| 136 | ESP_DECRYPT_ERROR_NO_BUFFER, |
| 137 | n_left_from); |
| 138 | else |
| 139 | vlib_node_increment_counter (vm, esp4_decrypt_node.index, |
| 140 | ESP_DECRYPT_ERROR_NO_BUFFER, |
| 141 | n_left_from); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 142 | goto free_buffers_and_exit; |
| 143 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | |
| 145 | next_index = node->cached_next_index; |
| 146 | |
| 147 | while (n_left_from > 0) |
| 148 | { |
| 149 | u32 n_left_to_next; |
| 150 | |
| 151 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 152 | |
| 153 | while (n_left_from > 0 && n_left_to_next > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 154 | { |
| 155 | u32 i_bi0, o_bi0 = (u32) ~ 0, next0; |
| 156 | vlib_buffer_t *i_b0; |
| 157 | vlib_buffer_t *o_b0 = 0; |
| 158 | esp_header_t *esp0; |
| 159 | ipsec_sa_t *sa0; |
| 160 | u32 sa_index0 = ~0; |
| 161 | u32 seq; |
| 162 | ip4_header_t *ih4 = 0, *oh4 = 0; |
| 163 | ip6_header_t *ih6 = 0, *oh6 = 0; |
| 164 | u8 tunnel_mode = 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 | i_bi0 = from[0]; |
| 167 | from += 1; |
| 168 | n_left_from -= 1; |
| 169 | n_left_to_next -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 171 | next0 = ESP_DECRYPT_NEXT_DROP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 173 | i_b0 = vlib_get_buffer (vm, i_bi0); |
| 174 | esp0 = vlib_buffer_get_current (i_b0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
Damjan Marion | 9c6ae5f | 2016-11-15 23:20:01 +0100 | [diff] [blame] | 176 | sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 177 | sa0 = pool_elt_at_index (im->sad, sa_index0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 179 | seq = clib_host_to_net_u32 (esp0->seq); |
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 | /* anti-replay check */ |
| 182 | if (sa0->use_anti_replay) |
| 183 | { |
| 184 | int rv = 0; |
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_TRUE (sa0->use_esn)) |
| 187 | rv = esp_replay_check_esn (sa0, seq); |
| 188 | else |
| 189 | rv = esp_replay_check (sa0, seq); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 191 | if (PREDICT_FALSE (rv)) |
| 192 | { |
| 193 | clib_warning ("anti-replay SPI %u seq %u", sa0->spi, seq); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 194 | if (is_ip6) |
| 195 | vlib_node_increment_counter (vm, |
| 196 | esp6_decrypt_node.index, |
| 197 | ESP_DECRYPT_ERROR_REPLAY, 1); |
| 198 | else |
| 199 | vlib_node_increment_counter (vm, |
| 200 | esp4_decrypt_node.index, |
| 201 | ESP_DECRYPT_ERROR_REPLAY, 1); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 202 | o_bi0 = i_bi0; |
Sergio Gonzalez Monroy | 73c0110 | 2016-09-06 14:06:06 +0100 | [diff] [blame] | 203 | to_next[0] = o_bi0; |
| 204 | to_next += 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 205 | goto trace; |
| 206 | } |
| 207 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | |
Radu Nicolau | cb33dc2 | 2017-02-16 16:49:46 +0000 | [diff] [blame] | 209 | sa0->total_data_size += i_b0->current_length; |
| 210 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 211 | if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE)) |
| 212 | { |
| 213 | u8 sig[64]; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 214 | int icv_size = |
| 215 | em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 216 | clib_memset (sig, 0, sizeof (sig)); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 217 | u8 *icv = |
| 218 | vlib_buffer_get_current (i_b0) + i_b0->current_length - |
| 219 | icv_size; |
| 220 | i_b0->current_length -= icv_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 222 | hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len, |
| 223 | (u8 *) esp0, i_b0->current_length, sig, sa0->use_esn, |
| 224 | sa0->seq_hi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 226 | if (PREDICT_FALSE (memcmp (icv, sig, icv_size))) |
| 227 | { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 228 | if (is_ip6) |
| 229 | vlib_node_increment_counter (vm, |
| 230 | esp6_decrypt_node.index, |
| 231 | ESP_DECRYPT_ERROR_INTEG_ERROR, |
| 232 | 1); |
| 233 | else |
| 234 | vlib_node_increment_counter (vm, |
| 235 | esp4_decrypt_node.index, |
| 236 | ESP_DECRYPT_ERROR_INTEG_ERROR, |
| 237 | 1); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 238 | o_bi0 = i_bi0; |
Sergio Gonzalez Monroy | 73c0110 | 2016-09-06 14:06:06 +0100 | [diff] [blame] | 239 | to_next[0] = o_bi0; |
| 240 | to_next += 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 241 | goto trace; |
| 242 | } |
| 243 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 245 | if (PREDICT_TRUE (sa0->use_anti_replay)) |
| 246 | { |
| 247 | if (PREDICT_TRUE (sa0->use_esn)) |
| 248 | esp_replay_advance_esn (sa0, seq); |
| 249 | else |
| 250 | esp_replay_advance (sa0, seq); |
| 251 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 253 | /* grab free buffer */ |
| 254 | uword last_empty_buffer = vec_len (empty_buffers) - 1; |
| 255 | o_bi0 = empty_buffers[last_empty_buffer]; |
Sergio Gonzalez Monroy | 73c0110 | 2016-09-06 14:06:06 +0100 | [diff] [blame] | 256 | to_next[0] = o_bi0; |
| 257 | to_next += 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 258 | o_b0 = vlib_get_buffer (vm, o_bi0); |
| 259 | vlib_prefetch_buffer_with_index (vm, |
| 260 | empty_buffers[last_empty_buffer - |
| 261 | 1], STORE); |
| 262 | _vec_len (empty_buffers) = last_empty_buffer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 264 | /* add old buffer to the recycle list */ |
| 265 | vec_add1 (recycle, i_bi0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | |
“mukeshyadav1984” | 72454dd | 2017-11-28 10:52:34 -0800 | [diff] [blame] | 267 | if ((sa0->crypto_alg >= IPSEC_CRYPTO_ALG_AES_CBC_128 && |
| 268 | sa0->crypto_alg <= IPSEC_CRYPTO_ALG_AES_CBC_256) || |
| 269 | (sa0->crypto_alg >= IPSEC_CRYPTO_ALG_DES_CBC && |
| 270 | sa0->crypto_alg <= IPSEC_CRYPTO_ALG_3DES_CBC)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 271 | { |
“mukeshyadav1984” | 72454dd | 2017-11-28 10:52:34 -0800 | [diff] [blame] | 272 | const int BLOCK_SIZE = |
| 273 | em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].block_size;; |
| 274 | const int IV_SIZE = |
| 275 | em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].iv_size; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 276 | esp_footer_t *f0; |
| 277 | u8 ip_hdr_size = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 278 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 279 | int blocks = |
| 280 | (i_b0->current_length - sizeof (esp_header_t) - |
| 281 | IV_SIZE) / BLOCK_SIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 283 | o_b0->current_data = sizeof (ethernet_header_t); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 285 | /* transport mode */ |
| 286 | if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6)) |
| 287 | { |
| 288 | tunnel_mode = 0; |
Szymon Sliwa | 8640dbd | 2018-03-19 15:14:31 +0000 | [diff] [blame] | 289 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 290 | if (is_ip6) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 291 | { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 292 | ih6 = |
| 293 | (ip6_header_t *) ((u8 *) esp0 - |
| 294 | sizeof (ip6_header_t)); |
| 295 | ip_hdr_size = sizeof (ip6_header_t); |
| 296 | oh6 = vlib_buffer_get_current (o_b0); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 297 | } |
| 298 | else |
| 299 | { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 300 | ih4 = |
| 301 | (ip4_header_t *) ((u8 *) esp0 - |
| 302 | sizeof (ip4_header_t)); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 303 | oh4 = vlib_buffer_get_current (o_b0); |
| 304 | ip_hdr_size = sizeof (ip4_header_t); |
| 305 | } |
| 306 | } |
Matus Fabian | efc3330 | 2016-07-11 07:08:55 -0700 | [diff] [blame] | 307 | |
“mukeshyadav1984” | 72454dd | 2017-11-28 10:52:34 -0800 | [diff] [blame] | 308 | esp_decrypt_cbc (sa0->crypto_alg, |
| 309 | esp0->data + IV_SIZE, |
| 310 | (u8 *) vlib_buffer_get_current (o_b0) + |
| 311 | ip_hdr_size, BLOCK_SIZE * blocks, |
| 312 | sa0->crypto_key, esp0->data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 313 | |
“mukeshyadav1984” | 72454dd | 2017-11-28 10:52:34 -0800 | [diff] [blame] | 314 | o_b0->current_length = (blocks * BLOCK_SIZE) - 2 + ip_hdr_size; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 315 | o_b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 316 | f0 = |
| 317 | (esp_footer_t *) ((u8 *) vlib_buffer_get_current (o_b0) + |
| 318 | o_b0->current_length); |
| 319 | o_b0->current_length -= f0->pad_length; |
Matus Fabian | efc3330 | 2016-07-11 07:08:55 -0700 | [diff] [blame] | 320 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 321 | /* tunnel mode */ |
| 322 | if (PREDICT_TRUE (tunnel_mode)) |
| 323 | { |
| 324 | if (PREDICT_TRUE (f0->next_header == IP_PROTOCOL_IP_IN_IP)) |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 325 | { |
| 326 | next0 = ESP_DECRYPT_NEXT_IP4_INPUT; |
| 327 | oh4 = vlib_buffer_get_current (o_b0); |
| 328 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 329 | else if (f0->next_header == IP_PROTOCOL_IPV6) |
| 330 | next0 = ESP_DECRYPT_NEXT_IP6_INPUT; |
| 331 | else |
| 332 | { |
| 333 | clib_warning ("next header: 0x%x", f0->next_header); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 334 | if (is_ip6) |
| 335 | vlib_node_increment_counter (vm, |
| 336 | esp6_decrypt_node.index, |
| 337 | ESP_DECRYPT_ERROR_DECRYPTION_FAILED, |
| 338 | 1); |
| 339 | else |
| 340 | vlib_node_increment_counter (vm, |
| 341 | esp4_decrypt_node.index, |
| 342 | ESP_DECRYPT_ERROR_DECRYPTION_FAILED, |
| 343 | 1); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 344 | o_b0 = 0; |
| 345 | goto trace; |
| 346 | } |
| 347 | } |
| 348 | /* transport mode */ |
| 349 | else |
| 350 | { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 351 | if (is_ip6) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 352 | { |
| 353 | next0 = ESP_DECRYPT_NEXT_IP6_INPUT; |
| 354 | oh6->ip_version_traffic_class_and_flow_label = |
| 355 | ih6->ip_version_traffic_class_and_flow_label; |
| 356 | oh6->protocol = f0->next_header; |
| 357 | oh6->hop_limit = ih6->hop_limit; |
| 358 | oh6->src_address.as_u64[0] = ih6->src_address.as_u64[0]; |
| 359 | oh6->src_address.as_u64[1] = ih6->src_address.as_u64[1]; |
| 360 | oh6->dst_address.as_u64[0] = ih6->dst_address.as_u64[0]; |
| 361 | oh6->dst_address.as_u64[1] = ih6->dst_address.as_u64[1]; |
| 362 | oh6->payload_length = |
| 363 | clib_host_to_net_u16 (vlib_buffer_length_in_chain |
| 364 | (vm, |
| 365 | o_b0) - sizeof (ip6_header_t)); |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | next0 = ESP_DECRYPT_NEXT_IP4_INPUT; |
| 370 | oh4->ip_version_and_header_length = 0x45; |
| 371 | oh4->tos = ih4->tos; |
| 372 | oh4->fragment_id = 0; |
| 373 | oh4->flags_and_fragment_offset = 0; |
| 374 | oh4->ttl = ih4->ttl; |
| 375 | oh4->protocol = f0->next_header; |
| 376 | oh4->src_address.as_u32 = ih4->src_address.as_u32; |
| 377 | oh4->dst_address.as_u32 = ih4->dst_address.as_u32; |
| 378 | oh4->length = |
| 379 | clib_host_to_net_u16 (vlib_buffer_length_in_chain |
| 380 | (vm, o_b0)); |
| 381 | oh4->checksum = ip4_header_checksum (oh4); |
| 382 | } |
| 383 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 384 | |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 385 | /* for IPSec-GRE tunnel next node is ipsec-gre-input */ |
| 386 | if (PREDICT_FALSE |
Damjan Marion | 9c6ae5f | 2016-11-15 23:20:01 +0100 | [diff] [blame] | 387 | ((vnet_buffer (i_b0)->ipsec.flags) & |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 388 | IPSEC_FLAG_IPSEC_GRE_TUNNEL)) |
| 389 | next0 = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT; |
| 390 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 391 | vnet_buffer (o_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
Matthew Smith | 02e14b5 | 2017-09-14 09:05:35 -0500 | [diff] [blame] | 392 | vnet_buffer (o_b0)->sw_if_index[VLIB_RX] = |
| 393 | vnet_buffer (i_b0)->sw_if_index[VLIB_RX]; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 394 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 396 | trace: |
| 397 | if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 398 | { |
| 399 | if (o_b0) |
| 400 | { |
| 401 | o_b0->flags |= VLIB_BUFFER_IS_TRACED; |
| 402 | o_b0->trace_index = i_b0->trace_index; |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 403 | esp_decrypt_trace_t *tr = |
| 404 | vlib_add_trace (vm, node, o_b0, sizeof (*tr)); |
| 405 | tr->crypto_alg = sa0->crypto_alg; |
| 406 | tr->integ_alg = sa0->integ_alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 407 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 408 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | |
| 410 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 411 | n_left_to_next, o_bi0, next0); |
| 412 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 414 | } |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 415 | if (is_ip6) |
| 416 | vlib_node_increment_counter (vm, esp6_decrypt_node.index, |
| 417 | ESP_DECRYPT_ERROR_RX_PKTS, |
| 418 | from_frame->n_vectors); |
| 419 | else |
| 420 | vlib_node_increment_counter (vm, esp4_decrypt_node.index, |
| 421 | ESP_DECRYPT_ERROR_RX_PKTS, |
| 422 | from_frame->n_vectors); |
| 423 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 424 | |
| 425 | free_buffers_and_exit: |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 426 | if (recycle) |
| 427 | vlib_buffer_free (vm, recycle, vec_len (recycle)); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 428 | vec_free (recycle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 429 | return from_frame->n_vectors; |
| 430 | } |
| 431 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 432 | VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm, |
| 433 | vlib_node_runtime_t * node, |
| 434 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 435 | { |
| 436 | return esp_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ ); |
| 437 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 439 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 440 | VLIB_REGISTER_NODE (esp4_decrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 441 | .name = "esp4-decrypt", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 442 | .vector_size = sizeof (u32), |
| 443 | .format_trace = format_esp_decrypt_trace, |
| 444 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 445 | |
| 446 | .n_errors = ARRAY_LEN(esp_decrypt_error_strings), |
| 447 | .error_strings = esp_decrypt_error_strings, |
| 448 | |
| 449 | .n_next_nodes = ESP_DECRYPT_N_NEXT, |
| 450 | .next_nodes = { |
| 451 | #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n, |
| 452 | foreach_esp_decrypt_next |
| 453 | #undef _ |
| 454 | }, |
| 455 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 456 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 458 | VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm, |
| 459 | vlib_node_runtime_t * node, |
| 460 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 461 | { |
| 462 | return esp_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ ); |
| 463 | } |
| 464 | |
| 465 | /* *INDENT-OFF* */ |
| 466 | VLIB_REGISTER_NODE (esp6_decrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 467 | .name = "esp6-decrypt", |
| 468 | .vector_size = sizeof (u32), |
| 469 | .format_trace = format_esp_decrypt_trace, |
| 470 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 471 | |
| 472 | .n_errors = ARRAY_LEN(esp_decrypt_error_strings), |
| 473 | .error_strings = esp_decrypt_error_strings, |
| 474 | |
| 475 | .n_next_nodes = ESP_DECRYPT_N_NEXT, |
| 476 | .next_nodes = { |
| 477 | #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n, |
| 478 | foreach_esp_decrypt_next |
| 479 | #undef _ |
| 480 | }, |
| 481 | }; |
| 482 | /* *INDENT-ON* */ |
| 483 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 484 | /* |
| 485 | * fd.io coding-style-patch-verification: ON |
| 486 | * |
| 487 | * Local Variables: |
| 488 | * eval: (c-set-style "gnu") |
| 489 | * End: |
| 490 | */ |