Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * esp_encrypt.c : IPSec ESP encrypt 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. |
| 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> |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 23 | #include <vnet/ipsec/esp.h> |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 24 | #include <vnet/udp/udp_local.h> |
Damjan Marion | a5c308e | 2019-01-20 02:02:51 +0100 | [diff] [blame] | 25 | #include <dpdk/buffer.h> |
Damjan Marion | c3a814b | 2017-02-28 19:22:22 +0100 | [diff] [blame] | 26 | #include <dpdk/ipsec/ipsec.h> |
Neale Ranns | c27b436 | 2020-03-11 10:57:54 -0400 | [diff] [blame] | 27 | #include <vnet/ipsec/ipsec_tun.h> |
Damjan Marion | c3a814b | 2017-02-28 19:22:22 +0100 | [diff] [blame] | 28 | #include <dpdk/device/dpdk.h> |
| 29 | #include <dpdk/device/dpdk_priv.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 30 | |
| 31 | #define foreach_esp_encrypt_next \ |
| 32 | _(DROP, "error-drop") \ |
Sergio Gonzalez Monroy | 1b6f902 | 2016-12-12 10:37:49 +0000 | [diff] [blame] | 33 | _(IP4_LOOKUP, "ip4-lookup") \ |
| 34 | _(IP6_LOOKUP, "ip6-lookup") \ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 35 | _(INTERFACE_OUTPUT, "interface-output") |
| 36 | |
| 37 | #define _(v, s) ESP_ENCRYPT_NEXT_##v, |
| 38 | typedef enum |
| 39 | { |
| 40 | foreach_esp_encrypt_next |
| 41 | #undef _ |
| 42 | ESP_ENCRYPT_N_NEXT, |
| 43 | } esp_encrypt_next_t; |
| 44 | |
| 45 | #define foreach_esp_encrypt_error \ |
| 46 | _(RX_PKTS, "ESP pkts received") \ |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 47 | _(SEQ_CYCLED, "Sequence number cycled") \ |
Christian Hopps | 942b980 | 2020-07-14 09:41:43 -0400 | [diff] [blame] | 48 | _(ENQ_FAIL, "Enqueue encrypt failed (queue full)") \ |
Christian Hopps | f6cb044 | 2020-07-14 08:39:30 -0400 | [diff] [blame] | 49 | _(DISCARD, "Not enough crypto operations") \ |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 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 | |
| 54 | typedef enum |
| 55 | { |
| 56 | #define _(sym,str) ESP_ENCRYPT_ERROR_##sym, |
| 57 | foreach_esp_encrypt_error |
| 58 | #undef _ |
| 59 | ESP_ENCRYPT_N_ERROR, |
| 60 | } esp_encrypt_error_t; |
| 61 | |
| 62 | static char *esp_encrypt_error_strings[] = { |
| 63 | #define _(sym,string) string, |
| 64 | foreach_esp_encrypt_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_encrypt_node; |
| 69 | extern vlib_node_registration_t dpdk_esp6_encrypt_node; |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 70 | extern vlib_node_registration_t dpdk_esp4_encrypt_tun_node; |
| 71 | extern vlib_node_registration_t dpdk_esp6_encrypt_tun_node; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 72 | |
| 73 | typedef struct |
| 74 | { |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 75 | ipsec_crypto_alg_t crypto_alg; |
| 76 | ipsec_integ_alg_t integ_alg; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 77 | u8 packet_data[64]; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 78 | } esp_encrypt_trace_t; |
| 79 | |
| 80 | /* packet trace format function */ |
| 81 | static u8 * |
| 82 | format_esp_encrypt_trace (u8 * s, va_list * args) |
| 83 | { |
| 84 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 85 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 86 | esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 87 | ip4_header_t *ih4 = (ip4_header_t *) t->packet_data; |
Gabriel Ganne | e3ea797 | 2017-10-12 10:53:31 +0200 | [diff] [blame] | 88 | u32 indent = format_get_indent (s), offset; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 89 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 90 | s = format (s, "cipher %U auth %U\n", |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 91 | format_ipsec_crypto_alg, t->crypto_alg, |
| 92 | format_ipsec_integ_alg, t->integ_alg); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 93 | |
| 94 | if ((ih4->ip_version_and_header_length & 0xF0) == 0x60) |
| 95 | { |
| 96 | s = format (s, "%U%U", format_white_space, indent, |
| 97 | format_ip6_header, ih4); |
| 98 | offset = sizeof (ip6_header_t); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | s = format (s, "%U%U", format_white_space, indent, |
| 103 | format_ip4_header, ih4); |
| 104 | offset = ip4_header_bytes (ih4); |
| 105 | } |
| 106 | |
| 107 | s = format (s, "\n%U%U", format_white_space, indent, |
| 108 | format_esp_header, t->packet_data + offset); |
| 109 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 110 | return s; |
| 111 | } |
| 112 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 113 | always_inline uword |
| 114 | dpdk_esp_encrypt_inline (vlib_main_t * vm, |
| 115 | vlib_node_runtime_t * node, |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 116 | vlib_frame_t * from_frame, int is_ip6, int is_tun) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 117 | { |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 118 | u32 n_left_from, *from, *to_next, next_index, thread_index; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 119 | ipsec_main_t *im = &ipsec_main; |
Fan Zhang | 6c8533d | 2021-02-25 12:53:36 +0000 | [diff] [blame] | 120 | vnet_main_t *vnm = vnet_get_main (); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 121 | u32 thread_idx = vlib_get_thread_index (); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 122 | dpdk_crypto_main_t *dcm = &dpdk_crypto_main; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 123 | crypto_resource_t *res = 0; |
| 124 | ipsec_sa_t *sa0 = 0; |
| 125 | crypto_alg_t *cipher_alg = 0, *auth_alg = 0; |
| 126 | struct rte_cryptodev_sym_session *session = 0; |
| 127 | u32 ret, last_sa_index = ~0; |
| 128 | u8 numa = rte_socket_id (); |
| 129 | u8 is_aead = 0; |
| 130 | crypto_worker_main_t *cwm = |
| 131 | vec_elt_at_index (dcm->workers_main, thread_idx); |
| 132 | struct rte_crypto_op **ops = cwm->ops; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 133 | |
| 134 | from = vlib_frame_vector_args (from_frame); |
| 135 | n_left_from = from_frame->n_vectors; |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 136 | thread_index = vm->thread_index; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 137 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 138 | ret = crypto_alloc_ops (numa, ops, n_left_from); |
| 139 | if (ret) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 140 | { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 141 | if (is_ip6) |
| 142 | vlib_node_increment_counter (vm, dpdk_esp6_encrypt_node.index, |
Christian Hopps | f6cb044 | 2020-07-14 08:39:30 -0400 | [diff] [blame] | 143 | ESP_ENCRYPT_ERROR_DISCARD, n_left_from); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 144 | else |
| 145 | vlib_node_increment_counter (vm, dpdk_esp4_encrypt_node.index, |
Christian Hopps | f6cb044 | 2020-07-14 08:39:30 -0400 | [diff] [blame] | 146 | ESP_ENCRYPT_ERROR_DISCARD, n_left_from); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 147 | /* Discard whole frame */ |
Christian Hopps | f6cb044 | 2020-07-14 08:39:30 -0400 | [diff] [blame] | 148 | vlib_buffer_free (vm, from, n_left_from); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 149 | return n_left_from; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 152 | next_index = ESP_ENCRYPT_NEXT_DROP; |
| 153 | |
| 154 | while (n_left_from > 0) |
| 155 | { |
| 156 | u32 n_left_to_next; |
| 157 | |
| 158 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 159 | |
| 160 | while (n_left_from > 0 && n_left_to_next > 0) |
| 161 | { |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 162 | clib_error_t *error; |
Zhiyong Yang | 2c1904f | 2019-03-20 03:35:39 -0400 | [diff] [blame] | 163 | u32 bi0, bi1; |
| 164 | vlib_buffer_t *b0, *b1; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 165 | u32 sa_index0; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 166 | ip4_and_esp_header_t *ih0, *oh0 = 0; |
| 167 | ip6_and_esp_header_t *ih6_0, *oh6_0 = 0; |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 168 | ip4_and_udp_and_esp_header_t *ouh0 = 0; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 169 | esp_header_t *esp0; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 170 | esp_footer_t *f0; |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 171 | u8 next_hdr_type; |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 172 | u32 iv_size; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 173 | u16 orig_sz; |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 174 | u8 trunc_size; |
Radu Nicolau | 61fe15e | 2018-01-16 15:33:31 +0000 | [diff] [blame] | 175 | u16 rewrite_len; |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 176 | u16 udp_encap_adv = 0; |
Zhiyong Yang | 2c1904f | 2019-03-20 03:35:39 -0400 | [diff] [blame] | 177 | struct rte_mbuf *mb0; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 178 | struct rte_crypto_op *op; |
| 179 | u16 res_idx; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 180 | |
| 181 | bi0 = from[0]; |
| 182 | from += 1; |
| 183 | n_left_from -= 1; |
| 184 | |
| 185 | b0 = vlib_get_buffer (vm, bi0); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 186 | ih0 = vlib_buffer_get_current (b0); |
| 187 | mb0 = rte_mbuf_from_vlib_buffer (b0); |
| 188 | |
| 189 | /* ih0/ih6_0 */ |
| 190 | CLIB_PREFETCH (ih0, sizeof (ih6_0[0]), LOAD); |
| 191 | /* f0 */ |
| 192 | CLIB_PREFETCH (vlib_buffer_get_tail (b0), 20, STORE); |
| 193 | /* mb0 */ |
| 194 | CLIB_PREFETCH (mb0, CLIB_CACHE_LINE_BYTES, STORE); |
| 195 | |
Zhiyong Yang | 2c1904f | 2019-03-20 03:35:39 -0400 | [diff] [blame] | 196 | if (n_left_from > 1) |
| 197 | { |
| 198 | bi1 = from[1]; |
| 199 | b1 = vlib_get_buffer (vm, bi1); |
| 200 | |
| 201 | CLIB_PREFETCH (b1, CLIB_CACHE_LINE_BYTES, LOAD); |
| 202 | CLIB_PREFETCH (b1->data - CLIB_CACHE_LINE_BYTES, |
| 203 | CLIB_CACHE_LINE_BYTES, STORE); |
| 204 | } |
| 205 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 206 | op = ops[0]; |
| 207 | ops += 1; |
| 208 | ASSERT (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED); |
| 209 | |
| 210 | dpdk_op_priv_t *priv = crypto_op_get_priv (op); |
Kingwel Xie | 3b3464e | 2019-02-13 02:48:41 -0500 | [diff] [blame] | 211 | /* store bi in op private */ |
| 212 | priv->bi = bi0; |
Sergio Gonzalez Monroy | d8a34a5 | 2019-05-06 22:44:14 +0200 | [diff] [blame] | 213 | priv->encrypt = 1; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 214 | |
| 215 | u16 op_len = |
| 216 | sizeof (op[0]) + sizeof (op[0].sym[0]) + sizeof (priv[0]); |
| 217 | CLIB_PREFETCH (op, op_len, STORE); |
| 218 | |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 219 | if (is_tun) |
| 220 | { |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 221 | /* we are on a ipsec tunnel's feature arc */ |
Neale Ranns | c27b436 | 2020-03-11 10:57:54 -0400 | [diff] [blame] | 222 | vnet_buffer (b0)->ipsec.sad_index = |
| 223 | sa_index0 = ipsec_tun_protect_get_sa_out |
| 224 | (vnet_buffer (b0)->ip.adj_index[VLIB_TX]); |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 225 | } |
| 226 | else |
| 227 | sa_index0 = vnet_buffer (b0)->ipsec.sad_index; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 228 | |
| 229 | if (sa_index0 != last_sa_index) |
| 230 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 231 | sa0 = ipsec_sa_get (sa_index0); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 232 | |
| 233 | cipher_alg = |
| 234 | vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg); |
| 235 | auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg); |
| 236 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 237 | is_aead = (cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 238 | |
| 239 | if (is_aead) |
| 240 | auth_alg = cipher_alg; |
| 241 | |
| 242 | res_idx = get_resource (cwm, sa0); |
| 243 | |
| 244 | if (PREDICT_FALSE (res_idx == (u16) ~ 0)) |
| 245 | { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 246 | if (is_ip6) |
| 247 | vlib_node_increment_counter (vm, |
| 248 | dpdk_esp6_encrypt_node.index, |
| 249 | ESP_ENCRYPT_ERROR_NOSUP, 1); |
| 250 | else |
| 251 | vlib_node_increment_counter (vm, |
| 252 | dpdk_esp4_encrypt_node.index, |
| 253 | ESP_ENCRYPT_ERROR_NOSUP, 1); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 254 | to_next[0] = bi0; |
| 255 | to_next += 1; |
| 256 | n_left_to_next -= 1; |
| 257 | goto trace; |
| 258 | } |
| 259 | res = vec_elt_at_index (dcm->resource, res_idx); |
| 260 | |
| 261 | error = crypto_get_session (&session, sa_index0, res, cwm, 1); |
| 262 | if (PREDICT_FALSE (error || !session)) |
| 263 | { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 264 | if (is_ip6) |
| 265 | vlib_node_increment_counter (vm, |
| 266 | dpdk_esp6_encrypt_node.index, |
| 267 | ESP_ENCRYPT_ERROR_SESSION, |
| 268 | 1); |
| 269 | else |
| 270 | vlib_node_increment_counter (vm, |
| 271 | dpdk_esp4_encrypt_node.index, |
| 272 | ESP_ENCRYPT_ERROR_SESSION, |
| 273 | 1); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 274 | to_next[0] = bi0; |
| 275 | to_next += 1; |
| 276 | n_left_to_next -= 1; |
| 277 | goto trace; |
| 278 | } |
Sergio Gonzalez Monroy | 2096063 | 2017-10-12 11:43:41 +0100 | [diff] [blame] | 279 | |
| 280 | last_sa_index = sa_index0; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 281 | } |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 282 | |
| 283 | if (PREDICT_FALSE (esp_seq_advance (sa0))) |
| 284 | { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 285 | if (is_ip6) |
| 286 | vlib_node_increment_counter (vm, |
| 287 | dpdk_esp6_encrypt_node.index, |
| 288 | ESP_ENCRYPT_ERROR_SEQ_CYCLED, 1); |
| 289 | else |
| 290 | vlib_node_increment_counter (vm, |
| 291 | dpdk_esp4_encrypt_node.index, |
| 292 | ESP_ENCRYPT_ERROR_SEQ_CYCLED, 1); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 293 | //TODO: rekey SA |
| 294 | to_next[0] = bi0; |
| 295 | to_next += 1; |
| 296 | n_left_to_next -= 1; |
| 297 | goto trace; |
| 298 | } |
| 299 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 300 | orig_sz = b0->current_length; |
| 301 | |
| 302 | /* TODO multi-seg support - total_length_not_including_first_buffer */ |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 303 | vlib_increment_combined_counter |
| 304 | (&ipsec_sa_counters, thread_index, sa_index0, |
| 305 | 1, b0->current_length); |
Radu Nicolau | cb33dc2 | 2017-02-16 16:49:46 +0000 | [diff] [blame] | 306 | |
Matthew Smith | 5e8e8dc | 2019-05-09 08:55:11 -0500 | [diff] [blame] | 307 | /* Update tunnel interface tx counters */ |
| 308 | if (is_tun) |
| 309 | vlib_increment_combined_counter |
| 310 | (vim->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, |
| 311 | thread_index, vnet_buffer (b0)->sw_if_index[VLIB_TX], |
| 312 | 1, b0->current_length); |
| 313 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 314 | res->ops[res->n_ops] = op; |
| 315 | res->bi[res->n_ops] = bi0; |
| 316 | res->n_ops += 1; |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 317 | |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 318 | dpdk_gcm_cnt_blk *icb = &priv->cb; |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 319 | |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 320 | crypto_set_icb (icb, sa0->salt, sa0->seq, sa0->seq_hi); |
| 321 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 322 | iv_size = cipher_alg->iv_len; |
| 323 | trunc_size = auth_alg->trunc_size; |
| 324 | |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 325 | /* if UDP encapsulation is used adjust the address of the IP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 326 | if (ipsec_sa_is_set_UDP_ENCAP (sa0) && !is_ip6) |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 327 | udp_encap_adv = sizeof (udp_header_t); |
| 328 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 329 | if (ipsec_sa_is_set_IS_TUNNEL (sa0)) |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 330 | { |
Radu Nicolau | 61fe15e | 2018-01-16 15:33:31 +0000 | [diff] [blame] | 331 | rewrite_len = 0; |
Matthew G Smith | 5025d40 | 2019-08-06 08:43:50 -0500 | [diff] [blame] | 332 | if (!ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)) /* ip4 */ |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 333 | { |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 334 | /* in tunnel mode send it back to FIB */ |
| 335 | priv->next = DPDK_CRYPTO_INPUT_NEXT_IP4_LOOKUP; |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 336 | u8 adv = sizeof (ip4_header_t) + udp_encap_adv + |
| 337 | sizeof (esp_header_t) + iv_size; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 338 | vlib_buffer_advance (b0, -adv); |
| 339 | oh0 = vlib_buffer_get_current (b0); |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 340 | ouh0 = vlib_buffer_get_current (b0); |
Matthew G Smith | 5025d40 | 2019-08-06 08:43:50 -0500 | [diff] [blame] | 341 | next_hdr_type = (is_ip6 ? |
| 342 | IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 343 | /* |
| 344 | * oh0->ip4.ip_version_and_header_length = 0x45; |
| 345 | * oh0->ip4.tos = ih0->ip4.tos; |
| 346 | * oh0->ip4.fragment_id = 0; |
| 347 | * oh0->ip4.flags_and_fragment_offset = 0; |
| 348 | */ |
| 349 | oh0->ip4.checksum_data_64[0] = |
| 350 | clib_host_to_net_u64 (0x45ULL << 56); |
| 351 | /* |
| 352 | * oh0->ip4.ttl = 254; |
| 353 | * oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP; |
| 354 | */ |
| 355 | oh0->ip4.checksum_data_32[2] = |
| 356 | clib_host_to_net_u32 (0xfe320000); |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 357 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 358 | oh0->ip4.src_address.as_u32 = |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 359 | sa0->tunnel.t_src.ip.ip4.as_u32; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 360 | oh0->ip4.dst_address.as_u32 = |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 361 | sa0->tunnel.t_dst.ip.ip4.as_u32; |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 362 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 363 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 364 | { |
| 365 | oh0->ip4.protocol = IP_PROTOCOL_UDP; |
| 366 | esp0 = &ouh0->esp; |
| 367 | } |
| 368 | else |
| 369 | esp0 = &oh0->esp; |
| 370 | esp0->spi = clib_host_to_net_u32 (sa0->spi); |
| 371 | esp0->seq = clib_host_to_net_u32 (sa0->seq); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 372 | } |
Matthew G Smith | 5025d40 | 2019-08-06 08:43:50 -0500 | [diff] [blame] | 373 | else |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 374 | { |
Matthew G Smith | 5025d40 | 2019-08-06 08:43:50 -0500 | [diff] [blame] | 375 | /* ip6 */ |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 376 | /* in tunnel mode send it back to FIB */ |
| 377 | priv->next = DPDK_CRYPTO_INPUT_NEXT_IP6_LOOKUP; |
| 378 | |
| 379 | u8 adv = |
| 380 | sizeof (ip6_header_t) + sizeof (esp_header_t) + iv_size; |
| 381 | vlib_buffer_advance (b0, -adv); |
| 382 | ih6_0 = (ip6_and_esp_header_t *) ih0; |
| 383 | oh6_0 = vlib_buffer_get_current (b0); |
| 384 | |
Matthew G Smith | 5025d40 | 2019-08-06 08:43:50 -0500 | [diff] [blame] | 385 | next_hdr_type = (is_ip6 ? |
| 386 | IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 387 | |
| 388 | oh6_0->ip6.ip_version_traffic_class_and_flow_label = |
| 389 | ih6_0->ip6.ip_version_traffic_class_and_flow_label; |
| 390 | |
| 391 | oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP; |
| 392 | oh6_0->ip6.hop_limit = 254; |
| 393 | oh6_0->ip6.src_address.as_u64[0] = |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 394 | sa0->tunnel.t_src.ip.ip6.as_u64[0]; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 395 | oh6_0->ip6.src_address.as_u64[1] = |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 396 | sa0->tunnel.t_src.ip.ip6.as_u64[1]; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 397 | oh6_0->ip6.dst_address.as_u64[0] = |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 398 | sa0->tunnel.t_dst.ip.ip6.as_u64[0]; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 399 | oh6_0->ip6.dst_address.as_u64[1] = |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 400 | sa0->tunnel.t_dst.ip.ip6.as_u64[1]; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 401 | esp0 = &oh6_0->esp; |
| 402 | oh6_0->esp.spi = clib_host_to_net_u32 (sa0->spi); |
| 403 | oh6_0->esp.seq = clib_host_to_net_u32 (sa0->seq); |
| 404 | } |
Matthew G Smith | 5025d40 | 2019-08-06 08:43:50 -0500 | [diff] [blame] | 405 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 406 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
| 407 | } |
| 408 | else /* transport mode */ |
| 409 | { |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 410 | if (is_tun) |
| 411 | { |
| 412 | rewrite_len = 0; |
| 413 | priv->next = DPDK_CRYPTO_INPUT_NEXT_MIDCHAIN; |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | priv->next = DPDK_CRYPTO_INPUT_NEXT_INTERFACE_OUTPUT; |
| 418 | rewrite_len = vnet_buffer (b0)->ip.save_rewrite_length; |
| 419 | } |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 420 | u16 adv = sizeof (esp_header_t) + iv_size + udp_encap_adv; |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 421 | vlib_buffer_advance (b0, -adv - rewrite_len); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 422 | u8 *src = ((u8 *) ih0) - rewrite_len; |
| 423 | u8 *dst = vlib_buffer_get_current (b0); |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 424 | oh0 = vlib_buffer_get_current (b0) + rewrite_len; |
Alexander Chernavin | 82fc98f | 2020-04-03 10:18:44 -0400 | [diff] [blame] | 425 | ouh0 = vlib_buffer_get_current (b0) + rewrite_len; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 426 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 427 | if (is_ip6) |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 428 | { |
| 429 | orig_sz -= sizeof (ip6_header_t); |
| 430 | ih6_0 = (ip6_and_esp_header_t *) ih0; |
| 431 | next_hdr_type = ih6_0->ip6.protocol; |
| 432 | memmove (dst, src, rewrite_len + sizeof (ip6_header_t)); |
| 433 | oh6_0 = (ip6_and_esp_header_t *) oh0; |
| 434 | oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP; |
| 435 | esp0 = &oh6_0->esp; |
| 436 | } |
| 437 | else /* ipv4 */ |
| 438 | { |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 439 | u16 ip_size = ip4_header_bytes (&ih0->ip4); |
| 440 | orig_sz -= ip_size; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 441 | next_hdr_type = ih0->ip4.protocol; |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 442 | memmove (dst, src, rewrite_len + ip_size); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 443 | oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP; |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 444 | esp0 = (esp_header_t *) (((u8 *) oh0) + ip_size); |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 445 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 446 | { |
| 447 | oh0->ip4.protocol = IP_PROTOCOL_UDP; |
| 448 | esp0 = (esp_header_t *) |
| 449 | (((u8 *) oh0) + ip_size + udp_encap_adv); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP; |
| 454 | esp0 = (esp_header_t *) (((u8 *) oh0) + ip_size); |
| 455 | } |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 456 | } |
| 457 | esp0->spi = clib_host_to_net_u32 (sa0->spi); |
| 458 | esp0->seq = clib_host_to_net_u32 (sa0->seq); |
| 459 | } |
| 460 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 461 | if (ipsec_sa_is_set_UDP_ENCAP (sa0) && ouh0) |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 462 | { |
| 463 | ouh0->udp.src_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec); |
| 464 | ouh0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec); |
| 465 | ouh0->udp.checksum = 0; |
| 466 | } |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 467 | ASSERT (is_pow2 (cipher_alg->boundary)); |
| 468 | u16 mask = cipher_alg->boundary - 1; |
| 469 | u16 pad_payload_len = ((orig_sz + 2) + mask) & ~mask; |
| 470 | u8 pad_bytes = pad_payload_len - 2 - orig_sz; |
| 471 | |
| 472 | u8 *padding = |
| 473 | vlib_buffer_put_uninit (b0, pad_bytes + 2 + trunc_size); |
| 474 | |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 475 | /* The extra pad bytes would be overwritten by the digest */ |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 476 | if (pad_bytes) |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 477 | clib_memcpy_fast (padding, pad_data, 16); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 478 | |
| 479 | f0 = (esp_footer_t *) (padding + pad_bytes); |
| 480 | f0->pad_length = pad_bytes; |
| 481 | f0->next_header = next_hdr_type; |
| 482 | |
Matthew Smith | c458f5c | 2019-08-16 11:30:02 -0500 | [diff] [blame] | 483 | if (oh6_0) |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 484 | { |
| 485 | u16 len = b0->current_length - sizeof (ip6_header_t); |
Radu Nicolau | 61fe15e | 2018-01-16 15:33:31 +0000 | [diff] [blame] | 486 | oh6_0->ip6.payload_length = |
| 487 | clib_host_to_net_u16 (len - rewrite_len); |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 488 | } |
Matthew Smith | c458f5c | 2019-08-16 11:30:02 -0500 | [diff] [blame] | 489 | else if (oh0) |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 490 | { |
Radu Nicolau | 61fe15e | 2018-01-16 15:33:31 +0000 | [diff] [blame] | 491 | oh0->ip4.length = |
| 492 | clib_host_to_net_u16 (b0->current_length - rewrite_len); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 493 | oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4); |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 494 | if (ipsec_sa_is_set_UDP_ENCAP (sa0) && ouh0) |
Radu Nicolau | 0c58ad4 | 2018-06-13 16:39:05 +0100 | [diff] [blame] | 495 | { |
| 496 | ouh0->udp.length = |
| 497 | clib_host_to_net_u16 (clib_net_to_host_u16 |
| 498 | (ouh0->ip4.length) - |
| 499 | ip4_header_bytes (&ouh0->ip4)); |
| 500 | } |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 501 | } |
Matthew Smith | c458f5c | 2019-08-16 11:30:02 -0500 | [diff] [blame] | 502 | else /* should never happen */ |
| 503 | clib_warning ("No outer header found for ESP packet"); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 504 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 505 | b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 506 | |
| 507 | /* mbuf packet starts at ESP header */ |
| 508 | mb0->data_len = vlib_buffer_get_tail (b0) - ((u8 *) esp0); |
| 509 | mb0->pkt_len = vlib_buffer_get_tail (b0) - ((u8 *) esp0); |
| 510 | mb0->data_off = ((void *) esp0) - mb0->buf_addr; |
| 511 | |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 512 | u32 cipher_off, cipher_len, auth_len = 0; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 513 | u32 *aad = NULL; |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 514 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 515 | u8 *digest = vlib_buffer_get_tail (b0) - trunc_size; |
Fan Zhang | f0419a0 | 2020-12-01 15:10:43 +0000 | [diff] [blame] | 516 | u64 digest_paddr = mb0->buf_iova + digest - ((u8 *) mb0->buf_addr); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 517 | |
Christian E. Hopps | 5a24263 | 2019-09-27 15:19:19 -0400 | [diff] [blame] | 518 | if (!is_aead && (cipher_alg->alg == RTE_CRYPTO_CIPHER_AES_CBC || |
| 519 | cipher_alg->alg == RTE_CRYPTO_CIPHER_NULL)) |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 520 | { |
| 521 | cipher_off = sizeof (esp_header_t); |
| 522 | cipher_len = iv_size + pad_payload_len; |
| 523 | } |
| 524 | else /* CTR/GCM */ |
| 525 | { |
| 526 | u32 *esp_iv = (u32 *) (esp0 + 1); |
| 527 | esp_iv[0] = sa0->seq; |
| 528 | esp_iv[1] = sa0->seq_hi; |
| 529 | |
| 530 | cipher_off = sizeof (esp_header_t) + iv_size; |
| 531 | cipher_len = pad_payload_len; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | if (is_aead) |
| 535 | { |
| 536 | aad = (u32 *) priv->aad; |
Christian Hopps | d58419f | 2019-11-03 01:02:18 -0400 | [diff] [blame] | 537 | aad[0] = esp0->spi; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 538 | |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 539 | /* aad[3] should always be 0 */ |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 540 | if (PREDICT_FALSE (ipsec_sa_is_set_USE_ESN (sa0))) |
Christian Hopps | d58419f | 2019-11-03 01:02:18 -0400 | [diff] [blame] | 541 | { |
| 542 | aad[1] = clib_host_to_net_u32 (sa0->seq_hi); |
| 543 | aad[2] = esp0->seq; |
| 544 | } |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 545 | else |
Christian Hopps | d58419f | 2019-11-03 01:02:18 -0400 | [diff] [blame] | 546 | { |
| 547 | aad[1] = esp0->seq; |
| 548 | aad[2] = 0; |
| 549 | } |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 550 | } |
| 551 | else |
| 552 | { |
| 553 | auth_len = |
| 554 | vlib_buffer_get_tail (b0) - ((u8 *) esp0) - trunc_size; |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 555 | if (ipsec_sa_is_set_USE_ESN (sa0)) |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 556 | { |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 557 | u32 *_digest = (u32 *) digest; |
| 558 | _digest[0] = clib_host_to_net_u32 (sa0->seq_hi); |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 559 | auth_len += 4; |
| 560 | } |
| 561 | } |
| 562 | |
Sergio Gonzalez Monroy | 99214ce | 2017-11-26 15:25:43 +0000 | [diff] [blame] | 563 | crypto_op_setup (is_aead, mb0, op, session, cipher_off, cipher_len, |
| 564 | 0, auth_len, (u8 *) aad, digest, digest_paddr); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 565 | |
| 566 | trace: |
| 567 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 568 | { |
| 569 | esp_encrypt_trace_t *tr = |
| 570 | vlib_add_trace (vm, node, b0, sizeof (*tr)); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 571 | tr->crypto_alg = sa0->crypto_alg; |
| 572 | tr->integ_alg = sa0->integ_alg; |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 573 | u8 *p = vlib_buffer_get_current (b0); |
Alexander Chernavin | 82fc98f | 2020-04-03 10:18:44 -0400 | [diff] [blame] | 574 | if (!ipsec_sa_is_set_IS_TUNNEL (sa0) && !is_tun) |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 575 | p += vnet_buffer (b0)->ip.save_rewrite_length; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 576 | clib_memcpy_fast (tr->packet_data, p, sizeof (tr->packet_data)); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 580 | } |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 581 | if (is_ip6) |
| 582 | { |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 583 | vlib_node_increment_counter (vm, |
| 584 | (is_tun ? |
| 585 | dpdk_esp6_encrypt_tun_node.index : |
| 586 | dpdk_esp6_encrypt_node.index), |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 587 | ESP_ENCRYPT_ERROR_RX_PKTS, |
| 588 | from_frame->n_vectors); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 589 | |
Sergio Gonzalez Monroy | 35467f1 | 2019-01-30 11:26:00 +0100 | [diff] [blame] | 590 | crypto_enqueue_ops (vm, cwm, dpdk_esp6_encrypt_node.index, |
Sergio Gonzalez Monroy | d8a34a5 | 2019-05-06 22:44:14 +0200 | [diff] [blame] | 591 | ESP_ENCRYPT_ERROR_ENQ_FAIL, numa, 1 /* encrypt */ ); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 592 | } |
| 593 | else |
| 594 | { |
Neale Ranns | 7ec120e | 2020-01-21 04:58:02 +0000 | [diff] [blame] | 595 | vlib_node_increment_counter (vm, |
| 596 | (is_tun ? |
| 597 | dpdk_esp4_encrypt_tun_node.index : |
| 598 | dpdk_esp4_encrypt_node.index), |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 599 | ESP_ENCRYPT_ERROR_RX_PKTS, |
| 600 | from_frame->n_vectors); |
| 601 | |
Sergio Gonzalez Monroy | 35467f1 | 2019-01-30 11:26:00 +0100 | [diff] [blame] | 602 | crypto_enqueue_ops (vm, cwm, dpdk_esp4_encrypt_node.index, |
Sergio Gonzalez Monroy | d8a34a5 | 2019-05-06 22:44:14 +0200 | [diff] [blame] | 603 | ESP_ENCRYPT_ERROR_ENQ_FAIL, numa, 1 /* encrypt */ ); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 604 | } |
Sergio Gonzalez Monroy | acdc306 | 2017-08-24 14:09:17 +0100 | [diff] [blame] | 605 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 606 | 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] | 607 | |
| 608 | return from_frame->n_vectors; |
| 609 | } |
| 610 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 611 | VLIB_NODE_FN (dpdk_esp4_encrypt_node) (vlib_main_t * vm, |
| 612 | vlib_node_runtime_t * node, |
| 613 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 614 | { |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 615 | return dpdk_esp_encrypt_inline (vm, node, from_frame, 0 /*is_ip6 */ , 0); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 616 | } |
| 617 | |
Sergio Gonzalez Monroy | 539ed4f | 2017-05-17 11:43:41 +0100 | [diff] [blame] | 618 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 619 | VLIB_REGISTER_NODE (dpdk_esp4_encrypt_node) = { |
Klement Sekera | 7334393 | 2018-10-22 13:17:19 +0200 | [diff] [blame] | 620 | .name = "dpdk-esp4-encrypt", |
Sergio Gonzalez Monroy | 539ed4f | 2017-05-17 11:43:41 +0100 | [diff] [blame] | 621 | .flags = VLIB_NODE_FLAG_IS_OUTPUT, |
| 622 | .vector_size = sizeof (u32), |
| 623 | .format_trace = format_esp_encrypt_trace, |
| 624 | .n_errors = ARRAY_LEN (esp_encrypt_error_strings), |
| 625 | .error_strings = esp_encrypt_error_strings, |
| 626 | .n_next_nodes = 1, |
| 627 | .next_nodes = |
| 628 | { |
| 629 | [ESP_ENCRYPT_NEXT_DROP] = "error-drop", |
| 630 | } |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 631 | }; |
Sergio Gonzalez Monroy | 539ed4f | 2017-05-17 11:43:41 +0100 | [diff] [blame] | 632 | /* *INDENT-ON* */ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 633 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 634 | VLIB_NODE_FN (dpdk_esp6_encrypt_node) (vlib_main_t * vm, |
| 635 | vlib_node_runtime_t * node, |
| 636 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 637 | { |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 638 | return dpdk_esp_encrypt_inline (vm, node, from_frame, 1 /*is_ip6 */ , 0); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /* *INDENT-OFF* */ |
| 642 | VLIB_REGISTER_NODE (dpdk_esp6_encrypt_node) = { |
Klement Sekera | 7334393 | 2018-10-22 13:17:19 +0200 | [diff] [blame] | 643 | .name = "dpdk-esp6-encrypt", |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 644 | .flags = VLIB_NODE_FLAG_IS_OUTPUT, |
| 645 | .vector_size = sizeof (u32), |
| 646 | .format_trace = format_esp_encrypt_trace, |
| 647 | .n_errors = ARRAY_LEN (esp_encrypt_error_strings), |
| 648 | .error_strings = esp_encrypt_error_strings, |
| 649 | .n_next_nodes = 1, |
| 650 | .next_nodes = |
| 651 | { |
| 652 | [ESP_ENCRYPT_NEXT_DROP] = "error-drop", |
| 653 | } |
| 654 | }; |
| 655 | /* *INDENT-ON* */ |
| 656 | |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 657 | VLIB_NODE_FN (dpdk_esp4_encrypt_tun_node) (vlib_main_t * vm, |
| 658 | vlib_node_runtime_t * node, |
| 659 | vlib_frame_t * from_frame) |
| 660 | { |
| 661 | return dpdk_esp_encrypt_inline (vm, node, from_frame, 0 /*is_ip6 */ , 1); |
| 662 | } |
| 663 | |
| 664 | /* *INDENT-OFF* */ |
| 665 | VLIB_REGISTER_NODE (dpdk_esp4_encrypt_tun_node) = { |
| 666 | .name = "dpdk-esp4-encrypt-tun", |
| 667 | .flags = VLIB_NODE_FLAG_IS_OUTPUT, |
| 668 | .vector_size = sizeof (u32), |
| 669 | .format_trace = format_esp_encrypt_trace, |
| 670 | .n_errors = ARRAY_LEN (esp_encrypt_error_strings), |
| 671 | .error_strings = esp_encrypt_error_strings, |
| 672 | .n_next_nodes = 1, |
| 673 | .next_nodes = |
| 674 | { |
| 675 | [ESP_ENCRYPT_NEXT_DROP] = "error-drop", |
| 676 | } |
| 677 | }; |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 678 | /* *INDENT-ON* */ |
| 679 | |
| 680 | VLIB_NODE_FN (dpdk_esp6_encrypt_tun_node) (vlib_main_t * vm, |
| 681 | vlib_node_runtime_t * node, |
| 682 | vlib_frame_t * from_frame) |
| 683 | { |
| 684 | return dpdk_esp_encrypt_inline (vm, node, from_frame, 1 /*is_ip6 */ , 1); |
| 685 | } |
| 686 | |
| 687 | /* *INDENT-OFF* */ |
| 688 | VLIB_REGISTER_NODE (dpdk_esp6_encrypt_tun_node) = { |
| 689 | .name = "dpdk-esp6-encrypt-tun", |
| 690 | .flags = VLIB_NODE_FLAG_IS_OUTPUT, |
| 691 | .vector_size = sizeof (u32), |
| 692 | .format_trace = format_esp_encrypt_trace, |
| 693 | .n_errors = ARRAY_LEN (esp_encrypt_error_strings), |
| 694 | .error_strings = esp_encrypt_error_strings, |
| 695 | .n_next_nodes = 1, |
| 696 | .next_nodes = |
| 697 | { |
| 698 | [ESP_ENCRYPT_NEXT_DROP] = "error-drop", |
| 699 | } |
| 700 | }; |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 701 | /* *INDENT-ON* */ |
| 702 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 703 | /* |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 704 | * fd.io coding-style-patch-verification: ON |
| 705 | * |
| 706 | * Local Variables: |
| 707 | * eval: (c-set-style "gnu") |
| 708 | * End: |
| 709 | */ |