Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * esp_encrypt.c : IPSec ESP encrypt node |
| 3 | * |
| 4 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vnet/vnet.h> |
| 19 | #include <vnet/api_errno.h> |
| 20 | #include <vnet/ip/ip.h> |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 21 | #include <vnet/udp/udp.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 23 | #include <vnet/crypto/crypto.h> |
| 24 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | #include <vnet/ipsec/ipsec.h> |
| 26 | #include <vnet/ipsec/esp.h> |
| 27 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | #define foreach_esp_encrypt_next \ |
| 29 | _(DROP, "error-drop") \ |
Sergio Gonzalez Monroy | 1b6f902 | 2016-12-12 10:37:49 +0000 | [diff] [blame] | 30 | _(IP4_LOOKUP, "ip4-lookup") \ |
| 31 | _(IP6_LOOKUP, "ip6-lookup") \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | _(INTERFACE_OUTPUT, "interface-output") |
| 33 | |
| 34 | #define _(v, s) ESP_ENCRYPT_NEXT_##v, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 35 | typedef enum |
| 36 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | foreach_esp_encrypt_next |
| 38 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 39 | ESP_ENCRYPT_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | } esp_encrypt_next_t; |
| 41 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 42 | #define foreach_esp_encrypt_error \ |
| 43 | _(RX_PKTS, "ESP pkts received") \ |
| 44 | _(SEQ_CYCLED, "sequence number cycled (packet dropped)") \ |
| 45 | _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \ |
| 46 | _(CHAINED_BUFFER, "chained buffers (packet dropped)") \ |
| 47 | _(NO_TRAILER_SPACE, "no trailer space (packet dropped)") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 49 | typedef enum |
| 50 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | #define _(sym,str) ESP_ENCRYPT_ERROR_##sym, |
| 52 | foreach_esp_encrypt_error |
| 53 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 54 | ESP_ENCRYPT_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | } esp_encrypt_error_t; |
| 56 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 57 | static char *esp_encrypt_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | #define _(sym,string) string, |
| 59 | foreach_esp_encrypt_error |
| 60 | #undef _ |
| 61 | }; |
| 62 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 63 | typedef struct |
| 64 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 65 | u32 sa_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | u32 spi; |
| 67 | u32 seq; |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 68 | u8 udp_encap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | ipsec_crypto_alg_t crypto_alg; |
| 70 | ipsec_integ_alg_t integ_alg; |
| 71 | } esp_encrypt_trace_t; |
| 72 | |
| 73 | /* packet trace format function */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 74 | static u8 * |
| 75 | format_esp_encrypt_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | { |
| 77 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 78 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 79 | esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 81 | s = format (s, "esp: sa-index %d spi %u seq %u crypto %U integrity %U%s", |
| 82 | t->sa_index, t->spi, t->seq, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 83 | format_ipsec_crypto_alg, t->crypto_alg, |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 84 | format_ipsec_integ_alg, t->integ_alg, |
| 85 | t->udp_encap ? " udp-encap-enabled" : ""); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | return s; |
| 87 | } |
| 88 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 89 | /* pad packet in input buffer */ |
| 90 | static_always_inline u8 * |
| 91 | esp_add_footer_and_icv (vlib_buffer_t * b, u8 block_size, u8 icv_sz) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 93 | static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = { |
| 94 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 95 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x00, 0x00, |
| 96 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 98 | u16 min_length = b->current_length + sizeof (esp_footer_t); |
| 99 | u16 new_length = round_pow2 (min_length, block_size); |
| 100 | u8 pad_bytes = new_length - min_length; |
| 101 | esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (b) + |
| 102 | new_length - sizeof (esp_footer_t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 104 | if (pad_bytes) |
| 105 | clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, ESP_MAX_BLOCK_SIZE); |
| 106 | |
| 107 | f->pad_length = pad_bytes; |
| 108 | b->current_length = new_length + icv_sz; |
| 109 | return &f->next_header; |
| 110 | } |
| 111 | |
| 112 | static_always_inline void |
| 113 | esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp) |
| 114 | { |
| 115 | ip_csum_t sum = ip4->checksum; |
| 116 | u16 old_len = 0; |
| 117 | |
| 118 | if (is_transport) |
| 119 | { |
| 120 | u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP; |
| 121 | old_len = ip4->length; |
| 122 | sum = ip_csum_update (sum, ip4->protocol, prot, ip4_header_t, protocol); |
| 123 | ip4->protocol = prot; |
| 124 | } |
| 125 | |
| 126 | ip4->length = len = clib_net_to_host_u16 (len); |
| 127 | sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length); |
| 128 | ip4->checksum = ip_csum_fold (sum); |
| 129 | } |
| 130 | |
| 131 | static_always_inline void |
| 132 | esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len) |
| 133 | { |
| 134 | clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t)); |
| 135 | udp->length = clib_net_to_host_u16 (len); |
| 136 | } |
| 137 | |
| 138 | static_always_inline u8 |
| 139 | ext_hdr_is_pre_esp (u8 nexthdr) |
| 140 | { |
| 141 | #ifdef CLIB_HAVE_VEC128 |
| 142 | static const u8x16 ext_hdr_types = { |
| 143 | IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS, |
| 144 | IP_PROTOCOL_IPV6_ROUTE, |
| 145 | IP_PROTOCOL_IPV6_FRAGMENTATION, |
| 146 | }; |
| 147 | |
| 148 | return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr)); |
| 149 | #else |
| 150 | return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) | |
| 151 | (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) | |
| 152 | (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0); |
| 153 | #endif |
| 154 | } |
| 155 | |
| 156 | static_always_inline u8 |
| 157 | esp_get_ip6_hdr_len (ip6_header_t * ip6) |
| 158 | { |
| 159 | /* this code assumes that HbH, route and frag headers will be before |
| 160 | others, if that is not the case, they will end up encrypted */ |
| 161 | |
| 162 | u8 len = sizeof (ip6_header_t); |
| 163 | ip6_ext_header_t *p; |
| 164 | |
| 165 | /* if next packet doesn't have ext header */ |
| 166 | if (ext_hdr_is_pre_esp (ip6->protocol) == 0) |
| 167 | return len; |
| 168 | |
| 169 | p = (void *) (ip6 + 1); |
| 170 | len += ip6_ext_header_len (p); |
| 171 | |
| 172 | while (ext_hdr_is_pre_esp (p->next_hdr)) |
| 173 | { |
| 174 | len += ip6_ext_header_len (p); |
| 175 | p = ip6_ext_next_header (p); |
| 176 | } |
| 177 | |
| 178 | return len; |
| 179 | } |
| 180 | |
| 181 | static_always_inline int |
| 182 | esp_trailer_icv_overflow (vlib_node_runtime_t * node, vlib_buffer_t * b, |
| 183 | u16 * next, u16 buffer_data_size) |
| 184 | { |
| 185 | if (b->current_data + b->current_length <= buffer_data_size) |
| 186 | return 0; |
| 187 | |
| 188 | b->current_length -= buffer_data_size - b->current_data; |
| 189 | b->error = node->errors[ESP_ENCRYPT_ERROR_NO_TRAILER_SPACE]; |
| 190 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | static_always_inline void |
| 195 | esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 196 | vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts) |
| 197 | { |
| 198 | u32 n_fail, n_ops = vec_len (ops); |
| 199 | vnet_crypto_op_t *op = ops; |
| 200 | |
| 201 | if (n_ops == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | return; |
| 203 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 204 | n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 206 | while (n_fail) |
| 207 | { |
| 208 | ASSERT (op - ops < n_ops); |
| 209 | |
| 210 | if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED) |
| 211 | { |
| 212 | u32 bi = op->user_data; |
| 213 | b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR]; |
| 214 | nexts[bi] = ESP_ENCRYPT_NEXT_DROP; |
| 215 | n_fail--; |
| 216 | } |
| 217 | op++; |
| 218 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 221 | always_inline uword |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 222 | esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 223 | vlib_frame_t * frame, int is_ip6, int is_tun) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | ipsec_main_t *im = &ipsec_main; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 226 | ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index); |
| 227 | u32 *from = vlib_frame_vector_args (frame); |
| 228 | u32 n_left = frame->n_vectors; |
| 229 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 230 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 231 | u32 thread_index = vm->thread_index; |
| 232 | u16 buffer_data_size = vlib_buffer_get_default_data_size (vm); |
| 233 | u32 current_sa_index = ~0, current_sa_packets = 0; |
| 234 | u32 current_sa_bytes = 0, spi = 0; |
| 235 | u8 block_sz = 0, iv_sz = 0, icv_sz = 0; |
| 236 | ipsec_sa_t *sa0 = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 238 | vlib_get_buffers (vm, from, b, n_left); |
| 239 | vec_reset_length (ptd->crypto_ops); |
| 240 | vec_reset_length (ptd->integ_ops); |
| 241 | |
| 242 | while (n_left > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 243 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 244 | u32 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index; |
| 245 | dpo_id_t *dpo; |
| 246 | esp_header_t *esp; |
| 247 | u8 *payload, *next_hdr_ptr; |
| 248 | u16 payload_len; |
| 249 | u32 hdr_len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 251 | if (n_left > 2) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 252 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 253 | u8 *p; |
| 254 | vlib_prefetch_buffer_header (b[2], LOAD); |
| 255 | p = vlib_buffer_get_current (b[1]); |
| 256 | CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD); |
| 257 | p -= CLIB_CACHE_LINE_BYTES; |
| 258 | CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 259 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 261 | if (is_tun) |
| 262 | { |
| 263 | /* we are on a ipsec tunnel's feature arc */ |
| 264 | u32 next0; |
| 265 | sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0], |
| 266 | sizeof |
| 267 | (sa_index0)); |
| 268 | next[0] = next0; |
| 269 | } |
| 270 | else |
| 271 | sa_index0 = vnet_buffer (b[0])->ipsec.sad_index; |
| 272 | |
| 273 | if (sa_index0 != current_sa_index) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 274 | { |
| 275 | sa0 = pool_elt_at_index (im->sad, sa_index0); |
| 276 | current_sa_index = sa_index0; |
| 277 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 278 | sa_index0, current_sa_packets, |
| 279 | current_sa_bytes); |
| 280 | current_sa_packets = current_sa_bytes = 0; |
| 281 | spi = clib_net_to_host_u32 (sa0->spi); |
| 282 | block_sz = sa0->crypto_block_size; |
| 283 | icv_sz = sa0->integ_trunc_size; |
| 284 | iv_sz = sa0->crypto_iv_size; |
| 285 | } |
| 286 | |
| 287 | if (vlib_buffer_chain_linearize (vm, b[0]) != 1) |
| 288 | { |
| 289 | b[0]->error = node->errors[ESP_ENCRYPT_ERROR_CHAINED_BUFFER]; |
| 290 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 291 | goto trace; |
| 292 | } |
| 293 | |
| 294 | if (PREDICT_FALSE (esp_seq_advance (sa0))) |
| 295 | { |
| 296 | b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED]; |
| 297 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 298 | goto trace; |
| 299 | } |
| 300 | |
| 301 | /* space for IV */ |
| 302 | hdr_len = iv_sz; |
| 303 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 304 | if (ipsec_sa_is_set_IS_TUNNEL (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 305 | { |
| 306 | payload = vlib_buffer_get_current (b[0]); |
| 307 | next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz); |
| 308 | payload_len = b[0]->current_length; |
| 309 | |
| 310 | if (esp_trailer_icv_overflow (node, b[0], next, buffer_data_size)) |
| 311 | goto trace; |
| 312 | |
| 313 | /* ESP header */ |
| 314 | hdr_len += sizeof (*esp); |
| 315 | esp = (esp_header_t *) (payload - hdr_len); |
| 316 | |
| 317 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 318 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 319 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 320 | hdr_len += sizeof (udp_header_t); |
| 321 | esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len), |
| 322 | payload_len + hdr_len); |
| 323 | } |
| 324 | |
| 325 | /* IP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 326 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 327 | { |
| 328 | ip6_header_t *ip6; |
| 329 | u16 len = sizeof (ip6_header_t); |
| 330 | hdr_len += len; |
| 331 | ip6 = (ip6_header_t *) (payload - hdr_len); |
| 332 | clib_memcpy_fast (ip6, &sa0->ip6_hdr, len); |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 333 | *next_hdr_ptr = (is_ip6 ? |
| 334 | IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 335 | len = payload_len + hdr_len - len; |
| 336 | ip6->payload_length = clib_net_to_host_u16 (len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 337 | } |
| 338 | else |
| 339 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 340 | ip4_header_t *ip4; |
| 341 | u16 len = sizeof (ip4_header_t); |
| 342 | hdr_len += len; |
| 343 | ip4 = (ip4_header_t *) (payload - hdr_len); |
| 344 | clib_memcpy_fast (ip4, &sa0->ip4_hdr, len); |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 345 | *next_hdr_ptr = (is_ip6 ? |
| 346 | IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 347 | len = payload_len + hdr_len; |
| 348 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 349 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 350 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 351 | dpo = sa0->dpo + IPSEC_PROTOCOL_ESP; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 352 | if (!is_tun) |
| 353 | { |
| 354 | next[0] = dpo->dpoi_next_node; |
| 355 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index; |
| 356 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 357 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 358 | else /* transport mode */ |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 359 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 360 | u8 *l2_hdr, l2_len, *ip_hdr, ip_len; |
| 361 | udp_header_t *udp = 0; |
| 362 | u8 *old_ip_hdr = vlib_buffer_get_current (b[0]); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 363 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 364 | ip_len = is_ip6 ? |
| 365 | esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr) : |
| 366 | ip4_header_bytes ((ip4_header_t *) old_ip_hdr); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 367 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 368 | vlib_buffer_advance (b[0], ip_len); |
| 369 | payload = vlib_buffer_get_current (b[0]); |
| 370 | next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz); |
| 371 | payload_len = b[0]->current_length; |
| 372 | |
| 373 | if (esp_trailer_icv_overflow (node, b[0], next, buffer_data_size)) |
| 374 | goto trace; |
| 375 | |
| 376 | /* ESP header */ |
| 377 | hdr_len += sizeof (*esp); |
| 378 | esp = (esp_header_t *) (payload - hdr_len); |
| 379 | |
| 380 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 381 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 382 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 383 | hdr_len += sizeof (udp_header_t); |
| 384 | udp = (udp_header_t *) (payload - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 387 | /* IP header */ |
| 388 | hdr_len += ip_len; |
| 389 | ip_hdr = payload - hdr_len; |
| 390 | |
| 391 | /* L2 header */ |
| 392 | l2_len = vnet_buffer (b[0])->ip.save_rewrite_length; |
| 393 | hdr_len += l2_len; |
| 394 | l2_hdr = payload - hdr_len; |
| 395 | |
| 396 | /* copy l2 and ip header */ |
| 397 | clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len); |
| 398 | clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len); |
| 399 | |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 400 | if (is_ip6) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 401 | { |
| 402 | ip6_header_t *ip6 = (ip6_header_t *) (ip_hdr); |
| 403 | *next_hdr_ptr = ip6->protocol; |
| 404 | ip6->protocol = IP_PROTOCOL_IPSEC_ESP; |
| 405 | ip6->payload_length = payload_len + hdr_len - l2_len - ip_len; |
| 406 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 407 | else |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 408 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 409 | u16 len; |
| 410 | ip4_header_t *ip4 = (ip4_header_t *) (ip_hdr); |
| 411 | *next_hdr_ptr = ip4->protocol; |
| 412 | len = payload_len + hdr_len + l2_len; |
| 413 | if (udp) |
| 414 | { |
| 415 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1); |
| 416 | esp_fill_udp_hdr (sa0, udp, len - ip_len); |
| 417 | } |
| 418 | else |
| 419 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 420 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 421 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 422 | next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 425 | esp->spi = spi; |
| 426 | esp->seq = clib_net_to_host_u32 (sa0->seq); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 427 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 428 | if (sa0->crypto_enc_op_type) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 429 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 430 | vnet_crypto_op_t *op; |
| 431 | vec_add2_aligned (ptd->crypto_ops, op, 1, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | a03d182 | 2019-03-28 21:40:48 +0100 | [diff] [blame^] | 432 | vnet_crypto_op_init (op, sa0->crypto_enc_op_type); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 433 | op->iv = payload - iv_sz; |
| 434 | op->src = op->dst = payload; |
| 435 | op->key = sa0->crypto_key.data; |
| 436 | op->len = payload_len - icv_sz; |
| 437 | op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV; |
| 438 | op->user_data = b - bufs; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 439 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 440 | |
| 441 | if (sa0->integ_op_type) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 442 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 443 | vnet_crypto_op_t *op; |
| 444 | vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | a03d182 | 2019-03-28 21:40:48 +0100 | [diff] [blame^] | 445 | vnet_crypto_op_init (op, sa0->integ_op_type); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 446 | op->src = payload - iv_sz - sizeof (esp_header_t); |
| 447 | op->dst = payload + payload_len - icv_sz; |
| 448 | op->key = sa0->integ_key.data; |
| 449 | op->key_len = sa0->integ_key.len; |
| 450 | op->hmac_trunc_len = icv_sz; |
| 451 | op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 452 | op->user_data = b - bufs; |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 453 | if (ipsec_sa_is_set_USE_ESN (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 454 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 455 | u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi); |
| 456 | clib_memcpy_fast (op->dst, &seq_hi, sizeof (seq_hi)); |
| 457 | op->len += sizeof (seq_hi); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 461 | vlib_buffer_advance (b[0], 0LL - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 462 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 463 | current_sa_packets += 1; |
| 464 | current_sa_bytes += payload_len; |
| 465 | |
| 466 | trace: |
| 467 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 468 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 469 | esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 470 | sizeof (*tr)); |
| 471 | tr->sa_index = sa_index0; |
| 472 | tr->spi = sa0->spi; |
| 473 | tr->seq = sa0->seq - 1; |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 474 | tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 475 | tr->crypto_alg = sa0->crypto_alg; |
| 476 | tr->integ_alg = sa0->integ_alg; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 477 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 478 | /* next */ |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 479 | n_left -= 1; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 480 | next += 1; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 481 | b += 1; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 484 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 485 | current_sa_index, current_sa_packets, |
| 486 | current_sa_bytes); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 487 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 488 | esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts); |
| 489 | esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts); |
| 490 | |
| 491 | vlib_node_increment_counter (vm, node->node_index, |
| 492 | ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors); |
| 493 | |
| 494 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 495 | return frame->n_vectors; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 498 | VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm, |
| 499 | vlib_node_runtime_t * node, |
| 500 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 501 | { |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 502 | return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 0); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 503 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 504 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 505 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 506 | VLIB_REGISTER_NODE (esp4_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 507 | .name = "esp4-encrypt", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 508 | .vector_size = sizeof (u32), |
| 509 | .format_trace = format_esp_encrypt_trace, |
| 510 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 511 | |
| 512 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 513 | .error_strings = esp_encrypt_error_strings, |
| 514 | |
| 515 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
| 516 | .next_nodes = { |
| 517 | #define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n, |
| 518 | foreach_esp_encrypt_next |
| 519 | #undef _ |
| 520 | }, |
| 521 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 522 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 524 | VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm, |
| 525 | vlib_node_runtime_t * node, |
| 526 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 527 | { |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 528 | return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 0); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* *INDENT-OFF* */ |
| 532 | VLIB_REGISTER_NODE (esp6_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 533 | .name = "esp6-encrypt", |
| 534 | .vector_size = sizeof (u32), |
| 535 | .format_trace = format_esp_encrypt_trace, |
| 536 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 537 | |
| 538 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 539 | .error_strings = esp_encrypt_error_strings, |
| 540 | |
| 541 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
| 542 | .next_nodes = { |
| 543 | #define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n, |
| 544 | foreach_esp_encrypt_next |
| 545 | #undef _ |
| 546 | }, |
| 547 | }; |
| 548 | /* *INDENT-ON* */ |
| 549 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 550 | VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm, |
| 551 | vlib_node_runtime_t * node, |
| 552 | vlib_frame_t * from_frame) |
| 553 | { |
| 554 | return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 1); |
| 555 | } |
| 556 | |
| 557 | /* *INDENT-OFF* */ |
| 558 | VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = { |
| 559 | .name = "esp4-encrypt-tun", |
| 560 | .vector_size = sizeof (u32), |
| 561 | .format_trace = format_esp_encrypt_trace, |
| 562 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 563 | |
| 564 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 565 | .error_strings = esp_encrypt_error_strings, |
| 566 | }; |
| 567 | |
| 568 | VNET_FEATURE_INIT (esp4_encrypt_tun_feat_node, static) = |
| 569 | { |
| 570 | .arc_name = "ip4-output", |
| 571 | .node_name = "esp4-encrypt-tun", |
| 572 | .runs_before = VNET_FEATURES ("ip4-frag", |
| 573 | "adj-midchain-tx"), |
| 574 | }; |
| 575 | /* *INDENT-ON* */ |
| 576 | |
| 577 | VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm, |
| 578 | vlib_node_runtime_t * node, |
| 579 | vlib_frame_t * from_frame) |
| 580 | { |
| 581 | return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 1); |
| 582 | } |
| 583 | |
| 584 | /* *INDENT-OFF* */ |
| 585 | VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = { |
| 586 | .name = "esp6-encrypt-tun", |
| 587 | .vector_size = sizeof (u32), |
| 588 | .format_trace = format_esp_encrypt_trace, |
| 589 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 590 | |
| 591 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 592 | .error_strings = esp_encrypt_error_strings, |
| 593 | }; |
| 594 | |
| 595 | VNET_FEATURE_INIT (esp6_encrypt_tun_feat_node, static) = |
| 596 | { |
| 597 | .arc_name = "ip6-output", |
| 598 | .node_name = "esp6-encrypt-tun", |
| 599 | .runs_before = VNET_FEATURES ("ip6-frag", |
| 600 | "adj-midchain-tx"), |
| 601 | }; |
| 602 | /* *INDENT-ON* */ |
| 603 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 604 | /* |
| 605 | * fd.io coding-style-patch-verification: ON |
| 606 | * |
| 607 | * Local Variables: |
| 608 | * eval: (c-set-style "gnu") |
| 609 | * End: |
| 610 | */ |