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") \ |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame^] | 30 | _(HANDOFF, "handoff") \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | _(INTERFACE_OUTPUT, "interface-output") |
| 32 | |
| 33 | #define _(v, s) ESP_ENCRYPT_NEXT_##v, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 34 | typedef enum |
| 35 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | foreach_esp_encrypt_next |
| 37 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 38 | ESP_ENCRYPT_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | } esp_encrypt_next_t; |
| 40 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 41 | #define foreach_esp_encrypt_error \ |
| 42 | _(RX_PKTS, "ESP pkts received") \ |
| 43 | _(SEQ_CYCLED, "sequence number cycled (packet dropped)") \ |
| 44 | _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \ |
| 45 | _(CHAINED_BUFFER, "chained buffers (packet dropped)") \ |
| 46 | _(NO_TRAILER_SPACE, "no trailer space (packet dropped)") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 48 | typedef enum |
| 49 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | #define _(sym,str) ESP_ENCRYPT_ERROR_##sym, |
| 51 | foreach_esp_encrypt_error |
| 52 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 53 | ESP_ENCRYPT_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | } esp_encrypt_error_t; |
| 55 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 56 | static char *esp_encrypt_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | #define _(sym,string) string, |
| 58 | foreach_esp_encrypt_error |
| 59 | #undef _ |
| 60 | }; |
| 61 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 62 | typedef struct |
| 63 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 64 | u32 sa_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | u32 spi; |
| 66 | u32 seq; |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 67 | u32 sa_seq_hi; |
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 | |
Guillaume Solignac | 8f818cc | 2019-05-15 12:02:33 +0200 | [diff] [blame] | 81 | s = |
| 82 | format (s, |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 83 | "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s", |
| 84 | t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi, |
| 85 | format_ipsec_crypto_alg, |
Guillaume Solignac | 8f818cc | 2019-05-15 12:02:33 +0200 | [diff] [blame] | 86 | t->crypto_alg, format_ipsec_integ_alg, t->integ_alg, |
| 87 | t->udp_encap ? " udp-encap-enabled" : ""); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | return s; |
| 89 | } |
| 90 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 91 | /* pad packet in input buffer */ |
| 92 | static_always_inline u8 * |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 93 | esp_add_footer_and_icv (vlib_buffer_t * b, u8 block_size, u8 icv_sz, |
| 94 | u16 * next, vlib_node_runtime_t * node, |
| 95 | u16 buffer_data_size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 97 | static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = { |
| 98 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 99 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x00, 0x00, |
| 100 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 102 | u16 min_length = b->current_length + sizeof (esp_footer_t); |
| 103 | u16 new_length = round_pow2 (min_length, block_size); |
| 104 | u8 pad_bytes = new_length - min_length; |
| 105 | esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (b) + |
| 106 | new_length - sizeof (esp_footer_t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 108 | if (b->current_data + new_length + icv_sz > buffer_data_size) |
| 109 | { |
| 110 | b->error = node->errors[ESP_ENCRYPT_ERROR_NO_TRAILER_SPACE]; |
| 111 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 112 | return 0; |
| 113 | } |
| 114 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 115 | if (pad_bytes) |
BenoƮt Ganne | 4505f01 | 2019-12-07 09:14:27 -0700 | [diff] [blame] | 116 | { |
| 117 | ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE); |
| 118 | pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes); |
| 119 | clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes); |
| 120 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 121 | |
| 122 | f->pad_length = pad_bytes; |
| 123 | b->current_length = new_length + icv_sz; |
| 124 | return &f->next_header; |
| 125 | } |
| 126 | |
| 127 | static_always_inline void |
| 128 | esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp) |
| 129 | { |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 130 | ip_csum_t sum; |
| 131 | u16 old_len; |
| 132 | |
| 133 | len = clib_net_to_host_u16 (len); |
| 134 | old_len = ip4->length; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 135 | |
| 136 | if (is_transport) |
| 137 | { |
| 138 | u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 139 | |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 140 | sum = ip_csum_update (ip4->checksum, ip4->protocol, |
| 141 | prot, ip4_header_t, protocol); |
| 142 | ip4->protocol = prot; |
| 143 | |
| 144 | sum = ip_csum_update (sum, old_len, len, ip4_header_t, length); |
| 145 | } |
| 146 | else |
| 147 | sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length); |
| 148 | |
| 149 | ip4->length = len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 150 | ip4->checksum = ip_csum_fold (sum); |
| 151 | } |
| 152 | |
| 153 | static_always_inline void |
| 154 | esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len) |
| 155 | { |
| 156 | clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t)); |
| 157 | udp->length = clib_net_to_host_u16 (len); |
| 158 | } |
| 159 | |
| 160 | static_always_inline u8 |
| 161 | ext_hdr_is_pre_esp (u8 nexthdr) |
| 162 | { |
| 163 | #ifdef CLIB_HAVE_VEC128 |
| 164 | static const u8x16 ext_hdr_types = { |
| 165 | IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS, |
| 166 | IP_PROTOCOL_IPV6_ROUTE, |
| 167 | IP_PROTOCOL_IPV6_FRAGMENTATION, |
| 168 | }; |
| 169 | |
| 170 | return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr)); |
| 171 | #else |
| 172 | return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) | |
| 173 | (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) | |
| 174 | (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0); |
| 175 | #endif |
| 176 | } |
| 177 | |
| 178 | static_always_inline u8 |
| 179 | esp_get_ip6_hdr_len (ip6_header_t * ip6) |
| 180 | { |
| 181 | /* this code assumes that HbH, route and frag headers will be before |
| 182 | others, if that is not the case, they will end up encrypted */ |
| 183 | |
| 184 | u8 len = sizeof (ip6_header_t); |
| 185 | ip6_ext_header_t *p; |
| 186 | |
| 187 | /* if next packet doesn't have ext header */ |
| 188 | if (ext_hdr_is_pre_esp (ip6->protocol) == 0) |
| 189 | return len; |
| 190 | |
| 191 | p = (void *) (ip6 + 1); |
| 192 | len += ip6_ext_header_len (p); |
| 193 | |
| 194 | while (ext_hdr_is_pre_esp (p->next_hdr)) |
| 195 | { |
| 196 | len += ip6_ext_header_len (p); |
| 197 | p = ip6_ext_next_header (p); |
| 198 | } |
| 199 | |
| 200 | return len; |
| 201 | } |
| 202 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 203 | static_always_inline void |
| 204 | esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 205 | vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts) |
| 206 | { |
| 207 | u32 n_fail, n_ops = vec_len (ops); |
| 208 | vnet_crypto_op_t *op = ops; |
| 209 | |
| 210 | if (n_ops == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | return; |
| 212 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 213 | n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 215 | while (n_fail) |
| 216 | { |
| 217 | ASSERT (op - ops < n_ops); |
| 218 | |
| 219 | if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED) |
| 220 | { |
| 221 | u32 bi = op->user_data; |
| 222 | b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR]; |
| 223 | nexts[bi] = ESP_ENCRYPT_NEXT_DROP; |
| 224 | n_fail--; |
| 225 | } |
| 226 | op++; |
| 227 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Damjan Marion | d97918e | 2019-04-25 18:28:31 +0200 | [diff] [blame] | 230 | typedef struct |
| 231 | { |
| 232 | u32 salt; |
| 233 | u64 iv; |
| 234 | } __clib_packed esp_gcm_nonce_t; |
| 235 | |
| 236 | STATIC_ASSERT_SIZEOF (esp_gcm_nonce_t, 12); |
| 237 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 238 | always_inline uword |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 239 | esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 240 | vlib_frame_t * frame, int is_ip6, int is_tun) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | ipsec_main_t *im = &ipsec_main; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 243 | ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index); |
| 244 | u32 *from = vlib_frame_vector_args (frame); |
| 245 | u32 n_left = frame->n_vectors; |
| 246 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 247 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
Damjan Marion | d97918e | 2019-04-25 18:28:31 +0200 | [diff] [blame] | 248 | esp_gcm_nonce_t nonces[VLIB_FRAME_SIZE], *nonce = nonces; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 249 | u32 thread_index = vm->thread_index; |
| 250 | u16 buffer_data_size = vlib_buffer_get_default_data_size (vm); |
| 251 | u32 current_sa_index = ~0, current_sa_packets = 0; |
| 252 | u32 current_sa_bytes = 0, spi = 0; |
| 253 | u8 block_sz = 0, iv_sz = 0, icv_sz = 0; |
| 254 | ipsec_sa_t *sa0 = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 256 | vlib_get_buffers (vm, from, b, n_left); |
| 257 | vec_reset_length (ptd->crypto_ops); |
| 258 | vec_reset_length (ptd->integ_ops); |
| 259 | |
| 260 | while (n_left > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 261 | { |
Zhiyong Yang | 1cff643 | 2019-04-30 05:33:53 -0400 | [diff] [blame] | 262 | u32 sa_index0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 263 | dpo_id_t *dpo; |
| 264 | esp_header_t *esp; |
| 265 | u8 *payload, *next_hdr_ptr; |
| 266 | u16 payload_len; |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame^] | 267 | u32 hdr_len, config_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 269 | if (n_left > 2) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 270 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 271 | u8 *p; |
| 272 | vlib_prefetch_buffer_header (b[2], LOAD); |
| 273 | p = vlib_buffer_get_current (b[1]); |
| 274 | CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD); |
| 275 | p -= CLIB_CACHE_LINE_BYTES; |
| 276 | CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 277 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 278 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 279 | if (is_tun) |
| 280 | { |
| 281 | /* we are on a ipsec tunnel's feature arc */ |
| 282 | u32 next0; |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame^] | 283 | config_index = b[0]->current_config_index; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 284 | sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0], |
| 285 | sizeof |
| 286 | (sa_index0)); |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame^] | 287 | vnet_buffer (b[0])->ipsec.sad_index = sa_index0; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 288 | next[0] = next0; |
| 289 | } |
| 290 | else |
| 291 | sa_index0 = vnet_buffer (b[0])->ipsec.sad_index; |
| 292 | |
| 293 | if (sa_index0 != current_sa_index) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 294 | { |
Damjan Marion | 21f265f | 2019-06-05 15:45:50 +0200 | [diff] [blame] | 295 | if (current_sa_packets) |
| 296 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 297 | current_sa_index, |
| 298 | current_sa_packets, |
| 299 | current_sa_bytes); |
| 300 | current_sa_packets = current_sa_bytes = 0; |
| 301 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 302 | sa0 = pool_elt_at_index (im->sad, sa_index0); |
| 303 | current_sa_index = sa_index0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 304 | spi = clib_net_to_host_u32 (sa0->spi); |
| 305 | block_sz = sa0->crypto_block_size; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 306 | icv_sz = sa0->integ_icv_size; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 307 | iv_sz = sa0->crypto_iv_size; |
| 308 | } |
| 309 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame^] | 310 | if (PREDICT_FALSE (~0 == sa0->encrypt_thread_index)) |
| 311 | { |
| 312 | /* this is the first packet to use this SA, claim the SA |
| 313 | * for this thread. this could happen simultaneously on |
| 314 | * another thread */ |
| 315 | clib_atomic_cmp_and_swap (&sa0->encrypt_thread_index, ~0, |
| 316 | ipsec_sa_assign_thread (thread_index)); |
| 317 | } |
| 318 | |
| 319 | if (PREDICT_TRUE (thread_index != sa0->encrypt_thread_index)) |
| 320 | { |
| 321 | next[0] = ESP_ENCRYPT_NEXT_HANDOFF; |
| 322 | if (is_tun) |
| 323 | { |
| 324 | b[0]->current_config_index = config_index; |
| 325 | } |
| 326 | goto trace; |
| 327 | } |
| 328 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 329 | if (vlib_buffer_chain_linearize (vm, b[0]) != 1) |
| 330 | { |
| 331 | b[0]->error = node->errors[ESP_ENCRYPT_ERROR_CHAINED_BUFFER]; |
| 332 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 333 | goto trace; |
| 334 | } |
| 335 | |
| 336 | if (PREDICT_FALSE (esp_seq_advance (sa0))) |
| 337 | { |
| 338 | b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED]; |
| 339 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 340 | goto trace; |
| 341 | } |
| 342 | |
| 343 | /* space for IV */ |
| 344 | hdr_len = iv_sz; |
| 345 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 346 | if (ipsec_sa_is_set_IS_TUNNEL (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 347 | { |
| 348 | payload = vlib_buffer_get_current (b[0]); |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 349 | next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz, |
| 350 | next, node, |
| 351 | buffer_data_size); |
| 352 | if (!next_hdr_ptr) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 353 | goto trace; |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 354 | payload_len = b[0]->current_length; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 355 | |
| 356 | /* ESP header */ |
| 357 | hdr_len += sizeof (*esp); |
| 358 | esp = (esp_header_t *) (payload - hdr_len); |
| 359 | |
| 360 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 361 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 362 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 363 | hdr_len += sizeof (udp_header_t); |
| 364 | esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len), |
| 365 | payload_len + hdr_len); |
| 366 | } |
| 367 | |
| 368 | /* IP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 369 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 370 | { |
| 371 | ip6_header_t *ip6; |
| 372 | u16 len = sizeof (ip6_header_t); |
| 373 | hdr_len += len; |
| 374 | ip6 = (ip6_header_t *) (payload - hdr_len); |
| 375 | clib_memcpy_fast (ip6, &sa0->ip6_hdr, len); |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 376 | *next_hdr_ptr = (is_ip6 ? |
| 377 | IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 378 | len = payload_len + hdr_len - len; |
| 379 | ip6->payload_length = clib_net_to_host_u16 (len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 380 | } |
| 381 | else |
| 382 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 383 | ip4_header_t *ip4; |
| 384 | u16 len = sizeof (ip4_header_t); |
| 385 | hdr_len += len; |
| 386 | ip4 = (ip4_header_t *) (payload - hdr_len); |
| 387 | clib_memcpy_fast (ip4, &sa0->ip4_hdr, len); |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 388 | *next_hdr_ptr = (is_ip6 ? |
| 389 | IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 390 | len = payload_len + hdr_len; |
| 391 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 392 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 393 | |
Neale Ranns | 72f2a3a | 2019-06-17 15:43:38 +0000 | [diff] [blame] | 394 | dpo = &sa0->dpo; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 395 | if (!is_tun) |
| 396 | { |
| 397 | next[0] = dpo->dpoi_next_node; |
| 398 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index; |
| 399 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 400 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 401 | else /* transport mode */ |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 402 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 403 | u8 *l2_hdr, l2_len, *ip_hdr, ip_len; |
| 404 | udp_header_t *udp = 0; |
| 405 | u8 *old_ip_hdr = vlib_buffer_get_current (b[0]); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 406 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 407 | ip_len = is_ip6 ? |
| 408 | esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr) : |
| 409 | ip4_header_bytes ((ip4_header_t *) old_ip_hdr); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 410 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 411 | vlib_buffer_advance (b[0], ip_len); |
| 412 | payload = vlib_buffer_get_current (b[0]); |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 413 | next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz, |
| 414 | next, node, |
| 415 | buffer_data_size); |
| 416 | if (!next_hdr_ptr) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 417 | goto trace; |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 418 | payload_len = b[0]->current_length; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 419 | |
| 420 | /* ESP header */ |
| 421 | hdr_len += sizeof (*esp); |
| 422 | esp = (esp_header_t *) (payload - hdr_len); |
| 423 | |
| 424 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 425 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 426 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 427 | hdr_len += sizeof (udp_header_t); |
| 428 | udp = (udp_header_t *) (payload - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 431 | /* IP header */ |
| 432 | hdr_len += ip_len; |
| 433 | ip_hdr = payload - hdr_len; |
| 434 | |
| 435 | /* L2 header */ |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 436 | if (!is_tun) |
| 437 | { |
| 438 | l2_len = vnet_buffer (b[0])->ip.save_rewrite_length; |
| 439 | hdr_len += l2_len; |
| 440 | l2_hdr = payload - hdr_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 441 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 442 | /* copy l2 and ip header */ |
| 443 | clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len); |
| 444 | } |
| 445 | else |
| 446 | l2_len = 0; |
| 447 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 448 | clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len); |
| 449 | |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 450 | if (is_ip6) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 451 | { |
| 452 | ip6_header_t *ip6 = (ip6_header_t *) (ip_hdr); |
| 453 | *next_hdr_ptr = ip6->protocol; |
| 454 | ip6->protocol = IP_PROTOCOL_IPSEC_ESP; |
Neale Ranns | d207fd7 | 2019-04-18 17:18:12 -0700 | [diff] [blame] | 455 | ip6->payload_length = |
| 456 | clib_host_to_net_u16 (payload_len + hdr_len - l2_len - |
| 457 | ip_len); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 458 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 459 | else |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 460 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 461 | u16 len; |
| 462 | ip4_header_t *ip4 = (ip4_header_t *) (ip_hdr); |
| 463 | *next_hdr_ptr = ip4->protocol; |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 464 | len = payload_len + hdr_len - l2_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 465 | if (udp) |
| 466 | { |
| 467 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1); |
| 468 | esp_fill_udp_hdr (sa0, udp, len - ip_len); |
| 469 | } |
| 470 | else |
| 471 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 472 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 473 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 474 | if (!is_tun) |
| 475 | next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 478 | esp->spi = spi; |
| 479 | esp->seq = clib_net_to_host_u32 (sa0->seq); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 480 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 481 | if (sa0->crypto_enc_op_id) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 482 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 483 | vnet_crypto_op_t *op; |
| 484 | vec_add2_aligned (ptd->crypto_ops, op, 1, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 485 | vnet_crypto_op_init (op, sa0->crypto_enc_op_id); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 486 | op->src = op->dst = payload; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 487 | op->key_index = sa0->crypto_key_index; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 488 | op->len = payload_len - icv_sz; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 489 | op->user_data = b - bufs; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 490 | |
| 491 | if (ipsec_sa_is_set_IS_AEAD (sa0)) |
| 492 | { |
| 493 | /* |
| 494 | * construct the AAD in a scratch space in front |
| 495 | * of the IP header. |
| 496 | */ |
| 497 | op->aad = payload - hdr_len - sizeof (esp_aead_t); |
| 498 | |
| 499 | esp_aad_fill (op, esp, sa0); |
| 500 | |
| 501 | op->tag = payload + op->len; |
| 502 | op->tag_len = 16; |
Damjan Marion | d97918e | 2019-04-25 18:28:31 +0200 | [diff] [blame] | 503 | |
| 504 | u64 *iv = (u64 *) (payload - iv_sz); |
| 505 | nonce->salt = sa0->salt; |
| 506 | nonce->iv = *iv = clib_host_to_net_u64 (sa0->gcm_iv_counter++); |
| 507 | op->iv = (u8 *) nonce; |
| 508 | nonce++; |
| 509 | } |
| 510 | else |
| 511 | { |
| 512 | op->iv = payload - iv_sz; |
| 513 | op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 514 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 515 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 516 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 517 | if (sa0->integ_op_id) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 518 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 519 | vnet_crypto_op_t *op; |
| 520 | vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 521 | vnet_crypto_op_init (op, sa0->integ_op_id); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 522 | op->src = payload - iv_sz - sizeof (esp_header_t); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 523 | op->digest = payload + payload_len - icv_sz; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 524 | op->key_index = sa0->integ_key_index; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 525 | op->digest_len = icv_sz; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 526 | op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 527 | op->user_data = b - bufs; |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 528 | if (ipsec_sa_is_set_USE_ESN (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 529 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 530 | u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi); |
Neale Ranns | 49e7ef6 | 2019-04-10 17:24:29 +0000 | [diff] [blame] | 531 | clib_memcpy_fast (op->digest, &seq_hi, sizeof (seq_hi)); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 532 | op->len += sizeof (seq_hi); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 536 | vlib_buffer_advance (b[0], 0LL - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 537 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 538 | current_sa_packets += 1; |
| 539 | current_sa_bytes += payload_len; |
| 540 | |
| 541 | trace: |
| 542 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 543 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 544 | esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 545 | sizeof (*tr)); |
| 546 | tr->sa_index = sa_index0; |
| 547 | tr->spi = sa0->spi; |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 548 | tr->seq = sa0->seq; |
| 549 | tr->sa_seq_hi = sa0->seq_hi; |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 550 | tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 551 | tr->crypto_alg = sa0->crypto_alg; |
| 552 | tr->integ_alg = sa0->integ_alg; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 553 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 554 | /* next */ |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 555 | n_left -= 1; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 556 | next += 1; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 557 | b += 1; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 558 | } |
| 559 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 560 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 561 | current_sa_index, current_sa_packets, |
| 562 | current_sa_bytes); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 563 | esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts); |
| 564 | esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts); |
| 565 | |
| 566 | vlib_node_increment_counter (vm, node->node_index, |
| 567 | ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors); |
| 568 | |
| 569 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 570 | return frame->n_vectors; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 573 | VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm, |
| 574 | vlib_node_runtime_t * node, |
| 575 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 576 | { |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 577 | return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 0); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 578 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 579 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 580 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 581 | VLIB_REGISTER_NODE (esp4_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 582 | .name = "esp4-encrypt", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 583 | .vector_size = sizeof (u32), |
| 584 | .format_trace = format_esp_encrypt_trace, |
| 585 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 586 | |
| 587 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 588 | .error_strings = esp_encrypt_error_strings, |
| 589 | |
| 590 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
| 591 | .next_nodes = { |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame^] | 592 | [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop", |
| 593 | [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-handoff", |
| 594 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 595 | }, |
| 596 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 597 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 598 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 599 | VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm, |
| 600 | vlib_node_runtime_t * node, |
| 601 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 602 | { |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 603 | return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 0); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* *INDENT-OFF* */ |
| 607 | VLIB_REGISTER_NODE (esp6_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 608 | .name = "esp6-encrypt", |
| 609 | .vector_size = sizeof (u32), |
| 610 | .format_trace = format_esp_encrypt_trace, |
| 611 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 612 | |
| 613 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 614 | .error_strings = esp_encrypt_error_strings, |
| 615 | |
| 616 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
| 617 | .next_nodes = { |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame^] | 618 | [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop", |
| 619 | [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-handoff", |
| 620 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output", |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 621 | }, |
| 622 | }; |
| 623 | /* *INDENT-ON* */ |
| 624 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 625 | VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm, |
| 626 | vlib_node_runtime_t * node, |
| 627 | vlib_frame_t * from_frame) |
| 628 | { |
| 629 | return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 1); |
| 630 | } |
| 631 | |
| 632 | /* *INDENT-OFF* */ |
| 633 | VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = { |
| 634 | .name = "esp4-encrypt-tun", |
| 635 | .vector_size = sizeof (u32), |
| 636 | .format_trace = format_esp_encrypt_trace, |
| 637 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 638 | |
| 639 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 640 | .error_strings = esp_encrypt_error_strings, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 641 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame^] | 642 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 643 | .next_nodes = { |
| 644 | [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop", |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame^] | 645 | [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-handoff", |
| 646 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop", |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 647 | }, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 648 | }; |
| 649 | |
| 650 | VNET_FEATURE_INIT (esp4_encrypt_tun_feat_node, static) = |
| 651 | { |
| 652 | .arc_name = "ip4-output", |
| 653 | .node_name = "esp4-encrypt-tun", |
Neale Ranns | ea5bb77 | 2019-04-02 05:43:34 -0700 | [diff] [blame] | 654 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 655 | }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 656 | |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 657 | VNET_FEATURE_INIT (esp6o4_encrypt_tun_feat_node, static) = |
| 658 | { |
| 659 | .arc_name = "ip6-output", |
| 660 | .node_name = "esp4-encrypt-tun", |
| 661 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 662 | }; |
| 663 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 664 | VNET_FEATURE_INIT (esp4_ethernet_encrypt_tun_feat_node, static) = |
| 665 | { |
| 666 | .arc_name = "ethernet-output", |
| 667 | .node_name = "esp4-encrypt-tun", |
| 668 | .runs_before = VNET_FEATURES ("adj-midchain-tx", "adj-midchain-tx-no-count"), |
| 669 | }; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 670 | /* *INDENT-ON* */ |
| 671 | |
| 672 | VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm, |
| 673 | vlib_node_runtime_t * node, |
| 674 | vlib_frame_t * from_frame) |
| 675 | { |
| 676 | return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 1); |
| 677 | } |
| 678 | |
| 679 | /* *INDENT-OFF* */ |
| 680 | VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = { |
| 681 | .name = "esp6-encrypt-tun", |
| 682 | .vector_size = sizeof (u32), |
| 683 | .format_trace = format_esp_encrypt_trace, |
| 684 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 685 | |
| 686 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 687 | .error_strings = esp_encrypt_error_strings, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 688 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame^] | 689 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 690 | .next_nodes = { |
| 691 | [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop", |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame^] | 692 | [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-handoff", |
| 693 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop", |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 694 | }, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 695 | }; |
| 696 | |
| 697 | VNET_FEATURE_INIT (esp6_encrypt_tun_feat_node, static) = |
| 698 | { |
| 699 | .arc_name = "ip6-output", |
| 700 | .node_name = "esp6-encrypt-tun", |
Neale Ranns | ea5bb77 | 2019-04-02 05:43:34 -0700 | [diff] [blame] | 701 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 702 | }; |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 703 | |
| 704 | VNET_FEATURE_INIT (esp4o6_encrypt_tun_feat_node, static) = |
| 705 | { |
| 706 | .arc_name = "ip4-output", |
| 707 | .node_name = "esp6-encrypt-tun", |
| 708 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 709 | }; |
| 710 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 711 | /* *INDENT-ON* */ |
| 712 | |
Matthew Smith | 401aedf | 2019-07-08 14:45:04 -0500 | [diff] [blame] | 713 | typedef struct |
| 714 | { |
| 715 | u32 sa_index; |
| 716 | } esp_no_crypto_trace_t; |
| 717 | |
| 718 | static u8 * |
| 719 | format_esp_no_crypto_trace (u8 * s, va_list * args) |
| 720 | { |
| 721 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 722 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 723 | esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *); |
| 724 | |
| 725 | s = format (s, "esp-no-crypto: sa-index %u", t->sa_index); |
| 726 | |
| 727 | return s; |
| 728 | } |
| 729 | |
| 730 | enum |
| 731 | { |
| 732 | ESP_NO_CRYPTO_NEXT_DROP, |
| 733 | ESP_NO_CRYPTO_N_NEXT, |
| 734 | }; |
| 735 | |
| 736 | enum |
| 737 | { |
| 738 | ESP_NO_CRYPTO_ERROR_RX_PKTS, |
| 739 | }; |
| 740 | |
| 741 | static char *esp_no_crypto_error_strings[] = { |
| 742 | "Outbound ESP packets received", |
| 743 | }; |
| 744 | |
| 745 | always_inline uword |
| 746 | esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 747 | vlib_frame_t * frame) |
| 748 | { |
| 749 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 750 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
| 751 | u32 *from = vlib_frame_vector_args (frame); |
| 752 | u32 n_left = frame->n_vectors; |
| 753 | |
| 754 | vlib_get_buffers (vm, from, b, n_left); |
| 755 | |
| 756 | while (n_left > 0) |
| 757 | { |
| 758 | u32 next0; |
| 759 | u32 sa_index0; |
| 760 | |
| 761 | /* packets are always going to be dropped, but get the sa_index */ |
| 762 | sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0], |
| 763 | sizeof (sa_index0)); |
| 764 | |
| 765 | next[0] = ESP_NO_CRYPTO_NEXT_DROP; |
| 766 | |
| 767 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 768 | { |
| 769 | esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 770 | sizeof (*tr)); |
| 771 | tr->sa_index = sa_index0; |
| 772 | } |
| 773 | |
| 774 | n_left -= 1; |
| 775 | next += 1; |
| 776 | b += 1; |
| 777 | } |
| 778 | |
| 779 | vlib_node_increment_counter (vm, node->node_index, |
| 780 | ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors); |
| 781 | |
| 782 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 783 | |
| 784 | return frame->n_vectors; |
| 785 | } |
| 786 | |
| 787 | VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm, |
| 788 | vlib_node_runtime_t * node, |
| 789 | vlib_frame_t * from_frame) |
| 790 | { |
| 791 | return esp_no_crypto_inline (vm, node, from_frame); |
| 792 | } |
| 793 | |
| 794 | /* *INDENT-OFF* */ |
| 795 | VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) = |
| 796 | { |
| 797 | .name = "esp4-no-crypto", |
| 798 | .vector_size = sizeof (u32), |
| 799 | .format_trace = format_esp_no_crypto_trace, |
| 800 | .n_errors = ARRAY_LEN(esp_no_crypto_error_strings), |
| 801 | .error_strings = esp_no_crypto_error_strings, |
| 802 | .n_next_nodes = ESP_NO_CRYPTO_N_NEXT, |
| 803 | .next_nodes = { |
| 804 | [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop", |
| 805 | }, |
| 806 | }; |
| 807 | |
| 808 | VNET_FEATURE_INIT (esp4_no_crypto_tun_feat_node, static) = |
| 809 | { |
| 810 | .arc_name = "ip4-output", |
| 811 | .node_name = "esp4-no-crypto", |
| 812 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 813 | }; |
| 814 | |
| 815 | VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm, |
| 816 | vlib_node_runtime_t * node, |
| 817 | vlib_frame_t * from_frame) |
| 818 | { |
| 819 | return esp_no_crypto_inline (vm, node, from_frame); |
| 820 | } |
| 821 | |
| 822 | /* *INDENT-OFF* */ |
| 823 | VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) = |
| 824 | { |
| 825 | .name = "esp6-no-crypto", |
| 826 | .vector_size = sizeof (u32), |
| 827 | .format_trace = format_esp_no_crypto_trace, |
| 828 | .n_errors = ARRAY_LEN(esp_no_crypto_error_strings), |
| 829 | .error_strings = esp_no_crypto_error_strings, |
| 830 | .n_next_nodes = ESP_NO_CRYPTO_N_NEXT, |
| 831 | .next_nodes = { |
| 832 | [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop", |
| 833 | }, |
| 834 | }; |
| 835 | |
| 836 | VNET_FEATURE_INIT (esp6_no_crypto_tun_feat_node, static) = |
| 837 | { |
| 838 | .arc_name = "ip6-output", |
| 839 | .node_name = "esp6-no-crypto", |
| 840 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 841 | }; |
| 842 | /* *INDENT-ON* */ |
| 843 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 844 | /* |
| 845 | * fd.io coding-style-patch-verification: ON |
| 846 | * |
| 847 | * Local Variables: |
| 848 | * eval: (c-set-style "gnu") |
| 849 | * End: |
| 850 | */ |