Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * esp_decrypt.c : IPSec ESP Decrypt node using DPDK Cryptodev |
| 3 | * |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 4 | * Copyright (c) 2017 Intel and/or its affiliates. |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 7 | * You may obtain a opy of the License at: |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 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> |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 23 | #include <vnet/ipsec/esp.h> |
Damjan Marion | a5c308e | 2019-01-20 02:02:51 +0100 | [diff] [blame] | 24 | #include <dpdk/buffer.h> |
Damjan Marion | c3a814b | 2017-02-28 19:22:22 +0100 | [diff] [blame] | 25 | #include <dpdk/ipsec/ipsec.h> |
Damjan Marion | c3a814b | 2017-02-28 19:22:22 +0100 | [diff] [blame] | 26 | #include <dpdk/device/dpdk.h> |
| 27 | #include <dpdk/device/dpdk_priv.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 28 | |
| 29 | #define foreach_esp_decrypt_next \ |
| 30 | _(DROP, "error-drop") \ |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 31 | _(IP4_INPUT, "ip4-input-no-checksum") \ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 32 | _(IP6_INPUT, "ip6-input") |
| 33 | |
| 34 | #define _(v, s) ESP_DECRYPT_NEXT_##v, |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 35 | typedef enum |
| 36 | { |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 37 | foreach_esp_decrypt_next |
| 38 | #undef _ |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 39 | ESP_DECRYPT_N_NEXT, |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 40 | } esp_decrypt_next_t; |
| 41 | |
| 42 | #define foreach_esp_decrypt_error \ |
| 43 | _(RX_PKTS, "ESP pkts received") \ |
| 44 | _(DECRYPTION_FAILED, "ESP decryption failed") \ |
| 45 | _(REPLAY, "SA replayed packet") \ |
| 46 | _(NOT_IP, "Not IP packet (dropped)") \ |
Christian Hopps | 942b980 | 2020-07-14 09:41:43 -0400 | [diff] [blame] | 47 | _(ENQ_FAIL, "Enqueue decrypt failed (queue full)") \ |
Christian Hopps | f6cb044 | 2020-07-14 08:39:30 -0400 | [diff] [blame] | 48 | _(DISCARD, "Not enough crypto operations") \ |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 49 | _(BAD_LEN, "Invalid ciphertext length") \ |
| 50 | _(SESSION, "Failed to get crypto session") \ |
| 51 | _(NOSUP, "Cipher/Auth not supported") |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 52 | |
| 53 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 54 | typedef enum |
| 55 | { |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 56 | #define _(sym,str) ESP_DECRYPT_ERROR_##sym, |
| 57 | foreach_esp_decrypt_error |
| 58 | #undef _ |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 59 | ESP_DECRYPT_N_ERROR, |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 60 | } esp_decrypt_error_t; |
| 61 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 62 | static char *esp_decrypt_error_strings[] = { |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 63 | #define _(sym,string) string, |
| 64 | foreach_esp_decrypt_error |
| 65 | #undef _ |
| 66 | }; |
| 67 | |
Kingwel Xie | 040950a | 2018-11-28 02:10:35 -0500 | [diff] [blame] | 68 | extern vlib_node_registration_t dpdk_esp4_decrypt_node; |
| 69 | extern vlib_node_registration_t dpdk_esp6_decrypt_node; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 70 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 71 | typedef struct |
| 72 | { |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 73 | ipsec_crypto_alg_t crypto_alg; |
| 74 | ipsec_integ_alg_t integ_alg; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 75 | u8 packet_data[64]; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 76 | } esp_decrypt_trace_t; |
| 77 | |
| 78 | /* packet trace format function */ |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 79 | static u8 * |
| 80 | format_esp_decrypt_trace (u8 * s, va_list * args) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 81 | { |
| 82 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 83 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 84 | esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *); |
Gabriel Ganne | e3ea797 | 2017-10-12 10:53:31 +0200 | [diff] [blame] | 85 | u32 indent = format_get_indent (s); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 86 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 87 | s = format (s, "cipher %U auth %U\n", |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 88 | format_ipsec_crypto_alg, t->crypto_alg, |
| 89 | format_ipsec_integ_alg, t->integ_alg); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 90 | s = format (s, "%U%U", |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 91 | format_white_space, indent, format_esp_header, t->packet_data); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 92 | return s; |
| 93 | } |
| 94 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 95 | always_inline uword |
| 96 | dpdk_esp_decrypt_inline (vlib_main_t * vm, |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 97 | vlib_node_runtime_t * node, |
| 98 | vlib_frame_t * from_frame, int is_ip6) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 99 | { |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 100 | u32 n_left_from, *from, *to_next, next_index, thread_index; |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 101 | u32 thread_idx = vlib_get_thread_index (); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 102 | dpdk_crypto_main_t *dcm = &dpdk_crypto_main; |
| 103 | crypto_resource_t *res = 0; |
| 104 | ipsec_sa_t *sa0 = 0; |
| 105 | crypto_alg_t *cipher_alg = 0, *auth_alg = 0; |
| 106 | struct rte_cryptodev_sym_session *session = 0; |
| 107 | u32 ret, last_sa_index = ~0; |
| 108 | u8 numa = rte_socket_id (); |
| 109 | u8 is_aead = 0; |
| 110 | crypto_worker_main_t *cwm = |
| 111 | vec_elt_at_index (dcm->workers_main, thread_idx); |
| 112 | struct rte_crypto_op **ops = cwm->ops; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 113 | |
| 114 | from = vlib_frame_vector_args (from_frame); |
| 115 | n_left_from = from_frame->n_vectors; |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 116 | thread_index = vm->thread_index; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 117 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 118 | ret = crypto_alloc_ops (numa, ops, n_left_from); |
| 119 | if (ret) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 120 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 121 | if (is_ip6) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 122 | vlib_node_increment_counter (vm, dpdk_esp6_decrypt_node.index, |
Christian Hopps | f6cb044 | 2020-07-14 08:39:30 -0400 | [diff] [blame] | 123 | ESP_DECRYPT_ERROR_DISCARD, n_left_from); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 124 | else |
| 125 | vlib_node_increment_counter (vm, dpdk_esp4_decrypt_node.index, |
Christian Hopps | f6cb044 | 2020-07-14 08:39:30 -0400 | [diff] [blame] | 126 | ESP_DECRYPT_ERROR_DISCARD, n_left_from); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 127 | /* Discard whole frame */ |
Christian Hopps | f6cb044 | 2020-07-14 08:39:30 -0400 | [diff] [blame] | 128 | vlib_buffer_free (vm, from, n_left_from); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 129 | return n_left_from; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 132 | next_index = ESP_DECRYPT_NEXT_DROP; |
| 133 | |
| 134 | while (n_left_from > 0) |
| 135 | { |
| 136 | u32 n_left_to_next; |
| 137 | |
| 138 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 139 | |
| 140 | while (n_left_from > 0 && n_left_to_next > 0) |
| 141 | { |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 142 | clib_error_t *error; |
Damjan Marion | 1f4e1cb | 2019-03-28 19:19:31 +0100 | [diff] [blame] | 143 | u32 bi0, sa_index0, iv_size; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 144 | u8 trunc_size; |
| 145 | vlib_buffer_t *b0; |
| 146 | esp_header_t *esp0; |
| 147 | struct rte_mbuf *mb0; |
| 148 | struct rte_crypto_op *op; |
| 149 | u16 res_idx; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 150 | |
| 151 | bi0 = from[0]; |
| 152 | from += 1; |
| 153 | n_left_from -= 1; |
| 154 | |
| 155 | b0 = vlib_get_buffer (vm, bi0); |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 156 | mb0 = rte_mbuf_from_vlib_buffer (b0); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 157 | esp0 = vlib_buffer_get_current (b0); |
| 158 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 159 | /* ih0/ih6_0 */ |
| 160 | CLIB_PREFETCH (esp0, sizeof (esp0[0]) + 16, LOAD); |
| 161 | /* mb0 */ |
| 162 | CLIB_PREFETCH (mb0, CLIB_CACHE_LINE_BYTES, STORE); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 163 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 164 | op = ops[0]; |
| 165 | ops += 1; |
| 166 | ASSERT (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED); |
| 167 | |
| 168 | dpdk_op_priv_t *priv = crypto_op_get_priv (op); |
Kingwel Xie | 3b3464e | 2019-02-13 02:48:41 -0500 | [diff] [blame] | 169 | /* store bi in op private */ |
| 170 | priv->bi = bi0; |
Sergio Gonzalez Monroy | d8a34a5 | 2019-05-06 22:44:14 +0200 | [diff] [blame] | 171 | priv->encrypt = 0; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 172 | |
| 173 | u16 op_len = |
| 174 | sizeof (op[0]) + sizeof (op[0].sym[0]) + sizeof (priv[0]); |
| 175 | CLIB_PREFETCH (op, op_len, STORE); |
| 176 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 177 | sa_index0 = vnet_buffer (b0)->ipsec.sad_index; |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 178 | vlib_prefetch_combined_counter (&ipsec_sa_counters, |
| 179 | thread_index, sa_index0); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 180 | |
| 181 | if (sa_index0 != last_sa_index) |
| 182 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 183 | sa0 = ipsec_sa_get (sa_index0); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 184 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 185 | cipher_alg = |
| 186 | vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 187 | auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg); |
| 188 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 189 | is_aead = (cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 190 | if (is_aead) |
| 191 | auth_alg = cipher_alg; |
| 192 | |
| 193 | res_idx = get_resource (cwm, sa0); |
| 194 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 195 | if (PREDICT_FALSE (res_idx == (u16) ~ 0)) |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 196 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 197 | if (is_ip6) |
| 198 | vlib_node_increment_counter (vm, |
| 199 | dpdk_esp6_decrypt_node.index, |
| 200 | ESP_DECRYPT_ERROR_NOSUP, 1); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 201 | else |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 202 | vlib_node_increment_counter (vm, |
| 203 | dpdk_esp4_decrypt_node.index, |
| 204 | ESP_DECRYPT_ERROR_NOSUP, 1); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 205 | to_next[0] = bi0; |
| 206 | to_next += 1; |
| 207 | n_left_to_next -= 1; |
| 208 | goto trace; |
| 209 | } |
| 210 | res = vec_elt_at_index (dcm->resource, res_idx); |
| 211 | |
| 212 | error = crypto_get_session (&session, sa_index0, res, cwm, 0); |
| 213 | if (PREDICT_FALSE (error || !session)) |
| 214 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 215 | if (is_ip6) |
| 216 | vlib_node_increment_counter (vm, |
| 217 | dpdk_esp6_decrypt_node.index, |
| 218 | ESP_DECRYPT_ERROR_SESSION, |
| 219 | 1); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 220 | else |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 221 | vlib_node_increment_counter (vm, |
| 222 | dpdk_esp4_decrypt_node.index, |
| 223 | ESP_DECRYPT_ERROR_SESSION, |
| 224 | 1); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 225 | to_next[0] = bi0; |
| 226 | to_next += 1; |
| 227 | n_left_to_next -= 1; |
| 228 | goto trace; |
| 229 | } |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 230 | |
| 231 | last_sa_index = sa_index0; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 232 | } |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 233 | |
| 234 | /* anti-replay check */ |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 235 | if (ipsec_sa_anti_replay_check |
| 236 | (sa0, clib_host_to_net_u32 (esp0->seq))) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 237 | { |
Damjan Marion | 1f4e1cb | 2019-03-28 19:19:31 +0100 | [diff] [blame] | 238 | if (is_ip6) |
| 239 | vlib_node_increment_counter (vm, |
| 240 | dpdk_esp6_decrypt_node.index, |
| 241 | ESP_DECRYPT_ERROR_REPLAY, 1); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 242 | else |
Damjan Marion | 1f4e1cb | 2019-03-28 19:19:31 +0100 | [diff] [blame] | 243 | vlib_node_increment_counter (vm, |
| 244 | dpdk_esp4_decrypt_node.index, |
| 245 | ESP_DECRYPT_ERROR_REPLAY, 1); |
| 246 | to_next[0] = bi0; |
| 247 | to_next += 1; |
| 248 | n_left_to_next -= 1; |
| 249 | goto trace; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 252 | if (is_ip6) |
Klement Sekera | 7334393 | 2018-10-22 13:17:19 +0200 | [diff] [blame] | 253 | priv->next = DPDK_CRYPTO_INPUT_NEXT_DECRYPT6_POST; |
| 254 | else |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 255 | { |
| 256 | priv->next = DPDK_CRYPTO_INPUT_NEXT_DECRYPT4_POST; |
| 257 | b0->flags |= VNET_BUFFER_F_IS_IP4; |
| 258 | } |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 259 | |
| 260 | /* FIXME multi-seg */ |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 261 | vlib_increment_combined_counter |
| 262 | (&ipsec_sa_counters, thread_index, sa_index0, |
| 263 | 1, b0->current_length); |
Radu Nicolau | cb33dc2 | 2017-02-16 16:49:46 +0000 | [diff] [blame] | 264 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 265 | res->ops[res->n_ops] = op; |
| 266 | res->bi[res->n_ops] = bi0; |
| 267 | res->n_ops += 1; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 268 | |
| 269 | /* Convert vlib buffer to mbuf */ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 270 | mb0->data_len = b0->current_length; |
| 271 | mb0->pkt_len = b0->current_length; |
| 272 | mb0->data_off = RTE_PKTMBUF_HEADROOM + b0->current_data; |
| 273 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 274 | trunc_size = auth_alg->trunc_size; |
| 275 | iv_size = cipher_alg->iv_len; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 276 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 277 | /* Outer IP header has already been stripped */ |
| 278 | u16 payload_len = |
| 279 | b0->current_length - sizeof (esp_header_t) - iv_size - trunc_size; |
| 280 | |
| 281 | ASSERT (payload_len >= 4); |
| 282 | |
| 283 | if (payload_len & (cipher_alg->boundary - 1)) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 284 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 285 | if (is_ip6) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 286 | vlib_node_increment_counter (vm, dpdk_esp6_decrypt_node.index, |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 287 | ESP_DECRYPT_ERROR_BAD_LEN, 1); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 288 | else |
| 289 | vlib_node_increment_counter (vm, dpdk_esp4_decrypt_node.index, |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 290 | ESP_DECRYPT_ERROR_BAD_LEN, 1); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 291 | res->n_ops -= 1; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 292 | to_next[0] = bi0; |
| 293 | to_next += 1; |
| 294 | n_left_to_next -= 1; |
| 295 | goto trace; |
| 296 | } |
| 297 | |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 298 | u32 cipher_off, cipher_len; |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 299 | u32 auth_len = 0; |
Sergio Gonzalez Monroy | aa3a25c | 2017-10-31 15:55:39 +0000 | [diff] [blame] | 300 | u8 *aad = NULL; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 301 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 302 | u8 *iv = (u8 *) (esp0 + 1); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 303 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 304 | dpdk_gcm_cnt_blk *icb = &priv->cb; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 305 | |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 306 | cipher_off = sizeof (esp_header_t) + iv_size; |
| 307 | cipher_len = payload_len; |
| 308 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 309 | u8 *digest = vlib_buffer_get_tail (b0) - trunc_size; |
Fan Zhang | f0419a0 | 2020-12-01 15:10:43 +0000 | [diff] [blame] | 310 | u64 digest_paddr = mb0->buf_iova + digest - ((u8 *) mb0->buf_addr); |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 311 | |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 312 | if (!is_aead && cipher_alg->alg == RTE_CRYPTO_CIPHER_AES_CBC) |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 313 | clib_memcpy_fast (icb, iv, 16); |
| 314 | else /* CTR/GCM */ |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 315 | { |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 316 | u32 *_iv = (u32 *) iv; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 317 | |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 318 | crypto_set_icb (icb, sa0->salt, _iv[0], _iv[1]); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 319 | } |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 320 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 321 | if (is_aead) |
| 322 | { |
| 323 | aad = priv->aad; |
| 324 | u32 *_aad = (u32 *) aad; |
| 325 | clib_memcpy_fast (aad, esp0, 8); |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 326 | |
| 327 | /* _aad[3] should always be 0 */ |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 328 | if (PREDICT_FALSE (ipsec_sa_is_set_USE_ESN (sa0))) |
Christian Hopps | d58419f | 2019-11-03 01:02:18 -0400 | [diff] [blame] | 329 | { |
| 330 | _aad[2] = _aad[1]; |
| 331 | _aad[1] = clib_host_to_net_u32 (sa0->seq_hi); |
| 332 | } |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 333 | else |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 334 | _aad[2] = 0; |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 335 | } |
| 336 | else |
| 337 | { |
| 338 | auth_len = sizeof (esp_header_t) + iv_size + payload_len; |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 339 | |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 340 | if (ipsec_sa_is_set_USE_ESN (sa0)) |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 341 | { |
| 342 | clib_memcpy_fast (priv->icv, digest, trunc_size); |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 343 | u32 *_digest = (u32 *) digest; |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 344 | _digest[0] = clib_host_to_net_u32 (sa0->seq_hi); |
| 345 | auth_len += sizeof (sa0->seq_hi); |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 346 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 347 | digest = priv->icv; |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 348 | digest_paddr = |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 349 | op->phys_addr + (uintptr_t) priv->icv - (uintptr_t) op; |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 350 | } |
| 351 | } |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 352 | |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 353 | crypto_op_setup (is_aead, mb0, op, session, cipher_off, cipher_len, |
| 354 | 0, auth_len, aad, digest, digest_paddr); |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 355 | trace: |
| 356 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 357 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 358 | esp_decrypt_trace_t *tr = |
| 359 | vlib_add_trace (vm, node, b0, sizeof (*tr)); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 360 | tr->crypto_alg = sa0->crypto_alg; |
| 361 | tr->integ_alg = sa0->integ_alg; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 362 | clib_memcpy_fast (tr->packet_data, vlib_buffer_get_current (b0), |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 363 | sizeof (esp_header_t)); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 367 | } |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 368 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 369 | if (is_ip6) |
| 370 | { |
| 371 | vlib_node_increment_counter (vm, dpdk_esp6_decrypt_node.index, |
| 372 | ESP_DECRYPT_ERROR_RX_PKTS, |
| 373 | from_frame->n_vectors); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 374 | |
Sergio Gonzalez Monroy | 35467f1 | 2019-01-30 11:26:00 +0100 | [diff] [blame] | 375 | crypto_enqueue_ops (vm, cwm, dpdk_esp6_decrypt_node.index, |
Sergio Gonzalez Monroy | d8a34a5 | 2019-05-06 22:44:14 +0200 | [diff] [blame] | 376 | ESP_DECRYPT_ERROR_ENQ_FAIL, numa, 0 /* encrypt */ ); |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 377 | } |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 378 | else |
| 379 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 380 | vlib_node_increment_counter (vm, dpdk_esp4_decrypt_node.index, |
| 381 | ESP_DECRYPT_ERROR_RX_PKTS, |
| 382 | from_frame->n_vectors); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 383 | |
Sergio Gonzalez Monroy | 35467f1 | 2019-01-30 11:26:00 +0100 | [diff] [blame] | 384 | crypto_enqueue_ops (vm, cwm, dpdk_esp4_decrypt_node.index, |
Sergio Gonzalez Monroy | d8a34a5 | 2019-05-06 22:44:14 +0200 | [diff] [blame] | 385 | ESP_DECRYPT_ERROR_ENQ_FAIL, numa, 0 /* encrypt */ ); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 386 | } |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 387 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 388 | crypto_free_ops (numa, ops, cwm->ops + from_frame->n_vectors - ops); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 389 | |
| 390 | return from_frame->n_vectors; |
| 391 | } |
| 392 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 393 | VLIB_NODE_FN (dpdk_esp4_decrypt_node) (vlib_main_t * vm, |
| 394 | vlib_node_runtime_t * node, |
| 395 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 396 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 397 | return dpdk_esp_decrypt_inline (vm, node, from_frame, 0 /*is_ip6 */ ); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Sergio Gonzalez Monroy | 539ed4f | 2017-05-17 11:43:41 +0100 | [diff] [blame] | 400 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 401 | VLIB_REGISTER_NODE (dpdk_esp4_decrypt_node) = { |
Klement Sekera | 7334393 | 2018-10-22 13:17:19 +0200 | [diff] [blame] | 402 | .name = "dpdk-esp4-decrypt", |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 403 | .vector_size = sizeof (u32), |
| 404 | .format_trace = format_esp_decrypt_trace, |
| 405 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 406 | |
| 407 | .n_errors = ARRAY_LEN(esp_decrypt_error_strings), |
| 408 | .error_strings = esp_decrypt_error_strings, |
| 409 | |
| 410 | .n_next_nodes = ESP_DECRYPT_N_NEXT, |
| 411 | .next_nodes = { |
| 412 | #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n, |
| 413 | foreach_esp_decrypt_next |
| 414 | #undef _ |
| 415 | }, |
| 416 | }; |
Sergio Gonzalez Monroy | 539ed4f | 2017-05-17 11:43:41 +0100 | [diff] [blame] | 417 | /* *INDENT-ON* */ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 418 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 419 | VLIB_NODE_FN (dpdk_esp6_decrypt_node) (vlib_main_t * vm, |
| 420 | vlib_node_runtime_t * node, |
| 421 | vlib_frame_t * from_frame) |
Matthew Smith | 1dfdb28 | 2018-10-22 16:37:25 -0500 | [diff] [blame] | 422 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 423 | return dpdk_esp_decrypt_inline (vm, node, from_frame, 1 /*is_ip6 */ ); |
Matthew Smith | 1dfdb28 | 2018-10-22 16:37:25 -0500 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* *INDENT-OFF* */ |
| 427 | VLIB_REGISTER_NODE (dpdk_esp6_decrypt_node) = { |
Matthew Smith | 1dfdb28 | 2018-10-22 16:37:25 -0500 | [diff] [blame] | 428 | .name = "dpdk-esp6-decrypt", |
| 429 | .vector_size = sizeof (u32), |
| 430 | .format_trace = format_esp_decrypt_trace, |
| 431 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 432 | |
| 433 | .n_errors = ARRAY_LEN(esp_decrypt_error_strings), |
| 434 | .error_strings = esp_decrypt_error_strings, |
| 435 | |
| 436 | .n_next_nodes = ESP_DECRYPT_N_NEXT, |
| 437 | .next_nodes = { |
| 438 | #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n, |
| 439 | foreach_esp_decrypt_next |
| 440 | #undef _ |
| 441 | }, |
| 442 | }; |
| 443 | /* *INDENT-ON* */ |
| 444 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 445 | /* |
| 446 | * Decrypt Post Node |
| 447 | */ |
| 448 | |
| 449 | #define foreach_esp_decrypt_post_error \ |
| 450 | _(PKTS, "ESP post pkts") |
| 451 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 452 | typedef enum |
| 453 | { |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 454 | #define _(sym,str) ESP_DECRYPT_POST_ERROR_##sym, |
| 455 | foreach_esp_decrypt_post_error |
| 456 | #undef _ |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 457 | ESP_DECRYPT_POST_N_ERROR, |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 458 | } esp_decrypt_post_error_t; |
| 459 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 460 | static char *esp_decrypt_post_error_strings[] = { |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 461 | #define _(sym,string) string, |
| 462 | foreach_esp_decrypt_post_error |
| 463 | #undef _ |
| 464 | }; |
| 465 | |
Kingwel Xie | 040950a | 2018-11-28 02:10:35 -0500 | [diff] [blame] | 466 | extern vlib_node_registration_t dpdk_esp4_decrypt_post_node; |
| 467 | extern vlib_node_registration_t dpdk_esp6_decrypt_post_node; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 468 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 469 | static u8 * |
| 470 | format_esp_decrypt_post_trace (u8 * s, va_list * args) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 471 | { |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 472 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 473 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 474 | esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *); |
Gabriel Ganne | e3ea797 | 2017-10-12 10:53:31 +0200 | [diff] [blame] | 475 | u32 indent = format_get_indent (s); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 476 | |
| 477 | s = format (s, "cipher %U auth %U\n", |
| 478 | format_ipsec_crypto_alg, t->crypto_alg, |
| 479 | format_ipsec_integ_alg, t->integ_alg); |
| 480 | |
| 481 | ip4_header_t *ih4 = (ip4_header_t *) t->packet_data; |
| 482 | if ((ih4->ip_version_and_header_length & 0xF0) == 0x60) |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 483 | s = |
| 484 | format (s, "%U%U", format_white_space, indent, format_ip6_header, ih4); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 485 | else |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 486 | s = |
| 487 | format (s, "%U%U", format_white_space, indent, format_ip4_header, ih4); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 488 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 489 | return s; |
| 490 | } |
| 491 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 492 | always_inline uword |
| 493 | dpdk_esp_decrypt_post_inline (vlib_main_t * vm, |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 494 | vlib_node_runtime_t * node, |
| 495 | vlib_frame_t * from_frame, int is_ip6) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 496 | { |
| 497 | u32 n_left_from, *from, *to_next = 0, next_index; |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 498 | ipsec_sa_t *sa0; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 499 | u32 sa_index0 = ~0; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 500 | dpdk_crypto_main_t *dcm = &dpdk_crypto_main; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 501 | |
| 502 | from = vlib_frame_vector_args (from_frame); |
| 503 | n_left_from = from_frame->n_vectors; |
| 504 | |
| 505 | next_index = node->cached_next_index; |
| 506 | |
| 507 | while (n_left_from > 0) |
| 508 | { |
| 509 | u32 n_left_to_next; |
| 510 | |
| 511 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 512 | |
| 513 | while (n_left_from > 0 && n_left_to_next > 0) |
| 514 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 515 | esp_footer_t *f0; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 516 | u32 bi0, iv_size, next0; |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 517 | vlib_buffer_t *b0 = 0; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 518 | ip4_header_t *ih4 = 0, *oh4 = 0; |
| 519 | ip6_header_t *ih6 = 0, *oh6 = 0; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 520 | crypto_alg_t *cipher_alg, *auth_alg; |
| 521 | esp_header_t *esp0; |
| 522 | u8 trunc_size, is_aead; |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 523 | u16 udp_encap_adv = 0; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 524 | |
| 525 | next0 = ESP_DECRYPT_NEXT_DROP; |
| 526 | |
| 527 | bi0 = from[0]; |
| 528 | from += 1; |
| 529 | n_left_from -= 1; |
| 530 | n_left_to_next -= 1; |
| 531 | |
| 532 | b0 = vlib_get_buffer (vm, bi0); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 533 | esp0 = vlib_buffer_get_current (b0); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 534 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 535 | sa_index0 = vnet_buffer (b0)->ipsec.sad_index; |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 536 | sa0 = ipsec_sa_get (sa_index0); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 537 | |
| 538 | to_next[0] = bi0; |
| 539 | to_next += 1; |
| 540 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 541 | cipher_alg = vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg); |
| 542 | auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 543 | is_aead = cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 544 | if (is_aead) |
| 545 | auth_alg = cipher_alg; |
| 546 | |
| 547 | trunc_size = auth_alg->trunc_size; |
| 548 | |
| 549 | iv_size = cipher_alg->iv_len; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 550 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 551 | ipsec_sa_anti_replay_advance (sa0, |
| 552 | clib_host_to_net_u32 (esp0->seq)); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 553 | |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 554 | /* if UDP encapsulation is used adjust the address of the IP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 555 | if (ipsec_sa_is_set_UDP_ENCAP (sa0) |
| 556 | && (b0->flags & VNET_BUFFER_F_IS_IP4)) |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 557 | { |
| 558 | udp_encap_adv = sizeof (udp_header_t); |
| 559 | } |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 560 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 561 | if (b0->flags & VNET_BUFFER_F_IS_IP4) |
| 562 | ih4 = (ip4_header_t *) |
| 563 | ((u8 *) esp0 - udp_encap_adv - sizeof (ip4_header_t)); |
| 564 | else |
| 565 | ih4 = (ip4_header_t *) ((u8 *) esp0 - sizeof (ip6_header_t)); |
Szymon Sliwa | 65a2727 | 2018-05-09 14:28:08 +0200 | [diff] [blame] | 566 | |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 567 | vlib_buffer_advance (b0, sizeof (esp_header_t) + iv_size); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 568 | |
Sergio Gonzalez Monroy | 539ed4f | 2017-05-17 11:43:41 +0100 | [diff] [blame] | 569 | b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 570 | f0 = (esp_footer_t *) (vlib_buffer_get_tail (b0) - trunc_size - 2); |
| 571 | b0->current_length -= (f0->pad_length + trunc_size + 2); |
| 572 | #if 0 |
| 573 | /* check padding */ |
| 574 | const u8 *padding = vlib_buffer_get_tail (b0); |
| 575 | if (PREDICT_FALSE (memcmp (padding, pad_data, f0->pad_length))) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 576 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 577 | clib_warning ("bad padding"); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 578 | vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index, |
| 579 | ESP_DECRYPT_ERROR_DECRYPTION_FAILED, |
| 580 | 1); |
| 581 | goto trace; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 582 | } |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 583 | #endif |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 584 | if (ipsec_sa_is_set_IS_TUNNEL (sa0)) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 585 | { |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 586 | if (f0->next_header == IP_PROTOCOL_IP_IN_IP) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 587 | next0 = ESP_DECRYPT_NEXT_IP4_INPUT; |
Matthew G Smith | 5025d40 | 2019-08-06 08:43:50 -0500 | [diff] [blame] | 588 | else if (f0->next_header == IP_PROTOCOL_IPV6) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 589 | next0 = ESP_DECRYPT_NEXT_IP6_INPUT; |
| 590 | else |
| 591 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 592 | clib_warning ("next header: 0x%x", f0->next_header); |
| 593 | if (is_ip6) |
| 594 | vlib_node_increment_counter (vm, |
| 595 | dpdk_esp6_decrypt_node.index, |
| 596 | ESP_DECRYPT_ERROR_DECRYPTION_FAILED, |
| 597 | 1); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 598 | else |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 599 | vlib_node_increment_counter (vm, |
| 600 | dpdk_esp4_decrypt_node.index, |
| 601 | ESP_DECRYPT_ERROR_DECRYPTION_FAILED, |
| 602 | 1); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 603 | goto trace; |
| 604 | } |
| 605 | } |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 606 | else /* transport mode */ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 607 | { |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 608 | if ((ih4->ip_version_and_header_length & 0xF0) == 0x40) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 609 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 610 | u16 ih4_len = ip4_header_bytes (ih4); |
Alexander Chernavin | 82fc98f | 2020-04-03 10:18:44 -0400 | [diff] [blame] | 611 | vlib_buffer_advance (b0, -ih4_len); |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 612 | next0 = ESP_DECRYPT_NEXT_IP4_INPUT; |
Alexander Chernavin | 82fc98f | 2020-04-03 10:18:44 -0400 | [diff] [blame] | 613 | |
| 614 | oh4 = vlib_buffer_get_current (b0); |
| 615 | memmove (oh4, ih4, ih4_len); |
| 616 | oh4->protocol = f0->next_header; |
| 617 | oh4->length = clib_host_to_net_u16 (b0->current_length); |
| 618 | oh4->checksum = ip4_header_checksum (oh4); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 619 | } |
| 620 | else if ((ih4->ip_version_and_header_length & 0xF0) == 0x60) |
| 621 | { |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 622 | ih6 = (ip6_header_t *) ih4; |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 623 | vlib_buffer_advance (b0, -sizeof (ip6_header_t)); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 624 | oh6 = vlib_buffer_get_current (b0); |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 625 | memmove (oh6, ih6, sizeof (ip6_header_t)); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 626 | |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 627 | next0 = ESP_DECRYPT_NEXT_IP6_INPUT; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 628 | oh6->protocol = f0->next_header; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 629 | u16 len = b0->current_length - sizeof (ip6_header_t); |
| 630 | oh6->payload_length = clib_host_to_net_u16 (len); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 631 | } |
| 632 | else |
| 633 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 634 | clib_warning ("next header: 0x%x", f0->next_header); |
| 635 | if (is_ip6) |
| 636 | vlib_node_increment_counter (vm, |
| 637 | dpdk_esp6_decrypt_node.index, |
| 638 | ESP_DECRYPT_ERROR_DECRYPTION_FAILED, |
| 639 | 1); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 640 | else |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 641 | vlib_node_increment_counter (vm, |
| 642 | dpdk_esp4_decrypt_node.index, |
| 643 | ESP_DECRYPT_ERROR_DECRYPTION_FAILED, |
| 644 | 1); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 645 | goto trace; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 649 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 650 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 651 | trace: |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 652 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 653 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 654 | esp_decrypt_trace_t *tr = |
| 655 | vlib_add_trace (vm, node, b0, sizeof (*tr)); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 656 | tr->crypto_alg = sa0->crypto_alg; |
| 657 | tr->integ_alg = sa0->integ_alg; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 658 | ih4 = vlib_buffer_get_current (b0); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 659 | clib_memcpy_fast (tr->packet_data, ih4, sizeof (ip6_header_t)); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 663 | to_next, n_left_to_next, bi0, |
| 664 | next0); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 665 | } |
| 666 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 667 | } |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 668 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 669 | if (is_ip6) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 670 | vlib_node_increment_counter (vm, dpdk_esp6_decrypt_post_node.index, |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 671 | ESP_DECRYPT_POST_ERROR_PKTS, |
| 672 | from_frame->n_vectors); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 673 | else |
| 674 | vlib_node_increment_counter (vm, dpdk_esp4_decrypt_post_node.index, |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 675 | ESP_DECRYPT_POST_ERROR_PKTS, |
| 676 | from_frame->n_vectors); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 677 | |
| 678 | return from_frame->n_vectors; |
| 679 | } |
| 680 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 681 | VLIB_NODE_FN (dpdk_esp4_decrypt_post_node) (vlib_main_t * vm, |
| 682 | vlib_node_runtime_t * node, |
| 683 | vlib_frame_t * from_frame) |
| 684 | { |
| 685 | return dpdk_esp_decrypt_post_inline (vm, node, from_frame, 0 /*is_ip6 */ ); |
| 686 | } |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 687 | |
Sergio Gonzalez Monroy | 539ed4f | 2017-05-17 11:43:41 +0100 | [diff] [blame] | 688 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 689 | VLIB_REGISTER_NODE (dpdk_esp4_decrypt_post_node) = { |
Klement Sekera | 7334393 | 2018-10-22 13:17:19 +0200 | [diff] [blame] | 690 | .name = "dpdk-esp4-decrypt-post", |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 691 | .vector_size = sizeof (u32), |
| 692 | .format_trace = format_esp_decrypt_post_trace, |
| 693 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 694 | |
| 695 | .n_errors = ARRAY_LEN(esp_decrypt_post_error_strings), |
| 696 | .error_strings = esp_decrypt_post_error_strings, |
| 697 | |
| 698 | .n_next_nodes = ESP_DECRYPT_N_NEXT, |
| 699 | .next_nodes = { |
| 700 | #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n, |
| 701 | foreach_esp_decrypt_next |
| 702 | #undef _ |
| 703 | }, |
| 704 | }; |
Sergio Gonzalez Monroy | 539ed4f | 2017-05-17 11:43:41 +0100 | [diff] [blame] | 705 | /* *INDENT-ON* */ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 706 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 707 | VLIB_NODE_FN (dpdk_esp6_decrypt_post_node) (vlib_main_t * vm, |
| 708 | vlib_node_runtime_t * node, |
| 709 | vlib_frame_t * from_frame) |
Kingwel Xie | 040950a | 2018-11-28 02:10:35 -0500 | [diff] [blame] | 710 | { |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 711 | return dpdk_esp_decrypt_post_inline (vm, node, from_frame, 0 /*is_ip6 */ ); |
Kingwel Xie | 040950a | 2018-11-28 02:10:35 -0500 | [diff] [blame] | 712 | } |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 713 | |
| 714 | /* *INDENT-OFF* */ |
| 715 | VLIB_REGISTER_NODE (dpdk_esp6_decrypt_post_node) = { |
Klement Sekera | 7334393 | 2018-10-22 13:17:19 +0200 | [diff] [blame] | 716 | .name = "dpdk-esp6-decrypt-post", |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 717 | .vector_size = sizeof (u32), |
| 718 | .format_trace = format_esp_decrypt_post_trace, |
| 719 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 720 | |
| 721 | .n_errors = ARRAY_LEN(esp_decrypt_post_error_strings), |
| 722 | .error_strings = esp_decrypt_post_error_strings, |
| 723 | |
| 724 | .n_next_nodes = ESP_DECRYPT_N_NEXT, |
| 725 | .next_nodes = { |
| 726 | #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n, |
| 727 | foreach_esp_decrypt_next |
| 728 | #undef _ |
| 729 | }, |
| 730 | }; |
| 731 | /* *INDENT-ON* */ |
Kingwel Xie | 040950a | 2018-11-28 02:10:35 -0500 | [diff] [blame] | 732 | |
Kingwel Xie | 8861c1a | 2018-11-30 04:43:25 -0500 | [diff] [blame] | 733 | /* |
| 734 | * fd.io coding-style-patch-verification: ON |
| 735 | * |
| 736 | * Local Variables: |
| 737 | * eval: (c-set-style "gnu") |
| 738 | * End: |
| 739 | */ |